#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
  FILE *in, *out;
  int i;
  unsigned char byte;
  struct stat sbuf;
  char *outname;

  if (argc < 2) {
    printf("usage: %s <img file>\n", argv[0]);
    exit(1);
  }

  if (stat(argv[1], &sbuf) < 0) {
    perror("stat");
    exit(1);
  }
  
  in = fopen(argv[1], "r");
  if (!in) {
    perror(argv[1]);
    exit(1);
  }

  outname = malloc(strlen(argv[1]) + 3);
  sprintf(outname, "%s.c", argv[1]);
  out = fopen(outname, "w");
  if (!out) {
    perror(outname);
    exit(1);
  }

  fprintf(out, "#define IMG_SIZE %d\n", sbuf.st_size);
  fprintf(out, "unsigned char img_data[IMG_SIZE] = {\n  ");
  i=0;
  while (fread(&byte, 1, 1, in) > 0) {
    fprintf(out, "0x%02x, ", (unsigned int)byte);
    i++;
    if (i == 10) {
      fprintf(out, "\n  ");
      i = 0;
    }
  }
  fprintf(out, "};\n");
  fclose(out);
  fclose(in);
  return 0;
}
