]> git.sur5r.net Git - cc65/blob - src/sp65/main.c
Added the main conversion module. New option --convert-to. New short options
[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             "  -r file[,attrlist]\t\tRead an input file\n"
89             "  -v\t\t\t\tIncrease verbosity\n"
90             "  -w file[,attrlist]\t\tWrite the output to a file\n"
91             "\n"
92             "Long options:\n"
93             "  --convert-to fmt[,attrlist]\tConvert into target format\n"
94             "  --help\t\t\tHelp (this text)\n"
95             "  --pop\t\t\t\tRestore the original loaded image\n"
96             "  --read file[,attrlist]\tRead an input file\n"
97             "  --slice x,y,w,h\t\tGenerate a slice from the loaded bitmap\n"
98             "  --verbose\t\t\tIncrease verbosity\n"
99             "  --version\t\t\tPrint the version number and exit\n"
100             "  --write file[,attrlist]\tWrite the output to a file\n",
101             ProgName);
102 }
103
104
105
106 static void SetWorkBitmap (Bitmap* N)
107 /* Delete an old working bitmap and set a new one. The new one may be NULL
108  * to clear it.
109  */
110 {
111     /* If we have a distinct work bitmap, delete it */
112     if (C != 0 && C != B) {
113         FreeBitmap (C);
114     }
115
116     /* Set the new one */
117     C = N;
118 }
119
120
121
122 static void SetOutputData (StrBuf* N)
123 /* Delete the old output data and replace it by the given one. The new one
124  * may be NULL to clear it.
125  */
126 {
127     /* Delete the old output data */
128     if (D != 0) {
129         FreeStrBuf (D);
130     }
131
132     /* Set the new one */
133     D = N;
134 }
135
136
137
138 static void OptConvertTo (const char* Opt attribute ((unused)), const char* Arg)
139 /* Convert the bitmap into a target format */
140 {
141     static const char* NameList[] = {
142         "format"
143     };
144
145     /* Parse the argument */
146     Collection* A = ParseAttrList (Arg, NameList, 2);
147
148     /* We must have a bitmap */
149     if (C == 0) {
150         Error ("No bitmap to convert");
151     }
152
153     /* Convert the bitmap */
154     SetOutputData (ConvertTo (C, A));
155
156     /* Delete the attribute list */
157     FreeCollection (A);
158 }
159
160
161
162 static void OptHelp (const char* Opt attribute ((unused)),
163                      const char* Arg attribute ((unused)))
164 /* Print usage information and exit */
165 {
166     Usage ();
167     exit (EXIT_SUCCESS);
168 }
169
170
171
172 static void OptPop (const char* Opt attribute ((unused)),
173                     const char* Arg attribute ((unused)))
174 /* Restore the original image */
175 {
176     /* C and B must differ and we must have an original */
177     if (B == 0 || C == 0 || C == B) {
178         Error ("Nothing to pop");
179     }
180
181     /* Delete the changed image and restore the original one */
182     SetWorkBitmap (B);
183 }
184
185
186
187 static void OptRead (const char* Opt, const char* Arg)
188 /* Read an input file */
189 {
190     static const char* NameList[] = {
191         "name", "format"
192     };
193
194
195     /* Parse the argument */
196     Collection* A = ParseAttrList (Arg, NameList, 2);
197
198     /* Must have a file name given */
199     const char* FileName = NeedAttrVal (A, "name", Opt);
200
201     /* Determine the format of the input file */
202     int IF = ifAuto;
203     const char* Format = GetAttrVal (A, "format");
204     if (Format != 0) {
205         IF = FindInputFormat (Format);
206         if (IF < 0) {
207             Error ("Unknown input format `%s'", Format);
208         }
209     }
210
211     /* Clear the working copy */
212     SetWorkBitmap (0);
213
214     /* Delete the original */
215     FreeBitmap (B);
216
217     /* Read the file and use it as original and as working copy */
218     B = C = ReadInputFile (FileName, IF);
219
220     /* Delete the attribute list */
221     FreeCollection (A);
222 }
223
224
225
226 static void OptSlice (const char* Opt attribute ((unused)), const char* Arg)
227 /* Generate a slice of a bitmap */
228 {
229     unsigned X, Y, W, H;
230     unsigned char T;
231
232     /* We must have a bitmap otherwise we cannot slice */
233     if (C == 0) {
234         Error ("Nothing to slice");
235     }
236
237     /* The argument is X,Y,W,H */
238     if (sscanf (Arg, "%u,%u,%u,%u,%c", &X, &Y, &W, &H, &T) != 4) {
239         Error ("Invalid argument. Slice must be given as X,Y,W,H");
240     }
241
242     /* Check the coordinates to be within the original bitmap */
243     if (W > BM_MAX_WIDTH || H > BM_MAX_HEIGHT ||
244         X + W > GetBitmapWidth (C) ||
245         Y + H > GetBitmapHeight (C)) {
246         Error ("Invalid slice coordinates and/or size");
247     }
248
249     /* Create the slice */
250     SetWorkBitmap (SliceBitmap (C, X, Y, W, H));
251 }
252
253
254
255 static void OptVerbose (const char* Opt attribute ((unused)),
256                         const char* Arg attribute ((unused)))
257 /* Increase versbosity */
258 {
259     ++Verbosity;
260 }
261
262
263
264 static void OptVersion (const char* Opt attribute ((unused)),
265                         const char* Arg attribute ((unused)))
266 /* Print the assembler version */
267 {
268     fprintf (stderr,
269              "%s V%s - (C) Copyright 2012, Ullrich von Bassewitz\n",
270              ProgName, GetVersionAsString ());
271 }
272
273
274
275 static void OptWrite (const char* Opt, const char* Arg)
276 /* Write an output file */
277 {
278     static const char* NameList[] = {
279         "name", "format"
280     };
281
282
283     /* Parse the argument */
284     Collection* A = ParseAttrList (Arg, NameList, 2);
285
286     /* Must have a file name given */
287     const char* FileName = NeedAttrVal (A, "name", Opt);
288
289     /* Determine the format of the input file */
290     int OF = ofAuto;
291     const char* Format = GetAttrVal (A, "format");
292     if (Format != 0) {
293         OF = FindOutputFormat (Format);
294         if (OF < 0) {
295             Error ("Unknown output format `%s'", Format);
296         }
297     }
298
299     /* Write the file */
300     WriteOutputFile (FileName, 0, OF);
301
302     /* Delete the attribute list */
303     FreeCollection (A);
304 }
305
306
307
308 int main (int argc, char* argv [])
309 /* sp65 main program */
310 {
311     /* Program long options */
312     static const LongOpt OptTab[] = {
313         { "--convert-to",       1,      OptConvertTo            },
314         { "--help",             0,      OptHelp                 },
315         { "--pop",              0,      OptPop                  },
316         { "--read",             1,      OptRead                 },
317         { "--slice",            1,      OptSlice                },
318         { "--verbose",          0,      OptVerbose              },
319         { "--version",          0,      OptVersion              },
320         { "--write",            1,      OptWrite                },
321     };
322
323     unsigned I;
324
325     /* Initialize the cmdline module */
326     InitCmdLine (&argc, &argv, "sp65");
327
328     /* Check the parameters */
329     I = 1;
330     while (I < ArgCount) {
331
332         /* Get the argument */
333         const char* Arg = ArgVec[I];
334
335         /* Check for an option */
336         if (Arg [0] == '-') {
337             switch (Arg [1]) {
338
339                 case '-':
340                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
341                     break;
342
343                 case 'V':
344                     OptVersion (Arg, 0);
345                     break;
346
347                 case 'c':
348                     OptConvertTo (Arg, GetArg (&I, 2));
349                     break;
350
351                 case 'h':
352                     OptHelp (Arg, 0);
353                     break;
354
355                 case 'r':
356                     OptRead (Arg, GetArg (&I, 2));
357                     break;
358
359                 case 'v':
360                     OptVerbose (Arg, 0);
361                     break;
362
363                 case 'w':
364                     OptWrite (Arg, GetArg (&I, 2));
365                     break;
366
367                 default:
368                     UnknownOption (Arg);
369                     break;
370
371             }
372         } else {
373             /* We don't accept anything else */
374             AbEnd ("Don't know what to do with `%s'", Arg);
375         }
376
377         /* Next argument */
378         ++I;
379     }
380
381     /* Success */
382     return EXIT_SUCCESS;
383 }
384
385
386