From 2ed5ac364be550a52db08f688544456843233107 Mon Sep 17 00:00:00 2001 From: alpine9000 Date: Sat, 26 Mar 2016 18:23:14 +1100 Subject: [PATCH] More palette options --- tools/imagecon/imagecon.c | 27 +++++++++++++++++-- tools/imagecon/imagecon.h | 17 +++++++----- tools/imagecon/palette.c | 56 ++++++++++++++++++++++++--------------- 3 files changed, 69 insertions(+), 31 deletions(-) diff --git a/tools/imagecon/imagecon.c b/tools/imagecon/imagecon.c index 9df760f..abda434 100644 --- a/tools/imagecon/imagecon.c +++ b/tools/imagecon/imagecon.c @@ -20,7 +20,9 @@ imagecon_config_t config = { .quantize = 0, .dither = 0, .overridePalette = 0, - .paletteOffset = 0 + .paletteOffset = 0, + .maskTransparentColor = 0, + .fullColorPaletteFile = 0 }; @@ -45,7 +47,9 @@ usage() " --ham-brute-force\n"\ " --sliced-ham\n"\ " --dither\n"\ + " --transparent-color \n"\ " --use-palette \n"\ + " --full-color-palette-file\n"\ " --palette-offset \n"\ " --verbose\n", config.argv[0]); exit(1); @@ -304,7 +308,15 @@ outputMask(imagecon_image_t* ic, char* outFilename) for (int bit = 0; bit < 8; bit++) { int x = byte * 8 + 7 - bit; amiga_color_t c = color_getOriginalPixel(ic, x, y); - int bitmask = c.a > 0 ? 0xFF : 0; + int bitmask; + if (config.maskTransparentColor == 0) { + bitmask = c.a > 0 ? 0xFF : 0; + } else { + bitmask = + (c.r == config.maskTransparentColor->r && + c.g == config.maskTransparentColor->g && + c.b == config.maskTransparentColor->b) ? 0 : 0xff; + } for (int plane_index = 0; plane_index < numBitPlanes; plane_index++) { char* plane = bitplanes[plane_index]; plane[writeIndex] |= ((bitmask >> plane_index) & 1) << bit; @@ -488,11 +500,13 @@ main(int argc, char **argv) {"ham-brute-force", no_argument, &config.hamBruteForce, 1}, {"sliced-ham", no_argument, &config.slicedHam, 1}, {"dither", no_argument, &config.dither, 1}, + {"full-color-palette-file", no_argument, &config.fullColorPaletteFile, 1}, {"use-palette", required_argument, 0, 'p'}, {"palette-offset", required_argument, 0, 'l'}, {"output", required_argument, 0, 'o'}, {"colors", required_argument, 0, 'c'}, {"input", required_argument, 0, 'i'}, + {"transparent-color", required_argument, 0, 't'}, {0, 0, 0, 0} }; @@ -528,6 +542,15 @@ main(int argc, char **argv) abort_("Number of colors exceeds limit (%d colors)", MAX_PALETTE); } break; + case 't': + { + static amiga_color_t color; + if (sscanf(optarg, "%d,%d,%d",&color.r, &color.g, &color.b) != 3) { + abort_("invalid transparent color"); + } + config.maskTransparentColor = &color; + } + break; case '?': usage(); break; diff --git a/tools/imagecon/imagecon.h b/tools/imagecon/imagecon.h index d4d0e61..b82f4d2 100644 --- a/tools/imagecon/imagecon.h +++ b/tools/imagecon/imagecon.h @@ -17,6 +17,13 @@ #define CLAMP(x) (x > 255.0 ? 255.0 : (x < -255.0 ? -255.0 : x)) #define COLOR8(x) (x > 255.0 ? 255.0 : (x < 0.0 ? 0.0 : x)) +typedef struct { + int r; + int g; + int b; + int a; +} amiga_color_t; + typedef struct { int maxColors; int outputPalette; @@ -31,19 +38,15 @@ typedef struct { int slicedHam; int dither; char* overridePalette; + amiga_color_t* maskTransparentColor; int paletteOffset; + int fullColorPaletteFile; int quantize; int outputPng; int verbose; - char** argv; + char** argv; } imagecon_config_t; -typedef struct { - int r; - int g; - int b; - int a; -} amiga_color_t; typedef struct { float r; diff --git a/tools/imagecon/palette.c b/tools/imagecon/palette.c index 4682cd6..6f9840f 100644 --- a/tools/imagecon/palette.c +++ b/tools/imagecon/palette.c @@ -4,7 +4,11 @@ void palette_loadFile(imagecon_image_t* ic) { FILE* fp = file_openRead(config.overridePalette); int paletteIndex; - + + if (config.verbose) { + printf("palette_loadFile:\n"); + } + for (paletteIndex = 0; paletteIndex < MAX_PALETTE; paletteIndex++) { char buffer[255]; @@ -13,22 +17,29 @@ void palette_loadFile(imagecon_image_t* ic) break; } -#if 1 - unsigned int c; - sscanf(buffer, "%x\n", &c); - - ic->palette[paletteIndex].r = (c >> 8 & 0xF) << 4; - ic->palette[paletteIndex].g = (c >> 4 & 0xF) << 4; - ic->palette[paletteIndex].b = (c >> 0 & 0xF) << 4; - ic->palette[paletteIndex].a = 255; -#else // 24 bit palette version - sscanf(buffer, "%d %d %d %d\n", - &ic->palette[paletteIndex].r, - &ic->palette[paletteIndex].g, - &ic->palette[paletteIndex].b, - &ic->palette[paletteIndex].a); - -#endif + if (!config.fullColorPaletteFile) { + unsigned int c; + sscanf(buffer, "%x\n", &c); + + ic->palette[paletteIndex].r = (c >> 8 & 0xF) << 4; + ic->palette[paletteIndex].g = (c >> 4 & 0xF) << 4; + ic->palette[paletteIndex].b = (c >> 0 & 0xF) << 4; + ic->palette[paletteIndex].a = 255; + + if (config.verbose) { + printf("%03d %03d %03d\n", ic->palette[paletteIndex].r, ic->palette[paletteIndex].g, ic->palette[paletteIndex].b); + } + } else { + sscanf(buffer, "%d %d %d %d\n", + &ic->palette[paletteIndex].r, + &ic->palette[paletteIndex].g, + &ic->palette[paletteIndex].b, + &ic->palette[paletteIndex].a); + + if (config.verbose) { + printf("r: %03d g: %03d b: %03d a: %03d\n", ic->palette[paletteIndex].r, ic->palette[paletteIndex].g, ic->palette[paletteIndex].b, ic->palette[paletteIndex].a); + } + } } ic->numColors = paletteIndex; @@ -74,11 +85,12 @@ palette_output(imagecon_image_t* ic, char* outFilename) printf("%02d: hex=%03x r=%03d g=%03d b=%03d a=%03d\n", i , ic->palette[i].r << 8 | ic->palette[i].g << 4 | ic->palette[i].b, ic->palette[i].r, ic->palette[i].g, ic->palette[i].b, ic->palette[i].a); } if (paletteFP) { -#if 1 - fprintf(paletteFP, "%03x\n", RGB24TORGB12(ic->palette[i].r) << 8 | RGB24TORGB12(ic->palette[i].g) << 4 | RGB24TORGB12(ic->palette[i].b)); -#else // 24 bit palette - fprintf(paletteFP, "%03d %03d %03d %03d\n", ic->palette[i].r , ic->palette[i].g , ic->palette[i].b, ic->palette[i].a); -#endif + + if (!config.fullColorPaletteFile) { + fprintf(paletteFP, "%03x\n", RGB24TORGB12(ic->palette[i].r) << 8 | RGB24TORGB12(ic->palette[i].g) << 4 | RGB24TORGB12(ic->palette[i].b)); + } else { + fprintf(paletteFP, "%03d %03d %03d %03d\n", ic->palette[i].r , ic->palette[i].g , ic->palette[i].b, ic->palette[i].a); + } } if (paletteAsmFP) { fprintf(paletteAsmFP, "\tlea COLOR%02d(a6),a0\n\tmove.w #$%03x,(a0)\n", i+config.paletteOffset, RGB24TORGB12(ic->palette[i].r) << 8 | RGB24TORGB12(ic->palette[i].g) << 4 | RGB24TORGB12(ic->palette[i].b));