WindowsIconReader.st
changeset 1745 4fa0fad2a463
parent 1734 290f382d40e9
child 1754 cc844dc3504e
--- a/WindowsIconReader.st	Thu Apr 10 13:21:46 2003 +0200
+++ b/WindowsIconReader.st	Thu Apr 10 16:26:11 2003 +0200
@@ -16,7 +16,7 @@
 	instanceVariableNames:''
 	classVariableNames:''
 	poolDictionaries:''
-	category:'Graphics-Images-Support'
+	category:'Graphics-Images-Readers'
 !
 
 !WindowsIconReader class methodsFor:'documentation'!
@@ -376,9 +376,7 @@
 fromOS2Stream:aStream
     "read an image from an OS/2 BMP stream"
 
-    | header inDepth
-      rawMap rMap gMap bMap srcIndex dstIndex inBytesPerRow
-      data4 mask tmp bytesPerRow nColors nByte|
+    |header inDepth inBytesPerRow mask bytesPerRow nColors nByte|
 
     inStream := aStream.
     aStream binary.
@@ -405,32 +403,18 @@
         ]
     ].
 
-    "read the colormap"
+    "read the colormap; notice: its in BGR order (sigh)"
 
     nColors := 1 bitShift:inDepth.
+    colorMap := self readColorMap:nColors numBytesPerColor:3 from:aStream.
 
-    rawMap := ByteArray uninitializedNew:(nColors*3).
-    aStream nextBytes:(nColors*3) into:rawMap.
-    rMap := Array new:nColors.
-    gMap := Array new:nColors.
-    bMap := Array new:nColors.
-    srcIndex := 1.
-    1 to:nColors do:[:i |
-        bMap at:i put:(rawMap at:srcIndex).
-        srcIndex := srcIndex + 1.
-        gMap at:i put:(rawMap at:srcIndex).
-        srcIndex := srcIndex + 1.
-        rMap at:i put:(rawMap at:srcIndex).
-        srcIndex := srcIndex + 1.
-    ].
-
-    "read mask"
+    "read the mask"
 
     nByte := width * height + 7 // 8.
     mask := ByteArray uninitializedNew:nByte.
     aStream nextBytes:nByte into:mask.
 
-    "what is this"
+    "what is this ?"
 
     aStream nextBytes:nByte into:mask.
 
@@ -473,7 +457,6 @@
     photometric := #palette.
     samplesPerPixel := 1.
     bitsPerSample := #(8).
-    colorMap := Colormap redVector:rMap greenVector:gMap blueVector:bMap.
 
     "
      |i f|
@@ -578,8 +561,7 @@
     | fileSize header iSize inDepth inPlanes compression
       imgSize resH resV numColor numImportantColor
       dataStart t
-      rawMap rMap gMap bMap srcIndex
-      bytesPerRow fourBytesPerColorInfo|
+      bytesPerRow numBytesPerColorInColormap|
 
     inStream := aStream.
     aStream binary.
@@ -596,7 +578,7 @@
          its an Windows3.x BMP file
          or OS/2 vsn 2 BMP file
         "
-"/        'WinIconReader [info]: Win3.x or OS/2 vsn 2 format' infoPrintNL.
+        "/ 'WinIconReader [info]: Win3.x or OS/2 vsn 2 format' infoPrintCR.
 
         aStream nextBytes:(40-4) into:header startingAt:19.
 
@@ -617,22 +599,18 @@
             "
             inDepth <= 8 ifTrue:[
                 numColor := 1 bitShift:inDepth.
-                "/ 'WinIconReader [warning]: missing nColor in header - assume ' infoPrint. numColor infoPrintNL
+                "/ 'WinIconReader [warning]: missing nColor in header - assume ' infoPrint. numColor infoPrintCR
             ]
         ].
 
-        numColor ~~ 0 ifTrue:[
-            rawMap := ByteArray uninitializedNew:(numColor * 4).
-            aStream nextBytes:(numColor * 4) into:rawMap.
-            fourBytesPerColorInfo := true.
-        ].
+        numBytesPerColorInColormap := 4.
         dataStart := header wordAt:(16r0A + 1) MSB:false
     ] ifFalse:[
         (iSize == 12) ifTrue:[     "core-info header size"
             "
              its an OS/2 (vsn1.2) BMP file
             "
-"/           'WinIconReader [info]: OS/2 vsn 1.2 format' infoPrintNL.
+            "/ 'WinIconReader [info]: OS/2 vsn 1.2 format' infoPrintCR.
             aStream nextBytes:(12-4) into:header startingAt:19.
 
             width := header wordAt:(16r12 + 1) MSB:false.  "(header at:19) + ((header at:20) * 256).   "
@@ -640,9 +618,8 @@
             inPlanes := header wordAt:(16r16 + 1) MSB:false.
             inDepth := header wordAt:(16r18 + 1) MSB:false.
             numColor := 1 bitShift:inDepth.
-            rawMap := ByteArray uninitializedNew:(numColor * 3).
-            aStream nextBytes:(numColor * 3) into:rawMap.
-            fourBytesPerColorInfo := false.
+
+            numBytesPerColorInColormap := 3.
             compression := 0.
             "/ dataStart := header wordAt:(16r0A + 1) MSB:false.
             dataStart := nil.
@@ -652,29 +629,17 @@
     ].
 
     numColor ~~ 0 ifTrue:[
-        "read the colormap"
+        "read the colormap - notice: its in BGR order (sigh)"
 
-        rMap := ByteArray new:numColor.
-        gMap := ByteArray new:numColor.
-        bMap := ByteArray new:numColor.
-        srcIndex := 1.
-        1 to:numColor do:[:i |
-            bMap at:i put:(rawMap at:srcIndex).
-            srcIndex := srcIndex + 1.
-            gMap at:i put:(rawMap at:srcIndex).
-            srcIndex := srcIndex + 1.
-            rMap at:i put:(rawMap at:srcIndex).
-            srcIndex := srcIndex + 1.
-            fourBytesPerColorInfo ifTrue:[
-                srcIndex := srcIndex + 1.
-            ]
-        ].
+        colorMap := self 
+                        readColorMap:numColor 
+                        numBytesPerColor:numBytesPerColorInColormap
+                        from:aStream.
+
         numColor > (1 bitShift:inDepth) ifTrue:[
             'funny number of colors in image' infoPrintCR.
             numColor := 1 bitShift:inDepth.
-            rMap := rMap copyTo:numColor.
-            gMap := gMap copyTo:numColor.
-            bMap := bMap copyTo:numColor.
+            colorMap := colorMap copyTo:numColor.
         ].
     ].
 
@@ -743,7 +708,6 @@
     photometric := #palette.
     samplesPerPixel := 1.
     bitsPerSample := #(8).
-    colorMap := Colormap redVector:rMap greenVector:gMap blueVector:bMap.
 
     "Modified: / 17.9.1995 / 18:48:46 / claus"
     "Modified: / 16.9.1998 / 01:09:08 / cg"
@@ -771,9 +735,9 @@
 fromWindowsICOStream:aStream
     "read an image from a windows ICO stream"
 
-    | header inDepth
-      rawMap rMap gMap bMap srcIndex dstIndex
-      rawData mask tmp bytesPerRow nColor cmapSize|
+    |header inDepth
+     srcIndex dstIndex
+     rawData tmp bytesPerRow nColor cmapSize|
 
     inStream := aStream.
     aStream binary.
@@ -806,21 +770,11 @@
 
     "read the colormap"
     cmapSize := (1 bitShift:inDepth).
-    rawMap := ByteArray uninitializedNew:(cmapSize*4).
-    aStream nextBytes:(cmapSize*4) into:rawMap.
-    rMap := ByteArray new:cmapSize.
-    gMap := ByteArray new:cmapSize.
-    bMap := ByteArray new:cmapSize.
-    srcIndex := 1.
-    1 to:cmapSize do:[:i |
-        bMap at:i put:(rawMap at:srcIndex).
-        srcIndex := srcIndex + 1.
-        gMap at:i put:(rawMap at:srcIndex).
-        srcIndex := srcIndex + 1.
-        rMap at:i put:(rawMap at:srcIndex).
-        srcIndex := srcIndex + 1.
-        srcIndex := srcIndex + 1.
-    ].
+
+    colorMap := self
+                readColorMap:cmapSize
+                numBytesPerColor:4
+                from:aStream.
 
     "read the data bits"
 
@@ -848,12 +802,9 @@
     ].
     rawData := tmp.
 
-
     photometric := #palette.
     samplesPerPixel := 1.
 
-    colorMap := Colormap redVector:rMap greenVector:gMap blueVector:bMap.
-
     false ifTrue:[
         "expand into bytes"
 
@@ -871,6 +822,38 @@
     "
 
     "Modified: / 18.5.1999 / 15:40:00 / cg"
+!
+
+readColorMap:nColors numBytesPerColor:nRawBytesPerColor from:aStream
+    "read the colormap; notice: its in BGR order (sigh)."
+
+    |rawMap rMap gMap bMap 
+     srcIndex  "{ Class: SmallInteger }"
+     skipDelta "{ Class: SmallInteger }"|
+
+    rawMap := ByteArray uninitializedNew:(nColors*nRawBytesPerColor).
+    aStream nextBytes:(nColors*nRawBytesPerColor) into:rawMap.
+
+    rMap := ByteArray new:nColors.
+    gMap := ByteArray new:nColors.
+    bMap := ByteArray new:nColors.
+    srcIndex := 1.
+    skipDelta := nRawBytesPerColor - 3.
+
+    1 to:nColors do:[:i |
+        bMap at:i put:(rawMap at:srcIndex).
+        srcIndex := srcIndex + 1.
+        gMap at:i put:(rawMap at:srcIndex).
+        srcIndex := srcIndex + 1.
+        rMap at:i put:(rawMap at:srcIndex).
+        srcIndex := srcIndex + 1.
+        srcIndex := srcIndex + skipDelta.
+    ].
+
+    ^ Colormap 
+        redVector:rMap 
+        greenVector:gMap 
+        blueVector:bMap.
 ! !
 
 !WindowsIconReader methodsFor:'writing to file'!
@@ -1119,7 +1102,7 @@
 !WindowsIconReader class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview2/WindowsIconReader.st,v 1.51 2003-04-01 08:45:54 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview2/WindowsIconReader.st,v 1.52 2003-04-10 14:25:15 cg Exp $'
 ! !
 
 WindowsIconReader initialize!