home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************
- * *
- * spr2tga.c *
- * *
- * Sprite to TGA 16 bit format *
- * using 2nd generation sprite routines *
- * *
- * Version 2.00 (22-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,Y;
- int r,g,b;
- int bpp;
- DWORD rgb;
- WORD val;
- BYTE *tga_buf;
- BYTE *buf_ptr;
- int buf_size;
- spr_info_str spr;
- tga_hdr_str tga;
- char string[256];
-
- if(argc>1 && argv[1][0]=='-')
- {
- bpp = atoi(&argv[1][1]);
-
- if(bpp != 0)
- {
- if(bpp!=8 && bpp!=16 && bpp!=24)
- {
- fprintf(errf,"Bad bpp for TGA image\n");
- return(1);
- }
- argc--; argv++;
- }
- }
- else
- {
- bpp = 0;
- }
-
- file_args(argc, argv, &inf, &outf, &errf);
-
- read_sprite(&spr, inf);
-
- if(bpp==0 || bpp<spr.bpp)
- {
- switch(spr.bpp)
- {
- case 15: bpp=16; break;
- case 24: bpp=24; break;
- default: bpp=8; break;
- }
- }
-
- Y = spr.Y * spr.Yasp;
-
- tga.IDLength = 0;
- tga.MapType = bpp==8 ? 1:0;
- tga.ImageType = bpp==8 ? 1:2;
- tga.MapInfoOrgin = 0;
- tga.MapInfoLength = bpp==8 ? 256:0;
- tga.MapInfoWidth = bpp==8 ? 24:0;
- tga.ImageInfoX = 0;
- tga.ImageInfoY = 0;
- tga.ImageInfoDX = spr.X;
- tga.ImageInfoDY = Y;
- tga.ImageInfoDepth = bpp;
- tga.ImageInfoAttrib = 0;
-
- buf_size = spr.X * (bpp/8);
-
- if((tga_buf = malloc(buf_size)) == 0)
- {
- fprintf(errf,"Unable to allocate TGA buffer\n");
- exit(1);
- }
-
- write_struct(LE, (BYTE*)&tga, tga_hdr_descr, outf);
-
- sprintf(string,"Generating TGA %dx%dx%d:",tga.ImageInfoDX,
- tga.ImageInfoDY,
- tga.ImageInfoDepth);
- progress_start(string);
-
- /* write colour map if 8 bpp */
-
- if(bpp==8)
- {
- if(spr.has_palette==FALSE) default_palette(&spr);
-
- for(i=0; i<(int)tga.MapInfoLength; i++)
- {
- if(i<spr.cols)
- {
- r = (spr.palette[i] >> 8) & 0xFF;
- g = (spr.palette[i] >> 16) & 0xFF;
- b = (spr.palette[i] >> 24) & 0xFF;
- }
- else
- {
- r = g = b = 0;
- }
- fputc(b, outf);
- fputc(g, outf);
- fputc(r, outf);
- }
- }
-
- for(y=Y-1; y>=0; y--)
- {
- buf_ptr = tga_buf;
-
- for(x=0; x<spr.X; x++)
- {
- switch(bpp)
- {
- case 8:
- val = read_pixel_val(&spr, x, y/spr.Yasp);
- *buf_ptr++ = val;
- break;
-
- case 16:
- rgb = read_pixel_RGB(&spr, x, y/spr.Yasp);
-
- /* convert %BBBBBBBBGGGGGGGGRRRRRRRR00000000 */
- /* to %0RRRRRGGGGGBBBBB */
-
- val = (((rgb>>27) & 0x1F) << 0) +
- (((rgb>>19) & 0x1F) << 5) +
- (((rgb>>11) & 0x1F) << 10);
-
- buf_ptr[0] = val;
- buf_ptr[1] = val>>8;
- buf_ptr += 2;
- break;
-
- case 24:
- rgb = read_pixel_RGB(&spr, x, y/spr.Yasp);
- buf_ptr[0] = (rgb >> 24);
- buf_ptr[1] = (rgb >> 16);
- buf_ptr[2] = (rgb >> 8);
- buf_ptr += 3;
- break;
- }
- }
- fwrite(tga_buf, buf_size, 1, outf);
-
- progress(Y-y,Y);
- }
- progress_finish();
-
- return(0);
- }
-