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

Clean background #107

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Adam Langley <agl@imperialviolet.org>

Contributors:
=============
Dan Bloomberg <dan.bloomberg@gmail.com>
Misty De Meo <mistydemeo@gmail.com>
zdenop <zdenop@gmail.com>
Steven Lee http://www.rubypdf.com
Expand Down
53 changes: 37 additions & 16 deletions src/jbig2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ usage(const char *argv0) {
fprintf(stderr, " -t <threshold>: set classification threshold for symbol coder (def: %0.2f)\n", JBIG2_THRESHOLD_DEF);
fprintf(stderr, " -w <weight>: set classification weight for symbol coder (def: %0.2f)\n", JBIG2_WEIGHT_DEF);
fprintf(stderr, " -T <bw threshold>: set 1 bpp threshold (def: %d)\n", BW_THRESHOLD_DEF);
fprintf(stderr, " -G --global: simply threshold for BW images\n");
fprintf(stderr, " -N --normalize: background clean/normalize\n");
fprintf(stderr, " -r --refine: use refinement (requires -s: lossless)\n");
fprintf(stderr, " -O <outfile>: dump thresholded image as PNG\n");
fprintf(stderr, " -2: upsample 2x before thresholding\n");
Expand Down Expand Up @@ -213,9 +215,12 @@ int
main(int argc, char **argv) {
bool duplicate_line_removal = false;
bool pdfmode = false;
bool globalmode = false;
bool bgnmode = false;
float threshold = JBIG2_THRESHOLD_DEF;
float weight = JBIG2_WEIGHT_DEF;
int bw_threshold = BW_THRESHOLD_DEF;
int bwthresdelta = 0;
bool symbol_mode = false;
bool refine = false;
bool up2 = false, up4 = false;
Expand Down Expand Up @@ -320,9 +325,11 @@ main(int argc, char **argv) {
return 1;
}

if ((threshold < JBIG2_THRESHOLD_MIN) || (threshold > JBIG2_THRESHOLD_MAX)) {
if ((threshold < JBIG2_THRESHOLD_MIN) ||
(threshold > JBIG2_THRESHOLD_MAX)) {
fprintf(stderr, "Invalid value for threshold\n");
fprintf(stderr, "(must be between %0.2f and %0.2f)\n", JBIG2_THRESHOLD_MIN, JBIG2_THRESHOLD_MAX);
fprintf(stderr, "(must be between %0.2f and %0.2f)\n",
JBIG2_THRESHOLD_MIN, JBIG2_THRESHOLD_MAX);
return 10;
}
i++;
Expand All @@ -340,29 +347,32 @@ main(int argc, char **argv) {

if ((weight < JBIG2_WEIGHT_MIN) || (weight > JBIG2_WEIGHT_MAX)) {
fprintf(stderr, "Invalid value for weight\n");
fprintf(stderr, "(must be between %0.2f and %0.2f)\n", JBIG2_WEIGHT_MIN, JBIG2_WEIGHT_MAX);
fprintf(stderr, "(must be between %0.2f and %0.2f)\n",
JBIG2_WEIGHT_MIN, JBIG2_WEIGHT_MAX);
return 10;
}
i++;
continue;
}

if (strcmp(argv[i], "-T") == 0) {
char *endptr;
bw_threshold = strtol(argv[i+1], &endptr, 10);
if (*endptr) {
fprintf(stderr, "Cannot parse int value: %s\n", argv[i+1]);
usage(argv[0]);
return 1;
}
if (bw_threshold < BW_THRESHOLD_MIN || bw_threshold > BW_THRESHOLD_MAX) {
fprintf(stderr, "Invalid bw threshold: (%d..%d)\n", BW_THRESHOLD_MIN, BW_THRESHOLD_MAX);
return 11;
}
fprintf(stderr, "Binary thresholds 'T' are no longer used\n");
i++;
continue;
}

if (strcmp(argv[i], "-G") == 0 ||
strcmp(argv[i], "--global") == 0) {
globalmode = true;
continue;
}

if (strcmp(argv[i], "-N") == 0 ||
strcmp(argv[i], "--normalize") == 0) {
bgnmode = true;
continue;
}

// engage auto thresholding
if (strcmp(argv[i], "--auto-thresh") == 0 ||
strcmp(argv[i], "-a") == 0 ) {
Expand Down Expand Up @@ -421,6 +431,11 @@ main(int argc, char **argv) {
struct jbig2ctx *ctx = jbig2_init(threshold, weight, 0, 0, !pdfmode, refine ? 10 : -1);
int pageno = -1;

if (!globalmode) {
bwthresdelta = (bgnmode) ? 0 : 64;
bw_threshold = ((bw_threshold + bwthresdelta) < 256) ? (bw_threshold + bwthresdelta) : 255;
}

int numsubimages=0, subimage=0, num_pages = 0;
while (i < argc) {
if (subimage==numsubimages) {
Expand Down Expand Up @@ -456,7 +471,7 @@ main(int argc, char **argv) {
if (verbose)
pixInfo(source, "source image:");

PIX *pixl, *gray, *pixt;
PIX *pixl, *gray, *adapt, *pixt;
if ((pixl = pixRemoveColormap(source, REMOVE_CMAP_BASED_ON_SRC)) == NULL) {
fprintf(stderr, "Failed to remove colormap from %s\n", argv[i]);
return 1;
Expand All @@ -474,7 +489,13 @@ main(int argc, char **argv) {
fprintf(stderr, "Unsupported input image depth: %d\n", pixl->d);
return 1;
}
Pix *adapt = pixBackgroundNormSimple(gray, NULL, NULL);
if (globalmode) {
adapt = pixClone(gray);
} else if (bgnmode) {
adapt = pixBackgroundNormSimple(gray, NULL, NULL);
} else {
adapt = pixCleanBackgroundToWhite(gray, NULL, NULL, 1.0, 90, 190);
}
pixDestroy(&gray);
if (up2) {
pixt = pixScaleGray2xLIThresh(adapt, bw_threshold);
Expand Down
Loading