#include <stdio.h>
#include <alloc.h>

typedef struct { unsigned char id_chars;
                 unsigned char map_type;
                 unsigned char image_type;
                 unsigned int  map_origin;
                 unsigned int  map_length;
                 unsigned char map_entry_size;
                 unsigned int  x_origin;
                 unsigned int  y_origin;
                 unsigned int  width;
                 unsigned int  height;
                 unsigned char bpp;
                 unsigned char desc;
               } targa_header;

typedef struct { unsigned int width,
                              height,
                              colors;
               } img_header;

typedef struct { unsigned char r,
                               g,
                               b;
               } palette[256];


void main(int argc, char *argv[])
{
    FILE *i, *l, *r;
    char *left, *right;
    targa_header th;
    img_header ih;
    palette pal;
    unsigned char temp;
    long size;
    int loop;

    if (argc < 3)
    {
        printf("Usage %s infile.img outleft.tga outright.tga\n", argv[0]);
        exit(-1);
    }

    if ((i = fopen(argv[1], "rb")) == NULL)
    {
        printf("Could not open input file %s.\n", argv[1]);
        exit(-1);
    }

    if ((l = fopen(argv[2], "wb")) == NULL)
    {
        printf("Could not open left output file %s.\n", argv[2]);
        exit(-1);
    }

    if ((r = fopen(argv[3], "wb")) == NULL)
    {
        printf("Could not open right output file %s.\n", argv[3]);
        exit(-1);
    }

    fread(&ih, sizeof(ih), 1, i);         /* Read in the 6 byte .img header */
    fread(&pal, sizeof(pal), 1, i);       /* Read in the palette */

    size = ih.width * ih.height;          /* Calculate the size of he image */
                                          /* for the malloc() */

    left = malloc(size);                  /* Allocate memory for the images */
    right = malloc(size);                 /* Do both at once, as no image */
                                          /* should be larger than 256K total */

    fread(left, size, 1, i);              /* Read in the left image */
    fread(right, size, 1, i);             /* Read in the right image */

    for (loop = 0; loop < 255; loop++) /* .IMG files have palette entries */
    {                                  /* in the range 0..63, so we have to */
        temp = pal[loop].r * 4;        /* expand them to 0..255 */
        pal[loop].r = pal[loop].b * 4;
        pal[loop].g *= 4;              /* Targa palettes are also ordered */
        pal[loop].b = temp;            /* BGR, not RGB, so we have to swap */
    }                                  /* the R and B components */

    th.id_chars = 0;                   /* No image identification field */
    th.map_type = 1;                   /* Has to be 1 for image_type 1 */
    th.image_type = 1;                 /* Color-mapped uncompressed image */
    th.map_origin = 0;                 /* Color map starts at offset zero */
    th.map_length = 256;               /* and is 256 entries long */
    th.map_entry_size = 24;            /* Each palette entry is 24 bits long */
    th.x_origin = 0;                   /* The image origin is in the left, */
    th.y_origin = 0;                   /* top of the screen */
    th.width = 320;                    /* Obvious */
    th.height = 200;                   /* Obvious */
    th.bpp = 8;                        /* There are 8 bits per pixel */
    th.desc = 32;                      /* Bits 0..3: Attribute bits per pixel */
                                       /*         4: Reserved - set to 0 */
                                       /*         5: Screen origin */
                                       /*            0: Lower left corner */
                                       /*            1: Upper right corner */
                                       /*      6..7: Interleave */
                                       /*            00: Non-interleaved */
                                       /*            01: 2-way interleave */
                                       /*            10: 4-way interleave */
                                       /*            11: Reserved */

    close(i);                          /* Close the input file */

    fwrite(&th, sizeof(th), 1, l);     /* Write out the Targa header */
    fwrite(&pal, sizeof(pal), 1, l);   /* Write the palette */
    fwrite(left, size, 1, l);          /* Write the image */
    close(l);                          /* Close the file */

    fwrite(&th, sizeof(th), 1, r);     /* Write out the Targa header */
    fwrite(&pal, sizeof(pal), 1, r);   /* Write the palette */
    fwrite(right, size, 1, r);         /* Write the image */
    close(r);                          /* Close the file */

    free(left);                        /* Free the left image memory */
    free(right);                       /* Free the left image memory */
}
