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

  1. /************************************************************************
  2.  *                                    *
  3.  * Archimedes sprite to StarBase format bitmap converter         *
  4.  *                                    *
  5.  * Uses new sprite/io library routines                    *
  6.  *                                    *
  7.  * Version 5.00 (23-Aug-1993)                        *
  8.  *                                    *
  9.  * (C) 1989/1992/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 "sb.h"
  19.  
  20.  
  21. int main(int argc, char **argv)
  22. {
  23.         FILE *inf, *outf, *errf;
  24.         int          i,j,x,y,Y;
  25.         uint         r,g,b,p;
  26.     uint        *ptr;
  27.         uchar       *sb_buf;
  28.         uchar       *buf_ptr;
  29.         spr_info_str spr;
  30.         bf_header    hdr;
  31.         float        cmap[256][3];
  32.     char         string[256];
  33.  
  34.         file_args(argc, argv, &inf, &outf, &errf); 
  35.  
  36.         read_sprite(&spr, inf);
  37.  
  38.         if(spr.bpp > 8)
  39.         {
  40.                 fprintf(errf,"Only images with <= 8 BPP can be converted\n");
  41.                 return(1);
  42.         }
  43.  
  44.         for(i=0; i<spr.cols; i++)
  45.         {
  46.                 r = (spr.palette[i] >>  8) & 0xFF;
  47.                 g = (spr.palette[i] >> 16) & 0xFF;
  48.                 b = (spr.palette[i] >> 24) & 0xFF;
  49.  
  50.                 cmap[i][0] = (float)r / (float)255.0;
  51.                 cmap[i][1] = (float)g / (float)255.0;
  52.                 cmap[i][2] = (float)b / (float)255.0;
  53.  
  54.         for(j=0; j<3; j++)
  55.                 {
  56.                         ptr = (uint*)&cmap[i][j];
  57.                         *ptr = endian(BE,*ptr);
  58.                 }
  59.         }
  60.  
  61.         /* account for square/reqtangular pixels */
  62.  
  63.         if(spr.Yasp == 2)
  64.                 Y = spr.Y*2;
  65.         else
  66.                 Y = spr.Y;
  67.  
  68.         if((sb_buf = (uchar*)malloc(spr.X+4*8)) == 0) /* + wastage */
  69.         {
  70.                 fprintf(errf,"Unable to allocate sb buffer\n");
  71.                 exit(1);
  72.         }
  73.  
  74.         strcpy(hdr.file_id,   "Bitmapfile");
  75.         strcpy(hdr.device_id, "spr2sb");
  76.         hdr.rev         = 1;
  77.         hdr.bm_loc      = spr.cols*4*3+sizeof(bf_header);
  78.         hdr.eod_loc     = hdr.bm_loc + spr.X*Y;
  79.         hdr.xstart      = 0;
  80.         hdr.ystart      = 0;
  81.         hdr.xlen        = spr.X;
  82.         hdr.ylen        = Y;
  83.         hdr.bm_mode     = -1;
  84.         hdr.depth       = spr.bpp;
  85.         hdr.pixel_align = 8;
  86.         hdr.num_banks   = 1;
  87.         hdr.disp_en     = 255;
  88.         hdr.cmap_mode   = 0;
  89.         hdr.csize       = spr.cols;
  90.         hdr.back_index  = 0;
  91.  
  92.         write_struct(BE, (BYTE*)&hdr, bf_header_descr, outf);
  93.         fwrite(cmap, sizeof(float)*3, spr.cols, outf);
  94.  
  95.         sprintf(string,"Generating bitmap %dx%dx%d:",spr.X,Y,hdr.depth);
  96.     progress_start(string);
  97.  
  98.         for(y=0; y<Y; y+=spr.Yasp)
  99.         {
  100.         buf_ptr = sb_buf;
  101.  
  102.             for(x=0; x<spr.line_size; x++)
  103.             {
  104.                 p = spr.spr_data[x+(y/spr.Yasp)*spr.line_size];
  105.  
  106.                 switch(spr.bpp)
  107.                 {
  108.                 case 1:
  109.                         buf_ptr[0] =  p     & 1;
  110.                         buf_ptr[1] = (p>>1) & 1;
  111.                         buf_ptr[2] = (p>>2) & 1;
  112.                         buf_ptr[3] = (p>>3) & 1;
  113.                         buf_ptr[4] = (p>>4) & 1;
  114.                         buf_ptr[5] = (p>>5) & 1;
  115.                         buf_ptr[6] = (p>>6) & 1;
  116.                         buf_ptr[7] = (p>>7) & 1;
  117.             buf_ptr   += 8;
  118.                         break;
  119.                 case 2:
  120.                         buf_ptr[0] =  p     & 3;
  121.                         buf_ptr[1] = (p>>2) & 3;
  122.                         buf_ptr[2] = (p>>4) & 3;
  123.                         buf_ptr[3] = (p>>6) & 3;
  124.             buf_ptr   += 4;
  125.                         break;
  126.                 case 4:
  127.                         buf_ptr[0] = p & 0xF;
  128.                         buf_ptr[1] = p >> 4;
  129.             buf_ptr   += 2;
  130.                         break;
  131.                 case 8:
  132.                         *buf_ptr++ = p;
  133.                         break;
  134.                 }
  135.             }
  136.             fwrite(sb_buf, spr.X, 1, outf);
  137.             if(spr.Yasp==2)
  138.                 fwrite(sb_buf, spr.X, 1, outf);
  139.  
  140.             progress(y,Y);
  141.         }
  142.  
  143.         progress_finish();
  144. }
  145.