home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 460.lha / Qrt_to_IFF24 / qrt2iff24.c < prev    next >
C/C++ Source or Header  |  1991-01-05  |  5KB  |  199 lines

  1. /* Qrt2IFF24.c
  2.  
  3.    Format:   qrt2iff24 <infile> <outfile>
  4.  
  5.    Converts QRT 24bit image files (put out by DKBTrace and QRT) into
  6.    C= Amiga IFF 24 ILBM file without byterun compression.
  7.  
  8.    Works with any resolution QRT/DKB file.
  9.    I didn't check for file write errors so if you run out of
  10.    storage space it'll keep on going till the end...
  11.    Written on Lattice C V5.05.
  12.  
  13.    -Viet Ho (Viet.Ho@samba.acs.unc.edu)
  14.    15-AUG-90 02:48
  15. */
  16.  
  17. #include <stdio.h>
  18. #define EOL 0x0a
  19. main(int argc, char *argv[])
  20.  {
  21.      typedef unsigned long int ULONG;
  22.         typedef unsigned short int UWORD;
  23.      typedef short int WORD;
  24.         typedef unsigned char UBYTE;
  25.  
  26.  
  27.         FILE *fr, *fw;
  28.     ULONG isize,fsize;
  29.         int l,i,j,k,z,rgb[3][1024];
  30.         UWORD w,h;
  31.         UBYTE d,*b,*c,*BMHDloc;
  32.  
  33.  
  34.         UBYTE filesize[3];    /* imgsize + IFF header size */
  35.      UBYTE imgsize[3];     /* w*h*3 for 24 bit images   */
  36.         char bmhdname[] = { "ILBMBMHD",0,0,0x14 };
  37.  
  38.         struct {
  39.                 UWORD w,h;            /* ViewPort size */
  40.                  WORD x,y;            /* Image offset  */
  41.                 UBYTE nPlanes;
  42.                 UBYTE Mask;
  43.         UBYTE rle;
  44.         UBYTE pad;
  45.         UWORD TransColor;
  46.         UBYTE xAspect, yAspect;
  47.         UWORD pageWidth, pageHeight;
  48.                } BMHD;
  49.  
  50.  
  51.  
  52.   if (argc < 3) {
  53.     printf("Not enough filenames given.\n");
  54.     printf("Format: %s <file read> <file write>\n",argv[0]);
  55.     return(1);
  56.   }
  57.                                  /* Set pointer fr for file read */
  58.  
  59.   if (!(fr = fopen(argv[1],"r"))) {
  60.      printf("Error reading file %s.\n",argv[1]);
  61.      printf("Format: %s <file read> <file write>\n",argv[0]);
  62.      return(1);
  63.   }
  64.                      /* Set pointer fw for file write */
  65.   if (!(fw = fopen(argv[2],"w"))) {
  66.      printf("Error opening file %s.\n",argv[2]);
  67.      printf("Format: %s <file read> <file write>\n",argv[0]);
  68.      return(1);
  69.   }
  70.  
  71.  
  72. /* Take in the image resolution */
  73.  
  74.  fread(&isize,4,1,fr);   /* w-lohi h-lohi                */
  75.  
  76.  *b = *c = w = h = 0;
  77.  
  78.    b = &isize;
  79.    c = &w;
  80.    *c = *(b+1) ; *(c+1) = *b;
  81.  
  82.    c = &h;
  83.    *c = *(b+3); *(c+1) = *(b+2);
  84.  
  85.    printf("\nWidth: %d  Height: %d\n\n",w,h);
  86.  
  87.  
  88.                           /* Image resolution imbedded in qrt file:    */
  89.                           /*       w    h                 */
  90.                           /*       lohi|lohi                */
  91.                           /* loc:  0 1  2 3                */
  92.                           /*                        */
  93.  
  94. /* Create IFF ILBM header */
  95.  
  96.      BMHD.w = w;
  97.      BMHD.h = h;
  98.      BMHD.nPlanes = 24;
  99.      BMHD.pageWidth = w;
  100.      BMHD.pageHeight = h;
  101.      BMHD.x = BMHD.y = BMHD.Mask = BMHD.rle = BMHD.pad = BMHD.TransColor = 0;
  102.  
  103.      isize = w*h*3;     /* Generate image size from resolution + 24bits */
  104.      fsize = isize + sizeof(BMHD) + 14;
  105.  
  106.      b = &isize;    /*  We need to break C's 32 bit LONG type down  */
  107.      c = &fsize;    /*  to three bytes (24 bit) IFF limitation.     */
  108.  
  109.      for (i=1;i<4;i++) {         /* Break img/file length into 3 bytes */
  110.      imgsize[i]  = *++b;
  111.          filesize[i] = *++c;
  112.        }
  113.  
  114.  
  115.  /* Dump out the header....don't laugh...it works */
  116.  
  117.       fprintf(fw,"FORM%c%c%c%c",0,filesize[1],filesize[2],filesize[3]);
  118.       fwrite(bmhdname,1,sizeof(bmhdname),fw);
  119.       BMHDloc = &BMHD;
  120.       for (i=1;i<(sizeof(BMHD)+1);i++)  putc(*BMHDloc++,fw);
  121.       fprintf(fw,"BODY%c%c%c%c",0,imgsize[1],imgsize[2],imgsize[3]);
  122.  
  123.  
  124.  
  125.  
  126. /***Diagram******************
  127.  *                  R G B   *
  128.  *    rgb[i][j] i=  0 1 2   *
  129.  *           ^      UBYTE   *
  130.  *           |              *
  131.  *        j= pixel index    *
  132.  ****************************/
  133.  
  134. /*  IFF24 bit ordering for one scanline
  135.  
  136. Pixel         0   1   2   3   4   5  . . . . . --> WIDTH
  137.  
  138. Scanline 0    R0  R0  R0  R0  R0  R0  . . . . .
  139.               R1  R1  R1  R1  R1  R1  . . . . .
  140.               R2  R2  R2  R2  R2  R2  . . . . .
  141.               R3  R3  R3  R3  R3  R3  . . . . .
  142.               R4  R4  R4  R4  R4  R4  . . . . .
  143.               R5  R5  R5  R5  R5  R5  . . . . .
  144.               R6  R6  R6  R6  R6  R6  . . . . .
  145.               R7  R7  R7  R7  R7  R7  . . . . .
  146.  
  147.               G0  G0  G0  G0  G0  G0  . . . . .
  148.               G1  G1  G1  G1  G1  G1  . . . . .
  149.               G2  G2  G2  G2  G2  G2  . . . . .
  150.               G3  G3  G3  G3  G3  G3  . . . . .
  151.               G4  G4  G4  G4  G4  G4  . . . . .
  152.               G5  G5  G5  G5  G5  G5  . . . . .
  153.               G6  G6  G6  G6  G6  G6  . . . . .
  154.               G7  G7  G7  G7  G7  G7  . . . . .
  155.  
  156.               B0  B0  B0  B0  B0  B0  . . . . .
  157.               B1  B1  B1  B1  B1  B1  . . . . .
  158.               B2  B2  B2  B2  B2  B2  . . . . .
  159.               B3  B3  B3  B3  B3  B3  . . . . .
  160.               B4  B4  B4  B4  B4  B4  . . . . .
  161.               B5  B5  B5  B5  B5  B5  . . . . .
  162.               B6  B6  B6  B6  B6  B6  . . . . .
  163.               B7  B7  B7  B7  B7  B7  . . . . .
  164.  
  165.   */
  166.  
  167.  
  168.  
  169.   l = 0;
  170.   while (l++ < h) {
  171.  
  172.      /* Read in a scanline (w) of RGB's (pixels) */
  173.      getc(fr);getc(fr);  /*Weed out scanline ### */
  174.  
  175.      for (i=0;i<3;i++) for (j=0;j<w;j++)  rgb[i][j] = getc(fr);
  176.  
  177.  
  178.     /* Transformation....this should be done in assembly..but..nahhh */
  179.  
  180.     for (i=0;i<3;i++) {
  181.      for (z=0;z<8;z++) {
  182.       for (j=0;j<w;j += 8) {
  183.         d = 0;
  184.         for (k=0;k<8;k++) {
  185.           d <<= 1;
  186.           if (rgb[i][j+k] & 1)  d |= 1;  /* Last bit to be shifted, word shift first */
  187.           rgb[i][j+k] >>= 1;
  188.         }
  189.         putc(d,fw);    /* Writes one byte back out */
  190.       }
  191.      }
  192.     }
  193.    printf("  Scanline: %d\r",l);
  194.   }
  195.    printf("\nDone.\n");
  196.  fclose(fr);
  197.  fclose(fw);
  198.  }
  199.