home *** CD-ROM | disk | FTP | other *** search
- /* Main.c */
-
- /* Main code for AADraw
- * (K) All Rites Reversed - Copy What You Like (see file Copying)
- *
- * Authors:
- * Peter Hartley <peter@ant.co.uk>
- *
- * History:
- * 12-Aug-96 pdh Started
- * 12-Aug-96 *** Release 1.00
- * 14-Oct-96 pdh Fix so it works with Wimp_StartTask
- * 14-Oct-96 *** Release 1.01
- * 16-Oct-96 *** Release 1.02
- *
- * See also:
- * http:/www.ant.co.uk/~peter/software/aadraw.htm
- *
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
-
- #include "DeskLib:File.h"
- #include "DeskLib:Sprite.h"
- #include "DeskLib:Str.h"
- #include "DeskLib:SWI.h"
- #include "Extras:File.h"
- #include "Extras:Extras.h"
-
- #include "aadraw.h"
-
- /*=====================================*
- * Utility functions *
- * (should really be in ExtrasLib) *
- *=====================================*/
-
-
- void cmd_noerr( os_error *e )
- {
- if ( !e )
- return;
- fputs( e->errmess, stderr );
- exit(1);
- }
-
- /*---------------------------------------------------------------------------*
- * Calls to SWI XOS_Module (not in DeskLib) *
- *---------------------------------------------------------------------------*/
-
- BOOL OSModule_Present( char *name )
- {
- /* Call SWI XOS_Module, reason code 18=lookup */
- return (SWI(2,0,0x2001e,18,name) == 0);
- }
-
- os_error *OSModule_Load( char *pathname )
- {
- /* Call SWI XOS_Module, reason code 1=RMLoad */
- return SWI(2,0,0x2001e,1,pathname);
- }
-
- /*---------------------------------------------------------------------------*
- * Call SWI XTaskWindow_TaskInfo *
- * You may need to comment this out if your DeskLib is newer than mine *
- * and has this function in it *
- *---------------------------------------------------------------------------*/
-
- int TaskWindow_TaskInfo( int index )
- {
- int result;
- SWI(1,1,0x63380,index,&result);
- return result;
- }
-
-
- /*=========================*
- * Arguments to aadraw *
- *=========================*/
-
-
- void Usage( char *me )
- {
- fputs( "aadraw v1.02 by Peter Hartley (K) All Rites Reversed\n", stderr );
- fprintf( stderr,
- "Usage: %s [-o outfile] [-b BBGGRR] [-g [-t] [-i]] infile\n", me );
- fputs( " -o Name output file (default is <infile>2 or <infile>/gif)\n", stderr );
- fputs( " -b Set background colour (default is FFFFFF, white)\n", stderr );
- fputs( " -g Make a GIF instead of a sprite (InterGif must be on your path)\n", stderr );
- fputs( " -t Make a transparent GIF\n", stderr );
- fputs( " -i Make an interlaced GIF\n", stderr );
- fputs( " infile A RISCOS draw file\n", stderr );
- fputs( "See http://www.ant.co.uk/~peter/software/aadraw.htm for details\n", stderr );
- exit( 1 );
- }
-
- static char *infile = NULL;
- static char *outfile = NULL;
- BOOL bInterlace = FALSE, bTransparent = FALSE, bGIF = FALSE;
- int gBackground = 0xFFFFFF;
-
- void DecodeArgs( int argc, char *argv[] )
- {
- int i;
- for ( i = 1; i < argc; i++ )
- {
- if ( !stricmp( argv[i], "-o" ) )
- {
- if ( i == argc-1 )
- Usage( argv[0] );
- outfile = argv[i+1];
- i++;
- }
- else if ( !stricmp( argv[i], "-b" ) )
- {
- if ( i == argc-1 )
- Usage( argv[0] );
- sscanf( argv[i+1], "%x", &gBackground );
- i++;
- }
- else if ( !stricmp( argv[i], "-g" ) )
- {
- bGIF = TRUE;
- }
- else if ( !stricmp( argv[i], "-t" ) )
- {
- bTransparent = TRUE;
- }
- else if ( !stricmp( argv[i], "-i" ) )
- {
- bInterlace = TRUE;
- }
- else
- {
- if ( infile )
- Usage( argv[0] );
- infile = argv[i];
- }
- }
- if ( !infile )
- Usage( argv[0] );
- }
-
- /*---------------------------------------------------------------------------*
- * Check the DrawFile module is loaded, and try and load it if it isn't *
- *---------------------------------------------------------------------------*/
-
- void CheckDrawFile( void )
- {
- if ( OSModule_Present( "DrawFile" ) == 0 )
- {
- if ( OSModule_Load( "System:Modules.Drawfile" ) )
- {
- fprintf( stderr, "DrawFile module not loaded (and not in System:Modules where it should be)\n" );
- exit(1);
- }
- }
- }
-
- /*---------------------------------------------------------------------------*
- * Check we're not in a task window (RiscOS throws a wobbly if we redirect *
- * into a sprite if we are) *
- *---------------------------------------------------------------------------*/
-
- void CheckCommands( void )
- {
- if ( TaskWindow_TaskInfo(0) != 0 )
- {
- fprintf( stderr, "This program redirects output into a sprite during operation.\n" );
- fprintf( stderr, "It cannot therefore be run inside a task window. Press F12 and try again.\n" );
- exit(1);
- }
- }
-
- int main( int argc, char *argv[] )
- {
- int drawsize;
- unsigned int spritesize;
- void *pDraw;
- sprite_area area;
- #if 0
- clock_t t = clock();
- #endif
-
- DecodeArgs( argc, argv );
-
- CheckDrawFile();
-
- CheckCommands();
-
- drawsize = File_Size( infile );
-
- if ( drawsize <= 0 )
- {
- fprintf( stderr, "File %s not found\n", infile );
- return 1;
- }
-
- pDraw = malloc( drawsize );
-
- if ( !pDraw )
- {
- fprintf( stderr, "Out of memory\n" );
- return 1;
- }
-
- cmd_noerr( File_LoadFile( infile, pDraw ) );
-
- area = DrawToSprite( pDraw, drawsize, &spritesize, gBackground*256 );
-
- if ( bGIF )
- {
- char cmd[256];
- cmd_noerr( Sprite_Save( area, "<Wimp$Scrap>" ) );
- sprintf( cmd, "chain:intergif <Wimp$Scrap> %s %s -o ",
- bInterlace ? "-i" :"",
- bTransparent ? "-t 255" : "" );
- if ( outfile )
- strcat( cmd, outfile );
- else
- {
- strcat( cmd, infile );
- strcat( cmd, "/gif" );
- }
- system( cmd ); /* doesn't return */
- }
- else
- {
- if ( !outfile )
- {
- outfile = malloc( strlen( infile )+6 );
- sprintf( outfile, "%s2", infile );
- }
- cmd_noerr( Sprite_Save( area, outfile ) );
- }
-
- #if 0
- t = clock() - t;
- printf( "Time taken: %d.%02ds\n", t/100, t%100 );
- #endif
-
- return 0;
- }
-