使用stbi_write_png,如何将0和1的矩形字节数组转换为单色png文件?

问题描述 投票:0回答:1

[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以获得上述所需输出的正确方法是什么?

c++ bitmap
1个回答
1
投票

您需要CHANNELS = 1(因为每个像素1个字节)。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.