37
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
'From Smalltalk/X, Version:2.10.4 on 19-dec-1994 at 9:57:30 pm'!
|
|
14 |
|
|
15 |
ImageReader subclass:#TargaReader
|
|
16 |
instanceVariableNames:''
|
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
|
19 |
category:'Graphics-Images support'
|
|
20 |
!
|
|
21 |
|
|
22 |
!TargaReader class methodsFor:'documentation'!
|
|
23 |
|
|
24 |
copyright
|
|
25 |
"
|
|
26 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
|
27 |
All Rights Reserved
|
|
28 |
|
|
29 |
This software is furnished under a license and may be used
|
|
30 |
only in accordance with the terms of that license and with the
|
|
31 |
inclusion of the above copyright notice. This software may not
|
|
32 |
be provided or otherwise made available to, or used by, any
|
|
33 |
other person. No title to or ownership of the software is
|
|
34 |
hereby transferred.
|
|
35 |
"
|
|
36 |
!
|
|
37 |
|
|
38 |
version
|
|
39 |
"
|
|
40 |
$Header: /cvs/stx/stx/libview2/TargaReader.st,v 1.1 1995-02-06 00:41:26 claus Exp $
|
|
41 |
"
|
|
42 |
!
|
|
43 |
|
|
44 |
documentation
|
|
45 |
"
|
|
46 |
this class provides methods for loading targa-file (tga) images.
|
|
47 |
Limitations:
|
|
48 |
untested
|
|
49 |
only supports 24 bits/pixel format
|
|
50 |
Save not supported
|
|
51 |
|
|
52 |
I had two tga files to test this code with - it may not work with
|
|
53 |
other targa files.
|
|
54 |
Suggestions: adapt & use the pbmplus library here.
|
|
55 |
"
|
|
56 |
! !
|
|
57 |
|
|
58 |
!TargaReader class methodsFor:'initialization'!
|
|
59 |
|
|
60 |
initialize
|
|
61 |
Image fileFormats at:'.tga' put:self.
|
|
62 |
Image fileFormats at:'.TGA' put:self.
|
|
63 |
|
|
64 |
! !
|
|
65 |
|
|
66 |
!TargaReader class methodsFor:'testing'!
|
|
67 |
|
|
68 |
isValidImageFile:aFileName
|
|
69 |
"return true, if aFileName contains a targa-file image"
|
|
70 |
|
|
71 |
|inStream w h depth flags|
|
|
72 |
|
|
73 |
inStream := self streamReadingFile:aFileName.
|
|
74 |
inStream isNil ifTrue:[inStream close. ^ false].
|
|
75 |
inStream binary.
|
|
76 |
inStream skip:12. "/ skip 12 bytes
|
|
77 |
|
|
78 |
w := inStream nextShortMSB:false.
|
|
79 |
h := inStream nextShortMSB:false.
|
|
80 |
|
|
81 |
depth := inStream next.
|
|
82 |
flags := inStream next.
|
|
83 |
|
|
84 |
(#("8" 24) includes:depth) ifFalse:[
|
|
85 |
inStream close. ^ false
|
|
86 |
].
|
|
87 |
flags ~~ 16r20 ifTrue:[
|
|
88 |
inStream close. ^ false
|
|
89 |
].
|
|
90 |
|
|
91 |
inStream close.
|
|
92 |
^ true
|
|
93 |
|
|
94 |
"
|
|
95 |
TargaReader isValidImageFile:'bitmaps/test.tga'
|
|
96 |
TargaReader isValidImageFile:'bitmaps/garfield.gif'
|
|
97 |
"
|
|
98 |
! !
|
|
99 |
|
|
100 |
!TargaReader methodsFor:'reading from file'!
|
|
101 |
|
|
102 |
fromFile:aFileName
|
|
103 |
"read a targa-image from aFileName. return the receiver (with all
|
|
104 |
relevant instance variables set for the image) or nil on error"
|
|
105 |
|
|
106 |
|depth flags|
|
|
107 |
|
|
108 |
inStream := self class streamReadingFile:aFileName.
|
|
109 |
inStream isNil ifTrue:[
|
|
110 |
'TARGA: file open error' errorPrintNL.
|
|
111 |
^ nil
|
|
112 |
].
|
|
113 |
inStream binary.
|
|
114 |
inStream skip:12.
|
|
115 |
width := inStream nextShortMSB:false.
|
|
116 |
height := inStream nextShortMSB:false.
|
|
117 |
depth := inStream next.
|
|
118 |
depth ~~ 24 ifTrue:[
|
|
119 |
'TARGA: unsupported depth' errorPrintNL.
|
|
120 |
inStream close.
|
|
121 |
^ nil
|
|
122 |
].
|
|
123 |
flags := inStream next.
|
|
124 |
flags ~~ 16r20 ifTrue:[
|
|
125 |
^ false
|
|
126 |
].
|
|
127 |
|
|
128 |
data := ByteArray new:(width * height * (depth / 8)).
|
|
129 |
inStream nextBytes:(data size) into:data.
|
|
130 |
"
|
|
131 |
mhmh - order is blue-green-red
|
|
132 |
"
|
|
133 |
1 to:(data size - 2) by:3 do:[:i |
|
|
134 |
|t|
|
|
135 |
t := data at:i.
|
|
136 |
data at:i put:(data at:i+2).
|
|
137 |
data at:i+2 put:t
|
|
138 |
].
|
|
139 |
|
|
140 |
photometric := #rgb.
|
|
141 |
samplesPerPixel := 3.
|
|
142 |
bitsPerSample := #(8 8 8).
|
|
143 |
|
|
144 |
"
|
|
145 |
TargaReader fromFile:'bitmaps/test.tga'
|
|
146 |
"
|
|
147 |
! !
|
|
148 |
|
|
149 |
TargaReader initialize!
|