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