Source code: gl4java/utils/textures/TGATextureLoader.java
1 package gl4java.utils.textures;
2
3 import gl4java.*;
4
5 import java.io.*;
6 import java.net.*;
7
8 /**
9 * This is Class implements a TGA texture-loader !
10 * At this time, this loader only supports
11 * loading files, which are saved with
12 * the TGATextureGrabber !
13 * This means: 24 bpp, RGB, uncompressed, no-colormap,
14 * no image id-field !
15 *
16 * @see IOTextureLoader
17 * @see TextureLoader
18 */
19 public class TGATextureLoader
20 extends IOTextureLoader
21 {
22 public TGATextureLoader(GLFunc gl, GLUFunc glu)
23 {
24 super(gl, glu);
25 }
26
27 protected boolean readTexture(InputStream is)
28 {
29 try {
30 int cc;
31
32 glFormat=GL_RGB;
33
34 DataInputStream reader = new DataInputStream ( is );
35
36 //write TGA header
37 reader.readByte(); //ID length, 0 because no image id field
38 reader.readByte(); //no color map
39 cc = reader.readByte(); //image type (24 bit RGB, uncompressed)
40 if(cc!=2)
41 {
42 reader.close();
43 System.out.println("TGATextureLoader: File is not 24bit RGB Data !");
44 error=true;
45 return false;
46 }
47 reader.readShort(); //color map origin, ignore because no color map
48 reader.readShort(); //color map length, ignore because no color map
49 reader.readByte(); //color map entry size, ignore because no color map
50 reader.readShort(); //x origin
51 reader.readShort(); //y origin
52
53 cc = reader.readByte(); // image width low byte
54 short s = (short)((short)cc & 0x00ff);
55 cc = reader.readByte(); // image width high byte
56 s = (short) ( (short)( ((short)cc & 0x00ff)<<8 ) | s );
57 imageWidth = (int)s;
58
59 cc = reader.readByte(); // image height low byte
60 s = (short)((short)cc & 0x00ff);
61 cc = reader.readByte(); // image height high byte
62 s = (short) ( (short)( ((short)cc & 0x00ff)<<8 ) | s );
63 imageHeight = (int)s;
64
65 cc=reader.readByte(); // 24bpp
66 if(cc!=24)
67 {
68 reader.close();
69 System.out.println("TGATextureLoader: File is not 24bpp Data !");
70 error=true;
71 return false;
72 }
73 reader.readByte(); //description bits
74
75 if(3!=getComponents())
76 {
77 reader.close();
78 System.out.println("TGATextureLoader: Currenly only RGB (24bit) data is supported !");
79 error=true;
80 return false;
81 }
82
83 pixel=new byte[imageWidth * imageHeight * 3];
84
85 //read TGA image data
86 reader.read(pixel, 0, pixel.length);
87
88 //process image data:
89 // TGA pixels should be written in BGR format,
90 // so R en B should be switched
91 byte tmp;
92 for (int i=0; i<imageWidth*imageHeight*3; i+=3)
93 {
94 tmp=pixel[i];
95 pixel[i]=pixel[i+2];
96 pixel[i+2]=tmp;
97 }
98
99 reader.close();
100 setTextureSize();
101 return true;
102
103 } catch (Exception ex) {
104 System.out.println("An exception occured, while loading a TGATexture");
105 System.out.println(ex);
106 error=true;
107 }
108 return false;
109 }
110 }
111