22
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
28
|
3 |
All Rights Reserved
|
22
|
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:24:00 am'!
|
|
14 |
|
22
|
15 |
ImageReader subclass:#XPMReader
|
28
|
16 |
instanceVariableNames:''
|
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
32
|
19 |
category:'Graphics-Images support'
|
22
|
20 |
!
|
|
21 |
|
|
22 |
!XPMReader class methodsFor:'documentation'!
|
|
23 |
|
|
24 |
copyright
|
|
25 |
"
|
|
26 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
28
|
27 |
All Rights Reserved
|
22
|
28 |
|
|
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 |
!
|
|
37 |
|
|
38 |
version
|
|
39 |
"
|
107
|
40 |
$Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.10 1995-10-25 10:16:29 cg Exp $
|
22
|
41 |
"
|
|
42 |
!
|
|
43 |
|
|
44 |
documentation
|
|
45 |
"
|
|
46 |
this class provides methods for loading x-pixmap-file (xpm) images.
|
24
|
47 |
These images are used (in X) for palette images (see ctwm or hp-vue for a lot
|
|
48 |
of them). The code here is a hack - it may not work for all images (it works
|
|
49 |
for the testfiles I got here).
|
|
50 |
Limitations:
|
28
|
51 |
only reads the full-color specification, ignoring monochrome
|
|
52 |
and greyscale info.
|
24
|
53 |
|
28
|
54 |
Can only handle single-character index.
|
24
|
55 |
|
28
|
56 |
Does not (currently) handle none-colors (i.e. for image-masks).
|
24
|
57 |
|
107
|
58 |
Only understands single-word color names (i.e. names with spaces
|
|
59 |
are not supported)
|
|
60 |
|
28
|
61 |
Save not supported
|
22
|
62 |
|
|
63 |
Suggestions: adapt & use the XPM library here.
|
|
64 |
"
|
|
65 |
! !
|
|
66 |
|
28
|
67 |
!XPMReader class methodsFor:'initialization'!
|
|
68 |
|
|
69 |
initialize
|
|
70 |
Image fileFormats at:'.xpm' put:self.
|
|
71 |
! !
|
|
72 |
|
22
|
73 |
!XPMReader class methodsFor:'testing'!
|
|
74 |
|
|
75 |
isValidImageFile:aFileName
|
|
76 |
"return true, if aFileName contains an x-bitmap-file image"
|
|
77 |
|
|
78 |
|line inStream |
|
|
79 |
|
|
80 |
inStream := self streamReadingFile:aFileName.
|
|
81 |
inStream isNil ifTrue:[^ false].
|
|
82 |
|
|
83 |
line := inStream nextLine.
|
|
84 |
inStream close.
|
|
85 |
line isNil ifTrue:[^ false].
|
|
86 |
(line startsWith:'/* XPM') ifFalse:[^ false].
|
|
87 |
^ true
|
|
88 |
|
|
89 |
"
|
|
90 |
XPMReader isValidImageFile:'bitmaps/magtape.xpm'
|
|
91 |
XPMReader isValidImageFile:'bitmaps/ljet.xpm'
|
|
92 |
XPMReader isValidImageFile:'bitmaps/garfield.gif'
|
|
93 |
"
|
|
94 |
! !
|
|
95 |
|
|
96 |
!XPMReader methodsFor:'reading from file'!
|
|
97 |
|
|
98 |
colorNameFrom:aStream
|
|
99 |
"read either a color-name or value specified in X-notation
|
|
100 |
(#rrggbb where rr, gg and bb are 2-digit hex numbers)"
|
|
101 |
|
|
102 |
|s|
|
|
103 |
|
|
104 |
aStream peek == $# ifTrue:[
|
28
|
105 |
aStream next.
|
|
106 |
s := '#'.
|
24
|
107 |
] ifFalse:[
|
28
|
108 |
s := ''.
|
22
|
109 |
].
|
28
|
110 |
[aStream peek isLetterOrDigit] whileTrue:[
|
|
111 |
s := s copyWith:aStream next
|
24
|
112 |
].
|
|
113 |
^ s
|
22
|
114 |
!
|
|
115 |
|
41
|
116 |
fromStream:aStream
|
|
117 |
"read an XPM-image from aStream. Return the receiver
|
|
118 |
(with all relevant instance variables set for the image)
|
|
119 |
or nil on error"
|
22
|
120 |
|
|
121 |
|line
|
|
122 |
srcIndex "{ Class: SmallInteger }"
|
|
123 |
dstIndex "{ Class: SmallInteger }"
|
|
124 |
colorName colorMapSize redMap greenMap blueMap
|
|
125 |
charsPerPixel xlation s bitsPerPixel char lineDone|
|
|
126 |
|
41
|
127 |
inStream := aStream.
|
22
|
128 |
|
41
|
129 |
line := aStream nextLine.
|
22
|
130 |
(line notNil and:[line startsWith:'/* XPM']) ifFalse:[
|
46
|
131 |
'XPM: format error (expected XPM)' errorPrintNL.
|
|
132 |
^ nil
|
22
|
133 |
].
|
|
134 |
|
41
|
135 |
line := aStream nextLine.
|
107
|
136 |
[line notNil and:[(line startsWith:'/*') or:[line isBlank]]] whileTrue:[
|
46
|
137 |
line := aStream nextLine.
|
22
|
138 |
].
|
|
139 |
(line notNil and:[line startsWith:'static char']) ifFalse:[
|
46
|
140 |
'XPM: format error (expected static char)' errorPrintNL.
|
|
141 |
^ nil
|
22
|
142 |
].
|
41
|
143 |
line := aStream nextLine.
|
107
|
144 |
(line notNil and:[line startsWith:'/*']) ifTrue:[
|
|
145 |
[line notNil
|
|
146 |
and:[(line startsWith:'/*') or:[line startsWith:' *']]] whileTrue:[
|
|
147 |
line := aStream nextLine.
|
|
148 |
].
|
22
|
149 |
].
|
|
150 |
(line notNil and:[line startsWith:'"']) ifFalse:[
|
46
|
151 |
'XPM: format error (expected "ww hh nn mm)' errorPrintNL.
|
|
152 |
^ nil
|
22
|
153 |
].
|
|
154 |
s := ReadStream on:line.
|
|
155 |
s next. "skip quote"
|
|
156 |
width := Integer readFrom:s.
|
|
157 |
height := Integer readFrom:s.
|
|
158 |
colorMapSize := Integer readFrom:s.
|
|
159 |
charsPerPixel := Integer readFrom:s.
|
|
160 |
charsPerPixel ~~ 1 ifTrue:[
|
46
|
161 |
'XPM: can only handle single-character xpm-files' errorPrintNL.
|
|
162 |
^ nil
|
22
|
163 |
].
|
|
164 |
xlation := Array new:256.
|
|
165 |
|
107
|
166 |
redMap := ByteArray new:colorMapSize.
|
|
167 |
greenMap := ByteArray new:colorMapSize.
|
|
168 |
blueMap := ByteArray new:colorMapSize.
|
36
|
169 |
colorMap := Colormap redVector:redMap greenVector:greenMap blueVector:blueMap.
|
22
|
170 |
|
|
171 |
1 to:colorMapSize do:[:colorIndex |
|
107
|
172 |
|index line color t|
|
22
|
173 |
|
46
|
174 |
line := aStream nextLine.
|
|
175 |
[line notNil and:[line startsWith:'/*']] whileTrue:[
|
|
176 |
line := aStream nextLine.
|
|
177 |
].
|
|
178 |
(line notNil and:[line startsWith:'"']) ifFalse:[
|
|
179 |
'XPM: format error (expected color spec)' errorPrintNL.
|
|
180 |
^ nil
|
|
181 |
].
|
24
|
182 |
|
46
|
183 |
s := ReadStream on:line.
|
|
184 |
s next. "skip quote"
|
|
185 |
index := s next asciiValue.
|
|
186 |
xlation at:index put:colorIndex - 1.
|
22
|
187 |
|
46
|
188 |
lineDone := false.
|
|
189 |
[lineDone] whileFalse:[
|
|
190 |
s skipSeparators.
|
|
191 |
char := s peek.
|
|
192 |
char == $" ifTrue:[
|
|
193 |
lineDone := true
|
|
194 |
] ifFalse:[
|
|
195 |
char == $s ifTrue:[
|
|
196 |
"
|
|
197 |
symbolic name
|
|
198 |
"
|
|
199 |
s next.
|
|
200 |
s skipSeparators.
|
|
201 |
self colorNameFrom:s.
|
|
202 |
s skipSeparators.
|
|
203 |
] ifFalse:[
|
|
204 |
char == $m ifTrue:[
|
|
205 |
"
|
|
206 |
monochrome data
|
|
207 |
"
|
|
208 |
s next.
|
|
209 |
s skipSeparators.
|
|
210 |
self colorNameFrom:s.
|
|
211 |
s skipSeparators.
|
|
212 |
] ifFalse:[
|
|
213 |
(char == $g) ifTrue:[
|
|
214 |
"
|
|
215 |
greyscale data
|
|
216 |
"
|
|
217 |
s next.
|
|
218 |
s peek == 4 ifTrue:[s next].
|
|
219 |
s skipSeparators.
|
|
220 |
self colorNameFrom:s.
|
|
221 |
s skipSeparators.
|
|
222 |
] ifFalse:[
|
|
223 |
(char == $c) ifTrue:[
|
|
224 |
"
|
|
225 |
color data
|
|
226 |
"
|
|
227 |
s next.
|
|
228 |
s skipSeparators.
|
|
229 |
colorName := self colorNameFrom:s.
|
|
230 |
s skipSeparators.
|
|
231 |
] ifFalse:[
|
|
232 |
'XPM: format error got: ' errorPrint.
|
|
233 |
"/ char errorPrint. '(' errorPrint. char asciiValue printString errorPrint.
|
|
234 |
"/ '); ' errorPrint.
|
|
235 |
'(expected ''c'',''m'',''g'' or ''s'')' errorPrintNL.
|
|
236 |
^ nil
|
|
237 |
].
|
|
238 |
]
|
|
239 |
]
|
|
240 |
]
|
|
241 |
].
|
|
242 |
].
|
|
243 |
((colorName = 'none') or:[colorName = 'None']) ifTrue:[
|
|
244 |
"mhmh must add mask to Image-instances soon ..."
|
|
245 |
color := Color white
|
|
246 |
] ifFalse:[
|
|
247 |
color := Color name:colorName.
|
|
248 |
].
|
107
|
249 |
redMap at:colorIndex put:(color red asFloat * 255.0 // 100).
|
|
250 |
greenMap at:colorIndex put:(color green asFloat * 255.0 // 100).
|
|
251 |
blueMap at:colorIndex put:(color blue asFloat * 255.0 // 100).
|
22
|
252 |
].
|
|
253 |
|
|
254 |
"actually, could make it an image with less depth most of the time ..."
|
|
255 |
|
|
256 |
"
|
|
257 |
bitsPerPixel := ((colorMapSize - 1) log:2) truncated + 1.
|
|
258 |
"
|
|
259 |
bitsPerPixel := 8.
|
|
260 |
data := ByteArray new:(width * height).
|
|
261 |
|
|
262 |
dstIndex := 1.
|
|
263 |
1 to:height do:[:row |
|
46
|
264 |
line := aStream nextLine withoutSpaces.
|
|
265 |
[line notNil and:[line startsWith:'/*']] whileTrue:[
|
|
266 |
line := aStream nextLine withoutSpaces.
|
|
267 |
].
|
|
268 |
(line notNil and:[line startsWith:'"']) ifFalse:[
|
|
269 |
'XPM: format error (expected pixels)' errorPrintNL.
|
|
270 |
^ nil
|
|
271 |
].
|
|
272 |
srcIndex := 2.
|
|
273 |
1 to: width do:[:col |
|
|
274 |
|char|
|
22
|
275 |
|
46
|
276 |
char := line at:srcIndex.
|
|
277 |
data at:dstIndex put:(xlation at:char asciiValue).
|
|
278 |
srcIndex := srcIndex + 1.
|
|
279 |
dstIndex := dstIndex + 1
|
|
280 |
]
|
22
|
281 |
].
|
|
282 |
|
|
283 |
photometric := #palette.
|
|
284 |
samplesPerPixel := 1.
|
|
285 |
bitsPerSample := Array with:bitsPerPixel.
|
|
286 |
|
24
|
287 |
"
|
41
|
288 |
XPMReader fromStream:('bitmaps/ljet.xpm' asFilename readStream)
|
|
289 |
XPMReader fromStream:('bitmaps/magtape.xpm' asFilename readStream)
|
|
290 |
XPMReader fromStream:('bitmaps/pixmap.xpm' asFilename readStream)
|
|
291 |
XPMReader fromStream:('bitmaps/SBrowser.xbm' asFilename readStream)
|
107
|
292 |
"
|
|
293 |
|
|
294 |
"Created: 24.9.1995 / 06:20:06 / claus"
|
|
295 |
"Modified: 24.9.1995 / 07:07:33 / claus"
|
22
|
296 |
! !
|
41
|
297 |
|
|
298 |
XPMReader initialize!
|