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