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