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