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