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

  1. /************************************************************************
  2.  *                                                                      *
  3.  * Archimedes sprite to rasterfile format bitmap converter              *
  4.  *                                                                      *
  5.  * Version 2.00 (18-Nov-1993)                                           *
  6.  *                                                                      *
  7.  * (C) 1989/1993 DEEJ Technology PLC                                    *
  8.  *                                                                      *
  9.  ************************************************************************/
  10.  
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "io.h"
  15. #include "sprite.h"
  16. #include "ras.h"
  17.  
  18. int main(int argc, char **argv)
  19. {
  20.         FILE *inf, *outf, *errf;
  21.         int          i,j,x,y,X,Y;
  22.         uchar       *ras_buf;
  23.         uint         p;
  24.         int          line_size;
  25.         spr_info_str spr;
  26.         rasterfile   hdr;
  27.         char         string[256];
  28.  
  29.         file_args(argc, argv, &inf, &outf, &errf);
  30.  
  31.         read_sprite(&spr, inf);
  32.  
  33.         /* account for square/reqtangular pixels */
  34.  
  35.         if(spr.Yasp == 2)
  36.                 Y = spr.Y*2;
  37.         else
  38.                 Y = spr.Y;
  39.  
  40.         hdr.ras_magic     = RAS_MAGIC;
  41.         hdr.ras_width     = spr.X;
  42.         hdr.ras_height    = Y;
  43.         hdr.ras_type      = RT_STANDARD;
  44.         hdr.ras_maptype   = spr.has_palette ? RMT_EQUAL_RGB : RMT_NONE;
  45.         hdr.ras_maplength = spr.has_palette * 3;
  46.  
  47.         switch(spr.bpp)
  48.         {
  49.                 case 1:
  50.                 hdr.ras_depth = 1;
  51.                         line_size = ((spr.X + 15)/8) & ~1;
  52.                         break;
  53.  
  54.                 case 2:
  55.                 case 4:
  56.                 case 8:
  57.                 hdr.ras_depth = 8;
  58.                         line_size = (spr.X + 1) & ~1;
  59.                         break;
  60.  
  61.                 case 15:
  62.                 case 24:
  63.                 hdr.ras_depth = 24;
  64.                 line_size = (spr.X*3 + 1) & ~1;
  65.                     break;
  66.         }
  67.  
  68.         hdr.ras_length = line_size * Y;
  69.  
  70.         if((ras_buf = (uchar*)malloc(line_size)) == 0)
  71.         {
  72.                 fprintf(errf,"Unable to allocate raster buffer\n");
  73.                 exit(1);
  74.         }
  75.  
  76.         write_struct(BE, (BYTE*)&hdr, rasterfile_descr, outf);
  77.  
  78.         /* write colour map if bpp<=8 */
  79.  
  80.     if(spr.has_palette)
  81.     {
  82.                 for(j=8; j<=24; j+=8)
  83.                 {
  84.                         for(i=0; i<spr.has_palette; i++)
  85.                         {
  86.                                 fputc(spr.palette[i] >> j, outf);
  87.                         }
  88.                 }
  89.     }
  90.  
  91.         sprintf(string,"Generating raster %dx%dx%d:",spr.X,Y,hdr.ras_depth);
  92.         progress_start(string);
  93.  
  94.         X = hdr.ras_depth==1 ? line_size : spr.X;
  95.  
  96.         for(y=0; y<Y; y+=spr.Yasp)
  97.         {
  98.                 for(x=0; x<X; x++)
  99.                 {
  100.  
  101.                         switch(hdr.ras_depth)
  102.                         {
  103.                             case 1:
  104.                                 p = spr.spr_data[x+y*spr.line_size];
  105.                                 ras_buf[x] = bit_swap(p);
  106.                                 break;
  107.  
  108.                             case 8:
  109.                                 ras_buf[x] = read_pixel_val(&spr,x,y/spr.Yasp);
  110.                                 break;
  111.  
  112.                             case 24:
  113.                                 p = read_pixel_val(&spr,x,y/spr.Yasp);
  114.                                 ras_buf[x*3  ] = p >> 24;
  115.                                 ras_buf[x*3+1] = p >> 16;
  116.                                 ras_buf[x*3+2] = p >>  8;
  117.                                 break;
  118.                         }
  119.                 }
  120.  
  121.                 /* write one or two lines depending on Y aspect */
  122.  
  123.                 fwrite(ras_buf, line_size, 1, outf);
  124.  
  125.                 if(spr.Yasp == 2)
  126.                         fwrite(ras_buf, line_size, 1, outf);
  127.  
  128.                 progress(y,Y);
  129.         }
  130.  
  131.         progress_finish();
  132. }
  133.