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 makemap_gray(i_quantize *, int step);
static int makemap_palette(i_quantize *, i_img **imgs, int count);
makemap_mono(quant);
break;
+ case mc_gray:
+ makemap_gray(quant, 1);
+ break;
+
+ case mc_gray4:
+ makemap_gray(quant, 85);
+ break;
+
+ case mc_gray16:
+ makemap_gray(quant, 17);
+ break;
+
case mc_addi:
default:
makemap_addi(quant, imgs, count);
}
static void translate_closest(i_quantize *, i_img *, i_palidx *);
-static void translate_errdiff(i_quantize *, i_img *, i_palidx *);
+static int translate_errdiff(i_quantize *, i_img *, i_palidx *);
static void translate_addi(i_quantize *, i_img *, i_palidx *);
/*
break;
case pt_errdiff:
- translate_errdiff(quant, img, result);
+ if (!translate_errdiff(quant, img, result)) {
+ myfree(result);
+ return NULL;
+ }
break;
case pt_perturb:
typedef struct {
i_sample_t rgb[3];
- int count;
+ i_img_dim count;
} quant_color_entry;
#define MEDIAN_CUT_COLORS 32768
int max_index = 0, max_ch = 0; /* index/channel with biggest spread */
int max_size;
medcut_partition *workpart;
- int cum_total;
- int half;
+ i_img_dim cum_total;
+ i_img_dim half;
/* find the partition with the most biggest span with more than
one color */
/* fill in the color table - since we could still have partitions
that have more than one color, we need to average the colors */
for (part_num = 0; part_num < color_count; ++part_num) {
- long sums[3];
+ double sums[3];
medcut_partition *workpart;
workpart = parts+part_num;
for (i = workpart->start; i < workpart->start + workpart->size; ++i) {
for (ch = 0; ch < 3; ++ch) {
- sums[ch] += colors[i].rgb[ch] * colors[i].count;
+ sums[ch] += (int)(colors[i].rgb[ch]) * colors[i].count;
}
}
for (ch = 0; ch < 3; ++ch) {
quant->mc_count = 2;
}
+static void
+makemap_gray(i_quantize *quant, int step) {
+ int gray = 0;
+ int i = 0;
+
+ while (gray < 256) {
+ setcol(quant->mc_colors+i, gray, gray, gray, 255);
+ ++i;
+ gray += step;
+ }
+ quant->mc_count = i;
+}
+
static void
makemap_webmap(i_quantize *quant) {
int r, g, b;
} errdiff_t;
/* perform an error diffusion dither */
-static
-void
+static int
translate_errdiff(i_quantize *quant, i_img *img, i_palidx *out) {
int *map;
int mapw, maph, mapo;
mapo = maps[index].orig;
}
+ difftotal = 0;
+ for (i = 0; i < maph * mapw; ++i) {
+ if (map[i] < 0) {
+ i_push_errorf(0, "errdiff_map values must be non-negative, errdiff[%d] is negative", i);
+ return 0;
+ }
+ difftotal += map[i];
+ }
+
+ if (!difftotal) {
+ i_push_error(0, "error diffusion map must contain some non-zero values");
+ return 0;
+ }
+
errw = img->xsize+mapw;
err = mymalloc(sizeof(*err) * maph * errw);
/*errp = err+mapo;*/
memset(err, 0, sizeof(*err) * maph * errw);
- difftotal = 0;
- for (i = 0; i < maph * mapw; ++i)
- difftotal += map[i];
/*printf("map:\n");
for (dy = 0; dy < maph; ++dy) {
for (dx = 0; dx < mapw; ++dx) {
}
CF_CLEANUP;
myfree(err);
+
+ return 1;
}
/* Prescan finds the boxes in the image that have the highest number of colors
and that result is used as the initial value for the vectores */
c.cand++;
c.pdc=c.pixcnt/(c.cand*c.cand);
/* c.pdc=c.pixcnt/c.cand; */
- while(c.pdc < prescan[nidx+1].pdc && nidx < 511) {
+ while(nidx < 511 && c.pdc < prescan[nidx+1].pdc) {
prescan[nidx]=prescan[nidx+1];
nidx++;
}
void
cr_hashindex(cvec clr[256],int cnum,hashbox hb[512]) {
- int bx,mind,cd,cumcnt,bst_idx,i;
+ int bx,mind,cd,cumcnt,i;
/* printf("indexing... \n");*/
cumcnt=0;
mind=196608;
for(i=0; i<cnum; i++) {
cd = maxdist(bx,&clr[i]);
- if (cd < mind) { mind=cd; bst_idx=i; }
+ if (cd < mind) { mind=cd; }
}
hb[bx].cnt=0;