author | HG Automerge |
Thu, 19 Jan 2017 12:00:14 +0000 | |
branch | jv |
changeset 3854 | 4afd107bc911 |
parent 1846 | d29322944b05 |
child 3855 | 1db7742d33ad |
child 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 |
" |
42 | 18 |
|
1736 | 19 |
"{ Package: 'stx:libview2' }" |
20 |
||
391 | 21 |
ImageReader subclass:#XWDReader |
204 | 22 |
instanceVariableNames:'' |
23 |
classVariableNames:'' |
|
24 |
poolDictionaries:'' |
|
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1736
diff
changeset
|
25 |
category:'Graphics-Images-Readers' |
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 |
||
1805 | 85 |
!XWDReader methodsFor:'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 |
|
1846 | 106 |
self reportDimension. |
107 |
||
391 | 108 |
bitsPerPixel := header at: 12. |
109 |
bitsPerPixel == 24 ifTrue:[ |
|
110 |
bitsPerSample := #(8 8 8). |
|
111 |
samplesPerPixel := 3. |
|
112 |
photometric := #rgb |
|
113 |
] ifFalse:[ |
|
114 |
bitsPerSample := Array with:bitsPerPixel. |
|
115 |
samplesPerPixel := 1. |
|
116 |
photometric := #palette |
|
117 |
]. |
|
118 |
"/ depth ~~ bitsPerPixel ifTrue:[self halt]. |
|
119 |
||
120 |
colormapSize := header at: 19. |
|
121 |
nColors := header at: 20. |
|
44 | 122 |
|
391 | 123 |
colorMap := Array new:colormapSize. |
124 |
||
125 |
1 to:nColors do:[:i | |
|
126 |
|clr r g b| |
|
127 |
||
1736 | 128 |
inStream nextLong. |
129 |
r := inStream nextUnsignedShortMSB:true. |
|
130 |
g := inStream nextUnsignedShortMSB:true. |
|
131 |
b := inStream nextUnsignedShortMSB:true. |
|
391 | 132 |
clr := ColorValue scaledRed: (r bitShift: -3) |
133 |
scaledGreen: (g bitShift: -3) |
|
134 |
scaledBlue: (b bitShift: -3). |
|
135 |
colorMap at:i put:clr. |
|
1736 | 136 |
inStream nextWord. |
44 | 137 |
]. |
138 |
||
391 | 139 |
nColors+1 to:colormapSize do: [:i | colorMap at:i put:Color black]. |
140 |
||
141 |
bytesPerRow := width * bitsPerPixel // 8. |
|
142 |
((width * bitsPerPixel \\ 8) ~~ 0) ifTrue:[ |
|
143 |
bytesPerRow := bytesPerRow + 1 |
|
44 | 144 |
]. |
391 | 145 |
srcRowByteSize := width * bitsPerPixel + pad - 1 // pad * (pad / 8). |
42 | 146 |
|
391 | 147 |
data := ByteArray uninitializedNew: srcRowByteSize * height. |
148 |
srcRowByteSize == bytesPerRow ifTrue:[ |
|
1736 | 149 |
inStream nextBytes:srcRowByteSize * height into:data. |
391 | 150 |
] ifFalse:[ |
151 |
dstIndex := 1. |
|
152 |
1 to:height do:[:y | |
|
1736 | 153 |
inStream nextBytes:bytesPerRow into:data startingAt:dstIndex. |
154 |
inStream next:(srcRowByteSize - bytesPerRow). |
|
391 | 155 |
dstIndex := dstIndex + bytesPerRow. |
390 | 156 |
]. |
391 | 157 |
] |
1736 | 158 |
|
391 | 159 |
" |
160 |
XWDReader fromFile:'testfile.xwd' |
|
42 | 161 |
" |
49 | 162 |
" |
391 | 163 |
XWDReader save:(Image fromUser) onFile: '/tmp/st.xwd' |
164 |
(Image fromFile: '/tmp/st.xwd') inspect |
|
165 |
" |
|
42 | 166 |
! ! |
167 |
||
1805 | 168 |
!XWDReader methodsFor:'writing'! |
49 | 169 |
|
1814
2f204c2a957d
image saving - use save:onStream:
Claus Gittinger <cg@exept.de>
parents:
1805
diff
changeset
|
170 |
save:image onStream:aStream |
49 | 171 |
"Save as a version 7 color X11 window dump file (xwd) to the file fileName. |
172 |
This produces a mapped color table with 16 bit color. The xwd file can be |
|
204 | 173 |
viewed by the xwud program and printed with xpr. |
174 |
No compression is performed. |
|
49 | 175 |
|
205 | 176 |
See the file ...include/X11/XWDFile.h for a definition of the format." |
49 | 177 |
|
205 | 178 |
" |
49 | 179 |
Notice: |
204 | 180 |
this method was adapted from a goody in the uiuc archive |
181 |
(Prime time freeware). |
|
182 |
The original files header is: |
|
183 |
NAME imageToXWD |
|
184 |
AUTHOR Brad Schoening <brad@boole.com> |
|
185 |
FUNCTION Writes a Smalltalk image to an X11 xwd file |
|
186 |
ST-VERSION PPST 4.0 or 4.1 |
|
187 |
DISTRIBUTION world |
|
188 |
VERSION 1.0 |
|
189 |
DATE July 1993 |
|
51 | 190 |
|
204 | 191 |
thanks to Brad for giving us the base for this mehtod. |
49 | 192 |
" |
193 |
||
1814
2f204c2a957d
image saving - use save:onStream:
Claus Gittinger <cg@exept.de>
parents:
1805
diff
changeset
|
194 |
|rgbColor paletteColors ncolors dumpName headerSize| |
49 | 195 |
|
51 | 196 |
(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
|
197 |
^ Image cannotRepresentImageSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
198 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
199 |
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
|
200 |
]. |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
201 |
|
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
202 |
image mask notNil ifTrue:[ |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
203 |
Image informationLostQuerySignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
204 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
205 |
errorString:('XWD format does not support an imageMask'). |
49 | 206 |
]. |
207 |
||
208 |
dumpName := 'stdin'. |
|
209 |
headerSize := 4 * (25 + (dumpName size / 4) ceiling). |
|
210 |
paletteColors := image palette "colors". |
|
211 |
ncolors := paletteColors size. |
|
212 |
||
213 |
"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
|
214 |
|
49 | 215 |
aStream binary. |
216 |
aStream nextLongPut: headerSize. "total header size in bytes" |
|
217 |
aStream nextLongPut: 7. "XWD file version" |
|
218 |
aStream nextLongPut: 2. "pixmap format : ZPixmap" |
|
219 |
aStream nextLongPut: 8. "pixmap depth" |
|
220 |
aStream nextLongPut: image width. "pixmap cols" |
|
221 |
aStream nextLongPut: image height. "pixmap rows" |
|
222 |
aStream nextLongPut: 0. "bitmap x offset" |
|
223 |
aStream nextLongPut: 1. "byte order: MSBFirst" |
|
224 |
aStream nextLongPut: 8. "bitmap unit" |
|
225 |
aStream nextLongPut: 1. "bitmap bit order: MSBFirst" |
|
226 |
aStream nextLongPut: 8. "bitmap scanline pad" |
|
227 |
aStream nextLongPut: 8. "bits per pixel" |
|
228 |
aStream nextLongPut: image width. "bytes per scanline" |
|
229 |
aStream nextLongPut: 3. "colormap class : PseudoColor" |
|
230 |
aStream nextLongPut: 0. "Z red mask" |
|
231 |
aStream nextLongPut: 0. "Z green mask" |
|
232 |
aStream nextLongPut: 0. "Z blue mask" |
|
233 |
aStream nextLongPut: 8. "bits per rgb" |
|
234 |
aStream nextLongPut: 256. "number of color map entries" |
|
235 |
aStream nextLongPut: ncolors. "number of color structures" |
|
236 |
aStream nextLongPut: image width. "window width" |
|
237 |
aStream nextLongPut: image height. "window height" |
|
238 |
aStream nextLongPut: 0. "window upper left x coordinate" |
|
239 |
aStream nextLongPut: 0. "window upper left y coordinate" |
|
240 |
aStream nextLongPut: 0. "window border width" |
|
241 |
aStream nextPutAll: dumpName asByteArray. "name of dump" |
|
242 |
"Pad the string to the next 32-bit boundary" |
|
243 |
aStream nextPut: 0. "/ 6 |
|
244 |
aStream nextPut: 0. "/ 7 |
|
245 |
aStream nextPut: 0. "/ 8 |
|
246 |
||
1754 | 247 |
"/ [(aStream position1Based rem: 4) == 0] whileFalse: [ aStream nextPut: 0 ]. |
49 | 248 |
|
249 |
"Write out the color table. Each color table entry is 12 bytes long composed of: |
|
204 | 250 |
an index (4 bytes) |
251 |
red color value (2 bytes) |
|
252 |
green color value (2 bytes) |
|
253 |
blue color value (2 bytes) |
|
254 |
flag values (1 byte) |
|
255 |
pad (1 byte) |
|
49 | 256 |
" |
257 |
0 to: ncolors-1 do: [ :index | |
|
204 | 258 |
|r g b| |
49 | 259 |
|
204 | 260 |
aStream nextLongPut: index. |
261 |
rgbColor := paletteColors at: (1+index). |
|
262 |
(rgbColor isNil) ifTrue: [ rgbColor := ColorValue white ]. |
|
263 |
r := (rgbColor red / 100.0 * 65535) rounded. |
|
264 |
g := (rgbColor green / 100.0 * 65535) rounded. |
|
265 |
b := (rgbColor blue / 100.0 * 65535) rounded. |
|
49 | 266 |
|
204 | 267 |
aStream nextWordPut:r. |
268 |
aStream nextWordPut:g. |
|
269 |
aStream nextWordPut:b. |
|
270 |
aStream nextPut: 7. "flags" |
|
271 |
aStream nextPut: 0. "pad" |
|
49 | 272 |
]. |
273 |
||
274 |
"Write out the pixels as index color values" |
|
275 |
"/ Cursor write showWhile: [ |
|
99 | 276 |
"/ |cindex| |
83 | 277 |
"/ |
49 | 278 |
"/ 1 to: (image height) do: [ :row | |
279 |
"/ 1 to: (image width) do: [ :col | |
|
280 |
"/ cindex := image atPoint: (col-1)@(row-1). |
|
281 |
"/ aStream nextPut: cindex.]] |
|
282 |
"/ ]. |
|
283 |
aStream nextPutAll:image bits. |
|
284 |
||
285 |
" |
|
286 |
XWDReader save:(Image fromUser) onFile: '/tmp/st.xwd' |
|
287 |
(Image fromFile: '/tmp/st.xwd') inspect |
|
288 |
" |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
289 |
|
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
398
diff
changeset
|
290 |
"Modified: 27.2.1997 / 12:45:15 / cg" |
49 | 291 |
! ! |
204 | 292 |
|
391 | 293 |
!XWDReader class methodsFor:'documentation'! |
204 | 294 |
|
295 |
version |
|
1846 | 296 |
^ '$Header: /cvs/stx/stx/libview2/XWDReader.st,v 1.27 2003-11-19 15:24:16 cg Exp $' |
204 | 297 |
! ! |
1736 | 298 |
|
398 | 299 |
XWDReader initialize! |