class: MacOSXIconReader
authorClaus Gittinger <cg@exept.de>
Mon, 10 Nov 2014 23:21:31 +0100
changeset 3381 5166ef7c73b3
parent 3380 e7a59408d31a
child 3382 cca44f2caa2d
class: MacOSXIconReader added: #readPackBitsImageFrom:offset:width:height:depth: #read_h8mk_from: #read_ih32_from: changed:8 methods category of:
MacOSXIconReader.st
--- a/MacOSXIconReader.st	Sun Nov 09 23:17:21 2014 +0100
+++ b/MacOSXIconReader.st	Mon Nov 10 23:21:31 2014 +0100
@@ -136,29 +136,31 @@
         sizeRemaining := sizeRemaining - 4 - numChunkBytes.
 
         img := self readSingleIcon:chunkType from:chunkData.
-        imageCount == 0 ifTrue:[
-            firstImage := image := img.
-        ] ifFalse:[
-            imageCount == 1 ifTrue:[
-                imageSequence := ImageSequence new.
-                img imageSequence:imageSequence.
+        img notNil ifTrue:[
+            imageCount == 0 ifTrue:[
+                firstImage := image := img.
+            ] ifFalse:[
+                imageCount == 1 ifTrue:[
+                    imageSequence := ImageSequence new.
+                    img imageSequence:imageSequence.
 
-                "/ add frame for first image.
-                frame := ImageFrame new image:firstImage.
+                    "/ add frame for first image.
+                    frame := ImageFrame new image:firstImage.
+                    imageSequence add:frame.
+                ].  
+                img notNil ifTrue:[
+                    "/ unsupported images are skipped...
+                    img imageSequence:imageSequence.
+                ].
+
+                "/ add frame for this image.
+                frame := ImageFrame new image:img.
                 imageSequence add:frame.
-            ].  
-            img notNil ifTrue:[
-                "/ unsupported images are skipped...
-                img imageSequence:imageSequence.
             ].
-
-            "/ add frame for this image.
-            frame := ImageFrame new image:img.
-            imageSequence add:frame.
+            imageCount := imageCount + 1.
         ].
-        imageCount := imageCount + 1.
     ].
-
+self halt.
     "
      Image fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
     "
@@ -577,6 +579,82 @@
     ^ img
 !
 
+readPackBitsImageFrom:compressedData offset:offset width:w height:h depth:depth
+    |redData greenData blueData bytesPerRow bytesPerChannel n srcStart rowStart|
+
+    depth == 24 ifTrue:[
+        "/ rgb channels separate
+        bytesPerRow := w.
+        bytesPerChannel := bytesPerRow * h.
+
+        redData := ByteArray new:bytesPerChannel.
+        greenData := ByteArray new:bytesPerChannel.
+        blueData := ByteArray new:bytesPerChannel.
+        srcStart := 1+offset.
+        n := self class
+            decompressPackBitsFrom:compressedData at:srcStart to:redData at:1 count:bytesPerChannel.
+        srcStart := srcStart + n.
+        n := self class
+            decompressPackBitsFrom:compressedData at:srcStart to:greenData at:1 count:bytesPerChannel.
+        srcStart := srcStart + n.
+        n := self class
+            decompressPackBitsFrom:compressedData at:srcStart to:blueData at:1 count:bytesPerChannel.
+        photometric := #rgb.
+        bitsPerSample := #(8 8 8).
+        samplesPerPixel := 3.
+        width := w.
+        height := h.
+        data := ByteArray new:(self bytesPerRow * h).
+
+        rowStart := 1.
+        1 to:height do:[:r |
+            |ci|
+
+            ci := rowStart.
+            1 to:width do:[:c |
+                data at:ci put:(redData at:c).
+                data at:ci+1 put:(greenData at:c).
+                data at:ci+2 put:(blueData at:c).
+                ci := ci + 3.
+            ].
+            rowStart := rowStart + self bytesPerRow.
+        ].
+        ^ Depth24Image new
+            width:width
+            height:height
+            photometric:photometric
+            samplesPerPixel:samplesPerPixel
+            bitsPerSample:bitsPerSample
+            colorMap:nil
+            bits:data
+            mask:nil.
+    ].
+    depth == 8 ifTrue:[
+        "/ 8bit single channel
+        bytesPerRow := w.
+        bytesPerChannel := bytesPerRow * h.
+
+        compressedData size == bytesPerChannel ifTrue:[
+            data := compressedData.
+        ] ifFalse:[
+            data := ByteArray new:bytesPerChannel.
+            n := self class
+                decompressPackBitsFrom:compressedData at:1+offset to:data at:1 count:bytesPerChannel.
+        ].
+        ^ Depth8Image new
+            width:width
+            height:height
+            photometric:#blackIs0
+            samplesPerPixel:1
+            bitsPerSample:#(8)
+            colorMap:nil
+            bits:data
+            mask:nil.
+    ].
+self halt.
+    ^ nil
+!
+
 read_ICN__from:bytes
     "read an ICN# format icon"
 
@@ -604,6 +682,12 @@
     "
 !
 
+read_h8mk_from:bytes
+    "read an h8mk packbits format mask icon"
+
+    ^ self readPackBitsImageFrom:bytes asByteArray offset:0 width:48 height:48 depth:8.
+!
+
 read_ic07_from:bytes
     "read an ic07 (PNG or JPEG, 128) format icon"
 
@@ -731,15 +815,16 @@
     "
 !
 
-read_il32_from:bytes
-    "read an il32 format icon"
+read_ih32_from:bytes
+    "read an ih32 packbits format icon"
 
-self halt:'unimplemented'.
-    ^ nil
+    ^ self readPackBitsImageFrom:bytes asByteArray offset:0  width:48 height:48 depth:24.
+!
 
-    "
-     self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
-    "
+read_il32_from:bytes
+    "read an il32 packbits format icon"
+
+    ^ self readPackBitsImageFrom:bytes asByteArray offset:0  width:32 height:32 depth:24.
 !
 
 read_ipc4_from:bytes
@@ -765,21 +850,15 @@
 !
 
 read_is32_from:bytes
-    "read an is32 format icon"
+    "read an is32 packbits format icon"
 
-self halt:'unimplemented'.
-    ^ nil
-
-    "
-     self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
-    "
+    ^ self readPackBitsImageFrom:bytes asByteArray offset:0 width:16 height:16 depth:24.
 !
 
 read_it32_from:bytes
-    "read an it32 format icon"
+    "read an it32 packbits format icon"
 
-self halt:'unimplemented'.
-    ^ nil
+    ^ self readPackBitsImageFrom:bytes asByteArray offset:4 width:64 height:64 depth:24.
 
     "
      self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
@@ -787,46 +866,31 @@
 !
 
 read_l8mk_from:bytes
-    "read an l8mk format icon"
+    "read an l8mk packbits format mask icon"
 
-self halt:'unimplemented'.
-    ^ nil
-
-    "
-     self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
-    "
+    ^ self readPackBitsImageFrom:bytes asByteArray offset:0  width:32 height:32 depth:8.
 !
 
 read_s8mk_from:bytes
-    "read an s8mk format icon"
+    "read an s8mk packbits format mask icon"
 
-self halt:'unimplemented'.
-    ^ nil
-
-    "
-     self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
-    "
+    ^ self readPackBitsImageFrom:bytes asByteArray offset:0  width:16 height:16 depth:8.
 !
 
 read_t8mk_from:bytes
-    "read an t8mk format icon"
+    "read an t8mk packbits format mask icon"
 
-self halt:'unimplemented'.
-    ^ nil
-
-    "
-     self fromFile:'/Applications/TextEdit.app/Contents/Resources/txt.icns'
-    "
+    ^ self readPackBitsImageFrom:bytes asByteArray offset:0 width:64 height:64 depth:8.
 ! !
 
 !MacOSXIconReader class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview2/MacOSXIconReader.st,v 1.10 2014-07-24 17:51:17 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview2/MacOSXIconReader.st,v 1.11 2014-11-10 22:21:31 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libview2/MacOSXIconReader.st,v 1.10 2014-07-24 17:51:17 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview2/MacOSXIconReader.st,v 1.11 2014-11-10 22:21:31 cg Exp $'
 ! !