author | Claus Gittinger <cg@exept.de> |
Thu, 20 Aug 1998 00:40:06 +0200 | |
changeset 1049 | 1afeefeedd14 |
parent 1046 | 8670e67344de |
child 1144 | c86a1e3e3884 |
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. |
|
100 |
]. |
|
41 | 101 |
line isNil ifTrue:[ |
550 | 102 |
inStream close. |
103 |
^ false |
|
41 | 104 |
]. |
105 |
[line startsWith:'#'] whileFalse:[ |
|
558
98c4c983a02b
oops - handle read of binaries
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
106 |
Stream readErrorSignal handle:[:ex | |
98c4c983a02b
oops - handle read of binaries
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
107 |
line := nil. |
98c4c983a02b
oops - handle read of binaries
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
108 |
] do:[ |
98c4c983a02b
oops - handle read of binaries
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
109 |
line := inStream nextLine. |
98c4c983a02b
oops - handle read of binaries
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
110 |
]. |
550 | 111 |
line isNil ifTrue:[ |
112 |
inStream close. |
|
113 |
^ false |
|
114 |
] |
|
41 | 115 |
]. |
116 |
index1 := line indexOf:(Character space). |
|
117 |
index2 := line indexOf:(Character space) startingAt:(index1 + 1). |
|
118 |
(index2 == 0) ifTrue:[ |
|
550 | 119 |
inStream close. |
120 |
^ false |
|
41 | 121 |
]. |
122 |
keyword := line copyFrom:index1 to:(index2 - 1). |
|
123 |
(keyword endsWith:'_width') ifFalse:[ |
|
550 | 124 |
inStream close. |
125 |
^ false |
|
41 | 126 |
]. |
127 |
inStream close. |
|
128 |
^ true |
|
550 | 129 |
|
558
98c4c983a02b
oops - handle read of binaries
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
130 |
"Modified: 22.4.1997 / 23:12:00 / cg" |
41 | 131 |
! ! |
132 |
||
133 |
!XBMReader methodsFor:'reading from file'! |
|
134 |
||
135 |
fromStream:aStream |
|
202 | 136 |
"read an image in xbm format from aStream" |
137 |
||
524 | 138 |
|lineString |
41 | 139 |
index "{ Class: SmallInteger }" |
140 |
dstIndex "{ Class: SmallInteger }" |
|
141 |
bytesPerRow |
|
142 |
lo "{ Class: SmallInteger }" |
|
143 |
hi "{ Class: SmallInteger }" |
|
144 |
val "{ Class: SmallInteger }" |
|
145 |
reverseBits| |
|
146 |
||
147 |
inStream := aStream. |
|
148 |
||
524 | 149 |
lineString := aStream nextLine. |
150 |
lineString isNil ifTrue:[ |
|
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
151 |
^ self fileFormatError:'short file'. |
41 | 152 |
]. |
153 |
||
524 | 154 |
[lineString startsWith:'#'] whileFalse:[ |
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'. |
493 | 158 |
]. |
41 | 159 |
]. |
160 |
||
524 | 161 |
(lineString startsWith:'#define') ifFalse:[ |
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
162 |
^ self fileFormatError:'format error (expected #define)'. |
41 | 163 |
]. |
164 |
||
524 | 165 |
index := lineString indexOf:(Character space). |
166 |
index := lineString indexOf:(Character space) startingAt:(index + 1). |
|
41 | 167 |
(index == 0) ifTrue:[ |
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
168 |
^ self fileFormatError:'format error'. |
41 | 169 |
]. |
524 | 170 |
((lineString copyTo:index - 1) endsWith:'width') ifFalse:[ |
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
171 |
^ self fileFormatError:'format error (expected width)'. |
41 | 172 |
]. |
524 | 173 |
lineString := lineString copyFrom:(index + 1). |
1049 | 174 |
width := Number readFromString:lineString onError:nil. |
175 |
width isNil ifTrue:[ |
|
176 |
^ self fileFormatError:'format error (expected width)'. |
|
177 |
]. |
|
41 | 178 |
|
524 | 179 |
lineString := aStream nextLine. |
180 |
index := lineString indexOf:(Character space). |
|
181 |
index := lineString indexOf:(Character space) startingAt:(index + 1). |
|
41 | 182 |
(index == 0) ifTrue:[ |
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
183 |
^ self fileFormatError:'format error'. |
41 | 184 |
]. |
524 | 185 |
((lineString copyTo:index - 1) endsWith:'height') ifFalse:[ |
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
186 |
^ self fileFormatError:'format error (expected height)'. |
41 | 187 |
]. |
524 | 188 |
lineString := lineString copyFrom:(index + 1). |
1049 | 189 |
height := Number readFromString:lineString onError:nil. |
190 |
height isNil ifTrue:[ |
|
191 |
^ self fileFormatError:'format error (expected height)'. |
|
192 |
]. |
|
41 | 193 |
|
194 |
bytesPerRow := width // 8. |
|
195 |
((width \\ 8) ~~ 0) ifTrue:[ |
|
202 | 196 |
bytesPerRow := bytesPerRow + 1 |
41 | 197 |
]. |
198 |
||
199 |
reverseBits := self class reverseBits. |
|
200 |
||
201 |
data := ByteArray new:(bytesPerRow * height). |
|
202 |
dstIndex := 1. |
|
203 |
||
524 | 204 |
lineString := aStream nextLine. |
205 |
[lineString startsWith:'#'] whileTrue:[ |
|
206 |
lineString := aStream nextLine. |
|
41 | 207 |
]. |
208 |
||
524 | 209 |
[lineString notNil and:[(lineString startsWith:'static') not]] whileTrue:[ |
210 |
lineString := aStream nextLine. |
|
53 | 211 |
]. |
524 | 212 |
lineString := aStream nextLine. |
53 | 213 |
|
524 | 214 |
[lineString notNil] whileTrue:[ |
202 | 215 |
index := 1. |
216 |
[index ~~ 0] whileTrue:[ |
|
524 | 217 |
index := lineString indexOf:$x startingAt:index. |
202 | 218 |
(index ~~ 0) ifTrue:[ |
219 |
index := index + 1. |
|
524 | 220 |
hi := (lineString at:index) digitValue. |
202 | 221 |
index := index + 1. |
524 | 222 |
lo := (lineString at:index) digitValue. |
202 | 223 |
val := (hi bitShift:4) bitOr:lo. |
224 |
data at:dstIndex put:(reverseBits at:(val + 1)). |
|
225 |
dstIndex := dstIndex + 1 |
|
226 |
] |
|
227 |
]. |
|
524 | 228 |
lineString := aStream nextLine |
41 | 229 |
]. |
230 |
photometric := #whiteIs0. |
|
231 |
samplesPerPixel := 1. |
|
232 |
bitsPerSample := #(1). |
|
233 |
||
234 |
" |
|
235 |
XBMReader fromFile:'bitmaps/globe1.xbm' |
|
202 | 236 |
" |
237 |
||
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
238 |
"Modified: / 3.2.1998 / 18:19:45 / cg" |
41 | 239 |
! ! |
240 |
||
3 | 241 |
!XBMReader methodsFor:'writing to file'! |
242 |
||
243 |
save:image onFile:aFileName |
|
202 | 244 |
"save image as XBM file on aFileName. |
245 |
Only depth1 b&w images can be represented in this format." |
|
3 | 246 |
|
33 | 247 |
|reverseBits bits byte |
66 | 248 |
h "{ Class: SmallInteger }" |
33 | 249 |
srcIndex "{ Class: SmallInteger }" |
250 |
rowBytes "{ Class: SmallInteger }" | |
|
3 | 251 |
|
51 | 252 |
(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
|
253 |
^ Image cannotRepresentImageSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
254 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
255 |
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
|
256 |
]. |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
257 |
|
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
258 |
image mask notNil ifTrue:[ |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
259 |
Image informationLostQuerySignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
260 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
261 |
errorString:('XBM format does not support an imageMask'). |
51 | 262 |
]. |
263 |
||
3 | 264 |
outStream := FileStream newFileNamed:aFileName. |
265 |
outStream isNil ifTrue:[ |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
266 |
^ Image fileCreationErrorSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
267 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
268 |
errorString:('file creation error: ' , aFileName asString). |
3 | 269 |
]. |
270 |
||
271 |
width := image width. |
|
272 |
height := image height. |
|
273 |
photometric := image photometric. |
|
274 |
samplesPerPixel := image samplesPerPixel. |
|
275 |
bitsPerSample := image bitsPerSample. |
|
276 |
colorMap := image colorMap. |
|
277 |
||
278 |
outStream nextPutAll: '#define xbm_width '. |
|
279 |
outStream nextPutAll:(width printString). |
|
280 |
outStream cr. |
|
281 |
outStream nextPutAll: '#define xbm_height '. |
|
282 |
outStream nextPutAll:(height printString). |
|
283 |
outStream cr. |
|
284 |
outStream nextPutAll: 'static char xbm_bits[] = {'; cr. |
|
285 |
||
286 |
reverseBits := self class reverseBits. |
|
287 |
||
288 |
rowBytes := width + 7 // 8. |
|
289 |
data := image bits. |
|
290 |
srcIndex := 1. |
|
291 |
||
66 | 292 |
h := height. |
293 |
h timesRepeat:[ |
|
202 | 294 |
rowBytes timesRepeat:[ |
295 |
outStream nextPutAll: '0x'. |
|
296 |
bits := data at:srcIndex. srcIndex := srcIndex + 1. |
|
297 |
photometric == #blackIs0 ifTrue:[ |
|
298 |
bits := bits bitInvert bitAnd:16rFF |
|
299 |
]. |
|
300 |
byte := (reverseBits at:(bits + 1)). |
|
301 |
byte < 16 ifTrue:[ |
|
302 |
outStream nextPut:$0 |
|
303 |
]. |
|
272
477aeb0d62b6
printOn:radix: -> printOn:base:
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
304 |
byte printOn:outStream base:16. |
202 | 305 |
outStream nextPutAll: ', '. |
306 |
]. |
|
307 |
outStream cr |
|
3 | 308 |
]. |
309 |
outStream nextPutAll: '};'; cr. |
|
310 |
outStream close |
|
311 |
||
33 | 312 |
" |
313 |
XBMReader save:(Image fromFile:'bitmaps/SBrowser.xbm') onFile:'test.xbm' |
|
314 |
" |
|
315 |
" |
|
316 |
convert sun icon to XBM format: |
|
317 |
||
318 |
XBMReader save:(Image fromFile:'bitmaps/hello_world.icon') onFile:'test.xbm' |
|
319 |
" |
|
202 | 320 |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
321 |
"Modified: 27.2.1997 / 12:46:49 / cg" |
3 | 322 |
! ! |
323 |
||
135 | 324 |
!XBMReader class methodsFor:'documentation'! |
325 |
||
326 |
version |
|
1049 | 327 |
^ '$Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.40 1998-08-19 22:40:06 cg Exp $' |
135 | 328 |
! ! |
41 | 329 |
XBMReader initialize! |