author | HG Automerge |
Wed, 18 Jan 2017 10:05:30 +0000 | |
branch | jv |
changeset 3850 | 3e765b203652 |
parent 3797 | e17e95f379eb |
parent 3819 | 6cc53953e65c |
child 3854 | 4afd107bc911 |
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 |
3612 | 17 |
instanceVariableNames:'colorType bitsPerChannel depth compressionMethod filterMethod |
18 |
interlaceMode bytesPerScanline globalDataChunk thisScanline |
|
3762 | 19 |
prevScanline processTextChunks' |
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. |
3599 | 46 |
It is currenty unfinished (some interlaced image formats are unsupported, for example: grayscale+alpha) |
3437 | 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). |
|
3599 | 51 |
writer only generates unfiltered non-interlaced data |
2725 | 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 |
||
3762 | 216 |
type = 'iTXt' ifTrue:[^ self processITXTChunkLen:len]. |
217 |
type = 'iCCP' ifTrue:[^ self processICCPChunkLen:len]. |
|
218 |
||
308 | 219 |
('PNGReader: unrecognized chunk: ' , type , ' ignored.') infoPrintCR. |
220 |
||
221 |
inStream skip:len. |
|
222 |
^ true. |
|
223 |
||
224 |
"Created: 21.6.1996 / 21:10:37 / cg" |
|
225 |
"Modified: 21.6.1996 / 21:22:37 / cg" |
|
226 |
! |
|
227 |
||
228 |
processGAMAChunkLen:len |
|
229 |
inStream skip:len. |
|
230 |
^ true |
|
231 |
||
232 |
"Created: 21.6.1996 / 21:10:52 / cg" |
|
233 |
! |
|
234 |
||
2509 | 235 |
processGlobalIDATChunk |
236 |
interlaceMode == 0 ifTrue: [ |
|
237 |
^ self processNonInterlacedGlobalDATA |
|
238 |
]. |
|
239 |
^ self processInterlacedGlobalDATA |
|
240 |
! |
|
241 |
||
3762 | 242 |
processICCPChunkLen:len |
243 |
"/ ignored |
|
244 |
"/ inStream nextBytes:len. |
|
245 |
||
246 |
inStream skip:len. |
|
247 |
^ true |
|
248 |
||
249 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
250 |
! |
|
251 |
||
308 | 252 |
processIDATChunkLen:len |
1671 | 253 |
interlaceMode == 0 ifTrue: [ |
254 |
^ self processNonInterlacedDATA:len |
|
255 |
]. |
|
256 |
^ self processInterlacedDATA:len |
|
257 |
! |
|
258 |
||
3762 | 259 |
processIHDRChunkLen:len |
260 |
"/ ignored |
|
261 |
||
262 |
inStream skip:len. |
|
263 |
^ true |
|
264 |
||
265 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
266 |
! |
|
267 |
||
268 |
processITXTChunkLen:len |
|
269 |
"/ international (i.e.utf8) textual data. |
|
270 |
||
271 |
"/ currently ignored - not needed to display png images |
|
272 |
"/ |
|
273 |
"/ Keyword: 1-79 bytes (character string) |
|
274 |
"/ Null separator: 1 byte |
|
275 |
"/ Compression flag: 1 byte |
|
276 |
"/ Compression method: 1 byte |
|
277 |
"/ Language tag: 0 or more bytes (character string) |
|
278 |
"/ Null separator: 1 byte |
|
279 |
"/ Translated keyword: 0 or more bytes |
|
280 |
"/ Null separator: 1 byte |
|
281 |
"/ Text: 0 or more bytes |
|
282 |
||
283 |
"/ if needed, set processTextChunks to true somewhere... |
|
284 |
processTextChunks == true ifTrue:[ |
|
285 |
|chunkData keyword text i1 i2 i3 |
|
286 |
compressionFlag compressionMethod languageTag |
|
287 |
xlatedKeyword textBytes| |
|
288 |
||
289 |
chunkData := inStream nextBytes:len. |
|
290 |
i1 := chunkData indexOf:0. |
|
291 |
keyword := (chunkData copyTo:i1-1) asString. |
|
292 |
compressionFlag := chunkData at:i1+1. |
|
293 |
compressionMethod := chunkData at:i1+2. |
|
294 |
i2 := chunkData indexOf:0 startingAt:i1+3. |
|
295 |
languageTag := (chunkData copyFrom:i1+3 to:i2-1) asString. |
|
296 |
i3 := chunkData indexOf:0 startingAt:i2+1. |
|
297 |
xlatedKeyword := (chunkData copyFrom:i2+1 to:i3-1) asString. |
|
298 |
textBytes := chunkData copyFrom:i3+1. |
|
299 |
compressionFlag == 0 ifTrue:[ |
|
300 |
text := textBytes |
|
301 |
] ifFalse:[ |
|
302 |
compressionFlag == 1 ifTrue:[ |
|
303 |
"/ for now, only zlib compression is supported... |
|
304 |
'zlib compression currently unsupported' infoPrintCR. |
|
305 |
] ifFalse:[ |
|
306 |
'unsupported compression method' infoPrintCR. |
|
307 |
]. |
|
308 |
]. |
|
309 |
text := text utf8Decoded. |
|
310 |
self handleText:text keyword:keyword. |
|
311 |
] ifFalse:[ |
|
312 |
inStream skip:len. |
|
313 |
]. |
|
314 |
^ true |
|
315 |
||
316 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
317 |
! |
|
318 |
||
1671 | 319 |
processInterlacedDATA:len |
308 | 320 |
inStream skip:len. |
321 |
^ true |
|
1671 | 322 |
! |
308 | 323 |
|
2699 | 324 |
processInterlacedGlobalDATA |
3327 | 325 |
"adam7 interlace method" |
326 |
||
3612 | 327 |
| zlibReader filter |
328 |
bytesPerPass startingCol colIncrement rowIncrement |
|
329 |
startingRow |
|
330 |
temp "filtersSeen" |
|
331 |
cx "{ Class: SmallInteger }" |
|
332 |
sc "{ Class: SmallInteger }" |
|
333 |
cy "{ Class: SmallInteger }" |
|
334 |
startRow "{ Class: SmallInteger }" |
|
335 |
w "{ Class: SmallInteger }" |
|
336 |
h "{ Class: SmallInteger }" |
|
337 |
| |
|
2699 | 338 |
|
3327 | 339 |
interlaceMode == 1 ifFalse: [ |
340 |
self error:'unsupported interlaced mode'. |
|
341 |
^ self |
|
342 |
]. |
|
2699 | 343 |
|
344 |
"/ filtersSeen := Set new. |
|
345 |
||
346 |
startingCol := #(0 4 0 2 0 1 0 ). |
|
3327 | 347 |
startingRow := #(0 0 4 0 2 0 1 ). |
2699 | 348 |
colIncrement := #(8 8 4 4 2 2 1 ). |
349 |
rowIncrement := #(8 8 8 4 4 2 2 ). |
|
350 |
||
3819 | 351 |
zlibReader := ZipStream |
352 |
readOpenAsZipStreamOn:(globalDataChunk readStream) |
|
353 |
suppressHeaderAndChecksum:false. |
|
2699 | 354 |
zlibReader binary. |
355 |
||
3612 | 356 |
h := height. |
357 |
w := width. |
|
2699 | 358 |
1 to: 7 do: [:pass | |
359 |
(self doPass: pass) ifTrue:[ |
|
360 |
cx := colIncrement at: pass. |
|
361 |
sc := startingCol at: pass. |
|
3327 | 362 |
cy := rowIncrement at: pass. |
3612 | 363 |
bytesPerPass := (((w - sc + cx - 1) // cx * depth) + 7) // 8. |
2699 | 364 |
prevScanline := ByteArray new: bytesPerPass. |
365 |
thisScanline := ByteArray new: bytesPerScanline. |
|
3612 | 366 |
startRow := startingRow at: pass. |
367 |
startRow to: h-1 by: cy do: [:y | |
|
3343 | 368 |
filter := zlibReader nextByte. |
2699 | 369 |
"/ filtersSeen add: filter. |
370 |
(filter isNil or: [(filter between: 0 and: 4) not]) |
|
371 |
ifTrue: [^ self]. |
|
372 |
zlibReader next: bytesPerPass into: thisScanline startingAt: 1. |
|
373 |
filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerPass ]. |
|
374 |
self copyPixels:y at:sc by:cx. |
|
375 |
temp := prevScanline. |
|
376 |
prevScanline := thisScanline. |
|
377 |
thisScanline := temp. |
|
378 |
] |
|
379 |
] |
|
380 |
]. |
|
381 |
zlibReader atEnd ifFalse:[self error:'Unexpected data']. |
|
2909 | 382 |
|
383 |
"Modified: / 03-05-2011 / 12:03:33 / cg" |
|
2699 | 384 |
! |
385 |
||
1671 | 386 |
processNonInterlacedDATA:len |
2701 | 387 |
self halt:'not used' |
388 |
"/ |
|
389 |
"/ | zlibReader filter temp prevScanline thisScanline bytesPerScanline filtersSeen| |
|
390 |
"/ |
|
391 |
"/ zlibReader := ZipStream readOpenAsZipStreamOn:inStream suppressHeaderAndChecksum:false. |
|
392 |
"/ "/ zlibReader := ZLibReadStream on:inStream from: 1 to:len. |
|
393 |
"/ |
|
394 |
"/ prevScanline := ByteArray new: self bytesPerRow. |
|
395 |
"/ thisScanline := ByteArray new: self bytesPerRow. |
|
396 |
"/ 0 to: height - 1 do:[:index | |
|
397 |
"/ filter := (zlibReader next: 1) first. |
|
398 |
"/ filtersSeen add: filter. |
|
399 |
"/ (filter isNil or: [(filter between: 0 and: 4) not]) ifTrue: [^self]. |
|
400 |
"/ thisScanline := zlibReader next: bytesPerScanline into: thisScanline startingAt: 1. |
|
401 |
"/ self filterScanline: filter count: bytesPerScanline. |
|
402 |
"/ self copyPixels: index. |
|
403 |
"/ temp := prevScanline. |
|
404 |
"/ prevScanline := thisScanline. |
|
405 |
"/ thisScanline := temp |
|
406 |
"/ ] |
|
2509 | 407 |
! |
408 |
||
409 |
processNonInterlacedGlobalDATA |
|
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
410 |
| "data n" zlibReader filter temp bytesPerRow "filtersSeen" i| |
2696 | 411 |
|
412 |
"/ filtersSeen := Set new. |
|
2695 | 413 |
|
414 |
"/ data := ByteArray new:(self bytesPerRow * height)+1000. |
|
415 |
"/ n := ZipStream uncompress: globalDataChunk into: data. |
|
416 |
"/ self halt. |
|
2509 | 417 |
|
3819 | 418 |
zlibReader := ZipStream |
419 |
readOpenAsZipStreamOn:(globalDataChunk readStream) |
|
420 |
suppressHeaderAndChecksum:false. |
|
2696 | 421 |
zlibReader binary. |
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
422 |
bytesPerRow := self bytesPerRow. |
2509 | 423 |
|
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
424 |
prevScanline := ByteArray new: bytesPerRow. |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
425 |
thisScanline := ByteArray new: bytesPerRow. |
2725 | 426 |
0 to: height - 1 do:[:y | |
3343 | 427 |
filter := zlibReader nextByte. |
2696 | 428 |
"/ filtersSeen add: filter. |
2725 | 429 |
(filter notNil and: [filter between: 0 and: 4]) ifFalse: [ |
3673 | 430 |
'PNGReader: unsupported filter: ' infoPrint. filter infoPrintCR. |
2725 | 431 |
^ self |
432 |
]. |
|
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
433 |
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
|
434 |
filter ~~ 0 ifTrue:[ self filterScanline: filter count: bytesPerRow ]. |
2725 | 435 |
|
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
436 |
i := y * bytesPerRow. |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
437 |
data replaceFrom:i+1 to:(i+bytesPerRow) with:thisScanline startingAt:1. |
2725 | 438 |
|
2695 | 439 |
temp := prevScanline. |
440 |
prevScanline := thisScanline. |
|
441 |
thisScanline := temp |
|
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
442 |
]. |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
443 |
"/ 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
|
444 |
photometric == #rgba ifTrue:[ |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
445 |
| maskBits | |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
446 |
|
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
447 |
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
|
448 |
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
|
449 |
|
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
450 |
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
|
451 |
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
|
452 |
| alpha | |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
453 |
|
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
454 |
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
|
455 |
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
|
456 |
] |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
457 |
]. |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
458 |
mask bits: maskBits |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
459 |
]. |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
460 |
|
3642
79f73b8182e2
PNGReader: Remove obsolete wrong check for depth
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3619
diff
changeset
|
461 |
"Modified: / 11-04-2016 / 08:39:44 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
1671 | 462 |
! |
463 |
||
464 |
processPHYSChunkLen:len |
|
465 |
"physical pixel chunk - currently unhandled" |
|
466 |
||
2705 | 467 |
|pixelPerUnitX pixelPerUnitY unit| |
468 |
||
3554 | 469 |
pixelPerUnitX := inStream nextInt32MSB:true. |
470 |
pixelPerUnitY := inStream nextInt32MSB:true. |
|
2705 | 471 |
unit := inStream nextByte. |
472 |
||
2707 | 473 |
Verbose == true ifTrue:[ |
474 |
'PNGReader: PHYS chunk ignored:' infoPrintCR. |
|
475 |
'PNGReader: ppuX:' infoPrint. pixelPerUnitX infoPrintCR. |
|
476 |
'PNGReader: ppuY:' infoPrint. pixelPerUnitY infoPrintCR. |
|
477 |
'PNGReader: unit:' infoPrint. unit infoPrintCR. |
|
478 |
]. |
|
479 |
||
2705 | 480 |
^ true |
308 | 481 |
! |
482 |
||
483 |
processPLTEChunkLen:len |
|
1671 | 484 |
"read a color palette" |
485 |
||
2725 | 486 |
|n "{ Class: SmallInteger }" |
487 |
redBytes greenBytes blueBytes| |
|
308 | 488 |
|
489 |
(len \\ 3) ~~ 0 ifTrue:[ |
|
490 |
'PNGReader: invalid size of PLTE chunk' infoPrintCR. |
|
491 |
^ false |
|
492 |
]. |
|
493 |
n := len // 3. |
|
494 |
redBytes := ByteArray new:n. |
|
495 |
greenBytes := ByteArray new:n. |
|
496 |
blueBytes := ByteArray new:n. |
|
497 |
||
498 |
1 to:n do:[:i | |
|
499 |
redBytes at:i put:(inStream nextByte). |
|
500 |
greenBytes at:i put:(inStream nextByte). |
|
501 |
blueBytes at:i put:(inStream nextByte) |
|
502 |
]. |
|
503 |
||
2725 | 504 |
colorMap := MappedPalette |
505 |
redVector:redBytes |
|
506 |
greenVector:greenBytes |
|
507 |
blueVector:blueBytes. |
|
508 |
||
308 | 509 |
^ true |
510 |
||
511 |
"Created: 21.6.1996 / 21:22:28 / cg" |
|
512 |
"Modified: 21.6.1996 / 21:43:01 / cg" |
|
513 |
! |
|
514 |
||
515 |
processSBITChunkLen:len |
|
516 |
inStream skip:len. |
|
517 |
^ true |
|
518 |
||
519 |
"Created: 21.6.1996 / 21:13:09 / cg" |
|
520 |
! |
|
521 |
||
3762 | 522 |
processTEXTChunkLen:len |
523 |
"/ textual data in iso8859 coding. |
|
524 |
||
525 |
"/ currently ignored - not needed to display png images |
|
526 |
"/ Keyword: 1-79 bytes (character string) |
|
527 |
"/ Null separator: 1 byte |
|
528 |
"/ Text: n bytes (character string) |
|
529 |
||
530 |
"/ if needed, set processTextChunks to true somewhere... |
|
531 |
processTextChunks == true ifTrue:[ |
|
532 |
|chunkData keyword text i| |
|
533 |
||
534 |
chunkData := inStream nextBytes:len. |
|
535 |
i := chunkData indexOf:0. |
|
536 |
keyword := (chunkData copyTo:i-1) asString. |
|
537 |
text := (chunkData copyFrom:i+1) asString. |
|
538 |
self handleText:text keyword:keyword. |
|
539 |
] ifFalse:[ |
|
540 |
inStream skip:len. |
|
541 |
]. |
|
308 | 542 |
^ true |
543 |
||
544 |
"Modified: 21.6.1996 / 21:15:27 / cg" |
|
545 |
! |
|
546 |
||
547 |
processTIMEChunkLen:len |
|
548 |
inStream skip:len. |
|
549 |
^ true |
|
550 |
||
551 |
"Created: 21.6.1996 / 21:15:43 / cg" |
|
552 |
"Modified: 21.6.1996 / 21:20:42 / cg" |
|
553 |
! |
|
554 |
||
2707 | 555 |
processTRNSChunkLen:len |
556 |
inStream skip:len. |
|
557 |
^ true |
|
558 |
! |
|
559 |
||
308 | 560 |
processZTXTChunkLen:len |
3762 | 561 |
"/ compressed text |
562 |
||
563 |
"/ currently ignored - not needed to display png images |
|
308 | 564 |
inStream skip:len. |
565 |
^ true |
|
566 |
||
567 |
"Created: 21.6.1996 / 21:15:58 / cg" |
|
568 |
! ! |
|
569 |
||
2696 | 570 |
!PNGReader methodsFor:'private-filtering'! |
571 |
||
2909 | 572 |
filterAverage:count |
2699 | 573 |
"Use the average of the pixel to the left and the pixel above as a predictor" |
2909 | 574 |
|
575 |
|delta| |
|
2699 | 576 |
|
3609 | 577 |
%{ |
578 |
if (__isByteArray(__INST(thisScanline)) |
|
579 |
&& __isByteArray(__INST(prevScanline)) |
|
580 |
&& __isSmallInteger(__INST(depth)) |
|
581 |
&& __isSmallInteger(count)) { |
|
582 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
583 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
584 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
585 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
586 |
INT __count = __intVal(count); |
|
587 |
INT __delta = __intVal(__INST(depth)) / 8; |
|
588 |
int __i; |
|
589 |
||
590 |
if (__delta < 1) __delta = 1; |
|
591 |
for (__i=0; __i<__delta; __i++) { |
|
592 |
__thisScanline[__i] += (__prevScanline[__i] >> 1); |
|
593 |
} |
|
594 |
for (; __i < __count; __i++) { |
|
595 |
__thisScanline[__i] += ((__prevScanline[__i] + __thisScanline[__i-__delta]) >> 1); |
|
596 |
} |
|
597 |
RETURN(self); |
|
598 |
} |
|
599 |
%}. |
|
600 |
||
601 |
delta := depth // 8 max:1. |
|
2909 | 602 |
1 to:delta do:[:i | |
3384 | 603 |
thisScanline at:i put:((thisScanline at:i) + ((prevScanline at:i) // 2) bitAnd:255) |
2909 | 604 |
]. |
605 |
delta + 1 to:count do:[:i | |
|
3384 | 606 |
thisScanline at:i put:((thisScanline at:i) + (((prevScanline at:i) + (thisScanline at:i - delta)) // 2) bitAnd:255) |
2909 | 607 |
] |
608 |
||
609 |
"Modified: / 03-05-2011 / 12:14:01 / cg" |
|
2699 | 610 |
! |
611 |
||
2696 | 612 |
filterHorizontal:count |
613 |
"use the pixel to the left as a predictor" |
|
614 |
||
615 |
|delta| |
|
616 |
||
3608 | 617 |
%{ |
618 |
if (__isByteArray(__INST(thisScanline)) |
|
619 |
&& __isByteArray(__INST(prevScanline)) |
|
620 |
&& __isSmallInteger(__INST(depth)) |
|
621 |
&& __isSmallInteger(count)) { |
|
622 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
623 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
624 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
625 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
626 |
INT __count = __intVal(count); |
|
627 |
INT __delta = __intVal(__INST(depth)) / 8; |
|
628 |
int __i; |
|
629 |
||
630 |
if (__delta < 1) __delta = 1; |
|
631 |
for (__i = __delta; __i < __count; __i++) { |
|
632 |
__thisScanline[__i] = __thisScanline[__i] + __thisScanline[__i-__delta]; |
|
633 |
} |
|
634 |
RETURN(self); |
|
635 |
} |
|
636 |
%}. |
|
637 |
||
638 |
delta := depth // 8 max:1. |
|
2696 | 639 |
delta+1 to:count do:[:i| |
640 |
thisScanline at:i put:(((thisScanline at:i)+ (thisScanline at:i-delta)) bitAnd:255) |
|
641 |
] |
|
642 |
! |
|
643 |
||
2699 | 644 |
filterNone: count |
2909 | 645 |
"/ no filter - scanline is as is |
646 |
||
647 |
"Modified: / 03-05-2011 / 12:14:20 / cg" |
|
2699 | 648 |
! |
649 |
||
2909 | 650 |
filterPaeth:count |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
651 |
"Select one of (the pixel to the left, the pixel above and the pixel to above left) to |
2909 | 652 |
predict the value of this pixel" |
653 |
||
654 |
|delta| |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
655 |
|
3607 | 656 |
%{ |
657 |
if (__isByteArray(__INST(thisScanline)) |
|
658 |
&& __isByteArray(__INST(prevScanline)) |
|
659 |
&& __isSmallInteger(__INST(depth)) |
|
660 |
&& __isSmallInteger(count)) { |
|
661 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
662 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
663 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
664 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
665 |
INT __count = __intVal(count); |
|
666 |
INT __delta = __intVal(__INST(depth)) / 8; |
|
667 |
int __i; |
|
668 |
||
669 |
if (__delta < 1) __delta = 1; |
|
670 |
for (__i=0; __i<__delta; __i++) { |
|
671 |
unsigned int __pix = __thisScanline[__i] + __prevScanline[__i]; |
|
672 |
__thisScanline[__i] = __pix; |
|
673 |
} |
|
674 |
for (; __i < __count; __i++) { |
|
675 |
int __pCenter = __thisScanline[__i]; |
|
676 |
int __pLeft = __thisScanline[__i - __delta]; |
|
677 |
int __pAbove = __prevScanline[__i]; |
|
678 |
int __pAboveLeft = __prevScanline[__i - __delta]; |
|
679 |
int __pa, __pb, __pc; |
|
680 |
int __p; |
|
681 |
||
682 |
__pa = (__pAbove - __pAboveLeft); |
|
683 |
__pa = __pa < 0 ? -__pa : __pa; |
|
684 |
__pb = (__pLeft - __pAboveLeft); |
|
685 |
__pb = __pb < 0 ? -__pb : __pb; |
|
686 |
__pc = __pLeft + __pAbove - __pAboveLeft - __pAboveLeft; |
|
687 |
__pc = __pc < 0 ? -__pc : __pc; |
|
688 |
if ((__pa <= __pb) && (__pa <= __pc)) __p = __pLeft; |
|
689 |
else if (__pb <= __pc) __p = __pAbove; |
|
690 |
else __p = __pAboveLeft; |
|
691 |
||
692 |
__thisScanline[__i] = __pCenter + __p; |
|
693 |
} |
|
694 |
RETURN(self); |
|
695 |
} |
|
696 |
%}. |
|
697 |
||
698 |
delta := depth // 8 max:1. |
|
2909 | 699 |
1 to:delta do:[:i | |
700 |
thisScanline |
|
701 |
at:i |
|
702 |
put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255) |
|
703 |
]. |
|
704 |
delta + 1 to:count do:[:i | |
|
705 |
thisScanline |
|
706 |
at:i |
|
707 |
put:(((thisScanline at:i) + (self |
|
708 |
paethPredictLeft:(thisScanline at:i - delta) |
|
709 |
above:(prevScanline at:i) |
|
710 |
aboveLeft:(prevScanline at:i - delta))) |
|
711 |
bitAnd:255) |
|
712 |
] |
|
713 |
||
714 |
"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
|
715 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
716 |
|
2696 | 717 |
filterScanline:filterType count:count |
3612 | 718 |
filterType == 0 ifTrue:[^ self]. |
719 |
||
2696 | 720 |
self |
3612 | 721 |
perform:(#(filterHorizontal: filterVertical: filterAverage: filterPaeth:) at:filterType) |
2696 | 722 |
with:count |
2909 | 723 |
|
724 |
"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
|
725 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
726 |
|
2909 | 727 |
filterVertical:count |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
728 |
"Use the pixel above as a predictor" |
2909 | 729 |
|
3608 | 730 |
%{ |
731 |
if (__isByteArray(__INST(thisScanline)) |
|
732 |
&& __isByteArray(__INST(prevScanline)) |
|
733 |
&& __isSmallInteger(count)) { |
|
734 |
unsigned char *__thisScanline = __byteArrayVal(__INST(thisScanline)); |
|
735 |
unsigned int __sz_this = __byteArraySize(__INST(thisScanline)); |
|
736 |
unsigned char *__prevScanline = __byteArrayVal(__INST(prevScanline)); |
|
737 |
unsigned int __sz_prev = __byteArraySize(__INST(prevScanline)); |
|
738 |
INT __count = __intVal(count); |
|
739 |
int __i; |
|
740 |
||
741 |
for (__i=0; __i<__count; __i++) { |
|
742 |
unsigned int __pix = __thisScanline[__i] + __prevScanline[__i]; |
|
743 |
__thisScanline[__i] = __pix; |
|
744 |
} |
|
745 |
RETURN(self); |
|
746 |
} |
|
747 |
%}. |
|
748 |
||
2909 | 749 |
1 to:count do:[:i | |
750 |
thisScanline |
|
751 |
at:i |
|
752 |
put:(((thisScanline at:i) + (prevScanline at:i)) bitAnd:255) |
|
753 |
] |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
754 |
|
2909 | 755 |
"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
|
756 |
! |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
757 |
|
3607 | 758 |
paethPredictLeft: l above: a aboveLeft: al |
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
759 |
"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
|
760 |
|
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
761 |
| pa pb pc | |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
762 |
|
3607 | 763 |
pa := a > al ifTrue: [a - al] ifFalse: [al - a]. |
764 |
pb := l > al ifTrue: [l - al] ifFalse: [al - l]. |
|
765 |
pc := l + a - al - al. |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
766 |
pc < 0 ifTrue: [ |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
767 |
pc := pc * -1 |
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
768 |
]. |
3607 | 769 |
((pa <= pb) and: [pa <= pc]) ifTrue: [^ l]. |
770 |
(pb <= pc) ifTrue: [^ a]. |
|
771 |
^ al |
|
2696 | 772 |
! ! |
773 |
||
2699 | 774 |
!PNGReader methodsFor:'private-pixel copy'! |
775 |
||
2909 | 776 |
copyPixels:y at:startX by:incX |
2704 | 777 |
"Handle interlaced pixels of supported colorTypes" |
2909 | 778 |
|
779 |
|s| |
|
2699 | 780 |
|
3598 | 781 |
"/ per color type copy methods |
782 |
s := #( |
|
783 |
#copyPixelsGray:at:by: "/ 0 |
|
2909 | 784 |
nil |
3598 | 785 |
#copyPixelsRGB:at:by: "/ 2 |
786 |
#copyPixelsIndexed:at:by: "/ 3 |
|
787 |
#copyPixelsGrayAlpha:at:by: "/ 4 |
|
2909 | 788 |
nil |
3598 | 789 |
#copyPixelsRGBA:at:by: "/ 6 |
2909 | 790 |
) at:colorType + 1. |
3437 | 791 |
self perform:s with:y with:startX with:incX |
1848 | 792 |
|
2909 | 793 |
"Modified: / 03-05-2011 / 12:02:45 / cg" |
3327 | 794 |
! |
795 |
||
3598 | 796 |
copyPixelsGray:y at:startX by:incX |
797 |
"Handle interlaced pixels of supported colorTypes. |
|
798 |
Untested code - please verify" |
|
799 |
||
3611 | 800 |
|srcIndex "{ Class: SmallInteger }" |
801 |
srcMask "{ Class: SmallInteger }" |
|
802 |
nPixels "{ Class: SmallInteger }" |
|
803 |
dstIndex "{ Class: SmallInteger }" |
|
804 |
dstMask "{ Class: SmallInteger }" |
|
805 |
x "{ Class: SmallInteger }" |
|
806 |
bits "{ Class: SmallInteger }" |
|
807 |
bitMask "{ Class: SmallInteger }" |
|
808 |
rS "{ Class: SmallInteger }" |
|
809 |
lS "{ Class: SmallInteger }" |
|
810 |
rowIndex "{ Class: SmallInteger }"| |
|
3598 | 811 |
|
812 |
nPixels := width // incX. |
|
813 |
srcIndex := 0. srcMask := 0. x := startX. |
|
814 |
||
815 |
depth == 1 ifTrue:[ |
|
816 |
dstIndex := (y * bytesPerScanline) + (startX // 8) + 1. |
|
817 |
dstMask := 16r80 >> (x \\ 8). |
|
818 |
||
819 |
1 to:nPixels do:[:cnt | |
|
820 |
srcMask == 0 ifTrue:[ |
|
821 |
srcMask := 16r80. |
|
822 |
srcIndex := srcIndex + 1. |
|
823 |
bits := thisScanline at:srcIndex. |
|
824 |
]. |
|
825 |
bitMask := (bits bitAnd:srcMask) == 0 ifTrue:[0] ifFalse:[dstMask]. |
|
826 |
bitMask ~~ 0 ifTrue:[ |
|
827 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
828 |
]. |
|
829 |
x := x + incX. |
|
830 |
dstMask := dstMask >> incX. |
|
831 |
dstMask == 0 ifTrue:[ |
|
832 |
dstIndex := dstIndex + 1. |
|
833 |
dstMask := 16r80 >> (x \\ 8). |
|
834 |
]. |
|
835 |
srcMask := srcMask bitShift:-1. |
|
836 |
]. |
|
837 |
^ self. |
|
838 |
]. |
|
839 |
||
840 |
depth == 2 ifTrue:[ |
|
841 |
dstIndex := (y * bytesPerScanline) + (startX // 4) + 1. |
|
842 |
lS := 6 - ((x \\ 4) * 2). |
|
843 |
rS := -1. |
|
844 |
1 to:nPixels do:[:cnt | |
|
845 |
rS < 0 ifTrue:[ |
|
846 |
srcIndex := srcIndex + 1. |
|
847 |
bits := thisScanline at:srcIndex. |
|
848 |
rS := 6. |
|
849 |
]. |
|
850 |
bitMask := ((bits >> rS) bitAnd:2r11). |
|
851 |
bitMask ~~ 0 ifTrue:[ |
|
852 |
bitMask := bitMask << lS. |
|
853 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
854 |
]. |
|
855 |
lS := lS - (incX * 2). |
|
856 |
lS < 0 ifTrue:[ |
|
857 |
lS <= -8 ifTrue:[ |
|
858 |
dstIndex := dstIndex + 2. |
|
859 |
lS := lS + 8 + 8. |
|
860 |
] ifFalse:[ |
|
861 |
dstIndex := dstIndex + 1. |
|
862 |
lS := lS + 8. |
|
863 |
]. |
|
864 |
]. |
|
865 |
rS := rS - 2. |
|
866 |
]. |
|
867 |
^ self. |
|
868 |
]. |
|
869 |
||
870 |
depth == 4 ifTrue:[ |
|
871 |
rowIndex := (y * bytesPerScanline) + 1. |
|
872 |
dstIndex := rowIndex + (x // 2). |
|
873 |
rS := -1. |
|
874 |
1 to:nPixels do:[:cnt | |
|
875 |
rS < 0 ifTrue:[ |
|
876 |
srcIndex := srcIndex + 1. |
|
877 |
bits := thisScanline at:srcIndex. |
|
878 |
rS := 4. |
|
879 |
]. |
|
880 |
bitMask := ((bits >> rS) bitAnd:2r1111). |
|
881 |
x even ifTrue:[ |
|
882 |
bitMask := bitMask bitShift:4. |
|
883 |
]. |
|
884 |
data at:dstIndex put:((data at:dstIndex) bitOr:bitMask). |
|
885 |
x := x + incX. |
|
886 |
dstIndex := rowIndex + (x // 2). |
|
887 |
rS := rS - 4. |
|
888 |
]. |
|
889 |
^ self. |
|
890 |
]. |
|
891 |
||
892 |
depth == 8 ifTrue:[ |
|
893 |
srcIndex := 1. |
|
894 |
dstIndex := (y * bytesPerScanline) + (startX) + 1. |
|
895 |
||
896 |
1 to:nPixels do:[:n | |
|
897 |
data at:dstIndex put:(thisScanline at:srcIndex). |
|
898 |
srcIndex := srcIndex + 1. |
|
899 |
dstIndex := dstIndex + incX |
|
900 |
]. |
|
901 |
^ self. |
|
902 |
]. |
|
903 |
||
904 |
depth == 16 ifTrue:[ |
|
905 |
srcIndex := 1. |
|
906 |
dstIndex := (y * bytesPerScanline) + (startX*2) + 1. |
|
907 |
||
908 |
1 to:nPixels do:[:n | |
|
3601 | 909 |
data unsignedInt16At:dstIndex put:(thisScanline unsignedInt16At:srcIndex). |
3598 | 910 |
srcIndex := srcIndex + 2. |
911 |
dstIndex := dstIndex + (incX * 2) |
|
912 |
]. |
|
913 |
^ self. |
|
914 |
]. |
|
915 |
self error:'unsupported depth'. |
|
916 |
! |
|
917 |
||
918 |
copyPixelsIndexed:y at:startX by:incX |
|
919 |
self copyPixelsGray:y at:startX by:incX |
|
920 |
! |
|
921 |
||
922 |
copyPixelsRGB:y at:startX by:incX |
|
923 |
"Handle interlaced pixels of supported colorTypes. |
|
924 |
Untested code - please verify" |
|
925 |
||
3611 | 926 |
|srcIndex "{ Class: SmallInteger }" |
927 |
nPixels "{ Class: SmallInteger }" |
|
928 |
dstIndex "{ Class: SmallInteger }" |
|
929 |
dstInc "{ Class: SmallInteger }" |
|
930 |
bpp "{ Class: SmallInteger }"| |
|
3598 | 931 |
|
932 |
bpp := bytesPerScanline // width. |
|
933 |
||
934 |
"/ 1 byte per r,g,b |
|
935 |
srcIndex := 1. |
|
936 |
dstIndex := (y * bytesPerScanline) + (startX * bpp) + 1. |
|
937 |
dstInc := incX * bpp. |
|
938 |
||
939 |
nPixels := width // incX. |
|
940 |
1 to:nPixels do:[:n | |
|
941 |
data replaceFrom:dstIndex to:dstIndex+bpp-1 with:thisScanline startingAt:srcIndex. |
|
942 |
srcIndex := srcIndex + bpp. |
|
943 |
dstIndex := dstIndex + dstInc |
|
944 |
]. |
|
945 |
! |
|
946 |
||
3327 | 947 |
copyPixelsRGBA:y at:startX by:incX |
948 |
"Handle interlaced pixels of supported colorTypes. |
|
949 |
Untested code - please verify" |
|
950 |
||
3611 | 951 |
|srcIndex "{ Class: SmallInteger }" |
952 |
nPixels "{ Class: SmallInteger }" |
|
953 |
dstIndex "{ Class: SmallInteger }" |
|
954 |
dstInc "{ Class: SmallInteger }"| |
|
3327 | 955 |
|
956 |
srcIndex := 1. |
|
3437 | 957 |
dstIndex := (y * bytesPerScanline) + (startX * 4) + 1. |
958 |
dstInc := incX * 4. |
|
3327 | 959 |
|
960 |
nPixels := thisScanline size. |
|
961 |
[srcIndex < nPixels] whileTrue:[ |
|
3437 | 962 |
data replaceFrom:dstIndex to:dstIndex+3 with:thisScanline startingAt:srcIndex. |
3327 | 963 |
srcIndex := srcIndex + 4. |
3437 | 964 |
dstIndex := dstIndex + dstInc |
3327 | 965 |
]. |
2699 | 966 |
! ! |
967 |
||
968 |
!PNGReader methodsFor:'private-reading'! |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
969 |
|
2683 | 970 |
getChunk |
971 |
|len type crc| |
|
972 |
||
973 |
inStream atEnd ifTrue:[^ false]. |
|
974 |
||
3554 | 975 |
len := inStream nextInt32MSB:true. |
2683 | 976 |
type := String new:4. |
977 |
(inStream nextBytes:4 into:type) ~~ 4 ifTrue:[^ false]. |
|
978 |
||
2707 | 979 |
Verbose == true ifTrue:[ |
980 |
'len: ' infoPrint. len infoPrint. ' type: ' infoPrint. type infoPrintCR. |
|
981 |
]. |
|
2683 | 982 |
|
983 |
type = 'IEND' ifTrue:[^ false]. |
|
984 |
||
985 |
(self processChunk:type len:len) ifFalse:[^ false]. |
|
986 |
||
3554 | 987 |
crc := inStream nextInt32MSB:true. "/ ignored - for now |
2683 | 988 |
^ true |
989 |
||
990 |
"Created: 21.6.1996 / 21:09:36 / cg" |
|
991 |
"Modified: 21.6.1996 / 21:20:26 / cg" |
|
992 |
! |
|
993 |
||
994 |
getIHDRChunk |
|
995 |
|len type crc| |
|
996 |
||
3554 | 997 |
len := inStream nextInt32MSB:true. |
2683 | 998 |
|
999 |
type := String new:4. |
|
1000 |
inStream nextBytes:4 into:type. |
|
1001 |
||
3183 | 1002 |
type = 'IHDR' ifFalse:[self halt:'expected IHDR magic'. ^ false]. |
1003 |
len == 13 ifFalse:[self halt:'unexpected IHDR length'. ^ false]. |
|
2683 | 1004 |
|
3554 | 1005 |
width := inStream nextInt32MSB:true. |
1006 |
height := inStream nextInt32MSB:true. |
|
2683 | 1007 |
|
1008 |
(width <= 0 or:[height <= 0]) ifTrue:[ |
|
1009 |
'PNGReader: invalid dimension(s)' infoPrintCR. |
|
1010 |
^ false. |
|
1011 |
]. |
|
1012 |
self reportDimension. |
|
1013 |
||
3607 | 1014 |
bitsPerChannel := inStream nextByte. |
1015 |
depth := bitsPerChannel. "/ will be changed by setColorType. |
|
2683 | 1016 |
colorType := inStream nextByte. |
1017 |
||
1018 |
compressionMethod := inStream nextByte. |
|
1019 |
filterMethod := inStream nextByte. |
|
1020 |
interlaceMode := inStream nextByte. |
|
1021 |
||
2707 | 1022 |
Verbose == true ifTrue:[ |
1023 |
'PNGReader: IHDR:' infoPrintCR. |
|
1024 |
'PNGReader: width: ' infoPrint. width infoPrintCR. |
|
1025 |
'PNGReader: height: ' infoPrint. height infoPrintCR. |
|
3607 | 1026 |
'PNGReader: bitsPerChannel: ' infoPrint. bitsPerChannel infoPrintCR. |
2707 | 1027 |
'PNGReader: colorType: ' infoPrint. colorType infoPrintCR. |
1028 |
'PNGReader: compressionMethod: ' infoPrint. compressionMethod infoPrintCR. |
|
1029 |
'PNGReader: filterMethod: ' infoPrint. filterMethod infoPrintCR. |
|
1030 |
'PNGReader: interlaceMode: ' infoPrint. interlaceMode infoPrintCR. |
|
1031 |
]. |
|
1032 |
||
3554 | 1033 |
crc := inStream nextInt32MSB:true. |
2683 | 1034 |
^ true |
1035 |
||
1036 |
"Modified: 21.6.1996 / 21:38:35 / cg" |
|
1037 |
! |
|
1038 |
||
3762 | 1039 |
handleText:text keyword:keyword |
1040 |
"/ things like exif data etc. |
|
1041 |
"/ self halt. |
|
1042 |
||
1043 |
"/Standard keywords for text chunks: |
|
1044 |
"/ |
|
1045 |
"/ Title Short (one line) title or caption for image |
|
1046 |
"/ Author Name of image's creator |
|
1047 |
"/ Description Description of image (possibly long) |
|
1048 |
"/ Copyright Copyright notice |
|
1049 |
"/ Creation Time Time of original image creation |
|
1050 |
"/ Software Software used to create the image |
|
1051 |
"/ Disclaimer Legal disclaimer |
|
1052 |
"/ Warning Warning of nature of content |
|
1053 |
"/ Source Device used to create the image |
|
1054 |
"/ Comment Miscellaneous comment; conversion from |
|
1055 |
"/ GIF comment |
|
1056 |
! |
|
1057 |
||
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
1058 |
setColorType:pngColorType |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
1059 |
pngColorType == 0 ifTrue:[ |
1848 | 1060 |
photometric := #blackIs0. |
1061 |
samplesPerPixel := 1. |
|
3607 | 1062 |
bitsPerSample := Array with:bitsPerChannel. |
1063 |
depth := bitsPerChannel. |
|
1848 | 1064 |
^ true. |
1065 |
]. |
|
1066 |
||
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
1067 |
pngColorType == 2 ifTrue:[ |
3607 | 1068 |
bitsPerChannel < 8 ifTrue:[ |
1848 | 1069 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
1070 |
^ false. |
|
1071 |
]. |
|
1072 |
photometric := #rgb. |
|
1073 |
samplesPerPixel := 3. |
|
3607 | 1074 |
bitsPerSample := Array with:bitsPerChannel with:bitsPerChannel with:bitsPerChannel. |
1075 |
depth := bitsPerChannel * 3. |
|
1848 | 1076 |
^ true. |
1077 |
]. |
|
1078 |
||
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
1079 |
pngColorType == 3 ifTrue:[ |
3607 | 1080 |
bitsPerChannel == 16 ifTrue:[ |
1848 | 1081 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
1082 |
^ false. |
|
1083 |
]. |
|
1084 |
photometric := #palette. |
|
1085 |
samplesPerPixel := 1. |
|
3607 | 1086 |
bitsPerSample := Array with:bitsPerChannel. |
1087 |
depth := bitsPerChannel. |
|
1848 | 1088 |
^ true. |
1089 |
]. |
|
1090 |
||
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
1091 |
pngColorType == 4 ifTrue:[ |
3607 | 1092 |
bitsPerChannel < 8 ifTrue:[ |
1848 | 1093 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
1094 |
^ false. |
|
1095 |
]. |
|
1096 |
photometric := #blackIs0. |
|
1097 |
samplesPerPixel := 2. |
|
3607 | 1098 |
bitsPerSample := Array with:bitsPerChannel with:bitsPerChannel. |
1099 |
depth := bitsPerChannel * 2. |
|
1848 | 1100 |
^ true. |
1101 |
]. |
|
1102 |
||
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
1103 |
pngColorType == 6 ifTrue:[ |
3607 | 1104 |
bitsPerChannel < 8 ifTrue:[ |
1848 | 1105 |
'PNGReader: invalid colorType/depth combination' infoPrintCR. |
1106 |
^ false. |
|
1107 |
]. |
|
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
1108 |
photometric := #rgba. |
1848 | 1109 |
samplesPerPixel := 4. |
3607 | 1110 |
bitsPerSample := Array with:bitsPerChannel with:bitsPerChannel with:bitsPerChannel with:bitsPerChannel. |
1111 |
depth := bitsPerChannel * 4. |
|
1848 | 1112 |
^ true. |
1113 |
]. |
|
1114 |
||
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
1115 |
('PNGReader: invalid colorType: ' , pngColorType printString , '.') infoPrintCR. |
1848 | 1116 |
^ false |
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
1117 |
|
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
1118 |
"Modified: / 01-09-2015 / 16:02:38 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
1848 | 1119 |
! ! |
1120 |
||
1805 | 1121 |
!PNGReader methodsFor:'reading'! |
308 | 1122 |
|
1123 |
fromStream:aStream |
|
1124 |
"read a stream containing a PNG image. |
|
1125 |
Leave image description in instance variables." |
|
1126 |
||
1127 |
|header| |
|
1128 |
||
1129 |
inStream := aStream. |
|
1130 |
aStream binary. |
|
1131 |
||
1671 | 1132 |
"PNG-files are always msb (network-world)" |
308 | 1133 |
byteOrder := #msb. |
1134 |
||
1135 |
header := ByteArray new:8. |
|
1136 |
aStream nextBytes:8 into:header. |
|
1137 |
||
2683 | 1138 |
header ~= (self pngHeader) ifTrue:[ |
308 | 1139 |
'PNGReader: not a png file.' infoPrintCR. |
3598 | 1140 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: not a png file'. |
308 | 1141 |
^ nil |
1142 |
]. |
|
2695 | 1143 |
|
308 | 1144 |
(self getIHDRChunk) ifFalse:[ |
1145 |
'PNGReader: required IHDR chunk missing.' infoPrintCR. |
|
3598 | 1146 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: required IHDR chunk missing'. |
308 | 1147 |
^ nil |
1148 |
]. |
|
1149 |
||
1150 |
compressionMethod ~~ 0 ifTrue:[ |
|
3598 | 1151 |
('PNGReader: compressionMethod %s not supported.' printfWith:compressionMethod printString) infoPrintCR. |
1152 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: unsupported compression method'. |
|
308 | 1153 |
^ nil |
1154 |
]. |
|
1155 |
filterMethod > 0 ifTrue:[ |
|
1156 |
'PNGReader: invalid filterMethod' infoPrintCR. |
|
3598 | 1157 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: invalid/unsupported filterMethod'. |
308 | 1158 |
^ nil. |
1159 |
]. |
|
1160 |
interlaceMode > 1 ifTrue:[ |
|
3598 | 1161 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: invalid/unsupported interlaceMode'. |
308 | 1162 |
'PNGReader: invalid interlaceMode' infoPrintCR. |
1163 |
^ nil. |
|
1164 |
]. |
|
1671 | 1165 |
(self setColorType:colorType) ifFalse:[ |
308 | 1166 |
^ nil |
1671 | 1167 |
]. |
2706 | 1168 |
|
1671 | 1169 |
[self getChunk] whileTrue. |
308 | 1170 |
|
3598 | 1171 |
bytesPerScanline := self bytesPerRow. |
1172 |
data := ByteArray new:(bytesPerScanline * height). |
|
2697
8e8223bf25af
more stuff added (but still not complete)
Claus Gittinger <cg@exept.de>
parents:
2696
diff
changeset
|
1173 |
|
2509 | 1174 |
globalDataChunk notNil ifTrue:[ |
3343 | 1175 |
self processGlobalIDATChunk. |
1176 |
globalDataChunk := nil. |
|
2509 | 1177 |
]. |
1178 |
||
308 | 1179 |
photometric == #palette ifTrue:[ |
2725 | 1180 |
colorMap isNil ifTrue:[ |
3598 | 1181 |
Image badImageFormatQuerySignal raiseRequestErrorString:'PNGReader: palette chunk missing'. |
1182 |
'PNGReader: palette chunk missing.' infoPrintCR. |
|
2725 | 1183 |
"/ ^ nil |
308 | 1184 |
]. |
1185 |
]. |
|
1186 |
||
1187 |
" |
|
1188 |
PNGReader fromFile:'/home/cg/libpng-0.89c/pngtest.png' |
|
1189 |
" |
|
1190 |
||
2910
d5171b9245e6
comment/format in: #fromStream:
Claus Gittinger <cg@exept.de>
parents:
2909
diff
changeset
|
1191 |
"Modified: / 03-05-2011 / 12:17:34 / cg" |
1805 | 1192 |
! ! |
1193 |
||
2683 | 1194 |
!PNGReader methodsFor:'writing to file'! |
1195 |
||
1196 |
pngHeader |
|
1197 |
^ #[137 80 78 71 13 10 26 10] |
|
1198 |
! |
|
1199 |
||
1200 |
save:image onStream:aStream |
|
1201 |
"save image in PNG-file-format onto aStream" |
|
1202 |
||
1203 |
outStream := aStream. |
|
1204 |
outStream binary. |
|
1205 |
||
2703 | 1206 |
byteOrder := #msb. |
2683 | 1207 |
width := image width. |
1208 |
height := image height. |
|
1209 |
photometric := image photometric. |
|
1210 |
samplesPerPixel := image samplesPerPixel. |
|
1211 |
bitsPerSample := image bitsPerSample. |
|
1212 |
colorMap := image colorMap. |
|
1213 |
data := image bits. |
|
1214 |
||
2703 | 1215 |
outStream nextPutAll:(self pngHeader). |
1216 |
self writeIHDRChunk. |
|
1217 |
photometric == #palette ifTrue: [self writePaletteChunk]. |
|
1218 |
self writeImageDataChunk. |
|
2683 | 1219 |
self writeEndChunk |
1220 |
! |
|
1221 |
||
2703 | 1222 |
writeChunk:chunkTypeChars size:len with:aBlock |
2708 | 1223 |
|crc realOutStream chunkBytes| |
1224 |
||
1225 |
realOutStream := outStream. |
|
1226 |
outStream := WriteStream on:(ByteArray new:len). |
|
1227 |
||
2703 | 1228 |
outStream nextPutBytes:4 from:chunkTypeChars startingAt:1. |
1229 |
aBlock value. |
|
2708 | 1230 |
|
1231 |
chunkBytes := outStream contents. |
|
1232 |
crc := CRC32Stream hashValueOf:chunkBytes. |
|
1233 |
||
1234 |
outStream := realOutStream. |
|
1235 |
||
3554 | 1236 |
realOutStream nextPutInt32:len MSB:true. |
2708 | 1237 |
realOutStream nextPutAll:chunkBytes. |
3554 | 1238 |
realOutStream nextPutInt32:crc MSB:true. |
2703 | 1239 |
! |
2683 | 1240 |
|
2703 | 1241 |
writeEndChunk |
1242 |
self |
|
1243 |
writeChunk: 'IEND' |
|
1244 |
size: 0 |
|
1245 |
with:[ ] |
|
2683 | 1246 |
! |
1247 |
||
1248 |
writeFileHeader |
|
1249 |
outStream nextPutAll:(self pngHeader) |
|
1250 |
! |
|
1251 |
||
2703 | 1252 |
writeIHDRChunk |
3100 | 1253 |
((photometric == #whiteIs0) or:[ (photometric == #blackIs0)]) ifTrue:[ |
2703 | 1254 |
colorType := 0. |
3461 | 1255 |
samplesPerPixel > 1 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
2703 | 1256 |
]. |
3724 | 1257 |
((photometric == #rgb) or:[(photometric == #rgba)]) ifTrue:[ |
2703 | 1258 |
colorType := 2. |
3461 | 1259 |
samplesPerPixel > 3 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
2703 | 1260 |
]. |
1261 |
(photometric == #palette) ifTrue:[ |
|
1262 |
colorType := 3. |
|
3461 | 1263 |
samplesPerPixel > 1 ifTrue:[ colorType := colorType + 4]. "/ +alpha |
2703 | 1264 |
]. |
3100 | 1265 |
colorType isNil ifTrue:[ |
3461 | 1266 |
self error:'unhandled photometric' |
3100 | 1267 |
]. |
2703 | 1268 |
|
2683 | 1269 |
self |
1270 |
writeChunk: 'IHDR' |
|
1271 |
size: 13 |
|
1272 |
with:[ |
|
1273 |
self writeLong:width. |
|
1274 |
self writeLong:height. |
|
2703 | 1275 |
outStream nextPut: 8. "Assume that all colors are 24bit" "/ depth |
1276 |
outStream nextPut: colorType. |
|
2683 | 1277 |
outStream nextPut: 0. "Compression" |
1278 |
outStream nextPut: 0. "Filter method" |
|
1279 |
outStream nextPut: 0 "Non-interlaced" |
|
1280 |
] |
|
1281 |
! |
|
1282 |
||
2703 | 1283 |
writeImageDataChunk |
2708 | 1284 |
|compressedByteStream compressedBytes zlibWriter bytesPerScanline idx| |
2703 | 1285 |
|
2708 | 1286 |
compressedByteStream := WriteStream on:(ByteArray new:100). |
3819 | 1287 |
zlibWriter := ZipStream |
1288 |
writeOpenAsZipStreamOn:compressedByteStream |
|
1289 |
suppressHeaderAndChecksum:false. |
|
2703 | 1290 |
zlibWriter binary. |
1291 |
||
1292 |
bytesPerScanline := self bytesPerRow. |
|
1293 |
||
2708 | 1294 |
idx := 1. |
1295 |
0 to:height-1 do:[:y | |
|
1296 |
|row| |
|
1297 |
||
1298 |
row := data copyFrom:idx to:(idx+bytesPerScanline-1). |
|
1299 |
zlibWriter nextPutAll:#[0]. "/ no filter |
|
1300 |
zlibWriter nextPutAll:row. |
|
1301 |
idx := idx + bytesPerScanline. |
|
2703 | 1302 |
]. |
2708 | 1303 |
|
1304 |
zlibWriter close. |
|
1305 |
compressedBytes := compressedByteStream contents. |
|
1306 |
||
1307 |
self |
|
1308 |
writeChunk: 'IDAT' |
|
1309 |
size:(compressedBytes size) |
|
1310 |
with:[ |
|
1311 |
outStream nextPutAll:compressedBytes |
|
1312 |
] |
|
2703 | 1313 |
! |
1314 |
||
2683 | 1315 |
writePaletteChunk |
1316 |
self |
|
1317 |
writeChunk: 'PLTE' |
|
1318 |
size: 3 * colorMap size |
|
1319 |
with:[ |
|
2703 | 1320 |
colorMap do:[:color | |
1321 |
outStream |
|
1322 |
nextPut: color redByte; |
|
1323 |
nextPut: color greenByte; |
|
1324 |
nextPut: color blueByte |
|
2683 | 1325 |
] |
1326 |
] |
|
1327 |
! ! |
|
1328 |
||
398 | 1329 |
!PNGReader class methodsFor:'documentation'! |
308 | 1330 |
|
1331 |
version |
|
3554 | 1332 |
^ '$Header$' |
2909 | 1333 |
! |
1334 |
||
1335 |
version_CVS |
|
3554 | 1336 |
^ '$Header$' |
3505
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
1337 |
! |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
1338 |
|
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
1339 |
version_HG |
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
1340 |
|
af4fb19df063
PNGReader: added support for reading 8bit alpha channel
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3461
diff
changeset
|
1341 |
^ '$Changeset: <not expanded> $' |
308 | 1342 |
! ! |
1671 | 1343 |
|
3100 | 1344 |
|
308 | 1345 |
PNGReader initialize! |