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