在 C++ 中转换 RGB 值数组的最简单方法是什么?到图像文件中?

发布于 2024-12-06 22:59:44 字数 166 浏览 1 评论 0原文

我一直在网上寻找一个好的、快速的解决方案,但还没有找到令我满意的东西。看起来它应该是微不足道的——只需对某个库中的函数进行一两次调用就可以了——但事实似乎并非如此。 libjpeg 和 libtiff 缺乏良好的文档,人们发布的解决方案涉及了解图像的生成方式和编写约 50 行代码。你们会如何在 C++ 中做到这一点?

I've been looking all over the net for a good, quick solution to this, and haven't found anything that has satisfied me yet. It seems like it should be trivial--just one or two calls to a function in some library and that's it--but that doesn't seem to be the case. libjpeg and libtiff lack good documentation and the solutions people have posted involve understanding how the image is produced and writing ~50 lines of code. How would you guys do this in C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

本宫微胖 2024-12-13 22:59:44

如果您想要“简单”而不是其他,请查看 stb_image_write.h

这是一个单一头文件,包括对写入 BMP、PNG 和 TGA 文件的支持。每种格式只需一次调用:

 int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
 int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
 int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);

If you want "simple" over anything else, then have a look at stb_image_write.h.

This is a single header file, which includes support for writing BMP, PNG and TGA files. Just a single call for each format:

 int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
 int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
 int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
仄言 2024-12-13 22:59:44

最简单的方法是将其保存为 Netpbm 图像。假设您的数组被打包为每像素 24 位,像素之间没有填充,那么您可以写出一个超级简单的标头,后跟二进制数据。例如:

void save_netpbm(const uint8_t *pixel_data, int width, int height, const char *filename)
{
    // Error checking omitted for expository purposes
    FILE *fout = fopen(filename, "wb");

    fprintf(fout, "P6\n%d %d\n255\n", width, height);
    fwrite(pixel_data, 1, width * height * 3, fout);

    fclose(fout);
}

The easiest way is to save it as a Netpbm image. Assuming that your array is packed into 24 bits per pixel with no padding between pixels, then you can write out a super-simple header followed by the binary data. For example:

void save_netpbm(const uint8_t *pixel_data, int width, int height, const char *filename)
{
    // Error checking omitted for expository purposes
    FILE *fout = fopen(filename, "wb");

    fprintf(fout, "P6\n%d %d\n255\n", width, height);
    fwrite(pixel_data, 1, width * height * 3, fout);

    fclose(fout);
}
我不会写诗 2024-12-13 22:59:44

在我的 Graphin 库中,您可以找到简单的函数:

bool EncodeJPGImage(image_write_function* pctl, void* streamPrm, unsigned width, unsigned height, unsigned char** rows, unsigned bpp, unsigned quality)

执行此转换。请参阅 http://code.google.com/ p/graphin/source/browse/trunk/src/imageio.cpp#412

库:http://code.google.com/p/graphin/

In my Graphin library you can find simple function:

bool EncodeJPGImage(image_write_function* pctl, void* streamPrm, unsigned width, unsigned height, unsigned char** rows, unsigned bpp, unsigned quality)

that does this conversion. See http://code.google.com/p/graphin/source/browse/trunk/src/imageio.cpp#412

The library: http://code.google.com/p/graphin/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文