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

  1. /************************************************************************
  2.  *                                                                      *
  3.  * xwd to extended sprite format bitmap converter                       *
  4.  *                                                                      *
  5.  * Reads stdio, outputs to out, with progress indication on err         *
  6.  * Uses new sprite library routines                                     *
  7.  * Handles different bit and byte orders                                *
  8.  *                                                                      *
  9.  * Version 1.60 (18-Nov-1993)                                           *
  10.  *                                                                      *
  11.  * (C) 1993 DEEJ Technology PLC                                         *
  12.  *                                                                      *
  13.  ************************************************************************/
  14.  
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include "io.h"
  19. #include "sprite.h"
  20. #include "xwd.h"
  21.  
  22. int main(int argc, char** argv)
  23. {
  24.         FILE        *inf;
  25.         FILE        *outf;
  26.         FILE        *errf;
  27.         int          i;
  28.         char         c;
  29.         int          x,y;
  30.         int          line_off;
  31.         uchar       *xwd_buf;
  32.         uchar        p;
  33.         int          order;
  34.         xwd_header   hdr;
  35.         xwd_colour   xcol;
  36.         spr_info_str spr;
  37.         char         string[256];
  38.  
  39.         file_args(argc, argv, &inf, &outf, &errf);
  40.  
  41.         read_struct(BE, (BYTE*)&hdr, xwd_header_descr, inf);
  42.  
  43.  
  44.         if(hdr.file_version!=7 && hdr.pixmap_format!=2)
  45.         {
  46.                 fprintf(errf, "Version and format not reqcognised (%d %d)\n",
  47.                                 hdr.file_version, hdr.pixmap_format);
  48.                 return(1);
  49.         }
  50.  
  51.         if(hdr.pixmap_depth != hdr.bits_per_pixel)
  52.         {
  53.                 fprintf(errf, "Depth && BPP not equal (%d %d)\n",
  54.                                 hdr.pixmap_depth, hdr.bits_per_pixel);
  55.                 return(2);
  56.         }
  57.  
  58.         if(hdr.bits_per_pixel > 8)
  59.         {
  60.                 fprintf(errf, "Only images with <= 8 BPP can be handled\n");
  61.                 return(3);
  62.         }
  63.  
  64.         if(hdr.byte_order==1)
  65.                 order = BIG_ENDIAN;
  66.         else
  67.                 order = LITTLE_ENDIAN;
  68.  
  69.         spr.X    = hdr.window_width;
  70.         spr.Y    = hdr.window_height;
  71.         spr.Xasp = 1;
  72.         spr.Yasp = 1;
  73.         spr.bpp  = hdr.bits_per_pixel;
  74.  
  75.         fill_info(&spr);
  76.  
  77.         /* skip additional info in header */
  78.  
  79.         for(i=sizeof(xwd_header); i<hdr.header_size; i++)
  80.                 c = fgetc(inf);
  81.  
  82.         /* read colour map */
  83.  
  84.         for(i=0; i<hdr.ncolours; i++)
  85.         {
  86.                 read_struct(BE, (BYTE*)&xcol, xwd_colour_descr, inf);
  87.  
  88.                 /* colour map components are 16 bit */
  89.  
  90.                 spr.palette[i] = ((xcol.blue  & 0xFF00) << 16) |
  91.                                  ((xcol.green & 0xFF00) << 8)  |
  92.                                   (xcol.red   & 0xFF00);
  93.         }
  94.  
  95.         alloc_spr_data(&spr);
  96.  
  97.         if((xwd_buf=(uchar*)malloc(hdr.bytes_per_line)) == 0)
  98.         {
  99.                 fprintf(errf,"Unable to allocate xwd buffer\n");
  100.                 exit(5);
  101.         }
  102.  
  103.         sprintf(string,"Generating sprite %dx%dx%d:",spr.X,spr.Y,spr.bpp);
  104.         progress_start(string);
  105.  
  106.         for(y=0; y<spr.Y; y++)
  107.         {
  108.                 fread(xwd_buf, hdr.bytes_per_line, 1, inf);
  109.  
  110.                 line_off = y*spr.line_size;
  111.  
  112.                 for(x=0; x<spr.line_size; x++)
  113.                 {
  114.                         p = xwd_buf[x];
  115.  
  116.                         if(hdr.bitmap_bit_order==1)
  117.                         {
  118.                                 switch(spr.bpp)
  119.                                 {
  120.                                 case 1:
  121.                                         spr.spr_data[x+line_off] = bit_swap(p);
  122.                                         break;
  123.  
  124.                                 case 2:
  125.                                         spr.spr_data[x+line_off] = bit2_swap(p);
  126.                                         break;
  127.  
  128.                                 case 4:
  129.                                         spr.spr_data[x+line_off] = bit4_swap(p);
  130.                                         break;
  131.  
  132.                                 case 8:
  133.                                         spr.spr_data[x+line_off] = p;
  134.                                         break;
  135.                                 }
  136.                         }
  137.                         else
  138.                         {
  139.                                         spr.spr_data[x+line_off] = p;
  140.                         }
  141.                 }
  142.                 progress(y,spr.Y);
  143.         }
  144.         write_sprite(&spr, outf);
  145.  
  146.         progress_finish();
  147. }
  148.