Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
Image.cpp
Go to the documentation of this file.
1/*
2--------------------------------------------------------------------------------
3This source file is part of Hydrax.
4Visit ---
5
6Copyright (C) 2008 Xavier Vergu�n Gonz�lez <xavierverguin@hotmail.com>
7 <xavyiy@gmail.com>
8
9This program is free software; you can redistribute it and/or modify it under
10the terms of the GNU Lesser General Public License as published by the Free Software
11Foundation; either version 2 of the License, or (at your option) any later
12version.
13
14This program is distributed in the hope that it will be useful, but WITHOUT
15ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17
18You should have received a copy of the GNU Lesser General Public License along with
19this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21http://www.gnu.org/copyleft/lesser.txt.
22--------------------------------------------------------------------------------
23*/
24
25#include <Image.h>
26
27namespace Hydrax
28{
30 : mSize(Hydrax::Size(Size.Width+1,Size.Height+1))
31 , mChannels(static_cast<int>(TYPE_RGBA))
32 , mData(0)
33 {
34 _Initialize(0);
35 }
36
37 Image::Image(const Size &Size, const Type &Type)
38 : mSize(Hydrax::Size(Size.Width+1,Size.Height+1))
39 , mChannels(static_cast<int>(Type))
40 , mData(0)
41 {
42 _Initialize(0);
43 }
44
45 Image::Image(const Size &Size, const Type &Type, const float &v)
46 : mSize(Hydrax::Size(Size.Width+1,Size.Height+1))
47 , mChannels(static_cast<int>(Type))
48 , mData(0)
49 {
50 _Initialize(v);
51 }
52
54 {
55 delete mData;
56 }
57
58 const float& Image::getValue(const int &x, const int &y, const int &c) const
59 {
60#if HYDRAX_IMAGE_CHECK_PIXELS == 1
61 if (c < 0 || c > mChannels ||
62 x < 0 || x > mSize.Width ||
63 y < 0 || y > mSize.Height)
64 {
65 HydraxLOG("Error in Image::getValue, x = " + Ogre::StringConverter::toString(x)
66 + " y = " + Ogre::StringConverter::toString(y)
67 + " Channel = " + Ogre::StringConverter::toString(c));
68
69 return 0;
70 }
71#endif
72
73 return mData[(y*mSize.Width+x)*mChannels+c];
74 }
75
76 float Image::getValueLI(const float &x, const float &y, const int &c) const
77 {
78#if HYDRAX_IMAGE_CHECK_PIXELS == 1
79 if (c < 0 || c > mChannels ||
80 x < 0 || x > mSize.Width ||
81 y < 0 || y > mSize.Height)
82 {
83 HydraxLOG("Error in Image::getValue, x = " + Ogre::StringConverter::toString(x)
84 + " y = " + Ogre::StringConverter::toString(y)
85 + " Channel = " + Ogre::StringConverter::toString(c));
86
87 return 0;
88 }
89#endif
90
91 int xINT = static_cast<int>(x),
92 yINT = static_cast<int>(y);
93
94 float xDIFF = x-xINT,
95 yDIFF = y-yINT,
96 _xDIFF = 1-xDIFF,
97 _yDIFF = 1-yDIFF;
98
99 // A B
100 //
101 //
102 // C D
103 float A = mData[(yINT*mSize.Width+xINT)*mChannels+c],
104 B = mData[(yINT*mSize.Width+xINT+1)*mChannels+c],
105 C = mData[((yINT+1)*mSize.Width+xINT)*mChannels+c],
106 D = mData[((yINT+1)*mSize.Width+xINT+1)*mChannels+c];
107
108 return A*_xDIFF*_yDIFF +
109 B* xDIFF*_yDIFF +
110 C*_xDIFF* yDIFF +
111 D* xDIFF* yDIFF;
112 }
113
114 Image::Pixel Image::getPixel(const int &x, const int &y) const
115 {
116#if HYDRAX_IMAGE_CHECK_PIXELS == 1
117 if (x < 0 || x > mSize.Width ||
118 y < 0 || y > mSize.Height)
119 {
120 HydraxLOG("Error in Image::getPixel, x = " + Ogre::StringConverter::toString(x)
121 + " y = " + Ogre::StringConverter::toString(y));
122
123 return Pixel(0);
124 }
125#endif
126
127 float v[4];
128
129 for(int k = 0; k < 4; k++)
130 {
131 if (mChannels >= k)
132 {
133 v[k] = getValue(x, y, k);
134 }
135 else
136 {
137 v[k] = 0;
138 }
139 }
140
141 return Pixel(v[0], v[1], v[2], v[3]);
142 }
143
144 Image::Pixel Image::getPixelLI(const float &x, const float &y) const
145 {
146#if HYDRAX_IMAGE_CHECK_PIXELS == 1
147 if (x < 0 || x > mSize.Width ||
148 y < 0 || y > mSize.Height)
149 {
150 HydraxLOG("Error in Image::getPixel, x = " + Ogre::StringConverter::toString(x)
151 + " y = " + Ogre::StringConverter::toString(y));
152
153 return Pixel(0);
154 }
155#endif
156
157 float v[4];
158
159 for(int k = 0; k < 4; k++)
160 {
161 if (mChannels >= k)
162 {
163 v[k] = getValueLI(x, y, k);
164 }
165 else
166 {
167 v[k] = 0;
168 }
169 }
170
171 return Pixel(v[0], v[1], v[2], v[3]);
172 }
173
174 void Image::setValue(const int &x, const int &y, const int &c, const float &v)
175 {
176#if HYDRAX_IMAGE_CHECK_PIXELS == 1
177 if (c < 0 || c > mChannels ||
178 x < 0 || x > mSize.Width ||
179 y < 0 || y > mSize.Height)
180 {
181 HydraxLOG("Error in Image::setValue, x = " + Ogre::StringConverter::toString(x)
182 + " y = " + Ogre::StringConverter::toString(y)
183 + " Channel = " + Ogre::StringConverter::toString(c));
184
185 return;
186 }
187#endif
188
189 mData[(y*mSize.Width+x)*mChannels+c] = v;
190 }
191
192 void Image::setPixel(const int &x, const int &y, const Pixel &p)
193 {
194#if HYDRAX_IMAGE_CHECK_PIXELS == 1
195 if (x < 0 || x > mSize.Width ||
196 y < 0 || y > mSize.Height)
197 {
198 HydraxLOG("Error in Image::setPixel, x = " + Ogre::StringConverter::toString(x)
199 + " y = " + Ogre::StringConverter::toString(y));
200
201 return;
202 }
203#endif
204
205 switch(mChannels)
206 {
207 case 1:
208 {
209 setValue(x, y, 0, p.red);
210 }
211 break;
212
213 case 2:
214 {
215 setValue(x, y, 0, p.red);
216 setValue(x, y, 1, p.green);
217 }
218 break;
219
220 case 3:
221 {
222 setValue(x, y, 0, p.red);
223 setValue(x, y, 1, p.green);
224 setValue(x, y, 2, p.blue);
225 }
226 break;
227
228 case 4:
229 {
230 setValue(x, y, 0, p.red);
231 setValue(x, y, 1, p.green);
232 setValue(x, y, 2, p.blue);
233 setValue(x, y, 3, p.alpha);
234 }
235 break;
236 }
237 }
238
239 void Image::_Initialize(const float &v)
240 {
241 mData = new float[(mSize.Width) *
242 (mSize.Height) *
243 mChannels];
244
245 int x,y,c;
246
247 for (x = 0; x < mSize.Width; x++)
248 {
249 for (y = 0; y < mSize.Height; y++)
250 {
251 for(c = 0; c < mChannels; c++)
252 {
253 mData[(y*mSize.Width+x)*mChannels+c] = v;
254 }
255 }
256 }
257 }
258}
#define HydraxLOG(msg)
Definition Application.h:60
Type
Image type enum.
Definition Image.h:48
Pixel getPixelLI(const float &x, const float &y) const
Get a pixel with linear interpolation, like x = 4.56, y = 8.34.
Definition Image.cpp:144
~Image()
Destructor.
Definition Image.cpp:53
float getValueLI(const float &x, const float &y, const int &c) const
Get a pixel value with linear interpolation, like x = 4.56, y = 8.34.
Definition Image.cpp:76
void setPixel(const int &x, const int &y, const Pixel &p)
Set a pixel.
Definition Image.cpp:192
Image(const Size &Size)
Constructor.
Definition Image.cpp:29
int mChannels
Number of channels.
Definition Image.h:267
Pixel getPixel(const int &x, const int &y) const
Get a pixel.
Definition Image.cpp:114
const float & getValue(const int &x, const int &y, const int &c) const
Get a pixel value.
Definition Image.cpp:58
void _Initialize(const float &v)
Initialize array (Reserve dynamic memory)
Definition Image.cpp:239
Size mSize
Image size.
Definition Image.h:265
void setValue(const int &x, const int &y, const int &c, const float &v)
Set a pixel value.
Definition Image.cpp:174
float * mData
Our image data.
Definition Image.h:270
Pixel structure.
Definition Image.h:69
float red
Pixel values (RGBA)
Definition Image.h:125
Struct wich contains an especific width and height value.
Definition Help.h:41
int Height
Height value.
Definition Help.h:45
int Width
Width value.
Definition Help.h:43