home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / 2 / 24bittls.zip / PRO2BMP.C < prev    next >
C/C++ Source or Header  |  1991-10-28  |  4KB  |  183 lines

  1. /* Pro2BMP.c - converts Amiga 3-D Professional RGB files to 24-bit
  2.    Microsoft Windows 3.0 format - djh */
  3.    
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #define PROIDLEN 9
  8. #define STD_METHOD 1
  9. #define RGBSIZE 3 /* Aztec pads sizeof(rgb) to 4! */
  10.  
  11. typedef struct {
  12.   /*char identifier[9];*/
  13.   short width,height,vxoff,vyoff,
  14.         vwidth,vheight;
  15.   long dflags,method;
  16. } ProRGBHeader;
  17.  
  18. typedef struct {
  19.   unsigned char r,g,b;
  20. } rgb;
  21.  
  22. #define BM 0x4D42
  23.  
  24. typedef struct {
  25.   unsigned char b,g,r;
  26. } w_rgb;
  27.  
  28. typedef struct {
  29.   /*short bfType;    avoid SPARC alignment problems - deal separately! */
  30.   long bfSize;
  31.   short bfReserved1,bfReserved2;
  32.   long bfOffBits;
  33. } BITMAPFILEHEADER;
  34.  
  35. typedef struct {
  36.   long biSize,biWidth,biHeight;
  37.   short biPlanes,biBitCount;
  38.   long biCompression,biSizeImage,
  39.        biXPelsPerMeter,biYPelsPerMeter,
  40.        biClrUsed,biClrImportant;
  41. } BITMAPINFOHEADER;
  42.  
  43. SafeRead(fp,buf,len)
  44. FILE *fp;
  45. char *buf;
  46. int len;
  47. {
  48.   if (fread(buf,1,len,fp)!=len) {
  49.     puts("\nError while reading file.\n");
  50.     return(TRUE);
  51.   }
  52.  
  53.   return(FALSE);
  54. }
  55.  
  56. SafeWrite(fp,buf,len)
  57. FILE *fp;
  58. char *buf;
  59. int len;
  60. {
  61.   if (fwrite(buf,1,len,fp)!=len) {
  62.     puts("\nError while writing file.\n");
  63.     return(TRUE);
  64.   }
  65.  
  66.   return(FALSE);
  67. }
  68.  
  69. Swap4(arg)
  70. int arg;
  71. {
  72.    return (
  73.      ((arg&0xFF000000) >> 24) |
  74.      ((arg&0x00FF0000) >> 8) |
  75.      ((arg&0x0000FF00) << 8) |
  76.      ((arg&0x000000FF) << 24));
  77. }
  78.  
  79. Swap2(arg)
  80. int arg;
  81. {
  82.    return (
  83.      ((arg&0xFF00) >> 8) |
  84.      ((arg&0x00FF) << 8));
  85. }
  86.  
  87.    unsigned char 
  88. main(argc,argv)
  89. int argc;
  90. char *argv[];
  91. {
  92.   static char pro_id[PROIDLEN+1];
  93.   ProRGBHeader pro_hdr;
  94.   static BITMAPFILEHEADER bm;
  95.   static BITMAPINFOHEADER bi;
  96.   UBYTE *buf=NULL,*tmp;
  97.   rgb *switch_p,color;
  98.   w_rgb *switch_b;
  99.   int i,j,linesize,bufsize;
  100.   unsigned short word;
  101.   FILE *fp=NULL;
  102.  
  103.   puts("Pro2BMP: 3-D Professional to Windows converter - djh\n");
  104.  
  105.   if (argc<3) {
  106.     puts("Usage: Pro2BMP <infile> <outfile>");
  107.     goto cleanup;
  108.   }
  109.  
  110.   if (!(fp=fopen(argv[1],"r"))) {
  111.     printf("Error: couldn't open ProRGB file %s for reading.\n",argv[1]);
  112.     goto cleanup;
  113.   }
  114.  
  115.   if (SafeRead(fp,pro_id,PROIDLEN)) goto cleanup;
  116.   if (SafeRead(fp,&pro_hdr,sizeof(pro_hdr))) goto cleanup;
  117.  
  118.   if (pro_hdr.method!=STD_METHOD) {
  119.     printf("\nError: storage method %d not recognized!\n",pro_hdr.method);
  120.     goto cleanup;
  121.   }
  122.  
  123.   printf("Converting [Width=%d,Height=%d]... ",pro_hdr.width,pro_hdr.height);
  124.  
  125.   linesize=pro_hdr.width*RGBSIZE;
  126.   bufsize=linesize*pro_hdr.height;
  127.  
  128.   tmp=buf=AllocMem(bufsize,MEMF_CHIP|MEMF_CLEAR);
  129.  
  130.   for (i=0;i<pro_hdr.height;i++) {
  131.     if (SafeRead(fp,tmp,linesize)) goto cleanup;
  132.     tmp+=linesize;
  133.   }
  134.  
  135.   fclose(fp); fp=NULL;
  136.  
  137.   if (!(fp=fopen(argv[2],"w"))) {
  138.     printf("Error: couldn't open BMP file %s for writing.\n",argv[2]);
  139.     goto cleanup;
  140.   }
  141.  
  142.   word=Swap2(BM);
  143.   if (SafeWrite(fp,&word,sizeof(word))) goto cleanup;
  144.  
  145.   bm.bfSize=
  146.     Swap4(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(short)+bufsize);
  147.   bm.bfOffBits=
  148.     Swap4(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(short));
  149.   if (SafeWrite(fp,&bm,sizeof(bm))) goto cleanup;
  150.  
  151.   bi.biSize=Swap4(sizeof(BITMAPINFOHEADER));
  152.   bi.biSizeImage=Swap4(bufsize);
  153.   bi.biWidth=Swap4(pro_hdr.width);
  154.   bi.biHeight=Swap4(pro_hdr.height);
  155.   bi.biPlanes=Swap2(1);
  156.   bi.biBitCount=Swap2(24);
  157.  
  158.   if (SafeWrite(fp,&bi,sizeof(bi))) goto cleanup;
  159.   
  160.   for (i=0;i<pro_hdr.height;i++) {
  161.     tmp-=linesize;
  162.  
  163.     switch_p=(rgb *)tmp;
  164.     switch_b=(w_rgb *)tmp;
  165.  
  166.     for (j=0;j<pro_hdr.width;j++) {
  167.       color=*switch_p++;
  168.       switch_b->r=color.r;
  169.       switch_b->g=color.g;
  170.       switch_b->b=color.b;
  171.       switch_b++;
  172.     }
  173.  
  174.     if (SafeWrite(fp,tmp,linesize)) goto cleanup;
  175.   }
  176.  
  177.   puts("Done.");
  178.  
  179. cleanup:
  180.   if (buf) FreeMem(buf,bufsize);
  181.   if (fp) fclose(fp);
  182. }
  183.