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