author | Claus Gittinger <cg@exept.de> |
Fri, 12 Sep 2003 12:38:06 +0200 | |
changeset 1812 | 5c902c4135f1 |
parent 1805 | 93f557cbe600 |
child 1814 | 2f204c2a957d |
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 |
||
1495 | 13 |
"{ Package: 'stx:libview2' }" |
14 |
||
0 | 15 |
ImageReader subclass:#XBMReader |
195 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1710
diff
changeset
|
19 |
category:'Graphics-Images-Readers' |
0 | 20 |
! |
21 |
||
21 | 22 |
!XBMReader class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
28 | 27 |
All Rights Reserved |
0 | 28 |
|
21 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
0 | 37 |
|
21 | 38 |
documentation |
39 |
" |
|
40 |
this class provides methods for loading and saving x-bitmap-file images. |
|
41 |
These images can (for example) be created using the bitmap editor supplied |
|
24 | 42 |
with X. |
43 |
Only monochrome images can be represented in this format. |
|
203 | 44 |
Both reading and writing of images is supported. |
195 | 45 |
|
46 |
[See also:] |
|
234 | 47 |
Image Form Icon |
197 | 48 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader |
210 | 49 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
197 | 50 |
XPMReader XWDReader |
220 | 51 |
|
52 |
[author:] |
|
53 |
Claus Gittinger |
|
21 | 54 |
" |
1812 | 55 |
! |
56 |
||
57 |
examples |
|
58 |
" |
|
59 |
Reading from a file: |
|
60 |
[exBegin] |
|
61 |
|image| |
|
62 |
||
63 |
image := Image fromFile:('../../goodies/bitmaps/xbmBitmaps/TicTacToe.xbm'). |
|
64 |
image inspect |
|
65 |
[exEnd] |
|
66 |
||
67 |
||
68 |
Saving to a file: |
|
69 |
[exBegin] |
|
70 |
|image| |
|
71 |
||
72 |
image := Image fromScreen:(0@0 corner:30@30). |
|
73 |
image := image asThresholdMonochromeImage. |
|
74 |
XBMReader save:image onFile:'/tmp/test.xbm'. |
|
75 |
'/tmp/test.xbm' asFilename contents asString inspect. |
|
76 |
(Image fromFile:('/tmp/test.xbm')) inspect. |
|
77 |
[exEnd] |
|
78 |
||
79 |
||
80 |
Or directly into a stream: |
|
81 |
[exBegin] |
|
82 |
|image stream| |
|
83 |
||
84 |
image := Image fromScreen:(0@0 corner:30@30). |
|
85 |
image := image asThresholdMonochromeImage. |
|
86 |
stream := WriteStream on:(String new). |
|
87 |
XPMReader save:image onStream:stream. |
|
88 |
stream contents inspect. |
|
89 |
[exEnd] |
|
90 |
" |
|
21 | 91 |
! ! |
0 | 92 |
|
28 | 93 |
!XBMReader class methodsFor:'initialization'! |
94 |
||
95 |
initialize |
|
202 | 96 |
"tell Image-class, that a new fileReader is present |
97 |
for the '.xbm' extension." |
|
98 |
||
647 | 99 |
MIMETypes defineImageType:'image/x-xbitmap' suffix:'xbm' reader:self. |
100 |
MIMETypes defineImageType:nil suffix:'bm' reader:self. |
|
202 | 101 |
|
399 | 102 |
"Modified: 1.2.1997 / 15:08:18 / cg" |
28 | 103 |
! ! |
104 |
||
41 | 105 |
!XBMReader class methodsFor:'testing'! |
106 |
||
135 | 107 |
canRepresent:anImage |
108 |
"return true, if anImage can be represented in my file format" |
|
109 |
||
1046
8670e67344de
fixed canRepresent for palette images.
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
110 |
|photometric clr0 clr1| |
135 | 111 |
|
1521 | 112 |
(anImage depth == 1) ifTrue:[ |
113 |
photometric := anImage photometric. |
|
114 |
((photometric == #blackIs0) or:[photometric == #whiteIs0]) ifTrue:[^ true]. |
|
1495 | 115 |
|
1521 | 116 |
photometric == #palette ifTrue:[ |
117 |
clr0 := anImage colorFromValue:0. |
|
118 |
clr1 := anImage colorFromValue:1. |
|
119 |
(clr0 = Color white and:[clr1 = Color black]) ifTrue:[^true]. |
|
120 |
(clr1 = Color white and:[clr0 = Color black]) ifTrue:[^true]. |
|
121 |
]. |
|
1046
8670e67344de
fixed canRepresent for palette images.
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
122 |
]. |
1521 | 123 |
Transcript showCR:'XBMReader: not a b&w image.'. |
1046
8670e67344de
fixed canRepresent for palette images.
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
124 |
^ false |
8670e67344de
fixed canRepresent for palette images.
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
125 |
|
8670e67344de
fixed canRepresent for palette images.
Claus Gittinger <cg@exept.de>
parents:
816
diff
changeset
|
126 |
"Modified: / 17.8.1998 / 10:17:01 / cg" |
135 | 127 |
! |
128 |
||
41 | 129 |
isValidImageFile:aFileName |
130 |
"return true, if aFileName contains an x-bitmap-file image" |
|
131 |
||
132 |
|line inStream index1 index2 keyword| |
|
133 |
||
134 |
inStream := self streamReadingFile:aFileName. |
|
135 |
inStream isNil ifTrue:[^ false]. |
|
136 |
||
550 | 137 |
Stream readErrorSignal handle:[:ex | |
138 |
line := nil. |
|
139 |
] do:[ |
|
1510
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1506
diff
changeset
|
140 |
Stream lineTooLongErrorSignal handle:[:ex | |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1506
diff
changeset
|
141 |
line := nil. |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1506
diff
changeset
|
142 |
Transcript showCR:'long line'. |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1506
diff
changeset
|
143 |
ex return. |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1506
diff
changeset
|
144 |
] do:[ |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1506
diff
changeset
|
145 |
line := inStream nextLine. |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1506
diff
changeset
|
146 |
]. |
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
147 |
[line notNil and:[line isEmpty]] whileTrue:[ |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
148 |
line := inStream nextLine. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
149 |
]. |
550 | 150 |
]. |
41 | 151 |
line isNil ifTrue:[ |
550 | 152 |
inStream close. |
153 |
^ false |
|
41 | 154 |
]. |
155 |
[line startsWith:'#'] whileFalse:[ |
|
558
98c4c983a02b
oops - handle read of binaries
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
156 |
Stream readErrorSignal handle:[:ex | |
98c4c983a02b
oops - handle read of binaries
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
157 |
line := nil. |
98c4c983a02b
oops - handle read of binaries
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
158 |
] do:[ |
1510
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1506
diff
changeset
|
159 |
Stream lineTooLongErrorSignal handle:[:ex | |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1506
diff
changeset
|
160 |
line := nil. |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1506
diff
changeset
|
161 |
Transcript showCR:'long line'. |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1506
diff
changeset
|
162 |
ex return. |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1506
diff
changeset
|
163 |
] do:[ |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1506
diff
changeset
|
164 |
line := inStream nextLine. |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1506
diff
changeset
|
165 |
]. |
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
166 |
[line notNil and:[line isEmpty]] whileTrue:[ |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
167 |
line := inStream nextLine. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
168 |
]. |
558
98c4c983a02b
oops - handle read of binaries
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
169 |
]. |
550 | 170 |
line isNil ifTrue:[ |
171 |
inStream close. |
|
172 |
^ false |
|
173 |
] |
|
41 | 174 |
]. |
175 |
index1 := line indexOf:(Character space). |
|
176 |
index2 := line indexOf:(Character space) startingAt:(index1 + 1). |
|
177 |
(index2 == 0) ifTrue:[ |
|
550 | 178 |
inStream close. |
179 |
^ false |
|
41 | 180 |
]. |
181 |
keyword := line copyFrom:index1 to:(index2 - 1). |
|
182 |
(keyword endsWith:'_width') ifFalse:[ |
|
550 | 183 |
inStream close. |
184 |
^ false |
|
41 | 185 |
]. |
186 |
inStream close. |
|
187 |
^ true |
|
550 | 188 |
|
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
189 |
"Modified: / 18.3.1999 / 11:33:39 / cg" |
41 | 190 |
! ! |
191 |
||
1805 | 192 |
!XBMReader methodsFor:'reading'! |
41 | 193 |
|
194 |
fromStream:aStream |
|
202 | 195 |
"read an image in xbm format from aStream" |
196 |
||
524 | 197 |
|lineString |
41 | 198 |
index "{ Class: SmallInteger }" |
199 |
dstIndex "{ Class: SmallInteger }" |
|
200 |
bytesPerRow |
|
201 |
lo "{ Class: SmallInteger }" |
|
202 |
hi "{ Class: SmallInteger }" |
|
203 |
val "{ Class: SmallInteger }" |
|
204 |
reverseBits| |
|
205 |
||
206 |
inStream := aStream. |
|
207 |
||
524 | 208 |
lineString := aStream nextLine. |
209 |
lineString isNil ifTrue:[ |
|
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
210 |
^ self fileFormatError:'short file'. |
41 | 211 |
]. |
212 |
||
524 | 213 |
[lineString startsWith:'#'] whileFalse:[ |
214 |
lineString := aStream nextLine. |
|
215 |
lineString isNil ifTrue:[ |
|
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
216 |
^ self fileFormatError:'short file'. |
493 | 217 |
]. |
41 | 218 |
]. |
219 |
||
524 | 220 |
(lineString startsWith:'#define') ifFalse:[ |
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
221 |
^ self fileFormatError:'format error (expected #define)'. |
41 | 222 |
]. |
223 |
||
524 | 224 |
index := lineString indexOf:(Character space). |
225 |
index := lineString indexOf:(Character space) startingAt:(index + 1). |
|
41 | 226 |
(index == 0) ifTrue:[ |
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
227 |
^ self fileFormatError:'format error'. |
41 | 228 |
]. |
524 | 229 |
((lineString copyTo:index - 1) endsWith:'width') ifFalse:[ |
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
230 |
^ self fileFormatError:'format error (expected width)'. |
41 | 231 |
]. |
524 | 232 |
lineString := lineString copyFrom:(index + 1). |
1049 | 233 |
width := Number readFromString:lineString onError:nil. |
234 |
width isNil ifTrue:[ |
|
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
235 |
^ self fileFormatError:'format error (expected width)'. |
1049 | 236 |
]. |
41 | 237 |
|
524 | 238 |
lineString := aStream nextLine. |
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
239 |
[lineString notNil and:[lineString isEmpty]] whileTrue:[ |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
240 |
lineString := aStream nextLine. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
241 |
]. |
524 | 242 |
index := lineString indexOf:(Character space). |
243 |
index := lineString indexOf:(Character space) startingAt:(index + 1). |
|
41 | 244 |
(index == 0) ifTrue:[ |
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
245 |
^ self fileFormatError:'format error'. |
41 | 246 |
]. |
524 | 247 |
((lineString copyTo:index - 1) endsWith:'height') ifFalse:[ |
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
248 |
^ self fileFormatError:'format error (expected height)'. |
41 | 249 |
]. |
524 | 250 |
lineString := lineString copyFrom:(index + 1). |
1049 | 251 |
height := Number readFromString:lineString onError:nil. |
252 |
height isNil ifTrue:[ |
|
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
253 |
^ self fileFormatError:'format error (expected height)'. |
1049 | 254 |
]. |
41 | 255 |
|
256 |
bytesPerRow := width // 8. |
|
257 |
((width \\ 8) ~~ 0) ifTrue:[ |
|
202 | 258 |
bytesPerRow := bytesPerRow + 1 |
41 | 259 |
]. |
260 |
||
261 |
reverseBits := self class reverseBits. |
|
262 |
||
263 |
data := ByteArray new:(bytesPerRow * height). |
|
264 |
dstIndex := 1. |
|
265 |
||
524 | 266 |
lineString := aStream nextLine. |
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
267 |
[(lineString startsWith:'#') |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
268 |
or:[lineString isEmpty]] whileTrue:[ |
524 | 269 |
lineString := aStream nextLine. |
41 | 270 |
]. |
271 |
||
524 | 272 |
[lineString notNil and:[(lineString startsWith:'static') not]] whileTrue:[ |
273 |
lineString := aStream nextLine. |
|
53 | 274 |
]. |
524 | 275 |
lineString := aStream nextLine. |
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
276 |
[lineString notNil and:[lineString isEmpty]] whileTrue:[ |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
277 |
lineString := aStream nextLine. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
278 |
]. |
53 | 279 |
|
524 | 280 |
[lineString notNil] whileTrue:[ |
1506
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
281 |
dstIndex <= data size ifTrue:[ |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
282 |
index := 1. |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
283 |
[index ~~ 0] whileTrue:[ |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
284 |
dstIndex <= data size ifTrue:[ |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
285 |
index := lineString indexOf:$x startingAt:index. |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
286 |
(index ~~ 0) ifTrue:[ |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
287 |
index := index + 1. |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
288 |
hi := (lineString at:index) digitValue. |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
289 |
index := index + 1. |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
290 |
lo := (lineString at:index) digitValue. |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
291 |
val := (hi bitShift:4) bitOr:lo. |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
292 |
data at:dstIndex put:(reverseBits at:(val + 1)). |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
293 |
dstIndex := dstIndex + 1 |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
294 |
] |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
295 |
] ifFalse:[ |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
296 |
index := 0. "/ break loop |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
297 |
] |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
298 |
]. |
202 | 299 |
]. |
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
300 |
lineString := aStream nextLine. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
301 |
[lineString notNil and:[lineString isEmpty]] whileTrue:[ |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
302 |
lineString := aStream nextLine. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
303 |
]. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
304 |
|
41 | 305 |
]. |
306 |
photometric := #whiteIs0. |
|
307 |
samplesPerPixel := 1. |
|
308 |
bitsPerSample := #(1). |
|
309 |
||
310 |
" |
|
311 |
XBMReader fromFile:'bitmaps/globe1.xbm' |
|
202 | 312 |
" |
313 |
||
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
314 |
"Modified: / 18.3.1999 / 11:32:46 / cg" |
41 | 315 |
! ! |
316 |
||
1805 | 317 |
!XBMReader methodsFor:'writing'! |
3 | 318 |
|
319 |
save:image onFile:aFileName |
|
202 | 320 |
"save image as XBM file on aFileName. |
321 |
Only depth1 b&w images can be represented in this format." |
|
3 | 322 |
|
1812 | 323 |
|stream| |
324 |
||
325 |
"sigh - must check before creating file" |
|
326 |
(self class canRepresent:image) ifFalse:[ |
|
327 |
^ Image cannotRepresentImageSignal |
|
328 |
raiseWith:image |
|
329 |
errorString:('XBM format only supports monochrome images'). |
|
330 |
]. |
|
331 |
||
332 |
image mask notNil ifTrue:[ |
|
333 |
Image informationLostQuerySignal |
|
334 |
raiseWith:image |
|
335 |
errorString:('XBM format does not support an imageMask'). |
|
336 |
]. |
|
337 |
||
338 |
[ |
|
339 |
stream := aFileName asFilename newReadWriteStream. |
|
340 |
] on:FileStream openErrorSignal do:[:ex| |
|
341 |
^ Image fileCreationErrorSignal |
|
342 |
raiseWith:image |
|
343 |
errorString:('file creation error: ' , aFileName asString). |
|
344 |
]. |
|
345 |
||
346 |
self save:image onStream:stream. |
|
347 |
stream close |
|
348 |
||
349 |
" |
|
350 |
XBMReader save:(Image fromFile:'../../goodies/bitmaps/xbmBitmaps/TicTacToe.xbm') onFile:'/tmp/test.xbm' |
|
351 |
" |
|
352 |
" |
|
353 |
convert sun icon to XBM format: |
|
354 |
||
355 |
XBMReader save:(Image fromFile:'bitmaps/hello_world.icon') onFile:'test.xbm' |
|
356 |
" |
|
357 |
||
358 |
"Modified: 27.2.1997 / 12:46:49 / cg" |
|
359 |
! |
|
360 |
||
361 |
save:image onStream:aStream |
|
362 |
"save image as XBM cdata on aStream. |
|
363 |
Only depth1 b&w images can be represented in this format." |
|
364 |
||
33 | 365 |
|reverseBits bits byte |
66 | 366 |
h "{ Class: SmallInteger }" |
33 | 367 |
srcIndex "{ Class: SmallInteger }" |
368 |
rowBytes "{ Class: SmallInteger }" | |
|
3 | 369 |
|
51 | 370 |
(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
|
371 |
^ Image cannotRepresentImageSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
372 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
373 |
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
|
374 |
]. |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
375 |
|
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
376 |
image mask notNil ifTrue:[ |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
377 |
Image informationLostQuerySignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
378 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
379 |
errorString:('XBM format does not support an imageMask'). |
51 | 380 |
]. |
381 |
||
1812 | 382 |
outStream := aStream. |
3 | 383 |
|
384 |
width := image width. |
|
385 |
height := image height. |
|
386 |
photometric := image photometric. |
|
387 |
samplesPerPixel := image samplesPerPixel. |
|
388 |
bitsPerSample := image bitsPerSample. |
|
389 |
colorMap := image colorMap. |
|
390 |
||
391 |
outStream nextPutAll: '#define xbm_width '. |
|
392 |
outStream nextPutAll:(width printString). |
|
393 |
outStream cr. |
|
394 |
outStream nextPutAll: '#define xbm_height '. |
|
395 |
outStream nextPutAll:(height printString). |
|
396 |
outStream cr. |
|
397 |
outStream nextPutAll: 'static char xbm_bits[] = {'; cr. |
|
398 |
||
399 |
reverseBits := self class reverseBits. |
|
400 |
||
401 |
rowBytes := width + 7 // 8. |
|
402 |
data := image bits. |
|
403 |
srcIndex := 1. |
|
404 |
||
66 | 405 |
h := height. |
406 |
h timesRepeat:[ |
|
202 | 407 |
rowBytes timesRepeat:[ |
408 |
outStream nextPutAll: '0x'. |
|
409 |
bits := data at:srcIndex. srcIndex := srcIndex + 1. |
|
410 |
photometric == #blackIs0 ifTrue:[ |
|
411 |
bits := bits bitInvert bitAnd:16rFF |
|
412 |
]. |
|
413 |
byte := (reverseBits at:(bits + 1)). |
|
414 |
byte < 16 ifTrue:[ |
|
415 |
outStream nextPut:$0 |
|
416 |
]. |
|
272
477aeb0d62b6
printOn:radix: -> printOn:base:
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
417 |
byte printOn:outStream base:16. |
202 | 418 |
outStream nextPutAll: ', '. |
419 |
]. |
|
420 |
outStream cr |
|
3 | 421 |
]. |
422 |
outStream nextPutAll: '};'; cr. |
|
423 |
||
33 | 424 |
" |
1812 | 425 |
XBMReader save:(Image fromFile:'../../goodies/bitmaps/xbmBitmaps/TicTacToe.xbm') onStream:Transcript |
33 | 426 |
" |
3 | 427 |
! ! |
428 |
||
135 | 429 |
!XBMReader class methodsFor:'documentation'! |
430 |
||
431 |
version |
|
1812 | 432 |
^ '$Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.49 2003-09-12 10:38:06 cg Exp $' |
135 | 433 |
! ! |
1710
ad35e6a18e98
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1521
diff
changeset
|
434 |
|
41 | 435 |
XBMReader initialize! |