author | penk |
Thu, 15 Nov 2001 12:09:54 +0100 | |
changeset 1529 | 0c9e38ea3261 |
parent 1521 | 3c54279a418c |
child 1650 | 916067641a59 |
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 |
||
1400 | 13 |
"{ Package: 'stx:libview2' }" |
14 |
||
22 | 15 |
ImageReader subclass:#XPMReader |
203 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
259 | 19 |
category:'Graphics-Images-Support' |
22 | 20 |
! |
21 |
||
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
22 |
!XPMReader class methodsFor:'documentation'! |
22 | 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 |
documentation |
|
39 |
" |
|
40 |
this class provides methods for loading x-pixmap-file (xpm) images. |
|
203 | 41 |
|
24 | 42 |
These images are used (in X) for palette images (see ctwm or hp-vue for a lot |
203 | 43 |
of them). |
44 |
The code here is a hack - it may not work for all images |
|
45 |
(it works for the testfiles I got here). |
|
24 | 46 |
|
203 | 47 |
Limitations: |
48 |
only reads the full-color specification, ignoring monochrome |
|
49 |
and greyscale info. |
|
24 | 50 |
|
203 | 51 |
Can only handle single-character index. |
24 | 52 |
|
203 | 53 |
Only understands single-word color names (i.e. names with spaces |
54 |
are not supported) |
|
55 |
||
459 | 56 |
Image writing is only supported for images with less than about 50 |
57 |
colors (single byte encoding). If present, the mask must be a |
|
58 |
single bit mask (i.e. no alpha channel). |
|
59 |
Due to the algorithm, writing may be slow for big images |
|
22 | 60 |
|
61 |
Suggestions: adapt & use the XPM library here. |
|
203 | 62 |
|
63 |
||
64 |
[See also:] |
|
234 | 65 |
Image Form Icon |
203 | 66 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader |
210 | 67 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
203 | 68 |
XBMReader XWDReader |
22 | 69 |
" |
70 |
! ! |
|
71 |
||
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
72 |
!XPMReader class methodsFor:'initialization'! |
28 | 73 |
|
74 |
initialize |
|
203 | 75 |
"tell Image-class, that a new fileReader is present |
400 | 76 |
for the '.xpm' and '.pm' extensions." |
203 | 77 |
|
647 | 78 |
MIMETypes defineImageType:'image/x-xpixmap' suffix:'xpm' reader:self. |
79 |
MIMETypes defineImageType:nil suffix:'pm' reader:self. |
|
398 | 80 |
|
400 | 81 |
"Modified: 1.2.1997 / 15:10:29 / cg" |
28 | 82 |
! ! |
83 |
||
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
84 |
!XPMReader class methodsFor:'testing'! |
22 | 85 |
|
460 | 86 |
canRepresent:anImage |
87 |
"return true, if anImage can be represented in my file format. |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
88 |
Currently only images with less than 80 colors are supported." |
460 | 89 |
|
90 |
anImage depth <= 6 ifTrue:[^ true]. |
|
91 |
anImage photometric == #palette ifTrue:[ |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
92 |
anImage colorMap size < 80 ifTrue:[^ true]. |
460 | 93 |
]. |
1495 | 94 |
anImage usedColors size < 80 ifTrue:[^ true]. |
1521 | 95 |
Transcript showCR:'XPMReader: too many colors in image.'. |
1495 | 96 |
^ false |
460 | 97 |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
98 |
"Modified: 27.2.1997 / 12:40:22 / cg" |
460 | 99 |
! |
100 |
||
22 | 101 |
isValidImageFile:aFileName |
102 |
"return true, if aFileName contains an x-bitmap-file image" |
|
103 |
||
104 |
|line inStream | |
|
105 |
||
106 |
inStream := self streamReadingFile:aFileName. |
|
107 |
inStream isNil ifTrue:[^ false]. |
|
108 |
||
550 | 109 |
Stream readErrorSignal handle:[:ex | |
110 |
line := nil |
|
111 |
] do:[ |
|
1510
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
112 |
Stream lineTooLongErrorSignal handle:[:ex | |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
113 |
line := nil. |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
114 |
Transcript showCR:'long line'. |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
115 |
ex return. |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
116 |
] do:[ |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
117 |
line := inStream nextLine. |
4e0133eec046
care for invalid format (long line)
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
118 |
] |
550 | 119 |
]. |
22 | 120 |
inStream close. |
121 |
line isNil ifTrue:[^ false]. |
|
122 |
(line startsWith:'/* XPM') ifFalse:[^ false]. |
|
123 |
^ true |
|
124 |
||
125 |
" |
|
562
921472d151d9
handle commented lines (in colorSpec)
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
126 |
XPMReader isValidImageFile:'fooBar' |
921472d151d9
handle commented lines (in colorSpec)
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
127 |
XPMReader isValidImageFile:'bitmaps/xpmBitmaps/device_images/ljet.xpm' |
921472d151d9
handle commented lines (in colorSpec)
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
128 |
XPMReader isValidImageFile:'bitmaps/gifImages/garfield.gif' |
550 | 129 |
" |
130 |
||
562
921472d151d9
handle commented lines (in colorSpec)
Claus Gittinger <cg@exept.de>
parents:
560
diff
changeset
|
131 |
"Modified: 24.4.1997 / 20:29:40 / cg" |
22 | 132 |
! ! |
133 |
||
134 |
!XPMReader methodsFor:'reading from file'! |
|
135 |
||
136 |
colorNameFrom:aStream |
|
137 |
"read either a color-name or value specified in X-notation |
|
138 |
(#rrggbb where rr, gg and bb are 2-digit hex numbers)" |
|
139 |
||
140 |
|s| |
|
141 |
||
142 |
aStream peek == $# ifTrue:[ |
|
28 | 143 |
aStream next. |
144 |
s := '#'. |
|
24 | 145 |
] ifFalse:[ |
28 | 146 |
s := ''. |
22 | 147 |
]. |
28 | 148 |
[aStream peek isLetterOrDigit] whileTrue:[ |
149 |
s := s copyWith:aStream next |
|
24 | 150 |
]. |
151 |
^ s |
|
22 | 152 |
! |
153 |
||
41 | 154 |
fromStream:aStream |
155 |
"read an XPM-image from aStream. Return the receiver |
|
156 |
(with all relevant instance variables set for the image) |
|
157 |
or nil on error" |
|
22 | 158 |
|
159 |
|line |
|
160 |
srcIndex "{ Class: SmallInteger }" |
|
161 |
dstIndex "{ Class: SmallInteger }" |
|
470 | 162 |
colorName monoName greyName grey4Name symbolicName colorMapSize redMap greenMap blueMap |
917 | 163 |
charsPerPixel xlation s bitsPerPixel lineDone maskPixelValue |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
164 |
state key lastKey lastChar1 lastChar2 c1 c2 lastXLation| |
22 | 165 |
|
41 | 166 |
inStream := aStream. |
22 | 167 |
|
41 | 168 |
line := aStream nextLine. |
22 | 169 |
(line notNil and:[line startsWith:'/* XPM']) ifFalse:[ |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
170 |
^ self fileFormatError:'format error (expected XPM)'. |
22 | 171 |
]. |
172 |
||
41 | 173 |
line := aStream nextLine. |
107 | 174 |
[line notNil and:[(line startsWith:'/*') or:[line isBlank]]] whileTrue:[ |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
175 |
line := aStream nextLine. |
22 | 176 |
]. |
177 |
(line notNil and:[line startsWith:'static char']) ifFalse:[ |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
178 |
^ self fileFormatError:'format error (expected static char)'. |
22 | 179 |
]. |
41 | 180 |
line := aStream nextLine. |
107 | 181 |
(line notNil and:[line startsWith:'/*']) ifTrue:[ |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
182 |
[line notNil |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
183 |
and:[(line startsWith:'/*') or:[line startsWith:' *']]] whileTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
184 |
line := aStream nextLine. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
185 |
]. |
22 | 186 |
]. |
471
350583677b5b
allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents:
470
diff
changeset
|
187 |
line notNil ifTrue:[ |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
188 |
line := line withoutSeparators |
471
350583677b5b
allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents:
470
diff
changeset
|
189 |
]. |
22 | 190 |
(line notNil and:[line startsWith:'"']) ifFalse:[ |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
191 |
^ self fileFormatError:'format error (expected "ww hh nn mm)'. |
22 | 192 |
]. |
193 |
s := ReadStream on:line. |
|
194 |
s next. "skip quote" |
|
195 |
width := Integer readFrom:s. |
|
196 |
height := Integer readFrom:s. |
|
197 |
colorMapSize := Integer readFrom:s. |
|
198 |
charsPerPixel := Integer readFrom:s. |
|
199 |
charsPerPixel ~~ 1 ifTrue:[ |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
200 |
xlation := Dictionary new:colorMapSize. |
509
df395b9c70c8
handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents:
471
diff
changeset
|
201 |
] ifFalse:[ |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
202 |
xlation := Array new:256. |
22 | 203 |
]. |
204 |
||
107 | 205 |
redMap := ByteArray new:colorMapSize. |
206 |
greenMap := ByteArray new:colorMapSize. |
|
207 |
blueMap := ByteArray new:colorMapSize. |
|
36 | 208 |
colorMap := Colormap redVector:redMap greenVector:greenMap blueVector:blueMap. |
22 | 209 |
1 to:colorMapSize do:[:colorIndex | |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
210 |
|index line color t word| |
22 | 211 |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
212 |
line := aStream nextLine. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
213 |
[line notNil and:[line startsWith:'/*']] whileTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
214 |
[line notNil and:[(line endsWith:'*/') not]] whileTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
215 |
line := aStream nextLine. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
216 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
217 |
line := aStream nextLine. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
218 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
219 |
line notNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
220 |
line := line withoutSeparators |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
221 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
222 |
(line notNil and:[line startsWith:'"']) ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
223 |
^ self fileFormatError:'format error (expected color spec)'. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
224 |
]. |
24 | 225 |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
226 |
s := ReadStream on:line. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
227 |
s next. "skip quote" |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
228 |
charsPerPixel ~~ 1 ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
229 |
key := s next:charsPerPixel. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
230 |
xlation at:key put:colorIndex - 1. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
231 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
232 |
index := s next asciiValue. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
233 |
xlation at:index put:colorIndex - 1. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
234 |
]. |
22 | 235 |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
236 |
lineDone := false. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
237 |
state := nil. |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
238 |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
239 |
[lineDone] whileFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
240 |
s skipSeparators. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
241 |
s peek == $# ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
242 |
word := self colorNameFrom:s |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
243 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
244 |
word := s nextAlphaNumericWord. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
245 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
246 |
word isNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
247 |
lineDone := true |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
248 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
249 |
word = 's' ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
250 |
"/ symbolic name ... |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
251 |
state := $s. symbolicName := ''. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
252 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
253 |
word = 'm' ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
254 |
"/ monochrome data |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
255 |
state := $m. monoName := ''. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
256 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
257 |
word = 'g' ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
258 |
"/ grey data |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
259 |
state := $g. greyName := ''. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
260 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
261 |
word = 'g4' ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
262 |
"/ grey data |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
263 |
state := $G. grey4Name := ''. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
264 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
265 |
word = 'c' ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
266 |
"/ color data |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
267 |
state := $c. colorName := ''. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
268 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
269 |
"/ append to name |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
270 |
state isNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
271 |
^ self fileFormatError:('format error got: ' |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
272 |
, word printString |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
273 |
, ' (expected ''c'',''m'',''g'' or ''s'')'). |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
274 |
]. |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
275 |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
276 |
state == $m ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
277 |
monoName := monoName , ' ' , word. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
278 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
279 |
state == $g ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
280 |
greyName := greyName , ' ' , word. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
281 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
282 |
state == $G ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
283 |
grey4Name := grey4Name , ' ' , word. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
284 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
285 |
state == $c ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
286 |
colorName := colorName , ' ' , word. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
287 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
288 |
state == $s ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
289 |
symbolicName := symbolicName , ' ' , word. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
290 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
291 |
(word startsWith:'#') ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
292 |
state := nil. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
293 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
294 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
295 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
296 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
297 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
298 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
299 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
300 |
]. |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
301 |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
302 |
colorName notNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
303 |
colorName := colorName withoutSeparators |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
304 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
305 |
monoName notNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
306 |
monoName := monoName withoutSeparators |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
307 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
308 |
greyName notNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
309 |
greyName := greyName withoutSeparators |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
310 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
311 |
grey4Name notNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
312 |
grey4Name := grey4Name withoutSeparators |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
313 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
314 |
symbolicName notNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
315 |
symbolicName := symbolicName withoutSeparators |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
316 |
]. |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
317 |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
318 |
"/ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
319 |
"/ for now - ignore everything, except |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
320 |
"/ colorName (if there is one) |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
321 |
"/ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
322 |
colorName isNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
323 |
colorName := greyName. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
324 |
colorName isNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
325 |
colorName := monoName. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
326 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
327 |
]. |
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
328 |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
329 |
(colorName sameAs: 'none') ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
330 |
color := Color noColor. "/ white |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
331 |
redMap at:colorIndex put:0. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
332 |
greenMap at:colorIndex put:0. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
333 |
blueMap at:colorIndex put:0. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
334 |
maskPixelValue := colorIndex-1. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
335 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
336 |
color := Color name:colorName ifIllegal:Color black. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
337 |
redMap at:colorIndex put:(color red asFloat * 255.0 // 100). |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
338 |
greenMap at:colorIndex put:(color green asFloat * 255.0 // 100). |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
339 |
blueMap at:colorIndex put:(color blue asFloat * 255.0 // 100). |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
340 |
]. |
22 | 341 |
]. |
342 |
||
343 |
"actually, could make it an image with less depth most of the time ..." |
|
344 |
||
345 |
" |
|
346 |
bitsPerPixel := ((colorMapSize - 1) log:2) truncated + 1. |
|
347 |
" |
|
1529 | 348 |
colorMapSize > 256 ifTrue:[ |
349 |
bitsPerPixel := 24. |
|
350 |
data := ByteArray new:(width * height * 3). |
|
351 |
] ifFalse:[ |
|
352 |
bitsPerPixel := 8. |
|
353 |
data := ByteArray new:(width * height). |
|
354 |
]. |
|
22 | 355 |
|
356 |
dstIndex := 1. |
|
357 |
1 to:height do:[:row | |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
358 |
line := aStream nextLine withoutSpaces. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
359 |
[line notNil and:[line startsWith:'/*']] whileTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
360 |
line := aStream nextLine withoutSpaces. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
361 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
362 |
line notNil ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
363 |
line := line withoutSeparators |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
364 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
365 |
(line notNil and:[line startsWith:'"']) ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
366 |
^ self fileFormatError:'format error (expected pixels)'. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
367 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
368 |
charsPerPixel == 1 ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
369 |
srcIndex := 2. "skip dquote" |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
370 |
1 to:width do:[:col | |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
371 |
key := line at:srcIndex. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
372 |
key ~~ lastKey ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
373 |
lastXLation := xlation at:key asciiValue. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
374 |
lastKey := key |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
375 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
376 |
data at:dstIndex put:lastXLation. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
377 |
srcIndex := srcIndex + 1. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
378 |
dstIndex := dstIndex + 1 |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
379 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
380 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
381 |
charsPerPixel == 2 ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
382 |
"/ sorry, but this ugly code does a lot for speed, |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
383 |
"/ when reading big Xpm files (factor=5 for banner8.xpm) ... |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
384 |
srcIndex := 2."skip dquote" |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
385 |
lastChar1 := lastChar2 := nil. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
386 |
key := String new:2. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
387 |
1 to:width do:[:col | |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
388 |
c1 := line at:srcIndex. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
389 |
c2 := line at:srcIndex+1. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
390 |
(c1 ~~ lastChar1 or:[c2 ~~ lastChar2]) ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
391 |
key at:1 put:c1. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
392 |
key at:2 put:c2. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
393 |
lastXLation := xlation at:key. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
394 |
lastChar1 := c1. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
395 |
lastChar2 := c2. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
396 |
]. |
1529 | 397 |
bitsPerPixel == 24 ifTrue:[ |
398 |
data at:dstIndex put:(colorMap at:lastXLation+1) redByte. |
|
399 |
data at:dstIndex+1 put:(colorMap at:lastXLation+1) greenByte. |
|
400 |
data at:dstIndex+2 put:(colorMap at:lastXLation+1) blueByte. |
|
401 |
dstIndex := dstIndex + 3. |
|
402 |
] ifFalse:[ |
|
403 |
data at:dstIndex put:lastXLation. |
|
404 |
dstIndex := dstIndex + 1. |
|
405 |
]. |
|
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
406 |
srcIndex := srcIndex + 2. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
407 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
408 |
] ifFalse:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
409 |
s := line readStream. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
410 |
s next. "/ skip dquote |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
411 |
1 to:width do:[:col | |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
412 |
key := s next:charsPerPixel. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
413 |
"/ data at:dstIndex put:(xlation at:key). |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
414 |
key ~= lastKey ifTrue:[ |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
415 |
lastXLation := xlation at:key. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
416 |
lastKey := key |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
417 |
]. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
418 |
data at:dstIndex put:lastXLation. |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
419 |
dstIndex := dstIndex + 1 |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
420 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
421 |
] |
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
422 |
] |
22 | 423 |
]. |
424 |
||
1529 | 425 |
bitsPerPixel == 24 ifTrue:[ |
426 |
photometric := #rgb. |
|
427 |
samplesPerPixel := 3. |
|
428 |
bitsPerSample := #(8 8 8). |
|
429 |
] ifFalse:[ |
|
430 |
photometric := #palette. |
|
431 |
samplesPerPixel := 1. |
|
432 |
bitsPerSample := Array with:bitsPerPixel. |
|
433 |
]. |
|
22 | 434 |
|
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
435 |
maskPixelValue notNil ifTrue:[ |
1403
94f9ec117aa5
tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents:
1400
diff
changeset
|
436 |
self buildMaskFromColor:maskPixelValue |
316
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
437 |
]. |
d39f3f8f7627
Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents:
259
diff
changeset
|
438 |
|
24 | 439 |
" |
41 | 440 |
XPMReader fromStream:('bitmaps/ljet.xpm' asFilename readStream) |
441 |
XPMReader fromStream:('bitmaps/magtape.xpm' asFilename readStream) |
|
442 |
XPMReader fromStream:('bitmaps/pixmap.xpm' asFilename readStream) |
|
443 |
XPMReader fromStream:('bitmaps/SBrowser.xbm' asFilename readStream) |
|
107 | 444 |
" |
445 |
||
817
dec0b7b06c79
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
693
diff
changeset
|
446 |
"Created: / 24.9.1995 / 06:20:06 / claus" |
dec0b7b06c79
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
693
diff
changeset
|
447 |
"Modified: / 24.9.1995 / 07:07:33 / claus" |
dec0b7b06c79
use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents:
693
diff
changeset
|
448 |
"Modified: / 5.7.1996 / 17:27:59 / stefan" |
1007 | 449 |
"Modified: / 27.7.1998 / 20:01:56 / cg" |
22 | 450 |
! ! |
41 | 451 |
|
1400 | 452 |
!XPMReader methodsFor:'writing to file'! |
459 | 453 |
|
454 |
colorNameOf:aColor |
|
955 | 455 |
"generate a name for a color. If its a standard color, |
456 |
return its name; otherwise return the hex representation." |
|
457 |
||
459 | 458 |
#(white black red green blue |
459 |
yellow magenta cyan orange) do:[:aStandardColorName | |
|
460 |
aColor = (Color name:aStandardColorName) ifTrue:[ |
|
461 |
^ aStandardColorName. |
|
462 |
] |
|
463 |
]. |
|
464 |
^ '#' |
|
465 |
, (aColor redByte hexPrintString:2) |
|
466 |
, (aColor greenByte hexPrintString:2) |
|
467 |
, (aColor blueByte hexPrintString:2) |
|
468 |
||
955 | 469 |
"Created: / 27.2.1997 / 11:48:40 / cg" |
470 |
"Modified: / 6.6.1998 / 20:58:49 / cg" |
|
459 | 471 |
! |
472 |
||
473 |
save:image onFile:aFileName |
|
474 |
"save image as XPM file on aFileName. |
|
475 |
Caveat: currently, only a maximum of roughly 50 colors is handled |
|
476 |
(i.e. very simple images)" |
|
477 |
||
478 |
|usedColors nColorsUsed nColors nChars baseName map maskColorIndex |
|
479 |
isMasked imageMask| |
|
480 |
||
1012
8dd3d70caedb
oops - usedColors returns a set.
Claus Gittinger <cg@exept.de>
parents:
1007
diff
changeset
|
481 |
usedColors := image usedColors asArray. |
459 | 482 |
nColors := nColorsUsed := usedColors size. |
483 |
(imageMask := image mask) notNil ifTrue:[ |
|
484 |
nColors := nColors + 1. |
|
485 |
maskColorIndex := nColors. |
|
486 |
]. |
|
487 |
nColors > 256 ifTrue:[ |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
488 |
^ Image cannotRepresentImageSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
489 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
490 |
errorString:('XPMReader cannot represent this image (too many colors)'). |
459 | 491 |
]. |
492 |
||
493 |
map := ($a to: $z) asOrderedCollection. |
|
494 |
map addAll:($A to: $Z) asOrderedCollection. |
|
495 |
map addAll:($0 to: $9) asOrderedCollection. |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
496 |
map addAll:#($. $, $` $^ $* $: $; $< $> $? $% $# $& $( $) $- $+ $=) asOrderedCollection. |
459 | 497 |
nChars := 1. |
498 |
||
499 |
nColors > map size ifTrue:[ |
|
668
2c741725cc08
allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
500 |
"/ ^ Image cannotRepresentImageSignal |
2c741725cc08
allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
501 |
"/ raiseWith:image |
2c741725cc08
allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
502 |
"/ errorString:('XPMReader cannot represent this image (too many colors)'). |
2c741725cc08
allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
503 |
map := OrderedCollection new. |
693 | 504 |
$a to: $j do:[:c1 | |
668
2c741725cc08
allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
505 |
map addAll:(($a to: $z) collect:[:c2 | c1 asString , c2 asString]). |
2c741725cc08
allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
506 |
]. |
2c741725cc08
allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
507 |
nChars := 2. |
459 | 508 |
]. |
509 |
||
510 |
outStream := FileStream newFileNamed:aFileName. |
|
511 |
outStream isNil ifTrue:[ |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
512 |
^ Image fileCreationErrorSignal |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
513 |
raiseWith:image |
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
514 |
errorString:('file creation error: ' , aFileName asString). |
459 | 515 |
]. |
516 |
||
517 |
baseName := aFileName asFilename baseName asFilename withoutSuffix asString. |
|
518 |
||
519 |
outStream nextPutLine:'/* XPM */'. |
|
520 |
outStream nextPutLine:'static char *' , baseName , '_xpm[] = {'. |
|
521 |
outStream nextPutLine:'/* width height ncolors chars_per_pixel */'. |
|
522 |
outStream nextPutLine:'"' , image width printString , ' ' |
|
523 |
, image height printString , ' ' |
|
524 |
, nColors printString , ' ' |
|
525 |
, nChars printString , '",'. |
|
526 |
||
527 |
||
528 |
outStream nextPutLine:'/* colors */'. |
|
529 |
1 to:nColorsUsed do:[:idx | |
|
530 |
|clr| |
|
531 |
||
532 |
clr := usedColors at:idx. |
|
533 |
outStream nextPutLine:'"' , (map at:idx) asString , |
|
534 |
' ' , 'c ' , (self colorNameOf:clr) , |
|
535 |
'",'. |
|
536 |
]. |
|
537 |
maskColorIndex notNil ifTrue:[ |
|
538 |
outStream nextPutLine:'"' , (map at:maskColorIndex) asString , |
|
539 |
' c none",'. |
|
540 |
]. |
|
541 |
||
542 |
outStream nextPutLine:'/* pixels */'. |
|
543 |
||
544 |
maskColorIndex isNil ifTrue:[ |
|
545 |
isMasked := false |
|
546 |
]. |
|
560 | 547 |
|
459 | 548 |
0 to:image height-1 do:[:y | |
549 |
outStream nextPutAll:'"'. |
|
550 |
0 to:image width-1 do:[:x | |
|
461
bacef118f54a
more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents:
460
diff
changeset
|
551 |
|clr idx| |
459 | 552 |
|
553 |
maskColorIndex notNil ifTrue:[ |
|
560 | 554 |
isMasked := (imageMask pixelAtX:x y:y) == 0 |
459 | 555 |
]. |
556 |
isMasked ifTrue:[ |
|
668
2c741725cc08
allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
557 |
outStream nextPutAll:(map at:maskColorIndex) asString |
459 | 558 |
] ifFalse:[ |
564 | 559 |
clr := image colorAtX:x y:y. |
459 | 560 |
idx := usedColors indexOf:clr. |
668
2c741725cc08
allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
561 |
outStream nextPutAll:(map at:idx) asString |
459 | 562 |
] |
563 |
]. |
|
564 |
outStream nextPutLine:'",'. |
|
565 |
]. |
|
469
7b58a1bf581c
dont forget to close the stream at the end
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
566 |
outStream nextPutLine:'};'. |
7b58a1bf581c
dont forget to close the stream at the end
Claus Gittinger <cg@exept.de>
parents:
461
diff
changeset
|
567 |
outStream close. |
459 | 568 |
|
1012
8dd3d70caedb
oops - usedColors returns a set.
Claus Gittinger <cg@exept.de>
parents:
1007
diff
changeset
|
569 |
"Modified: / 28.7.1998 / 21:52:13 / cg" |
459 | 570 |
! ! |
571 |
||
384
f139a7fc423b
fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents:
316
diff
changeset
|
572 |
!XPMReader class methodsFor:'documentation'! |
203 | 573 |
|
574 |
version |
|
1529 | 575 |
^ '$Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.47 2001-11-15 11:09:54 penk Exp $' |
203 | 576 |
! ! |
41 | 577 |
XPMReader initialize! |