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

  1. /************************************************************************
  2.  *                                    *
  3.  * Archimedes colour sprite to X windows bitmap/pixmap converter    *
  4.  *                                    *
  5.  * From v2.00 Works with any size of Mode 0/18/23 1x1 aspect ratio sprs *
  6.  * From v3.00 Reads palette to remove the need for inverting later    *
  7.  * From v4.00 Buffers input lines for improved performace        *
  8.  * From v4.10 Recognises VGA & SVGA mono modes                *
  9.  * From v4.20 Uses new sprite library functions                *
  10.  * From v4.30 Uses new io library functions                *
  11.  * From v5.00 Renamed, outputs in bitmap & 2 pixmap formats        *
  12.  *                                    *
  13.  * Version 5.01 (25-Nov-1993)                        *
  14.  *                                    *
  15.  * (C) 1989/1990/1992/1993 DEEJ Technology PLC                *
  16.  *                                    *
  17.  ************************************************************************/
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include "io.h"
  23. #include "sprite.h"
  24.  
  25. void make_bitmap(void);
  26. void make_pixmap(int);
  27.  
  28. FILE *inf, *outf, *errf;
  29. spr_info_str spr;
  30.  
  31. int main(int argc, char **argv)
  32. {
  33.     char type;
  34.     char string[256];
  35.  
  36.     if(argc>1 && argv[1][0]=='-')
  37.     {
  38.         type = argv[1][1];
  39.  
  40.         switch(type)
  41.         {
  42.         case 'h':
  43.             fprintf(stderr,
  44.                 "%s [<flags>] [<sprite>] [<output>] [<error>]\n",
  45.                 argv[0]);
  46.             fprintf(stderr,
  47.                 "<flags> := -h : help\n");
  48.             fprintf(stderr,
  49.                 "<flags> := -b : 2 colour X bitmap format (default)\n");
  50.             fprintf(stderr,
  51.                 "           -c : 2-16 colour X pixmap format\n");
  52.             fprintf(stderr,
  53.                 "           -p : 2-16 colour X pixmap 2 format\n");
  54.             fprintf(stderr,
  55.                 "Input defaults to stdin, output to stdout,\n");
  56.             fprintf(stderr,
  57.                 "error/progress to stderr\n");
  58.  
  59.             return(0);
  60.             break;
  61.  
  62.         case 'b':
  63.         case 'c':
  64.         case 'p':
  65.             /* flag ok */
  66.             break;
  67.  
  68.         default:
  69.             fprintf(stderr,"Unknown flag '%c'\n",type);
  70.             return(1);
  71.             break;
  72.         }
  73.  
  74.         argc--; argv++;
  75.     }
  76.     else
  77.     {
  78.         type = 'b';
  79.     }
  80.  
  81.     file_args(argc, argv, &inf, &outf, &errf);
  82.  
  83.         read_sprite(&spr, inf);
  84.  
  85.         if(type=='b')
  86.     {
  87.         if(spr.bpp!=1)
  88.             {
  89.                     fprintf(errf,"Only 1 BPP bitmaps supported\n");
  90.                     return(2);
  91.             }
  92.         else
  93.         {
  94.                 sprintf(string,"Generating bitmap %dx%d :",spr.X,spr.Y);
  95.             progress_start(string);
  96.  
  97.             make_bitmap();
  98.         }
  99.     }
  100.     else
  101.     {
  102.         if(spr.bpp>4)
  103.         {
  104.                     fprintf(errf,"Only 1-4 BPP pixmaps supported\n");
  105.                     return(2);
  106.         }
  107.         else
  108.         {
  109.                 sprintf(string,"Generating pixmap %dx%dx%d :",
  110.                         spr.X,spr.Y,spr.bpp);
  111.             progress_start(string);
  112.  
  113.             make_pixmap(type=='c' ? 1:2);
  114.         }
  115.     }
  116.  
  117.     progress_finish();
  118.  
  119.         return(0);
  120. }
  121.  
  122. void make_bitmap(void)
  123. {
  124.         int   x,y;
  125.         int   count,inv;
  126.         uchar a;
  127.         uchar *line;
  128.  
  129.         if((spr.palette[0] & 0xFFFFFF00) != 0)
  130.                 inv = 1;
  131.         else
  132.                 inv = 0;
  133.  
  134.         fprintf(outf, "#define bitmap_width %d\n",spr.X);
  135.         fprintf(outf, "#define bitmap_height %d\n",spr.Y);
  136.         fprintf(outf, "static char bitmap_bits[] = {\n");
  137.  
  138.         count=0;
  139.  
  140.         for(y=0; y<spr.Y; y++)
  141.         {
  142.                 line = spr.spr_data + y*spr.line_size;
  143.  
  144.                 for(x=0; x<(spr.X+7)/8; x++)
  145.                 {
  146.                         a=line[x];
  147.                         if(inv) a=a^255;      /* a EOR 255 for invert */
  148.                         fprintf(outf, "0x%X,",a);
  149.                         if(++count>=16)
  150.                         {
  151.                                 fputc('\n', outf);
  152.                                 count=0;
  153.                         }
  154.                 }
  155.                 progress(y,spr.Y);
  156.         }
  157.         fprintf(outf,"\n}\n");
  158.     fflush(outf);
  159. }
  160.  
  161. void make_pixmap(int type)
  162. {
  163. /*
  164.     static char cols[] = " .,:;!|$-=+*o0O@";
  165. */
  166.     static char cols[] = "0123456789ABCDEF";
  167.     char format[256];
  168.     char *line;
  169.     int i,x,y;
  170.  
  171.     if((line=malloc(spr.X+1))==0)
  172.     {
  173.         fprintf(errf,"Unable to allocate line buffer\n");
  174.         exit(3);
  175.     }
  176.  
  177.     if(type==1)
  178.     {
  179.         fprintf(outf,"/* XPM */\n");
  180.         fprintf(outf,"static char *pixmap[] = {\n");
  181.         fprintf(outf,"/* width height ncolours, cpp, [x_hot y_hot] */\n");
  182.         strcpy(format,"\"%d %d %d %d\",\n");
  183.     }
  184.     else
  185.     {
  186.         fprintf(outf,"! XPM2\n");
  187.  
  188.         strcpy(format,"%d %d %d %d\n");
  189.     }
  190.  
  191.     fprintf(outf, format, spr.X, spr.Y, spr.cols, 1);
  192.  
  193.     if(type==1)
  194.     {
  195.         fprintf(outf,"/* colours */\n");
  196.  
  197.         strcpy(format,"\"%c\tc #%04x%04x%04x\",\n");
  198.     }
  199.     else
  200.     {
  201.         strcpy(format,"%c c #%04x%04x%04x\n");
  202.     }
  203.  
  204.     for(i=0; i<spr.cols; i++)
  205.     {
  206.         fprintf(outf,format,cols[i],
  207.                     ((spr.palette[i] >>  8) & 0xFF) * 0x101,
  208.                     ((spr.palette[i] >> 16) & 0xFF) * 0x101,
  209.                     ((spr.palette[i] >> 24) & 0xFF) * 0x101);
  210.     }
  211.  
  212.     if(type==1)
  213.     {
  214.         fprintf(outf,"/* pixels */\n");
  215.  
  216.         strcpy(format,"\"%s\",\n");
  217.     }
  218.     else
  219.     {
  220.         strcpy(format,"%s\n");
  221.     }
  222.  
  223.     for(y=0; y<spr.Y; y++)
  224.     {
  225.         for(x=0; x<spr.X; x++)
  226.         {
  227.             line[x] = cols[read_pixel_val(&spr, x, y)];
  228.         }
  229.  
  230.         if(y==(spr.Y-1) && type==1)
  231.         {
  232.             strcpy(format,"\"%s\"};\n");
  233.         }
  234.  
  235.         fprintf(outf,format,line);
  236.  
  237.         progress(y,spr.Y);
  238.     }
  239. }
  240.