XPMReader.st
author Claus Gittinger <cg@exept.de>
Fri, 04 Apr 1997 15:02:02 +0200
changeset 509 df395b9c70c8
parent 471 350583677b5b
child 550 254480517864
permissions -rw-r--r--
handle read of multiChar encodings
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.
461
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
    86
     Currently only images with less than 80 colors are supported."
460
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:[
461
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
    90
        anImage colorMap size < 80 ifTrue:[^ true].
460
2c2215c50c28 added #canRepresent:
Claus Gittinger <cg@exept.de>
parents: 459
diff changeset
    91
    ].
461
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
    92
    ^ anImage usedColors size < 80
460
2c2215c50c28 added #canRepresent:
Claus Gittinger <cg@exept.de>
parents: 459
diff changeset
    93
461
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
    94
    "Modified: 27.2.1997 / 12:40:22 / cg"
460
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 }"
470
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   146
     colorName monoName greyName grey4Name symbolicName colorMapSize redMap greenMap blueMap
384
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
509
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   148
     state key|
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
    ].
471
350583677b5b allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
   173
    line notNil ifTrue:[
350583677b5b allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
   174
        line := line withoutSeparators
350583677b5b allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
   175
    ].
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   176
    (line notNil and:[line startsWith:'"']) ifFalse:[
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   177
        'XPM: format error (expected "ww hh nn mm)' errorPrintNL.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   178
        ^ nil
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   179
    ].
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   180
    s := ReadStream on:line.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   181
    s next.  "skip quote"
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   182
    width := Integer readFrom:s.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   183
    height := Integer readFrom:s.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   184
    colorMapSize := Integer readFrom:s.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   185
    charsPerPixel := Integer readFrom:s.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   186
    charsPerPixel ~~ 1 ifTrue:[
509
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   187
        xlation := Dictionary new:colorMapSize.
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   188
    ] ifFalse:[
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   189
        xlation := Array new:256.
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   190
    ].
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   191
107
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
   192
    redMap := ByteArray new:colorMapSize.
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
   193
    greenMap := ByteArray new:colorMapSize.
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
   194
    blueMap := ByteArray new:colorMapSize.
36
3a7ec58dff8e *** empty log message ***
claus
parents: 32
diff changeset
   195
    colorMap := Colormap redVector:redMap greenVector:greenMap blueVector:blueMap.
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   196
    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
   197
        |index line color t word|
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   198
316
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
        [line notNil and:[line startsWith:'/*']] whileTrue:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   201
            line := aStream nextLine.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   202
        ].
471
350583677b5b allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
   203
        line notNil ifTrue:[
350583677b5b allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
   204
            line := line withoutSeparators
350583677b5b allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
   205
        ].
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   206
        (line notNil and:[line startsWith:'"']) ifFalse:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   207
            'XPM: format error (expected color spec)' errorPrintNL.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   208
            ^ nil
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   209
        ].
24
6bc436eb4c4a *** empty log message ***
claus
parents: 22
diff changeset
   210
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   211
        s := ReadStream on:line.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   212
        s next. "skip quote"
509
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   213
        charsPerPixel ~~ 1 ifTrue:[
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   214
            key := s next:charsPerPixel.
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   215
            xlation at:key put:colorIndex - 1.
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   216
        ] ifFalse:[
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   217
            index := s next asciiValue.
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   218
            xlation at:index put:colorIndex - 1.
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   219
        ].
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   220
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   221
        lineDone := false.
384
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   222
        state := nil.
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   223
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   224
        [lineDone] whileFalse:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   225
            s skipSeparators.
384
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   226
            s peek == $# ifTrue:[
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   227
                word := self colorNameFrom:s
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   228
            ] ifFalse:[
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   229
                word := s nextAlphaNumericWord.
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   230
            ].
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   231
            word isNil ifTrue:[
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   232
                lineDone := true
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   233
            ] ifFalse:[
384
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   234
                word = 's' ifTrue:[
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   235
                    "/ symbolic name ...
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   236
                    state := $s. symbolicName := ''.
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   237
                ] ifFalse:[
384
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   238
                    word = 'm' ifTrue:[
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   239
                        "/ monochrome data
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   240
                        state := $m. monoName := ''.
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   241
                    ] ifFalse:[
384
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   242
                        word = 'g' ifTrue:[
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   243
                            "/ grey data
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   244
                            state := $g. greyName := ''.
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   245
                        ] ifFalse:[
470
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   246
                            word = 'g4' ifTrue:[
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   247
                                "/ grey data
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   248
                                state := $G. grey4Name := ''.
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   249
                            ] ifFalse:[
470
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   250
                                word = 'c' ifTrue:[
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   251
                                    "/ color data
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   252
                                    state := $c. colorName := ''.
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   253
                                ] ifFalse:[
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   254
                                    "/ append to name
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   255
                                    state isNil ifTrue:[
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   256
                                        'XPM: format error got: ' errorPrint.
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   257
                                        word errorPrint. 
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   258
                                         ' (expected ''c'',''m'',''g'' or ''s'')' errorPrintNL.
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   259
                                        ^ nil
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   260
                                    ].
384
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   261
470
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   262
                                    state == $m ifTrue:[
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   263
                                        monoName := monoName , ' ' , word.
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   264
                                    ].
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   265
                                    state == $g ifTrue:[
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   266
                                        greyName := greyName , ' ' , word.
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   267
                                    ].
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   268
                                    state == $G ifTrue:[
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   269
                                        grey4Name := grey4Name , ' ' , word.
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   270
                                    ].
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   271
                                    state == $c ifTrue:[
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   272
                                        colorName := colorName , ' ' , word.
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   273
                                    ].
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   274
                                    state == $s ifTrue:[
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   275
                                        symbolicName := symbolicName , ' ' , word.
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   276
                                    ].
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   277
                                    (word startsWith:'#') ifTrue:[
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   278
                                        state := nil.
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   279
                                    ]
384
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   280
                                ]
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   281
                            ]
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   282
                        ]
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   283
                    ]
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   284
                ]
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   285
            ].
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   286
        ].
384
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   287
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   288
        colorName notNil ifTrue:[
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   289
            colorName := colorName withoutSeparators
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   290
        ].
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   291
        monoName notNil ifTrue:[
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   292
            monoName := monoName withoutSeparators
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   293
        ].
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   294
        greyName notNil ifTrue:[
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   295
            greyName := greyName withoutSeparators
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   296
        ].
470
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   297
        grey4Name notNil ifTrue:[
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   298
            grey4Name := grey4Name withoutSeparators
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   299
        ].
384
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   300
        symbolicName notNil ifTrue:[
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   301
            symbolicName := symbolicName withoutSeparators
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   302
        ].
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   303
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   304
        "/
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   305
        "/ for now - ignore everything, except
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   306
        "/ colorName (if there is one)
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   307
        "/
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   308
        colorName isNil ifTrue:[
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   309
            colorName := greyName.
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   310
            colorName isNil ifTrue:[
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   311
                colorName := monoName.
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   312
            ]
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   313
        ].
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   314
470
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   315
        ((colorName = 'none') 
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   316
        or:[colorName = 'None'
4709c3f524cd checkin from browser
Claus Gittinger <cg@exept.de>
parents: 469
diff changeset
   317
        or:[colorName = 'NONE']]) ifTrue:[
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   318
            color := Color noColor. "/ white
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   319
            redMap at:colorIndex put:0.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   320
            greenMap at:colorIndex put:0.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   321
            blueMap at:colorIndex put:0.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   322
            maskPixelValue := colorIndex-1.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   323
        ] ifFalse:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   324
            color := Color name:colorName.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   325
            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
   326
            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
   327
            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
   328
        ].
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   329
    ].
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   330
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   331
    "actually, could make it an image with less depth most of the time ..."
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   332
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   333
"
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   334
    bitsPerPixel := ((colorMapSize - 1) log:2) truncated + 1.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   335
"
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   336
    bitsPerPixel := 8.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   337
    data := ByteArray new:(width * height).
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   338
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   339
    dstIndex := 1.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   340
    1 to:height do:[:row |
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   341
        line := aStream nextLine withoutSpaces.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   342
        [line notNil and:[line startsWith:'/*']] whileTrue:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   343
            line := aStream nextLine withoutSpaces.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   344
        ].
471
350583677b5b allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
   345
        line notNil ifTrue:[
350583677b5b allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
   346
            line := line withoutSeparators
350583677b5b allow lines to begin with spaces
Claus Gittinger <cg@exept.de>
parents: 470
diff changeset
   347
        ].
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   348
        (line notNil and:[line startsWith:'"']) ifFalse:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   349
            'XPM: format error (expected pixels)' errorPrintNL.
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   350
            ^ nil
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   351
        ].
509
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   352
        charsPerPixel ~~ 1 ifTrue:[
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   353
            s := line readStream.
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   354
            s next. "/ skip dquote
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   355
            1 to: width do:[:col |
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   356
                |key|
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   357
509
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   358
                key := s next:charsPerPixel.
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   359
                data at:dstIndex put:(xlation at:key).
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   360
                dstIndex := dstIndex + 1
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   361
            ]
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   362
        ] ifFalse:[
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   363
            srcIndex := 2.
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   364
            1 to: width do:[:col |
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   365
                |char|
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   366
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   367
                char := line at:srcIndex.
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   368
                data at:dstIndex put:(xlation at:char asciiValue).
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   369
                srcIndex := srcIndex + 1.
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   370
                dstIndex := dstIndex + 1
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   371
            ]
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   372
        ]
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   373
    ].
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   374
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   375
    photometric := #palette.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   376
    samplesPerPixel := 1.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   377
    bitsPerSample := Array with:bitsPerPixel.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   378
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   379
    maskPixelValue notNil ifTrue:[
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   380
        self buildMaskFromColor:maskPixelValue
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   381
    ].
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   382
24
6bc436eb4c4a *** empty log message ***
claus
parents: 22
diff changeset
   383
    "
41
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   384
     XPMReader fromStream:('bitmaps/ljet.xpm' asFilename readStream)
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   385
     XPMReader fromStream:('bitmaps/magtape.xpm' asFilename readStream)
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   386
     XPMReader fromStream:('bitmaps/pixmap.xpm' asFilename readStream) 
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   387
     XPMReader fromStream:('bitmaps/SBrowser.xbm' asFilename readStream)
107
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
   388
    "
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
   389
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
   390
    "Created: 24.9.1995 / 06:20:06 / claus"
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
   391
    "Modified: 24.9.1995 / 07:07:33 / claus"
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   392
    "Modified: 5.7.1996 / 17:27:59 / stefan"
509
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   393
    "Modified: 4.4.1997 / 15:01:28 / cg"
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   394
! !
41
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   395
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   396
!XPMReader methodsFor:'writing to a file'!
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   397
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   398
colorNameOf:aColor
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   399
    #(white black red green blue
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   400
      yellow magenta cyan orange) do:[:aStandardColorName |
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   401
        aColor = (Color name:aStandardColorName) ifTrue:[
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   402
            ^ aStandardColorName.
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
    ^ '#' 
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   406
     , (aColor redByte hexPrintString:2)
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   407
     , (aColor greenByte hexPrintString:2)
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   408
     , (aColor blueByte hexPrintString:2)
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
    "Created: 27.2.1997 / 11:48:40 / cg"
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   411
!
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
save:image onFile:aFileName
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   414
    "save image as XPM file on aFileName.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   415
     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
   416
             (i.e. very simple images)"
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   417
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   418
    |usedColors nColorsUsed nColors nChars baseName map maskColorIndex
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   419
     isMasked imageMask|
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
    usedColors := image usedColors.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   422
    nColors := nColorsUsed := usedColors size.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   423
    (imageMask := image mask) notNil ifTrue:[
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   424
        nColors := nColors + 1.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   425
        maskColorIndex := nColors.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   426
    ].
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   427
    nColors > 256 ifTrue:[
461
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
   428
        ^ Image cannotRepresentImageSignal 
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
   429
            raiseWith:image
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
   430
            errorString:('XPMReader cannot represent this image (too many colors)').
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   431
    ].
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   432
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   433
    map := ($a to: $z) asOrderedCollection.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   434
    map addAll:($A to: $Z) asOrderedCollection.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   435
    map addAll:($0 to: $9) asOrderedCollection.
461
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
   436
    map addAll:#($. $, $` $^ $* $: $; $< $> $? $% $# $& $( $) $- $+ $=) asOrderedCollection.
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   437
    nChars := 1.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   438
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   439
    nColors > map size ifTrue:[
461
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
   440
        ^ Image cannotRepresentImageSignal 
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
   441
            raiseWith:image
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
   442
            errorString:('XPMReader cannot represent this image (too many colors)').
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   443
    ].
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   444
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   445
    outStream := FileStream newFileNamed:aFileName.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   446
    outStream isNil ifTrue:[
461
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
   447
        ^ Image fileCreationErrorSignal 
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
   448
            raiseWith:image
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
   449
            errorString:('file creation error: ' , aFileName asString).
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   450
    ].
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   451
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   452
    baseName := aFileName asFilename baseName asFilename withoutSuffix asString.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   453
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   454
    outStream nextPutLine:'/* XPM */'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   455
    outStream nextPutLine:'static char *' , baseName , '_xpm[] = {'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   456
    outStream nextPutLine:'/* width height ncolors chars_per_pixel */'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   457
    outStream nextPutLine:'"' , image width printString , ' '
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   458
                              , image height printString , ' '
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   459
                              , nColors printString , ' '
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   460
                              , nChars printString , '",'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   461
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
    outStream nextPutLine:'/* colors */'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   464
    1 to:nColorsUsed do:[:idx |
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   465
        |clr|
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   466
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   467
        clr := usedColors at:idx.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   468
        outStream nextPutLine:'"' , (map at:idx) asString ,
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   469
                              ' ' , 'c ' , (self colorNameOf:clr) ,
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   470
                              '",'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   471
    ].
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   472
    maskColorIndex notNil ifTrue:[
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   473
        outStream nextPutLine:'"' , (map at:maskColorIndex) asString ,
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   474
                              ' c none",'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   475
    ].
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   476
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   477
    outStream nextPutLine:'/* pixels */'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   478
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   479
    maskColorIndex isNil ifTrue:[
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   480
        isMasked := false
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   481
    ].
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   482
    0 to:image height-1 do:[:y |
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   483
        outStream nextPutAll:'"'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   484
        0 to:image width-1 do:[:x |
461
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
   485
            |clr idx|
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   486
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   487
            maskColorIndex notNil ifTrue:[
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   488
                isMasked := (imageMask valueAtX:x y:y) == 0
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   489
            ].
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   490
            isMasked ifTrue:[
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   491
                outStream nextPut:(map at:maskColorIndex)
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   492
            ] ifFalse:[
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   493
                clr := image atX:x y:y.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   494
                idx := usedColors indexOf:clr.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   495
                outStream nextPut:(map at:idx)
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   496
            ]
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   497
        ].
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   498
        outStream nextPutLine:'",'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   499
    ].
469
7b58a1bf581c dont forget to close the stream at the end
Claus Gittinger <cg@exept.de>
parents: 461
diff changeset
   500
    outStream nextPutLine:'};'.
7b58a1bf581c dont forget to close the stream at the end
Claus Gittinger <cg@exept.de>
parents: 461
diff changeset
   501
    outStream close.
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   502
469
7b58a1bf581c dont forget to close the stream at the end
Claus Gittinger <cg@exept.de>
parents: 461
diff changeset
   503
    "Modified: 1.3.1997 / 21:34:15 / cg"
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   504
! !
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   505
384
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   506
!XPMReader class methodsFor:'documentation'!
203
a38debd57097 commentary
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   507
a38debd57097 commentary
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   508
version
509
df395b9c70c8 handle read of multiChar encodings
Claus Gittinger <cg@exept.de>
parents: 471
diff changeset
   509
    ^ '$Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.28 1997-04-04 13:02:02 cg Exp $'
203
a38debd57097 commentary
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   510
! !
41
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   511
XPMReader initialize!