home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
-
- pcxCLI.c
-
- (c) Daniel Pead 1991
-
- $Header$
-
- Command line interface to MakePCX - Link with o.makepcx
-
- Converts RISC-OS Sprites into Zsoft PCX V5 files.
-
- Requires a GETOPT function such as GNU getopt.
-
- Syntax:
-
- makepcx [-s <name | n | *> ] [-p <palette> | -d <palette> ]
- [-o <path|name>]
- <spritefile>
-
- -s <name> : Process only sprite called "name" from file
- -s <n> : Process only <n>th sprite from file (default 1)
- -s * : Process all sprites in file
- -d <palette> : Set default palette, where palette is:
- 1 : BBC TTL rgb
- 2 : RISC_OS Desktop (default)
- <file> : Read from palette file
- Only affects sprites without a palette.
- -p <palette> : Set palette for all sprites.
- -o <path> : Set output file for single sprite conversions
- or output path for multiple sprites.
- <spritefile> : Source sprite file
-
- This software may be freely distributed
-
- $Log$
-
- *******************************************************************************/
-
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <stddef.h>
- #include <string.h>
- #include <ctype.h>
-
- #include <errno.h>
- #include <sprite.h>
- #include <kernel.h>
- #include <swis.h>
- #include <getopt.h>
-
- #include "h.makepcx"
-
- /*******************************************************************************
-
- Syntax message
-
- *******************************************************************************/
-
- void syntax(void)
- {
- puts (
- "\nConverts RISC-OS Sprites into Zsoft PCX V5 files.\n\n"
- "Syntax:\n\n"
- " makepcx [-s <name | n | *> ] [-p <palette> | -d <palette> ]\n"
- " [-o <path|name>]\n"
- " <spritefile>\n\n"
- " -s <name> : Process only sprite called <name> from file\n"
- " -s <n> : Process only <n>th sprite from file (default 1)\n"
- " -s * : Process all sprites in file\n"
- " -d <palette> : Set default palette, where palette is:\n"
- " 1 : BBC TTL rgb\n"
- " 2 : RISC_OS Desktop (default)\n"
- " <file> : Read from palette file\n"
- " Only affects sprites without a palette.\n"
- " -p <palette> : Set palette for all sprites.\n"
- " -o <path> : Set output file for single sprite conversions\n"
- " or output path for multiple sprites. \n"
- " <spritefile> : Source sprite file\n"
- );
- }
-
- /*******************************************************************************
-
- Main function - CLI interface
-
- *******************************************************************************/
-
- int main(int argc, char * argv[])
- {
- char pcxpath[128]; /* Pathname for writing PCX file */
- char * out_name_start; /* Point to insert image name in above */
- char * sp_file_name; /* Name of sprite file */
- int pcxpathlen; /* Length of output path name */
- FILE * pcxfile; /* Output file handle */
-
- int ftype; /* File type as returned by ftype() */
-
- char sprite_name[13]; /* Sprite name to convert */
- int sprite_number; /* >0 : sprite number, =0 : use name <0 : all */
-
- int sp_area_len; /* Length of sprite area required */
- sprite_area * sp_area_p;/* Pointer to sprite area */
- sprite_ptr sp_p; /* Pointer to sprite */
-
-
- os_error * oserr_p; /* Error returned by OS call */
-
- char opt; /* CLI option letter */
- int count; /* Number of sprites to translate */
-
- /* Defaults */
-
- pcxpathlen = 0;
- sprite_number = 1; /* Default - convert first sprite in file */
-
- /*************************************************************************/
- /* Get option arguments */
- /*************************************************************************/
-
- optind = 0;
-
- while ( (opt = getopt ( argc, argv, "s:S:d:D:o:O:" ) ) != (char) EOF )
- {
- switch ( tolower(opt) )
- {
- case 's' : /* -s : Selects which sprite(s) from file to convert */
-
- if ( strisnum(optarg) )
- {
- /* -s <n> : Convert <n>th sprite in file */
-
- if ( (sprite_number = atoi ( optarg ))<=0 )
- do_error("Sprite number must be >0\n",0);
-
- }
- else
-
- if ( *optarg == '*' )
- {
- /* -s * : Convert all sprites */
-
- sprite_number = -1;
- }
- else
- {
- /* -s <name> : Convert named sprite */
-
- strncpy ( sprite_name, optarg, 12 );
- sprite_number = 0;
- }
- break;
-
-
- /* -p <palette> and -d <palette> */
-
- case 'p' : force_pal = TRUE;
- /* vvv fall through! vvv */
-
- case 'd' : switch ( *optarg )
- {
- case '1' : /* -d 1 : BBC palette (TTL RGB) */
- default_pal_type = 0;
- break;
-
- case '2' : /* -d 2 : RiscOS Desktop palette */
- default_pal_type = 1;
- break;
-
- default : /* -d <file> : Not yet supported */
- do_error ( "Opt -%c <file> coming RSN\n",opt);
- /*
- default_pal_type = 3;
- if (!load_default_palette(optarg))
- do_error (
- "Can't load palette \"%s\"/n",
- optarg
- ); */
- break;
- }
- break;
-
- case 'o' : /* -o <name> : Set output filename/pathname */
- strncpy ( pcxpath, optarg, 128 );
- pcxpathlen = strlen(pcxpath);
- break;
-
- default: /* Error - give syntax message */
- syntax();
- exit(0);
- }
- }
-
- /*************************************************************************/
- /* Decide on path name for output PCX file(s) */
- /*************************************************************************/
-
- if (!pcxpathlen)
- {
- /* Default path name */
- strcpy ( pcxpath, "pcx." );
- pcxpathlen = 4;
- }
-
- /* Pointer to where sprite name is inserted */
-
- out_name_start = &pcxpath[pcxpathlen];
-
- /* Is it a path or full file name ? */
-
- if ( pcxpath[pcxpathlen-1] == '.' )
- {
- /* Remove '.' for now */
- pcxpath[--pcxpathlen] = 0;
- --out_name_start;
- }
- else
- {
- if ( sprite_number >= 0 )
- {
- /* It's a file name - override sprite name */
- out_name_start = NULL;
- }
- /* Otherwise - it's a path name */
- }
-
- if ( (127 - pcxpathlen)<14 ) do_error ( "-o path name too long!\n",0);
-
- if ( out_name_start )
- {
- /* Check for and, if not there, create output directory */
-
- ftype = finfo(pcxpath, NULL);
- if ( ftype != FINFO_DIR )
- if (ftype != FINFO_ERR || mkdir(pcxpath) )
- do_error ( "Can't create the directory %s\n", (int) pcxpath );
-
- /* Now append the '.' */
-
- strncat ( pcxpath, ".", 127 );
- pcxpathlen++;
- out_name_start++;
- }
-
-
- /*************************************************************************/
- /* Load in input sprite file */
- /*************************************************************************/
-
- /* Check that user has supplied an input file name! */
-
- if ( optind >= argc )
- {
- syntax();
- exit(0);
- }
-
- sp_file_name = argv[optind];
-
- #ifdef DEBUG
- printf ( "Loading %s\n", sp_file_name );
- #endif
-
- /* Get file type & length */
-
- ftype = finfo ( sp_file_name, &sp_area_len );
-
- if (ftype!=0xFF9)
- do_error("\"%s\" not the name of a sprite file.\n",(int) sp_file_name);
-
- /* Allocate memory for sprite area & initialise */
-
- sp_area_len += 4; /* First word of sprite area not included in file */
-
- sp_area_p = (sprite_area *) malloc ( sp_area_len );
-
- if (!sp_area_p)
- do_error("No room for sprite in memory (need %d bytes)\n",sp_area_len);
-
- sprite_area_initialise ( sp_area_p, sp_area_len );
-
- #ifdef DEBUG
- printf ( "Loading sprites %d\n", sp_file_name );
- #endif
-
- /* Load in sprite file */
-
- if ( oserr_p = sprite_area_load ( sp_area_p, sp_file_name ) )
- do_os_error(oserr_p);
-
-
- /*************************************************************************/
- /* Locate sprite(s) to convert */
- /*************************************************************************/
-
- count = 1;
-
- if (sprite_number < 0 )
- {
- /* All sprites in file */
-
- sp_p = sprite_find ( sp_area_p, 1, NULL, NULL );
- if ( !sp_p ) do_error ("Empty sprite file!\n",0);
- count = sp_area_p->number;
- }
- else
- {
- /* Find sprite by name or number */
- sp_p = sprite_find ( sp_area_p, sprite_number, sprite_name, NULL );
-
- if ( !sp_p )
- {
- if (sprite_number > 0 )
- do_error ( "Required sprite (#%d) not in file\n", sprite_number);
- else
- do_error ( "Required sprite (%s) not in file\n",(int)sprite_name);
- }
- }
-
- /*************************************************************************/
- /* Now convert sprites to PCX format */
- /*************************************************************************/
-
- while ( count-- )
- {
- /* Set o/p file name */
-
- if (out_name_start)
- sprintf ( out_name_start, "%-12s", sp_h(sp_p).name );
-
- #ifdef DEBUG
- printf ( "Converting %-12s to %s\n",sp_h(sp_p).name, pcxpath );
- #endif
-
- /* Open o/p file */
-
- if ( ! (pcxfile = fopen ( pcxpath , "wb" ) ) )
- do_error("Can't open output file %s\n",(int) pcxpath);
-
- /* Do the conversion */
-
- make_pcx ( sp_area_p, sp_p, pcxfile );
-
- /* Close file & set type */
-
- fclose ( pcxfile );
- settype ( pcxpath, PCX_TYPE );
-
- /* Get pointer to next sprite */
-
- sp_p = (sprite_ptr) ( ((int)sp_p) + sp_h(sp_p).next );
- }
- return 0;
- }
-
-
- /*******************************************************************************
-
- Error actions for RISC-OS, CLIB and program errors respectively
-
- *******************************************************************************/
-
- void do_os_error(os_error * oserr)
- {
- fprintf ( stderr, "OS Error %x : %s\n", oserr->errnum, oserr->errmess );
- exit ( oserr->errnum );
- }
-
- void do_c_error( int c_err )
- {
- fprintf ( stderr, "CLIB Error %x : %s\n", c_err, strerror(c_err) );
- exit ( c_err );
- }
-
- void do_error ( char * string, int param )
- {
- fprintf ( stderr, string, param );
- exit ( 0 );
- }
-