$hexhex to RGB dump code

A place for your artistic side. Discuss techniques and tools for pixel art on the NES, GBC, or similar platforms.

Moderator: Moderators

Post Reply
User avatar
erana
Posts: 33
Joined: Thu Jan 27, 2011 3:08 am
Location: Europe

$hexhex to RGB dump code

Post by erana »

Some of you might find this useful. Read the example C code in a libpng source tarball to convert to png. You can also do things like gif2ascii etc.

---------8<---- start of C file hex2image.c -------->8---

/* compile with 'gcc -c hex2image.c -o hex2image.o; gcc -o hex2image hex2image.o' */

#include <stdio.h>

int main(int argc, char *argv[], char **envp)
{

if (argc < 2) {/* This returns if no argument is provided */
fprintf (stderr, "Usage : hex2image <filename>\n");
return -1;
}

FILE *fp = fopen (argv[1], "r");
unsigned int i = 0;
int n;
for (; (n = fscanf(fp, "$&x", &i)) != EOF;) {
if (n != 0) {
unsigned long int ui = (unsigned long int) i;
fprintf(stdout, "%lu", ui); /* prints RGB value; */
}
}

return 0;

}

-----8<--------end of C file hex2image.c----------->8-------
Post Reply