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