author | Claus Gittinger <cg@exept.de> |
Sat, 11 Nov 1995 17:05:49 +0100 | |
changeset 114 | e577a2f332d0 |
parent 99 | a656b0c9dd21 |
child 135 | ff507d9a242b |
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 |
||
41 | 13 |
'From Smalltalk/X, Version:2.10.4 on 18-feb-1995 at 2:23:58 am'! |
14 |
||
0 | 15 |
ImageReader subclass:#XBMReader |
28 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
33 | 19 |
category:'Graphics-Images support' |
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 |
version |
114
e577a2f332d0
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
39 |
^ '$Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.17 1995-11-11 16:05:42 cg Exp $' |
21 | 40 |
! |
41 |
||
42 |
documentation |
|
43 |
" |
|
44 |
this class provides methods for loading and saving x-bitmap-file images. |
|
45 |
These images can (for example) be created using the bitmap editor supplied |
|
24 | 46 |
with X. |
47 |
Only monochrome images can be represented in this format. |
|
48 |
See also: XPMRreader, SunReader, WinIconReader, GIFReader and TIFFReader |
|
21 | 49 |
" |
50 |
! ! |
|
0 | 51 |
|
28 | 52 |
!XBMReader class methodsFor:'initialization'! |
53 |
||
54 |
initialize |
|
55 |
Image fileFormats at:'.xbm' put:self. |
|
56 |
! ! |
|
57 |
||
41 | 58 |
!XBMReader class methodsFor:'testing'! |
59 |
||
60 |
isValidImageFile:aFileName |
|
61 |
"return true, if aFileName contains an x-bitmap-file image" |
|
62 |
||
63 |
|line inStream index1 index2 keyword| |
|
64 |
||
65 |
inStream := self streamReadingFile:aFileName. |
|
66 |
inStream isNil ifTrue:[^ false]. |
|
67 |
||
68 |
line := inStream nextLine. |
|
69 |
line isNil ifTrue:[ |
|
70 |
inStream close. |
|
71 |
^ false |
|
72 |
]. |
|
73 |
[line startsWith:'#'] whileFalse:[ |
|
74 |
line := inStream nextLine. |
|
75 |
line isNil ifTrue:[ |
|
76 |
inStream close. |
|
77 |
^ false |
|
78 |
] |
|
79 |
]. |
|
80 |
index1 := line indexOf:(Character space). |
|
81 |
index2 := line indexOf:(Character space) startingAt:(index1 + 1). |
|
82 |
(index2 == 0) ifTrue:[ |
|
83 |
inStream close. |
|
84 |
^ false |
|
85 |
]. |
|
86 |
keyword := line copyFrom:index1 to:(index2 - 1). |
|
87 |
(keyword endsWith:'_width') ifFalse:[ |
|
88 |
inStream close. |
|
89 |
^ false |
|
90 |
]. |
|
91 |
inStream close. |
|
92 |
^ true |
|
51 | 93 |
! |
94 |
||
95 |
canRepresent:anImage |
|
96 |
"return true, if anImage can be represented in my file format" |
|
97 |
||
98 |
|photometric| |
|
99 |
||
100 |
(anImage depth ~~ 1) ifTrue:[^ false.]. |
|
101 |
(((photometric := anImage photometric) ~~ #blackIs0) and:[photometric ~~ #whiteIs0]) ifTrue:[^ false.]. |
|
102 |
^ true |
|
41 | 103 |
! ! |
104 |
||
105 |
!XBMReader methodsFor:'reading from file'! |
|
106 |
||
107 |
fromStream:aStream |
|
108 |
|line |
|
109 |
index "{ Class: SmallInteger }" |
|
110 |
dstIndex "{ Class: SmallInteger }" |
|
111 |
bytesPerRow |
|
112 |
lo "{ Class: SmallInteger }" |
|
113 |
hi "{ Class: SmallInteger }" |
|
114 |
val "{ Class: SmallInteger }" |
|
115 |
reverseBits| |
|
116 |
||
117 |
inStream := aStream. |
|
118 |
||
119 |
line := aStream nextLine. |
|
120 |
line isNil ifTrue:[ |
|
51 | 121 |
'XBMReader: short file' errorPrintNL. |
122 |
^ nil |
|
41 | 123 |
]. |
124 |
||
125 |
[line startsWith:'#'] whileFalse:[ |
|
51 | 126 |
line := aStream nextLine |
41 | 127 |
]. |
128 |
||
129 |
(line startsWith:'#define') ifFalse:[ |
|
51 | 130 |
'XBMReader: format error (expected #define)' errorPrintNL. |
131 |
^ nil |
|
41 | 132 |
]. |
133 |
||
134 |
index := line indexOf:(Character space). |
|
135 |
index := line indexOf:(Character space) startingAt:(index + 1). |
|
136 |
(index == 0) ifTrue:[ |
|
51 | 137 |
'XBMReader: format error' errorPrintNL. |
138 |
^ nil |
|
41 | 139 |
]. |
140 |
((line copyTo:index - 1) endsWith:'width') ifFalse:[ |
|
51 | 141 |
'XBMReader: format error (expected width)' errorPrintNL. |
142 |
^ nil |
|
41 | 143 |
]. |
144 |
line := line copyFrom:(index + 1). |
|
145 |
width := Number readFromString:line. |
|
146 |
||
147 |
line := aStream nextLine. |
|
148 |
index := line indexOf:(Character space). |
|
149 |
index := line indexOf:(Character space) startingAt:(index + 1). |
|
150 |
(index == 0) ifTrue:[ |
|
51 | 151 |
'XBMReader: format error' errorPrintNL. |
152 |
^ nil |
|
41 | 153 |
]. |
154 |
((line copyTo:index - 1) endsWith:'height') ifFalse:[ |
|
51 | 155 |
'XBMReader: format error (expected height)' errorPrintNL. |
156 |
^ nil |
|
41 | 157 |
]. |
158 |
line := line copyFrom:(index + 1). |
|
159 |
height := Number readFromString:line. |
|
160 |
||
161 |
bytesPerRow := width // 8. |
|
162 |
((width \\ 8) ~~ 0) ifTrue:[ |
|
51 | 163 |
bytesPerRow := bytesPerRow + 1 |
41 | 164 |
]. |
165 |
||
166 |
reverseBits := self class reverseBits. |
|
167 |
||
168 |
data := ByteArray new:(bytesPerRow * height). |
|
169 |
dstIndex := 1. |
|
170 |
||
171 |
line := aStream nextLine. |
|
172 |
[line startsWith:'#'] whileTrue:[ |
|
53 | 173 |
line := aStream nextLine. |
41 | 174 |
]. |
175 |
||
53 | 176 |
[line notNil and:[(line startsWith:'static') not]] whileTrue:[ |
177 |
line := aStream nextLine. |
|
178 |
]. |
|
41 | 179 |
line := aStream nextLine. |
53 | 180 |
|
41 | 181 |
[line notNil] whileTrue:[ |
51 | 182 |
index := 1. |
183 |
[index ~~ 0] whileTrue:[ |
|
184 |
index := line indexOf:$x startingAt:index. |
|
185 |
(index ~~ 0) ifTrue:[ |
|
186 |
index := index + 1. |
|
187 |
hi := (line at:index) digitValue. |
|
188 |
index := index + 1. |
|
189 |
lo := (line at:index) digitValue. |
|
190 |
val := (hi bitShift:4) bitOr:lo. |
|
191 |
data at:dstIndex put:(reverseBits at:(val + 1)). |
|
192 |
dstIndex := dstIndex + 1 |
|
193 |
] |
|
194 |
]. |
|
195 |
line := aStream nextLine |
|
41 | 196 |
]. |
197 |
photometric := #whiteIs0. |
|
198 |
samplesPerPixel := 1. |
|
199 |
bitsPerSample := #(1). |
|
200 |
||
201 |
" |
|
202 |
XBMReader fromFile:'bitmaps/globe1.xbm' |
|
203 |
" |
|
204 |
! ! |
|
205 |
||
3 | 206 |
!XBMReader methodsFor:'writing to file'! |
207 |
||
208 |
save:image onFile:aFileName |
|
209 |
"save image as XBM file on aFileName" |
|
210 |
||
33 | 211 |
|reverseBits bits byte |
66 | 212 |
h "{ Class: SmallInteger }" |
33 | 213 |
srcIndex "{ Class: SmallInteger }" |
214 |
rowBytes "{ Class: SmallInteger }" | |
|
3 | 215 |
|
51 | 216 |
(self class canRepresent:image) ifFalse:[ |
217 |
self error:'can only save depth 1 B&W images'. |
|
218 |
^ nil. |
|
219 |
]. |
|
220 |
||
3 | 221 |
outStream := FileStream newFileNamed:aFileName. |
222 |
outStream isNil ifTrue:[ |
|
80 | 223 |
'XBMReader: create error' errorPrintNL. |
28 | 224 |
^ nil |
3 | 225 |
]. |
226 |
||
227 |
width := image width. |
|
228 |
height := image height. |
|
229 |
photometric := image photometric. |
|
230 |
samplesPerPixel := image samplesPerPixel. |
|
231 |
bitsPerSample := image bitsPerSample. |
|
232 |
colorMap := image colorMap. |
|
233 |
||
234 |
outStream nextPutAll: '#define xbm_width '. |
|
235 |
outStream nextPutAll:(width printString). |
|
236 |
outStream cr. |
|
237 |
outStream nextPutAll: '#define xbm_height '. |
|
238 |
outStream nextPutAll:(height printString). |
|
239 |
outStream cr. |
|
240 |
outStream nextPutAll: 'static char xbm_bits[] = {'; cr. |
|
241 |
||
242 |
reverseBits := self class reverseBits. |
|
243 |
||
244 |
rowBytes := width + 7 // 8. |
|
245 |
data := image bits. |
|
246 |
srcIndex := 1. |
|
247 |
||
66 | 248 |
h := height. |
249 |
h timesRepeat:[ |
|
28 | 250 |
rowBytes timesRepeat:[ |
251 |
outStream nextPutAll: '0x'. |
|
252 |
bits := data at:srcIndex. srcIndex := srcIndex + 1. |
|
51 | 253 |
photometric == #blackIs0 ifTrue:[ |
254 |
bits := bits bitInvert bitAnd:16rFF |
|
255 |
]. |
|
33 | 256 |
byte := (reverseBits at:(bits + 1)). |
257 |
byte < 16 ifTrue:[ |
|
258 |
outStream nextPut:$0 |
|
259 |
]. |
|
260 |
byte printOn:outStream radix:16. |
|
28 | 261 |
outStream nextPutAll: ', '. |
262 |
]. |
|
263 |
outStream cr |
|
3 | 264 |
]. |
265 |
outStream nextPutAll: '};'; cr. |
|
266 |
outStream close |
|
267 |
||
33 | 268 |
" |
269 |
XBMReader save:(Image fromFile:'bitmaps/SBrowser.xbm') onFile:'test.xbm' |
|
270 |
" |
|
271 |
" |
|
272 |
convert sun icon to XBM format: |
|
273 |
||
274 |
XBMReader save:(Image fromFile:'bitmaps/hello_world.icon') onFile:'test.xbm' |
|
275 |
" |
|
3 | 276 |
! ! |
277 |
||
41 | 278 |
XBMReader initialize! |