0
|
1 |
"
|
6
|
2 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
28
|
3 |
All Rights Reserved
|
0
|
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.
|
|
11 |
"
|
|
12 |
|
|
13 |
ImageReader subclass:#XBMReader
|
28
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
33
|
17 |
category:'Graphics-Images support'
|
0
|
18 |
!
|
|
19 |
|
|
20 |
XBMReader comment:'
|
21
|
21 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
28
|
22 |
All Rights Reserved
|
24
|
23 |
|
33
|
24 |
$Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.9 1994-11-17 14:31:40 claus Exp $
|
21
|
25 |
'!
|
0
|
26 |
|
21
|
27 |
!XBMReader class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
28
|
32 |
All Rights Reserved
|
0
|
33 |
|
21
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
0
|
42 |
|
21
|
43 |
version
|
|
44 |
"
|
33
|
45 |
$Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.9 1994-11-17 14:31:40 claus Exp $
|
21
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
this class provides methods for loading and saving x-bitmap-file images.
|
|
52 |
These images can (for example) be created using the bitmap editor supplied
|
24
|
53 |
with X.
|
|
54 |
Only monochrome images can be represented in this format.
|
|
55 |
See also: XPMRreader, SunReader, WinIconReader, GIFReader and TIFFReader
|
21
|
56 |
"
|
|
57 |
! !
|
0
|
58 |
|
28
|
59 |
!XBMReader class methodsFor:'initialization'!
|
|
60 |
|
|
61 |
initialize
|
|
62 |
Image fileFormats at:'.xbm' put:self.
|
|
63 |
! !
|
|
64 |
|
3
|
65 |
!XBMReader methodsFor:'writing to file'!
|
|
66 |
|
|
67 |
save:image onFile:aFileName
|
|
68 |
"save image as XBM file on aFileName"
|
|
69 |
|
33
|
70 |
|reverseBits bits byte
|
|
71 |
srcIndex "{ Class: SmallInteger }"
|
|
72 |
rowBytes "{ Class: SmallInteger }" |
|
3
|
73 |
|
|
74 |
outStream := FileStream newFileNamed:aFileName.
|
|
75 |
outStream isNil ifTrue:[
|
28
|
76 |
'create error' errorPrintNL.
|
|
77 |
^ nil
|
3
|
78 |
].
|
|
79 |
|
|
80 |
width := image width.
|
|
81 |
height := image height.
|
|
82 |
photometric := image photometric.
|
|
83 |
samplesPerPixel := image samplesPerPixel.
|
|
84 |
bitsPerSample := image bitsPerSample.
|
|
85 |
colorMap := image colorMap.
|
|
86 |
|
|
87 |
((samplesPerPixel ~~ 1)
|
|
88 |
or:[((bitsPerSample at:1) ~~ 1)
|
|
89 |
or:[(photometric ~~ #blackIs0) and:[photometric ~~ #whiteIs0]]]) ifTrue:[
|
28
|
90 |
self error:'can only save Depth1Images'.
|
|
91 |
outStream close.
|
|
92 |
^ nil.
|
3
|
93 |
].
|
|
94 |
|
|
95 |
outStream nextPutAll: '#define xbm_width '.
|
|
96 |
outStream nextPutAll:(width printString).
|
|
97 |
outStream cr.
|
|
98 |
outStream nextPutAll: '#define xbm_height '.
|
|
99 |
outStream nextPutAll:(height printString).
|
|
100 |
outStream cr.
|
|
101 |
outStream nextPutAll: 'static char xbm_bits[] = {'; cr.
|
|
102 |
|
|
103 |
reverseBits := self class reverseBits.
|
|
104 |
|
|
105 |
rowBytes := width + 7 // 8.
|
|
106 |
data := image bits.
|
|
107 |
srcIndex := 1.
|
|
108 |
|
|
109 |
height timesRepeat:[
|
28
|
110 |
rowBytes timesRepeat:[
|
|
111 |
outStream nextPutAll: '0x'.
|
|
112 |
bits := data at:srcIndex. srcIndex := srcIndex + 1.
|
33
|
113 |
byte := (reverseBits at:(bits + 1)).
|
|
114 |
byte < 16 ifTrue:[
|
|
115 |
outStream nextPut:$0
|
|
116 |
].
|
|
117 |
byte printOn:outStream radix:16.
|
28
|
118 |
outStream nextPutAll: ', '.
|
|
119 |
].
|
|
120 |
outStream cr
|
3
|
121 |
].
|
|
122 |
outStream nextPutAll: '};'; cr.
|
|
123 |
outStream close
|
|
124 |
|
33
|
125 |
"
|
|
126 |
XBMReader save:(Image fromFile:'bitmaps/SBrowser.xbm') onFile:'test.xbm'
|
|
127 |
"
|
|
128 |
"
|
|
129 |
convert sun icon to XBM format:
|
|
130 |
|
|
131 |
XBMReader save:(Image fromFile:'bitmaps/hello_world.icon') onFile:'test.xbm'
|
|
132 |
"
|
3
|
133 |
! !
|
|
134 |
|
|
135 |
!XBMReader class methodsFor:'testing'!
|
|
136 |
|
|
137 |
isValidImageFile:aFileName
|
|
138 |
"return true, if aFileName contains an x-bitmap-file image"
|
|
139 |
|
|
140 |
|line inStream index1 index2 keyword|
|
|
141 |
|
12
|
142 |
inStream := self streamReadingFile:aFileName.
|
3
|
143 |
inStream isNil ifTrue:[^ false].
|
|
144 |
|
|
145 |
line := inStream nextLine.
|
|
146 |
line isNil ifTrue:[
|
28
|
147 |
inStream close.
|
|
148 |
^ false
|
3
|
149 |
].
|
|
150 |
[line startsWith:'#'] whileFalse:[
|
28
|
151 |
line := inStream nextLine.
|
|
152 |
line isNil ifTrue:[
|
|
153 |
inStream close.
|
|
154 |
^ false
|
|
155 |
]
|
3
|
156 |
].
|
|
157 |
index1 := line indexOf:(Character space).
|
|
158 |
index2 := line indexOf:(Character space) startingAt:(index1 + 1).
|
|
159 |
(index2 == 0) ifTrue:[
|
28
|
160 |
inStream close.
|
|
161 |
^ false
|
3
|
162 |
].
|
|
163 |
keyword := line copyFrom:index1 to:(index2 - 1).
|
|
164 |
(keyword endsWith:'_width') ifFalse:[
|
28
|
165 |
^ false
|
3
|
166 |
].
|
|
167 |
inStream close.
|
|
168 |
^ true
|
|
169 |
! !
|
|
170 |
|
0
|
171 |
!XBMReader methodsFor:'reading from file'!
|
|
172 |
|
|
173 |
fromFile:aFileName
|
|
174 |
|line
|
|
175 |
index "{ Class: SmallInteger }"
|
|
176 |
dstIndex "{ Class: SmallInteger }"
|
|
177 |
bytesPerRow
|
|
178 |
lo "{ Class: SmallInteger }"
|
|
179 |
hi "{ Class: SmallInteger }"
|
3
|
180 |
val "{ Class: SmallInteger }"
|
|
181 |
reverseBits|
|
0
|
182 |
|
12
|
183 |
inStream := self class streamReadingFile:aFileName.
|
|
184 |
inStream isNil ifTrue:[^ nil].
|
0
|
185 |
|
|
186 |
line := inStream nextLine.
|
|
187 |
line isNil ifTrue:[
|
28
|
188 |
inStream close.
|
|
189 |
^ nil
|
0
|
190 |
].
|
|
191 |
|
|
192 |
[line startsWith:'#'] whileFalse:[
|
28
|
193 |
line := inStream nextLine
|
0
|
194 |
].
|
|
195 |
|
21
|
196 |
(line startsWith:'#define') ifFalse:[
|
28
|
197 |
'format error (expected #define)' errorPrintNL.
|
|
198 |
inStream close.
|
|
199 |
^ nil
|
21
|
200 |
].
|
|
201 |
|
0
|
202 |
index := line indexOf:(Character space).
|
|
203 |
index := line indexOf:(Character space) startingAt:(index + 1).
|
|
204 |
(index == 0) ifTrue:[
|
28
|
205 |
'format error' errorPrintNL.
|
|
206 |
inStream close.
|
|
207 |
^ nil
|
21
|
208 |
].
|
|
209 |
((line copyTo:index - 1) endsWith:'width') ifFalse:[
|
28
|
210 |
'format error (expected width)' errorPrintNL.
|
|
211 |
inStream close.
|
|
212 |
^ nil
|
0
|
213 |
].
|
|
214 |
line := line copyFrom:(index + 1).
|
|
215 |
width := Number readFromString:line.
|
|
216 |
|
|
217 |
line := inStream nextLine.
|
|
218 |
index := line indexOf:(Character space).
|
|
219 |
index := line indexOf:(Character space) startingAt:(index + 1).
|
|
220 |
(index == 0) ifTrue:[
|
28
|
221 |
'format error' errorPrintNL.
|
|
222 |
inStream close.
|
|
223 |
^ nil
|
21
|
224 |
].
|
|
225 |
((line copyTo:index - 1) endsWith:'height') ifFalse:[
|
28
|
226 |
'format error (expected height)' errorPrintNL.
|
|
227 |
inStream close.
|
|
228 |
^ nil
|
0
|
229 |
].
|
|
230 |
line := line copyFrom:(index + 1).
|
|
231 |
height := Number readFromString:line.
|
|
232 |
|
|
233 |
bytesPerRow := width // 8.
|
|
234 |
((width \\ 8) ~~ 0) ifTrue:[
|
28
|
235 |
bytesPerRow := bytesPerRow + 1
|
0
|
236 |
].
|
|
237 |
|
3
|
238 |
reverseBits := self class reverseBits.
|
0
|
239 |
|
|
240 |
data := ByteArray new:(bytesPerRow * height).
|
|
241 |
dstIndex := 1.
|
|
242 |
|
|
243 |
line := inStream nextLine.
|
|
244 |
[line startsWith:'#'] whileTrue:[
|
28
|
245 |
line := inStream nextLine
|
0
|
246 |
].
|
|
247 |
|
|
248 |
line := inStream nextLine.
|
|
249 |
[line notNil] whileTrue:[
|
28
|
250 |
index := 1.
|
|
251 |
[index ~~ 0] whileTrue:[
|
|
252 |
index := line indexOf:$x startingAt:index.
|
|
253 |
(index ~~ 0) ifTrue:[
|
|
254 |
index := index + 1.
|
|
255 |
hi := (line at:index) digitValue.
|
|
256 |
index := index + 1.
|
|
257 |
lo := (line at:index) digitValue.
|
|
258 |
val := (hi bitShift:4) bitOr:lo.
|
|
259 |
data at:dstIndex put:(reverseBits at:(val + 1)).
|
|
260 |
dstIndex := dstIndex + 1
|
|
261 |
]
|
|
262 |
].
|
|
263 |
line := inStream nextLine
|
0
|
264 |
].
|
|
265 |
photometric := #whiteIs0.
|
|
266 |
samplesPerPixel := 1.
|
21
|
267 |
bitsPerSample := #(1).
|
0
|
268 |
|
|
269 |
"XBMReader fromFile:'bitmaps/globe1.xbm'"
|
|
270 |
! !
|