更新的答案
如果您想在不使用的情况下解码RGB565并写入NetPBM格式的PNM文件
ImageMagick
,您可以执行以下操作:
#include <stdint.h> /* for uint8_t */
#include <stdio.h> /* for printf */
/* tell compiler what those GIMP types are */
typedef int guint;
typedef uint8_t guint8;
#include <YOURGIMPIMAGE>
int main(){
int w = gimp_image.width;
int h = gimp_image.height;
int i;
uint16_t* RGB565p = (uint16_t*)&(gimp_image.pixel_data);
/* Print P3 PNM header on stdout */
printf("P3\n%d %d\n255\n",w, h);
/* Print RGB pixels, ASCII, one RGB pixel per line */
for(i=0;i<w*h;i++){
uint16_t RGB565 = *RGB565p++;
uint8_t r = (RGB565 & 0xf800) >> 8;
uint8_t g = (RGB565 & 0x07e0) >> 3;
uint8_t b = (RGB565 & 0x001f) << 3;
printf("%d %d %d\n", r, g ,b);
}
}
编译方式:
clang example.c
跑步时使用:
./a.out > result.pnm
除了你的样本图像之外,我还没有对它进行过广泛的测试,所以你可能想用一些红色、绿色、蓝色和灰色来制作一个测试图像,以确保我所有的小细节都是正确的。
原始答案
恢复图像的最简单方法是
ImageMagick
去做吧。
所以,取你的C文件,添加一个
main()
它只需写入304x98x2字节,起始于
&(example.pixel_data)
到stdout:
使用以下内容编译它:
clang example.c -o program # or with GCC
gcc example.c -o program
然后运行它,写入的文件
ImageMagick
具有
./program > image.bin
告诉我
ImageMagick
它的大小、类型、位置以及您想要的结果:
magick -size 304x98 RGB565:image.bin result.png
我对下面的代码做了一个不太彻底的快速测试,它对我用GIMP生成的图像效果很好。请注意,它不处理alpha/透明度,但如果需要,可以添加它。另存为
program.c
:
#include <unistd.h> /* for write() */
#include <stdint.h> /* for uint8_t */
/* tell compiler what those GIMP types are */
typedef int guint;
typedef uint8_t guint8;
<PASTE YOUR GIMP FILE HERE>
int main(){
/* Work out how many bytes to write */
int nbytes = example.width * example.height * 2;
/* Write on stdout for redirection to a file - may need to reopen in binary mode if on Windows */
write(1, &(example.pixel_data), nbytes);
}
如果我用你通过谷歌硬盘提供的文件运行这个,我会得到: