我一直在尝试了解如何使用CImg的绘图功能,但是文档对我来说不是很清楚。我只想绘制一个像素,但不了解draw_point的工作原理。有人可以举一些draw_point以及如何声明图像的示例吗?另外,对于C ++,还有更好的选择吗?我只想要C ++最简单的映像库。我想逐像素处理一个空图像。有更好的选择吗?
我已经修改了CImg教程,以显示如何使用draw_point,代码如下:
#include "CImg.h"
using namespace cimg_library;
int main()
{
int size_x = 640;
int size_y = 480;
int size_z = 1;
int numberOfColorChannels = 3; // R G B
unsigned char initialValue = 0;
CImg<unsigned char> image(size_x, size_y, size_z, numberOfColorChannels, initialValue);
CImgDisplay display(image, "Click a point");
while (!display.is_closed())
{
display.wait();
if (display.button() && display.mouse_y() >= 0 && display.mouse_x() >= 0)
{
const int y = display.mouse_y();
const int x = display.mouse_x();
unsigned char randomColor[3];
randomColor[0] = rand() % 256;
randomColor[1] = rand() % 256;
randomColor[2] = rand() % 256;
image.draw_point(x, y, randomColor);
}
image.display(display);
}
return 0;
}
方法draw_point具有三个重载,这可能会混淆使用。我用了以下之一:
template<typename tc>
CImg<T>& draw_point(const int x0, const int y0,
const tc *const color, const float opacity=1)
请参阅此了解详情。
至于替代方案,如果您只想逐像素修改数据,也许可以使用libpng和libjpeg作为输入/输出来处理图像和原始数据。
无论如何,我建议您阅读有关CImg的更多文档。我已经在一些项目中使用过它,对我来说它非常方便。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句