IrisRGBReader.st
author Claus Gittinger <cg@exept.de>
Wed, 17 Sep 1997 20:11:14 +0200
changeset 698 f7e53b928204
parent 647 6f26c76aa0c9
child 810 93a9f3c4d8ec
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1997 by eXept Software AG / 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.
"

ImageReader subclass:#IrisRGBReader
	instanceVariableNames:'bytesPerPixel'
	classVariableNames:''
	poolDictionaries:''
	category:'Graphics-Images-Support'
!

!IrisRGBReader class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1997 by eXept Software AG / 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.
"
!

documentation
"
    this class provides methods for loading Iris RGB format images. 
    Only 3-byte/pixel format is supported.
    Writing is not (yet) supported.

    [author:]
        Claus Gittinger

    [See also:]
        Image Form Icon
        BlitImageReader FaceReader GIFReader JPEGReader PCXReader 
        ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader 
        XBMReader XPMReader XWDReader 
"
! !

!IrisRGBReader class methodsFor:'initialization'!

initialize
    "install myself in the Image classes fileFormat table
     for the `.rgb' extension."

    MIMETypes defineImageType:'image/x-rgb' suffix:'rgb' reader:self.

    "Modified: 14.4.1997 / 15:49:19 / cg"
! !

!IrisRGBReader class methodsFor:'testing'!

isValidImageFile:aFileName
    "return true, if aFileName contains an IRIS_RGB image"

    |inStream magic type bpp dim w h bytesPerPixel|

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

    inStream binary.

    magic := inStream nextShortMSB:true.
    type := inStream nextShortMSB:true.
    dim := inStream nextShortMSB:true.
    w := inStream nextShortMSB:true.
    h := inStream nextShortMSB:true.
    bytesPerPixel := inStream nextShortMSB:true.
    inStream close.

    magic ~~ 8r0732 ifTrue:[^ false].
    bpp := type bitAnd:16r00FF.
    bpp ~~ 1 ifTrue:[^ false].
    bytesPerPixel ~~ 3 ifTrue:[^ false].

    ^ true

    "
     IrisRGBReader isValidImageFile:'/home2/cg/capture.rgb'
    "

    "Modified: 14.4.1997 / 16:51:58 / cg"
! !

!IrisRGBReader methodsFor:'reading from file'!

fromStream:aStream
    "read a Portable bitmap file format as of Jeff Poskanzers Portable Bitmap Package.
     supported are IRIS_RGB, PGB and PNM files." 

    | magic type dim isRLE bpp|

    inStream := aStream.
    inStream binary.

    magic := inStream nextShortMSB:true.
    magic ~~ 8r0732 ifTrue:[
        'IrisRGBReader [warning]: bad magic' errorPrintCR.
        ^nil
    ].

    type := inStream nextShortMSB:true.
    dim := inStream nextShortMSB:true.
    width := inStream nextShortMSB:true.
    height := inStream nextShortMSB:true.
    bytesPerPixel := inStream nextShortMSB:true.

    isRLE := (type bitAnd:16rFF00) == 16r0100.
    bpp := type bitAnd:16r00FF.
    bpp ~~ 1 ifTrue:[
        'IrisRGBReader [warning]: not 1byte/pixel chan' errorPrintCR.
        ^nil
    ].
    bytesPerPixel ~~ 3 ifTrue:[
        'IrisRGBReader [warning]: can only read 3-channel images' errorPrintCR.
        ^nil
    ].

    isRLE ifTrue:[
        self readRLEData
    ] ifFalse:[
        self readVerbatimData
    ].

    photometric := #rgb.
    samplesPerPixel := 3.
    bitsPerSample := #(8 8 8).

    "
     IrisRGBReader fromFile:'/home2/cg/capture.rgb'
    "

    "Created: 14.4.1997 / 15:38:51 / cg"
    "Modified: 14.4.1997 / 17:06:05 / cg"
!

readRLEData 
    "read RLE compressed data"

    |rleBufferLen tableLen startTable lengthTable rleData
     cur badOrder y z pos tblIdx dstIdx|

    rleBufferLen := width * 2 + 10.
    tableLen := height * bytesPerPixel.

    startTable := Array new:tableLen.
    lengthTable := Array new:tableLen.
    rleData := ByteArray uninitializedNew:rleBufferLen.

    "/ read rowStart & length table

    inStream position:512+1.
    1 to:tableLen do:[:i |
        startTable at:i put:(inStream nextLongMSB:true).
    ].
    1 to:tableLen do:[:i |
        lengthTable at:i put:(inStream nextLongMSB:true).
    ].

    data := ByteArray uninitializedNew:(width*height*3).

    dstIdx := width * (height-1) * 3 + 1.

    0 to:(height-1) do:[:y |
        0 to:(bytesPerPixel-1) do:[:z |
            |start length|

            tblIdx := y + (z*height) + 1.

            start := startTable at:tblIdx.
            length := lengthTable at:tblIdx.

            inStream position:(start + 1).
            (inStream nextBytes:length into:rleData startingAt:1) ~~ length ifTrue:[
                self halt:'short read'
            ].
            (rleData at:length) ~~ 0 ifTrue:[
                self halt.
            ].

            self class
                decompressRLEFrom:rleData at:1 
                into:data at:dstIdx+z increment:3.

"/            self expandRow:rleData to:dstIdx+z. "/ (3-z).
        ].
        dstIdx := dstIdx - (width * 3).
    ].

    "
     IrisRGBReader fromFile:'/home2/cg/capture.rgb'
    "

    "Modified: 23.4.1997 / 18:59:11 / cg"
! !

!IrisRGBReader methodsFor:'testing '!

canRepresent:anImage
    "return true, if anImage can be represented in my file format.
     Currently only B&W and Depth8 images are supported."

    |depth|

"/    anImage photometric == #rgb ifTrue:[
"/        ^ false  "/ not yet implemented
"/    ].
"/    (depth := anImage depth) == 1 ifTrue:[^ true].
"/    depth == 8 ifTrue:[^ true].
    ^ false

    "Created: 14.4.1997 / 15:38:51 / cg"
    "Modified: 14.4.1997 / 15:58:20 / cg"
! !

!IrisRGBReader class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libview2/IrisRGBReader.st,v 1.4 1997-06-30 20:55:55 cg Exp $'
! !
IrisRGBReader initialize!