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

  1. /************************************************************************
  2.  *                                    *
  3.  * Archimedes sprite to xwd format bitmap converter             *
  4.  *                                    *
  5.  * Version 1.50 (23-Aug-1993)                        *
  6.  *                                    *
  7.  * (C) 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 "xwd.h"
  17.  
  18. int main(int argc, char **argv)
  19. {
  20.         FILE *inf, *outf, *errf;
  21.         int          i,x,y,Y;
  22.         uchar       *xwd_buf;
  23.         uchar        p;
  24.         spr_info_str spr;
  25.         xwd_header   hdr;
  26.         xwd_colour   xcol[256];
  27.     char         string[256];
  28.  
  29.         file_args(argc, argv, &inf, &outf, &errf);
  30.  
  31.         read_sprite(&spr, inf);
  32.  
  33.         if(spr.bpp > 8)
  34.         {
  35.                 fprintf(errf,"Only images with <= 8 BPP can be converted\n");
  36.                 return(1);
  37.         }
  38.  
  39.         /* account for square/reqtangular pixels */
  40.  
  41.         if(spr.Yasp == 2)
  42.                 Y = spr.Y*2;
  43.         else
  44.                 Y = spr.Y;
  45.  
  46.         if((xwd_buf = (uchar*)malloc(spr.line_size)) == 0)
  47.         {
  48.                 fprintf(errf,"Unable to allocate xwd buffer\n");
  49.                 exit(1);
  50.         }
  51.  
  52.         hdr.header_size       = sizeof(xwd_header);
  53.         hdr.file_version      = 7;
  54.         hdr.pixmap_format     = 2;
  55.         hdr.pixmap_depth      = spr.pix;
  56.         hdr.pixmap_width      = spr.X;
  57.         hdr.pixmap_height     = Y;
  58.         hdr.bits_per_pixel    = spr.bpp;
  59.         hdr.xoffset           = 0;
  60.         hdr.byte_order        = 1;
  61.         hdr.bitmap_bit_order  = 1;
  62.         hdr.bitmap_unit       = spr.bpp==4 ? 8:32;
  63.         hdr.bytes_per_line    = spr.line_size;
  64.         hdr.bitmap_pad        = 32;
  65.         hdr.visual_class      = 3;
  66.         hdr.red_mask          = 0;
  67.         hdr.green_mask        = 0;
  68.         hdr.blue_mask         = 0;
  69.         hdr.bits_per_rgb      = spr.bpp;
  70.         hdr.colourmap_entries = spr.cols;
  71.         hdr.ncolours          = spr.cols;
  72.         hdr.window_width      = spr.X;
  73.         hdr.window_height     = Y;
  74.         hdr.window_x          = 0;
  75.         hdr.window_y          = 0;
  76.         hdr.window_bdrwidth   = 0;
  77.  
  78.         write_struct(BE, (BYTE*)&hdr, xwd_header_descr, outf);
  79.  
  80.         sprintf(string,"Generating XWD %dx%dx%d:",spr.X,Y,hdr.bits_per_pixel);
  81.     progress_start(string);
  82.  
  83.         for(i=0; i<spr.cols; i++)
  84.         {
  85.                 xcol[i].pixel = i;
  86.                 xcol[i].red   = ((spr.palette[i] & 0x0000FF00) >>  8) * 0x0101;
  87.                 xcol[i].green = ((spr.palette[i] & 0x00FF0000) >> 16) * 0x0101;
  88.                 xcol[i].blue  = ((spr.palette[i] & 0xFF000000) >> 24) * 0x0101;
  89.                 xcol[i].flags = 0x07;
  90.                 write_struct(BE, (BYTE*)&xcol[i], xwd_colour_descr, outf);
  91.         }
  92.  
  93.         for(y=0; y<Y; y+=spr.Yasp)
  94.         {
  95.                 for(x=0; x<spr.line_size; x++)
  96.                 {
  97.                         p = spr.spr_data[x+(y/spr.Yasp)*spr.line_size];
  98.  
  99.                         switch(spr.bpp)
  100.                         {
  101.                         case 1:
  102.                                 xwd_buf[x] = bit_swap(p);
  103.                                 break;
  104.  
  105.                         case 2:
  106.                                 xwd_buf[x] = bit2_swap(p);
  107.                                 break;
  108.  
  109.                         case 4:
  110.                                 xwd_buf[x] = bit4_swap(p);
  111.                                 break;
  112.  
  113.                         case 8:
  114.                                 xwd_buf[x] = p;
  115.                                 break;
  116.                         }
  117.                 }
  118.  
  119.                 /* write one or two lines depending on Y aspect */
  120.  
  121.                 fwrite(xwd_buf, spr.line_size, 1, outf);
  122.  
  123.                 if(spr.Yasp == 2)
  124.                         fwrite(xwd_buf, spr.line_size, 1, outf);
  125.  
  126.                 progress(y,Y);
  127.         }
  128.  
  129.         progress_finish();
  130. }
  131.