home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / program / sprtools / c / bmp2spr < prev    next >
Encoding:
Text File  |  1995-02-02  |  5.0 KB  |  175 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  * Windows 3 BMP to extended sprite format bitmap converter             *
  4.  *                                                                      *
  5.  * Version 3.00 (02-Feb-1995)                                           *
  6.  *                                                                      *
  7.  * (C) 1993-5 DEEJ Technology PLC                                       *
  8.  *                                                                      *
  9.  ************************************************************************/
  10.  
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "io.h"
  15. #include "bmp.h"
  16. #include "sprite.h"
  17.  
  18. int main(int argc, char** argv)
  19. {
  20.         FILE        *inf;
  21.         FILE        *outf;
  22.         FILE        *errf;
  23.         int          i;
  24.         int          x,y;
  25.         uchar       *bmp_buf;
  26.         uchar        p;
  27.         uint         rgb;
  28.         int          size;
  29.         bmp_hdr_str  bmp;
  30.         spr_info_str spr;
  31.         char         string[256];
  32.  
  33.         file_args(argc, argv, &inf, &outf, &errf);
  34.  
  35.         read_struct(LE, (BYTE*)&bmp, bmp_rhdr_descr, inf);
  36.  
  37.  
  38.         if(bmp.id[0]!='B' || bmp.id[1]!='M' || (bmp.subheader_size!=0x28 &&
  39.                                                 bmp.subheader_size!=12))
  40.         {
  41.                 fprintf(errf, "BMP format not reqcognised\n");
  42.                 return(1);
  43.         }
  44.  
  45.         if(bmp.subheader_size == 12)
  46.         {
  47.                 bmp_shdr2_str sbmp;
  48.  
  49.             read_struct(LE, (BYTE*)&sbmp, bmp_shdr2_descr, inf);
  50.  
  51.              bmp.width           = sbmp.width;
  52.             bmp.height          = sbmp.height;
  53.             bmp.bit_planes      = sbmp.bit_planes;
  54.             bmp.bits_per_pixel  = sbmp.bits_per_pixel;
  55.  
  56.                    bmp.palette_entries = 1<<sbmp.bits_per_pixel;
  57.                    bmp.colours         = 1<<sbmp.bits_per_pixel;
  58.                    bmp.compression     = 0; /* ? */
  59.            }
  60.            else
  61.            {
  62.             read_struct(LE, (BYTE*)&bmp.width, bmp_shdr1_descr, inf);
  63.            }
  64.  
  65.         if(bmp.bit_planes != 1)
  66.         {
  67.                 fprintf(errf, "Only 1 bit plane images can be handled(%d)\n",
  68.                               bmp.bit_planes);
  69.                 return(2);
  70.         }
  71.  
  72.         if(bmp.compression > 8)
  73.         {
  74.                 fprintf(errf, "Only uncompressed images can be handled(%d)\n",
  75.                               bmp.compression);
  76.                 return(3);
  77.         }
  78.  
  79.         spr.X    = bmp.width;
  80.         spr.Y    = bmp.height;
  81.         spr.Xasp = 1;
  82.         spr.Yasp = 1;
  83.         spr.bpp  = bmp.bits_per_pixel;
  84.  
  85.         fill_info(&spr);
  86.  
  87.         /* read colour map */
  88.  
  89.     if(bmp.subheader_size == 12)
  90.     {
  91.             for(i=0; i<bmp.colours; i++)
  92.             {
  93.                     spr.palette[i] = (fgetc(inf) << 24)
  94.                                    | (fgetc(inf) << 16)
  95.                                    | (fgetc(inf) << 8);
  96.             }
  97.     }
  98.     else
  99.     {
  100.             for(i=0; i<bmp.colours; i++)
  101.             {
  102.                     spr.palette[i] = endian(BE,fgetdLE(inf));
  103.             }
  104.         }
  105.  
  106.         /* skip additional info in header */
  107.  
  108.         for(i=(int)ftell(inf); i<bmp.bitmap_offset; i++)
  109.         {
  110.                 p = fgetc(inf);
  111.         }
  112.  
  113.         alloc_spr_data(&spr);
  114.  
  115.         size = spr.line_size;
  116.  
  117.         if((bmp_buf=(uchar*)malloc(size)) == 0)
  118.         {
  119.                 fprintf(errf,"Unable to allocate bmp buffer\n");
  120.                 exit(5);
  121.         }
  122.  
  123.         sprintf(string,"Generating sprite %dx%dx%d:",spr.X,spr.Y,spr.bpp);
  124.         progress_start(string);
  125.  
  126.         for(y=spr.Y-1; y>=0; y--)
  127.         {
  128.                 fread(bmp_buf, size, 1, inf);
  129.  
  130.                 if(spr.bpp != 24)
  131.                 {
  132.                     for(x=0; x<spr.line_size; x++)
  133.                     {
  134.                         p = bmp_buf[x];
  135.  
  136.                         switch(spr.bpp)
  137.                         {
  138.                         case 1:
  139.                                 spr.spr_data[x+y*spr.line_size] = bit_swap(p);
  140.                                 break;
  141.  
  142.                         case 2:
  143.                                 spr.spr_data[x+y*spr.line_size] = bit2_swap(p);
  144.                                 break;
  145.  
  146.                         case 4:
  147.                                 spr.spr_data[x+y*spr.line_size] = bit4_swap(p);
  148.                                 break;
  149.  
  150.                         case 8:
  151.                                 spr.spr_data[x+y*spr.line_size] = p;
  152.                                 break;
  153.  
  154.                         }
  155.                     }
  156.                 }
  157.                 else
  158.                 {
  159.                         for(x=0; x<spr.X; x++)
  160.                         {
  161.                                 rgb = (bmp_buf[x*3+0] << 24) |
  162.                                       (bmp_buf[x*3+1] << 16) |
  163.                                       (bmp_buf[x*3+2] <<  8);
  164.  
  165.                                 write_pixel_val(&spr, x, y, rgb);
  166.                         }
  167.                 }
  168.                 progress(spr.Y-y-1,spr.Y);
  169.         }
  170.  
  171.         write_sprite(&spr, outf);
  172.  
  173.         progress_finish();
  174. }
  175.