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