我有一个功能可以将源图像的某些区域闪烁到目标图像。并且有一个问题:我有一个错误,但我无法找到它。可能它非常微不足道,但我花了很多时间在它上面:)。我的算法'倾斜'图像上的对象。在调试中,我看到它复制了比它应该多一些的像素(例如36836而不是36481)
struct Point
{
unsigned x, y;
Point(unsigned x, unsigned y) : x(x), y(y) { }
Point() : x(0), y(0) { }
};
struct Rect
{
Point lt, rd; // left-top and right-down vertexs
};
struct Img
{
vector<unsigned char> px; // pixel data in linear form (RBGARBGARBGA...)
unsigned w, h; // width and heigth of image
};
inline bool isInRect(const Point& p, const Rect& r)
{
return (p.x >= r.lt.x && p.y >= r.lt.y && p.x <= r.rd.x && p.y <= r.rd.y);
}
unsigned blit(const Img& src, Img& dest, const Rect& reg) // <--- THIS FUNCTION
{
dest.w = reg.rd.x - reg.lt.x;
dest.h = reg.rd.y - reg.lt.y;
dest.px.clear();
unsigned n = 0;
for(int i = (reg.lt.y * src.w + reg.lt.x) * 4; i < src.px.size(); i += 4)
{
unsigned y = (i / 4) / src.w;
unsigned x = (i / 4) % src.w;
if(isInRect(Point(x, y), reg))
{
dest.px.push_back(src.px[i]);
dest.px.push_back(src.px[i + 1]);
dest.px.push_back(src.px[i + 2]);
dest.px.push_back(src.px[i + 3]);
n += 4;
}
if(y > reg.rd.y)
break;
}
return n / 4;
}
示例:
图像碎片到blit:http://www.mediafire.com/view/1rb8dyc4z8xq5rl/arial-toblit.PNG
算法输出:http://www.mediafire.com/view/qe9j38gq5tp299v/arial-A.png