Palettes: Remove threshold parameter when extract from images

Using a number greater than 1 produce a huge number of entries in the palette because any small variation in the color create a new entry.

Remove the threshold reduce the precision of the color extracted, but always is better than having a palette with thounsands of colors.

Related to T78317

Still pending the memory crash when it's impossible to allocate memory for the palette in the popover, but with this change I don't think we get this error again.
This commit is contained in:
Antonio Vazquez 2020-06-27 10:26:46 +02:00
parent 4b96f47831
commit fec2b2a809
Notes: blender-bot 2023-02-13 21:58:50 +01:00
Referenced by issue #78317, Popover crash using a palette with many colors
1 changed files with 8 additions and 5 deletions

View File

@ -331,14 +331,14 @@ static int palette_extract_img_exec(bContext *C, wmOperator *op)
if (ibuf && ibuf->rect) {
/* Extract all colors. */
const int range = (int)pow(10.0f, threshold);
for (int row = 0; row < ibuf->y; row++) {
for (int col = 0; col < ibuf->x; col++) {
float color[4];
IMB_sampleImageAtLocation(ibuf, (float)col, (float)row, false, color);
const float range = pow(10.0f, threshold);
color[0] = truncf(color[0] * range) / range;
color[1] = truncf(color[1] * range) / range;
color[2] = truncf(color[2] * range) / range;
for (int i = 0; i < 3; i++) {
color[i] = truncf(color[i] * range) / range;
}
uint key = rgb_to_cpack(color[0], color[1], color[2]);
if (!BLI_ghash_haskey(color_table, POINTER_FROM_INT(key))) {
@ -363,6 +363,8 @@ static int palette_extract_img_exec(bContext *C, wmOperator *op)
static void PALETTE_OT_extract_from_image(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name = "Extract Palette from Image";
ot->idname = "PALETTE_OT_extract_from_image";
@ -376,7 +378,8 @@ static void PALETTE_OT_extract_from_image(wmOperatorType *ot)
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
RNA_def_int(ot->srna, "threshold", 1, 1, 4, "Threshold", "", 1, 4);
prop = RNA_def_int(ot->srna, "threshold", 1, 1, 1, "Threshold", "", 1, 1);
RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
}
/* Sort Palette color by Hue and Saturation. */