Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// show an image in the terminal using ascii colors
// demonstrates raw pixel access
// (only works on certain image file types, though)
#include <stdio.h>
#include <FreeImage.h>
int meta(FIBITMAP *dib) {
FITAG *tag = NULL;
FIMETADATA *mdhandle = NULL;
mdhandle = FreeImage_FindFirstMetadata(FIMD_EXIF_MAIN, dib, &tag);
if(mdhandle) {
do {
// process the tag
printf("%s\n", FreeImage_GetTagKey(tag));
// ...
} while(FreeImage_FindNextMetadata(mdhandle, &tag));
FreeImage_FindCloseMetadata(mdhandle);
} else {
printf("no metadata found for %p \n", dib);
}
}
int dump (FIBITMAP *dib) {
unsigned width = FreeImage_GetWidth(dib);
unsigned height = FreeImage_GetHeight(dib);
unsigned pitch = FreeImage_GetPitch(dib);
unsigned line = FreeImage_GetLine(dib);
int x, y;
FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(dib);
BYTE *bits = (BYTE*)FreeImage_GetBits(dib);
// test pixel access avoiding scanline calculations
// to speed-up the image processing
int bytespp = FreeImage_GetLine(dib) / FreeImage_GetWidth(dib);
if(image_type == FIT_BITMAP) {
for(y = height-1; y >= 0; y--) {
printf("\n");
for(x = 0; x < width; x++) {
int r,g,b;
if((bytespp == 3) || (bytespp == 4)) {
r = bits[y*pitch + x*bytespp + FI_RGBA_RED];
g = bits[y*pitch + x*bytespp + FI_RGBA_GREEN];
b = bits[y*pitch + x*bytespp + FI_RGBA_BLUE];
} else if ((bytespp == 1)) {
r = g = b = bits[y*pitch + x*bytespp];
} else {
printf("unknown bpp %d\n", bytespp);
return -1;
}
// print a full-blown square with the right color (most
// terminals should support this).
printf("\x1b[38;2;%d;%d;%dm█", r,g,b);
}
}
} else {
printf("unknown image type\n");
}
}
int main(int argc, char** argv) {
if(argc != 2) {
printf("missing filename argument\n");
return -1;
}
char *filename = argv[1];
FREE_IMAGE_FORMAT fif = FreeImage_GetFIFFromFilename(filename);
FIBITMAP *dib = FreeImage_Load(fif, filename, 0);
printf("loaded %p\n", dib);
if(dib) {
printf("%p is %d x %d x %d\n", dib, FreeImage_GetWidth(dib), FreeImage_GetHeight(dib), FreeImage_GetBPP(dib));
meta(dib);
dump(dib);
FreeImage_Unload(dib);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.