Initial revision
[blender.git] / source / gameengine / GamePlayer / common / GPC_RawImage.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32    
33 #include <iostream>
34 #include <string.h>
35
36 #include "GPC_RawImage.h"
37
38 #include "GPC_RawLogoArrays.h"
39
40
41 GPC_RawImage::GPC_RawImage()
42                 : m_data(0), m_dataSize(0), m_width(0), m_height(0)
43 {
44 }
45
46
47 bool GPC_RawImage::Load(
48                 char *srcName,
49                 int destWidth, int destHeight,
50                 TImageAlignment alignment, int offsetX, int offsetY)
51 {
52         int srcWidth, srcHeight;
53         bool success = true;
54         if(strcmp(srcName, "BlenderLogo") == 0)
55                 GetRawBlenderLogo(&m_data, &srcWidth, &srcHeight);
56         else
57                 if(strcmp(srcName, "Blender3DLogo") == 0)
58                         GetRawBlender3DLogo(&m_data, &srcWidth, &srcHeight);
59 #if 0
60         else
61                 if(strcmp(srcName, "NaNLogo") == 0)
62                         GetRawNaNLogo(&m_data, &srcWidth, &srcHeight);
63 #endif
64                 else  // unknown image
65                         success = false;
66
67         if(success)
68         {
69                 unsigned char *tempData = m_data;
70
71                 int numBytes = destWidth * destHeight * 4;
72                 m_data = new unsigned char[numBytes];  // re-use m_data ('unsigned char' was 'char')
73                 if(m_data)
74                 {
75                         ::memset(m_data, 0x00000000, numBytes);
76                         m_width = destWidth;
77                         m_height = destHeight;
78
79                         int srcBytesWidth = srcWidth * 4;
80                         int dstBytesWidth = m_width * 4;
81                         int numRows = (srcHeight + offsetY) < m_height ? srcHeight : m_height - offsetY;
82                         numBytes = (srcWidth + offsetX) < m_width ? srcBytesWidth : (m_width - offsetX) * 4;
83
84                         if((offsetX < m_width) && (offsetY < m_height))
85                         {
86                                 unsigned char* src = (unsigned char*)tempData;
87                                 unsigned char* dst = (unsigned char*)m_data;
88                                 if(alignment == alignTopLeft)
89                                 {
90                                         // Put original in upper left corner
91
92                                         // Add vertical offset
93                                         dst += offsetY * dstBytesWidth; 
94                                         // Add horizontal offset
95                                         dst += offsetX * 4;
96                                         for (int row = 0; row < numRows; row++)
97                                         {
98                                                 ::memcpy(dst, src, numBytes);
99                                                 src += srcBytesWidth;
100                                                 dst += dstBytesWidth;
101                                         }
102                                 }
103                                 else
104                                 {
105                                         // Put original in lower right corner
106
107                                         // Add vertical offset
108                                         dst += (m_height - (srcHeight + offsetY)) * dstBytesWidth;
109                                         // Add horizontal offset
110                                         if (m_width > (srcWidth + offsetX)) {
111                                                 dst += (m_width - (srcWidth + offsetX)) * 4;
112                                         }
113                                         else {
114                                                 src += (srcWidth + offsetX - m_width) * 4;
115                                         }
116                                         for (int row = 0; row < numRows; row++) {
117                                                 ::memcpy(dst, src, numBytes);
118                                                 src += srcBytesWidth;
119                                                 dst += dstBytesWidth;
120                                         }
121                                 }
122                         }
123 // doesn't compile under Linux                  delete [] tempData;
124                         delete tempData;
125                 }
126                 else {
127                         // Allocation failed, restore old data
128                         m_data = tempData;
129                         success = false;
130                 }
131         }
132         
133         return success;
134 }
135