Prefer smaller file only on non-image changing operations

When cli arguments request image-changing operation (like transform, scans or arith coding) to be applied, force output result file, even if it has bigger filesize than original
This commit is contained in:
Konstantin Kopachev
2018-07-12 14:34:49 -07:00
committed by Kornel
parent a4a721fce5
commit 86a9bd1b08

View File

@@ -42,6 +42,7 @@
static const char *progname; /* program name for error messages */ static const char *progname; /* program name for error messages */
static char *outfilename; /* for -outfile switch */ static char *outfilename; /* for -outfile switch */
static boolean prefer_smallest; /* use smallest of input or result file (if no image-changing options supplied) */
static JCOPY_OPTION copyoption; /* -copy switch */ static JCOPY_OPTION copyoption; /* -copy switch */
static jpeg_transform_info transformoption; /* image transformation options */ static jpeg_transform_info transformoption; /* image transformation options */
boolean memsrc = FALSE; /* for -memsrc switch */ boolean memsrc = FALSE; /* for -memsrc switch */
@@ -156,6 +157,7 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
transformoption.crop = FALSE; transformoption.crop = FALSE;
transformoption.slow_hflip = FALSE; transformoption.slow_hflip = FALSE;
cinfo->err->trace_level = 0; cinfo->err->trace_level = 0;
prefer_smallest = TRUE;
/* Scan command line options, adjust parameters */ /* Scan command line options, adjust parameters */
@@ -178,6 +180,7 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
/* No table optimization required for AC */ /* No table optimization required for AC */
cinfo->optimize_coding = FALSE; cinfo->optimize_coding = FALSE;
prefer_smallest = FALSE;
#else #else
fprintf(stderr, "%s: sorry, arithmetic coding not supported\n", fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
progname); progname);
@@ -207,6 +210,7 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
progname, argv[argn]); progname, argv[argn]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
prefer_smallest = FALSE;
#else #else
select_transform(JXFORM_NONE); /* force an error */ select_transform(JXFORM_NONE); /* force an error */
#endif #endif
@@ -242,6 +246,8 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
else else
usage(); usage();
prefer_smallest = FALSE;
} else if (keymatch(arg, "fastcrush", 4)) { } else if (keymatch(arg, "fastcrush", 4)) {
jpeg_c_set_bool_param(cinfo, JBOOLEAN_OPTIMIZE_SCANS, FALSE); jpeg_c_set_bool_param(cinfo, JBOOLEAN_OPTIMIZE_SCANS, FALSE);
@@ -249,6 +255,7 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
/* Force to grayscale. */ /* Force to grayscale. */
#if TRANSFORMS_SUPPORTED #if TRANSFORMS_SUPPORTED
transformoption.force_grayscale = TRUE; transformoption.force_grayscale = TRUE;
prefer_smallest = FALSE;
#else #else
select_transform(JXFORM_NONE); /* force an error */ select_transform(JXFORM_NONE); /* force an error */
#endif #endif
@@ -291,6 +298,7 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
/* Select simple progressive mode. */ /* Select simple progressive mode. */
#ifdef C_PROGRESSIVE_SUPPORTED #ifdef C_PROGRESSIVE_SUPPORTED
simple_progressive = TRUE; simple_progressive = TRUE;
prefer_smallest = FALSE;
/* We must postpone execution until num_components is known. */ /* We must postpone execution until num_components is known. */
#else #else
fprintf(stderr, "%s: sorry, progressive output was not compiled\n", fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
@@ -320,6 +328,7 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
} else if (keymatch(arg, "revert", 3)) { } else if (keymatch(arg, "revert", 3)) {
/* revert to old JPEG default */ /* revert to old JPEG default */
jpeg_c_set_int_param(cinfo, JINT_COMPRESS_PROFILE, JCP_FASTEST); jpeg_c_set_int_param(cinfo, JINT_COMPRESS_PROFILE, JCP_FASTEST);
prefer_smallest = FALSE;
} else if (keymatch(arg, "rotate", 2)) { } else if (keymatch(arg, "rotate", 2)) {
/* Rotate 90, 180, or 270 degrees (measured clockwise). */ /* Rotate 90, 180, or 270 degrees (measured clockwise). */
@@ -334,11 +343,14 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
else else
usage(); usage();
prefer_smallest = FALSE;
} else if (keymatch(arg, "scans", 1)) { } else if (keymatch(arg, "scans", 1)) {
/* Set scan script. */ /* Set scan script. */
#ifdef C_MULTISCAN_FILES_SUPPORTED #ifdef C_MULTISCAN_FILES_SUPPORTED
if (++argn >= argc) /* advance to next argument */ if (++argn >= argc) /* advance to next argument */
usage(); usage();
prefer_smallest = FALSE;
scansarg = argv[argn]; scansarg = argv[argn];
/* We must postpone reading the file in case -progressive appears. */ /* We must postpone reading the file in case -progressive appears. */
#else #else
@@ -350,14 +362,17 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
} else if (keymatch(arg, "transpose", 1)) { } else if (keymatch(arg, "transpose", 1)) {
/* Transpose (across UL-to-LR axis). */ /* Transpose (across UL-to-LR axis). */
select_transform(JXFORM_TRANSPOSE); select_transform(JXFORM_TRANSPOSE);
prefer_smallest = FALSE;
} else if (keymatch(arg, "transverse", 6)) { } else if (keymatch(arg, "transverse", 6)) {
/* Transverse transpose (across UR-to-LL axis). */ /* Transverse transpose (across UR-to-LL axis). */
select_transform(JXFORM_TRANSVERSE); select_transform(JXFORM_TRANSVERSE);
prefer_smallest = FALSE;
} else if (keymatch(arg, "trim", 3)) { } else if (keymatch(arg, "trim", 3)) {
/* Trim off any partial edge MCUs that the transform can't handle. */ /* Trim off any partial edge MCUs that the transform can't handle. */
transformoption.trim = TRUE; transformoption.trim = TRUE;
prefer_smallest = FALSE;
} else { } else {
usage(); /* bogus switch */ usage(); /* bogus switch */
@@ -378,6 +393,7 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
if (! read_scan_script(cinfo, scansarg)) if (! read_scan_script(cinfo, scansarg))
usage(); usage();
#endif #endif
} }
return argn; /* return index of next arg (file name) */ return argn; /* return index of next arg (file name) */
@@ -598,7 +614,7 @@ main (int argc, char **argv)
unsigned char *buffer = outbuffer; unsigned char *buffer = outbuffer;
unsigned long size = outsize; unsigned long size = outsize;
if (insize < size) { if (prefer_smallest && insize < size) {
size = insize; size = insize;
buffer = inbuffer; buffer = inbuffer;
} }