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

  1. /************************************************************************
  2.  *                                    *
  3.  * sb2spr.c                                *
  4.  *                                    *
  5.  * starbase bitmap to extened sprite format                *
  6.  *                                    *
  7.  * Version 2.00 (24-Jun-1993)                        *
  8.  *                                    *
  9.  * (C) 1992/3 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 "sb.h"
  19.  
  20. #define CSIZE 256
  21.  
  22. int main(int argc, char** argv)
  23. {
  24.         FILE *inf, *outf, *errf;
  25.         int          i,j,x,y;
  26.         char         c;
  27.         uint         r,g,b;
  28.     uint        *ptr;
  29.         uchar       *sb_buf;
  30.         float        cmap[CSIZE][3];
  31.         spr_info_str spr;
  32.         bf_header    hdr;
  33.     char         string[256];
  34.  
  35.         file_args(argc, argv, &inf, &outf, &errf);
  36.  
  37.         read_struct(BE, (BYTE*)&hdr, bf_header_descr, inf);
  38.  
  39.         if(hdr.bm_mode!=-1 || hdr.pixel_align!=8 ||
  40.            hdr.num_banks!=1 || hdr.csize>256)
  41.         {
  42.                 fprintf(stderr,"\nCannot convert this image\n\n");
  43.                 return(1);
  44.         }
  45.  
  46.         spr.X       = hdr.xlen;
  47.         spr.Y       = hdr.ylen;
  48.         spr.Xasp    = 1;
  49.         spr.Yasp    = 1;
  50.         spr.bpp     = hdr.depth;
  51.  
  52.         fill_info(&spr);
  53.  
  54.         fread(&cmap, sizeof(float)*3, hdr.csize, inf);
  55.  
  56.         for(i=0; i<hdr.csize; i++)
  57.         {
  58.                 for(j=0; j<3; j++)
  59.                 {
  60.                         ptr = (uint*)&cmap[i][j];
  61.                         *ptr = endian(BE,*ptr);
  62.                 }
  63.  
  64.                 r = (int)(cmap[i][0]*255.0); r=(r>255) ? 255:r;
  65.                 g = (int)(cmap[i][1]*255.0); g=(g>255) ? 255:g;
  66.                 b = (int)(cmap[i][2]*255.0); b=(b>255) ? 255:b;
  67.  
  68.                 spr.palette[i] = (b<<24) + (g<<16) + (r<<8);
  69.         }
  70.  
  71.         /*
  72.          * WARNING: it is dangerous to assume size of structures
  73.          * in memory is the same in the file when read with read_struct
  74.          */
  75.  
  76.         if(hdr.bm_loc < (sizeof(bf_header)+sizeof(float)*3*hdr.csize))
  77.         {
  78.                 fprintf(errf,"Bitmap location is wrong\n");
  79.                 return(2);
  80.         }
  81.         else
  82.         {
  83.                 for(i=(sizeof(bf_header)+sizeof(float)*3*hdr.csize);
  84.                     i<hdr.bm_loc; i++)
  85.                         c = fgetc(inf);
  86.         }
  87.  
  88.         alloc_spr_data(&spr);
  89.  
  90.         if((sb_buf = (uchar*)malloc(spr.X)) == 0)
  91.         {
  92.                 fprintf(errf,"Unable to allocate sb buffer\n");
  93.                 exit(1);
  94.         }
  95.  
  96.         sprintf(string,"Generating sprite %dx%dx%d:",spr.X,spr.Y,spr.bpp);
  97.     progress_start(string);
  98.  
  99.         for(y=0; y<spr.Y; y++)
  100.         {
  101.                 fread(sb_buf, spr.X, 1, inf);
  102.         
  103.                 for(x=0; x<spr.X; x++)
  104.                 {
  105.                         write_pixel_val(&spr, x, y, sb_buf[x]);
  106.                 }
  107.                 progress(y,spr.Y);
  108.         }
  109.         write_sprite(&spr,outf);
  110.  
  111.         progress_finish();
  112. }
  113.