author | Claus Gittinger <cg@exept.de> |
Mon, 24 Sep 2018 10:42:58 +0200 | |
changeset 4181 | f0b8d41ff628 |
parent 4104 | bb3be6b0aaa5 |
child 4203 | 6406f4c9a469 |
permissions | -rw-r--r-- |
308 | 1 |
" |
2 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
1671 | 12 |
"{ Package: 'stx:libview2' }" |
13 |
||
3461 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
308 | 16 |
ImageReader subclass:#PNGReader |
4005 | 17 |
instanceVariableNames:'colorType bitsPerChannel compressionMethod filterMethod |
3889 | 18 |
interlaceMode bytesPerScanline globalDataChunk thisScanline |
3900 | 19 |
prevScanline processTextChunks specialChunkHandlers |
4017 | 20 |
paletteAlphaEntries paletteIndexForMaskedPixels image forceRGB |
21 |
depthImage' |
|
3909 | 22 |
classVariableNames:'ColorTypeGray ColorTypeGrayAlpha ColorTypePalette ColorTypeRGB |
23 |
ColorTypeRGBAlpha Verbose' |
|
3889 | 24 |
poolDictionaries:'' |
25 |
category:'Graphics-Images-Readers' |
|
308 | 26 |
! |
27 |
||
398 | 28 |
!PNGReader class methodsFor:'documentation'! |
308 | 29 |
|
30 |
copyright |
|
31 |
" |
|
32 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
33 |
All Rights Reserved |
|
34 |
||
35 |
This software is furnished under a license and may be used |
|
36 |
only in accordance with the terms of that license and with the |
|
37 |
inclusion of the above copyright notice. This software may not |
|
38 |
be provided or otherwise made available to, or used by, any |
|
39 |
other person. No title to or ownership of the software is |
|
40 |
hereby transferred. |
|
41 |
" |
|
42 |
||
43 |
||
44 |
! |
|
45 |
||
46 |
documentation |
|
47 |
" |
|
3893 | 48 |
This class provides methods for loading and saving PNG pictures. |
3599 | 49 |
It is currenty unfinished (some interlaced image formats are unsupported, for example: grayscale+alpha) |
3437 | 50 |
In the meantime, use a pngtoXXX converter for interlaced images, and read XXX. |
308 | 51 |
|
2725 | 52 |
[caveats:] |
3893 | 53 |
writer can only store mask with depth24 images (for now). |
54 |
writer only generates unfiltered non-interlaced data. |
|
2725 | 55 |
|
4044 | 56 |
[Special:] |
57 |
the EnforcedImageTypeQuery is asked for; |
|
58 |
if #rgb is returned AND the image is rgba, then the alpha channel is ignored |
|
59 |
and an rgb (Depth24Image) is returned instead. |
|
60 |
||
308 | 61 |
[See also:] |
62 |
Image Form Icon |
|
4104 | 63 |
BlitImageReader FaceReader GIFReader JPEGReader MacOSXIconReader |
64 |
PBMReader PCXReader |
|
308 | 65 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
66 |
XBMReader XPMReader XWDReader |
|
67 |
||
68 |
[author:] |
|
69 |
Claus Gittinger |
|
70 |
" |
|
1694 | 71 |
! |
308 | 72 |
|
1694 | 73 |
examples |
74 |
" |
|
75 |
PNGReader fromFile:'/home/cg/AudioExplorer_51_files/use_small.png' |
|
2509 | 76 |
PNGReader fromFile:'\\Exeptn\tmp\images\expeccoScreenshot4020_5526507.png' |
2683 | 77 |
|
78 |
PNGReader fromFile:'C:\Users\cg\Desktop\croquet\cobalt-base-current-build-20090210\cobalt-base-current-build-20090210\content\models\textures\checkerboard.png' |
|
2695 | 79 |
PNGReader fromFile:'C:\Dokumente und Einstellungen\cg\Desktop\misc\PNGs\Delete.png' |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
80 |
PNGReader fromFile:'\\exeptn\unsaved\pd_stuff\PNGs\Delete.png' |
2699 | 81 |
PNGReader fromFile:'\\exeptn\unsaved\pd_stuff\PNGs\Down.png' |
2703 | 82 |
|
83 |
||
84 |
|img img2 outStream png| |
|
2725 | 85 |
|
2703 | 86 |
img := ToolbarIconLibrary error32x32Icon. |
87 |
outStream := WriteStream on:(ByteArray new:100). |
|
88 |
PNGReader save:img onStream:outStream. |
|
89 |
png := outStream contents. |
|
90 |
img2 := PNGReader fromStream:(png readStream). |
|
2708 | 91 |
self assert:(img bits = img2 bits). |
92 |
img inspect. |
|
2703 | 93 |
img2 inspect. |
1694 | 94 |
" |
308 | 95 |
! ! |
96 |
||
398 | 97 |
!PNGReader class methodsFor:'initialization'! |
308 | 98 |
|
99 |
initialize |
|
2699 | 100 |
"install myself in the Image classes fileFormat table for the `.png' extension." |
308 | 101 |
|
2709 | 102 |
MIMETypes defineImageType:'image/png' suffix:'png' reader:self. |
103 |
"/ backward compatibility from times when png was not yet so common... |
|
104 |
MIMETypes defineImageType:'image/x-png' suffix:nil reader:self. |
|
3900 | 105 |
|
106 |
ColorTypeGray := 0. |
|
107 |
ColorTypeRGB := 2. |
|
108 |
ColorTypePalette := 3. |
|
109 |
ColorTypeGrayAlpha := 4. |
|
110 |
ColorTypeRGBAlpha := 6. |
|
111 |
||
112 |
"Modified: / 16-02-2017 / 19:41:40 / cg" |
|
308 | 113 |
! ! |
114 |
||
3909 | 115 |
!PNGReader class methodsFor:'accessing'! |
116 |
||
117 |
pngHeader |
|
118 |
^ #[137 80 78 71 13 10 26 10] |
|
119 |
! ! |
|
120 |
||
2705 | 121 |
!PNGReader class methodsFor:'queries'! |
122 |
||
123 |
canRepresent:anImage |
|
124 |
"return true, if anImage can be represented in my file format. |
|
3893 | 125 |
Any image without mask is supported; |
126 |
only depth24 images with mask are (currently)." |
|
2705 | 127 |
|
3893 | 128 |
anImage mask notNil ifTrue:[ |
129 |
"/ for now, can only write depth24 images with mask |
|
130 |
^ anImage depth == 24 |
|
131 |
]. |
|
2705 | 132 |
^ true |
3893 | 133 |
|
134 |
"Modified (comment): / 16-02-2017 / 16:28:53 / cg" |
|
2705 | 135 |
! ! |
136 |
||
398 | 137 |
!PNGReader class methodsFor:'testing'! |
308 | 138 |
|
139 |
isValidImageFile:aFileName |
|
140 |
"return true, if aFileName contains a PNG image" |
|
141 |
||
142 |
|inStream magic| |
|
143 |
||
144 |
inStream := self streamReadingFile:aFileName. |
|
145 |
inStream isNil ifTrue:[^ false]. |
|
146 |
||
147 |
inStream binary. |
|
148 |
||
149 |
magic := ByteArray new:8. |
|
150 |
inStream nextBytes:8 into:magic. |
|
151 |
inStream close. |
|
152 |
||
153 |
^ (magic = #[137 80 78 71 13 10 26 10]) |
|
154 |
||
1694 | 155 |
" |
156 |
self isValidImageFile:'/home/cg/AudioExplorer_51_files/use_small.png' |
|
2683 | 157 |
self isValidImageFile:'C:\Users\cg\Desktop\croquet\cobalt-base-current-build-20090210\cobalt-base-current-build-20090210\content\models\textures\checkerboard.png' |
2695 | 158 |
self isValidImageFile:'C:\Dokumente und Einstellungen\cg\Desktop\misc\PNGs\Delete.png' |
1694 | 159 |
" |
160 |
||
308 | 161 |
"Modified: 21.6.1996 / 20:38:46 / cg" |
162 |
! ! |
|
163 |
||
3900 | 164 |
!PNGReader methodsFor:'accessing'! |
165 |
||
166 |
makeImage |
|
167 |
image isNil ifTrue:[ |
|
168 |
image := super makeImage. |
|
169 |
]. |
|
170 |
^ image |
|
171 |
||
172 |
"Created: / 17-02-2017 / 11:14:45 / cg" |
|
173 |
! |
|
174 |
||
175 |
pngHeader |
|
3909 | 176 |
<resource: #obsolete> |
177 |
^ self class pngHeader |
|
178 |
||
179 |
"Modified: / 22-02-2017 / 11:00:44 / cg" |
|
3900 | 180 |
! ! |
181 |
||
3889 | 182 |
!PNGReader methodsFor:'hooks'! |
183 |
||
184 |
specialChunkHandlerAt:chunkType put:aHandlerBlock |
|
185 |
"define a handler action, which processes unknown chunks. |
|
186 |
The handler block will be called with one or two arguments: |
|
187 |
the binary data of the chunk, and optionally the reader itself" |
|
188 |
||
189 |
specialChunkHandlers isNil ifTrue:[ |
|
190 |
specialChunkHandlers := Dictionary new |
|
191 |
]. |
|
192 |
specialChunkHandlers at:chunkType put:aHandlerBlock |
|
193 |
||
194 |
"Created: / 14-02-2017 / 11:43:33 / cg" |
|
195 |
! ! |
|
196 |
||
3900 | 197 |
!PNGReader methodsFor:'reading'! |
198 |
||
199 |
fromStream:aStream |
|
200 |
"read a stream containing a PNG image. |
|
201 |
Leave the image description in instance variables |
|
202 |
(i.e. to get the image, ask with image)." |
|
203 |
||
4017 | 204 |
|header bitsPerSampleOut bytesPerScanlineOut| |
3900 | 205 |
|
206 |
inStream := aStream. |
|
207 |
aStream binary. |
|
208 |
||
209 |
"PNG-files are always msb (network-world)" |
|
210 |
byteOrder := #msb. |
|
211 |
||
212 |
header := ByteArray new:8. |
|
213 |
aStream nextBytes:8 into:header. |
|
214 |
||
3909 | 215 |
header = (self class pngHeader) ifFalse:[ |
3900 | 216 |
Logger warning:'PNGReader: not a png file (%1)' with:aStream. |
217 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: not a png file'. |
|
218 |
^ nil |
|
219 |
]. |
|
220 |
||
221 |
(self getIHDRChunk) ifFalse:[ |
|
222 |
Logger warning:'PNGReader: required IHDR chunk missing (%1)' with:aStream. |
|
223 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: required IHDR chunk missing'. |
|
224 |
^ nil |
|
225 |
]. |
|
226 |
||
227 |
compressionMethod ~~ 0 ifTrue:[ |
|
228 |
('PNGReader: compressionMethod %s not supported.' printfWith:compressionMethod printString) infoPrintCR. |
|
229 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: unsupported compression method'. |
|
230 |
^ nil |
|
231 |
]. |
|
232 |
filterMethod > 0 ifTrue:[ |
|
233 |
'PNGReader: invalid filterMethod' infoPrintCR. |
|
234 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: invalid/unsupported filterMethod'. |
|
235 |
^ nil. |
|
236 |
]. |
|
237 |
interlaceMode > 1 ifTrue:[ |
|
238 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: invalid/unsupported interlaceMode'. |
|
239 |
'PNGReader: invalid interlaceMode' infoPrintCR. |
|
240 |
^ nil. |
|
241 |
]. |
|
242 |
(self setColorType:colorType) ifFalse:[ |
|
243 |
^ nil |
|
244 |
]. |
|
245 |
||
246 |
[self getChunk] whileTrue. |
|
247 |
||
4017 | 248 |
"/ kludge: if we get a grey image with alpha, |
249 |
"/ load as rgb+alpha |
|
250 |
forceRGB := false. |
|
251 |
depthImage := depth. |
|
252 |
||
253 |
"/ bytesPerScanline is always the in-bytesPerScanline |
|
3900 | 254 |
bytesPerScanline := self bytesPerRow. |
4017 | 255 |
bytesPerScanlineOut := bytesPerScanline. |
256 |
||
257 |
samplesPerPixel == 2 ifTrue:[ |
|
258 |
photometric == #blackIs0 ifTrue:[ |
|
259 |
depth == 16 ifTrue:[ |
|
4099 | 260 |
"/ force grey8+a8 into red8+green8+blue8+a8 |
4017 | 261 |
forceRGB := true. |
262 |
depthImage := 32. |
|
263 |
bytesPerScanlineOut := width * 4. |
|
264 |
bitsPerSampleOut := #[8 8 8 8]. |
|
265 |
] ifFalse:[ |
|
266 |
depth == 32 ifTrue:[ |
|
4099 | 267 |
"/ force grey16+a16 into red8+green8+blue8+a8 |
4017 | 268 |
forceRGB := true. |
269 |
depthImage := 32. |
|
270 |
bytesPerScanlineOut := width * 4. |
|
271 |
bitsPerSampleOut := #[8 8 8 8]. |
|
272 |
] ifFalse:[ |
|
4099 | 273 |
self error:'unsupported grayscale image depth'. |
4017 | 274 |
]. |
275 |
]. |
|
276 |
]. |
|
277 |
]. |
|
278 |
||
279 |
data := ByteArray new:(bytesPerScanlineOut * height). |
|
280 |
||
3900 | 281 |
globalDataChunk notNil ifTrue:[ |
282 |
self processGlobalIDATChunk. |
|
283 |
globalDataChunk := nil. |
|
284 |
]. |
|
285 |
||
286 |
photometric == #palette ifTrue:[ |
|
287 |
colorMap isNil ifTrue:[ |
|
288 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: palette chunk missing'. |
|
289 |
'PNGReader: palette chunk missing.' infoPrintCR. |
|
290 |
"/ ^ nil |
|
291 |
]. |
|
292 |
]. |
|
293 |
||
294 |
paletteAlphaEntries notNil ifTrue:[ |
|
295 |
"/ this vector now provides alpha values. |
|
296 |
"/ for now, we can only deal with 0/255 alpha values... |
|
3903 | 297 |
self generateMaskFromPaletteAlphaEntries |
3900 | 298 |
]. |
299 |
||
4017 | 300 |
forceRGB ifTrue:[ |
301 |
depth := depthImage. |
|
302 |
photometric := #rgba. |
|
303 |
bitsPerSample := bitsPerSampleOut. |
|
304 |
samplesPerPixel := 4. |
|
305 |
]. |
|
306 |
||
3900 | 307 |
" |
308 |
PNGReader fromFile:'/home/cg/libpng-0.89c/pngtest.png' |
|
3903 | 309 |
|
310 |
Verbose := true. |
|
311 |
PNGReader fromFile:(RegressionTests::ImageReaderTest packageDirectory construct:'testData/pngImages/tbbn3p08.png') |
|
312 |
||
3900 | 313 |
" |
314 |
||
4017 | 315 |
"Modified: / 17-09-2017 / 15:30:45 / cg" |
4099 | 316 |
"Modified (format): / 02-06-2018 / 06:55:59 / Claus Gittinger" |
3900 | 317 |
! ! |
318 |
||
319 |
!PNGReader methodsFor:'reading-private'! |
|
320 |
||
321 |
generateMaskFromPaletteAlphaEntries |
|
322 |
image isNil ifTrue:[ |
|
323 |
self makeImage |
|
324 |
]. |
|
325 |
||
3972 | 326 |
colorType == ColorTypePalette ifTrue:[ |
327 |
image depth == 8 ifTrue:[ |
|
328 |
"/ if there is only one entry to care for... |
|
329 |
(paletteAlphaEntries occurrencesOf:0) == 1 ifTrue:[ |
|
330 |
(paletteAlphaEntries conform:[:alpha | (alpha == 0) or:[alpha == 255]]) ifTrue:[ |
|
331 |
image mask:(self class |
|
332 |
buildMaskFromColor:(paletteAlphaEntries indexOf:0)-1 |
|
333 |
for:data depth:(image depth) |
|
334 |
width:width height:height). |
|
335 |
^ self |
|
336 |
]. |
|
3900 | 337 |
]. |
3972 | 338 |
]. |
339 |
||
3900 | 340 |
"/ multiple masked pixels... |
341 |
paletteAlphaEntries keysAndValuesDo:[:pixelIndex :alpha | |
|
3972 | 342 |
alpha == 255 ifTrue:[ |
3900 | 343 |
image createMaskForPixelValue:pixelIndex |
344 |
] ifFalse:[ |
|
3972 | 345 |
alpha == 0 ifTrue:[ |
3900 | 346 |
"/ ok; already |
347 |
] ifFalse:[ |
|
4057 | 348 |
image createMaskForPixelValue:pixelIndex |
3900 | 349 |
] |
350 |
]. |
|
351 |
]. |
|
3972 | 352 |
"/ image mask:nil. |
3900 | 353 |
^ self. |
354 |
]. |
|
355 |
||
356 |
colorType == ColorTypeGray ifTrue:[ |
|
357 |
|maskPixel| |
|
358 |
||
359 |
maskPixel := paletteAlphaEntries unsignedInt16At:1 MSB:true. |
|
360 |
image createMaskForPixelValue:maskPixel. |
|
361 |
^ self |
|
362 |
]. |
|
363 |
colorType == ColorTypeRGB ifTrue:[ |
|
364 |
|mr mg mb maskPixel| |
|
365 |
||
366 |
mr := paletteAlphaEntries unsignedInt16At:1 MSB:true. |
|
367 |
mg := paletteAlphaEntries unsignedInt16At:3 MSB:true. |
|
368 |
mb := paletteAlphaEntries unsignedInt16At:5 MSB:true. |
|
369 |
depth <= 32 ifTrue:[ |
|
370 |
"/ only use the low bits... |
|
371 |
mr > 255 ifTrue:[ self error:'only 8bit RGB supported']. |
|
372 |
mg > 255 ifTrue:[ self error:'only 8bit RGB supported']. |
|
373 |
mb > 255 ifTrue:[ self error:'only 8bit RGB supported']. |
|
374 |
maskPixel := ((mr bitShift:16) bitOr:(mg bitShift:8)) bitOr:mb. |
|
375 |
] ifFalse:[ |
|
376 |
maskPixel := ((mr bitShift:32) bitOr:(mg bitShift:16)) bitOr:mb. |
|
377 |
]. |
|
378 |
image createMaskForPixelValue:maskPixel. |
|
379 |
^ self |
|
380 |
]. |
|
381 |
||
382 |
"Created: / 17-02-2017 / 08:07:04 / cg" |
|
3972 | 383 |
"Modified: / 23-08-2017 / 17:24:30 / cg" |
3900 | 384 |
! |
385 |
||
386 |
getChunk |
|
387 |
|len type crc| |
|
388 |
||
389 |
inStream atEnd ifTrue:[^ false]. |
|
390 |
||
391 |
len := inStream nextInt32MSB:true. |
|
392 |
type := String new:4. |
|
393 |
(inStream nextBytes:4 into:type) ~~ 4 ifTrue:[^ false]. |
|
394 |
||
395 |
Verbose == true ifTrue:[ |
|
4099 | 396 |
'PNGReader: chunk: ' infoPrint. type infoPrint. |
397 |
' len: ' infoPrint. len infoPrintCR. |
|
3900 | 398 |
]. |
399 |
||
400 |
type = 'IEND' ifTrue:[^ false]. |
|
401 |
||
402 |
(self processChunk:type len:len) ifFalse:[^ false]. |
|
403 |
||
404 |
crc := inStream nextInt32MSB:true. "/ ignored - for now |
|
405 |
^ true |
|
406 |
||
4099 | 407 |
"Created: / 21-06-1996 / 21:09:36 / cg" |
408 |
"Modified: / 02-06-2018 / 06:42:58 / Claus Gittinger" |
|
3900 | 409 |
! |
410 |
||
411 |
getIHDRChunk |
|
412 |
|len type crc| |
|
413 |
||
414 |
len := inStream nextInt32MSB:true. |
|
415 |
||
416 |
type := String new:4. |
|
417 |
inStream nextBytes:4 into:type. |
|
418 |
||
419 |
type = 'IHDR' ifFalse:[ |
|
420 |
Logger warning:'PNGReader: expected IHDR magic (%1)' with:inStream. |
|
421 |
^ false |
|
422 |
]. |
|
423 |
len == 13 ifFalse:[ |
|
424 |
Logger warning:'PNGReader: bad IHDR size (%1)' with:inStream. |
|
425 |
^ false |
|
426 |
]. |
|
427 |
||
428 |
width := inStream nextInt32MSB:true. |
|
429 |
height := inStream nextInt32MSB:true. |
|
430 |
||
431 |
(width <= 0 or:[height <= 0]) ifTrue:[ |
|
432 |
Logger warning:'PNGReader: invalid dimension(s) (%1)' with:inStream. |
|
433 |
^ false. |
|
434 |
]. |
|
435 |
self reportDimension. |
|
436 |
||
437 |
bitsPerChannel := inStream nextByte. |
|
4005 | 438 |
depth := bitsPerChannel. "/ for now - might be changed by setColorType. |
3900 | 439 |
colorType := inStream nextByte. |
440 |
||
441 |
compressionMethod := inStream nextByte. |
|
442 |
filterMethod := inStream nextByte. |
|
443 |
interlaceMode := inStream nextByte. |
|
444 |
||
445 |
Verbose == true ifTrue:[ |
|
446 |
'PNGReader: IHDR:' infoPrintCR. |
|
447 |
'PNGReader: width: ' infoPrint. width infoPrintCR. |
|
448 |
'PNGReader: height: ' infoPrint. height infoPrintCR. |
|
449 |
'PNGReader: bitsPerChannel: ' infoPrint. bitsPerChannel infoPrintCR. |
|
450 |
'PNGReader: colorType: ' infoPrint. colorType infoPrintCR. |
|
451 |
'PNGReader: compressionMethod: ' infoPrint. compressionMethod infoPrintCR. |
|
452 |
'PNGReader: filterMethod: ' infoPrint. filterMethod infoPrintCR. |
|
453 |
'PNGReader: interlaceMode: ' infoPrint. interlaceMode infoPrintCR. |
|
454 |
]. |
|
455 |
||
456 |
crc := inStream nextInt32MSB:true. |
|
457 |
^ true |
|
458 |
||
459 |
"Modified: / 17-02-2017 / 11:35:20 / cg" |
|
4005 | 460 |
"Modified (comment): / 31-08-2017 / 18:03:41 / cg" |
3900 | 461 |
! |
462 |
||
463 |
handleText:text keyword:keyword |
|
464 |
"/ things like exif data etc. |
|
465 |
"/ self halt. |
|
466 |
||
467 |
"/Standard keywords for text chunks: |
|
468 |
"/ |
|
469 |
"/ Title Short (one line) title or caption for image |
|
470 |
"/ Author Name of image's creator |
|
471 |
"/ Description Description of image (possibly long) |
|
472 |
"/ Copyright Copyright notice |
|
473 |
"/ Creation Time Time of original image creation |
|
474 |
"/ Software Software used to create the image |
|
475 |
"/ Disclaimer Legal disclaimer |
|
476 |
"/ Warning Warning of nature of content |
|
477 |
"/ Source Device used to create the image |
|
478 |
"/ Comment Miscellaneous comment; conversion from |
|
479 |
"/ GIF comment |
|
480 |
! |
|
481 |
||
482 |
setColorType:colorType |
|
4005 | 483 |
colorType == ColorTypeGray "0" ifTrue:[ |
3900 | 484 |
photometric := #blackIs0. |
485 |
samplesPerPixel := 1. |
|
486 |
bitsPerSample := Array with:bitsPerChannel. |
|
487 |
depth := bitsPerChannel. |
|
488 |
^ true. |
|
489 |
]. |
|
490 |
||
4005 | 491 |
colorType == ColorTypeRGB "2" ifTrue:[ |
3900 | 492 |
photometric := #rgb. |
493 |
samplesPerPixel := 3. |
|
494 |
bitsPerSample := Array with:bitsPerChannel with:bitsPerChannel with:bitsPerChannel. |
|
495 |
depth := bitsPerChannel * 3. |
|
4005 | 496 |
|
497 |
bitsPerChannel < 8 ifTrue:[ |
|
498 |
bitsPerChannel == 5 ifTrue:[ |
|
499 |
depth := 16 |
|
500 |
] ifFalse:[ |
|
501 |
('PNGReader: unsupported bitsPerChannel:%1 for colorType rgb' bindWith:bitsPerChannel) infoPrintCR. |
|
502 |
]. |
|
503 |
]. |
|
3900 | 504 |
^ true. |
505 |
]. |
|
506 |
||
4005 | 507 |
colorType == ColorTypePalette "3" ifTrue:[ |
3900 | 508 |
bitsPerChannel == 16 ifTrue:[ |
4005 | 509 |
'PNGReader: invalid unsupported bitsPerChannel:%1 for colorType palette' infoPrintCR. |
3900 | 510 |
^ false. |
511 |
]. |
|
512 |
photometric := #palette. |
|
513 |
samplesPerPixel := 1. |
|
514 |
bitsPerSample := Array with:bitsPerChannel. |
|
515 |
depth := bitsPerChannel. |
|
516 |
^ true. |
|
517 |
]. |
|
518 |
||
4005 | 519 |
colorType == ColorTypeGrayAlpha "4" ifTrue:[ |
3900 | 520 |
bitsPerChannel < 8 ifTrue:[ |
4005 | 521 |
'PNGReader: unsupported colorType/depth combination' infoPrintCR. |
3900 | 522 |
^ false. |
523 |
]. |
|
524 |
photometric := #blackIs0. |
|
525 |
samplesPerPixel := 2. |
|
526 |
bitsPerSample := Array with:bitsPerChannel with:bitsPerChannel. |
|
527 |
depth := bitsPerChannel * 2. |
|
528 |
^ true. |
|
529 |
]. |
|
530 |
||
4005 | 531 |
colorType == ColorTypeRGBAlpha "6" ifTrue:[ |
3900 | 532 |
bitsPerChannel < 8 ifTrue:[ |
4005 | 533 |
'PNGReader: unsupported colorType/depth combination' infoPrintCR. |
3900 | 534 |
^ false. |
535 |
]. |
|
536 |
photometric := #rgba. |
|
4044 | 537 |
ImageReader::EnforcedImageTypeQuery query == #rgb ifTrue:[ |
538 |
photometric := #rgb |
|
539 |
]. |
|
3900 | 540 |
samplesPerPixel := 4. |
541 |
bitsPerSample := Array with:bitsPerChannel with:bitsPerChannel with:bitsPerChannel with:bitsPerChannel. |
|
542 |
depth := bitsPerChannel * 4. |
|
543 |
^ true. |
|
544 |
]. |
|
545 |
||
4005 | 546 |
('PNGReader: unsupported or invalid colorType: ' , colorType printString , '.') infoPrintCR. |
3900 | 547 |
^ false |
548 |
||
4005 | 549 |
"Modified: / 31-08-2017 / 17:58:18 / cg" |
3900 | 550 |
! ! |
551 |
||
552 |
!PNGReader methodsFor:'reading-private chunks'! |
|
308 | 553 |
|
2699 | 554 |
doPass: pass |
555 |
"Certain interlace passes are skipped with certain small image dimensions" |
|
556 |
||
557 |
pass = 1 ifTrue: [ ^ true ]. |
|
558 |
((width = 1) and: [height = 1]) ifTrue: [ ^ false ]. |
|
559 |
pass = 2 ifTrue: [ ^ width >= 5 ]. |
|
560 |
pass = 3 ifTrue: [ ^ height >= 5 ]. |
|
561 |
pass = 4 ifTrue: [ ^ (width >=3 ) or: [height >= 5] ]. |
|
562 |
pass = 5 ifTrue: [ ^ height >=3 ]. |
|
563 |
pass = 6 ifTrue: [ ^ width >=2 ]. |
|
564 |
pass = 7 ifTrue: [ ^ height >=2 ]. |
|
565 |
||
566 |
self error:'invalid argument'. |
|
567 |
^ true |
|
568 |
! |
|
569 |
||
308 | 570 |
processBKGDChunkLen:len |
571 |
inStream skip:len. |
|
572 |
^ true |
|
573 |
||
574 |
"Created: 21.6.1996 / 21:15:49 / cg" |
|
575 |
! |
|
576 |
||
2707 | 577 |
processCHRMChunkLen:len |
578 |
"cHRM Primary chromaticities chunk - currently unhandled" |
|
579 |
||
580 |
|whitePointX whitePointY redX redY greenX greenY blueX blueY| |
|
581 |
||
3554 | 582 |
whitePointX := (inStream nextInt32MSB:true) / 100000.0. |
583 |
whitePointY := (inStream nextInt32MSB:true) / 100000.0. |
|
584 |
redX := (inStream nextInt32MSB:true) / 100000.0. |
|
585 |
redY := (inStream nextInt32MSB:true) / 100000.0. |
|
586 |
greenX := (inStream nextInt32MSB:true) / 100000.0. |
|
587 |
greenY := (inStream nextInt32MSB:true) / 100000.0. |
|
588 |
blueX := (inStream nextInt32MSB:true) / 100000.0. |
|
589 |
blueY := (inStream nextInt32MSB:true) / 100000.0. |
|
2707 | 590 |
|
591 |
Verbose == true ifTrue:[ |
|
592 |
'PNGReader: CHRM chunk ignored:' infoPrintCR. |
|
593 |
'PNGReader: whitePointX:' infoPrint. whitePointX infoPrintCR. |
|
594 |
'PNGReader: whitePointY:' infoPrint. whitePointX infoPrintCR. |
|
595 |
'PNGReader: redX:' infoPrint. redX infoPrintCR. |
|
596 |
'PNGReader: redY:' infoPrint. redY infoPrintCR. |
|
597 |
'PNGReader: greenX:' infoPrint. greenX infoPrintCR. |
|
598 |
'PNGReader: greenY:' infoPrint. greenY infoPrintCR. |
|
599 |
'PNGReader: blueX:' infoPrint. blueX infoPrintCR. |
|
600 |
'PNGReader: blueY:' infoPrint. blueY infoPrintCR. |
|
601 |
]. |
|
602 |
^ true |
|
603 |
! |
|
604 |
||
308 | 605 |
processChunk:type len:len |
2509 | 606 |
|chunk| |
607 |
||
608 |
type = 'IDAT' ifTrue:[ |
|
609 |
"---since the compressed data can span multiple |
|
610 |
chunks, stitch them all together first. later, |
|
611 |
if memory is an issue, we need to figure out how |
|
612 |
to do this on the fly---" |
|
613 |
chunk := inStream next:len. |
|
3821 | 614 |
globalDataChunk isNil ifTrue:[ |
615 |
globalDataChunk := ReadWriteStream on:(ByteArray new:(height * self bytesPerRow)). |
|
616 |
]. |
|
617 |
globalDataChunk nextPutAll:chunk. |
|
2509 | 618 |
^ true |
619 |
]. |
|
620 |
||
308 | 621 |
type = 'gAMA' ifTrue:[^ self processGAMAChunkLen:len]. |
622 |
type = 'sBIT' ifTrue:[^ self processSBITChunkLen:len]. |
|
623 |
type = 'tEXt' ifTrue:[^ self processTEXTChunkLen:len]. |
|
624 |
type = 'tIME' ifTrue:[^ self processTIMEChunkLen:len]. |
|
625 |
type = 'bKGD' ifTrue:[^ self processBKGDChunkLen:len]. |
|
626 |
type = 'zTXt' ifTrue:[^ self processZTXTChunkLen:len]. |
|
627 |
type = 'PLTE' ifTrue:[^ self processPLTEChunkLen:len]. |
|
628 |
||
1671 | 629 |
type = 'pHYs' ifTrue:[^ self processPHYSChunkLen:len]. |
2707 | 630 |
type = 'cHRM' ifTrue:[^ self processCHRMChunkLen:len]. |
1671 | 631 |
type = 'tRNS' ifTrue:[^ self processTRNSChunkLen:len]. |
632 |
type = 'IHDR' ifTrue:[^ self processIHDRChunkLen:len]. |
|
633 |
||
3762 | 634 |
type = 'iTXt' ifTrue:[^ self processITXTChunkLen:len]. |
635 |
type = 'iCCP' ifTrue:[^ self processICCPChunkLen:len]. |
|
3900 | 636 |
type = 'hIST' ifTrue:[^ self processHISTChunkLen:len]. |
637 |
type = 'spAL' ifTrue:[^ self processSPALChunkLen:len]. |
|
3889 | 638 |
|
639 |
specialChunkHandlers notNil ifTrue:[ |
|
640 |
|handler chunkData| |
|
641 |
||
642 |
handler := specialChunkHandlers at:type ifAbsent:nil. |
|
643 |
handler notNil ifTrue:[ |
|
644 |
chunkData := inStream nextBytes:len. |
|
645 |
handler value:chunkData optionalArgument:self. |
|
646 |
^ true. |
|
647 |
]. |
|
648 |
]. |
|
649 |
||
3891 | 650 |
type = 'niVI' ifTrue:[^ self processNIVIChunkLen:len]. |
3950 | 651 |
type = 'exEX' ifTrue:[^ self processEXEXChunkLen:len]. |
3891 | 652 |
|
308 | 653 |
('PNGReader: unrecognized chunk: ' , type , ' ignored.') infoPrintCR. |
654 |
||
655 |
inStream skip:len. |
|
656 |
^ true. |
|
657 |
||
3889 | 658 |
"Created: / 21-06-1996 / 21:10:37 / cg" |
3950 | 659 |
"Modified: / 11-04-2017 / 17:04:12 / cg" |
4099 | 660 |
"Modified: / 02-06-2018 / 06:43:03 / Claus Gittinger" |
3950 | 661 |
! |
662 |
||
663 |
processEXEXChunkLen:len |
|
664 |
"/ this cunk contains an expecco sample network. |
|
665 |
"/ ...to read it (using the expecco importer), |
|
666 |
"/ create a reader with a chunkHook and extract it there. |
|
667 |
Logger info:('PNG: skip exEX chunk'). |
|
668 |
inStream skip:len. |
|
669 |
^ true. |
|
670 |
||
671 |
"Created: / 11-04-2017 / 17:06:27 / cg" |
|
308 | 672 |
! |
673 |
||
674 |
processGAMAChunkLen:len |
|
675 |
inStream skip:len. |
|
676 |
^ true |
|
677 |
||
678 |
"Created: 21.6.1996 / 21:10:52 / cg" |
|
679 |
! |
|
680 |
||
2509 | 681 |
processGlobalIDATChunk |
682 |
interlaceMode == 0 ifTrue: [ |
|
683 |
^ self processNonInterlacedGlobalDATA |
|
684 |
]. |
|
685 |
^ self processInterlacedGlobalDATA |
|
686 |
! |
|
687 |
||
3900 | 688 |
processHISTChunkLen:len |
689 |
"/ ignored |
|
690 |
"/ inStream nextBytes:len. |
|
691 |
||
692 |
inStream skip:len. |
|
693 |
^ true |
|
694 |
||
695 |
"Created: / 17-02-2017 / 11:31:36 / cg" |
|
696 |
! |
|
697 |
||
3762 | 698 |
processICCPChunkLen:len |
699 |
"/ ignored |
|
700 |
"/ inStream nextBytes:len. |
|
701 |
||
702 |
inStream skip:len. |
|
703 |
^ true |
|
704 |
||
705 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
706 |
! |
|
707 |
||
308 | 708 |
processIDATChunkLen:len |
1671 | 709 |
interlaceMode == 0 ifTrue: [ |
710 |
^ self processNonInterlacedDATA:len |
|
711 |
]. |
|
712 |
^ self processInterlacedDATA:len |
|
713 |
! |
|
714 |
||
3762 | 715 |
processIHDRChunkLen:len |
716 |
"/ ignored |
|
717 |
||
718 |
inStream skip:len. |
|
719 |
^ true |
|
720 |
||
721 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
722 |
! |
|
723 |
||
724 |
processITXTChunkLen:len |
|
725 |
"/ international (i.e.utf8) textual data. |
|
726 |
||
727 |
"/ currently ignored - not needed to display png images |
|
728 |
"/ |
|
729 |
"/ Keyword: 1-79 bytes (character string) |
|
730 |
"/ Null separator: 1 byte |
|
731 |
"/ Compression flag: 1 byte |
|
732 |
"/ Compression method: 1 byte |
|
733 |
"/ Language tag: 0 or more bytes (character string) |
|
734 |
"/ Null separator: 1 byte |
|
735 |
"/ Translated keyword: 0 or more bytes |
|
736 |
"/ Null separator: 1 byte |
|
737 |
"/ Text: 0 or more bytes |
|
738 |
||
739 |
"/ if needed, set processTextChunks to true somewhere... |
|
740 |
processTextChunks == true ifTrue:[ |
|
741 |
|chunkData keyword text i1 i2 i3 |
|
742 |
compressionFlag compressionMethod languageTag |
|
743 |
xlatedKeyword textBytes| |
|
744 |
||
745 |
chunkData := inStream nextBytes:len. |
|
746 |
i1 := chunkData indexOf:0. |
|
747 |
keyword := (chunkData copyTo:i1-1) asString. |
|
748 |
compressionFlag := chunkData at:i1+1. |
|
749 |
compressionMethod := chunkData at:i1+2. |
|
750 |
i2 := chunkData indexOf:0 startingAt:i1+3. |
|
751 |
languageTag := (chunkData copyFrom:i1+3 to:i2-1) asString. |
|
752 |
i3 := chunkData indexOf:0 startingAt:i2+1. |
|
753 |
xlatedKeyword := (chunkData copyFrom:i2+1 to:i3-1) asString. |
|
754 |
textBytes := chunkData copyFrom:i3+1. |
|
755 |
compressionFlag == 0 ifTrue:[ |
|
756 |
text := textBytes |
|
757 |
] ifFalse:[ |
|
758 |
compressionFlag == 1 ifTrue:[ |
|
759 |
"/ for now, only zlib compression is supported... |
|
3900 | 760 |
"/ ... no - not even that is |
761 |
Logger warning:'PNG: zlib compressed text currently unsupported'. |
|
3762 | 762 |
] ifFalse:[ |
3900 | 763 |
Logger warning:'PNG: unsupported text compression method'. |
3762 | 764 |
]. |
765 |
]. |
|
766 |
text := text utf8Decoded. |
|
767 |
self handleText:text keyword:keyword. |
|
768 |
] ifFalse:[ |
|
769 |
inStream skip:len. |
|
770 |
]. |
|
771 |
^ true |
|
772 |
||
3900 | 773 |
"Created: / 21-06-1996 / 21:15:58 / cg" |
774 |
"Modified: / 16-02-2017 / 19:51:21 / cg" |
|
3762 | 775 |
! |
776 |
||
1671 | 777 |
processInterlacedDATA:len |
4099 | 778 |
'PNGReader: skip unsupported interlaced data' infoPrintCR. |
779 |
||
308 | 780 |
inStream skip:len. |
781 |
^ true |
|
4099 | 782 |
|
783 |
"Modified: / 02-06-2018 / 06:40:22 / Claus Gittinger" |
|
1671 | 784 |
! |
308 | 785 |
|
2699 | 786 |
processInterlacedGlobalDATA |
3327 | 787 |
"adam7 interlace method" |
788 |
||
3612 | 789 |
| zlibReader filter |
790 |
bytesPerPass startingCol colIncrement rowIncrement |
|
791 |
startingRow |
|
792 |
temp "filtersSeen" |
|
793 |
cx "{ Class: SmallInteger }" |
|
794 |
sc "{ Class: SmallInteger }" |
|
795 |
cy "{ Class: SmallInteger }" |
|
796 |
startRow "{ Class: SmallInteger }" |
|
797 |
w "{ Class: SmallInteger }" |
|
798 |
h "{ Class: SmallInteger }" |
|
799 |
| |
|
2699 | 800 |
|
4099 | 801 |
Verbose == true ifTrue:[ |
802 |
'PNGReader: process interlaced global data' infoPrintCR. |
|
803 |
]. |
|
804 |
||
3327 | 805 |
interlaceMode == 1 ifFalse: [ |
806 |
self error:'unsupported interlaced mode'. |
|
807 |
^ self |
|
808 |
]. |
|
2699 | 809 |
|
810 |
"/ filtersSeen := Set new. |
|
811 |
||
812 |
startingCol := #(0 4 0 2 0 1 0 ). |
|
3327 | 813 |
startingRow := #(0 0 4 0 2 0 1 ). |
2699 | 814 |
colIncrement := #(8 8 4 4 2 2 1 ). |
815 |
rowIncrement := #(8 8 8 4 4 2 2 ). |
|
816 |
||
3821 | 817 |
globalDataChunk reset. |
818 |
zlibReader := ZipStream readOpenAsZipStreamOn:globalDataChunk suppressHeaderAndChecksum:false. |
|
2699 | 819 |
zlibReader binary. |
820 |
||
3612 | 821 |
h := height. |
822 |
w := width. |
|
2699 | 823 |
1 to: 7 do: [:pass | |
824 |
(self doPass: pass) ifTrue:[ |
|
825 |
cx := colIncrement at: pass. |
|
826 |
sc := startingCol at: pass. |
|
3327 | 827 |
cy := rowIncrement at: pass. |
3612 | 828 |
bytesPerPass := (((w - sc + cx - 1) // cx * depth) + 7) // 8. |
2699 | 829 |
prevScanline := ByteArray new: bytesPerPass. |
830 |
thisScanline := ByteArray new: bytesPerScanline. |
|
3612 | 831 |
startRow := startingRow at: pass. |
832 |
startRow to: h-1 by: cy do: [:y | |
|
3343 | 833 |
filter := zlibReader nextByte. |
2699 | 834 |
"/ filtersSeen add: filter. |
835 |
(filter isNil or: [(filter between: 0 and: 4) not]) |
|
836 |
ifTrue: [^ self]. |
|
837 |
zlibReader next: bytesPerPass into: thisScanline startingAt: 1. |
|
838 |
filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerPass ]. |
|
839 |
self copyPixels:y at:sc by:cx. |
|
840 |
temp := prevScanline. |
|
841 |
prevScanline := thisScanline. |
|
842 |
thisScanline := temp. |
|
843 |
] |
|
844 |
] |
|
845 |
]. |
|
846 |
zlibReader atEnd ifFalse:[self error:'Unexpected data']. |
|
2909 | 847 |
|
848 |
"Modified: / 03-05-2011 / 12:03:33 / cg" |
|
4099 | 849 |
"Modified: / 02-06-2018 / 06:43:26 / Claus Gittinger" |
2699 | 850 |
! |
851 |
||
3890 | 852 |
processNIVIChunkLen:len |
3889 | 853 |
"/ this cunk contains a labView vi. |
854 |
"/ ignored for now... |
|
3890 | 855 |
"/ ...to read it (using the expecco importer), |
856 |
"/ create a reader with a chunkHook and extract it there. |
|
3891 | 857 |
Logger info:('PNG: skip niVI chunk'). |
3890 | 858 |
inStream skip:len. |
859 |
^ true. |
|
3889 | 860 |
|
861 |
"Created: / 14-02-2017 / 08:57:29 / cg" |
|
3891 | 862 |
"Modified: / 14-02-2017 / 22:36:34 / cg" |
3889 | 863 |
! |
864 |
||
1671 | 865 |
processNonInterlacedDATA:len |
2701 | 866 |
self halt:'not used' |
867 |
"/ |
|
868 |
"/ | zlibReader filter temp prevScanline thisScanline bytesPerScanline filtersSeen| |
|
869 |
"/ |
|
870 |
"/ zlibReader := ZipStream readOpenAsZipStreamOn:inStream suppressHeaderAndChecksum:false. |
|
871 |
"/ "/ zlibReader := ZLibReadStream on:inStream from: 1 to:len. |
|
872 |
"/ |
|
873 |
"/ prevScanline := ByteArray new: self bytesPerRow. |
|
874 |
"/ thisScanline := ByteArray new: self bytesPerRow. |
|
875 |
"/ 0 to: height - 1 do:[:index | |
|
876 |
"/ filter := (zlibReader next: 1) first. |
|
877 |
"/ filtersSeen add: filter. |
|
878 |
"/ (filter isNil or: [(filter between: 0 and: 4) not]) ifTrue: [^self]. |
|
879 |
"/ thisScanline := zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
|
880 |
"/ self filterScanline: filter count: bytesPerScanline. |
|
881 |
"/ self copyPixels: index. |
|
882 |
"/ temp := prevScanline. |
|
883 |
"/ prevScanline := thisScanline. |
|
884 |
"/ thisScanline := temp |
|
885 |
"/ ] |
|
2509 | 886 |
! |
887 |
||
888 |
processNonInterlacedGlobalDATA |
|
3821 | 889 |
| "data n" zlibReader filter temp "filtersSeen" i| |
2696 | 890 |
|
4099 | 891 |
Verbose == true ifTrue:[ |
892 |
'PNGReader: process non-interlaced global data' infoPrintCR. |
|
893 |
]. |
|
894 |
||
2696 | 895 |
"/ filtersSeen := Set new. |
2695 | 896 |
|
897 |
"/ data := ByteArray new:(self bytesPerRow * height)+1000. |
|
898 |
"/ n := ZipStream uncompress: globalDataChunk into: data. |
|
899 |
"/ self halt. |
|
2509 | 900 |
|
3821 | 901 |
globalDataChunk reset. |
902 |
zlibReader := ZipStream readOpenAsZipStreamOn:globalDataChunk suppressHeaderAndChecksum:false. |
|
2696 | 903 |
zlibReader binary. |
2509 | 904 |
|
1671 | 905 |
prevScanline := ByteArray new: bytesPerScanline. |
906 |
thisScanline := ByteArray new: bytesPerScanline. |
|
3972 | 907 |
0 to: height-1 do:[:y | |
3343 | 908 |
filter := zlibReader nextByte. |
3893 | 909 |
zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
910 |
filter ~~ 0 ifTrue:[ |
|
911 |
"/ filtersSeen add: filter. |
|
912 |
(filter notNil and: [filter between: 0 and: 4]) ifFalse: [ |
|
913 |
'PNGReader: unsupported filter: ' infoPrint. filter infoPrintCR. |
|
914 |
^ self |
|
915 |
]. |
|
916 |
self filterScanline: filter count: bytesPerScanline |
|
2725 | 917 |
]. |
918 |
||
4099 | 919 |
forceRGB ifTrue:[ |
920 |
self copyPixels:y at:0 by:1. |
|
921 |
] ifFalse:[ |
|
922 |
i := y * bytesPerScanline. |
|
923 |
data replaceFrom:i+1 to:(i+bytesPerScanline) with:thisScanline startingAt:1. |
|
924 |
]. |
|
2695 | 925 |
temp := prevScanline. |
926 |
prevScanline := thisScanline. |
|
927 |
thisScanline := temp |
|
928 |
] |
|
3893 | 929 |
|
930 |
"Modified: / 16-02-2017 / 16:45:52 / cg" |
|
3972 | 931 |
"Modified (format): / 23-08-2017 / 17:05:01 / cg" |
4099 | 932 |
"Modified: / 02-06-2018 / 07:05:06 / Claus Gittinger" |
1671 | 933 |
! |
934 |
||
935 |
processPHYSChunkLen:len |
|
936 |
"physical pixel chunk - currently unhandled" |
|
937 |
||
2705 | 938 |
|pixelPerUnitX pixelPerUnitY unit| |
939 |
||
3554 | 940 |
pixelPerUnitX := inStream nextInt32MSB:true. |
941 |
pixelPerUnitY := inStream nextInt32MSB:true. |
|
2705 | 942 |
unit := inStream nextByte. |
943 |
||
2707 | 944 |
Verbose == true ifTrue:[ |
945 |
'PNGReader: PHYS chunk ignored:' infoPrintCR. |
|
946 |
'PNGReader: ppuX:' infoPrint. pixelPerUnitX infoPrintCR. |
|
947 |
'PNGReader: ppuY:' infoPrint. pixelPerUnitY infoPrintCR. |
|
948 |
'PNGReader: unit:' infoPrint. unit infoPrintCR. |
|
949 |
]. |
|
950 |
||
2705 | 951 |
^ true |
308 | 952 |
! |
953 |
||
954 |
processPLTEChunkLen:len |
|
1671 | 955 |
"read a color palette" |
956 |
||
2725 | 957 |
|n "{ Class: SmallInteger }" |
3972 | 958 |
redBytes greenBytes blueBytes |
959 |
triple| |
|
308 | 960 |
|
961 |
(len \\ 3) ~~ 0 ifTrue:[ |
|
962 |
'PNGReader: invalid size of PLTE chunk' infoPrintCR. |
|
963 |
^ false |
|
964 |
]. |
|
965 |
n := len // 3. |
|
966 |
redBytes := ByteArray new:n. |
|
967 |
greenBytes := ByteArray new:n. |
|
968 |
blueBytes := ByteArray new:n. |
|
969 |
||
3972 | 970 |
triple := ByteArray new:3. |
971 |
||
308 | 972 |
1 to:n do:[:i | |
3972 | 973 |
inStream next:3 into:triple startingAt:1. |
974 |
redBytes at:i put:(triple at:1). |
|
975 |
greenBytes at:i put:(triple at:2). |
|
976 |
blueBytes at:i put:(triple at:3) |
|
308 | 977 |
]. |
978 |
||
2725 | 979 |
colorMap := MappedPalette |
980 |
redVector:redBytes |
|
981 |
greenVector:greenBytes |
|
982 |
blueVector:blueBytes. |
|
983 |
||
308 | 984 |
^ true |
985 |
||
3972 | 986 |
"Created: / 21-06-1996 / 21:22:28 / cg" |
987 |
"Modified: / 23-08-2017 / 16:58:04 / cg" |
|
308 | 988 |
! |
989 |
||
990 |
processSBITChunkLen:len |
|
991 |
inStream skip:len. |
|
992 |
^ true |
|
993 |
||
994 |
"Created: 21.6.1996 / 21:13:09 / cg" |
|
995 |
! |
|
996 |
||
3900 | 997 |
processSPALChunkLen:len |
998 |
"/ ignored |
|
999 |
"/ inStream nextBytes:len. |
|
1000 |
||
1001 |
inStream skip:len. |
|
1002 |
^ true |
|
1003 |
||
1004 |
"Created: / 17-02-2017 / 11:31:40 / cg" |
|
1005 |
! |
|
1006 |
||
3762 | 1007 |
processTEXTChunkLen:len |
1008 |
"/ textual data in iso8859 coding. |
|
1009 |
||
1010 |
"/ currently ignored - not needed to display png images |
|
1011 |
"/ Keyword: 1-79 bytes (character string) |
|
1012 |
"/ Null separator: 1 byte |
|
1013 |
"/ Text: n bytes (character string) |
|
1014 |
||
1015 |
"/ if needed, set processTextChunks to true somewhere... |
|
1016 |
processTextChunks == true ifTrue:[ |
|
1017 |
|chunkData keyword text i| |
|
1018 |
||
1019 |
chunkData := inStream nextBytes:len. |
|
1020 |
i := chunkData indexOf:0. |
|
1021 |
keyword := (chunkData copyTo:i-1) asString. |
|
1022 |
text := (chunkData copyFrom:i+1) asString. |
|
1023 |
self handleText:text keyword:keyword. |
|
1024 |
] ifFalse:[ |
|
1025 |
inStream skip:len. |
|
1026 |
]. |
|
308 | 1027 |
^ true |
1028 |
||
1029 |
"Modified: 21.6.1996 / 21:15:27 / cg" |
|
1030 |
! |
|
1031 |
||
1032 |
processTIMEChunkLen:len |
|
1033 |
inStream skip:len. |
|
1034 |
^ true |
|
1035 |
||
1036 |
"Created: 21.6.1996 / 21:15:43 / cg" |
|
1037 |
"Modified: 21.6.1996 / 21:20:42 / cg" |
|
1038 |
! |
|
1039 |
||
2707 | 1040 |
processTRNSChunkLen:len |
3900 | 1041 |
paletteAlphaEntries := inStream nextBytes:len. |
3904 | 1042 |
|
1043 |
colorType == ColorTypeRGB ifTrue:[ |
|
1044 |
len ~~ 3 ifTrue:[ |
|
1045 |
Logger warning:'PNG: bad trns chunk size'. |
|
1046 |
paletteAlphaEntries := nil. |
|
1047 |
]. |
|
1048 |
^ true |
|
1049 |
]. |
|
1050 |
colorType == ColorTypePalette ifTrue:[ |
|
1051 |
len ~~ 1 ifTrue:[ |
|
1052 |
Logger warning:'PNG: bad trns chunk size'. |
|
1053 |
paletteAlphaEntries := nil. |
|
1054 |
]. |
|
1055 |
^ true |
|
1056 |
]. |
|
1057 |
colorType == ColorTypeGray ifTrue:[ |
|
1058 |
len ~~ 2 ifTrue:[ |
|
1059 |
Logger warning:'PNG: bad trns chunk size'. |
|
1060 |
paletteAlphaEntries := nil. |
|
1061 |
]. |
|
1062 |
^ true |
|
1063 |
]. |
|
1064 |
paletteAlphaEntries := nil. |
|
2707 | 1065 |
^ true |
3900 | 1066 |
|
3904 | 1067 |
"Modified (format): / 17-02-2017 / 16:57:27 / cg" |
2707 | 1068 |
! |
1069 |
||
308 | 1070 |
processZTXTChunkLen:len |
3762 | 1071 |
"/ compressed text |
1072 |
||
1073 |
"/ currently ignored - not needed to display png images |
|
308 | 1074 |
inStream skip:len. |
1075 |
^ true |
|
1076 |
||
1077 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
1078 |
! ! |
|
1079 |
||
3900 | 1080 |
!PNGReader methodsFor:'reading-private filtering'! |
2696 | 1081 |
|
2909 | 1082 |
filterAverage:count |
2699 | 1083 |
"Use the average of the pixel to the left and the pixel above as a predictor" |
2909 | 1084 |
|
1085 |
|delta| |
|
2699 | 1086 |
|
3609 | 1087 |
%{ |
1088 |
if (__isByteArray(__INST(thisScanline)) |
|
1089 |
&& __isByteArray(__INST(prevScanline)) |
|
1090 |
&& __isSmallInteger(__INST(depth)) |
|
1091 |
&& __isSmallInteger(count)) { |
|
1092 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
1093 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
1094 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
1095 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
1096 |
INT __count = __intVal(count); |
|
1097 |
INT __delta = __intVal(__INST(depth)) / 8; |
|
1098 |
int __i; |
|
1099 |
||
1100 |
if (__delta < 1) __delta = 1; |
|
1101 |
for (__i=0; __i<__delta; __i++) { |
|
1102 |
__thisScanline[__i] += (__prevScanline[__i] >> 1); |
|
1103 |
} |
|
1104 |
for (; __i < __count; __i++) { |
|
1105 |
__thisScanline[__i] += ((__prevScanline[__i] + __thisScanline[__i-__delta]) >> 1); |
|
1106 |
} |
|
1107 |
RETURN(self); |
|
1108 |
} |
|
1109 |
%}. |
|
1110 |
||
1111 |
delta := depth // 8 max:1. |
|
2909 | 1112 |
1 to:delta do:[:i | |
3384 | 1113 |
thisScanline at:i put:((thisScanline at:i) + ((prevScanline at:i) // 2) bitAnd:255) |
2909 | 1114 |
]. |
1115 |
delta + 1 to:count do:[:i | |
|
3384 | 1116 |
thisScanline at:i put:((thisScanline at:i) + (((prevScanline at:i) + (thisScanline at:i - delta)) // 2) bitAnd:255) |
2909 | 1117 |
] |
1118 |
||
1119 |
"Modified: / 03-05-2011 / 12:14:01 / cg" |
|
2699 | 1120 |
! |
1121 |
||
2696 | 1122 |
filterHorizontal:count |
1123 |
"use the pixel to the left as a predictor" |
|
1124 |
||
1125 |
|delta| |
|
1126 |
||
3608 | 1127 |
%{ |
1128 |
if (__isByteArray(__INST(thisScanline)) |
|
1129 |
&& __isByteArray(__INST(prevScanline)) |
|
1130 |
&& __isSmallInteger(__INST(depth)) |
|
1131 |
&& __isSmallInteger(count)) { |
|
1132 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
1133 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
1134 |
INT __count = __intVal(count); |
|
1135 |
INT __delta = __intVal(__INST(depth)) / 8; |
|
1136 |
int __i; |
|
1137 |
||
1138 |
if (__delta < 1) __delta = 1; |
|
1139 |
for (__i = __delta; __i < __count; __i++) { |
|
1140 |
__thisScanline[__i] = __thisScanline[__i] + __thisScanline[__i-__delta]; |
|
1141 |
} |
|
1142 |
RETURN(self); |
|
1143 |
} |
|
1144 |
%}. |
|
1145 |
||
1146 |
delta := depth // 8 max:1. |
|
2696 | 1147 |
delta+1 to:count do:[:i| |
1148 |
thisScanline at:i put:(((thisScanline at:i)+ (thisScanline at:i-delta)) bitAnd:255) |
|
1149 |
] |
|
4099 | 1150 |
|
1151 |
"Modified: / 02-06-2018 / 06:50:57 / Claus Gittinger" |
|
2696 | 1152 |
! |
1153 |
||
2699 | 1154 |
filterNone: count |
2909 | 1155 |
"/ no filter - scanline is as is |
1156 |
||
1157 |
"Modified: / 03-05-2011 / 12:14:20 / cg" |
|
2699 | 1158 |
! |
1159 |
||
2909 | 1160 |
filterPaeth:count |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1161 |
"Select one of (the pixel to the left, the pixel above and the pixel to above left) to |
2909 | 1162 |
predict the value of this pixel" |
1163 |
||
1164 |
|delta| |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1165 |
|
3607 | 1166 |
%{ |
1167 |
if (__isByteArray(__INST(thisScanline)) |
|
1168 |
&& __isByteArray(__INST(prevScanline)) |
|
1169 |
&& __isSmallInteger(__INST(depth)) |
|
1170 |
&& __isSmallInteger(count)) { |
|
1171 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
1172 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
1173 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
1174 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
1175 |
INT __count = __intVal(count); |
|
1176 |
INT __delta = __intVal(__INST(depth)) / 8; |
|
1177 |
int __i; |
|
1178 |
||
1179 |
if (__delta < 1) __delta = 1; |
|
1180 |
for (__i=0; __i<__delta; __i++) { |
|
1181 |
unsigned int __pix = __thisScanline[__i] + __prevScanline[__i]; |
|
1182 |
__thisScanline[__i] = __pix; |
|
1183 |
} |
|
1184 |
for (; __i < __count; __i++) { |
|
1185 |
int __pCenter = __thisScanline[__i]; |
|
1186 |
int __pLeft = __thisScanline[__i - __delta]; |
|
1187 |
int __pAbove = __prevScanline[__i]; |
|
1188 |
int __pAboveLeft = __prevScanline[__i - __delta]; |
|
1189 |
int __pa, __pb, __pc; |
|
1190 |
int __p; |
|
1191 |
||
1192 |
__pa = (__pAbove - __pAboveLeft); |
|
1193 |
__pa = __pa < 0 ? -__pa : __pa; |
|
1194 |
__pb = (__pLeft - __pAboveLeft); |
|
1195 |
__pb = __pb < 0 ? -__pb : __pb; |
|
1196 |
__pc = __pLeft + __pAbove - __pAboveLeft - __pAboveLeft; |
|
1197 |
__pc = __pc < 0 ? -__pc : __pc; |
|
1198 |
if ((__pa <= __pb) && (__pa <= __pc)) __p = __pLeft; |
|
1199 |
else if (__pb <= __pc) __p = __pAbove; |
|
1200 |
else __p = __pAboveLeft; |
|
1201 |
||
1202 |
__thisScanline[__i] = __pCenter + __p; |
|
1203 |
} |
|
1204 |
RETURN(self); |
|
1205 |
} |
|
1206 |
%}. |
|
1207 |
||
1208 |
delta := depth // 8 max:1. |
|
2909 | 1209 |
1 to:delta do:[:i | |
1210 |
thisScanline |
|
1211 |
at:i |
|
1212 |
put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255) |
|
1213 |
]. |
|
1214 |
delta + 1 to:count do:[:i | |
|
1215 |
thisScanline |
|
1216 |
at:i |
|
1217 |
put:(((thisScanline at:i) + (self |
|
1218 |
paethPredictLeft:(thisScanline at:i - delta) |
|
1219 |
above:(prevScanline at:i) |
|
1220 |
aboveLeft:(prevScanline at:i - delta))) |
|
1221 |
bitAnd:255) |
|
1222 |
] |
|
1223 |
||
1224 |
"Modified: / 03-05-2011 / 12:14:43 / cg" |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1225 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1226 |
|
2696 | 1227 |
filterScanline:filterType count:count |
3612 | 1228 |
filterType == 0 ifTrue:[^ self]. |
1229 |
||
2696 | 1230 |
self |
3612 | 1231 |
perform:(#(filterHorizontal: filterVertical: filterAverage: filterPaeth:) at:filterType) |
2696 | 1232 |
with:count |
2909 | 1233 |
|
1234 |
"Modified: / 03-05-2011 / 12:13:31 / cg" |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1235 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1236 |
|
2909 | 1237 |
filterVertical:count |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1238 |
"Use the pixel above as a predictor" |
2909 | 1239 |
|
3608 | 1240 |
%{ |
1241 |
if (__isByteArray(__INST(thisScanline)) |
|
1242 |
&& __isByteArray(__INST(prevScanline)) |
|
1243 |
&& __isSmallInteger(count)) { |
|
1244 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
1245 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
1246 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
1247 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
1248 |
INT __count = __intVal(count); |
|
1249 |
int __i; |
|
1250 |
||
1251 |
for (__i=0; __i<__count; __i++) { |
|
1252 |
unsigned int __pix = __thisScanline[__i] + __prevScanline[__i]; |
|
1253 |
__thisScanline[__i] = __pix; |
|
1254 |
} |
|
1255 |
RETURN(self); |
|
1256 |
} |
|
1257 |
%}. |
|
1258 |
||
2909 | 1259 |
1 to:count do:[:i | |
1260 |
thisScanline |
|
1261 |
at:i |
|
1262 |
put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255) |
|
1263 |
] |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1264 |
|
2909 | 1265 |
"Modified: / 03-05-2011 / 12:14:34 / cg" |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1266 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1267 |
|
3607 | 1268 |
paethPredictLeft: l above: a aboveLeft: al |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1269 |
"Predicts the value of a pixel based on nearby pixels, based on Paeth (GG II, 1991)" |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1270 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1271 |
| pa pb pc | |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1272 |
|
3607 | 1273 |
pa := a > al ifTrue: [a - al] ifFalse: [al - a]. |
1274 |
pb := l > al ifTrue: [l - al] ifFalse: [al - l]. |
|
1275 |
pc := l + a - al - al. |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1276 |
pc < 0 ifTrue: [ |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1277 |
pc := pc * -1 |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1278 |
]. |
3607 | 1279 |
((pa <= pb) and: [pa <= pc]) ifTrue: [^ l]. |
1280 |
(pb <= pc) ifTrue: [^ a]. |
|
1281 |
^ al |
|
2696 | 1282 |
! ! |
1283 |
||
3900 | 1284 |
!PNGReader methodsFor:'reading-private pixel copy'! |
2699 | 1285 |
|
2909 | 1286 |
copyPixels:y at:startX by:incX |
2704 | 1287 |
"Handle interlaced pixels of supported colorTypes" |
2909 | 1288 |
|
1289 |
|s| |
|
2699 | 1290 |
|
3598 | 1291 |
"/ per color type copy methods |
1292 |
s := #( |
|
3900 | 1293 |
#copyPixelsGray:at:by: "/ 0 = ColorTypeGray |
2909 | 1294 |
nil |
3900 | 1295 |
#copyPixelsRGB:at:by: "/ 2 = ColorTypeRGB |
1296 |
#copyPixelsIndexed:at:by: "/ 3 = ColorTypePalette |
|
1297 |
#copyPixelsGrayAlpha:at:by: "/ 4 = ColorTypeGrayAlpha |
|
2909 | 1298 |
nil |
3900 | 1299 |
#copyPixelsRGBA:at:by: "/ 6 = ColorTypeRGBA |
2909 | 1300 |
) at:colorType + 1. |
3437 | 1301 |
self perform:s with:y with:startX with:incX |
1848 | 1302 |
|
2909 | 1303 |
"Modified: / 03-05-2011 / 12:02:45 / cg" |
3900 | 1304 |
"Modified (comment): / 16-02-2017 / 19:42:47 / cg" |
3327 | 1305 |
! |
1306 |
||
3598 | 1307 |
copyPixelsGray:y at:startX by:incX |
1308 |
"Handle interlaced pixels of supported colorTypes. |
|
1309 |
Untested code - please verify" |
|
1310 |
||
3611 | 1311 |
|srcIndex "{ Class: SmallInteger }" |
1312 |
srcMask "{ Class: SmallInteger }" |
|
1313 |
nPixels "{ Class: SmallInteger }" |
|
1314 |
dstIndex "{ Class: SmallInteger }" |
|
1315 |
dstMask "{ Class: SmallInteger }" |
|
1316 |
x "{ Class: SmallInteger }" |
|
1317 |
bits "{ Class: SmallInteger }" |
|
1318 |
bitMask "{ Class: SmallInteger }" |
|
1319 |
rS "{ Class: SmallInteger }" |
|
1320 |
lS "{ Class: SmallInteger }" |
|
1321 |
rowIndex "{ Class: SmallInteger }"| |
|
3598 | 1322 |
|
1323 |
nPixels := width // incX. |
|
1324 |
srcIndex := 0. srcMask := 0. x := startX. |
|
1325 |
||
1326 |
depth == 1 ifTrue:[ |
|
1327 |
dstIndex := (y * bytesPerScanline) + (startX // 8) + 1. |
|
1328 |
dstMask := 16r80 >> (x \\ 8). |
|
1329 |
||
1330 |
1 to:nPixels do:[:cnt | |
|
1331 |
srcMask == 0 ifTrue:[ |
|
1332 |
srcMask := 16r80. |
|
1333 |
srcIndex := srcIndex + 1. |
|
1334 |
bits := thisScanline at:srcIndex. |
|
1335 |
]. |
|
1336 |
bitMask := (bits bitAnd:srcMask) == 0 ifTrue:[0] ifFalse:[dstMask]. |
|
1337 |
bitMask ~~ 0 ifTrue:[ |
|
1338 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
1339 |
]. |
|
1340 |
x := x + incX. |
|
1341 |
dstMask := dstMask >> incX. |
|
1342 |
dstMask == 0 ifTrue:[ |
|
1343 |
dstIndex := dstIndex + 1. |
|
1344 |
dstMask := 16r80 >> (x \\ 8). |
|
1345 |
]. |
|
1346 |
srcMask := srcMask bitShift:-1. |
|
1347 |
]. |
|
1348 |
^ self. |
|
1349 |
]. |
|
1350 |
||
1351 |
depth == 2 ifTrue:[ |
|
1352 |
dstIndex := (y * bytesPerScanline) + (startX // 4) + 1. |
|
1353 |
lS := 6 - ((x \\ 4) * 2). |
|
1354 |
rS := -1. |
|
1355 |
1 to:nPixels do:[:cnt | |
|
1356 |
rS < 0 ifTrue:[ |
|
1357 |
srcIndex := srcIndex + 1. |
|
1358 |
bits := thisScanline at:srcIndex. |
|
1359 |
rS := 6. |
|
1360 |
]. |
|
1361 |
bitMask := ((bits >> rS) bitAnd:2r11). |
|
1362 |
bitMask ~~ 0 ifTrue:[ |
|
1363 |
bitMask := bitMask << lS. |
|
1364 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
1365 |
]. |
|
1366 |
lS := lS - (incX * 2). |
|
1367 |
lS < 0 ifTrue:[ |
|
1368 |
lS <= -8 ifTrue:[ |
|
1369 |
dstIndex := dstIndex + 2. |
|
1370 |
lS := lS + 8 + 8. |
|
1371 |
] ifFalse:[ |
|
1372 |
dstIndex := dstIndex + 1. |
|
1373 |
lS := lS + 8. |
|
1374 |
]. |
|
1375 |
]. |
|
1376 |
rS := rS - 2. |
|
1377 |
]. |
|
1378 |
^ self. |
|
1379 |
]. |
|
1380 |
||
1381 |
depth == 4 ifTrue:[ |
|
1382 |
rowIndex := (y * bytesPerScanline) + 1. |
|
1383 |
dstIndex := rowIndex + (x // 2). |
|
1384 |
rS := -1. |
|
1385 |
1 to:nPixels do:[:cnt | |
|
1386 |
rS < 0 ifTrue:[ |
|
1387 |
srcIndex := srcIndex + 1. |
|
1388 |
bits := thisScanline at:srcIndex. |
|
1389 |
rS := 4. |
|
1390 |
]. |
|
1391 |
bitMask := ((bits >> rS) bitAnd:2r1111). |
|
1392 |
x even ifTrue:[ |
|
1393 |
bitMask := bitMask bitShift:4. |
|
1394 |
]. |
|
1395 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
1396 |
x := x + incX. |
|
1397 |
dstIndex := rowIndex + (x // 2). |
|
1398 |
rS := rS - 4. |
|
1399 |
]. |
|
1400 |
^ self. |
|
1401 |
]. |
|
1402 |
||
1403 |
depth == 8 ifTrue:[ |
|
1404 |
srcIndex := 1. |
|
1405 |
dstIndex := (y * bytesPerScanline) + (startX) + 1. |
|
1406 |
||
1407 |
1 to:nPixels do:[:n | |
|
1408 |
data at:dstIndex put:(thisScanline at:srcIndex). |
|
1409 |
srcIndex := srcIndex + 1. |
|
1410 |
dstIndex := dstIndex + incX |
|
1411 |
]. |
|
1412 |
^ self. |
|
1413 |
]. |
|
1414 |
||
1415 |
depth == 16 ifTrue:[ |
|
1416 |
srcIndex := 1. |
|
1417 |
dstIndex := (y * bytesPerScanline) + (startX*2) + 1. |
|
1418 |
||
1419 |
1 to:nPixels do:[:n | |
|
3601 | 1420 |
data unsignedInt16At:dstIndex put:(thisScanline unsignedInt16At:srcIndex). |
3598 | 1421 |
srcIndex := srcIndex + 2. |
1422 |
dstIndex := dstIndex + (incX * 2) |
|
1423 |
]. |
|
1424 |
^ self. |
|
1425 |
]. |
|
1426 |
self error:'unsupported depth'. |
|
1427 |
! |
|
1428 |
||
4017 | 1429 |
copyPixelsGrayAlpha:y at:startX by:incX |
1430 |
"Handle interlaced pixels of supported colorTypes." |
|
1431 |
||
1432 |
|srcIndex "{ Class: SmallInteger }" |
|
1433 |
srcMask "{ Class: SmallInteger }" |
|
1434 |
nPixels "{ Class: SmallInteger }" |
|
1435 |
dstIndex "{ Class: SmallInteger }" |
|
1436 |
dstMask "{ Class: SmallInteger }" |
|
1437 |
x "{ Class: SmallInteger }" |
|
1438 |
bits "{ Class: SmallInteger }" |
|
1439 |
bitMask "{ Class: SmallInteger }" |
|
1440 |
rS "{ Class: SmallInteger }" |
|
1441 |
lS "{ Class: SmallInteger }" |
|
1442 |
rowIndex "{ Class: SmallInteger }" |
|
1443 |
pix alpha gray| |
|
1444 |
||
1445 |
nPixels := width // incX. |
|
1446 |
srcIndex := 0. srcMask := 0. x := startX. |
|
1447 |
||
1448 |
"/ depth == 1 ifTrue:[ |
|
1449 |
"/ dstIndex := (y * bytesPerScanline) + (startX // 8) + 1. |
|
1450 |
"/ dstMask := 16r80 >> (x \\ 8). |
|
1451 |
"/ |
|
1452 |
"/ 1 to:nPixels do:[:cnt | |
|
1453 |
"/ srcMask == 0 ifTrue:[ |
|
1454 |
"/ srcMask := 16r80. |
|
1455 |
"/ srcIndex := srcIndex + 1. |
|
1456 |
"/ bits := thisScanline at:srcIndex. |
|
1457 |
"/ ]. |
|
1458 |
"/ bitMask := (bits bitAnd:srcMask) == 0 ifTrue:[0] ifFalse:[dstMask]. |
|
1459 |
"/ bitMask ~~ 0 ifTrue:[ |
|
1460 |
"/ data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
1461 |
"/ ]. |
|
1462 |
"/ x := x + incX. |
|
1463 |
"/ dstMask := dstMask >> incX. |
|
1464 |
"/ dstMask == 0 ifTrue:[ |
|
1465 |
"/ dstIndex := dstIndex + 1. |
|
1466 |
"/ dstMask := 16r80 >> (x \\ 8). |
|
1467 |
"/ ]. |
|
1468 |
"/ srcMask := srcMask bitShift:-1. |
|
1469 |
"/ ]. |
|
1470 |
"/ ^ self. |
|
1471 |
"/ ]. |
|
1472 |
"/ |
|
1473 |
"/ depth == 2 ifTrue:[ |
|
1474 |
"/ dstIndex := (y * bytesPerScanline) + (startX // 4) + 1. |
|
1475 |
"/ lS := 6 - ((x \\ 4) * 2). |
|
1476 |
"/ rS := -1. |
|
1477 |
"/ 1 to:nPixels do:[:cnt | |
|
1478 |
"/ rS < 0 ifTrue:[ |
|
1479 |
"/ srcIndex := srcIndex + 1. |
|
1480 |
"/ bits := thisScanline at:srcIndex. |
|
1481 |
"/ rS := 6. |
|
1482 |
"/ ]. |
|
1483 |
"/ bitMask := ((bits >> rS) bitAnd:2r11). |
|
1484 |
"/ bitMask ~~ 0 ifTrue:[ |
|
1485 |
"/ bitMask := bitMask << lS. |
|
1486 |
"/ data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
1487 |
"/ ]. |
|
1488 |
"/ lS := lS - (incX * 2). |
|
1489 |
"/ lS < 0 ifTrue:[ |
|
1490 |
"/ lS <= -8 ifTrue:[ |
|
1491 |
"/ dstIndex := dstIndex + 2. |
|
1492 |
"/ lS := lS + 8 + 8. |
|
1493 |
"/ ] ifFalse:[ |
|
1494 |
"/ dstIndex := dstIndex + 1. |
|
1495 |
"/ lS := lS + 8. |
|
1496 |
"/ ]. |
|
1497 |
"/ ]. |
|
1498 |
"/ rS := rS - 2. |
|
1499 |
"/ ]. |
|
1500 |
"/ ^ self. |
|
1501 |
"/ ]. |
|
1502 |
"/ |
|
1503 |
"/ depth == 4 ifTrue:[ |
|
1504 |
"/ rowIndex := (y * bytesPerScanline) + 1. |
|
1505 |
"/ dstIndex := rowIndex + (x // 2). |
|
1506 |
"/ rS := -1. |
|
1507 |
"/ 1 to:nPixels do:[:cnt | |
|
1508 |
"/ rS < 0 ifTrue:[ |
|
1509 |
"/ srcIndex := srcIndex + 1. |
|
1510 |
"/ bits := thisScanline at:srcIndex. |
|
1511 |
"/ rS := 4. |
|
1512 |
"/ ]. |
|
1513 |
"/ bitMask := ((bits >> rS) bitAnd:2r1111). |
|
1514 |
"/ x even ifTrue:[ |
|
1515 |
"/ bitMask := bitMask bitShift:4. |
|
1516 |
"/ ]. |
|
1517 |
"/ data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
1518 |
"/ x := x + incX. |
|
1519 |
"/ dstIndex := rowIndex + (x // 2). |
|
1520 |
"/ rS := rS - 4. |
|
1521 |
"/ ]. |
|
1522 |
"/ ^ self. |
|
1523 |
"/ ]. |
|
1524 |
"/ |
|
1525 |
"/ depth == 8 ifTrue:[ |
|
1526 |
"/ srcIndex := 1. |
|
1527 |
"/ dstIndex := (y * bytesPerScanline) + (startX) + 1. |
|
1528 |
"/ |
|
1529 |
"/ 1 to:nPixels do:[:n | |
|
1530 |
"/ data at:dstIndex put:(thisScanline at:srcIndex). |
|
1531 |
"/ srcIndex := srcIndex + 1. |
|
1532 |
"/ dstIndex := dstIndex + incX |
|
1533 |
"/ ]. |
|
1534 |
"/ ^ self. |
|
1535 |
"/ ]. |
|
1536 |
||
1537 |
forceRGB ifTrue:[ |
|
1538 |
(depth == 16 and:[depthImage == 32]) ifTrue:[ |
|
1539 |
"/ converting gray8+alpha8 to r8+g8+b8+alpha8 |
|
1540 |
||
1541 |
srcIndex := 1. |
|
1542 |
dstIndex := (y * (width * 4)) + (startX*4) + 1. |
|
1543 |
||
1544 |
1 to:nPixels do:[:n | |
|
4099 | 1545 |
pix := thisScanline unsignedInt16At:srcIndex MSB:true. |
4017 | 1546 |
alpha := pix bitAnd:16rFF. |
1547 |
gray := (pix rightShift:8) bitAnd:16rFF. |
|
1548 |
"/ put r-g-b-a |
|
1549 |
data at:dstIndex put:gray. |
|
1550 |
data at:dstIndex+1 put:gray. |
|
1551 |
data at:dstIndex+2 put:gray. |
|
1552 |
data at:dstIndex+3 put:alpha. |
|
1553 |
||
1554 |
srcIndex := srcIndex + 2. |
|
1555 |
dstIndex := dstIndex + (incX * 4) |
|
1556 |
]. |
|
1557 |
^ self. |
|
1558 |
]. |
|
1559 |
(depth == 32 and:[depthImage == 32]) ifTrue:[ |
|
1560 |
"/ converting gray16+alpha16 to r8+g8+b8+alpha8 |
|
1561 |
||
1562 |
srcIndex := 1. |
|
1563 |
dstIndex := (y * (width * 4)) + (startX*4) + 1. |
|
1564 |
||
1565 |
1 to:nPixels do:[:n | |
|
4099 | 1566 |
pix := thisScanline unsignedInt32At:srcIndex MSB:true. |
4017 | 1567 |
|
1568 |
alpha := pix bitAnd:16rFFFF. |
|
1569 |
gray := (pix rightShift:16) bitAnd:16rFFFF. |
|
1570 |
"/ reduce to 8 bit |
|
1571 |
alpha := alpha rightShift:8. |
|
1572 |
gray := gray rightShift:8. |
|
1573 |
||
1574 |
"/ put r-g-b-a |
|
1575 |
data at:dstIndex put:gray. |
|
1576 |
data at:dstIndex+1 put:gray. |
|
1577 |
data at:dstIndex+2 put:gray. |
|
1578 |
data at:dstIndex+3 put:alpha. |
|
1579 |
||
1580 |
srcIndex := srcIndex + 4. |
|
1581 |
dstIndex := dstIndex + (incX * 4) |
|
1582 |
]. |
|
1583 |
^ self. |
|
1584 |
]. |
|
1585 |
]. |
|
1586 |
self error:'unsupported depth'. |
|
1587 |
||
1588 |
"Created: / 17-09-2017 / 14:20:24 / cg" |
|
1589 |
"Modified: / 17-09-2017 / 15:28:38 / cg" |
|
4099 | 1590 |
"Modified: / 02-06-2018 / 07:08:15 / Claus Gittinger" |
4017 | 1591 |
! |
1592 |
||
3598 | 1593 |
copyPixelsIndexed:y at:startX by:incX |
1594 |
self copyPixelsGray:y at:startX by:incX |
|
1595 |
! |
|
1596 |
||
1597 |
copyPixelsRGB:y at:startX by:incX |
|
1598 |
"Handle interlaced pixels of supported colorTypes. |
|
1599 |
Untested code - please verify" |
|
1600 |
||
3611 | 1601 |
|srcIndex "{ Class: SmallInteger }" |
1602 |
nPixels "{ Class: SmallInteger }" |
|
1603 |
dstIndex "{ Class: SmallInteger }" |
|
1604 |
dstInc "{ Class: SmallInteger }" |
|
1605 |
bpp "{ Class: SmallInteger }"| |
|
3598 | 1606 |
|
1607 |
bpp := bytesPerScanline // width. |
|
1608 |
||
1609 |
"/ 1 byte per r,g,b |
|
1610 |
srcIndex := 1. |
|
1611 |
dstIndex := (y * bytesPerScanline) + (startX * bpp) + 1. |
|
1612 |
dstInc := incX * bpp. |
|
1613 |
||
1614 |
nPixels := width // incX. |
|
1615 |
1 to:nPixels do:[:n | |
|
1616 |
data replaceFrom:dstIndex to:dstIndex+bpp-1 with:thisScanline startingAt:srcIndex. |
|
1617 |
srcIndex := srcIndex + bpp. |
|
1618 |
dstIndex := dstIndex + dstInc |
|
1619 |
]. |
|
1620 |
! |
|
1621 |
||
3327 | 1622 |
copyPixelsRGBA:y at:startX by:incX |
1623 |
"Handle interlaced pixels of supported colorTypes. |
|
1624 |
Untested code - please verify" |
|
1625 |
||
3611 | 1626 |
|srcIndex "{ Class: SmallInteger }" |
1627 |
nPixels "{ Class: SmallInteger }" |
|
1628 |
dstIndex "{ Class: SmallInteger }" |
|
1629 |
dstInc "{ Class: SmallInteger }"| |
|
3327 | 1630 |
|
1631 |
srcIndex := 1. |
|
3437 | 1632 |
dstIndex := (y * bytesPerScanline) + (startX * 4) + 1. |
1633 |
dstInc := incX * 4. |
|
3327 | 1634 |
|
1635 |
nPixels := thisScanline size. |
|
1636 |
[srcIndex < nPixels] whileTrue:[ |
|
3437 | 1637 |
data replaceFrom:dstIndex to:dstIndex+3 with:thisScanline startingAt:srcIndex. |
3327 | 1638 |
srcIndex := srcIndex + 4. |
3437 | 1639 |
dstIndex := dstIndex + dstInc |
3327 | 1640 |
]. |
2699 | 1641 |
! ! |
1642 |
||
3900 | 1643 |
!PNGReader methodsFor:'writing'! |
2683 | 1644 |
|
1645 |
save:image onStream:aStream |
|
1646 |
"save image in PNG-file-format onto aStream" |
|
1647 |
||
1648 |
outStream := aStream. |
|
1649 |
outStream binary. |
|
1650 |
||
2703 | 1651 |
byteOrder := #msb. |
4004 | 1652 |
|
2683 | 1653 |
width := image width. |
1654 |
height := image height. |
|
1655 |
photometric := image photometric. |
|
1656 |
samplesPerPixel := image samplesPerPixel. |
|
1657 |
bitsPerSample := image bitsPerSample. |
|
1658 |
colorMap := image colorMap. |
|
1659 |
data := image bits. |
|
3893 | 1660 |
mask := image mask. |
1661 |
depth := self bitsPerPixel. |
|
3900 | 1662 |
|
3909 | 1663 |
outStream nextPutAll:(self class pngHeader). |
3893 | 1664 |
|
3900 | 1665 |
image mask notNil ifTrue:[ |
3972 | 1666 |
((photometric ~~ #rgb) and:[(photometric ~~ #rgba) and:[(photometric ~~ #argb)]]) ifTrue: [ |
3949 | 1667 |
self determinePaletteIndexForMaskedPixels |
3904 | 1668 |
] ifFalse:[ |
1669 |
photometric := #rgba. |
|
1670 |
]. |
|
3900 | 1671 |
]. |
1672 |
||
2703 | 1673 |
self writeIHDRChunk. |
1674 |
photometric == #palette ifTrue: [self writePaletteChunk]. |
|
3900 | 1675 |
paletteIndexForMaskedPixels notNil ifTrue: [self writeTRNSChunk]. |
2703 | 1676 |
self writeImageDataChunk. |
2683 | 1677 |
self writeEndChunk |
3893 | 1678 |
|
3972 | 1679 |
"Modified: / 23-08-2017 / 16:25:00 / cg" |
4004 | 1680 |
"Modified (format): / 31-08-2017 / 17:26:28 / cg" |
3900 | 1681 |
! ! |
1682 |
||
1683 |
!PNGReader methodsFor:'writing-private'! |
|
1684 |
||
1685 |
determinePaletteIndexForMaskedPixels |
|
1686 |
"/ if all masked pixels are 0, and 0 is not used elsewhere in the image, |
|
1687 |
"/ write it as such. |
|
1688 |
"/ Otherwise, find an unallocated palette index, and assign masked pixels to it. |
|
1689 |
||
1690 |
|usedPixels freePixels pixelIdx| |
|
1691 |
||
1692 |
colorMap size < 256 ifTrue:[ |
|
1693 |
paletteIndexForMaskedPixels := colorMap size. |
|
1694 |
] ifFalse:[ |
|
1695 |
usedPixels := data usedValues. |
|
1696 |
usedPixels size == 256 ifTrue:[ |
|
1697 |
self error:'cannot represent image (no palette slot for mask)' |
|
1698 |
]. |
|
1699 |
freePixels := (0 to:255) asSet removeAllFoundIn:usedPixels. |
|
1700 |
paletteIndexForMaskedPixels := freePixels first. |
|
1701 |
]. |
|
1702 |
||
4181 | 1703 |
depth == 8 ifTrue:[ |
1704 |
"/ rewrite data: wherever masked, change pixel to paletteIndexForMaskedPixels |
|
1705 |
data := data copy. |
|
1706 |
pixelIdx := 1. |
|
1707 |
0 to:height-1 do:[:y | |
|
1708 |
0 to:width-1 do:[:x | |
|
1709 |
(mask pixelAtX:x y:y) == 0 ifTrue:[ |
|
1710 |
data at:pixelIdx put:paletteIndexForMaskedPixels. |
|
1711 |
]. |
|
1712 |
pixelIdx := pixelIdx + 1 |
|
1713 |
] |
|
1714 |
]. |
|
3900 | 1715 |
]. |
1716 |
||
1717 |
" |
|
1718 |
PNGReader save:(ToolbarIconLibrary systemBrowserIcon) onFile:'/tmp/icon.png' |
|
1719 |
ImageEditor openOnFile:'/tmp/icon.png' |
|
1720 |
" |
|
1721 |
||
1722 |
"Created: / 16-02-2017 / 19:59:39 / cg" |
|
1723 |
"Modified: / 17-02-2017 / 09:25:57 / cg" |
|
4181 | 1724 |
"Modified: / 24-09-2018 / 10:39:34 / Claus Gittinger" |
3900 | 1725 |
! ! |
1726 |
||
1727 |
!PNGReader methodsFor:'writing-private chunks'! |
|
2683 | 1728 |
|
3949 | 1729 |
determineColorTypeAndHasMask |
4004 | 1730 |
"sets colorType as side effect; |
1731 |
returns boolean if a mask is present" |
|
1732 |
||
3949 | 1733 |
((photometric == #whiteIs0) or:[ (photometric == #blackIs0)]) ifTrue:[ |
1734 |
colorType := ColorTypeGray. |
|
1735 |
^ (samplesPerPixel > 1) |
|
1736 |
]. |
|
1737 |
(photometric == #rgb) ifTrue:[ |
|
1738 |
colorType := ColorTypeRGB. |
|
1739 |
^ (samplesPerPixel > 3) |
|
1740 |
]. |
|
1741 |
(photometric == #rgba) ifTrue:[ |
|
1742 |
colorType := ColorTypeRGBAlpha. |
|
1743 |
^ (samplesPerPixel > 3) |
|
1744 |
]. |
|
1745 |
(photometric == #palette) ifTrue:[ |
|
1746 |
colorType := ColorTypePalette. |
|
1747 |
^ (samplesPerPixel > 1) |
|
1748 |
]. |
|
1749 |
self error:'unhandled photometric: ',photometric asString. |
|
1750 |
^ false. |
|
1751 |
||
1752 |
"Created: / 11-04-2017 / 12:39:30 / cg" |
|
4004 | 1753 |
"Modified (comment): / 31-08-2017 / 17:24:31 / cg" |
3949 | 1754 |
! |
1755 |
||
2703 | 1756 |
writeChunk:chunkTypeChars size:len with:aBlock |
2708 | 1757 |
|crc realOutStream chunkBytes| |
1758 |
||
1759 |
realOutStream := outStream. |
|
1760 |
outStream := WriteStream on:(ByteArray new:len). |
|
1761 |
||
2703 | 1762 |
outStream nextPutBytes:4 from:chunkTypeChars startingAt:1. |
1763 |
aBlock value. |
|
2708 | 1764 |
|
1765 |
chunkBytes := outStream contents. |
|
1766 |
crc := CRC32Stream hashValueOf:chunkBytes. |
|
1767 |
||
1768 |
outStream := realOutStream. |
|
1769 |
||
3554 | 1770 |
realOutStream nextPutInt32:len MSB:true. |
2708 | 1771 |
realOutStream nextPutAll:chunkBytes. |
3554 | 1772 |
realOutStream nextPutInt32:crc MSB:true. |
2703 | 1773 |
! |
2683 | 1774 |
|
2703 | 1775 |
writeEndChunk |
1776 |
self |
|
1777 |
writeChunk: 'IEND' |
|
1778 |
size: 0 |
|
1779 |
with:[ ] |
|
2683 | 1780 |
! |
1781 |
||
2703 | 1782 |
writeIHDRChunk |
4005 | 1783 |
|hasMask bitsPerChannel| |
3893 | 1784 |
|
3949 | 1785 |
hasMask := self determineColorTypeAndHasMask. |
3893 | 1786 |
hasMask ifTrue:[ |
3900 | 1787 |
"/ make it grayAlpha / rgba |
1788 |
((colorType == ColorTypeGray) or:[colorType == ColorTypeGray]) ifTrue:[ |
|
1789 |
colorType := colorType bitOr:4. "/ +alpha |
|
1790 |
]. |
|
3893 | 1791 |
]. |
4005 | 1792 |
|
1793 |
"bitsperchannel" |
|
1794 |
depth <= 8 ifTrue:[ |
|
1795 |
bitsPerChannel := depth |
|
1796 |
] ifFalse:[ |
|
4010 | 1797 |
(colorType == ColorTypeRGB or:[colorType == ColorTypeRGBAlpha]) ifTrue:[ |
4005 | 1798 |
"/ all channels must have the same nr of bits |
1799 |
self assert:(bitsPerSample asSet size == 1). |
|
1800 |
bitsPerChannel := bitsPerSample first |
|
1801 |
] ifFalse:[ |
|
1802 |
bitsPerChannel := depth |
|
1803 |
] |
|
1804 |
]. |
|
3893 | 1805 |
|
2683 | 1806 |
self |
1807 |
writeChunk: 'IHDR' |
|
3949 | 1808 |
size: (4+4+5) |
2683 | 1809 |
with:[ |
4004 | 1810 |
outStream nextPutInt32:width MSB:true. |
1811 |
outStream nextPutInt32:height MSB:true. |
|
4005 | 1812 |
outStream nextPut: bitsPerChannel. |
2703 | 1813 |
outStream nextPut: colorType. |
2683 | 1814 |
outStream nextPut: 0. "Compression" |
1815 |
outStream nextPut: 0. "Filter method" |
|
1816 |
outStream nextPut: 0 "Non-interlaced" |
|
1817 |
] |
|
3893 | 1818 |
|
4010 | 1819 |
"Modified: / 04-09-2017 / 18:34:15 / cg" |
2683 | 1820 |
! |
1821 |
||
2703 | 1822 |
writeImageDataChunk |
3893 | 1823 |
|compressedByteStream compressedBytes zlibWriter idx |
3935 | 1824 |
row32 bytesPerPixel| |
2703 | 1825 |
|
1826 |
bytesPerScanline := self bytesPerRow. |
|
3821 | 1827 |
|
1828 |
compressedByteStream := WriteStream on:(ByteArray new:(bytesPerScanline*height)). |
|
1829 |
zlibWriter := ZipStream writeOpenAsZipStreamOn:compressedByteStream suppressHeaderAndChecksum:false. |
|
1830 |
zlibWriter binary. |
|
2703 | 1831 |
|
3900 | 1832 |
(mask notNil and:[photometric ~~ #palette]) ifTrue:[ |
3935 | 1833 |
(depth == 32) ifTrue:[ |
1834 |
bytesPerPixel := 4 |
|
1835 |
] ifFalse:[ |
|
1836 |
"/ for now - only support depth24 + mask |
|
1837 |
self assert:(depth == 24). |
|
1838 |
bytesPerPixel := 3. |
|
1839 |
]. |
|
3893 | 1840 |
|
3935 | 1841 |
"/ on-the-fly place mask into the alpha channel. |
3893 | 1842 |
row32 := ByteArray new:(4 * width). |
3935 | 1843 |
self assert:(bytesPerScanline == (bytesPerPixel * width)). |
1844 |
||
3893 | 1845 |
idx := 1. |
1846 |
0 to:height-1 do:[:y | |
|
1847 |
|dstIdx maskRow| |
|
2708 | 1848 |
|
3893 | 1849 |
"/ expand rgb to rgba |
1850 |
maskRow := mask rowAt:y. |
|
3935 | 1851 |
|
3893 | 1852 |
dstIdx := 1. |
1853 |
1 to:width do:[:x | |
|
1854 |
row32 at:dstIdx put:(data at:idx). |
|
1855 |
row32 at:dstIdx+1 put:(data at:idx+1). |
|
1856 |
row32 at:dstIdx+2 put:(data at:idx+2). |
|
1857 |
row32 at:dstIdx+3 put:((maskRow at:x) == 0 ifTrue:[0] ifFalse:[16rFF]). |
|
3935 | 1858 |
idx := idx + bytesPerPixel. |
3893 | 1859 |
dstIdx := dstIdx + 4. |
1860 |
]. |
|
3935 | 1861 |
|
3893 | 1862 |
zlibWriter nextPutAll:#[0]. "/ no filter |
1863 |
zlibWriter nextPutAll:row32. |
|
1864 |
]. |
|
1865 |
] ifFalse:[ |
|
1866 |
idx := 1. |
|
1867 |
0 to:height-1 do:[:y | |
|
1868 |
|row| |
|
1869 |
||
1870 |
row := data copyFrom:idx to:(idx+bytesPerScanline-1). |
|
1871 |
zlibWriter nextPutAll:#[0]. "/ no filter |
|
1872 |
zlibWriter nextPutAll:row. |
|
1873 |
idx := idx + bytesPerScanline. |
|
1874 |
]. |
|
2703 | 1875 |
]. |
2708 | 1876 |
|
1877 |
zlibWriter close. |
|
1878 |
compressedBytes := compressedByteStream contents. |
|
1879 |
||
1880 |
self |
|
1881 |
writeChunk: 'IDAT' |
|
1882 |
size:(compressedBytes size) |
|
1883 |
with:[ |
|
1884 |
outStream nextPutAll:compressedBytes |
|
1885 |
] |
|
3893 | 1886 |
|
3935 | 1887 |
"Modified: / 25-02-2017 / 14:27:34 / cg" |
3949 | 1888 |
"Modified (format): / 11-04-2017 / 12:41:22 / cg" |
2703 | 1889 |
! |
1890 |
||
2683 | 1891 |
writePaletteChunk |
1892 |
self |
|
1893 |
writeChunk: 'PLTE' |
|
1894 |
size: 3 * colorMap size |
|
1895 |
with:[ |
|
2703 | 1896 |
colorMap do:[:color | |
1897 |
outStream |
|
1898 |
nextPut: color redByte; |
|
1899 |
nextPut: color greenByte; |
|
1900 |
nextPut: color blueByte |
|
2683 | 1901 |
] |
1902 |
] |
|
3900 | 1903 |
! |
1904 |
||
1905 |
writeTRNSChunk |
|
1906 |
self |
|
1907 |
writeChunk: 'tRNS' |
|
1908 |
size: 1 |
|
1909 |
with:[ |
|
1910 |
outStream nextPut: paletteIndexForMaskedPixels |
|
1911 |
] |
|
1912 |
||
1913 |
"Created: / 16-02-2017 / 19:58:06 / cg" |
|
2683 | 1914 |
! ! |
1915 |
||
398 | 1916 |
!PNGReader class methodsFor:'documentation'! |
308 | 1917 |
|
1918 |
version |
|
3554 | 1919 |
^ '$Header$' |
2909 | 1920 |
! |
1921 |
||
1922 |
version_CVS |
|
3554 | 1923 |
^ '$Header$' |
308 | 1924 |
! ! |
1671 | 1925 |
|
3100 | 1926 |
|
308 | 1927 |
PNGReader initialize! |