7 TGAimage* read_tga(const char* path) {
8 FILE* tga_file = fopen(path, "rb");
9 if (tga_file == NULL) {
15 if (fread(&header, sizeof(TGAheader), 1, tga_file) != 1) {
21 GLint image_components;
23 if (header.image_bpp == 32) {
24 image_format = GL_BGRA_EXT;
25 image_components = GL_RGBA8;
27 else if (header.image_bpp == 24) {
28 image_format = GL_BGR_EXT;
29 image_components = GL_RGB8;
31 else if (header.image_bpp == 8) {
32 image_format = GL_LUMINANCE;
33 image_components = GL_LUMINANCE8;
40 unsigned long image_size = header.image_width * header.image_height * (header.image_bpp >> 3);
42 GLbyte* bytes = malloc(image_size * sizeof(GLbyte));
48 if (fread(bytes, image_size, 1, tga_file) != 1) {
56 TGAimage* image = malloc(sizeof(TGAimage));
61 (*image).header = header;
62 (*image).image_format = image_format;
63 (*image).image_components = image_components;
64 (*image).bytes = bytes;