我在C ++中有一个称为Image的类,其中有一个宽度,高度和一个长向量,该向量以一种方式存储所有像素值,它迭代第一列,然后向下移动一行,迭代第二遍列等。(0 ist黑色,255是白色)
现在,我编写了一个函数void Chess(),该函数将像素值转换为具有像素的基本象棋图案(当您将其发出时)。
我的下一个想法是创建具有变化尺寸的棋盘,例如宽度8,高度8,并且棋盘的每个正方形都是2x2像素而不是1x1
现在,如果我尝试实现它,我唯一的“想法”就是使用2个计数器,一个计数器跟踪它不是在每个像素之后而是在新的像素大小之后从白色变为黑色。此外,在必须反转多少行之后,您还必须有第二个计数器来跟踪,因此您应从黑色而不是白色开始。
但是我无法弄清楚,最终我得到了太多的If语句以及for和while循环,因此总是杂乱无章。必须有一种明显更简单的方法。有人有什么想法吗?
class Image
{
public:
typedef uint16_t PixelType;
private:
int width_;
int height_;
std::vector<PixelType> data_;
public:
// Standardkonstruktor: initialisiere Bild mit Groesse (0,0)
Image()
: width_(0)
, height_(0)
{}
Image(unsigned int width, unsigned int height)
: width_(width)
, height_(height)
{
int size = width * height;
for(int j=0; j < height; j++)
{
for(int i=0; i < width; i++)
{
data_.push_back(0);
}
}
}
void chess() // change an image to chess board pattern
{
data_.clear();
int width = (*this).width();
int height = (*this).height();
for(int w=0; w < width; w++)
{
for(int h=1; h <= height; h++)
{
/*std::cout << "P=("+ std::to_string(w) + "," + std::to_string(h) + ")" << std::endl;
std::cout << std::to_string(h%2) <<std::endl;
for testing purposes only */
if(w%2==0)
{
if( !(h%2==0) )
{
data_.push_back(255);
}
else
{
data_.push_back(0);
}
}
else
{
if( !(h%2==0) )
{
data_.push_back(0);
} else
{
data_.push_back(255);
}
}
}
}
}
};
您可以尝试根据w和h的奇偶性为矩阵着色。如果w + h甚至是黑色,则其他颜色是白色。从0开始以h和w开头。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句