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