image.h的
#ifndef _IMAGE
#define _IMAGE
#include "Array.h"
#include "Serializable.h"
#include "Vec3.h"
using namespace math;
class Image : public Array<Vec3<float>>, public Serializable
{
public:
enum channel_t { RED = 0, GREEN, BLUE };
protected:
Vec3<float> *buffer;
public:
Vec3<float> getPixel(unsigned int x, unsigned int y) const;
/*code*/
};
#endif
Image.cpp
#include "Image.h"
using namespace math;
Vec3<float> Image::getPixel(unsigned int x, unsigned int y) const {
math::Vec3<float> v;
int s = 0;
if ((x <= Image::getWidth()) && (y <= Image::getHeight()) && (x >= 0) && (y >= 0))
{
for (unsigned int i = 1; i == getWidth(); i++)
{
for (unsigned int j = 1; i == getHeight(); j++)
{
if (i == x - 1 && j == y - 1)
goto label;
else
s = s + 3;
}
}
label:
v[Image::RED] = buffer[s + RED]; <-------------------HERE
v[Image::GREEN] = buffer[s + GREEN]; <-------------------I GET AN
v[Image::BLUE] = buffer[s + BLUE]; <-------------------ERROR
}
return v;
}//end of getPixel
Vec3.h
#ifndef _Vec3_
#define _Vec3_
namespace math
{
template <typename S>
class Vec3
{
public:
// data members
//! The first coordinate of the vector
union { S x, r; };
//! The second coordinate of the vector
union { S y, g; };
//! The third coordinate of the vector
union { S z, b; };
S & operator [] (size_t index)
{
return *((S*)this + index);
}
Vec3<S> operator + (const Vec3<S> & right)
{
Vec3<S> left;
left.x = x + right.x;
left.y = y + right.y;
left.z = z + right.z;
return left;
}
Vec3<S> operator - (const Vec3<S> & right)
{
Vec3<S> left;
left.x = x - right.x;
left.y = y - right.y;
left.z = z - right.z;
return left;
}
Vec3<S> operator * (const Vec3<S> & right)
{
Vec3<S> left;
left.x = x * right.x;
left.y = y * right.y;
left.z = z * right.z;
return left;
}
Vec3<S> operator * (S right)
{
Vec3<S> left;
left.x = x * right;
left.y = y * right;
left.z = z * right;
return left;
}
Vec3<S> operator / (S right)
{
Vec3<S> left;
left.x = x / right;
left.y = y / right;
left.z = z / right;
return left;
}
Vec3<S> operator / (const Vec3<S> & right)
{
Vec3<S> left;
left.x = x / right.x;
left.y = y / right.y;
left.z = z / right.z;
return left;
}
Vec3<S> & operator += (const Vec3<S> & right)
{
x += right.x;
y += right.y;
z += right.z;
return *this;
}
Vec3<S> & operator -= (const Vec3<S> & right)
{
x -= right.x;
y -= right.y;
z -= right.z;
return *this;
}
Vec3<S> & operator /= (const Vec3<S> & right)
{
x /= right.x;
y /= right.y;
z /= right.z;
return *this;
}
Vec3<S> & operator *= (const Vec3<S> & right)
{
x *= right.x;
y *= right.y;
z *= right.z;
return *this;
}
Vec3<S> & operator *= (S right)
{
x *= right;
y *= right;
z *= right;
return *this;
}
Vec3<S> & operator /= (S right)
{
x /= right;
y /= right;
z /= right;
return *this;
}
Vec3<S>(S x, S y, S z) : x(x), y(y), z(z) {}
Vec3<S>(S val) : x(val), y(val), z(val) {}
Vec3<S>() : x(), y(), z() {}
Vec3<S>(const Vec3<S> & right) : x(right.x), y(right.y), z(right.z) {}
Vec3<S> & operator = (const Vec3<S> & right)
{
x = right.x;
y = right.y;
z = right.z;
return *this;
}
bool operator == (const Vec3<S> & right) const
{
return x == right.x && y == right.y && z == right.z;
}
bool operator != (const Vec3<S> & right) const
{
return x != right.x || y != right.y || z != right.z;
}
};
template<typename S>
Vec3<S> operator * (S a, Vec3<S> v)
{
return v*a;
}
template<typename S>
Vec3<S> operator * (int a, Vec3<S> v)
{
return v*S(a);
}
template<typename S>
Vec3<S> operator * (Vec3<S> v, int a)
{
return v*S(a);
}
template<typename S>
Vec3<S> operator / (Vec3<S> v, int a)
{
return v/S(a);
}
} // namespace math
#endif
答案 0 :(得分:0)
v和缓冲区有不同的类型:
Vec3<float> *buffer;
math::Vec3<float> v;
缓冲区是指向Vec3的指针,当你写&#34;缓冲区[s + RED];&#34;你试图获得指针的s + RED元素。但是在v vector和v [Image :: RED]是红色通道(它是浮动的)。
尝试使用它:
v[Image::RED] = buffer[s][RED];
答案 1 :(得分:0)
buffer
不是math::Vec3
类型,而是指向此类对象的指针,也可以解释为此类对象的数组。 buffer[s + RED]
是此数组的一个元素,因此它是math::Vec3
类型,而v[Image::RED]
属于float
类型。
您可能想写
v[Image::RED] = buffer[s][Image::RED];
答案 2 :(得分:0)
v
的类型为math::Vec3<float>
,v[0]
为float
。
buffer
是指向Vec3<float>
的指针,通过索引它,您使用指针的operator[]
,即数组访问。因此,通过buffer[i]
,您会得到一个Vec3<float>
,它无法转换为简单的float
。
答案 3 :(得分:0)
buffer
是指向<{1}}的指针,因此Vec3<float>
是buffer[k]
。
Vec3<float>
是v[x]
,而不是float
。
您的索引建议您打算Vec3<float>
成为&#34;平坦的&#34;将像素表示为buffer
s,在这种情况下,您可以使用
float
(或std::vector<float> buffer;
,但这会引发一系列内存管理问题。)
你可以使用算术而不是搜索循环:
char* buffer;
(这使用从零开始的索引;您的代码似乎与像素是从零还是从一开始有冲突。)