00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <math.h>
00023 #include <string.h>
00024 #include <image/ImageBitmap.h>
00025 #include <common/Defines.h>
00026 #include <SDL/SDL.h>
00027
00028 ImageBitmap::ImageBitmap() :
00029 width_(0), height_(0), alpha_(false), newbits_(0),
00030 owner_(true)
00031 {
00032
00033 }
00034
00035 ImageBitmap::ImageBitmap(int startWidth, int startHeight, bool alpha, unsigned char fill) :
00036 width_(startWidth), height_(startHeight), alpha_(alpha), newbits_(0),
00037 owner_(true)
00038 {
00039 createBlankInternal(startWidth, startHeight, alpha, fill);
00040 }
00041
00042 ImageBitmap::~ImageBitmap()
00043 {
00044 clear();
00045 }
00046
00047 void ImageBitmap::clear()
00048 {
00049 if (owner_) delete [] newbits_;
00050 newbits_ = 0;
00051 width_ = 0;
00052 height_ = 0;
00053 }
00054
00055 bool ImageBitmap::loadFromFile(const char * filename, const char *alphafilename, bool invert)
00056 {
00057 ImageBitmap bitmap;
00058 if (!bitmap.loadFromFile(filename, false)) return false;
00059 ImageBitmap alpha;
00060 if (!alpha.loadFromFile(alphafilename, false)) return false;
00061
00062 if (bitmap.getBits() && alpha.getBits() &&
00063 bitmap.getWidth() == alpha.getWidth() &&
00064 bitmap.getHeight() == alpha.getHeight())
00065 {
00066 createBlankInternal(bitmap.getWidth(), bitmap.getHeight(), true);
00067 unsigned char *bbits = bitmap.getBits();
00068 unsigned char *abits = alpha.getBits();
00069 unsigned char *bits = getBits();
00070 for (int y=0; y<bitmap.getHeight(); y++)
00071 {
00072 for (int x=0; x<bitmap.getWidth(); x++)
00073 {
00074 bits[0] = bbits[0];
00075 bits[1] = bbits[1];
00076 bits[2] = bbits[2];
00077
00078 unsigned char avg = (unsigned char)(int(abits[0] + abits[1] + abits[2]) / 3);
00079 if (invert)
00080 {
00081 bits[3] = (unsigned char)(255 - avg);
00082 }
00083 else
00084 {
00085 bits[3] = avg;
00086 }
00087
00088 bbits += 3;
00089 abits += 3;
00090 bits += 4;
00091 }
00092 }
00093 }
00094
00095 return true;
00096 }
00097
00098 bool ImageBitmap::loadFromFile(const char * filename, bool alpha)
00099 {
00100 SDL_Surface *image = SDL_LoadBMP(filename);
00101 if (!image) return false;
00102
00103 if (image->format->BitsPerPixel != 24)
00104 {
00105 S3D::dialogExit("ImageBitmap",
00106 S3D::formatStringBuffer("ERROR: Bitmap \"%s\" is not encoded as a 24bit bitmap",
00107 filename));
00108 }
00109
00110
00111 createBlankInternal(image->w, image->h, alpha);
00112
00113
00114
00115 unsigned char *from = (unsigned char *) image->pixels;
00116 for (int i=0; i<height_; i ++)
00117 {
00118 unsigned char *destRow = ((unsigned char *) newbits_) + ((height_ - i - 1) * (width_ * getComponents()));
00119 for (int j=0; j<width_; j++)
00120 {
00121 unsigned char *dest = destRow + (j * getComponents());
00122
00123 dest[0] = from[2];
00124 dest[1] = from[1];
00125 dest[2] = from[0];
00126 if (alpha)
00127 {
00128 dest[3] = (unsigned char)(from[0]+from[1]+from[2]==0?0:255);
00129 }
00130
00131 from+=3;
00132 }
00133 }
00134
00135 SDL_FreeSurface(image);
00136 return true;
00137 }
00138
00139 void ImageBitmap::createBlankInternal(int width, int height, bool alpha, unsigned char fill)
00140 {
00141 clear();
00142 width_ = width;
00143 height_ = height;
00144 alpha_ = alpha;
00145 int bitsize = getComponents() * width * height;
00146
00147 newbits_ = new unsigned char[bitsize];
00148 memset(newbits_, fill, bitsize);
00149 }