XBMReader.st
changeset 28 8daff0234d2e
parent 24 6bc436eb4c4a
child 33 be90784ee668
equal deleted inserted replaced
27:93da277c5ddd 28:8daff0234d2e
     1 "
     1 "
     2  COPYRIGHT (c) 1992 by Claus Gittinger
     2  COPYRIGHT (c) 1992 by Claus Gittinger
     3               All Rights Reserved
     3 	      All Rights Reserved
     4 
     4 
     5  This software is furnished under a license and may be used
     5  This software is furnished under a license and may be used
     6  only in accordance with the terms of that license and with the
     6  only in accordance with the terms of that license and with the
     7  inclusion of the above copyright notice.   This software may not
     7  inclusion of the above copyright notice.   This software may not
     8  be provided or otherwise made available to, or used by, any
     8  be provided or otherwise made available to, or used by, any
     9  other person.  No title to or ownership of the software is
     9  other person.  No title to or ownership of the software is
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
    12 
    12 
    13 ImageReader subclass:#XBMReader
    13 ImageReader subclass:#XBMReader
    14          instanceVariableNames:''
    14 	 instanceVariableNames:''
    15          classVariableNames:''
    15 	 classVariableNames:''
    16          poolDictionaries:''
    16 	 poolDictionaries:''
    17          category:'Graphics-Support'
    17 	 category:'Graphics-Support'
    18 !
    18 !
    19 
    19 
    20 XBMReader comment:'
    20 XBMReader comment:'
    21 COPYRIGHT (c) 1992 by Claus Gittinger
    21 COPYRIGHT (c) 1992 by Claus Gittinger
    22               All Rights Reserved
    22 	      All Rights Reserved
    23 
    23 
    24 $Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.7 1994-08-05 01:16:28 claus Exp $
    24 $Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.8 1994-10-10 02:34:18 claus Exp $
    25 '!
    25 '!
    26 
    26 
    27 !XBMReader class methodsFor:'documentation'!
    27 !XBMReader class methodsFor:'documentation'!
    28 
    28 
    29 copyright
    29 copyright
    30 "
    30 "
    31  COPYRIGHT (c) 1992 by Claus Gittinger
    31  COPYRIGHT (c) 1992 by Claus Gittinger
    32               All Rights Reserved
    32 	      All Rights Reserved
    33 
    33 
    34  This software is furnished under a license and may be used
    34  This software is furnished under a license and may be used
    35  only in accordance with the terms of that license and with the
    35  only in accordance with the terms of that license and with the
    36  inclusion of the above copyright notice.   This software may not
    36  inclusion of the above copyright notice.   This software may not
    37  be provided or otherwise made available to, or used by, any
    37  be provided or otherwise made available to, or used by, any
    40 "
    40 "
    41 !
    41 !
    42 
    42 
    43 version
    43 version
    44 "
    44 "
    45 $Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.7 1994-08-05 01:16:28 claus Exp $
    45 $Header: /cvs/stx/stx/libview2/XBMReader.st,v 1.8 1994-10-10 02:34:18 claus Exp $
    46 "
    46 "
    47 !
    47 !
    48 
    48 
    49 documentation
    49 documentation
    50 "
    50 "
    54     Only monochrome images can be represented in this format.
    54     Only monochrome images can be represented in this format.
    55     See also: XPMRreader, SunReader, WinIconReader, GIFReader and TIFFReader
    55     See also: XPMRreader, SunReader, WinIconReader, GIFReader and TIFFReader
    56 "
    56 "
    57 ! !
    57 ! !
    58 
    58 
       
    59 !XBMReader class methodsFor:'initialization'!
       
    60 
       
    61 initialize
       
    62     Image fileFormats at:'.xbm'  put:self.
       
    63 ! !
       
    64 
    59 !XBMReader methodsFor:'writing to file'!
    65 !XBMReader methodsFor:'writing to file'!
    60 
    66 
    61 save:image onFile:aFileName
    67 save:image onFile:aFileName
    62     "save image as XBM file on aFileName"
    68     "save image as XBM file on aFileName"
    63 
    69 
    64     |reverseBits bits srcIndex rowBytes|
    70     |reverseBits bits srcIndex rowBytes|
    65 
    71 
    66     outStream := FileStream newFileNamed:aFileName.
    72     outStream := FileStream newFileNamed:aFileName.
    67     outStream isNil ifTrue:[
    73     outStream isNil ifTrue:[
    68         'create error' errorPrintNL. 
    74 	'create error' errorPrintNL. 
    69         ^ nil
    75 	^ nil
    70     ].
    76     ].
    71 
    77 
    72     width := image width.
    78     width := image width.
    73     height := image height.
    79     height := image height.
    74     photometric := image photometric.
    80     photometric := image photometric.
    77     colorMap := image colorMap.
    83     colorMap := image colorMap.
    78 
    84 
    79     ((samplesPerPixel ~~ 1)
    85     ((samplesPerPixel ~~ 1)
    80     or:[((bitsPerSample at:1) ~~ 1)
    86     or:[((bitsPerSample at:1) ~~ 1)
    81     or:[(photometric ~~ #blackIs0) and:[photometric ~~ #whiteIs0]]]) ifTrue:[
    87     or:[(photometric ~~ #blackIs0) and:[photometric ~~ #whiteIs0]]]) ifTrue:[
    82         self error:'can only save Depth1Images'.
    88 	self error:'can only save Depth1Images'.
    83         outStream close.
    89 	outStream close.
    84         ^ nil.
    90 	^ nil.
    85     ].
    91     ].
    86 
    92 
    87     outStream nextPutAll: '#define xbm_width '.
    93     outStream nextPutAll: '#define xbm_width '.
    88     outStream nextPutAll:(width printString).
    94     outStream nextPutAll:(width printString).
    89     outStream cr.
    95     outStream cr.
    97     rowBytes := width + 7 // 8.
   103     rowBytes := width + 7 // 8.
    98     data := image bits.
   104     data := image bits.
    99     srcIndex := 1.
   105     srcIndex := 1.
   100 
   106 
   101     height timesRepeat:[
   107     height timesRepeat:[
   102         rowBytes timesRepeat:[
   108 	rowBytes timesRepeat:[
   103             outStream nextPutAll: '0x'.
   109 	    outStream nextPutAll: '0x'.
   104             bits := data at:srcIndex. srcIndex := srcIndex + 1.
   110 	    bits := data at:srcIndex. srcIndex := srcIndex + 1.
   105             (reverseBits at:(bits + 1)) printOn:outStream radix:16.
   111 	    (reverseBits at:(bits + 1)) printOn:outStream radix:16.
   106             outStream nextPutAll: ', '.
   112 	    outStream nextPutAll: ', '.
   107         ].
   113 	].
   108         outStream cr
   114 	outStream cr
   109     ].
   115     ].
   110     outStream nextPutAll: '};'; cr.
   116     outStream nextPutAll: '};'; cr.
   111     outStream close
   117     outStream close
   112 
   118 
   113     "XBMReader save:(Image fromFile:'bitmaps/SBrowser.xbm') onFile:'test.xbm'"
   119     "XBMReader save:(Image fromFile:'bitmaps/SBrowser.xbm') onFile:'test.xbm'"
   123     inStream := self streamReadingFile:aFileName.
   129     inStream := self streamReadingFile:aFileName.
   124     inStream isNil ifTrue:[^ false].
   130     inStream isNil ifTrue:[^ false].
   125 
   131 
   126     line := inStream nextLine.
   132     line := inStream nextLine.
   127     line isNil ifTrue:[
   133     line isNil ifTrue:[
   128         inStream close.
   134 	inStream close.
   129         ^ false
   135 	^ false
   130     ].
   136     ].
   131     [line startsWith:'#'] whileFalse:[
   137     [line startsWith:'#'] whileFalse:[
   132         line := inStream nextLine.
   138 	line := inStream nextLine.
   133         line isNil ifTrue:[
   139 	line isNil ifTrue:[
   134             inStream close.
   140 	    inStream close.
   135             ^ false
   141 	    ^ false
   136         ]
   142 	]
   137     ].
   143     ].
   138     index1 := line indexOf:(Character space).
   144     index1 := line indexOf:(Character space).
   139     index2 := line indexOf:(Character space) startingAt:(index1 + 1).
   145     index2 := line indexOf:(Character space) startingAt:(index1 + 1).
   140     (index2 == 0) ifTrue:[
   146     (index2 == 0) ifTrue:[
   141         inStream close.
   147 	inStream close.
   142         ^ false
   148 	^ false
   143     ].
   149     ].
   144     keyword := line copyFrom:index1 to:(index2 - 1).
   150     keyword := line copyFrom:index1 to:(index2 - 1).
   145     (keyword endsWith:'_width') ifFalse:[
   151     (keyword endsWith:'_width') ifFalse:[
   146         ^ false
   152 	^ false
   147     ].
   153     ].
   148     inStream close.
   154     inStream close.
   149     ^ true
   155     ^ true
   150 ! !
   156 ! !
   151 
   157 
   164     inStream := self class streamReadingFile:aFileName.
   170     inStream := self class streamReadingFile:aFileName.
   165     inStream isNil ifTrue:[^ nil].
   171     inStream isNil ifTrue:[^ nil].
   166 
   172 
   167     line := inStream nextLine.
   173     line := inStream nextLine.
   168     line isNil ifTrue:[
   174     line isNil ifTrue:[
   169         inStream close.
   175 	inStream close.
   170         ^ nil
   176 	^ nil
   171     ].
   177     ].
   172 
   178 
   173     [line startsWith:'#'] whileFalse:[
   179     [line startsWith:'#'] whileFalse:[
   174         line := inStream nextLine
   180 	line := inStream nextLine
   175     ].
   181     ].
   176 
   182 
   177     (line startsWith:'#define') ifFalse:[
   183     (line startsWith:'#define') ifFalse:[
   178         'format error (expected #define)' errorPrintNL.
   184 	'format error (expected #define)' errorPrintNL.
   179         inStream close.
   185 	inStream close.
   180         ^ nil
   186 	^ nil
   181     ].
   187     ].
   182 
   188 
   183     index := line indexOf:(Character space).
   189     index := line indexOf:(Character space).
   184     index := line indexOf:(Character space) startingAt:(index + 1).
   190     index := line indexOf:(Character space) startingAt:(index + 1).
   185     (index == 0) ifTrue:[
   191     (index == 0) ifTrue:[
   186         'format error' errorPrintNL.
   192 	'format error' errorPrintNL.
   187         inStream close.
   193 	inStream close.
   188         ^ nil
   194 	^ nil
   189     ].
   195     ].
   190     ((line copyTo:index - 1) endsWith:'width') ifFalse:[
   196     ((line copyTo:index - 1) endsWith:'width') ifFalse:[
   191         'format error (expected width)' errorPrintNL.
   197 	'format error (expected width)' errorPrintNL.
   192         inStream close.
   198 	inStream close.
   193         ^ nil
   199 	^ nil
   194     ].
   200     ].
   195     line := line copyFrom:(index + 1).
   201     line := line copyFrom:(index + 1).
   196     width := Number readFromString:line.
   202     width := Number readFromString:line.
   197 
   203 
   198     line := inStream nextLine.
   204     line := inStream nextLine.
   199     index := line indexOf:(Character space).
   205     index := line indexOf:(Character space).
   200     index := line indexOf:(Character space) startingAt:(index + 1).
   206     index := line indexOf:(Character space) startingAt:(index + 1).
   201     (index == 0) ifTrue:[
   207     (index == 0) ifTrue:[
   202         'format error' errorPrintNL.
   208 	'format error' errorPrintNL.
   203         inStream close.
   209 	inStream close.
   204         ^ nil
   210 	^ nil
   205     ].
   211     ].
   206     ((line copyTo:index - 1) endsWith:'height') ifFalse:[
   212     ((line copyTo:index - 1) endsWith:'height') ifFalse:[
   207         'format error (expected height)' errorPrintNL.
   213 	'format error (expected height)' errorPrintNL.
   208         inStream close.
   214 	inStream close.
   209         ^ nil
   215 	^ nil
   210     ].
   216     ].
   211     line := line copyFrom:(index + 1).
   217     line := line copyFrom:(index + 1).
   212     height := Number readFromString:line.
   218     height := Number readFromString:line.
   213 
   219 
   214     bytesPerRow := width // 8.
   220     bytesPerRow := width // 8.
   215     ((width \\ 8) ~~ 0) ifTrue:[
   221     ((width \\ 8) ~~ 0) ifTrue:[
   216         bytesPerRow := bytesPerRow + 1
   222 	bytesPerRow := bytesPerRow + 1
   217     ].
   223     ].
   218 
   224 
   219     reverseBits := self class reverseBits.
   225     reverseBits := self class reverseBits.
   220 
   226 
   221     data := ByteArray new:(bytesPerRow * height).
   227     data := ByteArray new:(bytesPerRow * height).
   222     dstIndex := 1.
   228     dstIndex := 1.
   223 
   229 
   224     line := inStream nextLine.
   230     line := inStream nextLine.
   225     [line startsWith:'#'] whileTrue:[
   231     [line startsWith:'#'] whileTrue:[
   226         line := inStream nextLine
   232 	line := inStream nextLine
   227     ].
   233     ].
   228 
   234 
   229     line := inStream nextLine.
   235     line := inStream nextLine.
   230     [line notNil] whileTrue:[
   236     [line notNil] whileTrue:[
   231         index := 1.
   237 	index := 1.
   232         [index ~~ 0] whileTrue:[
   238 	[index ~~ 0] whileTrue:[
   233             index := line indexOf:$x startingAt:index.
   239 	    index := line indexOf:$x startingAt:index.
   234             (index ~~ 0) ifTrue:[
   240 	    (index ~~ 0) ifTrue:[
   235                 index := index + 1.
   241 		index := index + 1.
   236                 hi := (line at:index) digitValue.
   242 		hi := (line at:index) digitValue.
   237                 index := index + 1.
   243 		index := index + 1.
   238                 lo := (line at:index) digitValue.
   244 		lo := (line at:index) digitValue.
   239                 val := (hi bitShift:4) bitOr:lo.
   245 		val := (hi bitShift:4) bitOr:lo.
   240                 data at:dstIndex put:(reverseBits at:(val + 1)).
   246 		data at:dstIndex put:(reverseBits at:(val + 1)).
   241                 dstIndex := dstIndex + 1
   247 		dstIndex := dstIndex + 1
   242             ]
   248 	    ]
   243         ].
   249 	].
   244         line := inStream nextLine
   250 	line := inStream nextLine
   245     ].
   251     ].
   246     photometric := #whiteIs0.
   252     photometric := #whiteIs0.
   247     samplesPerPixel := 1.
   253     samplesPerPixel := 1.
   248     bitsPerSample := #(1).
   254     bitsPerSample := #(1).
   249 
   255