Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2002 by Linus Nielsen Feltzing |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 21 | /**************************************************************************** |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 22 | * |
| 23 | * Converts BMP files to Rockbox bitmap format |
| 24 | * |
| 25 | * 1999-05-03 Linus Nielsen Feltzing |
| 26 | * |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 27 | * 2005-07-06 Jens Arnold |
| 28 | * added reading of 4, 16, 24 and 32 bit bmps |
| 29 | * added 2 new target formats (playergfx and iriver 4-grey) |
| 30 | * |
| 31 | ****************************************************************************/ |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 32 | |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <stdbool.h> |
Daniel Stenberg | afecfe4 | 2008-04-10 21:16:00 +0000 | [diff] [blame] | 37 | #include <unistd.h> |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 38 | |
| 39 | #include <sys/types.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <fcntl.h> |
| 42 | |
| 43 | #define debugf printf |
| 44 | |
| 45 | #ifdef __GNUC__ |
| 46 | #define STRUCT_PACKED __attribute__((packed)) |
| 47 | #else |
| 48 | #define STRUCT_PACKED |
| 49 | #pragma pack (push, 2) |
| 50 | #endif |
| 51 | |
| 52 | struct Fileheader |
| 53 | { |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 54 | unsigned short Type; /* signature - 'BM' */ |
Jens Arnold | 41e1aa8 | 2006-02-16 20:03:07 +0000 | [diff] [blame] | 55 | unsigned int Size; /* file size in bytes */ |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 56 | unsigned short Reserved1; /* 0 */ |
| 57 | unsigned short Reserved2; /* 0 */ |
Daniel Stenberg | dcc9a8a | 2006-02-08 13:05:37 +0000 | [diff] [blame] | 58 | unsigned int OffBits; /* offset to bitmap */ |
| 59 | unsigned int StructSize; /* size of this struct (40) */ |
| 60 | unsigned int Width; /* bmap width in pixels */ |
| 61 | unsigned int Height; /* bmap height in pixels */ |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 62 | unsigned short Planes; /* num planes - always 1 */ |
| 63 | unsigned short BitCount; /* bits per pixel */ |
Daniel Stenberg | dcc9a8a | 2006-02-08 13:05:37 +0000 | [diff] [blame] | 64 | unsigned int Compression; /* compression flag */ |
| 65 | unsigned int SizeImage; /* image size in bytes */ |
| 66 | int XPelsPerMeter; /* horz resolution */ |
| 67 | int YPelsPerMeter; /* vert resolution */ |
| 68 | unsigned int ClrUsed; /* 0 -> color table size */ |
| 69 | unsigned int ClrImportant; /* important color count */ |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 70 | } STRUCT_PACKED; |
| 71 | |
| 72 | struct RGBQUAD |
| 73 | { |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 74 | unsigned char rgbBlue; |
| 75 | unsigned char rgbGreen; |
| 76 | unsigned char rgbRed; |
| 77 | unsigned char rgbReserved; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 78 | } STRUCT_PACKED; |
| 79 | |
Daniel Stenberg | 2d81cf9 | 2003-05-21 06:31:44 +0000 | [diff] [blame] | 80 | short readshort(void* value) |
| 81 | { |
| 82 | unsigned char* bytes = (unsigned char*) value; |
| 83 | return bytes[0] | (bytes[1] << 8); |
| 84 | } |
| 85 | |
Daniel Stenberg | dcc9a8a | 2006-02-08 13:05:37 +0000 | [diff] [blame] | 86 | int readint(void* value) |
Daniel Stenberg | 2d81cf9 | 2003-05-21 06:31:44 +0000 | [diff] [blame] | 87 | { |
| 88 | unsigned char* bytes = (unsigned char*) value; |
| 89 | return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24); |
| 90 | } |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 91 | |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 92 | unsigned char brightness(struct RGBQUAD color) |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 93 | { |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 94 | return (3 * (unsigned int)color.rgbRed + 6 * (unsigned int)color.rgbGreen |
| 95 | + (unsigned int)color.rgbBlue) / 10; |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Daniel Stenberg | 635b830 | 2006-02-28 07:36:38 +0000 | [diff] [blame] | 98 | #ifndef O_BINARY |
| 99 | #define O_BINARY 0 /* systems that don't have O_BINARY won't make a difference |
| 100 | on text and binary files */ |
| 101 | #endif |
| 102 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 103 | /**************************************************************************** |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 104 | * read_bmp_file() |
| 105 | * |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 106 | * Reads an uncompressed BMP file and puts the data in a 4-byte-per-pixel |
| 107 | * (RGBQUAD) array. Returns 0 on success. |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 108 | * |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 109 | ***************************************************************************/ |
| 110 | |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 111 | int read_bmp_file(char* filename, |
Daniel Stenberg | dcc9a8a | 2006-02-08 13:05:37 +0000 | [diff] [blame] | 112 | int *get_width, /* in pixels */ |
| 113 | int *get_height, /* in pixels */ |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 114 | struct RGBQUAD **bitmap) |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 115 | { |
Daniel Stenberg | 793ca18 | 2002-08-21 12:11:51 +0000 | [diff] [blame] | 116 | struct Fileheader fh; |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 117 | struct RGBQUAD palette[256]; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 118 | |
Daniel Stenberg | 635b830 | 2006-02-28 07:36:38 +0000 | [diff] [blame] | 119 | int fd = open(filename, O_RDONLY| O_BINARY); |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 120 | unsigned short data; |
Daniel Stenberg | 793ca18 | 2002-08-21 12:11:51 +0000 | [diff] [blame] | 121 | unsigned char *bmp; |
Daniel Stenberg | dcc9a8a | 2006-02-08 13:05:37 +0000 | [diff] [blame] | 122 | int width, height; |
| 123 | int padded_width; |
| 124 | int size; |
| 125 | int row, col, i; |
| 126 | int numcolors, compression; |
Daniel Stenberg | 793ca18 | 2002-08-21 12:11:51 +0000 | [diff] [blame] | 127 | int depth; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 128 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 129 | if (fd == -1) |
Daniel Stenberg | 793ca18 | 2002-08-21 12:11:51 +0000 | [diff] [blame] | 130 | { |
| 131 | debugf("error - can't open '%s'\n", filename); |
| 132 | return 1; |
| 133 | } |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 134 | if (read(fd, &fh, sizeof(struct Fileheader)) != |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 135 | sizeof(struct Fileheader)) |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 136 | { |
| 137 | debugf("error - can't Read Fileheader Stucture\n"); |
| 138 | close(fd); |
| 139 | return 2; |
| 140 | } |
Greg White | 1ab8f32 | 2007-01-17 19:54:35 +0000 | [diff] [blame] | 141 | |
Daniel Stenberg | dcc9a8a | 2006-02-08 13:05:37 +0000 | [diff] [blame] | 142 | compression = readint(&fh.Compression); |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 143 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 144 | if (compression != 0) |
| 145 | { |
Daniel Stenberg | afecfe4 | 2008-04-10 21:16:00 +0000 | [diff] [blame] | 146 | debugf("error - Unsupported compression %d\n", compression); |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 147 | close(fd); |
| 148 | return 3; |
| 149 | } |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 150 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 151 | depth = readshort(&fh.BitCount); |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 152 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 153 | if (depth <= 8) |
| 154 | { |
Daniel Stenberg | dcc9a8a | 2006-02-08 13:05:37 +0000 | [diff] [blame] | 155 | numcolors = readint(&fh.ClrUsed); |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 156 | if (numcolors == 0) |
| 157 | numcolors = 1 << depth; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 158 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 159 | if (read(fd, &palette[0], numcolors * sizeof(struct RGBQUAD)) |
Daniel Stenberg | afecfe4 | 2008-04-10 21:16:00 +0000 | [diff] [blame] | 160 | != (int)(numcolors * sizeof(struct RGBQUAD))) |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 161 | { |
| 162 | debugf("error - Can't read bitmap's color palette\n"); |
| 163 | close(fd); |
| 164 | return 4; |
| 165 | } |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 166 | } |
Daniel Stenberg | 793ca18 | 2002-08-21 12:11:51 +0000 | [diff] [blame] | 167 | |
Daniel Stenberg | dcc9a8a | 2006-02-08 13:05:37 +0000 | [diff] [blame] | 168 | width = readint(&fh.Width); |
| 169 | height = readint(&fh.Height); |
Jens Arnold | 9b1c9db | 2006-02-27 21:59:48 +0000 | [diff] [blame] | 170 | padded_width = ((width * depth + 31) / 8) & ~3; /* aligned 4-bytes boundaries */ |
Daniel Stenberg | 793ca18 | 2002-08-21 12:11:51 +0000 | [diff] [blame] | 171 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 172 | size = padded_width * height; /* read this many bytes */ |
| 173 | bmp = (unsigned char *)malloc(size); |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 174 | *bitmap = (struct RGBQUAD *)malloc(width * height * sizeof(struct RGBQUAD)); |
Daniel Stenberg | 463e329 | 2004-12-20 09:09:49 +0000 | [diff] [blame] | 175 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 176 | if ((bmp == NULL) || (*bitmap == NULL)) |
| 177 | { |
| 178 | debugf("error - Out of memory\n"); |
| 179 | close(fd); |
| 180 | return 5; |
| 181 | } |
Daniel Stenberg | 463e329 | 2004-12-20 09:09:49 +0000 | [diff] [blame] | 182 | |
Daniel Stenberg | dcc9a8a | 2006-02-08 13:05:37 +0000 | [diff] [blame] | 183 | if (lseek(fd, (off_t)readint(&fh.OffBits), SEEK_SET) < 0) |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 184 | { |
| 185 | debugf("error - Can't seek to start of image data\n"); |
| 186 | close(fd); |
| 187 | return 6; |
| 188 | } |
Daniel Stenberg | dcc9a8a | 2006-02-08 13:05:37 +0000 | [diff] [blame] | 189 | if (read(fd, (unsigned char*)bmp, (int)size) != size) |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 190 | { |
| 191 | debugf("error - Can't read image\n"); |
| 192 | close(fd); |
| 193 | return 7; |
| 194 | } |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 195 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 196 | close(fd); |
| 197 | *get_width = width; |
| 198 | *get_height = height; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 199 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 200 | switch (depth) |
| 201 | { |
| 202 | case 1: |
| 203 | for (row = 0; row < height; row++) |
| 204 | for (col = 0; col < width; col++) |
| 205 | { |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 206 | data = (bmp[(height - 1 - row) * padded_width + col / 8] |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 207 | >> (~col & 7)) & 1; |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 208 | (*bitmap)[row * width + col] = palette[data]; |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 209 | } |
| 210 | break; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 211 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 212 | case 4: |
| 213 | for (row = 0; row < height; row++) |
| 214 | for (col = 0; col < width; col++) |
| 215 | { |
Greg White | 1ab8f32 | 2007-01-17 19:54:35 +0000 | [diff] [blame] | 216 | data = (bmp[(height - 1 - row) * padded_width + col / 2] |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 217 | >> (4 * (~col & 1))) & 0x0F; |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 218 | (*bitmap)[row * width + col] = palette[data]; |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 219 | } |
| 220 | break; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 221 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 222 | case 8: |
| 223 | for (row = 0; row < height; row++) |
| 224 | for (col = 0; col < width; col++) |
| 225 | { |
| 226 | data = bmp[(height - 1 - row) * padded_width + col]; |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 227 | (*bitmap)[row * width + col] = palette[data]; |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 228 | } |
| 229 | break; |
Greg White | 1ab8f32 | 2007-01-17 19:54:35 +0000 | [diff] [blame] | 230 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 231 | case 16: |
| 232 | for (row = 0; row < height; row++) |
| 233 | for (col = 0; col < width; col++) |
| 234 | { |
| 235 | data = readshort(&bmp[(height - 1 - row) * padded_width + 2 * col]); |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 236 | (*bitmap)[row * width + col].rgbRed = |
| 237 | ((data >> 7) & 0xF8) | ((data >> 12) & 0x07); |
| 238 | (*bitmap)[row * width + col].rgbGreen = |
| 239 | ((data >> 2) & 0xF8) | ((data >> 7) & 0x07); |
| 240 | (*bitmap)[row * width + col].rgbBlue = |
| 241 | ((data << 3) & 0xF8) | ((data >> 2) & 0x07); |
| 242 | (*bitmap)[row * width + col].rgbReserved = 0; |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 243 | } |
| 244 | break; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 245 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 246 | case 24: |
| 247 | for (row = 0; row < height; row++) |
| 248 | for (col = 0; col < width; col++) |
| 249 | { |
| 250 | i = (height - 1 - row) * padded_width + 3 * col; |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 251 | (*bitmap)[row * width + col].rgbRed = bmp[i+2]; |
| 252 | (*bitmap)[row * width + col].rgbGreen = bmp[i+1]; |
| 253 | (*bitmap)[row * width + col].rgbBlue = bmp[i]; |
| 254 | (*bitmap)[row * width + col].rgbReserved = 0; |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 255 | } |
| 256 | break; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 257 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 258 | case 32: |
| 259 | for (row = 0; row < height; row++) |
| 260 | for (col = 0; col < width; col++) |
Greg White | 1ab8f32 | 2007-01-17 19:54:35 +0000 | [diff] [blame] | 261 | { |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 262 | i = (height - 1 - row) * padded_width + 4 * col; |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 263 | (*bitmap)[row * width + col].rgbRed = bmp[i+2]; |
| 264 | (*bitmap)[row * width + col].rgbGreen = bmp[i+1]; |
| 265 | (*bitmap)[row * width + col].rgbBlue = bmp[i]; |
| 266 | (*bitmap)[row * width + col].rgbReserved = 0; |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 267 | } |
| 268 | break; |
| 269 | |
| 270 | default: /* should never happen */ |
| 271 | debugf("error - Unsupported bitmap depth %d.\n", depth); |
| 272 | return 8; |
| 273 | } |
| 274 | |
| 275 | free(bmp); |
Greg White | 1ab8f32 | 2007-01-17 19:54:35 +0000 | [diff] [blame] | 276 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 277 | return 0; /* success */ |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 280 | /**************************************************************************** |
| 281 | * transform_bitmap() |
| 282 | * |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 283 | * Transform a 4-byte-per-pixel bitmap (RGBQUAD) into one of the supported |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 284 | * destination formats |
| 285 | ****************************************************************************/ |
| 286 | |
Daniel Stenberg | dcc9a8a | 2006-02-08 13:05:37 +0000 | [diff] [blame] | 287 | int transform_bitmap(const struct RGBQUAD *src, int width, int height, |
| 288 | int format, unsigned short **dest, int *dst_width, |
Jens Arnold | 41e1aa8 | 2006-02-16 20:03:07 +0000 | [diff] [blame] | 289 | int *dst_height, int *dst_depth) |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 290 | { |
Daniel Stenberg | dcc9a8a | 2006-02-08 13:05:37 +0000 | [diff] [blame] | 291 | int row, col; |
Jens Arnold | 41e1aa8 | 2006-02-16 20:03:07 +0000 | [diff] [blame] | 292 | int dst_w, dst_h, dst_d; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 293 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 294 | switch (format) |
| 295 | { |
Jens Arnold | 780f79e | 2006-11-10 20:26:01 +0000 | [diff] [blame] | 296 | case 0: /* Archos recorders, Ondio, Iriver H1x0 monochrome */ |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 297 | dst_w = width; |
| 298 | dst_h = (height + 7) / 8; |
Jens Arnold | 41e1aa8 | 2006-02-16 20:03:07 +0000 | [diff] [blame] | 299 | dst_d = 8; |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 300 | break; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 301 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 302 | case 1: /* Archos player graphics library */ |
| 303 | dst_w = (width + 7) / 8; |
| 304 | dst_h = height; |
Jens Arnold | 41e1aa8 | 2006-02-16 20:03:07 +0000 | [diff] [blame] | 305 | dst_d = 8; |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 306 | break; |
Daniel Stenberg | 8c78975 | 2002-08-19 08:04:48 +0000 | [diff] [blame] | 307 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 308 | case 2: /* Iriver H1x0 4-grey */ |
| 309 | dst_w = width; |
| 310 | dst_h = (height + 3) / 4; |
Jens Arnold | 41e1aa8 | 2006-02-16 20:03:07 +0000 | [diff] [blame] | 311 | dst_d = 8; |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 312 | break; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 313 | |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 314 | case 3: /* Canonical 8-bit grayscale */ |
| 315 | dst_w = width; |
| 316 | dst_h = height; |
Jens Arnold | 41e1aa8 | 2006-02-16 20:03:07 +0000 | [diff] [blame] | 317 | dst_d = 8; |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 318 | break; |
| 319 | |
Linus Nielsen Feltzing | 057c8ec | 2005-11-14 14:47:23 +0000 | [diff] [blame] | 320 | case 4: /* 16-bit packed RGB (5-6-5) */ |
Dave Chapman | 4202df0 | 2005-11-15 02:20:05 +0000 | [diff] [blame] | 321 | case 5: /* 16-bit packed and byte-swapped RGB (5-6-5) */ |
| 322 | dst_w = width; |
| 323 | dst_h = height; |
Jens Arnold | 41e1aa8 | 2006-02-16 20:03:07 +0000 | [diff] [blame] | 324 | dst_d = 16; |
| 325 | break; |
| 326 | |
| 327 | case 6: /* greyscale iPods 4-grey */ |
| 328 | dst_w = (width + 3) / 4; |
| 329 | dst_h = height; |
| 330 | dst_d = 8; |
Dave Chapman | 4202df0 | 2005-11-15 02:20:05 +0000 | [diff] [blame] | 331 | break; |
Greg White | 1ab8f32 | 2007-01-17 19:54:35 +0000 | [diff] [blame] | 332 | |
Jens Arnold | 05ddd9a | 2006-07-28 07:35:45 +0000 | [diff] [blame] | 333 | case 7: /* greyscale X5 remote 4-grey */ |
| 334 | dst_w = width; |
| 335 | dst_h = (height + 7) / 8; |
| 336 | dst_d = 16; |
| 337 | break; |
Dave Chapman | 4202df0 | 2005-11-15 02:20:05 +0000 | [diff] [blame] | 338 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 339 | default: /* unknown */ |
| 340 | debugf("error - Undefined destination format\n"); |
| 341 | return 1; |
| 342 | } |
Greg White | 1ab8f32 | 2007-01-17 19:54:35 +0000 | [diff] [blame] | 343 | |
Linus Nielsen Feltzing | 36cca23 | 2005-11-30 09:06:43 +0000 | [diff] [blame] | 344 | *dest = (unsigned short *)malloc(dst_w * dst_h * sizeof(short)); |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 345 | if (*dest == NULL) |
| 346 | { |
| 347 | debugf("error - Out of memory.\n"); |
| 348 | return 2; |
| 349 | } |
Linus Nielsen Feltzing | 6e46156 | 2005-11-30 09:02:59 +0000 | [diff] [blame] | 350 | memset(*dest, 0, dst_w * dst_h * sizeof(short)); |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 351 | *dst_width = dst_w; |
| 352 | *dst_height = dst_h; |
Jens Arnold | 41e1aa8 | 2006-02-16 20:03:07 +0000 | [diff] [blame] | 353 | *dst_depth = dst_d; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 354 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 355 | switch (format) |
| 356 | { |
Jens Arnold | 780f79e | 2006-11-10 20:26:01 +0000 | [diff] [blame] | 357 | case 0: /* Archos recorders, Ondio, Iriver H1x0 b&w */ |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 358 | for (row = 0; row < height; row++) |
| 359 | for (col = 0; col < width; col++) |
| 360 | { |
Greg White | 1ab8f32 | 2007-01-17 19:54:35 +0000 | [diff] [blame] | 361 | (*dest)[(row/8) * dst_w + col] |= |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 362 | (~brightness(src[row * width + col]) & 0x80) >> (~row & 7); |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 363 | } |
| 364 | break; |
| 365 | |
| 366 | case 1: /* Archos player graphics library */ |
| 367 | for (row = 0; row < height; row++) |
| 368 | for (col = 0; col < width; col++) |
| 369 | { |
Greg White | 1ab8f32 | 2007-01-17 19:54:35 +0000 | [diff] [blame] | 370 | (*dest)[row * dst_w + (col/8)] |= |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 371 | (~brightness(src[row * width + col]) & 0x80) >> (col & 7); |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 372 | } |
| 373 | break; |
| 374 | |
| 375 | case 2: /* Iriver H1x0 4-grey */ |
| 376 | for (row = 0; row < height; row++) |
| 377 | for (col = 0; col < width; col++) |
| 378 | { |
| 379 | (*dest)[(row/4) * dst_w + col] |= |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 380 | (~brightness(src[row * width + col]) & 0xC0) >> (2 * (~row & 3)); |
| 381 | } |
| 382 | break; |
| 383 | |
| 384 | case 3: /* Canonical 8-bit grayscale */ |
| 385 | for (row = 0; row < height; row++) |
| 386 | for (col = 0; col < width; col++) |
| 387 | { |
| 388 | (*dest)[row * dst_w + col] = brightness(src[row * width + col]); |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 389 | } |
| 390 | break; |
Linus Nielsen Feltzing | 057c8ec | 2005-11-14 14:47:23 +0000 | [diff] [blame] | 391 | |
| 392 | case 4: /* 16-bit packed RGB (5-6-5) */ |
Dave Chapman | 4202df0 | 2005-11-15 02:20:05 +0000 | [diff] [blame] | 393 | case 5: /* 16-bit packed and byte-swapped RGB (5-6-5) */ |
Linus Nielsen Feltzing | 057c8ec | 2005-11-14 14:47:23 +0000 | [diff] [blame] | 394 | for (row = 0; row < height; row++) |
| 395 | for (col = 0; col < width; col++) |
| 396 | { |
| 397 | unsigned short rgb = |
| 398 | (((src[row * width + col].rgbRed >> 3) << 11) | |
| 399 | ((src[row * width + col].rgbGreen >> 2) << 5) | |
| 400 | ((src[row * width + col].rgbBlue >> 3))); |
| 401 | |
Dave Chapman | 4202df0 | 2005-11-15 02:20:05 +0000 | [diff] [blame] | 402 | if (format == 4) |
| 403 | (*dest)[row * dst_w + col] = rgb; |
| 404 | else |
| 405 | (*dest)[row * dst_w + col] = ((rgb&0xff00)>>8)|((rgb&0x00ff)<<8); |
Linus Nielsen Feltzing | 057c8ec | 2005-11-14 14:47:23 +0000 | [diff] [blame] | 406 | } |
| 407 | break; |
Greg White | 1ab8f32 | 2007-01-17 19:54:35 +0000 | [diff] [blame] | 408 | |
Jens Arnold | 41e1aa8 | 2006-02-16 20:03:07 +0000 | [diff] [blame] | 409 | case 6: /* greyscale iPods 4-grey */ |
| 410 | for (row = 0; row < height; row++) |
| 411 | for (col = 0; col < width; col++) |
| 412 | { |
| 413 | (*dest)[row * dst_w + (col/4)] |= |
Jens Arnold | fcab617 | 2006-04-04 00:55:16 +0000 | [diff] [blame] | 414 | (~brightness(src[row * width + col]) & 0xC0) >> (2 * (col & 3)); |
Jens Arnold | 41e1aa8 | 2006-02-16 20:03:07 +0000 | [diff] [blame] | 415 | } |
| 416 | break; |
Greg White | 1ab8f32 | 2007-01-17 19:54:35 +0000 | [diff] [blame] | 417 | |
Jens Arnold | 05ddd9a | 2006-07-28 07:35:45 +0000 | [diff] [blame] | 418 | case 7: /* greyscale X5 remote 4-grey */ |
| 419 | for (row = 0; row < height; row++) |
| 420 | for (col = 0; col < width; col++) |
| 421 | { |
| 422 | unsigned short data = (~brightness(src[row * width + col]) & 0xC0) >> 6; |
Greg White | 1ab8f32 | 2007-01-17 19:54:35 +0000 | [diff] [blame] | 423 | |
Jens Arnold | 05ddd9a | 2006-07-28 07:35:45 +0000 | [diff] [blame] | 424 | data = (data | (data << 7)) & 0x0101; |
| 425 | (*dest)[(row/8) * dst_w + col] |= data << (row & 7); |
| 426 | } |
| 427 | break; |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 428 | } |
Greg White | 1ab8f32 | 2007-01-17 19:54:35 +0000 | [diff] [blame] | 429 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 430 | return 0; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 433 | /**************************************************************************** |
| 434 | * generate_c_source() |
| 435 | * |
| 436 | * Outputs a C source code with the bitmap in an array, accompanied by |
| 437 | * some #define's |
| 438 | ****************************************************************************/ |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 439 | |
Dave Chapman | 95538f6 | 2006-02-21 01:23:30 +0000 | [diff] [blame] | 440 | void generate_c_source(char *id, char* header_dir, int width, int height, |
Daniel Stenberg | dcc9a8a | 2006-02-08 13:05:37 +0000 | [diff] [blame] | 441 | const unsigned short *t_bitmap, int t_width, |
Jens Arnold | 41e1aa8 | 2006-02-16 20:03:07 +0000 | [diff] [blame] | 442 | int t_height, int t_depth) |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 443 | { |
| 444 | FILE *f; |
Dave Chapman | 95538f6 | 2006-02-21 01:23:30 +0000 | [diff] [blame] | 445 | FILE *fh; |
Daniel Stenberg | dcc9a8a | 2006-02-08 13:05:37 +0000 | [diff] [blame] | 446 | int i, a; |
Dave Chapman | 95538f6 | 2006-02-21 01:23:30 +0000 | [diff] [blame] | 447 | char header_name[1024]; |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 448 | |
| 449 | if (!id || !id[0]) |
| 450 | id = "bitmap"; |
| 451 | |
Dave Chapman | 95538f6 | 2006-02-21 01:23:30 +0000 | [diff] [blame] | 452 | f = stdout; |
| 453 | |
| 454 | if (header_dir && header_dir[0]) |
| 455 | { |
| 456 | snprintf(header_name,sizeof(header_name),"%s/%s.h",header_dir,id); |
| 457 | fh = fopen(header_name,"w+"); |
| 458 | |
| 459 | if (fh == NULL) |
| 460 | { |
| 461 | debugf("error - can't open '%s'\n", header_name); |
| 462 | return; |
| 463 | } |
| 464 | fprintf(fh, |
Daniel Stenberg | afecfe4 | 2008-04-10 21:16:00 +0000 | [diff] [blame] | 465 | "#define BMPHEIGHT_%s %d\n" |
| 466 | "#define BMPWIDTH_%s %d\n", |
Dave Chapman | 95538f6 | 2006-02-21 01:23:30 +0000 | [diff] [blame] | 467 | id, height, id, width); |
| 468 | if (t_depth <= 8) |
| 469 | fprintf(fh, "extern const unsigned char %s[];\n", id); |
| 470 | else |
| 471 | fprintf(fh, "extern const unsigned short %s[];\n", id); |
| 472 | |
| 473 | fclose(fh); |
| 474 | } else { |
| 475 | fprintf(f, |
Daniel Stenberg | afecfe4 | 2008-04-10 21:16:00 +0000 | [diff] [blame] | 476 | "#define BMPHEIGHT_%s %d\n" |
| 477 | "#define BMPWIDTH_%s %d\n", |
Dave Chapman | 95538f6 | 2006-02-21 01:23:30 +0000 | [diff] [blame] | 478 | id, height, id, width); |
| 479 | } |
| 480 | |
Jens Arnold | 41e1aa8 | 2006-02-16 20:03:07 +0000 | [diff] [blame] | 481 | if (t_depth <= 8) |
Linus Nielsen Feltzing | 057c8ec | 2005-11-14 14:47:23 +0000 | [diff] [blame] | 482 | fprintf(f, "const unsigned char %s[] = {\n", id); |
| 483 | else |
| 484 | fprintf(f, "const unsigned short %s[] = {\n", id); |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 485 | |
| 486 | for (i = 0; i < t_height; i++) |
| 487 | { |
| 488 | for (a = 0; a < t_width; a++) |
Linus Nielsen Feltzing | 057c8ec | 2005-11-14 14:47:23 +0000 | [diff] [blame] | 489 | { |
Jens Arnold | 41e1aa8 | 2006-02-16 20:03:07 +0000 | [diff] [blame] | 490 | if (t_depth <= 8) |
Linus Nielsen Feltzing | 057c8ec | 2005-11-14 14:47:23 +0000 | [diff] [blame] | 491 | fprintf(f, "0x%02x,%c", t_bitmap[i * t_width + a], |
| 492 | (a + 1) % 13 ? ' ' : '\n'); |
| 493 | else |
| 494 | fprintf(f, "0x%04x,%c", t_bitmap[i * t_width + a], |
| 495 | (a + 1) % 10 ? ' ' : '\n'); |
| 496 | } |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 497 | fprintf(f, "\n"); |
| 498 | } |
| 499 | |
| 500 | fprintf(f, "\n};\n"); |
| 501 | } |
| 502 | |
Daniel Stenberg | afecfe4 | 2008-04-10 21:16:00 +0000 | [diff] [blame] | 503 | void generate_raw_file(const unsigned short *t_bitmap, |
Dave Chapman | 024537d | 2007-01-18 09:30:25 +0000 | [diff] [blame] | 504 | int t_width, int t_height, int t_depth) |
Greg White | b3c66f5 | 2007-01-17 18:30:04 +0000 | [diff] [blame] | 505 | { |
| 506 | FILE *f; |
| 507 | int i, a; |
Dave Chapman | 024537d | 2007-01-18 09:30:25 +0000 | [diff] [blame] | 508 | unsigned char lo,hi; |
Greg White | b3c66f5 | 2007-01-17 18:30:04 +0000 | [diff] [blame] | 509 | |
| 510 | f = stdout; |
| 511 | |
| 512 | for (i = 0; i < t_height; i++) |
| 513 | { |
| 514 | for (a = 0; a < t_width; a++) |
| 515 | { |
| 516 | if (t_depth <= 8) |
Dave Chapman | 024537d | 2007-01-18 09:30:25 +0000 | [diff] [blame] | 517 | { |
| 518 | lo = (t_bitmap[i * t_width + a] & 0x00ff); |
| 519 | fwrite(&lo, 1, 1, f); |
| 520 | } |
| 521 | else |
| 522 | { |
| 523 | lo = (t_bitmap[i * t_width + a] & 0x00ff); |
| 524 | hi = (t_bitmap[i * t_width + a] & 0xff00) >> 8; |
| 525 | fwrite(&lo, 1, 1, f); |
| 526 | fwrite(&hi, 1, 1, f); |
| 527 | } |
Greg White | b3c66f5 | 2007-01-17 18:30:04 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 532 | /**************************************************************************** |
| 533 | * generate_ascii() |
| 534 | * |
| 535 | * Outputs an ascii picture of the bitmap |
| 536 | ****************************************************************************/ |
| 537 | |
Daniel Stenberg | dcc9a8a | 2006-02-08 13:05:37 +0000 | [diff] [blame] | 538 | void generate_ascii(int width, int height, struct RGBQUAD *bitmap) |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 539 | { |
| 540 | FILE *f; |
Daniel Stenberg | dcc9a8a | 2006-02-08 13:05:37 +0000 | [diff] [blame] | 541 | int x, y; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 542 | |
| 543 | f = stdout; |
| 544 | |
| 545 | /* for screen output debugging */ |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 546 | for (y = 0; y < height; y++) |
| 547 | { |
| 548 | for (x = 0; x < width; x++) |
| 549 | { |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 550 | fprintf(f, (brightness(bitmap[y * width + x]) & 0x80) ? " " : "*"); |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 551 | } |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 552 | fprintf(f, "\n"); |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
| 556 | void print_usage(void) |
| 557 | { |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 558 | printf("Usage: %s [-i <id>] [-a] <bitmap file>\n" |
| 559 | "\t-i <id> Bitmap name (default is filename without extension)\n" |
Dave Chapman | 95538f6 | 2006-02-21 01:23:30 +0000 | [diff] [blame] | 560 | "\t-h <dir> Create header file in <dir>/<id>.h\n" |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 561 | "\t-a Show ascii picture of bitmap\n" |
Dave Chapman | 024537d | 2007-01-18 09:30:25 +0000 | [diff] [blame] | 562 | "\t-r Generate RAW file (little-endian)\n" |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 563 | "\t-f <n> Generate destination format n, default = 0\n" |
Jens Arnold | 780f79e | 2006-11-10 20:26:01 +0000 | [diff] [blame] | 564 | "\t 0 Archos recorder, Ondio, Iriver H1x0 mono\n" |
Daniel Stenberg | afecfe4 | 2008-04-10 21:16:00 +0000 | [diff] [blame] | 565 | , APPLICATION_NAME); |
| 566 | printf("\t 1 Archos player graphics library\n" |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 567 | "\t 2 Iriver H1x0 4-grey\n" |
Jens Arnold | 05ddd9a | 2006-07-28 07:35:45 +0000 | [diff] [blame] | 568 | "\t 3 Canonical 8-bit greyscale\n" |
Linus Nielsen Feltzing | 057c8ec | 2005-11-14 14:47:23 +0000 | [diff] [blame] | 569 | "\t 4 16-bit packed 5-6-5 RGB (iriver H300)\n" |
Dave Chapman | 4202df0 | 2005-11-15 02:20:05 +0000 | [diff] [blame] | 570 | "\t 5 16-bit packed and byte-swapped 5-6-5 RGB (iPod)\n" |
Jens Arnold | 05ddd9a | 2006-07-28 07:35:45 +0000 | [diff] [blame] | 571 | "\t 6 Greyscale iPod 4-grey\n" |
Daniel Stenberg | afecfe4 | 2008-04-10 21:16:00 +0000 | [diff] [blame] | 572 | "\t 7 Greyscale X5 remote 4-grey\n"); |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 573 | printf("build date: " __DATE__ "\n\n"); |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | int main(int argc, char **argv) |
| 577 | { |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 578 | char *bmp_filename = NULL; |
| 579 | char *id = NULL; |
Dave Chapman | 95538f6 | 2006-02-21 01:23:30 +0000 | [diff] [blame] | 580 | char* header_dir = NULL; |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 581 | int i; |
| 582 | int ascii = false; |
| 583 | int format = 0; |
Jens Arnold | 6ae8104 | 2005-07-28 16:31:02 +0000 | [diff] [blame] | 584 | struct RGBQUAD *bitmap = NULL; |
Linus Nielsen Feltzing | 057c8ec | 2005-11-14 14:47:23 +0000 | [diff] [blame] | 585 | unsigned short *t_bitmap = NULL; |
Daniel Stenberg | dcc9a8a | 2006-02-08 13:05:37 +0000 | [diff] [blame] | 586 | int width, height; |
Jens Arnold | 41e1aa8 | 2006-02-16 20:03:07 +0000 | [diff] [blame] | 587 | int t_width, t_height, t_depth; |
Greg White | b3c66f5 | 2007-01-17 18:30:04 +0000 | [diff] [blame] | 588 | bool raw = false; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 589 | |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 590 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 591 | for (i = 1;i < argc;i++) |
| 592 | { |
| 593 | if (argv[i][0] == '-') |
| 594 | { |
| 595 | switch (argv[i][1]) |
| 596 | { |
Dave Chapman | 95538f6 | 2006-02-21 01:23:30 +0000 | [diff] [blame] | 597 | case 'h': /* .h filename */ |
| 598 | if (argv[i][2]) |
| 599 | { |
| 600 | header_dir = &argv[i][2]; |
| 601 | } |
| 602 | else if (argc > i+1) |
| 603 | { |
| 604 | header_dir = argv[i+1]; |
| 605 | i++; |
| 606 | } |
| 607 | else |
| 608 | { |
| 609 | print_usage(); |
| 610 | exit(1); |
| 611 | } |
| 612 | break; |
| 613 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 614 | case 'i': /* ID */ |
| 615 | if (argv[i][2]) |
| 616 | { |
| 617 | id = &argv[i][2]; |
| 618 | } |
| 619 | else if (argc > i+1) |
| 620 | { |
| 621 | id = argv[i+1]; |
| 622 | i++; |
| 623 | } |
| 624 | else |
| 625 | { |
| 626 | print_usage(); |
| 627 | exit(1); |
| 628 | } |
| 629 | break; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 630 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 631 | case 'a': /* Ascii art */ |
| 632 | ascii = true; |
| 633 | break; |
Greg White | 1ab8f32 | 2007-01-17 19:54:35 +0000 | [diff] [blame] | 634 | |
| 635 | case 'r': /* Raw File */ |
Greg White | b3c66f5 | 2007-01-17 18:30:04 +0000 | [diff] [blame] | 636 | raw = true; |
| 637 | break; |
Greg White | 1ab8f32 | 2007-01-17 19:54:35 +0000 | [diff] [blame] | 638 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 639 | case 'f': |
| 640 | if (argv[i][2]) |
| 641 | { |
| 642 | format = atoi(&argv[i][2]); |
| 643 | } |
| 644 | else if (argc > i+1) |
| 645 | { |
| 646 | format = atoi(argv[i+1]); |
| 647 | i++; |
| 648 | } |
| 649 | else |
| 650 | { |
| 651 | print_usage(); |
| 652 | exit(1); |
| 653 | } |
| 654 | break; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 655 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 656 | default: |
| 657 | print_usage(); |
| 658 | exit(1); |
| 659 | break; |
| 660 | } |
| 661 | } |
| 662 | else |
| 663 | { |
| 664 | if (!bmp_filename) |
| 665 | { |
| 666 | bmp_filename = argv[i]; |
| 667 | } |
| 668 | else |
| 669 | { |
| 670 | print_usage(); |
| 671 | exit(1); |
| 672 | } |
| 673 | } |
| 674 | } |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 675 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 676 | if (!bmp_filename) |
| 677 | { |
| 678 | print_usage(); |
| 679 | exit(1); |
| 680 | } |
| 681 | |
| 682 | if (!id) |
| 683 | { |
| 684 | char *ptr=strrchr(bmp_filename, '/'); |
| 685 | if (ptr) |
| 686 | ptr++; |
| 687 | else |
| 688 | ptr = bmp_filename; |
| 689 | id = strdup(ptr); |
| 690 | for (i = 0; id[i]; i++) |
| 691 | if (id[i] == '.') |
| 692 | id[i] = '\0'; |
| 693 | } |
| 694 | |
| 695 | if (read_bmp_file(bmp_filename, &width, &height, &bitmap)) |
| 696 | exit(1); |
Greg White | 1ab8f32 | 2007-01-17 19:54:35 +0000 | [diff] [blame] | 697 | |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 698 | |
| 699 | if (ascii) |
| 700 | { |
| 701 | generate_ascii(width, height, bitmap); |
| 702 | } |
| 703 | else |
| 704 | { |
Dave Chapman | 024537d | 2007-01-18 09:30:25 +0000 | [diff] [blame] | 705 | if (transform_bitmap(bitmap, width, height, format, &t_bitmap, |
| 706 | &t_width, &t_height, &t_depth)) |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 707 | exit(1); |
Greg White | b3c66f5 | 2007-01-17 18:30:04 +0000 | [diff] [blame] | 708 | if(raw) |
Daniel Stenberg | afecfe4 | 2008-04-10 21:16:00 +0000 | [diff] [blame] | 709 | generate_raw_file(t_bitmap, t_width, t_height, t_depth); |
Greg White | b3c66f5 | 2007-01-17 18:30:04 +0000 | [diff] [blame] | 710 | else |
Dave Chapman | 024537d | 2007-01-18 09:30:25 +0000 | [diff] [blame] | 711 | generate_c_source(id, header_dir, width, height, t_bitmap, |
| 712 | t_width, t_height, t_depth); |
Jens Arnold | c5e87ae | 2005-07-07 22:32:42 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | return 0; |
Mats Lidell | 8a700fe | 2002-08-16 14:02:48 +0000 | [diff] [blame] | 716 | } |