]> git.sur5r.net Git - cc65/blob - src/sp65/main.c
Removed (pretty inconsistently used) tab chars from source code base.
[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, "%s V%s\n", ProgName, GetVersionAsString ());
288 }
289
290
291
292 static void OptWrite (const char* Opt attribute ((unused)), const char* Arg)
293 /* Write an output file */
294 {
295     static const char* NameList[] = {
296         "name", "format"
297     };
298
299
300     /* Parse the argument */
301     Collection* A = ParseAttrList (Arg, NameList, 2);
302
303     /* We must have output data */
304     if (D == 0) {
305         Error ("No conversion, so there's nothing to write");
306     }
307
308     /* Write the file */
309     WriteOutputFile (D, A, C);
310
311     /* Delete the attribute list */
312     FreeAttrList (A);
313 }
314
315
316
317 int main (int argc, char* argv [])
318 /* sp65 main program */
319 {
320     /* Program long options */
321     static const LongOpt OptTab[] = {
322         { "--convert-to",       1,      OptConvertTo            },
323         { "--dump-palette",     0,      OptDumpPalette          },
324         { "--help",             0,      OptHelp                 },
325         { "--list-conversions", 0,      OptListConversions      },
326         { "--pop",              0,      OptPop                  },
327         { "--read",             1,      OptRead                 },
328         { "--slice",            1,      OptSlice                },
329         { "--verbose",          0,      OptVerbose              },
330         { "--version",          0,      OptVersion              },
331         { "--write",            1,      OptWrite                },
332     };
333
334     unsigned I;
335
336     /* Initialize the cmdline module */
337     InitCmdLine (&argc, &argv, "sp65");
338
339     /* Check the parameters */
340     I = 1;
341     while (I < ArgCount) {
342
343         /* Get the argument */
344         const char* Arg = ArgVec[I];
345
346         /* Check for an option */
347         if (Arg[0] == '-') {
348             switch (Arg[1]) {
349
350                 case '-':
351                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
352                     break;
353
354                 case 'V':
355                     OptVersion (Arg, 0);
356                     break;
357
358                 case 'c':
359                     OptConvertTo (Arg, GetArg (&I, 2));
360                     break;
361
362                 case 'h':
363                     OptHelp (Arg, 0);
364                     break;
365
366                 case 'l':
367                     if (Arg[2] == 'c') {
368                         OptListConversions (Arg, 0);
369                     } else {
370                         UnknownOption (Arg);
371                     }
372                     break;
373
374                 case 'r':
375                     OptRead (Arg, GetArg (&I, 2));
376                     break;
377
378                 case 'v':
379                     OptVerbose (Arg, 0);
380                     break;
381
382                 case 'w':
383                     OptWrite (Arg, GetArg (&I, 2));
384                     break;
385
386                 default:
387                     UnknownOption (Arg);
388                     break;
389
390             }
391         } else {
392             /* We don't accept anything else */
393             AbEnd ("Don't know what to do with `%s'", Arg);
394         }
395
396         /* Next argument */
397         ++I;
398     }
399
400     /* Cleanup data */
401     SetWorkBitmap (C);
402     FreeBitmap (B);
403     FreeStrBuf (D);
404
405     /* Success */
406     return EXIT_SUCCESS;
407 }
408
409
410