}
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
for (ch = 0; ch < 3; ++ch) {
quant->mc_colors[i].channel[ch] = colors[i].rgb[ch];
}
+ quant->mc_colors[i].rgba.a = 255;
}
quant->mc_count = out;
}
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_colors[part_num].channel[ch] = sums[ch] / workpart->pixels;
}
+ quant->mc_colors[part_num].rgba.a = 255;
}
quant->mc_count = color_count;
}
} 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);
+ goto fail;
+ }
+ difftotal += map[i];
+ }
+
+ if (!difftotal) {
+ i_push_error(0, "error diffusion map must contain some non-zero values");
+ goto fail;
+ }
+
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;
+
+ fail:
+ CF_CLEANUP;
+
+ return 0;
}
/* 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;