2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU Lesser General Public License as published
4 * by the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 // Convert input files to .h files consisting of hex arrays
33 int main(int argc
, char *argv
[])
35 if(argc
< 2) return 1;
37 for(argc
--; argc
> 0; argc
--)
41 char variable
[1024], header_fn
[1024], output_fn
[1024], *suffix
, *prefix
;
43 int bytes_per_row
= 16;
44 char row
[1024], byte
[1024], character
;
48 in
= fopen(argv
[argc
], "rb");
51 stat(argv
[argc
], &st
);
52 total_bytes
= (long)st
.st_size
;
54 // Replace . with _png and append .h to filename
55 strcpy(output_fn
, argv
[argc
]);
56 suffix
= strrchr(output_fn
, '.');
57 if(suffix
) *suffix
= '_';
58 strcat(output_fn
, ".h");
60 out
= fopen(output_fn
, "w");
68 // Strip leading directories for variable and header
69 prefix
= strrchr(output_fn
, '/');
75 strcpy(header_fn
, prefix
);
76 for(i
= 0; i
< strlen(header_fn
); i
++)
78 // Replace leading digits
79 if(i
== 0 && isdigit(header_fn
[i
]))
82 for(k
= strlen(header_fn
); k
>= 0; k
--)
84 header_fn
[k
+ 1] = header_fn
[k
];
89 // Replace . with _ for header
90 if(header_fn
[i
] == '.')
93 header_fn
[i
] = toupper(header_fn
[i
]);
96 // Strip .h for variable
97 strcpy(variable
, prefix
);
98 suffix
= strrchr(variable
, '.');
99 if(suffix
) *suffix
= 0;
101 // Replace leading digits
102 if(isdigit(variable
[0]))
105 for(k
= strlen(variable
); k
>= 0; k
--)
107 variable
[k
+ 1] = variable
[k
];
113 fprintf(out
, "#ifndef %s\n"
116 "static unsigned char %s[] = \n{\n",
121 // Print the size of the file
122 fprintf(out
, "\t0x%02x, 0x%02x, 0x%02x, 0x%02x, \n",
123 (unsigned long)(total_bytes
& 0xff000000) >> 24,
124 (unsigned long)(total_bytes
& 0xff0000) >> 16,
125 (unsigned long)(total_bytes
& 0xff00) >> 8,
126 (unsigned long)(total_bytes
& 0xff));
128 while(total_bytes
> 0)
131 for(i
= 0; i
< bytes_per_row
&& total_bytes
> 0; i
++)
134 sprintf(byte
, "0x%02x", fgetc(in
));
136 sprintf(byte
, ", 0x%02x", fgetc(in
));
141 sprintf(byte
, ", \n");
145 fprintf(out
, "%s%s", row
, byte
);
148 fprintf(out
, "};\n\n#endif\n");