author | Claus Gittinger <cg@exept.de> |
Thu, 06 Mar 1997 15:16:03 +0100 | |
changeset 494 | ce8c074d5e6b |
parent 461 | bacef118f54a |
child 623 | ea587e8fd435 |
permissions | -rw-r--r-- |
44 | 1 |
" |
204 | 2 |
COPYRIGHT (c) 1995 by Claus Gittinger |
3 |
All Rights Reserved |
|
44 | 4 |
|
204 | 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. |
|
51 | 11 |
|
12 |
||
204 | 13 |
The above copyright does not apply to: |
14 |
XWDReader>>save:onFile: |
|
15 |
which was written by Brad Schoening <brad@boole.com> |
|
16 |
who placed it into the public domain. |
|
44 | 17 |
" |
42 | 18 |
|
391 | 19 |
ImageReader subclass:#XWDReader |
204 | 20 |
instanceVariableNames:'' |
21 |
classVariableNames:'' |
|
22 |
poolDictionaries:'' |
|
259 | 23 |
category:'Graphics-Images-Support' |
42 | 24 |
! |
25 |
||
391 | 26 |
!XWDReader class methodsFor:'documentation'! |
44 | 27 |
|
28 |
copyright |
|
29 |
" |
|
204 | 30 |
COPYRIGHT (c) 1995 by Claus Gittinger |
31 |
All Rights Reserved |
|
44 | 32 |
|
204 | 33 |
This software is furnished under a license and may be used |
34 |
only in accordance with the terms of that license and with the |
|
35 |
inclusion of the above copyright notice. This software may not |
|
36 |
be provided or otherwise made available to, or used by, any |
|
37 |
other person. No title to or ownership of the software is |
|
38 |
hereby transferred. |
|
51 | 39 |
|
40 |
||
204 | 41 |
The above copyright does not apply to: |
42 |
XWDReader>>save:onFile: |
|
43 |
which was written by Brad Schoening <brad@boole.com> |
|
44 |
who placed it into the public domain. |
|
44 | 45 |
" |
46 |
! |
|
47 |
||
48 |
documentation |
|
49 |
" |
|
205 | 50 |
this class provides methods for loading/saving of x-window dump (xwd) images. |
204 | 51 |
Both reading and writing of images is supported. |
52 |
||
53 |
[See also:] |
|
234 | 54 |
Image Form Icon |
204 | 55 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader |
210 | 56 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
204 | 57 |
XBMReader XPMReader |
44 | 58 |
" |
59 |
! ! |
|
60 |
||
398 | 61 |
!XWDReader class methodsFor:'initialization'! |
62 |
||
63 |
initialize |
|
64 |
"tell Image-class, that a new fileReader is present |
|
65 |
for the '.xwd' extension." |
|
66 |
||
67 |
Image addReader:self suffix:'xwd'. |
|
68 |
||
69 |
"Created: 1.2.1997 / 15:04:46 / cg" |
|
70 |
! ! |
|
71 |
||
391 | 72 |
!XWDReader class methodsFor:'queries'! |
51 | 73 |
|
74 |
canRepresent:anImage |
|
391 | 75 |
"return true, if anImage can be represented in my file format. |
76 |
Only depth8 palette images are supported." |
|
51 | 77 |
|
391 | 78 |
anImage depth ~~ 8 ifTrue:[^ false]. |
79 |
anImage photometric ~~ #palette ifTrue:[^ false]. |
|
80 |
^ true |
|
51 | 81 |
! ! |
82 |
||
391 | 83 |
!XWDReader methodsFor:'image reading'! |
42 | 84 |
|
204 | 85 |
fromStream:aStream |
391 | 86 |
"read an image in XWD (X Window Dump) format from aStream." |
42 | 87 |
|
391 | 88 |
|header nColors pad |
89 |
srcRowByteSize bytesPerRow bitsPerPixel colormapSize depth |
|
90 |
dstIndex| |
|
42 | 91 |
|
92 |
aStream binary. |
|
93 |
||
391 | 94 |
header := (1 to: 25) collect: [:i | aStream nextLong]. |
95 |
||
96 |
"skip ..." |
|
97 |
101 to:(header at: 1) do: [:i | aStream next]. |
|
98 |
||
99 |
depth := header at: 4. |
|
100 |
width := header at: 5. |
|
101 |
height := header at: 6. |
|
102 |
pad := header at: 11. |
|
42 | 103 |
|
391 | 104 |
bitsPerPixel := header at: 12. |
105 |
bitsPerPixel == 24 ifTrue:[ |
|
106 |
bitsPerSample := #(8 8 8). |
|
107 |
samplesPerPixel := 3. |
|
108 |
photometric := #rgb |
|
109 |
] ifFalse:[ |
|
110 |
bitsPerSample := Array with:bitsPerPixel. |
|
111 |
samplesPerPixel := 1. |
|
112 |
photometric := #palette |
|
113 |
]. |
|
114 |
"/ depth ~~ bitsPerPixel ifTrue:[self halt]. |
|
115 |
||
116 |
colormapSize := header at: 19. |
|
117 |
nColors := header at: 20. |
|
44 | 118 |
|
391 | 119 |
colorMap := Array new:colormapSize. |
120 |
||
121 |
1 to:nColors do:[:i | |
|
122 |
|clr r g b| |
|
123 |
||
124 |
aStream nextLong. |
|
125 |
r := aStream nextUnsignedShortMSB:true. |
|
126 |
g := aStream nextUnsignedShortMSB:true. |
|
127 |
b := aStream nextUnsignedShortMSB:true. |
|
128 |
clr := ColorValue scaledRed: (r bitShift: -3) |
|
129 |
scaledGreen: (g bitShift: -3) |
|
130 |
scaledBlue: (b bitShift: -3). |
|
131 |
colorMap at:i put:clr. |
|
132 |
aStream nextWord. |
|
44 | 133 |
]. |
134 |
||
391 | 135 |
nColors+1 to:colormapSize do: [:i | colorMap at:i put:Color black]. |
136 |
||
137 |
bytesPerRow := width * bitsPerPixel // 8. |
|
138 |
((width * bitsPerPixel \\ 8) ~~ 0) ifTrue:[ |
|
139 |
bytesPerRow := bytesPerRow + 1 |
|
44 | 140 |
]. |
391 | 141 |
srcRowByteSize := width * bitsPerPixel + pad - 1 // pad * (pad / 8). |
42 | 142 |
|
391 | 143 |
data := ByteArray uninitializedNew: srcRowByteSize * height. |
144 |
srcRowByteSize == bytesPerRow ifTrue:[ |
|
145 |
aStream nextBytes:srcRowByteSize * height into:data. |
|
146 |
] ifFalse:[ |
|
147 |
dstIndex := 1. |
|
148 |
1 to:height do:[:y | |
|
149 |
aStream nextBytes:bytesPerRow into:data startingAt:dstIndex. |
|
150 |
aStream next:(srcRowByteSize - bytesPerRow). |
|
151 |
dstIndex := dstIndex + bytesPerRow. |
|
390 | 152 |
]. |
391 | 153 |
] |
154 |
" |
|
155 |
XWDReader fromFile:'testfile.xwd' |
|
42 | 156 |
" |
49 | 157 |
" |
391 | 158 |
XWDReader save:(Image fromUser) onFile: '/tmp/st.xwd' |
159 |
(Image fromFile: '/tmp/st.xwd') inspect |
|
160 |
" |
|
42 | 161 |
! ! |
162 |
||
391 | 163 |
!XWDReader methodsFor:'image writing'! |
49 | 164 |
|
165 |
save:image onFile:fileName |
|
166 |
"Save as a version 7 color X11 window dump file (xwd) to the file fileName. |
|
167 |
This produces a mapped color table with 16 bit color. The xwd file can be |
|
204 | 168 |
viewed by the xwud program and printed with xpr. |
169 |
No compression is performed. |
|
49 | 170 |
|
205 | 171 |
See the file ...include/X11/XWDFile.h for a definition of the format." |
49 | 172 |
|
205 | 173 |
" |
49 | 174 |
Notice: |
204 | 175 |
this method was adapted from a goody in the uiuc archive |
176 |
(Prime time freeware). |
|
177 |
The original files header is: |
|
178 |
NAME imageToXWD |
|
179 |
AUTHOR Brad Schoening <brad@boole.com> |
|
180 |
FUNCTION Writes a Smalltalk image to an X11 xwd file |
|
181 |
ST-VERSION PPST 4.0 or 4.1 |
|
182 |
DISTRIBUTION world |
|
183 |
VERSION 1.0 |
|
184 |
DATE July 1993 |
|
51 | 185 |
|
204 | 186 |
thanks to Brad for giving us the base for this mehtod. |
49 | 187 |
" |
188 |
||
83 | 189 |
|aStream rgbColor paletteColors ncolors dumpName headerSize| |
49 | 190 |
|
51 | 191 |
(self class canRepresent:image) ifFalse:[ |
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
192 |
^ Image cannotRepresentImageSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
193 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
194 |
errorString:('XWD format cannot represent this image'). |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
195 |
]. |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
196 |
|
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
197 |
image mask notNil ifTrue:[ |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
198 |
Image informationLostQuerySignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
199 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
200 |
errorString:('XWD format does not support an imageMask'). |
49 | 201 |
]. |
202 |
||
203 |
dumpName := 'stdin'. |
|
204 |
headerSize := 4 * (25 + (dumpName size / 4) ceiling). |
|
205 |
paletteColors := image palette "colors". |
|
206 |
ncolors := paletteColors size. |
|
207 |
||
208 |
"create the header (each item is 32 bits long)" |
|
209 |
aStream := fileName asFilename writeStream. |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
210 |
aStream isNil ifTrue:[ |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
211 |
^ Image fileCreationErrorSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
212 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
213 |
errorString:('file creation error: ' , fileName asString). |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
214 |
]. |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
215 |
|
49 | 216 |
aStream binary. |
217 |
aStream nextLongPut: headerSize. "total header size in bytes" |
|
218 |
aStream nextLongPut: 7. "XWD file version" |
|
219 |
aStream nextLongPut: 2. "pixmap format : ZPixmap" |
|
220 |
aStream nextLongPut: 8. "pixmap depth" |
|
221 |
aStream nextLongPut: image width. "pixmap cols" |
|
222 |
aStream nextLongPut: image height. "pixmap rows" |
|
223 |
aStream nextLongPut: 0. "bitmap x offset" |
|
224 |
aStream nextLongPut: 1. "byte order: MSBFirst" |
|
225 |
aStream nextLongPut: 8. "bitmap unit" |
|
226 |
aStream nextLongPut: 1. "bitmap bit order: MSBFirst" |
|
227 |
aStream nextLongPut: 8. "bitmap scanline pad" |
|
228 |
aStream nextLongPut: 8. "bits per pixel" |
|
229 |
aStream nextLongPut: image width. "bytes per scanline" |
|
230 |
aStream nextLongPut: 3. "colormap class : PseudoColor" |
|
231 |
aStream nextLongPut: 0. "Z red mask" |
|
232 |
aStream nextLongPut: 0. "Z green mask" |
|
233 |
aStream nextLongPut: 0. "Z blue mask" |
|
234 |
aStream nextLongPut: 8. "bits per rgb" |
|
235 |
aStream nextLongPut: 256. "number of color map entries" |
|
236 |
aStream nextLongPut: ncolors. "number of color structures" |
|
237 |
aStream nextLongPut: image width. "window width" |
|
238 |
aStream nextLongPut: image height. "window height" |
|
239 |
aStream nextLongPut: 0. "window upper left x coordinate" |
|
240 |
aStream nextLongPut: 0. "window upper left y coordinate" |
|
241 |
aStream nextLongPut: 0. "window border width" |
|
242 |
aStream nextPutAll: dumpName asByteArray. "name of dump" |
|
243 |
"Pad the string to the next 32-bit boundary" |
|
244 |
aStream nextPut: 0. "/ 6 |
|
245 |
aStream nextPut: 0. "/ 7 |
|
246 |
aStream nextPut: 0. "/ 8 |
|
247 |
||
248 |
"/ [(aStream position rem: 4) == 0] whileFalse: [ aStream nextPut: 0 ]. |
|
249 |
||
250 |
"Write out the color table. Each color table entry is 12 bytes long composed of: |
|
204 | 251 |
an index (4 bytes) |
252 |
red color value (2 bytes) |
|
253 |
green color value (2 bytes) |
|
254 |
blue color value (2 bytes) |
|
255 |
flag values (1 byte) |
|
256 |
pad (1 byte) |
|
49 | 257 |
" |
258 |
0 to: ncolors-1 do: [ :index | |
|
204 | 259 |
|r g b| |
49 | 260 |
|
204 | 261 |
aStream nextLongPut: index. |
262 |
rgbColor := paletteColors at: (1+index). |
|
263 |
(rgbColor isNil) ifTrue: [ rgbColor := ColorValue white ]. |
|
264 |
r := (rgbColor red / 100.0 * 65535) rounded. |
|
265 |
g := (rgbColor green / 100.0 * 65535) rounded. |
|
266 |
b := (rgbColor blue / 100.0 * 65535) rounded. |
|
49 | 267 |
|
204 | 268 |
aStream nextWordPut:r. |
269 |
aStream nextWordPut:g. |
|
270 |
aStream nextWordPut:b. |
|
271 |
aStream nextPut: 7. "flags" |
|
272 |
aStream nextPut: 0. "pad" |
|
49 | 273 |
]. |
274 |
||
275 |
"Write out the pixels as index color values" |
|
276 |
"/ Cursor write showWhile: [ |
|
99 | 277 |
"/ |cindex| |
83 | 278 |
"/ |
49 | 279 |
"/ 1 to: (image height) do: [ :row | |
280 |
"/ 1 to: (image width) do: [ :col | |
|
281 |
"/ cindex := image atPoint: (col-1)@(row-1). |
|
282 |
"/ aStream nextPut: cindex.]] |
|
283 |
"/ ]. |
|
284 |
aStream nextPutAll:image bits. |
|
285 |
||
286 |
aStream close |
|
287 |
||
288 |
" |
|
289 |
XWDReader save:(Image fromUser) onFile: '/tmp/st.xwd' |
|
290 |
(Image fromFile: '/tmp/st.xwd') inspect |
|
291 |
" |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
292 |
|
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
293 |
"Modified: 27.2.1997 / 12:45:15 / cg" |
49 | 294 |
! ! |
204 | 295 |
|
391 | 296 |
!XWDReader class methodsFor:'documentation'! |
204 | 297 |
|
298 |
version |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
299 |
^ '$Header: /cvs/stx/stx/libview2/XWDReader.st,v 1.19 1997-02-27 11:50:47 cg Exp $' |
204 | 300 |
! ! |
398 | 301 |
XWDReader initialize! |