]> git.sur5r.net Git - cc65/blob - src/sp65/main.c
Added the capability to generate multicolor sprites.
[cc65] / src / sp65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*            Main program of the sp65 sprite and bitmap utility             */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2012,      Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40
41 /* common */
42 #include "abend.h"
43 #include "cmdline.h"
44 #include "print.h"
45 #include "version.h"
46
47 /* sp65 */
48 #include "attr.h"
49 #include "convert.h"
50 #include "error.h"
51 #include "input.h"
52 #include "output.h"
53
54
55
56 /*****************************************************************************/
57 /*                                   Data                                    */
58 /*****************************************************************************/
59
60
61
62 /* Bitmap first read */
63 static Bitmap* B;
64
65 /* Bitmap working copy */
66 static Bitmap* C;
67
68 /* Output data from convertion */
69 static StrBuf* D;
70
71
72
73 /*****************************************************************************/
74 /*                                   Code                                    */
75 /*****************************************************************************/
76
77
78
79 static void Usage (void)
80 /* Print usage information and exit */
81 {
82     printf (
83             "Usage: %s [options] file [options] [file]\n"
84             "Short options:\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"
92             "\n"
93             "Long options:\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",
103             ProgName);
104 }
105
106
107
108 static void SetWorkBitmap (Bitmap* N)
109 /* Delete an old working bitmap and set a new one. The new one may be NULL
110  * to clear it.
111  */
112 {
113     /* If we have a distinct work bitmap, delete it */
114     if (C != 0 && C != B) {
115         FreeBitmap (C);
116     }
117
118     /* Set the new one */
119     C = N;
120 }
121
122
123
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.
127  */
128 {
129     /* Delete the old output data */
130     if (D != 0) {
131         FreeStrBuf (D);
132     }
133
134     /* Set the new one */
135     D = N;
136 }
137
138
139
140 static void OptConvertTo (const char* Opt attribute ((unused)), const char* Arg)
141 /* Convert the bitmap into a target format */
142 {
143     static const char* NameList[] = {
144         "format"
145     };
146
147     /* Parse the argument */
148     Collection* A = ParseAttrList (Arg, NameList, 2);
149
150     /* We must have a bitmap */
151     if (C == 0) {
152         Error ("No bitmap to convert");
153     }
154
155     /* Convert the bitmap */
156     SetOutputData (ConvertTo (C, A));
157
158     /* Delete the attribute list */
159     FreeAttrList (A);
160 }
161
162
163
164 static void OptDumpPalette (const char* Opt attribute ((unused)),
165                             const char* Arg attribute ((unused)))
166 /* Dump the palette of the current work bitmap */
167 {
168     /* We must have a bitmap ... */
169     if (C == 0) {
170         Error ("No bitmap");
171     }
172
173     /* ... which must be indexed */
174     if (!BitmapIsIndexed (C)) {
175         Error ("Current bitmap is not indexed");
176     }
177
178     /* Dump the palette */
179     DumpPalette (stdout, GetBitmapPalette (C));
180 }
181
182
183
184 static void OptHelp (const char* Opt attribute ((unused)),
185                      const char* Arg attribute ((unused)))
186 /* Print usage information and exit */
187 {
188     Usage ();
189     exit (EXIT_SUCCESS);
190 }
191
192
193
194 static void OptListConversions (const char* Opt attribute ((unused)),
195                                 const char* Arg attribute ((unused)))
196 /* Print a list of all conversions */
197 {
198     ListConversionTargets (stdout);
199     exit (EXIT_SUCCESS);
200 }
201
202
203
204 static void OptPop (const char* Opt attribute ((unused)),
205                     const char* Arg attribute ((unused)))
206 /* Restore the original image */
207 {
208     /* C and B must differ and we must have an original */
209     if (B == 0 || C == 0 || C == B) {
210         Error ("Nothing to pop");
211     }
212
213     /* Delete the changed image and restore the original one */
214     SetWorkBitmap (B);
215 }
216
217
218
219 static void OptRead (const char* Opt attribute ((unused)), const char* Arg)
220 /* Read an input file */
221 {
222     static const char* NameList[] = {
223         "name", "format"
224     };
225
226
227     /* Parse the argument */
228     Collection* A = ParseAttrList (Arg, NameList, 2);
229
230     /* Clear the working copy */
231     SetWorkBitmap (0);
232
233     /* Delete the original */
234     FreeBitmap (B);
235
236     /* Read the file and use it as original and as working copy */
237     B = C = ReadInputFile (A);
238
239     /* Delete the attribute list */
240     FreeAttrList (A);
241 }
242
243
244
245 static void OptSlice (const char* Opt attribute ((unused)), const char* Arg)
246 /* Generate a slice of a bitmap */
247 {
248     unsigned X, Y, W, H;
249     unsigned char T;
250
251     /* We must have a bitmap otherwise we cannot slice */
252     if (C == 0) {
253         Error ("Nothing to slice");
254     }
255
256     /* The argument is X,Y,W,H */
257     if (sscanf (Arg, "%u,%u,%u,%u,%c", &X, &Y, &W, &H, &T) != 4) {
258         Error ("Invalid argument. Slice must be given as X,Y,W,H");
259     }
260
261     /* Check the coordinates to be within the original bitmap */
262     if (W > BM_MAX_WIDTH || H > BM_MAX_HEIGHT ||
263         X + W > GetBitmapWidth (C) ||
264         Y + H > GetBitmapHeight (C)) {
265         Error ("Invalid slice coordinates and/or size");
266     }
267
268     /* Create the slice */
269     SetWorkBitmap (SliceBitmap (C, X, Y, W, H));
270 }
271
272
273
274 static void OptVerbose (const char* Opt attribute ((unused)),
275                         const char* Arg attribute ((unused)))
276 /* Increase versbosity */
277 {
278     ++Verbosity;
279 }
280
281
282
283 static void OptVersion (const char* Opt attribute ((unused)),
284                         const char* Arg attribute ((unused)))
285 /* Print the assembler version */
286 {
287     fprintf (stderr,
288              "%s V%s - (C) Copyright 2012, Ullrich von Bassewitz\n",
289              ProgName, GetVersionAsString ());
290 }
291
292
293
294 static void OptWrite (const char* Opt attribute ((unused)), const char* Arg)
295 /* Write an output file */
296 {
297     static const char* NameList[] = {
298         "name", "format"
299     };
300
301
302     /* Parse the argument */
303     Collection* A = ParseAttrList (Arg, NameList, 2);
304
305     /* Write the file */
306     WriteOutputFile (D, A);
307
308     /* Delete the attribute list */
309     FreeAttrList (A);
310 }
311
312
313
314 int main (int argc, char* argv [])
315 /* sp65 main program */
316 {
317     /* Program long options */
318     static const LongOpt OptTab[] = {
319         { "--convert-to",       1,      OptConvertTo            },
320         { "--dump-palette",     0,      OptDumpPalette          },
321         { "--help",             0,      OptHelp                 },
322         { "--list-conversions", 0,      OptListConversions      },
323         { "--pop",              0,      OptPop                  },
324         { "--read",             1,      OptRead                 },
325         { "--slice",            1,      OptSlice                },
326         { "--verbose",          0,      OptVerbose              },
327         { "--version",          0,      OptVersion              },
328         { "--write",            1,      OptWrite                },
329     };
330
331     unsigned I;
332
333     /* Initialize the cmdline module */
334     InitCmdLine (&argc, &argv, "sp65");
335
336     /* Check the parameters */
337     I = 1;
338     while (I < ArgCount) {
339
340         /* Get the argument */
341         const char* Arg = ArgVec[I];
342
343         /* Check for an option */
344         if (Arg[0] == '-') {
345             switch (Arg[1]) {
346
347                 case '-':
348                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
349                     break;
350
351                 case 'V':
352                     OptVersion (Arg, 0);
353                     break;
354
355                 case 'c':
356                     OptConvertTo (Arg, GetArg (&I, 2));
357                     break;
358
359                 case 'h':
360                     OptHelp (Arg, 0);
361                     break;
362
363                 case 'l':
364                     if (Arg[2] == 'c') {
365                         OptListConversions (Arg, 0);
366                     } else {
367                         UnknownOption (Arg);
368                     }
369                     break;
370
371                 case 'r':
372                     OptRead (Arg, GetArg (&I, 2));
373                     break;
374
375                 case 'v':
376                     OptVerbose (Arg, 0);
377                     break;
378
379                 case 'w':
380                     OptWrite (Arg, GetArg (&I, 2));
381                     break;
382
383                 default:
384                     UnknownOption (Arg);
385                     break;
386
387             }
388         } else {
389             /* We don't accept anything else */
390             AbEnd ("Don't know what to do with `%s'", Arg);
391         }
392
393         /* Next argument */
394         ++I;
395     }
396
397     /* Cleanup data */
398     SetWorkBitmap (C);
399     FreeBitmap (B);
400     FreeStrBuf (D);
401
402     /* Success */
403     return EXIT_SUCCESS;
404 }
405
406
407