author | Claus Gittinger <cg@exept.de> |
Wed, 03 Dec 2008 13:01:17 +0100 | |
changeset 2573 | 8259385b69e3 |
parent 1848 | 864ca2cd4e71 |
child 3855 | 1db7742d33ad |
child 3990 | 1edec82fe2fa |
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 |
||
1848 | 115 |
!PCXReader methodsFor:'private-reading'! |
27 | 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). |
1759
f52382e5a9b8
slowly getting rid of the Colormap class
Claus Gittinger <cg@exept.de>
parents:
1756
diff
changeset
|
123 |
^ MappedPalette 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 |
||
1759
f52382e5a9b8
slowly getting rid of the Colormap class
Claus Gittinger <cg@exept.de>
parents:
1756
diff
changeset
|
153 |
^ MappedPalette 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 |
readRestAfterHeader |
|
201 | 216 |
"read an raw image in pcx format from aStream. |
217 |
The header has already been read into the header argument." |
|
218 |
||
27 | 219 |
| inDepth version compression nPlanes xmin ymin xmax ymax |
1737 | 220 |
paletteType |
221 |
byte "{Class: SmallInteger }" |
|
222 |
nMaxPad| |
|
27 | 223 |
|
531 | 224 |
"/ typedef struct { /*header for PCX bitmap files*/ |
566 | 225 |
"/ unsigned char signature; /*1 PCX file identifier*/ |
226 |
"/ unsigned char version; /*2 version compatibility level*/ |
|
227 |
"/ unsigned char encoding; /*3 encoding method*/ |
|
228 |
"/ unsigned char bitsperpix; /*4 bits per pixel, or depth*/ |
|
229 |
"/ unsigned short Xleft; /*5 X position of left edge*/ |
|
230 |
"/ unsigned short Ytop; /*7 Y position of top edge*/ |
|
231 |
"/ unsigned short Xright; /*9 X position of right edge*/ |
|
232 |
"/ unsigned short Ybottom; /*11 Y position of bottom edge*/ |
|
233 |
"/ unsigned short Xscreensize; /*13 X screen res of source image*/ |
|
234 |
"/ unsigned short Yscreensize; /*15 Y screen res of source image*/ |
|
235 |
"/ unsigned char PCXpalette[16][3]; /*17 PCX color map*/ |
|
236 |
"/ unsigned char reserved1; /*17+48 should be 0, 1 if std res fax*/ |
|
237 |
"/ unsigned char planes; /*66 bit planes in image*/ |
|
238 |
"/ unsigned short linesize; /*67 byte delta between scanlines */ |
|
531 | 239 |
"/ unsigned short paletteinfo; /*0 == undef |
240 |
"/ 1 == color |
|
241 |
"/ 2 == grayscale*/ |
|
242 |
"/ unsigned char reserved2[58]; /*fill to struct size of 128*/ |
|
243 |
"/ } PCX_HEADER; |
|
244 |
||
27 | 245 |
version := header at:2. |
246 |
"/ 'version=' print. version printNL. |
|
247 |
compression := header at:3. |
|
248 |
"/ 'compression=' print. compression printNL. |
|
249 |
(#(0 1) includes:compression) ifFalse:[ |
|
812
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
250 |
^ self fileFormatError:'unknown compression'. |
27 | 251 |
]. |
530 | 252 |
|
27 | 253 |
inDepth := header at:4. |
254 |
"/ 'depth=' print. inDepth printNL. |
|
255 |
nPlanes := header at:66. |
|
256 |
"/ 'planes=' print. nPlanes printNL. |
|
1737 | 257 |
sourceBytesPerRow := header wordAt:67 MSB:false. |
83 | 258 |
"/ 'srcBytesPerRow=' print. srcBytesPerRow printNL. |
27 | 259 |
paletteType := header at:69. |
260 |
||
261 |
" |
|
262 |
although it would be easy to implement ... |
|
263 |
I have no test pictures for other formats. |
|
264 |
So its not (yet) implemented |
|
265 |
" |
|
266 |
((inDepth ~~ 8) or:[nPlanes ~~ 1]) ifTrue:[ |
|
812
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
267 |
"/ 'PCXReader: depth: ' errorPrint. inDepth errorPrint. |
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
268 |
"/ ' planes:' errorPrint. nPlanes errorPrintNL. |
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
269 |
^ self fileFormatError:'can only handle 1-plane 256 color images'. |
27 | 270 |
]. |
271 |
||
83 | 272 |
xmin := header wordAt:5 MSB:false. |
273 |
ymin := header wordAt:7 MSB:false. |
|
274 |
xmax := header wordAt:9 MSB:false. |
|
275 |
ymax := header wordAt:11 MSB:false. |
|
27 | 276 |
|
277 |
width := (xmax - xmin + 1). |
|
278 |
height := (ymax - ymin + 1). |
|
279 |
"/ 'width=' print. width printNL. |
|
280 |
"/ 'height=' print. width printNL. |
|
281 |
||
1846 | 282 |
self reportDimension. |
283 |
||
27 | 284 |
(version == 2) ifTrue:[ |
1737 | 285 |
colorMap := self extractColorMap16. |
27 | 286 |
]. |
287 |
||
29 | 288 |
compression == 1 ifTrue:[ |
1737 | 289 |
self readCompressedData |
27 | 290 |
] ifFalse:[ |
1737 | 291 |
self readUncompressedData |
27 | 292 |
]. |
293 |
||
294 |
(version == 5) ifTrue:[ |
|
1562
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
295 |
"/ 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
|
296 |
nMaxPad := 15. |
1737 | 297 |
byte := self nextByteFromBufferOrStream. |
298 |
||
1562
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
299 |
[(byte ~~ 16r0C) and:[nMaxPad > 0]] whileTrue:[ |
1737 | 300 |
byte := self nextByteFromBufferOrStream. |
561 | 301 |
]. |
1562
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
302 |
(byte == 16r0C) ifFalse:[ |
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
303 |
'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
|
304 |
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
|
305 |
]. |
3b444efb61ee
bug fix: palette is padded after RLE data.
Claus Gittinger <cg@exept.de>
parents:
812
diff
changeset
|
306 |
|
1737 | 307 |
colorMap := self readColorMap256. |
27 | 308 |
]. |
309 |
||
310 |
photometric := #palette. |
|
311 |
samplesPerPixel := 1. |
|
312 |
bitsPerSample := #(8). |
|
313 |
||
314 |
" |
|
315 |
|i f| |
|
29 | 316 |
i := Image fromFile:'somefile.pcx'. |
317 |
i inspect. |
|
27 | 318 |
" |
499 | 319 |
|
812
01d6b05a1276
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
320 |
"Modified: / 3.2.1998 / 17:59:03 / cg" |
1737 | 321 |
! |
322 |
||
323 |
readUncompressedData |
|
324 |
|dstIndex| |
|
325 |
||
326 |
" |
|
327 |
actually untested ... |
|
328 |
" |
|
329 |
data := ByteArray uninitializedNew:(height * width). |
|
330 |
sourceBytesPerRow ~~ width ifTrue:[ |
|
331 |
dstIndex := 1. |
|
332 |
1 to:height do:[:row | |
|
333 |
inStream nextBytes:width into:data startingAt:dstIndex. |
|
334 |
dstIndex := dstIndex + width. |
|
335 |
inStream skip:(sourceBytesPerRow - width). |
|
336 |
] |
|
337 |
] ifFalse:[ |
|
338 |
inStream nextBytes:(height * width) into:data. |
|
339 |
]. |
|
340 |
nBuffer := 0. |
|
27 | 341 |
! ! |
342 |
||
1848 | 343 |
!PCXReader methodsFor:'reading'! |
344 |
||
345 |
readImage |
|
346 |
"read an image in pcx format from inStream" |
|
347 |
||
348 |
inStream binary. |
|
349 |
||
350 |
header := ByteArray uninitializedNew:128. |
|
351 |
(inStream nextBytes:128 into:header) == 128 ifFalse:[ |
|
352 |
^ self fileFormatError:'short file'. |
|
353 |
]. |
|
354 |
||
355 |
(self class isValidPCXHeader:header) ifFalse:[ |
|
356 |
^ self fileFormatError:'wrong header'. |
|
357 |
]. |
|
358 |
||
359 |
self readRestAfterHeader. |
|
360 |
! ! |
|
361 |
||
201 | 362 |
!PCXReader class methodsFor:'documentation'! |
363 |
||
364 |
version |
|
1848 | 365 |
^ '$Header: /cvs/stx/stx/libview2/PCXReader.st,v 1.33 2003-11-19 15:38:20 cg Exp $' |
201 | 366 |
! ! |
1737 | 367 |
|
27 | 368 |
PCXReader initialize! |