author | Claus Gittinger <cg@exept.de> |
Thu, 23 Nov 1995 18:49:05 +0100 | |
changeset 129 | f890eaabc487 |
parent 119 | 0d406b0e6cc9 |
child 203 | a38debd57097 |
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 |
||
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 |
|
119 | 39 |
^ '$Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.13 1995-11-22 14:08:36 cg Exp $' |
22 | 40 |
! |
41 |
||
42 |
documentation |
|
43 |
" |
|
44 |
this class provides methods for loading x-pixmap-file (xpm) images. |
|
24 | 45 |
These images are used (in X) for palette images (see ctwm or hp-vue for a lot |
46 |
of them). The code here is a hack - it may not work for all images (it works |
|
47 |
for the testfiles I got here). |
|
48 |
Limitations: |
|
28 | 49 |
only reads the full-color specification, ignoring monochrome |
50 |
and greyscale info. |
|
24 | 51 |
|
28 | 52 |
Can only handle single-character index. |
24 | 53 |
|
28 | 54 |
Does not (currently) handle none-colors (i.e. for image-masks). |
24 | 55 |
|
107 | 56 |
Only understands single-word color names (i.e. names with spaces |
57 |
are not supported) |
|
58 |
||
28 | 59 |
Save not supported |
22 | 60 |
|
61 |
Suggestions: adapt & use the XPM library here. |
|
62 |
" |
|
63 |
! ! |
|
64 |
||
28 | 65 |
!XPMReader class methodsFor:'initialization'! |
66 |
||
67 |
initialize |
|
68 |
Image fileFormats at:'.xpm' put:self. |
|
69 |
! ! |
|
70 |
||
22 | 71 |
!XPMReader class methodsFor:'testing'! |
72 |
||
73 |
isValidImageFile:aFileName |
|
74 |
"return true, if aFileName contains an x-bitmap-file image" |
|
75 |
||
76 |
|line inStream | |
|
77 |
||
78 |
inStream := self streamReadingFile:aFileName. |
|
79 |
inStream isNil ifTrue:[^ false]. |
|
80 |
||
81 |
line := inStream nextLine. |
|
82 |
inStream close. |
|
83 |
line isNil ifTrue:[^ false]. |
|
84 |
(line startsWith:'/* XPM') ifFalse:[^ false]. |
|
85 |
^ true |
|
86 |
||
87 |
" |
|
88 |
XPMReader isValidImageFile:'bitmaps/magtape.xpm' |
|
89 |
XPMReader isValidImageFile:'bitmaps/ljet.xpm' |
|
90 |
XPMReader isValidImageFile:'bitmaps/garfield.gif' |
|
91 |
" |
|
92 |
! ! |
|
93 |
||
94 |
!XPMReader methodsFor:'reading from file'! |
|
95 |
||
96 |
colorNameFrom:aStream |
|
97 |
"read either a color-name or value specified in X-notation |
|
98 |
(#rrggbb where rr, gg and bb are 2-digit hex numbers)" |
|
99 |
||
100 |
|s| |
|
101 |
||
102 |
aStream peek == $# ifTrue:[ |
|
28 | 103 |
aStream next. |
104 |
s := '#'. |
|
24 | 105 |
] ifFalse:[ |
28 | 106 |
s := ''. |
22 | 107 |
]. |
28 | 108 |
[aStream peek isLetterOrDigit] whileTrue:[ |
109 |
s := s copyWith:aStream next |
|
24 | 110 |
]. |
111 |
^ s |
|
22 | 112 |
! |
113 |
||
41 | 114 |
fromStream:aStream |
115 |
"read an XPM-image from aStream. Return the receiver |
|
116 |
(with all relevant instance variables set for the image) |
|
117 |
or nil on error" |
|
22 | 118 |
|
119 |
|line |
|
120 |
srcIndex "{ Class: SmallInteger }" |
|
121 |
dstIndex "{ Class: SmallInteger }" |
|
122 |
colorName colorMapSize redMap greenMap blueMap |
|
123 |
charsPerPixel xlation s bitsPerPixel char lineDone| |
|
124 |
||
41 | 125 |
inStream := aStream. |
22 | 126 |
|
41 | 127 |
line := aStream nextLine. |
22 | 128 |
(line notNil and:[line startsWith:'/* XPM']) ifFalse:[ |
46 | 129 |
'XPM: format error (expected XPM)' errorPrintNL. |
130 |
^ nil |
|
22 | 131 |
]. |
132 |
||
41 | 133 |
line := aStream nextLine. |
107 | 134 |
[line notNil and:[(line startsWith:'/*') or:[line isBlank]]] whileTrue:[ |
46 | 135 |
line := aStream nextLine. |
22 | 136 |
]. |
137 |
(line notNil and:[line startsWith:'static char']) ifFalse:[ |
|
46 | 138 |
'XPM: format error (expected static char)' errorPrintNL. |
139 |
^ nil |
|
22 | 140 |
]. |
41 | 141 |
line := aStream nextLine. |
107 | 142 |
(line notNil and:[line startsWith:'/*']) ifTrue:[ |
143 |
[line notNil |
|
144 |
and:[(line startsWith:'/*') or:[line startsWith:' *']]] whileTrue:[ |
|
145 |
line := aStream nextLine. |
|
146 |
]. |
|
22 | 147 |
]. |
148 |
(line notNil and:[line startsWith:'"']) ifFalse:[ |
|
46 | 149 |
'XPM: format error (expected "ww hh nn mm)' errorPrintNL. |
150 |
^ nil |
|
22 | 151 |
]. |
152 |
s := ReadStream on:line. |
|
153 |
s next. "skip quote" |
|
154 |
width := Integer readFrom:s. |
|
155 |
height := Integer readFrom:s. |
|
156 |
colorMapSize := Integer readFrom:s. |
|
157 |
charsPerPixel := Integer readFrom:s. |
|
158 |
charsPerPixel ~~ 1 ifTrue:[ |
|
46 | 159 |
'XPM: can only handle single-character xpm-files' errorPrintNL. |
160 |
^ nil |
|
22 | 161 |
]. |
162 |
xlation := Array new:256. |
|
163 |
||
107 | 164 |
redMap := ByteArray new:colorMapSize. |
165 |
greenMap := ByteArray new:colorMapSize. |
|
166 |
blueMap := ByteArray new:colorMapSize. |
|
36 | 167 |
colorMap := Colormap redVector:redMap greenVector:greenMap blueVector:blueMap. |
22 | 168 |
|
169 |
1 to:colorMapSize do:[:colorIndex | |
|
107 | 170 |
|index line color t| |
22 | 171 |
|
46 | 172 |
line := aStream nextLine. |
173 |
[line notNil and:[line startsWith:'/*']] whileTrue:[ |
|
174 |
line := aStream nextLine. |
|
175 |
]. |
|
176 |
(line notNil and:[line startsWith:'"']) ifFalse:[ |
|
177 |
'XPM: format error (expected color spec)' errorPrintNL. |
|
178 |
^ nil |
|
179 |
]. |
|
24 | 180 |
|
46 | 181 |
s := ReadStream on:line. |
182 |
s next. "skip quote" |
|
183 |
index := s next asciiValue. |
|
184 |
xlation at:index put:colorIndex - 1. |
|
22 | 185 |
|
46 | 186 |
lineDone := false. |
187 |
[lineDone] whileFalse:[ |
|
188 |
s skipSeparators. |
|
189 |
char := s peek. |
|
190 |
char == $" ifTrue:[ |
|
191 |
lineDone := true |
|
192 |
] ifFalse:[ |
|
193 |
char == $s ifTrue:[ |
|
194 |
" |
|
195 |
symbolic name |
|
196 |
" |
|
197 |
s next. |
|
198 |
s skipSeparators. |
|
199 |
self colorNameFrom:s. |
|
200 |
s skipSeparators. |
|
201 |
] ifFalse:[ |
|
202 |
char == $m ifTrue:[ |
|
203 |
" |
|
204 |
monochrome data |
|
205 |
" |
|
206 |
s next. |
|
207 |
s skipSeparators. |
|
208 |
self colorNameFrom:s. |
|
209 |
s skipSeparators. |
|
210 |
] ifFalse:[ |
|
211 |
(char == $g) ifTrue:[ |
|
212 |
" |
|
213 |
greyscale data |
|
214 |
" |
|
215 |
s next. |
|
216 |
s peek == 4 ifTrue:[s next]. |
|
217 |
s skipSeparators. |
|
218 |
self colorNameFrom:s. |
|
219 |
s skipSeparators. |
|
220 |
] ifFalse:[ |
|
221 |
(char == $c) ifTrue:[ |
|
222 |
" |
|
223 |
color data |
|
224 |
" |
|
225 |
s next. |
|
226 |
s skipSeparators. |
|
227 |
colorName := self colorNameFrom:s. |
|
228 |
s skipSeparators. |
|
229 |
] ifFalse:[ |
|
230 |
'XPM: format error got: ' errorPrint. |
|
231 |
"/ char errorPrint. '(' errorPrint. char asciiValue printString errorPrint. |
|
232 |
"/ '); ' errorPrint. |
|
233 |
'(expected ''c'',''m'',''g'' or ''s'')' errorPrintNL. |
|
234 |
^ nil |
|
235 |
]. |
|
236 |
] |
|
237 |
] |
|
238 |
] |
|
239 |
]. |
|
240 |
]. |
|
241 |
((colorName = 'none') or:[colorName = 'None']) ifTrue:[ |
|
242 |
"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
|
243 |
color := Color noColor. "/ white |
8a11ebd8ca8b
handle mask color as black (used to be white)
Claus Gittinger <cg@exept.de>
parents:
114
diff
changeset
|
244 |
redMap at:colorIndex put:0. |
8a11ebd8ca8b
handle mask color as black (used to be white)
Claus Gittinger <cg@exept.de>
parents:
114
diff
changeset
|
245 |
greenMap at:colorIndex put:0. |
8a11ebd8ca8b
handle mask color as black (used to be white)
Claus Gittinger <cg@exept.de>
parents:
114
diff
changeset
|
246 |
blueMap at:colorIndex put:0. |
46 | 247 |
] ifFalse:[ |
248 |
color := Color name:colorName. |
|
116
8a11ebd8ca8b
handle mask color as black (used to be white)
Claus Gittinger <cg@exept.de>
parents:
114
diff
changeset
|
249 |
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
|
250 |
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
|
251 |
blueMap at:colorIndex put:(color blue asFloat * 255.0 // 100). |
46 | 252 |
]. |
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) |
|
107 | 293 |
" |
294 |
||
295 |
"Created: 24.9.1995 / 06:20:06 / claus" |
|
296 |
"Modified: 24.9.1995 / 07:07:33 / claus" |
|
22 | 297 |
! ! |
41 | 298 |
|
299 |
XPMReader initialize! |