author | Claus Gittinger <cg@exept.de> |
Thu, 23 Nov 1995 18:49:05 +0100 | |
changeset 129 | f890eaabc487 |
parent 114 | e577a2f332d0 |
child 210 | 5405de794686 |
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 |
" |
|
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 |
|
114
e577a2f332d0
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
107
diff
changeset
|
39 |
^ '$Header: /cvs/stx/stx/libview2/TargaReader.st,v 1.5 1995-11-11 16:05:17 cg Exp $' |
37 | 40 |
! |
41 |
||
42 |
documentation |
|
43 |
" |
|
44 |
this class provides methods for loading targa-file (tga) images. |
|
45 |
Limitations: |
|
46 |
untested |
|
47 |
only supports 24 bits/pixel format |
|
48 |
Save not supported |
|
49 |
||
50 |
I had two tga files to test this code with - it may not work with |
|
51 |
other targa files. |
|
52 |
Suggestions: adapt & use the pbmplus library here. |
|
53 |
" |
|
54 |
! ! |
|
55 |
||
56 |
!TargaReader class methodsFor:'initialization'! |
|
57 |
||
58 |
initialize |
|
59 |
Image fileFormats at:'.tga' put:self. |
|
60 |
Image fileFormats at:'.TGA' put:self. |
|
61 |
||
62 |
! ! |
|
63 |
||
64 |
!TargaReader class methodsFor:'testing'! |
|
65 |
||
66 |
isValidImageFile:aFileName |
|
67 |
"return true, if aFileName contains a targa-file image" |
|
68 |
||
43 | 69 |
|aStream w h depth flags| |
37 | 70 |
|
43 | 71 |
aStream := self streamReadingFile:aFileName. |
72 |
aStream isNil ifTrue:[^ false]. |
|
73 |
aStream binary. |
|
74 |
aStream skip:12. "/ skip 12 bytes |
|
37 | 75 |
|
43 | 76 |
w := aStream nextShortMSB:false. |
77 |
h := aStream nextShortMSB:false. |
|
37 | 78 |
|
43 | 79 |
depth := aStream next. |
80 |
flags := aStream next. |
|
37 | 81 |
|
82 |
(#("8" 24) includes:depth) ifFalse:[ |
|
43 | 83 |
aStream close. ^ false |
37 | 84 |
]. |
85 |
flags ~~ 16r20 ifTrue:[ |
|
43 | 86 |
aStream close. ^ false |
37 | 87 |
]. |
88 |
||
43 | 89 |
aStream close. |
37 | 90 |
^ true |
91 |
||
92 |
" |
|
93 |
TargaReader isValidImageFile:'bitmaps/test.tga' |
|
94 |
TargaReader isValidImageFile:'bitmaps/garfield.gif' |
|
95 |
" |
|
96 |
! ! |
|
97 |
||
98 |
!TargaReader methodsFor:'reading from file'! |
|
99 |
||
43 | 100 |
fromStream:aStream |
37 | 101 |
"read a targa-image from aFileName. return the receiver (with all |
102 |
relevant instance variables set for the image) or nil on error" |
|
103 |
||
107 | 104 |
|depth flags nBytes ok| |
37 | 105 |
|
43 | 106 |
inStream := aStream. |
107 |
aStream binary. |
|
108 |
||
109 |
aStream skip:12. |
|
110 |
width := aStream nextShortMSB:false. |
|
111 |
height := aStream nextShortMSB:false. |
|
112 |
depth := aStream next. |
|
113 |
depth ~~ 24 ifTrue:[ |
|
114 |
'TARGA: unsupported depth' errorPrintNL. |
|
37 | 115 |
^ nil |
116 |
]. |
|
43 | 117 |
flags := aStream next. |
37 | 118 |
flags ~~ 16r20 ifTrue:[ |
119 |
^ false |
|
120 |
]. |
|
121 |
||
122 |
data := ByteArray new:(width * height * (depth / 8)). |
|
43 | 123 |
aStream nextBytes:(data size) into:data. |
37 | 124 |
" |
125 |
mhmh - order is blue-green-red |
|
126 |
" |
|
107 | 127 |
nBytes := data size. |
128 |
ok := false. |
|
129 |
||
130 |
%{ /* OPTIONAL */ |
|
131 |
if (__isByteArray(_INST(data))) { |
|
132 |
int lastIndex = __intVal(nBytes) - 2; |
|
133 |
unsigned char *cp = __ByteArrayInstPtr(_INST(data))->ba_element; |
|
134 |
int i; |
|
135 |
unsigned char t; |
|
136 |
||
137 |
for (i=0; i<lastIndex; i+=3, cp+=3) { |
|
138 |
t = cp[0]; |
|
139 |
cp[0] = cp[2]; |
|
140 |
cp[2] = t; |
|
141 |
} |
|
142 |
ok = true; |
|
143 |
} |
|
144 |
%}. |
|
145 |
ok ifFalse:[ |
|
146 |
1 to:(data size - 2) by:3 do:[:i | |
|
147 |
|t| |
|
148 |
t := data at:i. |
|
149 |
data at:i put:(data at:i+2). |
|
150 |
data at:i+2 put:t |
|
151 |
] |
|
37 | 152 |
]. |
153 |
||
154 |
photometric := #rgb. |
|
155 |
samplesPerPixel := 3. |
|
156 |
bitsPerSample := #(8 8 8). |
|
157 |
||
158 |
" |
|
159 |
TargaReader fromFile:'bitmaps/test.tga' |
|
160 |
" |
|
161 |
! ! |
|
162 |
||
163 |
TargaReader initialize! |