diff options
author | Jörg Frings-Fürst <debian@jff.email> | 2022-02-07 06:56:10 +0100 |
---|---|---|
committer | Jörg Frings-Fürst <debian@jff.email> | 2022-02-07 06:56:10 +0100 |
commit | 43f33c72804e63306580755049c09d5a81ad83e3 (patch) | |
tree | 805caeb00a832ed96a331907987d4e6a2056a391 /frontend/scanimage.c | |
parent | 0f86c5490ea7c2218a4394bf4c21bf3f9267a47c (diff) | |
parent | 6427d36e269d8c14e6bc55373102a4ebc971ca38 (diff) |
Merge branch 'release/debian/1.1.1-1'debian/1.1.1-1
Diffstat (limited to 'frontend/scanimage.c')
-rw-r--r-- | frontend/scanimage.c | 198 |
1 files changed, 162 insertions, 36 deletions
diff --git a/frontend/scanimage.c b/frontend/scanimage.c index 901a7c8..b587b9d 100644 --- a/frontend/scanimage.c +++ b/frontend/scanimage.c @@ -60,6 +60,10 @@ #include "sicc.h" #include "stiff.h" +#ifdef HAVE_LIBJPEG +#include "jpegtopdf.h" +#endif + #include "../include/md5.h" #ifndef PATH_MAX @@ -119,6 +123,7 @@ static struct option basic_options[] = { #define OUTPUT_TIFF 2 #define OUTPUT_PNG 3 #define OUTPUT_JPEG 4 +#define OUTPUT_PDF 5 #define BASE_OPTSTRING "d:hi:Lf:o:B::nvVTAbp" #define STRIP_HEIGHT 256 /* # lines we increment image height */ @@ -633,6 +638,9 @@ print_option (SANE_Device * device, int opt_num, const SANE_Option_Descriptor *o else if(!(opt->cap & SANE_CAP_SOFT_SELECT) && (opt->cap & SANE_CAP_SOFT_DETECT)) fputs (" [read-only]", stdout); + else if (opt->cap & SANE_CAP_ADVANCED) + fputs (" [advanced]", stdout); + fputs ("\n ", stdout); column = 8; @@ -1328,7 +1336,7 @@ advance (Image * image) } static SANE_Status -scan_it (FILE *ofp) +scan_it (FILE *ofp, void* pw) { int i, len, first_frame = 1, offset = 0, must_buffer = 0; uint64_t hundred_percent = 0; @@ -1354,6 +1362,8 @@ scan_it (FILE *ofp) struct jpeg_error_mgr jerr; #endif + (void)pw; + do { if (!first_frame) @@ -1448,6 +1458,14 @@ scan_it (FILE *ofp) break; #endif #ifdef HAVE_LIBJPEG + case OUTPUT_PDF: + sane_pdf_start_page ( pw, parm.pixels_per_line, parm.lines, + resolution_value, SANE_PDF_IMAGE_COLOR, + SANE_PDF_ROTATE_OFF); + write_jpeg_header (parm.format, parm.pixels_per_line, + parm.lines, resolution_value, + ofp, &cinfo, &jerr); + break; case OUTPUT_JPEG: write_jpeg_header (parm.format, parm.pixels_per_line, parm.lines, resolution_value, @@ -1465,7 +1483,7 @@ scan_it (FILE *ofp) pngbuf = malloc(parm.bytes_per_line); #endif #ifdef HAVE_LIBJPEG - if(output_format == OUTPUT_JPEG) + if(output_format == OUTPUT_JPEG || output_format == OUTPUT_PDF) jpegbuf = malloc(parm.bytes_per_line); #endif @@ -1629,7 +1647,7 @@ scan_it (FILE *ofp) else #endif #ifdef HAVE_LIBJPEG - if (output_format == OUTPUT_JPEG) + if (output_format == OUTPUT_JPEG || output_format == OUTPUT_PDF) { int i = 0; int left = len; @@ -1729,6 +1747,14 @@ scan_it (FILE *ofp) break; #endif #ifdef HAVE_LIBJPEG + case OUTPUT_PDF: + sane_pdf_start_page ( pw, parm.pixels_per_line, parm.lines, + resolution_value, SANE_PDF_IMAGE_COLOR, + SANE_PDF_ROTATE_OFF); + write_jpeg_header (parm.format, parm.pixels_per_line, + parm.lines, resolution_value, + ofp, &cinfo, &jerr); + break; case OUTPUT_JPEG: write_jpeg_header (parm.format, parm.pixels_per_line, parm.lines, resolution_value, @@ -1760,7 +1786,7 @@ scan_it (FILE *ofp) png_write_end(png_ptr, info_ptr); #endif #ifdef HAVE_LIBJPEG - if(output_format == OUTPUT_JPEG) + if(output_format == OUTPUT_JPEG || output_format == OUTPUT_PDF) jpeg_finish_compress(&cinfo); #endif @@ -1775,7 +1801,7 @@ cleanup: } #endif #ifdef HAVE_LIBJPEG - if(output_format == OUTPUT_JPEG) { + if(output_format == OUTPUT_JPEG || output_format == OUTPUT_PDF) { jpeg_destroy_compress(&cinfo); free(jpegbuf); } @@ -2017,7 +2043,8 @@ static int guess_output_format(const char* output_file) { ".jpg", OUTPUT_JPEG }, { ".jpeg", OUTPUT_JPEG }, { ".tiff", OUTPUT_TIFF }, - { ".tif", OUTPUT_TIFF } + { ".tif", OUTPUT_TIFF }, + { ".pdf", OUTPUT_PDF } }; for (unsigned i = 0; i < sizeof(formats) / sizeof(formats[0]); ++i) { @@ -2052,6 +2079,7 @@ main (int argc, char **argv) SANE_Status status; char *full_optstring; SANE_Int version_code; + void *pw = NULL; FILE *ofp = NULL; buffer_size = (32 * 1024); /* default size */ @@ -2155,6 +2183,15 @@ main (int argc, char **argv) exit(1); #endif } + else if (strcmp (optarg, "pdf") == 0) + { +#ifdef HAVE_LIBJPEG + output_format = OUTPUT_PDF; +#else + fprintf(stderr, "PDF support not compiled in\n"); + exit(1); +#endif + } else if (strcmp (optarg, "pnm") == 0) { output_format = OUTPUT_PNM; @@ -2308,7 +2345,7 @@ standard output.\n\ Parameters are separated by a blank from single-character options (e.g.\n\ -d epson) and by a \"=\" from multi-character options (e.g. --device-name=epson).\n\ -d, --device-name=DEVICE use a given scanner device (e.g. hp:/dev/scanner)\n\ - --format=pnm|tiff|png|jpeg file format of output file\n\ + --format=pnm|tiff|png|jpeg|pdf file format of output file\n\ -i, --icc-profile=PROFILE include this ICC profile into TIFF file\n", prog_name); printf ("\ -L, --list-devices show available scanner devices\n\ @@ -2622,6 +2659,9 @@ List of available devices:", prog_name); break; #endif #ifdef HAVE_LIBJPEG + case OUTPUT_PDF: + format = "out%d.pdf"; + break; case OUTPUT_JPEG: format = "out%d.jpg"; break; @@ -2642,6 +2682,13 @@ List of available devices:", prog_name); scanimage_exit(1); } } +#ifdef HAVE_LIBJPEG + if (output_format == OUTPUT_PDF) + { + sane_pdf_open(&pw, ofp ); + sane_pdf_start_doc( pw ); + } +#endif } if (batch) @@ -2667,11 +2714,14 @@ List of available devices:", prog_name); { char path[PATH_MAX]; char part_path[PATH_MAX]; - if (batch) /* format is NULL unless batch mode */ + if (batch) /* format is NULL unless batch mode */ { sprintf (path, format, n); /* love --(C++) */ strcpy (part_path, path); - strcat (part_path, ".part"); +#ifdef HAVE_LIBJPEG + if (output_format != OUTPUT_PDF) +#endif + strcat (part_path, ".part"); } @@ -2689,6 +2739,13 @@ List of available devices:", prog_name); { if (ofp) { +#ifdef HAVE_LIBJPEG + if (output_format == OUTPUT_PDF) + { + sane_pdf_end_doc( pw ); + sane_pdf_close ( pw ); + } +#endif fclose (ofp); ofp = NULL; } @@ -2711,8 +2768,15 @@ List of available devices:", prog_name); { fprintf (stderr, "%s: sane_start: %s\n", prog_name, sane_strstatus (status)); - if (ofp) + if (ofp ) { +#ifdef HAVE_LIBJPEG + if (output_format == OUTPUT_PDF) + { + sane_pdf_end_doc( pw ); + sane_pdf_close ( pw ); + } +#endif fclose (ofp); ofp = NULL; } @@ -2723,15 +2787,42 @@ List of available devices:", prog_name); /* write to .part file while scanning is in progress */ if (batch) { - if (NULL == (ofp = fopen (part_path, "w"))) +#ifdef HAVE_LIBJPEG + SANE_Bool init_pdf = SANE_FALSE; +#endif + if (ofp == NULL) + { + ofp = fopen (part_path, "w"); +#ifdef HAVE_LIBJPEG + if (output_format == OUTPUT_PDF && ofp != NULL) + init_pdf = SANE_TRUE; +#endif + } + if (NULL == ofp) { fprintf (stderr, "cannot open %s\n", part_path); sane_cancel (device); return SANE_STATUS_ACCESS_DENIED; } +#ifdef HAVE_LIBJPEG + if (init_pdf ) + { + sane_pdf_open( &pw, ofp ); + sane_pdf_start_doc ( pw ); + } +#endif + } + + status = scan_it (ofp, pw); + +#ifdef HAVE_LIBJPEG + if (output_format == OUTPUT_PDF) + { + sane_pdf_end_page( pw ); + fflush( ofp ); } +#endif - status = scan_it (ofp); if (batch) { fprintf (stderr, "Scanned page %d.", n); @@ -2745,32 +2836,47 @@ List of available devices:", prog_name); status = SANE_STATUS_GOOD; if (batch) { - if (!ofp || 0 != fclose(ofp)) - { - fprintf (stderr, "cannot close image file\n"); - sane_cancel (device); - return SANE_STATUS_ACCESS_DENIED; - } - else +#ifdef HAVE_LIBJPEG + if (output_format != OUTPUT_PDF) { - ofp = NULL; - /* let the fully scanned file show up */ - if (rename (part_path, path)) - { - fprintf (stderr, "cannot rename %s to %s\n", - part_path, path); - sane_cancel (device); - return SANE_STATUS_ACCESS_DENIED; - } - if (batch_print) - { - fprintf (stdout, "%s\n", path); - fflush (stdout); - } +#endif + if (!ofp || 0 != fclose(ofp)) + { + fprintf (stderr, "cannot close image file\n"); + sane_cancel (device); + return SANE_STATUS_ACCESS_DENIED; + } + else + { + ofp = NULL; + /* let the fully scanned file show up */ + if (rename (part_path, path)) + { + fprintf (stderr, "cannot rename %s to %s\n", + part_path, path); + sane_cancel (device); + return SANE_STATUS_ACCESS_DENIED; + } + if (batch_print) + { + fprintf (stdout, "%s\n", path); + fflush (stdout); + } + } +#ifdef HAVE_LIBJPEG } +#endif } else { +#ifdef HAVE_LIBJPEG + if (output_format == OUTPUT_PDF) + { + sane_pdf_end_doc( pw ); + fflush( ofp ); + sane_pdf_close ( pw ); + } +#endif if (output_file && ofp) { fclose(ofp); @@ -2783,13 +2889,20 @@ List of available devices:", prog_name); { if (ofp) { - fclose (ofp); - ofp = NULL; - } + fclose (ofp); + ofp = NULL; + } unlink (part_path); } else { +#ifdef HAVE_LIBJPEG + if (output_format == OUTPUT_PDF) + { + sane_pdf_end_doc( pw ); + sane_pdf_close ( pw ); + } +#endif if (output_file && ofp) { fclose(ofp); @@ -2807,6 +2920,19 @@ List of available devices:", prog_name); if (batch) { +#ifdef HAVE_LIBJPEG + if (output_format == OUTPUT_PDF) + { + if (output_file && ofp) + { + sane_pdf_end_doc( pw ); + fflush( ofp ); + sane_pdf_close ( pw ); + fclose(ofp); + ofp = NULL; + } + } +#endif int num_pgs = (n - batch_start_at) / batch_increment; fprintf (stderr, "Batch terminated, %d page%s scanned\n", num_pgs, num_pgs == 1 ? "" : "s"); |