author | Claus Gittinger <cg@exept.de> |
Wed, 09 Apr 2003 12:49:21 +0200 | |
changeset 1743 | 2f206c9f1319 |
parent 1737 | a1ed08195ee7 |
child 1745 | 4fa0fad2a463 |
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 |
" |
|
12 |
||
1562
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
13 |
"{ Package: 'stx:libview2' }" |
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
14 |
|
27 | 15 |
ImageReader subclass:#PCXReader |
1737 | 16 |
instanceVariableNames:'header buffer nBuffer bufferIndex sourceBytesPerRow' |
201 | 17 |
classVariableNames:'' |
18 |
poolDictionaries:'' |
|
259 | 19 |
category:'Graphics-Images-Support' |
27 | 20 |
! |
21 |
||
29 | 22 |
!PCXReader class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
27 |
All Rights Reserved |
|
28 |
||
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
102 | 40 |
this class provides methods for loading 8-plane PCX bitmap files. |
201 | 41 |
|
29 | 42 |
Due to not having too many examples for testing, this could fail |
201 | 43 |
to read some files. |
44 |
(especially, I have no uncompressed files for testing). |
|
45 |
||
46 |
Only 8-bit (i.e. 256 color) PCX images are supported. |
|
47 |
Image writing is not supported. |
|
48 |
||
49 |
[See also:] |
|
531 | 50 |
Image Form Icon |
51 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader |
|
52 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
|
53 |
XBMReader XPMReader XWDReader |
|
29 | 54 |
" |
1737 | 55 |
! |
56 |
||
57 |
examples |
|
58 |
" |
|
59 |
Image fromFile:'/usr/share/lilo/suse_640x480.pcx' |
|
60 |
" |
|
29 | 61 |
! ! |
62 |
||
43 | 63 |
!PCXReader class methodsFor:'initialization'! |
64 |
||
65 |
initialize |
|
201 | 66 |
"tell Image-class, that a new fileReader is present |
398 | 67 |
for the '.pcx' extension." |
43 | 68 |
|
647 | 69 |
MIMETypes defineImageType:'image/x-pcx' suffix:'pcx' reader:self. |
201 | 70 |
|
630 | 71 |
"Modified: 27.6.1997 / 18:39:23 / cg" |
43 | 72 |
! ! |
73 |
||
27 | 74 |
!PCXReader class methodsFor:'testing'! |
75 |
||
76 |
isValidImageFile:aFilename |
|
77 |
"return true, if aFilename contains a PCX image" |
|
78 |
||
29 | 79 |
|count header inStream| |
27 | 80 |
|
81 |
inStream := self streamReadingFile:aFilename. |
|
82 |
inStream isNil ifTrue:[^ false]. |
|
83 |
inStream binary. |
|
84 |
||
85 |
header := ByteArray uninitializedNew:128. |
|
29 | 86 |
count := inStream nextBytes:128 into:header. |
27 | 87 |
inStream close. |
88 |
||
29 | 89 |
((count == 128) and:[self isValidPCXHeader:header]) ifFalse:[ |
27 | 90 |
^ false |
91 |
]. |
|
92 |
^ true |
|
102 | 93 |
|
94 |
"Modified: 17.9.1995 / 17:32:07 / claus" |
|
201 | 95 |
! |
96 |
||
97 |
isValidPCXHeader:aHeader |
|
98 |
"return true, if aHeader looks like a PCX image header" |
|
99 |
||
530 | 100 |
"check magic number" |
201 | 101 |
((aHeader at:1) ~~ 16r0A) ifTrue:[ |
531 | 102 |
^ false |
201 | 103 |
]. |
104 |
||
105 |
"check version" |
|
106 |
(#(0 2 3 5) includes:(aHeader at:2)) ifFalse:[ |
|
531 | 107 |
^ false |
201 | 108 |
]. |
109 |
||
110 |
^ true |
|
530 | 111 |
|
112 |
"Modified: 16.4.1997 / 22:24:32 / cg" |
|
27 | 113 |
! ! |
114 |
||
115 |
!PCXReader methodsFor:'reading from file'! |
|
116 |
||
1737 | 117 |
extractColorMap16 |
118 |
"extract the 16-entry colormap from the header" |
|
119 |
||
120 |
|rawMap srcIndex rMap gMap bMap| |
|
121 |
||
122 |
rawMap := ByteArray uninitializedNew:(16*3). |
|
123 |
rawMap replaceFrom:1 to:(16*3) with:header startingAt:17. |
|
124 |
rMap := ByteArray new:16. |
|
125 |
gMap := ByteArray new:16. |
|
126 |
bMap := ByteArray new:16. |
|
127 |
srcIndex := 1. |
|
128 |
1 to:16 do:[:i | |
|
129 |
|r g b| |
|
130 |
||
131 |
r := rawMap at:srcIndex. |
|
132 |
g := rawMap at:srcIndex+1. |
|
133 |
b := rawMap at:srcIndex+2. |
|
134 |
||
135 |
rMap at:i put:r. |
|
136 |
gMap at:i put:g. |
|
137 |
bMap at:i put:b. |
|
138 |
srcIndex := srcIndex + 3. |
|
139 |
]. |
|
140 |
^ Colormap redVector:rMap greenVector:gMap blueVector:bMap. |
|
141 |
! |
|
142 |
||
143 |
nextByteFromBufferOrStream |
|
144 |
|byte| |
|
145 |
||
146 |
nBuffer ~~ 0 ifTrue:[ |
|
147 |
byte := buffer at:bufferIndex. |
|
148 |
bufferIndex := bufferIndex + 1. nBuffer := nBuffer - 1. |
|
149 |
] ifFalse:[ |
|
150 |
byte := inStream next |
|
151 |
]. |
|
152 |
^ byte |
|
153 |
! |
|
154 |
||
155 |
readColorMap256 |
|
156 |
|rawMap mapSize rMap gMap bMap srcIndex| |
|
157 |
||
158 |
rawMap := ByteArray uninitializedNew:(256*3). |
|
159 |
nBuffer ~~ 0 ifTrue:[ |
|
160 |
mapSize := buffer size - bufferIndex + 1. |
|
161 |
mapSize := mapSize min:(256*3). |
|
162 |
rawMap replaceFrom:1 to:mapSize with:buffer startingAt:bufferIndex. |
|
163 |
nBuffer < (256*3) ifTrue:[ |
|
164 |
inStream nextBytes:((256*3)-nBuffer) into:rawMap startingAt:nBuffer+1 |
|
165 |
] |
|
166 |
] ifFalse:[ |
|
167 |
inStream nextBytes:(256*3) into:rawMap. |
|
168 |
]. |
|
169 |
rMap := Array new:256. |
|
170 |
gMap := Array new:256. |
|
171 |
bMap := Array new:256. |
|
172 |
srcIndex := 1. |
|
173 |
||
174 |
1 to:256 do:[:i | |
|
175 |
|r g b| |
|
176 |
||
177 |
r := rawMap at:srcIndex. |
|
178 |
g := rawMap at:srcIndex+1. |
|
179 |
b := rawMap at:srcIndex+2. |
|
201 | 180 |
|
1737 | 181 |
rMap at:i put:r. |
182 |
gMap at:i put:g. |
|
183 |
bMap at:i put:b. |
|
184 |
srcIndex := srcIndex + 3. |
|
185 |
]. |
|
186 |
||
187 |
^ Colormap redVector:rMap greenVector:gMap blueVector:bMap. |
|
188 |
! |
|
189 |
||
190 |
readCompressedData |
|
191 |
|bendIndex rowIndex dstIndex endIndex byte nByte value idx2 |
|
192 |
srcIndex| |
|
193 |
||
194 |
data := ByteArray uninitializedNew:(height * sourceBytesPerRow). |
|
195 |
||
196 |
buffer := ByteArray uninitializedNew:4096. |
|
197 |
bufferIndex := 1. |
|
198 |
bendIndex := 1. |
|
201 | 199 |
|
1737 | 200 |
rowIndex := 1. |
201 |
1 to:height do:[:row | |
|
202 |
dstIndex := rowIndex. |
|
203 |
endIndex := dstIndex + sourceBytesPerRow. |
|
204 |
[dstIndex < endIndex] whileTrue:[ |
|
205 |
bufferIndex == bendIndex ifTrue:[ |
|
206 |
nBuffer := inStream nextBytes:4096 into:buffer. |
|
207 |
bufferIndex := 1. |
|
208 |
bendIndex := nBuffer + 1. |
|
209 |
]. |
|
210 |
byte := buffer at:bufferIndex. |
|
211 |
bufferIndex := bufferIndex + 1. |
|
212 |
((byte bitAnd:2r11000000) ~~ 2r11000000) ifTrue:[ |
|
213 |
data at:dstIndex put:byte. |
|
214 |
dstIndex := dstIndex + 1. |
|
215 |
] ifFalse:[ |
|
216 |
nByte := byte bitAnd:2r00111111. |
|
217 |
bufferIndex == bendIndex ifTrue:[ |
|
218 |
nBuffer := inStream nextBytes:4096 into:buffer. |
|
219 |
bufferIndex := 1. |
|
220 |
bendIndex := nBuffer + 1. |
|
221 |
]. |
|
222 |
value := buffer at:bufferIndex. |
|
223 |
bufferIndex := bufferIndex + 1. |
|
224 |
idx2 := ((dstIndex + nByte) min:endIndex) - 1. |
|
225 |
data from:dstIndex to:idx2 put:value. |
|
226 |
dstIndex := dstIndex + nByte. |
|
227 |
]. |
|
228 |
]. |
|
229 |
rowIndex := endIndex. |
|
230 |
]. |
|
231 |
||
232 |
"/ have to compress - above code reads sourceBytesPerRow |
|
233 |
"/ (to keep in sync with RLE); but we want width bytesPerRow |
|
234 |
"/ Can compress in the data-area; leftover pixels are simply ignored |
|
235 |
"/ by other image processing code |
|
236 |
"/ |
|
237 |
sourceBytesPerRow ~~ width ifTrue:[ |
|
238 |
dstIndex := width + 1. |
|
239 |
srcIndex := sourceBytesPerRow + 1. |
|
240 |
2 to:height do:[:row | |
|
241 |
data replaceFrom:dstIndex to:dstIndex+width-1 with:data startingAt:srcIndex. |
|
242 |
dstIndex := dstIndex + width. |
|
243 |
srcIndex := srcIndex + sourceBytesPerRow |
|
244 |
] |
|
245 |
]. |
|
246 |
nBuffer := endIndex - bufferIndex. |
|
247 |
! |
|
248 |
||
249 |
readImage |
|
250 |
"read an image in pcx format from inStream" |
|
201 | 251 |
|
252 |
inStream binary. |
|
253 |
||
1737 | 254 |
header := ByteArray uninitializedNew:128. |
255 |
(inStream nextBytes:128 into:header) == 128 ifFalse:[ |
|
812
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
256 |
^ self fileFormatError:'short file'. |
201 | 257 |
]. |
258 |
||
259 |
(self class isValidPCXHeader:header) ifFalse:[ |
|
812
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
260 |
^ self fileFormatError:'wrong header'. |
201 | 261 |
]. |
262 |
||
1737 | 263 |
self readRestAfterHeader. |
201 | 264 |
! |
265 |
||
1737 | 266 |
readRestAfterHeader |
201 | 267 |
"read an raw image in pcx format from aStream. |
268 |
The header has already been read into the header argument." |
|
269 |
||
27 | 270 |
| inDepth version compression nPlanes xmin ymin xmax ymax |
1737 | 271 |
paletteType |
272 |
byte "{Class: SmallInteger }" |
|
273 |
nMaxPad| |
|
27 | 274 |
|
531 | 275 |
"/ typedef struct { /*header for PCX bitmap files*/ |
566 | 276 |
"/ unsigned char signature; /*1 PCX file identifier*/ |
277 |
"/ unsigned char version; /*2 version compatibility level*/ |
|
278 |
"/ unsigned char encoding; /*3 encoding method*/ |
|
279 |
"/ unsigned char bitsperpix; /*4 bits per pixel, or depth*/ |
|
280 |
"/ unsigned short Xleft; /*5 X position of left edge*/ |
|
281 |
"/ unsigned short Ytop; /*7 Y position of top edge*/ |
|
282 |
"/ unsigned short Xright; /*9 X position of right edge*/ |
|
283 |
"/ unsigned short Ybottom; /*11 Y position of bottom edge*/ |
|
284 |
"/ unsigned short Xscreensize; /*13 X screen res of source image*/ |
|
285 |
"/ unsigned short Yscreensize; /*15 Y screen res of source image*/ |
|
286 |
"/ unsigned char PCXpalette[16][3]; /*17 PCX color map*/ |
|
287 |
"/ unsigned char reserved1; /*17+48 should be 0, 1 if std res fax*/ |
|
288 |
"/ unsigned char planes; /*66 bit planes in image*/ |
|
289 |
"/ unsigned short linesize; /*67 byte delta between scanlines */ |
|
531 | 290 |
"/ unsigned short paletteinfo; /*0 == undef |
291 |
"/ 1 == color |
|
292 |
"/ 2 == grayscale*/ |
|
293 |
"/ unsigned char reserved2[58]; /*fill to struct size of 128*/ |
|
294 |
"/ } PCX_HEADER; |
|
295 |
||
27 | 296 |
version := header at:2. |
297 |
"/ 'version=' print. version printNL. |
|
298 |
compression := header at:3. |
|
299 |
"/ 'compression=' print. compression printNL. |
|
300 |
(#(0 1) includes:compression) ifFalse:[ |
|
812
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
301 |
^ self fileFormatError:'unknown compression'. |
27 | 302 |
]. |
530 | 303 |
|
27 | 304 |
inDepth := header at:4. |
305 |
"/ 'depth=' print. inDepth printNL. |
|
306 |
nPlanes := header at:66. |
|
307 |
"/ 'planes=' print. nPlanes printNL. |
|
1737 | 308 |
sourceBytesPerRow := header wordAt:67 MSB:false. |
83 | 309 |
"/ 'srcBytesPerRow=' print. srcBytesPerRow printNL. |
27 | 310 |
paletteType := header at:69. |
311 |
||
312 |
" |
|
313 |
although it would be easy to implement ... |
|
314 |
I have no test pictures for other formats. |
|
315 |
So its not (yet) implemented |
|
316 |
" |
|
317 |
((inDepth ~~ 8) or:[nPlanes ~~ 1]) ifTrue:[ |
|
812
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
318 |
"/ 'PCXReader: depth: ' errorPrint. inDepth errorPrint. |
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
319 |
"/ ' planes:' errorPrint. nPlanes errorPrintNL. |
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
320 |
^ self fileFormatError:'can only handle 1-plane 256 color images'. |
27 | 321 |
]. |
322 |
||
83 | 323 |
xmin := header wordAt:5 MSB:false. |
324 |
ymin := header wordAt:7 MSB:false. |
|
325 |
xmax := header wordAt:9 MSB:false. |
|
326 |
ymax := header wordAt:11 MSB:false. |
|
27 | 327 |
|
328 |
width := (xmax - xmin + 1). |
|
329 |
height := (ymax - ymin + 1). |
|
330 |
"/ 'width=' print. width printNL. |
|
331 |
"/ 'height=' print. width printNL. |
|
332 |
||
333 |
(version == 2) ifTrue:[ |
|
1737 | 334 |
colorMap := self extractColorMap16. |
27 | 335 |
]. |
336 |
||
29 | 337 |
compression == 1 ifTrue:[ |
1737 | 338 |
self readCompressedData |
27 | 339 |
] ifFalse:[ |
1737 | 340 |
self readUncompressedData |
27 | 341 |
]. |
342 |
||
343 |
(version == 5) ifTrue:[ |
|
1562
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
344 |
"/ RLE data is padded - skip over zeros for the 0C-byte |
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
345 |
nMaxPad := 15. |
1737 | 346 |
byte := self nextByteFromBufferOrStream. |
347 |
||
1562
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
348 |
[(byte ~~ 16r0C) and:[nMaxPad > 0]] whileTrue:[ |
1737 | 349 |
byte := self nextByteFromBufferOrStream. |
561 | 350 |
]. |
1562
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
351 |
(byte == 16r0C) ifFalse:[ |
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
352 |
'PCXREADER: no valid 256-entry palette (got' errorPrint. |
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
353 |
byte errorPrint. '; expected ' errorPrint. 16rC0 errorPrint. ')' errorPrintCR. |
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
354 |
]. |
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
355 |
|
1737 | 356 |
colorMap := self readColorMap256. |
27 | 357 |
]. |
358 |
||
359 |
photometric := #palette. |
|
360 |
samplesPerPixel := 1. |
|
361 |
bitsPerSample := #(8). |
|
362 |
||
363 |
" |
|
364 |
|i f| |
|
29 | 365 |
i := Image fromFile:'somefile.pcx'. |
366 |
i inspect. |
|
27 | 367 |
" |
499 | 368 |
|
812
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
369 |
"Modified: / 3.2.1998 / 17:59:03 / cg" |
1737 | 370 |
! |
371 |
||
372 |
readUncompressedData |
|
373 |
|dstIndex| |
|
374 |
||
375 |
" |
|
376 |
actually untested ... |
|
377 |
" |
|
378 |
data := ByteArray uninitializedNew:(height * width). |
|
379 |
sourceBytesPerRow ~~ width ifTrue:[ |
|
380 |
dstIndex := 1. |
|
381 |
1 to:height do:[:row | |
|
382 |
inStream nextBytes:width into:data startingAt:dstIndex. |
|
383 |
dstIndex := dstIndex + width. |
|
384 |
inStream skip:(sourceBytesPerRow - width). |
|
385 |
] |
|
386 |
] ifFalse:[ |
|
387 |
inStream nextBytes:(height * width) into:data. |
|
388 |
]. |
|
389 |
nBuffer := 0. |
|
27 | 390 |
! ! |
391 |
||
201 | 392 |
!PCXReader class methodsFor:'documentation'! |
393 |
||
394 |
version |
|
1737 | 395 |
^ '$Header: /cvs/stx/stx/libview2/PCXReader.st,v 1.27 2003-04-04 17:07:54 cg Exp $' |
201 | 396 |
! ! |
1737 | 397 |
|
27 | 398 |
PCXReader initialize! |