Skip to content

Instantly share code, notes, and snippets.

@svenk
Created February 6, 2017 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svenk/a56a468ec25ddd187b8463f97fd118ee to your computer and use it in GitHub Desktop.
Save svenk/a56a468ec25ddd187b8463f97fd118ee to your computer and use it in GitHub Desktop.
Convert a GIMP exported Header file back to a PNM Image which can be viewed with any program
#include "The-GIMP-exported-file.h"
#include <stdio.h>
/* This code converts a GIMP C export back to a readable PNM Image. Such
header file looks like
static const struct {
unsigned int width;
unsigned int height;
unsigned int bytes_per_pixel; // 2:RGB16, 3:RGB, 4:RGBA
unsigned char pixel_data[1252 * 695 * 3 + 1];
} Image = {
1252, 695, 3,
"\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377"
...
SvenK 2017 Public Domain
*/
int main(int argc, char** argv) {
printf("P3\n");
printf("%d %d\n", Image.width, Image.height);
for(int x=0; x<1252 * 695 * 3; x++) {
printf("%d ", Image.pixel_data[x]);
}
}
@navy1978
Copy link

navy1978 commented Jul 21, 2021

Hi I'm trying to use this code to get the original Image , I used different RGBA files as input with no luck, for example this: https://github.com/dnrl/UE4/blob/ad355f1b341437dbf04c5c4c2102466b5f8d871e/Engine/Source/ThirdParty/SDL2/SDL-gui-backend/src/test/SDL_test_imageFace.c

int main(int argc, char** argv) {
printf("P4\n");
printf("%d %d\n", Image.width, Image.height);
for(int x=0; x<25 * 38 * 4; x++) {
printf("%d ", Image.pixel_data[x]);
}

}
it results in this image:
image

Any suggestions?

@svenk
Copy link
Author

svenk commented Jul 27, 2021

I guess your problem is because your image is RGBA but my code was only tested for RGB images.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment