TargaReader.st
author Claus Gittinger <cg@exept.de>
Tue, 23 Apr 1996 13:06:56 +0200
changeset 210 5405de794686
parent 114 e577a2f332d0
child 234 b6352d13e792
permissions -rw-r--r--
checkin from browser

"
 COPYRIGHT (c) 1994 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.
"

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

!TargaReader class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1994 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.
"
!

documentation
"
    this class provides methods for loading targa-file (tga) images.
    Limitations: 
        not fully tested (I only had a few targe files to check things)
        only supports 24 bits/pixel format
        Image saving not supported

    I had two tga files to test this code with - it may not work with
    other targa files.
    Suggestions: adapt & use the pbmplus library here.

    [See also:]
        BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader 
        ST80FormReader SunRasterReader TIFFReader WindowsIconReader 
        XBMReader XPMReader XWDReader
"
! !

!TargaReader class methodsFor:'initialization'!

initialize
    "tell Image-class, that a new fileReader is present
     for the '.tga' and '.TGA' extensions."

    Image fileFormats at:'.tga'  put:self.
    Image fileFormats at:'.TGA'  put:self.

! !

!TargaReader class methodsFor:'testing'!

isValidImageFile:aFileName
    "return true, if aFileName contains a targa-file image"

    |aStream w h depth flags|

    aStream := self streamReadingFile:aFileName.
    aStream isNil ifTrue:[^ false].
    aStream binary.
    aStream skip:12.   "/ skip 12 bytes

    w := aStream nextShortMSB:false.
    h := aStream nextShortMSB:false.

    depth := aStream next.
    flags := aStream next.

    (#("8" 24) includes:depth) ifFalse:[
	aStream close. ^ false
    ].
    flags ~~ 16r20 ifTrue:[
	aStream close. ^ false
    ].

    aStream close. 
    ^ true

    "
     TargaReader isValidImageFile:'bitmaps/test.tga'    
     TargaReader isValidImageFile:'bitmaps/garfield.gif'  
    " 
! !

!TargaReader methodsFor:'reading from file'!

fromStream:aStream
    "read a targa-image from aFileName. return the receiver (with all
     relevant instance variables set for the image) or nil on error"

    |depth flags nBytes ok|

    inStream := aStream.
    aStream binary.

    aStream skip:12.
    width := aStream nextShortMSB:false.
    height := aStream nextShortMSB:false.
    depth := aStream next.
    depth ~~ 24 ifTrue:[
	'TARGA: unsupported depth' errorPrintNL.
	^ nil
    ].
    flags := aStream next.
    flags ~~ 16r20 ifTrue:[
	^ false
    ].

    data := ByteArray new:(width * height * (depth / 8)).
    aStream nextBytes:(data size) into:data.
    "
     mhmh - order is blue-green-red
    "
    nBytes := data size.
    ok := false.

%{  /* OPTIONAL */
    if (__isByteArray(_INST(data))) {
	int lastIndex = __intVal(nBytes) - 2;
	unsigned char *cp = __ByteArrayInstPtr(_INST(data))->ba_element;
	int i;
	unsigned char t;

	for (i=0; i<lastIndex; i+=3, cp+=3) {
	    t = cp[0];
	    cp[0] = cp[2];
	    cp[2] = t;
	}
	ok = true;
    }
%}.
    ok ifFalse:[
	1 to:(data size - 2) by:3 do:[:i |
	    |t|
	    t := data at:i.
	    data at:i put:(data at:i+2).
	    data at:i+2 put:t
	]
    ].

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

    "
     TargaReader fromFile:'bitmaps/test.tga' 
    " 
! !

!TargaReader class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libview2/TargaReader.st,v 1.6 1996-04-23 11:05:56 cg Exp $'
! !
TargaReader initialize!