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