home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / a / aadraw / Source / c / main next >
Encoding:
Text File  |  1996-10-16  |  6.4 KB  |  246 lines

  1. /* Main.c */
  2.  
  3. /* Main code for AADraw
  4.  * (K) All Rites Reversed - Copy What You Like (see file Copying)
  5.  *
  6.  * Authors:
  7.  *      Peter Hartley       <peter@ant.co.uk>
  8.  *
  9.  * History:
  10.  *      12-Aug-96 pdh Started
  11.  *      12-Aug-96 *** Release 1.00
  12.  *      14-Oct-96 pdh Fix so it works with Wimp_StartTask
  13.  *      14-Oct-96 *** Release 1.01
  14.  *      16-Oct-96 *** Release 1.02
  15.  *
  16.  * See also:
  17.  *      http:/www.ant.co.uk/~peter/software/aadraw.htm
  18.  *
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <time.h>
  25.  
  26. #include "DeskLib:File.h"
  27. #include "DeskLib:Sprite.h"
  28. #include "DeskLib:Str.h"
  29. #include "DeskLib:SWI.h"
  30. #include "Extras:File.h"
  31. #include "Extras:Extras.h"
  32.  
  33. #include "aadraw.h"
  34.  
  35.     /*=====================================*
  36.      *   Utility functions                 *
  37.      *   (should really be in ExtrasLib)   *
  38.      *=====================================*/
  39.  
  40.  
  41. void cmd_noerr( os_error *e )
  42. {
  43.     if ( !e )
  44.         return;
  45.     fputs( e->errmess, stderr );
  46.     exit(1);
  47. }
  48.  
  49. /*---------------------------------------------------------------------------*
  50.  * Calls to SWI XOS_Module (not in DeskLib)                                  *
  51.  *---------------------------------------------------------------------------*/
  52.  
  53. BOOL OSModule_Present( char *name )
  54. {
  55.     /* Call SWI XOS_Module, reason code 18=lookup */
  56.     return (SWI(2,0,0x2001e,18,name) == 0);
  57. }
  58.  
  59. os_error *OSModule_Load( char *pathname )
  60. {
  61.     /* Call SWI XOS_Module, reason code 1=RMLoad */
  62.     return SWI(2,0,0x2001e,1,pathname);
  63. }
  64.  
  65. /*---------------------------------------------------------------------------*
  66.  * Call SWI XTaskWindow_TaskInfo                                             *
  67.  * You may need to comment this out if your DeskLib is newer than mine       *
  68.  * and has this function in it                                               *
  69.  *---------------------------------------------------------------------------*/
  70.  
  71. int TaskWindow_TaskInfo( int index )
  72. {
  73.     int result;
  74.     SWI(1,1,0x63380,index,&result);
  75.     return result;
  76. }
  77.  
  78.  
  79.     /*=========================*
  80.      *   Arguments to aadraw   *
  81.      *=========================*/
  82.  
  83.  
  84. void Usage( char *me )
  85. {
  86.     fputs( "aadraw v1.02 by Peter Hartley (K) All Rites Reversed\n", stderr );
  87.     fprintf( stderr,
  88. "Usage: %s [-o outfile] [-b BBGGRR] [-g [-t] [-i]] infile\n", me );
  89.     fputs( "        -o Name output file (default is <infile>2 or <infile>/gif)\n", stderr );
  90.     fputs( "        -b Set background colour (default is FFFFFF, white)\n", stderr );
  91.     fputs( "        -g Make a GIF instead of a sprite (InterGif must be on your path)\n", stderr );
  92.     fputs( "            -t Make a transparent GIF\n", stderr );
  93.     fputs( "            -i Make an interlaced GIF\n", stderr );
  94.     fputs( "        infile  A RISCOS draw file\n", stderr );
  95.     fputs( "See http://www.ant.co.uk/~peter/software/aadraw.htm for details\n", stderr );
  96.     exit( 1 );
  97. }
  98.  
  99. static char *infile = NULL;
  100. static char *outfile = NULL;
  101. BOOL bInterlace = FALSE, bTransparent = FALSE, bGIF = FALSE;
  102. int gBackground = 0xFFFFFF;
  103.  
  104. void DecodeArgs( int argc, char *argv[] )
  105. {
  106.     int i;
  107.     for ( i = 1; i < argc; i++ )
  108.     {
  109.         if ( !stricmp( argv[i], "-o" ) )
  110.         {
  111.             if ( i == argc-1 )
  112.                 Usage( argv[0] );
  113.             outfile = argv[i+1];
  114.             i++;
  115.         }
  116.         else if ( !stricmp( argv[i], "-b" ) )
  117.         {
  118.             if ( i == argc-1 )
  119.                 Usage( argv[0] );
  120.             sscanf( argv[i+1], "%x", &gBackground );
  121.             i++;
  122.         }
  123.         else if ( !stricmp( argv[i], "-g" ) )
  124.         {
  125.             bGIF = TRUE;
  126.         }
  127.         else if ( !stricmp( argv[i], "-t" ) )
  128.         {
  129.             bTransparent = TRUE;
  130.         }
  131.         else if ( !stricmp( argv[i], "-i" ) )
  132.         {
  133.             bInterlace = TRUE;
  134.         }
  135.         else
  136.         {
  137.             if ( infile )
  138.                 Usage( argv[0] );
  139.             infile = argv[i];
  140.         }
  141.     }
  142.     if ( !infile )
  143.         Usage( argv[0] );
  144. }
  145.  
  146. /*---------------------------------------------------------------------------*
  147.  * Check the DrawFile module is loaded, and try and load it if it isn't      *
  148.  *---------------------------------------------------------------------------*/
  149.  
  150. void CheckDrawFile( void )
  151. {
  152.     if ( OSModule_Present( "DrawFile" ) == 0 )
  153.     {
  154.         if ( OSModule_Load( "System:Modules.Drawfile" ) )
  155.         {
  156.             fprintf( stderr, "DrawFile module not loaded (and not in System:Modules where it should be)\n" );
  157.             exit(1);
  158.         }
  159.     }
  160. }
  161.  
  162. /*---------------------------------------------------------------------------*
  163.  * Check we're not in a task window (RiscOS throws a wobbly if we redirect   *
  164.  * into a sprite if we are)                                                  *
  165.  *---------------------------------------------------------------------------*/
  166.  
  167. void CheckCommands( void )
  168. {
  169.     if ( TaskWindow_TaskInfo(0) != 0 )
  170.     {
  171.         fprintf( stderr, "This program redirects output into a sprite during operation.\n" );
  172.         fprintf( stderr, "It cannot therefore be run inside a task window. Press F12 and try again.\n" );
  173.         exit(1);
  174.     }
  175. }
  176.  
  177. int main( int argc, char *argv[] )
  178. {
  179.     int drawsize;
  180.     unsigned int spritesize;
  181.     void *pDraw;
  182.     sprite_area area;
  183. #if 0
  184.     clock_t t = clock();
  185. #endif
  186.  
  187.     DecodeArgs( argc, argv );
  188.  
  189.     CheckDrawFile();
  190.  
  191.     CheckCommands();
  192.  
  193.     drawsize = File_Size( infile );
  194.  
  195.     if ( drawsize <= 0 )
  196.     {
  197.         fprintf( stderr, "File %s not found\n", infile );
  198.         return 1;
  199.     }
  200.  
  201.     pDraw = malloc( drawsize );
  202.  
  203.     if ( !pDraw )
  204.     {
  205.         fprintf( stderr, "Out of memory\n" );
  206.         return 1;
  207.     }
  208.  
  209.     cmd_noerr( File_LoadFile( infile, pDraw ) );
  210.  
  211.     area = DrawToSprite( pDraw, drawsize, &spritesize, gBackground*256 );
  212.  
  213.     if ( bGIF )
  214.     {
  215.         char cmd[256];
  216.         cmd_noerr( Sprite_Save( area, "<Wimp$Scrap>" ) );
  217.         sprintf( cmd, "chain:intergif <Wimp$Scrap> %s %s -o ",
  218.                  bInterlace ? "-i" :"",
  219.                  bTransparent ? "-t 255" : "" );
  220.         if ( outfile )
  221.             strcat( cmd, outfile );
  222.         else
  223.         {
  224.             strcat( cmd, infile );
  225.             strcat( cmd, "/gif" );
  226.         }
  227.         system( cmd );      /* doesn't return */
  228.     }
  229.     else
  230.     {
  231.         if ( !outfile )
  232.         {
  233.             outfile = malloc( strlen( infile )+6 );
  234.             sprintf( outfile, "%s2", infile );
  235.         }
  236.         cmd_noerr( Sprite_Save( area, outfile ) );
  237.     }
  238.  
  239. #if 0
  240.     t = clock() - t;
  241.     printf( "Time taken: %d.%02ds\n", t/100, t%100 );
  242. #endif
  243.  
  244.     return 0;
  245. }
  246.