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