/* BMPimage - some classes for generating BMP files This include file is refered to at http://www.iwriteiam.nl/D0906.html#13 Copyright (C) 2009 Frans Faase 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., 675 Mass Ave, Cambridge, MA 02139, USA. GNU General Public License: http://www.iwriteiam.nl/GNU.txt */ class AbstractBMPimage { public: unsigned long width; unsigned long height; virtual unsigned char red(unsigned long x, unsigned long y) = 0; virtual unsigned char green(unsigned long x, unsigned long y) = 0; virtual unsigned char blue(unsigned long x, unsigned long y) = 0; void save(char *name) { FILE *f = fopen(name, "wb"); if (f != 0) { save(f); fclose(f); } } void save(FILE* f) { unsigned short fileType = 0x4D42; unsigned long fileSize; unsigned short reserveA = 0; unsigned short reserveB = 0; unsigned long imageOffset = 54; unsigned long infoSize = 40; unsigned short planes = 1; unsigned short depth = 24; unsigned long compression = 0; unsigned long imageSize = 0; unsigned long xpels = 0; unsigned long ypels = 0; unsigned long clrsUsed = 0; unsigned long clrsImpt = 0; int padding = width % 4; imageSize = (width * 3 + padding) * height; fileSize = imageSize + 54; _write(f, fileType); _write(f, fileSize); _write(f, reserveA); _write(f, reserveB); _write(f, imageOffset); _write(f, infoSize); _write(f, width); _write(f, height); _write(f, planes); _write(f, depth); _write(f, compression); _write(f, imageSize); _write(f, xpels); _write(f, ypels); _write(f, clrsUsed); _write(f, clrsImpt); for (long y = height-1; y >= 0; y--) { for (unsigned long x = 0; x < width; x++) { _write(f, red(x, y)); _write(f, green(x, y)); _write(f, blue(x, y)); } for (int k = 0; k < padding; k++) _write(f, (unsigned char)0); } } private: void _write(FILE*f, unsigned char ch) { fputc(ch, f); } void _write(FILE*f, unsigned short s) { fputc(s, f); fputc(s >> 8, f); } void _write(FILE*f, unsigned long l) { fputc(l, f); fputc(l >> 8, f); fputc(l >> 16, f); fputc(l >> 24, f); } }; class CharTableBMPimage : public AbstractBMPimage { public: CharTableBMPimage() : _image(0) { for (int i = 0; i < 256; i++) _table[i].red = _table[i].green = _table[i].blue = 0; } ~CharTableBMPimage() { delete _image; } virtual unsigned char red(unsigned long x, unsigned long y) { return _table[_image[x + y * width]].red; } virtual unsigned char green(unsigned long x, unsigned long y) { return _table[_image[x + y * width]].green; } virtual unsigned char blue(unsigned long x, unsigned long y) { return _table[_image[x + y * width]].blue; } void allocate() { _image = new unsigned char[height * width]; for (unsigned long i = 0; i < height * width; i++) _image[i] = ' '; } void set(unsigned long x, unsigned long y, char v) { _image[x + y * width] = (unsigned char)v; } void define(char v, int r, int g, int b) { _table[(unsigned char)v].red = r; _table[(unsigned char)v].green = g; _table[(unsigned char)v].blue = b; } private: struct { unsigned char red; unsigned char green; unsigned char blue; } _table[256]; unsigned char *_image; };