2 * Copyright 2018-2020 Iwo 'Outfrost' Bujkiewicz
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
14 TgaImage* readTga(const char* path) {
15 FILE* tgaFile = fopen(path, "rb");
16 if (tgaFile == NULL) {
22 if (fread(&header, sizeof(TgaHeader), 1, tgaFile) != 1) {
28 GLint imageComponents;
30 switch (header.imageBpp) {
32 imageFormat = GL_BGRA;
33 imageComponents = GL_RGBA8;
37 imageComponents = GL_RGB8;
40 imageFormat = GL_LUMINANCE;
41 imageComponents = GL_LUMINANCE8;
48 unsigned long imageSize = header.imageWidth * header.imageHeight * (header.imageBpp >> 3);
50 GLbyte* bytes = malloc(imageSize * sizeof(GLbyte));
56 if (fread(bytes, imageSize, 1, tgaFile) != 1) {
64 TgaImage* image = malloc(sizeof(TgaImage));
69 (*image).header = header;
70 (*image).imageFormat = imageFormat;
71 (*image).imageComponents = imageComponents;
72 (*image).bytes = bytes;