home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine 1996 / ARCHIVE_96.iso / discs / shareware / share_43 / source / c / GLUETGA < prev    next >
Text File  |  1991-08-22  |  6KB  |  216 lines

  1. /*********************************************************
  2.  
  3. GLUETGA.C  -  Glues together several partial .TGA files
  4.               (using the header Y offset for line #) and
  5.           concatenates them into one full screen .TGA
  6.           file of whatever resolution used.  Nearly any
  7.           resolution should work, but all partial files
  8.           MUST be of the same overall frame X-Y size.
  9.  
  10.           By Aaron A. Collins, written on 6/2/90
  11.  
  12.           This file is released to the Public Domain.
  13.  
  14.           PICLAB is a trademark of The Stone Soup Group.
  15.           TARGA is a trademark of AT&T/Truevision.
  16.       
  17.  *********************************************************/
  18.  
  19. #include "frame.h"
  20.  
  21. #define HDRLEN 18L
  22.  
  23. void main(argc,argv)
  24. int argc;
  25. char *argv[];
  26. {
  27.     int xres, yres, xhi, yhi, yorigin, yorghi, outxres, outyres, numfiles;
  28.     register int x, y, index;
  29.     char outname[80], tmpname[80];
  30.     char inname[20][80];
  31.     unsigned char *buffer;
  32.     long posn;
  33.     FILE *in, *out;
  34.  
  35.     printf("\n\nTARGA-24 Image File Concatenation Utility\n");
  36.     printf("By Aaron A. Collins.  Written 3/30/90\n\n");
  37.  
  38.     if (argc < 3)
  39.     {
  40.         printf("Usage: %s OutFile[.TGA] InFile[.TGA] [InFile[.TGA]] ... \n\n",argv[0]);
  41.         exit(1);
  42.     }
  43.  
  44.     strcpy(outname, argv[1]);      /* get output filename */
  45.  
  46.     numfiles = argc - 2;          /* number of input files to do */
  47.  
  48.     for (x = 0; x < numfiles; x++)
  49.     {
  50.         strcpy(&inname[x][0], argv[2 + x]); /* get in filename(s) */
  51.         strcpy(tmpname, &inname[x][0]);
  52.         strupr(tmpname);        /* Cvt to uppercase */
  53.         if (!strstr(tmpname, ".TGA"))    /* didn't supply .TGA ext, */
  54.             if (!strchr(tmpname, '.')) /*AND didn't give ANY ext*/
  55.                 strcat(&inname[x][0], ".TGA"); /*add on .TGA*/
  56.     }
  57.  
  58.     if ((in = fopen(&inname[0][0], "rb")) == NULL)  /*try 1st file ext*/
  59.     {
  60.         printf("ERROR - Couldn't open file %s[.TGA]\n", argv[1]);
  61.         exit(1);
  62.     }
  63.  
  64.     strcpy(tmpname, outname);
  65.     strupr(tmpname);        /* Cvt to uppercase */
  66.     if (!strstr(tmpname, ".TGA"))    /* if user didn't supply .TGA ext, */
  67.         if (!strchr(tmpname, '.'))  /* AND didn't supply ANY ext */
  68.             strcat(outname, ".TGA");
  69.     
  70.  
  71.     if ((out = fopen(outname, "wb")) == NULL)
  72.     {
  73.         printf("ERROR - Couldn't create file %s\n", outname);
  74.         fclose(in);
  75.         exit(1);
  76.     }
  77.  
  78.     printf("Creating Output file = %s\n\n", outname); /* show stats */
  79.  
  80.     /** Copy 1st part of standard Targa header **/
  81.  
  82.     for (x = 0; x < 10; x++)    /* 00, 00, 02, then 7 00's... */
  83.         fputc(fgetc(in), out);
  84.  
  85.     /** discard y orgin, and rewrite 0 as new in rest of Targa hdr **/
  86.  
  87.     fgetc(in);
  88.     fgetc(in);
  89.     fputc(0, out);
  90.     fputc(0, out);
  91.     
  92.     /** load x and y resolution, and rewrite as rest of Targa hdr **/
  93.  
  94.     outxres = fgetc(in);            /* lo order */
  95.     xhi = fgetc(in);            /* hi order */
  96.     outxres += ((unsigned int) xhi) << 8;
  97.  
  98.     fputc(outxres & 0x00ff, out);        /* write lo order */
  99.     fputc((outxres & 0xff00) >> 8, out);    /* write hi order */
  100.  
  101.     outyres = fgetc(in);            /* now do yres the same... */
  102.     yhi = fgetc(in);
  103.     outyres += ((unsigned int) yhi) << 8;
  104.  
  105.     fputc(outyres & 0x00ff, out);
  106.     fputc((outyres & 0xff00) >> 8, out);
  107.  
  108.     fputc(fgetc(in), out);    /* 24 bits/pixel (16 million colors!) (24) */
  109.     fputc(fgetc(in), out);    /* Set bit to indicate top-down display (32)*/
  110.     
  111.     fclose(in);        /* Close the input file, read body later. */
  112.     
  113.     printf("Image  X  resolution = %d\n", outxres);     /* show more stats */
  114.     printf("Image  Y  resolution = %d\n\n", outyres);
  115.  
  116.     if ((buffer = malloc(outxres * 3)) == NULL)
  117.     {
  118.         printf("ERROR - couldn't allocate memory for buffer!\n");
  119.         fclose (out);
  120.         unlink (outname);
  121.     }
  122.  
  123.     /* write out zeroes to the entire output file's body */
  124.  
  125.     for (x = 0; x < outxres; x++)        /* clear line buffer */
  126.     {
  127.         index = x * 3;
  128.         buffer[index++] = '\0';        /* write null BGR triples */
  129.         buffer[index++] = '\0';
  130.         buffer[index] = '\0';
  131.     }
  132.     
  133.     for (y = 0; y < outyres; y++)         /* for the number of lines */
  134.         if (fwrite(buffer, 3, outxres, out) != outxres) /*wrt a line*/
  135.         {
  136.             printf("ERROR - Couldn't create file %s - out of disk space?\n", outname);
  137.             fclose(in);
  138.             fclose(out);
  139.             unlink(outname);
  140.             exit(1);
  141.         }
  142.  
  143.     for (index = 0; index < numfiles; index++) /* for # of input files */
  144.     {
  145.         if ((in = fopen(&inname[index][0], "rb")) == NULL)
  146.         {
  147.             printf("ERROR - Couldn't open file %s\n", &inname[index][0]);
  148.             fclose(out);
  149.             unlink(outname);
  150.             exit(1);
  151.         }
  152.         printf("Glueing File:  %s\t", &inname[index][0]);
  153.  
  154.         /** Read and discard 1st part of standard Targa header **/
  155.  
  156.         for (x = 0; x < 10; x++)   /* 00, 00, 02, then 7 00's... */
  157.             fgetc(in);
  158.  
  159.         /** load y origin for this segment... **/
  160.  
  161.         yorigin = fgetc(in);
  162.         yorghi = fgetc(in);
  163.         yorigin += ((unsigned int) yorghi) << 8;
  164.         
  165.         /** load x and y resolution, check if okay... **/
  166.  
  167.         xres = fgetc(in);            /* lo order */
  168.         xhi = fgetc(in);            /* hi order */
  169.         xres += ((unsigned int) xhi) << 8;
  170.         if (xres > outxres)            /* too big? */
  171.         {
  172.             printf("ERROR - X res. of %s (%d) exceeds maximum (%d)!\n", &inname[index][0], xres, outxres);
  173.             fclose(in);            /* close files */
  174.             fclose(out);
  175.             unlink(outname);        /* delete bad file */
  176.             exit(1);
  177.         }
  178.  
  179.         yres = fgetc(in);        /* now do yres the same... */
  180.         yhi = fgetc(in);
  181.         yres += ((unsigned int) yhi) << 8;
  182.         if (yres > outyres)            /* too big? */
  183.         {
  184.             printf("ERROR - Y res. of %s (%d) exceeds maximum (%d)!\n", &inname[index][0], yres, outyres);
  185.             fclose(in);            /* close files */
  186.             fclose(out);
  187.             unlink(outname);        /* delete bad file */
  188.             exit(1);
  189.         }
  190.  
  191.         fgetc(in); /* absorb 24 bits/pixel (16 million colors!) (24)*/
  192.         fgetc(in); /* also bit to indicate top-down display (32)*/
  193.  
  194.         printf("to line:   %d", yorigin);
  195.  
  196.         posn = (long) yorigin * ((long) outxres * 3) + HDRLEN;
  197.         fseek(out, posn, 0);    /* position output file for copy */
  198.  
  199.         for (y = yorigin; y < yres; y++) /* for every line in file */
  200.         {
  201.             printf("\b\b\b%3d", y);       /* disp. current line # */
  202.  
  203.             /* read in a whole line */
  204.             if (fread(buffer, 3, xres, in) != xres)
  205.                 break;        /* stop if less bytes read */
  206.  
  207.             fwrite(buffer, 3, xres, out); /* copy BGR triplets */
  208.         }
  209.         printf("\n");
  210.         fclose(in);        /* close this input file */
  211.     }
  212.     fflush(out);            /* close output file when done */
  213.     fclose(out);
  214.     exit(0);
  215. }
  216.