author | Claus Gittinger <cg@exept.de> |
Tue, 29 Aug 2017 23:23:24 +0200 | |
changeset 4001 | 153c0234371a |
parent 3998 | f38061cec746 |
child 4002 | 91e154c3580d |
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 |
|
37 | 65 |
" |
1738 | 66 |
! |
67 |
||
68 |
format |
|
69 |
" |
|
70 |
Shorts etc. are lsb. |
|
71 |
||
72 |
offset: |
|
73 |
||
74 |
1 lenID byte |
|
75 |
2 hasColorMap byte |
|
76 |
3 imageType byte |
|
77 |
1 MapRGB |
|
78 |
2 RawRGB |
|
79 |
3 RawMono |
|
80 |
9 MapEnCode |
|
81 |
10 RawEnCode |
|
82 |
4, 5 cmapOffset short |
|
83 |
6, 7 cmapLength short |
|
84 |
8 cmapEntrySize short |
|
85 |
9,10 xOrg short |
|
86 |
11,12 yOrg short |
|
87 |
||
88 |
13,14 width |
|
89 |
15,16 height |
|
90 |
17 depth |
|
91 |
18 flags |
|
92 |
0000 xxxx attribute-bits-per-pixel |
|
93 |
0000 0001 greysc |
|
94 |
0000 0010 colour |
|
95 |
0000 0011 mapped |
|
96 |
0000 0100 rleEncoded |
|
97 |
0000 1000 interlaced |
|
98 |
00xx 0000 origin (0 -> lower-left / 1 -> l-r / 2 -> u-l / 3 -> u-r) |
|
99 |
xx00 0000 interleave (0 -> none / 1 -> odd/even / 2 ->4-fould / 3 reserved) |
|
100 |
||
101 |
19.. len lenID-bytes |
|
102 |
.. colorMap cmapLength*3 bytes iff hasColorMap ~~ 0 |
|
103 |
" |
|
37 | 104 |
! ! |
105 |
||
106 |
!TargaReader class methodsFor:'initialization'! |
|
107 |
||
108 |
initialize |
|
210 | 109 |
"tell Image-class, that a new fileReader is present |
398 | 110 |
for the '.tga' extension." |
210 | 111 |
|
647 | 112 |
MIMETypes defineImageType:'image/x-targa' suffix:'tga' reader:self. |
37 | 113 |
|
630 | 114 |
"Modified: 27.6.1997 / 18:39:43 / cg" |
37 | 115 |
! ! |
116 |
||
117 |
!TargaReader class methodsFor:'testing'! |
|
118 |
||
119 |
isValidImageFile:aFileName |
|
120 |
"return true, if aFileName contains a targa-file image" |
|
121 |
||
1577 | 122 |
|aStream w h depth flags imageType ok| |
123 |
||
124 |
ok := true. |
|
37 | 125 |
|
43 | 126 |
aStream := self streamReadingFile:aFileName. |
127 |
aStream isNil ifTrue:[^ false]. |
|
128 |
aStream binary. |
|
1577 | 129 |
|
130 |
aStream next. "lenID" |
|
131 |
aStream next. "hasColorMap" |
|
132 |
imageType := aStream next. |
|
133 |
aStream skip:2. "cmapOffset" |
|
134 |
aStream skip:2. "cmapLength" |
|
135 |
aStream next. "cmapEntrySize" |
|
136 |
aStream skip:2. "xOrg" |
|
137 |
aStream skip:2. "yOrg" |
|
37 | 138 |
|
43 | 139 |
w := aStream nextShortMSB:false. |
140 |
h := aStream nextShortMSB:false. |
|
37 | 141 |
|
43 | 142 |
depth := aStream next. |
143 |
flags := aStream next. |
|
37 | 144 |
|
1577 | 145 |
"/ MapRGB == 1 |
146 |
"/ RawRGB == 2 |
|
147 |
"/ RawMono == 3 |
|
148 |
"/ MapEnCode == 9 |
|
149 |
"/ RawEnCode == 10 |
|
150 |
||
151 |
(#(1 2 3 9 10) includes:imageType) ifFalse:[ |
|
152 |
"/ 'TargaReader [warning]: unsupported imageType: ' errorPrint. imageType errorPrintCR. |
|
153 |
ok := false |
|
154 |
] ifTrue:[ |
|
155 |
(#(8 "16" 24 32) includes:depth) ifFalse:[ |
|
156 |
ok := false |
|
157 |
]. |
|
37 | 158 |
]. |
43 | 159 |
aStream close. |
1577 | 160 |
^ ok |
37 | 161 |
|
162 |
" |
|
163 |
TargaReader isValidImageFile:'bitmaps/test.tga' |
|
164 |
TargaReader isValidImageFile:'bitmaps/garfield.gif' |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
165 |
" |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
166 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
167 |
"Modified: 21.4.1997 / 20:46:52 / cg" |
37 | 168 |
! ! |
169 |
||
1848 | 170 |
!TargaReader methodsFor:'private-reading'! |
37 | 171 |
|
1738 | 172 |
handleImageOrientation |
173 |
|rowIdx startIdx endIdx t| |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
174 |
|
1738 | 175 |
orientation == #topLeft ifTrue:[ |
176 |
^ self. |
|
37 | 177 |
]. |
178 |
||
1738 | 179 |
orientation == #topRight ifTrue:[ |
180 |
"/ flip horizontal |
|
181 |
rowIdx := 1. |
|
182 |
1 to:height do:[:row | |
|
183 |
startIdx := rowIdx. |
|
184 |
endIdx := rowIdx + bytesPerRow - bytesPerPixel. |
|
185 |
1 to:width//2 do:[:x | |
|
186 |
0 to:bytesPerPixel-1 do:[:c | |
|
187 |
t := data at:startIdx+c. |
|
188 |
data at:startIdx+c put:(data at:endIdx+c). |
|
189 |
data at:endIdx+c put:t. |
|
190 |
]. |
|
191 |
startIdx := startIdx + bytesPerPixel. |
|
192 |
endIdx := endIdx - bytesPerPixel. |
|
1065
32a68620ddab
care for EOF while reading colorMap.
Claus Gittinger <cg@exept.de>
parents:
918
diff
changeset
|
193 |
]. |
1738 | 194 |
rowIdx := rowIdx + width. |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
195 |
]. |
1738 | 196 |
^ self. |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
197 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
198 |
|
1738 | 199 |
orientation == #bottomLeft ifTrue:[ |
200 |
"/ flip vertical |
|
201 |
startIdx := 1. |
|
202 |
endIdx := 1 + ((height - 1) * bytesPerRow). |
|
203 |
t := ByteArray new:bytesPerRow. |
|
204 |
1 to:height//2 do:[:row | |
|
205 |
t replaceFrom:1 to:bytesPerRow with:data startingAt:startIdx. |
|
206 |
data replaceFrom:startIdx to:startIdx+bytesPerRow-1 with:data startingAt:endIdx. |
|
207 |
data replaceFrom:endIdx to:endIdx+bytesPerRow-1 with:t startingAt:1. |
|
208 |
startIdx := startIdx + bytesPerRow. |
|
209 |
endIdx := endIdx - bytesPerRow |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
210 |
]. |
1738 | 211 |
^ self. |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
212 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
213 |
|
1738 | 214 |
'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
|
215 |
! |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
216 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
217 |
read24 |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
218 |
"read a 24 bit/pixel targa-image" |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
219 |
|
1738 | 220 |
|totalBytes| |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
221 |
|
1738 | 222 |
totalBytes := width * height * 3. |
223 |
data := ByteArray new:totalBytes. |
|
224 |
inStream nextBytes:totalBytes into:data. |
|
299 | 225 |
|
37 | 226 |
" |
299 | 227 |
mhmh - pixel-byte order is blue-green-red |
228 |
swap blue & red bytes |
|
37 | 229 |
" |
1738 | 230 |
self class swap:totalBytes bytesFromRGB_to_BGR_in:data. |
37 | 231 |
|
3998 | 232 |
"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
|
233 |
! |
37 | 234 |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
235 |
read24RLE |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
236 |
"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
|
237 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
238 |
|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
|
239 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
240 |
data := ByteArray new:((total := width * height * 3)). |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
241 |
count := 0. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
242 |
dstIndex := 1. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
243 |
[count < total] whileTrue:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
244 |
code := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
245 |
n := (code bitAnd:16r7F) + 1. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
246 |
(code bitAnd:16r80) ~~ 0 ifTrue:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
247 |
b := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
248 |
g := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
249 |
r := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
250 |
n timesRepeat:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
251 |
data at:dstIndex put:r. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
252 |
data at:dstIndex+1 put:g. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
253 |
data at:dstIndex+2 put:b. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
254 |
dstIndex := dstIndex + 3 |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
255 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
256 |
] ifFalse:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
257 |
n timesRepeat:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
258 |
b := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
259 |
g := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
260 |
r := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
261 |
data at:dstIndex put:r. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
262 |
data at:dstIndex+1 put:g. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
263 |
data at:dstIndex+2 put:b. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
264 |
dstIndex := dstIndex + 3 |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
265 |
] |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
266 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
267 |
count := count + (n * 3). |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
268 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
269 |
|
3998 | 270 |
"Created: / 21-04-1997 / 20:43:23 / cg" |
271 |
"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
|
272 |
! |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
273 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
274 |
read32 |
3998 | 275 |
"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
|
276 |
|
1738 | 277 |
|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
|
278 |
|
3998 | 279 |
totalBytes := width * height * 4. |
1738 | 280 |
data := ByteArray new:totalBytes. |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
281 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
282 |
dstIndex := 1. |
1738 | 283 |
remainingBytes := totalBytes. |
284 |
[remainingBytes > 0] whileTrue:[ |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
285 |
b := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
286 |
g := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
287 |
r := inStream nextByte. |
3998 | 288 |
a := inStream nextByte. |
1738 | 289 |
data at:dstIndex put:r. |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
290 |
data at:dstIndex+1 put:g. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
291 |
data at:dstIndex+2 put:b. |
3998 | 292 |
data at:dstIndex+3 put:a. |
293 |
dstIndex := dstIndex + 4. |
|
294 |
remainingBytes := remainingBytes - 4. |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
295 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
296 |
|
1738 | 297 |
" |
298 |
TargaReader fromFile:'/phys/exept//unsaved1/pd_stuff/graphic/3d_engines/crystal_space/CS/plugins/video/canvas/sdl/img/move.tga' |
|
299 |
TargaReader fromFile:'/phys/exept/unsaved2/smalltalk/Squeak/croquet/Croquet/Content/Cisco/2520/Textures/2520caseback.tga' |
|
300 |
TargaReader fromFile:'/phys/exept/unsaved2/smalltalk/Squeak/croquet/Croquet/Content/Cisco/2520/Textures/2520dimm1front.tga' |
|
301 |
" |
|
3998 | 302 |
|
303 |
"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
|
304 |
! |
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 |
read32RLE |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
307 |
"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
|
308 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
309 |
|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
|
310 |
|
3998 | 311 |
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
|
312 |
count := 0. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
313 |
dstIndex := 1. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
314 |
[count < total] whileTrue:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
315 |
code := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
316 |
n := (code bitAnd:16r7F) + 1. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
317 |
(code bitAnd:16r80) ~~ 0 ifTrue:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
318 |
b := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
319 |
g := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
320 |
r := inStream nextByte. |
3998 | 321 |
a := inStream nextByte. |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
322 |
n timesRepeat:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
323 |
data at:dstIndex put:r. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
324 |
data at:dstIndex+1 put:g. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
325 |
data at:dstIndex+2 put:b. |
3998 | 326 |
data at:dstIndex+3 put:a. |
327 |
dstIndex := dstIndex + 4 |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
328 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
329 |
] ifFalse:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
330 |
n timesRepeat:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
331 |
b := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
332 |
g := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
333 |
r := inStream nextByte. |
3998 | 334 |
a := inStream nextByte. |
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
335 |
data at:dstIndex put:r. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
336 |
data at:dstIndex+1 put:g. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
337 |
data at:dstIndex+2 put:b. |
3998 | 338 |
data at:dstIndex+3 put:a. |
339 |
dstIndex := dstIndex + 4 |
|
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 |
count := count + (n * 3). |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
343 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
344 |
|
3998 | 345 |
"Created: / 21-04-1997 / 20:43:54 / cg" |
346 |
"Modified: / 29-08-2017 / 23:09:01 / cg" |
|
555
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
347 |
! |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
348 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
349 |
read8 |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
350 |
"read an 8 bit/pixel targa-image" |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
351 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
352 |
data := ByteArray new:(width * height). |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
353 |
inStream nextBytes:(data size) into:data. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
354 |
|
3998 | 355 |
"Created: / 21-04-1997 / 20:12:35 / cg" |
356 |
"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
|
357 |
! |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
358 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
359 |
read8RLE |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
360 |
"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
|
361 |
|
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
362 |
|total count dstIndex code n byte| |
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 |
data := ByteArray new:((total := width * height)). |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
365 |
count := 0. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
366 |
dstIndex := 1. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
367 |
[count < total] whileTrue:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
368 |
code := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
369 |
n := (code bitAnd:16r7F) + 1. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
370 |
(code bitAnd:16r80) ~~ 0 ifTrue:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
371 |
byte := inStream nextByte. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
372 |
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
|
373 |
] ifFalse:[ |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
374 |
inStream nextBytes:n into:data startingAt:dstIndex. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
375 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
376 |
count := count + n. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
377 |
dstIndex := dstIndex + n. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
378 |
]. |
f51ec2cc4482
support RLE, 8 and 32 bit images
Claus Gittinger <cg@exept.de>
parents:
551
diff
changeset
|
379 |
|
3998 | 380 |
"Created: / 21-04-1997 / 20:19:46 / cg" |
381 |
"Modified: / 29-08-2017 / 23:10:46 / cg" |
|
1738 | 382 |
! |
383 |
||
384 |
readColorMap:cmapLength |
|
385 |
colorMap := Array new:cmapLength. |
|
386 |
1 to:cmapLength do:[:index | |
|
387 |
|r g b| |
|
388 |
||
389 |
b := inStream nextByte. |
|
390 |
g := inStream nextByte. |
|
391 |
r := inStream nextByte. |
|
392 |
(r isNil or:[g isNil or:[b isNil]]) ifTrue:[ |
|
393 |
^ self fileFormatError:('short file - probably not targa format'). |
|
394 |
]. |
|
395 |
colorMap at:index put:(Color redByte:r greenByte:g blueByte:b). |
|
396 |
]. |
|
397 |
^ colorMap |
|
37 | 398 |
! ! |
399 |
||
1848 | 400 |
!TargaReader methodsFor:'reading'! |
401 |
||
402 |
readImage |
|
403 |
"read a targa-image from aFileName. return the receiver (with all |
|
404 |
relevant instance variables set for the image) or nil on error" |
|
405 |
||
3998 | 406 |
|depth flags lenID hasColorMap |
1848 | 407 |
cmapOffset cmapLength cmapEntrySize xOrg yOrg rle| |
408 |
||
409 |
inStream binary. |
|
410 |
||
411 |
lenID := inStream next. |
|
412 |
hasColorMap := inStream next. |
|
413 |
imageType := inStream next. |
|
414 |
cmapOffset := inStream nextShortMSB:false. |
|
415 |
cmapLength := inStream nextShortMSB:false. |
|
416 |
cmapEntrySize := inStream next. |
|
417 |
xOrg := inStream nextShortMSB:false. |
|
418 |
yOrg := inStream nextShortMSB:false. |
|
419 |
||
420 |
width := inStream nextShortMSB:false. |
|
421 |
height := inStream nextShortMSB:false. |
|
422 |
depth := inStream next. |
|
423 |
(#(8 "16" 24 32) includes:depth) ifFalse:[ |
|
3998 | 424 |
^ self fileFormatError:'unsupported depth: %1' with:depth printString. |
1848 | 425 |
]. |
426 |
depth == 32 ifTrue:[ |
|
427 |
'TargaReader [info]: alpha channel ignored' infoPrintCR. |
|
428 |
] ifFalse:[ |
|
429 |
'TargaReader [info]: depth: ' infoPrint. depth infoPrintCR. |
|
430 |
]. |
|
431 |
||
432 |
"/ MapRGB == 1 |
|
433 |
"/ RawRGB == 2 |
|
434 |
"/ RawMono == 3 |
|
435 |
"/ MapEnCode == 9 |
|
436 |
"/ RawEnCode == 10 |
|
437 |
||
3998 | 438 |
(#(1 2 3 9 10) includes:imageType) ifFalse:[ |
1848 | 439 |
"/ 'TargaReader [warning]: unsupported imageType: ' errorPrint. imageType errorPrintCR. |
3998 | 440 |
^ self fileFormatError:'unsupported imageType: %1' with:imageType printString. |
1848 | 441 |
]. |
442 |
'TargaReader [info]: imageType: ' infoPrint. imageType infoPrintCR. |
|
443 |
||
444 |
self reportDimension. |
|
445 |
||
446 |
"/ flags: |
|
447 |
"/ 0000 xxxx attribute-bits-per-pixel |
|
448 |
"/ 0000 0001 greysc |
|
449 |
"/ 0000 0010 colour |
|
450 |
"/ 0000 0011 mapped |
|
451 |
"/ 0000 0100 rleEncoded |
|
452 |
"/ 0000 1000 interlaced |
|
453 |
"/ 00xx 0000 origin (0 -> lower-left / 1 -> l-r / 2 -> u-l / 3 -> u-r) |
|
454 |
"/ xx00 0000 interleave (0 -> none / 1 -> odd/even / 2 ->4-fould / 3 reserved) |
|
455 |
"/ |
|
456 |
flags := inStream next. |
|
457 |
||
458 |
(flags bitAnd:2r11000000) ~~ 0 ifTrue:[ |
|
3998 | 459 |
^ self fileFormatError:'unsupported interlace: %1' with:flags printString. |
1848 | 460 |
]. |
461 |
||
462 |
rle := flags bitTest:2r000001000. |
|
463 |
flags := flags bitAnd:2r111110111. |
|
464 |
||
465 |
(flags bitAnd:2r00001111) ~~ 0 ifTrue:[ |
|
3998 | 466 |
^ self fileFormatError:'unsupported flags: %1' with:flags printString. |
1848 | 467 |
]. |
468 |
||
469 |
(flags bitAnd:2r00110000) == 16r20 ifTrue:[ |
|
470 |
orientation := #topLeft |
|
471 |
] ifFalse:[ |
|
472 |
(flags bitAnd:2r00110000) == 16r30 ifTrue:[ |
|
473 |
orientation := #topRight |
|
474 |
] ifFalse:[ |
|
475 |
(flags bitAnd:2r00110000) == 16r10 ifTrue:[ |
|
476 |
orientation := #bottomRight |
|
477 |
] ifFalse:[ |
|
478 |
(flags bitAnd:2r00110000) == 0 ifTrue:[ |
|
479 |
orientation := #bottomLeft |
|
480 |
] |
|
481 |
] |
|
482 |
] |
|
483 |
]. |
|
484 |
||
485 |
lenID ~~ 0 ifTrue:[ |
|
486 |
inStream skip:lenID |
|
487 |
]. |
|
488 |
||
489 |
hasColorMap ~~ 0 ifTrue:[ |
|
490 |
"/ read the colorMap |
|
491 |
colorMap := self readColorMap:cmapLength. |
|
492 |
'TargaReader [info]: has colorMap' infoPrintCR. |
|
493 |
]. |
|
494 |
||
495 |
depth == 32 ifTrue:[ |
|
496 |
imageType == 2 ifTrue:[ |
|
3998 | 497 |
"/ rle ifTrue:[self halt:'oops - should not happen']. |
1848 | 498 |
self read32. |
499 |
] ifFalse:[ |
|
3998 | 500 |
"/ rle ifFalse:[self halt:'oops - should not happen']. |
1848 | 501 |
self read32RLE. |
502 |
]. |
|
3998 | 503 |
|
504 |
photometric := #rgba. |
|
505 |
samplesPerPixel := 4. |
|
506 |
bitsPerSample := #(8 8 8 8). |
|
507 |
||
508 |
bytesPerRow := width*4. |
|
509 |
bytesPerPixel := 4. |
|
510 |
]. |
|
511 |
||
512 |
depth == 24 ifTrue:[ |
|
513 |
imageType == 2 ifTrue:[ |
|
514 |
"/ rle ifTrue:[self halt:'oops - should not happen']. |
|
515 |
self read24. |
|
516 |
] ifFalse:[ |
|
517 |
"/ rle ifFalse:[self halt:'oops - should not happen']. |
|
518 |
self read24RLE. |
|
519 |
]. |
|
520 |
||
521 |
photometric := #rgb. |
|
522 |
samplesPerPixel := 3. |
|
523 |
bitsPerSample := #(8 8 8). |
|
524 |
||
1848 | 525 |
bytesPerRow := width*3. |
526 |
bytesPerPixel := 3. |
|
527 |
]. |
|
3998 | 528 |
|
1848 | 529 |
depth == 8 ifTrue:[ |
3998 | 530 |
(imageType == 1 or:[imageType == 3]) ifTrue:[ |
531 |
"/ rle ifTrue:[self halt:'oops - should not happen']. |
|
1848 | 532 |
self read8. |
533 |
] ifFalse:[ |
|
3998 | 534 |
"/ rle ifFalse:[self halt:'oops - should not happen']. |
1848 | 535 |
self read8RLE |
536 |
]. |
|
3998 | 537 |
|
538 |
(imageType == 3 or:[colorMap isNil]) ifTrue:[ |
|
539 |
photometric := #blackIs0. |
|
540 |
] ifFalse:[ |
|
541 |
photometric := #palette. |
|
542 |
]. |
|
543 |
samplesPerPixel := 1. |
|
544 |
bitsPerSample := #(8). |
|
545 |
||
1848 | 546 |
bytesPerRow := width. |
547 |
bytesPerPixel := 1. |
|
548 |
]. |
|
549 |
||
550 |
self handleImageOrientation. |
|
551 |
||
552 |
" |
|
553 |
TargaReader fromFile:'bitmaps/test.tga' |
|
554 |
" |
|
555 |
||
3998 | 556 |
"Modified: / 13-10-1998 / 19:50:48 / ps" |
557 |
"Modified: / 29-08-2017 / 23:10:23 / cg" |
|
1848 | 558 |
! ! |
559 |
||
210 | 560 |
!TargaReader class methodsFor:'documentation'! |
561 |
||
562 |
version |
|
3998 | 563 |
^ '$Header$' |
4001 | 564 |
! |
565 |
||
566 |
version_CVS |
|
567 |
^ '$Header$' |
|
210 | 568 |
! ! |
1738 | 569 |
|
3998 | 570 |
|
37 | 571 |
TargaReader initialize! |