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