Currently I have a ray-tracing program that produces RGB values in floating point (from 0 to 1) or short
values (from 0 to 255). (The relevant code is in ImageTools.cpp and ImageTools.hpp.)
目前我有一个光线追踪程序,可以产生浮点(从0到1)或短值(从0到255)的RGB值。 (相关代码在ImageTools.cpp和ImageTools.hpp中。)
Currently I can produce images by writing PPM, which is horrendously large and bloated.
目前我可以通过编写PPM来制作图像,PPM非常庞大且臃肿。
How can I write a PNG image from one of these arrays?
如何从其中一个阵列写入PNG图像?
I prefer not to add a dependency on anything that's not commonly installed on most Linux systems, as I have 0 dependencies so far.
我不想在大多数Linux系统上没有通常安装的任何东西上添加依赖项,因为到目前为止我有0个依赖项。
3 个解决方案
#1
3
You could use Boost GIL
你可以使用Boost GIL
If you have the raw image data (or can produce it in some way):
如果您有原始图像数据(或者可以以某种方式生成):
unsigned char r[width * height]; // red
unsigned char g[width * height]; // green
unsigned char b[width * height]; // blue
You can write a .png file in this way:
你可以这样写一个.png文件:
#include <boost/gil/extension/io/png_io.hpp>
boost::gil::rgb8c_planar_view_t view = boost::gil::planar_rgb_view(width, height, r, g, b, width);
boost::gil::png_write_view("out.png", view);
If the image also contains an alpha channel
如果图像还包含alpha通道
unsigned char a[width * height];
then
#include <boost/gil/extension/io/png_io.hpp>
boost::gil::rgba8c_planar_view_t view = boost::gil::planar_rgba_view(width, height, r, g, b, a, width);
boost::gil::png_write_view("out.png", view);
Remember to link your program with -lpng
请记住将您的程序与-lpng链接
Also take a look at boost gil create image
另外看看boost gil创建图像
#2
2
I would keep the application simple and just continue to write a PPM file, then use an existing converter such as ImageMagick (convert), GraphicsMagick (gm convert), or pnmtopng, which are all open source and free to use, or any of a number of other free or proprietary converters.
我会保持应用程序简单,只需继续编写PPM文件,然后使用现有的转换器,如ImageMagick(转换),GraphicsMagick(gm转换)或pnmtopng,它们都是开源的,可以免费使用,或任何一个其他免费或专有转换器的数量。
You can easily write PPM files in a raw format ("P6") that takes up much less file space and is faster to write and read than the ASCII "P3" format. You don't even need to store the PPM as a file; just pipe it to pnmtopng:
您可以轻松地以原始格式(“P6”)编写PPM文件,这样可以占用更少的文件空间,并且比ASCII“P3”格式更快地进行写入和读取。您甚至不需要将PPM存储为文件;把它管道到pnmtopng:
your_application | pnmtopng > out.png
pnmtopng is part of the netpbm library which I assume you are already using. It depends upon libpng and zlib, but you've probably already got those as well.
pnmtopng是netpbm库的一部分,我假设你已经在使用它了。这取决于libpng和zlib,但你可能已经有了这些。
#3
1
I'm assuming you don't want to implement the standard yourself and are hence willing to compromise by introducing some dependencies in your project. If so, libpng is commonly installed on most linux systems. http://www.libpng.org/pub/png/libpng.html
我假设你不想自己实现标准,因此愿意通过在项目中引入一些依赖来妥协。如果是这样,libpng通常安装在大多数Linux系统上。 http://www.libpng.org/pub/png/libpng.html
You might need to install the devel package to pick up the headers, though this was included on my stock CentOS 6.2 machine.
您可能需要安装devel软件包以获取标头,尽管这已包含在我的CentOS 6.2机器上。
#1
3
You could use Boost GIL
你可以使用Boost GIL
If you have the raw image data (or can produce it in some way):
如果您有原始图像数据(或者可以以某种方式生成):
unsigned char r[width * height]; // red
unsigned char g[width * height]; // green
unsigned char b[width * height]; // blue
You can write a .png file in this way:
你可以这样写一个.png文件:
#include <boost/gil/extension/io/png_io.hpp>
boost::gil::rgb8c_planar_view_t view = boost::gil::planar_rgb_view(width, height, r, g, b, width);
boost::gil::png_write_view("out.png", view);
If the image also contains an alpha channel
如果图像还包含alpha通道
unsigned char a[width * height];
then
#include <boost/gil/extension/io/png_io.hpp>
boost::gil::rgba8c_planar_view_t view = boost::gil::planar_rgba_view(width, height, r, g, b, a, width);
boost::gil::png_write_view("out.png", view);
Remember to link your program with -lpng
请记住将您的程序与-lpng链接
Also take a look at boost gil create image
另外看看boost gil创建图像
#2
2
I would keep the application simple and just continue to write a PPM file, then use an existing converter such as ImageMagick (convert), GraphicsMagick (gm convert), or pnmtopng, which are all open source and free to use, or any of a number of other free or proprietary converters.
我会保持应用程序简单,只需继续编写PPM文件,然后使用现有的转换器,如ImageMagick(转换),GraphicsMagick(gm转换)或pnmtopng,它们都是开源的,可以免费使用,或任何一个其他免费或专有转换器的数量。
You can easily write PPM files in a raw format ("P6") that takes up much less file space and is faster to write and read than the ASCII "P3" format. You don't even need to store the PPM as a file; just pipe it to pnmtopng:
您可以轻松地以原始格式(“P6”)编写PPM文件,这样可以占用更少的文件空间,并且比ASCII“P3”格式更快地进行写入和读取。您甚至不需要将PPM存储为文件;把它管道到pnmtopng:
your_application | pnmtopng > out.png
pnmtopng is part of the netpbm library which I assume you are already using. It depends upon libpng and zlib, but you've probably already got those as well.
pnmtopng是netpbm库的一部分,我假设你已经在使用它了。这取决于libpng和zlib,但你可能已经有了这些。
#3
1
I'm assuming you don't want to implement the standard yourself and are hence willing to compromise by introducing some dependencies in your project. If so, libpng is commonly installed on most linux systems. http://www.libpng.org/pub/png/libpng.html
我假设你不想自己实现标准,因此愿意通过在项目中引入一些依赖来妥协。如果是这样,libpng通常安装在大多数Linux系统上。 http://www.libpng.org/pub/png/libpng.html
You might need to install the devel package to pick up the headers, though this was included on my stock CentOS 6.2 machine.
您可能需要安装devel软件包以获取标头,尽管这已包含在我的CentOS 6.2机器上。