author | Claus Gittinger <cg@exept.de> |
Sat, 01 Feb 1997 15:07:04 +0100 | |
changeset 398 | aef700d15416 |
parent 357 | 4bcb93f5892e |
child 399 | f87821ab7b30 |
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'. |
202 | 62 |
|
398 | 63 |
"Modified: 1.2.1997 / 15:04:09 / cg" |
28 | 64 |
! ! |
65 |
||
41 | 66 |
!XBMReader class methodsFor:'testing'! |
67 |
||
135 | 68 |
canRepresent:anImage |
69 |
"return true, if anImage can be represented in my file format" |
|
70 |
||
71 |
|photometric| |
|
72 |
||
73 |
(anImage depth ~~ 1) ifTrue:[^ false.]. |
|
74 |
(((photometric := anImage photometric) ~~ #blackIs0) and:[photometric ~~ #whiteIs0]) ifTrue:[^ false.]. |
|
75 |
^ true |
|
76 |
! |
|
77 |
||
41 | 78 |
isValidImageFile:aFileName |
79 |
"return true, if aFileName contains an x-bitmap-file image" |
|
80 |
||
81 |
|line inStream index1 index2 keyword| |
|
82 |
||
83 |
inStream := self streamReadingFile:aFileName. |
|
84 |
inStream isNil ifTrue:[^ false]. |
|
85 |
||
86 |
line := inStream nextLine. |
|
87 |
line isNil ifTrue:[ |
|
88 |
inStream close. |
|
89 |
^ false |
|
90 |
]. |
|
91 |
[line startsWith:'#'] whileFalse:[ |
|
92 |
line := inStream nextLine. |
|
93 |
line isNil ifTrue:[ |
|
94 |
inStream close. |
|
95 |
^ false |
|
96 |
] |
|
97 |
]. |
|
98 |
index1 := line indexOf:(Character space). |
|
99 |
index2 := line indexOf:(Character space) startingAt:(index1 + 1). |
|
100 |
(index2 == 0) ifTrue:[ |
|
101 |
inStream close. |
|
102 |
^ false |
|
103 |
]. |
|
104 |
keyword := line copyFrom:index1 to:(index2 - 1). |
|
105 |
(keyword endsWith:'_width') ifFalse:[ |
|
106 |
inStream close. |
|
107 |
^ false |
|
108 |
]. |
|
109 |
inStream close. |
|
110 |
^ true |
|
111 |
! ! |
|
112 |
||
113 |
!XBMReader methodsFor:'reading from file'! |
|
114 |
||
115 |
fromStream:aStream |
|
202 | 116 |
"read an image in xbm format from aStream" |
117 |
||
41 | 118 |
|line |
119 |
index "{ Class: SmallInteger }" |
|
120 |
dstIndex "{ Class: SmallInteger }" |
|
121 |
bytesPerRow |
|
122 |
lo "{ Class: SmallInteger }" |
|
123 |
hi "{ Class: SmallInteger }" |
|
124 |
val "{ Class: SmallInteger }" |
|
125 |
reverseBits| |
|
126 |
||
127 |
inStream := aStream. |
|
128 |
||
129 |
line := aStream nextLine. |
|
130 |
line isNil ifTrue:[ |
|
357
4bcb93f5892e
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
272
diff
changeset
|
131 |
'XBMReader [warning]: short file' errorPrintCR. |
202 | 132 |
^ nil |
41 | 133 |
]. |
134 |
||
135 |
[line startsWith:'#'] whileFalse:[ |
|
202 | 136 |
line := aStream nextLine |
41 | 137 |
]. |
138 |
||
139 |
(line startsWith:'#define') ifFalse:[ |
|
357
4bcb93f5892e
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
272
diff
changeset
|
140 |
'XBMReader [warning]: format error (expected #define)' errorPrintCR. |
202 | 141 |
^ nil |
41 | 142 |
]. |
143 |
||
144 |
index := line indexOf:(Character space). |
|
145 |
index := line indexOf:(Character space) startingAt:(index + 1). |
|
146 |
(index == 0) ifTrue:[ |
|
357
4bcb93f5892e
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
272
diff
changeset
|
147 |
'XBMReader [warning]: format error' errorPrintCR. |
202 | 148 |
^ nil |
41 | 149 |
]. |
150 |
((line copyTo:index - 1) endsWith:'width') ifFalse:[ |
|
357
4bcb93f5892e
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
272
diff
changeset
|
151 |
'XBMReader [warning]: format error (expected width)' errorPrintCR. |
202 | 152 |
^ nil |
41 | 153 |
]. |
154 |
line := line copyFrom:(index + 1). |
|
155 |
width := Number readFromString:line. |
|
156 |
||
157 |
line := aStream nextLine. |
|
158 |
index := line indexOf:(Character space). |
|
159 |
index := line indexOf:(Character space) startingAt:(index + 1). |
|
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 |
]. |
164 |
((line copyTo:index - 1) endsWith:'height') ifFalse:[ |
|
357
4bcb93f5892e
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
272
diff
changeset
|
165 |
'XBMReader [warning]: format error (expected height)' errorPrintCR. |
202 | 166 |
^ nil |
41 | 167 |
]. |
168 |
line := line copyFrom:(index + 1). |
|
169 |
height := Number readFromString:line. |
|
170 |
||
171 |
bytesPerRow := width // 8. |
|
172 |
((width \\ 8) ~~ 0) ifTrue:[ |
|
202 | 173 |
bytesPerRow := bytesPerRow + 1 |
41 | 174 |
]. |
175 |
||
176 |
reverseBits := self class reverseBits. |
|
177 |
||
178 |
data := ByteArray new:(bytesPerRow * height). |
|
179 |
dstIndex := 1. |
|
180 |
||
181 |
line := aStream nextLine. |
|
182 |
[line startsWith:'#'] whileTrue:[ |
|
202 | 183 |
line := aStream nextLine. |
41 | 184 |
]. |
185 |
||
53 | 186 |
[line notNil and:[(line startsWith:'static') not]] whileTrue:[ |
202 | 187 |
line := aStream nextLine. |
53 | 188 |
]. |
41 | 189 |
line := aStream nextLine. |
53 | 190 |
|
41 | 191 |
[line notNil] whileTrue:[ |
202 | 192 |
index := 1. |
193 |
[index ~~ 0] whileTrue:[ |
|
194 |
index := line indexOf:$x startingAt:index. |
|
195 |
(index ~~ 0) ifTrue:[ |
|
196 |
index := index + 1. |
|
197 |
hi := (line at:index) digitValue. |
|
198 |
index := index + 1. |
|
199 |
lo := (line at:index) digitValue. |
|
200 |
val := (hi bitShift:4) bitOr:lo. |
|
201 |
data at:dstIndex put:(reverseBits at:(val + 1)). |
|
202 |
dstIndex := dstIndex + 1 |
|
203 |
] |
|
204 |
]. |
|
205 |
line := aStream nextLine |
|
41 | 206 |
]. |
207 |
photometric := #whiteIs0. |
|
208 |
samplesPerPixel := 1. |
|
209 |
bitsPerSample := #(1). |
|
210 |
||
211 |
" |
|
212 |
XBMReader fromFile:'bitmaps/globe1.xbm' |
|
202 | 213 |
" |
214 |
||
357
4bcb93f5892e
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
272
diff
changeset
|
215 |
"Modified: 10.1.1997 / 18:16:34 / cg" |
41 | 216 |
! ! |
217 |
||
3 | 218 |
!XBMReader methodsFor:'writing to file'! |
219 |
||
220 |
save:image onFile:aFileName |
|
202 | 221 |
"save image as XBM file on aFileName. |
222 |
Only depth1 b&w images can be represented in this format." |
|
3 | 223 |
|
33 | 224 |
|reverseBits bits byte |
66 | 225 |
h "{ Class: SmallInteger }" |
33 | 226 |
srcIndex "{ Class: SmallInteger }" |
227 |
rowBytes "{ Class: SmallInteger }" | |
|
3 | 228 |
|
51 | 229 |
(self class canRepresent:image) ifFalse:[ |
202 | 230 |
self error:'can only save depth 1 B&W images'. |
231 |
^ nil. |
|
51 | 232 |
]. |
233 |
||
3 | 234 |
outStream := FileStream newFileNamed:aFileName. |
235 |
outStream isNil ifTrue:[ |
|
357
4bcb93f5892e
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
272
diff
changeset
|
236 |
'XBMReader [error]: create error' errorPrintCR. |
202 | 237 |
^ nil |
3 | 238 |
]. |
239 |
||
240 |
width := image width. |
|
241 |
height := image height. |
|
242 |
photometric := image photometric. |
|
243 |
samplesPerPixel := image samplesPerPixel. |
|
244 |
bitsPerSample := image bitsPerSample. |
|
245 |
colorMap := image colorMap. |
|
246 |
||
247 |
outStream nextPutAll: '#define xbm_width '. |
|
248 |
outStream nextPutAll:(width printString). |
|
249 |
outStream cr. |
|
250 |
outStream nextPutAll: '#define xbm_height '. |
|
251 |
outStream nextPutAll:(height printString). |
|
252 |
outStream cr. |
|
253 |
outStream nextPutAll: 'static char xbm_bits[] = {'; cr. |
|
254 |
||
255 |
reverseBits := self class reverseBits. |
|
256 |
||
257 |
rowBytes := width + 7 // 8. |
|
258 |
data := image bits. |
|
259 |
srcIndex := 1. |
|
260 |
||
66 | 261 |
h := height. |
262 |
h timesRepeat:[ |
|
202 | 263 |
rowBytes timesRepeat:[ |
264 |
outStream nextPutAll: '0x'. |
|
265 |
bits := data at:srcIndex. srcIndex := srcIndex + 1. |
|
266 |
photometric == #blackIs0 ifTrue:[ |
|
267 |
bits := bits bitInvert bitAnd:16rFF |
|
268 |
]. |
|
269 |
byte := (reverseBits at:(bits + 1)). |
|
270 |
byte < 16 ifTrue:[ |
|
271 |
outStream nextPut:$0 |
|
272 |
]. |
|
272
477aeb0d62b6
printOn:radix: -> printOn:base:
Claus Gittinger <cg@exept.de>
parents:
259
diff
changeset
|
273 |
byte printOn:outStream base:16. |
202 | 274 |
outStream nextPutAll: ', '. |
275 |
]. |
|
276 |
outStream cr |
|
3 | 277 |
]. |
278 |
outStream nextPutAll: '};'; cr. |
|
279 |
outStream close |
|
280 |
||
33 | 281 |
" |
282 |
XBMReader save:(Image fromFile:'bitmaps/SBrowser.xbm') onFile:'test.xbm' |
|
283 |
" |
|
284 |
" |
|
285 |
convert sun icon to XBM format: |
|
286 |
||
287 |
XBMReader save:(Image fromFile:'bitmaps/hello_world.icon') onFile:'test.xbm' |
|
288 |
" |
|
202 | 289 |
|
357
4bcb93f5892e
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
272
diff
changeset
|
290 |
"Modified: 10.1.1997 / 18:16:47 / cg" |
3 | 291 |
! ! |
292 |
||
135 | 293 |
!XBMReader class methodsFor:'documentation'! |
294 |
||
295 |
version |
|
398 | 296 |
^ '$Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.29 1997-02-01 14:05:48 cg Exp $' |
135 | 297 |
! ! |
41 | 298 |
XBMReader initialize! |