我已经使用 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,
"\206\061\206\061..... }
有没有办法在 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/透明度,但可以在必要时添加。将其保存为 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 云端硬盘提供的文件运行此程序,我会得到:
关于c++ - 将 C 源图像转储转换为原始图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70215743/