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