aboutsummaryrefslogtreecommitdiffstats
path: root/gl4java/utils/textures/TGATextureGrabber.java
blob: 2d1c432c9eede1a79f887884442eb77f12551b54 (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
package gl4java.utils.textures;

import gl4java.*;

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

public class TGATextureGrabber
extends TextureGrabber
{
  public TGATextureGrabber(GLFunc gl)
  {
    super(gl);
  }

  public boolean write2File(OutputStream os)
  {
    try {
	    DataOutputStream fout= new DataOutputStream(os);
	    
	    //write TGA header
	    fout.writeByte(0); //ID length, 0 because no image id field
	    fout.writeByte(0); //no color map
	    fout.writeByte(2); //image type (24 bit RGB, uncompressed)
	    fout.writeShort(0); //color map origin, ignore because no color map
	    fout.writeShort(0); //color map length, ignore because no color map
	    fout.writeByte(0); //color map entry size, ignore because no color map
	    fout.writeShort(0); //x origin
	    fout.writeShort(0); //y origin
	    short s = (short)width;
	    fout.writeByte((byte)(s & 0x00ff));      //image width low byte
	    fout.writeByte((byte)((s & 0xff00)>>8)); //image width high byte
	    s = (short)height;
	    fout.writeByte((byte)(s & 0x00ff));      //image height low byte
	    fout.writeByte((byte)((s & 0xff00)>>8)); //image height high byte
	    fout.writeByte(24); //bpp
	    fout.writeByte(0); //description bits
	    
	    //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<(width*height*3); i+=3) {
	      tmp=pixels[i];
	      pixels[i]=pixels[i+2];
	      pixels[i+2]=tmp;
	    }
	    
	    //write TGA image data
	    fout.write(pixels, 0, pixels.length);
	    
	    fout.flush();
	    fout.close();
    } catch (Exception ex) {
    	System.out.println("TGATextureGrabber.write2File <os> failed !\n"+ex);
	return false;
    }
    return true;
  }
}