X-Git-Url: http://git.imager.perl.org/imager.git/blobdiff_plain/f771d0eca7fde9c5963fcfcf7ebd4ed3bfa52bc0..e17b7029eb8e9c20f30e5208de1f49d243fa514c:/quant.c diff --git a/quant.c b/quant.c index 8615d05e..13096ccc 100644 --- a/quant.c +++ b/quant.c @@ -2,10 +2,11 @@ currently only used by gif.c, but maybe we'll support producing 8-bit (or bigger indexed) png files at some point */ -#include "image.h" +#include "imager.h" static void makemap_addi(i_quantize *, i_img **imgs, int count); static void makemap_mediancut(i_quantize *, i_img **imgs, int count); +static void makemap_mono(i_quantize *); static void @@ -26,8 +27,20 @@ setcol(i_color *cl,unsigned char r,unsigned char g,unsigned char b,unsigned char handle multiple colour maps. */ +/* +=item i_quant_makemap(C, C, C) + +=category Image quantization + +Analyzes the C images in C according to the rules in +C to build a color map (optimal or not depending on +C<< quant->make_colors >>). + +=cut +*/ + void -quant_makemap(i_quantize *quant, i_img **imgs, int count) { +i_quant_makemap(i_quantize *quant, i_img **imgs, int count) { if (quant->translate == pt_giflib) { /* giflib does it's own color table generation */ @@ -59,6 +72,10 @@ quant_makemap(i_quantize *quant, i_img **imgs, int count) { makemap_mediancut(quant, imgs, count); break; + case mc_mono: + makemap_mono(quant); + break; + case mc_addi: default: makemap_addi(quant, imgs, count); @@ -66,18 +83,31 @@ quant_makemap(i_quantize *quant, i_img **imgs, int count) { } } -#ifdef HAVE_LIBGIF -static void translate_giflib(i_quantize *, i_img *, i_palidx *); -#endif static void translate_closest(i_quantize *, i_img *, i_palidx *); static void translate_errdiff(i_quantize *, i_img *, i_palidx *); static void translate_addi(i_quantize *, i_img *, i_palidx *); -/* Quantize the image given the palette in quant. +/* +=item i_quant_translate(C, C) + +=category Image quantization + +Quantize the image given the palette in C. + +On success returns a pointer to a memory block of C<< img->xsize * +img->ysize >> C entries. + +On failure returns NULL. + +You should call myfree() on the returned block when you're done with +it. - The giflib quantizer ignores the palette. +This function will fail if the supplied palette contains no colors. + +=cut */ -i_palidx *quant_translate(i_quantize *quant, i_img *img) { +i_palidx * +i_quant_translate(i_quantize *quant, i_img *img) { i_palidx *result; int bytes; @@ -179,9 +209,11 @@ frand(void) { return rand()/(RAND_MAX+1.0); } +#ifdef NOTEF static int eucl_d(cvec* cv,i_color *cl) { return PWR2(cv->r-cl->channel[0])+PWR2(cv->g-cl->channel[1])+PWR2(cv->b-cl->channel[2]); } +#endif static int @@ -589,7 +621,8 @@ makemap_mediancut(i_quantize *quant, i_img **imgs, int count) { color_count = 1; while (color_count < quant->mc_size) { - int max_index, max_ch; /* index/channel with biggest spread */ + /* initialized to avoid compiler warnings */ + int max_index = 0, max_ch = 0; /* index/channel with biggest spread */ int max_size; medcut_partition *workpart; int cum_total; @@ -669,6 +702,19 @@ makemap_mediancut(i_quantize *quant, i_img **imgs, int count) { i_mempool_destroy(&mp); } +static void +makemap_mono(i_quantize *quant) { + quant->mc_colors[0].rgba.r = 0; + quant->mc_colors[0].rgba.g = 0; + quant->mc_colors[0].rgba.b = 0; + quant->mc_colors[0].rgba.a = 255; + quant->mc_colors[1].rgba.r = 255; + quant->mc_colors[1].rgba.g = 255; + quant->mc_colors[1].rgba.b = 255; + quant->mc_colors[1].rgba.a = 255; + quant->mc_count = 2; +} + #define pboxjump 32 /* Define one of the following 4 symbols to choose a colour search method @@ -747,7 +793,7 @@ static int distcomp(void const *a, void const *b) { welcome. */ static void hbsetup(i_quantize *quant, hashbox *hb) { - long *dists, mind, maxd, cd; + long *dists, mind, maxd; int cr, cb, cg, hbnum, i; i_color cenc; #ifdef HB_SORT @@ -1060,7 +1106,7 @@ static int rand2dist_find(i_color val, i_quantize *quant, i_dists *dists, int in #endif static void translate_addi(i_quantize *quant, i_img *img, i_palidx *out) { - int x, y, i, k, bst_idx; + int x, y, i, k, bst_idx = 0; i_color val; int pixdev = quant->perturb; CF_VARS; @@ -1157,9 +1203,7 @@ translate_errdiff(i_quantize *quant, i_img *img, i_palidx *out) { int errw; int difftotal; int x, y, dx, dy; - int minr, maxr, ming, maxg, minb, maxb, cr, cg, cb; - i_color find; - int bst_idx; + int bst_idx = 0; CF_VARS; if ((quant->errdiff & ed_mask) == ed_custom) { @@ -1441,7 +1485,21 @@ static void transparent_threshold(i_quantize *, i_palidx *, i_img *, i_palidx); static void transparent_errdiff(i_quantize *, i_palidx *, i_img *, i_palidx); static void transparent_ordered(i_quantize *, i_palidx *, i_img *, i_palidx); -void quant_transparent(i_quantize *quant, i_palidx *data, i_img *img, +/* +=item i_quant_transparent(C, C, C, C) + +=category Image quantization + +Dither the alpha channel on C into the palette indexes in +C. Pixels to be transparent are replaced with C. + +The method used depends on the tr_* members of C. + +=cut +*/ + +void +i_quant_transparent(i_quantize *quant, i_palidx *data, i_img *img, i_palidx trans_index) { switch (quant->transp) {