问题描述
我正在使用带有以下代码的 stb_image stb_image_write 和 CImg
int width,height,bpp;
img = stbi_load(imgPath,&width,&height,&bpp,3);
CImg<unsigned char> cimg(img,width,1,bpp);
CImg<unsigned char> wimg(width,10,3,0);
cimg.append(wimg,'y');
cimg.display();
stbi_write_png("save.png",cimg,width * 3);
它只是创建一个 10 像素的黑线并附加到图像底部,当我显示它时它工作正常,但是当我保存它时显示扭曲
下面的代码工作正常,用stb读取图像在cimg中做一些改变并用stb保存它把小图像覆盖到大图像
int width,bpp;
int width2,height2,bpp2;
CImg<unsigned char> gradient(img1,bpp,1);
CImg<unsigned char> overlay(img2,bpp2,width2,1);
CImg<unsigned char> gradient(img1,1);
gradient.draw_image(0,overlay);
stbi_write_png("gradient.png",gradient,width * 3);
解决方法
好的,我按照 Mark Setchell 在关于 interleave 的评论中提到的那样工作,所以我必须按照下面的帖子中提到的方式排列缓冲区结构
CImg library creates distorted images on rotation
所以如果我需要使用这三个库,那么我的代码如下
int width,height,bpp;
setHeader();
img = stbi_load(imgPath,&width,&height,&bpp,3);
//Load with stb type
CImg<unsigned char> cimg(img,bpp,width,1);
//Convert cimg type
cimg.permute_axes("yzcx");
//Can work with all type of cimg functions
CImg<unsigned char> wimg(width,10,1,3,0);
cimg.append(wimg,'y');
//Convert back to stb type to save
cimg.permute_axes("cxyz");
stbi_write_png("save.png",height+10,cimg,width * 3);