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