SunRasterReader.st
changeset 0 3f9277473954
child 3 78aaa5408119
equal deleted inserted replaced
-1:000000000000 0:3f9277473954
       
     1 "
       
     2  COPYRIGHT (c) 1993 by Claus Gittinger
       
     3               All Rights Reserved
       
     4 
       
     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
       
     7  inclusion of the above copyright notice.   This software may not
       
     8  be provided or otherwise made available to, or used by, any
       
     9  other person.  No title to or ownership of the software is
       
    10  hereby transferred.
       
    11 "
       
    12 
       
    13 ImageReader subclass:#SunRasterReader
       
    14          instanceVariableNames:''
       
    15          classVariableNames:''
       
    16          poolDictionaries:''
       
    17          category:'Graphics-Support'
       
    18 !
       
    19 
       
    20 SunRasterReader comment:'
       
    21 
       
    22 COPYRIGHT (c) 1993 by Claus Gittinger
       
    23               All Rights Reserved
       
    24 
       
    25 this class provides methods for loading Sun Raster file images
       
    26 
       
    27 %W% %E%
       
    28 written Summer 91 by claus
       
    29 '!
       
    30 
       
    31 !SunRasterReader methodsFor:'reading from file'!
       
    32 
       
    33 fromFile: aFilename 
       
    34     | rasterType mapType mapBytes imageWords form depth 
       
    35       rMap gMap bMap mapLen
       
    36       bits a b c index|
       
    37 
       
    38     inStream := FileStream readonlyFileNamed:aFilename.
       
    39     inStream isNil ifTrue:[
       
    40         'open error' printNewline. 
       
    41         ^ nil
       
    42     ].
       
    43 
       
    44     inStream binary.
       
    45 
       
    46     ((inStream nextWord = 16r59A6) 
       
    47     and:[inStream nextWord = 16r6A95]) ifFalse: [
       
    48 "
       
    49     inStream nextLong = 16r59A66A95 ifFalse: [
       
    50 "
       
    51         inStream close.
       
    52         self error: 'Not a Sun Raster File (bad magic number)'
       
    53     ].
       
    54 
       
    55     width := inStream nextLong.
       
    56     height := inStream nextLong.
       
    57 
       
    58     depth := inStream nextLong.
       
    59     inStream nextLong.   "Ignore the image length since I can't rely on it anyway."
       
    60     rasterType _ inStream nextLong.
       
    61     mapType := inStream nextLong.  "Ignore the raster maptype."
       
    62     mapBytes := inStream nextLong.  
       
    63 
       
    64     depth = 8 ifTrue: [
       
    65         mapLen := (mapBytes // 3).
       
    66         rMap := ByteArray new:mapLen.
       
    67         gMap := ByteArray new:mapLen.
       
    68         bMap := ByteArray new:mapLen.
       
    69         inStream nextBytes:mapLen into:rMap.
       
    70         inStream nextBytes:mapLen into:gMap.
       
    71         inStream nextBytes:mapLen into:bMap.
       
    72 
       
    73         data := ByteArray new:(width * height).
       
    74         inStream nextBytes:(width * height) into:data.
       
    75 
       
    76         photometric := #palette.
       
    77         samplesPerPixel := 1.
       
    78         bitsPerSample := #(8).
       
    79         colorMap := Array with:rMap with:gMap with:bMap.
       
    80         inStream close.
       
    81         ^ self
       
    82     ].
       
    83     depth ~~ 1 ifTrue: [
       
    84         inStream close.
       
    85         self error: 'Raster file is not monochrome'
       
    86     ].
       
    87 
       
    88     form := nil.
       
    89 
       
    90     inStream skip: mapBytes.  "Skip the color map."
       
    91     imageWords _ (width / 16) ceiling * height.
       
    92     data := ByteArray new:(imageWords * 2).
       
    93 
       
    94     (rasterType between: 0 and: 2) ifFalse: [
       
    95         inStream close.
       
    96         self error: 'Unknown raster file rasterType'
       
    97     ].
       
    98 
       
    99     (rasterType = 2)  ifFalse: [
       
   100         "no compression of bytes"
       
   101         inStream nextBytes:(imageWords * 2) into:data
       
   102     ] ifTrue: [ 
       
   103         "run length compression of bytes"
       
   104 
       
   105         bits _ ByteArray new: imageWords * 2.
       
   106         index := 1.
       
   107         a _ inStream next.
       
   108         [a notNil] whileTrue: [
       
   109             (a = 128) ifFalse: [
       
   110                 bits at:index put: a.
       
   111                 index := index + 1
       
   112             ] ifTrue: [
       
   113                 b _ inStream next.
       
   114                 b = 0 ifTrue: [
       
   115                     bits at:index put:128 .
       
   116                     index := index + 1
       
   117                 ] ifFalse: [
       
   118                     c := inStream next.
       
   119                     1 to:(b+1) do:[:i |
       
   120                         bits at:index put:c.
       
   121                         index := index + 1
       
   122                     ]
       
   123                 ]
       
   124             ].
       
   125             a _ inStream next
       
   126         ].
       
   127         1 to: imageWords do: [:i | form bitsWordAt: i put: (bits wordAt: i)]
       
   128     ].
       
   129     photometric := #whiteIs0.
       
   130     samplesPerPixel := 1.
       
   131     bitsPerSample := #(1).
       
   132     inStream close
       
   133 
       
   134     "Image fromFile:'../fileIn/bitmaps/founders.im8'"
       
   135 ! !