SDL_Color map_palette(const SDL_Color& c, int palette) { color result = map_palette(color(c.r, c.g, c.b, 255), palette); SDL_Color res = { result.r(), result.g(), result.b(), 255 }; return res; }
int main(int argc, char *argv[]) { char fn_src[500+1], fn_dst[500+1], *opt_src, *opt_dst; int fd, ft_src, ft_dst, i, stride, bytes, flag, m; GBM_ERR rc; GBMFT gbmft; GBM gbm; GBMRGB gbmrgb[0x100]; byte *data; char *map = "none"; byte remap[0x100]; double gam = 2.1, shelf = 0.0; /*...scommand line arguments:8:*/ for ( i = 1; i < argc; i++ ) { if ( argv[i][0] != '-' ) break; switch ( argv[i][1] ) { case 'm': if ( ++i == argc ) fatal("expected map argument"); map = argv[i]; break; case 'g': if ( ++i == argc ) usage(); gam = get_opt_double(argv[i], "gam"); if ( gam < 0.1 || gam > 10.0 ) fatal("only gammas in the range 0.1 to 10.0 are sensible"); break; case 's': if ( ++i == argc ) usage(); shelf = get_opt_double(argv[i], "shelf"); break; default: usage(); break; } } /*...e*/ /*...sdeduce mapping and bits per pixel etc\46\:8:*/ { int j; for ( j = 0; j < N_MAPINFOS; j++ ) if ( same(map, mapinfos[j].name, strlen(map) + 1) ) break; if ( j == N_MAPINFOS ) fatal("unrecognised mapping %s", map); m = mapinfos[j].m; } /*...e*/ if ( i == argc ) usage(); strcpy(fn_src, argv[i++]); strcpy(fn_dst, ( i == argc ) ? fn_src : argv[i++]); if ( i < argc ) usage(); if ( (opt_src = strchr(fn_src, ',')) != NULL ) *opt_src++ = '\0'; else opt_src = ""; if ( (opt_dst = strchr(fn_dst, ',')) != NULL ) *opt_dst++ = '\0'; else opt_dst = ""; gbm_init(); if ( gbm_guess_filetype(fn_src, &ft_src) != GBM_ERR_OK ) fatal("can't guess bitmap file format for %s", fn_src); if ( gbm_guess_filetype(fn_dst, &ft_dst) != GBM_ERR_OK ) fatal("can't guess bitmap file format for %s", fn_dst); if ( (fd = open(fn_src, O_RDONLY | O_BINARY)) == -1 ) fatal("can't open %s", fn_src); if ( (rc = gbm_read_header(fn_src, fd, ft_src, &gbm, opt_src)) != GBM_ERR_OK ) { close(fd); fatal("can't read header of %s: %s", fn_src, gbm_err(rc)); } gbm_query_filetype(ft_dst, &gbmft); switch ( gbm.bpp ) { case 24: flag = GBM_FT_W24; break; case 8: flag = GBM_FT_W8; break; case 4: flag = GBM_FT_W4; break; case 1: flag = GBM_FT_W1; break; } if ( (gbmft.flags & flag) == 0 ) { close(fd); fatal("output bitmap format %s does not support writing %d bpp data", gbmft.short_name, gbm.bpp); } if ( (rc = gbm_read_palette(fd, ft_src, &gbm, gbmrgb)) != GBM_ERR_OK ) { close(fd); fatal("can't read palette of %s: %s", fn_src, gbm_err(rc)); } stride = ( ((gbm.w * gbm.bpp + 31)/32) * 4 ); bytes = stride * gbm.h; if ( (data = malloc(bytes)) == NULL ) { close(fd); fatal("out of memory allocating %d bytes for bitmap", bytes); } if ( (rc = gbm_read_data(fd, ft_src, &gbm, data)) != GBM_ERR_OK ) { close(fd); fatal("can't read bitmap data of %s: %s", fn_src, gbm_err(rc)); } close(fd); map_compute(m, remap, gam, shelf); if ( gbm.bpp == 24 ) map_data(data, gbm.w, gbm.h, remap); else map_palette(gbmrgb, 1 << gbm.bpp, remap); if ( (fd = open(fn_dst, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, S_IREAD | S_IWRITE)) == -1 ) fatal("can't create %s", fn_dst); if ( (rc = gbm_write(fn_dst, fd, ft_dst, &gbm, gbmrgb, data, opt_dst)) != GBM_ERR_OK ) { close(fd); remove(fn_dst); fatal("can't write %s: %s", fn_dst, gbm_err(rc)); } close(fd); free(data); gbm_deinit(); return 0; }