author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Sun, 14 Feb 2016 07:32:08 +0000 | |
branch | jv |
changeset 3556 | c2c45185ef9d |
parent 3505 | af4fb19df063 |
parent 3554 | f1820748e4a9 |
child 3600 | 5325c3ad7507 |
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 |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
17 |
instanceVariableNames:'colorType bitsPerChannel depth compressionMethod filterMethod |
2725 | 18 |
interlaceMode bytesPerScanline globalDataChunk thisScanline |
19 |
prevScanline' |
|
2707 | 20 |
classVariableNames:'Verbose' |
308 | 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 |
" |
|
2725 | 45 |
This class will provide methods for loading and saving PNG pictures. |
3437 | 46 |
It is currenty unfinished (interlaced image support is missing resp. being developed as we encounter more formats). |
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). |
|
51 |
writer only generates unfiltered rows or interlace data |
|
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. |
|
196 |
globalDataChunk := globalDataChunk isNil |
|
197 |
ifTrue: [chunk] |
|
198 |
ifFalse:[globalDataChunk,chunk]. |
|
199 |
^ true |
|
200 |
"/ ^ self processIDATChunkLen:len |
|
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 |
||
308 | 216 |
('PNGReader: unrecognized chunk: ' , type , ' ignored.') infoPrintCR. |
217 |
||
218 |
inStream skip:len. |
|
219 |
^ true. |
|
220 |
||
221 |
"Created: 21.6.1996 / 21:10:37 / cg" |
|
222 |
"Modified: 21.6.1996 / 21:22:37 / cg" |
|
223 |
! |
|
224 |
||
225 |
processGAMAChunkLen:len |
|
226 |
inStream skip:len. |
|
227 |
^ true |
|
228 |
||
229 |
"Created: 21.6.1996 / 21:10:52 / cg" |
|
230 |
! |
|
231 |
||
2509 | 232 |
processGlobalIDATChunk |
233 |
interlaceMode == 0 ifTrue: [ |
|
234 |
^ self processNonInterlacedGlobalDATA |
|
235 |
]. |
|
236 |
^ self processInterlacedGlobalDATA |
|
237 |
! |
|
238 |
||
308 | 239 |
processIDATChunkLen:len |
1671 | 240 |
interlaceMode == 0 ifTrue: [ |
241 |
^ self processNonInterlacedDATA:len |
|
242 |
]. |
|
243 |
^ self processInterlacedDATA:len |
|
244 |
! |
|
245 |
||
246 |
processInterlacedDATA:len |
|
308 | 247 |
inStream skip:len. |
248 |
^ true |
|
1671 | 249 |
! |
308 | 250 |
|
2699 | 251 |
processInterlacedGlobalDATA |
3327 | 252 |
"adam7 interlace method" |
253 |
||
2699 | 254 |
| zlibReader filter bytesPerPass startingCol colIncrement rowIncrement |
3327 | 255 |
startingRow cx sc temp "filtersSeen" bitsPerPixel cy| |
2699 | 256 |
|
3327 | 257 |
interlaceMode == 1 ifFalse: [ |
258 |
self error:'unsupported interlaced mode'. |
|
259 |
^ self |
|
260 |
]. |
|
2699 | 261 |
|
262 |
"/ filtersSeen := Set new. |
|
263 |
||
264 |
startingCol := #(0 4 0 2 0 1 0 ). |
|
3327 | 265 |
startingRow := #(0 0 4 0 2 0 1 ). |
2699 | 266 |
colIncrement := #(8 8 4 4 2 2 1 ). |
267 |
rowIncrement := #(8 8 8 4 4 2 2 ). |
|
268 |
||
2701 | 269 |
zlibReader := ZipStream readOpenAsZipStreamOn:(globalDataChunk readStream) suppressHeaderAndChecksum:false. |
2699 | 270 |
zlibReader binary. |
271 |
bytesPerScanline := self bytesPerRow. |
|
272 |
bitsPerPixel := self bitsPerPixel. |
|
273 |
||
274 |
1 to: 7 do: [:pass | |
|
275 |
(self doPass: pass) ifTrue:[ |
|
276 |
cx := colIncrement at: pass. |
|
277 |
sc := startingCol at: pass. |
|
3327 | 278 |
cy := rowIncrement at: pass. |
2699 | 279 |
bytesPerPass := width - sc + cx - 1 // cx * bitsPerPixel + 7 // 8. |
280 |
prevScanline := ByteArray new: bytesPerPass. |
|
281 |
thisScanline := ByteArray new: bytesPerScanline. |
|
3327 | 282 |
(startingRow at: pass) to: height - 1 by: cy do: [:y | |
3343 | 283 |
filter := zlibReader nextByte. |
2699 | 284 |
"/ filtersSeen add: filter. |
285 |
(filter isNil or: [(filter between: 0 and: 4) not]) |
|
286 |
ifTrue: [^ self]. |
|
287 |
zlibReader next: bytesPerPass into: thisScanline startingAt: 1. |
|
288 |
filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerPass ]. |
|
289 |
self copyPixels:y at:sc by:cx. |
|
290 |
temp := prevScanline. |
|
291 |
prevScanline := thisScanline. |
|
292 |
thisScanline := temp. |
|
293 |
] |
|
294 |
] |
|
295 |
]. |
|
296 |
zlibReader atEnd ifFalse:[self error:'Unexpected data']. |
|
2909 | 297 |
|
298 |
"Modified: / 03-05-2011 / 12:03:33 / cg" |
|
2699 | 299 |
! |
300 |
||
1671 | 301 |
processNonInterlacedDATA:len |
2701 | 302 |
self halt:'not used' |
303 |
"/ |
|
304 |
"/ | zlibReader filter temp prevScanline thisScanline bytesPerScanline filtersSeen| |
|
305 |
"/ |
|
306 |
"/ zlibReader := ZipStream readOpenAsZipStreamOn:inStream suppressHeaderAndChecksum:false. |
|
307 |
"/ "/ zlibReader := ZLibReadStream on:inStream from: 1 to:len. |
|
308 |
"/ |
|
309 |
"/ prevScanline := ByteArray new: self bytesPerRow. |
|
310 |
"/ thisScanline := ByteArray new: self bytesPerRow. |
|
311 |
"/ 0 to: height - 1 do:[:index | |
|
312 |
"/ filter := (zlibReader next: 1) first. |
|
313 |
"/ filtersSeen add: filter. |
|
314 |
"/ (filter isNil or: [(filter between: 0 and: 4) not]) ifTrue: [^self]. |
|
315 |
"/ thisScanline := zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
|
316 |
"/ self filterScanline: filter count: bytesPerScanline. |
|
317 |
"/ self copyPixels: index. |
|
318 |
"/ temp := prevScanline. |
|
319 |
"/ prevScanline := thisScanline. |
|
320 |
"/ thisScanline := temp |
|
321 |
"/ ] |
|
2509 | 322 |
! |
323 |
||
324 |
processNonInterlacedGlobalDATA |
|
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
325 |
| "data n" zlibReader filter temp bytesPerRow "filtersSeen" i| |
2696 | 326 |
|
327 |
"/ filtersSeen := Set new. |
|
2695 | 328 |
|
329 |
"/ data := ByteArray new:(self bytesPerRow * height)+1000. |
|
330 |
"/ n := ZipStream uncompress: globalDataChunk into: data. |
|
331 |
"/ self halt. |
|
2509 | 332 |
|
2701 | 333 |
zlibReader := ZipStream readOpenAsZipStreamOn:(globalDataChunk readStream) suppressHeaderAndChecksum:false. |
2696 | 334 |
zlibReader binary. |
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
335 |
bytesPerRow := self bytesPerRow. |
2509 | 336 |
|
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
337 |
prevScanline := ByteArray new: bytesPerRow. |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
338 |
thisScanline := ByteArray new: bytesPerRow. |
2725 | 339 |
0 to: height - 1 do:[:y | |
3343 | 340 |
filter := zlibReader nextByte. |
2696 | 341 |
"/ filtersSeen add: filter. |
2725 | 342 |
(filter notNil and: [filter between: 0 and: 4]) ifFalse: [ |
343 |
'PNGReader: unsupported filter' infoPrintCR. |
|
344 |
^ self |
|
345 |
]. |
|
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
346 |
zlibReader next: bytesPerRow into: thisScanline startingAt: 1. |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
347 |
filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerRow ]. |
2725 | 348 |
|
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
349 |
i := y * bytesPerRow. |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
350 |
data replaceFrom:i+1 to:(i+bytesPerRow) with:thisScanline startingAt:1. |
2725 | 351 |
|
2695 | 352 |
temp := prevScanline. |
353 |
prevScanline := thisScanline. |
|
354 |
thisScanline := temp |
|
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
355 |
]. |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
356 |
"/ Fill in mask if image contains alpha channel |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
357 |
photometric == #rgba ifTrue:[ |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
358 |
| maskBits | |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
359 |
|
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
360 |
depth ~~ 8 ifTrue:[ |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
361 |
self notYetImplemented. |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
362 |
]. |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
363 |
mask := AlphaMask width: width height: height. |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
364 |
maskBits := ByteArray new: width * height. |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
365 |
|
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
366 |
0 to: height - 1 do:[:y | |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
367 |
0 to: width - 1 do:[:x | |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
368 |
| alpha | |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
369 |
|
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
370 |
alpha := data at: (y * bytesPerRow) + (x * 4) + 1. |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
371 |
maskBits at: (y*width) + x + 1 put: alpha |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
372 |
] |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
373 |
]. |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
374 |
mask bits: maskBits |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
375 |
]. |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
376 |
|
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
377 |
"Modified: / 01-09-2015 / 17:13:04 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
1671 | 378 |
! |
379 |
||
380 |
processPHYSChunkLen:len |
|
381 |
"physical pixel chunk - currently unhandled" |
|
382 |
||
2705 | 383 |
|pixelPerUnitX pixelPerUnitY unit| |
384 |
||
3554 | 385 |
pixelPerUnitX := inStream nextInt32MSB:true. |
386 |
pixelPerUnitY := inStream nextInt32MSB:true. |
|
2705 | 387 |
unit := inStream nextByte. |
388 |
||
2707 | 389 |
Verbose == true ifTrue:[ |
390 |
'PNGReader: PHYS chunk ignored:' infoPrintCR. |
|
391 |
'PNGReader: ppuX:' infoPrint. pixelPerUnitX infoPrintCR. |
|
392 |
'PNGReader: ppuY:' infoPrint. pixelPerUnitY infoPrintCR. |
|
393 |
'PNGReader: unit:' infoPrint. unit infoPrintCR. |
|
394 |
]. |
|
395 |
||
2705 | 396 |
^ true |
308 | 397 |
! |
398 |
||
399 |
processPLTEChunkLen:len |
|
1671 | 400 |
"read a color palette" |
401 |
||
2725 | 402 |
|n "{ Class: SmallInteger }" |
403 |
redBytes greenBytes blueBytes| |
|
308 | 404 |
|
405 |
(len \\ 3) ~~ 0 ifTrue:[ |
|
406 |
'PNGReader: invalid size of PLTE chunk' infoPrintCR. |
|
407 |
^ false |
|
408 |
]. |
|
409 |
n := len // 3. |
|
410 |
redBytes := ByteArray new:n. |
|
411 |
greenBytes := ByteArray new:n. |
|
412 |
blueBytes := ByteArray new:n. |
|
413 |
||
414 |
1 to:n do:[:i | |
|
415 |
redBytes at:i put:(inStream nextByte). |
|
416 |
greenBytes at:i put:(inStream nextByte). |
|
417 |
blueBytes at:i put:(inStream nextByte) |
|
418 |
]. |
|
419 |
||
2725 | 420 |
colorMap := MappedPalette |
421 |
redVector:redBytes |
|
422 |
greenVector:greenBytes |
|
423 |
blueVector:blueBytes. |
|
424 |
||
308 | 425 |
^ true |
426 |
||
427 |
"Created: 21.6.1996 / 21:22:28 / cg" |
|
428 |
"Modified: 21.6.1996 / 21:43:01 / cg" |
|
429 |
! |
|
430 |
||
431 |
processSBITChunkLen:len |
|
432 |
inStream skip:len. |
|
433 |
^ true |
|
434 |
||
435 |
"Created: 21.6.1996 / 21:13:09 / cg" |
|
436 |
! |
|
437 |
||
438 |
processTEXTChunkLen:len |
|
439 |
inStream skip:len. |
|
440 |
^ true |
|
441 |
||
442 |
"Modified: 21.6.1996 / 21:15:27 / cg" |
|
443 |
! |
|
444 |
||
445 |
processTIMEChunkLen:len |
|
446 |
inStream skip:len. |
|
447 |
^ true |
|
448 |
||
449 |
"Created: 21.6.1996 / 21:15:43 / cg" |
|
450 |
"Modified: 21.6.1996 / 21:20:42 / cg" |
|
451 |
! |
|
452 |
||
2707 | 453 |
processTRNSChunkLen:len |
454 |
inStream skip:len. |
|
455 |
^ true |
|
456 |
! |
|
457 |
||
308 | 458 |
processZTXTChunkLen:len |
459 |
inStream skip:len. |
|
460 |
^ true |
|
461 |
||
462 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
463 |
! ! |
|
464 |
||
2696 | 465 |
!PNGReader methodsFor:'private-filtering'! |
466 |
||
2909 | 467 |
filterAverage:count |
2699 | 468 |
"Use the average of the pixel to the left and the pixel above as a predictor" |
2909 | 469 |
|
470 |
|delta| |
|
2699 | 471 |
|
2909 | 472 |
delta := self bitsPerPixel // 8 max:1. |
473 |
1 to:delta do:[:i | |
|
3384 | 474 |
thisScanline at:i put:((thisScanline at:i) + ((prevScanline at:i) // 2) bitAnd:255) |
2909 | 475 |
]. |
476 |
delta + 1 to:count do:[:i | |
|
3384 | 477 |
thisScanline at:i put:((thisScanline at:i) + (((prevScanline at:i) + (thisScanline at:i - delta)) // 2) bitAnd:255) |
2909 | 478 |
] |
479 |
||
480 |
"Modified: / 03-05-2011 / 12:14:01 / cg" |
|
2699 | 481 |
! |
482 |
||
2696 | 483 |
filterHorizontal:count |
484 |
"use the pixel to the left as a predictor" |
|
485 |
||
486 |
|delta| |
|
487 |
||
488 |
delta := self bitsPerPixel // 8 max:1. |
|
489 |
delta+1 to:count do:[:i| |
|
490 |
thisScanline at:i put:(((thisScanline at:i)+ (thisScanline at:i-delta)) bitAnd:255) |
|
491 |
] |
|
492 |
! |
|
493 |
||
2699 | 494 |
filterNone: count |
2909 | 495 |
"/ no filter - scanline is as is |
496 |
||
497 |
"Modified: / 03-05-2011 / 12:14:20 / cg" |
|
2699 | 498 |
! |
499 |
||
2909 | 500 |
filterPaeth:count |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
501 |
"Select one of (the pixel to the left, the pixel above and the pixel to above left) to |
2909 | 502 |
predict the value of this pixel" |
503 |
||
504 |
|delta| |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
505 |
|
2909 | 506 |
delta := self bitsPerPixel // 8 max:1. |
507 |
1 to:delta do:[:i | |
|
508 |
thisScanline |
|
509 |
at:i |
|
510 |
put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255) |
|
511 |
]. |
|
512 |
delta + 1 to:count do:[:i | |
|
513 |
thisScanline |
|
514 |
at:i |
|
515 |
put:(((thisScanline at:i) + (self |
|
516 |
paethPredictLeft:(thisScanline at:i - delta) |
|
517 |
above:(prevScanline at:i) |
|
518 |
aboveLeft:(prevScanline at:i - delta))) |
|
519 |
bitAnd:255) |
|
520 |
] |
|
521 |
||
522 |
"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
|
523 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
524 |
|
2696 | 525 |
filterScanline:filterType count:count |
526 |
self |
|
2909 | 527 |
perform:(#(filterNone: filterHorizontal: filterVertical: filterAverage: filterPaeth:) at:filterType+1) |
2696 | 528 |
with:count |
2909 | 529 |
|
530 |
"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
|
531 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
532 |
|
2909 | 533 |
filterVertical:count |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
534 |
"Use the pixel above as a predictor" |
2909 | 535 |
|
536 |
1 to:count do:[:i | |
|
537 |
thisScanline |
|
538 |
at:i |
|
539 |
put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255) |
|
540 |
] |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
541 |
|
2909 | 542 |
"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
|
543 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
544 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
545 |
paethPredictLeft: a above: b aboveLeft: c |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
546 |
"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
|
547 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
548 |
| pa pb pc | |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
549 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
550 |
pa := b > c ifTrue: [b - c] ifFalse: [c - b]. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
551 |
pb := a > c ifTrue: [a - c] ifFalse: [c - a]. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
552 |
pc := a + b - c - c. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
553 |
pc < 0 ifTrue: [ |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
554 |
pc := pc * -1 |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
555 |
]. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
556 |
((pa <= pb) and: [pa <= pc]) ifTrue: [^ a]. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
557 |
(pb <= pc) ifTrue: [^ b]. |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
558 |
^ c |
2696 | 559 |
! ! |
560 |
||
2699 | 561 |
!PNGReader methodsFor:'private-pixel copy'! |
562 |
||
2909 | 563 |
copyPixels:y at:startX by:incX |
2704 | 564 |
"Handle interlaced pixels of supported colorTypes" |
2909 | 565 |
|
566 |
|s| |
|
2699 | 567 |
|
2909 | 568 |
s := #( #copyPixelsGray:at:by: |
569 |
nil |
|
570 |
#copyPixelsRGB:at:by: |
|
571 |
#copyPixelsIndexed:at:by: |
|
572 |
#copyPixelsGrayAlpha:at:by: |
|
573 |
nil |
|
574 |
#copyPixelsRGBA:at:by: |
|
575 |
) at:colorType + 1. |
|
3437 | 576 |
self perform:s with:y with:startX with:incX |
1848 | 577 |
|
2909 | 578 |
"Modified: / 03-05-2011 / 12:02:45 / cg" |
3327 | 579 |
! |
580 |
||
581 |
copyPixelsRGBA:y at:startX by:incX |
|
582 |
"Handle interlaced pixels of supported colorTypes. |
|
583 |
Untested code - please verify" |
|
584 |
||
3437 | 585 |
|srcIndex nPixels dstIndex dstInc| |
3327 | 586 |
|
587 |
srcIndex := 1. |
|
3437 | 588 |
dstIndex := (y * bytesPerScanline) + (startX * 4) + 1. |
589 |
dstInc := incX * 4. |
|
3327 | 590 |
|
591 |
nPixels := thisScanline size. |
|
592 |
[srcIndex < nPixels] whileTrue:[ |
|
3437 | 593 |
data replaceFrom:dstIndex to:dstIndex+3 with:thisScanline startingAt:srcIndex. |
3327 | 594 |
srcIndex := srcIndex + 4. |
3437 | 595 |
dstIndex := dstIndex + dstInc |
3327 | 596 |
]. |
2699 | 597 |
! ! |
598 |
||
599 |
!PNGReader methodsFor:'private-reading'! |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
600 |
|
2683 | 601 |
getChunk |
602 |
|len type crc| |
|
603 |
||
604 |
inStream atEnd ifTrue:[^ false]. |
|
605 |
||
3554 | 606 |
len := inStream nextInt32MSB:true. |
2683 | 607 |
type := String new:4. |
608 |
(inStream nextBytes:4 into:type) ~~ 4 ifTrue:[^ false]. |
|
609 |
||
2707 | 610 |
Verbose == true ifTrue:[ |
611 |
'len: ' infoPrint. len infoPrint. ' type: ' infoPrint. type infoPrintCR. |
|
612 |
]. |
|
2683 | 613 |
|
614 |
type = 'IEND' ifTrue:[^ false]. |
|
615 |
||
616 |
(self processChunk:type len:len) ifFalse:[^ false]. |
|
617 |
||
3554 | 618 |
crc := inStream nextInt32MSB:true. "/ ignored - for now |
2683 | 619 |
^ true |
620 |
||
621 |
"Created: 21.6.1996 / 21:09:36 / cg" |
|
622 |
"Modified: 21.6.1996 / 21:20:26 / cg" |
|
623 |
! |
|
624 |
||
625 |
getIHDRChunk |
|
626 |
|len type crc| |
|
627 |
||
3554 | 628 |
len := inStream nextInt32MSB:true. |
2683 | 629 |
|
630 |
type := String new:4. |
|
631 |
inStream nextBytes:4 into:type. |
|
632 |
||
3183 | 633 |
type = 'IHDR' ifFalse:[self halt:'expected IHDR magic'. ^ false]. |
634 |
len == 13 ifFalse:[self halt:'unexpected IHDR length'. ^ false]. |
|
2683 | 635 |
|
3554 | 636 |
width := inStream nextInt32MSB:true. |
637 |
height := inStream nextInt32MSB:true. |
|
2683 | 638 |
|
639 |
(width <= 0 or:[height <= 0]) ifTrue:[ |
|
640 |
'PNGReader: invalid dimension(s)' infoPrintCR. |
|
641 |
^ false. |
|
642 |
]. |
|
643 |
self reportDimension. |
|
644 |
||
645 |
depth := inStream nextByte. "/ bits-per-channel |
|
646 |
colorType := inStream nextByte. |
|
647 |
||
648 |
compressionMethod := inStream nextByte. |
|
649 |
filterMethod := inStream nextByte. |
|
650 |
interlaceMode := inStream nextByte. |
|
651 |
||
2707 | 652 |
Verbose == true ifTrue:[ |
653 |
'PNGReader: IHDR:' infoPrintCR. |
|
654 |
'PNGReader: width: ' infoPrint. width infoPrintCR. |
|
655 |
'PNGReader: height: ' infoPrint. height infoPrintCR. |
|
656 |
'PNGReader: depth: ' infoPrint. depth infoPrintCR. |
|
657 |
'PNGReader: colorType: ' infoPrint. colorType infoPrintCR. |
|
658 |
'PNGReader: compressionMethod: ' infoPrint. compressionMethod infoPrintCR. |
|
659 |
'PNGReader: filterMethod: ' infoPrint. filterMethod infoPrintCR. |
|
660 |
'PNGReader: interlaceMode: ' infoPrint. interlaceMode infoPrintCR. |
|
661 |
]. |
|
662 |
||
3554 | 663 |
crc := inStream nextInt32MSB:true. |
2683 | 664 |
^ true |
665 |
||
666 |
"Modified: 21.6.1996 / 21:38:35 / cg" |
|
667 |
! |
|
668 |
||
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
669 |
setColorType:pngColorType |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
670 |
pngColorType == 0 ifTrue:[ |
1848 | 671 |
photometric := #blackIs0. |
672 |
samplesPerPixel := 1. |
|
673 |
bitsPerSample := Array with:depth. |
|
674 |
^ true. |
|
675 |
]. |
|
676 |
||
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
677 |
pngColorType == 2 ifTrue:[ |
1848 | 678 |
depth < 8 ifTrue:[ |
679 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
|
680 |
^ false. |
|
681 |
]. |
|
682 |
photometric := #rgb. |
|
683 |
samplesPerPixel := 3. |
|
684 |
bitsPerSample := Array with:depth with:depth with:depth. |
|
685 |
^ true. |
|
686 |
]. |
|
687 |
||
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
688 |
pngColorType == 3 ifTrue:[ |
1848 | 689 |
depth == 16 ifTrue:[ |
690 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
|
691 |
^ false. |
|
692 |
]. |
|
693 |
photometric := #palette. |
|
694 |
samplesPerPixel := 1. |
|
695 |
bitsPerSample := Array with:depth. |
|
696 |
^ true. |
|
697 |
]. |
|
698 |
||
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
699 |
pngColorType == 4 ifTrue:[ |
1848 | 700 |
depth < 8 ifTrue:[ |
701 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
|
702 |
^ false. |
|
703 |
]. |
|
704 |
photometric := #blackIs0. |
|
705 |
samplesPerPixel := 2. |
|
706 |
bitsPerSample := Array with:depth with:depth. |
|
707 |
^ true. |
|
708 |
]. |
|
709 |
||
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
710 |
pngColorType == 6 ifTrue:[ |
1848 | 711 |
depth < 8 ifTrue:[ |
712 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
|
713 |
^ false. |
|
714 |
]. |
|
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
715 |
photometric := #rgba. |
1848 | 716 |
samplesPerPixel := 4. |
717 |
bitsPerSample := Array with:depth with:depth with:depth with:depth. |
|
718 |
^ true. |
|
719 |
]. |
|
720 |
||
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
721 |
('PNGReader: invalid colorType: ' , pngColorType printString , '.') infoPrintCR. |
1848 | 722 |
^ false |
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
723 |
|
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
724 |
"Modified: / 01-09-2015 / 16:02:38 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
1848 | 725 |
! ! |
726 |
||
1805 | 727 |
!PNGReader methodsFor:'reading'! |
308 | 728 |
|
729 |
fromStream:aStream |
|
730 |
"read a stream containing a PNG image. |
|
731 |
Leave image description in instance variables." |
|
732 |
||
733 |
|header| |
|
734 |
||
735 |
inStream := aStream. |
|
736 |
aStream binary. |
|
737 |
||
1671 | 738 |
"PNG-files are always msb (network-world)" |
308 | 739 |
byteOrder := #msb. |
740 |
||
741 |
header := ByteArray new:8. |
|
742 |
aStream nextBytes:8 into:header. |
|
743 |
||
2683 | 744 |
header ~= (self pngHeader) ifTrue:[ |
308 | 745 |
'PNGReader: not a png file.' infoPrintCR. |
746 |
^ nil |
|
747 |
]. |
|
2695 | 748 |
|
308 | 749 |
(self getIHDRChunk) ifFalse:[ |
750 |
'PNGReader: required IHDR chunk missing.' infoPrintCR. |
|
751 |
^ nil |
|
752 |
]. |
|
753 |
||
754 |
compressionMethod ~~ 0 ifTrue:[ |
|
755 |
('PNGReader: compressionMethod ' , compressionMethod printString , ' not supported.') infoPrintCR. |
|
756 |
^ nil |
|
757 |
]. |
|
758 |
filterMethod > 0 ifTrue:[ |
|
759 |
'PNGReader: invalid filterMethod' infoPrintCR. |
|
760 |
^ nil. |
|
761 |
]. |
|
762 |
interlaceMode > 1 ifTrue:[ |
|
763 |
'PNGReader: invalid interlaceMode' infoPrintCR. |
|
764 |
^ nil. |
|
765 |
]. |
|
1671 | 766 |
(self setColorType:colorType) ifFalse:[ |
308 | 767 |
^ nil |
1671 | 768 |
]. |
2706 | 769 |
|
1671 | 770 |
[self getChunk] whileTrue. |
308 | 771 |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
772 |
data := ByteArray new:(self bytesPerRow * height). |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
773 |
|
2509 | 774 |
globalDataChunk notNil ifTrue:[ |
3343 | 775 |
self processGlobalIDATChunk. |
776 |
globalDataChunk := nil. |
|
2509 | 777 |
]. |
778 |
||
308 | 779 |
photometric == #palette ifTrue:[ |
2725 | 780 |
colorMap isNil ifTrue:[ |
308 | 781 |
'PNGReader: missing palette chunk.' infoPrintCR. |
2725 | 782 |
"/ ^ nil |
308 | 783 |
]. |
784 |
]. |
|
785 |
||
786 |
" |
|
787 |
PNGReader fromFile:'/home/cg/libpng-0.89c/pngtest.png' |
|
788 |
" |
|
789 |
||
2910
d5171b9245e6
comment/format in: #fromStream:
Claus Gittinger <cg@exept.de>
parents:
2909
diff
changeset
|
790 |
"Modified: / 03-05-2011 / 12:17:34 / cg" |
1805 | 791 |
! ! |
792 |
||
2683 | 793 |
!PNGReader methodsFor:'writing to file'! |
794 |
||
795 |
pngHeader |
|
796 |
^ #[137 80 78 71 13 10 26 10] |
|
797 |
! |
|
798 |
||
799 |
save:image onStream:aStream |
|
800 |
"save image in PNG-file-format onto aStream" |
|
801 |
||
802 |
outStream := aStream. |
|
803 |
outStream binary. |
|
804 |
||
2703 | 805 |
byteOrder := #msb. |
2683 | 806 |
width := image width. |
807 |
height := image height. |
|
808 |
photometric := image photometric. |
|
809 |
samplesPerPixel := image samplesPerPixel. |
|
810 |
bitsPerSample := image bitsPerSample. |
|
811 |
colorMap := image colorMap. |
|
812 |
data := image bits. |
|
813 |
||
2703 | 814 |
outStream nextPutAll:(self pngHeader). |
815 |
self writeIHDRChunk. |
|
816 |
photometric == #palette ifTrue: [self writePaletteChunk]. |
|
817 |
self writeImageDataChunk. |
|
2683 | 818 |
self writeEndChunk |
819 |
! |
|
820 |
||
2703 | 821 |
writeChunk:chunkTypeChars size:len with:aBlock |
2708 | 822 |
|crc realOutStream chunkBytes| |
823 |
||
824 |
realOutStream := outStream. |
|
825 |
outStream := WriteStream on:(ByteArray new:len). |
|
826 |
||
2703 | 827 |
outStream nextPutBytes:4 from:chunkTypeChars startingAt:1. |
828 |
aBlock value. |
|
2708 | 829 |
|
830 |
chunkBytes := outStream contents. |
|
831 |
crc := CRC32Stream hashValueOf:chunkBytes. |
|
832 |
||
833 |
outStream := realOutStream. |
|
834 |
||
3554 | 835 |
realOutStream nextPutInt32:len MSB:true. |
2708 | 836 |
realOutStream nextPutAll:chunkBytes. |
3554 | 837 |
realOutStream nextPutInt32:crc MSB:true. |
2703 | 838 |
! |
2683 | 839 |
|
2703 | 840 |
writeEndChunk |
841 |
self |
|
842 |
writeChunk: 'IEND' |
|
843 |
size: 0 |
|
844 |
with:[ ] |
|
2683 | 845 |
! |
846 |
||
847 |
writeFileHeader |
|
848 |
outStream nextPutAll:(self pngHeader) |
|
849 |
! |
|
850 |
||
2703 | 851 |
writeIHDRChunk |
852 |
|colorType| |
|
853 |
||
3100 | 854 |
((photometric == #whiteIs0) or:[ (photometric == #blackIs0)]) ifTrue:[ |
2703 | 855 |
colorType := 0. |
3461 | 856 |
samplesPerPixel > 1 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
2703 | 857 |
]. |
858 |
(photometric == #rgb) ifTrue:[ |
|
859 |
colorType := 2. |
|
3461 | 860 |
samplesPerPixel > 3 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
2703 | 861 |
]. |
862 |
(photometric == #palette) ifTrue:[ |
|
863 |
colorType := 3. |
|
3461 | 864 |
samplesPerPixel > 1 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
2703 | 865 |
]. |
3100 | 866 |
colorType isNil ifTrue:[ |
3461 | 867 |
self error:'unhandled photometric' |
3100 | 868 |
]. |
2703 | 869 |
|
2683 | 870 |
self |
871 |
writeChunk: 'IHDR' |
|
872 |
size: 13 |
|
873 |
with:[ |
|
874 |
self writeLong:width. |
|
875 |
self writeLong:height. |
|
2703 | 876 |
outStream nextPut: 8. "Assume that all colors are 24bit" "/ depth |
877 |
outStream nextPut: colorType. |
|
2683 | 878 |
outStream nextPut: 0. "Compression" |
879 |
outStream nextPut: 0. "Filter method" |
|
880 |
outStream nextPut: 0 "Non-interlaced" |
|
881 |
] |
|
882 |
! |
|
883 |
||
2703 | 884 |
writeImageDataChunk |
2708 | 885 |
|compressedByteStream compressedBytes zlibWriter bytesPerScanline idx| |
2703 | 886 |
|
2708 | 887 |
compressedByteStream := WriteStream on:(ByteArray new:100). |
888 |
zlibWriter := ZipStream writeOpenAsZipStreamOn:compressedByteStream suppressHeaderAndChecksum:false. |
|
2703 | 889 |
zlibWriter binary. |
890 |
||
891 |
bytesPerScanline := self bytesPerRow. |
|
892 |
||
2708 | 893 |
idx := 1. |
894 |
0 to:height-1 do:[:y | |
|
895 |
|row| |
|
896 |
||
897 |
row := data copyFrom:idx to:(idx+bytesPerScanline-1). |
|
898 |
zlibWriter nextPutAll:#[0]. "/ no filter |
|
899 |
zlibWriter nextPutAll:row. |
|
900 |
idx := idx + bytesPerScanline. |
|
2703 | 901 |
]. |
2708 | 902 |
|
903 |
zlibWriter close. |
|
904 |
compressedBytes := compressedByteStream contents. |
|
905 |
||
906 |
self |
|
907 |
writeChunk: 'IDAT' |
|
908 |
size:(compressedBytes size) |
|
909 |
with:[ |
|
910 |
outStream nextPutAll:compressedBytes |
|
911 |
] |
|
2703 | 912 |
! |
913 |
||
2683 | 914 |
writePaletteChunk |
915 |
self |
|
916 |
writeChunk: 'PLTE' |
|
917 |
size: 3 * colorMap size |
|
918 |
with:[ |
|
2703 | 919 |
colorMap do:[:color | |
920 |
outStream |
|
921 |
nextPut: color redByte; |
|
922 |
nextPut: color greenByte; |
|
923 |
nextPut: color blueByte |
|
2683 | 924 |
] |
925 |
] |
|
926 |
! ! |
|
927 |
||
398 | 928 |
!PNGReader class methodsFor:'documentation'! |
308 | 929 |
|
930 |
version |
|
3554 | 931 |
^ '$Header$' |
2909 | 932 |
! |
933 |
||
934 |
version_CVS |
|
3554 | 935 |
^ '$Header$' |
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
936 |
! |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
937 |
|
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
938 |
version_HG |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
939 |
|
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
940 |
^ '$Changeset: <not expanded> $' |
308 | 941 |
! ! |
1671 | 942 |
|
3100 | 943 |
|
308 | 944 |
PNGReader initialize! |