author | Claus Gittinger <cg@exept.de> |
Tue, 29 Aug 2017 12:38:15 +0200 | |
changeset 3995 | ab4741a8e8f5 |
parent 3994 | 8ab8729d5dec |
child 3996 | 9f8c87c19905 |
permissions | -rw-r--r-- |
27 | 1 |
" |
2 |
COPYRIGHT (c) 1994 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 |
" |
|
1562
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
12 |
"{ Package: 'stx:libview2' }" |
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
13 |
|
3990 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
27 | 16 |
ImageReader subclass:#PCXReader |
3995 | 17 |
instanceVariableNames:'header sourceBytesPerRow bitsPerPixelIn depth nPlanes compression |
18 |
nPlanesUsed' |
|
201 | 19 |
classVariableNames:'' |
20 |
poolDictionaries:'' |
|
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1737
diff
changeset
|
21 |
category:'Graphics-Images-Readers' |
27 | 22 |
! |
23 |
||
29 | 24 |
!PCXReader class methodsFor:'documentation'! |
25 |
||
26 |
copyright |
|
27 |
" |
|
28 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
29 |
All Rights Reserved |
|
30 |
||
31 |
This software is furnished under a license and may be used |
|
32 |
only in accordance with the terms of that license and with the |
|
33 |
inclusion of the above copyright notice. This software may not |
|
34 |
be provided or otherwise made available to, or used by, any |
|
35 |
other person. No title to or ownership of the software is |
|
36 |
hereby transferred. |
|
37 |
" |
|
38 |
! |
|
39 |
||
40 |
documentation |
|
41 |
" |
|
3992 | 42 |
this class provides methods to load PCX bitmap files. |
43 |
PCX used to be a popular image format in the early PC times, |
|
44 |
but became almost obsolete in the meantime. |
|
3993 | 45 |
This reader is not tuned and performs slow on non-8bit images; |
46 |
if at all, use it to convert old image files to newer formats, |
|
47 |
such as png, tiff or jpg. |
|
3992 | 48 |
|
49 |
Due to not having too many examples for testing, |
|
50 |
this could fail to read some files. |
|
201 | 51 |
(especially, I have no uncompressed files for testing). |
52 |
||
3992 | 53 |
Only 1,2,4,8 and 24-bit PCX images are supported. |
54 |
Image writing is not. |
|
201 | 55 |
|
56 |
[See also:] |
|
3992 | 57 |
Image Form Icon |
58 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader |
|
59 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
|
60 |
XBMReader XPMReader XWDReader |
|
29 | 61 |
" |
1737 | 62 |
! |
63 |
||
64 |
examples |
|
65 |
" |
|
3992 | 66 |
[exBegin] |
1737 | 67 |
Image fromFile:'/usr/share/lilo/suse_640x480.pcx' |
3992 | 68 |
[exEnd] |
69 |
[exBegin] |
|
70 |
Image fromFile:'../../goodies/bitmaps/pcxImages/lena_depth8_palette.pcx' |
|
71 |
[exEnd] |
|
1737 | 72 |
" |
29 | 73 |
! ! |
74 |
||
43 | 75 |
!PCXReader class methodsFor:'initialization'! |
76 |
||
77 |
initialize |
|
201 | 78 |
"tell Image-class, that a new fileReader is present |
398 | 79 |
for the '.pcx' extension." |
43 | 80 |
|
647 | 81 |
MIMETypes defineImageType:'image/x-pcx' suffix:'pcx' reader:self. |
201 | 82 |
|
630 | 83 |
"Modified: 27.6.1997 / 18:39:23 / cg" |
43 | 84 |
! ! |
85 |
||
27 | 86 |
!PCXReader class methodsFor:'testing'! |
87 |
||
88 |
isValidImageFile:aFilename |
|
89 |
"return true, if aFilename contains a PCX image" |
|
90 |
||
29 | 91 |
|count header inStream| |
27 | 92 |
|
93 |
inStream := self streamReadingFile:aFilename. |
|
94 |
inStream isNil ifTrue:[^ false]. |
|
95 |
inStream binary. |
|
96 |
||
97 |
header := ByteArray uninitializedNew:128. |
|
29 | 98 |
count := inStream nextBytes:128 into:header. |
27 | 99 |
inStream close. |
100 |
||
29 | 101 |
((count == 128) and:[self isValidPCXHeader:header]) ifFalse:[ |
27 | 102 |
^ false |
103 |
]. |
|
104 |
^ true |
|
102 | 105 |
|
106 |
"Modified: 17.9.1995 / 17:32:07 / claus" |
|
201 | 107 |
! |
108 |
||
109 |
isValidPCXHeader:aHeader |
|
110 |
"return true, if aHeader looks like a PCX image header" |
|
111 |
||
530 | 112 |
"check magic number" |
201 | 113 |
((aHeader at:1) ~~ 16r0A) ifTrue:[ |
531 | 114 |
^ false |
201 | 115 |
]. |
116 |
||
117 |
"check version" |
|
118 |
(#(0 2 3 5) includes:(aHeader at:2)) ifFalse:[ |
|
531 | 119 |
^ false |
201 | 120 |
]. |
121 |
||
122 |
^ true |
|
530 | 123 |
|
124 |
"Modified: 16.4.1997 / 22:24:32 / cg" |
|
27 | 125 |
! ! |
126 |
||
3995 | 127 |
!PCXReader methodsFor:'obsolete'! |
1737 | 128 |
|
129 |
readCompressedData |
|
3995 | 130 |
|rowStartIndex endIndex byte nByte value idx2 |
3993 | 131 |
srcIndex dstIndex srcRowStartIndex dstRowStartIndex bytesPerPane planeData imageBytesPerRow| |
1737 | 132 |
|
3994 | 133 |
(nPlanes > 1 and:[depth == 8]) ifTrue:[ |
134 |
^ self readCompressedDepth24Data. |
|
135 |
]. |
|
3995 | 136 |
|
3991 | 137 |
imageBytesPerRow := (((width * depth) + 7) // 8). |
3995 | 138 |
|
3991 | 139 |
bytesPerPane := height * (imageBytesPerRow max:sourceBytesPerRow). |
140 |
planeData := 1 to:nPlanes collect:[:planeNr | ByteArray uninitializedNew:bytesPerPane]. |
|
141 |
data := planeData at:1. |
|
201 | 142 |
|
3993 | 143 |
rowStartIndex := 1. |
1737 | 144 |
1 to:height do:[:row | |
3991 | 145 |
1 to:nPlanes do:[:planeNr | |
3993 | 146 |
|planeBytes| |
3991 | 147 |
|
3993 | 148 |
planeBytes := planeData at:planeNr. |
3995 | 149 |
|
3993 | 150 |
dstIndex := rowStartIndex. |
3991 | 151 |
endIndex := dstIndex + sourceBytesPerRow. |
3995 | 152 |
|
3991 | 153 |
[dstIndex < endIndex] whileTrue:[ |
3995 | 154 |
byte := inStream nextByte. |
3991 | 155 |
((byte bitAnd:2r11000000) ~~ 2r11000000) ifTrue:[ |
3993 | 156 |
planeBytes at:dstIndex put:byte. |
3991 | 157 |
dstIndex := dstIndex + 1. |
158 |
] ifFalse:[ |
|
159 |
nByte := byte bitAnd:2r00111111. |
|
3995 | 160 |
value := inStream nextByte. |
3991 | 161 |
idx2 := ((dstIndex + nByte) min:endIndex) - 1. |
3993 | 162 |
planeBytes from:dstIndex to:idx2 put:value. |
3991 | 163 |
dstIndex := dstIndex + nByte. |
164 |
]. |
|
1737 | 165 |
]. |
166 |
]. |
|
3993 | 167 |
"/ rowStartIndex := endIndex. |
3994 | 168 |
rowStartIndex := rowStartIndex + (imageBytesPerRow * nPlanes). |
1737 | 169 |
]. |
3990 | 170 |
|
3992 | 171 |
"/ now merge the planes |
172 |
nPlanes > 1 ifTrue:[ |
|
173 |
depth == 8 ifTrue:[ |
|
3993 | 174 |
(nPlanes >= 3) ifTrue:[ |
3992 | 175 |
"/ a simple rgb image |
3993 | 176 |
nPlanes := nPlanes min:4. |
177 |
||
3992 | 178 |
data := ByteArray uninitializedNew:(nPlanes*width*height). |
3993 | 179 |
srcRowStartIndex := dstRowStartIndex := 1. |
180 |
1 to:height do:[:y | |
|
181 |
1 to:nPlanes do:[:p | |
|
182 |
|planeBytes| |
|
3995 | 183 |
|
3993 | 184 |
dstIndex := dstRowStartIndex + (p - 1). |
185 |
srcIndex := srcRowStartIndex. |
|
186 |
planeBytes := planeData at:p. |
|
187 |
1 to:width do:[:x | |
|
188 |
data at:dstIndex put:(planeBytes at:srcIndex). |
|
189 |
dstIndex := dstIndex + nPlanes. |
|
190 |
srcIndex := srcIndex + 1. |
|
191 |
]. |
|
192 |
]. |
|
193 |
srcRowStartIndex := srcRowStartIndex + imageBytesPerRow. |
|
194 |
dstRowStartIndex := dstRowStartIndex + (width * nPlanes). |
|
195 |
]. |
|
196 |
]. |
|
197 |
depth := nPlanes * 8. |
|
198 |
photometric := nPlanes==3 ifTrue:[#rgb] ifFalse:[#rgba]. |
|
199 |
] ifFalse:[ |
|
200 |
(depth == 1) ifTrue:[ |
|
201 |
|newDepth nPlanesUsed| |
|
202 |
||
203 |
nPlanesUsed := nPlanes min:4. |
|
204 |
newDepth := (nPlanesUsed * depth) nextPowerOf2. |
|
3995 | 205 |
|
3993 | 206 |
data := ByteArray new:((width*height*newDepth)+7)//8. |
3992 | 207 |
srcIndex := dstIndex := 1. |
208 |
1 to:height do:[:y | |
|
3993 | 209 |
|inMask outBitCount outBits| |
3995 | 210 |
|
3993 | 211 |
inMask := 16r80. |
212 |
outBitCount := 0. |
|
213 |
outBits := 0. |
|
3992 | 214 |
1 to:width do:[:x | |
3993 | 215 |
1 to:nPlanesUsed do:[:p | |
216 |
outBits := (outBits bitShift:1). |
|
217 |
(((planeData at:p) at:srcIndex) bitAnd:inMask) ~~ 0 ifTrue:[ |
|
218 |
outBits := outBits bitOr:1. |
|
219 |
]. |
|
220 |
]. |
|
221 |
outBits := outBits bitShift:(newDepth-nPlanesUsed). |
|
222 |
outBitCount := outBitCount + newDepth. |
|
3995 | 223 |
|
3993 | 224 |
outBitCount >= 8 ifTrue:[ |
225 |
data at:dstIndex put:((data at:dstIndex) bitOr:outBits). |
|
3992 | 226 |
dstIndex := dstIndex + 1. |
3993 | 227 |
outBitCount := 0. |
228 |
outBits := 0. |
|
229 |
]. |
|
230 |
inMask := inMask rightShift:1. |
|
231 |
inMask == 0 ifTrue:[ |
|
232 |
inMask := 16r80. |
|
233 |
srcIndex := srcIndex + 1. |
|
234 |
]. |
|
3992 | 235 |
]. |
236 |
]. |
|
3993 | 237 |
depth := newDepth. |
238 |
"/ rgbi colormap. |
|
239 |
self halt. |
|
240 |
||
241 |
] ifFalse:[ |
|
242 |
self halt. |
|
3992 | 243 |
]. |
244 |
]. |
|
245 |
]. |
|
246 |
||
3991 | 247 |
"/ sourceBytesPerRow ~~ (((width * depth) + 7) // 8) ifTrue:[ |
248 |
"/ "/ have to compress - above code reads sourceBytesPerRow |
|
249 |
"/ "/ (to keep in sync with RLE); but we want width bytesPerRow in the image data. |
|
250 |
"/ "/ Can compress in the data-area; leftover pixels are simply ignored |
|
251 |
"/ "/ by other image processing code |
|
252 |
"/ "/ |
|
253 |
"/ 1 to:nPlanes do:[:planeNr | |
|
254 |
"/ |dst| |
|
255 |
"/ |
|
256 |
"/ dst := planeData at:planeNr. |
|
257 |
"/ |
|
258 |
"/ dstIndex := width + 1. |
|
259 |
"/ srcIndex := sourceBytesPerRow + 1. |
|
260 |
"/ 2 to:height do:[:row | |
|
261 |
"/ dst replaceFrom:dstIndex to:dstIndex+width-1 with:dst startingAt:srcIndex. |
|
262 |
"/ dstIndex := dstIndex + width. |
|
263 |
"/ srcIndex := srcIndex + sourceBytesPerRow |
|
264 |
"/ ] |
|
265 |
"/ ]. |
|
266 |
"/ ]. |
|
267 |
||
3995 | 268 |
"Created: / 29-08-2017 / 11:33:27 / cg" |
3994 | 269 |
! |
270 |
||
271 |
readCompressedDepth24Data |
|
3995 | 272 |
|rowStartIndex rowBytes endIndex byte nByte value idx2 |
3994 | 273 |
srcIndex dstIndex imageBytesPerRow| |
274 |
||
275 |
imageBytesPerRow := (((width * depth * nPlanes) + 7) // 8). |
|
276 |
||
277 |
data := ByteArray new:(nPlanes*width*height). |
|
278 |
||
279 |
rowBytes := ByteArray new:(sourceBytesPerRow * nPlanes). |
|
280 |
||
281 |
rowStartIndex := 1. |
|
282 |
1 to:height do:[:row | |
|
283 |
dstIndex := 1. |
|
284 |
endIndex := 1 + (sourceBytesPerRow * nPlanes). |
|
285 |
[dstIndex < endIndex] whileTrue:[ |
|
3995 | 286 |
byte := inStream nextByte. |
3994 | 287 |
((byte bitAnd:2r11000000) ~~ 2r11000000) ifTrue:[ |
288 |
rowBytes at:dstIndex put:byte. |
|
289 |
dstIndex := dstIndex + 1. |
|
290 |
] ifFalse:[ |
|
291 |
nByte := byte bitAnd:2r00111111. |
|
3995 | 292 |
value := inStream nextByte. |
3994 | 293 |
idx2 := ((dstIndex + nByte) min:endIndex) - 1. |
294 |
rowBytes from:dstIndex to:idx2 put:value. |
|
295 |
dstIndex := dstIndex + nByte. |
|
296 |
]. |
|
297 |
]. |
|
298 |
||
299 |
dstIndex := rowStartIndex. |
|
300 |
srcIndex := 1. |
|
301 |
1 to:width do:[:x | |
|
302 |
data at:dstIndex put:(rowBytes at:x). |
|
303 |
data at:dstIndex+1 put:(rowBytes at:x+sourceBytesPerRow). |
|
304 |
data at:dstIndex+2 put:(rowBytes at:x+sourceBytesPerRow+sourceBytesPerRow). |
|
305 |
dstIndex := dstIndex + 3. |
|
306 |
]. |
|
307 |
rowStartIndex := rowStartIndex + imageBytesPerRow. |
|
308 |
]. |
|
309 |
depth := depth * nPlanes. |
|
310 |
||
311 |
"Created: / 29-08-2017 / 02:13:07 / cg" |
|
3995 | 312 |
"Modified: / 29-08-2017 / 08:40:02 / cg" |
313 |
! |
|
314 |
||
315 |
readUncompressedData |
|
316 |
|dstIndex| |
|
317 |
||
318 |
" |
|
319 |
actually untested ... |
|
320 |
" |
|
321 |
data := ByteArray uninitializedNew:(height * width). |
|
322 |
sourceBytesPerRow ~~ width ifTrue:[ |
|
323 |
dstIndex := 1. |
|
324 |
1 to:height do:[:row | |
|
325 |
inStream nextBytes:width into:data startingAt:dstIndex. |
|
326 |
dstIndex := dstIndex + width. |
|
327 |
inStream skip:(sourceBytesPerRow - width). |
|
328 |
] |
|
329 |
] ifFalse:[ |
|
330 |
inStream nextBytes:(height * width) into:data. |
|
331 |
]. |
|
332 |
||
333 |
"Modified: / 29-08-2017 / 08:39:16 / cg" |
|
334 |
! ! |
|
335 |
||
336 |
!PCXReader methodsFor:'private-reading'! |
|
337 |
||
338 |
extractColorMap16 |
|
339 |
"extract the 16-entry colormap from the header" |
|
340 |
||
341 |
|rawMap| |
|
342 |
||
343 |
rawMap := header copyFrom:17 to:(17 + (16*3) - 1). |
|
344 |
^ MappedPalette rgbBytesVector:rawMap |
|
345 |
! |
|
346 |
||
347 |
readColorMap256 |
|
348 |
"read the 256-entry colormap at the end" |
|
349 |
||
350 |
|rawMap| |
|
351 |
||
352 |
rawMap := ByteArray uninitializedNew:(256*3). |
|
353 |
inStream nextBytes:(256*3) into:rawMap. |
|
354 |
^ MappedPalette rgbBytesVector:rawMap |
|
355 |
||
356 |
"Modified: / 29-08-2017 / 08:38:15 / cg" |
|
357 |
"Modified (comment): / 29-08-2017 / 11:32:29 / cg" |
|
358 |
! |
|
359 |
||
360 |
readImageData |
|
361 |
|rowStartIndex imageBytesPerRow| |
|
362 |
||
363 |
imageBytesPerRow := (((width * depth) + 7) // 8). |
|
364 |
data := ByteArray new:(imageBytesPerRow*height). |
|
365 |
||
366 |
rowStartIndex := 1. |
|
367 |
1 to:height do:[:row | |
|
368 |
self readScanlineTo:data startingAt:rowStartIndex. |
|
369 |
rowStartIndex := rowStartIndex + imageBytesPerRow. |
|
370 |
]. |
|
371 |
||
372 |
"Created: / 29-08-2017 / 09:54:39 / cg" |
|
373 |
"Modified: / 29-08-2017 / 11:42:15 / cg" |
|
1737 | 374 |
! |
375 |
||
376 |
readRestAfterHeader |
|
201 | 377 |
"read an raw image in pcx format from aStream. |
378 |
The header has already been read into the header argument." |
|
379 |
||
3995 | 380 |
|version xmin ymin xmax ymax paletteType| |
27 | 381 |
|
531 | 382 |
"/ typedef struct { /*header for PCX bitmap files*/ |
3990 | 383 |
"/ unsigned char signature; /*1 PCX file identifier */ |
384 |
"/ unsigned char version; /*2 version compatibility level */ |
|
385 |
"/ unsigned char encoding; /*3 compression method */ |
|
386 |
"/ unsigned char bitsperpix; /*4 bits per pixel, or depth */ |
|
387 |
"/ unsigned short Xleft; /*5 X position of left edge */ |
|
388 |
"/ unsigned short Ytop; /*7 Y position of top edge */ |
|
389 |
"/ unsigned short Xright; /*9 X position of right edge */ |
|
390 |
"/ unsigned short Ybottom; /*11 Y position of bottom edge */ |
|
391 |
"/ unsigned short Xscreensize; /*13 X screen res of source image */ |
|
392 |
"/ unsigned short Yscreensize; /*15 Y screen res of source image */ |
|
393 |
"/ unsigned char PCXpalette[16][3]; /*17 PCX color map */ |
|
394 |
"/ unsigned char reserved1; /*17+48 should be 0, 1 if std res fax */ |
|
566 | 395 |
"/ unsigned char planes; /*66 bit planes in image*/ |
396 |
"/ unsigned short linesize; /*67 byte delta between scanlines */ |
|
3990 | 397 |
"/ unsigned char paletteinfo; /*69 paletteType */ |
398 |
"/ /*0 == undef |
|
531 | 399 |
"/ 1 == color |
400 |
"/ 2 == grayscale*/ |
|
3990 | 401 |
"/ unsigned char reserved2[58]; /*70 fill to struct size of 128*/ |
531 | 402 |
"/ } PCX_HEADER; |
403 |
||
27 | 404 |
version := header at:2. |
3990 | 405 |
"/ 'version=' print. version printNL. |
27 | 406 |
compression := header at:3. |
3990 | 407 |
"/ 'compression=' print. compression printNL. |
27 | 408 |
(#(0 1) includes:compression) ifFalse:[ |
812
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
409 |
^ self fileFormatError:'unknown compression'. |
27 | 410 |
]. |
530 | 411 |
|
3995 | 412 |
bitsPerPixelIn := header at:4. |
3990 | 413 |
"/ 'depth=' print. depth printNL. |
27 | 414 |
nPlanes := header at:66. |
3990 | 415 |
"/ 'planes=' print. nPlanes printNL. |
1737 | 416 |
sourceBytesPerRow := header wordAt:67 MSB:false. |
3990 | 417 |
"/ 'srcBytesPerRow=' print. srcBytesPerRow printNL. |
27 | 418 |
paletteType := header at:69. |
419 |
||
83 | 420 |
xmin := header wordAt:5 MSB:false. |
421 |
ymin := header wordAt:7 MSB:false. |
|
422 |
xmax := header wordAt:9 MSB:false. |
|
423 |
ymax := header wordAt:11 MSB:false. |
|
27 | 424 |
|
425 |
width := (xmax - xmin + 1). |
|
426 |
height := (ymax - ymin + 1). |
|
3990 | 427 |
"/ 'width=' print. width printNL. |
428 |
"/ 'height=' print. width printNL. |
|
27 | 429 |
|
3991 | 430 |
" |
431 |
although it would be easy to implement ... |
|
432 |
I have no test pictures for other formats. |
|
433 |
So its not (yet) implemented |
|
434 |
" |
|
3995 | 435 |
((#(1 2 4 8) includes:bitsPerPixelIn) "and:[nPlanes == 1]") ifFalse:[ |
3991 | 436 |
"/ 'PCXReader: depth: ' errorPrint. depth errorPrint. |
437 |
"/ ' planes:' errorPrint. nPlanes errorPrintNL. |
|
3992 | 438 |
^ self fileFormatError:'can only handle depth''s 1,2,4 or 8'. |
439 |
]. |
|
440 |
(nPlanes between:1 and:4) ifFalse:[ |
|
441 |
^ self fileFormatError:'can only handle 1 to 4 planes'. |
|
3991 | 442 |
]. |
443 |
||
3995 | 444 |
nPlanesUsed := nPlanes. |
445 |
depth := bitsPerPixelIn * nPlanes. |
|
446 |
bitsPerPixelIn ~~ 8 ifTrue:[ |
|
447 |
"/ for 3 planes, single rgb bit, we will generate a depth4 image. |
|
448 |
"/ for 3 planes, two bits per rgb, we will generate a depth8 image |
|
449 |
"/ for 3 planes, four bits per rgb, we will generate a depth16 image |
|
450 |
nPlanesUsed := nPlanes min:4. |
|
451 |
depth := (nPlanesUsed * bitsPerPixelIn) nextPowerOf2. |
|
452 |
]. |
|
453 |
||
1846 | 454 |
self reportDimension. |
455 |
||
3992 | 456 |
"/ precompute a first guess at the photometric; |
3995 | 457 |
"/ warning: might be changed by readImageData |
3990 | 458 |
paletteType == 2 ifTrue:[ |
459 |
photometric := #blackIs0. |
|
460 |
] ifFalse:[ |
|
461 |
depth == 1 ifTrue:[ |
|
462 |
photometric := #blackIs0. |
|
463 |
] ifFalse:[ |
|
464 |
photometric := #palette. |
|
561 | 465 |
]. |
27 | 466 |
]. |
3995 | 467 |
self readImageData. |
27 | 468 |
|
3993 | 469 |
depth == 24 ifTrue:[ |
470 |
samplesPerPixel := 3. |
|
471 |
bitsPerSample := #( 8 8 8 ). |
|
472 |
photometric := #rgb. |
|
473 |
] ifFalse:[ |
|
474 |
depth == 32 ifTrue:[ |
|
475 |
samplesPerPixel := 4. |
|
476 |
bitsPerSample := #( 8 8 8 8). |
|
477 |
photometric := #rgba. |
|
478 |
] ifFalse:[ |
|
479 |
samplesPerPixel := 1. |
|
480 |
bitsPerSample := { depth }. |
|
481 |
]. |
|
482 |
]. |
|
483 |
||
3991 | 484 |
photometric == #palette ifTrue:[ |
485 |
(version == 5) ifTrue:[ |
|
486 |
true "depth == 8" ifTrue:[ |
|
3995 | 487 |
| nMaxPad byte "{Class: SmallInteger }" | |
488 |
||
3991 | 489 |
inStream isPositionable ifTrue:[ |
490 |
"/ seek to the end, minus 3*256-1 bytes and check there |
|
491 |
inStream position:(inStream fileSize - (3*256)-1). |
|
492 |
byte := inStream next. |
|
493 |
] ifFalse:[ |
|
494 |
"/ RLE data is padded - skip over zeros for the 0C-byte |
|
495 |
nMaxPad := 15. |
|
3995 | 496 |
byte := inStream next. |
3990 | 497 |
|
3991 | 498 |
[(byte ~~ 16r0C) and:[nMaxPad > 0]] whileTrue:[ |
3995 | 499 |
byte := inStream next. |
3991 | 500 |
nMaxPad := nMaxPad - 1. |
501 |
]. |
|
3990 | 502 |
]. |
3991 | 503 |
(byte == 16r0C) ifTrue:[ |
504 |
colorMap := self readColorMap256. |
|
505 |
] ifFalse:[ |
|
3990 | 506 |
'PCXREADER: no valid 256-entry palette (got' errorPrint. |
507 |
byte errorPrint. '; expected ' errorPrint. 16rC0 errorPrint. ')' errorPrintCR. |
|
508 |
]. |
|
509 |
]. |
|
510 |
]. |
|
3991 | 511 |
((version == 2) or:[ colorMap isNil and:[ (depth <= 4) ] ]) ifTrue:[ |
512 |
"/ take palette from header |
|
513 |
colorMap := self extractColorMap16. |
|
514 |
]. |
|
3990 | 515 |
]. |
516 |
||
27 | 517 |
" |
518 |
|i f| |
|
29 | 519 |
i := Image fromFile:'somefile.pcx'. |
520 |
i inspect. |
|
27 | 521 |
" |
499 | 522 |
|
3995 | 523 |
"Modified: / 29-08-2017 / 12:24:11 / cg" |
1737 | 524 |
! |
525 |
||
3995 | 526 |
readScanlineTo:data startingAt:startIndex |
527 |
"read a single scanline into data starting at startIndex" |
|
528 |
||
529 |
|rowBytes endIndex byte nByte value idx2 dstIndex imageBytesPerRow rowOffset| |
|
530 |
||
531 |
imageBytesPerRow := (((width * bitsPerPixelIn * nPlanes) + 7) // 8). |
|
1737 | 532 |
|
3995 | 533 |
"/ multiband images: |
534 |
"/ need to read into a temporary buffer, |
|
535 |
"/ then extract the bands and merge the pixels |
|
536 |
"/ i.e. read as rrr...rrrggg...gggbbb...bbb |
|
537 |
"/ then merge bands into rgbrgb...rgbrgb |
|
538 |
"/ notice that each scanline is rle encoded (all bands together) |
|
539 |
||
540 |
rowBytes := ByteArray new:(sourceBytesPerRow * nPlanes). |
|
541 |
||
542 |
compression == 0 ifTrue:[ |
|
543 |
inStream nextBytes:(sourceBytesPerRow * nPlanes) into:rowBytes startingAt:1. |
|
544 |
] ifFalse:[ |
|
1737 | 545 |
dstIndex := 1. |
3995 | 546 |
endIndex := 1 + (sourceBytesPerRow * nPlanes). |
547 |
[dstIndex < endIndex] whileTrue:[ |
|
548 |
byte := inStream nextByte. |
|
549 |
byte notNil ifTrue:[ |
|
550 |
((byte bitAnd:2r11000000) ~~ 2r11000000) ifTrue:[ |
|
551 |
rowBytes at:dstIndex put:byte. |
|
552 |
dstIndex := dstIndex + 1. |
|
553 |
] ifFalse:[ |
|
554 |
nByte := byte bitAnd:2r00111111. |
|
555 |
value := inStream nextByte. |
|
556 |
idx2 := ((dstIndex + nByte) min:endIndex) - 1. |
|
557 |
rowBytes from:dstIndex to:idx2 put:value. |
|
558 |
dstIndex := dstIndex + nByte. |
|
559 |
]. |
|
560 |
] ifFalse:[ |
|
561 |
"/ oops - short read!! |
|
562 |
dstIndex := endIndex + 1. |
|
563 |
] |
|
564 |
]. |
|
565 |
]. |
|
566 |
||
567 |
nPlanes > 1 ifTrue:[ |
|
568 |
"/ merge the bands |
|
569 |
bitsPerPixelIn == 8 ifTrue:[ |
|
570 |
"/ bytewise is easy |
|
571 |
||
572 |
self assert:(sourceBytesPerRow >= width). |
|
573 |
||
574 |
rowOffset := 0. |
|
575 |
0 to:nPlanesUsed-1 do:[:planeOffs | |
|
576 |
dstIndex := startIndex + planeOffs. |
|
577 |
1 to:width do:[:x | |
|
578 |
data at:dstIndex put:(rowBytes at:x+rowOffset). |
|
579 |
dstIndex := dstIndex + nPlanes. |
|
580 |
]. |
|
581 |
rowOffset := rowOffset + sourceBytesPerRow. |
|
582 |
]. |
|
583 |
] ifFalse:[ |
|
584 |
"/ need some bit-stuffing to merge planes... |
|
585 |
depth <= 8 ifTrue:[ |
|
586 |
"/ merge into bytes |
|
587 |
|m0 srcByteIndex inShift mask outBitCount pixelBits outShift xOffs bits| |
|
588 |
||
589 |
m0 := #( 16r80 16rC0 nil 16rF0 ) at:bitsPerPixelIn. |
|
590 |
mask := #( 1 3 0 7 ) at:bitsPerPixelIn. |
|
591 |
||
592 |
dstIndex := startIndex. |
|
593 |
||
594 |
xOffs := 1. |
|
595 |
inShift := 8-bitsPerPixelIn. |
|
596 |
outShift := 8-(depth). |
|
597 |
||
598 |
1 to:width do:[:x | |
|
599 |
"/ collect pixel's bits from planes |
|
600 |
pixelBits := 0. |
|
601 |
rowOffset := 0. |
|
602 |
1 to:nPlanesUsed do:[:p | |
|
603 |
byte := rowBytes at:xOffs+rowOffset. |
|
604 |
bits := (byte rightShift:inShift) bitAnd:mask. |
|
605 |
"/ bits now contains the plane's bits in the low bit positions |
|
606 |
pixelBits := (pixelBits bitShift:bitsPerPixelIn) bitOr:bits. |
|
607 |
||
608 |
rowOffset := rowOffset + sourceBytesPerRow. |
|
609 |
]. |
|
610 |
inShift := inShift - bitsPerPixelIn. |
|
611 |
inShift < 0 ifTrue:[ |
|
612 |
inShift := 8-bitsPerPixelIn. |
|
613 |
xOffs := xOffs + 1. |
|
614 |
]. |
|
615 |
"/ pixelBits now contains the pixel's rgb bits in low bit positions. |
|
616 |
||
617 |
"/ write output |
|
618 |
byte := data at:dstIndex. |
|
619 |
byte := byte bitOr:(pixelBits bitShift:outShift). |
|
620 |
data at:dstIndex put:byte. |
|
621 |
||
622 |
"/ update shift. |
|
623 |
outShift := outShift - depth. |
|
624 |
outShift < 0 ifTrue:[ |
|
625 |
outShift := 8-(depth). |
|
626 |
dstIndex := dstIndex + 1. |
|
627 |
]. |
|
628 |
] |
|
629 |
] ifFalse:[ |
|
630 |
depth == 16 ifTrue:[ |
|
631 |
"/ merge into 16bit ints |
|
632 |
self halt. |
|
633 |
] ifFalse:[ |
|
634 |
self error. |
|
635 |
]. |
|
636 |
]. |
|
637 |
]. |
|
1737 | 638 |
] ifFalse:[ |
3995 | 639 |
data replaceFrom:startIndex to:(startIndex + imageBytesPerRow - 1) with:rowBytes startingAt:1 |
1737 | 640 |
]. |
3995 | 641 |
|
642 |
"Created: / 29-08-2017 / 09:49:41 / cg" |
|
643 |
"Modified: / 29-08-2017 / 12:27:12 / cg" |
|
27 | 644 |
! ! |
645 |
||
1848 | 646 |
!PCXReader methodsFor:'reading'! |
647 |
||
648 |
readImage |
|
649 |
"read an image in pcx format from inStream" |
|
650 |
||
651 |
inStream binary. |
|
652 |
||
653 |
header := ByteArray uninitializedNew:128. |
|
654 |
(inStream nextBytes:128 into:header) == 128 ifFalse:[ |
|
655 |
^ self fileFormatError:'short file'. |
|
656 |
]. |
|
657 |
||
658 |
(self class isValidPCXHeader:header) ifFalse:[ |
|
659 |
^ self fileFormatError:'wrong header'. |
|
660 |
]. |
|
661 |
||
662 |
self readRestAfterHeader. |
|
663 |
! ! |
|
664 |
||
201 | 665 |
!PCXReader class methodsFor:'documentation'! |
666 |
||
667 |
version |
|
3990 | 668 |
^ '$Header$' |
3991 | 669 |
! |
670 |
||
671 |
version_CVS |
|
672 |
^ '$Header$' |
|
201 | 673 |
! ! |
1737 | 674 |
|
3990 | 675 |
|
27 | 676 |
PCXReader initialize! |