1 /*****************************************************************************/
5 /* Main program of the sp65 sprite and bitmap utility */
9 /* (C) 2012, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
56 /*****************************************************************************/
58 /*****************************************************************************/
62 /* Bitmap first read */
65 /* Bitmap working copy */
68 /* Output data from convertion */
73 /*****************************************************************************/
75 /*****************************************************************************/
79 static void Usage (void)
80 /* Print usage information and exit */
83 "Usage: %s [options] file [options] [file]\n"
85 " -V\t\t\t\tPrint the version number and exit\n"
86 " -c fmt[,attrlist]\t\tConvert into target format\n"
87 " -h\t\t\t\tHelp (this text)\n"
88 " -lc\t\t\t\tList all possible conversions\n"
89 " -r file[,attrlist]\t\tRead an input file\n"
90 " -v\t\t\t\tIncrease verbosity\n"
91 " -w file[,attrlist]\t\tWrite the output to a file\n"
94 " --convert-to fmt[,attrlist]\tConvert into target format\n"
95 " --help\t\t\tHelp (this text)\n"
96 " --list-conversions\t\tList all possible conversions\n"
97 " --pop\t\t\t\tRestore the original loaded image\n"
98 " --read file[,attrlist]\tRead an input file\n"
99 " --slice x,y,w,h\t\tGenerate a slice from the loaded bitmap\n"
100 " --verbose\t\t\tIncrease verbosity\n"
101 " --version\t\t\tPrint the version number and exit\n"
102 " --write file[,attrlist]\tWrite the output to a file\n",
108 static void SetWorkBitmap (Bitmap* N)
109 /* Delete an old working bitmap and set a new one. The new one may be NULL
113 /* If we have a distinct work bitmap, delete it */
114 if (C != 0 && C != B) {
118 /* Set the new one */
124 static void SetOutputData (StrBuf* N)
125 /* Delete the old output data and replace it by the given one. The new one
126 * may be NULL to clear it.
129 /* Delete the old output data */
134 /* Set the new one */
140 static void OptConvertTo (const char* Opt attribute ((unused)), const char* Arg)
141 /* Convert the bitmap into a target format */
143 static const char* NameList[] = {
147 /* Parse the argument */
148 Collection* A = ParseAttrList (Arg, NameList, 2);
150 /* We must have a bitmap */
152 Error ("No bitmap to convert");
155 /* Convert the bitmap */
156 SetOutputData (ConvertTo (C, A));
158 /* Delete the attribute list */
164 static void OptHelp (const char* Opt attribute ((unused)),
165 const char* Arg attribute ((unused)))
166 /* Print usage information and exit */
174 static void OptListConversions (const char* Opt attribute ((unused)),
175 const char* Arg attribute ((unused)))
176 /* Print a list of all conversions */
178 ListConversionTargets (stdout);
184 static void OptPop (const char* Opt attribute ((unused)),
185 const char* Arg attribute ((unused)))
186 /* Restore the original image */
188 /* C and B must differ and we must have an original */
189 if (B == 0 || C == 0 || C == B) {
190 Error ("Nothing to pop");
193 /* Delete the changed image and restore the original one */
199 static void OptRead (const char* Opt attribute ((unused)), const char* Arg)
200 /* Read an input file */
202 static const char* NameList[] = {
207 /* Parse the argument */
208 Collection* A = ParseAttrList (Arg, NameList, 2);
210 /* Clear the working copy */
213 /* Delete the original */
216 /* Read the file and use it as original and as working copy */
217 B = C = ReadInputFile (A);
219 /* Delete the attribute list */
225 static void OptSlice (const char* Opt attribute ((unused)), const char* Arg)
226 /* Generate a slice of a bitmap */
231 /* We must have a bitmap otherwise we cannot slice */
233 Error ("Nothing to slice");
236 /* The argument is X,Y,W,H */
237 if (sscanf (Arg, "%u,%u,%u,%u,%c", &X, &Y, &W, &H, &T) != 4) {
238 Error ("Invalid argument. Slice must be given as X,Y,W,H");
241 /* Check the coordinates to be within the original bitmap */
242 if (W > BM_MAX_WIDTH || H > BM_MAX_HEIGHT ||
243 X + W > GetBitmapWidth (C) ||
244 Y + H > GetBitmapHeight (C)) {
245 Error ("Invalid slice coordinates and/or size");
248 /* Create the slice */
249 SetWorkBitmap (SliceBitmap (C, X, Y, W, H));
254 static void OptVerbose (const char* Opt attribute ((unused)),
255 const char* Arg attribute ((unused)))
256 /* Increase versbosity */
263 static void OptVersion (const char* Opt attribute ((unused)),
264 const char* Arg attribute ((unused)))
265 /* Print the assembler version */
268 "%s V%s - (C) Copyright 2012, Ullrich von Bassewitz\n",
269 ProgName, GetVersionAsString ());
274 static void OptWrite (const char* Opt attribute ((unused)), const char* Arg)
275 /* Write an output file */
277 static const char* NameList[] = {
282 /* Parse the argument */
283 Collection* A = ParseAttrList (Arg, NameList, 2);
286 WriteOutputFile (D, A);
288 /* Delete the attribute list */
294 int main (int argc, char* argv [])
295 /* sp65 main program */
297 /* Program long options */
298 static const LongOpt OptTab[] = {
299 { "--convert-to", 1, OptConvertTo },
300 { "--help", 0, OptHelp },
301 { "--list-conversions", 0, OptListConversions },
302 { "--pop", 0, OptPop },
303 { "--read", 1, OptRead },
304 { "--slice", 1, OptSlice },
305 { "--verbose", 0, OptVerbose },
306 { "--version", 0, OptVersion },
307 { "--write", 1, OptWrite },
312 /* Initialize the cmdline module */
313 InitCmdLine (&argc, &argv, "sp65");
315 /* Check the parameters */
317 while (I < ArgCount) {
319 /* Get the argument */
320 const char* Arg = ArgVec[I];
322 /* Check for an option */
327 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
335 OptConvertTo (Arg, GetArg (&I, 2));
344 OptListConversions (Arg, 0);
351 OptRead (Arg, GetArg (&I, 2));
359 OptWrite (Arg, GetArg (&I, 2));
368 /* We don't accept anything else */
369 AbEnd ("Don't know what to do with `%s'", Arg);