summaryrefslogtreecommitdiff
path: root/tools/bmp2rb.c
blob: 2a7dcdee9235b259f5058ff24afb9b5a56ae5743 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Linus Nielsen Feltzing
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
/****************************************************************************
 *
 * Converts BMP files to Rockbox bitmap format
 *
 * 1999-05-03 Linus Nielsen Feltzing
 *
 * 2005-07-06 Jens Arnold
 *            added reading of 4, 16, 24 and 32 bit bmps
 *            added 2 new target formats (playergfx and iriver 4-grey)
 *
 ****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define debugf printf

#ifdef __GNUC__
#define STRUCT_PACKED __attribute__((packed))
#else
#define STRUCT_PACKED
#pragma pack (push, 2)
#endif

struct Fileheader
{
    unsigned short Type;          /* signature - 'BM' */
    unsigned int  Size;          /* file size in bytes */
    unsigned short Reserved1;     /* 0 */
    unsigned short Reserved2;     /* 0 */
    unsigned int  OffBits;       /* offset to bitmap */
    unsigned int  StructSize;    /* size of this struct (40) */
    unsigned int  Width;         /* bmap width in pixels */
    unsigned int  Height;        /* bmap height in pixels */
    unsigned short Planes;        /* num planes - always 1 */
    unsigned short BitCount;      /* bits per pixel */
    unsigned int  Compression;   /* compression flag */
    unsigned int  SizeImage;     /* image size in bytes */
    int           XPelsPerMeter; /* horz resolution */
    int           YPelsPerMeter; /* vert resolution */
    unsigned int  ClrUsed;       /* 0 -> color table size */
    unsigned int  ClrImportant;  /* important color count */
} STRUCT_PACKED;

struct RGBQUAD
{
    unsigned char rgbBlue;
    unsigned char rgbGreen;
    unsigned char rgbRed;
    unsigned char rgbReserved;
} STRUCT_PACKED;

union RAWDATA {
    void *d; /* unspecified */
    unsigned short *d16; /* depth <= 16 */
    struct { unsigned char b, g, r; } *d24; /* depth = 24 BGR */
};

short readshort(void* value)
{
    unsigned char* bytes = (unsigned char*) value;
    return bytes[0] | (bytes[1] << 8);
}

int readint(void* value)
{
    unsigned char* bytes = (unsigned char*) value;
    return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
}

unsigned char brightness(struct RGBQUAD color)
{
    return (3 * (unsigned int)color.rgbRed + 6 * (unsigned int)color.rgbGreen
              + (unsigned int)color.rgbBlue) / 10;
}

#ifndef O_BINARY
#define O_BINARY 0 /* systems that don't have O_BINARY won't make a difference
                      on text and binary files */
#endif

/****************************************************************************
 * read_bmp_file()
 *
 * Reads an uncompressed BMP file and puts the data in a 4-byte-per-pixel
 * (RGBQUAD) array. Returns 0 on success.
 *
 ***************************************************************************/

int read_bmp_file(char* filename,
                  int *get_width,  /* in pixels */
                  int *get_height, /* in pixels */
                  struct RGBQUAD **bitmap)
{
    struct Fileheader fh;
    struct RGBQUAD palette[256];

    int fd = open(filename, O_RDONLY| O_BINARY);
    unsigned short data;
    unsigned char *bmp;
    int width, height;
    int padded_width;
    int size;
    int row, col, i;
    int numcolors, compression;
    int depth;

    if (fd == -1)
    {
        debugf("error - can't open '%s'\n", filename);
        return 1;
    }
    if (read(fd, &fh, sizeof(struct Fileheader)) !=
        sizeof(struct Fileheader))
    {
        debugf("error - can't Read Fileheader Stucture\n");
        close(fd);
        return 2;
    }

    compression = readint(&fh.Compression);

    if (compression != 0)
    {
        debugf("error - Unsupported compression %d\n", compression);
        close(fd);
        return 3;
    }

    depth = readshort(&fh.BitCount);

    if (depth <= 8)
    {
        numcolors = readint(&fh.ClrUsed);
        if (numcolors == 0)
            numcolors = 1 << depth;

        if (read(fd, &palette[0], numcolors * sizeof(struct RGBQUAD))
            != (int)(numcolors * sizeof(struct RGBQUAD)))
        {
            debugf("error - Can't read bitmap's color palette\n");
            close(fd);
            return 4;
        }
    }

    width = readint(&fh.Width);
    height = readint(&fh.Height);
    padded_width = ((width * depth + 31) / 8) & ~3; /* aligned 4-bytes boundaries */

    size = padded_width * height; /* read this many bytes */
    bmp = (unsigned char *)malloc(size);
    *bitmap = (struct RGBQUAD *)malloc(width * height * sizeof(struct RGBQUAD));

    if ((bmp == NULL) || (*bitmap == NULL))
    {
        debugf("error - Out of memory\n");
        close(fd);
        return 5;
    }

    if (lseek(fd, (off_t)readint(&fh.OffBits), SEEK_SET) < 0)
    {
        debugf("error - Can't seek to start of image data\n");
        close(fd);
        return 6;
    }
    if (read(fd, (unsigned char*)bmp, (int)size) != size)
    {
        debugf("error - Can't read image\n");
        close(fd);
        return 7;
    }

    close(fd);
    *get_width = width;
    *get_height = height;

    switch (depth)
    {
      case 1:
        for (row = 0; row < height; row++)
            for (col = 0; col < width; col++)
            {
                data = (bmp[(height - 1 - row) * padded_width + col / 8]
                        >> (~col & 7)) & 1;
                (*bitmap)[row * width + col] = palette[data];
            }
        break;

      case 4:
        for (row = 0; row < height; row++)
            for (col = 0; col < width; col++)
            {
                data = (bmp[(height - 1 - row) * padded_width + col / 2]
                        >> (4 * (~col & 1))) & 0x0F;
                (*bitmap)[row * width + col] = palette[data];
            }
        break;

      case 8:
        for (row = 0; row < height; row++)
            for (col = 0; col < width; col++)
            {
                data = bmp[(height - 1 - row) * padded_width + col];
                (*bitmap)[row * width + col] = palette[data];
            }
        break;

      case 16:
        for (row = 0; row < height; row++)
            for (col = 0; col < width; col++)
            {
                data = readshort(&bmp[(height - 1 - row) * padded_width + 2 * col]);
                (*bitmap)[row * width + col].rgbRed =
                    ((data >> 7) & 0xF8) | ((data >> 12) & 0x07);
                (*bitmap)[row * width + col].rgbGreen =
                    ((data >> 2) & 0xF8) | ((data >> 7) & 0x07);
                (*bitmap)[row * width + col].rgbBlue =
                    ((data << 3) & 0xF8) | ((data >> 2) & 0x07);
                (*bitmap)[row * width + col].rgbReserved = 0;
            }
        break;

      case 24:
        for (row = 0; row < height; row++)
            for (col = 0; col < width; col++)
            {
                i = (height - 1 - row) * padded_width + 3 * col;
                (*bitmap)[row * width + col].rgbRed = bmp[i+2];
                (*bitmap)[row * width + col].rgbGreen = bmp[i+1];
                (*bitmap)[row * width + col].rgbBlue = bmp[i];
                (*bitmap)[row * width + col].rgbReserved = 0;
            }
        break;

      case 32:
        for (row = 0; row < height; row++)
            for (col = 0; col < width; col++)
            {
                i = (height - 1 - row) * padded_width + 4 * col;
                (*bitmap)[row * width + col].rgbRed = bmp[i+2];
                (*bitmap)[row * width + col].rgbGreen = bmp[i+1];
                (*bitmap)[row * width + col].rgbBlue = bmp[i];
                (*bitmap)[row * width + col].rgbReserved = 0;
            }
        break;

      default: /* should never happen */
        debugf("error - Unsupported bitmap depth %d.\n", depth);
        return 8;
    }

    free(bmp);

    return 0; /* success */
}

/****************************************************************************
 * transform_bitmap()
 *
 * Transform a 4-byte-per-pixel bitmap (RGBQUAD) into one of the supported
 * destination formats
 ****************************************************************************/

int transform_bitmap(const struct RGBQUAD *src, int width, int height,
                     int format, union RAWDATA *dst, int *dst_width,
                     int *dst_height, int *dst_depth)
{
    int row, col;
    int dst_w, dst_h, dst_d;
    int alloc_size;
    union RAWDATA dest;

    switch (format)
    {
      case 0: /* Archos recorders, Ondio, Iriver H1x0 monochrome */
        dst_w = width;
        dst_h = (height + 7) / 8;
        dst_d = 8;
        break;

      case 1: /* Archos player graphics library */
        dst_w = (width + 7) / 8;
        dst_h = height;
        dst_d = 8;
        break;

      case 2: /* Iriver H1x0 4-grey */
        dst_w = width;
        dst_h = (height + 3) / 4;
        dst_d = 8;
        break;

      case 3: /* Canonical 8-bit grayscale */
        dst_w = width;
        dst_h = height;
        dst_d = 8;
        break;

      case 4: /* 16-bit packed RGB (5-6-5) */
      case 5: /* 16-bit packed and byte-swapped RGB (5-6-5) */
      case 8: /* 16-bit packed RGB (5-6-5) vertical stride*/
        dst_w = width;
        dst_h = height;
        dst_d = 16;
        break;

      case 6: /* greyscale iPods 4-grey */
        dst_w = (width + 3) / 4;
        dst_h = height;
        dst_d = 8;
        break;

      case 7: /* greyscale X5 remote 4-grey */
        dst_w = width;
        dst_h = (height + 7) / 8;
        dst_d = 16;
        break;

      case 9: /* 24-bit BGR */
        dst_w = width;
        dst_h = height;
        dst_d = 24;
        break;

      default: /* unknown */
        debugf("error - Undefined destination format\n");
        return 1;
    }

    if (dst_d <= 16)
        alloc_size = sizeof(dest.d16[0]);
    else /* 24 bit */
        alloc_size = sizeof(dest.d24[0]);
    dest.d = calloc(dst_w * dst_h, alloc_size);

    if (dest.d == NULL)
    {
        debugf("error - Out of memory.\n");
        return 2;
    }
    *dst_width = dst_w;
    *dst_height = dst_h;
    *dst_depth = dst_d;
    *dst = dest;

    switch (format)
    {
      case 0: /* Archos recorders, Ondio, Iriver H1x0 b&w */
        for (row = 0; row < height; row++)
            for (col = 0; col < width; col++)
            {
                dest.d16[(row/8) * dst_w + col] |=
                       (~brightness(src[row * width + col]) & 0x80) >> (~row & 7);
            }
        break;

      case 1: /* Archos player graphics library */
        for (row = 0; row < height; row++)
            for (col = 0; col < width; col++)
            {
                dest.d16[row * dst_w + (col/8)] |=
                       (~brightness(src[row * width + col]) & 0x80) >> (col & 7);
            }
        break;

      case 2: /* Iriver H1x0 4-grey */
        for (row = 0; row < height; row++)
            for (col = 0; col < width; col++)
            {
                dest.d16[(row/4) * dst_w + col] |=
                       (~brightness(src[row * width + col]) & 0xC0) >> (2 * (~row & 3));
            }
        break;

      case 3: /* Canonical 8-bit grayscale */
        for (row = 0; row < height; row++)
            for (col = 0; col < width; col++)
            {
                dest.d16[row * dst_w + col] = brightness(src[row * width + col]);
            }
        break;

      case 4: /* 16-bit packed RGB (5-6-5) */
      case 5: /* 16-bit packed and byte-swapped RGB (5-6-5) */
        for (row = 0; row < height; row++)
            for (col = 0; col < width; col++)
            {
                unsigned short rgb =
                    (((src[row * width + col].rgbRed >> 3) << 11) |
                     ((src[row * width + col].rgbGreen >> 2) << 5) |
                     ((src[row * width + col].rgbBlue >> 3)));

                if (format == 4)
                    dest.d16[row * dst_w + col] = rgb;
                else
                    dest.d16[row * dst_w + col] = ((rgb&0xff00)>>8)|((rgb&0x00ff)<<8);
            }
        break;

      case 6: /* greyscale iPods 4-grey */
        for (row = 0; row < height; row++)
            for (col = 0; col < width; col++)
            {
                dest.d16[row * dst_w + (col/4)] |=
                       (~brightness(src[row * width + col]) & 0xC0) >> (2 * (col & 3));
            }
        break;

      case 7: /* greyscale X5 remote 4-grey */
        for (row = 0; row < height; row++)
            for (col = 0; col < width; col++)
            {
                unsigned short data = (~brightness(src[row * width + col]) & 0xC0) >> 6;

                data = (data | (data << 7)) & 0x0101;
                dest.d16[(row/8) * dst_w + col] |= data << (row & 7);
            }
        break;
        
      case 8: /* 16-bit packed RGB (5-6-5) vertical stride*/
        for (row = 0; row < height; row++)
            for (col = 0; col < width; col++)
            {
                unsigned short rgb =
                    (((src[row * width + col].rgbRed >> 3) << 11) |
                     ((src[row * width + col].rgbGreen >> 2) << 5) |
                     ((src[row * width + col].rgbBlue >> 3)));
                     
                dest.d16[col * dst_h + row] = rgb;
            }
        break;

      case 9: /* 24-bit RGB */
        for (row = 0; row < height; row++)
            for (col = 0; col < width; col++)
            {
                dest.d24[row * width + col].r = src[row * width + col].rgbRed;
                dest.d24[row * width + col].g = src[row * width + col].rgbGreen;
                dest.d24[row * width + col].b = src[row * width + col].rgbBlue;
            }
        break;
    }

    return 0;
}

/****************************************************************************
 * generate_c_source()
 *
 * Outputs a C source code with the bitmap in an array, accompanied by
 * some #define's
 ****************************************************************************/

void generate_c_source(char *id, char* header_dir, int width, int height,
                       const union RAWDATA *t_bitmap, int t_width,
                       int t_height, int t_depth, bool t_mono, bool create_bm)
{
    FILE *f;
    FILE *fh;
    int i, a;
    char header_name[1024];
    bool have_header = header_dir && header_dir[0];
    create_bm = have_header && create_bm;

    if (t_depth > 16)
    {
        fprintf(stderr, "Generating C source not supported for this format\n");
        fprintf(stderr, "because Rockbox does not support this display depth yet.\n");
        fprintf(stderr, "However you are welcome to fix this!\n");
        return;
    }

    if (!id || !id[0])
        id = "bitmap";

    f = stdout;

    if (have_header)
    {
        snprintf(header_name,sizeof(header_name),"%s/%s.h",header_dir,id);
        fh = fopen(header_name,"w+");

        if (fh == NULL)
        {
            debugf("error - can't open '%s'\n", header_name);
            return;
        }
        fprintf(fh,
                "#define BMPHEIGHT_%s %d\n"
                "#define BMPWIDTH_%s %d\n",
                id, height, id, width);
        if (t_depth <= 8)
            fprintf(fh, "extern const unsigned char %s[];\n", id);
        else
            fprintf(fh, "extern const unsigned short %s[];\n", id);

        if (create_bm)
        {
            fprintf(f, "#include \"lcd.h\"\n");
            fprintf(fh, "extern const struct bitmap bm_%s;\n", id);
        }
        fclose(fh);
    } else {
        fprintf(f,
                "#define BMPHEIGHT_%s %d\n"
                "#define BMPWIDTH_%s %d\n",
                id, height, id, width);
    }

    if (create_bm) {
        fprintf(f, "#include \"%s\"\n", header_name);
    }

    if (t_depth <= 8)
        fprintf(f, "const unsigned char %s[] = {\n", id);
    else
        fprintf(f, "const unsigned short %s[] = {\n", id);

    for (i = 0; i < t_height; i++)
    {
        for (a = 0; a < t_width; a++)
        {
            if (t_depth <= 8)
                fprintf(f, "0x%02x,%c", t_bitmap->d16[i * t_width + a],
                        (a + 1) % 13 ? ' ' : '\n');
            else if (t_depth == 16)
                fprintf(f, "0x%04x,%c", t_bitmap->d16[i * t_width + a],
                        (a + 1) % 10 ? ' ' : '\n');
        }
        fprintf(f, "\n");
    }

    fprintf(f, "\n};\n\n");

    if (create_bm) {
        char format_line[] = "    .format = FORMAT_NATIVE, \n";
        fprintf(f, "const struct bitmap bm_%s = { \n"
                   "    .width = BMPWIDTH_%s, \n"
                   "    .height = BMPHEIGHT_%s, \n"
                   "%s"
                   "    .data = (unsigned char*)%s,\n"
                   "};\n",
                    id, id, id,
                    t_mono ? "" : format_line,
                    id);
    }
}

void generate_raw_file(const union RAWDATA *t_bitmap, 
                       int t_width, int t_height, int t_depth)
{
    FILE *f;
    int i, a;
    unsigned char lo,hi;

    f = stdout;

    for (i = 0; i < t_height; i++)
    {
        for (a = 0; a < t_width; a++)
        {
            if (t_depth <= 8)
            {
                lo = (t_bitmap->d16[i * t_width + a] & 0x00ff);
                fwrite(&lo, 1, 1, f);
            }
            else if (t_depth == 16)
            {
                lo = (t_bitmap->d16[i * t_width + a] & 0x00ff);
                hi = (t_bitmap->d16[i * t_width + a] & 0xff00) >> 8;
                fwrite(&lo, 1, 1, f);
                fwrite(&hi, 1, 1, f);
            }
            else /* 24 */
            {
                fwrite(&t_bitmap->d24[i * t_width + a], 3, 1, f);
            }
        }
    }
}

/****************************************************************************
 * generate_ascii()
 *
 * Outputs an ascii picture of the bitmap
 ****************************************************************************/

void generate_ascii(int width, int height, struct RGBQUAD *bitmap)
{
    FILE *f;
    int x, y;

    f = stdout;

    /* for screen output debugging */
    for (y = 0; y < height; y++)
    {
        for (x = 0; x < width; x++)
        {
            fprintf(f, (brightness(bitmap[y * width + x]) & 0x80) ? " " : "*");
        }
        fprintf(f, "\n");
    }
}

void print_usage(void)
{
    printf("Usage: %s [-i <id>] [-a] <bitmap file>\n"
           "\t-i <id>  Bitmap name (default is filename without extension)\n"
           "\t-h <dir> Create header file in <dir>/<id>.h\n"
           "\t-a       Show ascii picture of bitmap\n"
           "\t-b       Create bitmap struct along with pixel array\n"
           "\t-r       Generate RAW file (little-endian)\n"
           "\t-f <n>   Generate destination format n, default = 0\n"
           "\t         0  Archos recorder, Ondio, Iriver H1x0 mono\n"
           , APPLICATION_NAME);
    printf("\t         1  Archos player graphics library\n"
           "\t         2  Iriver H1x0 4-grey\n"
           "\t         3  Canonical 8-bit greyscale\n"
           "\t         4  16-bit packed 5-6-5 RGB (iriver H300)\n"
           "\t         5  16-bit packed and byte-swapped 5-6-5 RGB (iPod, Fuzev2)\n"
           "\t         6  Greyscale iPod 4-grey\n"
           "\t         7  Greyscale X5 remote 4-grey\n"
           "\t         8  16-bit packed 5-6-5 RGB with a vertical stride\n"
           "\t         9  24-bit BGR (raw only for now)\n");
    printf("build date: " __DATE__ "\n\n");
}

int main(int argc, char **argv)
{
    char *bmp_filename = NULL;
    char *id = NULL;
    char* header_dir = NULL;
    int i;
    int ascii = false;
    int format = 0;
    struct RGBQUAD *bitmap = NULL;
    union RAWDATA t_bitmap = { NULL };
    int width, height;
    int t_width, t_height, t_depth;
    bool raw = false;
    bool create_bm = false;


    for (i = 1;i < argc;i++)
    {
        if (argv[i][0] == '-')
        {
            switch (argv[i][1])
            {
              case 'h':   /* .h filename */
                if (argv[i][2])
                {
                    header_dir = &argv[i][2];
                }
                else if (argc > i+1)
                {
                    header_dir = argv[i+1];
                    i++;
                }
                else
                {
                    print_usage();
                    exit(1);
                }
                break;

              case 'i':   /* ID */
                if (argv[i][2])
                {
                    id = &argv[i][2];
                }
                else if (argc > i+1)
                {
                    id = argv[i+1];
                    i++;
                }
                else
                {
                    print_usage();
                    exit(1);
                }
                break;

              case 'a':   /* Ascii art */
                ascii = true;
                break;

              case 'b':
                create_bm = true;
                break;

              case 'r':   /* Raw File */
                raw = true;
                break;

              case 'f':
                if (argv[i][2])
                {
                    format = atoi(&argv[i][2]);
                }
                else if (argc > i+1)
                {
                    format = atoi(argv[i+1]);
                    i++;
                }
                else
                {
                    print_usage();
                    exit(1);
                }
                break;

              default:
                print_usage();
                exit(1);
                break;
            }
        }
        else
        {
            if (!bmp_filename)
            {
                bmp_filename = argv[i];
            }
            else
            {
                print_usage();
                exit(1);
            }
        }
    }

    if (!bmp_filename)
    {
        print_usage();
        exit(1);
    }

    if (!id)
    {
        char *ptr=strrchr(bmp_filename, '/');
        if (ptr)
            ptr++;
        else
            ptr = bmp_filename;
        id = strdup(ptr);
        for (i = 0; id[i]; i++)
            if (id[i] == '.')
                id[i] = '\0';
    }

    if (read_bmp_file(bmp_filename, &width, &height, &bitmap))
        exit(1);


    if (ascii)
    {
        generate_ascii(width, height, bitmap);
    }
    else
    {
        if (transform_bitmap(bitmap, width, height, format, &t_bitmap, 
                             &t_width, &t_height, &t_depth))
            exit(1);
        if(raw)
            generate_raw_file(&t_bitmap, t_width, t_height, t_depth);
        else
            generate_c_source(id, header_dir, width, height, &t_bitmap, 
                              t_width, t_height, t_depth,
                              format <= 1, create_bm);
    }

    return 0;
}