我正在编写一个简单的程序,将一堆单独的图像文件粘贴到一个动画和扩展图中。但是我遇到了问题。
据我所知:
unsigned char* image = stbi_load(wholefoldername.c_str(),&width,&height,&channels,4);
将图像加载到数组中,如下所示:[r,g,b,a,r,g,b,a,...]
因此,例如,如果我加载... 25张120 * 120的图像,然后尝试将它们放入5x5的网格中
然后我可以做类似的事情:
int imageheight = images[0].height;
int imagewidth = images[0].width;
int images_tall = 5;
int images_wide = 5;
int channels = images[0].channels;
int totalwidth = imagewidth * images_wide;
int totalheight = imageheight * images_tall;
unsigned char *image2 = new unsigned char[totalwidth * totalheight * channels]; //(w * imagew * height * imageh) * channels
int imageindex = 0;
for(int imgcol = 0; imgcol < images_tall; imgcol++) //Image columns 0-5
for (int row =0; row < imagewidth; row++) //Row0 - 120;
for (int img = 0; img < images_wide; img++) //Image Rows 0-5
for (int col = 0; col < imagewidth; col++) //Col 0-120
for (int channel = 0; channel < channels; channel++) //Channel 0-4
{
image2[imageindex++] = images[(imgcol * images_wide) + img].image[(row * imagewidth) + col];
}
问题是,当我尝试使用机顶盒保存图像时:
stbi_write_png("svg4.png", totalwidth, totalheight, channels, image2, channels * totalwidth);
如果我将其保存为单个频道,则可以正确显示:
但是,如果我尝试保存更多频道,则保存1个频道:
现在,这也假设我进行预成型时:
stbi_load(wholefoldername.c_str(),&width,&height,&channels,targchannel);
将“ targchannel”值设置为我想要的频道数。
在for循环中我哪里出错了?对我来说似乎正确。