[stb image library,如何使用调用stbi_image_write函数将矩形元素的矩形数组(每个元素的值为0或1)转换为单色png图像(其中1表示彩色像素)?这是我们到目前为止的代码:
using namespace std;
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
/* ... */
const uint32_t BITMAP_SIZE = height * width;
uint8_t* bitmap = new uint8_t[BITMAP_SIZE];
for (int i = 0; i < BITMAP_SIZE; i++) // clear the bitmap
bitmap[0] = 0;
/* write 1s to some elements of bitmap */
constexpr int CHANNELS = 4; // indexed (really 1 or 0)
string filename = "my_image.png";
stbi_write_png(/* what parameters should be passed here? */);
delete [] bitmap;
将参数传递给stbi_write_png以获得上述所需输出的正确方法是什么?
您需要CHANNELS = 1(因为每个像素1个字节)。