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