From 5dbd51d1ec36ba4389a7ec18ff041fd04ee3c5da Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Wed, 20 Mar 2019 23:09:03 +0200 Subject: [PATCH 1/6] scanimage: Allow specification of the output path via option --- frontend/scanimage.c | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/frontend/scanimage.c b/frontend/scanimage.c index 51b3b81ae..e66f504eb 100644 --- a/frontend/scanimage.c +++ b/frontend/scanimage.c @@ -93,6 +93,7 @@ static struct option basic_options[] = { {"help", no_argument, NULL, 'h'}, {"verbose", no_argument, NULL, 'v'}, {"progress", no_argument, NULL, 'p'}, + {"output-file", required_argument, NULL, 'o'}, {"test", no_argument, NULL, 'T'}, {"all-options", no_argument, NULL, 'A'}, {"version", no_argument, NULL, 'V'}, @@ -116,7 +117,7 @@ static struct option basic_options[] = { #define OUTPUT_PNG 2 #define OUTPUT_JPEG 3 -#define BASE_OPTSTRING "d:hi:Lf:B::nvVTAbp" +#define BASE_OPTSTRING "d:hi:Lf:o:B::nvVTAbp" #define STRIP_HEIGHT 256 /* # lines we increment image height */ static struct option *all_options; @@ -125,6 +126,7 @@ static int *option_number; static SANE_Handle device; static int verbose; static int progress = 0; +static const char* output_file = NULL; static int test; static int all; static int output_format = OUTPUT_PNM; @@ -2033,6 +2035,9 @@ main (int argc, char **argv) case 'p': progress = 1; break; + case 'o': + output_file = optarg; + break; case 'B': if (optarg) buffer_size = 1024 * atoi(optarg); @@ -2247,6 +2252,7 @@ Parameters are separated by a blank from single-character options (e.g.\n\ printf ("\ --accept-md5-only only accept authorization requests using md5\n\ -p, --progress print progress messages\n\ +-o, --output-file=PATH save output to the given file instead of stdout\n\ -n, --dont-scan only set options, don't actually scan\n\ -T, --test test backend thoroughly\n\ -A, --all-options list all available backend options\n\ @@ -2389,6 +2395,7 @@ Parameters are separated by a blank from single-character options (e.g.\n\ case 'd': case 'h': case 'p': + case 'o': case 'v': case 'V': case 'T': @@ -2535,7 +2542,19 @@ List of available devices:", prog_name); } if (!batch) - ofp = stdout; + { + ofp = stdout; + if (output_file != NULL) + { + ofp = fopen(output_file, "w"); + if (ofp == NULL) + { + fprintf(stderr, "%s: could not open input file '%s', " + "exiting\n", prog_name, output_file); + scanimage_exit(1); + } + } + } if (batch) { @@ -2662,6 +2681,14 @@ List of available devices:", prog_name); } } } + else + { + if (output_file && ofp) + { + fclose(ofp); + ofp = NULL; + } + } break; default: if (batch) @@ -2673,6 +2700,15 @@ List of available devices:", prog_name); } unlink (part_path); } + else + { + if (output_file && ofp) + { + fclose(ofp); + ofp = NULL; + } + unlink (output_file); + } break; } /* switch */ n += batch_increment; From 877cc29d88529f9af8e898a52b4be99863a20eea Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Sun, 28 Apr 2019 23:28:17 +0300 Subject: [PATCH 2/6] scanimage: Raise an error if --format option is an unknown format --- frontend/scanimage.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/frontend/scanimage.c b/frontend/scanimage.c index e66f504eb..08679903b 100644 --- a/frontend/scanimage.c +++ b/frontend/scanimage.c @@ -2093,8 +2093,23 @@ main (int argc, char **argv) exit(1); #endif } - else - output_format = OUTPUT_PNM; + else if (strcmp (optarg, "pnm") == 0) + { + output_format = OUTPUT_PNM; + } + else + { + fprintf(stderr, "Unknown output image format '%s'.\n", optarg); + fprintf(stderr, "Supported formats: pnm, tiff"); +#ifdef HAVE_LIBPNG + fprintf(stderr, ", png"); +#endif +#ifdef HAVE_LIBJPEG + fprintf(stderr, ", jpeg"); +#endif + fprintf(stderr, ".\n"); + exit(1); + } break; case OPTION_MD5: accept_only_md5_auth = 1; From 7f4944f0a7d1c7ce90d5f4f79078112bc5d52dff Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Sun, 28 Apr 2019 23:28:18 +0300 Subject: [PATCH 3/6] scanimage: Warn when output format is not set --- frontend/scanimage.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/frontend/scanimage.c b/frontend/scanimage.c index 08679903b..a88682941 100644 --- a/frontend/scanimage.c +++ b/frontend/scanimage.c @@ -112,10 +112,11 @@ static struct option basic_options[] = { {0, 0, NULL, 0} }; -#define OUTPUT_PNM 0 -#define OUTPUT_TIFF 1 -#define OUTPUT_PNG 2 -#define OUTPUT_JPEG 3 +#define OUTPUT_UNKNOWN 0 +#define OUTPUT_PNM 1 +#define OUTPUT_TIFF 2 +#define OUTPUT_PNG 3 +#define OUTPUT_JPEG 4 #define BASE_OPTSTRING "d:hi:Lf:o:B::nvVTAbp" #define STRIP_HEIGHT 256 /* # lines we increment image height */ @@ -129,7 +130,7 @@ static int progress = 0; static const char* output_file = NULL; static int test; static int all; -static int output_format = OUTPUT_PNM; +static int output_format = OUTPUT_UNKNOWN; static int help; static int dont_scan = 0; static const char *prog_name; @@ -2278,6 +2279,12 @@ Parameters are separated by a blank from single-character options (e.g.\n\ -V, --version print version information\n"); } + if (output_format == OUTPUT_UNKNOWN) + { + printf("Output format is not set, using pnm as a default.\n"); + output_format = OUTPUT_PNM; + } + if (!devname) { /* If no device name was specified explicitly, we look at the From 86e917b04b9bd1fbeb53d58323b118f30c8efc15 Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Sun, 28 Apr 2019 23:28:19 +0300 Subject: [PATCH 4/6] scanimage: Prevent --output-file and --batch to be used together --- frontend/scanimage.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/frontend/scanimage.c b/frontend/scanimage.c index a88682941..895beb968 100644 --- a/frontend/scanimage.c +++ b/frontend/scanimage.c @@ -2256,7 +2256,8 @@ Parameters are separated by a blank from single-character options (e.g.\n\ %%m (model), %%t (type), %%i (index number), and\n\ %%n (newline)\n\ -b, --batch[=FORMAT] working in batch mode, FORMAT is `out%%d.pnm' `out%%d.tif'\n\ - `out%%d.png' or `out%%d.jpg' by default depending on --format\n"); + `out%%d.png' or `out%%d.jpg' by default depending on --format\n\ + This option is incompatible with --output-file."); printf ("\ --batch-start=# page number to start naming files with\n\ --batch-count=# how many pages to scan in batch mode\n\ @@ -2268,7 +2269,8 @@ Parameters are separated by a blank from single-character options (e.g.\n\ printf ("\ --accept-md5-only only accept authorization requests using md5\n\ -p, --progress print progress messages\n\ --o, --output-file=PATH save output to the given file instead of stdout\n\ +-o, --output-file=PATH save output to the given file instead of stdout.\n\ + This option is incompatible with --batch.\n\ -n, --dont-scan only set options, don't actually scan\n\ -T, --test test backend thoroughly\n\ -A, --all-options list all available backend options\n\ @@ -2279,6 +2281,12 @@ Parameters are separated by a blank from single-character options (e.g.\n\ -V, --version print version information\n"); } + if (batch && output_file != NULL) + { + fprintf(stderr, "--batch and --output-file can't be used together.\n"); + exit(1); + } + if (output_format == OUTPUT_UNKNOWN) { printf("Output format is not set, using pnm as a default.\n"); From 728de89d7166708331907726571c7027b4921d82 Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Sun, 28 Apr 2019 23:28:20 +0300 Subject: [PATCH 5/6] scanimage: Guess --format from --output-file if possible --- frontend/scanimage.c | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/frontend/scanimage.c b/frontend/scanimage.c index 895beb968..2a307309a 100644 --- a/frontend/scanimage.c +++ b/frontend/scanimage.c @@ -1971,6 +1971,43 @@ static void print_options(SANE_Device * device, SANE_Int num_dev_options, SANE_B fputc ('\n', stdout); } +static int guess_output_format(const char* output_file) +{ + if (output_file == NULL) + { + printf("Output format is not set, using pnm as a default.\n"); + return OUTPUT_PNM; + } + + // if the user passes us a path with a known extension then he won't be surprised if we figure + // out correct --format option. No warning is necessary in that case. + const char* extension = strrchr(output_file, '.'); + if (extension != NULL) + { + struct { + const char* extension; + int output_format; + } formats[] = { + { ".pnm", OUTPUT_PNM }, + { ".png", OUTPUT_PNG }, + { ".jpg", OUTPUT_JPEG }, + { ".jpeg", OUTPUT_JPEG }, + { ".tiff", OUTPUT_TIFF }, + { ".tif", OUTPUT_TIFF } + }; + for (unsigned i = 0; i < sizeof(formats) / sizeof(formats[0]); ++i) + { + if (strcmp(extension, formats[i].extension) == 0) + return formats[i].output_format; + } + } + + // it would be very confusing if user makes a typo in the filename and the output format changes. + // This is most likely not what the user wanted. + fprintf(stderr, "Could not guess output format from the given path and no --format given.\n"); + exit(1); +} + int main (int argc, char **argv) { @@ -2288,10 +2325,7 @@ Parameters are separated by a blank from single-character options (e.g.\n\ } if (output_format == OUTPUT_UNKNOWN) - { - printf("Output format is not set, using pnm as a default.\n"); - output_format = OUTPUT_PNM; - } + output_format = guess_output_format(output_file); if (!devname) { From ee6d6da339459bc8393547005e087e97d3d4987b Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Sun, 28 Apr 2019 23:28:21 +0300 Subject: [PATCH 6/6] scanimage: Update manual page --- doc/scanimage.man | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/doc/scanimage.man b/doc/scanimage.man index 447314058..83624a4f9 100644 --- a/doc/scanimage.man +++ b/doc/scanimage.man @@ -24,6 +24,7 @@ scanimage \- scan an image .RB [ \-\-batch\-double ] .RB [ \-\-accept\-md5\-only ] .RB [ \-p | \-\-progress ] +.RB [ \-o | \-\-output-file ] .RB [ \-n | \-\-dont\-scan ] .RB [ \-T | \-\-test ] .RB [ \-A | \-\-all-options ] @@ -101,7 +102,8 @@ will attempt to open the first available device. The .B \-\-format .I format -option selects how image data is written to standard output. +option selects how image data is written to standard output or the file specified by +the \-\-output\-file option. .I format can be .BR pnm , @@ -111,7 +113,7 @@ or .BR jpeg . If .B \-\-format -is not used, PNM is written. +is not specified, PNM is written by default. .PP The .B \-i @@ -169,6 +171,7 @@ to. Each page is written out to a single file. If .I format is not specified, the default of out%d.pnm (or out%d.tif for \-\-format tiff, out%d.png for \-\-format png or out%d.jpg for \-\- format jpeg) will be used. +This option is incompatible with the \-\-output\-path option. .I format is given as a printf style string with one integer parameter. .B \-\-batch\-start @@ -215,6 +218,16 @@ already been received by (in percent). .PP The +.B \-o +or +.B \-\-output\-file +option requests that +.B scanimage +saves the scanning output to the given path. This option is incompatible with the +\-\-batch option. The program will try to guess \-\-format from the file name. +If that is not possible, it will print an error message and exit. +.PP +The .B \-n or .B \-\-dont\-scan