nuclear@4: #include nuclear@4: #include nuclear@4: #include nuclear@4: #include nuclear@4: nuclear@5: #define RODATA_STR "__attribute__((section(\".rodata\")))" nuclear@5: nuclear@4: unsigned char *read_image(int *xsz, int *ysz, int *maxval); nuclear@4: nuclear@5: int main(int argc, char **argv) nuclear@4: { nuclear@5: int i, j, width, height, xtiles, ytiles, maxval; nuclear@5: unsigned char *pixels, *tiles, *src, *dest; nuclear@5: const char *prefix = "img_"; nuclear@5: nuclear@5: if(argv[1]) { nuclear@5: prefix = argv[1]; nuclear@5: } nuclear@4: nuclear@4: if(!(pixels = read_image(&width, &height, &maxval))) { nuclear@4: return 1; nuclear@4: } nuclear@4: fprintf(stderr, "read image %dx%d (maxval: %d)\n", width, height, maxval); nuclear@4: nuclear@5: xtiles = width / 8; nuclear@5: ytiles = height / 8; nuclear@5: /* each tile is 32 bytes, 8 rows of 4 bytes each */ nuclear@5: if(!(tiles = malloc(xtiles * ytiles * 32))) { nuclear@5: free(pixels); nuclear@5: return 1; nuclear@5: } nuclear@4: nuclear@5: /* if we have 15 as max val, we'll assume they are straight palette indices nuclear@5: * otherwise, do color quantization and palette allocation nuclear@5: */ nuclear@5: if(maxval == 15) { nuclear@5: /* just drop green/blue components in-place, keep red as index */ nuclear@5: dest = src = pixels; nuclear@5: for(i=0; i 0) { nuclear@5: printf(",\n"); nuclear@5: } else { nuclear@5: putchar('\n'); nuclear@5: } nuclear@5: printf("\t{"); nuclear@5: for(j=0; j<32; j++) { nuclear@5: if(j == 0) { nuclear@5: printf("%u", (unsigned int)*src++); nuclear@5: } else { nuclear@5: printf(", %u", (unsigned int)*src++); nuclear@5: } nuclear@5: } nuclear@5: printf(" }"); nuclear@5: } nuclear@5: printf("\n};\n"); nuclear@5: nuclear@5: nuclear@4: return 0; nuclear@4: } nuclear@4: nuclear@4: char *clean_line(char *s) nuclear@4: { nuclear@4: char *tmp; nuclear@4: nuclear@4: while(*s && isspace(*s)) { nuclear@4: ++s; nuclear@4: } nuclear@4: nuclear@4: tmp = strchr(s, '#'); nuclear@4: if(tmp) *tmp = 0; nuclear@4: tmp = strchr(s, '\n'); nuclear@4: if(tmp) *tmp = 0; nuclear@4: tmp = strchr(s, '\r'); nuclear@4: if(tmp) *tmp = 0; nuclear@4: nuclear@4: return s; nuclear@4: } nuclear@4: nuclear@4: unsigned char *read_image(int *width, int *height, int *maxval) nuclear@4: { nuclear@4: char buf[256]; nuclear@4: int i, xsz, ysz, hdrline = 0; nuclear@4: unsigned char *pixels; nuclear@4: nuclear@4: while(hdrline < 3 && fgets(buf, sizeof buf, stdin)) { nuclear@4: char *line = clean_line(buf); nuclear@4: if(!*line) continue; nuclear@4: nuclear@4: switch(hdrline) { nuclear@4: case 0: nuclear@4: if(strcmp(line, "P6") != 0) { nuclear@4: goto inval; nuclear@4: } nuclear@4: break; nuclear@4: nuclear@4: case 1: nuclear@4: if(sscanf(line, "%d %d", &xsz, &ysz) != 2) { nuclear@4: goto inval; nuclear@4: } nuclear@4: break; nuclear@4: nuclear@4: case 2: nuclear@4: if(sscanf(line, "%d", maxval) != 1) { nuclear@4: goto inval; nuclear@4: } nuclear@4: break; nuclear@4: } nuclear@4: ++hdrline; nuclear@4: } nuclear@4: nuclear@4: if(!(pixels = malloc(xsz * ysz * 3))) { nuclear@4: fprintf(stderr, "failed to allocate image: %dx%d\n", xsz, ysz); nuclear@4: return 0; nuclear@4: } nuclear@4: nuclear@4: for(i=0; i