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