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