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