XPMReader.st
author Claus Gittinger <cg@exept.de>
Fri, 04 Apr 2003 19:07:54 +0200
changeset 1737 a1ed08195ee7
parent 1710 ad35e6a18e98
child 1743 2f206c9f1319
permissions -rw-r--r--
refactorings
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:''
259
62b1bbafd9ba category change
Claus Gittinger <cg@exept.de>
parents: 234
diff changeset
    19
	category:'Graphics-Images-Support'
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.
36
3a7ec58dff8e *** empty log message ***
claus
parents: 32
diff changeset
   161
    colorMap := Colormap redVector:redMap greenVector:greenMap blueVector:blueMap.
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   162
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   163
    1 to:colorMapSize do:[:colorIndex |
1403
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   164
        |index line color t word|
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   165
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   166
        line := inStream nextLine.
1403
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   167
        [line notNil and:[line startsWith:'/*']] whileTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   168
            [line notNil and:[(line endsWith:'*/') not]] whileTrue:[
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   169
                line := inStream nextLine.
1403
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   170
            ].
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   171
            line := inStream nextLine.
1403
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   172
        ].
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   173
        line notNil ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   174
            line := line withoutSeparators
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   175
        ].
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   176
        (line notNil and:[line startsWith:'"']) ifFalse:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   177
            ^ 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
   178
        ].
24
6bc436eb4c4a *** empty log message ***
claus
parents: 22
diff changeset
   179
1403
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   180
        s := ReadStream on:line.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   181
        s next. "skip quote"
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   182
        charsPerPixel ~~ 1 ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   183
            key := s next:charsPerPixel.
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   184
            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
   185
        ] ifFalse:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   186
            index := s next asciiValue.
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   187
            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
   188
        ].
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   189
1403
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   190
        lineDone := false.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   191
        state := nil.
384
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   192
1403
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   193
        [lineDone] whileFalse:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   194
            s skipSeparators.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   195
            s peek == $# ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   196
                word := self colorNameFrom:s
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   197
            ] ifFalse:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   198
                word := s nextAlphaNumericWord.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   199
            ].
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   200
            word isNil ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   201
                lineDone := true
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   202
            ] ifFalse:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   203
                word = 's' ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   204
                    "/ symbolic name ...
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   205
                    state := $s. symbolicName := ''.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   206
                ] ifFalse:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   207
                    word = 'm' ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   208
                        "/ monochrome data
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   209
                        state := $m. monoName := ''.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   210
                    ] ifFalse:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   211
                        word = 'g' ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   212
                            "/ grey data
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   213
                            state := $g. greyName := ''.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   214
                        ] ifFalse:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   215
                            word = 'g4' ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   216
                                "/ grey data
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   217
                                state := $G. grey4Name := ''.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   218
                            ] ifFalse:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   219
                                word = 'c' ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   220
                                    "/ color data
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   221
                                    state := $c. colorName := ''.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   222
                                ] ifFalse:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   223
                                    "/ append to name
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   224
                                    state isNil ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   225
                                        ^ self fileFormatError:('format error got: ' 
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   226
                                                                , word printString 
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   227
                                                                , ' (expected ''c'',''m'',''g'' or ''s'')').
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   228
                                    ].
384
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   229
1403
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   230
                                    state == $m ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   231
                                        monoName := monoName , ' ' , word.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   232
                                    ].
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   233
                                    state == $g ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   234
                                        greyName := greyName , ' ' , word.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   235
                                    ].
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   236
                                    state == $G ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   237
                                        grey4Name := grey4Name , ' ' , word.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   238
                                    ].
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   239
                                    state == $c ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   240
                                        colorName := colorName , ' ' , word.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   241
                                    ].
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   242
                                    state == $s ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   243
                                        symbolicName := symbolicName , ' ' , word.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   244
                                    ].
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   245
                                    (word startsWith:'#') ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   246
                                        state := nil.
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
            ].
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   254
        ].
384
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   255
1403
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   256
        colorName notNil ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   257
            colorName := colorName withoutSeparators
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   258
        ].
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   259
        monoName notNil ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   260
            monoName := monoName withoutSeparators
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   261
        ].
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   262
        greyName notNil ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   263
            greyName := greyName withoutSeparators
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   264
        ].
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   265
        grey4Name notNil ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   266
            grey4Name := grey4Name withoutSeparators
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   267
        ].
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   268
        symbolicName notNil ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   269
            symbolicName := symbolicName withoutSeparators
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   270
        ].
384
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   271
1403
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   272
        "/
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   273
        "/ for now - ignore everything, except
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   274
        "/ colorName (if there is one)
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   275
        "/
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   276
        colorName isNil ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   277
            colorName := greyName.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   278
            colorName isNil ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   279
                colorName := monoName.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   280
            ]
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   281
        ].
384
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   282
1403
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   283
        (colorName sameAs: 'none') ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   284
            color := Color noColor. "/ white
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   285
            redMap at:colorIndex put:0.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   286
            greenMap at:colorIndex put:0.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   287
            blueMap at:colorIndex put:0.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   288
            maskPixelValue := colorIndex-1.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   289
        ] ifFalse:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   290
            color := Color name:colorName ifIllegal:Color black.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   291
            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
   292
            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
   293
            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
   294
        ].
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   295
    ].
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   296
!
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   297
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   298
readImage
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   299
    "read an XPM-image from my inStream. Return the receiver 
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   300
     (with all relevant instance variables set for the image) 
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   301
     or nil on error"
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   302
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   303
    |line 
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   304
     srcIndex "{ Class: SmallInteger }"
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   305
     dstIndex "{ Class: SmallInteger }"
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   306
     colorMapSize   
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   307
     s bitsPerPixel key lastKey lastChar1 lastChar2 c1 c2 lastXLation|
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   308
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   309
    line := inStream nextLine.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   310
    (line notNil and:[line startsWith:'/* XPM']) ifFalse:[
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   311
        ^ self fileFormatError:'format error (expected XPM)'.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   312
    ].
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   313
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   314
    line := inStream nextLine.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   315
    [line notNil and:[(line startsWith:'/*') or:[line isBlank or:[(line startsWith:' *')]]]] whileTrue:[
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   316
        line := inStream nextLine.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   317
    ].
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   318
    (line notNil and:[line startsWith:'static char']) ifFalse:[
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   319
        ^ self fileFormatError:'format error (expected static char)'.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   320
    ].
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   321
    line := inStream nextLine.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   322
    (line notNil and:[line startsWith:'/*']) ifTrue:[
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   323
        [line notNil 
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   324
         and:[(line startsWith:'/*') or:[line startsWith:' *']]] whileTrue:[
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   325
            line := inStream nextLine.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   326
        ].
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   327
    ].
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   328
    line notNil ifTrue:[
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   329
        line := line withoutSeparators
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   330
    ].
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   331
    (line notNil and:[line startsWith:'"']) ifFalse:[
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   332
        ^ self fileFormatError:'format error (expected "ww hh nn mm)'.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   333
    ].
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   334
    s := ReadStream on:line.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   335
    s next.  "skip quote"
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   336
    width := Integer readFrom:s.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   337
    height := Integer readFrom:s.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   338
    colorMapSize := Integer readFrom:s.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   339
    charsPerPixel := Integer readFrom:s.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   340
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   341
    charsPerPixel ~~ 1 ifTrue:[
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   342
        characterTranslation := Dictionary new:colorMapSize.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   343
    ] ifFalse:[
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   344
        characterTranslation := Array new:256.
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   345
    ].
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   346
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   347
    self readColorMap:colorMapSize.
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   348
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   349
    "actually, could make it an image with less depth most of the time ..."
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   350
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   351
"
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   352
    bitsPerPixel := ((colorMapSize - 1) log:2) truncated + 1.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   353
"
1529
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   354
    colorMapSize > 256 ifTrue:[
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   355
        bitsPerPixel := 24.
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   356
        data := ByteArray new:(width * height * 3).
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   357
    ] ifFalse:[
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   358
        bitsPerPixel := 8.
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   359
        data := ByteArray new:(width * height).
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   360
    ].
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   361
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   362
    dstIndex := 1.
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   363
    1 to:height do:[:row |
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   364
        line := inStream nextLine withoutSpaces.
1403
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   365
        [line notNil and:[line startsWith:'/*']] whileTrue:[
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   366
            line := inStream nextLine withoutSpaces.
1403
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   367
        ].
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   368
        line notNil ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   369
            line := line withoutSeparators
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   370
        ].
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   371
        (line notNil and:[line startsWith:'"']) ifFalse:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   372
            ^ self fileFormatError:'format error (expected pixels)'.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   373
        ].
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   374
        charsPerPixel == 1 ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   375
            srcIndex := 2. "skip dquote"
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   376
            1 to:width do:[:col |
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   377
                key := line at:srcIndex.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   378
                key ~~ lastKey ifTrue:[
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   379
                    lastXLation := characterTranslation at:key asciiValue.
1403
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   380
                    lastKey := key
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   381
                ].
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   382
                data at:dstIndex put:lastXLation.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   383
                srcIndex := srcIndex + 1.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   384
                dstIndex := dstIndex + 1
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   385
            ]
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   386
        ] ifFalse:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   387
            charsPerPixel == 2 ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   388
                "/ 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
   389
                "/ 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
   390
                srcIndex := 2."skip dquote"
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   391
                lastChar1 := lastChar2 := nil.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   392
                key := String new:2.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   393
                1 to:width do:[:col |
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   394
                    c1 := line at:srcIndex.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   395
                    c2 := line at:srcIndex+1.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   396
                    (c1 ~~ lastChar1 or:[c2 ~~ lastChar2]) ifTrue:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   397
                        key at:1 put:c1.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   398
                        key at:2 put:c2.
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   399
                        lastXLation := characterTranslation at:key.
1403
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   400
                        lastChar1 := c1.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   401
                        lastChar2 := c2.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   402
                    ].
1529
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   403
                    bitsPerPixel == 24 ifTrue:[
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   404
                        data at:dstIndex   put:(colorMap at:lastXLation+1) redByte.
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   405
                        data at:dstIndex+1 put:(colorMap at:lastXLation+1) greenByte.
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   406
                        data at:dstIndex+2 put:(colorMap at:lastXLation+1) blueByte.
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   407
                        dstIndex := dstIndex + 3.
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   408
                    ] ifFalse:[
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   409
                        data at:dstIndex put:lastXLation.
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   410
                        dstIndex := dstIndex + 1.
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   411
                    ].
1403
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   412
                    srcIndex := srcIndex + 2.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   413
                ]
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   414
            ] ifFalse:[
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   415
                s := line readStream.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   416
                s next. "/ skip dquote
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   417
                1 to:width do:[:col |
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   418
                    key := s next:charsPerPixel.
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   419
"/                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
   420
                    key ~= lastKey ifTrue:[
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   421
                        lastXLation := characterTranslation at:key.
1403
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   422
                        lastKey := key
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   423
                    ].
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   424
                    data at:dstIndex put:lastXLation.
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   425
                    dstIndex := dstIndex + 1
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   426
                ]
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
        ]
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   429
    ].
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   430
1529
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   431
    bitsPerPixel == 24 ifTrue:[
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   432
        photometric := #rgb.
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   433
        samplesPerPixel := 3.
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   434
        bitsPerSample := #(8 8 8).
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   435
    ] ifFalse:[
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   436
        photometric := #palette.
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   437
        samplesPerPixel := 1.
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   438
        bitsPerSample := Array with:bitsPerPixel.
0c9e38ea3261 xpm bitmaps with more than 256 colors.
penk
parents: 1521
diff changeset
   439
    ].
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   440
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   441
    maskPixelValue notNil ifTrue:[
1403
94f9ec117aa5 tuned image reading with 2-char color encoding
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   442
        self buildMaskFromColor:maskPixelValue
316
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   443
    ].
d39f3f8f7627 Support color 'none' (transparency masks).
Stefan Vogel <sv@exept.de>
parents: 259
diff changeset
   444
24
6bc436eb4c4a *** empty log message ***
claus
parents: 22
diff changeset
   445
    "
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   446
     XPMReader fromStream:('../../goodies/bitmaps/xpmBitmaps/FATAL.xpm' asFilename readStream)
107
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
   447
    "
Claus Gittinger <cg@exept.de>
parents: 99
diff changeset
   448
817
dec0b7b06c79 use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents: 693
diff changeset
   449
    "Created: / 24.9.1995 / 06:20:06 / claus"
dec0b7b06c79 use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents: 693
diff changeset
   450
    "Modified: / 24.9.1995 / 07:07:33 / claus"
dec0b7b06c79 use common fileFormatError reporter
Claus Gittinger <cg@exept.de>
parents: 693
diff changeset
   451
    "Modified: / 5.7.1996 / 17:27:59 / stefan"
1007
0b8d790b8e3b care for bad color names.
Claus Gittinger <cg@exept.de>
parents: 955
diff changeset
   452
    "Modified: / 27.7.1998 / 20:01:56 / cg"
22
24b4aff428c0 Initial revision
claus
parents:
diff changeset
   453
! !
41
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   454
1400
8a7b55b08bf3 category change
Claus Gittinger <cg@exept.de>
parents: 1012
diff changeset
   455
!XPMReader methodsFor:'writing to file'!
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   456
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   457
colorNameOf:aColor
955
0fdf55c5cc7f comment
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   458
    "generate a name for a color. If its a standard color,
0fdf55c5cc7f comment
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   459
     return its name; otherwise return the hex representation."
0fdf55c5cc7f comment
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   460
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   461
    #(white black red green blue
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   462
      yellow magenta cyan orange) do:[:aStandardColorName |
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   463
        aColor = (Color name:aStandardColorName) ifTrue:[
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   464
            ^ aStandardColorName.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   465
        ]
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
     , (aColor redByte hexPrintString:2)
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   469
     , (aColor greenByte hexPrintString:2)
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   470
     , (aColor blueByte hexPrintString:2)
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   471
955
0fdf55c5cc7f comment
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   472
    "Created: / 27.2.1997 / 11:48:40 / cg"
0fdf55c5cc7f comment
Claus Gittinger <cg@exept.de>
parents: 917
diff changeset
   473
    "Modified: / 6.6.1998 / 20:58:49 / cg"
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   474
!
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
save:image onFile:aFileName
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   477
    "save image as XPM file on aFileName.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   478
     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
   479
             (i.e. very simple images)"
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   480
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   481
    |usedColors nColorsUsed nColors nChars baseName map maskColorIndex
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   482
     isMasked imageMask|
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   483
1012
8dd3d70caedb oops - usedColors returns a set.
Claus Gittinger <cg@exept.de>
parents: 1007
diff changeset
   484
    usedColors := image usedColors asArray.
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   485
    nColors := nColorsUsed := usedColors size.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   486
    (imageMask := image mask) notNil ifTrue:[
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   487
        nColors := nColors + 1.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   488
        maskColorIndex := nColors.
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
    nColors > 256 ifTrue:[
461
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
   491
        ^ Image cannotRepresentImageSignal 
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
   492
            raiseWith:image
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
   493
            errorString:('XPMReader cannot represent this image (too many colors)').
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   494
    ].
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
    map := ($a to: $z) asOrderedCollection.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   497
    map addAll:($A to: $Z) asOrderedCollection.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   498
    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
   499
    map addAll:#($. $, $` $^ $* $: $; $< $> $? $% $# $& $( $) $- $+ $=) asOrderedCollection.
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   500
    nChars := 1.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   501
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   502
    nColors > map size ifTrue:[
668
2c741725cc08 allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   503
"/        ^ Image cannotRepresentImageSignal 
2c741725cc08 allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   504
"/            raiseWith:image
2c741725cc08 allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   505
"/            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
   506
        map := OrderedCollection new.
693
e228da8c7ddf Fix compiler warning.
Stefan Vogel <sv@exept.de>
parents: 668
diff changeset
   507
        $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
   508
            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
   509
        ].
2c741725cc08 allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   510
        nChars := 2.
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   511
    ].
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   512
1710
ad35e6a18e98 Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents: 1650
diff changeset
   513
    [
ad35e6a18e98 Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents: 1650
diff changeset
   514
        outStream := aFileName asFilename newReadWriteStream.
ad35e6a18e98 Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents: 1650
diff changeset
   515
    ] 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
   516
        ^ Image fileCreationErrorSignal 
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
   517
            raiseWith:image
bacef118f54a more signals to be raised if anything happens during
Claus Gittinger <cg@exept.de>
parents: 460
diff changeset
   518
            errorString:('file creation error: ' , aFileName asString).
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   519
    ].
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
    baseName := aFileName asFilename baseName asFilename withoutSuffix asString.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   522
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   523
    outStream nextPutLine:'/* XPM */'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   524
    outStream nextPutLine:'static char *' , baseName , '_xpm[] = {'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   525
    outStream nextPutLine:'/* width height ncolors chars_per_pixel */'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   526
    outStream nextPutLine:'"' , image width printString , ' '
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   527
                              , image height printString , ' '
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   528
                              , nColors printString , ' '
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   529
                              , nChars printString , '",'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   530
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   531
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   532
    outStream nextPutLine:'/* colors */'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   533
    1 to:nColorsUsed do:[:idx |
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   534
        |clr|
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   535
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   536
        clr := usedColors at:idx.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   537
        outStream nextPutLine:'"' , (map at:idx) asString ,
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   538
                              ' ' , 'c ' , (self colorNameOf:clr) ,
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   539
                              '",'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   540
    ].
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   541
    maskColorIndex notNil ifTrue:[
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   542
        outStream nextPutLine:'"' , (map at:maskColorIndex) asString ,
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   543
                              ' c none",'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   544
    ].
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   545
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   546
    outStream nextPutLine:'/* pixels */'.
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
    maskColorIndex isNil ifTrue:[
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   549
        isMasked := false
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   550
    ].
560
4cce4f58bbbf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 550
diff changeset
   551
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   552
    0 to:image height-1 do:[:y |
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   553
        outStream nextPutAll:'"'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   554
        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
   555
            |clr idx|
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   556
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   557
            maskColorIndex notNil ifTrue:[
560
4cce4f58bbbf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 550
diff changeset
   558
                isMasked := (imageMask pixelAtX:x y:y) == 0
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   559
            ].
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   560
            isMasked ifTrue:[
668
2c741725cc08 allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   561
                outStream nextPutAll:(map at:maskColorIndex) asString
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   562
            ] ifFalse:[
564
8a31b6fbe9a6 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 562
diff changeset
   563
                clr := image colorAtX:x y:y.
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   564
                idx := usedColors indexOf:clr.
668
2c741725cc08 allow save of images with up-to 256 colors
Claus Gittinger <cg@exept.de>
parents: 647
diff changeset
   565
                outStream nextPutAll:(map at:idx) asString
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   566
            ]
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   567
        ].
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   568
        outStream nextPutLine:'",'.
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   569
    ].
469
7b58a1bf581c dont forget to close the stream at the end
Claus Gittinger <cg@exept.de>
parents: 461
diff changeset
   570
    outStream nextPutLine:'};'.
7b58a1bf581c dont forget to close the stream at the end
Claus Gittinger <cg@exept.de>
parents: 461
diff changeset
   571
    outStream close.
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   572
1012
8dd3d70caedb oops - usedColors returns a set.
Claus Gittinger <cg@exept.de>
parents: 1007
diff changeset
   573
    "Modified: / 28.7.1998 / 21:52:13 / cg"
459
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   574
! !
9cdd02f994a4 added image writing support
Claus Gittinger <cg@exept.de>
parents: 400
diff changeset
   575
384
f139a7fc423b fixed color reading - color names may consist of multiple
Claus Gittinger <cg@exept.de>
parents: 316
diff changeset
   576
!XPMReader class methodsFor:'documentation'!
203
a38debd57097 commentary
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   577
a38debd57097 commentary
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   578
version
1737
a1ed08195ee7 refactorings
Claus Gittinger <cg@exept.de>
parents: 1710
diff changeset
   579
    ^ '$Header: /cvs/stx/stx/libview2/XPMReader.st,v 1.50 2003-04-04 17:07:22 cg Exp $'
203
a38debd57097 commentary
Claus Gittinger <cg@exept.de>
parents: 119
diff changeset
   580
! !
1650
916067641a59 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1529
diff changeset
   581
41
66edc847b9c8 *** empty log message ***
claus
parents: 36
diff changeset
   582
XPMReader initialize!