XPMReader.st
author Stefan Vogel <sv@exept.de>
Fri, 05 Jul 1996 23:09:05 +0200
changeset 316 d39f3f8f7627
parent 259 62b1bbafd9ba
child 384 f139a7fc423b
permissions -rw-r--r--
Support color 'none' (transparency masks).
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
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
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
a38debd57097 commentary
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    54
        Image writing is not (yet) supported
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    55
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    56
    Suggestions: adapt & use the XPM library here.
203
a38debd57097 commentary
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    57
a38debd57097 commentary
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    58
a38debd57097 commentary
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    59
    [See also:]
234
b6352d13e792 xrefs in documentation
Claus Gittinger <cg@exept.de>
parents: 210
diff changeset
    60
        Image Form Icon
203
a38debd57097 commentary
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    61
        BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader 
210
5405de794686 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 203
diff changeset
    62
        ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader 
203
a38debd57097 commentary
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    63
        XBMReader XWDReader 
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    64
"
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    65
! !
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    66
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
    67
!XPMReader  class methodsFor:'initialization'!
28
8daff0234d2e *** empty log message ***
claus
parents: 24
diff changeset
    68
8daff0234d2e *** empty log message ***
claus
parents: 24
diff changeset
    69
initialize
203
a38debd57097 commentary
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    70
    "tell Image-class, that a new fileReader is present
a38debd57097 commentary
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    71
     for the '.xpm' extension."
a38debd57097 commentary
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
    72
28
8daff0234d2e *** empty log message ***
claus
parents: 24
diff changeset
    73
    Image fileFormats at:'.xpm'  put:self.
8daff0234d2e *** empty log message ***
claus
parents: 24
diff changeset
    74
! !
8daff0234d2e *** empty log message ***
claus
parents: 24
diff changeset
    75
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
    76
!XPMReader  class methodsFor:'testing'!
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    77
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    78
isValidImageFile:aFileName
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    79
    "return true, if aFileName contains an x-bitmap-file image"
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    80
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    81
    |line inStream |
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    82
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    83
    inStream := self streamReadingFile:aFileName.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    84
    inStream isNil ifTrue:[^ false].
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    85
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    86
    line := inStream nextLine.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    87
    inStream close.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    88
    line isNil ifTrue:[^ false].
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    89
    (line startsWith:'/* XPM') ifFalse:[^ false].
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    90
    ^ true
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    91
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    92
    "
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    93
     XPMReader isValidImageFile:'bitmaps/magtape.xpm'    
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    94
     XPMReader isValidImageFile:'bitmaps/ljet.xpm'      
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    95
     XPMReader isValidImageFile:'bitmaps/garfield.gif' 
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    96
    " 
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    97
! !
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    98
24b4aff428c0 Initial revision
claus
parents:
diff changeset
    99
!XPMReader methodsFor:'reading from file'!
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   100
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   101
colorNameFrom:aStream
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   102
    "read either a color-name or value specified in X-notation
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   103
     (#rrggbb where rr, gg and bb are 2-digit hex numbers)"
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   104
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   105
    |s|
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   106
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   107
    aStream peek == $# ifTrue:[
28
8daff0234d2e *** empty log message ***
claus
parents: 24
diff changeset
   108
	aStream next.
8daff0234d2e *** empty log message ***
claus
parents: 24
diff changeset
   109
	s := '#'.
24
6bc436eb4c4a *** empty log message ***
claus
parents: 22
diff changeset
   110
    ] ifFalse:[
28
8daff0234d2e *** empty log message ***
claus
parents: 24
diff changeset
   111
	s := ''.
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   112
    ].
28
8daff0234d2e *** empty log message ***
claus
parents: 24
diff changeset
   113
    [aStream peek isLetterOrDigit] whileTrue:[
8daff0234d2e *** empty log message ***
claus
parents: 24
diff changeset
   114
	s := s copyWith:aStream next
24
6bc436eb4c4a *** empty log message ***
claus
parents: 22
diff changeset
   115
    ].
6bc436eb4c4a *** empty log message ***
claus
parents: 22
diff changeset
   116
    ^ s
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   117
!
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   118
41
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   119
fromStream:aStream
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   120
    "read an XPM-image from aStream. Return the receiver 
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   121
     (with all relevant instance variables set for the image) 
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   122
     or nil on error"
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   123
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   124
    |line 
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   125
     srcIndex "{ Class: SmallInteger }"
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   126
     dstIndex "{ Class: SmallInteger }"
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   127
     colorName colorMapSize redMap greenMap blueMap
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   128
     charsPerPixel xlation s bitsPerPixel char lineDone maskPixelValue|
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   129
41
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   130
    inStream := aStream.
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   131
41
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   132
    line := aStream nextLine.
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   133
    (line notNil and:[line startsWith:'/* XPM']) ifFalse:[
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   134
        'XPM: format error (expected XPM)' errorPrintNL.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   135
        ^ nil
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
    line := aStream nextLine.
107
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
   139
    [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
   140
        line := aStream nextLine.
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   141
    ].
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   142
    (line notNil and:[line startsWith:'static char']) ifFalse:[
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   143
        'XPM: format error (expected static char)' errorPrintNL.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   144
        ^ nil
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   145
    ].
41
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   146
    line := aStream nextLine.
107
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
   147
    (line notNil and:[line startsWith:'/*']) ifTrue:[
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   148
        [line notNil 
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   149
         and:[(line startsWith:'/*') or:[line startsWith:' *']]] whileTrue:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   150
            line := aStream nextLine.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   151
        ].
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   152
    ].
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   153
    (line notNil and:[line startsWith:'"']) ifFalse:[
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   154
        'XPM: format error (expected "ww hh nn mm)' 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
    s := ReadStream on:line.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   158
    s next.  "skip quote"
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   159
    width := Integer readFrom:s.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   160
    height := Integer readFrom:s.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   161
    colorMapSize := Integer readFrom:s.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   162
    charsPerPixel := Integer readFrom:s.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   163
    charsPerPixel ~~ 1 ifTrue:[
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   164
        'XPM: can only handle single-character xpm-files' errorPrintNL.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   165
        ^ nil
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   166
    ].
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   167
    xlation := Array new:256.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   168
107
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
   169
    redMap := ByteArray new:colorMapSize.
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
   170
    greenMap := ByteArray new:colorMapSize.
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
   171
    blueMap := ByteArray new:colorMapSize.
36
3a7ec58dff8e *** empty log message ***
claus
parents: 32
diff changeset
   172
    colorMap := Colormap redVector:redMap greenVector:greenMap blueVector:blueMap.
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   173
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   174
    1 to:colorMapSize do:[:colorIndex |
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   175
        |index line color t|
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   176
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   177
        line := aStream nextLine.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   178
        [line notNil and:[line startsWith:'/*']] whileTrue:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   179
            line := aStream nextLine.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   180
        ].
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   181
        (line notNil and:[line startsWith:'"']) ifFalse:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   182
            'XPM: format error (expected color spec)' errorPrintNL.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   183
            ^ nil
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   184
        ].
24
6bc436eb4c4a *** empty log message ***
claus
parents: 22
diff changeset
   185
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   186
        s := ReadStream on:line.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   187
        s next. "skip quote"
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   188
        index := s next asciiValue.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   189
        xlation at:index put:colorIndex - 1.
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   190
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   191
        lineDone := false.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   192
        [lineDone] whileFalse:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   193
            s skipSeparators.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   194
            char := s peek.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   195
            char == $" ifTrue:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   196
                lineDone := true
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   197
            ] ifFalse:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   198
                char == $s ifTrue:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   199
                    "
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   200
                     symbolic name
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   201
                    "
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   202
                    s next.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   203
                    s skipSeparators.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   204
                    self colorNameFrom:s.  
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   205
                    s skipSeparators.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   206
                ] ifFalse:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   207
                    char == $m ifTrue:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   208
                        "
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   209
                         monochrome data
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   210
                        "
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   211
                        s next.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   212
                        s skipSeparators.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   213
                        self colorNameFrom:s.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   214
                        s skipSeparators.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   215
                    ] ifFalse:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   216
                        (char == $g) ifTrue:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   217
                            "
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   218
                             greyscale data
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   219
                            "
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   220
                            s next.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   221
                            s peek == 4 ifTrue:[s next].
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   222
                            s skipSeparators.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   223
                            self colorNameFrom:s.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   224
                            s skipSeparators.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   225
                        ] ifFalse:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   226
                            (char == $c) ifTrue:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   227
                                "
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   228
                                 color data
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   229
                                "
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   230
                                s next.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   231
                                s skipSeparators.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   232
                                colorName := self colorNameFrom:s.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   233
                                s skipSeparators.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   234
                            ] ifFalse:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   235
                                'XPM: format error got: ' errorPrint.
46
c49b204c2ef0 *** empty log message ***
claus
parents: 41
diff changeset
   236
"/                                char errorPrint. '(' errorPrint. char asciiValue printString errorPrint.
c49b204c2ef0 *** empty log message ***
claus
parents: 41
diff changeset
   237
"/                                '); ' errorPrint.
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   238
                                 '(expected ''c'',''m'',''g'' or ''s'')' errorPrintNL.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   239
                                ^ nil
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   240
                            ].
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   241
                        ]
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   242
                    ]
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   243
                ]
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   244
            ].
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   245
        ].
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   246
        ((colorName = 'none') or:[colorName = 'None']) ifTrue:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   247
            color := Color noColor. "/ white
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   248
            redMap at:colorIndex put:0.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   249
            greenMap at:colorIndex put:0.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   250
            blueMap at:colorIndex put:0.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   251
            maskPixelValue := colorIndex-1.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   252
        ] ifFalse:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   253
            color := Color name:colorName.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   254
            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
   255
            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
   256
            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
   257
        ].
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   258
    ].
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   259
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   260
    "actually, could make it an image with less depth most of the time ..."
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   261
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   262
"
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   263
    bitsPerPixel := ((colorMapSize - 1) log:2) truncated + 1.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   264
"
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   265
    bitsPerPixel := 8.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   266
    data := ByteArray new:(width * height).
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   267
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   268
    dstIndex := 1.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   269
    1 to:height do:[:row |
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   270
        line := aStream nextLine withoutSpaces.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   271
        [line notNil and:[line startsWith:'/*']] whileTrue:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   272
            line := aStream nextLine withoutSpaces.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   273
        ].
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   274
        (line notNil and:[line startsWith:'"']) ifFalse:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   275
            'XPM: format error (expected pixels)' errorPrintNL.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   276
            ^ nil
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   277
        ].
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   278
        srcIndex := 2.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   279
        1 to: width do:[:col |
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   280
            |char|
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   281
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   282
            char := line at:srcIndex.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   283
            data at:dstIndex put:(xlation at:char asciiValue).
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   284
            srcIndex := srcIndex + 1.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   285
            dstIndex := dstIndex + 1
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   286
        ]
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   287
    ].
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   288
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   289
    photometric := #palette.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   290
    samplesPerPixel := 1.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   291
    bitsPerSample := Array with:bitsPerPixel.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   292
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   293
    maskPixelValue notNil ifTrue:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   294
        self buildMaskFromColor:maskPixelValue
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   295
    ].
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   296
24
6bc436eb4c4a *** empty log message ***
claus
parents: 22
diff changeset
   297
    "
41
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   298
     XPMReader fromStream:('bitmaps/ljet.xpm' asFilename readStream)
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   299
     XPMReader fromStream:('bitmaps/magtape.xpm' asFilename readStream)
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   300
     XPMReader fromStream:('bitmaps/pixmap.xpm' asFilename readStream) 
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   301
     XPMReader fromStream:('bitmaps/SBrowser.xbm' asFilename readStream)
107
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
   302
    "
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
   303
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
   304
    "Created: 24.9.1995 / 06:20:06 / claus"
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
   305
    "Modified: 24.9.1995 / 07:07:33 / claus"
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   306
    "Modified: 5.7.1996 / 17:27:59 / stefan"
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   307
! !
41
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   308
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   309
!XPMReader  class methodsFor:'documentation'!
203
a38debd57097 commentary
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   310
a38debd57097 commentary
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   311
version
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   312
    ^ '$Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.18 1996-07-05 21:09:05 stefan Exp $'
203
a38debd57097 commentary
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   313
! !
41
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   314
XPMReader initialize!