将 C 源图像转储转换为原始图像

Convert C-Source image dump into original image

我使用 GIMP 创建了一个 C 源图像转储,如下所示:

/* GIMP RGBA C-Source image dump (example.c) */

static const struct {
  guint      width;
  guint      height;
  guint      bytes_per_pixel; /* 2:RGB16, 3:RGB, 4:RGBA */ 
  guint8     pixel_data[304 * 98 * 2 + 1];
} example= {
  304, 98, 2,
  "6161..... }

有没有办法在 GIMP 中再次读取它以恢复原始图像?因为这似乎不可能。 或者是否存在可以进行这种反向转换的工具?

已编辑

根据一些建议,我尝试编写一个简单的 C 程序来进行反向转换,结果与在互联网上找到的另一个代码非常相似,但两者都不起作用:

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "imgs_press.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

using namespace std;

int main(int argc, char** argv) {
    int fd;
    char *name = "orignal_img.pnm";
    fd = open(name, O_WRONLY | O_CREAT, 0644);
    if (fd == -1) {
        perror("open failed");
        exit(1);
    }

    if (dup2(fd, 1) == -1) {
        perror("dup2 failed"); 
        exit(1);
    }

    // file descriptor 1, i.e. stdout, now points to the file
    // "helloworld" which is open for writing
    // You can now use printf which writes specifically to stdout

  printf("P2\n");
  printf("%d %d\n", press_high.width, press_high.height);
  for(int x=0; x<press_high.width * press_high.height * 2; x++) {
    printf("%d ", press_high.pixel_data[x]);
  }
}

根据 n-1-8e9-wheres-my-share-m 的建议,也许我需要使用正确的解码来操纵像素,但我不知道该怎么做,有人有其他建议吗?

我得到的图像确实失真了:

更新答案

如果你想在不使用ImageMagick的情况下解码RGB565并写入NetPBM格式的PNM文件,你可以这样做:

#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(),它只是将从 &(example.pixel_data) 开始的 304x98x2 字节写入标准输出:

用类似的东西编译它:

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/transparency 但可以在必要时添加。另存为 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);
}

如果我 运行 使用您通过 Google 驱动器提供的文件,我得到: