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

  1. /************************************************************************
  2.  *                                                                      *
  3.  * sun raster to extended sprite format bitmap converter                *
  4.  *                                                                      *
  5.  * Version 2.00 (18-Nov-1993)                                           *
  6.  *        2.50 (17-Jun-1994) 24 bit raster file extensions             *
  7.  *                                                                      *
  8.  * (C) 1993/4 DEEJ Technology PLC                                       *
  9.  *                                                                      *
  10.  ************************************************************************/
  11.  
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "io.h"
  16. #include "sprite.h"
  17. #include "ras.h"
  18.  
  19. int main(int argc, char** argv)
  20. {
  21.         FILE        *inf;
  22.         FILE        *outf;
  23.         FILE        *errf;
  24.         int          i,j;
  25.         int          x,y;
  26.         int         X;
  27.         int          line_size;
  28.         int          line_off;
  29.         uint         p;
  30.         uchar       *ras_buf;
  31.         uchar        cmap[256][3];
  32.         rasterfile   hdr;
  33.         spr_info_str spr;
  34.         char         string[256];
  35.  
  36.         file_args(argc, argv, &inf, &outf, &errf);
  37.  
  38.         read_struct(BE, (BYTE*)&hdr, rasterfile_descr, inf);
  39.  
  40.         if(hdr.ras_magic != RAS_MAGIC)
  41.         {
  42.                 fprintf(errf, "This is not a raster file\n");
  43.                 return(1);
  44.         }
  45.  
  46.         if(hdr.ras_type!=RT_OLD && hdr.ras_type!=RT_STANDARD)
  47.         {
  48.                 fprintf(errf, "Only RT_OLD & RT_STANDARD types supported\n");
  49.                 return(2);
  50.         }
  51.  
  52.         if(hdr.ras_maptype!=RMT_EQUAL_RGB && hdr.ras_maptype!=RMT_NONE)
  53.         {
  54.                 fprintf(errf, "Only RMT_EQUAL_RGB and RMT_NONE map types supported\n");
  55.                 return(3);
  56.         }
  57.  
  58.         if(hdr.ras_depth!=1 && hdr.ras_depth!=8 && hdr.ras_depth!=24)
  59.         {
  60.                 fprintf(errf, "Only images with 1,8 or 24 BPP can be handled\n");
  61.                 return(4);
  62.         }
  63.  
  64.         spr.X    = hdr.ras_width;
  65.         spr.Y    = hdr.ras_height;
  66.         spr.Xasp = 1;
  67.         spr.Yasp = 1;
  68.         spr.bpp  = hdr.ras_depth;
  69.  
  70.         fill_info(&spr);
  71.  
  72.         alloc_spr_data(&spr);
  73.  
  74.         switch(hdr.ras_depth)
  75.         {
  76.                 case 1:
  77.                         line_size = ((hdr.ras_width + 15)/8) & ~1;
  78.                         break;
  79.                 case 8:
  80.                         line_size = (hdr.ras_width + 1) & ~1;
  81.                         break;
  82.  
  83.         case 24:
  84.             line_size = (hdr.ras_width*3 + 1) & ~1;
  85.             break;
  86.         }
  87.  
  88.         if((ras_buf=(uchar*)malloc(line_size)) == 0)
  89.         {
  90.                 fprintf(errf,"Unable to allocate raster buffer\n");
  91.                 exit(5);
  92.         }
  93.  
  94.         /* read colour map */
  95.  
  96.     if(hdr.ras_maptype!=RMT_NONE)
  97.     {
  98.             for(j=0; j<3; j++)
  99.             {
  100.                     for(i=0; i<hdr.ras_maplength/3; i++)
  101.                     {
  102.                             cmap[i][j] = fgetc(inf);
  103.                     }
  104.             }
  105.  
  106.             for(i=0; i<spr.cols; i++)
  107.             {
  108.                     spr.palette[i] = (cmap[i][2] << 24) |
  109.                                      (cmap[i][1] << 16) |
  110.                                      (cmap[i][0] << 8);
  111.             }
  112.     }
  113.  
  114.         sprintf(string,"Generating sprite %dx%dx%d:",spr.X,spr.Y,spr.bpp);
  115.         progress_start(string);
  116.  
  117.         X = spr.bpp==24 ? spr.X : spr.line_size;
  118.  
  119.         for(y=0; y<spr.Y; y++)
  120.         {
  121.                 fread(ras_buf, line_size, 1, inf);
  122.  
  123.                 line_off = y * spr.line_size;
  124.  
  125.                 for(x=0; x<X; x++)
  126.                 {
  127.  
  128.                         switch(spr.bpp)
  129.                         {
  130.                             case 1:
  131.                                 spr.spr_data[x+line_off] = bit_swap(ras_buf[x]);
  132.                                 break;
  133.  
  134.                             case 8:
  135.                                 spr.spr_data[x+line_off] = ras_buf[x];
  136.                                 break;
  137.  
  138.                             case 24:
  139.                                 p = (ras_buf[x*3  ] << 24) +
  140.                                     (ras_buf[x*3+1] << 16) +
  141.                                     (ras_buf[x*3+2] <<  8);
  142.                                 write_pixel_val(&spr, x, y, p);
  143.                                 break;
  144.                         }
  145.                 }
  146.                 progress(y,spr.Y);
  147.         }
  148.  
  149.         write_sprite(&spr, outf);
  150.  
  151.         progress_finish();
  152. }
  153.