author | Claus Gittinger <cg@exept.de> |
Sun, 29 Jan 2017 02:26:51 +0100 | |
changeset 3853 | 5a78ffcf69de |
parent 3821 | 940aaefb5f77 |
child 3854 | 4afd107bc911 |
child 3889 | b7c261a8614d |
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 |
||
3461 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
308 | 16 |
ImageReader subclass:#PNGReader |
3821 | 17 |
instanceVariableNames:'colorType bitsPerChannel depth compressionMethod filterMethod |
18 |
interlaceMode bytesPerScanline globalDataChunk thisScanline |
|
19 |
prevScanline processTextChunks' |
|
20 |
classVariableNames:'Verbose' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Graphics-Images-Readers' |
|
308 | 23 |
! |
24 |
||
398 | 25 |
!PNGReader class methodsFor:'documentation'! |
308 | 26 |
|
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
30 |
All Rights Reserved |
|
31 |
||
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
||
40 |
||
41 |
! |
|
42 |
||
43 |
documentation |
|
44 |
" |
|
2725 | 45 |
This class will provide methods for loading and saving PNG pictures. |
3599 | 46 |
It is currenty unfinished (some interlaced image formats are unsupported, for example: grayscale+alpha) |
3437 | 47 |
In the meantime, use a pngtoXXX converter for interlaced images, and read XXX. |
308 | 48 |
|
2725 | 49 |
[caveats:] |
50 |
writer ignores any mask (for now). |
|
3599 | 51 |
writer only generates unfiltered non-interlaced data |
2725 | 52 |
|
308 | 53 |
[See also:] |
54 |
Image Form Icon |
|
55 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader |
|
56 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
|
57 |
XBMReader XPMReader XWDReader |
|
58 |
||
59 |
[author:] |
|
60 |
Claus Gittinger |
|
61 |
" |
|
1694 | 62 |
! |
308 | 63 |
|
1694 | 64 |
examples |
65 |
" |
|
66 |
PNGReader fromFile:'/home/cg/AudioExplorer_51_files/use_small.png' |
|
2509 | 67 |
PNGReader fromFile:'\\Exeptn\tmp\images\expeccoScreenshot4020_5526507.png' |
2683 | 68 |
|
69 |
PNGReader fromFile:'C:\Users\cg\Desktop\croquet\cobalt-base-current-build-20090210\cobalt-base-current-build-20090210\content\models\textures\checkerboard.png' |
|
2695 | 70 |
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
|
71 |
PNGReader fromFile:'\\exeptn\unsaved\pd_stuff\PNGs\Delete.png' |
2699 | 72 |
PNGReader fromFile:'\\exeptn\unsaved\pd_stuff\PNGs\Down.png' |
2703 | 73 |
|
74 |
||
75 |
|img img2 outStream png| |
|
2725 | 76 |
|
2703 | 77 |
img := ToolbarIconLibrary error32x32Icon. |
78 |
outStream := WriteStream on:(ByteArray new:100). |
|
79 |
PNGReader save:img onStream:outStream. |
|
80 |
png := outStream contents. |
|
81 |
img2 := PNGReader fromStream:(png readStream). |
|
2708 | 82 |
self assert:(img bits = img2 bits). |
83 |
img inspect. |
|
2703 | 84 |
img2 inspect. |
1694 | 85 |
" |
308 | 86 |
! ! |
87 |
||
398 | 88 |
!PNGReader class methodsFor:'initialization'! |
308 | 89 |
|
90 |
initialize |
|
2699 | 91 |
"install myself in the Image classes fileFormat table for the `.png' extension." |
308 | 92 |
|
2709 | 93 |
MIMETypes defineImageType:'image/png' suffix:'png' reader:self. |
94 |
"/ backward compatibility from times when png was not yet so common... |
|
95 |
MIMETypes defineImageType:'image/x-png' suffix:nil reader:self. |
|
308 | 96 |
! ! |
97 |
||
2705 | 98 |
!PNGReader class methodsFor:'queries'! |
99 |
||
100 |
canRepresent:anImage |
|
101 |
"return true, if anImage can be represented in my file format. |
|
102 |
Any image is supported." |
|
103 |
||
104 |
^ true |
|
105 |
! ! |
|
106 |
||
398 | 107 |
!PNGReader class methodsFor:'testing'! |
308 | 108 |
|
109 |
isValidImageFile:aFileName |
|
110 |
"return true, if aFileName contains a PNG image" |
|
111 |
||
112 |
|inStream magic| |
|
113 |
||
114 |
inStream := self streamReadingFile:aFileName. |
|
115 |
inStream isNil ifTrue:[^ false]. |
|
116 |
||
117 |
inStream binary. |
|
118 |
||
119 |
magic := ByteArray new:8. |
|
120 |
inStream nextBytes:8 into:magic. |
|
121 |
inStream close. |
|
122 |
||
123 |
^ (magic = #[137 80 78 71 13 10 26 10]) |
|
124 |
||
1694 | 125 |
" |
126 |
self isValidImageFile:'/home/cg/AudioExplorer_51_files/use_small.png' |
|
2683 | 127 |
self isValidImageFile:'C:\Users\cg\Desktop\croquet\cobalt-base-current-build-20090210\cobalt-base-current-build-20090210\content\models\textures\checkerboard.png' |
2695 | 128 |
self isValidImageFile:'C:\Dokumente und Einstellungen\cg\Desktop\misc\PNGs\Delete.png' |
1694 | 129 |
" |
130 |
||
308 | 131 |
"Modified: 21.6.1996 / 20:38:46 / cg" |
132 |
! ! |
|
133 |
||
2696 | 134 |
!PNGReader methodsFor:'private-chunks'! |
308 | 135 |
|
2699 | 136 |
doPass: pass |
137 |
"Certain interlace passes are skipped with certain small image dimensions" |
|
138 |
||
139 |
pass = 1 ifTrue: [ ^ true ]. |
|
140 |
((width = 1) and: [height = 1]) ifTrue: [ ^ false ]. |
|
141 |
pass = 2 ifTrue: [ ^ width >= 5 ]. |
|
142 |
pass = 3 ifTrue: [ ^ height >= 5 ]. |
|
143 |
pass = 4 ifTrue: [ ^ (width >=3 ) or: [height >= 5] ]. |
|
144 |
pass = 5 ifTrue: [ ^ height >=3 ]. |
|
145 |
pass = 6 ifTrue: [ ^ width >=2 ]. |
|
146 |
pass = 7 ifTrue: [ ^ height >=2 ]. |
|
147 |
||
148 |
self error:'invalid argument'. |
|
149 |
^ true |
|
150 |
! |
|
151 |
||
308 | 152 |
processBKGDChunkLen:len |
153 |
inStream skip:len. |
|
154 |
^ true |
|
155 |
||
156 |
"Created: 21.6.1996 / 21:15:49 / cg" |
|
157 |
! |
|
158 |
||
2707 | 159 |
processCHRMChunkLen:len |
160 |
"cHRM Primary chromaticities chunk - currently unhandled" |
|
161 |
||
162 |
|whitePointX whitePointY redX redY greenX greenY blueX blueY| |
|
163 |
||
3554 | 164 |
whitePointX := (inStream nextInt32MSB:true) / 100000.0. |
165 |
whitePointY := (inStream nextInt32MSB:true) / 100000.0. |
|
166 |
redX := (inStream nextInt32MSB:true) / 100000.0. |
|
167 |
redY := (inStream nextInt32MSB:true) / 100000.0. |
|
168 |
greenX := (inStream nextInt32MSB:true) / 100000.0. |
|
169 |
greenY := (inStream nextInt32MSB:true) / 100000.0. |
|
170 |
blueX := (inStream nextInt32MSB:true) / 100000.0. |
|
171 |
blueY := (inStream nextInt32MSB:true) / 100000.0. |
|
2707 | 172 |
|
173 |
Verbose == true ifTrue:[ |
|
174 |
'PNGReader: CHRM chunk ignored:' infoPrintCR. |
|
175 |
'PNGReader: whitePointX:' infoPrint. whitePointX infoPrintCR. |
|
176 |
'PNGReader: whitePointY:' infoPrint. whitePointX infoPrintCR. |
|
177 |
'PNGReader: redX:' infoPrint. redX infoPrintCR. |
|
178 |
'PNGReader: redY:' infoPrint. redY infoPrintCR. |
|
179 |
'PNGReader: greenX:' infoPrint. greenX infoPrintCR. |
|
180 |
'PNGReader: greenY:' infoPrint. greenY infoPrintCR. |
|
181 |
'PNGReader: blueX:' infoPrint. blueX infoPrintCR. |
|
182 |
'PNGReader: blueY:' infoPrint. blueY infoPrintCR. |
|
183 |
]. |
|
184 |
^ true |
|
185 |
! |
|
186 |
||
308 | 187 |
processChunk:type len:len |
2509 | 188 |
|chunk| |
189 |
||
190 |
type = 'IDAT' ifTrue:[ |
|
191 |
"---since the compressed data can span multiple |
|
192 |
chunks, stitch them all together first. later, |
|
193 |
if memory is an issue, we need to figure out how |
|
194 |
to do this on the fly---" |
|
195 |
chunk := inStream next:len. |
|
3821 | 196 |
globalDataChunk isNil ifTrue:[ |
197 |
globalDataChunk := ReadWriteStream on:(ByteArray new:(height * self bytesPerRow)). |
|
198 |
]. |
|
199 |
globalDataChunk nextPutAll:chunk. |
|
2509 | 200 |
^ true |
201 |
]. |
|
202 |
||
308 | 203 |
type = 'gAMA' ifTrue:[^ self processGAMAChunkLen:len]. |
204 |
type = 'sBIT' ifTrue:[^ self processSBITChunkLen:len]. |
|
205 |
type = 'tEXt' ifTrue:[^ self processTEXTChunkLen:len]. |
|
206 |
type = 'tIME' ifTrue:[^ self processTIMEChunkLen:len]. |
|
207 |
type = 'bKGD' ifTrue:[^ self processBKGDChunkLen:len]. |
|
208 |
type = 'zTXt' ifTrue:[^ self processZTXTChunkLen:len]. |
|
209 |
type = 'PLTE' ifTrue:[^ self processPLTEChunkLen:len]. |
|
210 |
||
1671 | 211 |
type = 'pHYs' ifTrue:[^ self processPHYSChunkLen:len]. |
2707 | 212 |
type = 'cHRM' ifTrue:[^ self processCHRMChunkLen:len]. |
1671 | 213 |
type = 'tRNS' ifTrue:[^ self processTRNSChunkLen:len]. |
214 |
type = 'IHDR' ifTrue:[^ self processIHDRChunkLen:len]. |
|
215 |
||
3762 | 216 |
type = 'iTXt' ifTrue:[^ self processITXTChunkLen:len]. |
217 |
type = 'iCCP' ifTrue:[^ self processICCPChunkLen:len]. |
|
218 |
||
308 | 219 |
('PNGReader: unrecognized chunk: ' , type , ' ignored.') infoPrintCR. |
220 |
||
221 |
inStream skip:len. |
|
222 |
^ true. |
|
223 |
||
224 |
"Created: 21.6.1996 / 21:10:37 / cg" |
|
225 |
"Modified: 21.6.1996 / 21:22:37 / cg" |
|
226 |
! |
|
227 |
||
228 |
processGAMAChunkLen:len |
|
229 |
inStream skip:len. |
|
230 |
^ true |
|
231 |
||
232 |
"Created: 21.6.1996 / 21:10:52 / cg" |
|
233 |
! |
|
234 |
||
2509 | 235 |
processGlobalIDATChunk |
236 |
interlaceMode == 0 ifTrue: [ |
|
237 |
^ self processNonInterlacedGlobalDATA |
|
238 |
]. |
|
239 |
^ self processInterlacedGlobalDATA |
|
240 |
! |
|
241 |
||
3762 | 242 |
processICCPChunkLen:len |
243 |
"/ ignored |
|
244 |
"/ inStream nextBytes:len. |
|
245 |
||
246 |
inStream skip:len. |
|
247 |
^ true |
|
248 |
||
249 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
250 |
! |
|
251 |
||
308 | 252 |
processIDATChunkLen:len |
1671 | 253 |
interlaceMode == 0 ifTrue: [ |
254 |
^ self processNonInterlacedDATA:len |
|
255 |
]. |
|
256 |
^ self processInterlacedDATA:len |
|
257 |
! |
|
258 |
||
3762 | 259 |
processIHDRChunkLen:len |
260 |
"/ ignored |
|
261 |
||
262 |
inStream skip:len. |
|
263 |
^ true |
|
264 |
||
265 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
266 |
! |
|
267 |
||
268 |
processITXTChunkLen:len |
|
269 |
"/ international (i.e.utf8) textual data. |
|
270 |
||
271 |
"/ currently ignored - not needed to display png images |
|
272 |
"/ |
|
273 |
"/ Keyword: 1-79 bytes (character string) |
|
274 |
"/ Null separator: 1 byte |
|
275 |
"/ Compression flag: 1 byte |
|
276 |
"/ Compression method: 1 byte |
|
277 |
"/ Language tag: 0 or more bytes (character string) |
|
278 |
"/ Null separator: 1 byte |
|
279 |
"/ Translated keyword: 0 or more bytes |
|
280 |
"/ Null separator: 1 byte |
|
281 |
"/ Text: 0 or more bytes |
|
282 |
||
283 |
"/ if needed, set processTextChunks to true somewhere... |
|
284 |
processTextChunks == true ifTrue:[ |
|
285 |
|chunkData keyword text i1 i2 i3 |
|
286 |
compressionFlag compressionMethod languageTag |
|
287 |
xlatedKeyword textBytes| |
|
288 |
||
289 |
chunkData := inStream nextBytes:len. |
|
290 |
i1 := chunkData indexOf:0. |
|
291 |
keyword := (chunkData copyTo:i1-1) asString. |
|
292 |
compressionFlag := chunkData at:i1+1. |
|
293 |
compressionMethod := chunkData at:i1+2. |
|
294 |
i2 := chunkData indexOf:0 startingAt:i1+3. |
|
295 |
languageTag := (chunkData copyFrom:i1+3 to:i2-1) asString. |
|
296 |
i3 := chunkData indexOf:0 startingAt:i2+1. |
|
297 |
xlatedKeyword := (chunkData copyFrom:i2+1 to:i3-1) asString. |
|
298 |
textBytes := chunkData copyFrom:i3+1. |
|
299 |
compressionFlag == 0 ifTrue:[ |
|
300 |
text := textBytes |
|
301 |
] ifFalse:[ |
|
302 |
compressionFlag == 1 ifTrue:[ |
|
303 |
"/ for now, only zlib compression is supported... |
|
304 |
'zlib compression currently unsupported' infoPrintCR. |
|
305 |
] ifFalse:[ |
|
306 |
'unsupported compression method' infoPrintCR. |
|
307 |
]. |
|
308 |
]. |
|
309 |
text := text utf8Decoded. |
|
310 |
self handleText:text keyword:keyword. |
|
311 |
] ifFalse:[ |
|
312 |
inStream skip:len. |
|
313 |
]. |
|
314 |
^ true |
|
315 |
||
316 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
317 |
! |
|
318 |
||
1671 | 319 |
processInterlacedDATA:len |
308 | 320 |
inStream skip:len. |
321 |
^ true |
|
1671 | 322 |
! |
308 | 323 |
|
2699 | 324 |
processInterlacedGlobalDATA |
3327 | 325 |
"adam7 interlace method" |
326 |
||
3612 | 327 |
| zlibReader filter |
328 |
bytesPerPass startingCol colIncrement rowIncrement |
|
329 |
startingRow |
|
330 |
temp "filtersSeen" |
|
331 |
cx "{ Class: SmallInteger }" |
|
332 |
sc "{ Class: SmallInteger }" |
|
333 |
cy "{ Class: SmallInteger }" |
|
334 |
startRow "{ Class: SmallInteger }" |
|
335 |
w "{ Class: SmallInteger }" |
|
336 |
h "{ Class: SmallInteger }" |
|
337 |
| |
|
2699 | 338 |
|
3327 | 339 |
interlaceMode == 1 ifFalse: [ |
340 |
self error:'unsupported interlaced mode'. |
|
341 |
^ self |
|
342 |
]. |
|
2699 | 343 |
|
344 |
"/ filtersSeen := Set new. |
|
345 |
||
346 |
startingCol := #(0 4 0 2 0 1 0 ). |
|
3327 | 347 |
startingRow := #(0 0 4 0 2 0 1 ). |
2699 | 348 |
colIncrement := #(8 8 4 4 2 2 1 ). |
349 |
rowIncrement := #(8 8 8 4 4 2 2 ). |
|
350 |
||
3821 | 351 |
globalDataChunk reset. |
352 |
zlibReader := ZipStream readOpenAsZipStreamOn:globalDataChunk suppressHeaderAndChecksum:false. |
|
2699 | 353 |
zlibReader binary. |
354 |
||
3612 | 355 |
h := height. |
356 |
w := width. |
|
2699 | 357 |
1 to: 7 do: [:pass | |
358 |
(self doPass: pass) ifTrue:[ |
|
359 |
cx := colIncrement at: pass. |
|
360 |
sc := startingCol at: pass. |
|
3327 | 361 |
cy := rowIncrement at: pass. |
3612 | 362 |
bytesPerPass := (((w - sc + cx - 1) // cx * depth) + 7) // 8. |
2699 | 363 |
prevScanline := ByteArray new: bytesPerPass. |
364 |
thisScanline := ByteArray new: bytesPerScanline. |
|
3612 | 365 |
startRow := startingRow at: pass. |
366 |
startRow to: h-1 by: cy do: [:y | |
|
3343 | 367 |
filter := zlibReader nextByte. |
2699 | 368 |
"/ filtersSeen add: filter. |
369 |
(filter isNil or: [(filter between: 0 and: 4) not]) |
|
370 |
ifTrue: [^ self]. |
|
371 |
zlibReader next: bytesPerPass into: thisScanline startingAt: 1. |
|
372 |
filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerPass ]. |
|
373 |
self copyPixels:y at:sc by:cx. |
|
374 |
temp := prevScanline. |
|
375 |
prevScanline := thisScanline. |
|
376 |
thisScanline := temp. |
|
377 |
] |
|
378 |
] |
|
379 |
]. |
|
380 |
zlibReader atEnd ifFalse:[self error:'Unexpected data']. |
|
2909 | 381 |
|
382 |
"Modified: / 03-05-2011 / 12:03:33 / cg" |
|
2699 | 383 |
! |
384 |
||
1671 | 385 |
processNonInterlacedDATA:len |
2701 | 386 |
self halt:'not used' |
387 |
"/ |
|
388 |
"/ | zlibReader filter temp prevScanline thisScanline bytesPerScanline filtersSeen| |
|
389 |
"/ |
|
390 |
"/ zlibReader := ZipStream readOpenAsZipStreamOn:inStream suppressHeaderAndChecksum:false. |
|
391 |
"/ "/ zlibReader := ZLibReadStream on:inStream from: 1 to:len. |
|
392 |
"/ |
|
393 |
"/ prevScanline := ByteArray new: self bytesPerRow. |
|
394 |
"/ thisScanline := ByteArray new: self bytesPerRow. |
|
395 |
"/ 0 to: height - 1 do:[:index | |
|
396 |
"/ filter := (zlibReader next: 1) first. |
|
397 |
"/ filtersSeen add: filter. |
|
398 |
"/ (filter isNil or: [(filter between: 0 and: 4) not]) ifTrue: [^self]. |
|
399 |
"/ thisScanline := zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
|
400 |
"/ self filterScanline: filter count: bytesPerScanline. |
|
401 |
"/ self copyPixels: index. |
|
402 |
"/ temp := prevScanline. |
|
403 |
"/ prevScanline := thisScanline. |
|
404 |
"/ thisScanline := temp |
|
405 |
"/ ] |
|
2509 | 406 |
! |
407 |
||
408 |
processNonInterlacedGlobalDATA |
|
3821 | 409 |
| "data n" zlibReader filter temp "filtersSeen" i| |
2696 | 410 |
|
411 |
"/ filtersSeen := Set new. |
|
2695 | 412 |
|
413 |
"/ data := ByteArray new:(self bytesPerRow * height)+1000. |
|
414 |
"/ n := ZipStream uncompress: globalDataChunk into: data. |
|
415 |
"/ self halt. |
|
2509 | 416 |
|
3821 | 417 |
globalDataChunk reset. |
418 |
zlibReader := ZipStream readOpenAsZipStreamOn:globalDataChunk suppressHeaderAndChecksum:false. |
|
2696 | 419 |
zlibReader binary. |
2509 | 420 |
|
1671 | 421 |
prevScanline := ByteArray new: bytesPerScanline. |
422 |
thisScanline := ByteArray new: bytesPerScanline. |
|
2725 | 423 |
0 to: height - 1 do:[:y | |
3343 | 424 |
filter := zlibReader nextByte. |
2696 | 425 |
"/ filtersSeen add: filter. |
2725 | 426 |
(filter notNil and: [filter between: 0 and: 4]) ifFalse: [ |
3673 | 427 |
'PNGReader: unsupported filter: ' infoPrint. filter infoPrintCR. |
2725 | 428 |
^ self |
429 |
]. |
|
2696 | 430 |
zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
431 |
filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerScanline ]. |
|
2725 | 432 |
|
433 |
i := y * bytesPerScanline. |
|
434 |
data replaceFrom:i+1 to:(i+bytesPerScanline) with:thisScanline startingAt:1. |
|
435 |
||
2695 | 436 |
temp := prevScanline. |
437 |
prevScanline := thisScanline. |
|
438 |
thisScanline := temp |
|
439 |
] |
|
1671 | 440 |
! |
441 |
||
442 |
processPHYSChunkLen:len |
|
443 |
"physical pixel chunk - currently unhandled" |
|
444 |
||
2705 | 445 |
|pixelPerUnitX pixelPerUnitY unit| |
446 |
||
3554 | 447 |
pixelPerUnitX := inStream nextInt32MSB:true. |
448 |
pixelPerUnitY := inStream nextInt32MSB:true. |
|
2705 | 449 |
unit := inStream nextByte. |
450 |
||
2707 | 451 |
Verbose == true ifTrue:[ |
452 |
'PNGReader: PHYS chunk ignored:' infoPrintCR. |
|
453 |
'PNGReader: ppuX:' infoPrint. pixelPerUnitX infoPrintCR. |
|
454 |
'PNGReader: ppuY:' infoPrint. pixelPerUnitY infoPrintCR. |
|
455 |
'PNGReader: unit:' infoPrint. unit infoPrintCR. |
|
456 |
]. |
|
457 |
||
2705 | 458 |
^ true |
308 | 459 |
! |
460 |
||
461 |
processPLTEChunkLen:len |
|
1671 | 462 |
"read a color palette" |
463 |
||
2725 | 464 |
|n "{ Class: SmallInteger }" |
465 |
redBytes greenBytes blueBytes| |
|
308 | 466 |
|
467 |
(len \\ 3) ~~ 0 ifTrue:[ |
|
468 |
'PNGReader: invalid size of PLTE chunk' infoPrintCR. |
|
469 |
^ false |
|
470 |
]. |
|
471 |
n := len // 3. |
|
472 |
redBytes := ByteArray new:n. |
|
473 |
greenBytes := ByteArray new:n. |
|
474 |
blueBytes := ByteArray new:n. |
|
475 |
||
476 |
1 to:n do:[:i | |
|
477 |
redBytes at:i put:(inStream nextByte). |
|
478 |
greenBytes at:i put:(inStream nextByte). |
|
479 |
blueBytes at:i put:(inStream nextByte) |
|
480 |
]. |
|
481 |
||
2725 | 482 |
colorMap := MappedPalette |
483 |
redVector:redBytes |
|
484 |
greenVector:greenBytes |
|
485 |
blueVector:blueBytes. |
|
486 |
||
308 | 487 |
^ true |
488 |
||
489 |
"Created: 21.6.1996 / 21:22:28 / cg" |
|
490 |
"Modified: 21.6.1996 / 21:43:01 / cg" |
|
491 |
! |
|
492 |
||
493 |
processSBITChunkLen:len |
|
494 |
inStream skip:len. |
|
495 |
^ true |
|
496 |
||
497 |
"Created: 21.6.1996 / 21:13:09 / cg" |
|
498 |
! |
|
499 |
||
3762 | 500 |
processTEXTChunkLen:len |
501 |
"/ textual data in iso8859 coding. |
|
502 |
||
503 |
"/ currently ignored - not needed to display png images |
|
504 |
"/ Keyword: 1-79 bytes (character string) |
|
505 |
"/ Null separator: 1 byte |
|
506 |
"/ Text: n bytes (character string) |
|
507 |
||
508 |
"/ if needed, set processTextChunks to true somewhere... |
|
509 |
processTextChunks == true ifTrue:[ |
|
510 |
|chunkData keyword text i| |
|
511 |
||
512 |
chunkData := inStream nextBytes:len. |
|
513 |
i := chunkData indexOf:0. |
|
514 |
keyword := (chunkData copyTo:i-1) asString. |
|
515 |
text := (chunkData copyFrom:i+1) asString. |
|
516 |
self handleText:text keyword:keyword. |
|
517 |
] ifFalse:[ |
|
518 |
inStream skip:len. |
|
519 |
]. |
|
308 | 520 |
^ true |
521 |
||
522 |
"Modified: 21.6.1996 / 21:15:27 / cg" |
|
523 |
! |
|
524 |
||
525 |
processTIMEChunkLen:len |
|
526 |
inStream skip:len. |
|
527 |
^ true |
|
528 |
||
529 |
"Created: 21.6.1996 / 21:15:43 / cg" |
|
530 |
"Modified: 21.6.1996 / 21:20:42 / cg" |
|
531 |
! |
|
532 |
||
2707 | 533 |
processTRNSChunkLen:len |
534 |
inStream skip:len. |
|
535 |
^ true |
|
536 |
! |
|
537 |
||
308 | 538 |
processZTXTChunkLen:len |
3762 | 539 |
"/ compressed text |
540 |
||
541 |
"/ currently ignored - not needed to display png images |
|
308 | 542 |
inStream skip:len. |
543 |
^ true |
|
544 |
||
545 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
546 |
! ! |
|
547 |
||
2696 | 548 |
!PNGReader methodsFor:'private-filtering'! |
549 |
||
2909 | 550 |
filterAverage:count |
2699 | 551 |
"Use the average of the pixel to the left and the pixel above as a predictor" |
2909 | 552 |
|
553 |
|delta| |
|
2699 | 554 |
|
3609 | 555 |
%{ |
556 |
if (__isByteArray(__INST(thisScanline)) |
|
557 |
&& __isByteArray(__INST(prevScanline)) |
|
558 |
&& __isSmallInteger(__INST(depth)) |
|
559 |
&& __isSmallInteger(count)) { |
|
560 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
561 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
562 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
563 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
564 |
INT __count = __intVal(count); |
|
565 |
INT __delta = __intVal(__INST(depth)) / 8; |
|
566 |
int __i; |
|
567 |
||
568 |
if (__delta < 1) __delta = 1; |
|
569 |
for (__i=0; __i<__delta; __i++) { |
|
570 |
__thisScanline[__i] += (__prevScanline[__i] >> 1); |
|
571 |
} |
|
572 |
for (; __i < __count; __i++) { |
|
573 |
__thisScanline[__i] += ((__prevScanline[__i] + __thisScanline[__i-__delta]) >> 1); |
|
574 |
} |
|
575 |
RETURN(self); |
|
576 |
} |
|
577 |
%}. |
|
578 |
||
579 |
delta := depth // 8 max:1. |
|
2909 | 580 |
1 to:delta do:[:i | |
3384 | 581 |
thisScanline at:i put:((thisScanline at:i) + ((prevScanline at:i) // 2) bitAnd:255) |
2909 | 582 |
]. |
583 |
delta + 1 to:count do:[:i | |
|
3384 | 584 |
thisScanline at:i put:((thisScanline at:i) + (((prevScanline at:i) + (thisScanline at:i - delta)) // 2) bitAnd:255) |
2909 | 585 |
] |
586 |
||
587 |
"Modified: / 03-05-2011 / 12:14:01 / cg" |
|
2699 | 588 |
! |
589 |
||
2696 | 590 |
filterHorizontal:count |
591 |
"use the pixel to the left as a predictor" |
|
592 |
||
593 |
|delta| |
|
594 |
||
3608 | 595 |
%{ |
596 |
if (__isByteArray(__INST(thisScanline)) |
|
597 |
&& __isByteArray(__INST(prevScanline)) |
|
598 |
&& __isSmallInteger(__INST(depth)) |
|
599 |
&& __isSmallInteger(count)) { |
|
600 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
601 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
602 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
603 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
604 |
INT __count = __intVal(count); |
|
605 |
INT __delta = __intVal(__INST(depth)) / 8; |
|
606 |
int __i; |
|
607 |
||
608 |
if (__delta < 1) __delta = 1; |
|
609 |
for (__i = __delta; __i < __count; __i++) { |
|
610 |
__thisScanline[__i] = __thisScanline[__i] + __thisScanline[__i-__delta]; |
|
611 |
} |
|
612 |
RETURN(self); |
|
613 |
} |
|
614 |
%}. |
|
615 |
||
616 |
delta := depth // 8 max:1. |
|
2696 | 617 |
delta+1 to:count do:[:i| |
618 |
thisScanline at:i put:(((thisScanline at:i)+ (thisScanline at:i-delta)) bitAnd:255) |
|
619 |
] |
|
620 |
! |
|
621 |
||
2699 | 622 |
filterNone: count |
2909 | 623 |
"/ no filter - scanline is as is |
624 |
||
625 |
"Modified: / 03-05-2011 / 12:14:20 / cg" |
|
2699 | 626 |
! |
627 |
||
2909 | 628 |
filterPaeth:count |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
629 |
"Select one of (the pixel to the left, the pixel above and the pixel to above left) to |
2909 | 630 |
predict the value of this pixel" |
631 |
||
632 |
|delta| |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
633 |
|
3607 | 634 |
%{ |
635 |
if (__isByteArray(__INST(thisScanline)) |
|
636 |
&& __isByteArray(__INST(prevScanline)) |
|
637 |
&& __isSmallInteger(__INST(depth)) |
|
638 |
&& __isSmallInteger(count)) { |
|
639 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
640 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
641 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
642 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
643 |
INT __count = __intVal(count); |
|
644 |
INT __delta = __intVal(__INST(depth)) / 8; |
|
645 |
int __i; |
|
646 |
||
647 |
if (__delta < 1) __delta = 1; |
|
648 |
for (__i=0; __i<__delta; __i++) { |
|
649 |
unsigned int __pix = __thisScanline[__i] + __prevScanline[__i]; |
|
650 |
__thisScanline[__i] = __pix; |
|
651 |
} |
|
652 |
for (; __i < __count; __i++) { |
|
653 |
int __pCenter = __thisScanline[__i]; |
|
654 |
int __pLeft = __thisScanline[__i - __delta]; |
|
655 |
int __pAbove = __prevScanline[__i]; |
|
656 |
int __pAboveLeft = __prevScanline[__i - __delta]; |
|
657 |
int __pa, __pb, __pc; |
|
658 |
int __p; |
|
659 |
||
660 |
__pa = (__pAbove - __pAboveLeft); |
|
661 |
__pa = __pa < 0 ? -__pa : __pa; |
|
662 |
__pb = (__pLeft - __pAboveLeft); |
|
663 |
__pb = __pb < 0 ? -__pb : __pb; |
|
664 |
__pc = __pLeft + __pAbove - __pAboveLeft - __pAboveLeft; |
|
665 |
__pc = __pc < 0 ? -__pc : __pc; |
|
666 |
if ((__pa <= __pb) && (__pa <= __pc)) __p = __pLeft; |
|
667 |
else if (__pb <= __pc) __p = __pAbove; |
|
668 |
else __p = __pAboveLeft; |
|
669 |
||
670 |
__thisScanline[__i] = __pCenter + __p; |
|
671 |
} |
|
672 |
RETURN(self); |
|
673 |
} |
|
674 |
%}. |
|
675 |
||
676 |
delta := depth // 8 max:1. |
|
2909 | 677 |
1 to:delta do:[:i | |
678 |
thisScanline |
|
679 |
at:i |
|
680 |
put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255) |
|
681 |
]. |
|
682 |
delta + 1 to:count do:[:i | |
|
683 |
thisScanline |
|
684 |
at:i |
|
685 |
put:(((thisScanline at:i) + (self |
|
686 |
paethPredictLeft:(thisScanline at:i - delta) |
|
687 |
above:(prevScanline at:i) |
|
688 |
aboveLeft:(prevScanline at:i - delta))) |
|
689 |
bitAnd:255) |
|
690 |
] |
|
691 |
||
692 |
"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
|
693 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
694 |
|
2696 | 695 |
filterScanline:filterType count:count |
3612 | 696 |
filterType == 0 ifTrue:[^ self]. |
697 |
||
2696 | 698 |
self |
3612 | 699 |
perform:(#(filterHorizontal: filterVertical: filterAverage: filterPaeth:) at:filterType) |
2696 | 700 |
with:count |
2909 | 701 |
|
702 |
"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
|
703 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
704 |
|
2909 | 705 |
filterVertical:count |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
706 |
"Use the pixel above as a predictor" |
2909 | 707 |
|
3608 | 708 |
%{ |
709 |
if (__isByteArray(__INST(thisScanline)) |
|
710 |
&& __isByteArray(__INST(prevScanline)) |
|
711 |
&& __isSmallInteger(count)) { |
|
712 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
713 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
714 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
715 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
716 |
INT __count = __intVal(count); |
|
717 |
int __i; |
|
718 |
||
719 |
for (__i=0; __i<__count; __i++) { |
|
720 |
unsigned int __pix = __thisScanline[__i] + __prevScanline[__i]; |
|
721 |
__thisScanline[__i] = __pix; |
|
722 |
} |
|
723 |
RETURN(self); |
|
724 |
} |
|
725 |
%}. |
|
726 |
||
2909 | 727 |
1 to:count do:[:i | |
728 |
thisScanline |
|
729 |
at:i |
|
730 |
put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255) |
|
731 |
] |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
732 |
|
2909 | 733 |
"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
|
734 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
735 |
|
3607 | 736 |
paethPredictLeft: l above: a aboveLeft: al |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
737 |
"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
|
738 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
739 |
| pa pb pc | |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
740 |
|
3607 | 741 |
pa := a > al ifTrue: [a - al] ifFalse: [al - a]. |
742 |
pb := l > al ifTrue: [l - al] ifFalse: [al - l]. |
|
743 |
pc := l + a - al - al. |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
744 |
pc < 0 ifTrue: [ |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
745 |
pc := pc * -1 |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
746 |
]. |
3607 | 747 |
((pa <= pb) and: [pa <= pc]) ifTrue: [^ l]. |
748 |
(pb <= pc) ifTrue: [^ a]. |
|
749 |
^ al |
|
2696 | 750 |
! ! |
751 |
||
2699 | 752 |
!PNGReader methodsFor:'private-pixel copy'! |
753 |
||
2909 | 754 |
copyPixels:y at:startX by:incX |
2704 | 755 |
"Handle interlaced pixels of supported colorTypes" |
2909 | 756 |
|
757 |
|s| |
|
2699 | 758 |
|
3598 | 759 |
"/ per color type copy methods |
760 |
s := #( |
|
761 |
#copyPixelsGray:at:by: "/ 0 |
|
2909 | 762 |
nil |
3598 | 763 |
#copyPixelsRGB:at:by: "/ 2 |
764 |
#copyPixelsIndexed:at:by: "/ 3 |
|
765 |
#copyPixelsGrayAlpha:at:by: "/ 4 |
|
2909 | 766 |
nil |
3598 | 767 |
#copyPixelsRGBA:at:by: "/ 6 |
2909 | 768 |
) at:colorType + 1. |
3437 | 769 |
self perform:s with:y with:startX with:incX |
1848 | 770 |
|
2909 | 771 |
"Modified: / 03-05-2011 / 12:02:45 / cg" |
3327 | 772 |
! |
773 |
||
3598 | 774 |
copyPixelsGray:y at:startX by:incX |
775 |
"Handle interlaced pixels of supported colorTypes. |
|
776 |
Untested code - please verify" |
|
777 |
||
3611 | 778 |
|srcIndex "{ Class: SmallInteger }" |
779 |
srcMask "{ Class: SmallInteger }" |
|
780 |
nPixels "{ Class: SmallInteger }" |
|
781 |
dstIndex "{ Class: SmallInteger }" |
|
782 |
dstMask "{ Class: SmallInteger }" |
|
783 |
x "{ Class: SmallInteger }" |
|
784 |
bits "{ Class: SmallInteger }" |
|
785 |
bitMask "{ Class: SmallInteger }" |
|
786 |
rS "{ Class: SmallInteger }" |
|
787 |
lS "{ Class: SmallInteger }" |
|
788 |
rowIndex "{ Class: SmallInteger }"| |
|
3598 | 789 |
|
790 |
nPixels := width // incX. |
|
791 |
srcIndex := 0. srcMask := 0. x := startX. |
|
792 |
||
793 |
depth == 1 ifTrue:[ |
|
794 |
dstIndex := (y * bytesPerScanline) + (startX // 8) + 1. |
|
795 |
dstMask := 16r80 >> (x \\ 8). |
|
796 |
||
797 |
1 to:nPixels do:[:cnt | |
|
798 |
srcMask == 0 ifTrue:[ |
|
799 |
srcMask := 16r80. |
|
800 |
srcIndex := srcIndex + 1. |
|
801 |
bits := thisScanline at:srcIndex. |
|
802 |
]. |
|
803 |
bitMask := (bits bitAnd:srcMask) == 0 ifTrue:[0] ifFalse:[dstMask]. |
|
804 |
bitMask ~~ 0 ifTrue:[ |
|
805 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
806 |
]. |
|
807 |
x := x + incX. |
|
808 |
dstMask := dstMask >> incX. |
|
809 |
dstMask == 0 ifTrue:[ |
|
810 |
dstIndex := dstIndex + 1. |
|
811 |
dstMask := 16r80 >> (x \\ 8). |
|
812 |
]. |
|
813 |
srcMask := srcMask bitShift:-1. |
|
814 |
]. |
|
815 |
^ self. |
|
816 |
]. |
|
817 |
||
818 |
depth == 2 ifTrue:[ |
|
819 |
dstIndex := (y * bytesPerScanline) + (startX // 4) + 1. |
|
820 |
lS := 6 - ((x \\ 4) * 2). |
|
821 |
rS := -1. |
|
822 |
1 to:nPixels do:[:cnt | |
|
823 |
rS < 0 ifTrue:[ |
|
824 |
srcIndex := srcIndex + 1. |
|
825 |
bits := thisScanline at:srcIndex. |
|
826 |
rS := 6. |
|
827 |
]. |
|
828 |
bitMask := ((bits >> rS) bitAnd:2r11). |
|
829 |
bitMask ~~ 0 ifTrue:[ |
|
830 |
bitMask := bitMask << lS. |
|
831 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
832 |
]. |
|
833 |
lS := lS - (incX * 2). |
|
834 |
lS < 0 ifTrue:[ |
|
835 |
lS <= -8 ifTrue:[ |
|
836 |
dstIndex := dstIndex + 2. |
|
837 |
lS := lS + 8 + 8. |
|
838 |
] ifFalse:[ |
|
839 |
dstIndex := dstIndex + 1. |
|
840 |
lS := lS + 8. |
|
841 |
]. |
|
842 |
]. |
|
843 |
rS := rS - 2. |
|
844 |
]. |
|
845 |
^ self. |
|
846 |
]. |
|
847 |
||
848 |
depth == 4 ifTrue:[ |
|
849 |
rowIndex := (y * bytesPerScanline) + 1. |
|
850 |
dstIndex := rowIndex + (x // 2). |
|
851 |
rS := -1. |
|
852 |
1 to:nPixels do:[:cnt | |
|
853 |
rS < 0 ifTrue:[ |
|
854 |
srcIndex := srcIndex + 1. |
|
855 |
bits := thisScanline at:srcIndex. |
|
856 |
rS := 4. |
|
857 |
]. |
|
858 |
bitMask := ((bits >> rS) bitAnd:2r1111). |
|
859 |
x even ifTrue:[ |
|
860 |
bitMask := bitMask bitShift:4. |
|
861 |
]. |
|
862 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
863 |
x := x + incX. |
|
864 |
dstIndex := rowIndex + (x // 2). |
|
865 |
rS := rS - 4. |
|
866 |
]. |
|
867 |
^ self. |
|
868 |
]. |
|
869 |
||
870 |
depth == 8 ifTrue:[ |
|
871 |
srcIndex := 1. |
|
872 |
dstIndex := (y * bytesPerScanline) + (startX) + 1. |
|
873 |
||
874 |
1 to:nPixels do:[:n | |
|
875 |
data at:dstIndex put:(thisScanline at:srcIndex). |
|
876 |
srcIndex := srcIndex + 1. |
|
877 |
dstIndex := dstIndex + incX |
|
878 |
]. |
|
879 |
^ self. |
|
880 |
]. |
|
881 |
||
882 |
depth == 16 ifTrue:[ |
|
883 |
srcIndex := 1. |
|
884 |
dstIndex := (y * bytesPerScanline) + (startX*2) + 1. |
|
885 |
||
886 |
1 to:nPixels do:[:n | |
|
3601 | 887 |
data unsignedInt16At:dstIndex put:(thisScanline unsignedInt16At:srcIndex). |
3598 | 888 |
srcIndex := srcIndex + 2. |
889 |
dstIndex := dstIndex + (incX * 2) |
|
890 |
]. |
|
891 |
^ self. |
|
892 |
]. |
|
893 |
self error:'unsupported depth'. |
|
894 |
! |
|
895 |
||
896 |
copyPixelsIndexed:y at:startX by:incX |
|
897 |
self copyPixelsGray:y at:startX by:incX |
|
898 |
! |
|
899 |
||
900 |
copyPixelsRGB:y at:startX by:incX |
|
901 |
"Handle interlaced pixels of supported colorTypes. |
|
902 |
Untested code - please verify" |
|
903 |
||
3611 | 904 |
|srcIndex "{ Class: SmallInteger }" |
905 |
nPixels "{ Class: SmallInteger }" |
|
906 |
dstIndex "{ Class: SmallInteger }" |
|
907 |
dstInc "{ Class: SmallInteger }" |
|
908 |
bpp "{ Class: SmallInteger }"| |
|
3598 | 909 |
|
910 |
bpp := bytesPerScanline // width. |
|
911 |
||
912 |
"/ 1 byte per r,g,b |
|
913 |
srcIndex := 1. |
|
914 |
dstIndex := (y * bytesPerScanline) + (startX * bpp) + 1. |
|
915 |
dstInc := incX * bpp. |
|
916 |
||
917 |
nPixels := width // incX. |
|
918 |
1 to:nPixels do:[:n | |
|
919 |
data replaceFrom:dstIndex to:dstIndex+bpp-1 with:thisScanline startingAt:srcIndex. |
|
920 |
srcIndex := srcIndex + bpp. |
|
921 |
dstIndex := dstIndex + dstInc |
|
922 |
]. |
|
923 |
! |
|
924 |
||
3327 | 925 |
copyPixelsRGBA:y at:startX by:incX |
926 |
"Handle interlaced pixels of supported colorTypes. |
|
927 |
Untested code - please verify" |
|
928 |
||
3611 | 929 |
|srcIndex "{ Class: SmallInteger }" |
930 |
nPixels "{ Class: SmallInteger }" |
|
931 |
dstIndex "{ Class: SmallInteger }" |
|
932 |
dstInc "{ Class: SmallInteger }"| |
|
3327 | 933 |
|
934 |
srcIndex := 1. |
|
3437 | 935 |
dstIndex := (y * bytesPerScanline) + (startX * 4) + 1. |
936 |
dstInc := incX * 4. |
|
3327 | 937 |
|
938 |
nPixels := thisScanline size. |
|
939 |
[srcIndex < nPixels] whileTrue:[ |
|
3437 | 940 |
data replaceFrom:dstIndex to:dstIndex+3 with:thisScanline startingAt:srcIndex. |
3327 | 941 |
srcIndex := srcIndex + 4. |
3437 | 942 |
dstIndex := dstIndex + dstInc |
3327 | 943 |
]. |
2699 | 944 |
! ! |
945 |
||
946 |
!PNGReader methodsFor:'private-reading'! |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
947 |
|
2683 | 948 |
getChunk |
949 |
|len type crc| |
|
950 |
||
951 |
inStream atEnd ifTrue:[^ false]. |
|
952 |
||
3554 | 953 |
len := inStream nextInt32MSB:true. |
2683 | 954 |
type := String new:4. |
955 |
(inStream nextBytes:4 into:type) ~~ 4 ifTrue:[^ false]. |
|
956 |
||
2707 | 957 |
Verbose == true ifTrue:[ |
958 |
'len: ' infoPrint. len infoPrint. ' type: ' infoPrint. type infoPrintCR. |
|
959 |
]. |
|
2683 | 960 |
|
961 |
type = 'IEND' ifTrue:[^ false]. |
|
962 |
||
963 |
(self processChunk:type len:len) ifFalse:[^ false]. |
|
964 |
||
3554 | 965 |
crc := inStream nextInt32MSB:true. "/ ignored - for now |
2683 | 966 |
^ true |
967 |
||
968 |
"Created: 21.6.1996 / 21:09:36 / cg" |
|
969 |
"Modified: 21.6.1996 / 21:20:26 / cg" |
|
970 |
! |
|
971 |
||
972 |
getIHDRChunk |
|
973 |
|len type crc| |
|
974 |
||
3554 | 975 |
len := inStream nextInt32MSB:true. |
2683 | 976 |
|
977 |
type := String new:4. |
|
978 |
inStream nextBytes:4 into:type. |
|
979 |
||
3183 | 980 |
type = 'IHDR' ifFalse:[self halt:'expected IHDR magic'. ^ false]. |
981 |
len == 13 ifFalse:[self halt:'unexpected IHDR length'. ^ false]. |
|
2683 | 982 |
|
3554 | 983 |
width := inStream nextInt32MSB:true. |
984 |
height := inStream nextInt32MSB:true. |
|
2683 | 985 |
|
986 |
(width <= 0 or:[height <= 0]) ifTrue:[ |
|
987 |
'PNGReader: invalid dimension(s)' infoPrintCR. |
|
988 |
^ false. |
|
989 |
]. |
|
990 |
self reportDimension. |
|
991 |
||
3607 | 992 |
bitsPerChannel := inStream nextByte. |
993 |
depth := bitsPerChannel. "/ will be changed by setColorType. |
|
2683 | 994 |
colorType := inStream nextByte. |
995 |
||
996 |
compressionMethod := inStream nextByte. |
|
997 |
filterMethod := inStream nextByte. |
|
998 |
interlaceMode := inStream nextByte. |
|
999 |
||
2707 | 1000 |
Verbose == true ifTrue:[ |
1001 |
'PNGReader: IHDR:' infoPrintCR. |
|
1002 |
'PNGReader: width: ' infoPrint. width infoPrintCR. |
|
1003 |
'PNGReader: height: ' infoPrint. height infoPrintCR. |
|
3607 | 1004 |
'PNGReader: bitsPerChannel: ' infoPrint. bitsPerChannel infoPrintCR. |
2707 | 1005 |
'PNGReader: colorType: ' infoPrint. colorType infoPrintCR. |
1006 |
'PNGReader: compressionMethod: ' infoPrint. compressionMethod infoPrintCR. |
|
1007 |
'PNGReader: filterMethod: ' infoPrint. filterMethod infoPrintCR. |
|
1008 |
'PNGReader: interlaceMode: ' infoPrint. interlaceMode infoPrintCR. |
|
1009 |
]. |
|
1010 |
||
3554 | 1011 |
crc := inStream nextInt32MSB:true. |
2683 | 1012 |
^ true |
1013 |
||
1014 |
"Modified: 21.6.1996 / 21:38:35 / cg" |
|
1015 |
! |
|
1016 |
||
3762 | 1017 |
handleText:text keyword:keyword |
1018 |
"/ things like exif data etc. |
|
1019 |
"/ self halt. |
|
1020 |
||
1021 |
"/Standard keywords for text chunks: |
|
1022 |
"/ |
|
1023 |
"/ Title Short (one line) title or caption for image |
|
1024 |
"/ Author Name of image's creator |
|
1025 |
"/ Description Description of image (possibly long) |
|
1026 |
"/ Copyright Copyright notice |
|
1027 |
"/ Creation Time Time of original image creation |
|
1028 |
"/ Software Software used to create the image |
|
1029 |
"/ Disclaimer Legal disclaimer |
|
1030 |
"/ Warning Warning of nature of content |
|
1031 |
"/ Source Device used to create the image |
|
1032 |
"/ Comment Miscellaneous comment; conversion from |
|
1033 |
"/ GIF comment |
|
1034 |
! |
|
1035 |
||
1848 | 1036 |
setColorType:colorType |
1037 |
colorType == 0 ifTrue:[ |
|
1038 |
photometric := #blackIs0. |
|
1039 |
samplesPerPixel := 1. |
|
3607 | 1040 |
bitsPerSample := Array with:bitsPerChannel. |
1041 |
depth := bitsPerChannel. |
|
1848 | 1042 |
^ true. |
1043 |
]. |
|
1044 |
||
1045 |
colorType == 2 ifTrue:[ |
|
3607 | 1046 |
bitsPerChannel < 8 ifTrue:[ |
1848 | 1047 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
1048 |
^ false. |
|
1049 |
]. |
|
1050 |
photometric := #rgb. |
|
1051 |
samplesPerPixel := 3. |
|
3607 | 1052 |
bitsPerSample := Array with:bitsPerChannel with:bitsPerChannel with:bitsPerChannel. |
1053 |
depth := bitsPerChannel * 3. |
|
1848 | 1054 |
^ true. |
1055 |
]. |
|
1056 |
||
1057 |
colorType == 3 ifTrue:[ |
|
3607 | 1058 |
bitsPerChannel == 16 ifTrue:[ |
1848 | 1059 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
1060 |
^ false. |
|
1061 |
]. |
|
1062 |
photometric := #palette. |
|
1063 |
samplesPerPixel := 1. |
|
3607 | 1064 |
bitsPerSample := Array with:bitsPerChannel. |
1065 |
depth := bitsPerChannel. |
|
1848 | 1066 |
^ true. |
1067 |
]. |
|
1068 |
||
1069 |
colorType == 4 ifTrue:[ |
|
3607 | 1070 |
bitsPerChannel < 8 ifTrue:[ |
1848 | 1071 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
1072 |
^ false. |
|
1073 |
]. |
|
1074 |
photometric := #blackIs0. |
|
1075 |
samplesPerPixel := 2. |
|
3607 | 1076 |
bitsPerSample := Array with:bitsPerChannel with:bitsPerChannel. |
1077 |
depth := bitsPerChannel * 2. |
|
1848 | 1078 |
^ true. |
1079 |
]. |
|
1080 |
||
1081 |
colorType == 6 ifTrue:[ |
|
3607 | 1082 |
bitsPerChannel < 8 ifTrue:[ |
1848 | 1083 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
1084 |
^ false. |
|
1085 |
]. |
|
2702 | 1086 |
photometric := #rgb. |
1848 | 1087 |
samplesPerPixel := 4. |
3607 | 1088 |
bitsPerSample := Array with:bitsPerChannel with:bitsPerChannel with:bitsPerChannel with:bitsPerChannel. |
1089 |
depth := bitsPerChannel * 4. |
|
1848 | 1090 |
^ true. |
1091 |
]. |
|
1092 |
||
1093 |
('PNGReader: invalid colorType: ' , colorType printString , '.') infoPrintCR. |
|
1094 |
^ false |
|
1095 |
! ! |
|
1096 |
||
1805 | 1097 |
!PNGReader methodsFor:'reading'! |
308 | 1098 |
|
1099 |
fromStream:aStream |
|
1100 |
"read a stream containing a PNG image. |
|
1101 |
Leave image description in instance variables." |
|
1102 |
||
1103 |
|header| |
|
1104 |
||
1105 |
inStream := aStream. |
|
1106 |
aStream binary. |
|
1107 |
||
1671 | 1108 |
"PNG-files are always msb (network-world)" |
308 | 1109 |
byteOrder := #msb. |
1110 |
||
1111 |
header := ByteArray new:8. |
|
1112 |
aStream nextBytes:8 into:header. |
|
1113 |
||
2683 | 1114 |
header ~= (self pngHeader) ifTrue:[ |
308 | 1115 |
'PNGReader: not a png file.' infoPrintCR. |
3598 | 1116 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: not a png file'. |
308 | 1117 |
^ nil |
1118 |
]. |
|
2695 | 1119 |
|
308 | 1120 |
(self getIHDRChunk) ifFalse:[ |
1121 |
'PNGReader: required IHDR chunk missing.' infoPrintCR. |
|
3598 | 1122 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: required IHDR chunk missing'. |
308 | 1123 |
^ nil |
1124 |
]. |
|
1125 |
||
1126 |
compressionMethod ~~ 0 ifTrue:[ |
|
3598 | 1127 |
('PNGReader: compressionMethod %s not supported.' printfWith:compressionMethod printString) infoPrintCR. |
1128 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: unsupported compression method'. |
|
308 | 1129 |
^ nil |
1130 |
]. |
|
1131 |
filterMethod > 0 ifTrue:[ |
|
1132 |
'PNGReader: invalid filterMethod' infoPrintCR. |
|
3598 | 1133 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: invalid/unsupported filterMethod'. |
308 | 1134 |
^ nil. |
1135 |
]. |
|
1136 |
interlaceMode > 1 ifTrue:[ |
|
3598 | 1137 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: invalid/unsupported interlaceMode'. |
308 | 1138 |
'PNGReader: invalid interlaceMode' infoPrintCR. |
1139 |
^ nil. |
|
1140 |
]. |
|
1671 | 1141 |
(self setColorType:colorType) ifFalse:[ |
308 | 1142 |
^ nil |
1671 | 1143 |
]. |
2706 | 1144 |
|
1671 | 1145 |
[self getChunk] whileTrue. |
308 | 1146 |
|
3598 | 1147 |
bytesPerScanline := self bytesPerRow. |
1148 |
data := ByteArray new:(bytesPerScanline * height). |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1149 |
|
2509 | 1150 |
globalDataChunk notNil ifTrue:[ |
3343 | 1151 |
self processGlobalIDATChunk. |
1152 |
globalDataChunk := nil. |
|
2509 | 1153 |
]. |
1154 |
||
308 | 1155 |
photometric == #palette ifTrue:[ |
2725 | 1156 |
colorMap isNil ifTrue:[ |
3598 | 1157 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: palette chunk missing'. |
1158 |
'PNGReader: palette chunk missing.' infoPrintCR. |
|
2725 | 1159 |
"/ ^ nil |
308 | 1160 |
]. |
1161 |
]. |
|
1162 |
||
1163 |
" |
|
1164 |
PNGReader fromFile:'/home/cg/libpng-0.89c/pngtest.png' |
|
1165 |
" |
|
1166 |
||
2910
d5171b9245e6
comment/format in: #fromStream:
Claus Gittinger <cg@exept.de>
parents:
2909
diff
changeset
|
1167 |
"Modified: / 03-05-2011 / 12:17:34 / cg" |
1805 | 1168 |
! ! |
1169 |
||
2683 | 1170 |
!PNGReader methodsFor:'writing to file'! |
1171 |
||
1172 |
pngHeader |
|
1173 |
^ #[137 80 78 71 13 10 26 10] |
|
1174 |
! |
|
1175 |
||
1176 |
save:image onStream:aStream |
|
1177 |
"save image in PNG-file-format onto aStream" |
|
1178 |
||
1179 |
outStream := aStream. |
|
1180 |
outStream binary. |
|
1181 |
||
2703 | 1182 |
byteOrder := #msb. |
2683 | 1183 |
width := image width. |
1184 |
height := image height. |
|
1185 |
photometric := image photometric. |
|
1186 |
samplesPerPixel := image samplesPerPixel. |
|
1187 |
bitsPerSample := image bitsPerSample. |
|
1188 |
colorMap := image colorMap. |
|
1189 |
data := image bits. |
|
1190 |
||
2703 | 1191 |
outStream nextPutAll:(self pngHeader). |
1192 |
self writeIHDRChunk. |
|
1193 |
photometric == #palette ifTrue: [self writePaletteChunk]. |
|
1194 |
self writeImageDataChunk. |
|
2683 | 1195 |
self writeEndChunk |
1196 |
! |
|
1197 |
||
2703 | 1198 |
writeChunk:chunkTypeChars size:len with:aBlock |
2708 | 1199 |
|crc realOutStream chunkBytes| |
1200 |
||
1201 |
realOutStream := outStream. |
|
1202 |
outStream := WriteStream on:(ByteArray new:len). |
|
1203 |
||
2703 | 1204 |
outStream nextPutBytes:4 from:chunkTypeChars startingAt:1. |
1205 |
aBlock value. |
|
2708 | 1206 |
|
1207 |
chunkBytes := outStream contents. |
|
1208 |
crc := CRC32Stream hashValueOf:chunkBytes. |
|
1209 |
||
1210 |
outStream := realOutStream. |
|
1211 |
||
3554 | 1212 |
realOutStream nextPutInt32:len MSB:true. |
2708 | 1213 |
realOutStream nextPutAll:chunkBytes. |
3554 | 1214 |
realOutStream nextPutInt32:crc MSB:true. |
2703 | 1215 |
! |
2683 | 1216 |
|
2703 | 1217 |
writeEndChunk |
1218 |
self |
|
1219 |
writeChunk: 'IEND' |
|
1220 |
size: 0 |
|
1221 |
with:[ ] |
|
2683 | 1222 |
! |
1223 |
||
1224 |
writeFileHeader |
|
1225 |
outStream nextPutAll:(self pngHeader) |
|
1226 |
! |
|
1227 |
||
2703 | 1228 |
writeIHDRChunk |
3100 | 1229 |
((photometric == #whiteIs0) or:[ (photometric == #blackIs0)]) ifTrue:[ |
2703 | 1230 |
colorType := 0. |
3461 | 1231 |
samplesPerPixel > 1 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
2703 | 1232 |
]. |
3724 | 1233 |
((photometric == #rgb) or:[(photometric == #rgba)]) ifTrue:[ |
2703 | 1234 |
colorType := 2. |
3461 | 1235 |
samplesPerPixel > 3 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
2703 | 1236 |
]. |
1237 |
(photometric == #palette) ifTrue:[ |
|
1238 |
colorType := 3. |
|
3461 | 1239 |
samplesPerPixel > 1 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
2703 | 1240 |
]. |
3100 | 1241 |
colorType isNil ifTrue:[ |
3461 | 1242 |
self error:'unhandled photometric' |
3100 | 1243 |
]. |
2703 | 1244 |
|
2683 | 1245 |
self |
1246 |
writeChunk: 'IHDR' |
|
1247 |
size: 13 |
|
1248 |
with:[ |
|
1249 |
self writeLong:width. |
|
1250 |
self writeLong:height. |
|
2703 | 1251 |
outStream nextPut: 8. "Assume that all colors are 24bit" "/ depth |
1252 |
outStream nextPut: colorType. |
|
2683 | 1253 |
outStream nextPut: 0. "Compression" |
1254 |
outStream nextPut: 0. "Filter method" |
|
1255 |
outStream nextPut: 0 "Non-interlaced" |
|
1256 |
] |
|
1257 |
! |
|
1258 |
||
2703 | 1259 |
writeImageDataChunk |
3821 | 1260 |
|compressedByteStream compressedBytes zlibWriter idx| |
2703 | 1261 |
|
1262 |
bytesPerScanline := self bytesPerRow. |
|
3821 | 1263 |
|
1264 |
compressedByteStream := WriteStream on:(ByteArray new:(bytesPerScanline*height)). |
|
1265 |
zlibWriter := ZipStream writeOpenAsZipStreamOn:compressedByteStream suppressHeaderAndChecksum:false. |
|
1266 |
zlibWriter binary. |
|
2703 | 1267 |
|
2708 | 1268 |
idx := 1. |
1269 |
0 to:height-1 do:[:y | |
|
1270 |
|row| |
|
1271 |
||
1272 |
row := data copyFrom:idx to:(idx+bytesPerScanline-1). |
|
1273 |
zlibWriter nextPutAll:#[0]. "/ no filter |
|
1274 |
zlibWriter nextPutAll:row. |
|
1275 |
idx := idx + bytesPerScanline. |
|
2703 | 1276 |
]. |
2708 | 1277 |
|
1278 |
zlibWriter close. |
|
1279 |
compressedBytes := compressedByteStream contents. |
|
1280 |
||
1281 |
self |
|
1282 |
writeChunk: 'IDAT' |
|
1283 |
size:(compressedBytes size) |
|
1284 |
with:[ |
|
1285 |
outStream nextPutAll:compressedBytes |
|
1286 |
] |
|
2703 | 1287 |
! |
1288 |
||
2683 | 1289 |
writePaletteChunk |
1290 |
self |
|
1291 |
writeChunk: 'PLTE' |
|
1292 |
size: 3 * colorMap size |
|
1293 |
with:[ |
|
2703 | 1294 |
colorMap do:[:color | |
1295 |
outStream |
|
1296 |
nextPut: color redByte; |
|
1297 |
nextPut: color greenByte; |
|
1298 |
nextPut: color blueByte |
|
2683 | 1299 |
] |
1300 |
] |
|
1301 |
! ! |
|
1302 |
||
398 | 1303 |
!PNGReader class methodsFor:'documentation'! |
308 | 1304 |
|
1305 |
version |
|
3554 | 1306 |
^ '$Header$' |
2909 | 1307 |
! |
1308 |
||
1309 |
version_CVS |
|
3554 | 1310 |
^ '$Header$' |
308 | 1311 |
! ! |
1671 | 1312 |
|
3100 | 1313 |
|
308 | 1314 |
PNGReader initialize! |