TargaReader.st
author Claus Gittinger <cg@exept.de>
Sat, 11 Nov 1995 17:05:49 +0100
changeset 114 e577a2f332d0
parent 107 7e7debba3a26
child 210 5405de794686
permissions -rw-r--r--
uff - version methods changed to return stings

"
 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.
"

'From Smalltalk/X, Version:2.10.4 on 19-dec-1994 at 9:57:30 pm'!

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.
"
!

version
    ^ '$Header: /cvs/stx/stx/libview2/TargaReader.st,v 1.5 1995-11-11 16:05:17 cg Exp $'
!

documentation
"
    this class provides methods for loading targa-file (tga) images.
    Limitations: 
	untested
	only supports 24 bits/pixel format
	Save 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.
"
! !

!TargaReader class methodsFor:'initialization'!

initialize
    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 initialize!