我用 GIMP 創建了一個 C-Source 影像轉儲,如下所示:
/* 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 中再次讀取它以取回原始影像?因為這似乎不可能。或者是否存在可以進行這種反向轉換的工具?
EDITED 根據一些建議,我嘗試撰寫一個簡單的 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>
使用命名空間標準;
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 所建議的,也許我需要使用正確的解碼來操縱像素,但我不知道該怎么做,有人有其他建議嗎?
我得到的影像確實扭曲了:

uj5u.com熱心網友回復:
更新答案
如果你想在不使用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()簡單地將 304x98x2 位元組開始寫入&(example.pixel_data)標準輸出:
用類似的東西編譯它:
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 Drive 提供的檔案運行它,我會得到:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/374901.html
