#BUGFIX by cg
authorClaus Gittinger <cg@exept.de>
Mon, 28 Aug 2017 03:36:12 +0200
changeset 3987 d061c7dccdbe
parent 3986 a71704884650
child 3988 1bc80a8c7566
#BUGFIX by cg class: TIFFReader changed: #readDeflateTiffImageData #readImageDataUsingDecompresor:
TIFFReader.st
--- a/TIFFReader.st	Mon Aug 28 02:34:42 2017 +0200
+++ b/TIFFReader.st	Mon Aug 28 03:36:12 2017 +0200
@@ -897,137 +897,147 @@
 !
 
 readDeflateTiffImageData
-    |bytesPerRowIn bytesPerRow nPlanes overAllBytes
-     bytesPerStrip "{ Class: SmallInteger }"
-     nBytes        "{ Class: SmallInteger }"
-     stripNr       "{ Class: SmallInteger }"
-     offset        "{ Class: SmallInteger }"
-     row           "{ Class: SmallInteger }" 
-     zlibReader nread msb
-     convertFloats convertDoubles conversionBuffer|
-
-    nPlanes := samplesPerPixel.
-    convertFloats := convertDoubles := false.
-    
-    (nPlanes >= 3) ifTrue:[
-        bytesPerRowIn := width * ((bitsPerSample sum + 7) // 8).
-        (bitsPerSample conform:[:each | each == 8]) ifTrue:[
-            sampleFormat == SAMPLEFORMAT_UINT ifFalse:[
-                ^ self fileFormatError:'unsupported sample format'.
-            ].
-        ] ifFalse:[
-            sampleFormat == SAMPLEFORMAT_IEEEFP ifTrue:[
-                nPlanes == 3 ifTrue:[
-                    (bitsPerSample conform:[:each | each == 64]) ifTrue:[
-                        convertDoubles := true.
-                        bytesPerRow := width * nPlanes.
-                        bitsPerSample := #(8 8 8).
-                    ] ifFalse:[
-                        (bitsPerSample conform:[:each | each == 32]) ifTrue:[
-                            convertFloats := true.
-                            bytesPerRow := width * nPlanes.
-                            bitsPerSample := #(8 8 8).
-                        ] ifFalse:[
-                            ^ self fileFormatError:'only 64/64/64 bits/sample are supported with IEEE_FP samples'.
-                        ].    
-                    ].    
-                ] ifFalse:[
-                    ^ self fileFormatError:'only support 3 planes with IEEE_FP sample format'.
-                ]    
-            ] ifFalse:[    
-                ^ self fileFormatError:'unsupported sample format'.
-            ].
-        ]
-    ] ifFalse:[
-        (nPlanes == 2) ifTrue:[
-            (planarConfiguration ~~ PLANARCONFIG_SEPARATE) ifTrue:[
-                ^ self fileFormatError:'only separate planes are supported'.
-            ].
-            'TIFFReader [info]: ignoring alpha plane' infoPrintCR.
-            nPlanes := 1
-        ].
-        (nPlanes == 1) ifFalse:[
-            ^ self fileFormatError:'unsupported nPlanes: ' , nPlanes printString, '; only 3-sample rgb / monochrome supported'.
-        ].
-        bytesPerRowIn := (width * (bitsPerSample at:1) + 7) // 8.
-    ].
-    stripRowCounts notNil ifTrue:[
-        ^ self fileFormatError:'stripRowCounts not supported'.
-    ].
-
-    "/ 'TIFFReader: decompressing Deflate ...' infoPrintNL.
-
-    bytesPerRow isNil ifTrue:[ bytesPerRow := bytesPerRowIn ].
-    
-    overAllBytes := bytesPerRow * height.
-    data := ByteArray new:overAllBytes.
-    (convertFloats or:[convertDoubles]) ifTrue:[
-        conversionBuffer := ByteArray new:(bytesPerRowIn * rowsPerStrip).
-    ].    
-    
-    offset := 1.
-    stripNr := 0.
-
-    row := 1.
-    bytesPerStrip := bytesPerRow * rowsPerStrip.
-
-    [row <= height] whileTrue:[
-        stripNr := stripNr + 1.
-        self positionToStrip:stripNr.
-        nBytes := stripByteCounts at:stripNr.
-        
-        zlibReader := ZipStream readOpenAsZipStreamOn:inStream suppressHeaderAndChecksum:false.
-        zlibReader binary.
-
-        conversionBuffer notNil ifTrue:[
-            nread := zlibReader next:nBytes into:conversionBuffer startingAt:1.
-            msb := (byteOrder == #msb).
-            convertFloats ifTrue:[
-                |i|
-
-                self assert:(nread \\ 4) == 0.
-                i := 0.
-                1 to:nread-1 by:4 do:[:iF |
-                    |dVal byteVal|
-                    dVal := conversionBuffer floatAt:iF MSB:msb.
-                    "/ rescale from 0..1 to 0..255
-                    byteVal := (dVal * 255) asInteger clampBetween:0 and:255.
-                    data at:offset+i put:byteVal.
-                    i := i + 1.
-                ].
-            ] ifFalse:[
-                convertDoubles ifTrue:[
-                    |i|
-                    
-                    self assert:(nread \\ 8) == 0.
-                    i := 0.
-                    1 to:nread-1 by:8 do:[:iF |
-                        |dVal byteVal|
-                        dVal := conversionBuffer doubleAt:iF MSB:msb.
-                        "/ rescale from 0..1 to 0..255
-                        byteVal := (dVal * 255) asInteger clampBetween:0 and:255.
-                        data at:offset+i put:byteVal.
-                        i := i + 1.
-                    ].
-                ]
-            ].    
-        ] ifFalse:[    
-            nread := zlibReader next:nBytes into:data startingAt:offset.
-        ].
-        
-        offset := offset + bytesPerStrip.
-        row := row + rowsPerStrip
-    ].
-
-    (predictor ~~ 1) ifTrue:[
-        (predictor == 2) ifTrue:[
-            self class decodeDelta:nPlanes in:data width:width height:height
-        ] ifFalse:[
-            ^ self fileFormatError:'unsupported predictor'
-        ].    
-    ]
-
-    "Modified: / 27-08-2017 / 15:38:08 / cg"
+    self readImageDataUsingDecompresor:
+        [:inBytes :inCount :outBytes :outOffset :outCount |
+            |zlibReader|
+            
+            zlibReader := ZipStream readOpenAsZipStreamOn:(inBytes readStream) suppressHeaderAndChecksum:false.
+            zlibReader binary.
+            zlibReader next:outCount into:outBytes startingAt:outOffset.
+            outCount.
+        ].
+
+"/    |bytesPerRowIn bytesPerRow nPlanes overAllBytes
+"/     bytesPerStrip "{ Class: SmallInteger }"
+"/     nBytes        "{ Class: SmallInteger }"
+"/     stripNr       "{ Class: SmallInteger }"
+"/     offset        "{ Class: SmallInteger }"
+"/     row           "{ Class: SmallInteger }" 
+"/     zlibReader nread msb
+"/     convertFloats convertDoubles conversionBuffer|
+"/
+"/    nPlanes := samplesPerPixel.
+"/    convertFloats := convertDoubles := false.
+"/    
+"/    (nPlanes >= 3) ifTrue:[
+"/        bytesPerRowIn := width * ((bitsPerSample sum + 7) // 8).
+"/        (bitsPerSample conform:[:each | each == 8]) ifTrue:[
+"/            sampleFormat == SAMPLEFORMAT_UINT ifFalse:[
+"/                ^ self fileFormatError:'unsupported sample format'.
+"/            ].
+"/        ] ifFalse:[
+"/            sampleFormat == SAMPLEFORMAT_IEEEFP ifTrue:[
+"/                nPlanes == 3 ifTrue:[
+"/                    (bitsPerSample conform:[:each | each == 64]) ifTrue:[
+"/                        convertDoubles := true.
+"/                        bytesPerRow := width * nPlanes.
+"/                        bitsPerSample := #(8 8 8).
+"/                    ] ifFalse:[
+"/                        (bitsPerSample conform:[:each | each == 32]) ifTrue:[
+"/                            convertFloats := true.
+"/                            bytesPerRow := width * nPlanes.
+"/                            bitsPerSample := #(8 8 8).
+"/                        ] ifFalse:[
+"/                            ^ self fileFormatError:'only 64/64/64 bits/sample are supported with IEEE_FP samples'.
+"/                        ].    
+"/                    ].    
+"/                ] ifFalse:[
+"/                    ^ self fileFormatError:'only support 3 planes with IEEE_FP sample format'.
+"/                ]    
+"/            ] ifFalse:[    
+"/                ^ self fileFormatError:'unsupported sample format'.
+"/            ].
+"/        ]
+"/    ] ifFalse:[
+"/        (nPlanes == 2) ifTrue:[
+"/            (planarConfiguration ~~ PLANARCONFIG_SEPARATE) ifTrue:[
+"/                ^ self fileFormatError:'only separate planes are supported'.
+"/            ].
+"/            'TIFFReader [info]: ignoring alpha plane' infoPrintCR.
+"/            nPlanes := 1
+"/        ].
+"/        (nPlanes == 1) ifFalse:[
+"/            ^ self fileFormatError:'unsupported nPlanes: ' , nPlanes printString, '; only 3-sample rgb / monochrome supported'.
+"/        ].
+"/        bytesPerRowIn := (width * (bitsPerSample at:1) + 7) // 8.
+"/    ].
+"/    stripRowCounts notNil ifTrue:[
+"/        ^ self fileFormatError:'stripRowCounts not supported'.
+"/    ].
+"/
+"/    "/ 'TIFFReader: decompressing Deflate ...' infoPrintNL.
+"/
+"/    bytesPerRow isNil ifTrue:[ bytesPerRow := bytesPerRowIn ].
+"/    
+"/    overAllBytes := bytesPerRow * height.
+"/    data := ByteArray new:overAllBytes.
+"/    (convertFloats or:[convertDoubles]) ifTrue:[
+"/        conversionBuffer := ByteArray new:(bytesPerRowIn * rowsPerStrip).
+"/    ].    
+"/    
+"/    offset := 1.
+"/    stripNr := 0.
+"/
+"/    row := 1.
+"/    bytesPerStrip := bytesPerRow * rowsPerStrip.
+"/
+"/    [row <= height] whileTrue:[
+"/        stripNr := stripNr + 1.
+"/        self positionToStrip:stripNr.
+"/        nBytes := stripByteCounts at:stripNr.
+"/        
+"/        zlibReader := ZipStream readOpenAsZipStreamOn:inStream suppressHeaderAndChecksum:false.
+"/        zlibReader binary.
+"/
+"/        conversionBuffer notNil ifTrue:[
+"/            nread := zlibReader next:nBytes into:conversionBuffer startingAt:1.
+"/            msb := (byteOrder == #msb).
+"/            convertFloats ifTrue:[
+"/                |i|
+"/
+"/                self assert:(nread \\ 4) == 0.
+"/                i := 0.
+"/                1 to:nread-1 by:4 do:[:iF |
+"/                    |dVal byteVal|
+"/                    dVal := conversionBuffer floatAt:iF MSB:msb.
+"/                    "/ rescale from 0..1 to 0..255
+"/                    byteVal := (dVal * 255) asInteger clampBetween:0 and:255.
+"/                    data at:offset+i put:byteVal.
+"/                    i := i + 1.
+"/                ].
+"/            ] ifFalse:[
+"/                convertDoubles ifTrue:[
+"/                    |i|
+"/                    
+"/                    self assert:(nread \\ 8) == 0.
+"/                    i := 0.
+"/                    1 to:nread-1 by:8 do:[:iF |
+"/                        |dVal byteVal|
+"/                        dVal := conversionBuffer doubleAt:iF MSB:msb.
+"/                        "/ rescale from 0..1 to 0..255
+"/                        byteVal := (dVal * 255) asInteger clampBetween:0 and:255.
+"/                        data at:offset+i put:byteVal.
+"/                        i := i + 1.
+"/                    ].
+"/                ]
+"/            ].    
+"/        ] ifFalse:[    
+"/            nread := zlibReader next:nBytes into:data startingAt:offset.
+"/        ].
+"/        
+"/        offset := offset + bytesPerStrip.
+"/        row := row + rowsPerStrip
+"/    ].
+"/
+"/    (predictor ~~ 1) ifTrue:[
+"/        (predictor == 2) ifTrue:[
+"/            self class decodeDelta:nPlanes in:data width:width height:height
+"/        ] ifFalse:[
+"/            ^ self fileFormatError:'unsupported predictor'
+"/        ].    
+"/    ]
+
+    "Modified (format): / 28-08-2017 / 03:06:39 / cg"
 !
 
 readImageDataUsingDecompresor:decompressorBlock
@@ -1038,19 +1048,27 @@
      row           "{ Class: SmallInteger }" 
      msb
      convert convertFloats convertDoubles convertHalfFloats 
-     convertInt16s convertFillOrder
+     swapInt16s convertFillOrder
      inBuffer conversionBuffer floats nDecompressed 
      rowsInThisStrip bytesInThisStrip|
 
     nPlanes := samplesPerPixel.
     convert := convertFloats := convertDoubles := convertHalfFloats := false.
-    convertInt16s := convertFillOrder := false.
+    swapInt16s := convertFillOrder := false.
     
     (nPlanes >= 3) ifTrue:[
         (planarConfiguration ~~ PLANARCONFIG_CONTIG) ifTrue:[
             ^ self fileFormatError:'only non separate planes are supported with 3 or 4 planes'.
         ].
-
+        (nPlanes > 4) ifTrue:[
+            photometric == #cmyk ifTrue:[
+                "/ ignore alpha
+                'TIFFReader [info]: ignoring alpha plane' infoPrintCR.
+                nPlanes := samplesPerPixel := 4.
+                bitsPerSample := bitsPerSample copyTo:4.
+            ].
+        ].
+        
         bytesPerRowIn := width * ((bitsPerSample sum + 7) // 8).
         sampleFormat == SAMPLEFORMAT_UINT ifTrue:[
             (bitsPerSample conform:[:each | each == 8]) ifTrue:[
@@ -1104,7 +1122,9 @@
         ].
         bytesPerRowIn := (width * (bitsPerSample at:1) + 7) // 8.
         (bitsPerSample at:1) == 16 ifTrue:[
-            convertInt16s := true.
+            byteOrder ~~ #msb ifTrue:[
+"/                swapInt16s := true.
+            ].    
         ].    
     ].
     fillOrder ~~ #msb ifTrue:[
@@ -1201,7 +1221,7 @@
         stripNr := stripNr + 1.
     ].
 
-    convertInt16s ifTrue:[
+    swapInt16s ifTrue:[
         data swapBytes
     ].
     convertFillOrder ifTrue:[
@@ -1219,7 +1239,7 @@
     ].
 
     "Created: / 27-08-2017 / 22:48:17 / cg"
-    "Modified: / 28-08-2017 / 02:30:33 / cg"
+    "Modified: / 28-08-2017 / 03:12:44 / cg"
 !
 
 readJBIGTiffImageData