使用 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 图像库,如何使用对 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 个字节(。
stbi_write_png(filename.c_str(), width, height,
CHANNELS, bitmap, width * CHANNELS);
相关文章:
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 基于字节数组生成静态范围整数值
- 将字节数组转换为带有字节序问题的指针
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- 字节数组初始化会导致 DirectX 崩溃
- 使用 swig 追加到字节数组
- C++ 替换字节数组中项的顺序
- QDataStream 读取和写入的字节数比 QFile::length() 报告要多
- 如何从保存在 Java 中C++的字节数组中读取数字?
- 如何在C++(Arduino)中将浮点数组转换为字节数组
- 将字节数组转换为多维数组
- 为什么 sizeof 在 C++ 中给出不正确的字节数?
- 如何将带有空字符的字节数组馈送到 std::iostream 中?
- C++字节数组到整数
- 将字符串转换为无符号字符数组/字节数组
- 用于 progmem 的C++和头文件压缩的 Web 文件字节数组
- mmap 中的长度是字节数还是页数?
- 如果我向一个12字节的缓冲区写入的字节数少于12,会发生什么情况
- 从 UTF-8 字节数组创建字符串?
- ReadFile() 读取的字节数少于其参数中的指定值