2002-08-16 14:02:48 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 by Linus Nielsen Feltzing
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* 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.
|
2002-08-16 14:02:48 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2005-07-07 22:32:42 +00:00
|
|
|
/****************************************************************************
|
2002-08-16 14:02:48 +00:00
|
|
|
*
|
|
|
|
* Converts BMP files to Rockbox bitmap format
|
|
|
|
*
|
|
|
|
* 1999-05-03 Linus Nielsen Feltzing
|
|
|
|
*
|
2005-07-07 22:32:42 +00:00
|
|
|
* 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)
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2002-08-16 14:02:48 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
2008-04-10 21:16:00 +00:00
|
|
|
#include <unistd.h>
|
2002-08-16 14:02:48 +00:00
|
|
|
|
|
|
|
#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
|
|
|
|
{
|
2005-07-07 22:32:42 +00:00
|
|
|
unsigned short Type; /* signature - 'BM' */
|
2006-02-16 20:03:07 +00:00
|
|
|
unsigned int Size; /* file size in bytes */
|
2005-07-07 22:32:42 +00:00
|
|
|
unsigned short Reserved1; /* 0 */
|
|
|
|
unsigned short Reserved2; /* 0 */
|
2006-02-08 13:05:37 +00:00
|
|
|
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 */
|
2005-07-07 22:32:42 +00:00
|
|
|
unsigned short Planes; /* num planes - always 1 */
|
|
|
|
unsigned short BitCount; /* bits per pixel */
|
2006-02-08 13:05:37 +00:00
|
|
|
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 */
|
2002-08-16 14:02:48 +00:00
|
|
|
} STRUCT_PACKED;
|
|
|
|
|
|
|
|
struct RGBQUAD
|
|
|
|
{
|
2005-07-07 22:32:42 +00:00
|
|
|
unsigned char rgbBlue;
|
|
|
|
unsigned char rgbGreen;
|
|
|
|
unsigned char rgbRed;
|
|
|
|
unsigned char rgbReserved;
|
2002-08-16 14:02:48 +00:00
|
|
|
} STRUCT_PACKED;
|
|
|
|
|
2003-05-21 06:31:44 +00:00
|
|
|
short readshort(void* value)
|
|
|
|
{
|
|
|
|
unsigned char* bytes = (unsigned char*) value;
|
|
|
|
return bytes[0] | (bytes[1] << 8);
|
|
|
|
}
|
|
|
|
|
2006-02-08 13:05:37 +00:00
|
|
|
int readint(void* value)
|
2003-05-21 06:31:44 +00:00
|
|
|
{
|
|
|
|
unsigned char* bytes = (unsigned char*) value;
|
|
|
|
return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
|
|
|
|
}
|
2002-08-16 14:02:48 +00:00
|
|
|
|
2005-07-28 16:31:02 +00:00
|
|
|
unsigned char brightness(struct RGBQUAD color)
|
2005-07-07 22:32:42 +00:00
|
|
|
{
|
2005-07-28 16:31:02 +00:00
|
|
|
return (3 * (unsigned int)color.rgbRed + 6 * (unsigned int)color.rgbGreen
|
|
|
|
+ (unsigned int)color.rgbBlue) / 10;
|
2005-07-07 22:32:42 +00:00
|
|
|
}
|
|
|
|
|
2006-02-28 07:36:38 +00:00
|
|
|
#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
|
|
|
|
|
2005-07-07 22:32:42 +00:00
|
|
|
/****************************************************************************
|
2002-08-16 14:02:48 +00:00
|
|
|
* read_bmp_file()
|
|
|
|
*
|
2005-07-28 16:31:02 +00:00
|
|
|
* Reads an uncompressed BMP file and puts the data in a 4-byte-per-pixel
|
|
|
|
* (RGBQUAD) array. Returns 0 on success.
|
2002-08-16 14:02:48 +00:00
|
|
|
*
|
2005-07-07 22:32:42 +00:00
|
|
|
***************************************************************************/
|
|
|
|
|
2002-08-16 14:02:48 +00:00
|
|
|
int read_bmp_file(char* filename,
|
2006-02-08 13:05:37 +00:00
|
|
|
int *get_width, /* in pixels */
|
|
|
|
int *get_height, /* in pixels */
|
2005-07-28 16:31:02 +00:00
|
|
|
struct RGBQUAD **bitmap)
|
2002-08-16 14:02:48 +00:00
|
|
|
{
|
2002-08-21 12:11:51 +00:00
|
|
|
struct Fileheader fh;
|
2005-07-07 22:32:42 +00:00
|
|
|
struct RGBQUAD palette[256];
|
2002-08-16 14:02:48 +00:00
|
|
|
|
2006-02-28 07:36:38 +00:00
|
|
|
int fd = open(filename, O_RDONLY| O_BINARY);
|
2005-07-07 22:32:42 +00:00
|
|
|
unsigned short data;
|
2002-08-21 12:11:51 +00:00
|
|
|
unsigned char *bmp;
|
2006-02-08 13:05:37 +00:00
|
|
|
int width, height;
|
|
|
|
int padded_width;
|
|
|
|
int size;
|
|
|
|
int row, col, i;
|
|
|
|
int numcolors, compression;
|
2002-08-21 12:11:51 +00:00
|
|
|
int depth;
|
2002-08-16 14:02:48 +00:00
|
|
|
|
2005-07-07 22:32:42 +00:00
|
|
|
if (fd == -1)
|
2002-08-21 12:11:51 +00:00
|
|
|
{
|
|
|
|
debugf("error - can't open '%s'\n", filename);
|
|
|
|
return 1;
|
|
|
|
}
|
2005-07-07 22:32:42 +00:00
|
|
|
if (read(fd, &fh, sizeof(struct Fileheader)) !=
|
2002-08-16 14:02:48 +00:00
|
|
|
sizeof(struct Fileheader))
|
2005-07-07 22:32:42 +00:00
|
|
|
{
|
|
|
|
debugf("error - can't Read Fileheader Stucture\n");
|
|
|
|
close(fd);
|
|
|
|
return 2;
|
|
|
|
}
|
2007-01-17 19:54:35 +00:00
|
|
|
|
2006-02-08 13:05:37 +00:00
|
|
|
compression = readint(&fh.Compression);
|
2005-07-07 22:32:42 +00:00
|
|
|
|
|
|
|
if (compression != 0)
|
|
|
|
{
|
2008-04-10 21:16:00 +00:00
|
|
|
debugf("error - Unsupported compression %d\n", compression);
|
2005-07-07 22:32:42 +00:00
|
|
|
close(fd);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
depth = readshort(&fh.BitCount);
|
|
|
|
|
|
|
|
if (depth <= 8)
|
|
|
|
{
|
2006-02-08 13:05:37 +00:00
|
|
|
numcolors = readint(&fh.ClrUsed);
|
2005-07-07 22:32:42 +00:00
|
|
|
if (numcolors == 0)
|
|
|
|
numcolors = 1 << depth;
|
|
|
|
|
|
|
|
if (read(fd, &palette[0], numcolors * sizeof(struct RGBQUAD))
|
2008-04-10 21:16:00 +00:00
|
|
|
!= (int)(numcolors * sizeof(struct RGBQUAD)))
|
2005-07-07 22:32:42 +00:00
|
|
|
{
|
|
|
|
debugf("error - Can't read bitmap's color palette\n");
|
|
|
|
close(fd);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-08 13:05:37 +00:00
|
|
|
width = readint(&fh.Width);
|
|
|
|
height = readint(&fh.Height);
|
2006-02-27 21:59:48 +00:00
|
|
|
padded_width = ((width * depth + 31) / 8) & ~3; /* aligned 4-bytes boundaries */
|
2005-07-07 22:32:42 +00:00
|
|
|
|
|
|
|
size = padded_width * height; /* read this many bytes */
|
|
|
|
bmp = (unsigned char *)malloc(size);
|
2005-07-28 16:31:02 +00:00
|
|
|
*bitmap = (struct RGBQUAD *)malloc(width * height * sizeof(struct RGBQUAD));
|
2005-07-07 22:32:42 +00:00
|
|
|
|
|
|
|
if ((bmp == NULL) || (*bitmap == NULL))
|
|
|
|
{
|
|
|
|
debugf("error - Out of memory\n");
|
|
|
|
close(fd);
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
2006-02-08 13:05:37 +00:00
|
|
|
if (lseek(fd, (off_t)readint(&fh.OffBits), SEEK_SET) < 0)
|
2005-07-07 22:32:42 +00:00
|
|
|
{
|
|
|
|
debugf("error - Can't seek to start of image data\n");
|
|
|
|
close(fd);
|
|
|
|
return 6;
|
|
|
|
}
|
2006-02-08 13:05:37 +00:00
|
|
|
if (read(fd, (unsigned char*)bmp, (int)size) != size)
|
2005-07-07 22:32:42 +00:00
|
|
|
{
|
|
|
|
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++)
|
|
|
|
{
|
2005-07-28 16:31:02 +00:00
|
|
|
data = (bmp[(height - 1 - row) * padded_width + col / 8]
|
2005-07-07 22:32:42 +00:00
|
|
|
>> (~col & 7)) & 1;
|
2005-07-28 16:31:02 +00:00
|
|
|
(*bitmap)[row * width + col] = palette[data];
|
2005-07-07 22:32:42 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
for (row = 0; row < height; row++)
|
|
|
|
for (col = 0; col < width; col++)
|
|
|
|
{
|
2007-01-17 19:54:35 +00:00
|
|
|
data = (bmp[(height - 1 - row) * padded_width + col / 2]
|
2005-07-07 22:32:42 +00:00
|
|
|
>> (4 * (~col & 1))) & 0x0F;
|
2005-07-28 16:31:02 +00:00
|
|
|
(*bitmap)[row * width + col] = palette[data];
|
2005-07-07 22:32:42 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
for (row = 0; row < height; row++)
|
|
|
|
for (col = 0; col < width; col++)
|
|
|
|
{
|
|
|
|
data = bmp[(height - 1 - row) * padded_width + col];
|
2005-07-28 16:31:02 +00:00
|
|
|
(*bitmap)[row * width + col] = palette[data];
|
2005-07-07 22:32:42 +00:00
|
|
|
}
|
|
|
|
break;
|
2007-01-17 19:54:35 +00:00
|
|
|
|
2005-07-07 22:32:42 +00:00
|
|
|
case 16:
|
|
|
|
for (row = 0; row < height; row++)
|
|
|
|
for (col = 0; col < width; col++)
|
|
|
|
{
|
|
|
|
data = readshort(&bmp[(height - 1 - row) * padded_width + 2 * col]);
|
2005-07-28 16:31:02 +00:00
|
|
|
(*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;
|
2005-07-07 22:32:42 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 24:
|
|
|
|
for (row = 0; row < height; row++)
|
|
|
|
for (col = 0; col < width; col++)
|
|
|
|
{
|
|
|
|
i = (height - 1 - row) * padded_width + 3 * col;
|
2005-07-28 16:31:02 +00:00
|
|
|
(*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;
|
2005-07-07 22:32:42 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 32:
|
|
|
|
for (row = 0; row < height; row++)
|
|
|
|
for (col = 0; col < width; col++)
|
2007-01-17 19:54:35 +00:00
|
|
|
{
|
2005-07-07 22:32:42 +00:00
|
|
|
i = (height - 1 - row) * padded_width + 4 * col;
|
2005-07-28 16:31:02 +00:00
|
|
|
(*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;
|
2005-07-07 22:32:42 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* should never happen */
|
|
|
|
debugf("error - Unsupported bitmap depth %d.\n", depth);
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(bmp);
|
2007-01-17 19:54:35 +00:00
|
|
|
|
2005-07-07 22:32:42 +00:00
|
|
|
return 0; /* success */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* transform_bitmap()
|
|
|
|
*
|
2005-07-28 16:31:02 +00:00
|
|
|
* Transform a 4-byte-per-pixel bitmap (RGBQUAD) into one of the supported
|
2005-07-07 22:32:42 +00:00
|
|
|
* destination formats
|
|
|
|
****************************************************************************/
|
|
|
|
|
2006-02-08 13:05:37 +00:00
|
|
|
int transform_bitmap(const struct RGBQUAD *src, int width, int height,
|
|
|
|
int format, unsigned short **dest, int *dst_width,
|
2006-02-16 20:03:07 +00:00
|
|
|
int *dst_height, int *dst_depth)
|
2005-07-07 22:32:42 +00:00
|
|
|
{
|
2006-02-08 13:05:37 +00:00
|
|
|
int row, col;
|
2006-02-16 20:03:07 +00:00
|
|
|
int dst_w, dst_h, dst_d;
|
2005-07-07 22:32:42 +00:00
|
|
|
|
|
|
|
switch (format)
|
|
|
|
{
|
2006-11-10 20:26:01 +00:00
|
|
|
case 0: /* Archos recorders, Ondio, Iriver H1x0 monochrome */
|
2005-07-07 22:32:42 +00:00
|
|
|
dst_w = width;
|
|
|
|
dst_h = (height + 7) / 8;
|
2006-02-16 20:03:07 +00:00
|
|
|
dst_d = 8;
|
2005-07-07 22:32:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* Archos player graphics library */
|
|
|
|
dst_w = (width + 7) / 8;
|
|
|
|
dst_h = height;
|
2006-02-16 20:03:07 +00:00
|
|
|
dst_d = 8;
|
2005-07-07 22:32:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* Iriver H1x0 4-grey */
|
|
|
|
dst_w = width;
|
|
|
|
dst_h = (height + 3) / 4;
|
2006-02-16 20:03:07 +00:00
|
|
|
dst_d = 8;
|
2005-07-07 22:32:42 +00:00
|
|
|
break;
|
|
|
|
|
2005-07-28 16:31:02 +00:00
|
|
|
case 3: /* Canonical 8-bit grayscale */
|
|
|
|
dst_w = width;
|
|
|
|
dst_h = height;
|
2006-02-16 20:03:07 +00:00
|
|
|
dst_d = 8;
|
2005-07-28 16:31:02 +00:00
|
|
|
break;
|
|
|
|
|
2005-11-14 14:47:23 +00:00
|
|
|
case 4: /* 16-bit packed RGB (5-6-5) */
|
2006-02-16 20:03:07 +00:00
|
|
|
case 5: /* 16-bit packed and byte-swapped RGB (5-6-5) */
|
2009-08-30 23:24:22 +00:00
|
|
|
case 8: /* 16-bit packed RGB (5-6-5) vertical stride*/
|
2005-11-14 14:47:23 +00:00
|
|
|
dst_w = width;
|
|
|
|
dst_h = height;
|
2006-02-16 20:03:07 +00:00
|
|
|
dst_d = 16;
|
2005-11-14 14:47:23 +00:00
|
|
|
break;
|
|
|
|
|
2006-02-16 20:03:07 +00:00
|
|
|
case 6: /* greyscale iPods 4-grey */
|
|
|
|
dst_w = (width + 3) / 4;
|
2005-11-15 02:20:05 +00:00
|
|
|
dst_h = height;
|
2006-02-16 20:03:07 +00:00
|
|
|
dst_d = 8;
|
2005-11-15 02:20:05 +00:00
|
|
|
break;
|
2007-01-17 19:54:35 +00:00
|
|
|
|
2006-07-28 07:35:45 +00:00
|
|
|
case 7: /* greyscale X5 remote 4-grey */
|
|
|
|
dst_w = width;
|
|
|
|
dst_h = (height + 7) / 8;
|
|
|
|
dst_d = 16;
|
|
|
|
break;
|
2005-11-15 02:20:05 +00:00
|
|
|
|
2005-07-07 22:32:42 +00:00
|
|
|
default: /* unknown */
|
|
|
|
debugf("error - Undefined destination format\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2007-01-17 19:54:35 +00:00
|
|
|
|
2005-11-30 09:06:43 +00:00
|
|
|
*dest = (unsigned short *)malloc(dst_w * dst_h * sizeof(short));
|
2005-07-07 22:32:42 +00:00
|
|
|
if (*dest == NULL)
|
|
|
|
{
|
|
|
|
debugf("error - Out of memory.\n");
|
|
|
|
return 2;
|
|
|
|
}
|
2005-11-30 09:02:59 +00:00
|
|
|
memset(*dest, 0, dst_w * dst_h * sizeof(short));
|
2005-07-07 22:32:42 +00:00
|
|
|
*dst_width = dst_w;
|
|
|
|
*dst_height = dst_h;
|
2006-02-16 20:03:07 +00:00
|
|
|
*dst_depth = dst_d;
|
2005-07-07 22:32:42 +00:00
|
|
|
|
|
|
|
switch (format)
|
|
|
|
{
|
2006-11-10 20:26:01 +00:00
|
|
|
case 0: /* Archos recorders, Ondio, Iriver H1x0 b&w */
|
2005-07-07 22:32:42 +00:00
|
|
|
for (row = 0; row < height; row++)
|
|
|
|
for (col = 0; col < width; col++)
|
|
|
|
{
|
2007-01-17 19:54:35 +00:00
|
|
|
(*dest)[(row/8) * dst_w + col] |=
|
2005-07-28 16:31:02 +00:00
|
|
|
(~brightness(src[row * width + col]) & 0x80) >> (~row & 7);
|
2005-07-07 22:32:42 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* Archos player graphics library */
|
|
|
|
for (row = 0; row < height; row++)
|
|
|
|
for (col = 0; col < width; col++)
|
|
|
|
{
|
2007-01-17 19:54:35 +00:00
|
|
|
(*dest)[row * dst_w + (col/8)] |=
|
2005-07-28 16:31:02 +00:00
|
|
|
(~brightness(src[row * width + col]) & 0x80) >> (col & 7);
|
2005-07-07 22:32:42 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* Iriver H1x0 4-grey */
|
|
|
|
for (row = 0; row < height; row++)
|
|
|
|
for (col = 0; col < width; col++)
|
|
|
|
{
|
|
|
|
(*dest)[(row/4) * dst_w + col] |=
|
2005-07-28 16:31:02 +00:00
|
|
|
(~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)[row * dst_w + col] = brightness(src[row * width + col]);
|
2005-07-07 22:32:42 +00:00
|
|
|
}
|
|
|
|
break;
|
2005-11-14 14:47:23 +00:00
|
|
|
|
|
|
|
case 4: /* 16-bit packed RGB (5-6-5) */
|
2005-11-15 02:20:05 +00:00
|
|
|
case 5: /* 16-bit packed and byte-swapped RGB (5-6-5) */
|
2005-11-14 14:47:23 +00:00
|
|
|
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)));
|
|
|
|
|
2005-11-15 02:20:05 +00:00
|
|
|
if (format == 4)
|
|
|
|
(*dest)[row * dst_w + col] = rgb;
|
|
|
|
else
|
|
|
|
(*dest)[row * dst_w + col] = ((rgb&0xff00)>>8)|((rgb&0x00ff)<<8);
|
2005-11-14 14:47:23 +00:00
|
|
|
}
|
|
|
|
break;
|
2007-01-17 19:54:35 +00:00
|
|
|
|
2006-02-16 20:03:07 +00:00
|
|
|
case 6: /* greyscale iPods 4-grey */
|
|
|
|
for (row = 0; row < height; row++)
|
|
|
|
for (col = 0; col < width; col++)
|
|
|
|
{
|
|
|
|
(*dest)[row * dst_w + (col/4)] |=
|
2006-04-04 00:55:16 +00:00
|
|
|
(~brightness(src[row * width + col]) & 0xC0) >> (2 * (col & 3));
|
2006-02-16 20:03:07 +00:00
|
|
|
}
|
|
|
|
break;
|
2007-01-17 19:54:35 +00:00
|
|
|
|
2006-07-28 07:35:45 +00:00
|
|
|
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;
|
2007-01-17 19:54:35 +00:00
|
|
|
|
2006-07-28 07:35:45 +00:00
|
|
|
data = (data | (data << 7)) & 0x0101;
|
|
|
|
(*dest)[(row/8) * dst_w + col] |= data << (row & 7);
|
|
|
|
}
|
|
|
|
break;
|
2009-08-30 23:24:22 +00:00
|
|
|
|
|
|
|
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)[col * dst_h + row] = rgb;
|
|
|
|
}
|
|
|
|
break;
|
2005-07-07 22:32:42 +00:00
|
|
|
}
|
2007-01-17 19:54:35 +00:00
|
|
|
|
2005-07-07 22:32:42 +00:00
|
|
|
return 0;
|
2002-08-16 14:02:48 +00:00
|
|
|
}
|
|
|
|
|
2005-07-07 22:32:42 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* generate_c_source()
|
|
|
|
*
|
|
|
|
* Outputs a C source code with the bitmap in an array, accompanied by
|
|
|
|
* some #define's
|
|
|
|
****************************************************************************/
|
|
|
|
|
2006-02-21 01:23:30 +00:00
|
|
|
void generate_c_source(char *id, char* header_dir, int width, int height,
|
2006-02-08 13:05:37 +00:00
|
|
|
const unsigned short *t_bitmap, int t_width,
|
2011-11-13 15:37:16 +00:00
|
|
|
int t_height, int t_depth, bool t_mono, bool create_bm)
|
2002-08-16 14:02:48 +00:00
|
|
|
{
|
2005-07-07 22:32:42 +00:00
|
|
|
FILE *f;
|
2006-02-21 01:23:30 +00:00
|
|
|
FILE *fh;
|
2006-02-08 13:05:37 +00:00
|
|
|
int i, a;
|
2006-02-21 01:23:30 +00:00
|
|
|
char header_name[1024];
|
2011-11-13 15:37:16 +00:00
|
|
|
bool have_header = header_dir && header_dir[0];
|
|
|
|
create_bm = have_header && create_bm;
|
2002-08-16 14:02:48 +00:00
|
|
|
|
2005-07-07 22:32:42 +00:00
|
|
|
if (!id || !id[0])
|
|
|
|
id = "bitmap";
|
2002-08-19 08:04:48 +00:00
|
|
|
|
2006-02-21 01:23:30 +00:00
|
|
|
f = stdout;
|
|
|
|
|
2011-11-13 15:37:16 +00:00
|
|
|
if (have_header)
|
2006-02-21 01:23:30 +00:00
|
|
|
{
|
|
|
|
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,
|
2008-04-10 21:16:00 +00:00
|
|
|
"#define BMPHEIGHT_%s %d\n"
|
|
|
|
"#define BMPWIDTH_%s %d\n",
|
2006-02-21 01:23:30 +00:00
|
|
|
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);
|
|
|
|
|
2011-11-13 15:37:16 +00:00
|
|
|
if (create_bm)
|
|
|
|
{
|
|
|
|
fprintf(f, "#include \"lcd.h\"\n");
|
|
|
|
fprintf(fh, "extern const struct bitmap bm_%s;\n", id);
|
|
|
|
}
|
2006-02-21 01:23:30 +00:00
|
|
|
fclose(fh);
|
|
|
|
} else {
|
|
|
|
fprintf(f,
|
2008-04-10 21:16:00 +00:00
|
|
|
"#define BMPHEIGHT_%s %d\n"
|
|
|
|
"#define BMPWIDTH_%s %d\n",
|
2006-02-21 01:23:30 +00:00
|
|
|
id, height, id, width);
|
|
|
|
}
|
|
|
|
|
2011-11-13 15:37:16 +00:00
|
|
|
if (create_bm) {
|
|
|
|
fprintf(f, "#include \"%s\"\n", header_name);
|
|
|
|
}
|
|
|
|
|
2006-02-16 20:03:07 +00:00
|
|
|
if (t_depth <= 8)
|
2005-11-14 14:47:23 +00:00
|
|
|
fprintf(f, "const unsigned char %s[] = {\n", id);
|
|
|
|
else
|
|
|
|
fprintf(f, "const unsigned short %s[] = {\n", id);
|
2002-08-16 14:02:48 +00:00
|
|
|
|
2005-07-07 22:32:42 +00:00
|
|
|
for (i = 0; i < t_height; i++)
|
|
|
|
{
|
|
|
|
for (a = 0; a < t_width; a++)
|
2005-11-14 14:47:23 +00:00
|
|
|
{
|
2006-02-16 20:03:07 +00:00
|
|
|
if (t_depth <= 8)
|
2005-11-14 14:47:23 +00:00
|
|
|
fprintf(f, "0x%02x,%c", t_bitmap[i * t_width + a],
|
|
|
|
(a + 1) % 13 ? ' ' : '\n');
|
|
|
|
else
|
|
|
|
fprintf(f, "0x%04x,%c", t_bitmap[i * t_width + a],
|
|
|
|
(a + 1) % 10 ? ' ' : '\n');
|
|
|
|
}
|
2005-07-07 22:32:42 +00:00
|
|
|
fprintf(f, "\n");
|
|
|
|
}
|
2002-08-16 14:02:48 +00:00
|
|
|
|
2011-11-13 15:37:16 +00:00
|
|
|
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);
|
|
|
|
}
|
2002-08-16 14:02:48 +00:00
|
|
|
}
|
|
|
|
|
2008-04-10 21:16:00 +00:00
|
|
|
void generate_raw_file(const unsigned short *t_bitmap,
|
2007-01-18 09:30:25 +00:00
|
|
|
int t_width, int t_height, int t_depth)
|
2007-01-17 18:30:04 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
int i, a;
|
2007-01-18 09:30:25 +00:00
|
|
|
unsigned char lo,hi;
|
2007-01-17 18:30:04 +00:00
|
|
|
|
|
|
|
f = stdout;
|
|
|
|
|
|
|
|
for (i = 0; i < t_height; i++)
|
|
|
|
{
|
|
|
|
for (a = 0; a < t_width; a++)
|
|
|
|
{
|
|
|
|
if (t_depth <= 8)
|
2007-01-18 09:30:25 +00:00
|
|
|
{
|
|
|
|
lo = (t_bitmap[i * t_width + a] & 0x00ff);
|
|
|
|
fwrite(&lo, 1, 1, f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lo = (t_bitmap[i * t_width + a] & 0x00ff);
|
|
|
|
hi = (t_bitmap[i * t_width + a] & 0xff00) >> 8;
|
|
|
|
fwrite(&lo, 1, 1, f);
|
|
|
|
fwrite(&hi, 1, 1, f);
|
|
|
|
}
|
2007-01-17 18:30:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-07 22:32:42 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* generate_ascii()
|
|
|
|
*
|
|
|
|
* Outputs an ascii picture of the bitmap
|
|
|
|
****************************************************************************/
|
2002-08-16 14:02:48 +00:00
|
|
|
|
2006-02-08 13:05:37 +00:00
|
|
|
void generate_ascii(int width, int height, struct RGBQUAD *bitmap)
|
2002-08-16 14:02:48 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
2006-02-08 13:05:37 +00:00
|
|
|
int x, y;
|
2002-08-16 14:02:48 +00:00
|
|
|
|
|
|
|
f = stdout;
|
|
|
|
|
|
|
|
/* for screen output debugging */
|
2005-07-07 22:32:42 +00:00
|
|
|
for (y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
2005-07-28 16:31:02 +00:00
|
|
|
fprintf(f, (brightness(bitmap[y * width + x]) & 0x80) ? " " : "*");
|
2002-08-16 14:02:48 +00:00
|
|
|
}
|
2005-07-07 22:32:42 +00:00
|
|
|
fprintf(f, "\n");
|
2002-08-16 14:02:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_usage(void)
|
|
|
|
{
|
2005-07-07 22:32:42 +00:00
|
|
|
printf("Usage: %s [-i <id>] [-a] <bitmap file>\n"
|
|
|
|
"\t-i <id> Bitmap name (default is filename without extension)\n"
|
2006-02-21 01:23:30 +00:00
|
|
|
"\t-h <dir> Create header file in <dir>/<id>.h\n"
|
2005-07-07 22:32:42 +00:00
|
|
|
"\t-a Show ascii picture of bitmap\n"
|
2011-11-13 15:37:16 +00:00
|
|
|
"\t-b Create bitmap struct along with pixel array\n"
|
2007-01-18 09:30:25 +00:00
|
|
|
"\t-r Generate RAW file (little-endian)\n"
|
2005-07-07 22:32:42 +00:00
|
|
|
"\t-f <n> Generate destination format n, default = 0\n"
|
2006-11-10 20:26:01 +00:00
|
|
|
"\t 0 Archos recorder, Ondio, Iriver H1x0 mono\n"
|
2008-04-10 21:16:00 +00:00
|
|
|
, APPLICATION_NAME);
|
|
|
|
printf("\t 1 Archos player graphics library\n"
|
2005-07-07 22:32:42 +00:00
|
|
|
"\t 2 Iriver H1x0 4-grey\n"
|
2006-07-28 07:35:45 +00:00
|
|
|
"\t 3 Canonical 8-bit greyscale\n"
|
2005-11-14 14:47:23 +00:00
|
|
|
"\t 4 16-bit packed 5-6-5 RGB (iriver H300)\n"
|
2010-04-05 07:10:01 +00:00
|
|
|
"\t 5 16-bit packed and byte-swapped 5-6-5 RGB (iPod, Fuzev2)\n"
|
2006-07-28 07:35:45 +00:00
|
|
|
"\t 6 Greyscale iPod 4-grey\n"
|
2009-08-30 23:24:22 +00:00
|
|
|
"\t 7 Greyscale X5 remote 4-grey\n"
|
|
|
|
"\t 8 16-bit packed 5-6-5 RGB with a vertical stride\n");
|
2005-07-07 22:32:42 +00:00
|
|
|
printf("build date: " __DATE__ "\n\n");
|
2002-08-16 14:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2005-07-07 22:32:42 +00:00
|
|
|
char *bmp_filename = NULL;
|
|
|
|
char *id = NULL;
|
2006-02-21 01:23:30 +00:00
|
|
|
char* header_dir = NULL;
|
2005-07-07 22:32:42 +00:00
|
|
|
int i;
|
|
|
|
int ascii = false;
|
|
|
|
int format = 0;
|
2005-07-28 16:31:02 +00:00
|
|
|
struct RGBQUAD *bitmap = NULL;
|
2005-11-14 14:47:23 +00:00
|
|
|
unsigned short *t_bitmap = NULL;
|
2006-02-08 13:05:37 +00:00
|
|
|
int width, height;
|
2006-02-16 20:03:07 +00:00
|
|
|
int t_width, t_height, t_depth;
|
2007-01-17 18:30:04 +00:00
|
|
|
bool raw = false;
|
2011-11-13 15:37:16 +00:00
|
|
|
bool create_bm = false;
|
2005-07-07 22:32:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
for (i = 1;i < argc;i++)
|
|
|
|
{
|
|
|
|
if (argv[i][0] == '-')
|
|
|
|
{
|
|
|
|
switch (argv[i][1])
|
|
|
|
{
|
2006-02-21 01:23:30 +00:00
|
|
|
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;
|
|
|
|
|
2005-07-07 22:32:42 +00:00
|
|
|
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;
|
2007-01-17 19:54:35 +00:00
|
|
|
|
2011-11-13 15:37:16 +00:00
|
|
|
case 'b':
|
|
|
|
create_bm = true;
|
|
|
|
break;
|
|
|
|
|
2007-01-17 19:54:35 +00:00
|
|
|
case 'r': /* Raw File */
|
2007-01-17 18:30:04 +00:00
|
|
|
raw = true;
|
|
|
|
break;
|
2007-01-17 19:54:35 +00:00
|
|
|
|
2005-07-07 22:32:42 +00:00
|
|
|
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);
|
2007-01-17 19:54:35 +00:00
|
|
|
|
2005-07-07 22:32:42 +00:00
|
|
|
|
|
|
|
if (ascii)
|
|
|
|
{
|
|
|
|
generate_ascii(width, height, bitmap);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-01-18 09:30:25 +00:00
|
|
|
if (transform_bitmap(bitmap, width, height, format, &t_bitmap,
|
|
|
|
&t_width, &t_height, &t_depth))
|
2005-07-07 22:32:42 +00:00
|
|
|
exit(1);
|
2007-01-17 18:30:04 +00:00
|
|
|
if(raw)
|
2008-04-10 21:16:00 +00:00
|
|
|
generate_raw_file(t_bitmap, t_width, t_height, t_depth);
|
2007-01-17 18:30:04 +00:00
|
|
|
else
|
2007-01-18 09:30:25 +00:00
|
|
|
generate_c_source(id, header_dir, width, height, t_bitmap,
|
2011-11-13 15:37:16 +00:00
|
|
|
t_width, t_height, t_depth,
|
|
|
|
format <= 1, create_bm);
|
2005-07-07 22:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2002-08-16 14:02:48 +00:00
|
|
|
}
|