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 |
ImageReader subclass:#TargaReader
|
210
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
259
|
17 |
category:'Graphics-Images-Support'
|
37
|
18 |
!
|
|
19 |
|
|
20 |
!TargaReader class methodsFor:'documentation'!
|
|
21 |
|
|
22 |
copyright
|
|
23 |
"
|
|
24 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
|
25 |
All Rights Reserved
|
|
26 |
|
|
27 |
This software is furnished under a license and may be used
|
|
28 |
only in accordance with the terms of that license and with the
|
|
29 |
inclusion of the above copyright notice. This software may not
|
|
30 |
be provided or otherwise made available to, or used by, any
|
|
31 |
other person. No title to or ownership of the software is
|
|
32 |
hereby transferred.
|
|
33 |
"
|
|
34 |
!
|
|
35 |
|
|
36 |
documentation
|
|
37 |
"
|
|
38 |
this class provides methods for loading targa-file (tga) images.
|
|
39 |
Limitations:
|
210
|
40 |
not fully tested (I only had a few targe files to check things)
|
299
|
41 |
only supports 24 bits/pixel uncompressed format
|
210
|
42 |
Image saving not supported
|
37
|
43 |
|
|
44 |
I had two tga files to test this code with - it may not work with
|
299
|
45 |
other targa files (it certainly does not work with RLE-encoded or
|
|
46 |
1/8 bit images).
|
|
47 |
|
37
|
48 |
Suggestions: adapt & use the pbmplus library here.
|
210
|
49 |
|
|
50 |
[See also:]
|
234
|
51 |
Image Form Icon
|
210
|
52 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader
|
|
53 |
ST80FormReader SunRasterReader TIFFReader WindowsIconReader
|
|
54 |
XBMReader XPMReader XWDReader
|
37
|
55 |
"
|
|
56 |
! !
|
|
57 |
|
|
58 |
!TargaReader class methodsFor:'initialization'!
|
|
59 |
|
|
60 |
initialize
|
210
|
61 |
"tell Image-class, that a new fileReader is present
|
398
|
62 |
for the '.tga' extension."
|
210
|
63 |
|
398
|
64 |
Image addReader:self suffix:'tga'.
|
37
|
65 |
|
398
|
66 |
"Modified: 1.2.1997 / 15:03:37 / cg"
|
37
|
67 |
! !
|
|
68 |
|
|
69 |
!TargaReader class methodsFor:'testing'!
|
|
70 |
|
|
71 |
isValidImageFile:aFileName
|
|
72 |
"return true, if aFileName contains a targa-file image"
|
|
73 |
|
43
|
74 |
|aStream w h depth flags|
|
37
|
75 |
|
43
|
76 |
aStream := self streamReadingFile:aFileName.
|
|
77 |
aStream isNil ifTrue:[^ false].
|
|
78 |
aStream binary.
|
|
79 |
aStream skip:12. "/ skip 12 bytes
|
37
|
80 |
|
43
|
81 |
w := aStream nextShortMSB:false.
|
|
82 |
h := aStream nextShortMSB:false.
|
37
|
83 |
|
43
|
84 |
depth := aStream next.
|
|
85 |
flags := aStream next.
|
37
|
86 |
|
|
87 |
(#("8" 24) includes:depth) ifFalse:[
|
43
|
88 |
aStream close. ^ false
|
37
|
89 |
].
|
|
90 |
flags ~~ 16r20 ifTrue:[
|
43
|
91 |
aStream close. ^ false
|
37
|
92 |
].
|
|
93 |
|
43
|
94 |
aStream close.
|
37
|
95 |
^ true
|
|
96 |
|
|
97 |
"
|
|
98 |
TargaReader isValidImageFile:'bitmaps/test.tga'
|
|
99 |
TargaReader isValidImageFile:'bitmaps/garfield.gif'
|
|
100 |
"
|
|
101 |
! !
|
|
102 |
|
|
103 |
!TargaReader methodsFor:'reading from file'!
|
|
104 |
|
43
|
105 |
fromStream:aStream
|
37
|
106 |
"read a targa-image from aFileName. return the receiver (with all
|
|
107 |
relevant instance variables set for the image) or nil on error"
|
|
108 |
|
299
|
109 |
|depth flags nBytes ok lenID hasColorMap imageType
|
|
110 |
cmapOffset cmapLength cmapEntrySize xOrg yOrg
|
|
111 |
t
|
|
112 |
dSize "{ Class: SmallInteger }" |
|
37
|
113 |
|
43
|
114 |
inStream := aStream.
|
|
115 |
aStream binary.
|
|
116 |
|
299
|
117 |
lenID := aStream next.
|
|
118 |
hasColorMap := aStream next.
|
|
119 |
imageType := aStream next.
|
|
120 |
cmapOffset := aStream nextShortMSB:false.
|
|
121 |
cmapLength := aStream nextShortMSB:false.
|
|
122 |
cmapEntrySize := aStream next.
|
|
123 |
xOrg := aStream nextShortMSB:false.
|
|
124 |
yOrg := aStream nextShortMSB:false.
|
|
125 |
|
43
|
126 |
width := aStream nextShortMSB:false.
|
|
127 |
height := aStream nextShortMSB:false.
|
|
128 |
depth := aStream next.
|
|
129 |
depth ~~ 24 ifTrue:[
|
299
|
130 |
'TARGA: unsupported depth' errorPrintNL.
|
|
131 |
^ nil
|
37
|
132 |
].
|
299
|
133 |
|
|
134 |
"/ flags:
|
|
135 |
"/ 0000 xxxx attribute-bits-per-pixel
|
|
136 |
"/ 00xx 0000 origin (0 -> lower-left / 1 -> l-r / 2 -> u-l / 3 -> u-r)
|
|
137 |
"/ xx00 0000 interleave (0 -> none / 1 -> odd/even / 2 ->4-fould / 3 reserved)
|
|
138 |
"/
|
43
|
139 |
flags := aStream next.
|
37
|
140 |
flags ~~ 16r20 ifTrue:[
|
299
|
141 |
^ false
|
|
142 |
].
|
|
143 |
|
|
144 |
lenID ~~ 0 ifTrue:[
|
|
145 |
aStream skip:lenID
|
37
|
146 |
].
|
|
147 |
|
|
148 |
data := ByteArray new:(width * height * (depth / 8)).
|
43
|
149 |
aStream nextBytes:(data size) into:data.
|
299
|
150 |
|
|
151 |
|
37
|
152 |
"
|
299
|
153 |
mhmh - pixel-byte order is blue-green-red
|
|
154 |
swap blue & red bytes
|
37
|
155 |
"
|
107
|
156 |
nBytes := data size.
|
|
157 |
ok := false.
|
|
158 |
|
|
159 |
%{ /* OPTIONAL */
|
|
160 |
if (__isByteArray(_INST(data))) {
|
299
|
161 |
int __lastIndex = __intVal(nBytes) - 2;
|
|
162 |
unsigned char *__cp = __ByteArrayInstPtr(_INST(data))->ba_element;
|
|
163 |
int __i;
|
|
164 |
unsigned char __t;
|
107
|
165 |
|
299
|
166 |
for (__i=0; __i<__lastIndex; __i+=3, __cp+=3) {
|
|
167 |
__t = __cp[0];
|
|
168 |
__cp[0] = __cp[2];
|
|
169 |
__cp[2] = __t;
|
|
170 |
}
|
|
171 |
ok = true;
|
107
|
172 |
}
|
|
173 |
%}.
|
|
174 |
ok ifFalse:[
|
299
|
175 |
dSize := data size.
|
|
176 |
1 to:(dSize - 2) by:3 do:[:i |
|
|
177 |
t := data at:i.
|
|
178 |
data at:i put:(data at:i+2).
|
|
179 |
data at:i+2 put:t
|
|
180 |
]
|
37
|
181 |
].
|
|
182 |
|
|
183 |
photometric := #rgb.
|
|
184 |
samplesPerPixel := 3.
|
|
185 |
bitsPerSample := #(8 8 8).
|
|
186 |
|
|
187 |
"
|
|
188 |
TargaReader fromFile:'bitmaps/test.tga'
|
|
189 |
"
|
|
190 |
! !
|
|
191 |
|
210
|
192 |
!TargaReader class methodsFor:'documentation'!
|
|
193 |
|
|
194 |
version
|
398
|
195 |
^ '$Header: /cvs/stx/stx/libview2/TargaReader.st,v 1.10 1997-02-01 14:05:35 cg Exp $'
|
210
|
196 |
! !
|
37
|
197 |
TargaReader initialize!
|