author | Stefan Vogel <sv@exept.de> |
Tue, 22 Jul 2008 20:49:37 +0200 | |
changeset 2523 | 497ef51ffbe6 |
parent 2509 | 866b56cabe7e |
child 2683 | 8c7880d9cedb |
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 |
15 |
instanceVariableNames:'colorType depth compressionMethod filterMethod interlaceMode |
|
2509 | 16 |
redBytes greenBytes blueBytes bytesPerScanline globalDataChunk' |
308 | 17 |
classVariableNames:'' |
18 |
poolDictionaries:'' |
|
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1694
diff
changeset
|
19 |
category:'Graphics-Images-Readers' |
308 | 20 |
! |
21 |
||
398 | 22 |
!PNGReader class methodsFor:'documentation'! |
308 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
27 |
All Rights Reserved |
|
28 |
||
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
||
37 |
||
38 |
! |
|
39 |
||
40 |
documentation |
|
41 |
" |
|
1694 | 42 |
This class will provide methods for loading PNG pictures. |
43 |
It is currenty unfinished and untested (need a Zlib inflater). |
|
44 |
In the meantime, use a pngtoXXX converter. |
|
308 | 45 |
|
46 |
[See also:] |
|
47 |
Image Form Icon |
|
48 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader |
|
49 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
|
50 |
XBMReader XPMReader XWDReader |
|
51 |
||
52 |
[author:] |
|
53 |
Claus Gittinger |
|
54 |
" |
|
1694 | 55 |
! |
308 | 56 |
|
1694 | 57 |
examples |
58 |
" |
|
59 |
PNGReader fromFile:'/home/cg/AudioExplorer_51_files/use_small.png' |
|
2509 | 60 |
PNGReader fromFile:'\\Exeptn\tmp\images\expeccoScreenshot4020_5526507.png' |
1694 | 61 |
" |
308 | 62 |
! ! |
63 |
||
398 | 64 |
!PNGReader class methodsFor:'initialization'! |
308 | 65 |
|
66 |
initialize |
|
67 |
"install myself in the Image classes fileFormat table |
|
398 | 68 |
for the `.png' extension." |
308 | 69 |
|
647 | 70 |
"/ not yet finished - do not add |
71 |
"/ MIMETypes defineImageType:'image/x-png' suffix:'png' reader:self. |
|
308 | 72 |
|
398 | 73 |
"Modified: 1.2.1997 / 15:02:47 / cg" |
308 | 74 |
! ! |
75 |
||
398 | 76 |
!PNGReader class methodsFor:'testing'! |
308 | 77 |
|
78 |
isValidImageFile:aFileName |
|
79 |
"return true, if aFileName contains a PNG image" |
|
80 |
||
81 |
|inStream magic| |
|
82 |
||
83 |
inStream := self streamReadingFile:aFileName. |
|
84 |
inStream isNil ifTrue:[^ false]. |
|
85 |
||
86 |
inStream binary. |
|
87 |
||
88 |
magic := ByteArray new:8. |
|
89 |
inStream nextBytes:8 into:magic. |
|
90 |
inStream close. |
|
91 |
||
92 |
^ (magic = #[137 80 78 71 13 10 26 10]) |
|
93 |
||
1694 | 94 |
" |
95 |
self isValidImageFile:'/home/cg/AudioExplorer_51_files/use_small.png' |
|
96 |
" |
|
97 |
||
308 | 98 |
"Modified: 21.6.1996 / 20:38:46 / cg" |
99 |
! ! |
|
100 |
||
101 |
!PNGReader methodsFor:'private'! |
|
102 |
||
103 |
getChunk |
|
104 |
|len type crc| |
|
105 |
||
106 |
inStream atEnd ifTrue:[^ false]. |
|
107 |
||
108 |
len := inStream nextLongMSB:true. |
|
109 |
type := String new:4. |
|
110 |
(inStream nextBytes:4 into:type) ~~ 4 ifTrue:[^ false]. |
|
111 |
||
112 |
"/ 'len: ' print. len print. ' type: ' print. type printCR. |
|
113 |
||
114 |
type = 'IEND' ifTrue:[^ false]. |
|
115 |
||
116 |
(self processChunk:type len:len) ifFalse:[^ false]. |
|
117 |
||
1671 | 118 |
crc := inStream nextLongMSB:true. "/ ignored - for now |
308 | 119 |
^ true |
120 |
||
121 |
"Created: 21.6.1996 / 21:09:36 / cg" |
|
122 |
"Modified: 21.6.1996 / 21:20:26 / cg" |
|
123 |
! |
|
124 |
||
125 |
getIHDRChunk |
|
126 |
|len type crc| |
|
127 |
||
128 |
len := inStream nextLongMSB:true. |
|
129 |
||
130 |
type := String new:4. |
|
131 |
inStream nextBytes:4 into:type. |
|
132 |
||
133 |
type = 'IHDR' ifFalse:[self halt. ^ false]. |
|
134 |
len == 13 ifFalse:[self halt. ^ false]. |
|
135 |
||
136 |
width := inStream nextLongMSB:true. |
|
137 |
height := inStream nextLongMSB:true. |
|
138 |
||
139 |
(width <= 0 or:[height <= 0]) ifTrue:[ |
|
140 |
'PNGReader: invalid dimension(s)' infoPrintCR. |
|
141 |
^ false. |
|
142 |
]. |
|
1846 | 143 |
self reportDimension. |
308 | 144 |
|
145 |
depth := inStream nextByte. |
|
146 |
colorType := inStream nextByte. |
|
147 |
||
148 |
compressionMethod := inStream nextByte. |
|
149 |
filterMethod := inStream nextByte. |
|
150 |
interlaceMode := inStream nextByte. |
|
151 |
||
152 |
inStream skip:4. |
|
153 |
"/ crc := inStream nextLongMSB:true. |
|
154 |
^ true |
|
155 |
||
156 |
"Modified: 21.6.1996 / 21:38:35 / cg" |
|
157 |
! |
|
158 |
||
159 |
processBKGDChunkLen:len |
|
160 |
inStream skip:len. |
|
161 |
^ true |
|
162 |
||
163 |
"Created: 21.6.1996 / 21:15:49 / cg" |
|
164 |
! |
|
165 |
||
166 |
processChunk:type len:len |
|
2509 | 167 |
|chunk| |
168 |
||
169 |
type = 'IDAT' ifTrue:[ |
|
170 |
"---since the compressed data can span multiple |
|
171 |
chunks, stitch them all together first. later, |
|
172 |
if memory is an issue, we need to figure out how |
|
173 |
to do this on the fly---" |
|
174 |
chunk := inStream next:len. |
|
175 |
globalDataChunk := globalDataChunk isNil |
|
176 |
ifTrue: [chunk] |
|
177 |
ifFalse:[globalDataChunk,chunk]. |
|
178 |
^ true |
|
179 |
"/ ^ self processIDATChunkLen:len |
|
180 |
]. |
|
181 |
||
308 | 182 |
type = 'gAMA' ifTrue:[^ self processGAMAChunkLen:len]. |
183 |
type = 'sBIT' ifTrue:[^ self processSBITChunkLen:len]. |
|
184 |
type = 'tEXt' ifTrue:[^ self processTEXTChunkLen:len]. |
|
185 |
type = 'tIME' ifTrue:[^ self processTIMEChunkLen:len]. |
|
186 |
type = 'bKGD' ifTrue:[^ self processBKGDChunkLen:len]. |
|
187 |
type = 'zTXt' ifTrue:[^ self processZTXTChunkLen:len]. |
|
188 |
type = 'PLTE' ifTrue:[^ self processPLTEChunkLen:len]. |
|
189 |
||
1671 | 190 |
type = 'pHYs' ifTrue:[^ self processPHYSChunkLen:len]. |
191 |
type = 'tRNS' ifTrue:[^ self processTRNSChunkLen:len]. |
|
192 |
type = 'IHDR' ifTrue:[^ self processIHDRChunkLen:len]. |
|
193 |
||
308 | 194 |
('PNGReader: unrecognized chunk: ' , type , ' ignored.') infoPrintCR. |
195 |
||
196 |
inStream skip:len. |
|
197 |
^ true. |
|
198 |
||
199 |
"Created: 21.6.1996 / 21:10:37 / cg" |
|
200 |
"Modified: 21.6.1996 / 21:22:37 / cg" |
|
201 |
! |
|
202 |
||
203 |
processGAMAChunkLen:len |
|
204 |
inStream skip:len. |
|
205 |
^ true |
|
206 |
||
207 |
"Created: 21.6.1996 / 21:10:52 / cg" |
|
208 |
! |
|
209 |
||
2509 | 210 |
processGlobalIDATChunk |
211 |
interlaceMode == 0 ifTrue: [ |
|
212 |
^ self processNonInterlacedGlobalDATA |
|
213 |
]. |
|
214 |
^ self processInterlacedGlobalDATA |
|
215 |
! |
|
216 |
||
308 | 217 |
processIDATChunkLen:len |
1671 | 218 |
interlaceMode == 0 ifTrue: [ |
219 |
^ self processNonInterlacedDATA:len |
|
220 |
]. |
|
221 |
^ self processInterlacedDATA:len |
|
222 |
! |
|
223 |
||
224 |
processIHDRChunkLen:len |
|
225 |
"header chunk - currently unhandled" |
|
226 |
||
2509 | 227 |
|bitsPerChannel interlaceMethod palette image bitsPerPixel rowSize| |
1671 | 228 |
|
229 |
self halt. |
|
230 |
width := inStream nextLongMSB:true. "/ 1..4 |
|
231 |
height := inStream nextLongMSB:true. "/ 5..8 |
|
232 |
bitsPerChannel := inStream nextByte. "/ 9 |
|
233 |
colorType := inStream nextByte. "/ 10 |
|
234 |
inStream nextByte. "/ 11 |
|
235 |
inStream nextByte. "/ 12 |
|
236 |
interlaceMethod := inStream nextByte. "/ 13 |
|
237 |
||
238 |
(#(2 4 6) includes: colorType) ifTrue:[ |
|
239 |
depth := 32. |
|
240 |
palette := self class defaultPalette. |
|
241 |
image := Image extent: width @ height depth: depth palette: palette |
|
242 |
]. |
|
243 |
(#(0 3) includes: colorType) ifTrue:[ |
|
244 |
depth := bitsPerChannel min: 8. |
|
245 |
image := Image extent: width @ height |
|
246 |
depth: depth |
|
247 |
palette: (colorType = 0 |
|
248 |
ifTrue: [self grayColorsFor: depth] |
|
249 |
ifFalse: [palette := self class defaultPalette])]. |
|
250 |
bitsPerPixel := (BPP at: colorType + 1) at: bitsPerChannel highBit. |
|
251 |
bytesPerScanline := (width * bitsPerPixel + 7) // 8. |
|
252 |
rowSize := image width * image depth + 31 >> 5 |
|
253 |
! |
|
254 |
||
255 |
processInterlacedDATA:len |
|
308 | 256 |
inStream skip:len. |
257 |
^ true |
|
1671 | 258 |
! |
308 | 259 |
|
1671 | 260 |
processNonInterlacedDATA:len |
261 |
| zlibReader filter temp prevScanline thisScanline bytesPerScanline filtersSeen| |
|
262 |
||
2509 | 263 |
zlibReader := ZipStream readOpenAsZipStreamOn:inStream. |
264 |
"/ zlibReader := ZLibReadStream on:inStream from: 1 to:len. |
|
265 |
||
266 |
prevScanline := ByteArray new: self bytesPerRow. |
|
267 |
thisScanline := ByteArray new: self bytesPerRow. |
|
268 |
0 to: height - 1 do: |
|
269 |
[:index | |
|
270 |
filter := (zlibReader next: 1) first. |
|
271 |
filtersSeen add: filter. |
|
272 |
(filter isNil or: [(filter between: 0 and: 4) not]) ifTrue: [^self]. |
|
273 |
thisScanline := zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
|
274 |
self filterScanline: filter count: bytesPerScanline. |
|
275 |
self copyPixels: index. |
|
276 |
temp := prevScanline. |
|
277 |
prevScanline := thisScanline. |
|
278 |
thisScanline := temp] |
|
279 |
! |
|
280 |
||
281 |
processNonInterlacedGlobalDATA |
|
282 |
| zlibReader filter temp prevScanline thisScanline bytesPerScanline filtersSeen| |
|
283 |
||
284 |
zlibReader := ZipStream readOpenAsZipStreamOn:(globalDataChunk readStream). |
|
285 |
bytesPerScanline := self bytesPerRow. |
|
286 |
||
1671 | 287 |
prevScanline := ByteArray new: bytesPerScanline. |
288 |
thisScanline := ByteArray new: bytesPerScanline. |
|
289 |
0 to: height - 1 do: |
|
290 |
[:index | |
|
291 |
filter := (zlibReader next: 1) first. |
|
292 |
filtersSeen add: filter. |
|
293 |
(filter isNil or: [(filter between: 0 and: 4) not]) ifTrue: [^self]. |
|
294 |
thisScanline := zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
|
295 |
self filterScanline: filter count: bytesPerScanline. |
|
296 |
self copyPixels: index. |
|
297 |
temp := prevScanline. |
|
298 |
prevScanline := thisScanline. |
|
299 |
thisScanline := temp] |
|
300 |
! |
|
301 |
||
302 |
processPHYSChunkLen:len |
|
303 |
"physical pixel chunk - currently unhandled" |
|
304 |
||
305 |
'PNGReader: unhandled chunk type: PHYS' infoPrintCR. |
|
306 |
^ false |
|
308 | 307 |
! |
308 |
||
309 |
processPLTEChunkLen:len |
|
1671 | 310 |
"read a color palette" |
311 |
||
308 | 312 |
|n "{ Class: SmallInteger }"| |
313 |
||
314 |
(len \\ 3) ~~ 0 ifTrue:[ |
|
315 |
'PNGReader: invalid size of PLTE chunk' infoPrintCR. |
|
316 |
^ false |
|
317 |
]. |
|
318 |
n := len // 3. |
|
319 |
redBytes := ByteArray new:n. |
|
320 |
greenBytes := ByteArray new:n. |
|
321 |
blueBytes := ByteArray new:n. |
|
322 |
||
323 |
1 to:n do:[:i | |
|
324 |
redBytes at:i put:(inStream nextByte). |
|
325 |
greenBytes at:i put:(inStream nextByte). |
|
326 |
blueBytes at:i put:(inStream nextByte) |
|
327 |
]. |
|
328 |
||
329 |
^ true |
|
330 |
||
331 |
"Created: 21.6.1996 / 21:22:28 / cg" |
|
332 |
"Modified: 21.6.1996 / 21:43:01 / cg" |
|
333 |
! |
|
334 |
||
335 |
processSBITChunkLen:len |
|
336 |
inStream skip:len. |
|
337 |
^ true |
|
338 |
||
339 |
"Created: 21.6.1996 / 21:13:09 / cg" |
|
340 |
! |
|
341 |
||
342 |
processTEXTChunkLen:len |
|
343 |
inStream skip:len. |
|
344 |
^ true |
|
345 |
||
346 |
"Modified: 21.6.1996 / 21:15:27 / cg" |
|
347 |
! |
|
348 |
||
349 |
processTIMEChunkLen:len |
|
350 |
inStream skip:len. |
|
351 |
^ true |
|
352 |
||
353 |
"Created: 21.6.1996 / 21:15:43 / cg" |
|
354 |
"Modified: 21.6.1996 / 21:20:42 / cg" |
|
355 |
! |
|
356 |
||
357 |
processZTXTChunkLen:len |
|
358 |
inStream skip:len. |
|
359 |
^ true |
|
360 |
||
361 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
362 |
! ! |
|
363 |
||
1848 | 364 |
!PNGReader methodsFor:'private-reading'! |
365 |
||
366 |
setColorType:colorType |
|
367 |
colorType == 0 ifTrue:[ |
|
368 |
photometric := #blackIs0. |
|
369 |
samplesPerPixel := 1. |
|
370 |
bitsPerSample := Array with:depth. |
|
371 |
^ true. |
|
372 |
]. |
|
373 |
||
374 |
colorType == 2 ifTrue:[ |
|
375 |
depth < 8 ifTrue:[ |
|
376 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
|
377 |
^ false. |
|
378 |
]. |
|
379 |
photometric := #rgb. |
|
380 |
samplesPerPixel := 3. |
|
381 |
bitsPerSample := Array with:depth with:depth with:depth. |
|
382 |
^ true. |
|
383 |
]. |
|
384 |
||
385 |
colorType == 3 ifTrue:[ |
|
386 |
depth == 16 ifTrue:[ |
|
387 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
|
388 |
^ false. |
|
389 |
]. |
|
390 |
photometric := #palette. |
|
391 |
samplesPerPixel := 1. |
|
392 |
bitsPerSample := Array with:depth. |
|
393 |
^ true. |
|
394 |
]. |
|
395 |
||
396 |
colorType == 4 ifTrue:[ |
|
397 |
depth < 8 ifTrue:[ |
|
398 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
|
399 |
^ false. |
|
400 |
]. |
|
401 |
photometric := #blackIs0. |
|
402 |
samplesPerPixel := 2. |
|
403 |
bitsPerSample := Array with:depth with:depth. |
|
404 |
^ true. |
|
405 |
]. |
|
406 |
||
407 |
colorType == 6 ifTrue:[ |
|
408 |
depth < 8 ifTrue:[ |
|
409 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
|
410 |
^ false. |
|
411 |
]. |
|
412 |
photometric := #rgb. |
|
413 |
samplesPerPixel := 4. |
|
414 |
bitsPerSample := Array with:depth with:depth with:depth with:depth. |
|
415 |
^ true. |
|
416 |
]. |
|
417 |
||
418 |
('PNGReader: invalid colorType: ' , colorType printString , '.') infoPrintCR. |
|
419 |
^ false |
|
420 |
! ! |
|
421 |
||
1805 | 422 |
!PNGReader methodsFor:'reading'! |
308 | 423 |
|
424 |
fromStream:aStream |
|
425 |
"read a stream containing a PNG image. |
|
426 |
Leave image description in instance variables." |
|
427 |
||
428 |
|header| |
|
429 |
||
430 |
inStream := aStream. |
|
431 |
aStream binary. |
|
432 |
||
1671 | 433 |
"PNG-files are always msb (network-world)" |
308 | 434 |
byteOrder := #msb. |
435 |
||
436 |
header := ByteArray new:8. |
|
437 |
aStream nextBytes:8 into:header. |
|
438 |
||
439 |
header ~= #[137 80 78 71 13 10 26 10] ifTrue:[ |
|
440 |
'PNGReader: not a png file.' infoPrintCR. |
|
441 |
^ nil |
|
442 |
]. |
|
443 |
(self getIHDRChunk) ifFalse:[ |
|
444 |
'PNGReader: required IHDR chunk missing.' infoPrintCR. |
|
445 |
^ nil |
|
446 |
]. |
|
447 |
||
448 |
compressionMethod ~~ 0 ifTrue:[ |
|
449 |
('PNGReader: compressionMethod ' , compressionMethod printString , ' not supported.') infoPrintCR. |
|
450 |
^ nil |
|
451 |
]. |
|
452 |
filterMethod > 0 ifTrue:[ |
|
453 |
'PNGReader: invalid filterMethod' infoPrintCR. |
|
454 |
^ nil. |
|
455 |
]. |
|
456 |
interlaceMode > 1 ifTrue:[ |
|
457 |
'PNGReader: invalid interlaceMode' infoPrintCR. |
|
458 |
^ nil. |
|
459 |
]. |
|
1671 | 460 |
(self setColorType:colorType) ifFalse:[ |
308 | 461 |
^ nil |
1671 | 462 |
]. |
308 | 463 |
|
1671 | 464 |
[self getChunk] whileTrue. |
308 | 465 |
|
2509 | 466 |
globalDataChunk notNil ifTrue:[ |
467 |
self processGlobalIDATChunk |
|
468 |
]. |
|
469 |
||
308 | 470 |
photometric == #palette ifTrue:[ |
471 |
redBytes isNil ifTrue:[ |
|
472 |
'PNGReader: missing palette chunk.' infoPrintCR. |
|
473 |
^ nil |
|
474 |
]. |
|
475 |
||
1759
f52382e5a9b8
slowly getting rid of the Colormap class
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
476 |
colorMap := MappedPalette |
308 | 477 |
redVector:redBytes |
478 |
greenVector:greenBytes |
|
479 |
blueVector:blueBytes. |
|
480 |
]. |
|
481 |
||
482 |
self halt. |
|
483 |
" |
|
484 |
PNGReader fromFile:'/home/cg/libpng-0.89c/pngtest.png' |
|
485 |
" |
|
486 |
||
487 |
"Modified: 21.6.1996 / 21:44:34 / cg" |
|
1805 | 488 |
! ! |
489 |
||
398 | 490 |
!PNGReader class methodsFor:'documentation'! |
308 | 491 |
|
492 |
version |
|
2509 | 493 |
^ '$Header: /cvs/stx/stx/libview2/PNGReader.st,v 1.11 2008-06-30 09:36:58 cg Exp $' |
308 | 494 |
! ! |
1671 | 495 |
|
308 | 496 |
PNGReader initialize! |