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