tga & normals
[csRadar.git] / csrTga.h
1 #pragma pack(push, 1)
2 struct tga_header
3 {
4 char idlength;
5 char colourmaptype;
6 char datatypecode;
7 short int colourmaporigin;
8 short int colourmaplength;
9 char colourmapdepth;
10 short int x_origin;
11 short int y_origin;
12 short width;
13 short height;
14 char bitsperpixel;
15 char imagedescriptor;
16 };
17 #pragma pack(pop)
18
19 // Requires RGBA data. Can write grayscale (comp=1), RGB (comp=3) or RGBA (comp=4)
20 int csr_tga_write( const char *path, u32 width, u32 height, int comp, u8 *rgba )
21 {
22 FILE *fp = fopen( path, "wb" );
23
24 if( fp )
25 {
26 struct tga_header header =
27 {
28 .datatypecode = 2,
29 .width = width,
30 .height = height,
31 .bitsperpixel = comp * 8,
32 .imagedescriptor = 0x20
33 };
34
35 fwrite( &header, sizeof( struct tga_header ), 1, fp );
36
37 for( u32 i = 0; i < width * height; i ++ )
38 {
39 u8 *colour = rgba + i*4;
40 u8 flipped[4];
41
42 if( comp >= 3 )
43 {
44 flipped[0] = colour[2];
45 flipped[1] = colour[1];
46 flipped[2] = colour[0];
47 flipped[3] = colour[3];
48
49 colour = flipped;
50 }
51
52 fwrite( colour, 1, comp, fp );
53 }
54
55 fclose( fp );
56 return 1;
57 }
58 else
59 {
60 return 0;
61 }
62 }