author | Claus Gittinger <cg@exept.de> |
Tue, 08 Mar 2016 18:23:24 +0100 | |
changeset 3599 | 9bc697bd01d9 |
parent 3598 | bdd7048a0f2e |
child 3600 | 5325c3ad7507 |
child 3601 | 05caf7f1e324 |
permissions | -rw-r--r-- |
3599 | 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 |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
19 |
instanceVariableNames:'colorType bitsPerChannel depth compressionMethod filterMethod |
2725 | 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 |
||
2699 | 256 |
| zlibReader filter bytesPerPass startingCol colIncrement rowIncrement |
3327 | 257 |
startingRow cx sc temp "filtersSeen" bitsPerPixel cy| |
2699 | 258 |
|
3327 | 259 |
interlaceMode == 1 ifFalse: [ |
260 |
self error:'unsupported interlaced mode'. |
|
261 |
^ self |
|
262 |
]. |
|
2699 | 263 |
|
264 |
"/ filtersSeen := Set new. |
|
265 |
||
266 |
startingCol := #(0 4 0 2 0 1 0 ). |
|
3327 | 267 |
startingRow := #(0 0 4 0 2 0 1 ). |
2699 | 268 |
colIncrement := #(8 8 4 4 2 2 1 ). |
269 |
rowIncrement := #(8 8 8 4 4 2 2 ). |
|
270 |
||
2701 | 271 |
zlibReader := ZipStream readOpenAsZipStreamOn:(globalDataChunk readStream) suppressHeaderAndChecksum:false. |
2699 | 272 |
zlibReader binary. |
273 |
bitsPerPixel := self bitsPerPixel. |
|
274 |
||
275 |
1 to: 7 do: [:pass | |
|
276 |
(self doPass: pass) ifTrue:[ |
|
277 |
cx := colIncrement at: pass. |
|
278 |
sc := startingCol at: pass. |
|
3327 | 279 |
cy := rowIncrement at: pass. |
3598 | 280 |
bytesPerPass := (((width - sc + cx - 1) // cx * bitsPerPixel) + 7) // 8. |
2699 | 281 |
prevScanline := ByteArray new: bytesPerPass. |
282 |
thisScanline := ByteArray new: bytesPerScanline. |
|
3327 | 283 |
(startingRow at: pass) to: height - 1 by: cy do: [:y | |
3343 | 284 |
filter := zlibReader nextByte. |
2699 | 285 |
"/ filtersSeen add: filter. |
286 |
(filter isNil or: [(filter between: 0 and: 4) not]) |
|
287 |
ifTrue: [^ self]. |
|
288 |
zlibReader next: bytesPerPass into: thisScanline startingAt: 1. |
|
289 |
filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerPass ]. |
|
290 |
self copyPixels:y at:sc by:cx. |
|
291 |
temp := prevScanline. |
|
292 |
prevScanline := thisScanline. |
|
293 |
thisScanline := temp. |
|
294 |
] |
|
295 |
] |
|
296 |
]. |
|
297 |
zlibReader atEnd ifFalse:[self error:'Unexpected data']. |
|
2909 | 298 |
|
299 |
"Modified: / 03-05-2011 / 12:03:33 / cg" |
|
2699 | 300 |
! |
301 |
||
1671 | 302 |
processNonInterlacedDATA:len |
2701 | 303 |
self halt:'not used' |
304 |
"/ |
|
305 |
"/ | zlibReader filter temp prevScanline thisScanline bytesPerScanline filtersSeen| |
|
306 |
"/ |
|
307 |
"/ zlibReader := ZipStream readOpenAsZipStreamOn:inStream suppressHeaderAndChecksum:false. |
|
308 |
"/ "/ zlibReader := ZLibReadStream on:inStream from: 1 to:len. |
|
309 |
"/ |
|
310 |
"/ prevScanline := ByteArray new: self bytesPerRow. |
|
311 |
"/ thisScanline := ByteArray new: self bytesPerRow. |
|
312 |
"/ 0 to: height - 1 do:[:index | |
|
313 |
"/ filter := (zlibReader next: 1) first. |
|
314 |
"/ filtersSeen add: filter. |
|
315 |
"/ (filter isNil or: [(filter between: 0 and: 4) not]) ifTrue: [^self]. |
|
316 |
"/ thisScanline := zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
|
317 |
"/ self filterScanline: filter count: bytesPerScanline. |
|
318 |
"/ self copyPixels: index. |
|
319 |
"/ temp := prevScanline. |
|
320 |
"/ prevScanline := thisScanline. |
|
321 |
"/ thisScanline := temp |
|
322 |
"/ ] |
|
2509 | 323 |
! |
324 |
||
325 |
processNonInterlacedGlobalDATA |
|
2725 | 326 |
| "data n" zlibReader filter temp bytesPerScanline "filtersSeen" i| |
2696 | 327 |
|
328 |
"/ filtersSeen := Set new. |
|
2695 | 329 |
|
330 |
"/ data := ByteArray new:(self bytesPerRow * height)+1000. |
|
331 |
"/ n := ZipStream uncompress: globalDataChunk into: data. |
|
332 |
"/ self halt. |
|
2509 | 333 |
|
2701 | 334 |
zlibReader := ZipStream readOpenAsZipStreamOn:(globalDataChunk readStream) suppressHeaderAndChecksum:false. |
2696 | 335 |
zlibReader binary. |
2509 | 336 |
bytesPerScanline := self bytesPerRow. |
337 |
||
1671 | 338 |
prevScanline := ByteArray new: bytesPerScanline. |
339 |
thisScanline := ByteArray new: bytesPerScanline. |
|
2725 | 340 |
0 to: height - 1 do:[:y | |
3343 | 341 |
filter := zlibReader nextByte. |
2696 | 342 |
"/ filtersSeen add: filter. |
2725 | 343 |
(filter notNil and: [filter between: 0 and: 4]) ifFalse: [ |
344 |
'PNGReader: unsupported filter' infoPrintCR. |
|
345 |
^ self |
|
346 |
]. |
|
2696 | 347 |
zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
348 |
filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerScanline ]. |
|
2725 | 349 |
|
350 |
i := y * bytesPerScanline. |
|
351 |
data replaceFrom:i+1 to:(i+bytesPerScanline) with:thisScanline startingAt:1. |
|
352 |
||
2695 | 353 |
temp := prevScanline. |
354 |
prevScanline := thisScanline. |
|
355 |
thisScanline := temp |
|
356 |
] |
|
1671 | 357 |
! |
358 |
||
359 |
processPHYSChunkLen:len |
|
360 |
"physical pixel chunk - currently unhandled" |
|
361 |
||
2705 | 362 |
|pixelPerUnitX pixelPerUnitY unit| |
363 |
||
3554 | 364 |
pixelPerUnitX := inStream nextInt32MSB:true. |
365 |
pixelPerUnitY := inStream nextInt32MSB:true. |
|
2705 | 366 |
unit := inStream nextByte. |
367 |
||
2707 | 368 |
Verbose == true ifTrue:[ |
369 |
'PNGReader: PHYS chunk ignored:' infoPrintCR. |
|
370 |
'PNGReader: ppuX:' infoPrint. pixelPerUnitX infoPrintCR. |
|
371 |
'PNGReader: ppuY:' infoPrint. pixelPerUnitY infoPrintCR. |
|
372 |
'PNGReader: unit:' infoPrint. unit infoPrintCR. |
|
373 |
]. |
|
374 |
||
2705 | 375 |
^ true |
308 | 376 |
! |
377 |
||
378 |
processPLTEChunkLen:len |
|
1671 | 379 |
"read a color palette" |
380 |
||
2725 | 381 |
|n "{ Class: SmallInteger }" |
382 |
redBytes greenBytes blueBytes| |
|
308 | 383 |
|
384 |
(len \\ 3) ~~ 0 ifTrue:[ |
|
385 |
'PNGReader: invalid size of PLTE chunk' infoPrintCR. |
|
386 |
^ false |
|
387 |
]. |
|
388 |
n := len // 3. |
|
389 |
redBytes := ByteArray new:n. |
|
390 |
greenBytes := ByteArray new:n. |
|
391 |
blueBytes := ByteArray new:n. |
|
392 |
||
393 |
1 to:n do:[:i | |
|
394 |
redBytes at:i put:(inStream nextByte). |
|
395 |
greenBytes at:i put:(inStream nextByte). |
|
396 |
blueBytes at:i put:(inStream nextByte) |
|
397 |
]. |
|
398 |
||
2725 | 399 |
colorMap := MappedPalette |
400 |
redVector:redBytes |
|
401 |
greenVector:greenBytes |
|
402 |
blueVector:blueBytes. |
|
403 |
||
308 | 404 |
^ true |
405 |
||
406 |
"Created: 21.6.1996 / 21:22:28 / cg" |
|
407 |
"Modified: 21.6.1996 / 21:43:01 / cg" |
|
408 |
! |
|
409 |
||
410 |
processSBITChunkLen:len |
|
411 |
inStream skip:len. |
|
412 |
^ true |
|
413 |
||
414 |
"Created: 21.6.1996 / 21:13:09 / cg" |
|
415 |
! |
|
416 |
||
417 |
processTEXTChunkLen:len |
|
418 |
inStream skip:len. |
|
419 |
^ true |
|
420 |
||
421 |
"Modified: 21.6.1996 / 21:15:27 / cg" |
|
422 |
! |
|
423 |
||
424 |
processTIMEChunkLen:len |
|
425 |
inStream skip:len. |
|
426 |
^ true |
|
427 |
||
428 |
"Created: 21.6.1996 / 21:15:43 / cg" |
|
429 |
"Modified: 21.6.1996 / 21:20:42 / cg" |
|
430 |
! |
|
431 |
||
2707 | 432 |
processTRNSChunkLen:len |
433 |
inStream skip:len. |
|
434 |
^ true |
|
435 |
! |
|
436 |
||
308 | 437 |
processZTXTChunkLen:len |
438 |
inStream skip:len. |
|
439 |
^ true |
|
440 |
||
441 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
442 |
! ! |
|
443 |
||
2696 | 444 |
!PNGReader methodsFor:'private-filtering'! |
445 |
||
2909 | 446 |
filterAverage:count |
2699 | 447 |
"Use the average of the pixel to the left and the pixel above as a predictor" |
2909 | 448 |
|
449 |
|delta| |
|
2699 | 450 |
|
2909 | 451 |
delta := self bitsPerPixel // 8 max:1. |
452 |
1 to:delta do:[:i | |
|
3384 | 453 |
thisScanline at:i put:((thisScanline at:i) + ((prevScanline at:i) // 2) bitAnd:255) |
2909 | 454 |
]. |
455 |
delta + 1 to:count do:[:i | |
|
3384 | 456 |
thisScanline at:i put:((thisScanline at:i) + (((prevScanline at:i) + (thisScanline at:i - delta)) // 2) bitAnd:255) |
2909 | 457 |
] |
458 |
||
459 |
"Modified: / 03-05-2011 / 12:14:01 / cg" |
|
2699 | 460 |
! |
461 |
||
2696 | 462 |
filterHorizontal:count |
463 |
"use the pixel to the left as a predictor" |
|
464 |
||
465 |
|delta| |
|
466 |
||
467 |
delta := self bitsPerPixel // 8 max:1. |
|
468 |
delta+1 to:count do:[:i| |
|
469 |
thisScanline at:i put:(((thisScanline at:i)+ (thisScanline at:i-delta)) bitAnd:255) |
|
470 |
] |
|
471 |
! |
|
472 |
||
2699 | 473 |
filterNone: count |
2909 | 474 |
"/ no filter - scanline is as is |
475 |
||
476 |
"Modified: / 03-05-2011 / 12:14:20 / cg" |
|
2699 | 477 |
! |
478 |
||
2909 | 479 |
filterPaeth:count |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
480 |
"Select one of (the pixel to the left, the pixel above and the pixel to above left) to |
2909 | 481 |
predict the value of this pixel" |
482 |
||
483 |
|delta| |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
484 |
|
2909 | 485 |
delta := self bitsPerPixel // 8 max:1. |
486 |
1 to:delta do:[:i | |
|
487 |
thisScanline |
|
488 |
at:i |
|
489 |
put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255) |
|
490 |
]. |
|
491 |
delta + 1 to:count do:[:i | |
|
492 |
thisScanline |
|
493 |
at:i |
|
494 |
put:(((thisScanline at:i) + (self |
|
495 |
paethPredictLeft:(thisScanline at:i - delta) |
|
496 |
above:(prevScanline at:i) |
|
497 |
aboveLeft:(prevScanline at:i - delta))) |
|
498 |
bitAnd:255) |
|
499 |
] |
|
500 |
||
501 |
"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
|
502 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
503 |
|
2696 | 504 |
filterScanline:filterType count:count |
505 |
self |
|
2909 | 506 |
perform:(#(filterNone: filterHorizontal: filterVertical: filterAverage: filterPaeth:) at:filterType+1) |
2696 | 507 |
with:count |
2909 | 508 |
|
509 |
"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
|
510 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
511 |
|
2909 | 512 |
filterVertical:count |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
513 |
"Use the pixel above as a predictor" |
2909 | 514 |
|
515 |
1 to:count do:[:i | |
|
516 |
thisScanline |
|
517 |
at:i |
|
518 |
put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255) |
|
519 |
] |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
520 |
|
2909 | 521 |
"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
|
522 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
523 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
524 |
paethPredictLeft: a above: b aboveLeft: c |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
525 |
"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
|
526 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
527 |
| pa pb pc | |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
528 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
529 |
pa := b > c ifTrue: [b - c] ifFalse: [c - b]. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
530 |
pb := a > c ifTrue: [a - c] ifFalse: [c - a]. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
531 |
pc := a + b - c - c. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
532 |
pc < 0 ifTrue: [ |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
533 |
pc := pc * -1 |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
534 |
]. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
535 |
((pa <= pb) and: [pa <= pc]) ifTrue: [^ a]. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
536 |
(pb <= pc) ifTrue: [^ b]. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
537 |
^ c |
2696 | 538 |
! ! |
539 |
||
2699 | 540 |
!PNGReader methodsFor:'private-pixel copy'! |
541 |
||
2909 | 542 |
copyPixels:y at:startX by:incX |
2704 | 543 |
"Handle interlaced pixels of supported colorTypes" |
2909 | 544 |
|
545 |
|s| |
|
2699 | 546 |
|
3598 | 547 |
"/ per color type copy methods |
548 |
s := #( |
|
549 |
#copyPixelsGray:at:by: "/ 0 |
|
2909 | 550 |
nil |
3598 | 551 |
#copyPixelsRGB:at:by: "/ 2 |
552 |
#copyPixelsIndexed:at:by: "/ 3 |
|
553 |
#copyPixelsGrayAlpha:at:by: "/ 4 |
|
2909 | 554 |
nil |
3598 | 555 |
#copyPixelsRGBA:at:by: "/ 6 |
2909 | 556 |
) at:colorType + 1. |
3437 | 557 |
self perform:s with:y with:startX with:incX |
1848 | 558 |
|
2909 | 559 |
"Modified: / 03-05-2011 / 12:02:45 / cg" |
3327 | 560 |
! |
561 |
||
3598 | 562 |
copyPixelsGray:y at:startX by:incX |
563 |
"Handle interlaced pixels of supported colorTypes. |
|
564 |
Untested code - please verify" |
|
565 |
||
566 |
|srcIndex srcMask nPixels dstIndex dstMask x |
|
567 |
bits bitMask rS lS rowIndex| |
|
568 |
||
569 |
nPixels := width // incX. |
|
570 |
srcIndex := 0. srcMask := 0. x := startX. |
|
571 |
||
572 |
depth == 1 ifTrue:[ |
|
573 |
dstIndex := (y * bytesPerScanline) + (startX // 8) + 1. |
|
574 |
dstMask := 16r80 >> (x \\ 8). |
|
575 |
||
576 |
1 to:nPixels do:[:cnt | |
|
577 |
srcMask == 0 ifTrue:[ |
|
578 |
srcMask := 16r80. |
|
579 |
srcIndex := srcIndex + 1. |
|
580 |
bits := thisScanline at:srcIndex. |
|
581 |
]. |
|
582 |
bitMask := (bits bitAnd:srcMask) == 0 ifTrue:[0] ifFalse:[dstMask]. |
|
583 |
bitMask ~~ 0 ifTrue:[ |
|
584 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
585 |
]. |
|
586 |
x := x + incX. |
|
587 |
dstMask := dstMask >> incX. |
|
588 |
dstMask == 0 ifTrue:[ |
|
589 |
dstIndex := dstIndex + 1. |
|
590 |
dstMask := 16r80 >> (x \\ 8). |
|
591 |
]. |
|
592 |
srcMask := srcMask bitShift:-1. |
|
593 |
]. |
|
594 |
^ self. |
|
595 |
]. |
|
596 |
||
597 |
depth == 2 ifTrue:[ |
|
598 |
dstIndex := (y * bytesPerScanline) + (startX // 4) + 1. |
|
599 |
lS := 6 - ((x \\ 4) * 2). |
|
600 |
rS := -1. |
|
601 |
1 to:nPixels do:[:cnt | |
|
602 |
rS < 0 ifTrue:[ |
|
603 |
srcIndex := srcIndex + 1. |
|
604 |
bits := thisScanline at:srcIndex. |
|
605 |
rS := 6. |
|
606 |
]. |
|
607 |
bitMask := ((bits >> rS) bitAnd:2r11). |
|
608 |
bitMask ~~ 0 ifTrue:[ |
|
609 |
bitMask := bitMask << lS. |
|
610 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
611 |
]. |
|
612 |
lS := lS - (incX * 2). |
|
613 |
lS < 0 ifTrue:[ |
|
614 |
lS <= -8 ifTrue:[ |
|
615 |
dstIndex := dstIndex + 2. |
|
616 |
lS := lS + 8 + 8. |
|
617 |
] ifFalse:[ |
|
618 |
dstIndex := dstIndex + 1. |
|
619 |
lS := lS + 8. |
|
620 |
]. |
|
621 |
]. |
|
622 |
rS := rS - 2. |
|
623 |
]. |
|
624 |
^ self. |
|
625 |
]. |
|
626 |
||
627 |
depth == 4 ifTrue:[ |
|
628 |
rowIndex := (y * bytesPerScanline) + 1. |
|
629 |
dstIndex := rowIndex + (x // 2). |
|
630 |
rS := -1. |
|
631 |
1 to:nPixels do:[:cnt | |
|
632 |
rS < 0 ifTrue:[ |
|
633 |
srcIndex := srcIndex + 1. |
|
634 |
bits := thisScanline at:srcIndex. |
|
635 |
rS := 4. |
|
636 |
]. |
|
637 |
bitMask := ((bits >> rS) bitAnd:2r1111). |
|
638 |
x even ifTrue:[ |
|
639 |
bitMask := bitMask bitShift:4. |
|
640 |
]. |
|
641 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
642 |
x := x + incX. |
|
643 |
dstIndex := rowIndex + (x // 2). |
|
644 |
rS := rS - 4. |
|
645 |
]. |
|
646 |
^ self. |
|
647 |
]. |
|
648 |
||
649 |
depth == 8 ifTrue:[ |
|
650 |
srcIndex := 1. |
|
651 |
dstIndex := (y * bytesPerScanline) + (startX) + 1. |
|
652 |
||
653 |
1 to:nPixels do:[:n | |
|
654 |
data at:dstIndex put:(thisScanline at:srcIndex). |
|
655 |
srcIndex := srcIndex + 1. |
|
656 |
dstIndex := dstIndex + incX |
|
657 |
]. |
|
658 |
^ self. |
|
659 |
]. |
|
660 |
||
661 |
depth == 16 ifTrue:[ |
|
662 |
srcIndex := 1. |
|
663 |
dstIndex := (y * bytesPerScanline) + (startX*2) + 1. |
|
664 |
||
665 |
1 to:nPixels do:[:n | |
|
666 |
data wordAt:dstIndex put:(thisScanline wordAt:srcIndex). |
|
667 |
srcIndex := srcIndex + 2. |
|
668 |
dstIndex := dstIndex + (incX * 2) |
|
669 |
]. |
|
670 |
^ self. |
|
671 |
]. |
|
672 |
self error:'unsupported depth'. |
|
673 |
! |
|
674 |
||
675 |
copyPixelsIndexed:y at:startX by:incX |
|
676 |
self copyPixelsGray:y at:startX by:incX |
|
677 |
! |
|
678 |
||
679 |
copyPixelsRGB:y at:startX by:incX |
|
680 |
"Handle interlaced pixels of supported colorTypes. |
|
681 |
Untested code - please verify" |
|
682 |
||
683 |
|srcIndex nPixels dstIndex dstInc bpp| |
|
684 |
||
685 |
bpp := bytesPerScanline // width. |
|
686 |
||
687 |
"/ 1 byte per r,g,b |
|
688 |
srcIndex := 1. |
|
689 |
dstIndex := (y * bytesPerScanline) + (startX * bpp) + 1. |
|
690 |
dstInc := incX * bpp. |
|
691 |
||
692 |
nPixels := width // incX. |
|
693 |
1 to:nPixels do:[:n | |
|
694 |
data replaceFrom:dstIndex to:dstIndex+bpp-1 with:thisScanline startingAt:srcIndex. |
|
695 |
srcIndex := srcIndex + bpp. |
|
696 |
dstIndex := dstIndex + dstInc |
|
697 |
]. |
|
698 |
! |
|
699 |
||
3327 | 700 |
copyPixelsRGBA:y at:startX by:incX |
701 |
"Handle interlaced pixels of supported colorTypes. |
|
702 |
Untested code - please verify" |
|
703 |
||
3437 | 704 |
|srcIndex nPixels dstIndex dstInc| |
3327 | 705 |
|
706 |
srcIndex := 1. |
|
3437 | 707 |
dstIndex := (y * bytesPerScanline) + (startX * 4) + 1. |
708 |
dstInc := incX * 4. |
|
3327 | 709 |
|
710 |
nPixels := thisScanline size. |
|
711 |
[srcIndex < nPixels] whileTrue:[ |
|
3437 | 712 |
data replaceFrom:dstIndex to:dstIndex+3 with:thisScanline startingAt:srcIndex. |
3327 | 713 |
srcIndex := srcIndex + 4. |
3437 | 714 |
dstIndex := dstIndex + dstInc |
3327 | 715 |
]. |
2699 | 716 |
! ! |
717 |
||
718 |
!PNGReader methodsFor:'private-reading'! |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
719 |
|
2683 | 720 |
getChunk |
721 |
|len type crc| |
|
722 |
||
723 |
inStream atEnd ifTrue:[^ false]. |
|
724 |
||
3554 | 725 |
len := inStream nextInt32MSB:true. |
2683 | 726 |
type := String new:4. |
727 |
(inStream nextBytes:4 into:type) ~~ 4 ifTrue:[^ false]. |
|
728 |
||
2707 | 729 |
Verbose == true ifTrue:[ |
730 |
'len: ' infoPrint. len infoPrint. ' type: ' infoPrint. type infoPrintCR. |
|
731 |
]. |
|
2683 | 732 |
|
733 |
type = 'IEND' ifTrue:[^ false]. |
|
734 |
||
735 |
(self processChunk:type len:len) ifFalse:[^ false]. |
|
736 |
||
3554 | 737 |
crc := inStream nextInt32MSB:true. "/ ignored - for now |
2683 | 738 |
^ true |
739 |
||
740 |
"Created: 21.6.1996 / 21:09:36 / cg" |
|
741 |
"Modified: 21.6.1996 / 21:20:26 / cg" |
|
742 |
! |
|
743 |
||
744 |
getIHDRChunk |
|
745 |
|len type crc| |
|
746 |
||
3554 | 747 |
len := inStream nextInt32MSB:true. |
2683 | 748 |
|
749 |
type := String new:4. |
|
750 |
inStream nextBytes:4 into:type. |
|
751 |
||
3183 | 752 |
type = 'IHDR' ifFalse:[self halt:'expected IHDR magic'. ^ false]. |
753 |
len == 13 ifFalse:[self halt:'unexpected IHDR length'. ^ false]. |
|
2683 | 754 |
|
3554 | 755 |
width := inStream nextInt32MSB:true. |
756 |
height := inStream nextInt32MSB:true. |
|
2683 | 757 |
|
758 |
(width <= 0 or:[height <= 0]) ifTrue:[ |
|
759 |
'PNGReader: invalid dimension(s)' infoPrintCR. |
|
760 |
^ false. |
|
761 |
]. |
|
762 |
self reportDimension. |
|
763 |
||
764 |
depth := inStream nextByte. "/ bits-per-channel |
|
765 |
colorType := inStream nextByte. |
|
766 |
||
767 |
compressionMethod := inStream nextByte. |
|
768 |
filterMethod := inStream nextByte. |
|
769 |
interlaceMode := inStream nextByte. |
|
770 |
||
2707 | 771 |
Verbose == true ifTrue:[ |
772 |
'PNGReader: IHDR:' infoPrintCR. |
|
773 |
'PNGReader: width: ' infoPrint. width infoPrintCR. |
|
774 |
'PNGReader: height: ' infoPrint. height infoPrintCR. |
|
775 |
'PNGReader: depth: ' infoPrint. depth infoPrintCR. |
|
776 |
'PNGReader: colorType: ' infoPrint. colorType infoPrintCR. |
|
777 |
'PNGReader: compressionMethod: ' infoPrint. compressionMethod infoPrintCR. |
|
778 |
'PNGReader: filterMethod: ' infoPrint. filterMethod infoPrintCR. |
|
779 |
'PNGReader: interlaceMode: ' infoPrint. interlaceMode infoPrintCR. |
|
780 |
]. |
|
781 |
||
3554 | 782 |
crc := inStream nextInt32MSB:true. |
2683 | 783 |
^ true |
784 |
||
785 |
"Modified: 21.6.1996 / 21:38:35 / cg" |
|
786 |
! |
|
787 |
||
1848 | 788 |
setColorType:colorType |
789 |
colorType == 0 ifTrue:[ |
|
790 |
photometric := #blackIs0. |
|
791 |
samplesPerPixel := 1. |
|
792 |
bitsPerSample := Array with:depth. |
|
793 |
^ true. |
|
794 |
]. |
|
795 |
||
796 |
colorType == 2 ifTrue:[ |
|
797 |
depth < 8 ifTrue:[ |
|
798 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
|
799 |
^ false. |
|
800 |
]. |
|
801 |
photometric := #rgb. |
|
802 |
samplesPerPixel := 3. |
|
803 |
bitsPerSample := Array with:depth with:depth with:depth. |
|
804 |
^ true. |
|
805 |
]. |
|
806 |
||
807 |
colorType == 3 ifTrue:[ |
|
808 |
depth == 16 ifTrue:[ |
|
809 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
|
810 |
^ false. |
|
811 |
]. |
|
812 |
photometric := #palette. |
|
813 |
samplesPerPixel := 1. |
|
814 |
bitsPerSample := Array with:depth. |
|
815 |
^ true. |
|
816 |
]. |
|
817 |
||
818 |
colorType == 4 ifTrue:[ |
|
819 |
depth < 8 ifTrue:[ |
|
820 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
|
821 |
^ false. |
|
822 |
]. |
|
823 |
photometric := #blackIs0. |
|
824 |
samplesPerPixel := 2. |
|
825 |
bitsPerSample := Array with:depth with:depth. |
|
826 |
^ true. |
|
827 |
]. |
|
828 |
||
829 |
colorType == 6 ifTrue:[ |
|
830 |
depth < 8 ifTrue:[ |
|
831 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
|
832 |
^ false. |
|
833 |
]. |
|
2702 | 834 |
photometric := #rgb. |
1848 | 835 |
samplesPerPixel := 4. |
836 |
bitsPerSample := Array with:depth with:depth with:depth with:depth. |
|
837 |
^ true. |
|
838 |
]. |
|
839 |
||
840 |
('PNGReader: invalid colorType: ' , colorType printString , '.') infoPrintCR. |
|
841 |
^ false |
|
842 |
! ! |
|
843 |
||
1805 | 844 |
!PNGReader methodsFor:'reading'! |
308 | 845 |
|
846 |
fromStream:aStream |
|
847 |
"read a stream containing a PNG image. |
|
848 |
Leave image description in instance variables." |
|
849 |
||
850 |
|header| |
|
851 |
||
852 |
inStream := aStream. |
|
853 |
aStream binary. |
|
854 |
||
1671 | 855 |
"PNG-files are always msb (network-world)" |
308 | 856 |
byteOrder := #msb. |
857 |
||
858 |
header := ByteArray new:8. |
|
859 |
aStream nextBytes:8 into:header. |
|
860 |
||
2683 | 861 |
header ~= (self pngHeader) ifTrue:[ |
308 | 862 |
'PNGReader: not a png file.' infoPrintCR. |
3598 | 863 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: not a png file'. |
308 | 864 |
^ nil |
865 |
]. |
|
2695 | 866 |
|
308 | 867 |
(self getIHDRChunk) ifFalse:[ |
868 |
'PNGReader: required IHDR chunk missing.' infoPrintCR. |
|
3598 | 869 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: required IHDR chunk missing'. |
308 | 870 |
^ nil |
871 |
]. |
|
872 |
||
873 |
compressionMethod ~~ 0 ifTrue:[ |
|
3598 | 874 |
('PNGReader: compressionMethod %s not supported.' printfWith:compressionMethod printString) infoPrintCR. |
875 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: unsupported compression method'. |
|
308 | 876 |
^ nil |
877 |
]. |
|
878 |
filterMethod > 0 ifTrue:[ |
|
879 |
'PNGReader: invalid filterMethod' infoPrintCR. |
|
3598 | 880 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: invalid/unsupported filterMethod'. |
308 | 881 |
^ nil. |
882 |
]. |
|
883 |
interlaceMode > 1 ifTrue:[ |
|
3598 | 884 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: invalid/unsupported interlaceMode'. |
308 | 885 |
'PNGReader: invalid interlaceMode' infoPrintCR. |
886 |
^ nil. |
|
887 |
]. |
|
1671 | 888 |
(self setColorType:colorType) ifFalse:[ |
308 | 889 |
^ nil |
1671 | 890 |
]. |
2706 | 891 |
|
1671 | 892 |
[self getChunk] whileTrue. |
308 | 893 |
|
3598 | 894 |
bytesPerScanline := self bytesPerRow. |
895 |
data := ByteArray new:(bytesPerScanline * height). |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
896 |
|
2509 | 897 |
globalDataChunk notNil ifTrue:[ |
3343 | 898 |
self processGlobalIDATChunk. |
899 |
globalDataChunk := nil. |
|
2509 | 900 |
]. |
901 |
||
308 | 902 |
photometric == #palette ifTrue:[ |
2725 | 903 |
colorMap isNil ifTrue:[ |
3598 | 904 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: palette chunk missing'. |
905 |
'PNGReader: palette chunk missing.' infoPrintCR. |
|
2725 | 906 |
"/ ^ nil |
308 | 907 |
]. |
908 |
]. |
|
909 |
||
910 |
" |
|
911 |
PNGReader fromFile:'/home/cg/libpng-0.89c/pngtest.png' |
|
912 |
" |
|
913 |
||
2910
d5171b9245e6
comment/format in: #fromStream:
Claus Gittinger <cg@exept.de>
parents:
2909
diff
changeset
|
914 |
"Modified: / 03-05-2011 / 12:17:34 / cg" |
1805 | 915 |
! ! |
916 |
||
2683 | 917 |
!PNGReader methodsFor:'writing to file'! |
918 |
||
919 |
pngHeader |
|
920 |
^ #[137 80 78 71 13 10 26 10] |
|
921 |
! |
|
922 |
||
923 |
save:image onStream:aStream |
|
924 |
"save image in PNG-file-format onto aStream" |
|
925 |
||
926 |
outStream := aStream. |
|
927 |
outStream binary. |
|
928 |
||
2703 | 929 |
byteOrder := #msb. |
2683 | 930 |
width := image width. |
931 |
height := image height. |
|
932 |
photometric := image photometric. |
|
933 |
samplesPerPixel := image samplesPerPixel. |
|
934 |
bitsPerSample := image bitsPerSample. |
|
935 |
colorMap := image colorMap. |
|
936 |
data := image bits. |
|
937 |
||
2703 | 938 |
outStream nextPutAll:(self pngHeader). |
939 |
self writeIHDRChunk. |
|
940 |
photometric == #palette ifTrue: [self writePaletteChunk]. |
|
941 |
self writeImageDataChunk. |
|
2683 | 942 |
self writeEndChunk |
943 |
! |
|
944 |
||
2703 | 945 |
writeChunk:chunkTypeChars size:len with:aBlock |
2708 | 946 |
|crc realOutStream chunkBytes| |
947 |
||
948 |
realOutStream := outStream. |
|
949 |
outStream := WriteStream on:(ByteArray new:len). |
|
950 |
||
2703 | 951 |
outStream nextPutBytes:4 from:chunkTypeChars startingAt:1. |
952 |
aBlock value. |
|
2708 | 953 |
|
954 |
chunkBytes := outStream contents. |
|
955 |
crc := CRC32Stream hashValueOf:chunkBytes. |
|
956 |
||
957 |
outStream := realOutStream. |
|
958 |
||
3554 | 959 |
realOutStream nextPutInt32:len MSB:true. |
2708 | 960 |
realOutStream nextPutAll:chunkBytes. |
3554 | 961 |
realOutStream nextPutInt32:crc MSB:true. |
2703 | 962 |
! |
2683 | 963 |
|
2703 | 964 |
writeEndChunk |
965 |
self |
|
966 |
writeChunk: 'IEND' |
|
967 |
size: 0 |
|
968 |
with:[ ] |
|
2683 | 969 |
! |
970 |
||
971 |
writeFileHeader |
|
972 |
outStream nextPutAll:(self pngHeader) |
|
973 |
! |
|
974 |
||
2703 | 975 |
writeIHDRChunk |
976 |
|colorType| |
|
977 |
||
3100 | 978 |
((photometric == #whiteIs0) or:[ (photometric == #blackIs0)]) ifTrue:[ |
2703 | 979 |
colorType := 0. |
3461 | 980 |
samplesPerPixel > 1 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
2703 | 981 |
]. |
982 |
(photometric == #rgb) ifTrue:[ |
|
983 |
colorType := 2. |
|
3461 | 984 |
samplesPerPixel > 3 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
2703 | 985 |
]. |
986 |
(photometric == #palette) ifTrue:[ |
|
987 |
colorType := 3. |
|
3461 | 988 |
samplesPerPixel > 1 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
2703 | 989 |
]. |
3100 | 990 |
colorType isNil ifTrue:[ |
3461 | 991 |
self error:'unhandled photometric' |
3100 | 992 |
]. |
2703 | 993 |
|
2683 | 994 |
self |
995 |
writeChunk: 'IHDR' |
|
996 |
size: 13 |
|
997 |
with:[ |
|
998 |
self writeLong:width. |
|
999 |
self writeLong:height. |
|
2703 | 1000 |
outStream nextPut: 8. "Assume that all colors are 24bit" "/ depth |
1001 |
outStream nextPut: colorType. |
|
2683 | 1002 |
outStream nextPut: 0. "Compression" |
1003 |
outStream nextPut: 0. "Filter method" |
|
1004 |
outStream nextPut: 0 "Non-interlaced" |
|
1005 |
] |
|
1006 |
! |
|
1007 |
||
2703 | 1008 |
writeImageDataChunk |
2708 | 1009 |
|compressedByteStream compressedBytes zlibWriter bytesPerScanline idx| |
2703 | 1010 |
|
2708 | 1011 |
compressedByteStream := WriteStream on:(ByteArray new:100). |
1012 |
zlibWriter := ZipStream writeOpenAsZipStreamOn:compressedByteStream suppressHeaderAndChecksum:false. |
|
2703 | 1013 |
zlibWriter binary. |
1014 |
||
1015 |
bytesPerScanline := self bytesPerRow. |
|
1016 |
||
2708 | 1017 |
idx := 1. |
1018 |
0 to:height-1 do:[:y | |
|
1019 |
|row| |
|
1020 |
||
1021 |
row := data copyFrom:idx to:(idx+bytesPerScanline-1). |
|
1022 |
zlibWriter nextPutAll:#[0]. "/ no filter |
|
1023 |
zlibWriter nextPutAll:row. |
|
1024 |
idx := idx + bytesPerScanline. |
|
2703 | 1025 |
]. |
2708 | 1026 |
|
1027 |
zlibWriter close. |
|
1028 |
compressedBytes := compressedByteStream contents. |
|
1029 |
||
1030 |
self |
|
1031 |
writeChunk: 'IDAT' |
|
1032 |
size:(compressedBytes size) |
|
1033 |
with:[ |
|
1034 |
outStream nextPutAll:compressedBytes |
|
1035 |
] |
|
2703 | 1036 |
! |
1037 |
||
2683 | 1038 |
writePaletteChunk |
1039 |
self |
|
1040 |
writeChunk: 'PLTE' |
|
1041 |
size: 3 * colorMap size |
|
1042 |
with:[ |
|
2703 | 1043 |
colorMap do:[:color | |
1044 |
outStream |
|
1045 |
nextPut: color redByte; |
|
1046 |
nextPut: color greenByte; |
|
1047 |
nextPut: color blueByte |
|
2683 | 1048 |
] |
1049 |
] |
|
1050 |
! ! |
|
1051 |
||
398 | 1052 |
!PNGReader class methodsFor:'documentation'! |
308 | 1053 |
|
1054 |
version |
|
3554 | 1055 |
^ '$Header$' |
2909 | 1056 |
! |
1057 |
||
1058 |
version_CVS |
|
3554 | 1059 |
^ '$Header$' |
308 | 1060 |
! ! |
1671 | 1061 |
|
3100 | 1062 |
|
308 | 1063 |
PNGReader initialize! |