home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************
- * *
- * tga2spr.c *
- * *
- * TGA bitmap to extened sprite format *
- * *
- * Version 2.00 (23-Aug-1993) *
- * *
- * (C) 1993 DEEJ Technology PLC *
- * *
- ************************************************************************/
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include "io.h"
- #include "sprite.h"
- #include "tga.h"
-
- int main(int argc, char** argv)
- {
- FILE *inf, *outf, *errf;
- int i,x,y,y2;
- BYTE r,g,b;
- DWORD rgb;
- WORD val;
- int flipY = 0;
- BYTE *tga_buf;
- BYTE *buf_ptr;
- int buf_size;
- spr_info_str spr;
- tga_hdr_str tga;
- char string[256];
-
- if(argc>1 && (strcmp(argv[1],"-y")==0 || strcmp(argv[1],"-Y")==0))
- {
- flipY = 32;
- argc--; argv++;
- }
-
- file_args(argc, argv, &inf, &outf, &errf);
-
- read_struct(LE, (BYTE*)&tga, tga_hdr_descr, inf);
-
- if(tga.ImageInfoDepth!=8 &&
- tga.ImageInfoDepth!=16 &&
- tga.ImageInfoDepth!=24)
- {
- fprintf(errf,"Bad bpp for TGA image (%d)\n",tga.ImageInfoDepth);
- return(1);
- }
-
- if(tga.MapType!=1 && tga.MapType!=0)
- {
- fprintf(errf,"Bad map type for TGA image (%d)\n",tga.MapType);
- return(2);
- }
- if(tga.ImageType!=1 && tga.ImageType!=2)
- {
- fprintf(errf,"Bad Image type for TGA image (%d)\n",
- tga.ImageType);
- return(4);
- }
- if(tga.MapType==1 && tga.ImageType==1 &&
- (tga.MapInfoOrgin!=0 ||
- tga.MapInfoLength>256 ||
- tga.MapInfoWidth!=24) )
- {
- fprintf(errf,"Bad map attributes for TGA image\n");
- return(3);
- }
-
- spr.X = tga.ImageInfoDX;
- spr.Y = tga.ImageInfoDY;
- spr.Xasp = 1;
- spr.Yasp = 1;
- spr.bpp = tga.ImageInfoDepth==16 ? 15:tga.ImageInfoDepth;
-
- fill_info(&spr);
-
- if(tga.MapType==1)
- {
- if(tga.ImageType==1)
- {
- /* read colour map for mapped image */
-
- spr.has_palette = tga.MapInfoLength;
-
- for(i=0; i<(int)tga.MapInfoLength; i++)
- {
- b = fgetc(inf);
- g = fgetc(inf);
- r = fgetc(inf);
-
- spr.palette[i] = (b<<24) | (g<<16) | (r<<8);
- }
- }
- else
- {
- /* attemp to skip any map for non mapped image */
-
- fprintf(errf,"Warning: colour map in non mapped image\n");
-
- for(i=0; i<(int)tga.MapInfoLength*(int)tga.MapInfoWidth/8; i++)
- b = fgetc(inf);
- }
- }
-
- alloc_spr_data(&spr);
-
- buf_size = tga.ImageInfoDX * (tga.ImageInfoDepth/8);
-
- if((tga_buf = (BYTE*)malloc(buf_size)) == 0)
- {
- fprintf(errf,"Unable to allocate TGA buffer\n");
- exit(5);
- }
-
- sprintf(string,"Generating sprite %dx%dx%d:",spr.X, spr.Y, spr.bpp);
- progress_start(string);
-
- for(y=spr.Y-1; y>=0; y--)
- {
- fread(tga_buf, buf_size, 1, inf);
-
- buf_ptr = tga_buf;
-
- if((tga.ImageInfoAttrib & 32) != flipY)
- y2 = (spr.Y-1)-y;
- else
- y2 = y;
-
- for(x=0; x<spr.X; x++)
- {
- switch(tga.ImageInfoDepth)
- {
- case 8:
- val = *buf_ptr++;
- write_pixel_val(&spr, x, y2, val);
- break;
-
- case 16:
- val = buf_ptr[0] | (buf_ptr[1]<<8);
- buf_ptr += 2;
-
- /* convert %0BBBBBGGGGGRRRRR */
- /* to %0RRRRRGGGGGBBBBB */
-
- rgb = ((val>>10) & 0x001F) |
- (val & 0x03E0) |
- ((val<<10) & 0x7C00);
-
- write_pixel_val(&spr, x, y2, rgb);
- break;
-
- case 24:
- rgb = (buf_ptr[0]<<24) |
- (buf_ptr[1]<<16) |
- (buf_ptr[2]<< 8);
- buf_ptr += 3;
- write_pixel_val(&spr, x, y2, rgb);
- break;
- }
- }
- progress(spr.Y-y,spr.Y);
- }
-
- write_sprite(&spr, outf);
-
- progress_finish();
-
- return(0);
- }
-