author | mawalch |
Wed, 28 Sep 2016 13:20:15 +0200 | |
changeset 3750 | 73f907f202bc |
parent 3724 | 4a753c4710c9 |
child 3727 | 35e32e0e69f5 |
child 3762 | 036884e46f66 |
permissions | -rw-r--r-- |
3724 | 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 |
3612 | 19 |
instanceVariableNames:'colorType bitsPerChannel depth compressionMethod filterMethod |
20 |
interlaceMode bytesPerScanline globalDataChunk thisScanline |
|
21 |
prevScanline' |
|
2707 | 22 |
classVariableNames:'Verbose' |
308 | 23 |
poolDictionaries:'' |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1694
diff
changeset
|
24 |
category:'Graphics-Images-Readers' |
308 | 25 |
! |
26 |
||
398 | 27 |
!PNGReader class methodsFor:'documentation'! |
308 | 28 |
|
29 |
copyright |
|
30 |
" |
|
31 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
32 |
All Rights Reserved |
|
33 |
||
34 |
This software is furnished under a license and may be used |
|
35 |
only in accordance with the terms of that license and with the |
|
36 |
inclusion of the above copyright notice. This software may not |
|
37 |
be provided or otherwise made available to, or used by, any |
|
38 |
other person. No title to or ownership of the software is |
|
39 |
hereby transferred. |
|
40 |
" |
|
41 |
||
42 |
||
43 |
! |
|
44 |
||
45 |
documentation |
|
46 |
" |
|
2725 | 47 |
This class will provide methods for loading and saving PNG pictures. |
3599 | 48 |
It is currenty unfinished (some interlaced image formats are unsupported, for example: grayscale+alpha) |
3437 | 49 |
In the meantime, use a pngtoXXX converter for interlaced images, and read XXX. |
308 | 50 |
|
2725 | 51 |
[caveats:] |
52 |
writer ignores any mask (for now). |
|
3599 | 53 |
writer only generates unfiltered non-interlaced data |
2725 | 54 |
|
308 | 55 |
[See also:] |
56 |
Image Form Icon |
|
57 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader |
|
58 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
|
59 |
XBMReader XPMReader XWDReader |
|
60 |
||
61 |
[author:] |
|
62 |
Claus Gittinger |
|
63 |
" |
|
1694 | 64 |
! |
308 | 65 |
|
1694 | 66 |
examples |
67 |
" |
|
68 |
PNGReader fromFile:'/home/cg/AudioExplorer_51_files/use_small.png' |
|
2509 | 69 |
PNGReader fromFile:'\\Exeptn\tmp\images\expeccoScreenshot4020_5526507.png' |
2683 | 70 |
|
71 |
PNGReader fromFile:'C:\Users\cg\Desktop\croquet\cobalt-base-current-build-20090210\cobalt-base-current-build-20090210\content\models\textures\checkerboard.png' |
|
2695 | 72 |
PNGReader fromFile:'C:\Dokumente und Einstellungen\cg\Desktop\misc\PNGs\Delete.png' |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
73 |
PNGReader fromFile:'\\exeptn\unsaved\pd_stuff\PNGs\Delete.png' |
2699 | 74 |
PNGReader fromFile:'\\exeptn\unsaved\pd_stuff\PNGs\Down.png' |
2703 | 75 |
|
76 |
||
77 |
|img img2 outStream png| |
|
2725 | 78 |
|
2703 | 79 |
img := ToolbarIconLibrary error32x32Icon. |
80 |
outStream := WriteStream on:(ByteArray new:100). |
|
81 |
PNGReader save:img onStream:outStream. |
|
82 |
png := outStream contents. |
|
83 |
img2 := PNGReader fromStream:(png readStream). |
|
2708 | 84 |
self assert:(img bits = img2 bits). |
85 |
img inspect. |
|
2703 | 86 |
img2 inspect. |
1694 | 87 |
" |
308 | 88 |
! ! |
89 |
||
398 | 90 |
!PNGReader class methodsFor:'initialization'! |
308 | 91 |
|
92 |
initialize |
|
2699 | 93 |
"install myself in the Image classes fileFormat table for the `.png' extension." |
308 | 94 |
|
2709 | 95 |
MIMETypes defineImageType:'image/png' suffix:'png' reader:self. |
96 |
"/ backward compatibility from times when png was not yet so common... |
|
97 |
MIMETypes defineImageType:'image/x-png' suffix:nil reader:self. |
|
308 | 98 |
! ! |
99 |
||
2705 | 100 |
!PNGReader class methodsFor:'queries'! |
101 |
||
102 |
canRepresent:anImage |
|
103 |
"return true, if anImage can be represented in my file format. |
|
104 |
Any image is supported." |
|
105 |
||
106 |
^ true |
|
107 |
! ! |
|
108 |
||
398 | 109 |
!PNGReader class methodsFor:'testing'! |
308 | 110 |
|
111 |
isValidImageFile:aFileName |
|
112 |
"return true, if aFileName contains a PNG image" |
|
113 |
||
114 |
|inStream magic| |
|
115 |
||
116 |
inStream := self streamReadingFile:aFileName. |
|
117 |
inStream isNil ifTrue:[^ false]. |
|
118 |
||
119 |
inStream binary. |
|
120 |
||
121 |
magic := ByteArray new:8. |
|
122 |
inStream nextBytes:8 into:magic. |
|
123 |
inStream close. |
|
124 |
||
125 |
^ (magic = #[137 80 78 71 13 10 26 10]) |
|
126 |
||
1694 | 127 |
" |
128 |
self isValidImageFile:'/home/cg/AudioExplorer_51_files/use_small.png' |
|
2683 | 129 |
self isValidImageFile:'C:\Users\cg\Desktop\croquet\cobalt-base-current-build-20090210\cobalt-base-current-build-20090210\content\models\textures\checkerboard.png' |
2695 | 130 |
self isValidImageFile:'C:\Dokumente und Einstellungen\cg\Desktop\misc\PNGs\Delete.png' |
1694 | 131 |
" |
132 |
||
308 | 133 |
"Modified: 21.6.1996 / 20:38:46 / cg" |
134 |
! ! |
|
135 |
||
2696 | 136 |
!PNGReader methodsFor:'private-chunks'! |
308 | 137 |
|
2699 | 138 |
doPass: pass |
139 |
"Certain interlace passes are skipped with certain small image dimensions" |
|
140 |
||
141 |
pass = 1 ifTrue: [ ^ true ]. |
|
142 |
((width = 1) and: [height = 1]) ifTrue: [ ^ false ]. |
|
143 |
pass = 2 ifTrue: [ ^ width >= 5 ]. |
|
144 |
pass = 3 ifTrue: [ ^ height >= 5 ]. |
|
145 |
pass = 4 ifTrue: [ ^ (width >=3 ) or: [height >= 5] ]. |
|
146 |
pass = 5 ifTrue: [ ^ height >=3 ]. |
|
147 |
pass = 6 ifTrue: [ ^ width >=2 ]. |
|
148 |
pass = 7 ifTrue: [ ^ height >=2 ]. |
|
149 |
||
150 |
self error:'invalid argument'. |
|
151 |
^ true |
|
152 |
! |
|
153 |
||
308 | 154 |
processBKGDChunkLen:len |
155 |
inStream skip:len. |
|
156 |
^ true |
|
157 |
||
158 |
"Created: 21.6.1996 / 21:15:49 / cg" |
|
159 |
! |
|
160 |
||
2707 | 161 |
processCHRMChunkLen:len |
162 |
"cHRM Primary chromaticities chunk - currently unhandled" |
|
163 |
||
164 |
|whitePointX whitePointY redX redY greenX greenY blueX blueY| |
|
165 |
||
3554 | 166 |
whitePointX := (inStream nextInt32MSB:true) / 100000.0. |
167 |
whitePointY := (inStream nextInt32MSB:true) / 100000.0. |
|
168 |
redX := (inStream nextInt32MSB:true) / 100000.0. |
|
169 |
redY := (inStream nextInt32MSB:true) / 100000.0. |
|
170 |
greenX := (inStream nextInt32MSB:true) / 100000.0. |
|
171 |
greenY := (inStream nextInt32MSB:true) / 100000.0. |
|
172 |
blueX := (inStream nextInt32MSB:true) / 100000.0. |
|
173 |
blueY := (inStream nextInt32MSB:true) / 100000.0. |
|
2707 | 174 |
|
175 |
Verbose == true ifTrue:[ |
|
176 |
'PNGReader: CHRM chunk ignored:' infoPrintCR. |
|
177 |
'PNGReader: whitePointX:' infoPrint. whitePointX infoPrintCR. |
|
178 |
'PNGReader: whitePointY:' infoPrint. whitePointX infoPrintCR. |
|
179 |
'PNGReader: redX:' infoPrint. redX infoPrintCR. |
|
180 |
'PNGReader: redY:' infoPrint. redY infoPrintCR. |
|
181 |
'PNGReader: greenX:' infoPrint. greenX infoPrintCR. |
|
182 |
'PNGReader: greenY:' infoPrint. greenY infoPrintCR. |
|
183 |
'PNGReader: blueX:' infoPrint. blueX infoPrintCR. |
|
184 |
'PNGReader: blueY:' infoPrint. blueY infoPrintCR. |
|
185 |
]. |
|
186 |
^ true |
|
187 |
! |
|
188 |
||
308 | 189 |
processChunk:type len:len |
2509 | 190 |
|chunk| |
191 |
||
192 |
type = 'IDAT' ifTrue:[ |
|
193 |
"---since the compressed data can span multiple |
|
194 |
chunks, stitch them all together first. later, |
|
195 |
if memory is an issue, we need to figure out how |
|
196 |
to do this on the fly---" |
|
197 |
chunk := inStream next:len. |
|
198 |
globalDataChunk := globalDataChunk isNil |
|
199 |
ifTrue: [chunk] |
|
200 |
ifFalse:[globalDataChunk,chunk]. |
|
201 |
^ true |
|
202 |
"/ ^ self processIDATChunkLen:len |
|
203 |
]. |
|
204 |
||
308 | 205 |
type = 'gAMA' ifTrue:[^ self processGAMAChunkLen:len]. |
206 |
type = 'sBIT' ifTrue:[^ self processSBITChunkLen:len]. |
|
207 |
type = 'tEXt' ifTrue:[^ self processTEXTChunkLen:len]. |
|
208 |
type = 'tIME' ifTrue:[^ self processTIMEChunkLen:len]. |
|
209 |
type = 'bKGD' ifTrue:[^ self processBKGDChunkLen:len]. |
|
210 |
type = 'zTXt' ifTrue:[^ self processZTXTChunkLen:len]. |
|
211 |
type = 'PLTE' ifTrue:[^ self processPLTEChunkLen:len]. |
|
212 |
||
1671 | 213 |
type = 'pHYs' ifTrue:[^ self processPHYSChunkLen:len]. |
2707 | 214 |
type = 'cHRM' ifTrue:[^ self processCHRMChunkLen:len]. |
1671 | 215 |
type = 'tRNS' ifTrue:[^ self processTRNSChunkLen:len]. |
216 |
type = 'IHDR' ifTrue:[^ self processIHDRChunkLen:len]. |
|
217 |
||
308 | 218 |
('PNGReader: unrecognized chunk: ' , type , ' ignored.') infoPrintCR. |
219 |
||
220 |
inStream skip:len. |
|
221 |
^ true. |
|
222 |
||
223 |
"Created: 21.6.1996 / 21:10:37 / cg" |
|
224 |
"Modified: 21.6.1996 / 21:22:37 / cg" |
|
225 |
! |
|
226 |
||
227 |
processGAMAChunkLen:len |
|
228 |
inStream skip:len. |
|
229 |
^ true |
|
230 |
||
231 |
"Created: 21.6.1996 / 21:10:52 / cg" |
|
232 |
! |
|
233 |
||
2509 | 234 |
processGlobalIDATChunk |
235 |
interlaceMode == 0 ifTrue: [ |
|
236 |
^ self processNonInterlacedGlobalDATA |
|
237 |
]. |
|
238 |
^ self processInterlacedGlobalDATA |
|
239 |
! |
|
240 |
||
308 | 241 |
processIDATChunkLen:len |
1671 | 242 |
interlaceMode == 0 ifTrue: [ |
243 |
^ self processNonInterlacedDATA:len |
|
244 |
]. |
|
245 |
^ self processInterlacedDATA:len |
|
246 |
! |
|
247 |
||
248 |
processInterlacedDATA:len |
|
308 | 249 |
inStream skip:len. |
250 |
^ true |
|
1671 | 251 |
! |
308 | 252 |
|
2699 | 253 |
processInterlacedGlobalDATA |
3327 | 254 |
"adam7 interlace method" |
255 |
||
3612 | 256 |
| zlibReader filter |
257 |
bytesPerPass startingCol colIncrement rowIncrement |
|
258 |
startingRow |
|
259 |
temp "filtersSeen" |
|
260 |
cx "{ Class: SmallInteger }" |
|
261 |
sc "{ Class: SmallInteger }" |
|
262 |
cy "{ Class: SmallInteger }" |
|
263 |
startRow "{ Class: SmallInteger }" |
|
264 |
w "{ Class: SmallInteger }" |
|
265 |
h "{ Class: SmallInteger }" |
|
266 |
| |
|
2699 | 267 |
|
3327 | 268 |
interlaceMode == 1 ifFalse: [ |
269 |
self error:'unsupported interlaced mode'. |
|
270 |
^ self |
|
271 |
]. |
|
2699 | 272 |
|
273 |
"/ filtersSeen := Set new. |
|
274 |
||
275 |
startingCol := #(0 4 0 2 0 1 0 ). |
|
3327 | 276 |
startingRow := #(0 0 4 0 2 0 1 ). |
2699 | 277 |
colIncrement := #(8 8 4 4 2 2 1 ). |
278 |
rowIncrement := #(8 8 8 4 4 2 2 ). |
|
279 |
||
2701 | 280 |
zlibReader := ZipStream readOpenAsZipStreamOn:(globalDataChunk readStream) suppressHeaderAndChecksum:false. |
2699 | 281 |
zlibReader binary. |
282 |
||
3612 | 283 |
h := height. |
284 |
w := width. |
|
2699 | 285 |
1 to: 7 do: [:pass | |
286 |
(self doPass: pass) ifTrue:[ |
|
287 |
cx := colIncrement at: pass. |
|
288 |
sc := startingCol at: pass. |
|
3327 | 289 |
cy := rowIncrement at: pass. |
3612 | 290 |
bytesPerPass := (((w - sc + cx - 1) // cx * depth) + 7) // 8. |
2699 | 291 |
prevScanline := ByteArray new: bytesPerPass. |
292 |
thisScanline := ByteArray new: bytesPerScanline. |
|
3612 | 293 |
startRow := startingRow at: pass. |
294 |
startRow to: h-1 by: cy do: [:y | |
|
3343 | 295 |
filter := zlibReader nextByte. |
2699 | 296 |
"/ filtersSeen add: filter. |
297 |
(filter isNil or: [(filter between: 0 and: 4) not]) |
|
298 |
ifTrue: [^ self]. |
|
299 |
zlibReader next: bytesPerPass into: thisScanline startingAt: 1. |
|
300 |
filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerPass ]. |
|
301 |
self copyPixels:y at:sc by:cx. |
|
302 |
temp := prevScanline. |
|
303 |
prevScanline := thisScanline. |
|
304 |
thisScanline := temp. |
|
305 |
] |
|
306 |
] |
|
307 |
]. |
|
308 |
zlibReader atEnd ifFalse:[self error:'Unexpected data']. |
|
2909 | 309 |
|
310 |
"Modified: / 03-05-2011 / 12:03:33 / cg" |
|
2699 | 311 |
! |
312 |
||
1671 | 313 |
processNonInterlacedDATA:len |
2701 | 314 |
self halt:'not used' |
315 |
"/ |
|
316 |
"/ | zlibReader filter temp prevScanline thisScanline bytesPerScanline filtersSeen| |
|
317 |
"/ |
|
318 |
"/ zlibReader := ZipStream readOpenAsZipStreamOn:inStream suppressHeaderAndChecksum:false. |
|
319 |
"/ "/ zlibReader := ZLibReadStream on:inStream from: 1 to:len. |
|
320 |
"/ |
|
321 |
"/ prevScanline := ByteArray new: self bytesPerRow. |
|
322 |
"/ thisScanline := ByteArray new: self bytesPerRow. |
|
323 |
"/ 0 to: height - 1 do:[:index | |
|
324 |
"/ filter := (zlibReader next: 1) first. |
|
325 |
"/ filtersSeen add: filter. |
|
326 |
"/ (filter isNil or: [(filter between: 0 and: 4) not]) ifTrue: [^self]. |
|
327 |
"/ thisScanline := zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
|
328 |
"/ self filterScanline: filter count: bytesPerScanline. |
|
329 |
"/ self copyPixels: index. |
|
330 |
"/ temp := prevScanline. |
|
331 |
"/ prevScanline := thisScanline. |
|
332 |
"/ thisScanline := temp |
|
333 |
"/ ] |
|
2509 | 334 |
! |
335 |
||
336 |
processNonInterlacedGlobalDATA |
|
2725 | 337 |
| "data n" zlibReader filter temp bytesPerScanline "filtersSeen" i| |
2696 | 338 |
|
339 |
"/ filtersSeen := Set new. |
|
2695 | 340 |
|
341 |
"/ data := ByteArray new:(self bytesPerRow * height)+1000. |
|
342 |
"/ n := ZipStream uncompress: globalDataChunk into: data. |
|
343 |
"/ self halt. |
|
2509 | 344 |
|
2701 | 345 |
zlibReader := ZipStream readOpenAsZipStreamOn:(globalDataChunk readStream) suppressHeaderAndChecksum:false. |
2696 | 346 |
zlibReader binary. |
2509 | 347 |
bytesPerScanline := self bytesPerRow. |
348 |
||
1671 | 349 |
prevScanline := ByteArray new: bytesPerScanline. |
350 |
thisScanline := ByteArray new: bytesPerScanline. |
|
2725 | 351 |
0 to: height - 1 do:[:y | |
3343 | 352 |
filter := zlibReader nextByte. |
2696 | 353 |
"/ filtersSeen add: filter. |
2725 | 354 |
(filter notNil and: [filter between: 0 and: 4]) ifFalse: [ |
3673 | 355 |
'PNGReader: unsupported filter: ' infoPrint. filter infoPrintCR. |
2725 | 356 |
^ self |
357 |
]. |
|
2696 | 358 |
zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
359 |
filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerScanline ]. |
|
2725 | 360 |
|
361 |
i := y * bytesPerScanline. |
|
362 |
data replaceFrom:i+1 to:(i+bytesPerScanline) with:thisScanline startingAt:1. |
|
363 |
||
2695 | 364 |
temp := prevScanline. |
365 |
prevScanline := thisScanline. |
|
366 |
thisScanline := temp |
|
367 |
] |
|
1671 | 368 |
! |
369 |
||
370 |
processPHYSChunkLen:len |
|
371 |
"physical pixel chunk - currently unhandled" |
|
372 |
||
2705 | 373 |
|pixelPerUnitX pixelPerUnitY unit| |
374 |
||
3554 | 375 |
pixelPerUnitX := inStream nextInt32MSB:true. |
376 |
pixelPerUnitY := inStream nextInt32MSB:true. |
|
2705 | 377 |
unit := inStream nextByte. |
378 |
||
2707 | 379 |
Verbose == true ifTrue:[ |
380 |
'PNGReader: PHYS chunk ignored:' infoPrintCR. |
|
381 |
'PNGReader: ppuX:' infoPrint. pixelPerUnitX infoPrintCR. |
|
382 |
'PNGReader: ppuY:' infoPrint. pixelPerUnitY infoPrintCR. |
|
383 |
'PNGReader: unit:' infoPrint. unit infoPrintCR. |
|
384 |
]. |
|
385 |
||
2705 | 386 |
^ true |
308 | 387 |
! |
388 |
||
389 |
processPLTEChunkLen:len |
|
1671 | 390 |
"read a color palette" |
391 |
||
2725 | 392 |
|n "{ Class: SmallInteger }" |
393 |
redBytes greenBytes blueBytes| |
|
308 | 394 |
|
395 |
(len \\ 3) ~~ 0 ifTrue:[ |
|
396 |
'PNGReader: invalid size of PLTE chunk' infoPrintCR. |
|
397 |
^ false |
|
398 |
]. |
|
399 |
n := len // 3. |
|
400 |
redBytes := ByteArray new:n. |
|
401 |
greenBytes := ByteArray new:n. |
|
402 |
blueBytes := ByteArray new:n. |
|
403 |
||
404 |
1 to:n do:[:i | |
|
405 |
redBytes at:i put:(inStream nextByte). |
|
406 |
greenBytes at:i put:(inStream nextByte). |
|
407 |
blueBytes at:i put:(inStream nextByte) |
|
408 |
]. |
|
409 |
||
2725 | 410 |
colorMap := MappedPalette |
411 |
redVector:redBytes |
|
412 |
greenVector:greenBytes |
|
413 |
blueVector:blueBytes. |
|
414 |
||
308 | 415 |
^ true |
416 |
||
417 |
"Created: 21.6.1996 / 21:22:28 / cg" |
|
418 |
"Modified: 21.6.1996 / 21:43:01 / cg" |
|
419 |
! |
|
420 |
||
421 |
processSBITChunkLen:len |
|
422 |
inStream skip:len. |
|
423 |
^ true |
|
424 |
||
425 |
"Created: 21.6.1996 / 21:13:09 / cg" |
|
426 |
! |
|
427 |
||
428 |
processTEXTChunkLen:len |
|
429 |
inStream skip:len. |
|
430 |
^ true |
|
431 |
||
432 |
"Modified: 21.6.1996 / 21:15:27 / cg" |
|
433 |
! |
|
434 |
||
435 |
processTIMEChunkLen:len |
|
436 |
inStream skip:len. |
|
437 |
^ true |
|
438 |
||
439 |
"Created: 21.6.1996 / 21:15:43 / cg" |
|
440 |
"Modified: 21.6.1996 / 21:20:42 / cg" |
|
441 |
! |
|
442 |
||
2707 | 443 |
processTRNSChunkLen:len |
444 |
inStream skip:len. |
|
445 |
^ true |
|
446 |
! |
|
447 |
||
308 | 448 |
processZTXTChunkLen:len |
449 |
inStream skip:len. |
|
450 |
^ true |
|
451 |
||
452 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
453 |
! ! |
|
454 |
||
2696 | 455 |
!PNGReader methodsFor:'private-filtering'! |
456 |
||
2909 | 457 |
filterAverage:count |
2699 | 458 |
"Use the average of the pixel to the left and the pixel above as a predictor" |
2909 | 459 |
|
460 |
|delta| |
|
2699 | 461 |
|
3609 | 462 |
%{ |
463 |
if (__isByteArray(__INST(thisScanline)) |
|
464 |
&& __isByteArray(__INST(prevScanline)) |
|
465 |
&& __isSmallInteger(__INST(depth)) |
|
466 |
&& __isSmallInteger(count)) { |
|
467 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
468 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
469 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
470 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
471 |
INT __count = __intVal(count); |
|
472 |
INT __delta = __intVal(__INST(depth)) / 8; |
|
473 |
int __i; |
|
474 |
||
475 |
if (__delta < 1) __delta = 1; |
|
476 |
for (__i=0; __i<__delta; __i++) { |
|
477 |
__thisScanline[__i] += (__prevScanline[__i] >> 1); |
|
478 |
} |
|
479 |
for (; __i < __count; __i++) { |
|
480 |
__thisScanline[__i] += ((__prevScanline[__i] + __thisScanline[__i-__delta]) >> 1); |
|
481 |
} |
|
482 |
RETURN(self); |
|
483 |
} |
|
484 |
%}. |
|
485 |
||
486 |
delta := depth // 8 max:1. |
|
2909 | 487 |
1 to:delta do:[:i | |
3384 | 488 |
thisScanline at:i put:((thisScanline at:i) + ((prevScanline at:i) // 2) bitAnd:255) |
2909 | 489 |
]. |
490 |
delta + 1 to:count do:[:i | |
|
3384 | 491 |
thisScanline at:i put:((thisScanline at:i) + (((prevScanline at:i) + (thisScanline at:i - delta)) // 2) bitAnd:255) |
2909 | 492 |
] |
493 |
||
494 |
"Modified: / 03-05-2011 / 12:14:01 / cg" |
|
2699 | 495 |
! |
496 |
||
2696 | 497 |
filterHorizontal:count |
498 |
"use the pixel to the left as a predictor" |
|
499 |
||
500 |
|delta| |
|
501 |
||
3608 | 502 |
%{ |
503 |
if (__isByteArray(__INST(thisScanline)) |
|
504 |
&& __isByteArray(__INST(prevScanline)) |
|
505 |
&& __isSmallInteger(__INST(depth)) |
|
506 |
&& __isSmallInteger(count)) { |
|
507 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
508 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
509 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
510 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
511 |
INT __count = __intVal(count); |
|
512 |
INT __delta = __intVal(__INST(depth)) / 8; |
|
513 |
int __i; |
|
514 |
||
515 |
if (__delta < 1) __delta = 1; |
|
516 |
for (__i = __delta; __i < __count; __i++) { |
|
517 |
__thisScanline[__i] = __thisScanline[__i] + __thisScanline[__i-__delta]; |
|
518 |
} |
|
519 |
RETURN(self); |
|
520 |
} |
|
521 |
%}. |
|
522 |
||
523 |
delta := depth // 8 max:1. |
|
2696 | 524 |
delta+1 to:count do:[:i| |
525 |
thisScanline at:i put:(((thisScanline at:i)+ (thisScanline at:i-delta)) bitAnd:255) |
|
526 |
] |
|
527 |
! |
|
528 |
||
2699 | 529 |
filterNone: count |
2909 | 530 |
"/ no filter - scanline is as is |
531 |
||
532 |
"Modified: / 03-05-2011 / 12:14:20 / cg" |
|
2699 | 533 |
! |
534 |
||
2909 | 535 |
filterPaeth:count |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
536 |
"Select one of (the pixel to the left, the pixel above and the pixel to above left) to |
2909 | 537 |
predict the value of this pixel" |
538 |
||
539 |
|delta| |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
540 |
|
3607 | 541 |
%{ |
542 |
if (__isByteArray(__INST(thisScanline)) |
|
543 |
&& __isByteArray(__INST(prevScanline)) |
|
544 |
&& __isSmallInteger(__INST(depth)) |
|
545 |
&& __isSmallInteger(count)) { |
|
546 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
547 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
548 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
549 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
550 |
INT __count = __intVal(count); |
|
551 |
INT __delta = __intVal(__INST(depth)) / 8; |
|
552 |
int __i; |
|
553 |
||
554 |
if (__delta < 1) __delta = 1; |
|
555 |
for (__i=0; __i<__delta; __i++) { |
|
556 |
unsigned int __pix = __thisScanline[__i] + __prevScanline[__i]; |
|
557 |
__thisScanline[__i] = __pix; |
|
558 |
} |
|
559 |
for (; __i < __count; __i++) { |
|
560 |
int __pCenter = __thisScanline[__i]; |
|
561 |
int __pLeft = __thisScanline[__i - __delta]; |
|
562 |
int __pAbove = __prevScanline[__i]; |
|
563 |
int __pAboveLeft = __prevScanline[__i - __delta]; |
|
564 |
int __pa, __pb, __pc; |
|
565 |
int __p; |
|
566 |
||
567 |
__pa = (__pAbove - __pAboveLeft); |
|
568 |
__pa = __pa < 0 ? -__pa : __pa; |
|
569 |
__pb = (__pLeft - __pAboveLeft); |
|
570 |
__pb = __pb < 0 ? -__pb : __pb; |
|
571 |
__pc = __pLeft + __pAbove - __pAboveLeft - __pAboveLeft; |
|
572 |
__pc = __pc < 0 ? -__pc : __pc; |
|
573 |
if ((__pa <= __pb) && (__pa <= __pc)) __p = __pLeft; |
|
574 |
else if (__pb <= __pc) __p = __pAbove; |
|
575 |
else __p = __pAboveLeft; |
|
576 |
||
577 |
__thisScanline[__i] = __pCenter + __p; |
|
578 |
} |
|
579 |
RETURN(self); |
|
580 |
} |
|
581 |
%}. |
|
582 |
||
583 |
delta := depth // 8 max:1. |
|
2909 | 584 |
1 to:delta do:[:i | |
585 |
thisScanline |
|
586 |
at:i |
|
587 |
put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255) |
|
588 |
]. |
|
589 |
delta + 1 to:count do:[:i | |
|
590 |
thisScanline |
|
591 |
at:i |
|
592 |
put:(((thisScanline at:i) + (self |
|
593 |
paethPredictLeft:(thisScanline at:i - delta) |
|
594 |
above:(prevScanline at:i) |
|
595 |
aboveLeft:(prevScanline at:i - delta))) |
|
596 |
bitAnd:255) |
|
597 |
] |
|
598 |
||
599 |
"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
|
600 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
601 |
|
2696 | 602 |
filterScanline:filterType count:count |
3612 | 603 |
filterType == 0 ifTrue:[^ self]. |
604 |
||
2696 | 605 |
self |
3612 | 606 |
perform:(#(filterHorizontal: filterVertical: filterAverage: filterPaeth:) at:filterType) |
2696 | 607 |
with:count |
2909 | 608 |
|
609 |
"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
|
610 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
611 |
|
2909 | 612 |
filterVertical:count |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
613 |
"Use the pixel above as a predictor" |
2909 | 614 |
|
3608 | 615 |
%{ |
616 |
if (__isByteArray(__INST(thisScanline)) |
|
617 |
&& __isByteArray(__INST(prevScanline)) |
|
618 |
&& __isSmallInteger(count)) { |
|
619 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
620 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
621 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
622 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
623 |
INT __count = __intVal(count); |
|
624 |
int __i; |
|
625 |
||
626 |
for (__i=0; __i<__count; __i++) { |
|
627 |
unsigned int __pix = __thisScanline[__i] + __prevScanline[__i]; |
|
628 |
__thisScanline[__i] = __pix; |
|
629 |
} |
|
630 |
RETURN(self); |
|
631 |
} |
|
632 |
%}. |
|
633 |
||
2909 | 634 |
1 to:count do:[:i | |
635 |
thisScanline |
|
636 |
at:i |
|
637 |
put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255) |
|
638 |
] |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
639 |
|
2909 | 640 |
"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
|
641 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
642 |
|
3607 | 643 |
paethPredictLeft: l above: a aboveLeft: al |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
644 |
"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
|
645 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
646 |
| pa pb pc | |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
647 |
|
3607 | 648 |
pa := a > al ifTrue: [a - al] ifFalse: [al - a]. |
649 |
pb := l > al ifTrue: [l - al] ifFalse: [al - l]. |
|
650 |
pc := l + a - al - al. |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
651 |
pc < 0 ifTrue: [ |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
652 |
pc := pc * -1 |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
653 |
]. |
3607 | 654 |
((pa <= pb) and: [pa <= pc]) ifTrue: [^ l]. |
655 |
(pb <= pc) ifTrue: [^ a]. |
|
656 |
^ al |
|
2696 | 657 |
! ! |
658 |
||
2699 | 659 |
!PNGReader methodsFor:'private-pixel copy'! |
660 |
||
2909 | 661 |
copyPixels:y at:startX by:incX |
2704 | 662 |
"Handle interlaced pixels of supported colorTypes" |
2909 | 663 |
|
664 |
|s| |
|
2699 | 665 |
|
3598 | 666 |
"/ per color type copy methods |
667 |
s := #( |
|
668 |
#copyPixelsGray:at:by: "/ 0 |
|
2909 | 669 |
nil |
3598 | 670 |
#copyPixelsRGB:at:by: "/ 2 |
671 |
#copyPixelsIndexed:at:by: "/ 3 |
|
672 |
#copyPixelsGrayAlpha:at:by: "/ 4 |
|
2909 | 673 |
nil |
3598 | 674 |
#copyPixelsRGBA:at:by: "/ 6 |
2909 | 675 |
) at:colorType + 1. |
3437 | 676 |
self perform:s with:y with:startX with:incX |
1848 | 677 |
|
2909 | 678 |
"Modified: / 03-05-2011 / 12:02:45 / cg" |
3327 | 679 |
! |
680 |
||
3598 | 681 |
copyPixelsGray:y at:startX by:incX |
682 |
"Handle interlaced pixels of supported colorTypes. |
|
683 |
Untested code - please verify" |
|
684 |
||
3611 | 685 |
|srcIndex "{ Class: SmallInteger }" |
686 |
srcMask "{ Class: SmallInteger }" |
|
687 |
nPixels "{ Class: SmallInteger }" |
|
688 |
dstIndex "{ Class: SmallInteger }" |
|
689 |
dstMask "{ Class: SmallInteger }" |
|
690 |
x "{ Class: SmallInteger }" |
|
691 |
bits "{ Class: SmallInteger }" |
|
692 |
bitMask "{ Class: SmallInteger }" |
|
693 |
rS "{ Class: SmallInteger }" |
|
694 |
lS "{ Class: SmallInteger }" |
|
695 |
rowIndex "{ Class: SmallInteger }"| |
|
3598 | 696 |
|
697 |
nPixels := width // incX. |
|
698 |
srcIndex := 0. srcMask := 0. x := startX. |
|
699 |
||
700 |
depth == 1 ifTrue:[ |
|
701 |
dstIndex := (y * bytesPerScanline) + (startX // 8) + 1. |
|
702 |
dstMask := 16r80 >> (x \\ 8). |
|
703 |
||
704 |
1 to:nPixels do:[:cnt | |
|
705 |
srcMask == 0 ifTrue:[ |
|
706 |
srcMask := 16r80. |
|
707 |
srcIndex := srcIndex + 1. |
|
708 |
bits := thisScanline at:srcIndex. |
|
709 |
]. |
|
710 |
bitMask := (bits bitAnd:srcMask) == 0 ifTrue:[0] ifFalse:[dstMask]. |
|
711 |
bitMask ~~ 0 ifTrue:[ |
|
712 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
713 |
]. |
|
714 |
x := x + incX. |
|
715 |
dstMask := dstMask >> incX. |
|
716 |
dstMask == 0 ifTrue:[ |
|
717 |
dstIndex := dstIndex + 1. |
|
718 |
dstMask := 16r80 >> (x \\ 8). |
|
719 |
]. |
|
720 |
srcMask := srcMask bitShift:-1. |
|
721 |
]. |
|
722 |
^ self. |
|
723 |
]. |
|
724 |
||
725 |
depth == 2 ifTrue:[ |
|
726 |
dstIndex := (y * bytesPerScanline) + (startX // 4) + 1. |
|
727 |
lS := 6 - ((x \\ 4) * 2). |
|
728 |
rS := -1. |
|
729 |
1 to:nPixels do:[:cnt | |
|
730 |
rS < 0 ifTrue:[ |
|
731 |
srcIndex := srcIndex + 1. |
|
732 |
bits := thisScanline at:srcIndex. |
|
733 |
rS := 6. |
|
734 |
]. |
|
735 |
bitMask := ((bits >> rS) bitAnd:2r11). |
|
736 |
bitMask ~~ 0 ifTrue:[ |
|
737 |
bitMask := bitMask << lS. |
|
738 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
739 |
]. |
|
740 |
lS := lS - (incX * 2). |
|
741 |
lS < 0 ifTrue:[ |
|
742 |
lS <= -8 ifTrue:[ |
|
743 |
dstIndex := dstIndex + 2. |
|
744 |
lS := lS + 8 + 8. |
|
745 |
] ifFalse:[ |
|
746 |
dstIndex := dstIndex + 1. |
|
747 |
lS := lS + 8. |
|
748 |
]. |
|
749 |
]. |
|
750 |
rS := rS - 2. |
|
751 |
]. |
|
752 |
^ self. |
|
753 |
]. |
|
754 |
||
755 |
depth == 4 ifTrue:[ |
|
756 |
rowIndex := (y * bytesPerScanline) + 1. |
|
757 |
dstIndex := rowIndex + (x // 2). |
|
758 |
rS := -1. |
|
759 |
1 to:nPixels do:[:cnt | |
|
760 |
rS < 0 ifTrue:[ |
|
761 |
srcIndex := srcIndex + 1. |
|
762 |
bits := thisScanline at:srcIndex. |
|
763 |
rS := 4. |
|
764 |
]. |
|
765 |
bitMask := ((bits >> rS) bitAnd:2r1111). |
|
766 |
x even ifTrue:[ |
|
767 |
bitMask := bitMask bitShift:4. |
|
768 |
]. |
|
769 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
770 |
x := x + incX. |
|
771 |
dstIndex := rowIndex + (x // 2). |
|
772 |
rS := rS - 4. |
|
773 |
]. |
|
774 |
^ self. |
|
775 |
]. |
|
776 |
||
777 |
depth == 8 ifTrue:[ |
|
778 |
srcIndex := 1. |
|
779 |
dstIndex := (y * bytesPerScanline) + (startX) + 1. |
|
780 |
||
781 |
1 to:nPixels do:[:n | |
|
782 |
data at:dstIndex put:(thisScanline at:srcIndex). |
|
783 |
srcIndex := srcIndex + 1. |
|
784 |
dstIndex := dstIndex + incX |
|
785 |
]. |
|
786 |
^ self. |
|
787 |
]. |
|
788 |
||
789 |
depth == 16 ifTrue:[ |
|
790 |
srcIndex := 1. |
|
791 |
dstIndex := (y * bytesPerScanline) + (startX*2) + 1. |
|
792 |
||
793 |
1 to:nPixels do:[:n | |
|
3601 | 794 |
data unsignedInt16At:dstIndex put:(thisScanline unsignedInt16At:srcIndex). |
3598 | 795 |
srcIndex := srcIndex + 2. |
796 |
dstIndex := dstIndex + (incX * 2) |
|
797 |
]. |
|
798 |
^ self. |
|
799 |
]. |
|
800 |
self error:'unsupported depth'. |
|
801 |
! |
|
802 |
||
803 |
copyPixelsIndexed:y at:startX by:incX |
|
804 |
self copyPixelsGray:y at:startX by:incX |
|
805 |
! |
|
806 |
||
807 |
copyPixelsRGB:y at:startX by:incX |
|
808 |
"Handle interlaced pixels of supported colorTypes. |
|
809 |
Untested code - please verify" |
|
810 |
||
3611 | 811 |
|srcIndex "{ Class: SmallInteger }" |
812 |
nPixels "{ Class: SmallInteger }" |
|
813 |
dstIndex "{ Class: SmallInteger }" |
|
814 |
dstInc "{ Class: SmallInteger }" |
|
815 |
bpp "{ Class: SmallInteger }"| |
|
3598 | 816 |
|
817 |
bpp := bytesPerScanline // width. |
|
818 |
||
819 |
"/ 1 byte per r,g,b |
|
820 |
srcIndex := 1. |
|
821 |
dstIndex := (y * bytesPerScanline) + (startX * bpp) + 1. |
|
822 |
dstInc := incX * bpp. |
|
823 |
||
824 |
nPixels := width // incX. |
|
825 |
1 to:nPixels do:[:n | |
|
826 |
data replaceFrom:dstIndex to:dstIndex+bpp-1 with:thisScanline startingAt:srcIndex. |
|
827 |
srcIndex := srcIndex + bpp. |
|
828 |
dstIndex := dstIndex + dstInc |
|
829 |
]. |
|
830 |
! |
|
831 |
||
3327 | 832 |
copyPixelsRGBA:y at:startX by:incX |
833 |
"Handle interlaced pixels of supported colorTypes. |
|
834 |
Untested code - please verify" |
|
835 |
||
3611 | 836 |
|srcIndex "{ Class: SmallInteger }" |
837 |
nPixels "{ Class: SmallInteger }" |
|
838 |
dstIndex "{ Class: SmallInteger }" |
|
839 |
dstInc "{ Class: SmallInteger }"| |
|
3327 | 840 |
|
841 |
srcIndex := 1. |
|
3437 | 842 |
dstIndex := (y * bytesPerScanline) + (startX * 4) + 1. |
843 |
dstInc := incX * 4. |
|
3327 | 844 |
|
845 |
nPixels := thisScanline size. |
|
846 |
[srcIndex < nPixels] whileTrue:[ |
|
3437 | 847 |
data replaceFrom:dstIndex to:dstIndex+3 with:thisScanline startingAt:srcIndex. |
3327 | 848 |
srcIndex := srcIndex + 4. |
3437 | 849 |
dstIndex := dstIndex + dstInc |
3327 | 850 |
]. |
2699 | 851 |
! ! |
852 |
||
853 |
!PNGReader methodsFor:'private-reading'! |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
854 |
|
2683 | 855 |
getChunk |
856 |
|len type crc| |
|
857 |
||
858 |
inStream atEnd ifTrue:[^ false]. |
|
859 |
||
3554 | 860 |
len := inStream nextInt32MSB:true. |
2683 | 861 |
type := String new:4. |
862 |
(inStream nextBytes:4 into:type) ~~ 4 ifTrue:[^ false]. |
|
863 |
||
2707 | 864 |
Verbose == true ifTrue:[ |
865 |
'len: ' infoPrint. len infoPrint. ' type: ' infoPrint. type infoPrintCR. |
|
866 |
]. |
|
2683 | 867 |
|
868 |
type = 'IEND' ifTrue:[^ false]. |
|
869 |
||
870 |
(self processChunk:type len:len) ifFalse:[^ false]. |
|
871 |
||
3554 | 872 |
crc := inStream nextInt32MSB:true. "/ ignored - for now |
2683 | 873 |
^ true |
874 |
||
875 |
"Created: 21.6.1996 / 21:09:36 / cg" |
|
876 |
"Modified: 21.6.1996 / 21:20:26 / cg" |
|
877 |
! |
|
878 |
||
879 |
getIHDRChunk |
|
880 |
|len type crc| |
|
881 |
||
3554 | 882 |
len := inStream nextInt32MSB:true. |
2683 | 883 |
|
884 |
type := String new:4. |
|
885 |
inStream nextBytes:4 into:type. |
|
886 |
||
3183 | 887 |
type = 'IHDR' ifFalse:[self halt:'expected IHDR magic'. ^ false]. |
888 |
len == 13 ifFalse:[self halt:'unexpected IHDR length'. ^ false]. |
|
2683 | 889 |
|
3554 | 890 |
width := inStream nextInt32MSB:true. |
891 |
height := inStream nextInt32MSB:true. |
|
2683 | 892 |
|
893 |
(width <= 0 or:[height <= 0]) ifTrue:[ |
|
894 |
'PNGReader: invalid dimension(s)' infoPrintCR. |
|
895 |
^ false. |
|
896 |
]. |
|
897 |
self reportDimension. |
|
898 |
||
3607 | 899 |
bitsPerChannel := inStream nextByte. |
900 |
depth := bitsPerChannel. "/ will be changed by setColorType. |
|
2683 | 901 |
colorType := inStream nextByte. |
902 |
||
903 |
compressionMethod := inStream nextByte. |
|
904 |
filterMethod := inStream nextByte. |
|
905 |
interlaceMode := inStream nextByte. |
|
906 |
||
2707 | 907 |
Verbose == true ifTrue:[ |
908 |
'PNGReader: IHDR:' infoPrintCR. |
|
909 |
'PNGReader: width: ' infoPrint. width infoPrintCR. |
|
910 |
'PNGReader: height: ' infoPrint. height infoPrintCR. |
|
3607 | 911 |
'PNGReader: bitsPerChannel: ' infoPrint. bitsPerChannel infoPrintCR. |
2707 | 912 |
'PNGReader: colorType: ' infoPrint. colorType infoPrintCR. |
913 |
'PNGReader: compressionMethod: ' infoPrint. compressionMethod infoPrintCR. |
|
914 |
'PNGReader: filterMethod: ' infoPrint. filterMethod infoPrintCR. |
|
915 |
'PNGReader: interlaceMode: ' infoPrint. interlaceMode infoPrintCR. |
|
916 |
]. |
|
917 |
||
3554 | 918 |
crc := inStream nextInt32MSB:true. |
2683 | 919 |
^ true |
920 |
||
921 |
"Modified: 21.6.1996 / 21:38:35 / cg" |
|
922 |
! |
|
923 |
||
1848 | 924 |
setColorType:colorType |
925 |
colorType == 0 ifTrue:[ |
|
926 |
photometric := #blackIs0. |
|
927 |
samplesPerPixel := 1. |
|
3607 | 928 |
bitsPerSample := Array with:bitsPerChannel. |
929 |
depth := bitsPerChannel. |
|
1848 | 930 |
^ true. |
931 |
]. |
|
932 |
||
933 |
colorType == 2 ifTrue:[ |
|
3607 | 934 |
bitsPerChannel < 8 ifTrue:[ |
1848 | 935 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
936 |
^ false. |
|
937 |
]. |
|
938 |
photometric := #rgb. |
|
939 |
samplesPerPixel := 3. |
|
3607 | 940 |
bitsPerSample := Array with:bitsPerChannel with:bitsPerChannel with:bitsPerChannel. |
941 |
depth := bitsPerChannel * 3. |
|
1848 | 942 |
^ true. |
943 |
]. |
|
944 |
||
945 |
colorType == 3 ifTrue:[ |
|
3607 | 946 |
bitsPerChannel == 16 ifTrue:[ |
1848 | 947 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
948 |
^ false. |
|
949 |
]. |
|
950 |
photometric := #palette. |
|
951 |
samplesPerPixel := 1. |
|
3607 | 952 |
bitsPerSample := Array with:bitsPerChannel. |
953 |
depth := bitsPerChannel. |
|
1848 | 954 |
^ true. |
955 |
]. |
|
956 |
||
957 |
colorType == 4 ifTrue:[ |
|
3607 | 958 |
bitsPerChannel < 8 ifTrue:[ |
1848 | 959 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
960 |
^ false. |
|
961 |
]. |
|
962 |
photometric := #blackIs0. |
|
963 |
samplesPerPixel := 2. |
|
3607 | 964 |
bitsPerSample := Array with:bitsPerChannel with:bitsPerChannel. |
965 |
depth := bitsPerChannel * 2. |
|
1848 | 966 |
^ true. |
967 |
]. |
|
968 |
||
969 |
colorType == 6 ifTrue:[ |
|
3607 | 970 |
bitsPerChannel < 8 ifTrue:[ |
1848 | 971 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
972 |
^ false. |
|
973 |
]. |
|
2702 | 974 |
photometric := #rgb. |
1848 | 975 |
samplesPerPixel := 4. |
3607 | 976 |
bitsPerSample := Array with:bitsPerChannel with:bitsPerChannel with:bitsPerChannel with:bitsPerChannel. |
977 |
depth := bitsPerChannel * 4. |
|
1848 | 978 |
^ true. |
979 |
]. |
|
980 |
||
981 |
('PNGReader: invalid colorType: ' , colorType printString , '.') infoPrintCR. |
|
982 |
^ false |
|
983 |
! ! |
|
984 |
||
1805 | 985 |
!PNGReader methodsFor:'reading'! |
308 | 986 |
|
987 |
fromStream:aStream |
|
988 |
"read a stream containing a PNG image. |
|
989 |
Leave image description in instance variables." |
|
990 |
||
991 |
|header| |
|
992 |
||
993 |
inStream := aStream. |
|
994 |
aStream binary. |
|
995 |
||
1671 | 996 |
"PNG-files are always msb (network-world)" |
308 | 997 |
byteOrder := #msb. |
998 |
||
999 |
header := ByteArray new:8. |
|
1000 |
aStream nextBytes:8 into:header. |
|
1001 |
||
2683 | 1002 |
header ~= (self pngHeader) ifTrue:[ |
308 | 1003 |
'PNGReader: not a png file.' infoPrintCR. |
3598 | 1004 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: not a png file'. |
308 | 1005 |
^ nil |
1006 |
]. |
|
2695 | 1007 |
|
308 | 1008 |
(self getIHDRChunk) ifFalse:[ |
1009 |
'PNGReader: required IHDR chunk missing.' infoPrintCR. |
|
3598 | 1010 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: required IHDR chunk missing'. |
308 | 1011 |
^ nil |
1012 |
]. |
|
1013 |
||
1014 |
compressionMethod ~~ 0 ifTrue:[ |
|
3598 | 1015 |
('PNGReader: compressionMethod %s not supported.' printfWith:compressionMethod printString) infoPrintCR. |
1016 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: unsupported compression method'. |
|
308 | 1017 |
^ nil |
1018 |
]. |
|
1019 |
filterMethod > 0 ifTrue:[ |
|
1020 |
'PNGReader: invalid filterMethod' infoPrintCR. |
|
3598 | 1021 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: invalid/unsupported filterMethod'. |
308 | 1022 |
^ nil. |
1023 |
]. |
|
1024 |
interlaceMode > 1 ifTrue:[ |
|
3598 | 1025 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: invalid/unsupported interlaceMode'. |
308 | 1026 |
'PNGReader: invalid interlaceMode' infoPrintCR. |
1027 |
^ nil. |
|
1028 |
]. |
|
1671 | 1029 |
(self setColorType:colorType) ifFalse:[ |
308 | 1030 |
^ nil |
1671 | 1031 |
]. |
2706 | 1032 |
|
1671 | 1033 |
[self getChunk] whileTrue. |
308 | 1034 |
|
3598 | 1035 |
bytesPerScanline := self bytesPerRow. |
1036 |
data := ByteArray new:(bytesPerScanline * height). |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1037 |
|
2509 | 1038 |
globalDataChunk notNil ifTrue:[ |
3343 | 1039 |
self processGlobalIDATChunk. |
1040 |
globalDataChunk := nil. |
|
2509 | 1041 |
]. |
1042 |
||
308 | 1043 |
photometric == #palette ifTrue:[ |
2725 | 1044 |
colorMap isNil ifTrue:[ |
3598 | 1045 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: palette chunk missing'. |
1046 |
'PNGReader: palette chunk missing.' infoPrintCR. |
|
2725 | 1047 |
"/ ^ nil |
308 | 1048 |
]. |
1049 |
]. |
|
1050 |
||
1051 |
" |
|
1052 |
PNGReader fromFile:'/home/cg/libpng-0.89c/pngtest.png' |
|
1053 |
" |
|
1054 |
||
2910
d5171b9245e6
comment/format in: #fromStream:
Claus Gittinger <cg@exept.de>
parents:
2909
diff
changeset
|
1055 |
"Modified: / 03-05-2011 / 12:17:34 / cg" |
1805 | 1056 |
! ! |
1057 |
||
2683 | 1058 |
!PNGReader methodsFor:'writing to file'! |
1059 |
||
1060 |
pngHeader |
|
1061 |
^ #[137 80 78 71 13 10 26 10] |
|
1062 |
! |
|
1063 |
||
1064 |
save:image onStream:aStream |
|
1065 |
"save image in PNG-file-format onto aStream" |
|
1066 |
||
1067 |
outStream := aStream. |
|
1068 |
outStream binary. |
|
1069 |
||
2703 | 1070 |
byteOrder := #msb. |
2683 | 1071 |
width := image width. |
1072 |
height := image height. |
|
1073 |
photometric := image photometric. |
|
1074 |
samplesPerPixel := image samplesPerPixel. |
|
1075 |
bitsPerSample := image bitsPerSample. |
|
1076 |
colorMap := image colorMap. |
|
1077 |
data := image bits. |
|
1078 |
||
2703 | 1079 |
outStream nextPutAll:(self pngHeader). |
1080 |
self writeIHDRChunk. |
|
1081 |
photometric == #palette ifTrue: [self writePaletteChunk]. |
|
1082 |
self writeImageDataChunk. |
|
2683 | 1083 |
self writeEndChunk |
1084 |
! |
|
1085 |
||
2703 | 1086 |
writeChunk:chunkTypeChars size:len with:aBlock |
2708 | 1087 |
|crc realOutStream chunkBytes| |
1088 |
||
1089 |
realOutStream := outStream. |
|
1090 |
outStream := WriteStream on:(ByteArray new:len). |
|
1091 |
||
2703 | 1092 |
outStream nextPutBytes:4 from:chunkTypeChars startingAt:1. |
1093 |
aBlock value. |
|
2708 | 1094 |
|
1095 |
chunkBytes := outStream contents. |
|
1096 |
crc := CRC32Stream hashValueOf:chunkBytes. |
|
1097 |
||
1098 |
outStream := realOutStream. |
|
1099 |
||
3554 | 1100 |
realOutStream nextPutInt32:len MSB:true. |
2708 | 1101 |
realOutStream nextPutAll:chunkBytes. |
3554 | 1102 |
realOutStream nextPutInt32:crc MSB:true. |
2703 | 1103 |
! |
2683 | 1104 |
|
2703 | 1105 |
writeEndChunk |
1106 |
self |
|
1107 |
writeChunk: 'IEND' |
|
1108 |
size: 0 |
|
1109 |
with:[ ] |
|
2683 | 1110 |
! |
1111 |
||
1112 |
writeFileHeader |
|
1113 |
outStream nextPutAll:(self pngHeader) |
|
1114 |
! |
|
1115 |
||
2703 | 1116 |
writeIHDRChunk |
3100 | 1117 |
((photometric == #whiteIs0) or:[ (photometric == #blackIs0)]) ifTrue:[ |
2703 | 1118 |
colorType := 0. |
3461 | 1119 |
samplesPerPixel > 1 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
2703 | 1120 |
]. |
3724 | 1121 |
((photometric == #rgb) or:[(photometric == #rgba)]) ifTrue:[ |
2703 | 1122 |
colorType := 2. |
3461 | 1123 |
samplesPerPixel > 3 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
2703 | 1124 |
]. |
1125 |
(photometric == #palette) ifTrue:[ |
|
1126 |
colorType := 3. |
|
3461 | 1127 |
samplesPerPixel > 1 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
2703 | 1128 |
]. |
3100 | 1129 |
colorType isNil ifTrue:[ |
3461 | 1130 |
self error:'unhandled photometric' |
3100 | 1131 |
]. |
2703 | 1132 |
|
2683 | 1133 |
self |
1134 |
writeChunk: 'IHDR' |
|
1135 |
size: 13 |
|
1136 |
with:[ |
|
1137 |
self writeLong:width. |
|
1138 |
self writeLong:height. |
|
2703 | 1139 |
outStream nextPut: 8. "Assume that all colors are 24bit" "/ depth |
1140 |
outStream nextPut: colorType. |
|
2683 | 1141 |
outStream nextPut: 0. "Compression" |
1142 |
outStream nextPut: 0. "Filter method" |
|
1143 |
outStream nextPut: 0 "Non-interlaced" |
|
1144 |
] |
|
1145 |
! |
|
1146 |
||
2703 | 1147 |
writeImageDataChunk |
2708 | 1148 |
|compressedByteStream compressedBytes zlibWriter bytesPerScanline idx| |
2703 | 1149 |
|
2708 | 1150 |
compressedByteStream := WriteStream on:(ByteArray new:100). |
1151 |
zlibWriter := ZipStream writeOpenAsZipStreamOn:compressedByteStream suppressHeaderAndChecksum:false. |
|
2703 | 1152 |
zlibWriter binary. |
1153 |
||
1154 |
bytesPerScanline := self bytesPerRow. |
|
1155 |
||
2708 | 1156 |
idx := 1. |
1157 |
0 to:height-1 do:[:y | |
|
1158 |
|row| |
|
1159 |
||
1160 |
row := data copyFrom:idx to:(idx+bytesPerScanline-1). |
|
1161 |
zlibWriter nextPutAll:#[0]. "/ no filter |
|
1162 |
zlibWriter nextPutAll:row. |
|
1163 |
idx := idx + bytesPerScanline. |
|
2703 | 1164 |
]. |
2708 | 1165 |
|
1166 |
zlibWriter close. |
|
1167 |
compressedBytes := compressedByteStream contents. |
|
1168 |
||
1169 |
self |
|
1170 |
writeChunk: 'IDAT' |
|
1171 |
size:(compressedBytes size) |
|
1172 |
with:[ |
|
1173 |
outStream nextPutAll:compressedBytes |
|
1174 |
] |
|
2703 | 1175 |
! |
1176 |
||
2683 | 1177 |
writePaletteChunk |
1178 |
self |
|
1179 |
writeChunk: 'PLTE' |
|
1180 |
size: 3 * colorMap size |
|
1181 |
with:[ |
|
2703 | 1182 |
colorMap do:[:color | |
1183 |
outStream |
|
1184 |
nextPut: color redByte; |
|
1185 |
nextPut: color greenByte; |
|
1186 |
nextPut: color blueByte |
|
2683 | 1187 |
] |
1188 |
] |
|
1189 |
! ! |
|
1190 |
||
398 | 1191 |
!PNGReader class methodsFor:'documentation'! |
308 | 1192 |
|
1193 |
version |
|
3554 | 1194 |
^ '$Header$' |
2909 | 1195 |
! |
1196 |
||
1197 |
version_CVS |
|
3554 | 1198 |
^ '$Header$' |
308 | 1199 |
! ! |
1671 | 1200 |
|
3100 | 1201 |
|
308 | 1202 |
PNGReader initialize! |