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:''
|
|
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:]
|
|
48 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader
|
|
49 |
ST80FormReader SunRasterReader TIFFReader WindowsIconReader
|
|
50 |
XBMreader XPMReader XWDReader
|
29
|
51 |
"
|
|
52 |
! !
|
|
53 |
|
43
|
54 |
!PCXReader class methodsFor:'initialization'!
|
|
55 |
|
|
56 |
initialize
|
201
|
57 |
"tell Image-class, that a new fileReader is present
|
|
58 |
for the '.pcx' and '.PCX' extensions."
|
43
|
59 |
|
201
|
60 |
Image fileFormats at:'.pcx' put:self.
|
|
61 |
Image fileFormats at:'.PCX' put:self.
|
|
62 |
|
43
|
63 |
! !
|
|
64 |
|
27
|
65 |
!PCXReader class methodsFor:'testing'!
|
|
66 |
|
|
67 |
isValidImageFile:aFilename
|
|
68 |
"return true, if aFilename contains a PCX image"
|
|
69 |
|
29
|
70 |
|count header inStream|
|
27
|
71 |
|
|
72 |
inStream := self streamReadingFile:aFilename.
|
|
73 |
inStream isNil ifTrue:[^ false].
|
|
74 |
inStream binary.
|
|
75 |
|
|
76 |
header := ByteArray uninitializedNew:128.
|
29
|
77 |
count := inStream nextBytes:128 into:header.
|
27
|
78 |
inStream close.
|
|
79 |
|
29
|
80 |
((count == 128) and:[self isValidPCXHeader:header]) ifFalse:[
|
27
|
81 |
^ false
|
|
82 |
].
|
|
83 |
^ true
|
102
|
84 |
|
|
85 |
"Modified: 17.9.1995 / 17:32:07 / claus"
|
201
|
86 |
!
|
|
87 |
|
|
88 |
isValidPCXHeader:aHeader
|
|
89 |
"return true, if aHeader looks like a PCX image header"
|
|
90 |
|
|
91 |
"check id"
|
|
92 |
((aHeader at:1) ~~ 16r0A) ifTrue:[
|
|
93 |
^ false
|
|
94 |
].
|
|
95 |
|
|
96 |
"check version"
|
|
97 |
(#(0 2 3 5) includes:(aHeader at:2)) ifFalse:[
|
|
98 |
^ false
|
|
99 |
].
|
|
100 |
|
|
101 |
^ true
|
27
|
102 |
! !
|
|
103 |
|
|
104 |
!PCXReader methodsFor:'reading from file'!
|
|
105 |
|
201
|
106 |
fromStream:aStream
|
|
107 |
"read an image in pcx format from aStream"
|
|
108 |
|
|
109 |
|fileSize header img|
|
|
110 |
|
|
111 |
inStream := aStream.
|
|
112 |
|
|
113 |
inStream binary.
|
|
114 |
fileSize := aStream size.
|
|
115 |
|
|
116 |
fileSize < 128 ifTrue:[
|
|
117 |
self error:'PCXREADER: short file'.
|
|
118 |
^ nil
|
|
119 |
].
|
|
120 |
|
|
121 |
header := ByteArray uninitializedNew:128.
|
|
122 |
aStream nextBytes:128 into:header.
|
|
123 |
|
|
124 |
(self class isValidPCXHeader:header) ifFalse:[
|
|
125 |
self error:'PCXREADER: wrong header'.
|
|
126 |
^ nil
|
|
127 |
].
|
|
128 |
|
|
129 |
img := self fromStreamWithHeader:header.
|
|
130 |
^ img
|
|
131 |
!
|
|
132 |
|
27
|
133 |
fromStreamWithHeader:header
|
201
|
134 |
"read an raw image in pcx format from aStream.
|
|
135 |
The header has already been read into the header argument."
|
|
136 |
|
27
|
137 |
| inDepth version compression nPlanes xmin ymin xmax ymax
|
|
138 |
paletteType rawMap rMap gMap bMap
|
32
|
139 |
endIndex "{Class: SmallInteger }"
|
|
140 |
srcIndex "{Class: SmallInteger }"
|
|
141 |
dstIndex "{Class: SmallInteger }"
|
|
142 |
rowIndex "{Class: SmallInteger }"
|
|
143 |
h "{Class: SmallInteger }"
|
|
144 |
byte "{Class: SmallInteger }"
|
|
145 |
nByte "{Class: SmallInteger }"
|
83
|
146 |
srcBytesPerRow "{Class: SmallInteger }"
|
29
|
147 |
value "{Class: SmallInteger }"
|
|
148 |
idx2 "{Class: SmallInteger }"
|
|
149 |
dataBytes buffer
|
|
150 |
bufferIndex "{Class: SmallInteger }"
|
35
|
151 |
bendIndex "{Class: SmallInteger }"
|
29
|
152 |
nBuffer "{Class: SmallInteger }"|
|
27
|
153 |
|
|
154 |
version := header at:2.
|
|
155 |
"/ 'version=' print. version printNL.
|
|
156 |
compression := header at:3.
|
|
157 |
"/ 'compression=' print. compression printNL.
|
|
158 |
(#(0 1) includes:compression) ifFalse:[
|
201
|
159 |
self error:'PCXREADER: unknown compression'.
|
|
160 |
^ nil
|
27
|
161 |
].
|
|
162 |
inDepth := header at:4.
|
|
163 |
"/ 'depth=' print. inDepth printNL.
|
|
164 |
nPlanes := header at:66.
|
|
165 |
"/ 'planes=' print. nPlanes printNL.
|
83
|
166 |
srcBytesPerRow := header wordAt:67 MSB:false.
|
|
167 |
"/ 'srcBytesPerRow=' print. srcBytesPerRow printNL.
|
27
|
168 |
paletteType := header at:69.
|
|
169 |
|
|
170 |
"
|
|
171 |
although it would be easy to implement ...
|
|
172 |
I have no test pictures for other formats.
|
|
173 |
So its not (yet) implemented
|
|
174 |
"
|
|
175 |
((inDepth ~~ 8) or:[nPlanes ~~ 1]) ifTrue:[
|
201
|
176 |
'PCXReader: depth: ' errorPrint. inDepth errorPrint.
|
|
177 |
' planes:' errorPrint. nPlanes errorPrintNL.
|
|
178 |
'PCXReader: can only handle 1-plane 256 color images' errorPrintNL.
|
|
179 |
^ nil
|
27
|
180 |
].
|
|
181 |
|
83
|
182 |
xmin := header wordAt:5 MSB:false.
|
|
183 |
ymin := header wordAt:7 MSB:false.
|
|
184 |
xmax := header wordAt:9 MSB:false.
|
|
185 |
ymax := header wordAt:11 MSB:false.
|
27
|
186 |
|
|
187 |
width := (xmax - xmin + 1).
|
|
188 |
height := (ymax - ymin + 1).
|
|
189 |
"/ 'width=' print. width printNL.
|
|
190 |
"/ 'height=' print. width printNL.
|
|
191 |
|
|
192 |
(version == 2) ifTrue:[
|
201
|
193 |
"read the 16-entry colormap"
|
27
|
194 |
|
201
|
195 |
rawMap := ByteArray uninitializedNew:(16*3).
|
|
196 |
rawMap replaceFrom:1 to:(16*3) with:header startingAt:17.
|
|
197 |
rMap := Array new:16.
|
|
198 |
gMap := Array new:16.
|
|
199 |
bMap := Array new:16.
|
|
200 |
srcIndex := 1.
|
|
201 |
1 to:16 do:[:i |
|
|
202 |
rMap at:i put:(rawMap at:srcIndex).
|
|
203 |
srcIndex := srcIndex + 1.
|
|
204 |
gMap at:i put:(rawMap at:srcIndex).
|
|
205 |
srcIndex := srcIndex + 1.
|
|
206 |
bMap at:i put:(rawMap at:srcIndex).
|
|
207 |
srcIndex := srcIndex + 1.
|
|
208 |
].
|
27
|
209 |
].
|
|
210 |
|
29
|
211 |
compression == 1 ifTrue:[
|
201
|
212 |
data := dataBytes := ByteArray uninitializedNew:(height * srcBytesPerRow).
|
27
|
213 |
|
201
|
214 |
buffer := ByteArray uninitializedNew:4096.
|
|
215 |
bufferIndex := 1.
|
|
216 |
bendIndex := 1.
|
29
|
217 |
|
201
|
218 |
rowIndex := 1.
|
|
219 |
h := height.
|
|
220 |
1 to:h do:[:row |
|
|
221 |
dstIndex := rowIndex.
|
|
222 |
endIndex := dstIndex + srcBytesPerRow.
|
|
223 |
[dstIndex < endIndex] whileTrue:[
|
|
224 |
bufferIndex == bendIndex ifTrue:[
|
|
225 |
nBuffer := inStream nextBytes:4096 into:buffer.
|
|
226 |
bufferIndex := 1.
|
|
227 |
bendIndex := nBuffer + 1.
|
|
228 |
].
|
|
229 |
byte := buffer at:bufferIndex.
|
|
230 |
bufferIndex := bufferIndex + 1.
|
|
231 |
((byte bitAnd:2r11000000) ~~ 2r11000000) ifTrue:[
|
|
232 |
dataBytes at:dstIndex put:byte.
|
|
233 |
dstIndex := dstIndex + 1.
|
|
234 |
] ifFalse:[
|
|
235 |
nByte := byte bitAnd:2r00111111.
|
|
236 |
bufferIndex == bendIndex ifTrue:[
|
|
237 |
nBuffer := inStream nextBytes:4096 into:buffer.
|
|
238 |
bufferIndex := 1.
|
|
239 |
bendIndex := nBuffer + 1.
|
|
240 |
].
|
|
241 |
value := buffer at:bufferIndex.
|
|
242 |
bufferIndex := bufferIndex + 1.
|
|
243 |
idx2 := ((dstIndex + nByte) min:endIndex) - 1.
|
|
244 |
dataBytes from:dstIndex to:idx2 put:value.
|
|
245 |
dstIndex := dstIndex + nByte.
|
|
246 |
].
|
|
247 |
].
|
|
248 |
rowIndex := endIndex.
|
|
249 |
].
|
29
|
250 |
|
201
|
251 |
"/ have to compress - above code reads srcBytesPerRow
|
|
252 |
"/ (to keep in sync with RLE); but we want width bytesPerRow
|
|
253 |
"/ Can compress in the data-area; leftover pixels are simply ignored
|
|
254 |
"/ by other image processing code
|
|
255 |
"/
|
|
256 |
srcBytesPerRow ~~ width ifTrue:[
|
|
257 |
dstIndex := width + 1.
|
|
258 |
srcIndex := srcBytesPerRow + 1.
|
|
259 |
2 to:h do:[:row |
|
|
260 |
dataBytes replaceFrom:dstIndex to:dstIndex+width-1 with:dataBytes startingAt:srcIndex.
|
|
261 |
dstIndex := dstIndex + width.
|
|
262 |
srcIndex := srcIndex + srcBytesPerRow
|
|
263 |
]
|
|
264 |
].
|
|
265 |
nBuffer := endIndex - bufferIndex.
|
27
|
266 |
] ifFalse:[
|
201
|
267 |
"
|
|
268 |
actually untested ...
|
|
269 |
"
|
|
270 |
data := dataBytes := ByteArray uninitializedNew:(height * width).
|
|
271 |
srcBytesPerRow ~~ width ifTrue:[
|
|
272 |
dstIndex := 1.
|
|
273 |
1 to:h do:[:row |
|
|
274 |
inStream nextBytes:width into:data startingAt:dstIndex.
|
|
275 |
dstIndex := dstIndex + width.
|
|
276 |
inStream skip:(srcBytesPerRow - width).
|
|
277 |
]
|
|
278 |
] ifFalse:[
|
|
279 |
inStream nextBytes:(height * width) into:data.
|
|
280 |
].
|
|
281 |
nBuffer := 0.
|
27
|
282 |
].
|
|
283 |
|
|
284 |
(version == 5) ifTrue:[
|
201
|
285 |
"read the 256-entry colormap"
|
27
|
286 |
|
201
|
287 |
nBuffer ~~ 0 ifTrue:[
|
|
288 |
byte := buffer at:bufferIndex.
|
|
289 |
bufferIndex := bufferIndex + 1. nBuffer := nBuffer - 1.
|
|
290 |
] ifFalse:[
|
|
291 |
byte := inStream next
|
|
292 |
].
|
29
|
293 |
|
201
|
294 |
byte == 16r0C ifFalse:[
|
|
295 |
'PCXREADER: no valid 256-entry palette (got' errorPrint.
|
|
296 |
byte errorPrint. '; expected ' errorPrint. 16rC0 errorPrint. ')' errorPrintNL.
|
|
297 |
].
|
|
298 |
rawMap := ByteArray uninitializedNew:(256*3).
|
|
299 |
nBuffer ~~ 0 ifTrue:[
|
|
300 |
rawMap replaceFrom:1 to:(256*3) with:buffer startingAt:bufferIndex.
|
|
301 |
nBuffer < (256*3) ifTrue:[
|
|
302 |
inStream nextBytes:((256*3)-nBuffer) into:rawMap startingAt:nBuffer+1
|
|
303 |
]
|
|
304 |
] ifFalse:[
|
|
305 |
inStream nextBytes:(256*3) into:rawMap.
|
|
306 |
].
|
|
307 |
rMap := Array new:256.
|
|
308 |
gMap := Array new:256.
|
|
309 |
bMap := Array new:256.
|
|
310 |
srcIndex := 1.
|
|
311 |
1 to:256 do:[:i |
|
|
312 |
rMap at:i put:(rawMap at:srcIndex).
|
|
313 |
srcIndex := srcIndex + 1.
|
|
314 |
gMap at:i put:(rawMap at:srcIndex).
|
|
315 |
srcIndex := srcIndex + 1.
|
|
316 |
bMap at:i put:(rawMap at:srcIndex).
|
|
317 |
srcIndex := srcIndex + 1.
|
|
318 |
].
|
27
|
319 |
].
|
|
320 |
|
|
321 |
photometric := #palette.
|
|
322 |
samplesPerPixel := 1.
|
|
323 |
bitsPerSample := #(8).
|
35
|
324 |
colorMap := Colormap redVector:rMap greenVector:gMap blueVector:bMap.
|
27
|
325 |
|
|
326 |
"
|
|
327 |
|i f|
|
29
|
328 |
i := Image fromFile:'somefile.pcx'.
|
|
329 |
i inspect.
|
27
|
330 |
"
|
|
331 |
! !
|
|
332 |
|
201
|
333 |
!PCXReader class methodsFor:'documentation'!
|
|
334 |
|
|
335 |
version
|
|
336 |
^ '$Header: /cvs/stx/stx/libview2/PCXReader.st,v 1.12 1996-04-23 10:36:29 cg Exp $'
|
|
337 |
! !
|
27
|
338 |
PCXReader initialize!
|