author | Claus Gittinger <cg@exept.de> |
Fri, 01 Sep 2017 15:36:01 +0200 | |
changeset 4009 | bece1481d314 |
parent 4002 | 91e154c3580d |
child 4016 | 47c4ea8134ec |
permissions | -rw-r--r-- |
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 |
" |
|
1577 | 12 |
"{ Package: 'stx:libview2' }" |
13 |
||
3998 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
37 | 16 |
ImageReader subclass:#TargaReader |
3998 | 17 |
instanceVariableNames:'orientation bytesPerRow bytesPerPixel imageType' |
210 | 18 |
classVariableNames:'' |
19 |
poolDictionaries:'' |
|
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1738
diff
changeset
|
20 |
category:'Graphics-Images-Readers' |
37 | 21 |
! |
22 |
||
23 |
!TargaReader class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
39 |
documentation |
|
40 |
" |
|
41 |
this class provides methods for loading targa-file (tga) images. |
|
556 | 42 |
|
3998 | 43 |
The Targa-File format is an image file format used by a wide variety of both |
44 |
scanners and imaging software, and exists in many incarnations. |
|
45 |
For details, please refer to the 'Truevision Technical Guide': |
|
46 |
||
37 | 47 |
Limitations: |
556 | 48 |
not fully tested (I only had a few targa files to check things) |
3998 | 49 |
only supports 8, 24 and 32 bits/pixel formats; |
210 | 50 |
Image saving not supported |
37 | 51 |
|
3998 | 52 |
I had two tga files to test this code with; |
53 |
therefore it may pr may not work with other targa files |
|
54 |
(it certainly does not work with 1/16 bit images). |
|
299 | 55 |
|
37 | 56 |
Suggestions: adapt & use the pbmplus library here. |
210 | 57 |
|
58 |
[See also:] |
|
234 | 59 |
Image Form Icon |
210 | 60 |
BlitImageReader FaceReader GIFReader JPEGReader PBMReader PCXReader |
61 |
ST80FormReader SunRasterReader TIFFReader WindowsIconReader |
|
62 |
XBMReader XPMReader XWDReader |
|
4001 | 63 |
|
64 |
http://www.dca.fee.unicamp.br/~martino/disciplinas/ea978/tgaffs.pdf |
|
4002 | 65 |
|
66 |
[examples:] |
|
67 |
Smalltalk loadPackage:'stx:goodies/communication'. |
|
68 |
Smalltalk loadPackage:'exept:libcrypt/ssl'. |
|
69 |
||
70 |
|file| |
|
71 |
file := (HTTPInterface getStreamFor:'https://samples.libav.org/image-samples/TGA/rgb32rle.tga') stream contents. |
|
72 |
(TargaReader fromStream:file readStream) inspect. |
|
73 |
||
74 |
|file| |
|
75 |
file := (HTTPInterface getStreamFor:'https://samples.libav.org/image-samples/TGA/rgb16rle.tga') stream contents. |
|
76 |
TargaReader fromStream:file readStream. |
|
37 | 77 |
" |
1738 | 78 |
! |
79 |
||
80 |
format |
|
81 |
" |
|
82 |
Shorts etc. are lsb. |
|
83 |
||
84 |
offset: |
|
85 |
||
86 |
1 lenID byte |
|
87 |
2 hasColorMap byte |
|
88 |
3 imageType byte |
|
89 |
1 MapRGB |
|
90 |
2 RawRGB |
|
91 |
3 RawMono |
|
92 |
9 MapEnCode |
|
93 |
10 RawEnCode |
|
94 |
4, 5 cmapOffset short |
|
95 |
6, 7 cmapLength short |
|
96 |
8 cmapEntrySize short |
|
97 |
9,10 xOrg short |
|
98 |
11,12 yOrg short |
|
99 |
||
100 |
13,14 width |
|
101 |
15,16 height |
|
102 |
17 depth |
|
103 |
18 flags |
|
104 |
0000 xxxx attribute-bits-per-pixel |
|
105 |
0000 0001 greysc |
|
106 |
0000 0010 colour |
|
107 |
0000 0011 mapped |
|
108 |
0000 0100 rleEncoded |
|
109 |
0000 1000 interlaced |
|
110 |
00xx 0000 origin (0 -> lower-left / 1 -> l-r / 2 -> u-l / 3 -> u-r) |
|
111 |
xx00 0000 interleave (0 -> none / 1 -> odd/even / 2 ->4-fould / 3 reserved) |
|
112 |
||
113 |
19.. len lenID-bytes |
|
114 |
.. colorMap cmapLength*3 bytes iff hasColorMap ~~ 0 |
|
115 |
" |
|
37 | 116 |
! ! |
117 |
||
118 |
!TargaReader class methodsFor:'initialization'! |
|
119 |
||
120 |
initialize |
|
210 | 121 |
"tell Image-class, that a new fileReader is present |
398 | 122 |
for the '.tga' extension." |
210 | 123 |
|
647 | 124 |
MIMETypes defineImageType:'image/x-targa' suffix:'tga' reader:self. |
37 | 125 |
|
630 | 126 |
"Modified: 27.6.1997 / 18:39:43 / cg" |
37 | 127 |
! ! |
128 |
||
129 |
!TargaReader class methodsFor:'testing'! |
|
130 |
||
131 |
isValidImageFile:aFileName |
|
132 |
"return true, if aFileName contains a targa-file image" |
|
133 |
||
1577 | 134 |
|aStream w h depth flags imageType ok| |
135 |
||
136 |
ok := true. |
|
37 | 137 |
|
43 | 138 |
aStream := self streamReadingFile:aFileName. |
139 |
aStream isNil ifTrue:[^ false]. |
|
140 |
aStream binary. |
|
1577 | 141 |
|
142 |
aStream next. "lenID" |
|
143 |
aStream next. "hasColorMap" |
|
144 |
imageType := aStream next. |
|
145 |
aStream skip:2. "cmapOffset" |
|
146 |
aStream skip:2. "cmapLength" |
|
147 |
aStream next. "cmapEntrySize" |
|
148 |
aStream skip:2. "xOrg" |
|
149 |
aStream skip:2. "yOrg" |
|
37 | 150 |
|
43 | 151 |
w := aStream nextShortMSB:false. |
152 |
h := aStream nextShortMSB:false. |
|
37 | 153 |
|
43 | 154 |
depth := aStream next. |
155 |
flags := aStream next. |
|
37 | 156 |
|
1577 | 157 |
"/ MapRGB == 1 |
158 |
"/ RawRGB == 2 |
|
159 |
"/ RawMono == 3 |
|
160 |
"/ MapEnCode == 9 |
|
161 |
"/ RawEnCode == 10 |
|
162 |
||
163 |
(#(1 2 3 9 10) includes:imageType) ifFalse:[ |
|
164 |
"/ 'TargaReader [warning]: unsupported imageType: ' errorPrint. imageType errorPrintCR. |
|
165 |
ok := false |
|
166 |
] ifTrue:[ |
|
167 |
(#(8 "16" 24 32) includes:depth) ifFalse:[ |
|
168 |
ok := false |
|
169 |
]. |
|
37 | 170 |
]. |
43 | 171 |
aStream close. |
1577 | 172 |
^ ok |
37 | 173 |
|
174 |
" |
|
175 |
TargaReader isValidImageFile:'bitmaps/test.tga' |
|
176 |
TargaReader isValidImageFile:'bitmaps/garfield.gif' |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
177 |
" |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
178 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
179 |
"Modified: 21.4.1997 / 20:46:52 / cg" |
37 | 180 |
! ! |
181 |
||
1848 | 182 |
!TargaReader methodsFor:'private-reading'! |
37 | 183 |
|
1738 | 184 |
handleImageOrientation |
185 |
|rowIdx startIdx endIdx t| |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
186 |
|
1738 | 187 |
orientation == #topLeft ifTrue:[ |
188 |
^ self. |
|
37 | 189 |
]. |
190 |
||
1738 | 191 |
orientation == #topRight ifTrue:[ |
192 |
"/ flip horizontal |
|
193 |
rowIdx := 1. |
|
194 |
1 to:height do:[:row | |
|
195 |
startIdx := rowIdx. |
|
196 |
endIdx := rowIdx + bytesPerRow - bytesPerPixel. |
|
197 |
1 to:width//2 do:[:x | |
|
198 |
0 to:bytesPerPixel-1 do:[:c | |
|
199 |
t := data at:startIdx+c. |
|
200 |
data at:startIdx+c put:(data at:endIdx+c). |
|
201 |
data at:endIdx+c put:t. |
|
202 |
]. |
|
203 |
startIdx := startIdx + bytesPerPixel. |
|
204 |
endIdx := endIdx - bytesPerPixel. |
|
1065
32a68620ddab
care for EOF while reading colorMap.
Claus Gittinger <cg@exept.de>
parents:
918
diff
changeset
|
205 |
]. |
1738 | 206 |
rowIdx := rowIdx + width. |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
207 |
]. |
1738 | 208 |
^ self. |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
209 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
210 |
|
1738 | 211 |
orientation == #bottomLeft ifTrue:[ |
212 |
"/ flip vertical |
|
213 |
startIdx := 1. |
|
214 |
endIdx := 1 + ((height - 1) * bytesPerRow). |
|
215 |
t := ByteArray new:bytesPerRow. |
|
216 |
1 to:height//2 do:[:row | |
|
217 |
t replaceFrom:1 to:bytesPerRow with:data startingAt:startIdx. |
|
218 |
data replaceFrom:startIdx to:startIdx+bytesPerRow-1 with:data startingAt:endIdx. |
|
219 |
data replaceFrom:endIdx to:endIdx+bytesPerRow-1 with:t startingAt:1. |
|
220 |
startIdx := startIdx + bytesPerRow. |
|
221 |
endIdx := endIdx - bytesPerRow |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
222 |
]. |
1738 | 223 |
^ self. |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
224 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
225 |
|
1738 | 226 |
'TargaReader [warning]: unsupported orientation: ' errorPrint. orientation errorPrintCR. |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
227 |
! |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
228 |
|
4002 | 229 |
read16 |
230 |
"read a 16 bit/pixel targa-image" |
|
231 |
||
232 |
|totalBytes remainingBytes dstIndex b1 b2| |
|
233 |
||
234 |
totalBytes := width * height * 2. |
|
235 |
data := ByteArray new:totalBytes. |
|
236 |
||
237 |
dstIndex := 1. |
|
238 |
remainingBytes := totalBytes. |
|
239 |
[remainingBytes > 0] whileTrue:[ |
|
240 |
b1 := inStream nextByte. |
|
241 |
b2 := inStream nextByte. |
|
242 |
data at:dstIndex put:b1. |
|
243 |
data at:dstIndex+1 put:b2. |
|
244 |
dstIndex := dstIndex + 2. |
|
245 |
remainingBytes := remainingBytes - 2. |
|
246 |
]. |
|
247 |
||
248 |
"Created: / 29-08-2017 / 23:38:30 / cg" |
|
249 |
! |
|
250 |
||
251 |
read16RLE |
|
252 |
"read a 16 bit/pixel rle encoded targa-image" |
|
253 |
||
254 |
|total count dstIndex code n b1 b2| |
|
255 |
||
256 |
data := ByteArray new:((total := width * height * 2)). |
|
257 |
count := 0. |
|
258 |
dstIndex := 1. |
|
259 |
[count < total] whileTrue:[ |
|
260 |
code := inStream nextByte. |
|
261 |
n := (code bitAnd:16r7F) + 1. |
|
262 |
(code bitAnd:16r80) ~~ 0 ifTrue:[ |
|
263 |
b1 := inStream nextByte. |
|
264 |
b2 := inStream nextByte. |
|
265 |
n timesRepeat:[ |
|
266 |
data at:dstIndex put:b1. |
|
267 |
data at:dstIndex+1 put:b2. |
|
268 |
dstIndex := dstIndex + 2 |
|
269 |
]. |
|
270 |
] ifFalse:[ |
|
271 |
n timesRepeat:[ |
|
272 |
b1 := inStream nextByte. |
|
273 |
b2 := inStream nextByte. |
|
274 |
data at:dstIndex put:b1. |
|
275 |
data at:dstIndex+1 put:b2. |
|
276 |
dstIndex := dstIndex + 2 |
|
277 |
] |
|
278 |
]. |
|
279 |
count := count + (n * 2). |
|
280 |
]. |
|
281 |
||
282 |
"Created: / 29-08-2017 / 23:39:19 / cg" |
|
283 |
! |
|
284 |
||
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
285 |
read24 |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
286 |
"read a 24 bit/pixel targa-image" |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
287 |
|
1738 | 288 |
|totalBytes| |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
289 |
|
1738 | 290 |
totalBytes := width * height * 3. |
291 |
data := ByteArray new:totalBytes. |
|
292 |
inStream nextBytes:totalBytes into:data. |
|
299 | 293 |
|
37 | 294 |
" |
299 | 295 |
mhmh - pixel-byte order is blue-green-red |
296 |
swap blue & red bytes |
|
37 | 297 |
" |
1738 | 298 |
self class swap:totalBytes bytesFromRGB_to_BGR_in:data. |
37 | 299 |
|
3998 | 300 |
"Modified: / 29-08-2017 / 23:06:02 / cg" |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
301 |
! |
37 | 302 |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
303 |
read24RLE |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
304 |
"read an 8 bit/pixel rle encoded targa-image" |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
305 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
306 |
|total count dstIndex code n r g b| |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
307 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
308 |
data := ByteArray new:((total := width * height * 3)). |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
309 |
count := 0. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
310 |
dstIndex := 1. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
311 |
[count < total] whileTrue:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
312 |
code := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
313 |
n := (code bitAnd:16r7F) + 1. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
314 |
(code bitAnd:16r80) ~~ 0 ifTrue:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
315 |
b := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
316 |
g := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
317 |
r := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
318 |
n timesRepeat:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
319 |
data at:dstIndex put:r. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
320 |
data at:dstIndex+1 put:g. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
321 |
data at:dstIndex+2 put:b. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
322 |
dstIndex := dstIndex + 3 |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
323 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
324 |
] ifFalse:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
325 |
n timesRepeat:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
326 |
b := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
327 |
g := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
328 |
r := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
329 |
data at:dstIndex put:r. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
330 |
data at:dstIndex+1 put:g. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
331 |
data at:dstIndex+2 put:b. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
332 |
dstIndex := dstIndex + 3 |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
333 |
] |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
334 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
335 |
count := count + (n * 3). |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
336 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
337 |
|
3998 | 338 |
"Created: / 21-04-1997 / 20:43:23 / cg" |
339 |
"Modified: / 29-08-2017 / 23:06:35 / cg" |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
340 |
! |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
341 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
342 |
read32 |
3998 | 343 |
"read a 32 bit/pixel targa-image" |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
344 |
|
1738 | 345 |
|totalBytes remainingBytes dstIndex a r g b| |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
346 |
|
3998 | 347 |
totalBytes := width * height * 4. |
1738 | 348 |
data := ByteArray new:totalBytes. |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
349 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
350 |
dstIndex := 1. |
1738 | 351 |
remainingBytes := totalBytes. |
352 |
[remainingBytes > 0] whileTrue:[ |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
353 |
b := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
354 |
g := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
355 |
r := inStream nextByte. |
3998 | 356 |
a := inStream nextByte. |
1738 | 357 |
data at:dstIndex put:r. |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
358 |
data at:dstIndex+1 put:g. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
359 |
data at:dstIndex+2 put:b. |
3998 | 360 |
data at:dstIndex+3 put:a. |
361 |
dstIndex := dstIndex + 4. |
|
362 |
remainingBytes := remainingBytes - 4. |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
363 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
364 |
|
1738 | 365 |
" |
366 |
TargaReader fromFile:'/phys/exept//unsaved1/pd_stuff/graphic/3d_engines/crystal_space/CS/plugins/video/canvas/sdl/img/move.tga' |
|
367 |
TargaReader fromFile:'/phys/exept/unsaved2/smalltalk/Squeak/croquet/Croquet/Content/Cisco/2520/Textures/2520caseback.tga' |
|
368 |
TargaReader fromFile:'/phys/exept/unsaved2/smalltalk/Squeak/croquet/Croquet/Content/Cisco/2520/Textures/2520dimm1front.tga' |
|
369 |
" |
|
3998 | 370 |
|
371 |
"Modified: / 29-08-2017 / 23:09:42 / cg" |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
372 |
! |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
373 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
374 |
read32RLE |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
375 |
"read a 32 bit/pixel rle encoded targa-image; skip alpha channel (for now)" |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
376 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
377 |
|total count dstIndex code n a r g b| |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
378 |
|
3998 | 379 |
data := ByteArray new:((total := width * height * 4)). |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
380 |
count := 0. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
381 |
dstIndex := 1. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
382 |
[count < total] whileTrue:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
383 |
code := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
384 |
n := (code bitAnd:16r7F) + 1. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
385 |
(code bitAnd:16r80) ~~ 0 ifTrue:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
386 |
b := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
387 |
g := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
388 |
r := inStream nextByte. |
3998 | 389 |
a := inStream nextByte. |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
390 |
n timesRepeat:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
391 |
data at:dstIndex put:r. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
392 |
data at:dstIndex+1 put:g. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
393 |
data at:dstIndex+2 put:b. |
3998 | 394 |
data at:dstIndex+3 put:a. |
395 |
dstIndex := dstIndex + 4 |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
396 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
397 |
] ifFalse:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
398 |
n timesRepeat:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
399 |
b := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
400 |
g := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
401 |
r := inStream nextByte. |
3998 | 402 |
a := inStream nextByte. |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
403 |
data at:dstIndex put:r. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
404 |
data at:dstIndex+1 put:g. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
405 |
data at:dstIndex+2 put:b. |
3998 | 406 |
data at:dstIndex+3 put:a. |
407 |
dstIndex := dstIndex + 4 |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
408 |
] |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
409 |
]. |
4002 | 410 |
count := count + (n * 4). |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
411 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
412 |
|
3998 | 413 |
"Created: / 21-04-1997 / 20:43:54 / cg" |
4002 | 414 |
"Modified: / 29-08-2017 / 23:35:48 / cg" |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
415 |
! |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
416 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
417 |
read8 |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
418 |
"read an 8 bit/pixel targa-image" |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
419 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
420 |
data := ByteArray new:(width * height). |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
421 |
inStream nextBytes:(data size) into:data. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
422 |
|
3998 | 423 |
"Created: / 21-04-1997 / 20:12:35 / cg" |
424 |
"Modified: / 29-08-2017 / 23:10:32 / cg" |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
425 |
! |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
426 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
427 |
read8RLE |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
428 |
"read an 8 bit/pixel rle encoded targa-image" |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
429 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
430 |
|total count dstIndex code n byte| |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
431 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
432 |
data := ByteArray new:((total := width * height)). |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
433 |
count := 0. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
434 |
dstIndex := 1. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
435 |
[count < total] whileTrue:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
436 |
code := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
437 |
n := (code bitAnd:16r7F) + 1. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
438 |
(code bitAnd:16r80) ~~ 0 ifTrue:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
439 |
byte := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
440 |
data from:dstIndex to:dstIndex+n-1 put:byte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
441 |
] ifFalse:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
442 |
inStream nextBytes:n into:data startingAt:dstIndex. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
443 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
444 |
count := count + n. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
445 |
dstIndex := dstIndex + n. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
446 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
447 |
|
3998 | 448 |
"Created: / 21-04-1997 / 20:19:46 / cg" |
449 |
"Modified: / 29-08-2017 / 23:10:46 / cg" |
|
1738 | 450 |
! |
451 |
||
452 |
readColorMap:cmapLength |
|
453 |
colorMap := Array new:cmapLength. |
|
454 |
1 to:cmapLength do:[:index | |
|
455 |
|r g b| |
|
456 |
||
457 |
b := inStream nextByte. |
|
458 |
g := inStream nextByte. |
|
459 |
r := inStream nextByte. |
|
460 |
(r isNil or:[g isNil or:[b isNil]]) ifTrue:[ |
|
461 |
^ self fileFormatError:('short file - probably not targa format'). |
|
462 |
]. |
|
463 |
colorMap at:index put:(Color redByte:r greenByte:g blueByte:b). |
|
464 |
]. |
|
465 |
^ colorMap |
|
37 | 466 |
! ! |
467 |
||
1848 | 468 |
!TargaReader methodsFor:'reading'! |
469 |
||
470 |
readImage |
|
471 |
"read a targa-image from aFileName. return the receiver (with all |
|
472 |
relevant instance variables set for the image) or nil on error" |
|
473 |
||
3998 | 474 |
|depth flags lenID hasColorMap |
1848 | 475 |
cmapOffset cmapLength cmapEntrySize xOrg yOrg rle| |
476 |
||
477 |
inStream binary. |
|
478 |
||
479 |
lenID := inStream next. |
|
480 |
hasColorMap := inStream next. |
|
481 |
imageType := inStream next. |
|
482 |
cmapOffset := inStream nextShortMSB:false. |
|
483 |
cmapLength := inStream nextShortMSB:false. |
|
484 |
cmapEntrySize := inStream next. |
|
485 |
xOrg := inStream nextShortMSB:false. |
|
486 |
yOrg := inStream nextShortMSB:false. |
|
487 |
||
488 |
width := inStream nextShortMSB:false. |
|
489 |
height := inStream nextShortMSB:false. |
|
490 |
depth := inStream next. |
|
4002 | 491 |
(#(8 15 16 24 32) includes:depth) ifFalse:[ |
3998 | 492 |
^ self fileFormatError:'unsupported depth: %1' with:depth printString. |
1848 | 493 |
]. |
494 |
depth == 32 ifTrue:[ |
|
495 |
'TargaReader [info]: alpha channel ignored' infoPrintCR. |
|
496 |
] ifFalse:[ |
|
497 |
'TargaReader [info]: depth: ' infoPrint. depth infoPrintCR. |
|
498 |
]. |
|
499 |
||
500 |
"/ MapRGB == 1 |
|
501 |
"/ RawRGB == 2 |
|
502 |
"/ RawMono == 3 |
|
503 |
"/ MapEnCode == 9 |
|
504 |
"/ RawEnCode == 10 |
|
505 |
||
3998 | 506 |
(#(1 2 3 9 10) includes:imageType) ifFalse:[ |
1848 | 507 |
"/ 'TargaReader [warning]: unsupported imageType: ' errorPrint. imageType errorPrintCR. |
3998 | 508 |
^ self fileFormatError:'unsupported imageType: %1' with:imageType printString. |
1848 | 509 |
]. |
510 |
'TargaReader [info]: imageType: ' infoPrint. imageType infoPrintCR. |
|
511 |
||
512 |
self reportDimension. |
|
513 |
||
514 |
"/ flags: |
|
515 |
"/ 0000 xxxx attribute-bits-per-pixel |
|
516 |
"/ 0000 0001 greysc |
|
517 |
"/ 0000 0010 colour |
|
518 |
"/ 0000 0011 mapped |
|
519 |
"/ 0000 0100 rleEncoded |
|
520 |
"/ 0000 1000 interlaced |
|
521 |
"/ 00xx 0000 origin (0 -> lower-left / 1 -> l-r / 2 -> u-l / 3 -> u-r) |
|
522 |
"/ xx00 0000 interleave (0 -> none / 1 -> odd/even / 2 ->4-fould / 3 reserved) |
|
523 |
"/ |
|
524 |
flags := inStream next. |
|
525 |
||
526 |
(flags bitAnd:2r11000000) ~~ 0 ifTrue:[ |
|
3998 | 527 |
^ self fileFormatError:'unsupported interlace: %1' with:flags printString. |
1848 | 528 |
]. |
529 |
||
530 |
rle := flags bitTest:2r000001000. |
|
531 |
flags := flags bitAnd:2r111110111. |
|
532 |
||
533 |
(flags bitAnd:2r00001111) ~~ 0 ifTrue:[ |
|
3998 | 534 |
^ self fileFormatError:'unsupported flags: %1' with:flags printString. |
1848 | 535 |
]. |
536 |
||
537 |
(flags bitAnd:2r00110000) == 16r20 ifTrue:[ |
|
538 |
orientation := #topLeft |
|
539 |
] ifFalse:[ |
|
540 |
(flags bitAnd:2r00110000) == 16r30 ifTrue:[ |
|
541 |
orientation := #topRight |
|
542 |
] ifFalse:[ |
|
543 |
(flags bitAnd:2r00110000) == 16r10 ifTrue:[ |
|
544 |
orientation := #bottomRight |
|
545 |
] ifFalse:[ |
|
546 |
(flags bitAnd:2r00110000) == 0 ifTrue:[ |
|
547 |
orientation := #bottomLeft |
|
548 |
] |
|
549 |
] |
|
550 |
] |
|
551 |
]. |
|
552 |
||
553 |
lenID ~~ 0 ifTrue:[ |
|
554 |
inStream skip:lenID |
|
555 |
]. |
|
556 |
||
557 |
hasColorMap ~~ 0 ifTrue:[ |
|
558 |
"/ read the colorMap |
|
559 |
colorMap := self readColorMap:cmapLength. |
|
560 |
'TargaReader [info]: has colorMap' infoPrintCR. |
|
561 |
]. |
|
562 |
||
563 |
depth == 32 ifTrue:[ |
|
564 |
imageType == 2 ifTrue:[ |
|
3998 | 565 |
"/ rle ifTrue:[self halt:'oops - should not happen']. |
1848 | 566 |
self read32. |
567 |
] ifFalse:[ |
|
3998 | 568 |
"/ rle ifFalse:[self halt:'oops - should not happen']. |
1848 | 569 |
self read32RLE. |
570 |
]. |
|
3998 | 571 |
|
572 |
photometric := #rgba. |
|
573 |
samplesPerPixel := 4. |
|
574 |
bitsPerSample := #(8 8 8 8). |
|
575 |
||
576 |
bytesPerRow := width*4. |
|
577 |
bytesPerPixel := 4. |
|
578 |
]. |
|
579 |
||
580 |
depth == 24 ifTrue:[ |
|
581 |
imageType == 2 ifTrue:[ |
|
582 |
"/ rle ifTrue:[self halt:'oops - should not happen']. |
|
583 |
self read24. |
|
584 |
] ifFalse:[ |
|
585 |
"/ rle ifFalse:[self halt:'oops - should not happen']. |
|
586 |
self read24RLE. |
|
587 |
]. |
|
588 |
||
589 |
photometric := #rgb. |
|
590 |
samplesPerPixel := 3. |
|
591 |
bitsPerSample := #(8 8 8). |
|
592 |
||
1848 | 593 |
bytesPerRow := width*3. |
594 |
bytesPerPixel := 3. |
|
595 |
]. |
|
3998 | 596 |
|
4002 | 597 |
((depth == 15) or:[depth == 16]) ifTrue:[ |
598 |
imageType == 2 ifTrue:[ |
|
599 |
"/ rle ifTrue:[self halt:'oops - should not happen']. |
|
600 |
self read16. |
|
601 |
] ifFalse:[ |
|
602 |
"/ rle ifFalse:[self halt:'oops - should not happen']. |
|
603 |
self read16RLE. |
|
604 |
]. |
|
605 |
||
606 |
photometric := #rgb. |
|
607 |
samplesPerPixel := 3. |
|
608 |
bitsPerSample := #(5 5 5). |
|
609 |
||
610 |
bytesPerRow := width*2. |
|
611 |
bytesPerPixel := 2. |
|
612 |
]. |
|
613 |
||
1848 | 614 |
depth == 8 ifTrue:[ |
3998 | 615 |
(imageType == 1 or:[imageType == 3]) ifTrue:[ |
616 |
"/ rle ifTrue:[self halt:'oops - should not happen']. |
|
1848 | 617 |
self read8. |
618 |
] ifFalse:[ |
|
3998 | 619 |
"/ rle ifFalse:[self halt:'oops - should not happen']. |
1848 | 620 |
self read8RLE |
621 |
]. |
|
3998 | 622 |
|
623 |
(imageType == 3 or:[colorMap isNil]) ifTrue:[ |
|
624 |
photometric := #blackIs0. |
|
625 |
] ifFalse:[ |
|
626 |
photometric := #palette. |
|
627 |
]. |
|
628 |
samplesPerPixel := 1. |
|
629 |
bitsPerSample := #(8). |
|
630 |
||
1848 | 631 |
bytesPerRow := width. |
632 |
bytesPerPixel := 1. |
|
633 |
]. |
|
634 |
||
635 |
self handleImageOrientation. |
|
636 |
||
637 |
" |
|
638 |
TargaReader fromFile:'bitmaps/test.tga' |
|
639 |
" |
|
640 |
||
3998 | 641 |
"Modified: / 13-10-1998 / 19:50:48 / ps" |
4002 | 642 |
"Modified: / 29-08-2017 / 23:41:12 / cg" |
1848 | 643 |
! ! |
644 |
||
210 | 645 |
!TargaReader class methodsFor:'documentation'! |
646 |
||
647 |
version |
|
3998 | 648 |
^ '$Header$' |
4001 | 649 |
! |
650 |
||
651 |
version_CVS |
|
652 |
^ '$Header$' |
|
210 | 653 |
! ! |
1738 | 654 |
|
3998 | 655 |
|
37 | 656 |
TargaReader initialize! |