author | Stefan Vogel <sv@exept.de> |
Thu, 23 Apr 2015 23:25:39 +0200 | |
changeset 3461 | eb5a53c6b5b2 |
parent 3437 | 4e1cf7b6b492 |
child 3505 | af4fb19df063 |
child 3554 | f1820748e4a9 |
permissions | -rw-r--r-- |
3461 | 1 |
"{ Encoding: utf8 }" |
2 |
||
308 | 3 |
" |
4 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
5 |
All Rights Reserved |
|
6 |
||
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
1671 | 14 |
"{ Package: 'stx:libview2' }" |
15 |
||
3461 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
308 | 18 |
ImageReader subclass:#PNGReader |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
19 |
instanceVariableNames:'colorType bitsPerChannel depth compressionMethod filterMethod |
2725 | 20 |
interlaceMode bytesPerScanline globalDataChunk thisScanline |
21 |
prevScanline' |
|
2707 | 22 |
classVariableNames:'Verbose' |
308 | 23 |
poolDictionaries:'' |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1694
diff
changeset
|
24 |
category:'Graphics-Images-Readers' |
308 | 25 |
! |
26 |
||
398 | 27 |
!PNGReader class methodsFor:'documentation'! |
308 | 28 |
|
29 |
copyright |
|
30 |
" |
|
31 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
32 |
All Rights Reserved |
|
33 |
||
34 |
This software is furnished under a license and may be used |
|
35 |
only in accordance with the terms of that license and with the |
|
36 |
inclusion of the above copyright notice. This software may not |
|
37 |
be provided or otherwise made available to, or used by, any |
|
38 |
other person. No title to or ownership of the software is |
|
39 |
hereby transferred. |
|
40 |
" |
|
41 |
||
42 |
||
43 |
! |
|
44 |
||
45 |
documentation |
|
46 |
" |
|
2725 | 47 |
This class will provide methods for loading and saving PNG pictures. |
3437 | 48 |
It is currenty unfinished (interlaced image support is missing resp. being developed as we encounter more formats). |
49 |
In the meantime, use a pngtoXXX converter for interlaced images, and read XXX. |
|
308 | 50 |
|
2725 | 51 |
[caveats:] |
52 |
writer ignores any mask (for now). |
|
53 |
writer only generates unfiltered rows or interlace data |
|
54 |
||
308 | 55 |
[See also:] |
56 |
Image Form Icon |
|
57 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader |
|
58 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
|
59 |
XBMReader XPMReader XWDReader |
|
60 |
||
61 |
[author:] |
|
62 |
Claus Gittinger |
|
63 |
" |
|
1694 | 64 |
! |
308 | 65 |
|
1694 | 66 |
examples |
67 |
" |
|
68 |
PNGReader fromFile:'/home/cg/AudioExplorer_51_files/use_small.png' |
|
2509 | 69 |
PNGReader fromFile:'\\Exeptn\tmp\images\expeccoScreenshot4020_5526507.png' |
2683 | 70 |
|
71 |
PNGReader fromFile:'C:\Users\cg\Desktop\croquet\cobalt-base-current-build-20090210\cobalt-base-current-build-20090210\content\models\textures\checkerboard.png' |
|
2695 | 72 |
PNGReader fromFile:'C:\Dokumente und Einstellungen\cg\Desktop\misc\PNGs\Delete.png' |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
73 |
PNGReader fromFile:'\\exeptn\unsaved\pd_stuff\PNGs\Delete.png' |
2699 | 74 |
PNGReader fromFile:'\\exeptn\unsaved\pd_stuff\PNGs\Down.png' |
2703 | 75 |
|
76 |
||
77 |
|img img2 outStream png| |
|
2725 | 78 |
|
2703 | 79 |
img := ToolbarIconLibrary error32x32Icon. |
80 |
outStream := WriteStream on:(ByteArray new:100). |
|
81 |
PNGReader save:img onStream:outStream. |
|
82 |
png := outStream contents. |
|
83 |
img2 := PNGReader fromStream:(png readStream). |
|
2708 | 84 |
self assert:(img bits = img2 bits). |
85 |
img inspect. |
|
2703 | 86 |
img2 inspect. |
1694 | 87 |
" |
308 | 88 |
! ! |
89 |
||
398 | 90 |
!PNGReader class methodsFor:'initialization'! |
308 | 91 |
|
92 |
initialize |
|
2699 | 93 |
"install myself in the Image classes fileFormat table for the `.png' extension." |
308 | 94 |
|
2709 | 95 |
MIMETypes defineImageType:'image/png' suffix:'png' reader:self. |
96 |
"/ backward compatibility from times when png was not yet so common... |
|
97 |
MIMETypes defineImageType:'image/x-png' suffix:nil reader:self. |
|
308 | 98 |
! ! |
99 |
||
2705 | 100 |
!PNGReader class methodsFor:'queries'! |
101 |
||
102 |
canRepresent:anImage |
|
103 |
"return true, if anImage can be represented in my file format. |
|
104 |
Any image is supported." |
|
105 |
||
106 |
^ true |
|
107 |
! ! |
|
108 |
||
398 | 109 |
!PNGReader class methodsFor:'testing'! |
308 | 110 |
|
111 |
isValidImageFile:aFileName |
|
112 |
"return true, if aFileName contains a PNG image" |
|
113 |
||
114 |
|inStream magic| |
|
115 |
||
116 |
inStream := self streamReadingFile:aFileName. |
|
117 |
inStream isNil ifTrue:[^ false]. |
|
118 |
||
119 |
inStream binary. |
|
120 |
||
121 |
magic := ByteArray new:8. |
|
122 |
inStream nextBytes:8 into:magic. |
|
123 |
inStream close. |
|
124 |
||
125 |
^ (magic = #[137 80 78 71 13 10 26 10]) |
|
126 |
||
1694 | 127 |
" |
128 |
self isValidImageFile:'/home/cg/AudioExplorer_51_files/use_small.png' |
|
2683 | 129 |
self isValidImageFile:'C:\Users\cg\Desktop\croquet\cobalt-base-current-build-20090210\cobalt-base-current-build-20090210\content\models\textures\checkerboard.png' |
2695 | 130 |
self isValidImageFile:'C:\Dokumente und Einstellungen\cg\Desktop\misc\PNGs\Delete.png' |
1694 | 131 |
" |
132 |
||
308 | 133 |
"Modified: 21.6.1996 / 20:38:46 / cg" |
134 |
! ! |
|
135 |
||
2696 | 136 |
!PNGReader methodsFor:'private-chunks'! |
308 | 137 |
|
2699 | 138 |
doPass: pass |
139 |
"Certain interlace passes are skipped with certain small image dimensions" |
|
140 |
||
141 |
pass = 1 ifTrue: [ ^ true ]. |
|
142 |
((width = 1) and: [height = 1]) ifTrue: [ ^ false ]. |
|
143 |
pass = 2 ifTrue: [ ^ width >= 5 ]. |
|
144 |
pass = 3 ifTrue: [ ^ height >= 5 ]. |
|
145 |
pass = 4 ifTrue: [ ^ (width >=3 ) or: [height >= 5] ]. |
|
146 |
pass = 5 ifTrue: [ ^ height >=3 ]. |
|
147 |
pass = 6 ifTrue: [ ^ width >=2 ]. |
|
148 |
pass = 7 ifTrue: [ ^ height >=2 ]. |
|
149 |
||
150 |
self error:'invalid argument'. |
|
151 |
^ true |
|
152 |
! |
|
153 |
||
308 | 154 |
processBKGDChunkLen:len |
155 |
inStream skip:len. |
|
156 |
^ true |
|
157 |
||
158 |
"Created: 21.6.1996 / 21:15:49 / cg" |
|
159 |
! |
|
160 |
||
2707 | 161 |
processCHRMChunkLen:len |
162 |
"cHRM Primary chromaticities chunk - currently unhandled" |
|
163 |
||
164 |
|whitePointX whitePointY redX redY greenX greenY blueX blueY| |
|
165 |
||
166 |
whitePointX := (inStream nextLongMSB:true) / 100000.0. |
|
167 |
whitePointY := (inStream nextLongMSB:true) / 100000.0. |
|
168 |
redX := (inStream nextLongMSB:true) / 100000.0. |
|
169 |
redY := (inStream nextLongMSB:true) / 100000.0. |
|
170 |
greenX := (inStream nextLongMSB:true) / 100000.0. |
|
171 |
greenY := (inStream nextLongMSB:true) / 100000.0. |
|
172 |
blueX := (inStream nextLongMSB:true) / 100000.0. |
|
173 |
blueY := (inStream nextLongMSB:true) / 100000.0. |
|
174 |
||
175 |
Verbose == true ifTrue:[ |
|
176 |
'PNGReader: CHRM chunk ignored:' infoPrintCR. |
|
177 |
'PNGReader: whitePointX:' infoPrint. whitePointX infoPrintCR. |
|
178 |
'PNGReader: whitePointY:' infoPrint. whitePointX infoPrintCR. |
|
179 |
'PNGReader: redX:' infoPrint. redX infoPrintCR. |
|
180 |
'PNGReader: redY:' infoPrint. redY infoPrintCR. |
|
181 |
'PNGReader: greenX:' infoPrint. greenX infoPrintCR. |
|
182 |
'PNGReader: greenY:' infoPrint. greenY infoPrintCR. |
|
183 |
'PNGReader: blueX:' infoPrint. blueX infoPrintCR. |
|
184 |
'PNGReader: blueY:' infoPrint. blueY infoPrintCR. |
|
185 |
]. |
|
186 |
^ true |
|
187 |
! |
|
188 |
||
308 | 189 |
processChunk:type len:len |
2509 | 190 |
|chunk| |
191 |
||
192 |
type = 'IDAT' ifTrue:[ |
|
193 |
"---since the compressed data can span multiple |
|
194 |
chunks, stitch them all together first. later, |
|
195 |
if memory is an issue, we need to figure out how |
|
196 |
to do this on the fly---" |
|
197 |
chunk := inStream next:len. |
|
198 |
globalDataChunk := globalDataChunk isNil |
|
199 |
ifTrue: [chunk] |
|
200 |
ifFalse:[globalDataChunk,chunk]. |
|
201 |
^ true |
|
202 |
"/ ^ self processIDATChunkLen:len |
|
203 |
]. |
|
204 |
||
308 | 205 |
type = 'gAMA' ifTrue:[^ self processGAMAChunkLen:len]. |
206 |
type = 'sBIT' ifTrue:[^ self processSBITChunkLen:len]. |
|
207 |
type = 'tEXt' ifTrue:[^ self processTEXTChunkLen:len]. |
|
208 |
type = 'tIME' ifTrue:[^ self processTIMEChunkLen:len]. |
|
209 |
type = 'bKGD' ifTrue:[^ self processBKGDChunkLen:len]. |
|
210 |
type = 'zTXt' ifTrue:[^ self processZTXTChunkLen:len]. |
|
211 |
type = 'PLTE' ifTrue:[^ self processPLTEChunkLen:len]. |
|
212 |
||
1671 | 213 |
type = 'pHYs' ifTrue:[^ self processPHYSChunkLen:len]. |
2707 | 214 |
type = 'cHRM' ifTrue:[^ self processCHRMChunkLen:len]. |
1671 | 215 |
type = 'tRNS' ifTrue:[^ self processTRNSChunkLen:len]. |
216 |
type = 'IHDR' ifTrue:[^ self processIHDRChunkLen:len]. |
|
217 |
||
308 | 218 |
('PNGReader: unrecognized chunk: ' , type , ' ignored.') infoPrintCR. |
219 |
||
220 |
inStream skip:len. |
|
221 |
^ true. |
|
222 |
||
223 |
"Created: 21.6.1996 / 21:10:37 / cg" |
|
224 |
"Modified: 21.6.1996 / 21:22:37 / cg" |
|
225 |
! |
|
226 |
||
227 |
processGAMAChunkLen:len |
|
228 |
inStream skip:len. |
|
229 |
^ true |
|
230 |
||
231 |
"Created: 21.6.1996 / 21:10:52 / cg" |
|
232 |
! |
|
233 |
||
2509 | 234 |
processGlobalIDATChunk |
235 |
interlaceMode == 0 ifTrue: [ |
|
236 |
^ self processNonInterlacedGlobalDATA |
|
237 |
]. |
|
238 |
^ self processInterlacedGlobalDATA |
|
239 |
! |
|
240 |
||
308 | 241 |
processIDATChunkLen:len |
1671 | 242 |
interlaceMode == 0 ifTrue: [ |
243 |
^ self processNonInterlacedDATA:len |
|
244 |
]. |
|
245 |
^ self processInterlacedDATA:len |
|
246 |
! |
|
247 |
||
248 |
processInterlacedDATA:len |
|
308 | 249 |
inStream skip:len. |
250 |
^ true |
|
1671 | 251 |
! |
308 | 252 |
|
2699 | 253 |
processInterlacedGlobalDATA |
3327 | 254 |
"adam7 interlace method" |
255 |
||
2699 | 256 |
| zlibReader filter bytesPerPass startingCol colIncrement rowIncrement |
3327 | 257 |
startingRow cx sc temp "filtersSeen" bitsPerPixel cy| |
2699 | 258 |
|
3327 | 259 |
interlaceMode == 1 ifFalse: [ |
260 |
self error:'unsupported interlaced mode'. |
|
261 |
^ self |
|
262 |
]. |
|
2699 | 263 |
|
264 |
"/ filtersSeen := Set new. |
|
265 |
||
266 |
startingCol := #(0 4 0 2 0 1 0 ). |
|
3327 | 267 |
startingRow := #(0 0 4 0 2 0 1 ). |
2699 | 268 |
colIncrement := #(8 8 4 4 2 2 1 ). |
269 |
rowIncrement := #(8 8 8 4 4 2 2 ). |
|
270 |
||
2701 | 271 |
zlibReader := ZipStream readOpenAsZipStreamOn:(globalDataChunk readStream) suppressHeaderAndChecksum:false. |
2699 | 272 |
zlibReader binary. |
273 |
bytesPerScanline := self bytesPerRow. |
|
274 |
bitsPerPixel := self bitsPerPixel. |
|
275 |
||
276 |
1 to: 7 do: [:pass | |
|
277 |
(self doPass: pass) ifTrue:[ |
|
278 |
cx := colIncrement at: pass. |
|
279 |
sc := startingCol at: pass. |
|
3327 | 280 |
cy := rowIncrement at: pass. |
2699 | 281 |
bytesPerPass := width - sc + cx - 1 // cx * bitsPerPixel + 7 // 8. |
282 |
prevScanline := ByteArray new: bytesPerPass. |
|
283 |
thisScanline := ByteArray new: bytesPerScanline. |
|
3327 | 284 |
(startingRow at: pass) to: height - 1 by: cy do: [:y | |
3343 | 285 |
filter := zlibReader nextByte. |
2699 | 286 |
"/ filtersSeen add: filter. |
287 |
(filter isNil or: [(filter between: 0 and: 4) not]) |
|
288 |
ifTrue: [^ self]. |
|
289 |
zlibReader next: bytesPerPass into: thisScanline startingAt: 1. |
|
290 |
filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerPass ]. |
|
291 |
self copyPixels:y at:sc by:cx. |
|
292 |
temp := prevScanline. |
|
293 |
prevScanline := thisScanline. |
|
294 |
thisScanline := temp. |
|
295 |
] |
|
296 |
] |
|
297 |
]. |
|
298 |
zlibReader atEnd ifFalse:[self error:'Unexpected data']. |
|
2909 | 299 |
|
300 |
"Modified: / 03-05-2011 / 12:03:33 / cg" |
|
2699 | 301 |
! |
302 |
||
1671 | 303 |
processNonInterlacedDATA:len |
2701 | 304 |
self halt:'not used' |
305 |
"/ |
|
306 |
"/ | zlibReader filter temp prevScanline thisScanline bytesPerScanline filtersSeen| |
|
307 |
"/ |
|
308 |
"/ zlibReader := ZipStream readOpenAsZipStreamOn:inStream suppressHeaderAndChecksum:false. |
|
309 |
"/ "/ zlibReader := ZLibReadStream on:inStream from: 1 to:len. |
|
310 |
"/ |
|
311 |
"/ prevScanline := ByteArray new: self bytesPerRow. |
|
312 |
"/ thisScanline := ByteArray new: self bytesPerRow. |
|
313 |
"/ 0 to: height - 1 do:[:index | |
|
314 |
"/ filter := (zlibReader next: 1) first. |
|
315 |
"/ filtersSeen add: filter. |
|
316 |
"/ (filter isNil or: [(filter between: 0 and: 4) not]) ifTrue: [^self]. |
|
317 |
"/ thisScanline := zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
|
318 |
"/ self filterScanline: filter count: bytesPerScanline. |
|
319 |
"/ self copyPixels: index. |
|
320 |
"/ temp := prevScanline. |
|
321 |
"/ prevScanline := thisScanline. |
|
322 |
"/ thisScanline := temp |
|
323 |
"/ ] |
|
2509 | 324 |
! |
325 |
||
326 |
processNonInterlacedGlobalDATA |
|
2725 | 327 |
| "data n" zlibReader filter temp bytesPerScanline "filtersSeen" i| |
2696 | 328 |
|
329 |
"/ filtersSeen := Set new. |
|
2695 | 330 |
|
331 |
"/ data := ByteArray new:(self bytesPerRow * height)+1000. |
|
332 |
"/ n := ZipStream uncompress: globalDataChunk into: data. |
|
333 |
"/ self halt. |
|
2509 | 334 |
|
2701 | 335 |
zlibReader := ZipStream readOpenAsZipStreamOn:(globalDataChunk readStream) suppressHeaderAndChecksum:false. |
2696 | 336 |
zlibReader binary. |
2509 | 337 |
bytesPerScanline := self bytesPerRow. |
338 |
||
1671 | 339 |
prevScanline := ByteArray new: bytesPerScanline. |
340 |
thisScanline := ByteArray new: bytesPerScanline. |
|
2725 | 341 |
0 to: height - 1 do:[:y | |
3343 | 342 |
filter := zlibReader nextByte. |
2696 | 343 |
"/ filtersSeen add: filter. |
2725 | 344 |
(filter notNil and: [filter between: 0 and: 4]) ifFalse: [ |
345 |
'PNGReader: unsupported filter' infoPrintCR. |
|
346 |
^ self |
|
347 |
]. |
|
2696 | 348 |
zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
349 |
filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerScanline ]. |
|
2725 | 350 |
|
351 |
i := y * bytesPerScanline. |
|
352 |
data replaceFrom:i+1 to:(i+bytesPerScanline) with:thisScanline startingAt:1. |
|
353 |
||
2695 | 354 |
temp := prevScanline. |
355 |
prevScanline := thisScanline. |
|
356 |
thisScanline := temp |
|
357 |
] |
|
1671 | 358 |
! |
359 |
||
360 |
processPHYSChunkLen:len |
|
361 |
"physical pixel chunk - currently unhandled" |
|
362 |
||
2705 | 363 |
|pixelPerUnitX pixelPerUnitY unit| |
364 |
||
365 |
pixelPerUnitX := inStream nextLongMSB:true. |
|
366 |
pixelPerUnitY := inStream nextLongMSB:true. |
|
367 |
unit := inStream nextByte. |
|
368 |
||
2707 | 369 |
Verbose == true ifTrue:[ |
370 |
'PNGReader: PHYS chunk ignored:' infoPrintCR. |
|
371 |
'PNGReader: ppuX:' infoPrint. pixelPerUnitX infoPrintCR. |
|
372 |
'PNGReader: ppuY:' infoPrint. pixelPerUnitY infoPrintCR. |
|
373 |
'PNGReader: unit:' infoPrint. unit infoPrintCR. |
|
374 |
]. |
|
375 |
||
2705 | 376 |
^ true |
308 | 377 |
! |
378 |
||
379 |
processPLTEChunkLen:len |
|
1671 | 380 |
"read a color palette" |
381 |
||
2725 | 382 |
|n "{ Class: SmallInteger }" |
383 |
redBytes greenBytes blueBytes| |
|
308 | 384 |
|
385 |
(len \\ 3) ~~ 0 ifTrue:[ |
|
386 |
'PNGReader: invalid size of PLTE chunk' infoPrintCR. |
|
387 |
^ false |
|
388 |
]. |
|
389 |
n := len // 3. |
|
390 |
redBytes := ByteArray new:n. |
|
391 |
greenBytes := ByteArray new:n. |
|
392 |
blueBytes := ByteArray new:n. |
|
393 |
||
394 |
1 to:n do:[:i | |
|
395 |
redBytes at:i put:(inStream nextByte). |
|
396 |
greenBytes at:i put:(inStream nextByte). |
|
397 |
blueBytes at:i put:(inStream nextByte) |
|
398 |
]. |
|
399 |
||
2725 | 400 |
colorMap := MappedPalette |
401 |
redVector:redBytes |
|
402 |
greenVector:greenBytes |
|
403 |
blueVector:blueBytes. |
|
404 |
||
308 | 405 |
^ true |
406 |
||
407 |
"Created: 21.6.1996 / 21:22:28 / cg" |
|
408 |
"Modified: 21.6.1996 / 21:43:01 / cg" |
|
409 |
! |
|
410 |
||
411 |
processSBITChunkLen:len |
|
412 |
inStream skip:len. |
|
413 |
^ true |
|
414 |
||
415 |
"Created: 21.6.1996 / 21:13:09 / cg" |
|
416 |
! |
|
417 |
||
418 |
processTEXTChunkLen:len |
|
419 |
inStream skip:len. |
|
420 |
^ true |
|
421 |
||
422 |
"Modified: 21.6.1996 / 21:15:27 / cg" |
|
423 |
! |
|
424 |
||
425 |
processTIMEChunkLen:len |
|
426 |
inStream skip:len. |
|
427 |
^ true |
|
428 |
||
429 |
"Created: 21.6.1996 / 21:15:43 / cg" |
|
430 |
"Modified: 21.6.1996 / 21:20:42 / cg" |
|
431 |
! |
|
432 |
||
2707 | 433 |
processTRNSChunkLen:len |
434 |
inStream skip:len. |
|
435 |
^ true |
|
436 |
! |
|
437 |
||
308 | 438 |
processZTXTChunkLen:len |
439 |
inStream skip:len. |
|
440 |
^ true |
|
441 |
||
442 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
443 |
! ! |
|
444 |
||
2696 | 445 |
!PNGReader methodsFor:'private-filtering'! |
446 |
||
2909 | 447 |
filterAverage:count |
2699 | 448 |
"Use the average of the pixel to the left and the pixel above as a predictor" |
2909 | 449 |
|
450 |
|delta| |
|
2699 | 451 |
|
2909 | 452 |
delta := self bitsPerPixel // 8 max:1. |
453 |
1 to:delta do:[:i | |
|
3384 | 454 |
thisScanline at:i put:((thisScanline at:i) + ((prevScanline at:i) // 2) bitAnd:255) |
2909 | 455 |
]. |
456 |
delta + 1 to:count do:[:i | |
|
3384 | 457 |
thisScanline at:i put:((thisScanline at:i) + (((prevScanline at:i) + (thisScanline at:i - delta)) // 2) bitAnd:255) |
2909 | 458 |
] |
459 |
||
460 |
"Modified: / 03-05-2011 / 12:14:01 / cg" |
|
2699 | 461 |
! |
462 |
||
2696 | 463 |
filterHorizontal:count |
464 |
"use the pixel to the left as a predictor" |
|
465 |
||
466 |
|delta| |
|
467 |
||
468 |
delta := self bitsPerPixel // 8 max:1. |
|
469 |
delta+1 to:count do:[:i| |
|
470 |
thisScanline at:i put:(((thisScanline at:i)+ (thisScanline at:i-delta)) bitAnd:255) |
|
471 |
] |
|
472 |
! |
|
473 |
||
2699 | 474 |
filterNone: count |
2909 | 475 |
"/ no filter - scanline is as is |
476 |
||
477 |
"Modified: / 03-05-2011 / 12:14:20 / cg" |
|
2699 | 478 |
! |
479 |
||
2909 | 480 |
filterPaeth:count |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
481 |
"Select one of (the pixel to the left, the pixel above and the pixel to above left) to |
2909 | 482 |
predict the value of this pixel" |
483 |
||
484 |
|delta| |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
485 |
|
2909 | 486 |
delta := self bitsPerPixel // 8 max:1. |
487 |
1 to:delta do:[:i | |
|
488 |
thisScanline |
|
489 |
at:i |
|
490 |
put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255) |
|
491 |
]. |
|
492 |
delta + 1 to:count do:[:i | |
|
493 |
thisScanline |
|
494 |
at:i |
|
495 |
put:(((thisScanline at:i) + (self |
|
496 |
paethPredictLeft:(thisScanline at:i - delta) |
|
497 |
above:(prevScanline at:i) |
|
498 |
aboveLeft:(prevScanline at:i - delta))) |
|
499 |
bitAnd:255) |
|
500 |
] |
|
501 |
||
502 |
"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
|
503 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
504 |
|
2696 | 505 |
filterScanline:filterType count:count |
506 |
self |
|
2909 | 507 |
perform:(#(filterNone: filterHorizontal: filterVertical: filterAverage: filterPaeth:) at:filterType+1) |
2696 | 508 |
with:count |
2909 | 509 |
|
510 |
"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
|
511 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
512 |
|
2909 | 513 |
filterVertical:count |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
514 |
"Use the pixel above as a predictor" |
2909 | 515 |
|
516 |
1 to:count do:[:i | |
|
517 |
thisScanline |
|
518 |
at:i |
|
519 |
put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255) |
|
520 |
] |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
521 |
|
2909 | 522 |
"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
|
523 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
524 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
525 |
paethPredictLeft: a above: b aboveLeft: c |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
526 |
"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
|
527 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
528 |
| pa pb pc | |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
529 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
530 |
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
|
531 |
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
|
532 |
pc := a + b - c - c. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
533 |
pc < 0 ifTrue: [ |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
534 |
pc := pc * -1 |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
535 |
]. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
536 |
((pa <= pb) and: [pa <= pc]) ifTrue: [^ a]. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
537 |
(pb <= pc) ifTrue: [^ b]. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
538 |
^ c |
2696 | 539 |
! ! |
540 |
||
2699 | 541 |
!PNGReader methodsFor:'private-pixel copy'! |
542 |
||
2909 | 543 |
copyPixels:y at:startX by:incX |
2704 | 544 |
"Handle interlaced pixels of supported colorTypes" |
2909 | 545 |
|
546 |
|s| |
|
2699 | 547 |
|
2909 | 548 |
s := #( #copyPixelsGray:at:by: |
549 |
nil |
|
550 |
#copyPixelsRGB:at:by: |
|
551 |
#copyPixelsIndexed:at:by: |
|
552 |
#copyPixelsGrayAlpha:at:by: |
|
553 |
nil |
|
554 |
#copyPixelsRGBA:at:by: |
|
555 |
) at:colorType + 1. |
|
3437 | 556 |
self perform:s with:y with:startX with:incX |
1848 | 557 |
|
2909 | 558 |
"Modified: / 03-05-2011 / 12:02:45 / cg" |
3327 | 559 |
! |
560 |
||
561 |
copyPixelsRGBA:y at:startX by:incX |
|
562 |
"Handle interlaced pixels of supported colorTypes. |
|
563 |
Untested code - please verify" |
|
564 |
||
3437 | 565 |
|srcIndex nPixels dstIndex dstInc| |
3327 | 566 |
|
567 |
srcIndex := 1. |
|
3437 | 568 |
dstIndex := (y * bytesPerScanline) + (startX * 4) + 1. |
569 |
dstInc := incX * 4. |
|
3327 | 570 |
|
571 |
nPixels := thisScanline size. |
|
572 |
[srcIndex < nPixels] whileTrue:[ |
|
3437 | 573 |
data replaceFrom:dstIndex to:dstIndex+3 with:thisScanline startingAt:srcIndex. |
3327 | 574 |
srcIndex := srcIndex + 4. |
3437 | 575 |
dstIndex := dstIndex + dstInc |
3327 | 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. |
3461 | 834 |
samplesPerPixel > 1 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
2703 | 835 |
]. |
836 |
(photometric == #rgb) ifTrue:[ |
|
837 |
colorType := 2. |
|
3461 | 838 |
samplesPerPixel > 3 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
2703 | 839 |
]. |
840 |
(photometric == #palette) ifTrue:[ |
|
841 |
colorType := 3. |
|
3461 | 842 |
samplesPerPixel > 1 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
2703 | 843 |
]. |
3100 | 844 |
colorType isNil ifTrue:[ |
3461 | 845 |
self error:'unhandled photometric' |
3100 | 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 |
|
3461 | 909 |
^ '$Header: /cvs/stx/stx/libview2/PNGReader.st,v 1.37 2015-04-23 21:25:39 stefan Exp $' |
2909 | 910 |
! |
911 |
||
912 |
version_CVS |
|
3461 | 913 |
^ '$Header: /cvs/stx/stx/libview2/PNGReader.st,v 1.37 2015-04-23 21:25:39 stefan Exp $' |
308 | 914 |
! ! |
1671 | 915 |
|
3100 | 916 |
|
308 | 917 |
PNGReader initialize! |