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