aboutsummaryrefslogtreecommitdiff
path: root/src/images/ImageEncoder.c
blob: 0ea874bc3aaffd12ba26e0c1dc284e88397ccae2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/***********************************************************
ImageEncoder: Standalone program to convert any image into
        a static character array (each byte represented in
        hexadecimal) in a header file.  Use this for any
        images that you want to embed in the code so
        that it can run standalone (ie. when you are 
        disconnected from the network).  Currently used
        to spew out the wwwcactuscodeorg.jpg image used
        in thorn_HTTPD through the WebImage.cc extension
        to the HTTPD.
Compilation:
        $(CC) -o ImageEncoder ImageEncoder.c
Running:
        ImageEncoder <inputimage> <outputheader>
        This will take the input binary file and create a
        header file (something.h) which has the image 
        encoded as a static character array with the
        same name as the input image file (minus the extension.
**************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc,char *argv[]){
  char *s;
  char *varname=0;
  int c,counter=0;
  FILE *in,*out;
  if(argc<2) {
    fprintf(stderr,"Usage: %s <inputimage> <outputheader>\n",argv[0]);
    fprintf(stderr,"\tor %s <inputimage>   output to stdout\n",argv[0]);
    exit(0);
  }
  in = fopen(argv[1],"r");
  if(argc<3)
    out=stdout;
  else
    out = fopen(argv[2],"w");
  s = strrchr(argv[1],'/');
  if(s){
    varname = (char*)malloc(strlen(s)+1);
    strcpy(varname,s); /* strip */
  }
  else{
    varname = (char*)malloc(strlen(argv[1])+1);
    strcpy(varname,argv[1]);
  }
  /* printf("VarName=[%s]\n",varname); */
  s = strrchr(varname,'.'); 
  if(s){ /* strip .<ext> */
    *s='\0';
  }
  
  /* Now actually start writing stuff */
  fprintf(out,"#ifndef __%s_H_\n#define __%s_H_\n",
          varname,varname);
  fprintf(out,"\n\n");
  fprintf(out,"static unsigned char %s[]={\n",varname);
  /* inefficient, but it works */
  c=fgetc(in);
  while(c!=EOF){
    int tmp=c;
    c=fgetc(in);
    if(c!=EOF)
      fprintf(out,"0x%02X,",tmp);
    else
      fprintf(out,"0x%02X};",tmp);
    counter++;
    if(!(counter%12)) 
      fprintf(out,"\n\t");
  }
  fprintf(out,"\n\n#endif /* __%s_H_ */\n",varname);
  fclose(in);
  if(argc>2) 
    fclose(out);
  if(varname) free(varname);
}