8 TGAimage* read_tga(const char* path) {
9 FILE* tga_file = fopen(path, "rb");
10 if (tga_file == NULL) {
16 if (fread(&header, sizeof(TGAheader), 1, tga_file) != 1) {
22 GLint image_components;
24 if (header.image_bpp == 32) {
25 image_format = GL_BGRA_EXT;
26 image_components = GL_RGBA8;
28 else if (header.image_bpp == 24) {
29 image_format = GL_BGR_EXT;
30 image_components = GL_RGB8;
32 else if (header.image_bpp == 8) {
33 image_format = GL_LUMINANCE;
34 image_components = GL_LUMINANCE8;
41 unsigned long image_size = header.image_width * header.image_height * (header.image_bpp >> 3);
43 GLbyte* bytes = malloc(image_size * sizeof(GLbyte));
49 if (fread(bytes, image_size, 1, tga_file) != 1) {
57 TGAimage* image = malloc(sizeof(TGAimage));
62 (*image).header = header;
63 (*image).image_format = image_format;
64 (*image).image_components = image_components;
65 (*image).bytes = bytes;