nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #ifndef WIN32 nuclear@0: #include nuclear@0: #else nuclear@0: #include nuclear@0: #endif nuclear@0: #include nuclear@0: nuclear@0: struct palentry { nuclear@0: float r, g, b; nuclear@0: struct palentry *next; nuclear@0: }; nuclear@0: nuclear@0: int load_palette(const char *fname); nuclear@0: int proc_tile(const char *fname); nuclear@0: float find_nearest(float r, float g, float b); nuclear@0: nuclear@0: struct palentry *palette; nuclear@0: int palsize; nuclear@0: nuclear@0: int main(int argc, char **argv) nuclear@0: { nuclear@0: int i; nuclear@0: nuclear@0: for(i=1; i> 16) & 0xff; nuclear@0: g = (val >> 8) & 0xff; nuclear@0: b = val & 0xff; nuclear@0: } else { nuclear@0: fprintf(stderr, "unrecognized line \"%s\" in palette file: %s\n", line, fname); nuclear@0: goto fail; nuclear@0: } nuclear@0: nuclear@0: if(!(pent = malloc(sizeof *pent))) { nuclear@0: perror("failed to allocate palette entry"); nuclear@0: goto fail; nuclear@0: } nuclear@0: pent->r = (float)r / 255.0; nuclear@0: pent->g = (float)g / 255.0; nuclear@0: pent->b = (float)b / 255.0; nuclear@0: pent->next = 0; nuclear@0: nuclear@0: if(head) { nuclear@0: tail->next = pent; nuclear@0: tail = pent; nuclear@0: } else { nuclear@0: head = tail = pent; nuclear@0: } nuclear@0: nent++; nuclear@0: } nuclear@0: nuclear@0: /* flatten */ nuclear@0: if(!(newpal = malloc(nent * sizeof *newpal))) { nuclear@0: fprintf(stderr, "failed to allocate %d palette entries: %s\n", nent, strerror(errno)); nuclear@0: goto fail; nuclear@0: } nuclear@0: palette = newpal; nuclear@0: palsize = nent; nuclear@0: nuclear@0: while(head) { nuclear@0: struct palentry *ent = head; nuclear@0: head = head->next; nuclear@0: nuclear@0: *newpal++ = *ent; nuclear@0: free(ent); nuclear@0: } nuclear@0: nuclear@0: printf("loaded palette: %s (%d colors)\n", fname, nent); nuclear@0: nuclear@0: fclose(fp); nuclear@0: return 0; nuclear@0: nuclear@0: fail: nuclear@0: fclose(fp); nuclear@0: while(head) { nuclear@0: void *tmp = head; nuclear@0: head = head->next; nuclear@0: free(tmp); nuclear@0: } nuclear@0: return -1; nuclear@0: } nuclear@0: nuclear@0: int proc_tile(const char *fname) nuclear@0: { nuclear@0: int i; nuclear@0: int xsz, ysz; nuclear@0: unsigned char *pixels, *pptr; nuclear@0: char *outfname, *cptr; nuclear@0: FILE *fp; nuclear@0: const char *magic = "TILEIMAG"; nuclear@0: nuclear@0: outfname = alloca(strlen(fname) + 4); nuclear@0: strcpy(outfname, fname); nuclear@0: if((cptr = strrchr(outfname, '.'))) { nuclear@0: *cptr = 0; nuclear@0: } nuclear@0: strcat(outfname, ".til"); nuclear@0: nuclear@0: printf("processing tile: %s -> %s\n", fname, outfname); nuclear@0: nuclear@0: if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGB24))) { nuclear@0: fprintf(stderr, "failed to load source tile image: %s\n", fname); nuclear@0: return -1; nuclear@0: } nuclear@0: if(!(fp = fopen(outfname, "wb"))) { nuclear@0: fprintf(stderr, "failed to open output tile image for writing: %s: %s\n", outfname, strerror(errno)); nuclear@0: img_free_pixels(pixels); nuclear@0: return -1; nuclear@0: } nuclear@0: /* write header */ nuclear@0: fwrite(magic, 1, 8, fp); nuclear@0: fwrite(&xsz, sizeof xsz, 1, fp); nuclear@0: fwrite(&ysz, sizeof ysz, 1, fp); nuclear@0: nuclear@0: pptr = pixels; nuclear@0: for(i=0; i