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