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