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