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