aboutsummaryrefslogtreecommitdiffstats
path: root/gl4java/utils/textures/TGATextureLoader.java
blob: b0c915fd259fb6fbee146c11011209db33395cd3 (plain)
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
package gl4java.utils.textures;

import gl4java.*;

import java.io.*;
import java.net.*;

/**
 * This is Class implements a TGA texture-loader !
 * At this time, this loader only supports 
 * loading files, which are saved with
 * the TGATextureGrabber !
 * This means: 24 bpp, RGB, uncompressed, no-colormap,
 *             no image id-field !
 *
 * @see IOTextureLoader
 * @see TextureLoader
 */   
public class TGATextureLoader
extends IOTextureLoader
{
  public TGATextureLoader(GLFunc gl, GLUFunc glu)
  {
	super(gl, glu);
  }

  protected boolean readTexture(InputStream is)
  {
       try {
           int cc;

	   glFormat=GL_RGB;
	    
	   DataInputStream reader = new DataInputStream ( is );

	    //write TGA header
	    reader.readByte(); //ID length, 0 because no image id field
	    reader.readByte(); //no color map
	    cc = reader.readByte(); //image type (24 bit RGB, uncompressed)
	    if(cc!=2)
	    {
		    reader.close();
		    System.out.println("TGATextureLoader: File is not 24bit RGB Data !");
		    error=true;
		    return false;
	    }
	    reader.readShort(); //color map origin, ignore because no color map
	    reader.readShort(); //color map length, ignore because no color map
	    reader.readByte(); //color map entry size, ignore because no color map
	    reader.readShort(); //x origin
	    reader.readShort(); //y origin

	    cc = reader.readByte(); // image width low byte
	    short s = (short)((short)cc & 0x00ff);
	    cc = reader.readByte(); // image width high byte
	    s = (short) ( (short)( ((short)cc & 0x00ff)<<8 ) | s );
	    imageWidth = (int)s;

	    cc = reader.readByte(); // image height low byte
	    s = (short)((short)cc & 0x00ff);
	    cc = reader.readByte(); // image height high byte
	    s = (short) ( (short)( ((short)cc & 0x00ff)<<8 ) | s );
	    imageHeight = (int)s;

	    cc=reader.readByte(); // 24bpp
	    if(cc!=24)
	    {
		    reader.close();
		    System.out.println("TGATextureLoader: File is not 24bpp Data !");
		    error=true;
		    return false;
	    }
	    reader.readByte(); //description bits
	    
	    if(3!=getComponents())
	    {
		    reader.close();
			System.out.println("TGATextureLoader: Currenly only RGB (24bit) data is supported !");
		    error=true;
		    return false;
	    }

	    pixel=new byte[imageWidth * imageHeight * 3];

	    //read TGA image data
	    reader.read(pixel, 0, pixel.length);

	    //process image data: 
	    // TGA pixels should be written in BGR format, 
	    // so R en B should be switched
	    byte tmp;
	    for (int i=0; i<imageWidth*imageHeight*3; i+=3)
	    {
	      tmp=pixel[i];
	      pixel[i]=pixel[i+2];
	      pixel[i+2]=tmp;
	    }
	    
	    reader.close();
	    setTextureSize();
	    return true;

    } catch (Exception ex) {
	    System.out.println("An exception occured, while loading a TGATexture");
	    System.out.println(ex);
	    error=true;
    }
    return false;
  }
}