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:''
|
|
22 |
category:'Graphics-Images-Support'
|
|
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 |
].
|
|
145 |
|
|
146 |
depth := inStream nextByte.
|
|
147 |
colorType := inStream nextByte.
|
|
148 |
|
|
149 |
compressionMethod := inStream nextByte.
|
|
150 |
filterMethod := inStream nextByte.
|
|
151 |
interlaceMode := inStream nextByte.
|
|
152 |
|
|
153 |
inStream skip:4.
|
|
154 |
"/ crc := inStream nextLongMSB:true.
|
|
155 |
^ true
|
|
156 |
|
|
157 |
"Modified: 21.6.1996 / 21:38:35 / cg"
|
|
158 |
!
|
|
159 |
|
|
160 |
processBKGDChunkLen:len
|
|
161 |
inStream skip:len.
|
|
162 |
^ true
|
|
163 |
|
|
164 |
"Created: 21.6.1996 / 21:15:49 / cg"
|
|
165 |
!
|
|
166 |
|
|
167 |
processChunk:type len:len
|
|
168 |
type = 'gAMA' ifTrue:[^ self processGAMAChunkLen:len].
|
|
169 |
type = 'sBIT' ifTrue:[^ self processSBITChunkLen:len].
|
|
170 |
type = 'tEXt' ifTrue:[^ self processTEXTChunkLen:len].
|
|
171 |
type = 'IDAT' ifTrue:[^ self processIDATChunkLen:len].
|
|
172 |
type = 'tIME' ifTrue:[^ self processTIMEChunkLen:len].
|
|
173 |
type = 'bKGD' ifTrue:[^ self processBKGDChunkLen:len].
|
|
174 |
type = 'zTXt' ifTrue:[^ self processZTXTChunkLen:len].
|
|
175 |
type = 'PLTE' ifTrue:[^ self processPLTEChunkLen:len].
|
|
176 |
|
1671
|
177 |
type = 'pHYs' ifTrue:[^ self processPHYSChunkLen:len].
|
|
178 |
type = 'tRNS' ifTrue:[^ self processTRNSChunkLen:len].
|
|
179 |
type = 'IHDR' ifTrue:[^ self processIHDRChunkLen:len].
|
|
180 |
|
308
|
181 |
('PNGReader: unrecognized chunk: ' , type , ' ignored.') infoPrintCR.
|
|
182 |
|
|
183 |
inStream skip:len.
|
|
184 |
^ true.
|
|
185 |
|
|
186 |
"Created: 21.6.1996 / 21:10:37 / cg"
|
|
187 |
"Modified: 21.6.1996 / 21:22:37 / cg"
|
|
188 |
!
|
|
189 |
|
|
190 |
processGAMAChunkLen:len
|
|
191 |
inStream skip:len.
|
|
192 |
^ true
|
|
193 |
|
|
194 |
"Created: 21.6.1996 / 21:10:52 / cg"
|
|
195 |
!
|
|
196 |
|
|
197 |
processIDATChunkLen:len
|
1671
|
198 |
interlaceMode == 0 ifTrue: [
|
|
199 |
^ self processNonInterlacedDATA:len
|
|
200 |
].
|
|
201 |
^ self processInterlacedDATA:len
|
|
202 |
!
|
|
203 |
|
|
204 |
processIHDRChunkLen:len
|
|
205 |
"header chunk - currently unhandled"
|
|
206 |
|
|
207 |
|width height bitsPerChannel interlaceMethod palette image bitsPerPixel bytesPerScanline rowSize|
|
|
208 |
|
|
209 |
self halt.
|
|
210 |
width := inStream nextLongMSB:true. "/ 1..4
|
|
211 |
height := inStream nextLongMSB:true. "/ 5..8
|
|
212 |
bitsPerChannel := inStream nextByte. "/ 9
|
|
213 |
colorType := inStream nextByte. "/ 10
|
|
214 |
inStream nextByte. "/ 11
|
|
215 |
inStream nextByte. "/ 12
|
|
216 |
interlaceMethod := inStream nextByte. "/ 13
|
|
217 |
|
|
218 |
(#(2 4 6) includes: colorType) ifTrue:[
|
|
219 |
depth := 32.
|
|
220 |
palette := self class defaultPalette.
|
|
221 |
image := Image extent: width @ height depth: depth palette: palette
|
|
222 |
].
|
|
223 |
(#(0 3) includes: colorType) ifTrue:[
|
|
224 |
depth := bitsPerChannel min: 8.
|
|
225 |
image := Image extent: width @ height
|
|
226 |
depth: depth
|
|
227 |
palette: (colorType = 0
|
|
228 |
ifTrue: [self grayColorsFor: depth]
|
|
229 |
ifFalse: [palette := self class defaultPalette])].
|
|
230 |
bitsPerPixel := (BPP at: colorType + 1) at: bitsPerChannel highBit.
|
|
231 |
bytesPerScanline := (width * bitsPerPixel + 7) // 8.
|
|
232 |
rowSize := image width * image depth + 31 >> 5
|
|
233 |
!
|
|
234 |
|
|
235 |
processInterlacedDATA:len
|
308
|
236 |
inStream skip:len.
|
|
237 |
^ true
|
1671
|
238 |
!
|
308
|
239 |
|
1671
|
240 |
processNonInterlacedDATA:len
|
|
241 |
| zlibReader filter temp prevScanline thisScanline bytesPerScanline filtersSeen|
|
|
242 |
|
1694
|
243 |
zlibReader := ZLibReadStream on:inStream from: 1 to:len.
|
1671
|
244 |
prevScanline := ByteArray new: bytesPerScanline.
|
|
245 |
thisScanline := ByteArray new: bytesPerScanline.
|
|
246 |
0 to: height - 1 do:
|
|
247 |
[:index |
|
|
248 |
filter := (zlibReader next: 1) first.
|
|
249 |
filtersSeen add: filter.
|
|
250 |
(filter isNil or: [(filter between: 0 and: 4) not]) ifTrue: [^self].
|
|
251 |
thisScanline := zlibReader next: bytesPerScanline into: thisScanline startingAt: 1.
|
|
252 |
self filterScanline: filter count: bytesPerScanline.
|
|
253 |
self copyPixels: index.
|
|
254 |
temp := prevScanline.
|
|
255 |
prevScanline := thisScanline.
|
|
256 |
thisScanline := temp]
|
|
257 |
!
|
|
258 |
|
|
259 |
processPHYSChunkLen:len
|
|
260 |
"physical pixel chunk - currently unhandled"
|
|
261 |
|
|
262 |
'PNGReader: unhandled chunk type: PHYS' infoPrintCR.
|
|
263 |
^ false
|
308
|
264 |
!
|
|
265 |
|
|
266 |
processPLTEChunkLen:len
|
1671
|
267 |
"read a color palette"
|
|
268 |
|
308
|
269 |
|n "{ Class: SmallInteger }"|
|
|
270 |
|
|
271 |
(len \\ 3) ~~ 0 ifTrue:[
|
|
272 |
'PNGReader: invalid size of PLTE chunk' infoPrintCR.
|
|
273 |
^ false
|
|
274 |
].
|
|
275 |
n := len // 3.
|
|
276 |
redBytes := ByteArray new:n.
|
|
277 |
greenBytes := ByteArray new:n.
|
|
278 |
blueBytes := ByteArray new:n.
|
|
279 |
|
|
280 |
1 to:n do:[:i |
|
|
281 |
redBytes at:i put:(inStream nextByte).
|
|
282 |
greenBytes at:i put:(inStream nextByte).
|
|
283 |
blueBytes at:i put:(inStream nextByte)
|
|
284 |
].
|
|
285 |
|
|
286 |
^ true
|
|
287 |
|
|
288 |
"Created: 21.6.1996 / 21:22:28 / cg"
|
|
289 |
"Modified: 21.6.1996 / 21:43:01 / cg"
|
|
290 |
!
|
|
291 |
|
|
292 |
processSBITChunkLen:len
|
|
293 |
inStream skip:len.
|
|
294 |
^ true
|
|
295 |
|
|
296 |
"Created: 21.6.1996 / 21:13:09 / cg"
|
|
297 |
!
|
|
298 |
|
|
299 |
processTEXTChunkLen:len
|
|
300 |
inStream skip:len.
|
|
301 |
^ true
|
|
302 |
|
|
303 |
"Modified: 21.6.1996 / 21:15:27 / cg"
|
|
304 |
!
|
|
305 |
|
|
306 |
processTIMEChunkLen:len
|
|
307 |
inStream skip:len.
|
|
308 |
^ true
|
|
309 |
|
|
310 |
"Created: 21.6.1996 / 21:15:43 / cg"
|
|
311 |
"Modified: 21.6.1996 / 21:20:42 / cg"
|
|
312 |
!
|
|
313 |
|
|
314 |
processZTXTChunkLen:len
|
|
315 |
inStream skip:len.
|
|
316 |
^ true
|
|
317 |
|
|
318 |
"Created: 21.6.1996 / 21:15:58 / cg"
|
|
319 |
! !
|
|
320 |
|
|
321 |
!PNGReader methodsFor:'reading from file'!
|
|
322 |
|
|
323 |
fromStream:aStream
|
|
324 |
"read a stream containing a PNG image.
|
|
325 |
Leave image description in instance variables."
|
|
326 |
|
|
327 |
|header|
|
|
328 |
|
|
329 |
inStream := aStream.
|
|
330 |
aStream binary.
|
|
331 |
|
1671
|
332 |
"PNG-files are always msb (network-world)"
|
308
|
333 |
byteOrder := #msb.
|
|
334 |
|
|
335 |
header := ByteArray new:8.
|
|
336 |
aStream nextBytes:8 into:header.
|
|
337 |
|
|
338 |
header ~= #[137 80 78 71 13 10 26 10] ifTrue:[
|
|
339 |
'PNGReader: not a png file.' infoPrintCR.
|
|
340 |
^ nil
|
|
341 |
].
|
|
342 |
(self getIHDRChunk) ifFalse:[
|
|
343 |
'PNGReader: required IHDR chunk missing.' infoPrintCR.
|
|
344 |
^ nil
|
|
345 |
].
|
|
346 |
|
|
347 |
compressionMethod ~~ 0 ifTrue:[
|
|
348 |
('PNGReader: compressionMethod ' , compressionMethod printString , ' not supported.') infoPrintCR.
|
|
349 |
^ nil
|
|
350 |
].
|
|
351 |
filterMethod > 0 ifTrue:[
|
|
352 |
'PNGReader: invalid filterMethod' infoPrintCR.
|
|
353 |
^ nil.
|
|
354 |
].
|
|
355 |
interlaceMode > 1 ifTrue:[
|
|
356 |
'PNGReader: invalid interlaceMode' infoPrintCR.
|
|
357 |
^ nil.
|
|
358 |
].
|
1671
|
359 |
(self setColorType:colorType) ifFalse:[
|
308
|
360 |
^ nil
|
1671
|
361 |
].
|
308
|
362 |
|
1671
|
363 |
[self getChunk] whileTrue.
|
308
|
364 |
|
|
365 |
photometric == #palette ifTrue:[
|
|
366 |
redBytes isNil ifTrue:[
|
|
367 |
'PNGReader: missing palette chunk.' infoPrintCR.
|
|
368 |
^ nil
|
|
369 |
].
|
|
370 |
|
|
371 |
colorMap := Colormap
|
|
372 |
redVector:redBytes
|
|
373 |
greenVector:greenBytes
|
|
374 |
blueVector:blueBytes.
|
|
375 |
].
|
|
376 |
|
|
377 |
self halt.
|
|
378 |
"
|
|
379 |
PNGReader fromFile:'/home/cg/libpng-0.89c/pngtest.png'
|
|
380 |
"
|
|
381 |
|
|
382 |
"Modified: 21.6.1996 / 21:44:34 / cg"
|
1671
|
383 |
!
|
|
384 |
|
|
385 |
setColorType:colorType
|
|
386 |
colorType == 0 ifTrue:[
|
|
387 |
photometric := #blackIs0.
|
|
388 |
samplesPerPixel := 1.
|
|
389 |
bitsPerSample := Array with:depth.
|
|
390 |
^ true.
|
|
391 |
].
|
|
392 |
|
|
393 |
colorType == 2 ifTrue:[
|
|
394 |
depth < 8 ifTrue:[
|
|
395 |
'PNGReader: invalid colorType/depth combination' infoPrintCR.
|
|
396 |
^ false.
|
|
397 |
].
|
|
398 |
photometric := #rgb.
|
|
399 |
samplesPerPixel := 3.
|
|
400 |
bitsPerSample := Array with:depth with:depth with:depth.
|
|
401 |
^ true.
|
|
402 |
].
|
|
403 |
|
|
404 |
colorType == 3 ifTrue:[
|
|
405 |
depth == 16 ifTrue:[
|
|
406 |
'PNGReader: invalid colorType/depth combination' infoPrintCR.
|
|
407 |
^ false.
|
|
408 |
].
|
|
409 |
photometric := #palette.
|
|
410 |
samplesPerPixel := 1.
|
|
411 |
bitsPerSample := Array with:depth.
|
|
412 |
^ true.
|
|
413 |
].
|
|
414 |
|
|
415 |
colorType == 4 ifTrue:[
|
|
416 |
depth < 8 ifTrue:[
|
|
417 |
'PNGReader: invalid colorType/depth combination' infoPrintCR.
|
|
418 |
^ false.
|
|
419 |
].
|
|
420 |
photometric := #blackIs0.
|
|
421 |
samplesPerPixel := 2.
|
|
422 |
bitsPerSample := Array with:depth with:depth.
|
|
423 |
^ true.
|
|
424 |
].
|
|
425 |
|
|
426 |
colorType == 6 ifTrue:[
|
|
427 |
depth < 8 ifTrue:[
|
|
428 |
'PNGReader: invalid colorType/depth combination' infoPrintCR.
|
|
429 |
^ false.
|
|
430 |
].
|
|
431 |
photometric := #rgb.
|
|
432 |
samplesPerPixel := 4.
|
|
433 |
bitsPerSample := Array with:depth with:depth with:depth with:depth.
|
|
434 |
^ true.
|
|
435 |
].
|
|
436 |
|
|
437 |
('PNGReader: invalid colorType: ' , colorType printString , '.') infoPrintCR.
|
|
438 |
^ false
|
308
|
439 |
! !
|
|
440 |
|
398
|
441 |
!PNGReader class methodsFor:'documentation'!
|
308
|
442 |
|
|
443 |
version
|
1694
|
444 |
^ '$Header: /cvs/stx/stx/libview2/PNGReader.st,v 1.5 2003-02-04 08:57:23 cg Exp $'
|
308
|
445 |
! !
|
1671
|
446 |
|
308
|
447 |
PNGReader initialize!
|