home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / TGAUTL.ZIP / VSTAMP.C < prev    next >
C/C++ Source or Header  |  1990-03-26  |  9KB  |  394 lines

  1. /*
  2. ** Copyright (c) 1989, 1990
  3. ** Truevision, Inc.
  4. ** All Rights Reserverd
  5. **
  6. ** VSTAMP displays postage stamp data from a Truevision(R) TGA(tm) file
  7. ** on an ATVista(R) using STAGE(tm) routines.
  8. */
  9.  
  10. #include <malloc.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <stage.h>
  18. #include "tga.h"
  19.  
  20. extern void        main( int, char ** );
  21. extern void        DisplayPostageStamp( FILE *, TGAFile * );
  22. extern UINT8    ReadByte( FILE * );
  23. extern void        ReadCharField( FILE *, char *, int );
  24. extern int        ReadExtendedTGA( FILE *, TGAFile * );
  25. extern UINT16    ReadShort( FILE * );
  26. extern UINT32    ReadLong( FILE * );
  27.  
  28.  
  29. TGAFile        f;                /* control structure of image data */
  30.  
  31. char        *versionStr =
  32. "Truevision(R) ATVista(R) Postage Stamp Viewer Version 2.0 - March 24, 1990";
  33.  
  34. char        copyBuf[1024];
  35.  
  36. void
  37. main( argc, argv )
  38. int argc;
  39. char **argv;
  40. {
  41.     int            fileFound;
  42.     float        zoomFact;
  43.     double        doubleTmp;
  44.     char        *q;
  45.     FILE        *fp;
  46.     char        fileName[80];
  47.     struct stat    statbuf;
  48.  
  49.     puts( versionStr );
  50.     /*
  51.     ** The program can be invoked without an argument, in which case
  52.     ** the user will be prompted for the name of the image file to be
  53.     ** examined, or the image file name can be provided as an argument
  54.     ** to the command.
  55.     **
  56.     ** File names provided do not need to include the extension if
  57.     ** the image file extension is one of the standard strings common
  58.     ** to Truevision TGA image file names ( e.g., TGA, WIN, VST, VDA, ICB )
  59.     */
  60.     if ( StageAutoInit() == 0 )
  61.     {
  62.         puts( "VStamp: Unable to load or initialize STAGE" );
  63.         exit( 0 );
  64.     }
  65.     zoomFact = (float)1.0;
  66.     if ( argc == 1 )
  67.     {
  68.         printf( "Enter name of file to examine: " );
  69.         gets( fileName );
  70.         if ( strlen( fileName ) == 0 ) exit( 0 );
  71.     }
  72.     else
  73.     {
  74.         strcpy( fileName, argv[1] );
  75.         if ( argc > 2 )
  76.         {
  77.             doubleTmp = atof( argv[2] );
  78.             if ( doubleTmp > 0.0 )
  79.                 zoomFact = (float)doubleTmp;
  80.         }
  81.     }
  82.     /*
  83.     ** See if we can find the file as specified or with one of the
  84.     ** standard filename extensions...
  85.     */
  86.     fileFound = 0;
  87.     if ( stat( fileName, &statbuf ) == 0 ) fileFound = 1;
  88.     else
  89.     {
  90.         strcat( fileName, ".tga" );
  91.         q = strchr( fileName, '.' );
  92.         if ( stat( fileName, &statbuf ) == 0 ) fileFound = 1;
  93.         else
  94.         {
  95.             strcpy( q, ".vst" );
  96.             if ( stat( fileName, &statbuf ) == 0 ) fileFound = 1;
  97.             else
  98.             {
  99.                 strcpy( q, ".vda" );
  100.                 if ( stat( fileName, &statbuf ) == 0 ) fileFound = 1;
  101.                 else
  102.                 {
  103.                     strcpy( q, ".icb" );
  104.                     if ( stat( fileName, &statbuf ) == 0 ) fileFound = 1;
  105.                     else
  106.                     {
  107.                         strcpy( q, ".win" );
  108.                         if ( stat( fileName, &statbuf ) == 0 ) fileFound = 1;
  109.                         else
  110.                         {
  111.                             *q = '\0';
  112.                             printf("Unable to open image file %s\n", fileName );
  113.                         }
  114.                     }
  115.                 }
  116.             }
  117.         }
  118.     }
  119.     if ( fileFound )
  120.     {
  121.         printf( "Image File: %s\n", fileName );
  122.         fp = fopen( fileName, "rb" );
  123.         /*
  124.         ** It would be nice to be able to read in the entire
  125.         ** structure with one fread, but compiler dependent
  126.         ** structure alignment precludes the simplistic approach.
  127.         ** Instead, fill each field individually, and use routines
  128.         ** that will allow code to execute on various hosts by
  129.         ** recompilation with particular compiler flags.
  130.         **
  131.         ** Start by reading the fields associated with the original
  132.         ** TGA format.
  133.         */
  134.         f.idLength = ReadByte( fp );
  135.         f.mapType = ReadByte( fp );
  136.         f.imageType = ReadByte( fp );
  137.         f.mapOrigin = ReadShort( fp );
  138.         f.mapLength = ReadShort( fp );
  139.         f.mapWidth = ReadByte( fp );
  140.         f.xOrigin = ReadShort( fp );
  141.         f.yOrigin = ReadShort( fp );
  142.         f.imageWidth = ReadShort( fp );
  143.         f.imageHeight = ReadShort( fp );
  144.         f.pixelDepth = ReadByte( fp );
  145.         f.imageDesc = ReadByte( fp );
  146.         memset( f.idString, 0, 256 );
  147.         if ( f.idLength > 0 )
  148.         {
  149.             fread( f.idString, 1, f.idLength, fp );
  150.         }
  151.         /*
  152.         ** Now see if the file is the new (extended) TGA format.
  153.         */
  154.         if ( !fseek( fp, statbuf.st_size - 26, SEEK_SET ) )
  155.         {
  156.             f.extAreaOffset = ReadLong( fp );
  157.             f.devDirOffset = ReadLong( fp );
  158.             fgets( f.signature, 18, fp );
  159.             if ( strcmp( f.signature, "TRUEVISION-XFILE." ) )
  160.             {
  161.                 /*
  162.                 ** Reset offset values since this is not a new TGA file
  163.                 */
  164.                 puts( "VStamp: File is not in extended TGA format." );
  165.                 exit( 0 );
  166.             }
  167.             if ( f.extAreaOffset )
  168.             {
  169.                 if ( ReadExtendedTGA( fp, &f ) >= 0 )
  170.                 {
  171.                     if ( f.stampOffset )
  172.                     {
  173.                         DisplayPostageStamp( fp, &f );
  174.                         if ( argc > 2 )
  175.                         {
  176.                             SetZoom( FloatToFixed(zoomFact),
  177.                                         FloatToFixed(zoomFact) );
  178.                         }
  179.                     }
  180.                     else
  181.                     {
  182.                         puts( "VStamp: File does not contain postage stamp data." );
  183.                     }
  184.                 }
  185.             }
  186.             else
  187.             {
  188.                 puts( "VStamp: File does not contain extension area." );
  189.             }
  190.         }
  191.         else
  192.         {
  193.             puts( "VStamp: Error seeking to end of file for possible extension data" );
  194.         }
  195.         fclose( fp );
  196.         exit( 0 );
  197.     }
  198. }
  199.  
  200.  
  201.  
  202. void
  203. DisplayPostageStamp( fp, sp )
  204. FILE    *fp;
  205. TGAFile    *sp;
  206. {
  207.     Rect    r;
  208.     int        i;
  209.     char    *p;
  210.     int        bytesPerPixel;
  211.     int        bytesInRow;
  212.     int        orient;
  213.  
  214.     /*
  215.     ** If we got this far, the file pointer should be pointing
  216.     ** to the first byte of the postage stamp data (just after
  217.     ** the two bytes specifying the size of the stamp).
  218.     */
  219.     bytesPerPixel = (sp->pixelDepth + 7) >> 3;
  220.     bytesInRow = sp->stampWidth * bytesPerPixel;
  221.     if ( (p = malloc( bytesInRow )) != NULL )
  222.     {
  223.         /*
  224.         ** Test for top-to-bottom or bottom-to-top, but ignore
  225.         ** left-to-right info.
  226.         */
  227.         orient = (sp->imageDesc >> 5) & 0x1;
  228.         if ( orient == 0 )
  229.         {
  230.             SetRect( &r, 0, sp->stampHeight - 1,
  231.                 sp->stampWidth, sp->stampHeight );
  232.             /*
  233.             ** Postage stamp data always stored in uncompressed format
  234.             */
  235.             for ( i = 0; i < sp->stampHeight; ++i )
  236.             {
  237.                 fread( p, 1, bytesInRow, fp );
  238.                 PutHostImageToPort( r, p, (unsigned int)sp->pixelDepth );
  239.                 r.y1--;
  240.                 r.y2--;
  241.             }
  242.         }
  243.         else
  244.         {
  245.             SetRect( &r, 0, 0, sp->stampWidth, 1 );
  246.             for ( i = 0; i < sp->stampHeight; ++i )
  247.             {
  248.                 fread( p, 1, bytesInRow, fp );
  249.                 PutHostImageToPort( r, p, (unsigned int)sp->pixelDepth );
  250.                 r.y1++;
  251.                 r.y2++;
  252.             }
  253.         }
  254.         free( p );
  255.     }
  256.     else puts( "Unable to allocate scan line buffer" );
  257. }
  258.  
  259.  
  260. UINT8
  261. ReadByte( fp )
  262. FILE *fp;
  263. {
  264.     UINT8    value;
  265.  
  266. #if MSDOS
  267.     fread( &value, 1, 1, fp );
  268. #else
  269. #endif
  270.     return( value );
  271. }
  272.  
  273.  
  274. void
  275. ReadCharField( fp, p, n )
  276. FILE    *fp;
  277. char    *p;
  278. int        n;
  279. {
  280.     while ( n )
  281.     {
  282.         *p++ = (char)fgetc( fp );    /* no error check, no char conversion */
  283.         --n;
  284.     }
  285. }
  286.  
  287.  
  288. int
  289. ReadExtendedTGA( fp, sp )
  290. FILE    *fp;
  291. TGAFile    *sp;
  292. {
  293.     if ( !fseek( fp, sp->extAreaOffset, SEEK_SET ) )
  294.     {
  295.         sp->extSize = ReadShort( fp );
  296.         memset( sp->author, 0, 41 );
  297.         ReadCharField( fp, sp->author, 41 );
  298.         memset( &sp->authorCom[0][0], 0, 81 );
  299.         ReadCharField( fp, &sp->authorCom[0][0], 81 );
  300.         memset( &sp->authorCom[1][0], 0, 81 );
  301.         ReadCharField( fp, &sp->authorCom[1][0], 81 );
  302.         memset( &sp->authorCom[2][0], 0, 81 );
  303.         ReadCharField( fp, &sp->authorCom[2][0], 81 );
  304.         memset( &sp->authorCom[3][0], 0, 81 );
  305.         ReadCharField( fp, &sp->authorCom[3][0], 81 );
  306.  
  307.         sp->month = ReadShort( fp );
  308.         sp->day = ReadShort( fp );
  309.         sp->year = ReadShort( fp );
  310.         sp->hour = ReadShort( fp );
  311.         sp->minute = ReadShort( fp );
  312.         sp->second = ReadShort( fp );
  313.  
  314.         memset( sp->jobID, 0, 41 );
  315.         ReadCharField( fp, sp->jobID, 41 );
  316.         sp->jobHours = ReadShort( fp );
  317.         sp->jobMinutes = ReadShort( fp );
  318.         sp->jobSeconds = ReadShort( fp );
  319.  
  320.         memset( sp->softID, 0, 41 );
  321.         ReadCharField( fp, sp->softID, 41 );
  322.         sp->versionNum = ReadShort( fp );
  323.         sp->versionLet = ReadByte( fp );
  324.  
  325.         sp->keyColor = ReadLong( fp );
  326.         sp->pixNumerator = ReadShort( fp );
  327.         sp->pixDenominator = ReadShort( fp );
  328.  
  329.         sp->gammaNumerator = ReadShort( fp );
  330.         sp->gammaDenominator = ReadShort( fp );
  331.  
  332.         sp->colorCorrectOffset = ReadLong( fp );
  333.         sp->stampOffset = ReadLong( fp );
  334.         sp->scanLineOffset = ReadLong( fp );
  335.  
  336.         sp->alphaAttribute = ReadByte( fp );
  337.  
  338.         sp->colorCorrectTable = (UINT16 *)0;
  339.  
  340.         sp->postStamp = (void *)0;
  341.         if ( sp->stampOffset )
  342.         {
  343.             if ( !fseek( fp, sp->stampOffset, SEEK_SET ) )
  344.             {
  345.                 sp->stampWidth = ReadByte( fp );
  346.                 sp->stampHeight = ReadByte( fp );
  347.             }
  348.             else
  349.             {
  350.                 printf( "Error seeking to Postage Stamp, offset = 0x%08lx\n",
  351.                     sp->stampOffset );
  352.             }
  353.         }
  354.  
  355.         sp->scanLineTable = (UINT32 *)0;
  356.     }
  357.     else
  358.     {
  359.         printf( "Error seeking to Extended TGA Area, offset = 0x%08lx\n",
  360.             sp->extAreaOffset );
  361.         return( -1 );
  362.     }
  363.     return( 0 );
  364. }
  365.  
  366.  
  367. UINT32
  368. ReadLong( fp )
  369. FILE *fp;
  370. {
  371.     UINT32    value;
  372.  
  373. #if MSDOS
  374.     fread( &value, 1, 4, fp );
  375. #else
  376. #endif
  377.     return( value );
  378. }
  379.  
  380.  
  381.  
  382. UINT16
  383. ReadShort( fp )
  384. FILE *fp;
  385. {
  386.     UINT16    value;
  387.  
  388. #if MSDOS
  389.     fread( &value, 1, 2, fp );
  390. #else
  391. #endif
  392.     return( value );
  393. }
  394.