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