/* bitmap_func.cpp: Bitmap utilities Copyright (C) 2007 INAKIChan (Written by PlasmaFox) 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 "blackbox.h" // BITMAP FUNCTIONS //////////////////////////////////////////////////////////// int Bitmap_Load(BITMAP_FILE_PTR bitmap, char *filename, PALETTEENTRY palette[256]) { int file_handle, index; // UCHAR *temp_buffer = NULL; OFSTRUCT file_data; if ((file_handle = OpenFile(filename, &file_data, OF_READ)) == -1) { fprintf(stdout, "failed to open %s, aborting\n", filename); return(1); } _lread(file_handle, &bitmap->bitmapfileheader, sizeof(BITMAPFILEHEADER)); if (bitmap->bitmapfileheader.bfType!=BITMAP_ID) { fprintf(stdout, "%s isn't a bitmap, aborting (got %lx, wanted %lx)\n", filename, bitmap->bitmapfileheader.bfType, BITMAP_ID); _lclose(file_handle); return(1); } _lread(file_handle, &bitmap->bitmapinfoheader, sizeof(BITMAPFILEHEADER)); if (bitmap->bitmapinfoheader.biBitCount == 8) { _lread(file_handle, &bitmap->palette, MAX_COLORS_PALETTE*sizeof(PALETTEENTRY)); for (index=0; indexpalette[index].peRed; bitmap->palette[index].peRed = palette[index].peBlue; bitmap->palette[index].peBlue = temp_color; bitmap->palette[index].peFlags = PC_NOCOLLAPSE; } } lseek(file_handle, -(int)(bitmap->bitmapinfoheader.biSizeImage), SEEK_END); if (bitmap->bitmapinfoheader.biBitCount==8 || bitmap->bitmapinfoheader.biBitCount==16 || bitmap->bitmapinfoheader.biBitCount==24 || bitmap->bitmapinfoheader.biBitCount==40035) { if (bitmap->buffer) free(bitmap->buffer); if (!(bitmap->buffer = (UCHAR *)malloc(bitmap->bitmapinfoheader.biSizeImage))) { _lclose(file_handle); fprintf(stdout, "failed to read image data from %s\n", filename); return(1); } _lread(file_handle, bitmap->buffer, bitmap->bitmapinfoheader.biSizeImage); } else { fprintf(stdout, "serious error loading %s, aborting\n", filename); return(2); } _lclose(file_handle); #ifdef DEBUG printf("\ndebug:Bitmap_Load: filename:%s \nsize=%d \nwidth=%d \nheight=%d \nbitsperpixel=%d \ncolors=%d \nimpcolors=%d", filename, bitmap->bitmapinfoheader.biSizeImage, bitmap->bitmapinfoheader.biWidth, bitmap->bitmapinfoheader.biHeight, bitmap->bitmapinfoheader.biBitCount, bitmap->bitmapinfoheader.biClrUsed, bitmap->bitmapinfoheader.biClrImportant); #endif Bitmap_Flip(bitmap->buffer, bitmap->bitmapinfoheader.biWidth*(bitmap->bitmapinfoheader.biBitCount/8), bitmap->bitmapinfoheader.biHeight); return(0); } int Bitmap_Unload(BITMAP_FILE_PTR bitmap) { if (bitmap->buffer) { free(bitmap->buffer); bitmap->buffer = NULL; } return(0); } int Bitmap_Flip(UCHAR *image, int bytes_per_line, int height) { UCHAR *buffer; int index; if (!(buffer = (UCHAR *)malloc(bytes_per_line*height))) { #ifdef DEBUG fprintf(stdout, "err:Bitmap_Flip:malloc couldn't allocate memory for flipping\n"); #endif return(1); } memcpy(buffer,image,bytes_per_line*height); for (index=0; index < height; index++) memcpy(&image[((height-1) - index)*bytes_per_line], &buffer[index*bytes_per_line], bytes_per_line); free(buffer); return(0); } int Bitmap_Create(BITMAP_IMAGE_PTR image, int x, int y, int width, int height) { if (!(image->buffer = (UCHAR *)malloc(width*height))) { #ifdef DEBUG fprintf(stdout, "err:Bitmap_Create:malloc couldn't allocate memory for a %dx%d bitmap (%d bytes)\n", x, y, width*height); #endif return(0); } image->state = BITMAP_STATE_ALIVE; image->attr = 0; image->width = width; image->height = height; image->x = x; image->y = y; image->num_bytes = width*height; memset(image->buffer,0,width*height); return(0); } int Bitmap_Destroy(BITMAP_IMAGE_PTR image) { if (image && image->buffer) { free(image->buffer); memset(image,0,sizeof(BITMAP_IMAGE)); return(1); } else { #ifdef DEBUG fprintf(stdout, "err:Bitmap_Destroy:destroy bad bitmap ptr passed\n"); #endif return(1); } }