summaryrefslogtreecommitdiffstats
path: root/uisimulator/bmp.c
blob: 85a0fd2ffee220ecb97fa8663887c9bdb6226ab6 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Linus Nielsen Feltzing
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * 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
 *
 **********************************************/

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

#include "file.h"

#ifdef __GNUC__
#define STRUCT_PACKED __attribute__((packed))
#endif

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

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

static struct Fileheader fh;
static unsigned char* bmp;
static struct RGBQUAD palette[2]; /* two colors only */

static unsigned int bitmap_width, bitmap_height;
static unsigned char *bitmap;

#ifdef STANDALONE
static id_str[256];
static bool compress = false;
static bool assembly = false;
static unsigned char* converted_bmp;
static unsigned char* compressed_bmp;
static unsigned int width;
static unsigned int converted_size;
static unsigned int compressed_size;
static unsigned int rounded_width;
#endif

#define readshort(x) (((x&0xff00)>>8)|((x&0x00ff)<<8))
#define readlong(x) (((x&0xff000000)>>24)| \
                     ((x&0x00ff0000)>>8) | \
                     ((x&0x0000ff00)<<8) | \
                     ((x&0x000000ff)<<24))
                     

/*********************************************************************
 * read_bmp_file()
 *
 * Reads a monochrome BMP file and puts the data in a 1-pixel-per-byte
 * array. Returns 0 on success.
 *
 **********************************************/
int read_bmp_file(char* filename,
                  int *get_width,  /* in pixels */
                  int *get_height, /* in pixels */
                  char *bitmap)
{
   long PaddedWidth;
   int background;
   int fd = open(filename, O_RDONLY);
   long size;
   unsigned int row, col, byte, bit;
   int l;
   unsigned char *bmp;
   int width;
   int height;

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

      /* Exit if not monochrome */
      if(readshort(fh.BitCount) > 8)
      {
	debugf("error - Bitmap must be less than 8, got %d\n",
               readshort(fh.BitCount));
        close(fd);
        return 2;
      }

      /* Exit if too wide */
      if(readlong(fh.Width) > 112)
      {
	debugf("error - Bitmap is too wide (%d pixels, max is 112)\n",
               readlong(fh.Width));
        close(fd);
        return 3;
      }
      debugf("Bitmap is %d pixels wide\n", readlong(fh.Width));

      /* Exit if too high */
      if(readlong(fh.Height) > 64)
      {
	debugf("error - Bitmap is too high (%d pixels, max is 64)\n",
               readlong(fh.Height));
        close(fd);
	return 4;
      }
      debugf("Bitmap is %d pixels heigh\n", readlong(fh.Height));

      for(l=0;l < 2;l++)
      {
	if(read(fd, &palette[l],sizeof(struct RGBQUAD)) !=
           sizeof(struct RGBQUAD))
	{
          debugf("error - Can't read bitmap's color palette\n");
          close(fd);
          return 5;
	}
      }
      /* pass the other palettes */
      lseek(fd, 254*sizeof(struct RGBQUAD), SEEK_CUR);

      /* Try to guess the foreground and background colors.
         We assume that the foreground color is the darkest. */
      if(((int)palette[0].rgbRed +
          (int)palette[0].rgbGreen +
          (int)palette[0].rgbBlue) >
          ((int)palette[1].rgbRed +
          (int)palette[1].rgbGreen +
          (int)palette[1].rgbBlue))
      {
         background = 0;
      }
      else
      {
         background = 1;
      }

      /*  width = readlong(fh.Width)*readshort(fh.BitCount); */

      width = readlong(fh.Width);

      /* PaddedWidth = ((width+31)&(~0x1f))/8; */
      PaddedWidth = ((width+7)&(~0x7));
      size = PaddedWidth*readlong(fh.Height);

      bmp = (unsigned char *)malloc(size);

      if(bmp == NULL)
      {
          debugf("error - Out of memory\n");
          close(fd);
          return 6;
      }
      else
      {
          if(read(fd, (unsigned char*)bmp,(long)size) != size) {
              debugf("error - Can't read image\n");
              close(fd);
              return 7;
          }
      }

      bitmap_height = readlong(fh.Height);
      bitmap_width = readlong(fh.Width);

      *get_width = bitmap_width;
      *get_height = bitmap_height;

#if 0
      /* Now convert the bitmap into an array with 1 byte per pixel,
	exactly the size of the image */
      for(row = 0;row < bitmap_height;row++) {
	bit = 7;
	byte = 0;
	for(col = 0;col < bitmap_width;col++) {
          if((bmp[(bitmap_height - row - 1) * PaddedWidth + byte] &
              (1 << bit))) {

            bitmap[ (row/8) * bitmap_width + col ] |= 1<<(row&7);
          }
          else {
            bitmap[ (row/8) * bitmap_width + col ] &= ~ 1<<(row&7);
          }
          if(bit) {
            bit--;
          }
          else {
            bit = 7;
            byte++;
          }
	}
      }
#else
      /* Now convert the bitmap into an array with 1 byte per pixel,
         exactly the size of the image */

      for(row = 0;row < bitmap_height;row++) {
	for(col = 0;col < bitmap_width;col++) {
          if(bmp[(bitmap_height-1 -row) * PaddedWidth + col]) {
            bitmap[ (row/8) * bitmap_width + col ] &= ~ (1<<(row&7));
          }
          else {
            bitmap[ (row/8) * bitmap_width + col ] |= 1<<(row&7);
          }
	}
      }

#endif
   }
   close(fd);
   return 0; /* success */
}

#ifdef STANDALONE

/*********************************************************************
** read_next_converted_byte()
**
** Reads the next 6-pixel chunk from the 1-byte-per-pixel array,
** padding the last byte with zeros if the size is not divisible by 6.
**********************************************/
unsigned char read_next_converted_byte(void)
{
   unsigned char dest;
   unsigned int i;
   static unsigned int row = 0, col = 0;

   dest = 0;
   for(i = 0;i < 6 && col < bitmap_width;i++,col++)
   {
      if(bitmap[row * bitmap_width + col])
      {
	dest |= (unsigned char)(1 << (5-i));
      }
   }

   if(col >= bitmap_width)
   {
      col = 0;
      row++;
   }

   return dest;
}

/*********************************************************************
** convert_image()
**
** Converts the 1-byte-per-pixel array into a 6-pixel-per-byte array,
** i.e the BMP_FORMAT_VANILLA format.
**********************************************/
void convert_image(void)
{
   int newsize;
   unsigned int row, col;

   rounded_width = fh.Width/6 + ((fh.Width%6)?1:0);
   newsize = rounded_width * fh.Height;

   converted_bmp = (unsigned char *)malloc(newsize);

   for(row = 0;row < fh.Height;row++)
   {
      for(col = 0;col < rounded_width;col++)
      {
	converted_bmp[row * rounded_width + col] = read_next_converted_byte();
      }
   }
   converted_size = rounded_width * fh.Height;
}

#define COMPRESSED_ZEROS_AHEAD 0x40
#define COMPRESSED_ONES_AHEAD 0x80

/*********************************************************************
** compress_image()
**
** Compresses the BMP_FORMAT_VANILLA format with a simple RLE
** algorithm. The output is in the BMP_FORMAT_RLE format.
**********************************************/
void compress_image(void)
{
   unsigned int i, j, count;
   unsigned int index = 0;
   unsigned char val;

   compressed_bmp = (unsigned char *)malloc(converted_size);

   for(i = 0;i < converted_size;i++)
   {
      val = converted_bmp[i];

      if(val == 0|| val == 0x3f)
      {
	count = 0;
	while(count < 0x4000 && (i + count) < converted_size &&
	      converted_bmp[i+count] == val)
	{
	   count++;
	}
	if(count > 2)
	{
	   compressed_bmp[index++] = (unsigned char)
	      (((val == 0)?COMPRESSED_ZEROS_AHEAD:COMPRESSED_ONES_AHEAD) |
	      (count >> 8));
	   compressed_bmp[index++] = (unsigned char)(count & 0xff);
	}
	else
	{
	   for(j = 0;j < count;j++)
	   {
	      compressed_bmp[index++] = val;
	   }
	}
	i += count - 1;
      }
      else
      {
	compressed_bmp[index++] = val;
      }
   }

   compressed_size = index;
}

/*********************************************************************
** generate_c_source()
**
** Outputs a C source code with the converted/compressed bitmap in
** an array, accompanied by some #define's
**********************************************/
void generate_c_source(char *id, BOOL compressed)
{
   FILE *f;
   unsigned int i;
   unsigned int size;
   unsigned char *bmp;

   size = compressed?compressed_size:converted_size;
   bmp = compressed?compressed_bmp:converted_bmp;

   f = stdout;

   fprintf(f, "#define %s_WIDTH %d\n", id, rounded_width * 6);
   fprintf(f, "#define %s_HEIGHT %d\n", id, fh.Height);
   fprintf(f, "#define %s_SIZE %d\n", id, size + 6);
   if(compressed)
   {
      fprintf(f, "#define %s_ORIGINAL_SIZE %d\n", id, converted_size);
   }
   fprintf(f, "#define %s_FORMAT %s\n", id,
	      compressed?"BMP_FORMAT_RLE":"BMP_FORMAT_VANILLA");
   fprintf(f, "\nconst unsigned char bmpdata_%s[] = {", id);

   /* Header */
   fprintf(f, "\n    %s, 0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,",
	compressed?"BMP_FORMAT_RLE":"BMP_FORMAT_VANILLA",
	size >> 8, size & 0xff, 2, fh.Height, rounded_width * 6);

   for(i = 0;i < size;i++)
   {
      if(i % 10 == 0)
      {
	fprintf(f, "\n    ");
      }
      fprintf(f, "0x%02x,", bmp[i]);
   }
   fprintf(f, "\n};");
}

/*********************************************************************
** generate_asm_source()
**
** Outputs an ASM source code with the converted/compressed bitmap in
** an array, the first 2 bytes describing the X and Y size
**********************************************/
void generate_asm_source(char *id, BOOL compressed)
{
   FILE *f;
   unsigned int i;
   unsigned int size;
   unsigned char *bmp;

   size = compressed?compressed_size:converted_size;
   bmp = compressed?compressed_bmp:converted_bmp;

   f = stdout;

   fprintf(f, "bmpdata_%s:\n", id);
   /* Header */
   fprintf(f, "\n    db %s, %.2xh,%.2xh,%.2xh,%.2xh,%.2xh,",
	compressed?"1":"0",
	size >> 8, size & 0xff, 2, fh.Height, rounded_width * 6);

   for(i = 0;i < size;i++)
   {
      if(i % 10 == 0)
      {
	fprintf(f, "\n   db ");
      }
      fprintf(f, "%.2xh,", bmp[i]);
   }
   fprintf(f, "\n");
}

void print_usage(void)
{
   printf("bmp2mt - Converts BMP files to MT Pro source code format\n");
   printf("build date: " __DATE__ "\n\n");
   printf("Usage: %s [-i <id>] [-c] [-a] <bitmap file>\n"
   "-i <id>     Bitmap ID (default is filename without extension)\n"
   "-c          Compress (BMP_FORMAT_RLE)\n"
   "-f          Frexx Format!!!\n"
   "-a          Assembly format source code\n", APPLICATION_NAME);
}

#pragma argsused
int main(int argc, char **argv)
{
   char *bmp_filename = NULL;
   char *id = NULL;
   char errstr[80];
   int i;

   for(i = 1;i < argc;i++)
   {
      if(argv[i][0] == '-')
      {
	switch(argv[i][1])
	{
	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 'c':   /* Compressed */
	   compress = true;
	   break;

	case 'a':   /* Assembly */
	   assembly = true;
	   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)
   {
      id = strdup(bmp_filename);

      for(i = 0;id[i];i++)
      {
	if(id[i] == ' ')
	{
	   id[i] = '_';
	}
	else if(id[i] == '.')
	{
	   id[i] = '\0';
	   break;
	}
	else
	{
	   id[i] = (char)toupper(id[i]);
	}
      }
   }

   read_bmp_file(bmp_filename);
   convert_image();
   if(fh.Width % 6)
   {

      sprintf(errstr, "warning - width is not divisible by 6 (%d), "
		"padding with zeros to %d\n", fh.Width, rounded_width*6);
      print_error(errstr, 0);
   }

   if(compress)
   {
      compress_image();
   }

   if(assembly)
   {
      generate_asm_source(id, compress);
   }
   else
   {
      generate_c_source(id, compress);
   }
   return 0;
}

#endif