Mac下使用CImg
环境
macOS 10.14.4
准备
下载CImg.h 编译
#include "CImg.h"
g++ test.cpp -o main
报错
In file included from test.cpp:1:
./CImg.h:371:10: fatal error: 'X11/Xlib.h' file not found
#include <X11/Xlib.h>
^~~~~~~~~~~~
1 error generated
安装xquartz
brew cask install xquartz
重新编译
g++ test.cpp -o main -I/opt/X11/include
修改下test.cpp
#include "CImg.h"
int main(int argc, char const *argv[])
{
/* code */
return 0;
}
重新编译
g++ test.cpp -o main -I/opt/X11/include
编译成功 修改下test.cpp
#include "CImg.h"
using namespace cimg_library;
int main() {
//读取图片
CImg<unsigned char> img("1.bmp");
//显示图片
img.display("Ex1");
return 0;
}
重新编译
g++ test.cpp -o main -I/opt/X11/include
编译成功 运行
./main
报错
[CImg] *** CImgDisplayException *** CImgDisplay::screen_width(): Failed to open X11 display.
libc++abi.dylib: terminating with uncaught exception of type cimg_library::CImgDisplayException: CImgDisplay::screen_width(): Failed to open X11 display.
Abort trap: 6
下载安装 XQuartz 重启
运行成功
代码
1.读入 1.bmp 文件,并用 CImg.display() 显示。
#include "CImg.h"
using namespace cimg_library;
#define cimg_display_type 0
int main()
{
CImg<unsigned char> SrcImg;
SrcImg.load_bmp("1.bmp");
SrcImg.display();
return 0;
}
- 把 1.bmp 文件的白色区域变成红色,黑色区域变成绿色。
#include "CImg.h"
using namespace cimg_library;
#define cimg_display_type 0
int main()
{
CImg<unsigned char> SrcImg;
SrcImg.load_bmp("1.bmp");
cimg_forXY(SrcImg, x, y) {
if (SrcImg(x, y, 0) == 0 && SrcImg(x, y, 1) == 0 && SrcImg(x, y, 2) == 0) {
SrcImg(x, y, 0) = 0;
SrcImg(x, y, 1) = 255;
SrcImg(x, y, 2) = 0;
}
if (SrcImg(x, y, 0) == 255 && SrcImg(x, y, 1) == 255 && SrcImg(x, y, 2) == 255) {
SrcImg(x, y, 0) = 255;
SrcImg(x, y, 1) = 0;
SrcImg(x, y, 2) = 0;
}
}
SrcImg.display();
return 0;
}
- 在图上绘制一个等边三角形区域,其中心坐标(50,50),边长为 40,填充颜色为 蓝色。 蓝色RGB(0,0,255)
#include "CImg.h"
using namespace cimg_library;
#define cimg_display_type 0
void draw_triangle(CImg<unsigned char>& srcImg, int x1, int y1,
int x2, int y2, int x3, int y3) {
int width = srcImg.width();
int height = srcImg.height();
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
double vx1 = x2 - x1, vy1 = y2 - y1, vx2 = x3 - x1, vy2 = y3 - y1;
double vx0 = i - x1, vy0 = j - y1;
double u = (vx2*vy0 - vx0*vy2) / (double)(vx2*vy1 - vx1*vy2),
v = (vx1*vy0 - vx0*vy1) / (double)(vx1*vy2 - vx2*vy1);
if (u >= 0 && v >= 0 && u+v <= 1) {
srcImg(i, j, 0, 0) = 0;
srcImg(i, j, 0, 1) = 0;
srcImg(i, j, 0, 2) = 255;
}
}
}
}
int main()
{
CImg<unsigned char> SrcImg;
SrcImg.load_bmp("1.bmp");
draw_triangle(SrcImg, 50,73,30,38,70,38);
SrcImg.display();
return 0;
}
使用CImg绘图
#include "CImg.h"
using namespace cimg_library;
#define cimg_display_type 0
int main()
{
CImg<unsigned char> SrcImg;
SrcImg.load_bmp("1.bmp");
// draw_triangle(SrcImg, 50,73,30,38,70,38);
unsigned char asd[] = {0, 0, 255};
SrcImg.draw_triangle(50,73,30,38,70,38, asd);
SrcImg.display();
return 0;
}
- 在图上绘制一个圆形区域,圆心坐标(50,50),半径为 15,填充颜色为黄色。 黄色RGB(255.255.0)
#include "CImg.h"
using namespace cimg_library;
#define cimg_display_type 0
void draw_circle(CImg<unsigned char>& srcImg, int x1, int y1, double radius){
int width = srcImg.width();
int height = srcImg.height();
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
double a = (i - x1) * (i - x1);
double b =(j - y1) * (j - y1);
double distance = sqrt(a+b);
if (distance <= radius) {
srcImg(i, j, 0, 0) = 255;
srcImg(i, j, 0, 1) = 255;
srcImg(i, j, 0, 2) = 0;
}
}
}
}
int main()
{
CImg<unsigned char> SrcImg;
SrcImg.load_bmp("1.bmp");
draw_circle(SrcImg, 50,50,15);
SrcImg.display();
return 0;
}
使用CImg绘图
#include "CImg.h"
using namespace cimg_library;
#define cimg_display_type 0
int main()
{
CImg<unsigned char> SrcImg;
SrcImg.load_bmp("1.bmp");
unsigned char asd[] = {255, 255, 0};
SrcImg.draw_circle(50,50,15, asd);
SrcImg.display();
return 0;
}
- 在图上绘制一条长为 100 的直线段,起点坐标为(0, 0),方向角为 135 度,直线 的颜色为绿色。 绿色RGB(0,255,0)
#include "CImg.h"
using namespace cimg_library;
#define cimg_display_type 0
void draw_line(CImg<unsigned char>& srcImg){
for (int i = 0; i < 72; i++) {
srcImg(i, i, 0, 0) = 0;
srcImg(i, i, 0, 1) = 255;
srcImg(i, i, 0, 2) = 0;
}
}
int main()
{
CImg<unsigned char> SrcImg;
SrcImg.load_bmp("1.bmp");
draw_line(SrcImg);
SrcImg.display();
return 0;
}
使用CImg绘图
#include "CImg.h"
using namespace cimg_library;
#define cimg_display_type 0
int main()
{
CImg<unsigned char> SrcImg;
SrcImg.load_bmp("1.bmp");
unsigned char asd[] = {0, 255, 0};
SrcImg.draw_line(0,0,71,71, asd);
SrcImg.display();
return 0;
}
- 把上面的操作结果保存为 2.bmp。
#include "CImg.h"
using namespace cimg_library;
#define cimg_display_type 0
int main()
{
CImg<unsigned char> SrcImg;
SrcImg.load_bmp("1.bmp");
cimg_forXY(SrcImg, x, y) {
if (SrcImg(x, y, 0) == 0 && SrcImg(x, y, 1) == 0 && SrcImg(x, y, 2) == 0) {
SrcImg(x, y, 0) = 0;
SrcImg(x, y, 1) = 255;
SrcImg(x, y, 2) = 0;
}
if (SrcImg(x, y, 0) == 255 && SrcImg(x, y, 1) == 255 && SrcImg(x, y, 2) == 255) {
SrcImg(x, y, 0) = 255;
SrcImg(x, y, 1) = 0;
SrcImg(x, y, 2) = 0;
}
}
unsigned char green[] = {0, 255, 0};
unsigned char yellow[] = {255, 255, 0};
unsigned char blue[] = {0, 0, 255};
SrcImg.draw_triangle(50,73,30,38,70,38, blue);
SrcImg.draw_circle(50,50,15, yellow);
SrcImg.draw_line(0,0,71,71, green);
SrcImg.save("2.bmp");
return 0;
}