1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
/*
* Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistribution of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
* MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
* ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
* DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
* DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
* SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed or intended for use
* in the design, construction, operation or maintenance of any nuclear
* facility.
*
* Sun gratefully acknowledges that this software was originally authored
* and developed by Kenneth Bradley Russell and Christopher John Kline.
*/
package net.java.games.util;
import java.io.*;
import net.java.games.jogl.*;
/**
* Targa image reader adapted from sources of the <a href =
* "http://java.sun.com/products/jimi/">Jimi</a> image I/O class library.
*
* <P>
*
* Image decoder for image data stored in TGA file format.
* Currently only the original TGA file format is supported. This is
* because the new TGA format has data at the end of the file, getting
* to the end of a file in an InputStream orient environment presents
* several difficulties which are avoided at the moment.
*
* <P>
*
* This is a simple decoder and is only setup to load a single image
* from the input stream
*
* <P>
*
* @author Robin Luiten
* @author Kenneth Russell
* @version $Revision$
*/
public class TGAImage {
private Header header;
private int format;
private byte[] data;
private TGAImage(Header header) {
this.header = header;
}
/**
* This class reads in all of the TGA image header in addition it also
* reads in the imageID field as it is convenient to handle that here.
*
* @author Robin Luiten
* @version 1.1
*/
public static class Header {
/** Set of possible file format TGA types */
public final static int TYPE_NEW = 0;
public final static int TYPE_OLD = 1;
public final static int TYPE_UNK = 2; // cant rewind stream so unknown for now.
/** Set of possible image types in TGA file */
public final static int NO_IMAGE = 0; // no image data
public final static int UCOLORMAPPED = 1; // uncompressed color mapped image
public final static int UTRUECOLOR = 2; // uncompressed true color image
public final static int UBLACKWHITE = 3; // uncompressed black and white image
public final static int COLORMAPPED = 9; // compressed color mapped image
public final static int TRUECOLOR = 10; // compressed true color image
public final static int BLACKWHITE = 11; // compressed black and white image
/** Field image descriptor bitfield values definitions */
public final static int ID_ATTRIBPERPIXEL = 0xF;
public final static int ID_LEFTTORIGHT = 0x10;
public final static int ID_TOPTOBOTTOM = 0x20;
public final static int ID_INTERLEAVE = 0xC0;
/** Field image descriptor / interleave values */
public final static int I_NOTINTERLEAVED = 0;
public final static int I_TWOWAY = 1;
public final static int I_FOURWAY = 2;
/** Type of this TGA file format */
private int tgaType;
/** initial TGA image data fields */
private int idLength; // byte value
private int colorMapType; // byte value
private int imageType; // byte value
/** TGA image colour map fields */
private int firstEntryIndex;
private int colorMapLength;
private byte colorMapEntrySize;
/** TGA image specification fields */
private int xOrigin;
private int yOrigin;
private int width;
private int height;
private byte pixelDepth;
private byte imageDescriptor;
/** bitfields in imageDescriptor */
private byte attribPerPixel; // how many attribute bits per pixel
private boolean leftToRight; // order of data on scan line
private boolean topToBottom; // order scan lines stored
private byte interleave; // how rows are stored in image data
private byte[] imageIDbuf;
private String imageID;
public Header(LEDataInputStream in) throws IOException {
int ret;
tgaType = TYPE_OLD; // dont try and get footer.
// initial header fields
idLength = in.readUnsignedByte();
colorMapType = in.readUnsignedByte();
imageType = in.readUnsignedByte();
// color map header fields
firstEntryIndex = in.readUnsignedShort();
colorMapLength = in.readUnsignedShort();
colorMapEntrySize = in.readByte();
// TGA image specification fields
xOrigin = in.readUnsignedShort();
yOrigin = in.readUnsignedShort();
width = in.readUnsignedShort();
height = in.readUnsignedShort();
pixelDepth = in.readByte();
imageDescriptor = in.readByte();
attribPerPixel = (byte)(imageDescriptor & ID_ATTRIBPERPIXEL);
leftToRight = (imageDescriptor & ID_LEFTTORIGHT) != 0; // not used ?
topToBottom = (imageDescriptor & ID_TOPTOBOTTOM) != 0;
interleave = (byte)((imageDescriptor & ID_INTERLEAVE) >> 6);
if (idLength > 0) {
imageIDbuf = new byte[idLength];
in.read(imageIDbuf, 0, idLength);
imageID = new String(imageIDbuf, "US-ASCII");
}
}
public int tgaType() { return tgaType; }
/** initial TGA image data fields */
public int idLength() { return idLength; }
public int colorMapType() { return colorMapType; }
public int imageType() { return imageType; }
/** TGA image colour map fields */
public int firstEntryIndex() { return firstEntryIndex; }
public int colorMapLength() { return colorMapLength; }
public byte colorMapEntrySize() { return colorMapEntrySize; }
/** TGA image specification fields */
public int xOrigin() { return xOrigin; }
public int yOrigin() { return yOrigin; }
public int width() { return width; }
public int height() { return height; }
public byte pixelDepth() { return pixelDepth; }
public byte imageDescriptor() { return imageDescriptor; }
/** bitfields in imageDescriptor */
public byte attribPerPixel() { return attribPerPixel; }
public boolean leftToRight() { return leftToRight; }
public boolean topToBottom() { return topToBottom; }
public byte interleave() { return interleave; }
public byte[] imageIDbuf() { return imageIDbuf; }
public String imageID() { return imageID; }
public String toString() {
return "TGA Header " +
" id length: " + idLength +
" color map type: "+ colorMapType +
" image type: "+ imageType +
" first entry index: " + firstEntryIndex +
" color map length: " + colorMapLength +
" color map entry size: " + colorMapEntrySize +
" x Origin: " + xOrigin +
" y Origin: " + yOrigin +
" width: "+ width +
" height: "+ height +
" pixel depth: "+ pixelDepth +
" image descriptor: "+ imageDescriptor +
(imageIDbuf == null ? "" : (" ID String: " + imageID));
}
}
/**
* Identifies the image type of the tga image data and loads
* it into the JimiImage structure. This was taken from the
* prototype and modified for the new Jimi structure
*/
private void decodeImage(LEDataInputStream dIn) throws IOException {
switch (header.imageType()) {
case Header.UCOLORMAPPED:
throw new IOException("TGADecoder Uncompressed Colormapped images not supported");
case Header.UTRUECOLOR: // pixelDepth 15, 16, 24 and 32
switch (header.pixelDepth) {
case 16:
throw new IOException("TGADecoder Compressed 16-bit True Color images not supported");
case 24:
case 32:
decodeRGBImageU24_32(dIn);
break;
}
break;
case Header.UBLACKWHITE:
throw new IOException("TGADecoder Uncompressed Grayscale images not supported");
case Header.COLORMAPPED:
throw new IOException("TGADecoder Compressed Colormapped images not supported");
case Header.TRUECOLOR:
throw new IOException("TGADecoder Compressed True Color images not supported");
case Header.BLACKWHITE:
throw new IOException("TGADecoder Compressed Grayscale images not supported");
}
}
/**
* This assumes that the body is for a 24 bit or 32 bit for a
* RGB or ARGB image respectively.
*/
private void decodeRGBImageU24_32(LEDataInputStream dIn) throws IOException {
int i; // row index
int j; // column index
int y; // output row index
int raw; // index through the raw input buffer
int rawWidth = header.width() * (header.pixelDepth() / 8);
byte[] rawBuf = new byte[rawWidth];
data = new byte[rawWidth * header.height()];
if (header.pixelDepth() == 24) {
format = GL.GL_BGR_EXT;
} else {
assert header.pixelDepth() == 32;
format = GL.GL_BGRA_EXT;
}
for (i = 0; i < header.height(); ++i) {
dIn.readFully(rawBuf, 0, rawWidth);
if (header.topToBottom)
y = header.height - i - 1; // range 0 to (header.height - 1)
else
y = i;
System.arraycopy(rawBuf, 0, data, y * rawWidth, rawBuf.length);
}
}
public int getWidth() { return header.width(); }
public int getHeight() { return header.height(); }
/** Returns the OpenGL format for this texture; e.g. GL.GL_BGR_EXT or GL.GL_BGRA_EXT. */
public int getGLFormat() { return format; }
/** Returns the raw data for this texture in the correct
(bottom-to-top) order for calls to glTexImage2D. */
public byte[] getData() { return data; }
public static TGAImage read(String filename) throws IOException {
LEDataInputStream dIn = new LEDataInputStream(new BufferedInputStream(new FileInputStream(filename)));
Header header = new Header(dIn);
TGAImage res = new TGAImage(header);
res.decodeImage(dIn);
return res;
}
}
|