author | Claus Gittinger <cg@exept.de> |
Tue, 24 Jun 1997 22:05:25 +0200 | |
changeset 623 | ea587e8fd435 |
parent 558 | 98c4c983a02b |
child 647 | 6f26c76aa0c9 |
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 |
||
623 | 61 |
Image addReader:self suffix:'xbm' mimeType:'image/x-xbitmap'. |
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:[ |
|
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:[ |
|
357
4bcb93f5892e
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
272
diff
changeset
|
142 |
'XBMReader [warning]: short file' errorPrintCR. |
202 | 143 |
^ nil |
41 | 144 |
]. |
145 |
||
524 | 146 |
[lineString startsWith:'#'] whileFalse:[ |
147 |
lineString := aStream nextLine. |
|
148 |
lineString isNil ifTrue:[ |
|
493 | 149 |
^ nil |
150 |
]. |
|
41 | 151 |
]. |
152 |
||
524 | 153 |
(lineString startsWith:'#define') ifFalse:[ |
357
4bcb93f5892e
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
272
diff
changeset
|
154 |
'XBMReader [warning]: format error (expected #define)' errorPrintCR. |
202 | 155 |
^ nil |
41 | 156 |
]. |
157 |
||
524 | 158 |
index := lineString indexOf:(Character space). |
159 |
index := lineString indexOf:(Character space) startingAt:(index + 1). |
|
41 | 160 |
(index == 0) ifTrue:[ |
357
4bcb93f5892e
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
272
diff
changeset
|
161 |
'XBMReader [warning]: format error' errorPrintCR. |
202 | 162 |
^ nil |
41 | 163 |
]. |
524 | 164 |
((lineString copyTo:index - 1) endsWith:'width') ifFalse:[ |
357
4bcb93f5892e
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
272
diff
changeset
|
165 |
'XBMReader [warning]: format error (expected width)' errorPrintCR. |
202 | 166 |
^ nil |
41 | 167 |
]. |
524 | 168 |
lineString := lineString copyFrom:(index + 1). |
169 |
width := Number readFromString:lineString. |
|
41 | 170 |
|
524 | 171 |
lineString := aStream nextLine. |
172 |
index := lineString indexOf:(Character space). |
|
173 |
index := lineString indexOf:(Character space) startingAt:(index + 1). |
|
41 | 174 |
(index == 0) ifTrue:[ |
357
4bcb93f5892e
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
272
diff
changeset
|
175 |
'XBMReader [warning]: format error' errorPrintCR. |
202 | 176 |
^ nil |
41 | 177 |
]. |
524 | 178 |
((lineString copyTo:index - 1) endsWith:'height') ifFalse:[ |
357
4bcb93f5892e
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
272
diff
changeset
|
179 |
'XBMReader [warning]: format error (expected height)' errorPrintCR. |
202 | 180 |
^ nil |
41 | 181 |
]. |
524 | 182 |
lineString := lineString copyFrom:(index + 1). |
183 |
height := Number readFromString:lineString. |
|
41 | 184 |
|
185 |
bytesPerRow := width // 8. |
|
186 |
((width \\ 8) ~~ 0) ifTrue:[ |
|
202 | 187 |
bytesPerRow := bytesPerRow + 1 |
41 | 188 |
]. |
189 |
||
190 |
reverseBits := self class reverseBits. |
|
191 |
||
192 |
data := ByteArray new:(bytesPerRow * height). |
|
193 |
dstIndex := 1. |
|
194 |
||
524 | 195 |
lineString := aStream nextLine. |
196 |
[lineString startsWith:'#'] whileTrue:[ |
|
197 |
lineString := aStream nextLine. |
|
41 | 198 |
]. |
199 |
||
524 | 200 |
[lineString notNil and:[(lineString startsWith:'static') not]] whileTrue:[ |
201 |
lineString := aStream nextLine. |
|
53 | 202 |
]. |
524 | 203 |
lineString := aStream nextLine. |
53 | 204 |
|
524 | 205 |
[lineString notNil] whileTrue:[ |
202 | 206 |
index := 1. |
207 |
[index ~~ 0] whileTrue:[ |
|
524 | 208 |
index := lineString indexOf:$x startingAt:index. |
202 | 209 |
(index ~~ 0) ifTrue:[ |
210 |
index := index + 1. |
|
524 | 211 |
hi := (lineString at:index) digitValue. |
202 | 212 |
index := index + 1. |
524 | 213 |
lo := (lineString at:index) digitValue. |
202 | 214 |
val := (hi bitShift:4) bitOr:lo. |
215 |
data at:dstIndex put:(reverseBits at:(val + 1)). |
|
216 |
dstIndex := dstIndex + 1 |
|
217 |
] |
|
218 |
]. |
|
524 | 219 |
lineString := aStream nextLine |
41 | 220 |
]. |
221 |
photometric := #whiteIs0. |
|
222 |
samplesPerPixel := 1. |
|
223 |
bitsPerSample := #(1). |
|
224 |
||
225 |
" |
|
226 |
XBMReader fromFile:'bitmaps/globe1.xbm' |
|
202 | 227 |
" |
228 |
||
524 | 229 |
"Modified: 11.4.1997 / 16:51:21 / cg" |
41 | 230 |
! ! |
231 |
||
3 | 232 |
!XBMReader methodsFor:'writing to file'! |
233 |
||
234 |
save:image onFile:aFileName |
|
202 | 235 |
"save image as XBM file on aFileName. |
236 |
Only depth1 b&w images can be represented in this format." |
|
3 | 237 |
|
33 | 238 |
|reverseBits bits byte |
66 | 239 |
h "{ Class: SmallInteger }" |
33 | 240 |
srcIndex "{ Class: SmallInteger }" |
241 |
rowBytes "{ Class: SmallInteger }" | |
|
3 | 242 |
|
51 | 243 |
(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
|
244 |
^ Image cannotRepresentImageSignal |
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 only supports monochrome images'). |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
247 |
]. |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
248 |
|
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
249 |
image mask notNil ifTrue:[ |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
250 |
Image informationLostQuerySignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
251 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
252 |
errorString:('XBM format does not support an imageMask'). |
51 | 253 |
]. |
254 |
||
3 | 255 |
outStream := FileStream newFileNamed:aFileName. |
256 |
outStream isNil ifTrue:[ |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
257 |
^ Image fileCreationErrorSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
258 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
259 |
errorString:('file creation error: ' , aFileName asString). |
3 | 260 |
]. |
261 |
||
262 |
width := image width. |
|
263 |
height := image height. |
|
264 |
photometric := image photometric. |
|
265 |
samplesPerPixel := image samplesPerPixel. |
|
266 |
bitsPerSample := image bitsPerSample. |
|
267 |
colorMap := image colorMap. |
|
268 |
||
269 |
outStream nextPutAll: '#define xbm_width '. |
|
270 |
outStream nextPutAll:(width printString). |
|
271 |
outStream cr. |
|
272 |
outStream nextPutAll: '#define xbm_height '. |
|
273 |
outStream nextPutAll:(height printString). |
|
274 |
outStream cr. |
|
275 |
outStream nextPutAll: 'static char xbm_bits[] = {'; cr. |
|
276 |
||
277 |
reverseBits := self class reverseBits. |
|
278 |
||
279 |
rowBytes := width + 7 // 8. |
|
280 |
data := image bits. |
|
281 |
srcIndex := 1. |
|
282 |
||
66 | 283 |
h := height. |
284 |
h timesRepeat:[ |
|
202 | 285 |
rowBytes timesRepeat:[ |
286 |
outStream nextPutAll: '0x'. |
|
287 |
bits := data at:srcIndex. srcIndex := srcIndex + 1. |
|
288 |
photometric == #blackIs0 ifTrue:[ |
|
289 |
bits := bits bitInvert bitAnd:16rFF |
|
290 |
]. |
|
291 |
byte := (reverseBits at:(bits + 1)). |
|
292 |
byte < 16 ifTrue:[ |
|
293 |
outStream nextPut:$0 |
|
294 |
]. |
|
272
477aeb0d62b6
printOn:radix: -> printOn:base:
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
295 |
byte printOn:outStream base:16. |
202 | 296 |
outStream nextPutAll: ', '. |
297 |
]. |
|
298 |
outStream cr |
|
3 | 299 |
]. |
300 |
outStream nextPutAll: '};'; cr. |
|
301 |
outStream close |
|
302 |
||
33 | 303 |
" |
304 |
XBMReader save:(Image fromFile:'bitmaps/SBrowser.xbm') onFile:'test.xbm' |
|
305 |
" |
|
306 |
" |
|
307 |
convert sun icon to XBM format: |
|
308 |
||
309 |
XBMReader save:(Image fromFile:'bitmaps/hello_world.icon') onFile:'test.xbm' |
|
310 |
" |
|
202 | 311 |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
312 |
"Modified: 27.2.1997 / 12:46:49 / cg" |
3 | 313 |
! ! |
314 |
||
135 | 315 |
!XBMReader class methodsFor:'documentation'! |
316 |
||
317 |
version |
|
623 | 318 |
^ '$Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.36 1997-06-24 20:05:17 cg Exp $' |
135 | 319 |
! ! |
41 | 320 |
XBMReader initialize! |