123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528 |
- /*================================================================
- *
- * Project : Freyja
- * Author : Mongoose
- * Website : http://www.westga.edu/~stu7440/
- * Email : stu7440@westga.edu
- * Object :
- * License : GPL See file COPYING, also (C) 2000 Mongoose
- * Comments: TGA plug-in
- *
- * TODO: type should pass more info
- * 2 bits for RGBA | RGB | GREY
- * val for depth
- *
- * This file was generated using Mongoose's C++
- * template generator script. <stu7440@westga.edu>
- *
- *-- History ------------------------------------------------
- *
- * 2001-10-25:
- * Mongoose - support for screen origin bit
- *
- * 2000-10-15:
- * Mongoose - Created
- ================================================================*/
-
- #include <string.h>
- #include <stdarg.h>
-
- #include <mtk_tga.h>
-
- #ifdef DEBUG_MEMORY
- #include <memory_test.h>
- #endif
-
-
- int mtk_image__tga_check(FILE *f)
- {
- char buffer[10];
-
-
- if (!f)
- {
- perror("tga_check> Passed invalid file.\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("tga_check> Inavlid or unknown TGA format.\n");
- return -2;
- }
-
- return 0;
- }
-
-
- int mtk_image__tga_load(FILE *f, unsigned char **image,
- unsigned int *width, unsigned int *height, char *type)
- {
- mtk_image_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;
-
-
- if (!f)
- {
- fprintf(stderr, "mtk_image__tga_load> Invalid parameters.\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:
- *type = 0;//8;
- break;
- default:
- *type = 0;
- }
-
- #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)
- {
- }
- else
- {
- comment[i] = 183; // print a dot for invalid text
- }
- }
-
- comment[255] = 0;
-
- printf("Comment: '%s'\n", comment);
- }
-
- size = header.width * header.height;
-
- if (!size || (!(header.colormap_type == 0 &&
- (header.image_type == 2 || header.image_type == 10))))
- {
- fprintf(stderr, "mtk_image__tga_load> Unknown image format.\n");
- return -2;
- }
-
- *image = NULL;
-
- // 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 Lenght
- {
- 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, "mtk_image__tga_load> Image fread failed.\n");
- delete [] *image;
- 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 Lenght
- {
- 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, "mtk_image__tga_load> Image fread failed.\n");
- delete [] *image;
- 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("tga_load> 8bpp Not implemented\n");
- break;
- default:
- ;
- }
-
- #ifdef DEBUG_TGA
- char c;
-
- printf("Comment:\n");
-
- while (fread(&c, 1, 1, f) == 1)
- {
- printf("%c", c);
- }
-
- printf("\n");
- #endif
-
- return 0;
- }
-
-
- int mtk_image__tga_save(FILE *f, unsigned char *image,
- unsigned int width, unsigned int height, char type)
- {
- mtk_image_tga_t header;
- unsigned int size;
- // unsigned int i;
- // unsigned char tmp;
- char comment[64];
-
-
- if (!f || !image || !width || !height)
- {
- fprintf(stderr, "mtk_image__tga_save> Invalid parameters.\n");
- return -1;
- }
-
- // Mongoose 2002.01.10, Heh, kind of silly
- strncpy(comment, "Mongoose TGA 20030711", 63);
- comment[63] = 0;
-
- header.comment_lenght = 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 = width;
- header.height = height;
-
- header.desc_flags = 0;
-
- switch (type)
- {
- case 4:
- header.image_type = TGA_TYPE__COLOR;
- header.desc_flags = 32;
- 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);
-
- size = header.width * header.height;
-
- 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:
- size = header.width * header.height;
- break;
- }
-
- // Write image data
- if (fwrite(image, size, 1, f) < 1)
- {
- perror("mtk_image__tga_save> Disk write failed.\n");
- return -2;
- }
-
- return 0;
- }
-
-
- int mtk_image__tga_save_filename(unsigned char *image,
- unsigned int width, unsigned int height,
- char type,
- char *s, ...)
- {
- char buffer[1024];
- FILE *f;
- int v;
- va_list args;
-
-
- va_start(args, s);
- vsnprintf(buffer, 1023, s, args);
- va_end(args);
-
- f = fopen(buffer, "wb");
-
- if (!f)
- {
- perror(buffer);
- return -1;
- }
-
- v = mtk_image__tga_save(f, image, width, height, type);
- fclose(f);
-
- return v;
- }
-
-
- #ifdef UNIT_TEST_TGA
- int main(int argc, char *argv[])
- {
- FILE *f;
- unsigned char *image;
- unsigned int width;
- unsigned int height;
- char type;
-
- if (argc > 1)
- {
- f = fopen(argv[1], "r");
-
- if (!f)
- {
- perror("Failed to open file> ");
- return -1;
- }
-
- if (!mtk_image__tga_check(f))
- {
- if (!mtk_image__tga_load(f, &image, &width, &height, &type))
- {
- printf("Loaded %s successfully!\n", argv[1]);
- delete [] image;
- }
- }
-
- fclose(f);
- } else {
- printf("Usage: %s testfile.tga\n", argv[0]);
- }
-
- return 0;
- }
- #endif
|