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