Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue #3940 - remove colormap before thresholding #3942

Merged
merged 1 commit into from
Oct 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/ccmain/thresholder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ void ImageThresholder::SetImage(const Image pix) {
} else {
pix_ = src.copy();
}
src.destroy();
depth = pixGetDepth(pix_);
pix_channels_ = depth / 8;
pix_wpl_ = pixGetWpl(pix_);
Expand Down Expand Up @@ -287,15 +288,26 @@ bool ImageThresholder::ThresholdToPix(Image *pix) {
tprintf("Image too large: (%d, %d)\n", image_width_, image_height_);
return false;
}
Image original = GetPixRect();
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();
original.destroy();
} else {
OtsuThresholdRectToPix(pix_, pix);
if (pixGetColormap(original)) {
Image without_cmap =
pixRemoveColormap(original, REMOVE_CMAP_BASED_ON_SRC);
int depth = pixGetDepth(without_cmap);
if (depth > 1 && depth < 8) {
original = pixConvertTo8(without_cmap, false);
} else {
original = without_cmap.copy();
}
without_cmap.destroy();
}
OtsuThresholdRectToPix(original, pix);
}
original.destroy();
return true;
}

Expand Down Expand Up @@ -353,7 +365,7 @@ Image ImageThresholder::GetPixRect() {
Image ImageThresholder::GetPixRectGrey() {
auto pix = GetPixRect(); // May have to be reduced to grey.
int depth = pixGetDepth(pix);
if (depth != 8) {
if (depth != 8 || pixGetColormap(pix)) {
if (depth == 24) {
auto tmp = pixConvert24To32(pix);
pix.destroy();
Expand Down