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