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