author | fm |
Tue, 24 Apr 2007 18:17:40 +0200 | |
changeset 2325 | 634b74929d2d |
parent 2051 | 502fbd99483d |
child 2804 | 9b50492473d5 |
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 |
]. |
2030 | 123 |
('XBMReader [info]: image is not a b&w (depth 1) image.') infoPrintCR. |
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 |
||
1844
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
192 |
!XBMReader methodsFor:'private-reading'! |
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
193 |
|
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
194 |
extractValueFor:keyword fromLine:lineString |
2051 | 195 |
|index1 index2 value restString| |
1844
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
196 |
|
2051 | 197 |
index1 := lineString indexOf:(Character space). |
198 |
index2 := lineString indexOf:(Character space) startingAt:(index1 + 1). |
|
199 |
(index2 == 0) ifTrue:[ |
|
1844
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
200 |
^ nil. |
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
201 |
]. |
2051 | 202 |
((lineString copyTo:index2 - 1) endsWith:keyword) ifFalse:[ |
1844
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
203 |
^ nil. |
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
204 |
]. |
2051 | 205 |
restString := lineString copyFrom:(index2 + 1). |
1844
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
206 |
value := Number readFromString:restString onError:nil. |
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
207 |
value isNil ifTrue:[ |
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
208 |
^ nil. |
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
209 |
]. |
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
210 |
^ value |
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
211 |
! ! |
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
212 |
|
1805 | 213 |
!XBMReader methodsFor:'reading'! |
41 | 214 |
|
215 |
fromStream:aStream |
|
202 | 216 |
"read an image in xbm format from aStream" |
217 |
||
524 | 218 |
|lineString |
41 | 219 |
index "{ Class: SmallInteger }" |
220 |
dstIndex "{ Class: SmallInteger }" |
|
221 |
bytesPerRow |
|
222 |
lo "{ Class: SmallInteger }" |
|
223 |
hi "{ Class: SmallInteger }" |
|
224 |
val "{ Class: SmallInteger }" |
|
225 |
reverseBits| |
|
226 |
||
227 |
inStream := aStream. |
|
228 |
||
524 | 229 |
lineString := aStream nextLine. |
230 |
lineString isNil ifTrue:[ |
|
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
231 |
^ self fileFormatError:'short file'. |
41 | 232 |
]. |
233 |
||
524 | 234 |
[lineString startsWith:'#'] whileFalse:[ |
235 |
lineString := aStream nextLine. |
|
236 |
lineString isNil ifTrue:[ |
|
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
237 |
^ self fileFormatError:'short file'. |
493 | 238 |
]. |
41 | 239 |
]. |
240 |
||
524 | 241 |
(lineString startsWith:'#define') ifFalse:[ |
816
fe0abc1f956d
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
242 |
^ self fileFormatError:'format error (expected #define)'. |
41 | 243 |
]. |
244 |
||
1844
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
245 |
width := self extractValueFor:'width' fromLine:lineString. |
1049 | 246 |
width isNil ifTrue:[ |
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
247 |
^ self fileFormatError:'format error (expected width)'. |
1049 | 248 |
]. |
41 | 249 |
|
524 | 250 |
lineString := aStream nextLine. |
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
251 |
[lineString notNil and:[lineString isEmpty]] whileTrue:[ |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
252 |
lineString := aStream nextLine. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
253 |
]. |
1844
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
254 |
height := self extractValueFor:'height' fromLine:lineString. |
1049 | 255 |
height isNil ifTrue:[ |
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
256 |
^ self fileFormatError:'format error (expected height)'. |
1049 | 257 |
]. |
41 | 258 |
|
1844
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
259 |
self reportDimension. |
85c539787d5b
dimensionReport & refactoring
Claus Gittinger <cg@exept.de>
parents:
1814
diff
changeset
|
260 |
|
41 | 261 |
bytesPerRow := width // 8. |
262 |
((width \\ 8) ~~ 0) ifTrue:[ |
|
202 | 263 |
bytesPerRow := bytesPerRow + 1 |
41 | 264 |
]. |
265 |
||
266 |
reverseBits := self class reverseBits. |
|
267 |
||
268 |
data := ByteArray new:(bytesPerRow * height). |
|
269 |
dstIndex := 1. |
|
270 |
||
524 | 271 |
lineString := aStream nextLine. |
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
272 |
[(lineString startsWith:'#') |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
273 |
or:[lineString isEmpty]] whileTrue:[ |
524 | 274 |
lineString := aStream nextLine. |
41 | 275 |
]. |
276 |
||
524 | 277 |
[lineString notNil and:[(lineString startsWith:'static') not]] whileTrue:[ |
278 |
lineString := aStream nextLine. |
|
53 | 279 |
]. |
524 | 280 |
lineString := aStream nextLine. |
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
281 |
[lineString notNil and:[lineString isEmpty]] whileTrue:[ |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
282 |
lineString := aStream nextLine. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
283 |
]. |
53 | 284 |
|
524 | 285 |
[lineString notNil] whileTrue:[ |
1506
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
286 |
dstIndex <= data size ifTrue:[ |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
287 |
index := 1. |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
288 |
[index ~~ 0] whileTrue:[ |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
289 |
dstIndex <= data size ifTrue:[ |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
290 |
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
|
291 |
(index ~~ 0) ifTrue:[ |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
292 |
index := index + 1. |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
293 |
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
|
294 |
index := index + 1. |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
295 |
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
|
296 |
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
|
297 |
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
|
298 |
dstIndex := dstIndex + 1 |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
299 |
] |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
300 |
] ifFalse:[ |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
301 |
index := 0. "/ break loop |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
302 |
] |
60e56746dce6
ignore junk at the end of the bitmap (comments etc)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
303 |
]. |
202 | 304 |
]. |
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
305 |
lineString := aStream nextLine. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
306 |
[lineString notNil and:[lineString isEmpty]] whileTrue:[ |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
307 |
lineString := aStream nextLine. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
308 |
]. |
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
309 |
|
41 | 310 |
]. |
311 |
photometric := #whiteIs0. |
|
312 |
samplesPerPixel := 1. |
|
313 |
bitsPerSample := #(1). |
|
314 |
||
315 |
" |
|
316 |
XBMReader fromFile:'bitmaps/globe1.xbm' |
|
202 | 317 |
" |
318 |
||
1144
c86a1e3e3884
ignore empty lines in input.
Claus Gittinger <cg@exept.de>
parents:
1049
diff
changeset
|
319 |
"Modified: / 18.3.1999 / 11:32:46 / cg" |
41 | 320 |
! ! |
321 |
||
1805 | 322 |
!XBMReader methodsFor:'writing'! |
3 | 323 |
|
1812 | 324 |
save:image onStream:aStream |
325 |
"save image as XBM cdata on aStream. |
|
326 |
Only depth1 b&w images can be represented in this format." |
|
327 |
||
33 | 328 |
|reverseBits bits byte |
66 | 329 |
h "{ Class: SmallInteger }" |
33 | 330 |
srcIndex "{ Class: SmallInteger }" |
331 |
rowBytes "{ Class: SmallInteger }" | |
|
3 | 332 |
|
51 | 333 |
(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
|
334 |
^ Image cannotRepresentImageSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
335 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
336 |
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
|
337 |
]. |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
338 |
|
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
339 |
image mask notNil ifTrue:[ |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
340 |
Image informationLostQuerySignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
341 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
342 |
errorString:('XBM format does not support an imageMask'). |
51 | 343 |
]. |
344 |
||
1812 | 345 |
outStream := aStream. |
3 | 346 |
|
347 |
width := image width. |
|
348 |
height := image height. |
|
349 |
photometric := image photometric. |
|
350 |
samplesPerPixel := image samplesPerPixel. |
|
351 |
bitsPerSample := image bitsPerSample. |
|
352 |
colorMap := image colorMap. |
|
353 |
||
354 |
outStream nextPutAll: '#define xbm_width '. |
|
355 |
outStream nextPutAll:(width printString). |
|
356 |
outStream cr. |
|
357 |
outStream nextPutAll: '#define xbm_height '. |
|
358 |
outStream nextPutAll:(height printString). |
|
359 |
outStream cr. |
|
360 |
outStream nextPutAll: 'static char xbm_bits[] = {'; cr. |
|
361 |
||
362 |
reverseBits := self class reverseBits. |
|
363 |
||
364 |
rowBytes := width + 7 // 8. |
|
365 |
data := image bits. |
|
366 |
srcIndex := 1. |
|
367 |
||
66 | 368 |
h := height. |
369 |
h timesRepeat:[ |
|
202 | 370 |
rowBytes timesRepeat:[ |
371 |
outStream nextPutAll: '0x'. |
|
372 |
bits := data at:srcIndex. srcIndex := srcIndex + 1. |
|
373 |
photometric == #blackIs0 ifTrue:[ |
|
374 |
bits := bits bitInvert bitAnd:16rFF |
|
375 |
]. |
|
376 |
byte := (reverseBits at:(bits + 1)). |
|
377 |
byte < 16 ifTrue:[ |
|
378 |
outStream nextPut:$0 |
|
379 |
]. |
|
272
477aeb0d62b6
printOn:radix: -> printOn:base:
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
380 |
byte printOn:outStream base:16. |
202 | 381 |
outStream nextPutAll: ', '. |
382 |
]. |
|
383 |
outStream cr |
|
3 | 384 |
]. |
385 |
outStream nextPutAll: '};'; cr. |
|
386 |
||
33 | 387 |
" |
1812 | 388 |
XBMReader save:(Image fromFile:'../../goodies/bitmaps/xbmBitmaps/TicTacToe.xbm') onStream:Transcript |
33 | 389 |
" |
3 | 390 |
! ! |
391 |
||
135 | 392 |
!XBMReader class methodsFor:'documentation'! |
393 |
||
394 |
version |
|
2051 | 395 |
^ '$Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.53 2005-07-11 13:20:15 cg Exp $' |
135 | 396 |
! ! |
1710
ad35e6a18e98
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
1521
diff
changeset
|
397 |
|
41 | 398 |
XBMReader initialize! |