author | Claus Gittinger <cg@exept.de> |
Mon, 07 Sep 2009 20:45:59 +0200 | |
changeset 2736 | 4798b0251087 |
parent 2725 | a480f3671588 |
child 2909 | 3f9856162142 |
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 |
||
308 | 14 |
ImageReader subclass:#PNGReader |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
15 |
instanceVariableNames:'colorType bitsPerChannel depth compressionMethod filterMethod |
2725 | 16 |
interlaceMode bytesPerScanline globalDataChunk thisScanline |
17 |
prevScanline' |
|
2707 | 18 |
classVariableNames:'Verbose' |
308 | 19 |
poolDictionaries:'' |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1694
diff
changeset
|
20 |
category:'Graphics-Images-Readers' |
308 | 21 |
! |
22 |
||
398 | 23 |
!PNGReader class methodsFor:'documentation'! |
308 | 24 |
|
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
||
38 |
||
39 |
! |
|
40 |
||
41 |
documentation |
|
42 |
" |
|
2725 | 43 |
This class will provide methods for loading and saving PNG pictures. |
2700 | 44 |
It is currenty unfinished (interlaced image support is missing). |
2725 | 45 |
In the meantime, use a pngtoXXX converter for interlaced images. |
308 | 46 |
|
2725 | 47 |
[caveats:] |
48 |
writer ignores any mask (for now). |
|
49 |
writer only generates unfiltered rows or interlace data |
|
50 |
||
308 | 51 |
[See also:] |
52 |
Image Form Icon |
|
53 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader |
|
54 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
|
55 |
XBMReader XPMReader XWDReader |
|
56 |
||
57 |
[author:] |
|
58 |
Claus Gittinger |
|
59 |
" |
|
1694 | 60 |
! |
308 | 61 |
|
1694 | 62 |
examples |
63 |
" |
|
64 |
PNGReader fromFile:'/home/cg/AudioExplorer_51_files/use_small.png' |
|
2509 | 65 |
PNGReader fromFile:'\\Exeptn\tmp\images\expeccoScreenshot4020_5526507.png' |
2683 | 66 |
|
67 |
PNGReader fromFile:'C:\Users\cg\Desktop\croquet\cobalt-base-current-build-20090210\cobalt-base-current-build-20090210\content\models\textures\checkerboard.png' |
|
2695 | 68 |
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
|
69 |
PNGReader fromFile:'\\exeptn\unsaved\pd_stuff\PNGs\Delete.png' |
2699 | 70 |
PNGReader fromFile:'\\exeptn\unsaved\pd_stuff\PNGs\Down.png' |
2703 | 71 |
|
72 |
||
73 |
|img img2 outStream png| |
|
2725 | 74 |
|
2703 | 75 |
img := ToolbarIconLibrary error32x32Icon. |
76 |
outStream := WriteStream on:(ByteArray new:100). |
|
77 |
PNGReader save:img onStream:outStream. |
|
78 |
png := outStream contents. |
|
79 |
img2 := PNGReader fromStream:(png readStream). |
|
2708 | 80 |
self assert:(img bits = img2 bits). |
81 |
img inspect. |
|
2703 | 82 |
img2 inspect. |
1694 | 83 |
" |
308 | 84 |
! ! |
85 |
||
398 | 86 |
!PNGReader class methodsFor:'initialization'! |
308 | 87 |
|
88 |
initialize |
|
2699 | 89 |
"install myself in the Image classes fileFormat table for the `.png' extension." |
308 | 90 |
|
2709 | 91 |
MIMETypes defineImageType:'image/png' suffix:'png' reader:self. |
92 |
"/ backward compatibility from times when png was not yet so common... |
|
93 |
MIMETypes defineImageType:'image/x-png' suffix:nil reader:self. |
|
308 | 94 |
! ! |
95 |
||
2705 | 96 |
!PNGReader class methodsFor:'queries'! |
97 |
||
98 |
canRepresent:anImage |
|
99 |
"return true, if anImage can be represented in my file format. |
|
100 |
Any image is supported." |
|
101 |
||
102 |
^ true |
|
103 |
! ! |
|
104 |
||
398 | 105 |
!PNGReader class methodsFor:'testing'! |
308 | 106 |
|
107 |
isValidImageFile:aFileName |
|
108 |
"return true, if aFileName contains a PNG image" |
|
109 |
||
110 |
|inStream magic| |
|
111 |
||
112 |
inStream := self streamReadingFile:aFileName. |
|
113 |
inStream isNil ifTrue:[^ false]. |
|
114 |
||
115 |
inStream binary. |
|
116 |
||
117 |
magic := ByteArray new:8. |
|
118 |
inStream nextBytes:8 into:magic. |
|
119 |
inStream close. |
|
120 |
||
121 |
^ (magic = #[137 80 78 71 13 10 26 10]) |
|
122 |
||
1694 | 123 |
" |
124 |
self isValidImageFile:'/home/cg/AudioExplorer_51_files/use_small.png' |
|
2683 | 125 |
self isValidImageFile:'C:\Users\cg\Desktop\croquet\cobalt-base-current-build-20090210\cobalt-base-current-build-20090210\content\models\textures\checkerboard.png' |
2695 | 126 |
self isValidImageFile:'C:\Dokumente und Einstellungen\cg\Desktop\misc\PNGs\Delete.png' |
1694 | 127 |
" |
128 |
||
308 | 129 |
"Modified: 21.6.1996 / 20:38:46 / cg" |
130 |
! ! |
|
131 |
||
2696 | 132 |
!PNGReader methodsFor:'private-chunks'! |
308 | 133 |
|
2699 | 134 |
doPass: pass |
135 |
"Certain interlace passes are skipped with certain small image dimensions" |
|
136 |
||
137 |
pass = 1 ifTrue: [ ^ true ]. |
|
138 |
((width = 1) and: [height = 1]) ifTrue: [ ^ false ]. |
|
139 |
pass = 2 ifTrue: [ ^ width >= 5 ]. |
|
140 |
pass = 3 ifTrue: [ ^ height >= 5 ]. |
|
141 |
pass = 4 ifTrue: [ ^ (width >=3 ) or: [height >= 5] ]. |
|
142 |
pass = 5 ifTrue: [ ^ height >=3 ]. |
|
143 |
pass = 6 ifTrue: [ ^ width >=2 ]. |
|
144 |
pass = 7 ifTrue: [ ^ height >=2 ]. |
|
145 |
||
146 |
self error:'invalid argument'. |
|
147 |
^ true |
|
148 |
! |
|
149 |
||
308 | 150 |
processBKGDChunkLen:len |
151 |
inStream skip:len. |
|
152 |
^ true |
|
153 |
||
154 |
"Created: 21.6.1996 / 21:15:49 / cg" |
|
155 |
! |
|
156 |
||
2707 | 157 |
processCHRMChunkLen:len |
158 |
"cHRM Primary chromaticities chunk - currently unhandled" |
|
159 |
||
160 |
|whitePointX whitePointY redX redY greenX greenY blueX blueY| |
|
161 |
||
162 |
whitePointX := (inStream nextLongMSB:true) / 100000.0. |
|
163 |
whitePointY := (inStream nextLongMSB:true) / 100000.0. |
|
164 |
redX := (inStream nextLongMSB:true) / 100000.0. |
|
165 |
redY := (inStream nextLongMSB:true) / 100000.0. |
|
166 |
greenX := (inStream nextLongMSB:true) / 100000.0. |
|
167 |
greenY := (inStream nextLongMSB:true) / 100000.0. |
|
168 |
blueX := (inStream nextLongMSB:true) / 100000.0. |
|
169 |
blueY := (inStream nextLongMSB:true) / 100000.0. |
|
170 |
||
171 |
Verbose == true ifTrue:[ |
|
172 |
'PNGReader: CHRM chunk ignored:' infoPrintCR. |
|
173 |
'PNGReader: whitePointX:' infoPrint. whitePointX infoPrintCR. |
|
174 |
'PNGReader: whitePointY:' infoPrint. whitePointX infoPrintCR. |
|
175 |
'PNGReader: redX:' infoPrint. redX infoPrintCR. |
|
176 |
'PNGReader: redY:' infoPrint. redY infoPrintCR. |
|
177 |
'PNGReader: greenX:' infoPrint. greenX infoPrintCR. |
|
178 |
'PNGReader: greenY:' infoPrint. greenY infoPrintCR. |
|
179 |
'PNGReader: blueX:' infoPrint. blueX infoPrintCR. |
|
180 |
'PNGReader: blueY:' infoPrint. blueY infoPrintCR. |
|
181 |
]. |
|
182 |
^ true |
|
183 |
! |
|
184 |
||
308 | 185 |
processChunk:type len:len |
2509 | 186 |
|chunk| |
187 |
||
188 |
type = 'IDAT' ifTrue:[ |
|
189 |
"---since the compressed data can span multiple |
|
190 |
chunks, stitch them all together first. later, |
|
191 |
if memory is an issue, we need to figure out how |
|
192 |
to do this on the fly---" |
|
193 |
chunk := inStream next:len. |
|
194 |
globalDataChunk := globalDataChunk isNil |
|
195 |
ifTrue: [chunk] |
|
196 |
ifFalse:[globalDataChunk,chunk]. |
|
197 |
^ true |
|
198 |
"/ ^ self processIDATChunkLen:len |
|
199 |
]. |
|
200 |
||
308 | 201 |
type = 'gAMA' ifTrue:[^ self processGAMAChunkLen:len]. |
202 |
type = 'sBIT' ifTrue:[^ self processSBITChunkLen:len]. |
|
203 |
type = 'tEXt' ifTrue:[^ self processTEXTChunkLen:len]. |
|
204 |
type = 'tIME' ifTrue:[^ self processTIMEChunkLen:len]. |
|
205 |
type = 'bKGD' ifTrue:[^ self processBKGDChunkLen:len]. |
|
206 |
type = 'zTXt' ifTrue:[^ self processZTXTChunkLen:len]. |
|
207 |
type = 'PLTE' ifTrue:[^ self processPLTEChunkLen:len]. |
|
208 |
||
1671 | 209 |
type = 'pHYs' ifTrue:[^ self processPHYSChunkLen:len]. |
2707 | 210 |
type = 'cHRM' ifTrue:[^ self processCHRMChunkLen:len]. |
1671 | 211 |
type = 'tRNS' ifTrue:[^ self processTRNSChunkLen:len]. |
212 |
type = 'IHDR' ifTrue:[^ self processIHDRChunkLen:len]. |
|
213 |
||
308 | 214 |
('PNGReader: unrecognized chunk: ' , type , ' ignored.') infoPrintCR. |
215 |
||
216 |
inStream skip:len. |
|
217 |
^ true. |
|
218 |
||
219 |
"Created: 21.6.1996 / 21:10:37 / cg" |
|
220 |
"Modified: 21.6.1996 / 21:22:37 / cg" |
|
221 |
! |
|
222 |
||
223 |
processGAMAChunkLen:len |
|
224 |
inStream skip:len. |
|
225 |
^ true |
|
226 |
||
227 |
"Created: 21.6.1996 / 21:10:52 / cg" |
|
228 |
! |
|
229 |
||
2509 | 230 |
processGlobalIDATChunk |
231 |
interlaceMode == 0 ifTrue: [ |
|
232 |
^ self processNonInterlacedGlobalDATA |
|
233 |
]. |
|
234 |
^ self processInterlacedGlobalDATA |
|
235 |
! |
|
236 |
||
308 | 237 |
processIDATChunkLen:len |
1671 | 238 |
interlaceMode == 0 ifTrue: [ |
239 |
^ self processNonInterlacedDATA:len |
|
240 |
]. |
|
241 |
^ self processInterlacedDATA:len |
|
242 |
! |
|
243 |
||
244 |
processInterlacedDATA:len |
|
308 | 245 |
inStream skip:len. |
246 |
^ true |
|
1671 | 247 |
! |
308 | 248 |
|
2699 | 249 |
processInterlacedGlobalDATA |
250 |
| zlibReader filter bytesPerPass startingCol colIncrement rowIncrement |
|
251 |
startingRow cx sc temp "filtersSeen" bitsPerPixel copyMethod| |
|
252 |
||
2701 | 253 |
self error:'interlaced pictures not yet supported'. |
2699 | 254 |
|
255 |
"/ filtersSeen := Set new. |
|
256 |
||
257 |
startingCol := #(0 4 0 2 0 1 0 ). |
|
258 |
colIncrement := #(8 8 4 4 2 2 1 ). |
|
259 |
rowIncrement := #(8 8 8 4 4 2 2 ). |
|
260 |
startingRow := #(0 0 4 0 2 0 1 ). |
|
261 |
||
2701 | 262 |
zlibReader := ZipStream readOpenAsZipStreamOn:(globalDataChunk readStream) suppressHeaderAndChecksum:false. |
2699 | 263 |
zlibReader binary. |
264 |
bytesPerScanline := self bytesPerRow. |
|
265 |
bitsPerPixel := self bitsPerPixel. |
|
266 |
||
267 |
copyMethod := #(copyPixelsGray: nil copyPixelsRGB: copyPixelsIndexed: |
|
268 |
copyPixelsGrayAlpha: nil copyPixelsRGBA:) at:colorType+1. |
|
269 |
||
270 |
1 to: 7 do: [:pass | |
|
271 |
(self doPass: pass) ifTrue:[ |
|
272 |
cx := colIncrement at: pass. |
|
273 |
sc := startingCol at: pass. |
|
274 |
bytesPerPass := width - sc + cx - 1 // cx * bitsPerPixel + 7 // 8. |
|
275 |
prevScanline := ByteArray new: bytesPerPass. |
|
276 |
thisScanline := ByteArray new: bytesPerScanline. |
|
277 |
(startingRow at: pass) to: height - 1 by: (rowIncrement at: pass) do: [:y | |
|
278 |
filter := zlibReader next. |
|
279 |
"/ filtersSeen add: filter. |
|
280 |
(filter isNil or: [(filter between: 0 and: 4) not]) |
|
281 |
ifTrue: [^ self]. |
|
282 |
zlibReader next: bytesPerPass into: thisScanline startingAt: 1. |
|
283 |
filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerPass ]. |
|
284 |
self copyPixels:y at:sc by:cx. |
|
285 |
temp := prevScanline. |
|
286 |
prevScanline := thisScanline. |
|
287 |
thisScanline := temp. |
|
288 |
] |
|
289 |
] |
|
290 |
]. |
|
291 |
zlibReader atEnd ifFalse:[self error:'Unexpected data']. |
|
292 |
! |
|
293 |
||
1671 | 294 |
processNonInterlacedDATA:len |
2701 | 295 |
self halt:'not used' |
296 |
"/ |
|
297 |
"/ | zlibReader filter temp prevScanline thisScanline bytesPerScanline filtersSeen| |
|
298 |
"/ |
|
299 |
"/ zlibReader := ZipStream readOpenAsZipStreamOn:inStream suppressHeaderAndChecksum:false. |
|
300 |
"/ "/ zlibReader := ZLibReadStream on:inStream from: 1 to:len. |
|
301 |
"/ |
|
302 |
"/ prevScanline := ByteArray new: self bytesPerRow. |
|
303 |
"/ thisScanline := ByteArray new: self bytesPerRow. |
|
304 |
"/ 0 to: height - 1 do:[:index | |
|
305 |
"/ filter := (zlibReader next: 1) first. |
|
306 |
"/ filtersSeen add: filter. |
|
307 |
"/ (filter isNil or: [(filter between: 0 and: 4) not]) ifTrue: [^self]. |
|
308 |
"/ thisScanline := zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
|
309 |
"/ self filterScanline: filter count: bytesPerScanline. |
|
310 |
"/ self copyPixels: index. |
|
311 |
"/ temp := prevScanline. |
|
312 |
"/ prevScanline := thisScanline. |
|
313 |
"/ thisScanline := temp |
|
314 |
"/ ] |
|
2509 | 315 |
! |
316 |
||
317 |
processNonInterlacedGlobalDATA |
|
2725 | 318 |
| "data n" zlibReader filter temp bytesPerScanline "filtersSeen" i| |
2696 | 319 |
|
320 |
"/ filtersSeen := Set new. |
|
2695 | 321 |
|
322 |
"/ data := ByteArray new:(self bytesPerRow * height)+1000. |
|
323 |
"/ n := ZipStream uncompress: globalDataChunk into: data. |
|
324 |
"/ self halt. |
|
2509 | 325 |
|
2701 | 326 |
zlibReader := ZipStream readOpenAsZipStreamOn:(globalDataChunk readStream) suppressHeaderAndChecksum:false. |
2696 | 327 |
zlibReader binary. |
2509 | 328 |
bytesPerScanline := self bytesPerRow. |
329 |
||
1671 | 330 |
prevScanline := ByteArray new: bytesPerScanline. |
331 |
thisScanline := ByteArray new: bytesPerScanline. |
|
2725 | 332 |
0 to: height - 1 do:[:y | |
2695 | 333 |
filter := (zlibReader next: 1) first. |
2696 | 334 |
"/ filtersSeen add: filter. |
2725 | 335 |
(filter notNil and: [filter between: 0 and: 4]) ifFalse: [ |
336 |
'PNGReader: unsupported filter' infoPrintCR. |
|
337 |
^ self |
|
338 |
]. |
|
2696 | 339 |
zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
340 |
filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerScanline ]. |
|
2725 | 341 |
|
342 |
i := y * bytesPerScanline. |
|
343 |
data replaceFrom:i+1 to:(i+bytesPerScanline) with:thisScanline startingAt:1. |
|
344 |
||
2695 | 345 |
temp := prevScanline. |
346 |
prevScanline := thisScanline. |
|
347 |
thisScanline := temp |
|
348 |
] |
|
1671 | 349 |
! |
350 |
||
351 |
processPHYSChunkLen:len |
|
352 |
"physical pixel chunk - currently unhandled" |
|
353 |
||
2705 | 354 |
|pixelPerUnitX pixelPerUnitY unit| |
355 |
||
356 |
pixelPerUnitX := inStream nextLongMSB:true. |
|
357 |
pixelPerUnitY := inStream nextLongMSB:true. |
|
358 |
unit := inStream nextByte. |
|
359 |
||
2707 | 360 |
Verbose == true ifTrue:[ |
361 |
'PNGReader: PHYS chunk ignored:' infoPrintCR. |
|
362 |
'PNGReader: ppuX:' infoPrint. pixelPerUnitX infoPrintCR. |
|
363 |
'PNGReader: ppuY:' infoPrint. pixelPerUnitY infoPrintCR. |
|
364 |
'PNGReader: unit:' infoPrint. unit infoPrintCR. |
|
365 |
]. |
|
366 |
||
2705 | 367 |
^ true |
308 | 368 |
! |
369 |
||
370 |
processPLTEChunkLen:len |
|
1671 | 371 |
"read a color palette" |
372 |
||
2725 | 373 |
|n "{ Class: SmallInteger }" |
374 |
redBytes greenBytes blueBytes| |
|
308 | 375 |
|
376 |
(len \\ 3) ~~ 0 ifTrue:[ |
|
377 |
'PNGReader: invalid size of PLTE chunk' infoPrintCR. |
|
378 |
^ false |
|
379 |
]. |
|
380 |
n := len // 3. |
|
381 |
redBytes := ByteArray new:n. |
|
382 |
greenBytes := ByteArray new:n. |
|
383 |
blueBytes := ByteArray new:n. |
|
384 |
||
385 |
1 to:n do:[:i | |
|
386 |
redBytes at:i put:(inStream nextByte). |
|
387 |
greenBytes at:i put:(inStream nextByte). |
|
388 |
blueBytes at:i put:(inStream nextByte) |
|
389 |
]. |
|
390 |
||
2725 | 391 |
colorMap := MappedPalette |
392 |
redVector:redBytes |
|
393 |
greenVector:greenBytes |
|
394 |
blueVector:blueBytes. |
|
395 |
||
308 | 396 |
^ true |
397 |
||
398 |
"Created: 21.6.1996 / 21:22:28 / cg" |
|
399 |
"Modified: 21.6.1996 / 21:43:01 / cg" |
|
400 |
! |
|
401 |
||
402 |
processSBITChunkLen:len |
|
403 |
inStream skip:len. |
|
404 |
^ true |
|
405 |
||
406 |
"Created: 21.6.1996 / 21:13:09 / cg" |
|
407 |
! |
|
408 |
||
409 |
processTEXTChunkLen:len |
|
410 |
inStream skip:len. |
|
411 |
^ true |
|
412 |
||
413 |
"Modified: 21.6.1996 / 21:15:27 / cg" |
|
414 |
! |
|
415 |
||
416 |
processTIMEChunkLen:len |
|
417 |
inStream skip:len. |
|
418 |
^ true |
|
419 |
||
420 |
"Created: 21.6.1996 / 21:15:43 / cg" |
|
421 |
"Modified: 21.6.1996 / 21:20:42 / cg" |
|
422 |
! |
|
423 |
||
2707 | 424 |
processTRNSChunkLen:len |
425 |
inStream skip:len. |
|
426 |
^ true |
|
427 |
! |
|
428 |
||
308 | 429 |
processZTXTChunkLen:len |
430 |
inStream skip:len. |
|
431 |
^ true |
|
432 |
||
433 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
434 |
! ! |
|
435 |
||
2696 | 436 |
!PNGReader methodsFor:'private-filtering'! |
437 |
||
2699 | 438 |
filterAverage: count |
439 |
"Use the average of the pixel to the left and the pixel above as a predictor" |
|
440 |
||
441 |
| delta | |
|
442 |
delta := self bitsPerPixel // 8 max: 1. |
|
443 |
1 to: delta do: [:i | |
|
444 |
thisScanline at: i put: ((thisScanline at: i) + ((prevScanline at: i) // 2) bitAnd: 255)]. |
|
445 |
delta + 1 to: count do: [:i | |
|
446 |
thisScanline at: i put: |
|
447 |
((thisScanline at: i) |
|
448 |
+ ((prevScanline at: i) |
|
449 |
+ (thisScanline at: i - delta) // 2) bitAnd: 255)] |
|
450 |
! |
|
451 |
||
2696 | 452 |
filterHorizontal:count |
453 |
"use the pixel to the left as a predictor" |
|
454 |
||
455 |
|delta| |
|
456 |
||
457 |
delta := self bitsPerPixel // 8 max:1. |
|
458 |
delta+1 to:count do:[:i| |
|
459 |
thisScanline at:i put:(((thisScanline at:i)+ (thisScanline at:i-delta)) bitAnd:255) |
|
460 |
] |
|
461 |
! |
|
462 |
||
2699 | 463 |
filterNone: count |
464 |
! |
|
465 |
||
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
466 |
filterPaeth: count |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
467 |
"Select one of (the pixel to the left, the pixel above and the pixel to above left) to |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
468 |
predict the value of this pixel" |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
469 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
470 |
| delta | |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
471 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
472 |
delta _ self bitsPerPixel // 8 max: 1. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
473 |
1 to: delta do: [ :i | |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
474 |
thisScanline at: i put: |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
475 |
(((thisScanline at: i) + (prevScanline at: i)) bitAnd: 255)]. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
476 |
delta+1 to: count do: [ :i | |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
477 |
thisScanline |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
478 |
at: i |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
479 |
put: (((thisScanline at: i) + (self |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
480 |
paethPredictLeft: (thisScanline at: i-delta) |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
481 |
above: (prevScanline at: i) |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
482 |
aboveLeft: (prevScanline at: i-delta))) |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
483 |
bitAnd: 255)] |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
484 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
485 |
|
2696 | 486 |
filterScanline:filterType count:count |
487 |
self |
|
488 |
perform:(#(filterNone: filterHorizontal: filterVertical: filterAverage: filterPaeth:) |
|
489 |
at:filterType+1) |
|
490 |
with:count |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
491 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
492 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
493 |
filterVertical: count |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
494 |
"Use the pixel above as a predictor" |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
495 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
496 |
1 to: count do: [ :i | |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
497 |
thisScanline at: i put: (((thisScanline at: i) + |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
498 |
(prevScanline at: i)) bitAnd: 255) |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
499 |
] |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
500 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
501 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
502 |
paethPredictLeft: a above: b aboveLeft: c |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
503 |
"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
|
504 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
505 |
| pa pb pc | |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
506 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
507 |
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
|
508 |
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
|
509 |
pc := a + b - c - c. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
510 |
pc < 0 ifTrue: [ |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
511 |
pc := pc * -1 |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
512 |
]. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
513 |
((pa <= pb) and: [pa <= pc]) ifTrue: [^ a]. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
514 |
(pb <= pc) ifTrue: [^ b]. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
515 |
^ c |
2696 | 516 |
! ! |
517 |
||
2699 | 518 |
!PNGReader methodsFor:'private-pixel copy'! |
519 |
||
2725 | 520 |
copyPixels:y at:startX by:incX |
2704 | 521 |
"Handle interlaced pixels of supported colorTypes" |
2699 | 522 |
|
2704 | 523 |
| s | |
1848 | 524 |
|
2704 | 525 |
s := #(copyPixelsGray:at:by: nil copyPixelsRGB:at:by: copyPixelsIndexed:at:by: |
526 |
copyPixelsGrayAlpha:at:by: nil copyPixelsRGBA:at:by:) at: colorType+1. |
|
527 |
self perform: s asSymbol with: y with: startX with: incX |
|
2699 | 528 |
! ! |
529 |
||
530 |
!PNGReader methodsFor:'private-reading'! |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
531 |
|
2683 | 532 |
getChunk |
533 |
|len type crc| |
|
534 |
||
535 |
inStream atEnd ifTrue:[^ false]. |
|
536 |
||
537 |
len := inStream nextLongMSB:true. |
|
538 |
type := String new:4. |
|
539 |
(inStream nextBytes:4 into:type) ~~ 4 ifTrue:[^ false]. |
|
540 |
||
2707 | 541 |
Verbose == true ifTrue:[ |
542 |
'len: ' infoPrint. len infoPrint. ' type: ' infoPrint. type infoPrintCR. |
|
543 |
]. |
|
2683 | 544 |
|
545 |
type = 'IEND' ifTrue:[^ false]. |
|
546 |
||
547 |
(self processChunk:type len:len) ifFalse:[^ false]. |
|
548 |
||
549 |
crc := inStream nextLongMSB:true. "/ ignored - for now |
|
550 |
^ true |
|
551 |
||
552 |
"Created: 21.6.1996 / 21:09:36 / cg" |
|
553 |
"Modified: 21.6.1996 / 21:20:26 / cg" |
|
554 |
! |
|
555 |
||
556 |
getIHDRChunk |
|
557 |
|len type crc| |
|
558 |
||
559 |
len := inStream nextLongMSB:true. |
|
560 |
||
561 |
type := String new:4. |
|
562 |
inStream nextBytes:4 into:type. |
|
563 |
||
564 |
type = 'IHDR' ifFalse:[self halt. ^ false]. |
|
565 |
len == 13 ifFalse:[self halt. ^ false]. |
|
566 |
||
567 |
width := inStream nextLongMSB:true. |
|
568 |
height := inStream nextLongMSB:true. |
|
569 |
||
570 |
(width <= 0 or:[height <= 0]) ifTrue:[ |
|
571 |
'PNGReader: invalid dimension(s)' infoPrintCR. |
|
572 |
^ false. |
|
573 |
]. |
|
574 |
self reportDimension. |
|
575 |
||
576 |
depth := inStream nextByte. "/ bits-per-channel |
|
577 |
colorType := inStream nextByte. |
|
578 |
||
579 |
compressionMethod := inStream nextByte. |
|
580 |
filterMethod := inStream nextByte. |
|
581 |
interlaceMode := inStream nextByte. |
|
582 |
||
2707 | 583 |
Verbose == true ifTrue:[ |
584 |
'PNGReader: IHDR:' infoPrintCR. |
|
585 |
'PNGReader: width: ' infoPrint. width infoPrintCR. |
|
586 |
'PNGReader: height: ' infoPrint. height infoPrintCR. |
|
587 |
'PNGReader: depth: ' infoPrint. depth infoPrintCR. |
|
588 |
'PNGReader: colorType: ' infoPrint. colorType infoPrintCR. |
|
589 |
'PNGReader: compressionMethod: ' infoPrint. compressionMethod infoPrintCR. |
|
590 |
'PNGReader: filterMethod: ' infoPrint. filterMethod infoPrintCR. |
|
591 |
'PNGReader: interlaceMode: ' infoPrint. interlaceMode infoPrintCR. |
|
592 |
]. |
|
593 |
||
594 |
crc := inStream nextLongMSB:true. |
|
2683 | 595 |
^ true |
596 |
||
597 |
"Modified: 21.6.1996 / 21:38:35 / cg" |
|
598 |
! |
|
599 |
||
1848 | 600 |
setColorType:colorType |
601 |
colorType == 0 ifTrue:[ |
|
602 |
photometric := #blackIs0. |
|
603 |
samplesPerPixel := 1. |
|
604 |
bitsPerSample := Array with:depth. |
|
605 |
^ true. |
|
606 |
]. |
|
607 |
||
608 |
colorType == 2 ifTrue:[ |
|
609 |
depth < 8 ifTrue:[ |
|
610 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
|
611 |
^ false. |
|
612 |
]. |
|
613 |
photometric := #rgb. |
|
614 |
samplesPerPixel := 3. |
|
615 |
bitsPerSample := Array with:depth with:depth with:depth. |
|
616 |
^ true. |
|
617 |
]. |
|
618 |
||
619 |
colorType == 3 ifTrue:[ |
|
620 |
depth == 16 ifTrue:[ |
|
621 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
|
622 |
^ false. |
|
623 |
]. |
|
624 |
photometric := #palette. |
|
625 |
samplesPerPixel := 1. |
|
626 |
bitsPerSample := Array with:depth. |
|
627 |
^ true. |
|
628 |
]. |
|
629 |
||
630 |
colorType == 4 ifTrue:[ |
|
631 |
depth < 8 ifTrue:[ |
|
632 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
|
633 |
^ false. |
|
634 |
]. |
|
635 |
photometric := #blackIs0. |
|
636 |
samplesPerPixel := 2. |
|
637 |
bitsPerSample := Array with:depth with:depth. |
|
638 |
^ true. |
|
639 |
]. |
|
640 |
||
641 |
colorType == 6 ifTrue:[ |
|
642 |
depth < 8 ifTrue:[ |
|
643 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
|
644 |
^ false. |
|
645 |
]. |
|
2702 | 646 |
photometric := #rgb. |
1848 | 647 |
samplesPerPixel := 4. |
648 |
bitsPerSample := Array with:depth with:depth with:depth with:depth. |
|
649 |
^ true. |
|
650 |
]. |
|
651 |
||
652 |
('PNGReader: invalid colorType: ' , colorType printString , '.') infoPrintCR. |
|
653 |
^ false |
|
654 |
! ! |
|
655 |
||
1805 | 656 |
!PNGReader methodsFor:'reading'! |
308 | 657 |
|
658 |
fromStream:aStream |
|
659 |
"read a stream containing a PNG image. |
|
660 |
Leave image description in instance variables." |
|
661 |
||
662 |
|header| |
|
663 |
||
664 |
inStream := aStream. |
|
665 |
aStream binary. |
|
666 |
||
1671 | 667 |
"PNG-files are always msb (network-world)" |
308 | 668 |
byteOrder := #msb. |
669 |
||
670 |
header := ByteArray new:8. |
|
671 |
aStream nextBytes:8 into:header. |
|
672 |
||
2683 | 673 |
header ~= (self pngHeader) ifTrue:[ |
308 | 674 |
'PNGReader: not a png file.' infoPrintCR. |
675 |
^ nil |
|
676 |
]. |
|
2695 | 677 |
|
308 | 678 |
(self getIHDRChunk) ifFalse:[ |
679 |
'PNGReader: required IHDR chunk missing.' infoPrintCR. |
|
680 |
^ nil |
|
681 |
]. |
|
682 |
||
683 |
compressionMethod ~~ 0 ifTrue:[ |
|
684 |
('PNGReader: compressionMethod ' , compressionMethod printString , ' not supported.') infoPrintCR. |
|
685 |
^ nil |
|
686 |
]. |
|
687 |
filterMethod > 0 ifTrue:[ |
|
688 |
'PNGReader: invalid filterMethod' infoPrintCR. |
|
689 |
^ nil. |
|
690 |
]. |
|
691 |
interlaceMode > 1 ifTrue:[ |
|
692 |
'PNGReader: invalid interlaceMode' infoPrintCR. |
|
693 |
^ nil. |
|
694 |
]. |
|
1671 | 695 |
(self setColorType:colorType) ifFalse:[ |
308 | 696 |
^ nil |
1671 | 697 |
]. |
2706 | 698 |
|
1671 | 699 |
[self getChunk] whileTrue. |
308 | 700 |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
701 |
data := ByteArray new:(self bytesPerRow * height). |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
702 |
|
2509 | 703 |
globalDataChunk notNil ifTrue:[ |
704 |
self processGlobalIDATChunk |
|
705 |
]. |
|
706 |
||
308 | 707 |
photometric == #palette ifTrue:[ |
2725 | 708 |
colorMap isNil ifTrue:[ |
308 | 709 |
'PNGReader: missing palette chunk.' infoPrintCR. |
2725 | 710 |
"/ ^ nil |
308 | 711 |
]. |
712 |
]. |
|
713 |
||
714 |
" |
|
715 |
PNGReader fromFile:'/home/cg/libpng-0.89c/pngtest.png' |
|
716 |
" |
|
717 |
||
718 |
"Modified: 21.6.1996 / 21:44:34 / cg" |
|
1805 | 719 |
! ! |
720 |
||
2683 | 721 |
!PNGReader methodsFor:'writing to file'! |
722 |
||
723 |
pngHeader |
|
724 |
^ #[137 80 78 71 13 10 26 10] |
|
725 |
! |
|
726 |
||
727 |
save:image onStream:aStream |
|
728 |
"save image in PNG-file-format onto aStream" |
|
729 |
||
730 |
outStream := aStream. |
|
731 |
outStream binary. |
|
732 |
||
2703 | 733 |
byteOrder := #msb. |
2683 | 734 |
width := image width. |
735 |
height := image height. |
|
736 |
photometric := image photometric. |
|
737 |
samplesPerPixel := image samplesPerPixel. |
|
738 |
bitsPerSample := image bitsPerSample. |
|
739 |
colorMap := image colorMap. |
|
740 |
data := image bits. |
|
741 |
||
2703 | 742 |
outStream nextPutAll:(self pngHeader). |
743 |
self writeIHDRChunk. |
|
744 |
photometric == #palette ifTrue: [self writePaletteChunk]. |
|
745 |
self writeImageDataChunk. |
|
2683 | 746 |
self writeEndChunk |
747 |
! |
|
748 |
||
2703 | 749 |
writeChunk:chunkTypeChars size:len with:aBlock |
2708 | 750 |
|crc realOutStream chunkBytes| |
751 |
||
752 |
realOutStream := outStream. |
|
753 |
outStream := WriteStream on:(ByteArray new:len). |
|
754 |
||
2703 | 755 |
outStream nextPutBytes:4 from:chunkTypeChars startingAt:1. |
756 |
aBlock value. |
|
2708 | 757 |
|
758 |
chunkBytes := outStream contents. |
|
759 |
crc := CRC32Stream hashValueOf:chunkBytes. |
|
760 |
||
761 |
outStream := realOutStream. |
|
762 |
||
763 |
realOutStream nextPutLong:len MSB:true. |
|
764 |
realOutStream nextPutAll:chunkBytes. |
|
765 |
realOutStream nextPutLong:crc MSB:true. |
|
2703 | 766 |
! |
2683 | 767 |
|
2703 | 768 |
writeEndChunk |
769 |
self |
|
770 |
writeChunk: 'IEND' |
|
771 |
size: 0 |
|
772 |
with:[ ] |
|
2683 | 773 |
! |
774 |
||
775 |
writeFileHeader |
|
776 |
outStream nextPutAll:(self pngHeader) |
|
777 |
! |
|
778 |
||
2703 | 779 |
writeIHDRChunk |
780 |
|colorType| |
|
781 |
||
782 |
(photometric == #blackIs0) ifTrue:[ |
|
783 |
colorType := 0. |
|
784 |
samplesPerPixel size > 1 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
|
785 |
]. |
|
786 |
(photometric == #rgb) ifTrue:[ |
|
787 |
colorType := 2. |
|
788 |
samplesPerPixel size > 3 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
|
789 |
]. |
|
790 |
(photometric == #palette) ifTrue:[ |
|
791 |
colorType := 3. |
|
792 |
samplesPerPixel size > 1 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
|
793 |
]. |
|
794 |
||
2683 | 795 |
self |
796 |
writeChunk: 'IHDR' |
|
797 |
size: 13 |
|
798 |
with:[ |
|
799 |
self writeLong:width. |
|
800 |
self writeLong:height. |
|
2703 | 801 |
outStream nextPut: 8. "Assume that all colors are 24bit" "/ depth |
802 |
outStream nextPut: colorType. |
|
2683 | 803 |
outStream nextPut: 0. "Compression" |
804 |
outStream nextPut: 0. "Filter method" |
|
805 |
outStream nextPut: 0 "Non-interlaced" |
|
806 |
] |
|
807 |
! |
|
808 |
||
2703 | 809 |
writeImageDataChunk |
2708 | 810 |
|compressedByteStream compressedBytes zlibWriter bytesPerScanline idx| |
2703 | 811 |
|
2708 | 812 |
compressedByteStream := WriteStream on:(ByteArray new:100). |
813 |
zlibWriter := ZipStream writeOpenAsZipStreamOn:compressedByteStream suppressHeaderAndChecksum:false. |
|
2703 | 814 |
zlibWriter binary. |
815 |
||
816 |
bytesPerScanline := self bytesPerRow. |
|
817 |
||
2708 | 818 |
idx := 1. |
819 |
0 to:height-1 do:[:y | |
|
820 |
|row| |
|
821 |
||
822 |
row := data copyFrom:idx to:(idx+bytesPerScanline-1). |
|
823 |
zlibWriter nextPutAll:#[0]. "/ no filter |
|
824 |
zlibWriter nextPutAll:row. |
|
825 |
idx := idx + bytesPerScanline. |
|
2703 | 826 |
]. |
2708 | 827 |
|
828 |
zlibWriter close. |
|
829 |
compressedBytes := compressedByteStream contents. |
|
830 |
||
831 |
self |
|
832 |
writeChunk: 'IDAT' |
|
833 |
size:(compressedBytes size) |
|
834 |
with:[ |
|
835 |
outStream nextPutAll:compressedBytes |
|
836 |
] |
|
2703 | 837 |
! |
838 |
||
2683 | 839 |
writePaletteChunk |
840 |
self |
|
841 |
writeChunk: 'PLTE' |
|
842 |
size: 3 * colorMap size |
|
843 |
with:[ |
|
2703 | 844 |
colorMap do:[:color | |
845 |
outStream |
|
846 |
nextPut: color redByte; |
|
847 |
nextPut: color greenByte; |
|
848 |
nextPut: color blueByte |
|
2683 | 849 |
] |
850 |
] |
|
851 |
! ! |
|
852 |
||
398 | 853 |
!PNGReader class methodsFor:'documentation'! |
308 | 854 |
|
855 |
version |
|
2725 | 856 |
^ '$Header: /cvs/stx/stx/libview2/PNGReader.st,v 1.28 2009-08-07 15:31:40 cg Exp $' |
308 | 857 |
! ! |
1671 | 858 |
|
308 | 859 |
PNGReader initialize! |