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