diff --git a/AUTHORS b/AUTHORS index f46860f..5552139 100644 --- a/AUTHORS +++ b/AUTHORS @@ -4,6 +4,7 @@ Adam Langley Contributors: ============= +Dan Bloomberg Misty De Meo zdenop Steven Lee http://www.rubypdf.com diff --git a/src/jbig2.cc b/src/jbig2.cc index 79816d4..dc98fe4 100644 --- a/src/jbig2.cc +++ b/src/jbig2.cc @@ -62,6 +62,8 @@ usage(const char *argv0) { fprintf(stderr, " -t : set classification threshold for symbol coder (def: %0.2f)\n", JBIG2_THRESHOLD_DEF); fprintf(stderr, " -w : set classification weight for symbol coder (def: %0.2f)\n", JBIG2_WEIGHT_DEF); fprintf(stderr, " -T : 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 : dump thresholded image as PNG\n"); fprintf(stderr, " -2: upsample 2x before thresholding\n"); @@ -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; @@ -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++; @@ -340,7 +347,8 @@ 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++; @@ -348,21 +356,23 @@ main(int argc, char **argv) { } 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 ) { @@ -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) { @@ -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; @@ -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);