123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- /*!
- * \file src/utils/tga.cpp
- * \brief TGA image reader/writer
- *
- * \todo type should pass more info (2 bits for RGBA|RGB|GREY; val for depth)
- *
- * \author Mongoose
- * \author xythobuz
- */
-
- #include <stdio.h>
- #include <string.h>
- #include <stdarg.h>
-
- #include "global.h"
- #include "utils/tga.h"
-
- typedef enum {
- //TGA_TYPE__NO_DATA = 0,
- //TGA_TYPE__MAPPED = 1,
- TGA_TYPE__COLOR = 2,
- TGA_TYPE__GREYSCALE = 3,
- //TGA_TYPE__MAPPED_RLE = 9,
- TGA_TYPE__COLOR_RLE = 10
- } tga_type_t;
-
- typedef struct {
- unsigned char comment_lenght; //!< Number of bytes in comment
- unsigned char colormap_type; //!< 0 No colormap; 1 Has colormap
- unsigned char image_type; //!< 1 Colormapped; 2 Unmapped; 9 Colormapped RLE; 10 Unmapped RLE
- unsigned short colormap_index; //!< Index of first color map entry
- unsigned short colormap_lenght; //!< Number of color map entries
- unsigned char colormap_bbp; //!< 16, 24, or 32 bits per pixel format
- unsigned short origin_x; //!< X coor of lower-left corner
- unsigned short origin_y; //!< Y coor of lower-left corner
- unsigned short width; //!< Width in pixels
- unsigned short height; //!< Height in pixels
- unsigned char bpp; //!< Number of bits in a pixel index
- unsigned char desc_flags; //!< Various magic bits
- } tga_t;
-
- int tgaCheck(const char *filename) {
- char buffer[10];
-
- assert(filename != NULL);
- assert(filename[0] != '\0');
-
- FILE *f = fopen(filename, "rb");
- if (!f) {
- printf("tgaCheck> File not found\n");
- return -1;
- }
-
- /* Read the header */
- fseek(f, 0, SEEK_SET);
- fread(buffer, 8, 1, f);
-
- // buffer[1] = 0 - Means not color mapped (1 would mean mapped)
- if (!(buffer[1] == 0 && (buffer[2] == TGA_TYPE__COLOR ||
- //buffer[2] == TGA_TYPE__GREYSCALE ||
- buffer[2] == TGA_TYPE__COLOR_RLE))) {
- printf("tgaCheck> Inavlid or unknown TGA format.\n");
- fclose(f);
- return -2;
- }
-
- fclose(f);
- return 0;
- }
-
- int tgaLoad(const char *filename, unsigned char **image, unsigned int *width, unsigned int *height, char *type) {
- tga_t header;
- char comment[256];
- unsigned char pixel[4];
- unsigned char *swap_row = NULL;
- unsigned char tmp, packet;
- bool must_flip = 0;
- unsigned int size;
- unsigned int i, j;
-
- assert(filename != NULL);
- assert(filename[0] != '\0');
- assert(image != NULL);
- assert(width != NULL);
- assert(height != NULL);
- assert(type != NULL);
-
- FILE *f = fopen(filename, "rb");
- if (!f) {
- printf("tgaLoad> File not found\n");
- return -1;
- }
-
- fseek(f, 0, SEEK_SET);
-
- // Read TGA header
- fread(&header.comment_lenght, 1, 1, f);
- fread(&header.colormap_type, 1, 1, f);
- fread(&header.image_type, 1, 1, f);
- fread(&header.colormap_index, 2, 1, f);
- fread(&header.colormap_lenght, 2, 1, f);
- fread(&header.colormap_bbp, 1, 1, f);
-
- fread(&tmp, 1, 1, f);
- header.origin_x = tmp;
- fread(&tmp, 1, 1, f);
- header.origin_x += tmp * 256;
-
- fread(&tmp, 1, 1, f);
- header.origin_y = tmp;
- fread(&tmp, 1, 1, f);
- header.origin_y += tmp * 256;
-
- fread(&tmp, 1, 1, f);
- header.width = tmp;
- fread(&tmp, 1, 1, f);
- header.width += tmp * 256;
-
- fread(&tmp, 1, 1, f);
- header.height = tmp;
- fread(&tmp, 1, 1, f);
- header.height += tmp * 256;
-
- fread(&header.bpp, 1, 1, f);
- fread(&header.desc_flags, 1, 1, f);
-
- *width = header.width;
- *height = header.height;
-
- switch (header.bpp) {
- case 32:
- *type = 2; //32;
- break;
- case 24:
- *type = 1; //24;
- break;
- case 8:
- default:
- *type = 0; //8;
- break;
- }
-
- #ifdef DEBUG_TGA
- printf("TGA [%ix%i@%ibpp, %it, %ix, %iy, %uf]\n",
- header.width, header.height, header.bpp, header.image_type,
- header.origin_x, header.origin_y,
- header.desc_flags);
- #endif
-
- // Comments can be 0 - 255
- if (header.comment_lenght) {
- fread(&comment, 1, header.comment_lenght, f);
- for (i = 0; i < 255; ++i) {
- if (!(comment[i] > 32 && comment[i] < 127))
- comment[i] = 183; // print a dot for invalid text
- }
- comment[255] = 0;
- printf("Comment: '%s'\n", comment);
- }
-
- *image = NULL;
- size = header.width * header.height;
-
- if (!size || (!(header.colormap_type == 0 && (header.image_type == 2 || header.image_type == 10)))) {
- fprintf(stderr, "tgaLoad> Unknown image format.\n");
- fclose(f);
- return -2;
- }
-
- // Mongoose: Added 'screen origin bit' support back here
- if (!(header.desc_flags & 32)) {
- must_flip = true;
- }
-
- switch (header.bpp) {
- case 32:
- size *= 4;
- *image = new unsigned char [size];
- switch (header.image_type) {
- case TGA_TYPE__COLOR_RLE:
- for (i = 0; i < size;) {
- fread(&packet, 1, 1, f);
- if (packet & 0x80) { // Run Length
- packet = (packet &0x7F) + 1;
- fread(&pixel, 4, 1, f);
- for (j = 0; j < packet; j++) {
- (*image)[i++] = pixel[2];
- (*image)[i++] = pixel[1];
- (*image)[i++] = pixel[0];
- (*image)[i++] = pixel[3];
- }
- } else { // RAW
- packet = (packet &0x7F) + 1;
- for (j = 0; j < packet; j++) {
- fread(&pixel, 4, 1, f);
- (*image)[i++] = pixel[2];
- (*image)[i++] = pixel[1];
- (*image)[i++] = pixel[0];
- (*image)[i++] = pixel[3];
- }
- }
- }
- break;
- case TGA_TYPE__COLOR:
- if (fread((*image), size, 1, f) < 1) {
- fprintf(stderr, "tgaLoad> Image fread failed.\n");
- delete [] *image;
- fclose(f);
- return -4;
- }
- for (i = 0; i < size; i += 4) {
- tmp = (*image)[i];
- (*image)[i] = (*image)[i + 2];
- (*image)[i + 2] = tmp;
- }
- break;
- default:
- ;
- }
- if (must_flip) {
- swap_row = new unsigned char [header.width * 4];
- for (i = 0, j = header.height-1; (int)i < header.height/2; i++, j--) {
- memcpy(swap_row, &(*image)[i*header.width*4], header.width*4);
- memcpy(&(*image)[i*header.width*4], &(*image)[j*header.width*4],
- header.width*4);
- memcpy(&(*image)[j*header.width*4], swap_row, header.width*4);
- }
- delete [] swap_row;
- }
- break;
- case 24:
- size *= 3;
- *image = new unsigned char [size];
- switch (header.image_type) {
- case TGA_TYPE__COLOR_RLE:
- for (i = 0; i < size;) {
- fread(&packet, 1, 1, f);
- if (packet & 0x80) { // Run Length
- packet = (packet &0x7F) + 1;
- fread(&pixel, 3, 1, f);
- for (j = 0; j < packet; j++) {
- (*image)[i++] = pixel[2];
- (*image)[i++] = pixel[1];
- (*image)[i++] = pixel[0];
- }
- } else { // RAW
- packet = (packet &0x7F) + 1;
- for (j = 0; j < packet; j++) {
- fread(&pixel, 3, 1, f);
- (*image)[i++] = pixel[2];
- (*image)[i++] = pixel[1];
- (*image)[i++] = pixel[0];
- }
- }
- }
- break;
- case TGA_TYPE__COLOR:
- if (fread((*image), size, 1, f) < 1) {
- fprintf(stderr, "tgaLoad> Image fread failed.\n");
- delete [] *image;
- fclose(f);
- return -4;
- }
- for (i = 0; i < size; i += 3) {
- tmp = (*image)[i];
- (*image)[i] = (*image)[i + 2];
- (*image)[i + 2] = tmp;
- }
- break;
- default:
- ;
- }
- if (must_flip) {
- swap_row = new unsigned char [header.width * 3];
- for (i = 0, j = header.height - 1; (int)i < header.height / 2; i++, j--) {
- memcpy(swap_row, &(*image)[i*header.width*3], header.width*3);
- memcpy(&(*image)[i*header.width*3], &(*image)[j*header.width*3],
- header.width*3);
- memcpy(&(*image)[j*header.width*3], swap_row, header.width*3);
- }
- delete [] swap_row;
- }
- break;
- case 8:
- printf("tgaLoad> 8bpp Not implemented\n");
- break;
- default:
- ;
- }
-
- #ifdef DEBUG_TGA
- char c;
- printf("TGA Comment: ");
- while (fread(&c, 1, 1, f) == 1) {
- printf("%c", c);
- }
- printf("\n");
- #endif
-
- fclose(f);
- return 0;
- }
-
- int tgaSave(const char *filename, unsigned char *image, unsigned int width, unsigned int height, char type) {
- tga_t header;
- unsigned int size;
- char comment[64];
- //unsigned int i;
- //unsigned char tmp;
-
- assert(filename != NULL);
- assert(filename[0] != '\0');
- assert(image != NULL);
- assert(width > 0);
- assert(height > 0);
-
- FILE *f = fopen(filename, "wb");
- if (!f) {
- printf("tgaSave> File not found\n");
- return -1;
- }
-
- strncpy(comment, "OpenRaider TGA", 63);
- comment[63] = 0;
-
- header.comment_lenght = (unsigned char)strlen(comment);
- header.colormap_type = 0;
-
- // No colormaps
- header.colormap_index = 0;
- header.colormap_lenght = 0;
- header.colormap_bbp = 0;
-
- header.origin_x = header.origin_y = 0;
- header.width = (unsigned short)width;
- header.height = (unsigned short)height;
-
- header.desc_flags = 0;
-
- switch (type) {
- case 4:
- header.image_type = TGA_TYPE__COLOR;
- header.desc_flags = 32;
- header.bpp = 32;
- break;
- case 2:
- header.bpp = 32;
- break;
- case 1:
- header.image_type = TGA_TYPE__GREYSCALE;
- header.bpp = 8;
- break;
- default:
- header.image_type = TGA_TYPE__COLOR;
- header.bpp = 24;
- }
-
- // Write TGA header
- fwrite(&header.comment_lenght, 1, 1, f);
- fwrite(&header.colormap_type, 1, 1, f);
- fwrite(&header.image_type, 1, 1, f);
- fwrite(&header.colormap_index, 2, 1, f);
- fwrite(&header.colormap_lenght, 2, 1, f);
- fwrite(&header.colormap_bbp, 1, 1, f);
- fwrite(&header.origin_x, 2, 1, f);
- fwrite(&header.origin_y, 2, 1, f);
- fwrite(&header.width, 2, 1, f);
- fwrite(&header.height, 2, 1, f);
- fwrite(&header.bpp, 1, 1, f);
- fwrite(&header.desc_flags, 1, 1, f);
-
- // Write comment
- fwrite(&comment, 1, header.comment_lenght, f);
-
- switch (header.bpp) {
- case 32:
- size = header.width * header.height * 4;
-
- //for (i = 0; i < size; i += 4)
- //{
- // tmp = image[i];
- // image[i] = image[i + 2];
- // image[i + 2] = tmp;
- //}
- break;
- case 24:
- size = header.width * header.height * 3;
-
- //for (i = 0; i < size; i += 3)
- //{
- // tmp = image[i];
- // image[i] = image[i + 2];
- // image[i + 2] = tmp;
- //}
- break;
- case 8:
- default:
- size = header.width * header.height;
- break;
- }
-
- // Write image data
- if (fwrite(image, size, 1, f) < 1) {
- perror("tgaSave> Disk write failed.\n");
- fclose(f);
- return -2;
- }
-
- fclose(f);
- return 0;
- }
|