author | Claus Gittinger <cg@exept.de> |
Thu, 27 Feb 2020 16:08:38 +0100 | |
changeset 4457 | 8c328436fba9 |
parent 4433 | 61f93300f774 |
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 |
||
4433 | 452 |
"/ Verbose := true. |
453 |
"/ Verbose := false. |
|
454 |
||
3900 | 455 |
inStream atEnd ifTrue:[^ false]. |
456 |
||
457 |
len := inStream nextInt32MSB:true. |
|
458 |
type := String new:4. |
|
459 |
(inStream nextBytes:4 into:type) ~~ 4 ifTrue:[^ false]. |
|
460 |
||
461 |
Verbose == true ifTrue:[ |
|
4099 | 462 |
'PNGReader: chunk: ' infoPrint. type infoPrint. |
463 |
' len: ' infoPrint. len infoPrintCR. |
|
3900 | 464 |
]. |
465 |
||
466 |
type = 'IEND' ifTrue:[^ false]. |
|
467 |
||
468 |
(self processChunk:type len:len) ifFalse:[^ false]. |
|
469 |
||
470 |
crc := inStream nextInt32MSB:true. "/ ignored - for now |
|
471 |
^ true |
|
472 |
||
4099 | 473 |
"Created: / 21-06-1996 / 21:09:36 / cg" |
474 |
"Modified: / 02-06-2018 / 06:42:58 / Claus Gittinger" |
|
3900 | 475 |
! |
476 |
||
477 |
getIHDRChunk |
|
478 |
|len type crc| |
|
479 |
||
480 |
len := inStream nextInt32MSB:true. |
|
481 |
||
482 |
type := String new:4. |
|
483 |
inStream nextBytes:4 into:type. |
|
484 |
||
485 |
type = 'IHDR' ifFalse:[ |
|
486 |
Logger warning:'PNGReader: expected IHDR magic (%1)' with:inStream. |
|
487 |
^ false |
|
488 |
]. |
|
489 |
len == 13 ifFalse:[ |
|
490 |
Logger warning:'PNGReader: bad IHDR size (%1)' with:inStream. |
|
491 |
^ false |
|
492 |
]. |
|
493 |
||
494 |
width := inStream nextInt32MSB:true. |
|
495 |
height := inStream nextInt32MSB:true. |
|
496 |
||
497 |
(width <= 0 or:[height <= 0]) ifTrue:[ |
|
498 |
Logger warning:'PNGReader: invalid dimension(s) (%1)' with:inStream. |
|
499 |
^ false. |
|
500 |
]. |
|
501 |
self reportDimension. |
|
502 |
||
503 |
bitsPerChannel := inStream nextByte. |
|
4005 | 504 |
depth := bitsPerChannel. "/ for now - might be changed by setColorType. |
3900 | 505 |
colorType := inStream nextByte. |
506 |
||
507 |
compressionMethod := inStream nextByte. |
|
508 |
filterMethod := inStream nextByte. |
|
509 |
interlaceMode := inStream nextByte. |
|
510 |
||
511 |
Verbose == true ifTrue:[ |
|
512 |
'PNGReader: IHDR:' infoPrintCR. |
|
513 |
'PNGReader: width: ' infoPrint. width infoPrintCR. |
|
514 |
'PNGReader: height: ' infoPrint. height infoPrintCR. |
|
515 |
'PNGReader: bitsPerChannel: ' infoPrint. bitsPerChannel infoPrintCR. |
|
516 |
'PNGReader: colorType: ' infoPrint. colorType infoPrintCR. |
|
517 |
'PNGReader: compressionMethod: ' infoPrint. compressionMethod infoPrintCR. |
|
518 |
'PNGReader: filterMethod: ' infoPrint. filterMethod infoPrintCR. |
|
519 |
'PNGReader: interlaceMode: ' infoPrint. interlaceMode infoPrintCR. |
|
520 |
]. |
|
521 |
||
522 |
crc := inStream nextInt32MSB:true. |
|
523 |
^ true |
|
524 |
||
525 |
"Modified: / 17-02-2017 / 11:35:20 / cg" |
|
4005 | 526 |
"Modified (comment): / 31-08-2017 / 18:03:41 / cg" |
3900 | 527 |
! |
528 |
||
529 |
handleText:text keyword:keyword |
|
530 |
"/ things like exif data etc. |
|
531 |
"/ self halt. |
|
532 |
||
533 |
"/Standard keywords for text chunks: |
|
534 |
"/ |
|
4423 | 535 |
"/ Artist Short (one line) title or caption for image |
3900 | 536 |
"/ Author Name of image's creator |
4423 | 537 |
"/ Comment Miscellaneous comment; conversion from |
538 |
"/ GIF comment |
|
3900 | 539 |
"/ Copyright Copyright notice |
540 |
"/ Creation Time Time of original image creation |
|
4423 | 541 |
"/ Description Description of image (possibly long) |
542 |
"/ Disclaimer Legal disclaimer |
|
543 |
"/ Document |
|
544 |
"/ Label |
|
545 |
"/ Make |
|
546 |
"/ Model |
|
3900 | 547 |
"/ Software Software used to create the image |
4423 | 548 |
"/ Source Device used to create the image |
549 |
"/ TimeStamp |
|
550 |
"/ Title Short (one line) title or caption for image |
|
551 |
"/ URL |
|
3900 | 552 |
"/ Warning Warning of nature of content |
4423 | 553 |
"/ create-date |
554 |
"/ modify-date |
|
3900 | 555 |
! |
556 |
||
557 |
setColorType:colorType |
|
4005 | 558 |
colorType == ColorTypeGray "0" ifTrue:[ |
3900 | 559 |
photometric := #blackIs0. |
560 |
samplesPerPixel := 1. |
|
561 |
bitsPerSample := Array with:bitsPerChannel. |
|
562 |
depth := bitsPerChannel. |
|
563 |
^ true. |
|
564 |
]. |
|
565 |
||
4005 | 566 |
colorType == ColorTypeRGB "2" ifTrue:[ |
3900 | 567 |
photometric := #rgb. |
568 |
samplesPerPixel := 3. |
|
569 |
bitsPerSample := Array with:bitsPerChannel with:bitsPerChannel with:bitsPerChannel. |
|
570 |
depth := bitsPerChannel * 3. |
|
4005 | 571 |
|
572 |
bitsPerChannel < 8 ifTrue:[ |
|
573 |
bitsPerChannel == 5 ifTrue:[ |
|
574 |
depth := 16 |
|
575 |
] ifFalse:[ |
|
576 |
('PNGReader: unsupported bitsPerChannel:%1 for colorType rgb' bindWith:bitsPerChannel) infoPrintCR. |
|
577 |
]. |
|
578 |
]. |
|
3900 | 579 |
^ true. |
580 |
]. |
|
581 |
||
4005 | 582 |
colorType == ColorTypePalette "3" ifTrue:[ |
3900 | 583 |
bitsPerChannel == 16 ifTrue:[ |
4005 | 584 |
'PNGReader: invalid unsupported bitsPerChannel:%1 for colorType palette' infoPrintCR. |
3900 | 585 |
^ false. |
586 |
]. |
|
587 |
photometric := #palette. |
|
588 |
samplesPerPixel := 1. |
|
589 |
bitsPerSample := Array with:bitsPerChannel. |
|
590 |
depth := bitsPerChannel. |
|
591 |
^ true. |
|
592 |
]. |
|
593 |
||
4005 | 594 |
colorType == ColorTypeGrayAlpha "4" ifTrue:[ |
3900 | 595 |
bitsPerChannel < 8 ifTrue:[ |
4005 | 596 |
'PNGReader: unsupported colorType/depth combination' infoPrintCR. |
3900 | 597 |
^ false. |
598 |
]. |
|
599 |
photometric := #blackIs0. |
|
600 |
samplesPerPixel := 2. |
|
601 |
bitsPerSample := Array with:bitsPerChannel with:bitsPerChannel. |
|
602 |
depth := bitsPerChannel * 2. |
|
603 |
^ true. |
|
604 |
]. |
|
605 |
||
4005 | 606 |
colorType == ColorTypeRGBAlpha "6" ifTrue:[ |
3900 | 607 |
bitsPerChannel < 8 ifTrue:[ |
4005 | 608 |
'PNGReader: unsupported colorType/depth combination' infoPrintCR. |
3900 | 609 |
^ false. |
610 |
]. |
|
611 |
photometric := #rgba. |
|
4044 | 612 |
ImageReader::EnforcedImageTypeQuery query == #rgb ifTrue:[ |
613 |
photometric := #rgb |
|
614 |
]. |
|
3900 | 615 |
samplesPerPixel := 4. |
616 |
bitsPerSample := Array with:bitsPerChannel with:bitsPerChannel with:bitsPerChannel with:bitsPerChannel. |
|
617 |
depth := bitsPerChannel * 4. |
|
618 |
^ true. |
|
619 |
]. |
|
620 |
||
4005 | 621 |
('PNGReader: unsupported or invalid colorType: ' , colorType printString , '.') infoPrintCR. |
3900 | 622 |
^ false |
623 |
||
4005 | 624 |
"Modified: / 31-08-2017 / 17:58:18 / cg" |
3900 | 625 |
! ! |
626 |
||
627 |
!PNGReader methodsFor:'reading-private chunks'! |
|
308 | 628 |
|
2699 | 629 |
doPass: pass |
630 |
"Certain interlace passes are skipped with certain small image dimensions" |
|
631 |
||
4344 | 632 |
pass == 1 ifTrue: [ ^ true ]. |
2699 | 633 |
((width = 1) and: [height = 1]) ifTrue: [ ^ false ]. |
4344 | 634 |
pass == 2 ifTrue: [ ^ width >= 5 ]. |
635 |
pass == 3 ifTrue: [ ^ height >= 5 ]. |
|
636 |
pass == 4 ifTrue: [ ^ (width >=3 ) or: [height >= 5] ]. |
|
637 |
pass == 5 ifTrue: [ ^ height >=3 ]. |
|
638 |
pass == 6 ifTrue: [ ^ width >=2 ]. |
|
639 |
pass == 7 ifTrue: [ ^ height >=2 ]. |
|
2699 | 640 |
|
4276 | 641 |
ArgumentError raise. |
2699 | 642 |
^ true |
4276 | 643 |
|
644 |
"Modified: / 06-06-2019 / 23:21:38 / Claus Gittinger" |
|
2699 | 645 |
! |
646 |
||
308 | 647 |
processBKGDChunkLen:len |
4423 | 648 |
"/ background color chunk |
649 |
||
308 | 650 |
inStream skip:len. |
651 |
^ true |
|
652 |
||
653 |
"Created: 21.6.1996 / 21:15:49 / cg" |
|
654 |
! |
|
655 |
||
2707 | 656 |
processCHRMChunkLen:len |
657 |
"cHRM Primary chromaticities chunk - currently unhandled" |
|
658 |
||
659 |
|whitePointX whitePointY redX redY greenX greenY blueX blueY| |
|
660 |
||
3554 | 661 |
whitePointX := (inStream nextInt32MSB:true) / 100000.0. |
662 |
whitePointY := (inStream nextInt32MSB:true) / 100000.0. |
|
663 |
redX := (inStream nextInt32MSB:true) / 100000.0. |
|
664 |
redY := (inStream nextInt32MSB:true) / 100000.0. |
|
665 |
greenX := (inStream nextInt32MSB:true) / 100000.0. |
|
666 |
greenY := (inStream nextInt32MSB:true) / 100000.0. |
|
667 |
blueX := (inStream nextInt32MSB:true) / 100000.0. |
|
668 |
blueY := (inStream nextInt32MSB:true) / 100000.0. |
|
2707 | 669 |
|
670 |
Verbose == true ifTrue:[ |
|
4433 | 671 |
'PNGReader: CHRM (ignored):' infoPrintCR. |
2707 | 672 |
'PNGReader: whitePointX:' infoPrint. whitePointX infoPrintCR. |
673 |
'PNGReader: whitePointY:' infoPrint. whitePointX infoPrintCR. |
|
674 |
'PNGReader: redX:' infoPrint. redX infoPrintCR. |
|
675 |
'PNGReader: redY:' infoPrint. redY infoPrintCR. |
|
676 |
'PNGReader: greenX:' infoPrint. greenX infoPrintCR. |
|
677 |
'PNGReader: greenY:' infoPrint. greenY infoPrintCR. |
|
678 |
'PNGReader: blueX:' infoPrint. blueX infoPrintCR. |
|
679 |
'PNGReader: blueY:' infoPrint. blueY infoPrintCR. |
|
680 |
]. |
|
681 |
^ true |
|
682 |
! |
|
683 |
||
308 | 684 |
processChunk:type len:len |
4206 | 685 |
|chunk handler chunkData| |
2509 | 686 |
|
4206 | 687 |
specialChunkHandlers notNil ifTrue:[ |
688 |
handler := specialChunkHandlers at:type ifAbsent:nil. |
|
689 |
handler notNil ifTrue:[ |
|
690 |
chunkData := inStream nextBytes:len. |
|
691 |
handler value:chunkData optionalArgument:self. |
|
692 |
^ true. |
|
693 |
]. |
|
694 |
]. |
|
695 |
||
696 |
extractSpecialChunksOnly == true ifTrue:[ |
|
697 |
inStream skip:len. |
|
698 |
^ true. |
|
699 |
]. |
|
700 |
||
2509 | 701 |
type = 'IDAT' ifTrue:[ |
702 |
"---since the compressed data can span multiple |
|
703 |
chunks, stitch them all together first. later, |
|
704 |
if memory is an issue, we need to figure out how |
|
705 |
to do this on the fly---" |
|
706 |
chunk := inStream next:len. |
|
3821 | 707 |
globalDataChunk isNil ifTrue:[ |
708 |
globalDataChunk := ReadWriteStream on:(ByteArray new:(height * self bytesPerRow)). |
|
709 |
]. |
|
710 |
globalDataChunk nextPutAll:chunk. |
|
2509 | 711 |
^ true |
712 |
]. |
|
713 |
||
308 | 714 |
type = 'gAMA' ifTrue:[^ self processGAMAChunkLen:len]. |
715 |
type = 'sBIT' ifTrue:[^ self processSBITChunkLen:len]. |
|
716 |
type = 'tEXt' ifTrue:[^ self processTEXTChunkLen:len]. |
|
717 |
type = 'tIME' ifTrue:[^ self processTIMEChunkLen:len]. |
|
718 |
type = 'bKGD' ifTrue:[^ self processBKGDChunkLen:len]. |
|
719 |
type = 'zTXt' ifTrue:[^ self processZTXTChunkLen:len]. |
|
720 |
type = 'PLTE' ifTrue:[^ self processPLTEChunkLen:len]. |
|
721 |
||
1671 | 722 |
type = 'pHYs' ifTrue:[^ self processPHYSChunkLen:len]. |
2707 | 723 |
type = 'cHRM' ifTrue:[^ self processCHRMChunkLen:len]. |
1671 | 724 |
type = 'tRNS' ifTrue:[^ self processTRNSChunkLen:len]. |
725 |
type = 'IHDR' ifTrue:[^ self processIHDRChunkLen:len]. |
|
726 |
||
3762 | 727 |
type = 'iTXt' ifTrue:[^ self processITXTChunkLen:len]. |
728 |
type = 'iCCP' ifTrue:[^ self processICCPChunkLen:len]. |
|
3900 | 729 |
type = 'hIST' ifTrue:[^ self processHISTChunkLen:len]. |
730 |
type = 'spAL' ifTrue:[^ self processSPALChunkLen:len]. |
|
4211 | 731 |
type = 'sRGB' ifTrue:[^ self processSRGBChunkLen:len]. |
3889 | 732 |
|
3891 | 733 |
type = 'niVI' ifTrue:[^ self processNIVIChunkLen:len]. |
3950 | 734 |
type = 'exEX' ifTrue:[^ self processEXEXChunkLen:len]. |
3891 | 735 |
|
4424 | 736 |
"/ type = 'acTL' ifTrue:[]. "/ animation control |
737 |
"/ type = 'dSIG' ifTrue:[]. "/ signature |
|
738 |
"/ type = 'eXIf' ifTrue:[]. "/ EXIF: eXIf |
|
739 |
"/ type = 'fRAC' ifTrue:[]. "/ fractal params |
|
740 |
"/ type = 'gIFg' ifTrue:[]. "/ GIFGraphicControlExtension |
|
741 |
"/ type = 'gIFt' ifTrue:[]. "/ GIFPlainTextExtension |
|
742 |
"/ type = 'gIFx' ifTrue:[]. "/ GIFApplicationExtension |
|
743 |
"/ type = 'oFFs' ifTrue:[]. "/ ImageOffset |
|
744 |
"/ type = 'pCAL' ifTrue:[]. "/ PixelCalibration |
|
745 |
"/ type = 'sCAL' ifTrue:[]. "/ SubjectScale |
|
746 |
"/ type = 'sPLT' ifTrue:[]. "/ SuggestedPalette |
|
747 |
"/ type = 'sTER' ifTrue:[]. "/ StereoImage |
|
748 |
"/ type = 'tXMP' ifTrue:[]. "/ XMP obsolete |
|
749 |
"/ type = 'vpAg' ifTrue:[]. "/ VirtualPage |
|
750 |
"/ type = 'zxIf' ifTrue:[]. "/ EXIF: zxIf |
|
4423 | 751 |
|
308 | 752 |
('PNGReader: unrecognized chunk: ' , type , ' ignored.') infoPrintCR. |
753 |
||
754 |
inStream skip:len. |
|
755 |
^ true. |
|
756 |
||
3889 | 757 |
"Created: / 21-06-1996 / 21:10:37 / cg" |
3950 | 758 |
"Modified: / 11-04-2017 / 17:04:12 / cg" |
4211 | 759 |
"Modified: / 12-11-2018 / 15:18:58 / Claus Gittinger" |
3950 | 760 |
! |
761 |
||
762 |
processEXEXChunkLen:len |
|
763 |
"/ this cunk contains an expecco sample network. |
|
764 |
"/ ...to read it (using the expecco importer), |
|
765 |
"/ create a reader with a chunkHook and extract it there. |
|
4207 | 766 |
"/ Logger info:('PNG: skip exEX chunk'). |
3950 | 767 |
inStream skip:len. |
768 |
^ true. |
|
769 |
||
770 |
"Created: / 11-04-2017 / 17:06:27 / cg" |
|
4207 | 771 |
"Modified: / 29-10-2018 / 12:57:08 / Claus Gittinger" |
308 | 772 |
! |
773 |
||
774 |
processGAMAChunkLen:len |
|
775 |
inStream skip:len. |
|
776 |
^ true |
|
777 |
||
778 |
"Created: 21.6.1996 / 21:10:52 / cg" |
|
779 |
! |
|
780 |
||
2509 | 781 |
processGlobalIDATChunk |
782 |
interlaceMode == 0 ifTrue: [ |
|
783 |
^ self processNonInterlacedGlobalDATA |
|
784 |
]. |
|
785 |
^ self processInterlacedGlobalDATA |
|
786 |
! |
|
787 |
||
3900 | 788 |
processHISTChunkLen:len |
789 |
"/ ignored |
|
790 |
"/ inStream nextBytes:len. |
|
791 |
||
792 |
inStream skip:len. |
|
793 |
^ true |
|
794 |
||
795 |
"Created: / 17-02-2017 / 11:31:36 / cg" |
|
796 |
! |
|
797 |
||
3762 | 798 |
processICCPChunkLen:len |
799 |
"/ ignored |
|
800 |
"/ inStream nextBytes:len. |
|
801 |
||
802 |
inStream skip:len. |
|
803 |
^ true |
|
804 |
||
805 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
806 |
! |
|
807 |
||
308 | 808 |
processIDATChunkLen:len |
1671 | 809 |
interlaceMode == 0 ifTrue: [ |
810 |
^ self processNonInterlacedDATA:len |
|
811 |
]. |
|
812 |
^ self processInterlacedDATA:len |
|
813 |
! |
|
814 |
||
3762 | 815 |
processIHDRChunkLen:len |
4423 | 816 |
"/ image header chunk |
3762 | 817 |
"/ ignored |
818 |
||
819 |
inStream skip:len. |
|
820 |
^ true |
|
821 |
||
822 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
823 |
! |
|
824 |
||
825 |
processITXTChunkLen:len |
|
826 |
"/ international (i.e.utf8) textual data. |
|
827 |
||
828 |
"/ currently ignored - not needed to display png images |
|
829 |
"/ |
|
830 |
"/ Keyword: 1-79 bytes (character string) |
|
831 |
"/ Null separator: 1 byte |
|
832 |
"/ Compression flag: 1 byte |
|
833 |
"/ Compression method: 1 byte |
|
834 |
"/ Language tag: 0 or more bytes (character string) |
|
835 |
"/ Null separator: 1 byte |
|
836 |
"/ Translated keyword: 0 or more bytes |
|
837 |
"/ Null separator: 1 byte |
|
838 |
"/ Text: 0 or more bytes |
|
839 |
||
840 |
"/ if needed, set processTextChunks to true somewhere... |
|
841 |
processTextChunks == true ifTrue:[ |
|
842 |
|chunkData keyword text i1 i2 i3 |
|
843 |
compressionFlag compressionMethod languageTag |
|
844 |
xlatedKeyword textBytes| |
|
845 |
||
846 |
chunkData := inStream nextBytes:len. |
|
847 |
i1 := chunkData indexOf:0. |
|
848 |
keyword := (chunkData copyTo:i1-1) asString. |
|
849 |
compressionFlag := chunkData at:i1+1. |
|
850 |
compressionMethod := chunkData at:i1+2. |
|
851 |
i2 := chunkData indexOf:0 startingAt:i1+3. |
|
852 |
languageTag := (chunkData copyFrom:i1+3 to:i2-1) asString. |
|
853 |
i3 := chunkData indexOf:0 startingAt:i2+1. |
|
854 |
xlatedKeyword := (chunkData copyFrom:i2+1 to:i3-1) asString. |
|
855 |
textBytes := chunkData copyFrom:i3+1. |
|
856 |
compressionFlag == 0 ifTrue:[ |
|
857 |
text := textBytes |
|
858 |
] ifFalse:[ |
|
859 |
compressionFlag == 1 ifTrue:[ |
|
860 |
"/ for now, only zlib compression is supported... |
|
3900 | 861 |
"/ ... no - not even that is |
862 |
Logger warning:'PNG: zlib compressed text currently unsupported'. |
|
3762 | 863 |
] ifFalse:[ |
3900 | 864 |
Logger warning:'PNG: unsupported text compression method'. |
3762 | 865 |
]. |
866 |
]. |
|
867 |
text := text utf8Decoded. |
|
868 |
self handleText:text keyword:keyword. |
|
869 |
] ifFalse:[ |
|
870 |
inStream skip:len. |
|
871 |
]. |
|
872 |
^ true |
|
873 |
||
3900 | 874 |
"Created: / 21-06-1996 / 21:15:58 / cg" |
875 |
"Modified: / 16-02-2017 / 19:51:21 / cg" |
|
3762 | 876 |
! |
877 |
||
1671 | 878 |
processInterlacedDATA:len |
4099 | 879 |
'PNGReader: skip unsupported interlaced data' infoPrintCR. |
880 |
||
308 | 881 |
inStream skip:len. |
882 |
^ true |
|
4099 | 883 |
|
884 |
"Modified: / 02-06-2018 / 06:40:22 / Claus Gittinger" |
|
1671 | 885 |
! |
308 | 886 |
|
2699 | 887 |
processInterlacedGlobalDATA |
3327 | 888 |
"adam7 interlace method" |
889 |
||
3612 | 890 |
| zlibReader filter |
891 |
bytesPerPass startingCol colIncrement rowIncrement |
|
892 |
startingRow |
|
893 |
temp "filtersSeen" |
|
894 |
cx "{ Class: SmallInteger }" |
|
895 |
sc "{ Class: SmallInteger }" |
|
896 |
cy "{ Class: SmallInteger }" |
|
897 |
startRow "{ Class: SmallInteger }" |
|
898 |
w "{ Class: SmallInteger }" |
|
899 |
h "{ Class: SmallInteger }" |
|
900 |
| |
|
2699 | 901 |
|
4099 | 902 |
Verbose == true ifTrue:[ |
903 |
'PNGReader: process interlaced global data' infoPrintCR. |
|
904 |
]. |
|
905 |
||
3327 | 906 |
interlaceMode == 1 ifFalse: [ |
907 |
self error:'unsupported interlaced mode'. |
|
908 |
^ self |
|
909 |
]. |
|
2699 | 910 |
|
911 |
"/ filtersSeen := Set new. |
|
912 |
||
913 |
startingCol := #(0 4 0 2 0 1 0 ). |
|
3327 | 914 |
startingRow := #(0 0 4 0 2 0 1 ). |
2699 | 915 |
colIncrement := #(8 8 4 4 2 2 1 ). |
916 |
rowIncrement := #(8 8 8 4 4 2 2 ). |
|
917 |
||
3821 | 918 |
globalDataChunk reset. |
919 |
zlibReader := ZipStream readOpenAsZipStreamOn:globalDataChunk suppressHeaderAndChecksum:false. |
|
2699 | 920 |
zlibReader binary. |
921 |
||
3612 | 922 |
h := height. |
923 |
w := width. |
|
2699 | 924 |
1 to: 7 do: [:pass | |
925 |
(self doPass: pass) ifTrue:[ |
|
926 |
cx := colIncrement at: pass. |
|
927 |
sc := startingCol at: pass. |
|
3327 | 928 |
cy := rowIncrement at: pass. |
3612 | 929 |
bytesPerPass := (((w - sc + cx - 1) // cx * depth) + 7) // 8. |
2699 | 930 |
prevScanline := ByteArray new: bytesPerPass. |
931 |
thisScanline := ByteArray new: bytesPerScanline. |
|
3612 | 932 |
startRow := startingRow at: pass. |
933 |
startRow to: h-1 by: cy do: [:y | |
|
3343 | 934 |
filter := zlibReader nextByte. |
2699 | 935 |
"/ filtersSeen add: filter. |
936 |
(filter isNil or: [(filter between: 0 and: 4) not]) |
|
937 |
ifTrue: [^ self]. |
|
938 |
zlibReader next: bytesPerPass into: thisScanline startingAt: 1. |
|
939 |
filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerPass ]. |
|
940 |
self copyPixels:y at:sc by:cx. |
|
941 |
temp := prevScanline. |
|
942 |
prevScanline := thisScanline. |
|
943 |
thisScanline := temp. |
|
944 |
] |
|
945 |
] |
|
946 |
]. |
|
947 |
zlibReader atEnd ifFalse:[self error:'Unexpected data']. |
|
2909 | 948 |
|
949 |
"Modified: / 03-05-2011 / 12:03:33 / cg" |
|
4099 | 950 |
"Modified: / 02-06-2018 / 06:43:26 / Claus Gittinger" |
2699 | 951 |
! |
952 |
||
3890 | 953 |
processNIVIChunkLen:len |
3889 | 954 |
"/ this cunk contains a labView vi. |
955 |
"/ ignored for now... |
|
3890 | 956 |
"/ ...to read it (using the expecco importer), |
957 |
"/ create a reader with a chunkHook and extract it there. |
|
3891 | 958 |
Logger info:('PNG: skip niVI chunk'). |
3890 | 959 |
inStream skip:len. |
960 |
^ true. |
|
3889 | 961 |
|
962 |
"Created: / 14-02-2017 / 08:57:29 / cg" |
|
3891 | 963 |
"Modified: / 14-02-2017 / 22:36:34 / cg" |
3889 | 964 |
! |
965 |
||
1671 | 966 |
processNonInterlacedDATA:len |
2701 | 967 |
self halt:'not used' |
968 |
"/ |
|
969 |
"/ | zlibReader filter temp prevScanline thisScanline bytesPerScanline filtersSeen| |
|
970 |
"/ |
|
971 |
"/ zlibReader := ZipStream readOpenAsZipStreamOn:inStream suppressHeaderAndChecksum:false. |
|
972 |
"/ "/ zlibReader := ZLibReadStream on:inStream from: 1 to:len. |
|
973 |
"/ |
|
974 |
"/ prevScanline := ByteArray new: self bytesPerRow. |
|
975 |
"/ thisScanline := ByteArray new: self bytesPerRow. |
|
976 |
"/ 0 to: height - 1 do:[:index | |
|
977 |
"/ filter := (zlibReader next: 1) first. |
|
978 |
"/ filtersSeen add: filter. |
|
979 |
"/ (filter isNil or: [(filter between: 0 and: 4) not]) ifTrue: [^self]. |
|
980 |
"/ thisScanline := zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
|
981 |
"/ self filterScanline: filter count: bytesPerScanline. |
|
982 |
"/ self copyPixels: index. |
|
983 |
"/ temp := prevScanline. |
|
984 |
"/ prevScanline := thisScanline. |
|
985 |
"/ thisScanline := temp |
|
986 |
"/ ] |
|
2509 | 987 |
! |
988 |
||
989 |
processNonInterlacedGlobalDATA |
|
3821 | 990 |
| "data n" zlibReader filter temp "filtersSeen" i| |
2696 | 991 |
|
4099 | 992 |
Verbose == true ifTrue:[ |
993 |
'PNGReader: process non-interlaced global data' infoPrintCR. |
|
994 |
]. |
|
995 |
||
2696 | 996 |
"/ filtersSeen := Set new. |
2695 | 997 |
|
998 |
"/ data := ByteArray new:(self bytesPerRow * height)+1000. |
|
999 |
"/ n := ZipStream uncompress: globalDataChunk into: data. |
|
1000 |
"/ self halt. |
|
2509 | 1001 |
|
3821 | 1002 |
globalDataChunk reset. |
1003 |
zlibReader := ZipStream readOpenAsZipStreamOn:globalDataChunk suppressHeaderAndChecksum:false. |
|
2696 | 1004 |
zlibReader binary. |
2509 | 1005 |
|
1671 | 1006 |
prevScanline := ByteArray new: bytesPerScanline. |
1007 |
thisScanline := ByteArray new: bytesPerScanline. |
|
3972 | 1008 |
0 to: height-1 do:[:y | |
3343 | 1009 |
filter := zlibReader nextByte. |
3893 | 1010 |
zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
1011 |
filter ~~ 0 ifTrue:[ |
|
1012 |
"/ filtersSeen add: filter. |
|
1013 |
(filter notNil and: [filter between: 0 and: 4]) ifFalse: [ |
|
1014 |
'PNGReader: unsupported filter: ' infoPrint. filter infoPrintCR. |
|
1015 |
^ self |
|
1016 |
]. |
|
1017 |
self filterScanline: filter count: bytesPerScanline |
|
2725 | 1018 |
]. |
1019 |
||
4099 | 1020 |
forceRGB ifTrue:[ |
1021 |
self copyPixels:y at:0 by:1. |
|
1022 |
] ifFalse:[ |
|
1023 |
i := y * bytesPerScanline. |
|
1024 |
data replaceFrom:i+1 to:(i+bytesPerScanline) with:thisScanline startingAt:1. |
|
1025 |
]. |
|
2695 | 1026 |
temp := prevScanline. |
1027 |
prevScanline := thisScanline. |
|
1028 |
thisScanline := temp |
|
1029 |
] |
|
3893 | 1030 |
|
1031 |
"Modified: / 16-02-2017 / 16:45:52 / cg" |
|
3972 | 1032 |
"Modified (format): / 23-08-2017 / 17:05:01 / cg" |
4099 | 1033 |
"Modified: / 02-06-2018 / 07:05:06 / Claus Gittinger" |
1671 | 1034 |
! |
1035 |
||
1036 |
processPHYSChunkLen:len |
|
1037 |
"physical pixel chunk - currently unhandled" |
|
1038 |
||
2705 | 1039 |
|pixelPerUnitX pixelPerUnitY unit| |
1040 |
||
3554 | 1041 |
pixelPerUnitX := inStream nextInt32MSB:true. |
1042 |
pixelPerUnitY := inStream nextInt32MSB:true. |
|
2705 | 1043 |
unit := inStream nextByte. |
1044 |
||
2707 | 1045 |
Verbose == true ifTrue:[ |
4433 | 1046 |
'PNGReader: PHYS (ignored):' infoPrintCR. |
2707 | 1047 |
'PNGReader: ppuX:' infoPrint. pixelPerUnitX infoPrintCR. |
1048 |
'PNGReader: ppuY:' infoPrint. pixelPerUnitY infoPrintCR. |
|
1049 |
'PNGReader: unit:' infoPrint. unit infoPrintCR. |
|
1050 |
]. |
|
1051 |
||
2705 | 1052 |
^ true |
308 | 1053 |
! |
1054 |
||
1055 |
processPLTEChunkLen:len |
|
1671 | 1056 |
"read a color palette" |
1057 |
||
2725 | 1058 |
|n "{ Class: SmallInteger }" |
3972 | 1059 |
redBytes greenBytes blueBytes |
1060 |
triple| |
|
308 | 1061 |
|
1062 |
(len \\ 3) ~~ 0 ifTrue:[ |
|
1063 |
'PNGReader: invalid size of PLTE chunk' infoPrintCR. |
|
1064 |
^ false |
|
1065 |
]. |
|
1066 |
n := len // 3. |
|
1067 |
redBytes := ByteArray new:n. |
|
1068 |
greenBytes := ByteArray new:n. |
|
1069 |
blueBytes := ByteArray new:n. |
|
1070 |
||
3972 | 1071 |
triple := ByteArray new:3. |
1072 |
||
308 | 1073 |
1 to:n do:[:i | |
3972 | 1074 |
inStream next:3 into:triple startingAt:1. |
1075 |
redBytes at:i put:(triple at:1). |
|
1076 |
greenBytes at:i put:(triple at:2). |
|
1077 |
blueBytes at:i put:(triple at:3) |
|
308 | 1078 |
]. |
1079 |
||
2725 | 1080 |
colorMap := MappedPalette |
1081 |
redVector:redBytes |
|
1082 |
greenVector:greenBytes |
|
1083 |
blueVector:blueBytes. |
|
1084 |
||
308 | 1085 |
^ true |
1086 |
||
3972 | 1087 |
"Created: / 21-06-1996 / 21:22:28 / cg" |
1088 |
"Modified: / 23-08-2017 / 16:58:04 / cg" |
|
308 | 1089 |
! |
1090 |
||
1091 |
processSBITChunkLen:len |
|
1092 |
inStream skip:len. |
|
1093 |
^ true |
|
1094 |
||
1095 |
"Created: 21.6.1996 / 21:13:09 / cg" |
|
1096 |
! |
|
1097 |
||
3900 | 1098 |
processSPALChunkLen:len |
1099 |
"/ ignored |
|
1100 |
"/ inStream nextBytes:len. |
|
1101 |
||
1102 |
inStream skip:len. |
|
1103 |
^ true |
|
1104 |
||
1105 |
"Created: / 17-02-2017 / 11:31:40 / cg" |
|
1106 |
! |
|
1107 |
||
4211 | 1108 |
processSRGBChunkLen:len |
1109 |
"/ ignored |
|
1110 |
"/ inStream nextBytes:len. |
|
1111 |
||
1112 |
inStream skip:len. |
|
1113 |
^ true |
|
1114 |
||
1115 |
"Created: / 12-11-2018 / 15:19:38 / Claus Gittinger" |
|
1116 |
! |
|
1117 |
||
3762 | 1118 |
processTEXTChunkLen:len |
1119 |
"/ textual data in iso8859 coding. |
|
1120 |
||
1121 |
"/ currently ignored - not needed to display png images |
|
1122 |
"/ Keyword: 1-79 bytes (character string) |
|
1123 |
"/ Null separator: 1 byte |
|
1124 |
"/ Text: n bytes (character string) |
|
1125 |
||
1126 |
"/ if needed, set processTextChunks to true somewhere... |
|
1127 |
processTextChunks == true ifTrue:[ |
|
1128 |
|chunkData keyword text i| |
|
1129 |
||
1130 |
chunkData := inStream nextBytes:len. |
|
1131 |
i := chunkData indexOf:0. |
|
1132 |
keyword := (chunkData copyTo:i-1) asString. |
|
1133 |
text := (chunkData copyFrom:i+1) asString. |
|
1134 |
self handleText:text keyword:keyword. |
|
1135 |
] ifFalse:[ |
|
1136 |
inStream skip:len. |
|
1137 |
]. |
|
308 | 1138 |
^ true |
1139 |
||
1140 |
"Modified: 21.6.1996 / 21:15:27 / cg" |
|
1141 |
! |
|
1142 |
||
1143 |
processTIMEChunkLen:len |
|
1144 |
inStream skip:len. |
|
1145 |
^ true |
|
1146 |
||
1147 |
"Created: 21.6.1996 / 21:15:43 / cg" |
|
1148 |
"Modified: 21.6.1996 / 21:20:42 / cg" |
|
1149 |
! |
|
1150 |
||
2707 | 1151 |
processTRNSChunkLen:len |
3900 | 1152 |
paletteAlphaEntries := inStream nextBytes:len. |
3904 | 1153 |
|
1154 |
colorType == ColorTypeRGB ifTrue:[ |
|
1155 |
len ~~ 3 ifTrue:[ |
|
1156 |
Logger warning:'PNG: bad trns chunk size'. |
|
1157 |
paletteAlphaEntries := nil. |
|
1158 |
]. |
|
1159 |
^ true |
|
1160 |
]. |
|
1161 |
colorType == ColorTypePalette ifTrue:[ |
|
1162 |
len ~~ 1 ifTrue:[ |
|
1163 |
Logger warning:'PNG: bad trns chunk size'. |
|
1164 |
paletteAlphaEntries := nil. |
|
1165 |
]. |
|
1166 |
^ true |
|
1167 |
]. |
|
1168 |
colorType == ColorTypeGray ifTrue:[ |
|
1169 |
len ~~ 2 ifTrue:[ |
|
1170 |
Logger warning:'PNG: bad trns chunk size'. |
|
1171 |
paletteAlphaEntries := nil. |
|
1172 |
]. |
|
1173 |
^ true |
|
1174 |
]. |
|
1175 |
paletteAlphaEntries := nil. |
|
2707 | 1176 |
^ true |
3900 | 1177 |
|
3904 | 1178 |
"Modified (format): / 17-02-2017 / 16:57:27 / cg" |
2707 | 1179 |
! |
1180 |
||
308 | 1181 |
processZTXTChunkLen:len |
3762 | 1182 |
"/ compressed text |
1183 |
||
1184 |
"/ currently ignored - not needed to display png images |
|
308 | 1185 |
inStream skip:len. |
1186 |
^ true |
|
1187 |
||
1188 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
1189 |
! ! |
|
1190 |
||
3900 | 1191 |
!PNGReader methodsFor:'reading-private filtering'! |
2696 | 1192 |
|
2909 | 1193 |
filterAverage:count |
2699 | 1194 |
"Use the average of the pixel to the left and the pixel above as a predictor" |
2909 | 1195 |
|
1196 |
|delta| |
|
2699 | 1197 |
|
3609 | 1198 |
%{ |
1199 |
if (__isByteArray(__INST(thisScanline)) |
|
1200 |
&& __isByteArray(__INST(prevScanline)) |
|
1201 |
&& __isSmallInteger(__INST(depth)) |
|
1202 |
&& __isSmallInteger(count)) { |
|
1203 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
1204 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
1205 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
1206 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
1207 |
INT __count = __intVal(count); |
|
1208 |
INT __delta = __intVal(__INST(depth)) / 8; |
|
1209 |
int __i; |
|
1210 |
||
1211 |
if (__delta < 1) __delta = 1; |
|
1212 |
for (__i=0; __i<__delta; __i++) { |
|
1213 |
__thisScanline[__i] += (__prevScanline[__i] >> 1); |
|
1214 |
} |
|
1215 |
for (; __i < __count; __i++) { |
|
1216 |
__thisScanline[__i] += ((__prevScanline[__i] + __thisScanline[__i-__delta]) >> 1); |
|
1217 |
} |
|
1218 |
RETURN(self); |
|
1219 |
} |
|
1220 |
%}. |
|
1221 |
||
1222 |
delta := depth // 8 max:1. |
|
2909 | 1223 |
1 to:delta do:[:i | |
3384 | 1224 |
thisScanline at:i put:((thisScanline at:i) + ((prevScanline at:i) // 2) bitAnd:255) |
2909 | 1225 |
]. |
1226 |
delta + 1 to:count do:[:i | |
|
3384 | 1227 |
thisScanline at:i put:((thisScanline at:i) + (((prevScanline at:i) + (thisScanline at:i - delta)) // 2) bitAnd:255) |
2909 | 1228 |
] |
1229 |
||
1230 |
"Modified: / 03-05-2011 / 12:14:01 / cg" |
|
2699 | 1231 |
! |
1232 |
||
2696 | 1233 |
filterHorizontal:count |
1234 |
"use the pixel to the left as a predictor" |
|
1235 |
||
1236 |
|delta| |
|
1237 |
||
3608 | 1238 |
%{ |
1239 |
if (__isByteArray(__INST(thisScanline)) |
|
1240 |
&& __isByteArray(__INST(prevScanline)) |
|
1241 |
&& __isSmallInteger(__INST(depth)) |
|
1242 |
&& __isSmallInteger(count)) { |
|
1243 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
1244 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
1245 |
INT __count = __intVal(count); |
|
1246 |
INT __delta = __intVal(__INST(depth)) / 8; |
|
1247 |
int __i; |
|
1248 |
||
1249 |
if (__delta < 1) __delta = 1; |
|
1250 |
for (__i = __delta; __i < __count; __i++) { |
|
1251 |
__thisScanline[__i] = __thisScanline[__i] + __thisScanline[__i-__delta]; |
|
1252 |
} |
|
1253 |
RETURN(self); |
|
1254 |
} |
|
1255 |
%}. |
|
1256 |
||
1257 |
delta := depth // 8 max:1. |
|
2696 | 1258 |
delta+1 to:count do:[:i| |
1259 |
thisScanline at:i put:(((thisScanline at:i)+ (thisScanline at:i-delta)) bitAnd:255) |
|
1260 |
] |
|
4099 | 1261 |
|
1262 |
"Modified: / 02-06-2018 / 06:50:57 / Claus Gittinger" |
|
2696 | 1263 |
! |
1264 |
||
2699 | 1265 |
filterNone: count |
2909 | 1266 |
"/ no filter - scanline is as is |
1267 |
||
1268 |
"Modified: / 03-05-2011 / 12:14:20 / cg" |
|
2699 | 1269 |
! |
1270 |
||
2909 | 1271 |
filterPaeth:count |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1272 |
"Select one of (the pixel to the left, the pixel above and the pixel to above left) to |
2909 | 1273 |
predict the value of this pixel" |
1274 |
||
1275 |
|delta| |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1276 |
|
3607 | 1277 |
%{ |
1278 |
if (__isByteArray(__INST(thisScanline)) |
|
1279 |
&& __isByteArray(__INST(prevScanline)) |
|
1280 |
&& __isSmallInteger(__INST(depth)) |
|
1281 |
&& __isSmallInteger(count)) { |
|
1282 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
1283 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
1284 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
1285 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
1286 |
INT __count = __intVal(count); |
|
1287 |
INT __delta = __intVal(__INST(depth)) / 8; |
|
1288 |
int __i; |
|
1289 |
||
1290 |
if (__delta < 1) __delta = 1; |
|
1291 |
for (__i=0; __i<__delta; __i++) { |
|
1292 |
unsigned int __pix = __thisScanline[__i] + __prevScanline[__i]; |
|
1293 |
__thisScanline[__i] = __pix; |
|
1294 |
} |
|
1295 |
for (; __i < __count; __i++) { |
|
1296 |
int __pCenter = __thisScanline[__i]; |
|
1297 |
int __pLeft = __thisScanline[__i - __delta]; |
|
1298 |
int __pAbove = __prevScanline[__i]; |
|
1299 |
int __pAboveLeft = __prevScanline[__i - __delta]; |
|
1300 |
int __pa, __pb, __pc; |
|
1301 |
int __p; |
|
1302 |
||
1303 |
__pa = (__pAbove - __pAboveLeft); |
|
1304 |
__pa = __pa < 0 ? -__pa : __pa; |
|
1305 |
__pb = (__pLeft - __pAboveLeft); |
|
1306 |
__pb = __pb < 0 ? -__pb : __pb; |
|
1307 |
__pc = __pLeft + __pAbove - __pAboveLeft - __pAboveLeft; |
|
1308 |
__pc = __pc < 0 ? -__pc : __pc; |
|
1309 |
if ((__pa <= __pb) && (__pa <= __pc)) __p = __pLeft; |
|
1310 |
else if (__pb <= __pc) __p = __pAbove; |
|
1311 |
else __p = __pAboveLeft; |
|
1312 |
||
1313 |
__thisScanline[__i] = __pCenter + __p; |
|
1314 |
} |
|
1315 |
RETURN(self); |
|
1316 |
} |
|
1317 |
%}. |
|
1318 |
||
1319 |
delta := depth // 8 max:1. |
|
2909 | 1320 |
1 to:delta do:[:i | |
1321 |
thisScanline |
|
1322 |
at:i |
|
1323 |
put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255) |
|
1324 |
]. |
|
1325 |
delta + 1 to:count do:[:i | |
|
1326 |
thisScanline |
|
1327 |
at:i |
|
1328 |
put:(((thisScanline at:i) + (self |
|
1329 |
paethPredictLeft:(thisScanline at:i - delta) |
|
1330 |
above:(prevScanline at:i) |
|
1331 |
aboveLeft:(prevScanline at:i - delta))) |
|
1332 |
bitAnd:255) |
|
1333 |
] |
|
1334 |
||
1335 |
"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
|
1336 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1337 |
|
2696 | 1338 |
filterScanline:filterType count:count |
3612 | 1339 |
filterType == 0 ifTrue:[^ self]. |
1340 |
||
2696 | 1341 |
self |
3612 | 1342 |
perform:(#(filterHorizontal: filterVertical: filterAverage: filterPaeth:) at:filterType) |
2696 | 1343 |
with:count |
2909 | 1344 |
|
1345 |
"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
|
1346 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1347 |
|
2909 | 1348 |
filterVertical:count |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1349 |
"Use the pixel above as a predictor" |
2909 | 1350 |
|
3608 | 1351 |
%{ |
1352 |
if (__isByteArray(__INST(thisScanline)) |
|
1353 |
&& __isByteArray(__INST(prevScanline)) |
|
1354 |
&& __isSmallInteger(count)) { |
|
1355 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
1356 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
1357 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
1358 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
1359 |
INT __count = __intVal(count); |
|
1360 |
int __i; |
|
1361 |
||
1362 |
for (__i=0; __i<__count; __i++) { |
|
1363 |
unsigned int __pix = __thisScanline[__i] + __prevScanline[__i]; |
|
1364 |
__thisScanline[__i] = __pix; |
|
1365 |
} |
|
1366 |
RETURN(self); |
|
1367 |
} |
|
1368 |
%}. |
|
1369 |
||
2909 | 1370 |
1 to:count do:[:i | |
1371 |
thisScanline |
|
1372 |
at:i |
|
1373 |
put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255) |
|
1374 |
] |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1375 |
|
2909 | 1376 |
"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
|
1377 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1378 |
|
3607 | 1379 |
paethPredictLeft: l above: a aboveLeft: al |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1380 |
"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
|
1381 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1382 |
| pa pb pc | |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1383 |
|
3607 | 1384 |
pa := a > al ifTrue: [a - al] ifFalse: [al - a]. |
1385 |
pb := l > al ifTrue: [l - al] ifFalse: [al - l]. |
|
1386 |
pc := l + a - al - al. |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1387 |
pc < 0 ifTrue: [ |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1388 |
pc := pc * -1 |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1389 |
]. |
3607 | 1390 |
((pa <= pb) and: [pa <= pc]) ifTrue: [^ l]. |
1391 |
(pb <= pc) ifTrue: [^ a]. |
|
1392 |
^ al |
|
2696 | 1393 |
! ! |
1394 |
||
3900 | 1395 |
!PNGReader methodsFor:'reading-private pixel copy'! |
2699 | 1396 |
|
2909 | 1397 |
copyPixels:y at:startX by:incX |
2704 | 1398 |
"Handle interlaced pixels of supported colorTypes" |
2909 | 1399 |
|
1400 |
|s| |
|
2699 | 1401 |
|
3598 | 1402 |
"/ per color type copy methods |
1403 |
s := #( |
|
3900 | 1404 |
#copyPixelsGray:at:by: "/ 0 = ColorTypeGray |
2909 | 1405 |
nil |
3900 | 1406 |
#copyPixelsRGB:at:by: "/ 2 = ColorTypeRGB |
1407 |
#copyPixelsIndexed:at:by: "/ 3 = ColorTypePalette |
|
1408 |
#copyPixelsGrayAlpha:at:by: "/ 4 = ColorTypeGrayAlpha |
|
2909 | 1409 |
nil |
3900 | 1410 |
#copyPixelsRGBA:at:by: "/ 6 = ColorTypeRGBA |
2909 | 1411 |
) at:colorType + 1. |
3437 | 1412 |
self perform:s with:y with:startX with:incX |
1848 | 1413 |
|
2909 | 1414 |
"Modified: / 03-05-2011 / 12:02:45 / cg" |
3900 | 1415 |
"Modified (comment): / 16-02-2017 / 19:42:47 / cg" |
3327 | 1416 |
! |
1417 |
||
3598 | 1418 |
copyPixelsGray:y at:startX by:incX |
1419 |
"Handle interlaced pixels of supported colorTypes. |
|
1420 |
Untested code - please verify" |
|
1421 |
||
3611 | 1422 |
|srcIndex "{ Class: SmallInteger }" |
1423 |
srcMask "{ Class: SmallInteger }" |
|
1424 |
nPixels "{ Class: SmallInteger }" |
|
1425 |
dstIndex "{ Class: SmallInteger }" |
|
1426 |
dstMask "{ Class: SmallInteger }" |
|
1427 |
x "{ Class: SmallInteger }" |
|
1428 |
bits "{ Class: SmallInteger }" |
|
1429 |
bitMask "{ Class: SmallInteger }" |
|
1430 |
rS "{ Class: SmallInteger }" |
|
1431 |
lS "{ Class: SmallInteger }" |
|
1432 |
rowIndex "{ Class: SmallInteger }"| |
|
3598 | 1433 |
|
1434 |
nPixels := width // incX. |
|
1435 |
srcIndex := 0. srcMask := 0. x := startX. |
|
1436 |
||
1437 |
depth == 1 ifTrue:[ |
|
1438 |
dstIndex := (y * bytesPerScanline) + (startX // 8) + 1. |
|
1439 |
dstMask := 16r80 >> (x \\ 8). |
|
1440 |
||
1441 |
1 to:nPixels do:[:cnt | |
|
1442 |
srcMask == 0 ifTrue:[ |
|
1443 |
srcMask := 16r80. |
|
1444 |
srcIndex := srcIndex + 1. |
|
1445 |
bits := thisScanline at:srcIndex. |
|
1446 |
]. |
|
1447 |
bitMask := (bits bitAnd:srcMask) == 0 ifTrue:[0] ifFalse:[dstMask]. |
|
1448 |
bitMask ~~ 0 ifTrue:[ |
|
1449 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
1450 |
]. |
|
1451 |
x := x + incX. |
|
1452 |
dstMask := dstMask >> incX. |
|
1453 |
dstMask == 0 ifTrue:[ |
|
1454 |
dstIndex := dstIndex + 1. |
|
1455 |
dstMask := 16r80 >> (x \\ 8). |
|
1456 |
]. |
|
1457 |
srcMask := srcMask bitShift:-1. |
|
1458 |
]. |
|
1459 |
^ self. |
|
1460 |
]. |
|
1461 |
||
1462 |
depth == 2 ifTrue:[ |
|
1463 |
dstIndex := (y * bytesPerScanline) + (startX // 4) + 1. |
|
1464 |
lS := 6 - ((x \\ 4) * 2). |
|
1465 |
rS := -1. |
|
1466 |
1 to:nPixels do:[:cnt | |
|
1467 |
rS < 0 ifTrue:[ |
|
1468 |
srcIndex := srcIndex + 1. |
|
1469 |
bits := thisScanline at:srcIndex. |
|
1470 |
rS := 6. |
|
1471 |
]. |
|
1472 |
bitMask := ((bits >> rS) bitAnd:2r11). |
|
1473 |
bitMask ~~ 0 ifTrue:[ |
|
1474 |
bitMask := bitMask << lS. |
|
1475 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
1476 |
]. |
|
1477 |
lS := lS - (incX * 2). |
|
1478 |
lS < 0 ifTrue:[ |
|
1479 |
lS <= -8 ifTrue:[ |
|
1480 |
dstIndex := dstIndex + 2. |
|
1481 |
lS := lS + 8 + 8. |
|
1482 |
] ifFalse:[ |
|
1483 |
dstIndex := dstIndex + 1. |
|
1484 |
lS := lS + 8. |
|
1485 |
]. |
|
1486 |
]. |
|
1487 |
rS := rS - 2. |
|
1488 |
]. |
|
1489 |
^ self. |
|
1490 |
]. |
|
1491 |
||
1492 |
depth == 4 ifTrue:[ |
|
1493 |
rowIndex := (y * bytesPerScanline) + 1. |
|
1494 |
dstIndex := rowIndex + (x // 2). |
|
1495 |
rS := -1. |
|
1496 |
1 to:nPixels do:[:cnt | |
|
1497 |
rS < 0 ifTrue:[ |
|
1498 |
srcIndex := srcIndex + 1. |
|
1499 |
bits := thisScanline at:srcIndex. |
|
1500 |
rS := 4. |
|
1501 |
]. |
|
1502 |
bitMask := ((bits >> rS) bitAnd:2r1111). |
|
1503 |
x even ifTrue:[ |
|
1504 |
bitMask := bitMask bitShift:4. |
|
1505 |
]. |
|
1506 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
1507 |
x := x + incX. |
|
1508 |
dstIndex := rowIndex + (x // 2). |
|
1509 |
rS := rS - 4. |
|
1510 |
]. |
|
1511 |
^ self. |
|
1512 |
]. |
|
1513 |
||
1514 |
depth == 8 ifTrue:[ |
|
1515 |
srcIndex := 1. |
|
1516 |
dstIndex := (y * bytesPerScanline) + (startX) + 1. |
|
1517 |
||
1518 |
1 to:nPixels do:[:n | |
|
1519 |
data at:dstIndex put:(thisScanline at:srcIndex). |
|
1520 |
srcIndex := srcIndex + 1. |
|
1521 |
dstIndex := dstIndex + incX |
|
1522 |
]. |
|
1523 |
^ self. |
|
1524 |
]. |
|
1525 |
||
1526 |
depth == 16 ifTrue:[ |
|
1527 |
srcIndex := 1. |
|
1528 |
dstIndex := (y * bytesPerScanline) + (startX*2) + 1. |
|
1529 |
||
1530 |
1 to:nPixels do:[:n | |
|
3601 | 1531 |
data unsignedInt16At:dstIndex put:(thisScanline unsignedInt16At:srcIndex). |
3598 | 1532 |
srcIndex := srcIndex + 2. |
1533 |
dstIndex := dstIndex + (incX * 2) |
|
1534 |
]. |
|
1535 |
^ self. |
|
1536 |
]. |
|
1537 |
self error:'unsupported depth'. |
|
1538 |
! |
|
1539 |
||
4017 | 1540 |
copyPixelsGrayAlpha:y at:startX by:incX |
1541 |
"Handle interlaced pixels of supported colorTypes." |
|
1542 |
||
1543 |
|srcIndex "{ Class: SmallInteger }" |
|
1544 |
srcMask "{ Class: SmallInteger }" |
|
1545 |
nPixels "{ Class: SmallInteger }" |
|
1546 |
dstIndex "{ Class: SmallInteger }" |
|
1547 |
dstMask "{ Class: SmallInteger }" |
|
1548 |
x "{ Class: SmallInteger }" |
|
1549 |
bits "{ Class: SmallInteger }" |
|
1550 |
bitMask "{ Class: SmallInteger }" |
|
1551 |
rS "{ Class: SmallInteger }" |
|
1552 |
lS "{ Class: SmallInteger }" |
|
1553 |
rowIndex "{ Class: SmallInteger }" |
|
1554 |
pix alpha gray| |
|
1555 |
||
1556 |
nPixels := width // incX. |
|
1557 |
srcIndex := 0. srcMask := 0. x := startX. |
|
1558 |
||
1559 |
"/ depth == 1 ifTrue:[ |
|
1560 |
"/ dstIndex := (y * bytesPerScanline) + (startX // 8) + 1. |
|
1561 |
"/ dstMask := 16r80 >> (x \\ 8). |
|
1562 |
"/ |
|
1563 |
"/ 1 to:nPixels do:[:cnt | |
|
1564 |
"/ srcMask == 0 ifTrue:[ |
|
1565 |
"/ srcMask := 16r80. |
|
1566 |
"/ srcIndex := srcIndex + 1. |
|
1567 |
"/ bits := thisScanline at:srcIndex. |
|
1568 |
"/ ]. |
|
1569 |
"/ bitMask := (bits bitAnd:srcMask) == 0 ifTrue:[0] ifFalse:[dstMask]. |
|
1570 |
"/ bitMask ~~ 0 ifTrue:[ |
|
1571 |
"/ data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
1572 |
"/ ]. |
|
1573 |
"/ x := x + incX. |
|
1574 |
"/ dstMask := dstMask >> incX. |
|
1575 |
"/ dstMask == 0 ifTrue:[ |
|
1576 |
"/ dstIndex := dstIndex + 1. |
|
1577 |
"/ dstMask := 16r80 >> (x \\ 8). |
|
1578 |
"/ ]. |
|
1579 |
"/ srcMask := srcMask bitShift:-1. |
|
1580 |
"/ ]. |
|
1581 |
"/ ^ self. |
|
1582 |
"/ ]. |
|
1583 |
"/ |
|
1584 |
"/ depth == 2 ifTrue:[ |
|
1585 |
"/ dstIndex := (y * bytesPerScanline) + (startX // 4) + 1. |
|
1586 |
"/ lS := 6 - ((x \\ 4) * 2). |
|
1587 |
"/ rS := -1. |
|
1588 |
"/ 1 to:nPixels do:[:cnt | |
|
1589 |
"/ rS < 0 ifTrue:[ |
|
1590 |
"/ srcIndex := srcIndex + 1. |
|
1591 |
"/ bits := thisScanline at:srcIndex. |
|
1592 |
"/ rS := 6. |
|
1593 |
"/ ]. |
|
1594 |
"/ bitMask := ((bits >> rS) bitAnd:2r11). |
|
1595 |
"/ bitMask ~~ 0 ifTrue:[ |
|
1596 |
"/ bitMask := bitMask << lS. |
|
1597 |
"/ data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
1598 |
"/ ]. |
|
1599 |
"/ lS := lS - (incX * 2). |
|
1600 |
"/ lS < 0 ifTrue:[ |
|
1601 |
"/ lS <= -8 ifTrue:[ |
|
1602 |
"/ dstIndex := dstIndex + 2. |
|
1603 |
"/ lS := lS + 8 + 8. |
|
1604 |
"/ ] ifFalse:[ |
|
1605 |
"/ dstIndex := dstIndex + 1. |
|
1606 |
"/ lS := lS + 8. |
|
1607 |
"/ ]. |
|
1608 |
"/ ]. |
|
1609 |
"/ rS := rS - 2. |
|
1610 |
"/ ]. |
|
1611 |
"/ ^ self. |
|
1612 |
"/ ]. |
|
1613 |
"/ |
|
1614 |
"/ depth == 4 ifTrue:[ |
|
1615 |
"/ rowIndex := (y * bytesPerScanline) + 1. |
|
1616 |
"/ dstIndex := rowIndex + (x // 2). |
|
1617 |
"/ rS := -1. |
|
1618 |
"/ 1 to:nPixels do:[:cnt | |
|
1619 |
"/ rS < 0 ifTrue:[ |
|
1620 |
"/ srcIndex := srcIndex + 1. |
|
1621 |
"/ bits := thisScanline at:srcIndex. |
|
1622 |
"/ rS := 4. |
|
1623 |
"/ ]. |
|
1624 |
"/ bitMask := ((bits >> rS) bitAnd:2r1111). |
|
1625 |
"/ x even ifTrue:[ |
|
1626 |
"/ bitMask := bitMask bitShift:4. |
|
1627 |
"/ ]. |
|
1628 |
"/ data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
1629 |
"/ x := x + incX. |
|
1630 |
"/ dstIndex := rowIndex + (x // 2). |
|
1631 |
"/ rS := rS - 4. |
|
1632 |
"/ ]. |
|
1633 |
"/ ^ self. |
|
1634 |
"/ ]. |
|
1635 |
"/ |
|
1636 |
"/ depth == 8 ifTrue:[ |
|
1637 |
"/ srcIndex := 1. |
|
1638 |
"/ dstIndex := (y * bytesPerScanline) + (startX) + 1. |
|
1639 |
"/ |
|
1640 |
"/ 1 to:nPixels do:[:n | |
|
1641 |
"/ data at:dstIndex put:(thisScanline at:srcIndex). |
|
1642 |
"/ srcIndex := srcIndex + 1. |
|
1643 |
"/ dstIndex := dstIndex + incX |
|
1644 |
"/ ]. |
|
1645 |
"/ ^ self. |
|
1646 |
"/ ]. |
|
1647 |
||
1648 |
forceRGB ifTrue:[ |
|
1649 |
(depth == 16 and:[depthImage == 32]) ifTrue:[ |
|
1650 |
"/ converting gray8+alpha8 to r8+g8+b8+alpha8 |
|
1651 |
||
1652 |
srcIndex := 1. |
|
1653 |
dstIndex := (y * (width * 4)) + (startX*4) + 1. |
|
1654 |
||
1655 |
1 to:nPixels do:[:n | |
|
4099 | 1656 |
pix := thisScanline unsignedInt16At:srcIndex MSB:true. |
4017 | 1657 |
alpha := pix bitAnd:16rFF. |
1658 |
gray := (pix rightShift:8) bitAnd:16rFF. |
|
1659 |
"/ put r-g-b-a |
|
1660 |
data at:dstIndex put:gray. |
|
1661 |
data at:dstIndex+1 put:gray. |
|
1662 |
data at:dstIndex+2 put:gray. |
|
1663 |
data at:dstIndex+3 put:alpha. |
|
1664 |
||
1665 |
srcIndex := srcIndex + 2. |
|
1666 |
dstIndex := dstIndex + (incX * 4) |
|
1667 |
]. |
|
1668 |
^ self. |
|
1669 |
]. |
|
1670 |
(depth == 32 and:[depthImage == 32]) ifTrue:[ |
|
1671 |
"/ converting gray16+alpha16 to r8+g8+b8+alpha8 |
|
1672 |
||
1673 |
srcIndex := 1. |
|
1674 |
dstIndex := (y * (width * 4)) + (startX*4) + 1. |
|
1675 |
||
1676 |
1 to:nPixels do:[:n | |
|
4099 | 1677 |
pix := thisScanline unsignedInt32At:srcIndex MSB:true. |
4017 | 1678 |
|
1679 |
alpha := pix bitAnd:16rFFFF. |
|
1680 |
gray := (pix rightShift:16) bitAnd:16rFFFF. |
|
1681 |
"/ reduce to 8 bit |
|
1682 |
alpha := alpha rightShift:8. |
|
1683 |
gray := gray rightShift:8. |
|
1684 |
||
1685 |
"/ put r-g-b-a |
|
1686 |
data at:dstIndex put:gray. |
|
1687 |
data at:dstIndex+1 put:gray. |
|
1688 |
data at:dstIndex+2 put:gray. |
|
1689 |
data at:dstIndex+3 put:alpha. |
|
1690 |
||
1691 |
srcIndex := srcIndex + 4. |
|
1692 |
dstIndex := dstIndex + (incX * 4) |
|
1693 |
]. |
|
1694 |
^ self. |
|
1695 |
]. |
|
1696 |
]. |
|
1697 |
self error:'unsupported depth'. |
|
1698 |
||
1699 |
"Created: / 17-09-2017 / 14:20:24 / cg" |
|
1700 |
"Modified: / 17-09-2017 / 15:28:38 / cg" |
|
4099 | 1701 |
"Modified: / 02-06-2018 / 07:08:15 / Claus Gittinger" |
4017 | 1702 |
! |
1703 |
||
3598 | 1704 |
copyPixelsIndexed:y at:startX by:incX |
1705 |
self copyPixelsGray:y at:startX by:incX |
|
1706 |
! |
|
1707 |
||
1708 |
copyPixelsRGB:y at:startX by:incX |
|
1709 |
"Handle interlaced pixels of supported colorTypes. |
|
1710 |
Untested code - please verify" |
|
1711 |
||
3611 | 1712 |
|srcIndex "{ Class: SmallInteger }" |
1713 |
nPixels "{ Class: SmallInteger }" |
|
1714 |
dstIndex "{ Class: SmallInteger }" |
|
1715 |
dstInc "{ Class: SmallInteger }" |
|
1716 |
bpp "{ Class: SmallInteger }"| |
|
3598 | 1717 |
|
1718 |
bpp := bytesPerScanline // width. |
|
1719 |
||
1720 |
"/ 1 byte per r,g,b |
|
1721 |
srcIndex := 1. |
|
1722 |
dstIndex := (y * bytesPerScanline) + (startX * bpp) + 1. |
|
1723 |
dstInc := incX * bpp. |
|
1724 |
||
1725 |
nPixels := width // incX. |
|
1726 |
1 to:nPixels do:[:n | |
|
1727 |
data replaceFrom:dstIndex to:dstIndex+bpp-1 with:thisScanline startingAt:srcIndex. |
|
1728 |
srcIndex := srcIndex + bpp. |
|
1729 |
dstIndex := dstIndex + dstInc |
|
1730 |
]. |
|
1731 |
! |
|
1732 |
||
3327 | 1733 |
copyPixelsRGBA:y at:startX by:incX |
1734 |
"Handle interlaced pixels of supported colorTypes. |
|
1735 |
Untested code - please verify" |
|
1736 |
||
3611 | 1737 |
|srcIndex "{ Class: SmallInteger }" |
1738 |
nPixels "{ Class: SmallInteger }" |
|
1739 |
dstIndex "{ Class: SmallInteger }" |
|
1740 |
dstInc "{ Class: SmallInteger }"| |
|
3327 | 1741 |
|
1742 |
srcIndex := 1. |
|
3437 | 1743 |
dstIndex := (y * bytesPerScanline) + (startX * 4) + 1. |
1744 |
dstInc := incX * 4. |
|
3327 | 1745 |
|
1746 |
nPixels := thisScanline size. |
|
1747 |
[srcIndex < nPixels] whileTrue:[ |
|
3437 | 1748 |
data replaceFrom:dstIndex to:dstIndex+3 with:thisScanline startingAt:srcIndex. |
3327 | 1749 |
srcIndex := srcIndex + 4. |
3437 | 1750 |
dstIndex := dstIndex + dstInc |
3327 | 1751 |
]. |
2699 | 1752 |
! ! |
1753 |
||
3900 | 1754 |
!PNGReader methodsFor:'writing'! |
2683 | 1755 |
|
1756 |
save:image onStream:aStream |
|
1757 |
"save image in PNG-file-format onto aStream" |
|
1758 |
||
1759 |
outStream := aStream. |
|
1760 |
outStream binary. |
|
1761 |
||
2703 | 1762 |
byteOrder := #msb. |
4004 | 1763 |
|
2683 | 1764 |
width := image width. |
1765 |
height := image height. |
|
1766 |
photometric := image photometric. |
|
1767 |
samplesPerPixel := image samplesPerPixel. |
|
1768 |
bitsPerSample := image bitsPerSample. |
|
1769 |
colorMap := image colorMap. |
|
1770 |
data := image bits. |
|
3893 | 1771 |
mask := image mask. |
1772 |
depth := self bitsPerPixel. |
|
3900 | 1773 |
|
3909 | 1774 |
outStream nextPutAll:(self class pngHeader). |
3893 | 1775 |
|
3900 | 1776 |
image mask notNil ifTrue:[ |
3972 | 1777 |
((photometric ~~ #rgb) and:[(photometric ~~ #rgba) and:[(photometric ~~ #argb)]]) ifTrue: [ |
3949 | 1778 |
self determinePaletteIndexForMaskedPixels |
3904 | 1779 |
] ifFalse:[ |
1780 |
photometric := #rgba. |
|
1781 |
]. |
|
3900 | 1782 |
]. |
1783 |
||
2703 | 1784 |
self writeIHDRChunk. |
1785 |
photometric == #palette ifTrue: [self writePaletteChunk]. |
|
3900 | 1786 |
paletteIndexForMaskedPixels notNil ifTrue: [self writeTRNSChunk]. |
2703 | 1787 |
self writeImageDataChunk. |
4203 | 1788 |
specialChunks notNil ifTrue:[self writeSpecialChunks]. |
2683 | 1789 |
self writeEndChunk |
3893 | 1790 |
|
3972 | 1791 |
"Modified: / 23-08-2017 / 16:25:00 / cg" |
4004 | 1792 |
"Modified (format): / 31-08-2017 / 17:26:28 / cg" |
4203 | 1793 |
"Modified: / 27-10-2018 / 12:13:03 / Claus Gittinger" |
3900 | 1794 |
! ! |
1795 |
||
1796 |
!PNGReader methodsFor:'writing-private'! |
|
1797 |
||
1798 |
determinePaletteIndexForMaskedPixels |
|
1799 |
"/ if all masked pixels are 0, and 0 is not used elsewhere in the image, |
|
1800 |
"/ write it as such. |
|
1801 |
"/ Otherwise, find an unallocated palette index, and assign masked pixels to it. |
|
1802 |
||
1803 |
|usedPixels freePixels pixelIdx| |
|
1804 |
||
1805 |
colorMap size < 256 ifTrue:[ |
|
1806 |
paletteIndexForMaskedPixels := colorMap size. |
|
1807 |
] ifFalse:[ |
|
1808 |
usedPixels := data usedValues. |
|
1809 |
usedPixels size == 256 ifTrue:[ |
|
1810 |
self error:'cannot represent image (no palette slot for mask)' |
|
1811 |
]. |
|
1812 |
freePixels := (0 to:255) asSet removeAllFoundIn:usedPixels. |
|
1813 |
paletteIndexForMaskedPixels := freePixels first. |
|
1814 |
]. |
|
1815 |
||
4181 | 1816 |
depth == 8 ifTrue:[ |
1817 |
"/ rewrite data: wherever masked, change pixel to paletteIndexForMaskedPixels |
|
1818 |
data := data copy. |
|
1819 |
pixelIdx := 1. |
|
1820 |
0 to:height-1 do:[:y | |
|
1821 |
0 to:width-1 do:[:x | |
|
1822 |
(mask pixelAtX:x y:y) == 0 ifTrue:[ |
|
1823 |
data at:pixelIdx put:paletteIndexForMaskedPixels. |
|
1824 |
]. |
|
1825 |
pixelIdx := pixelIdx + 1 |
|
1826 |
] |
|
1827 |
]. |
|
3900 | 1828 |
]. |
1829 |
||
1830 |
" |
|
1831 |
PNGReader save:(ToolbarIconLibrary systemBrowserIcon) onFile:'/tmp/icon.png' |
|
1832 |
ImageEditor openOnFile:'/tmp/icon.png' |
|
1833 |
" |
|
1834 |
||
1835 |
"Created: / 16-02-2017 / 19:59:39 / cg" |
|
1836 |
"Modified: / 17-02-2017 / 09:25:57 / cg" |
|
4181 | 1837 |
"Modified: / 24-09-2018 / 10:39:34 / Claus Gittinger" |
3900 | 1838 |
! ! |
1839 |
||
1840 |
!PNGReader methodsFor:'writing-private chunks'! |
|
2683 | 1841 |
|
3949 | 1842 |
determineColorTypeAndHasMask |
4004 | 1843 |
"sets colorType as side effect; |
1844 |
returns boolean if a mask is present" |
|
1845 |
||
3949 | 1846 |
((photometric == #whiteIs0) or:[ (photometric == #blackIs0)]) ifTrue:[ |
1847 |
colorType := ColorTypeGray. |
|
1848 |
^ (samplesPerPixel > 1) |
|
1849 |
]. |
|
1850 |
(photometric == #rgb) ifTrue:[ |
|
1851 |
colorType := ColorTypeRGB. |
|
1852 |
^ (samplesPerPixel > 3) |
|
1853 |
]. |
|
1854 |
(photometric == #rgba) ifTrue:[ |
|
1855 |
colorType := ColorTypeRGBAlpha. |
|
1856 |
^ (samplesPerPixel > 3) |
|
1857 |
]. |
|
1858 |
(photometric == #palette) ifTrue:[ |
|
1859 |
colorType := ColorTypePalette. |
|
1860 |
^ (samplesPerPixel > 1) |
|
1861 |
]. |
|
1862 |
self error:'unhandled photometric: ',photometric asString. |
|
1863 |
^ false. |
|
1864 |
||
1865 |
"Created: / 11-04-2017 / 12:39:30 / cg" |
|
4004 | 1866 |
"Modified (comment): / 31-08-2017 / 17:24:31 / cg" |
3949 | 1867 |
! |
1868 |
||
2703 | 1869 |
writeChunk:chunkTypeChars size:len with:aBlock |
2708 | 1870 |
|crc realOutStream chunkBytes| |
1871 |
||
1872 |
realOutStream := outStream. |
|
1873 |
outStream := WriteStream on:(ByteArray new:len). |
|
1874 |
||
2703 | 1875 |
outStream nextPutBytes:4 from:chunkTypeChars startingAt:1. |
1876 |
aBlock value. |
|
2708 | 1877 |
|
1878 |
chunkBytes := outStream contents. |
|
1879 |
crc := CRC32Stream hashValueOf:chunkBytes. |
|
1880 |
||
1881 |
outStream := realOutStream. |
|
1882 |
||
3554 | 1883 |
realOutStream nextPutInt32:len MSB:true. |
2708 | 1884 |
realOutStream nextPutAll:chunkBytes. |
3554 | 1885 |
realOutStream nextPutInt32:crc MSB:true. |
2703 | 1886 |
! |
2683 | 1887 |
|
2703 | 1888 |
writeEndChunk |
1889 |
self |
|
1890 |
writeChunk: 'IEND' |
|
1891 |
size: 0 |
|
1892 |
with:[ ] |
|
2683 | 1893 |
! |
1894 |
||
2703 | 1895 |
writeIHDRChunk |
4005 | 1896 |
|hasMask bitsPerChannel| |
3893 | 1897 |
|
3949 | 1898 |
hasMask := self determineColorTypeAndHasMask. |
3893 | 1899 |
hasMask ifTrue:[ |
3900 | 1900 |
"/ make it grayAlpha / rgba |
1901 |
((colorType == ColorTypeGray) or:[colorType == ColorTypeGray]) ifTrue:[ |
|
1902 |
colorType := colorType bitOr:4. "/ +alpha |
|
1903 |
]. |
|
3893 | 1904 |
]. |
4005 | 1905 |
|
1906 |
"bitsperchannel" |
|
1907 |
depth <= 8 ifTrue:[ |
|
1908 |
bitsPerChannel := depth |
|
1909 |
] ifFalse:[ |
|
4010 | 1910 |
(colorType == ColorTypeRGB or:[colorType == ColorTypeRGBAlpha]) ifTrue:[ |
4005 | 1911 |
"/ all channels must have the same nr of bits |
1912 |
self assert:(bitsPerSample asSet size == 1). |
|
1913 |
bitsPerChannel := bitsPerSample first |
|
1914 |
] ifFalse:[ |
|
1915 |
bitsPerChannel := depth |
|
1916 |
] |
|
1917 |
]. |
|
3893 | 1918 |
|
2683 | 1919 |
self |
1920 |
writeChunk: 'IHDR' |
|
3949 | 1921 |
size: (4+4+5) |
2683 | 1922 |
with:[ |
4004 | 1923 |
outStream nextPutInt32:width MSB:true. |
1924 |
outStream nextPutInt32:height MSB:true. |
|
4005 | 1925 |
outStream nextPut: bitsPerChannel. |
2703 | 1926 |
outStream nextPut: colorType. |
2683 | 1927 |
outStream nextPut: 0. "Compression" |
1928 |
outStream nextPut: 0. "Filter method" |
|
1929 |
outStream nextPut: 0 "Non-interlaced" |
|
1930 |
] |
|
3893 | 1931 |
|
4010 | 1932 |
"Modified: / 04-09-2017 / 18:34:15 / cg" |
2683 | 1933 |
! |
1934 |
||
2703 | 1935 |
writeImageDataChunk |
3893 | 1936 |
|compressedByteStream compressedBytes zlibWriter idx |
3935 | 1937 |
row32 bytesPerPixel| |
2703 | 1938 |
|
1939 |
bytesPerScanline := self bytesPerRow. |
|
3821 | 1940 |
|
1941 |
compressedByteStream := WriteStream on:(ByteArray new:(bytesPerScanline*height)). |
|
1942 |
zlibWriter := ZipStream writeOpenAsZipStreamOn:compressedByteStream suppressHeaderAndChecksum:false. |
|
1943 |
zlibWriter binary. |
|
2703 | 1944 |
|
3900 | 1945 |
(mask notNil and:[photometric ~~ #palette]) ifTrue:[ |
3935 | 1946 |
(depth == 32) ifTrue:[ |
1947 |
bytesPerPixel := 4 |
|
1948 |
] ifFalse:[ |
|
1949 |
"/ for now - only support depth24 + mask |
|
1950 |
self assert:(depth == 24). |
|
1951 |
bytesPerPixel := 3. |
|
1952 |
]. |
|
3893 | 1953 |
|
3935 | 1954 |
"/ on-the-fly place mask into the alpha channel. |
3893 | 1955 |
row32 := ByteArray new:(4 * width). |
3935 | 1956 |
self assert:(bytesPerScanline == (bytesPerPixel * width)). |
1957 |
||
3893 | 1958 |
idx := 1. |
1959 |
0 to:height-1 do:[:y | |
|
1960 |
|dstIdx maskRow| |
|
2708 | 1961 |
|
3893 | 1962 |
"/ expand rgb to rgba |
1963 |
maskRow := mask rowAt:y. |
|
3935 | 1964 |
|
3893 | 1965 |
dstIdx := 1. |
1966 |
1 to:width do:[:x | |
|
1967 |
row32 at:dstIdx put:(data at:idx). |
|
1968 |
row32 at:dstIdx+1 put:(data at:idx+1). |
|
1969 |
row32 at:dstIdx+2 put:(data at:idx+2). |
|
1970 |
row32 at:dstIdx+3 put:((maskRow at:x) == 0 ifTrue:[0] ifFalse:[16rFF]). |
|
3935 | 1971 |
idx := idx + bytesPerPixel. |
3893 | 1972 |
dstIdx := dstIdx + 4. |
1973 |
]. |
|
3935 | 1974 |
|
3893 | 1975 |
zlibWriter nextPutAll:#[0]. "/ no filter |
1976 |
zlibWriter nextPutAll:row32. |
|
1977 |
]. |
|
1978 |
] ifFalse:[ |
|
1979 |
idx := 1. |
|
1980 |
0 to:height-1 do:[:y | |
|
1981 |
|row| |
|
1982 |
||
1983 |
row := data copyFrom:idx to:(idx+bytesPerScanline-1). |
|
1984 |
zlibWriter nextPutAll:#[0]. "/ no filter |
|
1985 |
zlibWriter nextPutAll:row. |
|
1986 |
idx := idx + bytesPerScanline. |
|
1987 |
]. |
|
2703 | 1988 |
]. |
2708 | 1989 |
|
1990 |
zlibWriter close. |
|
1991 |
compressedBytes := compressedByteStream contents. |
|
1992 |
||
1993 |
self |
|
1994 |
writeChunk: 'IDAT' |
|
1995 |
size:(compressedBytes size) |
|
1996 |
with:[ |
|
1997 |
outStream nextPutAll:compressedBytes |
|
1998 |
] |
|
3893 | 1999 |
|
3935 | 2000 |
"Modified: / 25-02-2017 / 14:27:34 / cg" |
3949 | 2001 |
"Modified (format): / 11-04-2017 / 12:41:22 / cg" |
2703 | 2002 |
! |
2003 |
||
2683 | 2004 |
writePaletteChunk |
2005 |
self |
|
2006 |
writeChunk: 'PLTE' |
|
2007 |
size: 3 * colorMap size |
|
2008 |
with:[ |
|
2703 | 2009 |
colorMap do:[:color | |
2010 |
outStream |
|
2011 |
nextPut: color redByte; |
|
2012 |
nextPut: color greenByte; |
|
2013 |
nextPut: color blueByte |
|
2683 | 2014 |
] |
2015 |
] |
|
3900 | 2016 |
! |
2017 |
||
4203 | 2018 |
writeSpecialChunks |
2019 |
specialChunks keysAndValuesDo:[:chunkType :chunkData | |
|
2020 |
self |
|
2021 |
writeChunk:chunkType |
|
2022 |
size:chunkData size |
|
2023 |
with:[ |
|
2024 |
outStream nextPutBytes:chunkData |
|
2025 |
] |
|
2026 |
]. |
|
2027 |
||
2028 |
"Created: / 27-10-2018 / 12:14:02 / Claus Gittinger" |
|
2029 |
! |
|
2030 |
||
3900 | 2031 |
writeTRNSChunk |
2032 |
self |
|
2033 |
writeChunk: 'tRNS' |
|
2034 |
size: 1 |
|
2035 |
with:[ |
|
2036 |
outStream nextPut: paletteIndexForMaskedPixels |
|
2037 |
] |
|
2038 |
||
2039 |
"Created: / 16-02-2017 / 19:58:06 / cg" |
|
2683 | 2040 |
! ! |
2041 |
||
398 | 2042 |
!PNGReader class methodsFor:'documentation'! |
308 | 2043 |
|
2044 |
version |
|
3554 | 2045 |
^ '$Header$' |
2909 | 2046 |
! |
2047 |
||
2048 |
version_CVS |
|
3554 | 2049 |
^ '$Header$' |
308 | 2050 |
! ! |
1671 | 2051 |
|
3100 | 2052 |
|
308 | 2053 |
PNGReader initialize! |