From a336f5ed2f8eb7994db2ea9d55d6b6b8dda67ece Mon Sep 17 00:00:00 2001 From: Andrew Sayers Date: Tue, 30 Mar 2021 12:07:24 +0100 Subject: [PATCH 1/2] xerox_mfp: make JPEG compression user-configurable JPEG compression improves scan time, and some scanners require it for high DPI values. But it loses some data, and we already support toggling it at compile-time. Add an advanced option so users can decide at scan-time. --- backend/xerox_mfp.c | 36 ++++++++++++++++++++++++++++++------ backend/xerox_mfp.h | 2 ++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/backend/xerox_mfp.c b/backend/xerox_mfp.c index 729ecb7d8..0711b0130 100644 --- a/backend/xerox_mfp.c +++ b/backend/xerox_mfp.c @@ -223,6 +223,11 @@ static int isSupportedDevice(struct device __sane_unused__ *dev) #endif } +static int isJPEGEnabled(struct device __sane_unused__ *dev) +{ + return isSupportedDevice(dev) && dev->compressionEnabled; +} + static void dbg_dump(struct device *dev) { int i; @@ -595,6 +600,22 @@ static void init_options(struct device *dev) dev->opt[OPT_SOURCE].constraint_type = SANE_CONSTRAINT_STRING_LIST; dev->opt[OPT_SOURCE].constraint.string_list = doc_sources; + dev->opt[OPT_JPEG].name = "jpeg"; + dev->opt[OPT_JPEG].title = SANE_I18N("jpeg compression"); + dev->opt[OPT_JPEG].desc = SANE_I18N("JPEG Image Compression"); + dev->opt[OPT_JPEG].unit = SANE_UNIT_NONE; + dev->opt[OPT_JPEG].type = SANE_TYPE_BOOL; + dev->opt[OPT_JPEG].cap |= SANE_CAP_ADVANCED; +#ifdef HAVE_LIBJPEG + dev->compressionEnabled = SANE_TRUE; + if (!isSupportedDevice(dev)) + dev->opt[OPT_JPEG].cap |= SANE_CAP_INACTIVE; + dev->val[OPT_JPEG].b = SANE_TRUE; +#else + dev->opt[OPT_JPEG].cap |= SANE_CAP_INACTIVE; + dev->val[OPT_JPEG].b = SANE_FALSE; +#endif + dev->opt[OPT_GROUP_GEO].name = SANE_NAME_GEOMETRY; dev->opt[OPT_GROUP_GEO].title = SANE_TITLE_GEOMETRY; dev->opt[OPT_GROUP_GEO].desc = SANE_DESC_GEOMETRY; @@ -647,7 +668,10 @@ static void set_parameters(struct device *dev) dev->para.pixels_per_line = dev->win_width / px_to_len; dev->para.bytes_per_line = dev->para.pixels_per_line; - if (!isSupportedDevice(dev)) { + DBG(5, dev->val[OPT_JPEG].b ? "JPEG compression enabled\n" : "JPEG compression disabled\n" ); + dev->compressionEnabled = dev->val[OPT_JPEG].b; + + if (!isJPEGEnabled(dev)) { #if BETTER_BASEDPI px_to_len = 1213.9 / dev->val[OPT_RESOLUTION].w; #endif @@ -776,7 +800,7 @@ static int dev_set_window(struct device *dev) /* Set to JPEG Lossy Compression, if mode is color (only for supported model)... * else go with Uncompressed (For backard compatibility with old models )*/ if (dev->composition == MODE_RGB24) { - if (isSupportedDevice(dev)) { + if (isJPEGEnabled(dev)) { cmd[0x14] = 0x6; } } @@ -1295,7 +1319,7 @@ sane_read(SANE_Handle h, SANE_Byte *buf, SANE_Int maxlen, SANE_Int *lenp) /* copying uncompressed data */ if (dev->composition == MODE_RGB24 && - isSupportedDevice(dev) && + isJPEGEnabled(dev) && dev->decDataSize > 0) { int diff = dev->total_img_size - dev->total_out_size; int bufLen = (diff < maxlen) ? diff : maxlen; @@ -1322,7 +1346,7 @@ sane_read(SANE_Handle h, SANE_Byte *buf, SANE_Int maxlen, SANE_Int *lenp) /* this will never happen */ DBG(1, "image overflow %d bytes\n", dev->total_img_size - dev->total_out_size); } - if (isSupportedDevice(dev) && + if (isJPEGEnabled(dev) && dev->composition == MODE_RGB24) { remove(encTmpFileName); } @@ -1377,7 +1401,7 @@ sane_read(SANE_Handle h, SANE_Byte *buf, SANE_Int maxlen, SANE_Int *lenp) if (buf && lenp) { /* read mode */ /* copy will do minimal of valid data */ if (dev->para.format == SANE_FRAME_RGB && dev->line_order) { - if (isSupportedDevice(dev)) { + if (isJPEGEnabled(dev)) { clrlen = dump_to_tmp_file(dev); /* decompress after reading entire block data*/ if (0 == dev->blocklen) { @@ -1491,7 +1515,7 @@ sane_start(SANE_Handle h) dev->total_img_size = dev->para.bytes_per_line * dev->para.lines; - if (isSupportedDevice(dev) && + if (isJPEGEnabled(dev) && dev->composition == MODE_RGB24) { int fd; remove(encTmpFileName); diff --git a/backend/xerox_mfp.h b/backend/xerox_mfp.h index d85fe1451..6aa83a459 100644 --- a/backend/xerox_mfp.h +++ b/backend/xerox_mfp.h @@ -38,6 +38,7 @@ enum options { OPT_MODE, /* color */ OPT_THRESHOLD, /* brightness */ OPT_SOURCE, /* affects max window size */ + OPT_JPEG, OPT_GROUP_GEO, OPT_SCAN_TL_X, /* for (OPT_SCAN_TL_X to OPT_SCAN_BR_Y) */ OPT_SCAN_TL_Y, @@ -103,6 +104,7 @@ struct device { int doc_source; /* document source */ int threshold; /* brightness */ int compressionTypes; + SANE_Bool compressionEnabled; /* CMD_READ data. It is per block only, image could be in many blocks */ int blocklen; /* image data block len (padding incl.) */ From 1a66f8c553251ef46eec06029b97bfd3bbe5e184 Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Thu, 30 Dec 2021 20:35:03 +0200 Subject: [PATCH 2/2] Add newsfragment --- newsfragments/xerox_mfp_jpeg_configurable.backend | 1 + 1 file changed, 1 insertion(+) create mode 100644 newsfragments/xerox_mfp_jpeg_configurable.backend diff --git a/newsfragments/xerox_mfp_jpeg_configurable.backend b/newsfragments/xerox_mfp_jpeg_configurable.backend new file mode 100644 index 000000000..259dfc143 --- /dev/null +++ b/newsfragments/xerox_mfp_jpeg_configurable.backend @@ -0,0 +1 @@ +xerox_mfp: Implemented an option to configure whether JPEG compression is used.