author | Claus Gittinger <cg@exept.de> |
Mon, 17 Aug 1998 10:17:18 +0200 | |
changeset 1046 | 8670e67344de |
parent 816 | fe0abc1f956d |
child 1049 | 1afeefeedd14 |
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). |
174 |
width := Number readFromString:lineString. |
|
41 | 175 |
|
524 | 176 |
lineString := aStream nextLine. |
177 |
index := lineString indexOf:(Character space). |
|
178 |
index := lineString indexOf:(Character space) startingAt:(index + 1). |
|
41 | 179 |
(index == 0) ifTrue:[ |
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
180 |
^ self fileFormatError:'format error'. |
41 | 181 |
]. |
524 | 182 |
((lineString copyTo:index - 1) endsWith:'height') ifFalse:[ |
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
183 |
^ self fileFormatError:'format error (expected height)'. |
41 | 184 |
]. |
524 | 185 |
lineString := lineString copyFrom:(index + 1). |
186 |
height := Number readFromString:lineString. |
|
41 | 187 |
|
188 |
bytesPerRow := width // 8. |
|
189 |
((width \\ 8) ~~ 0) ifTrue:[ |
|
202 | 190 |
bytesPerRow := bytesPerRow + 1 |
41 | 191 |
]. |
192 |
||
193 |
reverseBits := self class reverseBits. |
|
194 |
||
195 |
data := ByteArray new:(bytesPerRow * height). |
|
196 |
dstIndex := 1. |
|
197 |
||
524 | 198 |
lineString := aStream nextLine. |
199 |
[lineString startsWith:'#'] whileTrue:[ |
|
200 |
lineString := aStream nextLine. |
|
41 | 201 |
]. |
202 |
||
524 | 203 |
[lineString notNil and:[(lineString startsWith:'static') not]] whileTrue:[ |
204 |
lineString := aStream nextLine. |
|
53 | 205 |
]. |
524 | 206 |
lineString := aStream nextLine. |
53 | 207 |
|
524 | 208 |
[lineString notNil] whileTrue:[ |
202 | 209 |
index := 1. |
210 |
[index ~~ 0] whileTrue:[ |
|
524 | 211 |
index := lineString indexOf:$x startingAt:index. |
202 | 212 |
(index ~~ 0) ifTrue:[ |
213 |
index := index + 1. |
|
524 | 214 |
hi := (lineString at:index) digitValue. |
202 | 215 |
index := index + 1. |
524 | 216 |
lo := (lineString at:index) digitValue. |
202 | 217 |
val := (hi bitShift:4) bitOr:lo. |
218 |
data at:dstIndex put:(reverseBits at:(val + 1)). |
|
219 |
dstIndex := dstIndex + 1 |
|
220 |
] |
|
221 |
]. |
|
524 | 222 |
lineString := aStream nextLine |
41 | 223 |
]. |
224 |
photometric := #whiteIs0. |
|
225 |
samplesPerPixel := 1. |
|
226 |
bitsPerSample := #(1). |
|
227 |
||
228 |
" |
|
229 |
XBMReader fromFile:'bitmaps/globe1.xbm' |
|
202 | 230 |
" |
231 |
||
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
232 |
"Modified: / 3.2.1998 / 18:19:45 / cg" |
41 | 233 |
! ! |
234 |
||
3 | 235 |
!XBMReader methodsFor:'writing to file'! |
236 |
||
237 |
save:image onFile:aFileName |
|
202 | 238 |
"save image as XBM file on aFileName. |
239 |
Only depth1 b&w images can be represented in this format." |
|
3 | 240 |
|
33 | 241 |
|reverseBits bits byte |
66 | 242 |
h "{ Class: SmallInteger }" |
33 | 243 |
srcIndex "{ Class: SmallInteger }" |
244 |
rowBytes "{ Class: SmallInteger }" | |
|
3 | 245 |
|
51 | 246 |
(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
|
247 |
^ Image cannotRepresentImageSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
248 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
249 |
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
|
250 |
]. |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
251 |
|
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
252 |
image mask notNil ifTrue:[ |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
253 |
Image informationLostQuerySignal |
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 does not support an imageMask'). |
51 | 256 |
]. |
257 |
||
3 | 258 |
outStream := FileStream newFileNamed:aFileName. |
259 |
outStream isNil ifTrue:[ |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
260 |
^ Image fileCreationErrorSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
261 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
262 |
errorString:('file creation error: ' , aFileName asString). |
3 | 263 |
]. |
264 |
||
265 |
width := image width. |
|
266 |
height := image height. |
|
267 |
photometric := image photometric. |
|
268 |
samplesPerPixel := image samplesPerPixel. |
|
269 |
bitsPerSample := image bitsPerSample. |
|
270 |
colorMap := image colorMap. |
|
271 |
||
272 |
outStream nextPutAll: '#define xbm_width '. |
|
273 |
outStream nextPutAll:(width printString). |
|
274 |
outStream cr. |
|
275 |
outStream nextPutAll: '#define xbm_height '. |
|
276 |
outStream nextPutAll:(height printString). |
|
277 |
outStream cr. |
|
278 |
outStream nextPutAll: 'static char xbm_bits[] = {'; cr. |
|
279 |
||
280 |
reverseBits := self class reverseBits. |
|
281 |
||
282 |
rowBytes := width + 7 // 8. |
|
283 |
data := image bits. |
|
284 |
srcIndex := 1. |
|
285 |
||
66 | 286 |
h := height. |
287 |
h timesRepeat:[ |
|
202 | 288 |
rowBytes timesRepeat:[ |
289 |
outStream nextPutAll: '0x'. |
|
290 |
bits := data at:srcIndex. srcIndex := srcIndex + 1. |
|
291 |
photometric == #blackIs0 ifTrue:[ |
|
292 |
bits := bits bitInvert bitAnd:16rFF |
|
293 |
]. |
|
294 |
byte := (reverseBits at:(bits + 1)). |
|
295 |
byte < 16 ifTrue:[ |
|
296 |
outStream nextPut:$0 |
|
297 |
]. |
|
272
477aeb0d62b6
printOn:radix: -> printOn:base:
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
298 |
byte printOn:outStream base:16. |
202 | 299 |
outStream nextPutAll: ', '. |
300 |
]. |
|
301 |
outStream cr |
|
3 | 302 |
]. |
303 |
outStream nextPutAll: '};'; cr. |
|
304 |
outStream close |
|
305 |
||
33 | 306 |
" |
307 |
XBMReader save:(Image fromFile:'bitmaps/SBrowser.xbm') onFile:'test.xbm' |
|
308 |
" |
|
309 |
" |
|
310 |
convert sun icon to XBM format: |
|
311 |
||
312 |
XBMReader save:(Image fromFile:'bitmaps/hello_world.icon') onFile:'test.xbm' |
|
313 |
" |
|
202 | 314 |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
315 |
"Modified: 27.2.1997 / 12:46:49 / cg" |
3 | 316 |
! ! |
317 |
||
135 | 318 |
!XBMReader class methodsFor:'documentation'! |
319 |
||
320 |
version |
|
1046
8670e67344de
fixed canRepresent for palette images.
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
321 |
^ '$Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.39 1998-08-17 08:17:18 cg Exp $' |
135 | 322 |
! ! |
41 | 323 |
XBMReader initialize! |