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 |
|
398
|
61 |
Image addReader:self suffix:'pcx'.
|
201
|
62 |
|
398
|
63 |
"Modified: 1.2.1997 / 15:02:32 / 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:[
|
531
|
120 |
self error:'PCXREADER: short file'.
|
|
121 |
^ nil
|
201
|
122 |
].
|
|
123 |
|
|
124 |
header := ByteArray uninitializedNew:128.
|
|
125 |
aStream nextBytes:128 into:header.
|
|
126 |
|
|
127 |
(self class isValidPCXHeader:header) ifFalse:[
|
531
|
128 |
self error:'PCXREADER: wrong header'.
|
|
129 |
^ nil
|
201
|
130 |
].
|
|
131 |
|
|
132 |
img := self fromStreamWithHeader:header.
|
|
133 |
^ img
|
|
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*/
|
|
159 |
"/ unsigned char signature; /*PCX file identifier*/
|
|
160 |
"/ unsigned char version; /*version compatibility level*/
|
|
161 |
"/ unsigned char encoding; /*encoding method*/
|
|
162 |
"/ unsigned char bitsperpix; /*bits per pixel, or depth*/
|
|
163 |
"/ unsigned short Xleft; /*X position of left edge*/
|
|
164 |
"/ unsigned short Ytop; /*Y position of top edge*/
|
|
165 |
"/ unsigned short Xright; /*X position of right edge*/
|
|
166 |
"/ unsigned short Ybottom; /*Y position of bottom edge*/
|
|
167 |
"/ unsigned short Xscreensize; /*X screen res of source image*/
|
|
168 |
"/ unsigned short Yscreensize; /*Y screen res of source image*/
|
|
169 |
"/ unsigned char PCXpalette[16][3]; /*PCX color map*/
|
|
170 |
"/ unsigned char reserved1; /*should be 0, 1 if std res fax*/
|
|
171 |
"/ unsigned char planes; /*bit planes in image*/
|
|
172 |
"/ unsigned short linesize; /*byte delta between scanlines */
|
|
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:[
|
561
|
184 |
self error:'PCXREADER: unknown compression'.
|
|
185 |
^ nil
|
27
|
186 |
].
|
530
|
187 |
|
27
|
188 |
inDepth := header at:4.
|
|
189 |
"/ 'depth=' print. inDepth printNL.
|
|
190 |
nPlanes := header at:66.
|
|
191 |
"/ 'planes=' print. nPlanes printNL.
|
83
|
192 |
srcBytesPerRow := header wordAt:67 MSB:false.
|
|
193 |
"/ 'srcBytesPerRow=' print. srcBytesPerRow printNL.
|
27
|
194 |
paletteType := header at:69.
|
|
195 |
|
|
196 |
"
|
|
197 |
although it would be easy to implement ...
|
|
198 |
I have no test pictures for other formats.
|
|
199 |
So its not (yet) implemented
|
|
200 |
"
|
|
201 |
((inDepth ~~ 8) or:[nPlanes ~~ 1]) ifTrue:[
|
561
|
202 |
'PCXReader: depth: ' errorPrint. inDepth errorPrint.
|
|
203 |
' planes:' errorPrint. nPlanes errorPrintNL.
|
|
204 |
'PCXReader: can only handle 1-plane 256 color images' errorPrintNL.
|
|
205 |
^ nil
|
27
|
206 |
].
|
|
207 |
|
83
|
208 |
xmin := header wordAt:5 MSB:false.
|
|
209 |
ymin := header wordAt:7 MSB:false.
|
|
210 |
xmax := header wordAt:9 MSB:false.
|
|
211 |
ymax := header wordAt:11 MSB:false.
|
27
|
212 |
|
|
213 |
width := (xmax - xmin + 1).
|
|
214 |
height := (ymax - ymin + 1).
|
|
215 |
"/ 'width=' print. width printNL.
|
|
216 |
"/ 'height=' print. width printNL.
|
|
217 |
|
|
218 |
(version == 2) ifTrue:[
|
561
|
219 |
"read the 16-entry colormap"
|
27
|
220 |
|
561
|
221 |
rawMap := ByteArray uninitializedNew:(16*3).
|
|
222 |
rawMap replaceFrom:1 to:(16*3) with:header startingAt:17.
|
|
223 |
rMap := ByteArray new:16.
|
|
224 |
gMap := ByteArray new:16.
|
|
225 |
bMap := ByteArray new:16.
|
|
226 |
srcIndex := 1.
|
|
227 |
1 to:16 do:[:i |
|
|
228 |
rMap at:i put:(rawMap at:srcIndex).
|
|
229 |
srcIndex := srcIndex + 1.
|
|
230 |
gMap at:i put:(rawMap at:srcIndex).
|
|
231 |
srcIndex := srcIndex + 1.
|
|
232 |
bMap at:i put:(rawMap at:srcIndex).
|
|
233 |
srcIndex := srcIndex + 1.
|
|
234 |
].
|
27
|
235 |
].
|
|
236 |
|
29
|
237 |
compression == 1 ifTrue:[
|
561
|
238 |
data := dataBytes := ByteArray uninitializedNew:(height * srcBytesPerRow).
|
27
|
239 |
|
561
|
240 |
buffer := ByteArray uninitializedNew:4096.
|
|
241 |
bufferIndex := 1.
|
|
242 |
bendIndex := 1.
|
29
|
243 |
|
561
|
244 |
rowIndex := 1.
|
|
245 |
h := height.
|
|
246 |
1 to:h do:[:row |
|
|
247 |
dstIndex := rowIndex.
|
|
248 |
endIndex := dstIndex + srcBytesPerRow.
|
|
249 |
[dstIndex < endIndex] whileTrue:[
|
|
250 |
bufferIndex == bendIndex ifTrue:[
|
|
251 |
nBuffer := inStream nextBytes:4096 into:buffer.
|
|
252 |
bufferIndex := 1.
|
|
253 |
bendIndex := nBuffer + 1.
|
|
254 |
].
|
|
255 |
byte := buffer at:bufferIndex.
|
|
256 |
bufferIndex := bufferIndex + 1.
|
|
257 |
((byte bitAnd:2r11000000) ~~ 2r11000000) ifTrue:[
|
|
258 |
dataBytes at:dstIndex put:byte.
|
|
259 |
dstIndex := dstIndex + 1.
|
|
260 |
] ifFalse:[
|
|
261 |
nByte := byte bitAnd:2r00111111.
|
|
262 |
bufferIndex == bendIndex ifTrue:[
|
|
263 |
nBuffer := inStream nextBytes:4096 into:buffer.
|
|
264 |
bufferIndex := 1.
|
|
265 |
bendIndex := nBuffer + 1.
|
|
266 |
].
|
|
267 |
value := buffer at:bufferIndex.
|
|
268 |
bufferIndex := bufferIndex + 1.
|
|
269 |
idx2 := ((dstIndex + nByte) min:endIndex) - 1.
|
|
270 |
dataBytes from:dstIndex to:idx2 put:value.
|
|
271 |
dstIndex := dstIndex + nByte.
|
|
272 |
].
|
|
273 |
].
|
|
274 |
rowIndex := endIndex.
|
|
275 |
].
|
29
|
276 |
|
561
|
277 |
"/ have to compress - above code reads srcBytesPerRow
|
|
278 |
"/ (to keep in sync with RLE); but we want width bytesPerRow
|
|
279 |
"/ Can compress in the data-area; leftover pixels are simply ignored
|
|
280 |
"/ by other image processing code
|
|
281 |
"/
|
|
282 |
srcBytesPerRow ~~ width ifTrue:[
|
|
283 |
dstIndex := width + 1.
|
|
284 |
srcIndex := srcBytesPerRow + 1.
|
|
285 |
2 to:h do:[:row |
|
|
286 |
dataBytes replaceFrom:dstIndex to:dstIndex+width-1 with:dataBytes startingAt:srcIndex.
|
|
287 |
dstIndex := dstIndex + width.
|
|
288 |
srcIndex := srcIndex + srcBytesPerRow
|
|
289 |
]
|
|
290 |
].
|
|
291 |
nBuffer := endIndex - bufferIndex.
|
27
|
292 |
] ifFalse:[
|
561
|
293 |
"
|
|
294 |
uncompressed; actually untested ...
|
|
295 |
"
|
|
296 |
data := dataBytes := ByteArray uninitializedNew:(height * width).
|
|
297 |
srcBytesPerRow ~~ width ifTrue:[
|
|
298 |
dstIndex := 1.
|
|
299 |
1 to:h do:[:row |
|
|
300 |
inStream nextBytes:width into:data startingAt:dstIndex.
|
|
301 |
dstIndex := dstIndex + width.
|
|
302 |
inStream skip:(srcBytesPerRow - width).
|
|
303 |
]
|
|
304 |
] ifFalse:[
|
|
305 |
inStream nextBytes:(height * width) into:data.
|
|
306 |
].
|
|
307 |
nBuffer := 0.
|
27
|
308 |
].
|
|
309 |
|
|
310 |
(version == 5) ifTrue:[
|
561
|
311 |
"read the 256-entry colormap"
|
27
|
312 |
|
561
|
313 |
nBuffer ~~ 0 ifTrue:[
|
|
314 |
byte := buffer at:bufferIndex.
|
|
315 |
bufferIndex := bufferIndex + 1. nBuffer := nBuffer - 1.
|
|
316 |
] ifFalse:[
|
|
317 |
byte := inStream next
|
|
318 |
].
|
29
|
319 |
|
561
|
320 |
byte == 16r0C ifFalse:[
|
|
321 |
'PCXREADER: no valid 256-entry palette (got' errorPrint.
|
|
322 |
byte errorPrint. '; expected ' errorPrint. 16rC0 errorPrint. ')' errorPrintNL.
|
|
323 |
].
|
|
324 |
rawMap := ByteArray uninitializedNew:(256*3).
|
|
325 |
nBuffer ~~ 0 ifTrue:[
|
|
326 |
mapSize := buffer size - bufferIndex + 1.
|
|
327 |
mapSize := mapSize min:(256*3).
|
|
328 |
rawMap replaceFrom:1 to:mapSize with:buffer startingAt:bufferIndex.
|
|
329 |
nBuffer < (256*3) ifTrue:[
|
|
330 |
inStream nextBytes:((256*3)-nBuffer) into:rawMap startingAt:nBuffer+1
|
|
331 |
]
|
|
332 |
] ifFalse:[
|
|
333 |
inStream nextBytes:(256*3) into:rawMap.
|
|
334 |
].
|
|
335 |
rMap := Array new:256.
|
|
336 |
gMap := Array new:256.
|
|
337 |
bMap := Array new:256.
|
|
338 |
srcIndex := 1.
|
|
339 |
1 to:256 do:[:i |
|
|
340 |
rMap at:i put:(rawMap at:srcIndex).
|
|
341 |
srcIndex := srcIndex + 1.
|
|
342 |
gMap at:i put:(rawMap at:srcIndex).
|
|
343 |
srcIndex := srcIndex + 1.
|
|
344 |
bMap at:i put:(rawMap at:srcIndex).
|
|
345 |
srcIndex := srcIndex + 1.
|
|
346 |
].
|
27
|
347 |
].
|
|
348 |
|
|
349 |
photometric := #palette.
|
|
350 |
samplesPerPixel := 1.
|
|
351 |
bitsPerSample := #(8).
|
35
|
352 |
colorMap := Colormap redVector:rMap greenVector:gMap blueVector:bMap.
|
27
|
353 |
|
|
354 |
"
|
|
355 |
|i f|
|
29
|
356 |
i := Image fromFile:'somefile.pcx'.
|
|
357 |
i inspect.
|
27
|
358 |
"
|
499
|
359 |
|
561
|
360 |
"Modified: 24.4.1997 / 19:38:52 / cg"
|
27
|
361 |
! !
|
|
362 |
|
201
|
363 |
!PCXReader class methodsFor:'documentation'!
|
|
364 |
|
|
365 |
version
|
561
|
366 |
^ '$Header: /cvs/stx/stx/libview2/PCXReader.st,v 1.21 1997-04-24 17:47:57 cg Exp $'
|
201
|
367 |
! !
|
27
|
368 |
PCXReader initialize!
|