/* * Trevor's quick & dirty hack to convert from BMP to OTA. * * Copyright (C) 2003 Trevor Man * * 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 program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include inline unsigned long get_long(FILE *fp); inline unsigned int get_short(FILE *fp); int main(int argc, char *argv[]) { FILE *fp_in; FILE *fp_out = NULL; unsigned long file_size; unsigned long offset; unsigned long temp_long; unsigned int temp_short; unsigned long bitmap_width; unsigned long bitmap_height; unsigned int bitmap_planes; unsigned int bitmap_bitcount; int count; int padding_count; if (argc != 3 && argc != 4) { fprintf(stderr, "Usage: %s bmp_file ota_file [reverse]\n", argv[0]); return -1; } if ((fp_in = fopen(argv[1], "rb")) == NULL) { fprintf(stderr, "Unable to open %s for reading.\n", argv[1]); return -1; } if ((fp_out = fopen(argv[2], "wb")) == NULL) { fprintf(stderr, "Unable to open %s for writing.\n", argv[2]); return -1; } fseek(fp_in, 0, SEEK_END); file_size = ftell(fp_in); fseek(fp_in, 0, SEEK_SET); if (get_short(fp_in) != 0x4D42) { // Check for BM fprintf(stderr, "ERROR: BM signature not found.\n"); return -1; } if ((temp_long = get_long(fp_in)) != file_size) { fprintf(stderr, "WARNING: Header file size (%ld) and real file size (%ld) differ.\n", temp_long, file_size); } if ((temp_short = get_short(fp_in)) != 0) { // Check that reserved1 == 0 fprintf(stderr, "WARNING: Reserved1 (%X) field != 0\n", temp_short); } if ((temp_short = get_short(fp_in)) != 0) { // Check that reserved2 == 0 fprintf(stderr, "WARNING: Reserved2 (%X) field != 0\n", temp_short); } offset = get_long(fp_in); if ((temp_long = get_long(fp_in)) != 40) { // Check that sizeof(BITMAPINFOHEADER) == 40 fprintf(stderr, "WARNING: sizeof(BITMAPINFOHEADER) (%ld) != 40\n", temp_long); } if ((bitmap_width = get_long(fp_in)) != 72) { // Check that width == 72 fprintf(stderr, "ERROR: Invalid width (%ld). Must be 72.\n", bitmap_width); return -1; } if ((bitmap_height = get_long(fp_in)) != 28) { // Check that height == 28 fprintf(stderr, "ERROR: Invalid height (%ld). Must be 28.\n", bitmap_height); return -1; } if ((bitmap_planes = get_short(fp_in)) != 1) { // Check that planes == 1 fprintf(stderr, "ERROR: Invalid planes (%d). Must be 1.\n", bitmap_planes); return -1; } if ((bitmap_bitcount = get_short(fp_in)) != 1) { // Check that bitcount == 1 fprintf(stderr, "ERROR: Invalid bitcount (%d). Must be 1.\n", bitmap_bitcount); return -1; } if ((temp_long = get_long(fp_in)) != 0) { // Check that compression is turned off fprintf(stderr, "ERROR: Must be uncompressed BMP.\n"); return -1; } fseek(fp_in, offset, SEEK_SET); // Skip to bitmap data fputc(0x00, fp_out); fputc(bitmap_width, fp_out); fputc(bitmap_height, fp_out); fputc(bitmap_bitcount, fp_out); fseek(fp_out, 4 + ((bitmap_width / 8) * (bitmap_height - 1)), SEEK_SET); for (count = 0; count < (bitmap_height * bitmap_width); count += 8) { temp_short = fgetc(fp_in); if (argc == 4) temp_short = ~temp_short; fprintf(stdout, "%c%c%c%c%c%c%c%c", temp_short & 0x80 ? '#' : ' ', temp_short & 0x40 ? '#' : ' ', temp_short & 0x20 ? '#' : ' ', temp_short & 0x10 ? '#' : ' ', temp_short & 0x08 ? '#' : ' ', temp_short & 0x04 ? '#' : ' ', temp_short & 0x02 ? '#' : ' ', temp_short & 0x01 ? '#' : ' '); if (fp_out) fputc(temp_short, fp_out); if ((count > 0) && ((count + 8) % bitmap_width == 0)) { fprintf(stdout, "\n"); for (padding_count = 0; padding_count < (4 - (bitmap_width / 8) % 4); padding_count++) { fgetc(fp_in); } fseek(fp_out, 4 + ((bitmap_width / 8) * (bitmap_height - ((count + 8) / bitmap_width) - 1)), SEEK_SET); } } fclose(fp_in); if (fp_out) fclose(fp_out); return 0; } inline unsigned long get_long(FILE *fp) { return fgetc(fp) | fgetc(fp) << 8 | fgetc(fp) << 16 | fgetc(fp) << 24; } inline unsigned int get_short(FILE *fp) { return fgetc(fp) | fgetc(fp) << 8; }