handle colormaps correctly - fixes #4127
Some checks failed
CodeQL / Analyze (cpp) (push) Has been cancelled
sw / build (fedora:latest, ubuntu-22.04) (push) Has been cancelled
sw / build (macos-latest) (push) Has been cancelled
sw / build (windows-2022) (push) Has been cancelled

This commit is contained in:
zdenop 2024-12-15 18:26:15 +01:00 committed by Stefan Weil
parent d1fbbb88fc
commit 3299901b9c

View File

@ -283,30 +283,24 @@ bool ImageThresholder::ThresholdToPix(Image *pix) {
tprintf("Image too large: (%d, %d)\n", image_width_, image_height_);
return false;
}
Image original = GetPixRect();
// Handle binary image
if (pix_channels_ == 0) {
// We have a binary image, but it still has to be copied, as this API
// allows the caller to modify the output.
Image original = GetPixRect();
*pix = original.copy();
} else {
if (pixGetColormap(original)) {
Image tmp;
Image without_cmap =
pixRemoveColormap(original, REMOVE_CMAP_BASED_ON_SRC);
int depth = pixGetDepth(without_cmap);
if (depth > 1 && depth < 8) {
tmp = pixConvertTo8(without_cmap, false);
} else {
tmp = without_cmap.copy();
}
without_cmap.destroy();
OtsuThresholdRectToPix(tmp, pix);
tmp.destroy();
} else {
OtsuThresholdRectToPix(pix_, pix);
}
original.destroy();
return true;
}
// Handle colormaps
Image src = pix_;
if (pixGetColormap(src)) {
src = pixRemoveColormap(src, REMOVE_CMAP_BASED_ON_SRC);
}
OtsuThresholdRectToPix(src, pix);
if (src != pix_) {
src.destroy();
}
original.destroy();
return true;
}