使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
Using stbi_write_png, how do you convert a rectangular byte-array of 0s and 1s to a monochrome png file?
使用 stb image library,如何将矩形字节数组(每个元素的值为 0 或 1)转换为单色 png 图像,其中 1 代表彩色像素,使用调用stbi_image_write 函数?这是我们目前的代码:
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 个字节)。
stbi_write_png(filename.c_str(), width, height,
CHANNELS, bitmap, width * CHANNELS);
使用 stb image library,如何将矩形字节数组(每个元素的值为 0 或 1)转换为单色 png 图像,其中 1 代表彩色像素,使用调用stbi_image_write 函数?这是我们目前的代码:
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 个字节)。
stbi_write_png(filename.c_str(), width, height,
CHANNELS, bitmap, width * CHANNELS);