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

  1. /************************************************************************
  2.  *                                    *
  3.  * Archimedes extended sprite to PC Windows 3 BMP bitmap converter    *
  4.  *                                    *
  5.  * Version 3.00 (02-Feb-1995)                        *
  6.  *                                    *
  7.  * (C) 1992-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, *outf, *errf;
  21.         int          i,x,y;
  22.         int          Y,y2;
  23.         int          size, pal_size, line_size;
  24.         uint         rgb, bgr;
  25.         uchar        p;
  26.         BYTE        *bmp_buf;
  27.         spr_info_str spr;
  28.         bmp_hdr_str  bmp;
  29.         char         string[256];
  30.  
  31.         file_args(argc, argv, &inf, &outf, &errf);
  32.         read_sprite(&spr, inf);
  33.  
  34.         if(spr.bpp!=1 && spr.bpp!=4 && spr.bpp!=8 && spr.bpp!=24)
  35.         {
  36.                 fprintf(errf, "Only 1,4,8 & 24 BPP images can be handled\n");
  37.                 return(1);
  38.         }
  39.  
  40.         Y         = spr.Y * spr.Yasp;
  41.         line_size = spr.pix==32 ? ((spr.X*3+3) & ~3) : spr.line_size;
  42.         size      = spr.Y*line_size;
  43.         pal_size  = spr.has_palette*4;
  44.  
  45.         if((bmp_buf = malloc(size)) == 0)
  46.         {
  47.                 fprintf(stderr,"Unable to allocate BMP buffer\n");
  48.                 return(1);
  49.         }
  50.  
  51.         bmp.id[0]           = 'B';
  52.         bmp.id[1]           = 'M';
  53.         bmp.file_size       = size+pal_size+0x36;
  54.         bmp.resurved1       = 0;
  55.         bmp.resurved1       = 0;
  56.         bmp.bitmap_offset   = 0x36+pal_size;
  57.         bmp.subheader_size  = 0x28;
  58.         bmp.width           = spr.X;
  59.         bmp.height          = Y;
  60.         bmp.bit_planes      = 1;
  61.         bmp.bits_per_pixel  = spr.bpp;
  62.         bmp.compression     = 0;
  63.         bmp.bitmap_size     = size;
  64.         bmp.X_pix_per_meter = 0;
  65.         bmp.Y_pix_per_meter = 0;
  66.         bmp.palette_entries = spr.has_palette;
  67.         bmp.colours         = spr.cols;
  68.  
  69.         write_struct(LE, (void*)&bmp, bmp_whdr_descr, outf);
  70.  
  71.         if(spr.bpp <= 8)
  72.         {
  73.             for(i=0; i<spr.cols; i++)
  74.             {
  75.                 bgr = swap_endian(spr.palette[i]); /* BMP is opposite endian */
  76.                 fputdLE(bgr,outf);
  77.             }
  78.         }
  79.  
  80.         sprintf(string,"Generating BMP %dx%dx%d:",spr.X,Y,spr.bpp);
  81.         progress_start(string);
  82.  
  83.         for(y=Y-1; y>=0; y-=spr.Yasp)
  84.         {
  85.                 y2 = y/spr.Yasp;
  86.  
  87.                 if(spr.bpp != 24)
  88.                 {
  89.                     for(x=0; x<spr.line_size; x++)
  90.                     {
  91.                         p = spr.spr_data[x + y2*spr.line_size];
  92.  
  93.                         switch(spr.bpp)
  94.                         {
  95.                         case 1:
  96.                                 bmp_buf[x] = bit_swap(p);
  97.                                 break;
  98.  
  99.                         case 4:
  100.                                 bmp_buf[x] = bit4_swap(p);
  101.                                 break;
  102.  
  103.                         case 8:
  104.                                 bmp_buf[x] = p;
  105.                                 break;
  106.                         }
  107.                     }
  108.                 }
  109.                 else
  110.                 {
  111.                     for(x=0; x<spr.X; x++)
  112.                     {
  113.                         rgb = read_pixel_RGB(&spr, x, y2);
  114.  
  115.                         bmp_buf[x*3+0] = rgb >> 24;
  116.                         bmp_buf[x*3+1] = rgb >> 16;
  117.                         bmp_buf[x*3+2] = rgb >>  8;
  118.                     }
  119.                 }
  120.                 /* write one or two lines depending on Y aspect */
  121.  
  122.                 fwrite(bmp_buf, line_size, 1, outf);
  123.  
  124.                 if(spr.Yasp == 2)
  125.                         fwrite(bmp_buf, line_size, 1, outf);
  126.  
  127.                 progress(Y-y-1,Y);
  128.         }
  129.         progress_finish();
  130.  
  131.         fclose(inf);
  132.         fclose(outf);
  133.         return(0);
  134. }
  135.