author | Claus Gittinger <cg@exept.de> |
Mon, 28 Apr 2003 12:16:30 +0200 | |
changeset 1756 | 7c4f38c59b2a |
parent 1745 | 4fa0fad2a463 |
child 1759 | f52382e5a9b8 |
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:'' |
|
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1737
diff
changeset
|
19 |
category:'Graphics-Images-Readers' |
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 |
||
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1737
diff
changeset
|
120 |
|rawMap| |
1737 | 121 |
|
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1737
diff
changeset
|
122 |
rawMap := header copyFrom:17 to:(17 + (16*3) - 1). |
1756 | 123 |
^ Colormap rgbBytesVector:rawMap |
1737 | 124 |
! |
125 |
||
126 |
nextByteFromBufferOrStream |
|
127 |
|byte| |
|
128 |
||
129 |
nBuffer ~~ 0 ifTrue:[ |
|
130 |
byte := buffer at:bufferIndex. |
|
131 |
bufferIndex := bufferIndex + 1. nBuffer := nBuffer - 1. |
|
132 |
] ifFalse:[ |
|
133 |
byte := inStream next |
|
134 |
]. |
|
135 |
^ byte |
|
136 |
! |
|
137 |
||
138 |
readColorMap256 |
|
1756 | 139 |
|rawMap mapSize| |
1737 | 140 |
|
141 |
rawMap := ByteArray uninitializedNew:(256*3). |
|
142 |
nBuffer ~~ 0 ifTrue:[ |
|
143 |
mapSize := buffer size - bufferIndex + 1. |
|
144 |
mapSize := mapSize min:(256*3). |
|
145 |
rawMap replaceFrom:1 to:mapSize with:buffer startingAt:bufferIndex. |
|
146 |
nBuffer < (256*3) ifTrue:[ |
|
147 |
inStream nextBytes:((256*3)-nBuffer) into:rawMap startingAt:nBuffer+1 |
|
148 |
] |
|
149 |
] ifFalse:[ |
|
150 |
inStream nextBytes:(256*3) into:rawMap. |
|
151 |
]. |
|
152 |
||
1756 | 153 |
^ Colormap rgbBytesVector:rawMap |
1737 | 154 |
! |
155 |
||
156 |
readCompressedData |
|
157 |
|bendIndex rowIndex dstIndex endIndex byte nByte value idx2 |
|
158 |
srcIndex| |
|
159 |
||
160 |
data := ByteArray uninitializedNew:(height * sourceBytesPerRow). |
|
161 |
||
162 |
buffer := ByteArray uninitializedNew:4096. |
|
163 |
bufferIndex := 1. |
|
164 |
bendIndex := 1. |
|
201 | 165 |
|
1737 | 166 |
rowIndex := 1. |
167 |
1 to:height do:[:row | |
|
168 |
dstIndex := rowIndex. |
|
169 |
endIndex := dstIndex + sourceBytesPerRow. |
|
170 |
[dstIndex < endIndex] whileTrue:[ |
|
171 |
bufferIndex == bendIndex ifTrue:[ |
|
172 |
nBuffer := inStream nextBytes:4096 into:buffer. |
|
173 |
bufferIndex := 1. |
|
174 |
bendIndex := nBuffer + 1. |
|
175 |
]. |
|
176 |
byte := buffer at:bufferIndex. |
|
177 |
bufferIndex := bufferIndex + 1. |
|
178 |
((byte bitAnd:2r11000000) ~~ 2r11000000) ifTrue:[ |
|
179 |
data at:dstIndex put:byte. |
|
180 |
dstIndex := dstIndex + 1. |
|
181 |
] ifFalse:[ |
|
182 |
nByte := byte bitAnd:2r00111111. |
|
183 |
bufferIndex == bendIndex ifTrue:[ |
|
184 |
nBuffer := inStream nextBytes:4096 into:buffer. |
|
185 |
bufferIndex := 1. |
|
186 |
bendIndex := nBuffer + 1. |
|
187 |
]. |
|
188 |
value := buffer at:bufferIndex. |
|
189 |
bufferIndex := bufferIndex + 1. |
|
190 |
idx2 := ((dstIndex + nByte) min:endIndex) - 1. |
|
191 |
data from:dstIndex to:idx2 put:value. |
|
192 |
dstIndex := dstIndex + nByte. |
|
193 |
]. |
|
194 |
]. |
|
195 |
rowIndex := endIndex. |
|
196 |
]. |
|
197 |
||
198 |
"/ have to compress - above code reads sourceBytesPerRow |
|
199 |
"/ (to keep in sync with RLE); but we want width bytesPerRow |
|
200 |
"/ Can compress in the data-area; leftover pixels are simply ignored |
|
201 |
"/ by other image processing code |
|
202 |
"/ |
|
203 |
sourceBytesPerRow ~~ width ifTrue:[ |
|
204 |
dstIndex := width + 1. |
|
205 |
srcIndex := sourceBytesPerRow + 1. |
|
206 |
2 to:height do:[:row | |
|
207 |
data replaceFrom:dstIndex to:dstIndex+width-1 with:data startingAt:srcIndex. |
|
208 |
dstIndex := dstIndex + width. |
|
209 |
srcIndex := srcIndex + sourceBytesPerRow |
|
210 |
] |
|
211 |
]. |
|
212 |
nBuffer := endIndex - bufferIndex. |
|
213 |
! |
|
214 |
||
215 |
readImage |
|
216 |
"read an image in pcx format from inStream" |
|
201 | 217 |
|
218 |
inStream binary. |
|
219 |
||
1737 | 220 |
header := ByteArray uninitializedNew:128. |
221 |
(inStream nextBytes:128 into:header) == 128 ifFalse:[ |
|
812
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
222 |
^ self fileFormatError:'short file'. |
201 | 223 |
]. |
224 |
||
225 |
(self class isValidPCXHeader:header) ifFalse:[ |
|
812
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
226 |
^ self fileFormatError:'wrong header'. |
201 | 227 |
]. |
228 |
||
1737 | 229 |
self readRestAfterHeader. |
201 | 230 |
! |
231 |
||
1737 | 232 |
readRestAfterHeader |
201 | 233 |
"read an raw image in pcx format from aStream. |
234 |
The header has already been read into the header argument." |
|
235 |
||
27 | 236 |
| inDepth version compression nPlanes xmin ymin xmax ymax |
1737 | 237 |
paletteType |
238 |
byte "{Class: SmallInteger }" |
|
239 |
nMaxPad| |
|
27 | 240 |
|
531 | 241 |
"/ typedef struct { /*header for PCX bitmap files*/ |
566 | 242 |
"/ unsigned char signature; /*1 PCX file identifier*/ |
243 |
"/ unsigned char version; /*2 version compatibility level*/ |
|
244 |
"/ unsigned char encoding; /*3 encoding method*/ |
|
245 |
"/ unsigned char bitsperpix; /*4 bits per pixel, or depth*/ |
|
246 |
"/ unsigned short Xleft; /*5 X position of left edge*/ |
|
247 |
"/ unsigned short Ytop; /*7 Y position of top edge*/ |
|
248 |
"/ unsigned short Xright; /*9 X position of right edge*/ |
|
249 |
"/ unsigned short Ybottom; /*11 Y position of bottom edge*/ |
|
250 |
"/ unsigned short Xscreensize; /*13 X screen res of source image*/ |
|
251 |
"/ unsigned short Yscreensize; /*15 Y screen res of source image*/ |
|
252 |
"/ unsigned char PCXpalette[16][3]; /*17 PCX color map*/ |
|
253 |
"/ unsigned char reserved1; /*17+48 should be 0, 1 if std res fax*/ |
|
254 |
"/ unsigned char planes; /*66 bit planes in image*/ |
|
255 |
"/ unsigned short linesize; /*67 byte delta between scanlines */ |
|
531 | 256 |
"/ unsigned short paletteinfo; /*0 == undef |
257 |
"/ 1 == color |
|
258 |
"/ 2 == grayscale*/ |
|
259 |
"/ unsigned char reserved2[58]; /*fill to struct size of 128*/ |
|
260 |
"/ } PCX_HEADER; |
|
261 |
||
27 | 262 |
version := header at:2. |
263 |
"/ 'version=' print. version printNL. |
|
264 |
compression := header at:3. |
|
265 |
"/ 'compression=' print. compression printNL. |
|
266 |
(#(0 1) includes:compression) ifFalse:[ |
|
812
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
267 |
^ self fileFormatError:'unknown compression'. |
27 | 268 |
]. |
530 | 269 |
|
27 | 270 |
inDepth := header at:4. |
271 |
"/ 'depth=' print. inDepth printNL. |
|
272 |
nPlanes := header at:66. |
|
273 |
"/ 'planes=' print. nPlanes printNL. |
|
1737 | 274 |
sourceBytesPerRow := header wordAt:67 MSB:false. |
83 | 275 |
"/ 'srcBytesPerRow=' print. srcBytesPerRow printNL. |
27 | 276 |
paletteType := header at:69. |
277 |
||
278 |
" |
|
279 |
although it would be easy to implement ... |
|
280 |
I have no test pictures for other formats. |
|
281 |
So its not (yet) implemented |
|
282 |
" |
|
283 |
((inDepth ~~ 8) or:[nPlanes ~~ 1]) ifTrue:[ |
|
812
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
284 |
"/ 'PCXReader: depth: ' errorPrint. inDepth errorPrint. |
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
285 |
"/ ' planes:' errorPrint. nPlanes errorPrintNL. |
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
286 |
^ self fileFormatError:'can only handle 1-plane 256 color images'. |
27 | 287 |
]. |
288 |
||
83 | 289 |
xmin := header wordAt:5 MSB:false. |
290 |
ymin := header wordAt:7 MSB:false. |
|
291 |
xmax := header wordAt:9 MSB:false. |
|
292 |
ymax := header wordAt:11 MSB:false. |
|
27 | 293 |
|
294 |
width := (xmax - xmin + 1). |
|
295 |
height := (ymax - ymin + 1). |
|
296 |
"/ 'width=' print. width printNL. |
|
297 |
"/ 'height=' print. width printNL. |
|
298 |
||
299 |
(version == 2) ifTrue:[ |
|
1737 | 300 |
colorMap := self extractColorMap16. |
27 | 301 |
]. |
302 |
||
29 | 303 |
compression == 1 ifTrue:[ |
1737 | 304 |
self readCompressedData |
27 | 305 |
] ifFalse:[ |
1737 | 306 |
self readUncompressedData |
27 | 307 |
]. |
308 |
||
309 |
(version == 5) ifTrue:[ |
|
1562
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
310 |
"/ 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
|
311 |
nMaxPad := 15. |
1737 | 312 |
byte := self nextByteFromBufferOrStream. |
313 |
||
1562
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
314 |
[(byte ~~ 16r0C) and:[nMaxPad > 0]] whileTrue:[ |
1737 | 315 |
byte := self nextByteFromBufferOrStream. |
561 | 316 |
]. |
1562
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
317 |
(byte == 16r0C) ifFalse:[ |
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
318 |
'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
|
319 |
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
|
320 |
]. |
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
321 |
|
1737 | 322 |
colorMap := self readColorMap256. |
27 | 323 |
]. |
324 |
||
325 |
photometric := #palette. |
|
326 |
samplesPerPixel := 1. |
|
327 |
bitsPerSample := #(8). |
|
328 |
||
329 |
" |
|
330 |
|i f| |
|
29 | 331 |
i := Image fromFile:'somefile.pcx'. |
332 |
i inspect. |
|
27 | 333 |
" |
499 | 334 |
|
812
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
335 |
"Modified: / 3.2.1998 / 17:59:03 / cg" |
1737 | 336 |
! |
337 |
||
338 |
readUncompressedData |
|
339 |
|dstIndex| |
|
340 |
||
341 |
" |
|
342 |
actually untested ... |
|
343 |
" |
|
344 |
data := ByteArray uninitializedNew:(height * width). |
|
345 |
sourceBytesPerRow ~~ width ifTrue:[ |
|
346 |
dstIndex := 1. |
|
347 |
1 to:height do:[:row | |
|
348 |
inStream nextBytes:width into:data startingAt:dstIndex. |
|
349 |
dstIndex := dstIndex + width. |
|
350 |
inStream skip:(sourceBytesPerRow - width). |
|
351 |
] |
|
352 |
] ifFalse:[ |
|
353 |
inStream nextBytes:(height * width) into:data. |
|
354 |
]. |
|
355 |
nBuffer := 0. |
|
27 | 356 |
! ! |
357 |
||
201 | 358 |
!PCXReader class methodsFor:'documentation'! |
359 |
||
360 |
version |
|
1756 | 361 |
^ '$Header: /cvs/stx/stx/libview2/PCXReader.st,v 1.29 2003-04-28 10:16:30 cg Exp $' |
201 | 362 |
! ! |
1737 | 363 |
|
27 | 364 |
PCXReader initialize! |