class: ImageEditor
authorClaus Gittinger <cg@exept.de>
Fri, 24 Jul 2015 13:13:25 +0200
changeset 3224 98a659b6bd10
parent 3223 38d493fcc782
child 3225 cf0556d88d12
class: ImageEditor changed: #sortColorMapWith:
ImageEditor.st
--- a/ImageEditor.st	Tue Jul 07 12:42:46 2015 +0200
+++ b/ImageEditor.st	Fri Jul 24 13:13:25 2015 +0200
@@ -6184,14 +6184,16 @@
 sortColorMapWith:sortBlock
     "calculates a new color map for the image, sorting colors"
 
-    |depth newColorMap newImage oldImage usedColors oldToNew oldBits newBits tmpBits| 
+    |depth newColorMap newImage oldImage usedColors oldToNew oldBits newBits tmpBits
+     expectedSize w h| 
 
     oldImage := self image.
     depth := oldImage depth.
+    w := oldImage width.
+    h := oldImage height.
 
     usedColors := oldImage realColorMap.
 
-
     "/ translation table
     oldToNew := ByteArray new:(1 bitShift:depth).
     newColorMap := usedColors asArray.
@@ -6207,35 +6209,37 @@
     ].
 
     oldBits := oldImage bits.
+    "/ sanity check...
+    expectedSize := ((w * h * depth + 7) // 8).
+    (oldBits size < expectedSize) ifTrue:[
+        self halt:'incorrect pixeldata size'.
+        oldBits := (ByteArray new:expectedSize) replaceFrom:1 with:oldBits; yourself.
+    ].
     newBits := ByteArray new:(oldBits size).
     depth ~~ 8 ifTrue:[
+
         "/ expand/compress can only handle 8bits
-        tmpBits := ByteArray uninitializedNew:(oldImage width*oldImage height).
+        tmpBits := ByteArray uninitializedNew:(w*h).
         oldBits
             expandPixels:depth
-            width:oldImage width
-            height:oldImage height 
+            width:w height:h 
             into:tmpBits
             mapping:oldToNew.
         tmpBits
             compressPixels:depth 
-            width:oldImage width 
-            height:oldImage height 
+            width:w height:h 
             into:newBits 
             mapping:nil
     ] ifFalse:[
         oldBits
             expandPixels:depth
-            width:oldImage width
-            height:oldImage height 
+            width:w height:h 
             into:newBits
             mapping:oldToNew.
     ].
 
     newImage := oldImage species new
-                    width:oldImage width
-                    height:oldImage height
-                    depth:depth
+                    width:w height:h depth:depth
                     fromArray:newBits.
 
     newImage colorMap:newColorMap.