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