author | Claus Gittinger <cg@exept.de> |
Mon, 18 Sep 2017 09:09:00 +0200 | |
changeset 4015 | 7f7b255c15c2 |
parent 4010 | 3df2676d3e8a |
child 4017 | 0fea2b021ca6 |
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 |
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. |
|
4005 | 392 |
depth := bitsPerChannel. "/ for now - might be changed by setColorType. |
3900 | 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" |
|
4005 | 414 |
"Modified (comment): / 31-08-2017 / 18:03:41 / cg" |
3900 | 415 |
! |
416 |
||
417 |
handleText:text keyword:keyword |
|
418 |
"/ things like exif data etc. |
|
419 |
"/ self halt. |
|
420 |
||
421 |
"/Standard keywords for text chunks: |
|
422 |
"/ |
|
423 |
"/ Title Short (one line) title or caption for image |
|
424 |
"/ Author Name of image's creator |
|
425 |
"/ Description Description of image (possibly long) |
|
426 |
"/ Copyright Copyright notice |
|
427 |
"/ Creation Time Time of original image creation |
|
428 |
"/ Software Software used to create the image |
|
429 |
"/ Disclaimer Legal disclaimer |
|
430 |
"/ Warning Warning of nature of content |
|
431 |
"/ Source Device used to create the image |
|
432 |
"/ Comment Miscellaneous comment; conversion from |
|
433 |
"/ GIF comment |
|
434 |
! |
|
435 |
||
436 |
setColorType:colorType |
|
4005 | 437 |
colorType == ColorTypeGray "0" ifTrue:[ |
3900 | 438 |
photometric := #blackIs0. |
439 |
samplesPerPixel := 1. |
|
440 |
bitsPerSample := Array with:bitsPerChannel. |
|
441 |
depth := bitsPerChannel. |
|
442 |
^ true. |
|
443 |
]. |
|
444 |
||
4005 | 445 |
colorType == ColorTypeRGB "2" ifTrue:[ |
3900 | 446 |
photometric := #rgb. |
447 |
samplesPerPixel := 3. |
|
448 |
bitsPerSample := Array with:bitsPerChannel with:bitsPerChannel with:bitsPerChannel. |
|
449 |
depth := bitsPerChannel * 3. |
|
4005 | 450 |
|
451 |
bitsPerChannel < 8 ifTrue:[ |
|
452 |
bitsPerChannel == 5 ifTrue:[ |
|
453 |
depth := 16 |
|
454 |
] ifFalse:[ |
|
455 |
('PNGReader: unsupported bitsPerChannel:%1 for colorType rgb' bindWith:bitsPerChannel) infoPrintCR. |
|
456 |
]. |
|
457 |
]. |
|
3900 | 458 |
^ true. |
459 |
]. |
|
460 |
||
4005 | 461 |
colorType == ColorTypePalette "3" ifTrue:[ |
3900 | 462 |
bitsPerChannel == 16 ifTrue:[ |
4005 | 463 |
'PNGReader: invalid unsupported bitsPerChannel:%1 for colorType palette' infoPrintCR. |
3900 | 464 |
^ false. |
465 |
]. |
|
466 |
photometric := #palette. |
|
467 |
samplesPerPixel := 1. |
|
468 |
bitsPerSample := Array with:bitsPerChannel. |
|
469 |
depth := bitsPerChannel. |
|
470 |
^ true. |
|
471 |
]. |
|
472 |
||
4005 | 473 |
colorType == ColorTypeGrayAlpha "4" ifTrue:[ |
3900 | 474 |
bitsPerChannel < 8 ifTrue:[ |
4005 | 475 |
'PNGReader: unsupported colorType/depth combination' infoPrintCR. |
3900 | 476 |
^ false. |
477 |
]. |
|
478 |
photometric := #blackIs0. |
|
479 |
samplesPerPixel := 2. |
|
480 |
bitsPerSample := Array with:bitsPerChannel with:bitsPerChannel. |
|
481 |
depth := bitsPerChannel * 2. |
|
482 |
^ true. |
|
483 |
]. |
|
484 |
||
4005 | 485 |
colorType == ColorTypeRGBAlpha "6" ifTrue:[ |
3900 | 486 |
bitsPerChannel < 8 ifTrue:[ |
4005 | 487 |
'PNGReader: unsupported colorType/depth combination' infoPrintCR. |
3900 | 488 |
^ false. |
489 |
]. |
|
490 |
photometric := #rgba. |
|
491 |
samplesPerPixel := 4. |
|
492 |
bitsPerSample := Array with:bitsPerChannel with:bitsPerChannel with:bitsPerChannel with:bitsPerChannel. |
|
493 |
depth := bitsPerChannel * 4. |
|
494 |
^ true. |
|
495 |
]. |
|
496 |
||
4005 | 497 |
('PNGReader: unsupported or invalid colorType: ' , colorType printString , '.') infoPrintCR. |
3900 | 498 |
^ false |
499 |
||
4005 | 500 |
"Modified: / 31-08-2017 / 17:58:18 / cg" |
3900 | 501 |
! ! |
502 |
||
503 |
!PNGReader methodsFor:'reading-private chunks'! |
|
308 | 504 |
|
2699 | 505 |
doPass: pass |
506 |
"Certain interlace passes are skipped with certain small image dimensions" |
|
507 |
||
508 |
pass = 1 ifTrue: [ ^ true ]. |
|
509 |
((width = 1) and: [height = 1]) ifTrue: [ ^ false ]. |
|
510 |
pass = 2 ifTrue: [ ^ width >= 5 ]. |
|
511 |
pass = 3 ifTrue: [ ^ height >= 5 ]. |
|
512 |
pass = 4 ifTrue: [ ^ (width >=3 ) or: [height >= 5] ]. |
|
513 |
pass = 5 ifTrue: [ ^ height >=3 ]. |
|
514 |
pass = 6 ifTrue: [ ^ width >=2 ]. |
|
515 |
pass = 7 ifTrue: [ ^ height >=2 ]. |
|
516 |
||
517 |
self error:'invalid argument'. |
|
518 |
^ true |
|
519 |
! |
|
520 |
||
308 | 521 |
processBKGDChunkLen:len |
522 |
inStream skip:len. |
|
523 |
^ true |
|
524 |
||
525 |
"Created: 21.6.1996 / 21:15:49 / cg" |
|
526 |
! |
|
527 |
||
2707 | 528 |
processCHRMChunkLen:len |
529 |
"cHRM Primary chromaticities chunk - currently unhandled" |
|
530 |
||
531 |
|whitePointX whitePointY redX redY greenX greenY blueX blueY| |
|
532 |
||
3554 | 533 |
whitePointX := (inStream nextInt32MSB:true) / 100000.0. |
534 |
whitePointY := (inStream nextInt32MSB:true) / 100000.0. |
|
535 |
redX := (inStream nextInt32MSB:true) / 100000.0. |
|
536 |
redY := (inStream nextInt32MSB:true) / 100000.0. |
|
537 |
greenX := (inStream nextInt32MSB:true) / 100000.0. |
|
538 |
greenY := (inStream nextInt32MSB:true) / 100000.0. |
|
539 |
blueX := (inStream nextInt32MSB:true) / 100000.0. |
|
540 |
blueY := (inStream nextInt32MSB:true) / 100000.0. |
|
2707 | 541 |
|
542 |
Verbose == true ifTrue:[ |
|
543 |
'PNGReader: CHRM chunk ignored:' infoPrintCR. |
|
544 |
'PNGReader: whitePointX:' infoPrint. whitePointX infoPrintCR. |
|
545 |
'PNGReader: whitePointY:' infoPrint. whitePointX infoPrintCR. |
|
546 |
'PNGReader: redX:' infoPrint. redX infoPrintCR. |
|
547 |
'PNGReader: redY:' infoPrint. redY infoPrintCR. |
|
548 |
'PNGReader: greenX:' infoPrint. greenX infoPrintCR. |
|
549 |
'PNGReader: greenY:' infoPrint. greenY infoPrintCR. |
|
550 |
'PNGReader: blueX:' infoPrint. blueX infoPrintCR. |
|
551 |
'PNGReader: blueY:' infoPrint. blueY infoPrintCR. |
|
552 |
]. |
|
553 |
^ true |
|
554 |
! |
|
555 |
||
308 | 556 |
processChunk:type len:len |
2509 | 557 |
|chunk| |
558 |
||
559 |
type = 'IDAT' ifTrue:[ |
|
560 |
"---since the compressed data can span multiple |
|
561 |
chunks, stitch them all together first. later, |
|
562 |
if memory is an issue, we need to figure out how |
|
563 |
to do this on the fly---" |
|
564 |
chunk := inStream next:len. |
|
3821 | 565 |
globalDataChunk isNil ifTrue:[ |
566 |
globalDataChunk := ReadWriteStream on:(ByteArray new:(height * self bytesPerRow)). |
|
567 |
]. |
|
568 |
globalDataChunk nextPutAll:chunk. |
|
2509 | 569 |
^ true |
570 |
]. |
|
571 |
||
308 | 572 |
type = 'gAMA' ifTrue:[^ self processGAMAChunkLen:len]. |
573 |
type = 'sBIT' ifTrue:[^ self processSBITChunkLen:len]. |
|
574 |
type = 'tEXt' ifTrue:[^ self processTEXTChunkLen:len]. |
|
575 |
type = 'tIME' ifTrue:[^ self processTIMEChunkLen:len]. |
|
576 |
type = 'bKGD' ifTrue:[^ self processBKGDChunkLen:len]. |
|
577 |
type = 'zTXt' ifTrue:[^ self processZTXTChunkLen:len]. |
|
578 |
type = 'PLTE' ifTrue:[^ self processPLTEChunkLen:len]. |
|
579 |
||
1671 | 580 |
type = 'pHYs' ifTrue:[^ self processPHYSChunkLen:len]. |
2707 | 581 |
type = 'cHRM' ifTrue:[^ self processCHRMChunkLen:len]. |
1671 | 582 |
type = 'tRNS' ifTrue:[^ self processTRNSChunkLen:len]. |
583 |
type = 'IHDR' ifTrue:[^ self processIHDRChunkLen:len]. |
|
584 |
||
3762 | 585 |
type = 'iTXt' ifTrue:[^ self processITXTChunkLen:len]. |
586 |
type = 'iCCP' ifTrue:[^ self processICCPChunkLen:len]. |
|
3900 | 587 |
type = 'hIST' ifTrue:[^ self processHISTChunkLen:len]. |
588 |
type = 'spAL' ifTrue:[^ self processSPALChunkLen:len]. |
|
3889 | 589 |
|
590 |
specialChunkHandlers notNil ifTrue:[ |
|
591 |
|handler chunkData| |
|
592 |
||
593 |
handler := specialChunkHandlers at:type ifAbsent:nil. |
|
594 |
handler notNil ifTrue:[ |
|
595 |
chunkData := inStream nextBytes:len. |
|
596 |
handler value:chunkData optionalArgument:self. |
|
597 |
^ true. |
|
598 |
]. |
|
599 |
]. |
|
600 |
||
3891 | 601 |
type = 'niVI' ifTrue:[^ self processNIVIChunkLen:len]. |
3950 | 602 |
type = 'exEX' ifTrue:[^ self processEXEXChunkLen:len]. |
3891 | 603 |
|
308 | 604 |
('PNGReader: unrecognized chunk: ' , type , ' ignored.') infoPrintCR. |
605 |
||
606 |
inStream skip:len. |
|
607 |
^ true. |
|
608 |
||
3889 | 609 |
"Created: / 21-06-1996 / 21:10:37 / cg" |
3950 | 610 |
"Modified: / 11-04-2017 / 17:04:12 / cg" |
611 |
! |
|
612 |
||
613 |
processEXEXChunkLen:len |
|
614 |
"/ this cunk contains an expecco sample network. |
|
615 |
"/ ...to read it (using the expecco importer), |
|
616 |
"/ create a reader with a chunkHook and extract it there. |
|
617 |
Logger info:('PNG: skip exEX chunk'). |
|
618 |
inStream skip:len. |
|
619 |
^ true. |
|
620 |
||
621 |
"Created: / 11-04-2017 / 17:06:27 / cg" |
|
308 | 622 |
! |
623 |
||
624 |
processGAMAChunkLen:len |
|
625 |
inStream skip:len. |
|
626 |
^ true |
|
627 |
||
628 |
"Created: 21.6.1996 / 21:10:52 / cg" |
|
629 |
! |
|
630 |
||
2509 | 631 |
processGlobalIDATChunk |
632 |
interlaceMode == 0 ifTrue: [ |
|
633 |
^ self processNonInterlacedGlobalDATA |
|
634 |
]. |
|
635 |
^ self processInterlacedGlobalDATA |
|
636 |
! |
|
637 |
||
3900 | 638 |
processHISTChunkLen:len |
639 |
"/ ignored |
|
640 |
"/ inStream nextBytes:len. |
|
641 |
||
642 |
inStream skip:len. |
|
643 |
^ true |
|
644 |
||
645 |
"Created: / 17-02-2017 / 11:31:36 / cg" |
|
646 |
! |
|
647 |
||
3762 | 648 |
processICCPChunkLen:len |
649 |
"/ ignored |
|
650 |
"/ inStream nextBytes:len. |
|
651 |
||
652 |
inStream skip:len. |
|
653 |
^ true |
|
654 |
||
655 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
656 |
! |
|
657 |
||
308 | 658 |
processIDATChunkLen:len |
1671 | 659 |
interlaceMode == 0 ifTrue: [ |
660 |
^ self processNonInterlacedDATA:len |
|
661 |
]. |
|
662 |
^ self processInterlacedDATA:len |
|
663 |
! |
|
664 |
||
3762 | 665 |
processIHDRChunkLen:len |
666 |
"/ ignored |
|
667 |
||
668 |
inStream skip:len. |
|
669 |
^ true |
|
670 |
||
671 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
672 |
! |
|
673 |
||
674 |
processITXTChunkLen:len |
|
675 |
"/ international (i.e.utf8) textual data. |
|
676 |
||
677 |
"/ currently ignored - not needed to display png images |
|
678 |
"/ |
|
679 |
"/ Keyword: 1-79 bytes (character string) |
|
680 |
"/ Null separator: 1 byte |
|
681 |
"/ Compression flag: 1 byte |
|
682 |
"/ Compression method: 1 byte |
|
683 |
"/ Language tag: 0 or more bytes (character string) |
|
684 |
"/ Null separator: 1 byte |
|
685 |
"/ Translated keyword: 0 or more bytes |
|
686 |
"/ Null separator: 1 byte |
|
687 |
"/ Text: 0 or more bytes |
|
688 |
||
689 |
"/ if needed, set processTextChunks to true somewhere... |
|
690 |
processTextChunks == true ifTrue:[ |
|
691 |
|chunkData keyword text i1 i2 i3 |
|
692 |
compressionFlag compressionMethod languageTag |
|
693 |
xlatedKeyword textBytes| |
|
694 |
||
695 |
chunkData := inStream nextBytes:len. |
|
696 |
i1 := chunkData indexOf:0. |
|
697 |
keyword := (chunkData copyTo:i1-1) asString. |
|
698 |
compressionFlag := chunkData at:i1+1. |
|
699 |
compressionMethod := chunkData at:i1+2. |
|
700 |
i2 := chunkData indexOf:0 startingAt:i1+3. |
|
701 |
languageTag := (chunkData copyFrom:i1+3 to:i2-1) asString. |
|
702 |
i3 := chunkData indexOf:0 startingAt:i2+1. |
|
703 |
xlatedKeyword := (chunkData copyFrom:i2+1 to:i3-1) asString. |
|
704 |
textBytes := chunkData copyFrom:i3+1. |
|
705 |
compressionFlag == 0 ifTrue:[ |
|
706 |
text := textBytes |
|
707 |
] ifFalse:[ |
|
708 |
compressionFlag == 1 ifTrue:[ |
|
709 |
"/ for now, only zlib compression is supported... |
|
3900 | 710 |
"/ ... no - not even that is |
711 |
Logger warning:'PNG: zlib compressed text currently unsupported'. |
|
3762 | 712 |
] ifFalse:[ |
3900 | 713 |
Logger warning:'PNG: unsupported text compression method'. |
3762 | 714 |
]. |
715 |
]. |
|
716 |
text := text utf8Decoded. |
|
717 |
self handleText:text keyword:keyword. |
|
718 |
] ifFalse:[ |
|
719 |
inStream skip:len. |
|
720 |
]. |
|
721 |
^ true |
|
722 |
||
3900 | 723 |
"Created: / 21-06-1996 / 21:15:58 / cg" |
724 |
"Modified: / 16-02-2017 / 19:51:21 / cg" |
|
3762 | 725 |
! |
726 |
||
1671 | 727 |
processInterlacedDATA:len |
308 | 728 |
inStream skip:len. |
729 |
^ true |
|
1671 | 730 |
! |
308 | 731 |
|
2699 | 732 |
processInterlacedGlobalDATA |
3327 | 733 |
"adam7 interlace method" |
734 |
||
3612 | 735 |
| zlibReader filter |
736 |
bytesPerPass startingCol colIncrement rowIncrement |
|
737 |
startingRow |
|
738 |
temp "filtersSeen" |
|
739 |
cx "{ Class: SmallInteger }" |
|
740 |
sc "{ Class: SmallInteger }" |
|
741 |
cy "{ Class: SmallInteger }" |
|
742 |
startRow "{ Class: SmallInteger }" |
|
743 |
w "{ Class: SmallInteger }" |
|
744 |
h "{ Class: SmallInteger }" |
|
745 |
| |
|
2699 | 746 |
|
3327 | 747 |
interlaceMode == 1 ifFalse: [ |
748 |
self error:'unsupported interlaced mode'. |
|
749 |
^ self |
|
750 |
]. |
|
2699 | 751 |
|
752 |
"/ filtersSeen := Set new. |
|
753 |
||
754 |
startingCol := #(0 4 0 2 0 1 0 ). |
|
3327 | 755 |
startingRow := #(0 0 4 0 2 0 1 ). |
2699 | 756 |
colIncrement := #(8 8 4 4 2 2 1 ). |
757 |
rowIncrement := #(8 8 8 4 4 2 2 ). |
|
758 |
||
3821 | 759 |
globalDataChunk reset. |
760 |
zlibReader := ZipStream readOpenAsZipStreamOn:globalDataChunk suppressHeaderAndChecksum:false. |
|
2699 | 761 |
zlibReader binary. |
762 |
||
3612 | 763 |
h := height. |
764 |
w := width. |
|
2699 | 765 |
1 to: 7 do: [:pass | |
766 |
(self doPass: pass) ifTrue:[ |
|
767 |
cx := colIncrement at: pass. |
|
768 |
sc := startingCol at: pass. |
|
3327 | 769 |
cy := rowIncrement at: pass. |
3612 | 770 |
bytesPerPass := (((w - sc + cx - 1) // cx * depth) + 7) // 8. |
2699 | 771 |
prevScanline := ByteArray new: bytesPerPass. |
772 |
thisScanline := ByteArray new: bytesPerScanline. |
|
3612 | 773 |
startRow := startingRow at: pass. |
774 |
startRow to: h-1 by: cy do: [:y | |
|
3343 | 775 |
filter := zlibReader nextByte. |
2699 | 776 |
"/ filtersSeen add: filter. |
777 |
(filter isNil or: [(filter between: 0 and: 4) not]) |
|
778 |
ifTrue: [^ self]. |
|
779 |
zlibReader next: bytesPerPass into: thisScanline startingAt: 1. |
|
780 |
filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerPass ]. |
|
781 |
self copyPixels:y at:sc by:cx. |
|
782 |
temp := prevScanline. |
|
783 |
prevScanline := thisScanline. |
|
784 |
thisScanline := temp. |
|
785 |
] |
|
786 |
] |
|
787 |
]. |
|
788 |
zlibReader atEnd ifFalse:[self error:'Unexpected data']. |
|
2909 | 789 |
|
790 |
"Modified: / 03-05-2011 / 12:03:33 / cg" |
|
2699 | 791 |
! |
792 |
||
3890 | 793 |
processNIVIChunkLen:len |
3889 | 794 |
"/ this cunk contains a labView vi. |
795 |
"/ ignored for now... |
|
3890 | 796 |
"/ ...to read it (using the expecco importer), |
797 |
"/ create a reader with a chunkHook and extract it there. |
|
3891 | 798 |
Logger info:('PNG: skip niVI chunk'). |
3890 | 799 |
inStream skip:len. |
800 |
^ true. |
|
3889 | 801 |
|
802 |
"Created: / 14-02-2017 / 08:57:29 / cg" |
|
3891 | 803 |
"Modified: / 14-02-2017 / 22:36:34 / cg" |
3889 | 804 |
! |
805 |
||
1671 | 806 |
processNonInterlacedDATA:len |
2701 | 807 |
self halt:'not used' |
808 |
"/ |
|
809 |
"/ | zlibReader filter temp prevScanline thisScanline bytesPerScanline filtersSeen| |
|
810 |
"/ |
|
811 |
"/ zlibReader := ZipStream readOpenAsZipStreamOn:inStream suppressHeaderAndChecksum:false. |
|
812 |
"/ "/ zlibReader := ZLibReadStream on:inStream from: 1 to:len. |
|
813 |
"/ |
|
814 |
"/ prevScanline := ByteArray new: self bytesPerRow. |
|
815 |
"/ thisScanline := ByteArray new: self bytesPerRow. |
|
816 |
"/ 0 to: height - 1 do:[:index | |
|
817 |
"/ filter := (zlibReader next: 1) first. |
|
818 |
"/ filtersSeen add: filter. |
|
819 |
"/ (filter isNil or: [(filter between: 0 and: 4) not]) ifTrue: [^self]. |
|
820 |
"/ thisScanline := zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
|
821 |
"/ self filterScanline: filter count: bytesPerScanline. |
|
822 |
"/ self copyPixels: index. |
|
823 |
"/ temp := prevScanline. |
|
824 |
"/ prevScanline := thisScanline. |
|
825 |
"/ thisScanline := temp |
|
826 |
"/ ] |
|
2509 | 827 |
! |
828 |
||
829 |
processNonInterlacedGlobalDATA |
|
3821 | 830 |
| "data n" zlibReader filter temp "filtersSeen" i| |
2696 | 831 |
|
832 |
"/ filtersSeen := Set new. |
|
2695 | 833 |
|
834 |
"/ data := ByteArray new:(self bytesPerRow * height)+1000. |
|
835 |
"/ n := ZipStream uncompress: globalDataChunk into: data. |
|
836 |
"/ self halt. |
|
2509 | 837 |
|
3821 | 838 |
globalDataChunk reset. |
839 |
zlibReader := ZipStream readOpenAsZipStreamOn:globalDataChunk suppressHeaderAndChecksum:false. |
|
2696 | 840 |
zlibReader binary. |
2509 | 841 |
|
1671 | 842 |
prevScanline := ByteArray new: bytesPerScanline. |
843 |
thisScanline := ByteArray new: bytesPerScanline. |
|
3972 | 844 |
0 to: height-1 do:[:y | |
3343 | 845 |
filter := zlibReader nextByte. |
3893 | 846 |
zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
847 |
filter ~~ 0 ifTrue:[ |
|
848 |
"/ filtersSeen add: filter. |
|
849 |
(filter notNil and: [filter between: 0 and: 4]) ifFalse: [ |
|
850 |
'PNGReader: unsupported filter: ' infoPrint. filter infoPrintCR. |
|
851 |
^ self |
|
852 |
]. |
|
853 |
self filterScanline: filter count: bytesPerScanline |
|
2725 | 854 |
]. |
855 |
||
856 |
i := y * bytesPerScanline. |
|
857 |
data replaceFrom:i+1 to:(i+bytesPerScanline) with:thisScanline startingAt:1. |
|
858 |
||
2695 | 859 |
temp := prevScanline. |
860 |
prevScanline := thisScanline. |
|
861 |
thisScanline := temp |
|
862 |
] |
|
3893 | 863 |
|
864 |
"Modified: / 16-02-2017 / 16:45:52 / cg" |
|
3972 | 865 |
"Modified (format): / 23-08-2017 / 17:05:01 / cg" |
1671 | 866 |
! |
867 |
||
868 |
processPHYSChunkLen:len |
|
869 |
"physical pixel chunk - currently unhandled" |
|
870 |
||
2705 | 871 |
|pixelPerUnitX pixelPerUnitY unit| |
872 |
||
3554 | 873 |
pixelPerUnitX := inStream nextInt32MSB:true. |
874 |
pixelPerUnitY := inStream nextInt32MSB:true. |
|
2705 | 875 |
unit := inStream nextByte. |
876 |
||
2707 | 877 |
Verbose == true ifTrue:[ |
878 |
'PNGReader: PHYS chunk ignored:' infoPrintCR. |
|
879 |
'PNGReader: ppuX:' infoPrint. pixelPerUnitX infoPrintCR. |
|
880 |
'PNGReader: ppuY:' infoPrint. pixelPerUnitY infoPrintCR. |
|
881 |
'PNGReader: unit:' infoPrint. unit infoPrintCR. |
|
882 |
]. |
|
883 |
||
2705 | 884 |
^ true |
308 | 885 |
! |
886 |
||
887 |
processPLTEChunkLen:len |
|
1671 | 888 |
"read a color palette" |
889 |
||
2725 | 890 |
|n "{ Class: SmallInteger }" |
3972 | 891 |
redBytes greenBytes blueBytes |
892 |
triple| |
|
308 | 893 |
|
894 |
(len \\ 3) ~~ 0 ifTrue:[ |
|
895 |
'PNGReader: invalid size of PLTE chunk' infoPrintCR. |
|
896 |
^ false |
|
897 |
]. |
|
898 |
n := len // 3. |
|
899 |
redBytes := ByteArray new:n. |
|
900 |
greenBytes := ByteArray new:n. |
|
901 |
blueBytes := ByteArray new:n. |
|
902 |
||
3972 | 903 |
triple := ByteArray new:3. |
904 |
||
308 | 905 |
1 to:n do:[:i | |
3972 | 906 |
inStream next:3 into:triple startingAt:1. |
907 |
redBytes at:i put:(triple at:1). |
|
908 |
greenBytes at:i put:(triple at:2). |
|
909 |
blueBytes at:i put:(triple at:3) |
|
308 | 910 |
]. |
911 |
||
2725 | 912 |
colorMap := MappedPalette |
913 |
redVector:redBytes |
|
914 |
greenVector:greenBytes |
|
915 |
blueVector:blueBytes. |
|
916 |
||
308 | 917 |
^ true |
918 |
||
3972 | 919 |
"Created: / 21-06-1996 / 21:22:28 / cg" |
920 |
"Modified: / 23-08-2017 / 16:58:04 / cg" |
|
308 | 921 |
! |
922 |
||
923 |
processSBITChunkLen:len |
|
924 |
inStream skip:len. |
|
925 |
^ true |
|
926 |
||
927 |
"Created: 21.6.1996 / 21:13:09 / cg" |
|
928 |
! |
|
929 |
||
3900 | 930 |
processSPALChunkLen:len |
931 |
"/ ignored |
|
932 |
"/ inStream nextBytes:len. |
|
933 |
||
934 |
inStream skip:len. |
|
935 |
^ true |
|
936 |
||
937 |
"Created: / 17-02-2017 / 11:31:40 / cg" |
|
938 |
! |
|
939 |
||
3762 | 940 |
processTEXTChunkLen:len |
941 |
"/ textual data in iso8859 coding. |
|
942 |
||
943 |
"/ currently ignored - not needed to display png images |
|
944 |
"/ Keyword: 1-79 bytes (character string) |
|
945 |
"/ Null separator: 1 byte |
|
946 |
"/ Text: n bytes (character string) |
|
947 |
||
948 |
"/ if needed, set processTextChunks to true somewhere... |
|
949 |
processTextChunks == true ifTrue:[ |
|
950 |
|chunkData keyword text i| |
|
951 |
||
952 |
chunkData := inStream nextBytes:len. |
|
953 |
i := chunkData indexOf:0. |
|
954 |
keyword := (chunkData copyTo:i-1) asString. |
|
955 |
text := (chunkData copyFrom:i+1) asString. |
|
956 |
self handleText:text keyword:keyword. |
|
957 |
] ifFalse:[ |
|
958 |
inStream skip:len. |
|
959 |
]. |
|
308 | 960 |
^ true |
961 |
||
962 |
"Modified: 21.6.1996 / 21:15:27 / cg" |
|
963 |
! |
|
964 |
||
965 |
processTIMEChunkLen:len |
|
966 |
inStream skip:len. |
|
967 |
^ true |
|
968 |
||
969 |
"Created: 21.6.1996 / 21:15:43 / cg" |
|
970 |
"Modified: 21.6.1996 / 21:20:42 / cg" |
|
971 |
! |
|
972 |
||
2707 | 973 |
processTRNSChunkLen:len |
3900 | 974 |
paletteAlphaEntries := inStream nextBytes:len. |
3904 | 975 |
|
976 |
colorType == ColorTypeRGB ifTrue:[ |
|
977 |
len ~~ 3 ifTrue:[ |
|
978 |
Logger warning:'PNG: bad trns chunk size'. |
|
979 |
paletteAlphaEntries := nil. |
|
980 |
]. |
|
981 |
^ true |
|
982 |
]. |
|
983 |
colorType == ColorTypePalette ifTrue:[ |
|
984 |
len ~~ 1 ifTrue:[ |
|
985 |
Logger warning:'PNG: bad trns chunk size'. |
|
986 |
paletteAlphaEntries := nil. |
|
987 |
]. |
|
988 |
^ true |
|
989 |
]. |
|
990 |
colorType == ColorTypeGray ifTrue:[ |
|
991 |
len ~~ 2 ifTrue:[ |
|
992 |
Logger warning:'PNG: bad trns chunk size'. |
|
993 |
paletteAlphaEntries := nil. |
|
994 |
]. |
|
995 |
^ true |
|
996 |
]. |
|
997 |
paletteAlphaEntries := nil. |
|
2707 | 998 |
^ true |
3900 | 999 |
|
3904 | 1000 |
"Modified (format): / 17-02-2017 / 16:57:27 / cg" |
2707 | 1001 |
! |
1002 |
||
308 | 1003 |
processZTXTChunkLen:len |
3762 | 1004 |
"/ compressed text |
1005 |
||
1006 |
"/ currently ignored - not needed to display png images |
|
308 | 1007 |
inStream skip:len. |
1008 |
^ true |
|
1009 |
||
1010 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
1011 |
! ! |
|
1012 |
||
3900 | 1013 |
!PNGReader methodsFor:'reading-private filtering'! |
2696 | 1014 |
|
2909 | 1015 |
filterAverage:count |
2699 | 1016 |
"Use the average of the pixel to the left and the pixel above as a predictor" |
2909 | 1017 |
|
1018 |
|delta| |
|
2699 | 1019 |
|
3609 | 1020 |
%{ |
1021 |
if (__isByteArray(__INST(thisScanline)) |
|
1022 |
&& __isByteArray(__INST(prevScanline)) |
|
1023 |
&& __isSmallInteger(__INST(depth)) |
|
1024 |
&& __isSmallInteger(count)) { |
|
1025 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
1026 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
1027 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
1028 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
1029 |
INT __count = __intVal(count); |
|
1030 |
INT __delta = __intVal(__INST(depth)) / 8; |
|
1031 |
int __i; |
|
1032 |
||
1033 |
if (__delta < 1) __delta = 1; |
|
1034 |
for (__i=0; __i<__delta; __i++) { |
|
1035 |
__thisScanline[__i] += (__prevScanline[__i] >> 1); |
|
1036 |
} |
|
1037 |
for (; __i < __count; __i++) { |
|
1038 |
__thisScanline[__i] += ((__prevScanline[__i] + __thisScanline[__i-__delta]) >> 1); |
|
1039 |
} |
|
1040 |
RETURN(self); |
|
1041 |
} |
|
1042 |
%}. |
|
1043 |
||
1044 |
delta := depth // 8 max:1. |
|
2909 | 1045 |
1 to:delta do:[:i | |
3384 | 1046 |
thisScanline at:i put:((thisScanline at:i) + ((prevScanline at:i) // 2) bitAnd:255) |
2909 | 1047 |
]. |
1048 |
delta + 1 to:count do:[:i | |
|
3384 | 1049 |
thisScanline at:i put:((thisScanline at:i) + (((prevScanline at:i) + (thisScanline at:i - delta)) // 2) bitAnd:255) |
2909 | 1050 |
] |
1051 |
||
1052 |
"Modified: / 03-05-2011 / 12:14:01 / cg" |
|
2699 | 1053 |
! |
1054 |
||
2696 | 1055 |
filterHorizontal:count |
1056 |
"use the pixel to the left as a predictor" |
|
1057 |
||
1058 |
|delta| |
|
1059 |
||
3608 | 1060 |
%{ |
1061 |
if (__isByteArray(__INST(thisScanline)) |
|
1062 |
&& __isByteArray(__INST(prevScanline)) |
|
1063 |
&& __isSmallInteger(__INST(depth)) |
|
1064 |
&& __isSmallInteger(count)) { |
|
1065 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
1066 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
1067 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
1068 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
1069 |
INT __count = __intVal(count); |
|
1070 |
INT __delta = __intVal(__INST(depth)) / 8; |
|
1071 |
int __i; |
|
1072 |
||
1073 |
if (__delta < 1) __delta = 1; |
|
1074 |
for (__i = __delta; __i < __count; __i++) { |
|
1075 |
__thisScanline[__i] = __thisScanline[__i] + __thisScanline[__i-__delta]; |
|
1076 |
} |
|
1077 |
RETURN(self); |
|
1078 |
} |
|
1079 |
%}. |
|
1080 |
||
1081 |
delta := depth // 8 max:1. |
|
2696 | 1082 |
delta+1 to:count do:[:i| |
1083 |
thisScanline at:i put:(((thisScanline at:i)+ (thisScanline at:i-delta)) bitAnd:255) |
|
1084 |
] |
|
1085 |
! |
|
1086 |
||
2699 | 1087 |
filterNone: count |
2909 | 1088 |
"/ no filter - scanline is as is |
1089 |
||
1090 |
"Modified: / 03-05-2011 / 12:14:20 / cg" |
|
2699 | 1091 |
! |
1092 |
||
2909 | 1093 |
filterPaeth:count |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1094 |
"Select one of (the pixel to the left, the pixel above and the pixel to above left) to |
2909 | 1095 |
predict the value of this pixel" |
1096 |
||
1097 |
|delta| |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1098 |
|
3607 | 1099 |
%{ |
1100 |
if (__isByteArray(__INST(thisScanline)) |
|
1101 |
&& __isByteArray(__INST(prevScanline)) |
|
1102 |
&& __isSmallInteger(__INST(depth)) |
|
1103 |
&& __isSmallInteger(count)) { |
|
1104 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
1105 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
1106 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
1107 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
1108 |
INT __count = __intVal(count); |
|
1109 |
INT __delta = __intVal(__INST(depth)) / 8; |
|
1110 |
int __i; |
|
1111 |
||
1112 |
if (__delta < 1) __delta = 1; |
|
1113 |
for (__i=0; __i<__delta; __i++) { |
|
1114 |
unsigned int __pix = __thisScanline[__i] + __prevScanline[__i]; |
|
1115 |
__thisScanline[__i] = __pix; |
|
1116 |
} |
|
1117 |
for (; __i < __count; __i++) { |
|
1118 |
int __pCenter = __thisScanline[__i]; |
|
1119 |
int __pLeft = __thisScanline[__i - __delta]; |
|
1120 |
int __pAbove = __prevScanline[__i]; |
|
1121 |
int __pAboveLeft = __prevScanline[__i - __delta]; |
|
1122 |
int __pa, __pb, __pc; |
|
1123 |
int __p; |
|
1124 |
||
1125 |
__pa = (__pAbove - __pAboveLeft); |
|
1126 |
__pa = __pa < 0 ? -__pa : __pa; |
|
1127 |
__pb = (__pLeft - __pAboveLeft); |
|
1128 |
__pb = __pb < 0 ? -__pb : __pb; |
|
1129 |
__pc = __pLeft + __pAbove - __pAboveLeft - __pAboveLeft; |
|
1130 |
__pc = __pc < 0 ? -__pc : __pc; |
|
1131 |
if ((__pa <= __pb) && (__pa <= __pc)) __p = __pLeft; |
|
1132 |
else if (__pb <= __pc) __p = __pAbove; |
|
1133 |
else __p = __pAboveLeft; |
|
1134 |
||
1135 |
__thisScanline[__i] = __pCenter + __p; |
|
1136 |
} |
|
1137 |
RETURN(self); |
|
1138 |
} |
|
1139 |
%}. |
|
1140 |
||
1141 |
delta := depth // 8 max:1. |
|
2909 | 1142 |
1 to:delta do:[:i | |
1143 |
thisScanline |
|
1144 |
at:i |
|
1145 |
put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255) |
|
1146 |
]. |
|
1147 |
delta + 1 to:count do:[:i | |
|
1148 |
thisScanline |
|
1149 |
at:i |
|
1150 |
put:(((thisScanline at:i) + (self |
|
1151 |
paethPredictLeft:(thisScanline at:i - delta) |
|
1152 |
above:(prevScanline at:i) |
|
1153 |
aboveLeft:(prevScanline at:i - delta))) |
|
1154 |
bitAnd:255) |
|
1155 |
] |
|
1156 |
||
1157 |
"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
|
1158 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1159 |
|
2696 | 1160 |
filterScanline:filterType count:count |
3612 | 1161 |
filterType == 0 ifTrue:[^ self]. |
1162 |
||
2696 | 1163 |
self |
3612 | 1164 |
perform:(#(filterHorizontal: filterVertical: filterAverage: filterPaeth:) at:filterType) |
2696 | 1165 |
with:count |
2909 | 1166 |
|
1167 |
"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
|
1168 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1169 |
|
2909 | 1170 |
filterVertical:count |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1171 |
"Use the pixel above as a predictor" |
2909 | 1172 |
|
3608 | 1173 |
%{ |
1174 |
if (__isByteArray(__INST(thisScanline)) |
|
1175 |
&& __isByteArray(__INST(prevScanline)) |
|
1176 |
&& __isSmallInteger(count)) { |
|
1177 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
1178 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
1179 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
1180 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
1181 |
INT __count = __intVal(count); |
|
1182 |
int __i; |
|
1183 |
||
1184 |
for (__i=0; __i<__count; __i++) { |
|
1185 |
unsigned int __pix = __thisScanline[__i] + __prevScanline[__i]; |
|
1186 |
__thisScanline[__i] = __pix; |
|
1187 |
} |
|
1188 |
RETURN(self); |
|
1189 |
} |
|
1190 |
%}. |
|
1191 |
||
2909 | 1192 |
1 to:count do:[:i | |
1193 |
thisScanline |
|
1194 |
at:i |
|
1195 |
put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255) |
|
1196 |
] |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1197 |
|
2909 | 1198 |
"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
|
1199 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1200 |
|
3607 | 1201 |
paethPredictLeft: l above: a aboveLeft: al |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1202 |
"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
|
1203 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1204 |
| pa pb pc | |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1205 |
|
3607 | 1206 |
pa := a > al ifTrue: [a - al] ifFalse: [al - a]. |
1207 |
pb := l > al ifTrue: [l - al] ifFalse: [al - l]. |
|
1208 |
pc := l + a - al - al. |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1209 |
pc < 0 ifTrue: [ |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1210 |
pc := pc * -1 |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1211 |
]. |
3607 | 1212 |
((pa <= pb) and: [pa <= pc]) ifTrue: [^ l]. |
1213 |
(pb <= pc) ifTrue: [^ a]. |
|
1214 |
^ al |
|
2696 | 1215 |
! ! |
1216 |
||
3900 | 1217 |
!PNGReader methodsFor:'reading-private pixel copy'! |
2699 | 1218 |
|
2909 | 1219 |
copyPixels:y at:startX by:incX |
2704 | 1220 |
"Handle interlaced pixels of supported colorTypes" |
2909 | 1221 |
|
1222 |
|s| |
|
2699 | 1223 |
|
3598 | 1224 |
"/ per color type copy methods |
1225 |
s := #( |
|
3900 | 1226 |
#copyPixelsGray:at:by: "/ 0 = ColorTypeGray |
2909 | 1227 |
nil |
3900 | 1228 |
#copyPixelsRGB:at:by: "/ 2 = ColorTypeRGB |
1229 |
#copyPixelsIndexed:at:by: "/ 3 = ColorTypePalette |
|
1230 |
#copyPixelsGrayAlpha:at:by: "/ 4 = ColorTypeGrayAlpha |
|
2909 | 1231 |
nil |
3900 | 1232 |
#copyPixelsRGBA:at:by: "/ 6 = ColorTypeRGBA |
2909 | 1233 |
) at:colorType + 1. |
3437 | 1234 |
self perform:s with:y with:startX with:incX |
1848 | 1235 |
|
2909 | 1236 |
"Modified: / 03-05-2011 / 12:02:45 / cg" |
3900 | 1237 |
"Modified (comment): / 16-02-2017 / 19:42:47 / cg" |
3327 | 1238 |
! |
1239 |
||
3598 | 1240 |
copyPixelsGray:y at:startX by:incX |
1241 |
"Handle interlaced pixels of supported colorTypes. |
|
1242 |
Untested code - please verify" |
|
1243 |
||
3611 | 1244 |
|srcIndex "{ Class: SmallInteger }" |
1245 |
srcMask "{ Class: SmallInteger }" |
|
1246 |
nPixels "{ Class: SmallInteger }" |
|
1247 |
dstIndex "{ Class: SmallInteger }" |
|
1248 |
dstMask "{ Class: SmallInteger }" |
|
1249 |
x "{ Class: SmallInteger }" |
|
1250 |
bits "{ Class: SmallInteger }" |
|
1251 |
bitMask "{ Class: SmallInteger }" |
|
1252 |
rS "{ Class: SmallInteger }" |
|
1253 |
lS "{ Class: SmallInteger }" |
|
1254 |
rowIndex "{ Class: SmallInteger }"| |
|
3598 | 1255 |
|
1256 |
nPixels := width // incX. |
|
1257 |
srcIndex := 0. srcMask := 0. x := startX. |
|
1258 |
||
1259 |
depth == 1 ifTrue:[ |
|
1260 |
dstIndex := (y * bytesPerScanline) + (startX // 8) + 1. |
|
1261 |
dstMask := 16r80 >> (x \\ 8). |
|
1262 |
||
1263 |
1 to:nPixels do:[:cnt | |
|
1264 |
srcMask == 0 ifTrue:[ |
|
1265 |
srcMask := 16r80. |
|
1266 |
srcIndex := srcIndex + 1. |
|
1267 |
bits := thisScanline at:srcIndex. |
|
1268 |
]. |
|
1269 |
bitMask := (bits bitAnd:srcMask) == 0 ifTrue:[0] ifFalse:[dstMask]. |
|
1270 |
bitMask ~~ 0 ifTrue:[ |
|
1271 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
1272 |
]. |
|
1273 |
x := x + incX. |
|
1274 |
dstMask := dstMask >> incX. |
|
1275 |
dstMask == 0 ifTrue:[ |
|
1276 |
dstIndex := dstIndex + 1. |
|
1277 |
dstMask := 16r80 >> (x \\ 8). |
|
1278 |
]. |
|
1279 |
srcMask := srcMask bitShift:-1. |
|
1280 |
]. |
|
1281 |
^ self. |
|
1282 |
]. |
|
1283 |
||
1284 |
depth == 2 ifTrue:[ |
|
1285 |
dstIndex := (y * bytesPerScanline) + (startX // 4) + 1. |
|
1286 |
lS := 6 - ((x \\ 4) * 2). |
|
1287 |
rS := -1. |
|
1288 |
1 to:nPixels do:[:cnt | |
|
1289 |
rS < 0 ifTrue:[ |
|
1290 |
srcIndex := srcIndex + 1. |
|
1291 |
bits := thisScanline at:srcIndex. |
|
1292 |
rS := 6. |
|
1293 |
]. |
|
1294 |
bitMask := ((bits >> rS) bitAnd:2r11). |
|
1295 |
bitMask ~~ 0 ifTrue:[ |
|
1296 |
bitMask := bitMask << lS. |
|
1297 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
1298 |
]. |
|
1299 |
lS := lS - (incX * 2). |
|
1300 |
lS < 0 ifTrue:[ |
|
1301 |
lS <= -8 ifTrue:[ |
|
1302 |
dstIndex := dstIndex + 2. |
|
1303 |
lS := lS + 8 + 8. |
|
1304 |
] ifFalse:[ |
|
1305 |
dstIndex := dstIndex + 1. |
|
1306 |
lS := lS + 8. |
|
1307 |
]. |
|
1308 |
]. |
|
1309 |
rS := rS - 2. |
|
1310 |
]. |
|
1311 |
^ self. |
|
1312 |
]. |
|
1313 |
||
1314 |
depth == 4 ifTrue:[ |
|
1315 |
rowIndex := (y * bytesPerScanline) + 1. |
|
1316 |
dstIndex := rowIndex + (x // 2). |
|
1317 |
rS := -1. |
|
1318 |
1 to:nPixels do:[:cnt | |
|
1319 |
rS < 0 ifTrue:[ |
|
1320 |
srcIndex := srcIndex + 1. |
|
1321 |
bits := thisScanline at:srcIndex. |
|
1322 |
rS := 4. |
|
1323 |
]. |
|
1324 |
bitMask := ((bits >> rS) bitAnd:2r1111). |
|
1325 |
x even ifTrue:[ |
|
1326 |
bitMask := bitMask bitShift:4. |
|
1327 |
]. |
|
1328 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
1329 |
x := x + incX. |
|
1330 |
dstIndex := rowIndex + (x // 2). |
|
1331 |
rS := rS - 4. |
|
1332 |
]. |
|
1333 |
^ self. |
|
1334 |
]. |
|
1335 |
||
1336 |
depth == 8 ifTrue:[ |
|
1337 |
srcIndex := 1. |
|
1338 |
dstIndex := (y * bytesPerScanline) + (startX) + 1. |
|
1339 |
||
1340 |
1 to:nPixels do:[:n | |
|
1341 |
data at:dstIndex put:(thisScanline at:srcIndex). |
|
1342 |
srcIndex := srcIndex + 1. |
|
1343 |
dstIndex := dstIndex + incX |
|
1344 |
]. |
|
1345 |
^ self. |
|
1346 |
]. |
|
1347 |
||
1348 |
depth == 16 ifTrue:[ |
|
1349 |
srcIndex := 1. |
|
1350 |
dstIndex := (y * bytesPerScanline) + (startX*2) + 1. |
|
1351 |
||
1352 |
1 to:nPixels do:[:n | |
|
3601 | 1353 |
data unsignedInt16At:dstIndex put:(thisScanline unsignedInt16At:srcIndex). |
3598 | 1354 |
srcIndex := srcIndex + 2. |
1355 |
dstIndex := dstIndex + (incX * 2) |
|
1356 |
]. |
|
1357 |
^ self. |
|
1358 |
]. |
|
1359 |
self error:'unsupported depth'. |
|
1360 |
! |
|
1361 |
||
1362 |
copyPixelsIndexed:y at:startX by:incX |
|
1363 |
self copyPixelsGray:y at:startX by:incX |
|
1364 |
! |
|
1365 |
||
1366 |
copyPixelsRGB:y at:startX by:incX |
|
1367 |
"Handle interlaced pixels of supported colorTypes. |
|
1368 |
Untested code - please verify" |
|
1369 |
||
3611 | 1370 |
|srcIndex "{ Class: SmallInteger }" |
1371 |
nPixels "{ Class: SmallInteger }" |
|
1372 |
dstIndex "{ Class: SmallInteger }" |
|
1373 |
dstInc "{ Class: SmallInteger }" |
|
1374 |
bpp "{ Class: SmallInteger }"| |
|
3598 | 1375 |
|
1376 |
bpp := bytesPerScanline // width. |
|
1377 |
||
1378 |
"/ 1 byte per r,g,b |
|
1379 |
srcIndex := 1. |
|
1380 |
dstIndex := (y * bytesPerScanline) + (startX * bpp) + 1. |
|
1381 |
dstInc := incX * bpp. |
|
1382 |
||
1383 |
nPixels := width // incX. |
|
1384 |
1 to:nPixels do:[:n | |
|
1385 |
data replaceFrom:dstIndex to:dstIndex+bpp-1 with:thisScanline startingAt:srcIndex. |
|
1386 |
srcIndex := srcIndex + bpp. |
|
1387 |
dstIndex := dstIndex + dstInc |
|
1388 |
]. |
|
1389 |
! |
|
1390 |
||
3327 | 1391 |
copyPixelsRGBA:y at:startX by:incX |
1392 |
"Handle interlaced pixels of supported colorTypes. |
|
1393 |
Untested code - please verify" |
|
1394 |
||
3611 | 1395 |
|srcIndex "{ Class: SmallInteger }" |
1396 |
nPixels "{ Class: SmallInteger }" |
|
1397 |
dstIndex "{ Class: SmallInteger }" |
|
1398 |
dstInc "{ Class: SmallInteger }"| |
|
3327 | 1399 |
|
1400 |
srcIndex := 1. |
|
3437 | 1401 |
dstIndex := (y * bytesPerScanline) + (startX * 4) + 1. |
1402 |
dstInc := incX * 4. |
|
3327 | 1403 |
|
1404 |
nPixels := thisScanline size. |
|
1405 |
[srcIndex < nPixels] whileTrue:[ |
|
3437 | 1406 |
data replaceFrom:dstIndex to:dstIndex+3 with:thisScanline startingAt:srcIndex. |
3327 | 1407 |
srcIndex := srcIndex + 4. |
3437 | 1408 |
dstIndex := dstIndex + dstInc |
3327 | 1409 |
]. |
2699 | 1410 |
! ! |
1411 |
||
3900 | 1412 |
!PNGReader methodsFor:'writing'! |
2683 | 1413 |
|
1414 |
save:image onStream:aStream |
|
1415 |
"save image in PNG-file-format onto aStream" |
|
1416 |
||
1417 |
outStream := aStream. |
|
1418 |
outStream binary. |
|
1419 |
||
2703 | 1420 |
byteOrder := #msb. |
4004 | 1421 |
|
2683 | 1422 |
width := image width. |
1423 |
height := image height. |
|
1424 |
photometric := image photometric. |
|
1425 |
samplesPerPixel := image samplesPerPixel. |
|
1426 |
bitsPerSample := image bitsPerSample. |
|
1427 |
colorMap := image colorMap. |
|
1428 |
data := image bits. |
|
3893 | 1429 |
mask := image mask. |
1430 |
depth := self bitsPerPixel. |
|
3900 | 1431 |
|
3909 | 1432 |
outStream nextPutAll:(self class pngHeader). |
3893 | 1433 |
|
3900 | 1434 |
image mask notNil ifTrue:[ |
3972 | 1435 |
((photometric ~~ #rgb) and:[(photometric ~~ #rgba) and:[(photometric ~~ #argb)]]) ifTrue: [ |
3949 | 1436 |
self determinePaletteIndexForMaskedPixels |
3904 | 1437 |
] ifFalse:[ |
1438 |
photometric := #rgba. |
|
1439 |
]. |
|
3900 | 1440 |
]. |
1441 |
||
2703 | 1442 |
self writeIHDRChunk. |
1443 |
photometric == #palette ifTrue: [self writePaletteChunk]. |
|
3900 | 1444 |
paletteIndexForMaskedPixels notNil ifTrue: [self writeTRNSChunk]. |
2703 | 1445 |
self writeImageDataChunk. |
2683 | 1446 |
self writeEndChunk |
3893 | 1447 |
|
3972 | 1448 |
"Modified: / 23-08-2017 / 16:25:00 / cg" |
4004 | 1449 |
"Modified (format): / 31-08-2017 / 17:26:28 / cg" |
3900 | 1450 |
! ! |
1451 |
||
1452 |
!PNGReader methodsFor:'writing-private'! |
|
1453 |
||
1454 |
determinePaletteIndexForMaskedPixels |
|
1455 |
"/ if all masked pixels are 0, and 0 is not used elsewhere in the image, |
|
1456 |
"/ write it as such. |
|
1457 |
"/ Otherwise, find an unallocated palette index, and assign masked pixels to it. |
|
1458 |
||
1459 |
|usedPixels freePixels pixelIdx| |
|
1460 |
||
1461 |
colorMap size < 256 ifTrue:[ |
|
1462 |
paletteIndexForMaskedPixels := colorMap size. |
|
1463 |
] ifFalse:[ |
|
1464 |
usedPixels := data usedValues. |
|
1465 |
usedPixels size == 256 ifTrue:[ |
|
1466 |
self error:'cannot represent image (no palette slot for mask)' |
|
1467 |
]. |
|
1468 |
freePixels := (0 to:255) asSet removeAllFoundIn:usedPixels. |
|
1469 |
paletteIndexForMaskedPixels := freePixels first. |
|
1470 |
]. |
|
1471 |
||
1472 |
"/ rewrite data: wherever masked, change pixel to paletteIndexForMaskedPixels |
|
1473 |
||
1474 |
data := data copy. |
|
1475 |
pixelIdx := 1. |
|
1476 |
0 to:height-1 do:[:y | |
|
1477 |
0 to:width-1 do:[:x | |
|
1478 |
(mask pixelAtX:x y:y) == 0 ifTrue:[ |
|
1479 |
data at:pixelIdx put:paletteIndexForMaskedPixels. |
|
1480 |
]. |
|
1481 |
pixelIdx := pixelIdx + 1 |
|
1482 |
] |
|
1483 |
]. |
|
1484 |
||
1485 |
" |
|
1486 |
PNGReader save:(ToolbarIconLibrary systemBrowserIcon) onFile:'/tmp/icon.png' |
|
1487 |
ImageEditor openOnFile:'/tmp/icon.png' |
|
1488 |
" |
|
1489 |
||
1490 |
"Created: / 16-02-2017 / 19:59:39 / cg" |
|
1491 |
"Modified: / 17-02-2017 / 09:25:57 / cg" |
|
1492 |
! ! |
|
1493 |
||
1494 |
!PNGReader methodsFor:'writing-private chunks'! |
|
2683 | 1495 |
|
3949 | 1496 |
determineColorTypeAndHasMask |
4004 | 1497 |
"sets colorType as side effect; |
1498 |
returns boolean if a mask is present" |
|
1499 |
||
3949 | 1500 |
((photometric == #whiteIs0) or:[ (photometric == #blackIs0)]) ifTrue:[ |
1501 |
colorType := ColorTypeGray. |
|
1502 |
^ (samplesPerPixel > 1) |
|
1503 |
]. |
|
1504 |
(photometric == #rgb) ifTrue:[ |
|
1505 |
colorType := ColorTypeRGB. |
|
1506 |
^ (samplesPerPixel > 3) |
|
1507 |
]. |
|
1508 |
(photometric == #rgba) ifTrue:[ |
|
1509 |
colorType := ColorTypeRGBAlpha. |
|
1510 |
^ (samplesPerPixel > 3) |
|
1511 |
]. |
|
1512 |
(photometric == #palette) ifTrue:[ |
|
1513 |
colorType := ColorTypePalette. |
|
1514 |
^ (samplesPerPixel > 1) |
|
1515 |
]. |
|
1516 |
self error:'unhandled photometric: ',photometric asString. |
|
1517 |
^ false. |
|
1518 |
||
1519 |
"Created: / 11-04-2017 / 12:39:30 / cg" |
|
4004 | 1520 |
"Modified (comment): / 31-08-2017 / 17:24:31 / cg" |
3949 | 1521 |
! |
1522 |
||
2703 | 1523 |
writeChunk:chunkTypeChars size:len with:aBlock |
2708 | 1524 |
|crc realOutStream chunkBytes| |
1525 |
||
1526 |
realOutStream := outStream. |
|
1527 |
outStream := WriteStream on:(ByteArray new:len). |
|
1528 |
||
2703 | 1529 |
outStream nextPutBytes:4 from:chunkTypeChars startingAt:1. |
1530 |
aBlock value. |
|
2708 | 1531 |
|
1532 |
chunkBytes := outStream contents. |
|
1533 |
crc := CRC32Stream hashValueOf:chunkBytes. |
|
1534 |
||
1535 |
outStream := realOutStream. |
|
1536 |
||
3554 | 1537 |
realOutStream nextPutInt32:len MSB:true. |
2708 | 1538 |
realOutStream nextPutAll:chunkBytes. |
3554 | 1539 |
realOutStream nextPutInt32:crc MSB:true. |
2703 | 1540 |
! |
2683 | 1541 |
|
2703 | 1542 |
writeEndChunk |
1543 |
self |
|
1544 |
writeChunk: 'IEND' |
|
1545 |
size: 0 |
|
1546 |
with:[ ] |
|
2683 | 1547 |
! |
1548 |
||
2703 | 1549 |
writeIHDRChunk |
4005 | 1550 |
|hasMask bitsPerChannel| |
3893 | 1551 |
|
3949 | 1552 |
hasMask := self determineColorTypeAndHasMask. |
3893 | 1553 |
hasMask ifTrue:[ |
3900 | 1554 |
"/ make it grayAlpha / rgba |
1555 |
((colorType == ColorTypeGray) or:[colorType == ColorTypeGray]) ifTrue:[ |
|
1556 |
colorType := colorType bitOr:4. "/ +alpha |
|
1557 |
]. |
|
3893 | 1558 |
]. |
4005 | 1559 |
|
1560 |
"bitsperchannel" |
|
1561 |
depth <= 8 ifTrue:[ |
|
1562 |
bitsPerChannel := depth |
|
1563 |
] ifFalse:[ |
|
4010 | 1564 |
(colorType == ColorTypeRGB or:[colorType == ColorTypeRGBAlpha]) ifTrue:[ |
4005 | 1565 |
"/ all channels must have the same nr of bits |
1566 |
self assert:(bitsPerSample asSet size == 1). |
|
1567 |
bitsPerChannel := bitsPerSample first |
|
1568 |
] ifFalse:[ |
|
1569 |
bitsPerChannel := depth |
|
1570 |
] |
|
1571 |
]. |
|
3893 | 1572 |
|
2683 | 1573 |
self |
1574 |
writeChunk: 'IHDR' |
|
3949 | 1575 |
size: (4+4+5) |
2683 | 1576 |
with:[ |
4004 | 1577 |
outStream nextPutInt32:width MSB:true. |
1578 |
outStream nextPutInt32:height MSB:true. |
|
4005 | 1579 |
outStream nextPut: bitsPerChannel. |
2703 | 1580 |
outStream nextPut: colorType. |
2683 | 1581 |
outStream nextPut: 0. "Compression" |
1582 |
outStream nextPut: 0. "Filter method" |
|
1583 |
outStream nextPut: 0 "Non-interlaced" |
|
1584 |
] |
|
3893 | 1585 |
|
4010 | 1586 |
"Modified: / 04-09-2017 / 18:34:15 / cg" |
2683 | 1587 |
! |
1588 |
||
2703 | 1589 |
writeImageDataChunk |
3893 | 1590 |
|compressedByteStream compressedBytes zlibWriter idx |
3935 | 1591 |
row32 bytesPerPixel| |
2703 | 1592 |
|
1593 |
bytesPerScanline := self bytesPerRow. |
|
3821 | 1594 |
|
1595 |
compressedByteStream := WriteStream on:(ByteArray new:(bytesPerScanline*height)). |
|
1596 |
zlibWriter := ZipStream writeOpenAsZipStreamOn:compressedByteStream suppressHeaderAndChecksum:false. |
|
1597 |
zlibWriter binary. |
|
2703 | 1598 |
|
3900 | 1599 |
(mask notNil and:[photometric ~~ #palette]) ifTrue:[ |
3935 | 1600 |
(depth == 32) ifTrue:[ |
1601 |
bytesPerPixel := 4 |
|
1602 |
] ifFalse:[ |
|
1603 |
"/ for now - only support depth24 + mask |
|
1604 |
self assert:(depth == 24). |
|
1605 |
bytesPerPixel := 3. |
|
1606 |
]. |
|
3893 | 1607 |
|
3935 | 1608 |
"/ on-the-fly place mask into the alpha channel. |
3893 | 1609 |
row32 := ByteArray new:(4 * width). |
3935 | 1610 |
self assert:(bytesPerScanline == (bytesPerPixel * width)). |
1611 |
||
3893 | 1612 |
idx := 1. |
1613 |
0 to:height-1 do:[:y | |
|
1614 |
|dstIdx maskRow| |
|
2708 | 1615 |
|
3893 | 1616 |
"/ expand rgb to rgba |
1617 |
maskRow := mask rowAt:y. |
|
3935 | 1618 |
|
3893 | 1619 |
dstIdx := 1. |
1620 |
1 to:width do:[:x | |
|
1621 |
row32 at:dstIdx put:(data at:idx). |
|
1622 |
row32 at:dstIdx+1 put:(data at:idx+1). |
|
1623 |
row32 at:dstIdx+2 put:(data at:idx+2). |
|
1624 |
row32 at:dstIdx+3 put:((maskRow at:x) == 0 ifTrue:[0] ifFalse:[16rFF]). |
|
3935 | 1625 |
idx := idx + bytesPerPixel. |
3893 | 1626 |
dstIdx := dstIdx + 4. |
1627 |
]. |
|
3935 | 1628 |
|
3893 | 1629 |
zlibWriter nextPutAll:#[0]. "/ no filter |
1630 |
zlibWriter nextPutAll:row32. |
|
1631 |
]. |
|
1632 |
] ifFalse:[ |
|
1633 |
idx := 1. |
|
1634 |
0 to:height-1 do:[:y | |
|
1635 |
|row| |
|
1636 |
||
1637 |
row := data copyFrom:idx to:(idx+bytesPerScanline-1). |
|
1638 |
zlibWriter nextPutAll:#[0]. "/ no filter |
|
1639 |
zlibWriter nextPutAll:row. |
|
1640 |
idx := idx + bytesPerScanline. |
|
1641 |
]. |
|
2703 | 1642 |
]. |
2708 | 1643 |
|
1644 |
zlibWriter close. |
|
1645 |
compressedBytes := compressedByteStream contents. |
|
1646 |
||
1647 |
self |
|
1648 |
writeChunk: 'IDAT' |
|
1649 |
size:(compressedBytes size) |
|
1650 |
with:[ |
|
1651 |
outStream nextPutAll:compressedBytes |
|
1652 |
] |
|
3893 | 1653 |
|
3935 | 1654 |
"Modified: / 25-02-2017 / 14:27:34 / cg" |
3949 | 1655 |
"Modified (format): / 11-04-2017 / 12:41:22 / cg" |
2703 | 1656 |
! |
1657 |
||
2683 | 1658 |
writePaletteChunk |
1659 |
self |
|
1660 |
writeChunk: 'PLTE' |
|
1661 |
size: 3 * colorMap size |
|
1662 |
with:[ |
|
2703 | 1663 |
colorMap do:[:color | |
1664 |
outStream |
|
1665 |
nextPut: color redByte; |
|
1666 |
nextPut: color greenByte; |
|
1667 |
nextPut: color blueByte |
|
2683 | 1668 |
] |
1669 |
] |
|
3900 | 1670 |
! |
1671 |
||
1672 |
writeTRNSChunk |
|
1673 |
self |
|
1674 |
writeChunk: 'tRNS' |
|
1675 |
size: 1 |
|
1676 |
with:[ |
|
1677 |
outStream nextPut: paletteIndexForMaskedPixels |
|
1678 |
] |
|
1679 |
||
1680 |
"Created: / 16-02-2017 / 19:58:06 / cg" |
|
2683 | 1681 |
! ! |
1682 |
||
398 | 1683 |
!PNGReader class methodsFor:'documentation'! |
308 | 1684 |
|
1685 |
version |
|
3554 | 1686 |
^ '$Header$' |
2909 | 1687 |
! |
1688 |
||
1689 |
version_CVS |
|
3554 | 1690 |
^ '$Header$' |
308 | 1691 |
! ! |
1671 | 1692 |
|
3100 | 1693 |
|
308 | 1694 |
PNGReader initialize! |