author | Claus Gittinger <cg@exept.de> |
Thu, 06 May 1999 20:53:16 +0200 | |
changeset 1165 | 70fd3edc13b4 |
parent 1144 | c86a1e3e3884 |
child 1495 | 100e4e782f71 |
permissions | -rw-r--r-- |
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 |
|
195 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
259 | 17 |
category:'Graphics-Images-Support' |
0 | 18 |
! |
19 |
||
21 | 20 |
!XBMReader class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
28 | 25 |
All Rights Reserved |
0 | 26 |
|
21 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
0 | 35 |
|
21 | 36 |
documentation |
37 |
" |
|
38 |
this class provides methods for loading and saving x-bitmap-file images. |
|
39 |
These images can (for example) be created using the bitmap editor supplied |
|
24 | 40 |
with X. |
41 |
Only monochrome images can be represented in this format. |
|
203 | 42 |
Both reading and writing of images is supported. |
195 | 43 |
|
44 |
[See also:] |
|
234 | 45 |
Image Form Icon |
197 | 46 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader |
210 | 47 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
197 | 48 |
XPMReader XWDReader |
220 | 49 |
|
50 |
[author:] |
|
51 |
Claus Gittinger |
|
21 | 52 |
" |
53 |
! ! |
|
0 | 54 |
|
28 | 55 |
!XBMReader class methodsFor:'initialization'! |
56 |
||
57 |
initialize |
|
202 | 58 |
"tell Image-class, that a new fileReader is present |
59 |
for the '.xbm' extension." |
|
60 |
||
647 | 61 |
MIMETypes defineImageType:'image/x-xbitmap' suffix:'xbm' reader:self. |
62 |
MIMETypes defineImageType:nil suffix:'bm' reader:self. |
|
202 | 63 |
|
399 | 64 |
"Modified: 1.2.1997 / 15:08:18 / cg" |
28 | 65 |
! ! |
66 |
||
41 | 67 |
!XBMReader class methodsFor:'testing'! |
68 |
||
135 | 69 |
canRepresent:anImage |
70 |
"return true, if anImage can be represented in my file format" |
|
71 |
||
1046
8670e67344de
fixed canRepresent for palette images.
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
72 |
|photometric clr0 clr1| |
135 | 73 |
|
74 |
(anImage depth ~~ 1) ifTrue:[^ false.]. |
|
1046
8670e67344de
fixed canRepresent for palette images.
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
75 |
(photometric := anImage photometric) == #blackIs0 ifTrue:[^ true]. |
8670e67344de
fixed canRepresent for palette images.
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
76 |
photometric == #whiteIs0 ifTrue:[^ true]. |
8670e67344de
fixed canRepresent for palette images.
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
77 |
photometric == #palette ifTrue:[ |
8670e67344de
fixed canRepresent for palette images.
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
78 |
clr0 := anImage colorFromValue:0. |
8670e67344de
fixed canRepresent for palette images.
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
79 |
clr1 := anImage colorFromValue:1. |
8670e67344de
fixed canRepresent for palette images.
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
80 |
(clr0 = Color white and:[clr1 = Color black]) ifTrue:[^true]. |
8670e67344de
fixed canRepresent for palette images.
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
81 |
(clr1 = Color white and:[clr0 = Color black]) ifTrue:[^true]. |
8670e67344de
fixed canRepresent for palette images.
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
82 |
]. |
8670e67344de
fixed canRepresent for palette images.
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
83 |
^ false |
8670e67344de
fixed canRepresent for palette images.
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
84 |
|
8670e67344de
fixed canRepresent for palette images.
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
85 |
"Modified: / 17.8.1998 / 10:17:01 / cg" |
135 | 86 |
! |
87 |
||
41 | 88 |
isValidImageFile:aFileName |
89 |
"return true, if aFileName contains an x-bitmap-file image" |
|
90 |
||
91 |
|line inStream index1 index2 keyword| |
|
92 |
||
93 |
inStream := self streamReadingFile:aFileName. |
|
94 |
inStream isNil ifTrue:[^ false]. |
|
95 |
||
550 | 96 |
Stream readErrorSignal handle:[:ex | |
97 |
line := nil. |
|
98 |
] do:[ |
|
99 |
line := inStream nextLine. |
|
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
100 |
[line notNil and:[line isEmpty]] whileTrue:[ |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
101 |
line := inStream nextLine. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
102 |
]. |
550 | 103 |
]. |
41 | 104 |
line isNil ifTrue:[ |
550 | 105 |
inStream close. |
106 |
^ false |
|
41 | 107 |
]. |
108 |
[line startsWith:'#'] whileFalse:[ |
|
558
98c4c983a02b
oops - handle read of binaries
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
109 |
Stream readErrorSignal handle:[:ex | |
98c4c983a02b
oops - handle read of binaries
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
110 |
line := nil. |
98c4c983a02b
oops - handle read of binaries
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
111 |
] do:[ |
98c4c983a02b
oops - handle read of binaries
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
112 |
line := inStream nextLine. |
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
113 |
[line notNil and:[line isEmpty]] whileTrue:[ |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
114 |
line := inStream nextLine. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
115 |
]. |
558
98c4c983a02b
oops - handle read of binaries
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
116 |
]. |
550 | 117 |
line isNil ifTrue:[ |
118 |
inStream close. |
|
119 |
^ false |
|
120 |
] |
|
41 | 121 |
]. |
122 |
index1 := line indexOf:(Character space). |
|
123 |
index2 := line indexOf:(Character space) startingAt:(index1 + 1). |
|
124 |
(index2 == 0) ifTrue:[ |
|
550 | 125 |
inStream close. |
126 |
^ false |
|
41 | 127 |
]. |
128 |
keyword := line copyFrom:index1 to:(index2 - 1). |
|
129 |
(keyword endsWith:'_width') ifFalse:[ |
|
550 | 130 |
inStream close. |
131 |
^ false |
|
41 | 132 |
]. |
133 |
inStream close. |
|
134 |
^ true |
|
550 | 135 |
|
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
136 |
"Modified: / 18.3.1999 / 11:33:39 / cg" |
41 | 137 |
! ! |
138 |
||
139 |
!XBMReader methodsFor:'reading from file'! |
|
140 |
||
141 |
fromStream:aStream |
|
202 | 142 |
"read an image in xbm format from aStream" |
143 |
||
524 | 144 |
|lineString |
41 | 145 |
index "{ Class: SmallInteger }" |
146 |
dstIndex "{ Class: SmallInteger }" |
|
147 |
bytesPerRow |
|
148 |
lo "{ Class: SmallInteger }" |
|
149 |
hi "{ Class: SmallInteger }" |
|
150 |
val "{ Class: SmallInteger }" |
|
151 |
reverseBits| |
|
152 |
||
153 |
inStream := aStream. |
|
154 |
||
524 | 155 |
lineString := aStream nextLine. |
156 |
lineString isNil ifTrue:[ |
|
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
157 |
^ self fileFormatError:'short file'. |
41 | 158 |
]. |
159 |
||
524 | 160 |
[lineString startsWith:'#'] whileFalse:[ |
161 |
lineString := aStream nextLine. |
|
162 |
lineString isNil ifTrue:[ |
|
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
163 |
^ self fileFormatError:'short file'. |
493 | 164 |
]. |
41 | 165 |
]. |
166 |
||
524 | 167 |
(lineString startsWith:'#define') ifFalse:[ |
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
168 |
^ self fileFormatError:'format error (expected #define)'. |
41 | 169 |
]. |
170 |
||
524 | 171 |
index := lineString indexOf:(Character space). |
172 |
index := lineString indexOf:(Character space) startingAt:(index + 1). |
|
41 | 173 |
(index == 0) ifTrue:[ |
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
174 |
^ self fileFormatError:'format error'. |
41 | 175 |
]. |
524 | 176 |
((lineString copyTo:index - 1) endsWith:'width') ifFalse:[ |
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
177 |
^ self fileFormatError:'format error (expected width)'. |
41 | 178 |
]. |
524 | 179 |
lineString := lineString copyFrom:(index + 1). |
1049 | 180 |
width := Number readFromString:lineString onError:nil. |
181 |
width isNil ifTrue:[ |
|
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
182 |
^ self fileFormatError:'format error (expected width)'. |
1049 | 183 |
]. |
41 | 184 |
|
524 | 185 |
lineString := aStream nextLine. |
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
186 |
[lineString notNil and:[lineString isEmpty]] whileTrue:[ |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
187 |
lineString := aStream nextLine. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
188 |
]. |
524 | 189 |
index := lineString indexOf:(Character space). |
190 |
index := lineString indexOf:(Character space) startingAt:(index + 1). |
|
41 | 191 |
(index == 0) ifTrue:[ |
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
192 |
^ self fileFormatError:'format error'. |
41 | 193 |
]. |
524 | 194 |
((lineString copyTo:index - 1) endsWith:'height') ifFalse:[ |
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
195 |
^ self fileFormatError:'format error (expected height)'. |
41 | 196 |
]. |
524 | 197 |
lineString := lineString copyFrom:(index + 1). |
1049 | 198 |
height := Number readFromString:lineString onError:nil. |
199 |
height isNil ifTrue:[ |
|
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
200 |
^ self fileFormatError:'format error (expected height)'. |
1049 | 201 |
]. |
41 | 202 |
|
203 |
bytesPerRow := width // 8. |
|
204 |
((width \\ 8) ~~ 0) ifTrue:[ |
|
202 | 205 |
bytesPerRow := bytesPerRow + 1 |
41 | 206 |
]. |
207 |
||
208 |
reverseBits := self class reverseBits. |
|
209 |
||
210 |
data := ByteArray new:(bytesPerRow * height). |
|
211 |
dstIndex := 1. |
|
212 |
||
524 | 213 |
lineString := aStream nextLine. |
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
214 |
[(lineString startsWith:'#') |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
215 |
or:[lineString isEmpty]] whileTrue:[ |
524 | 216 |
lineString := aStream nextLine. |
41 | 217 |
]. |
218 |
||
524 | 219 |
[lineString notNil and:[(lineString startsWith:'static') not]] whileTrue:[ |
220 |
lineString := aStream nextLine. |
|
53 | 221 |
]. |
524 | 222 |
lineString := aStream nextLine. |
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
223 |
[lineString notNil and:[lineString isEmpty]] whileTrue:[ |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
224 |
lineString := aStream nextLine. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
225 |
]. |
53 | 226 |
|
524 | 227 |
[lineString notNil] whileTrue:[ |
202 | 228 |
index := 1. |
229 |
[index ~~ 0] whileTrue:[ |
|
524 | 230 |
index := lineString indexOf:$x startingAt:index. |
202 | 231 |
(index ~~ 0) ifTrue:[ |
232 |
index := index + 1. |
|
524 | 233 |
hi := (lineString at:index) digitValue. |
202 | 234 |
index := index + 1. |
524 | 235 |
lo := (lineString at:index) digitValue. |
202 | 236 |
val := (hi bitShift:4) bitOr:lo. |
237 |
data at:dstIndex put:(reverseBits at:(val + 1)). |
|
238 |
dstIndex := dstIndex + 1 |
|
239 |
] |
|
240 |
]. |
|
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
241 |
lineString := aStream nextLine. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
242 |
[lineString notNil and:[lineString isEmpty]] whileTrue:[ |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
243 |
lineString := aStream nextLine. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
244 |
]. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
245 |
|
41 | 246 |
]. |
247 |
photometric := #whiteIs0. |
|
248 |
samplesPerPixel := 1. |
|
249 |
bitsPerSample := #(1). |
|
250 |
||
251 |
" |
|
252 |
XBMReader fromFile:'bitmaps/globe1.xbm' |
|
202 | 253 |
" |
254 |
||
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
255 |
"Modified: / 18.3.1999 / 11:32:46 / cg" |
41 | 256 |
! ! |
257 |
||
3 | 258 |
!XBMReader methodsFor:'writing to file'! |
259 |
||
260 |
save:image onFile:aFileName |
|
202 | 261 |
"save image as XBM file on aFileName. |
262 |
Only depth1 b&w images can be represented in this format." |
|
3 | 263 |
|
33 | 264 |
|reverseBits bits byte |
66 | 265 |
h "{ Class: SmallInteger }" |
33 | 266 |
srcIndex "{ Class: SmallInteger }" |
267 |
rowBytes "{ Class: SmallInteger }" | |
|
3 | 268 |
|
51 | 269 |
(self class canRepresent:image) ifFalse:[ |
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
270 |
^ Image cannotRepresentImageSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
271 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
272 |
errorString:('XBM format only supports monochrome images'). |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
273 |
]. |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
274 |
|
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
275 |
image mask notNil ifTrue:[ |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
276 |
Image informationLostQuerySignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
277 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
278 |
errorString:('XBM format does not support an imageMask'). |
51 | 279 |
]. |
280 |
||
3 | 281 |
outStream := FileStream newFileNamed:aFileName. |
282 |
outStream isNil ifTrue:[ |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
283 |
^ Image fileCreationErrorSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
284 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
285 |
errorString:('file creation error: ' , aFileName asString). |
3 | 286 |
]. |
287 |
||
288 |
width := image width. |
|
289 |
height := image height. |
|
290 |
photometric := image photometric. |
|
291 |
samplesPerPixel := image samplesPerPixel. |
|
292 |
bitsPerSample := image bitsPerSample. |
|
293 |
colorMap := image colorMap. |
|
294 |
||
295 |
outStream nextPutAll: '#define xbm_width '. |
|
296 |
outStream nextPutAll:(width printString). |
|
297 |
outStream cr. |
|
298 |
outStream nextPutAll: '#define xbm_height '. |
|
299 |
outStream nextPutAll:(height printString). |
|
300 |
outStream cr. |
|
301 |
outStream nextPutAll: 'static char xbm_bits[] = {'; cr. |
|
302 |
||
303 |
reverseBits := self class reverseBits. |
|
304 |
||
305 |
rowBytes := width + 7 // 8. |
|
306 |
data := image bits. |
|
307 |
srcIndex := 1. |
|
308 |
||
66 | 309 |
h := height. |
310 |
h timesRepeat:[ |
|
202 | 311 |
rowBytes timesRepeat:[ |
312 |
outStream nextPutAll: '0x'. |
|
313 |
bits := data at:srcIndex. srcIndex := srcIndex + 1. |
|
314 |
photometric == #blackIs0 ifTrue:[ |
|
315 |
bits := bits bitInvert bitAnd:16rFF |
|
316 |
]. |
|
317 |
byte := (reverseBits at:(bits + 1)). |
|
318 |
byte < 16 ifTrue:[ |
|
319 |
outStream nextPut:$0 |
|
320 |
]. |
|
272
477aeb0d62b6
printOn:radix: -> printOn:base:
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
321 |
byte printOn:outStream base:16. |
202 | 322 |
outStream nextPutAll: ', '. |
323 |
]. |
|
324 |
outStream cr |
|
3 | 325 |
]. |
326 |
outStream nextPutAll: '};'; cr. |
|
327 |
outStream close |
|
328 |
||
33 | 329 |
" |
330 |
XBMReader save:(Image fromFile:'bitmaps/SBrowser.xbm') onFile:'test.xbm' |
|
331 |
" |
|
332 |
" |
|
333 |
convert sun icon to XBM format: |
|
334 |
||
335 |
XBMReader save:(Image fromFile:'bitmaps/hello_world.icon') onFile:'test.xbm' |
|
336 |
" |
|
202 | 337 |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
338 |
"Modified: 27.2.1997 / 12:46:49 / cg" |
3 | 339 |
! ! |
340 |
||
135 | 341 |
!XBMReader class methodsFor:'documentation'! |
342 |
||
343 |
version |
|
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
344 |
^ '$Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.41 1999-03-18 10:28:23 cg Exp $' |
135 | 345 |
! ! |
41 | 346 |
XBMReader initialize! |