+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include HDRFILE
+
+#define NBPL 5
+
+int main(int argc, char **argv)
+{
+ int i, j, k, palsz;
+ FILE *fp;
+ char *buf;
+ unsigned char *src;
+
+ if(!argv[1]) {
+ fprintf(stderr, "missing argument: output name\n");
+ return 1;
+ }
+
+ if(!(buf = malloc(strlen(argv[1]) + 4))) {
+ perror("malloc failed");
+ return 1;
+ }
+ sprintf(buf, "%s.img", argv[1]);
+
+ if(!(fp = fopen(buf, "wb"))) {
+ perror("failed to open output file");
+ return 1;
+ }
+
+ src = header_data;
+
+ printf("image: %dx%d\n", width, height);
+ for(i=0; i<height; i++) {
+ for(j=0; j<NBPL; j++) {
+ int bit = j;
+ unsigned char out = 0;
+ for(k=0; k<width; k++) {
+ out = (out << 1) | ((src[k] >> bit) & 1);
+ if((k & 7) == 7) {
+ fputc(out, fp);
+ }
+ }
+ }
+ src += width;
+ }
+ fclose(fp);
+
+ sprintf(buf, "%s.pal", argv[1]);
+ if(!(fp = fopen(buf, "wb"))) {
+ perror("failed to open palette output file");
+ return 1;
+ }
+
+ palsz = 1 << NBPL;
+
+ printf("palette:\n");
+ for(i=0; i<palsz; i++) {
+ int r = header_data_cmap[i][0] >> 4;
+ int g = header_data_cmap[i][1] >> 4;
+ int b = header_data_cmap[i][2] >> 4;
+ unsigned int col = ((r & 0xf) << 8) | ((g & 0xf) << 4) | (b & 0xf);
+ printf("0x%03x\n", col);
+ fputc(col >> 8, fp);
+ fputc(col, fp);
+ }
+ fclose(fp);
+
+ return 0;
+}