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 |
|
391
|
61 |
!XWDReader class methodsFor:'queries'!
|
51
|
62 |
|
|
63 |
canRepresent:anImage
|
391
|
64 |
"return true, if anImage can be represented in my file format.
|
|
65 |
Only depth8 palette images are supported."
|
51
|
66 |
|
391
|
67 |
anImage depth ~~ 8 ifTrue:[^ false].
|
|
68 |
anImage photometric ~~ #palette ifTrue:[^ false].
|
|
69 |
^ true
|
51
|
70 |
! !
|
|
71 |
|
391
|
72 |
!XWDReader methodsFor:'image reading'!
|
42
|
73 |
|
204
|
74 |
fromStream:aStream
|
391
|
75 |
"read an image in XWD (X Window Dump) format from aStream."
|
42
|
76 |
|
391
|
77 |
|header nColors pad
|
|
78 |
srcRowByteSize bytesPerRow bitsPerPixel colormapSize depth
|
|
79 |
dstIndex|
|
42
|
80 |
|
|
81 |
aStream binary.
|
|
82 |
|
391
|
83 |
header := (1 to: 25) collect: [:i | aStream nextLong].
|
|
84 |
|
|
85 |
"skip ..."
|
|
86 |
101 to:(header at: 1) do: [:i | aStream next].
|
|
87 |
|
|
88 |
depth := header at: 4.
|
|
89 |
width := header at: 5.
|
|
90 |
height := header at: 6.
|
|
91 |
pad := header at: 11.
|
42
|
92 |
|
391
|
93 |
bitsPerPixel := header at: 12.
|
|
94 |
bitsPerPixel == 24 ifTrue:[
|
|
95 |
bitsPerSample := #(8 8 8).
|
|
96 |
samplesPerPixel := 3.
|
|
97 |
photometric := #rgb
|
|
98 |
] ifFalse:[
|
|
99 |
bitsPerSample := Array with:bitsPerPixel.
|
|
100 |
samplesPerPixel := 1.
|
|
101 |
photometric := #palette
|
|
102 |
].
|
|
103 |
"/ depth ~~ bitsPerPixel ifTrue:[self halt].
|
|
104 |
|
|
105 |
colormapSize := header at: 19.
|
|
106 |
nColors := header at: 20.
|
44
|
107 |
|
391
|
108 |
colorMap := Array new:colormapSize.
|
|
109 |
|
|
110 |
1 to:nColors do:[:i |
|
|
111 |
|clr r g b|
|
|
112 |
|
|
113 |
aStream nextLong.
|
|
114 |
r := aStream nextUnsignedShortMSB:true.
|
|
115 |
g := aStream nextUnsignedShortMSB:true.
|
|
116 |
b := aStream nextUnsignedShortMSB:true.
|
|
117 |
clr := ColorValue scaledRed: (r bitShift: -3)
|
|
118 |
scaledGreen: (g bitShift: -3)
|
|
119 |
scaledBlue: (b bitShift: -3).
|
|
120 |
colorMap at:i put:clr.
|
|
121 |
aStream nextWord.
|
44
|
122 |
].
|
|
123 |
|
391
|
124 |
nColors+1 to:colormapSize do: [:i | colorMap at:i put:Color black].
|
|
125 |
|
|
126 |
bytesPerRow := width * bitsPerPixel // 8.
|
|
127 |
((width * bitsPerPixel \\ 8) ~~ 0) ifTrue:[
|
|
128 |
bytesPerRow := bytesPerRow + 1
|
44
|
129 |
].
|
391
|
130 |
srcRowByteSize := width * bitsPerPixel + pad - 1 // pad * (pad / 8).
|
42
|
131 |
|
391
|
132 |
data := ByteArray uninitializedNew: srcRowByteSize * height.
|
|
133 |
srcRowByteSize == bytesPerRow ifTrue:[
|
|
134 |
aStream nextBytes:srcRowByteSize * height into:data.
|
|
135 |
] ifFalse:[
|
|
136 |
dstIndex := 1.
|
|
137 |
1 to:height do:[:y |
|
|
138 |
aStream nextBytes:bytesPerRow into:data startingAt:dstIndex.
|
|
139 |
aStream next:(srcRowByteSize - bytesPerRow).
|
|
140 |
dstIndex := dstIndex + bytesPerRow.
|
390
|
141 |
].
|
391
|
142 |
]
|
|
143 |
"
|
|
144 |
XWDReader fromFile:'testfile.xwd'
|
42
|
145 |
"
|
49
|
146 |
"
|
391
|
147 |
XWDReader save:(Image fromUser) onFile: '/tmp/st.xwd'
|
|
148 |
(Image fromFile: '/tmp/st.xwd') inspect
|
|
149 |
"
|
42
|
150 |
! !
|
|
151 |
|
391
|
152 |
!XWDReader methodsFor:'image writing'!
|
49
|
153 |
|
|
154 |
save:image onFile:fileName
|
|
155 |
"Save as a version 7 color X11 window dump file (xwd) to the file fileName.
|
|
156 |
This produces a mapped color table with 16 bit color. The xwd file can be
|
204
|
157 |
viewed by the xwud program and printed with xpr.
|
|
158 |
No compression is performed.
|
49
|
159 |
|
205
|
160 |
See the file ...include/X11/XWDFile.h for a definition of the format."
|
49
|
161 |
|
205
|
162 |
"
|
49
|
163 |
Notice:
|
204
|
164 |
this method was adapted from a goody in the uiuc archive
|
|
165 |
(Prime time freeware).
|
|
166 |
The original files header is:
|
|
167 |
NAME imageToXWD
|
|
168 |
AUTHOR Brad Schoening <brad@boole.com>
|
|
169 |
FUNCTION Writes a Smalltalk image to an X11 xwd file
|
|
170 |
ST-VERSION PPST 4.0 or 4.1
|
|
171 |
DISTRIBUTION world
|
|
172 |
VERSION 1.0
|
|
173 |
DATE July 1993
|
51
|
174 |
|
204
|
175 |
thanks to Brad for giving us the base for this mehtod.
|
49
|
176 |
"
|
|
177 |
|
83
|
178 |
|aStream rgbColor paletteColors ncolors dumpName headerSize|
|
49
|
179 |
|
51
|
180 |
(self class canRepresent:image) ifFalse:[
|
204
|
181 |
self error:'XWD format only supports 8bit palette images'.
|
|
182 |
^ nil
|
49
|
183 |
].
|
|
184 |
|
|
185 |
dumpName := 'stdin'.
|
|
186 |
headerSize := 4 * (25 + (dumpName size / 4) ceiling).
|
|
187 |
paletteColors := image palette "colors".
|
|
188 |
ncolors := paletteColors size.
|
|
189 |
|
|
190 |
"create the header (each item is 32 bits long)"
|
|
191 |
aStream := fileName asFilename writeStream.
|
|
192 |
aStream binary.
|
|
193 |
aStream nextLongPut: headerSize. "total header size in bytes"
|
|
194 |
aStream nextLongPut: 7. "XWD file version"
|
|
195 |
aStream nextLongPut: 2. "pixmap format : ZPixmap"
|
|
196 |
aStream nextLongPut: 8. "pixmap depth"
|
|
197 |
aStream nextLongPut: image width. "pixmap cols"
|
|
198 |
aStream nextLongPut: image height. "pixmap rows"
|
|
199 |
aStream nextLongPut: 0. "bitmap x offset"
|
|
200 |
aStream nextLongPut: 1. "byte order: MSBFirst"
|
|
201 |
aStream nextLongPut: 8. "bitmap unit"
|
|
202 |
aStream nextLongPut: 1. "bitmap bit order: MSBFirst"
|
|
203 |
aStream nextLongPut: 8. "bitmap scanline pad"
|
|
204 |
aStream nextLongPut: 8. "bits per pixel"
|
|
205 |
aStream nextLongPut: image width. "bytes per scanline"
|
|
206 |
aStream nextLongPut: 3. "colormap class : PseudoColor"
|
|
207 |
aStream nextLongPut: 0. "Z red mask"
|
|
208 |
aStream nextLongPut: 0. "Z green mask"
|
|
209 |
aStream nextLongPut: 0. "Z blue mask"
|
|
210 |
aStream nextLongPut: 8. "bits per rgb"
|
|
211 |
aStream nextLongPut: 256. "number of color map entries"
|
|
212 |
aStream nextLongPut: ncolors. "number of color structures"
|
|
213 |
aStream nextLongPut: image width. "window width"
|
|
214 |
aStream nextLongPut: image height. "window height"
|
|
215 |
aStream nextLongPut: 0. "window upper left x coordinate"
|
|
216 |
aStream nextLongPut: 0. "window upper left y coordinate"
|
|
217 |
aStream nextLongPut: 0. "window border width"
|
|
218 |
aStream nextPutAll: dumpName asByteArray. "name of dump"
|
|
219 |
"Pad the string to the next 32-bit boundary"
|
|
220 |
aStream nextPut: 0. "/ 6
|
|
221 |
aStream nextPut: 0. "/ 7
|
|
222 |
aStream nextPut: 0. "/ 8
|
|
223 |
|
|
224 |
"/ [(aStream position rem: 4) == 0] whileFalse: [ aStream nextPut: 0 ].
|
|
225 |
|
|
226 |
"Write out the color table. Each color table entry is 12 bytes long composed of:
|
204
|
227 |
an index (4 bytes)
|
|
228 |
red color value (2 bytes)
|
|
229 |
green color value (2 bytes)
|
|
230 |
blue color value (2 bytes)
|
|
231 |
flag values (1 byte)
|
|
232 |
pad (1 byte)
|
49
|
233 |
"
|
|
234 |
0 to: ncolors-1 do: [ :index |
|
204
|
235 |
|r g b|
|
49
|
236 |
|
204
|
237 |
aStream nextLongPut: index.
|
|
238 |
rgbColor := paletteColors at: (1+index).
|
|
239 |
(rgbColor isNil) ifTrue: [ rgbColor := ColorValue white ].
|
|
240 |
r := (rgbColor red / 100.0 * 65535) rounded.
|
|
241 |
g := (rgbColor green / 100.0 * 65535) rounded.
|
|
242 |
b := (rgbColor blue / 100.0 * 65535) rounded.
|
49
|
243 |
|
204
|
244 |
aStream nextWordPut:r.
|
|
245 |
aStream nextWordPut:g.
|
|
246 |
aStream nextWordPut:b.
|
|
247 |
aStream nextPut: 7. "flags"
|
|
248 |
aStream nextPut: 0. "pad"
|
49
|
249 |
].
|
|
250 |
|
|
251 |
"Write out the pixels as index color values"
|
|
252 |
"/ Cursor write showWhile: [
|
99
|
253 |
"/ |cindex|
|
83
|
254 |
"/
|
49
|
255 |
"/ 1 to: (image height) do: [ :row |
|
|
256 |
"/ 1 to: (image width) do: [ :col |
|
|
257 |
"/ cindex := image atPoint: (col-1)@(row-1).
|
|
258 |
"/ aStream nextPut: cindex.]]
|
|
259 |
"/ ].
|
|
260 |
aStream nextPutAll:image bits.
|
|
261 |
|
|
262 |
aStream close
|
|
263 |
|
|
264 |
"
|
|
265 |
XWDReader save:(Image fromUser) onFile: '/tmp/st.xwd'
|
|
266 |
(Image fromFile: '/tmp/st.xwd') inspect
|
|
267 |
"
|
|
268 |
! !
|
204
|
269 |
|
391
|
270 |
!XWDReader class methodsFor:'documentation'!
|
204
|
271 |
|
|
272 |
version
|
391
|
273 |
^ '$Header: /cvs/stx/stx/libview2/XWDReader.st,v 1.17 1997-01-29 22:11:32 cg Exp $'
|
204
|
274 |
! !
|