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