/*****************************************************************************/ /* */ /* main.c */ /* */ /* Main program of the sp65 sprite and bitmap utility */ /* */ /* */ /* */ /* (C) 2012, Ullrich von Bassewitz */ /* Roemerstrasse 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ /* warranty. In no event will the authors be held liable for any damages */ /* arising from the use of this software. */ /* */ /* Permission is granted to anyone to use this software for any purpose, */ /* including commercial applications, and to alter it and redistribute it */ /* freely, subject to the following restrictions: */ /* */ /* 1. The origin of this software must not be misrepresented; you must not */ /* claim that you wrote the original software. If you use this software */ /* in a product, an acknowledgment in the product documentation would be */ /* appreciated but is not required. */ /* 2. Altered source versions must be plainly marked as such, and must not */ /* be misrepresented as being the original software. */ /* 3. This notice may not be removed or altered from any source */ /* distribution. */ /* */ /*****************************************************************************/ #include #include #include #include /* common */ #include "abend.h" #include "cmdline.h" #include "print.h" #include "version.h" /* sp65 */ #include "attr.h" #include "error.h" #include "input.h" #include "output.h" /*****************************************************************************/ /* Data */ /*****************************************************************************/ /* Bitmap first read */ static Bitmap* B; /* Bitmap last processed */ static Bitmap* C; /*****************************************************************************/ /* Code */ /*****************************************************************************/ static void Usage (void) /* Print usage information and exit */ { printf ( "Usage: %s [options] file [options] [file]\n" "Short options:\n" " -V\t\t\t\tPrint the version number and exit\n" " -h\t\t\t\tHelp (this text)\n" " -v\t\t\t\tIncrease verbosity\n" "\n" "Long options:\n" " --help\t\t\tHelp (this text)\n" " --pop\t\t\t\tRestore the original loaded image\n" " --read file[,attrlist]\tRead an input file\n" " --slice x,y,w,h\t\tGenerate a slice from the loaded bitmap\n" " --verbose\t\t\tIncrease verbosity\n" " --version\t\t\tPrint the version number and exit\n" " --write file[,attrlist]\tWrite an output file\n", ProgName); } static void SetWorkBitmap (Bitmap* N) /* Delete an old working bitmap and set a new one. The new one may be NULL * to clear it. */ { /* If we have a distinct work bitmap, delete it */ if (C != 0 && C != B) { FreeBitmap (C); } /* Set the new one */ C = N; } static void OptHelp (const char* Opt attribute ((unused)), const char* Arg attribute ((unused))) /* Print usage information and exit */ { Usage (); exit (EXIT_SUCCESS); } static void OptPop (const char* Opt attribute ((unused)), const char* Arg attribute ((unused))) /* Restore the original image */ { /* C and B must differ and we must have an original */ if (B == 0 || C == 0 || C == B) { Error ("Nothing to pop"); } /* Delete the changed image and restore the original one */ SetWorkBitmap (B); } static void OptRead (const char* Opt, const char* Arg) /* Read an input file */ { static const char* NameList[] = { "name", "format" }; /* Parse the argument */ Collection* A = ParseAttrList (Arg, NameList, 2); /* Must have a file name given */ const char* FileName = NeedAttrVal (A, "name", Opt); /* Determine the format of the input file */ int IF = ifAuto; const char* Format = GetAttrVal (A, "format"); if (Format != 0) { IF = FindInputFormat (Format); if (IF < 0) { Error ("Unknown input format `%s'", Format); } } /* Clear the working copy */ SetWorkBitmap (0); /* Delete the original */ FreeBitmap (B); /* Read the file and use it as original and as working copy */ B = C = ReadInputFile (FileName, IF); /* Delete the attribute list */ FreeCollection (A); } static void OptSlice (const char* Opt attribute ((unused)), const char* Arg) /* Generate a slice of a bitmap */ { unsigned X, Y, W, H; unsigned char T; /* We must have a bitmap otherwise we cannot slice */ if (C == 0) { Error ("Nothing to slice"); } /* The argument is X,Y,W,H */ if (sscanf (Arg, "%u,%u,%u,%u,%c", &X, &Y, &W, &H, &T) != 4) { Error ("Invalid argument. Slice must be given as X,Y,W,H"); } /* Check the coordinates to be within the original bitmap */ if (W > BM_MAX_WIDTH || H > BM_MAX_HEIGHT || X + W > GetBitmapWidth (C) || Y + H > GetBitmapHeight (C)) { Error ("Invalid slice coordinates and/or size"); } /* Create the slice */ SetWorkBitmap (SliceBitmap (C, X, Y, W, H)); } static void OptVerbose (const char* Opt attribute ((unused)), const char* Arg attribute ((unused))) /* Increase versbosity */ { ++Verbosity; } static void OptVersion (const char* Opt attribute ((unused)), const char* Arg attribute ((unused))) /* Print the assembler version */ { fprintf (stderr, "%s V%s - (C) Copyright 2012, Ullrich von Bassewitz\n", ProgName, GetVersionAsString ()); } static void OptWrite (const char* Opt, const char* Arg) /* Write an output file */ { static const char* NameList[] = { "name", "format" }; /* Parse the argument */ Collection* A = ParseAttrList (Arg, NameList, 2); /* Must have a file name given */ const char* FileName = NeedAttrVal (A, "name", Opt); /* Determine the format of the input file */ int OF = ofAuto; const char* Format = GetAttrVal (A, "format"); if (Format != 0) { OF = FindOutputFormat (Format); if (OF < 0) { Error ("Unknown output format `%s'", Format); } } /* Write the file */ WriteOutputFile (FileName, 0, OF); } int main (int argc, char* argv []) /* sp65 main program */ { /* Program long options */ static const LongOpt OptTab[] = { { "--help", 0, OptHelp }, { "--pop", 0, OptPop }, { "--read", 1, OptRead }, { "--slice", 1, OptSlice }, { "--verbose", 0, OptVerbose }, { "--version", 0, OptVersion }, { "--write", 1, OptWrite }, }; unsigned I; /* Initialize the cmdline module */ InitCmdLine (&argc, &argv, "sp65"); /* Check the parameters */ I = 1; while (I < ArgCount) { /* Get the argument */ const char* Arg = ArgVec[I]; /* Check for an option */ if (Arg [0] == '-') { switch (Arg [1]) { case '-': LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0])); break; case 'V': OptVersion (Arg, 0); break; case 'h': OptHelp (Arg, 0); break; case 'r': OptRead (Arg, GetArg (&I, 2)); break; case 'v': OptVerbose (Arg, 0); break; default: UnknownOption (Arg); break; } } else { /* We don't accept anything else */ AbEnd ("Don't know what to do with `%s'", Arg); } /* Next argument */ ++I; } /* Success */ return EXIT_SUCCESS; }