ImageBitmap.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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         // Create the internal byte array
00111         createBlankInternal(image->w, image->h, alpha);
00112 
00113         // Convert the returned bits from BGR to RGB
00114         // and flip the verticle scan lines
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 }

Generated on Mon Feb 16 15:14:50 2009 for Scorched3D by  doxygen 1.5.3