简体   繁体   English

如何快速将未签名的char数组保存到图像文件?

[英]How to quickly save an unsigned char array to image file?

I have an array of unsigned char array which is actually a RGBA raw image. 我有一个无符号字符数组数组,它​​实际上是RGBA原始图像。 How to export it into the image file? 如何将其导出到图像文件? Any image file format is OK. 任何图像文件格式都可以。

You can write it straight out as RGBA in binary. 您可以直接将其写为RGBA二进制文件。 Then use ImageMagick, which is installed on most Linux distros, and available for OSX and Windows to convert it to PNG or something more common: 然后使用大多数Linux发行版中安装的ImageMagick,它可用于OSX和Windows,将其转换为PNG或更常见的东西:

convert -size 300x400 -depth 8 binary.rgba result.png

You will need to tell ImageMagick the dimensions of the image as above because they are obviously not embedded within a raw file like they would be in a JPEG or PNG with a header. 您将需要像上面那样告诉ImageMagick图像的尺寸,因为它们显然没有像嵌入带有标题的JPEG或PNG中那样嵌入在原始文件中。

If the filename you choose does not end in .rgba , you will need to prefix it with RGBA: like this 如果您选择的文件名不以.rgba ,则需要使用RGBA:作为前缀RGBA:

convert -size ... RGBA:something.bin result.png

If you are using OSX, I personally think homebrew is the easiest way to install ImageMagick... 如果您使用的是OSX,我个人认为homebrew是安装ImageMagick的最简单方法。

brew install imagemagick

I think your best bet is just saving it to a .bmp file, because basically bitmap is a (upside down) array of rgba values. 我认为最好的选择是将其保存到.bmp文件中,因为基本上位图是rgba值的一个(倒置)数组。 So you can just write a simple header and then write it line by line to the file. 因此,您可以编写一个简单的标头,然后将其逐行写入文件。

For a header you'd have to write the file header first, and after that there is the DIB header. 对于标头,您必须首先编写文件标头,然后再写DIB标头。 There are multiple options for this, but i'd suggest the simple one . 对此有多种选择,但我建议使用简单的一种

The bitmap format has support for a lot of fancy stuff, but most of it is optional and for your propose not necessary. 位图格式支持很多奇特的东西,但是大多数都是可选的,您的建议不是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM