home *** CD-ROM | disk | FTP | other *** search
/ World of Graphics / WOGRAPH.BIN / 617.MTV2TGA.C < prev    next >
C/C++ Source or Header  |  1992-02-06  |  4KB  |  136 lines

  1. /*
  2.  * MTV to Targa graphics file converter
  3.  * EWZ 02-05-92 16:20 EST
  4.  *
  5.  * Note: This is not a general purpose conversion program. I didn't have
  6.  *       the specification for either of these graphics formats. I just
  7.  *     looked at the code for Rayshade 4.0 and DKB 2.12. So it 
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13.  
  14. struct targa_header_tag {
  15.    char fill1[2];        /* Filler all 0's */
  16.    char signature;        /* Signature Byte 2 */
  17.    char fill2[7];        /* Filler all 0's */
  18.    char fill3[2];        /* Filler all 0's */
  19.    char width_lo;        /* High and Low byte of width */
  20.    char width_hi;
  21.    char height_lo;        /* High and Low byte of height */
  22.    char height_hi;
  23.    char bit_per_pel;        /* 24 bits per pels */
  24.    char bitmask;        /* 32 bitmask, pertinent bit: top-down raster */
  25. } targa_header;
  26.  
  27. struct targa_pel_tag {
  28.    char blue;
  29.    char green;
  30.    char red;
  31. } targa_pel;
  32.  
  33. struct mtv_pel_tag {
  34.    char red;
  35.    char green;
  36.    char blue;
  37. } mtv_pel;
  38.  
  39. /* Function prototypes */
  40. int ReadWriteHeaders(int *ImageWidth, int *ImageHeight);
  41. int ReadWritePels(int ImageWidth, int ImageHeight);
  42. int main(int argc, char **argv);
  43.  
  44. /* Global variables */
  45. FILE *In_FileHandle;        /* Input filehandle - mtv file */
  46. FILE *Out_FileHandle;        /* Output filehandle - targa file */
  47. char In_Buffer[16000];
  48. char Out_Buffer[16000];
  49.  
  50. int main(int argc, char **argv)
  51. {
  52.    char In_Filename[80];    /* Input file name */
  53.    char Out_Filename[80];    /* Output file name */
  54.    int ImageWidth;
  55.    int ImageHeight;
  56.  
  57.    /* Get the filename if available */
  58.    if (argc > 1) {
  59.       sprintf(In_Filename, "%s.mtv", argv[1]);
  60.       sprintf(Out_Filename, "%s.tga", argv[1]);
  61.    } else {
  62.       printf("Usage: mtv2tga filename (without extension)\n");
  63.       return 0;
  64.    }
  65.  
  66.    /* Open the files, if any error report to user and quit */
  67.    if ((In_FileHandle = fopen(In_Filename, "rb")) == NULL) {
  68.       printf("Error opening input file %s\n", In_Filename);
  69.       return -1;
  70.    }
  71.    if ((Out_FileHandle = fopen(Out_Filename, "wb")) == NULL) {
  72.       printf("Error opening output file %s\n", Out_Filename);
  73.       return -2;
  74.    }
  75.  
  76.    /* Setup buffers to get a little better performance out of the I/O */
  77.    setvbuf(In_FileHandle, In_Buffer, _IOFBF, sizeof(In_Buffer));
  78.    setvbuf(Out_FileHandle, Out_Buffer, _IOFBF, sizeof(Out_Buffer));
  79.  
  80.    if (ReadWriteHeaders(&ImageWidth, &ImageHeight) < 0)
  81.       return -3;
  82.  
  83.    if (ReadWritePels(ImageWidth, ImageHeight) < 0)
  84.       return -4;
  85.  
  86.    fclose(In_FileHandle);
  87.    fclose(Out_FileHandle);
  88.    return 0;
  89. }
  90.  
  91. int ReadWriteHeaders(int *ImageWidth, int *ImageHeight)
  92. {
  93.    if (fscanf(In_FileHandle, "%d %d\n", ImageWidth, ImageHeight) != 2) {
  94.       printf("Error reading mtv header\n");
  95.       return -1;
  96.    }
  97.    printf("Image size %dx%d ", *ImageWidth, *ImageHeight);
  98.  
  99.    memset(&targa_header, 0, sizeof(targa_header));
  100.    targa_header.width_lo = (char) (*ImageWidth % 256);
  101.    targa_header.width_hi = (char) (*ImageWidth / 256);
  102.    targa_header.height_lo = (char) (*ImageHeight % 256);
  103.    targa_header.height_hi = (char) (*ImageHeight / 256);
  104.    targa_header.bit_per_pel = (char) 24;
  105.    targa_header.bitmask = (char) 32;
  106.    targa_header.signature = (char) 2;
  107.  
  108.    /* Write the targa header out to the file */
  109.    fwrite(&targa_header, sizeof(targa_header), 1, Out_FileHandle);
  110.    return 0;
  111. }
  112.  
  113. int ReadWritePels(int ImageWidth, int ImageHeight)
  114. {
  115.    int Width;
  116.    int Height;
  117.  
  118.    for (Height = 0; Height < ImageHeight; Height++) {
  119.       for (Width = 0; Width < ImageWidth; Width++) {
  120.      if (fread(&mtv_pel, sizeof(mtv_pel), 1, In_FileHandle) != 1) {
  121.         printf("\nTrouble reading MTV line\n");
  122.         return -1;
  123.      }
  124.  
  125.      /* Convert pels; different ordering */
  126.      targa_pel.red = mtv_pel.red;
  127.      targa_pel.blue = mtv_pel.blue;
  128.      targa_pel.green = mtv_pel.green;
  129.      fwrite(&targa_pel, sizeof(targa_pel), 1, Out_FileHandle);
  130.       }
  131.       printf("%4d\b\b\b\b", Height);
  132.    }
  133.    printf("done\n");
  134.    return 0;
  135. }
  136.