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 |
|
|
15 |
ImageReader subclass:#PNGReader
|
|
16 |
instanceVariableNames:'colorType depth compressionMethod filterMethod interlaceMode
|
|
17 |
redBytes greenBytes blueBytes'
|
|
18 |
classVariableNames:''
|
|
19 |
poolDictionaries:''
|
|
20 |
category:'Graphics-Images-Support'
|
|
21 |
!
|
|
22 |
|
398
|
23 |
!PNGReader class methodsFor:'documentation'!
|
308
|
24 |
|
|
25 |
copyright
|
|
26 |
"
|
|
27 |
COPYRIGHT (c) 1996 by Claus Gittinger
|
|
28 |
All Rights Reserved
|
|
29 |
|
|
30 |
This software is furnished under a license and may be used
|
|
31 |
only in accordance with the terms of that license and with the
|
|
32 |
inclusion of the above copyright notice. This software may not
|
|
33 |
be provided or otherwise made available to, or used by, any
|
|
34 |
other person. No title to or ownership of the software is
|
|
35 |
hereby transferred.
|
|
36 |
"
|
|
37 |
|
|
38 |
|
|
39 |
!
|
|
40 |
|
|
41 |
documentation
|
|
42 |
"
|
|
43 |
this class provides methods for loading PNG pictures.
|
|
44 |
It is currenty untested.
|
|
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 |
"
|
|
55 |
|
|
56 |
! !
|
|
57 |
|
398
|
58 |
!PNGReader class methodsFor:'initialization'!
|
308
|
59 |
|
|
60 |
initialize
|
|
61 |
"install myself in the Image classes fileFormat table
|
398
|
62 |
for the `.png' extension."
|
308
|
63 |
|
647
|
64 |
"/ not yet finished - do not add
|
|
65 |
"/ MIMETypes defineImageType:'image/x-png' suffix:'png' reader:self.
|
308
|
66 |
|
398
|
67 |
"Modified: 1.2.1997 / 15:02:47 / cg"
|
308
|
68 |
! !
|
|
69 |
|
398
|
70 |
!PNGReader class methodsFor:'testing'!
|
308
|
71 |
|
|
72 |
isValidImageFile:aFileName
|
|
73 |
"return true, if aFileName contains a PNG image"
|
|
74 |
|
|
75 |
|inStream magic|
|
|
76 |
|
|
77 |
inStream := self streamReadingFile:aFileName.
|
|
78 |
inStream isNil ifTrue:[^ false].
|
|
79 |
|
|
80 |
inStream binary.
|
|
81 |
|
|
82 |
magic := ByteArray new:8.
|
|
83 |
inStream nextBytes:8 into:magic.
|
|
84 |
inStream close.
|
|
85 |
|
|
86 |
^ (magic = #[137 80 78 71 13 10 26 10])
|
|
87 |
|
|
88 |
"Modified: 21.6.1996 / 20:38:46 / cg"
|
|
89 |
! !
|
|
90 |
|
|
91 |
!PNGReader methodsFor:'private'!
|
|
92 |
|
|
93 |
getChunk
|
|
94 |
|len type crc|
|
|
95 |
|
|
96 |
inStream atEnd ifTrue:[^ false].
|
|
97 |
|
|
98 |
len := inStream nextLongMSB:true.
|
|
99 |
type := String new:4.
|
|
100 |
(inStream nextBytes:4 into:type) ~~ 4 ifTrue:[^ false].
|
|
101 |
|
|
102 |
"/ 'len: ' print. len print. ' type: ' print. type printCR.
|
|
103 |
|
|
104 |
type = 'IEND' ifTrue:[^ false].
|
|
105 |
|
|
106 |
(self processChunk:type len:len) ifFalse:[^ false].
|
|
107 |
|
|
108 |
inStream skip:4.
|
|
109 |
"/ crc := inStream nextLongMSB:true.
|
|
110 |
^ true
|
|
111 |
|
|
112 |
"Created: 21.6.1996 / 21:09:36 / cg"
|
|
113 |
"Modified: 21.6.1996 / 21:20:26 / cg"
|
|
114 |
!
|
|
115 |
|
|
116 |
getIHDRChunk
|
|
117 |
|len type crc|
|
|
118 |
|
|
119 |
len := inStream nextLongMSB:true.
|
|
120 |
|
|
121 |
type := String new:4.
|
|
122 |
inStream nextBytes:4 into:type.
|
|
123 |
|
|
124 |
type = 'IHDR' ifFalse:[self halt. ^ false].
|
|
125 |
len == 13 ifFalse:[self halt. ^ false].
|
|
126 |
|
|
127 |
width := inStream nextLongMSB:true.
|
|
128 |
height := inStream nextLongMSB:true.
|
|
129 |
|
|
130 |
(width <= 0 or:[height <= 0]) ifTrue:[
|
|
131 |
'PNGReader: invalid dimension(s)' infoPrintCR.
|
|
132 |
^ false.
|
|
133 |
].
|
|
134 |
|
|
135 |
depth := inStream nextByte.
|
|
136 |
colorType := inStream nextByte.
|
|
137 |
|
|
138 |
compressionMethod := inStream nextByte.
|
|
139 |
filterMethod := inStream nextByte.
|
|
140 |
interlaceMode := inStream nextByte.
|
|
141 |
|
|
142 |
inStream skip:4.
|
|
143 |
"/ crc := inStream nextLongMSB:true.
|
|
144 |
^ true
|
|
145 |
|
|
146 |
"Modified: 21.6.1996 / 21:38:35 / cg"
|
|
147 |
!
|
|
148 |
|
|
149 |
processBKGDChunkLen:len
|
|
150 |
inStream skip:len.
|
|
151 |
^ true
|
|
152 |
|
|
153 |
"Created: 21.6.1996 / 21:15:49 / cg"
|
|
154 |
!
|
|
155 |
|
|
156 |
processChunk:type len:len
|
|
157 |
type = 'gAMA' ifTrue:[^ self processGAMAChunkLen:len].
|
|
158 |
type = 'sBIT' ifTrue:[^ self processSBITChunkLen:len].
|
|
159 |
type = 'tEXt' ifTrue:[^ self processTEXTChunkLen:len].
|
|
160 |
type = 'IDAT' ifTrue:[^ self processIDATChunkLen:len].
|
|
161 |
type = 'tIME' ifTrue:[^ self processTIMEChunkLen:len].
|
|
162 |
type = 'bKGD' ifTrue:[^ self processBKGDChunkLen:len].
|
|
163 |
type = 'zTXt' ifTrue:[^ self processZTXTChunkLen:len].
|
|
164 |
type = 'PLTE' ifTrue:[^ self processPLTEChunkLen:len].
|
|
165 |
|
|
166 |
('PNGReader: unrecognized chunk: ' , type , ' ignored.') infoPrintCR.
|
|
167 |
|
|
168 |
inStream skip:len.
|
|
169 |
^ true.
|
|
170 |
|
|
171 |
"Created: 21.6.1996 / 21:10:37 / cg"
|
|
172 |
"Modified: 21.6.1996 / 21:22:37 / cg"
|
|
173 |
!
|
|
174 |
|
|
175 |
processGAMAChunkLen:len
|
|
176 |
inStream skip:len.
|
|
177 |
^ true
|
|
178 |
|
|
179 |
"Created: 21.6.1996 / 21:10:52 / cg"
|
|
180 |
!
|
|
181 |
|
|
182 |
processIDATChunkLen:len
|
|
183 |
inStream skip:len.
|
|
184 |
^ true
|
|
185 |
|
|
186 |
"Created: 21.6.1996 / 21:15:35 / cg"
|
|
187 |
!
|
|
188 |
|
|
189 |
processPLTEChunkLen:len
|
|
190 |
|n "{ Class: SmallInteger }"|
|
|
191 |
|
|
192 |
(len \\ 3) ~~ 0 ifTrue:[
|
|
193 |
'PNGReader: invalid size of PLTE chunk' infoPrintCR.
|
|
194 |
^ false
|
|
195 |
].
|
|
196 |
n := len // 3.
|
|
197 |
redBytes := ByteArray new:n.
|
|
198 |
greenBytes := ByteArray new:n.
|
|
199 |
blueBytes := ByteArray new:n.
|
|
200 |
|
|
201 |
1 to:n do:[:i |
|
|
202 |
redBytes at:i put:(inStream nextByte).
|
|
203 |
greenBytes at:i put:(inStream nextByte).
|
|
204 |
blueBytes at:i put:(inStream nextByte)
|
|
205 |
].
|
|
206 |
|
|
207 |
^ true
|
|
208 |
|
|
209 |
"Created: 21.6.1996 / 21:22:28 / cg"
|
|
210 |
"Modified: 21.6.1996 / 21:43:01 / cg"
|
|
211 |
!
|
|
212 |
|
|
213 |
processSBITChunkLen:len
|
|
214 |
inStream skip:len.
|
|
215 |
^ true
|
|
216 |
|
|
217 |
"Created: 21.6.1996 / 21:13:09 / cg"
|
|
218 |
!
|
|
219 |
|
|
220 |
processTEXTChunkLen:len
|
|
221 |
inStream skip:len.
|
|
222 |
^ true
|
|
223 |
|
|
224 |
"Modified: 21.6.1996 / 21:15:27 / cg"
|
|
225 |
!
|
|
226 |
|
|
227 |
processTIMEChunkLen:len
|
|
228 |
inStream skip:len.
|
|
229 |
^ true
|
|
230 |
|
|
231 |
"Created: 21.6.1996 / 21:15:43 / cg"
|
|
232 |
"Modified: 21.6.1996 / 21:20:42 / cg"
|
|
233 |
!
|
|
234 |
|
|
235 |
processZTXTChunkLen:len
|
|
236 |
inStream skip:len.
|
|
237 |
^ true
|
|
238 |
|
|
239 |
"Created: 21.6.1996 / 21:15:58 / cg"
|
|
240 |
! !
|
|
241 |
|
|
242 |
!PNGReader methodsFor:'reading from file'!
|
|
243 |
|
|
244 |
fromStream:aStream
|
|
245 |
"read a stream containing a PNG image.
|
|
246 |
Leave image description in instance variables."
|
|
247 |
|
|
248 |
|header|
|
|
249 |
|
|
250 |
inStream := aStream.
|
|
251 |
aStream binary.
|
|
252 |
|
|
253 |
"GIF-files are always msb (network-world)"
|
|
254 |
byteOrder := #msb.
|
|
255 |
|
|
256 |
header := ByteArray new:8.
|
|
257 |
aStream nextBytes:8 into:header.
|
|
258 |
|
|
259 |
header ~= #[137 80 78 71 13 10 26 10] ifTrue:[
|
|
260 |
'PNGReader: not a png file.' infoPrintCR.
|
|
261 |
^ nil
|
|
262 |
].
|
|
263 |
(self getIHDRChunk) ifFalse:[
|
|
264 |
'PNGReader: required IHDR chunk missing.' infoPrintCR.
|
|
265 |
^ nil
|
|
266 |
].
|
|
267 |
|
|
268 |
compressionMethod ~~ 0 ifTrue:[
|
|
269 |
('PNGReader: compressionMethod ' , compressionMethod printString , ' not supported.') infoPrintCR.
|
|
270 |
^ nil
|
|
271 |
].
|
|
272 |
filterMethod > 0 ifTrue:[
|
|
273 |
'PNGReader: invalid filterMethod' infoPrintCR.
|
|
274 |
^ nil.
|
|
275 |
].
|
|
276 |
interlaceMode > 1 ifTrue:[
|
|
277 |
'PNGReader: invalid interlaceMode' infoPrintCR.
|
|
278 |
^ nil.
|
|
279 |
].
|
|
280 |
|
|
281 |
colorType == 0 ifTrue:[
|
|
282 |
photometric := #blackIs0.
|
|
283 |
samplesPerPixel := 1.
|
|
284 |
bitsPerSample := Array with:depth.
|
|
285 |
] ifFalse:[ colorType == 2 ifTrue:[
|
|
286 |
depth < 8 ifTrue:[
|
|
287 |
'PNGReader: invalid colorType/depth combination' infoPrintCR.
|
|
288 |
^ nil.
|
|
289 |
].
|
|
290 |
photometric := #rgb.
|
|
291 |
samplesPerPixel := 3.
|
|
292 |
bitsPerSample := Array with:depth with:depth with:depth.
|
|
293 |
] ifFalse:[ colorType == 3 ifTrue:[
|
|
294 |
depth == 16 ifTrue:[
|
|
295 |
'PNGReader: invalid colorType/depth combination' infoPrintCR.
|
|
296 |
^ nil.
|
|
297 |
].
|
|
298 |
photometric := #palette.
|
|
299 |
samplesPerPixel := 1.
|
|
300 |
bitsPerSample := Array with:depth.
|
|
301 |
] ifFalse:[ colorType == 4 ifTrue:[
|
|
302 |
depth < 8 ifTrue:[
|
|
303 |
'PNGReader: invalid colorType/depth combination' infoPrintCR.
|
|
304 |
^ nil.
|
|
305 |
].
|
|
306 |
photometric := #blackIs0.
|
|
307 |
samplesPerPixel := 2.
|
|
308 |
bitsPerSample := Array with:depth with:depth.
|
|
309 |
] ifFalse:[ colorType == 6 ifTrue:[
|
|
310 |
depth < 8 ifTrue:[
|
|
311 |
'PNGReader: invalid colorType/depth combination' infoPrintCR.
|
|
312 |
^ nil.
|
|
313 |
].
|
|
314 |
photometric := #rgb.
|
|
315 |
samplesPerPixel := 4.
|
|
316 |
bitsPerSample := Array with:depth with:depth with:depth with:depth.
|
|
317 |
] ifFalse:[
|
|
318 |
('PNGReader: invalid colorType: ' , colorType printString , '.') infoPrintCR.
|
|
319 |
^ nil
|
|
320 |
]]]]].
|
|
321 |
|
|
322 |
[self getChunk] whileTrue:[ ].
|
|
323 |
|
|
324 |
photometric == #palette ifTrue:[
|
|
325 |
redBytes isNil ifTrue:[
|
|
326 |
'PNGReader: missing palette chunk.' infoPrintCR.
|
|
327 |
^ nil
|
|
328 |
].
|
|
329 |
|
|
330 |
colorMap := Colormap
|
|
331 |
redVector:redBytes
|
|
332 |
greenVector:greenBytes
|
|
333 |
blueVector:blueBytes.
|
|
334 |
].
|
|
335 |
|
|
336 |
self halt.
|
|
337 |
"
|
|
338 |
PNGReader fromFile:'/home/cg/libpng-0.89c/pngtest.png'
|
|
339 |
"
|
|
340 |
|
|
341 |
"Modified: 21.6.1996 / 21:44:34 / cg"
|
|
342 |
! !
|
|
343 |
|
398
|
344 |
!PNGReader class methodsFor:'documentation'!
|
308
|
345 |
|
|
346 |
version
|
647
|
347 |
^ '$Header: /cvs/stx/stx/libview2/PNGReader.st,v 1.3 1997-06-30 20:56:07 cg Exp $'
|
308
|
348 |
! !
|
|
349 |
PNGReader initialize!
|