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