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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
/*
* Copyright (c) 2003-2005 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
* MICROSYSTEMS, 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 com.sun.opengl.util.texture.spi;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import javax.media.opengl.*;
import com.sun.opengl.util.*;
import com.sun.opengl.util.texture.spi.*;
import com.sun.opengl.util.texture.*;
/**
* Targa image reader and writer 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 int bpp;
private ByteBuffer 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_RIGHTTOLEFT = 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;
private byte[] imageIDbuf;
private String imageID;
// For construction from user data
Header() {
tgaType = TYPE_OLD; // dont try and get footer.
}
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();
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 (byte)(imageDescriptor & ID_ATTRIBPERPIXEL); }
public boolean rightToLeft() { return ((imageDescriptor & ID_RIGHTTOLEFT) != 0); }
public boolean topToBottom() { return ((imageDescriptor & ID_TOPTOBOTTOM) != 0); }
public byte interleave() { return (byte)((imageDescriptor & ID_INTERLEAVE) >> 6); }
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));
}
public int size() { return 18 + idLength; }
// buf must be in little-endian byte order
private void write(ByteBuffer buf) {
buf.put((byte) idLength);
buf.put((byte) colorMapType);
buf.put((byte) imageType);
buf.putShort((short) firstEntryIndex);
buf.putShort((short) colorMapLength);
buf.put((byte) colorMapEntrySize);
buf.putShort((short) xOrigin);
buf.putShort((short) yOrigin);
buf.putShort((short) width);
buf.putShort((short) height);
buf.put((byte) pixelDepth);
buf.put((byte) imageDescriptor);
if (idLength > 0) {
try {
byte[] chars = imageID.getBytes("US-ASCII");
buf.put(chars);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}
}
/**
* 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];
byte[] tmpData = new byte[rawWidth * header.height()];
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, tmpData, y * rawWidth, rawBuf.length);
}
if (header.pixelDepth() == 24) {
bpp=3;
if(GLProfile.isGL2()) {
format = GL2.GL_BGR;
} else {
format = GL.GL_RGB;
swapBGR(tmpData, rawWidth, header.height(), bpp);
}
} else {
assert header.pixelDepth() == 32;
bpp=4;
if(GLProfile.isGL2()) {
format = GL2.GL_BGRA;
} else {
format = GL.GL_RGBA;
swapBGR(tmpData, rawWidth, header.height(), bpp);
}
}
data = ByteBuffer.wrap(tmpData);
}
private static void swapBGR(byte[] data, int bWidth, int height, int bpp) {
byte r,b;
int k;
for(int i=0; i<height; ++i) {
for(int j=0; j<bWidth; j+=bpp) {
k=i*bWidth+j;
b=data[k+0];
r=data[k+2];
data[k+0]=r;
data[k+2]=b;
}
}
}
/** Returns the width of the image. */
public int getWidth() { return header.width(); }
/** Returns the height of the image. */
public int getHeight() { return header.height(); }
/** Returns the OpenGL format for this texture; e.g. GL.GL_BGR or GL.GL_BGRA. */
public int getGLFormat() { return format; }
/** Returns the bytes per pixel */
public int getBytesPerPixel() { return bpp; }
/** Returns the raw data for this texture in the correct
(bottom-to-top) order for calls to glTexImage2D. */
public ByteBuffer getData() { return data; }
/** Reads a Targa image from the specified file. */
public static TGAImage read(String filename) throws IOException {
return read(new FileInputStream(filename));
}
/** Reads a Targa image from the specified InputStream. */
public static TGAImage read(InputStream in) throws IOException {
LEDataInputStream dIn = new LEDataInputStream(new BufferedInputStream(in));
Header header = new Header(dIn);
TGAImage res = new TGAImage(header);
res.decodeImage(dIn);
return res;
}
/** Writes the image in Targa format to the specified file name. */
public void write(String filename) throws IOException {
write(new File(filename));
}
/** Writes the image in Targa format to the specified file. */
public void write(File file) throws IOException {
FileOutputStream stream = new FileOutputStream(file);
FileChannel chan = stream.getChannel();
ByteBuffer buf = ByteBuffer.allocate(header.size());
buf.order(ByteOrder.LITTLE_ENDIAN);
header.write(buf);
buf.rewind();
chan.write(buf);
chan.write(data);
data.rewind();
chan.force(true);
chan.close();
stream.close();
}
/** Creates a TGAImage from data supplied by the end user. Shares
data with the passed ByteBuffer. Assumes the data is already in
the correct byte order for writing to disk, i.e., BGR or
BGRA. */
public static TGAImage createFromData(int width,
int height,
boolean hasAlpha,
boolean topToBottom,
ByteBuffer data) {
Header header = new Header();
header.imageType = Header.UTRUECOLOR;
header.width = width;
header.height = height;
header.pixelDepth = (byte) (hasAlpha ? 32 : 24);
header.imageDescriptor = (byte) (topToBottom ? Header.ID_TOPTOBOTTOM : 0);
// Note ID not supported
TGAImage ret = new TGAImage(header);
ret.data = data;
return ret;
}
}
|