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 |
|
390
|
19 |
ImageReader subclass:#RGBReader
|
204
|
20 |
instanceVariableNames:''
|
|
21 |
classVariableNames:''
|
|
22 |
poolDictionaries:''
|
259
|
23 |
category:'Graphics-Images-Support'
|
42
|
24 |
!
|
|
25 |
|
390
|
26 |
!RGBReader 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 |
|
390
|
61 |
!RGBReader class methodsFor:'queries'!
|
51
|
62 |
|
|
63 |
canRepresent:anImage
|
390
|
64 |
"return true, if anImage can be represented in my file format."
|
51
|
65 |
|
390
|
66 |
^ false
|
|
67 |
|
|
68 |
"Modified: 29.1.1997 / 22:38:08 / cg"
|
51
|
69 |
! !
|
|
70 |
|
390
|
71 |
!RGBReader methodsFor:'image reading'!
|
42
|
72 |
|
204
|
73 |
fromStream:aStream
|
390
|
74 |
"read an image in RGB (IRIS image format) format from aStream."
|
42
|
75 |
|
390
|
76 |
|magic type t dim width height min max
|
|
77 |
wasteBytes buffer name clrMap header nColors pad
|
|
78 |
srcRowByteSize bytesPerRow bytesPerPixel colormapSize depth
|
|
79 |
dstIndex tableSize rowStartTable rowSizeTable|
|
42
|
80 |
|
|
81 |
aStream binary.
|
|
82 |
|
390
|
83 |
magic := aStream nextUnsignedShortMSB:true.
|
|
84 |
magic ~~ 8r732 ifTrue:[
|
|
85 |
'RGBReader [warning]: bad magic' errorPrintCR.
|
|
86 |
^ nil
|
|
87 |
].
|
|
88 |
type := aStream nextUnsignedShortMSB:true.
|
|
89 |
bytesPerPixel := type bitAnd: 16r00FF.
|
|
90 |
t := type bitAnd: 16rFF00.
|
|
91 |
t == 16r0000 ifTrue:[
|
|
92 |
"/ verbatim
|
|
93 |
type := #verbatim.
|
42
|
94 |
] ifFalse:[
|
390
|
95 |
t == 16r0100 ifTrue:[
|
|
96 |
"/ rle
|
|
97 |
type := #rle
|
|
98 |
] ifFalse:[
|
|
99 |
'RGBReader [warning]: bad type' errorPrintCR.
|
|
100 |
^ nil
|
|
101 |
].
|
42
|
102 |
].
|
390
|
103 |
'type: ' print. type printCR.
|
|
104 |
'bpp: ' print. bytesPerPixel printCR.
|
42
|
105 |
|
390
|
106 |
dim := aStream nextUnsignedShortMSB:true.
|
|
107 |
'dim: ' print. dim printCR.
|
|
108 |
width := aStream nextUnsignedShortMSB:true.
|
|
109 |
'w: ' print. width printCR.
|
|
110 |
height := aStream nextUnsignedShortMSB:true.
|
|
111 |
'h: ' print. height printCR.
|
|
112 |
samplesPerPixel := aStream nextUnsignedShortMSB:true.
|
|
113 |
'z: ' print. samplesPerPixel printCR.
|
44
|
114 |
|
390
|
115 |
min := aStream nextUnsignedLongMSB:true.
|
|
116 |
'min: ' print. min printCR.
|
|
117 |
max := aStream nextUnsignedLongMSB:true.
|
|
118 |
'max: ' print. max printCR.
|
|
119 |
wasteBytes := aStream nextUnsignedLongMSB:true.
|
|
120 |
'waste: ' print. wasteBytes printCR.
|
|
121 |
name := aStream nextBytes:80 into:(buffer := ByteArray new:80).
|
|
122 |
name := buffer asString.
|
|
123 |
name printCR.
|
|
124 |
clrMap := aStream nextUnsignedLongMSB:true.
|
|
125 |
'clrmap: ' print. clrMap printCR.
|
|
126 |
clrMap == 0 ifTrue:[
|
|
127 |
"/ cm_normal
|
|
128 |
] ifFalse:[
|
|
129 |
clrMap == 1 ifTrue:[
|
|
130 |
"/ cm_dithered
|
|
131 |
] ifFalse:[
|
|
132 |
clrMap == 2 ifTrue:[
|
|
133 |
"/ cm_screen
|
|
134 |
] ifFalse:[
|
|
135 |
clrMap == 3 ifTrue:[
|
|
136 |
"/ cm_colormap
|
|
137 |
] ifFalse:[
|
|
138 |
'RGBReader [warning]: bad colormap' errorPrintCR.
|
|
139 |
^ nil
|
|
140 |
]
|
|
141 |
]
|
|
142 |
]
|
44
|
143 |
].
|
|
144 |
|
390
|
145 |
bytesPerPixel ~~ 1 ifTrue:[
|
|
146 |
'RGBReader [warning]: unhandeld bpp' errorPrintCR.
|
|
147 |
^ nil
|
44
|
148 |
].
|
42
|
149 |
|
390
|
150 |
type == #rle ifTrue:[
|
|
151 |
aStream position:513.
|
|
152 |
tableSize := height * samplesPerPixel * 4.
|
|
153 |
rowStartTable := ByteArray new:tableSize.
|
|
154 |
rowSizeTable := ByteArray new:tableSize.
|
|
155 |
(aStream nextBytes:tableSize into:rowStartTable) ~~ tableSize ifTrue:[
|
|
156 |
'RGBReader [warning]: short read' errorPrintCR.
|
|
157 |
^ nil
|
|
158 |
].
|
|
159 |
(aStream nextBytes:tableSize into:rowSizeTable) ~~ tableSize ifTrue:[
|
|
160 |
'RGBReader [warning]: short read' errorPrintCR.
|
|
161 |
^ nil
|
|
162 |
].
|
44
|
163 |
] ifFalse:[
|
390
|
164 |
'RGBReader [warning]: only rle is handled' errorPrintCR.
|
|
165 |
^ nil
|
|
166 |
].
|
|
167 |
|
|
168 |
type == #rle ifTrue:[
|
|
169 |
0 to:height-1 do:[:y |
|
204
|
170 |
].
|
390
|
171 |
].
|
|
172 |
|
|
173 |
self halt.
|
|
174 |
|
42
|
175 |
"
|
390
|
176 |
RGBReader fromFile:'/home/cg/capture.rgb'
|
|
177 |
|
49
|
178 |
"
|
390
|
179 |
|
|
180 |
"Modified: 29.1.1997 / 23:09:50 / cg"
|
42
|
181 |
! !
|
|
182 |
|
390
|
183 |
!RGBReader methodsFor:'image writing'!
|
49
|
184 |
|
|
185 |
save:image onFile:fileName
|
|
186 |
"Save as a version 7 color X11 window dump file (xwd) to the file fileName.
|
|
187 |
This produces a mapped color table with 16 bit color. The xwd file can be
|
204
|
188 |
viewed by the xwud program and printed with xpr.
|
|
189 |
No compression is performed.
|
49
|
190 |
|
205
|
191 |
See the file ...include/X11/XWDFile.h for a definition of the format."
|
49
|
192 |
|
205
|
193 |
"
|
49
|
194 |
Notice:
|
204
|
195 |
this method was adapted from a goody in the uiuc archive
|
|
196 |
(Prime time freeware).
|
|
197 |
The original files header is:
|
|
198 |
NAME imageToXWD
|
|
199 |
AUTHOR Brad Schoening <brad@boole.com>
|
|
200 |
FUNCTION Writes a Smalltalk image to an X11 xwd file
|
|
201 |
ST-VERSION PPST 4.0 or 4.1
|
|
202 |
DISTRIBUTION world
|
|
203 |
VERSION 1.0
|
|
204 |
DATE July 1993
|
51
|
205 |
|
204
|
206 |
thanks to Brad for giving us the base for this mehtod.
|
49
|
207 |
"
|
|
208 |
|
83
|
209 |
|aStream rgbColor paletteColors ncolors dumpName headerSize|
|
49
|
210 |
|
51
|
211 |
(self class canRepresent:image) ifFalse:[
|
204
|
212 |
self error:'XWD format only supports 8bit palette images'.
|
|
213 |
^ nil
|
49
|
214 |
].
|
|
215 |
|
|
216 |
dumpName := 'stdin'.
|
|
217 |
headerSize := 4 * (25 + (dumpName size / 4) ceiling).
|
|
218 |
paletteColors := image palette "colors".
|
|
219 |
ncolors := paletteColors size.
|
|
220 |
|
|
221 |
"create the header (each item is 32 bits long)"
|
|
222 |
aStream := fileName asFilename writeStream.
|
|
223 |
aStream binary.
|
|
224 |
aStream nextLongPut: headerSize. "total header size in bytes"
|
|
225 |
aStream nextLongPut: 7. "XWD file version"
|
|
226 |
aStream nextLongPut: 2. "pixmap format : ZPixmap"
|
|
227 |
aStream nextLongPut: 8. "pixmap depth"
|
|
228 |
aStream nextLongPut: image width. "pixmap cols"
|
|
229 |
aStream nextLongPut: image height. "pixmap rows"
|
|
230 |
aStream nextLongPut: 0. "bitmap x offset"
|
|
231 |
aStream nextLongPut: 1. "byte order: MSBFirst"
|
|
232 |
aStream nextLongPut: 8. "bitmap unit"
|
|
233 |
aStream nextLongPut: 1. "bitmap bit order: MSBFirst"
|
|
234 |
aStream nextLongPut: 8. "bitmap scanline pad"
|
|
235 |
aStream nextLongPut: 8. "bits per pixel"
|
|
236 |
aStream nextLongPut: image width. "bytes per scanline"
|
|
237 |
aStream nextLongPut: 3. "colormap class : PseudoColor"
|
|
238 |
aStream nextLongPut: 0. "Z red mask"
|
|
239 |
aStream nextLongPut: 0. "Z green mask"
|
|
240 |
aStream nextLongPut: 0. "Z blue mask"
|
|
241 |
aStream nextLongPut: 8. "bits per rgb"
|
|
242 |
aStream nextLongPut: 256. "number of color map entries"
|
|
243 |
aStream nextLongPut: ncolors. "number of color structures"
|
|
244 |
aStream nextLongPut: image width. "window width"
|
|
245 |
aStream nextLongPut: image height. "window height"
|
|
246 |
aStream nextLongPut: 0. "window upper left x coordinate"
|
|
247 |
aStream nextLongPut: 0. "window upper left y coordinate"
|
|
248 |
aStream nextLongPut: 0. "window border width"
|
|
249 |
aStream nextPutAll: dumpName asByteArray. "name of dump"
|
|
250 |
"Pad the string to the next 32-bit boundary"
|
|
251 |
aStream nextPut: 0. "/ 6
|
|
252 |
aStream nextPut: 0. "/ 7
|
|
253 |
aStream nextPut: 0. "/ 8
|
|
254 |
|
|
255 |
"/ [(aStream position rem: 4) == 0] whileFalse: [ aStream nextPut: 0 ].
|
|
256 |
|
|
257 |
"Write out the color table. Each color table entry is 12 bytes long composed of:
|
204
|
258 |
an index (4 bytes)
|
|
259 |
red color value (2 bytes)
|
|
260 |
green color value (2 bytes)
|
|
261 |
blue color value (2 bytes)
|
|
262 |
flag values (1 byte)
|
|
263 |
pad (1 byte)
|
49
|
264 |
"
|
|
265 |
0 to: ncolors-1 do: [ :index |
|
204
|
266 |
|r g b|
|
49
|
267 |
|
204
|
268 |
aStream nextLongPut: index.
|
|
269 |
rgbColor := paletteColors at: (1+index).
|
|
270 |
(rgbColor isNil) ifTrue: [ rgbColor := ColorValue white ].
|
|
271 |
r := (rgbColor red / 100.0 * 65535) rounded.
|
|
272 |
g := (rgbColor green / 100.0 * 65535) rounded.
|
|
273 |
b := (rgbColor blue / 100.0 * 65535) rounded.
|
49
|
274 |
|
204
|
275 |
aStream nextWordPut:r.
|
|
276 |
aStream nextWordPut:g.
|
|
277 |
aStream nextWordPut:b.
|
|
278 |
aStream nextPut: 7. "flags"
|
|
279 |
aStream nextPut: 0. "pad"
|
49
|
280 |
].
|
|
281 |
|
|
282 |
"Write out the pixels as index color values"
|
|
283 |
"/ Cursor write showWhile: [
|
99
|
284 |
"/ |cindex|
|
83
|
285 |
"/
|
49
|
286 |
"/ 1 to: (image height) do: [ :row |
|
|
287 |
"/ 1 to: (image width) do: [ :col |
|
|
288 |
"/ cindex := image atPoint: (col-1)@(row-1).
|
|
289 |
"/ aStream nextPut: cindex.]]
|
|
290 |
"/ ].
|
|
291 |
aStream nextPutAll:image bits.
|
|
292 |
|
|
293 |
aStream close
|
|
294 |
|
|
295 |
"
|
|
296 |
XWDReader save:(Image fromUser) onFile: '/tmp/st.xwd'
|
|
297 |
(Image fromFile: '/tmp/st.xwd') inspect
|
|
298 |
"
|
390
|
299 |
|
|
300 |
"Created: 29.1.1997 / 22:37:48 / cg"
|
49
|
301 |
! !
|
204
|
302 |
|
390
|
303 |
!RGBReader class methodsFor:'documentation'!
|
204
|
304 |
|
|
305 |
version
|
390
|
306 |
^ '$Header: /cvs/stx/stx/libview2/XWDReader.st,v 1.16 1997-01-29 22:09:59 cg Exp $'
|
204
|
307 |
! !
|