Blender  V2.59
GPC_RawImage.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: GPC_RawImage.cpp 35170 2011-02-25 13:35:11Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program 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 this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00021  * All rights reserved.
00022  *
00023  * The Original Code is: all of this file.
00024  *
00025  * Contributor(s): none yet.
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  */
00029 
00035 #include <iostream>
00036 #include <string.h>
00037 
00038 #include "GPC_RawImage.h"
00039 #include "GPC_RawLogoArrays.h"
00040 
00041 
00042 GPC_RawImage::GPC_RawImage()
00043                 : m_data(0), m_dataSize(0), m_width(0), m_height(0)
00044 {
00045 }
00046 
00047 
00048 bool GPC_RawImage::Load(
00049                 const char *srcName,
00050                 int destWidth, int destHeight,
00051                 TImageAlignment alignment, int offsetX, int offsetY)
00052 {
00053         int srcWidth, srcHeight;
00054         bool success = true;
00055         if(strcmp(srcName, "BlenderLogo") == 0)
00056                 GetRawBlenderLogo(&m_data, &srcWidth, &srcHeight);
00057         else
00058                 if(strcmp(srcName, "Blender3DLogo") == 0)
00059                         GetRawBlender3DLogo(&m_data, &srcWidth, &srcHeight);
00060 #if 0
00061         else
00062                 if(strcmp(srcName, "NaNLogo") == 0)
00063                         GetRawNaNLogo(&m_data, &srcWidth, &srcHeight);
00064 #endif
00065                 else  // unknown image
00066                         success = false;
00067 
00068         if(success)
00069         {
00070                 unsigned char *tempData = m_data;
00071 
00072                 int numBytes = destWidth * destHeight * 4;
00073                 m_data = new unsigned char[numBytes];  // re-use m_data ('unsigned char' was 'char')
00074                 if(m_data)
00075                 {
00076                         ::memset(m_data, 0x00000000, numBytes);
00077                         m_width = destWidth;
00078                         m_height = destHeight;
00079 
00080                         int srcBytesWidth = srcWidth * 4;
00081                         int dstBytesWidth = m_width * 4;
00082                         int numRows = (srcHeight + offsetY) < m_height ? srcHeight : m_height - offsetY;
00083                         numBytes = (srcWidth + offsetX) < m_width ? srcBytesWidth : (m_width - offsetX) * 4;
00084 
00085                         if((offsetX < m_width) && (offsetY < m_height))
00086                         {
00087                                 unsigned char* src = (unsigned char*)tempData;
00088                                 unsigned char* dst = (unsigned char*)m_data;
00089                                 if(alignment == alignTopLeft)
00090                                 {
00091                                         // Put original in upper left corner
00092 
00093                                         // Add vertical offset
00094                                         dst += offsetY * dstBytesWidth; 
00095                                         // Add horizontal offset
00096                                         dst += offsetX * 4;
00097                                         for (int row = 0; row < numRows; row++)
00098                                         {
00099                                                 ::memcpy(dst, src, numBytes);
00100                                                 src += srcBytesWidth;
00101                                                 dst += dstBytesWidth;
00102                                         }
00103                                 }
00104                                 else
00105                                 {
00106                                         // Put original in lower right corner
00107 
00108                                         // Add vertical offset
00109                                         dst += (m_height - (srcHeight + offsetY)) * dstBytesWidth;
00110                                         // Add horizontal offset
00111                                         if (m_width > (srcWidth + offsetX)) {
00112                                                 dst += (m_width - (srcWidth + offsetX)) * 4;
00113                                         }
00114                                         else {
00115                                                 src += (srcWidth + offsetX - m_width) * 4;
00116                                         }
00117                                         for (int row = 0; row < numRows; row++) {
00118                                                 ::memcpy(dst, src, numBytes);
00119                                                 src += srcBytesWidth;
00120                                                 dst += dstBytesWidth;
00121                                         }
00122                                 }
00123                         }
00124 // doesn't compile under Linux                  delete [] tempData;
00125                         delete tempData;
00126                 }
00127                 else {
00128                         // Allocation failed, restore old data
00129                         m_data = tempData;
00130                         success = false;
00131                 }
00132         }
00133         
00134         return success;
00135 }
00136