1

I am writing a program that creates an image file. I want the "original" image to be hard-coded in the program's binary.

I was thinking...

char image[] = {
#include"image.jpg"
}

But I need to somehow convert the image into a format that can be #included into a c file as a char array?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
jsj
  • 9,019
  • 17
  • 58
  • 103

3 Answers3

10

Ok, use the unix tool xxd to create a c char array import of a given binary file, Like so:

$ xxd -i imgfile.dat > imgfile.h

This should produce output like:

unsigned char imgfile[] = {
  0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x0a
};
unsigned int imgfile_len = 12;

See also: "#include" a text file in a C program as a char[]

Community
  • 1
  • 1
IanNorton
  • 7,145
  • 2
  • 25
  • 28
2

You could write a little program that takes a jpg file as input and outputs a C initializer. Then #include that file.

#include <stdio.h>
int main(int argc, char **argv)
{
    int ch;
    int count = 0;
    printf("static const char array[] = {\n");
    while ((ch = getchar()) != -1) {
       printf("0x%02X, ", ch);
       ++count;
       if ((count % 16) == 0)
           printf("\n");
   }
   printf("\n};\n#define ARRAY_SIZE %d\n", count);
}

Compile the file (call it initialzer.c or something) and do:

./initializer <pic.jpg >jpeg.h

You could get fancy and make the array name configurable if you want.

sidyll
  • 57,726
  • 14
  • 108
  • 151
Richard Pennington
  • 19,673
  • 4
  • 43
  • 72
  • indeed, but how do I convert a jpeg into { 'a', 'b', 'c' } ?? – jsj Feb 06 '12 at 21:45
  • 1
    Just a quick and dirty addition: `printf("static const char %s[] = {\n", (argc > 1) ? argv[1] : "array");` — but better to stop going too far :-) argc == 2 might be another alternative. – sidyll Feb 06 '12 at 22:03
  • It is too easy to get sucked in. :-) – Richard Pennington Feb 06 '12 at 22:05
  • Quite true! By the way, markdown ate your pipes. I took the liberty to edit it – sidyll Feb 06 '12 at 22:08
  • You don't really need `#define ARRAY_SIZE` at the end. This is actually an array, you can use `sizeof`. – Chris Lutz Feb 06 '12 at 22:38
  • @ChrisLutz True, but I had that count lying around and it seemed a shame to waste it. – Richard Pennington Feb 06 '12 at 22:40
  • You _could_ always write the array to a .c file, then write a declaration of the array (with the size) to a separate .h file. Though most compilers will optimize what you have to the same thing anyway. (Also, no include guards?) – Chris Lutz Feb 06 '12 at 22:43
  • Hey, it was an on-the-fly example. I really should have been doing serious work anyway. Stack Overflow makes the mind wander. ;-) – Richard Pennington Feb 06 '12 at 22:48
0
  • Open the image file in binary read mode
  • Determine the size of the file
  • Allocate memory to store it
  • Read the file into memory using fread

Write the whole thing in a .h file and # include that file in your code.

Sufian Latif
  • 13,086
  • 3
  • 33
  • 70