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