home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / program / sprtools / c / tga2spr < prev    next >
Encoding:
Text File  |  1994-07-18  |  5.1 KB  |  174 lines

  1. /************************************************************************
  2.  *                                    *
  3.  * tga2spr.c                                *
  4.  *                                    *
  5.  * TGA bitmap to extened sprite format                    *
  6.  *                                    *
  7.  * Version 2.00 (23-Aug-1993)                        *
  8.  *                                    *
  9.  * (C) 1993 DEEJ Technology PLC                        *
  10.  *                                    *
  11.  ************************************************************************/
  12.  
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "io.h"
  17. #include "sprite.h"
  18. #include "tga.h"
  19.  
  20. int main(int argc, char** argv)
  21. {
  22.         FILE *inf, *outf, *errf;
  23.         int          i,x,y,y2;
  24.         BYTE         r,g,b;
  25.         DWORD        rgb;
  26.         WORD         val;
  27.         int          flipY = 0;
  28.         BYTE        *tga_buf;
  29.         BYTE        *buf_ptr;
  30.         int          buf_size;
  31.         spr_info_str spr;
  32.         tga_hdr_str  tga;
  33.     char         string[256];
  34.  
  35.         if(argc>1 && (strcmp(argv[1],"-y")==0 || strcmp(argv[1],"-Y")==0))
  36.         {
  37.                 flipY = 32;
  38.                 argc--; argv++;
  39.         }
  40.  
  41.         file_args(argc, argv, &inf, &outf, &errf);
  42.  
  43.         read_struct(LE, (BYTE*)&tga, tga_hdr_descr, inf);
  44.  
  45.         if(tga.ImageInfoDepth!=8 &&
  46.            tga.ImageInfoDepth!=16 &&
  47.            tga.ImageInfoDepth!=24)
  48.         {
  49.                 fprintf(errf,"Bad bpp for TGA image (%d)\n",tga.ImageInfoDepth);
  50.                 return(1);
  51.         }
  52.  
  53.         if(tga.MapType!=1 && tga.MapType!=0)
  54.         {
  55.                 fprintf(errf,"Bad map type for TGA image (%d)\n",tga.MapType);
  56.                 return(2);
  57.         }
  58.         if(tga.ImageType!=1 && tga.ImageType!=2)
  59.         {
  60.                 fprintf(errf,"Bad Image type for TGA image (%d)\n",
  61.                                                                 tga.ImageType);
  62.                 return(4);
  63.         }
  64.         if(tga.MapType==1 && tga.ImageType==1 &&
  65.                             (tga.MapInfoOrgin!=0   ||
  66.                              tga.MapInfoLength>256 ||
  67.                              tga.MapInfoWidth!=24) )
  68.         {
  69.                 fprintf(errf,"Bad map attributes for TGA image\n");
  70.                 return(3);
  71.         }
  72.  
  73.         spr.X    = tga.ImageInfoDX;
  74.         spr.Y    = tga.ImageInfoDY;
  75.         spr.Xasp = 1;
  76.         spr.Yasp = 1;
  77.         spr.bpp  = tga.ImageInfoDepth==16 ? 15:tga.ImageInfoDepth;
  78.  
  79.         fill_info(&spr);
  80.  
  81.         if(tga.MapType==1)
  82.         {
  83.             if(tga.ImageType==1)
  84.             {
  85.                 /* read colour map for mapped image */
  86.  
  87.                 spr.has_palette = tga.MapInfoLength;
  88.  
  89.                 for(i=0; i<(int)tga.MapInfoLength; i++)
  90.                 {
  91.                         b = fgetc(inf);
  92.                         g = fgetc(inf);
  93.                         r = fgetc(inf);
  94.  
  95.                         spr.palette[i] = (b<<24) | (g<<16) | (r<<8);
  96.                 }
  97.             }
  98.             else
  99.             {
  100.                 /* attemp to skip any map for non mapped image */
  101.  
  102.                 fprintf(errf,"Warning: colour map in non mapped image\n");
  103.  
  104.                 for(i=0; i<(int)tga.MapInfoLength*(int)tga.MapInfoWidth/8; i++)
  105.                         b = fgetc(inf);
  106.             }
  107.         }
  108.  
  109.         alloc_spr_data(&spr);
  110.  
  111.         buf_size = tga.ImageInfoDX * (tga.ImageInfoDepth/8);
  112.  
  113.         if((tga_buf = (BYTE*)malloc(buf_size)) == 0)
  114.         {
  115.                 fprintf(errf,"Unable to allocate TGA buffer\n");
  116.                 exit(5);
  117.         }
  118.  
  119.         sprintf(string,"Generating sprite %dx%dx%d:",spr.X, spr.Y, spr.bpp);
  120.     progress_start(string);
  121.  
  122.         for(y=spr.Y-1; y>=0; y--)
  123.         {
  124.                 fread(tga_buf, buf_size, 1, inf);
  125.  
  126.                 buf_ptr = tga_buf;
  127.  
  128.                 if((tga.ImageInfoAttrib & 32) != flipY)
  129.                         y2 = (spr.Y-1)-y;
  130.                 else
  131.                         y2 = y;
  132.  
  133.                 for(x=0; x<spr.X; x++)
  134.                 {
  135.                         switch(tga.ImageInfoDepth)
  136.                         {
  137.                         case 8:
  138.                                 val = *buf_ptr++;
  139.                                 write_pixel_val(&spr, x, y2, val);
  140.                                 break;
  141.  
  142.                         case 16:
  143.                                 val = buf_ptr[0] | (buf_ptr[1]<<8);
  144.                                 buf_ptr   += 2;
  145.  
  146.                                 /* convert %0BBBBBGGGGGRRRRR */
  147.                                 /* to      %0RRRRRGGGGGBBBBB */
  148.  
  149.                                 rgb = ((val>>10) & 0x001F) |
  150.                                        (val      & 0x03E0) |
  151.                                       ((val<<10) & 0x7C00);
  152.  
  153.                                 write_pixel_val(&spr, x, y2, rgb);
  154.                                 break;
  155.  
  156.                         case 24:
  157.                                 rgb = (buf_ptr[0]<<24) |
  158.                                       (buf_ptr[1]<<16) |
  159.                                       (buf_ptr[2]<< 8);
  160.                                 buf_ptr += 3;
  161.                                 write_pixel_val(&spr, x, y2, rgb);
  162.                                 break;
  163.                         }
  164.                 }
  165.                 progress(spr.Y-y,spr.Y);
  166.         }
  167.  
  168.         write_sprite(&spr, outf);
  169.  
  170.         progress_finish();
  171.  
  172.         return(0);
  173. }
  174.