TargaReader.st
author claus
Mon, 18 Sep 1995 00:52:44 +0200
changeset 103 8efb0016ad8c
parent 96 948318b2fbd4
child 107 7e7debba3a26
permissions -rw-r--r--
.

"
 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.3 1995-08-29 17:45:06 claus 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|

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