SunRasterReader.st
author claus
Sun, 02 Jul 1995 18:18:00 +0200
changeset 83 97fd04d167c8
parent 44 c6cf7d0d6337
child 96 948318b2fbd4
permissions -rw-r--r--
.

"
 COPYRIGHT (c) 1993 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

'From Smalltalk/X, Version:2.10.4 on 18-feb-1995 at 2:18:51 am'!

ImageReader subclass:#SunRasterReader
	 instanceVariableNames:''
	 classVariableNames:''
	 poolDictionaries:''
	 category:'Graphics-Images support'
!

SunRasterReader comment:'
COPYRIGHT (c) 1993 by Claus Gittinger
	      All Rights Reserved

$Header: /cvs/stx/stx/libview2/SunRasterReader.st,v 1.13 1995-07-02 16:17:30 claus Exp $
'!

!SunRasterReader class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1993 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

version
"
$Header: /cvs/stx/stx/libview2/SunRasterReader.st,v 1.13 1995-07-02 16:17:30 claus Exp $
"
!

documentation
"
    this class provides methods for loading Sun Raster file images.
    No image writing is implemented.
"
! !

!SunRasterReader class methodsFor:'initialization'!

initialize
    Image fileFormats at:'.icon'  put:self.
! !

!SunRasterReader class methodsFor:'testing'!

isValidImageFile:aFileName
    "return true, if aFileName contains a sunraster image"

    |inStream nr|

    inStream := self streamReadingFile:aFileName.
    inStream isNil ifTrue:[^ false].

    "try sun raster"
    inStream binary.
    ((inStream nextWord == 16r59A6) 
    and:[inStream nextWord == 16r6A95]) ifTrue: [
	inStream close.
	^ true
    ].

    inStream isPositionable ifFalse:[^ false].

    "try sun bitmap image format"
    inStream text.
    inStream reset.

    "must start with a comment"
    inStream skipSeparators.
    inStream next ~~ $/ ifTrue:[^ false].
    inStream next ~~ $* ifTrue:[^ false].

    (inStream skipThroughAll: 'idth') isNil ifTrue: [
	inStream close.
	^ false
    ].
    inStream next; skipSeparators.
    nr := Integer readFrom: inStream.
    (nr isNil or:[nr <= 0]) ifTrue: [
	inStream close.
	^ false
    ].

    (inStream skipThroughAll: 'eight') isNil ifTrue: [
	inStream close.
	^ false
    ].
    inStream next; skipSeparators.
    nr := Integer readFrom: inStream.
    (nr isNil or:[nr <= 0]) ifTrue: [
	inStream close.
	^ false
    ].

    inStream close.
    ^ true
! !

!SunRasterReader methodsFor:'reading from file'!

fromStream: aStream 
    | rasterType mapType mapBytes imageWords form depth 
      rMap gMap bMap mapLen
      bits a b c index pos|

    inStream := aStream.

    aStream binary.

    pos := aStream position.
    ((aStream nextWord == 16r59A6) 
    and:[aStream nextWord == 16r6A95]) ifFalse: [
"/        'SUNReader: not a SunRaster file' errorPrintNL.
	aStream position:pos.
	^ self fromSunIconStream:aStream
    ].

    width := aStream nextLong.
    height := aStream nextLong.

    depth := aStream nextLong.
    aStream nextLong.   "Ignore the image length since I can't rely on it anyway."
    rasterType := aStream nextLong.
    mapType := aStream nextLong.  "Ignore the raster maptype."
    mapBytes := aStream nextLong.  

    depth = 8 ifTrue: [
	mapLen := (mapBytes // 3).
	rMap := ByteArray uninitializedNew:mapLen.
	gMap := ByteArray uninitializedNew:mapLen.
	bMap := ByteArray uninitializedNew:mapLen.
	aStream nextBytes:mapLen into:rMap.
	aStream nextBytes:mapLen into:gMap.
	aStream nextBytes:mapLen into:bMap.

	data := ByteArray uninitializedNew:(width * height).
	aStream nextBytes:(width * height) into:data.

	photometric := #palette.
	samplesPerPixel := 1.
	bitsPerSample := #(8).
	colorMap := Colormap redVector:rMap greenVector:gMap blueVector:bMap.
	^ self
    ].
    depth ~~ 1 ifTrue: [
	'SUNReader: only depth 1 and 8 supported' errorPrintNL.
	^ nil
    ].

    form := nil.

    aStream skip: mapBytes.  "Skip the color map."
    imageWords := (width / 16) ceiling * height.
    data := ByteArray uninitializedNew:(imageWords * 2).

    (rasterType between: 0 and: 2) ifFalse: [
	'SUNReader: Unknown raster file rasterType' errorPrintNL.
	^ nil
    ].

    (rasterType = 2)  ifFalse: [
	"no compression of bytes"
	aStream nextBytes:(imageWords * 2) into:data
    ] ifTrue: [ 
	"run length compression of bytes"

	index := 1.
	a := aStream next.
	[a notNil] whileTrue: [
	    (a = 128) ifFalse: [
		data at:index put: a.
		index := index + 1
	    ] ifTrue: [
		b := aStream next.
		b = 0 ifTrue: [
		    data at:index put:128 .
		    index := index + 1
		] ifFalse: [
		    c := aStream next.
		    1 to:(b+1) do:[:i |
			data at:index put:c.
			index := index + 1
		    ]
		]
	    ].
	    a := aStream next
	].
    ].
    photometric := #whiteIs0.
    samplesPerPixel := 1.
    bitsPerSample := #(1).

    "
     Image fromFile:'bitmaps/founders.im8'
     Image fromFile:'bitmaps/bf.im8'
     SunRasterReader fromStream:'bitmaps/founders.im8' asFilename readStream
     SunRasterReader fromStream:'bitmaps/bf.im8' asFilename readStream
    "
!

fromSunIconStream: aStream 
    |index word 
     w "{ Class: SmallInteger }"
     h "{ Class: SmallInteger }"|

    inStream := aStream.
    aStream text.

    (aStream skipThroughAll:'idth') isNil ifTrue: [
	'SUNReader: Not a Sun Raster/Icon File' errorPrintNL.
	^nil
    ].
    aStream next; skipSeparators. "skip $="
    width := Integer readFrom: aStream.
    (width isNil or:[width <= 0]) ifTrue: [
	'SUNReader: format error (expected number)' errorPrintNL.
	^ nil
    ].
    w := width.

    (aStream skipThroughAll:'eight') isNil ifTrue: [
	'SUNReader: format error (expected height)' errorPrintNL.
	^ nil
    ].
    aStream next; skipSeparators. "skip $="
    height := Integer readFrom: aStream.
    (height isNil or:[height <= 0]) ifTrue: [
	'SUNReader: format error (expected number)' errorPrintNL.
	^nil
    ].
    h := height.

    data := ByteArray uninitializedNew:((width + 7 // 8) * height).
    photometric := #whiteIs0.
    samplesPerPixel := 1.
    bitsPerSample := #(1).

    index := 0.
    1 to:h do: [:row |
	1 to: (w + 15 // 16) do: [:col |
	    "rows are rounded up to next multiple of 16 bits"
	    (aStream skipThroughAll:'0x') isNil ifTrue: [^ nil]. 
	    word := Integer readFrom:aStream radix:16.
	    word isNil ifTrue:[
		'SUNReader: format error' errorPrintNL.
		^ nil
	    ].
	    data at: (index _ index + 1) put: (word bitShift:-8).
	    data at: (index _ index + 1) put: (word bitAnd:16rFF).
	]
    ].
! !

SunRasterReader initialize!