From 58df3795ee7f6504b65e7fac35c5fe5a6d53e1ec Mon Sep 17 00:00:00 2001 From: Thierry HUCHARD Date: Mon, 27 Jan 2020 20:47:56 +0100 Subject: [PATCH 01/14] Fix geometry. --- backend/escl/escl.c | 82 +++++++++++++++++++++++++++++++++------------ 1 file changed, 60 insertions(+), 22 deletions(-) diff --git a/backend/escl/escl.c b/backend/escl/escl.c index 982b7a505..db352f33a 100644 --- a/backend/escl/escl.c +++ b/backend/escl/escl.c @@ -392,12 +392,12 @@ init_options(SANE_String_Const name, escl_sane_t *s) s->opt[i].size = sizeof (SANE_Word); s->opt[i].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT; } - s->x_range.min = 0; - s->x_range.max = s->scanner->MaxWidth - s->scanner->MinWidth; - s->x_range.quant = 1; - s->y_range.min = 0; - s->y_range.max = s->scanner->MaxHeight - s->scanner->MinHeight; - s->y_range.quant = 1; + s->x_range.min = s->scanner->MinWidth; + s->x_range.max = s->scanner->MaxWidth; + s->x_range.quant = 0; + s->y_range.min = s->scanner->MinHeight; + s->y_range.max = s->scanner->MaxHeight; + s->y_range.quant = 0; s->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS; s->opt[OPT_NUM_OPTS].desc = SANE_DESC_NUM_OPTIONS; s->opt[OPT_NUM_OPTS].type = SANE_TYPE_INT; @@ -453,7 +453,7 @@ init_options(SANE_String_Const name, escl_sane_t *s) s->val[OPT_GRAY_PREVIEW].w = SANE_FALSE; s->opt[OPT_GEOMETRY_GROUP].title = SANE_TITLE_GEOMETRY; - s->opt[OPT_GEOMETRY_GROUP].desc = ""; + s->opt[OPT_GEOMETRY_GROUP].desc = SANE_DESC_GEOMETRY; s->opt[OPT_GEOMETRY_GROUP].type = SANE_TYPE_GROUP; s->opt[OPT_GEOMETRY_GROUP].cap = SANE_CAP_ADVANCED; s->opt[OPT_GEOMETRY_GROUP].constraint_type = SANE_CONSTRAINT_NONE; @@ -462,37 +462,45 @@ init_options(SANE_String_Const name, escl_sane_t *s) s->opt[OPT_TL_X].title = SANE_TITLE_SCAN_TL_X; s->opt[OPT_TL_X].desc = SANE_DESC_SCAN_TL_X; s->opt[OPT_TL_X].type = SANE_TYPE_FIXED; + s->opt[OPT_TL_X].size = sizeof(SANE_Fixed); + s->opt[OPT_TL_X].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT; s->opt[OPT_TL_X].unit = SANE_UNIT_PIXEL; s->opt[OPT_TL_X].constraint_type = SANE_CONSTRAINT_RANGE; s->opt[OPT_TL_X].constraint.range = &s->x_range; - s->val[OPT_TL_X].w = s->scanner->RiskyLeftMargin; + s->val[OPT_TL_X].w = s->x_range.min; s->opt[OPT_TL_Y].name = SANE_NAME_SCAN_TL_Y; s->opt[OPT_TL_Y].title = SANE_TITLE_SCAN_TL_Y; s->opt[OPT_TL_Y].desc = SANE_DESC_SCAN_TL_Y; s->opt[OPT_TL_Y].type = SANE_TYPE_FIXED; + s->opt[OPT_TL_Y].size = sizeof(SANE_Fixed); + s->opt[OPT_TL_Y].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT; s->opt[OPT_TL_Y].unit = SANE_UNIT_PIXEL; s->opt[OPT_TL_Y].constraint_type = SANE_CONSTRAINT_RANGE; s->opt[OPT_TL_Y].constraint.range = &s->y_range; - s->val[OPT_TL_Y].w = s->scanner->RiskyTopMargin; + s->val[OPT_TL_Y].w = s->y_range.min; s->opt[OPT_BR_X].name = SANE_NAME_SCAN_BR_X; s->opt[OPT_BR_X].title = SANE_TITLE_SCAN_BR_X; s->opt[OPT_BR_X].desc = SANE_DESC_SCAN_BR_X; s->opt[OPT_BR_X].type = SANE_TYPE_FIXED; + s->opt[OPT_BR_X].size = sizeof(SANE_Fixed); + s->opt[OPT_BR_X].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT; s->opt[OPT_BR_X].unit = SANE_UNIT_PIXEL; s->opt[OPT_BR_X].constraint_type = SANE_CONSTRAINT_RANGE; s->opt[OPT_BR_X].constraint.range = &s->x_range; - s->val[OPT_BR_X].w = s->scanner->MaxWidth; + s->val[OPT_BR_X].w = s->x_range.max; s->opt[OPT_BR_Y].name = SANE_NAME_SCAN_BR_Y; s->opt[OPT_BR_Y].title = SANE_TITLE_SCAN_BR_Y; s->opt[OPT_BR_Y].desc = SANE_DESC_SCAN_BR_Y; s->opt[OPT_BR_Y].type = SANE_TYPE_FIXED; + s->opt[OPT_BR_Y].size = sizeof(SANE_Fixed); + s->opt[OPT_BR_Y].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT; s->opt[OPT_BR_Y].unit = SANE_UNIT_PIXEL; s->opt[OPT_BR_Y].constraint_type = SANE_CONSTRAINT_RANGE; s->opt[OPT_BR_Y].constraint.range = &s->y_range; - s->val[OPT_BR_Y].w = s->scanner->MaxHeight; + s->val[OPT_BR_Y].w = s->y_range.max; return (status); } @@ -599,7 +607,7 @@ sane_get_option_descriptor(SANE_Handle h, SANE_Int n) if ((unsigned) n >= NUM_OPTIONS || n < 0) return (0); - return (s->opt + n); + return (&s->opt[n]); } /** @@ -627,12 +635,24 @@ sane_control_option(SANE_Handle h, SANE_Int n, SANE_Action a, void *v, SANE_Int return (SANE_STATUS_INVAL); if (a == SANE_ACTION_GET_VALUE) { switch (n) { + case OPT_TL_X: + printf("OPT_TL_X : %d\n", (int)handler->val[n].w); + *(SANE_Word *) v = handler->val[n].w; + break; + case OPT_TL_Y: + printf("OPT_TL_Y : %d\n", (int)handler->val[n].w); + *(SANE_Word *) v = handler->val[n].w; + break; + case OPT_BR_X: + printf("OPT_BR_X : %d\n", (int)handler->val[n].w); + *(SANE_Word *) v = handler->val[n].w; + break; + case OPT_BR_Y: + printf("OPT_BR_Y : %d\n", (int)handler->val[n].w); + *(SANE_Word *) v = handler->val[n].w; + break; case OPT_NUM_OPTS: case OPT_RESOLUTION: - case OPT_TL_X: - case OPT_TL_Y: - case OPT_BR_X: - case OPT_BR_Y: case OPT_PREVIEW: case OPT_GRAY_PREVIEW: *(SANE_Word *) v = handler->val[n].w; @@ -649,17 +669,33 @@ sane_control_option(SANE_Handle h, SANE_Int n, SANE_Action a, void *v, SANE_Int if (a == SANE_ACTION_SET_VALUE) { switch (n) { case OPT_TL_X: + handler->val[n].w = *(SANE_Word *) v; + printf("OPT_TL_X : %d\n", (int)handler->val[n].w); + if (i) + *i |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS | SANE_INFO_INEXACT; + break; case OPT_TL_Y: + handler->val[n].w = *(SANE_Word *) v; + printf("OPT_TL_Y : %d\n", (int)handler->val[n].w); + if (i) + *i |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS | SANE_INFO_INEXACT; + break; case OPT_BR_X: + handler->val[n].w = *(SANE_Word *) v; + printf("OPT_BR_X : %d\n", (int)handler->val[n].w); + if (i) + *i |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS | SANE_INFO_INEXACT; + break; case OPT_BR_Y: + handler->val[n].w = *(SANE_Word *) v; + printf("OPT_BR_Y : %d\n", (int)handler->val[n].w); + if (i) + *i |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS | SANE_INFO_INEXACT; + break; + case OPT_NUM_OPTS: + case OPT_RESOLUTION: case OPT_PREVIEW: case OPT_GRAY_PREVIEW: - handler->val[n].w = *(SANE_Word *) v; - if (i && handler->val[n].w != *(SANE_Word *) v) - *i |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS | SANE_INFO_INEXACT; - handler->val[n].w = *(SANE_Word *) v; - break; - case OPT_RESOLUTION: handler->val[n].w = *(SANE_Word *) v; if (i) *i |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS | SANE_INFO_INEXACT; @@ -709,6 +745,7 @@ sane_start(SANE_Handle h) handler->scanner->width = handler->val[OPT_BR_X].w; handler->scanner->pos_x = handler->val[OPT_TL_X].w; handler->scanner->pos_y = handler->val[OPT_TL_Y].w; + fprintf(stdout, "1-Size Image [%dx%d|%dx%d]\n", handler->scanner->pos_x, handler->scanner->pos_y, handler->scanner->width, handler->scanner->height); if(handler->scanner->default_color) free(handler->scanner->default_color); if (handler->val[OPT_PREVIEW].w == SANE_TRUE) @@ -763,6 +800,7 @@ sane_start(SANE_Handle h) else return SANE_STATUS_INVAL; + fprintf(stdout, "2-Size Image [%dx%d|%dx%d]\n", 0, 0, w, he); if (status != SANE_STATUS_GOOD) return (status); handler->ps.depth = 8; From 8c15724b432d6b60c30de04cf9ad8fa825f3b7da Mon Sep 17 00:00:00 2001 From: Thierry HUCHARD Date: Fri, 31 Jan 2020 17:19:42 +0100 Subject: [PATCH 02/14] Added milimeter-to-pixel and pixel-to-milimeter conversion function. --- backend/Makefile.am | 2 +- backend/escl/escl.h | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/backend/Makefile.am b/backend/Makefile.am index b0c7e182c..c375844cf 100644 --- a/backend/Makefile.am +++ b/backend/Makefile.am @@ -443,7 +443,7 @@ libescl_la_CPPFLAGS = $(AM_CPPFLAGS) $(JPEG_CFLAGS) $(PNG_CFLAGS) $(TIFF_CFLAGS) nodist_libsane_escl_la_SOURCES = escl-s.c libsane_escl_la_CPPFLAGS = $(AM_CPPFLAGS) $(JPEG_CFLAGS) $(PNG_CFLAGS) $(TIFF_CFLAGS) $(XML_CFLAGS) $(libcurl_CFLAGS) $(AVAHI_CFLAGS) -DBACKEND_NAME=escl libsane_escl_la_LDFLAGS = $(DIST_SANELIBS_LDFLAGS) -libsane_escl_la_LIBADD = $(COMMON_LIBS) libescl.la ../sanei/sanei_init_debug.lo ../sanei/sanei_constrain_value.lo ../sanei/sanei_config.lo sane_strstatus.lo $(JPEG_LIBS) $(PNG_LIBS) $(TIFF_LIBS) $(XML_LIBS) $(libcurl_LIBS) $(AVAHI_LIBS) +libsane_escl_la_LIBADD = $(COMMON_LIBS) libescl.la ../sanei/sanei_init_debug.lo ../sanei/sanei_constrain_value.lo ../sanei/sanei_config.lo sane_strstatus.lo $(MATH_LIB) $(JPEG_LIBS) $(PNG_LIBS) $(TIFF_LIBS) $(XML_LIBS) $(libcurl_LIBS) $(AVAHI_LIBS) endif endif endif diff --git a/backend/escl/escl.h b/backend/escl/escl.h index 82910bdfe..6fe5d86d5 100644 --- a/backend/escl/escl.h +++ b/backend/escl/escl.h @@ -43,6 +43,7 @@ #include "../include/sane/sane.h" #include +#include #ifndef BACKEND_NAME #define BACKEND_NAME escl @@ -151,6 +152,30 @@ enum NUM_OPTIONS }; +/** + * \fn static inline SANE_Fixed milimeter_to_pixel(SANE_Word pixels) + * \brief Function Function that converts pixels into millimeters. + * + * \return SANE_Fixed (Value in pixels) + */ +static inline SANE_Fixed +pixels_to_milimeters (SANE_Word pixels) +{ + return SANE_FIX((double) pixels * 25.4 / 300.0); +} + +/** + * \fn static inline SANE_Word milimeter_to_pixel(SANE_Fixed milimeter) + * \brief Function Function that converts millimeters into pixels. + * + * \return SANE_Word (Value in milimeters) + */ +static inline SANE_Word +milimeters_to_pixels (SANE_Fixed milimeters) +{ + return (SANE_Word) roundl(SANE_UNFIX(milimeters) * 300.0 / 25.4); +} + ESCL_Device *escl_devices(SANE_Status *status); SANE_Status escl_device_add(int port_nb, const char *model_name, char *ip_address, char *type); SANE_Status escl_status(SANE_String_Const name); From d42521dfd4875f54b089b60de2866146a03d0a9b Mon Sep 17 00:00:00 2001 From: Thierry HUCHARD Date: Fri, 31 Jan 2020 17:42:52 +0100 Subject: [PATCH 03/14] Geometry correction and use of the milimeter as a unit of measurement. --- backend/escl/escl.c | 75 ++++++++++++++------------------------------- 1 file changed, 23 insertions(+), 52 deletions(-) diff --git a/backend/escl/escl.c b/backend/escl/escl.c index db352f33a..6e6dd4ba5 100644 --- a/backend/escl/escl.c +++ b/backend/escl/escl.c @@ -392,11 +392,11 @@ init_options(SANE_String_Const name, escl_sane_t *s) s->opt[i].size = sizeof (SANE_Word); s->opt[i].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT; } - s->x_range.min = s->scanner->MinWidth; - s->x_range.max = s->scanner->MaxWidth; + s->x_range.min = pixels_to_milimeters(s->scanner->MinWidth); + s->x_range.max = pixels_to_milimeters(s->scanner->MaxWidth); s->x_range.quant = 0; - s->y_range.min = s->scanner->MinHeight; - s->y_range.max = s->scanner->MaxHeight; + s->y_range.min = pixels_to_milimeters(s->scanner->MinHeight); + s->y_range.max = pixels_to_milimeters(s->scanner->MaxHeight); s->y_range.quant = 0; s->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS; s->opt[OPT_NUM_OPTS].desc = SANE_DESC_NUM_OPTIONS; @@ -464,10 +464,9 @@ init_options(SANE_String_Const name, escl_sane_t *s) s->opt[OPT_TL_X].type = SANE_TYPE_FIXED; s->opt[OPT_TL_X].size = sizeof(SANE_Fixed); s->opt[OPT_TL_X].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT; - s->opt[OPT_TL_X].unit = SANE_UNIT_PIXEL; + s->opt[OPT_TL_X].unit = SANE_UNIT_MM; s->opt[OPT_TL_X].constraint_type = SANE_CONSTRAINT_RANGE; s->opt[OPT_TL_X].constraint.range = &s->x_range; - s->val[OPT_TL_X].w = s->x_range.min; s->opt[OPT_TL_Y].name = SANE_NAME_SCAN_TL_Y; s->opt[OPT_TL_Y].title = SANE_TITLE_SCAN_TL_Y; @@ -475,10 +474,9 @@ init_options(SANE_String_Const name, escl_sane_t *s) s->opt[OPT_TL_Y].type = SANE_TYPE_FIXED; s->opt[OPT_TL_Y].size = sizeof(SANE_Fixed); s->opt[OPT_TL_Y].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT; - s->opt[OPT_TL_Y].unit = SANE_UNIT_PIXEL; + s->opt[OPT_TL_Y].unit = SANE_UNIT_MM; s->opt[OPT_TL_Y].constraint_type = SANE_CONSTRAINT_RANGE; s->opt[OPT_TL_Y].constraint.range = &s->y_range; - s->val[OPT_TL_Y].w = s->y_range.min; s->opt[OPT_BR_X].name = SANE_NAME_SCAN_BR_X; s->opt[OPT_BR_X].title = SANE_TITLE_SCAN_BR_X; @@ -486,10 +484,9 @@ init_options(SANE_String_Const name, escl_sane_t *s) s->opt[OPT_BR_X].type = SANE_TYPE_FIXED; s->opt[OPT_BR_X].size = sizeof(SANE_Fixed); s->opt[OPT_BR_X].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT; - s->opt[OPT_BR_X].unit = SANE_UNIT_PIXEL; + s->opt[OPT_BR_X].unit = SANE_UNIT_MM; s->opt[OPT_BR_X].constraint_type = SANE_CONSTRAINT_RANGE; s->opt[OPT_BR_X].constraint.range = &s->x_range; - s->val[OPT_BR_X].w = s->x_range.max; s->opt[OPT_BR_Y].name = SANE_NAME_SCAN_BR_Y; s->opt[OPT_BR_Y].title = SANE_TITLE_SCAN_BR_Y; @@ -497,10 +494,9 @@ init_options(SANE_String_Const name, escl_sane_t *s) s->opt[OPT_BR_Y].type = SANE_TYPE_FIXED; s->opt[OPT_BR_Y].size = sizeof(SANE_Fixed); s->opt[OPT_BR_Y].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT; - s->opt[OPT_BR_Y].unit = SANE_UNIT_PIXEL; + s->opt[OPT_BR_Y].unit = SANE_UNIT_MM; s->opt[OPT_BR_Y].constraint_type = SANE_CONSTRAINT_RANGE; s->opt[OPT_BR_Y].constraint.range = &s->y_range; - s->val[OPT_BR_Y].w = s->y_range.max; return (status); } @@ -542,8 +538,8 @@ sane_open(SANE_String_Const name, SANE_Handle *h) handler->ps.depth = 8; handler->ps.last_frame = SANE_TRUE; handler->ps.format = SANE_FRAME_RGB; - handler->ps.pixels_per_line = handler->val[OPT_BR_X].w; - handler->ps.lines = handler->val[OPT_BR_Y].w; + handler->ps.pixels_per_line = milimeters_to_pixels(handler->val[OPT_BR_X].w); + handler->ps.lines = milimeters_to_pixels(handler->val[OPT_BR_Y].w); handler->ps.bytes_per_line = handler->ps.pixels_per_line * 3; status = sane_get_parameters(handler, 0); if (status != SANE_STATUS_GOOD) @@ -636,21 +632,9 @@ sane_control_option(SANE_Handle h, SANE_Int n, SANE_Action a, void *v, SANE_Int if (a == SANE_ACTION_GET_VALUE) { switch (n) { case OPT_TL_X: - printf("OPT_TL_X : %d\n", (int)handler->val[n].w); - *(SANE_Word *) v = handler->val[n].w; - break; case OPT_TL_Y: - printf("OPT_TL_Y : %d\n", (int)handler->val[n].w); - *(SANE_Word *) v = handler->val[n].w; - break; case OPT_BR_X: - printf("OPT_BR_X : %d\n", (int)handler->val[n].w); - *(SANE_Word *) v = handler->val[n].w; - break; case OPT_BR_Y: - printf("OPT_BR_Y : %d\n", (int)handler->val[n].w); - *(SANE_Word *) v = handler->val[n].w; - break; case OPT_NUM_OPTS: case OPT_RESOLUTION: case OPT_PREVIEW: @@ -669,29 +653,9 @@ sane_control_option(SANE_Handle h, SANE_Int n, SANE_Action a, void *v, SANE_Int if (a == SANE_ACTION_SET_VALUE) { switch (n) { case OPT_TL_X: - handler->val[n].w = *(SANE_Word *) v; - printf("OPT_TL_X : %d\n", (int)handler->val[n].w); - if (i) - *i |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS | SANE_INFO_INEXACT; - break; case OPT_TL_Y: - handler->val[n].w = *(SANE_Word *) v; - printf("OPT_TL_Y : %d\n", (int)handler->val[n].w); - if (i) - *i |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS | SANE_INFO_INEXACT; - break; case OPT_BR_X: - handler->val[n].w = *(SANE_Word *) v; - printf("OPT_BR_X : %d\n", (int)handler->val[n].w); - if (i) - *i |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS | SANE_INFO_INEXACT; - break; case OPT_BR_Y: - handler->val[n].w = *(SANE_Word *) v; - printf("OPT_BR_Y : %d\n", (int)handler->val[n].w); - if (i) - *i |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS | SANE_INFO_INEXACT; - break; case OPT_NUM_OPTS: case OPT_RESOLUTION: case OPT_PREVIEW: @@ -741,11 +705,6 @@ sane_start(SANE_Handle h) handler->write_scan_data = SANE_FALSE; handler->decompress_scan_data = SANE_FALSE; handler->end_read = SANE_FALSE; - handler->scanner->height = handler->val[OPT_BR_Y].w; - handler->scanner->width = handler->val[OPT_BR_X].w; - handler->scanner->pos_x = handler->val[OPT_TL_X].w; - handler->scanner->pos_y = handler->val[OPT_TL_Y].w; - fprintf(stdout, "1-Size Image [%dx%d|%dx%d]\n", handler->scanner->pos_x, handler->scanner->pos_y, handler->scanner->width, handler->scanner->height); if(handler->scanner->default_color) free(handler->scanner->default_color); if (handler->val[OPT_PREVIEW].w == SANE_TRUE) @@ -775,6 +734,15 @@ sane_start(SANE_Handle h) else handler->scanner->default_color = strdup("RGB24"); } + handler->scanner->height = milimeters_to_pixels(handler->val[OPT_BR_Y].w); + handler->scanner->width = milimeters_to_pixels(handler->val[OPT_BR_X].w); + handler->scanner->pos_x = milimeters_to_pixels(handler->val[OPT_TL_X].w); + handler->scanner->pos_y = milimeters_to_pixels(handler->val[OPT_TL_Y].w); + DBG(10, "Calculate Size Image [%dx%d|%dx%d]\n", + handler->scanner->pos_x, + handler->scanner->pos_y, + handler->scanner->height, + handler->scanner->width); if (!handler->scanner->default_color) { DBG (10, "Default Color allocation failure.\n"); return (SANE_STATUS_NO_MEM); @@ -797,8 +765,10 @@ sane_start(SANE_Handle h) { status = get_TIFF_data(handler->scanner, &w, &he, &bps); } - else + else { + DBG(10, "Unknow image format\n"); return SANE_STATUS_INVAL; + } fprintf(stdout, "2-Size Image [%dx%d|%dx%d]\n", 0, 0, w, he); if (status != SANE_STATUS_GOOD) @@ -809,6 +779,7 @@ sane_start(SANE_Handle h) handler->ps.bytes_per_line = w * bps; handler->ps.last_frame = SANE_TRUE; handler->ps.format = SANE_FRAME_RGB; + DBG(10, "Real Size Image [%dx%d|%dx%d]\n", 0, 0, w, he); return (status); } From 5298b0e0186eb74f16996fa962271f0b6c91e54d Mon Sep 17 00:00:00 2001 From: Thierry HUCHARD Date: Fri, 31 Jan 2020 18:28:04 +0100 Subject: [PATCH 04/14] Setting up the cut-out of the received image. --- backend/escl/escl_jpeg.c | 43 ++++++++++++++---- backend/escl/escl_png.c | 94 ++++++++++++++++++++++++++++++++-------- backend/escl/escl_tiff.c | 46 ++++++++++++++++++-- 3 files changed, 152 insertions(+), 31 deletions(-) diff --git a/backend/escl/escl_jpeg.c b/backend/escl/escl_jpeg.c index d6287efb0..e284863d1 100644 --- a/backend/escl/escl_jpeg.c +++ b/backend/escl/escl_jpeg.c @@ -162,6 +162,11 @@ get_JPEG_data(capabilities_t *scanner, int *w, int *h, int *bps) unsigned char *surface = NULL; struct my_error_mgr jerr; int lineSize = 0; + JDIMENSION x_off = 0; + JDIMENSION y_off = 0; + JDIMENSION wid = 0; + JDIMENSION hei = 0; + int pos = 0; if (scanner->tmp == NULL) return (SANE_STATUS_INVAL); @@ -174,6 +179,7 @@ get_JPEG_data(capabilities_t *scanner, int *w, int *h, int *bps) jpeg_destroy_decompress(&cinfo); if (surface != NULL) free(surface); + fseek(scanner->tmp, start, SEEK_SET); DBG( 1, "Escl Jpeg : Error reading jpeg\n"); if (scanner->tmp) { fclose(scanner->tmp); @@ -187,10 +193,23 @@ get_JPEG_data(capabilities_t *scanner, int *w, int *h, int *bps) cinfo.out_color_space = JCS_RGB; cinfo.quantize_colors = FALSE; jpeg_calc_output_dimensions(&cinfo); - surface = malloc(cinfo.output_width * cinfo.output_height * cinfo.output_components); + if (cinfo.output_width < (unsigned int)scanner->width) + scanner->width = cinfo.output_width; + if (scanner->pos_x < 0) + scanner->pos_x = 0; + + if (cinfo.output_height < (unsigned int)scanner->height) + scanner->height = cinfo.output_height; + if (scanner->pos_x < 0) + scanner->pos_x = 0; + + x_off = scanner->pos_x; + wid = scanner->width - x_off; + y_off = scanner->pos_y; + hei = scanner->height - y_off; + surface = malloc(wid * hei * cinfo.output_components); if (surface == NULL) { jpeg_destroy_decompress(&cinfo); - fseek(scanner->tmp, start, SEEK_SET); DBG( 1, "Escl Jpeg : Memory allocation problem\n"); if (scanner->tmp) { fclose(scanner->tmp); @@ -198,17 +217,23 @@ get_JPEG_data(capabilities_t *scanner, int *w, int *h, int *bps) } return (SANE_STATUS_NO_MEM); } - lineSize = cinfo.output_width * cinfo.output_components; jpeg_start_decompress(&cinfo); - while (cinfo.output_scanline < cinfo.output_height) { - rowptr[0] = (JSAMPROW)surface + (lineSize * cinfo.output_scanline); + if (x_off > 0 || wid < cinfo.output_width) + jpeg_crop_scanline(&cinfo, &x_off, &wid); + lineSize = wid * cinfo.output_components; + if (y_off > 0) + jpeg_skip_scanlines(&cinfo, y_off); + pos = 0; + while (cinfo.output_scanline < (unsigned int)scanner->height) { + rowptr[0] = (JSAMPROW)surface + (lineSize * pos); // ..cinfo.output_scanline); jpeg_read_scanlines(&cinfo, rowptr, (JDIMENSION) 1); - } + pos++; + } scanner->img_data = surface; - scanner->img_size = lineSize * cinfo.output_height; + scanner->img_size = lineSize * hei; scanner->img_read = 0; - *w = cinfo.output_width; - *h = cinfo.output_height; + *w = wid; + *h = hei; *bps = cinfo.output_components; jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); diff --git a/backend/escl/escl_png.c b/backend/escl/escl_png.c index 18f6f3510..6d105ed47 100644 --- a/backend/escl/escl_png.c +++ b/backend/escl/escl_png.c @@ -55,8 +55,13 @@ get_PNG_data(capabilities_t *scanner, int *w, int *h, int *components) unsigned int height = 0; /* hauteur */ int bps = 3; /* composantes d'un texel */ unsigned char *texels = NULL; /* données de l'image */ + unsigned char *surface = NULL; /* données de l'image */ unsigned int i = 0; png_byte magic[8]; + int x_off = 0, x = 0; + int wid = 0; + int y_off = 0, y = 0; + int hei = 0; // read magic number fread (magic, 1, sizeof (magic), scanner->tmp); @@ -154,27 +159,78 @@ get_PNG_data(capabilities_t *scanner, int *w, int *h, int *components) &bit_depth, &color_type, NULL, NULL, NULL); - *w = (int)width; - *h = (int)height; + if (width < (unsigned int)scanner->width) + scanner->width = width; + if (scanner->pos_x < 0) + scanner->pos_x = 0; + + if (height < (unsigned int)scanner->height) + scanner->height = height; + if (scanner->pos_x < 0) + scanner->pos_x = 0; + + x_off = scanner->pos_x; + wid = scanner->width - x_off; + y_off = scanner->pos_y; + hei = scanner->height - y_off; + *w = (int)wid; + *h = (int)hei; *components = bps; - // we can now allocate memory for storing pixel data - texels = (unsigned char *)malloc (sizeof (unsigned char) * width - * height * bps); - png_bytep *row_pointers; - // setup a pointer array. Each one points at the begening of a row. - row_pointers = (png_bytep *)malloc (sizeof (png_bytep) * height); - for (i = 0; i < height; ++i) - { - row_pointers[i] = (png_bytep)(texels + - ((height - (i + 1)) * width * bps)); - } - // read pixel data using row pointers - png_read_image (png_ptr, row_pointers); - // we don't need row pointers anymore - scanner->img_data = texels; - scanner->img_size = (int)(width * height * bps); + // we can now allocate memory for storing pixel data + texels = (unsigned char *)malloc (sizeof (unsigned char) * width + * height * bps); + if (!texels) { + DBG( 1, "Escl Png : texels Memory allocation problem\n"); + if (scanner->tmp) { + fclose(scanner->tmp); + scanner->tmp = NULL; + } + return (SANE_STATUS_NO_MEM); + } + png_bytep *row_pointers; + // setup a pointer array. Each one points at the begening of a row. + row_pointers = (png_bytep *)malloc (sizeof (png_bytep) * height); + if (!row_pointers) { + DBG( 1, "Escl Png : row_pointers Memory allocation problem\n"); + free(texels); + if (scanner->tmp) { + fclose(scanner->tmp); + scanner->tmp = NULL; + } + return (SANE_STATUS_NO_MEM); + } + for (i = 0; i < height; ++i) + { + row_pointers[i] = (png_bytep)(texels + + ((height - (i + 1)) * width * bps)); + } + // read pixel data using row pointers + png_read_image (png_ptr, row_pointers); + if (x_off > 0 || wid < scanner->width || + y_off > 0 || hei < scanner->height) { + surface = (unsigned char *)malloc (sizeof (unsigned char) * wid + * hei * bps); + if (surface) + { + for (y = 0; y < hei; y++) + { + for (x = 0; x < wid; x++) + { + surface[y * wid + x] = texels[(y + y_off) * width + x + x_off]; + } + } + free(texels); + } + else + surface = texels; + } + else + surface = texels; + // we don't need row pointers anymore + scanner->img_data = surface; + scanner->img_size = (int)(wid * hei * bps); scanner->img_read = 0; - free (row_pointers); + free (row_pointers); fclose(scanner->tmp); scanner->tmp = NULL; return (SANE_STATUS_GOOD); diff --git a/backend/escl/escl_tiff.c b/backend/escl/escl_tiff.c index 52aec204a..648134db6 100644 --- a/backend/escl/escl_tiff.c +++ b/backend/escl/escl_tiff.c @@ -56,8 +56,13 @@ get_TIFF_data(capabilities_t *scanner, int *w, int *h, int *components) uint32 width = 0; /* largeur */ uint32 height = 0; /* hauteur */ unsigned char *raster = NULL; /* données de l'image */ + unsigned char *surface = NULL; /* données de l'image */ int bps = 4; uint32 npixels = 0; + int x_off = 0, x = 0; + int wid = 0; + int y_off = 0, y = 0; + int hei = 0; lseek(fileno(scanner->tmp), 0, SEEK_SET); tif = TIFFFdOpen(fileno(scanner->tmp), "temp", "r"); @@ -76,7 +81,7 @@ get_TIFF_data(capabilities_t *scanner, int *w, int *h, int *components) raster = (unsigned char*) malloc(npixels * sizeof (uint32)); if (raster != NULL) { - DBG( 1, "Escl Tiff : Memory allocation problem.\n"); + DBG( 1, "Escl Tiff : raster Memory allocation problem.\n"); if (scanner->tmp) { fclose(scanner->tmp); scanner->tmp = NULL; @@ -93,9 +98,44 @@ get_TIFF_data(capabilities_t *scanner, int *w, int *h, int *components) } return (SANE_STATUS_INVAL); } - *w = (int)width; - *h = (int)height; + + if (width < (unsigned int)scanner->width) + scanner->width = width; + if (scanner->pos_x < 0) + scanner->pos_x = 0; + + if (height < (unsigned int)scanner->height) + scanner->height = height; + if (scanner->pos_x < 0) + scanner->pos_x = 0; + + x_off = scanner->pos_x; + wid = scanner->width - x_off; + y_off = scanner->pos_y; + hei = scanner->height - y_off; + *w = (int)wid; + *h = (int)hei; *components = bps; + if (x_off > 0 || wid < scanner->width || + y_off > 0 || hei < scanner->height) { + surface = (unsigned char *)malloc (sizeof (unsigned char) * wid + * hei * bps); + if (surface) + { + for (y = 0; y < hei; y++) + { + for (x = 0; x < wid; x++) + { + surface[y * wid + x] = raster[(y + y_off) * width + x + x_off]; + } + } + free(raster); + } + else + surface = raster; + } + else + surface = raster; // we don't need row pointers anymore scanner->img_data = raster; scanner->img_size = (int)(width * height * bps); From 0ca9390279415d9da084a1bae3e39d96f84c4f6f Mon Sep 17 00:00:00 2001 From: Thierry HUCHARD Date: Fri, 31 Jan 2020 18:32:32 +0100 Subject: [PATCH 05/14] Fix style. --- backend/escl/escl_tiff.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/escl/escl_tiff.c b/backend/escl/escl_tiff.c index 648134db6..c6fff18ce 100644 --- a/backend/escl/escl_tiff.c +++ b/backend/escl/escl_tiff.c @@ -117,7 +117,7 @@ get_TIFF_data(capabilities_t *scanner, int *w, int *h, int *components) *h = (int)hei; *components = bps; if (x_off > 0 || wid < scanner->width || - y_off > 0 || hei < scanner->height) { + y_off > 0 || hei < scanner->height) { surface = (unsigned char *)malloc (sizeof (unsigned char) * wid * hei * bps); if (surface) From 7ea6af06243c307660138c3e01fde85dd7d32297 Mon Sep 17 00:00:00 2001 From: Thierry HUCHARD Date: Sat, 8 Feb 2020 22:16:16 +0100 Subject: [PATCH 06/14] Replacing unit conversion functions with macros. --- backend/escl/escl.c | 20 ++++++++++---------- backend/escl/escl.h | 25 ++----------------------- 2 files changed, 12 insertions(+), 33 deletions(-) diff --git a/backend/escl/escl.c b/backend/escl/escl.c index 6e6dd4ba5..002206046 100644 --- a/backend/escl/escl.c +++ b/backend/escl/escl.c @@ -392,11 +392,11 @@ init_options(SANE_String_Const name, escl_sane_t *s) s->opt[i].size = sizeof (SANE_Word); s->opt[i].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT; } - s->x_range.min = pixels_to_milimeters(s->scanner->MinWidth); - s->x_range.max = pixels_to_milimeters(s->scanner->MaxWidth); + s->x_range.min = PIXEL_TO_MM(s->scanner->MinWidth, 300.0); + s->x_range.max = PIXEL_TO_MM(s->scanner->MaxWidth, 300.0); s->x_range.quant = 0; - s->y_range.min = pixels_to_milimeters(s->scanner->MinHeight); - s->y_range.max = pixels_to_milimeters(s->scanner->MaxHeight); + s->y_range.min = PIXEL_TO_MM(s->scanner->MinHeight, 300.0); + s->y_range.max = PIXEL_TO_MM(s->scanner->MaxHeight, 300.0); s->y_range.quant = 0; s->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS; s->opt[OPT_NUM_OPTS].desc = SANE_DESC_NUM_OPTIONS; @@ -538,8 +538,8 @@ sane_open(SANE_String_Const name, SANE_Handle *h) handler->ps.depth = 8; handler->ps.last_frame = SANE_TRUE; handler->ps.format = SANE_FRAME_RGB; - handler->ps.pixels_per_line = milimeters_to_pixels(handler->val[OPT_BR_X].w); - handler->ps.lines = milimeters_to_pixels(handler->val[OPT_BR_Y].w); + handler->ps.pixels_per_line = MM_TO_PIXEL(handler->val[OPT_BR_X].w, 300.0); + handler->ps.lines = MM_TO_PIXEL(handler->val[OPT_BR_Y].w, 300.0); handler->ps.bytes_per_line = handler->ps.pixels_per_line * 3; status = sane_get_parameters(handler, 0); if (status != SANE_STATUS_GOOD) @@ -734,10 +734,10 @@ sane_start(SANE_Handle h) else handler->scanner->default_color = strdup("RGB24"); } - handler->scanner->height = milimeters_to_pixels(handler->val[OPT_BR_Y].w); - handler->scanner->width = milimeters_to_pixels(handler->val[OPT_BR_X].w); - handler->scanner->pos_x = milimeters_to_pixels(handler->val[OPT_TL_X].w); - handler->scanner->pos_y = milimeters_to_pixels(handler->val[OPT_TL_Y].w); + handler->scanner->height = MM_TO_PIXEL(handler->val[OPT_BR_Y].w, 300.0); + handler->scanner->width = MM_TO_PIXEL(handler->val[OPT_BR_X].w, 300.0); + handler->scanner->pos_x = MM_TO_PIXEL(handler->val[OPT_TL_X].w, 300.0); + handler->scanner->pos_y = MM_TO_PIXEL(handler->val[OPT_TL_Y].w, 300.0); DBG(10, "Calculate Size Image [%dx%d|%dx%d]\n", handler->scanner->pos_x, handler->scanner->pos_y, diff --git a/backend/escl/escl.h b/backend/escl/escl.h index 6fe5d86d5..72405ec3e 100644 --- a/backend/escl/escl.h +++ b/backend/escl/escl.h @@ -152,29 +152,8 @@ enum NUM_OPTIONS }; -/** - * \fn static inline SANE_Fixed milimeter_to_pixel(SANE_Word pixels) - * \brief Function Function that converts pixels into millimeters. - * - * \return SANE_Fixed (Value in pixels) - */ -static inline SANE_Fixed -pixels_to_milimeters (SANE_Word pixels) -{ - return SANE_FIX((double) pixels * 25.4 / 300.0); -} - -/** - * \fn static inline SANE_Word milimeter_to_pixel(SANE_Fixed milimeter) - * \brief Function Function that converts millimeters into pixels. - * - * \return SANE_Word (Value in milimeters) - */ -static inline SANE_Word -milimeters_to_pixels (SANE_Fixed milimeters) -{ - return (SANE_Word) roundl(SANE_UNFIX(milimeters) * 300.0 / 25.4); -} +#define PIXEL_TO_MM(pixels, dpi) SANE_FIX((SANE_UNFIX(pixels) * (25.4) / dpi)) +#define MM_TO_PIXEL(millimeters, dpi) SANE_FIX((SANE_UNFIX(millimeters) * (dpi) / 25.4)) ESCL_Device *escl_devices(SANE_Status *status); SANE_Status escl_device_add(int port_nb, const char *model_name, char *ip_address, char *type); From af6f6d46559efc0ebdc017d485573449a5d6347b Mon Sep 17 00:00:00 2001 From: Thierry HUCHARD Date: Sat, 8 Feb 2020 22:36:49 +0100 Subject: [PATCH 07/14] Clarification of variable names. --- backend/escl/escl_jpeg.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/backend/escl/escl_jpeg.c b/backend/escl/escl_jpeg.c index e284863d1..178d9e5c6 100644 --- a/backend/escl/escl_jpeg.c +++ b/backend/escl/escl_jpeg.c @@ -154,7 +154,7 @@ output_no_message(j_common_ptr __sane_unused__ cinfo) * \return SANE_STATUS_GOOD (if everything is OK, otherwise, SANE_STATUS_NO_MEM/SANE_STATUS_INVAL) */ SANE_Status -get_JPEG_data(capabilities_t *scanner, int *w, int *h, int *bps) + get_JPEG_data(capabilities_t *scanner, int *width, int *height, int *bps) { int start = 0; struct jpeg_decompress_struct cinfo; @@ -164,8 +164,8 @@ get_JPEG_data(capabilities_t *scanner, int *w, int *h, int *bps) int lineSize = 0; JDIMENSION x_off = 0; JDIMENSION y_off = 0; - JDIMENSION wid = 0; - JDIMENSION hei = 0; + JDIMENSION w = 0; + JDIMENSION h = 0; int pos = 0; if (scanner->tmp == NULL) @@ -204,10 +204,10 @@ get_JPEG_data(capabilities_t *scanner, int *w, int *h, int *bps) scanner->pos_x = 0; x_off = scanner->pos_x; - wid = scanner->width - x_off; + w = scanner->width - x_off; y_off = scanner->pos_y; - hei = scanner->height - y_off; - surface = malloc(wid * hei * cinfo.output_components); + h = scanner->height - y_off; + surface = malloc(w * h * cinfo.output_components); if (surface == NULL) { jpeg_destroy_decompress(&cinfo); DBG( 1, "Escl Jpeg : Memory allocation problem\n"); @@ -218,9 +218,9 @@ get_JPEG_data(capabilities_t *scanner, int *w, int *h, int *bps) return (SANE_STATUS_NO_MEM); } jpeg_start_decompress(&cinfo); - if (x_off > 0 || wid < cinfo.output_width) - jpeg_crop_scanline(&cinfo, &x_off, &wid); - lineSize = wid * cinfo.output_components; + if (x_off > 0 || w < cinfo.output_width) + jpeg_crop_scanline(&cinfo, &x_off, &w); + lineSize = w * cinfo.output_components; if (y_off > 0) jpeg_skip_scanlines(&cinfo, y_off); pos = 0; @@ -230,10 +230,10 @@ get_JPEG_data(capabilities_t *scanner, int *w, int *h, int *bps) pos++; } scanner->img_data = surface; - scanner->img_size = lineSize * hei; + scanner->img_size = lineSize * h; scanner->img_read = 0; - *w = wid; - *h = hei; + *width = w; + *height = h; *bps = cinfo.output_components; jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); @@ -245,8 +245,8 @@ get_JPEG_data(capabilities_t *scanner, int *w, int *h, int *bps) SANE_Status get_JPEG_data(capabilities_t __sane_unused__ *scanner, - int __sane_unused__ *w, - int __sane_unused__ *h, + int __sane_unused__ *width, + int __sane_unused__ *height, int __sane_unused__ *bps) { return (SANE_STATUS_INVAL); From 6a9a0beee89e80bb5fb13cfc7177354a16c061f2 Mon Sep 17 00:00:00 2001 From: Thierry HUCHARD Date: Sun, 9 Feb 2020 09:30:43 +0100 Subject: [PATCH 08/14] Clarification and factoring of the code --- backend/Makefile.am | 2 +- backend/escl/escl.h | 18 ++-- backend/escl/escl_crop.c | 90 +++++++++++++++++++ backend/escl/escl_png.c | 183 ++++++++++++++------------------------- backend/escl/escl_tiff.c | 105 +++++++--------------- 5 files changed, 200 insertions(+), 198 deletions(-) create mode 100644 backend/escl/escl_crop.c diff --git a/backend/Makefile.am b/backend/Makefile.am index c375844cf..dc11a8a59 100644 --- a/backend/Makefile.am +++ b/backend/Makefile.am @@ -437,7 +437,7 @@ EXTRA_DIST += dmc.conf.in if have_libavahi if have_libcurl if have_libxml2 -libescl_la_SOURCES = escl/escl.c escl/escl_capabilities.c escl/escl_devices.c escl/escl.h escl/escl_newjob.c escl/escl_reset.c escl/escl_scan.c escl/escl_status.c escl/escl_jpeg.c escl/escl_png.c escl/escl_tiff.c +libescl_la_SOURCES = escl/escl.c escl/escl_capabilities.c escl/escl_devices.c escl/escl.h escl/escl_newjob.c escl/escl_reset.c escl/escl_scan.c escl/escl_status.c escl/escl_jpeg.c escl/escl_png.c escl/escl_tiff.c escl/escl_crop.c libescl_la_CPPFLAGS = $(AM_CPPFLAGS) $(JPEG_CFLAGS) $(PNG_CFLAGS) $(TIFF_CFLAGS) $(XML_CFLAGS) $(libcurl_CFLAGS) $(AVAHI_CFLAGS) -DBACKEND_NAME=escl nodist_libsane_escl_la_SOURCES = escl-s.c diff --git a/backend/escl/escl.h b/backend/escl/escl.h index 72405ec3e..f61561705 100644 --- a/backend/escl/escl.h +++ b/backend/escl/escl.h @@ -156,20 +156,26 @@ enum #define MM_TO_PIXEL(millimeters, dpi) SANE_FIX((SANE_UNFIX(millimeters) * (dpi) / 25.4)) ESCL_Device *escl_devices(SANE_Status *status); -SANE_Status escl_device_add(int port_nb, const char *model_name, char *ip_address, char *type); +SANE_Status escl_device_add(int port_nb, const char *model_name, + char *ip_address, char *type); SANE_Status escl_status(SANE_String_Const name); capabilities_t *escl_capabilities(SANE_String_Const name, SANE_Status *status); -char *escl_newjob(capabilities_t *scanner, SANE_String_Const name, SANE_Status *status); -SANE_Status escl_scan(capabilities_t *scanner, SANE_String_Const name, char *result); +char *escl_newjob(capabilities_t *scanner, SANE_String_Const name, + SANE_Status *status); +SANE_Status escl_scan(capabilities_t *scanner, SANE_String_Const name, + char *result); void escl_scanner(SANE_String_Const name, char *result); +unsigned char *escl_crop_surface(capabilities_t *scanner, unsigned char *surface, + int w, int h, int bps, int *width, int *height); + // JPEG -SANE_Status get_JPEG_data(capabilities_t *scanner, int *w, int *h, int *bps); +SANE_Status get_JPEG_data(capabilities_t *scanner, int *width, int *height, int *bps); // PNG -SANE_Status get_PNG_data(capabilities_t *scanner, int *w, int *h, int *bps); +SANE_Status get_PNG_data(capabilities_t *scanner, int *width, int *height, int *bps); // TIFF -SANE_Status get_TIFF_data(capabilities_t *scanner, int *w, int *h, int *bps); +SANE_Status get_TIFF_data(capabilities_t *scanner, int *width, int *height, int *bps); #endif diff --git a/backend/escl/escl_crop.c b/backend/escl/escl_crop.c new file mode 100644 index 000000000..e6dbadf20 --- /dev/null +++ b/backend/escl/escl_crop.c @@ -0,0 +1,90 @@ +/* sane - Scanner Access Now Easy. + + Copyright (C) 2020 Thierry HUCHARD + + This file is part of the SANE package. + + SANE is free software; you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + + SANE is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with sane; see the file COPYING. If not, write to the Free + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + + This file implements a SANE backend for eSCL scanners. */ + +#define DEBUG_DECLARE_ONLY +#include "../include/sane/config.h" + +#include "escl.h" +#include +#include +#include + +unsigned char * +escl_crop_surface(capabilities_t *scanner, + unsigned char *surface, + int w, + int h, + int bps, + int *width, + int *height) +{ + int x_off = 0, x = 0; + int real_w = 0; + int y_off = 0, y = 0; + int real_h = 0; + unsigned char *surface_crop = NULL; + + DBG( 1, "Escl Image Crop\n"); + if (w < (int)scanner->width) + scanner->width = w; + if (scanner->pos_x < 0) + scanner->pos_x = 0; + + if (h < (int)scanner->height) + scanner->height = h; + if (scanner->pos_x < 0) + scanner->pos_x = 0; + + x_off = scanner->pos_x; + real_w = scanner->width - x_off; + y_off = scanner->pos_y; + real_h = scanner->height - y_off; + *width = real_w; + *height = real_h; + if (x_off > 0 || real_w < scanner->width || + y_off > 0 || real_h < scanner->height) { + surface_crop = (unsigned char *)malloc (sizeof (unsigned char) * real_w + * real_h * bps); + if(!surface_crop) { + DBG( 1, "Escl Crop : Surface_crop Memory allocation problem\n"); + free(surface); + surface = NULL; + goto finish; + } + for (y = 0; y < real_h; y++) + { + for (x = 0; x < real_w; x++) + { + surface_crop[y * real_w + x] = surface[(y + y_off) * w + x + x_off]; + } + } + free(surface); + surface = surface_crop; + } + // we don't need row pointers anymore + scanner->img_data = surface; + scanner->img_size = (int)(real_w * real_h * bps); + scanner->img_read = 0; +finish: + return surface; +} + diff --git a/backend/escl/escl_png.c b/backend/escl/escl_png.c index 6d105ed47..188dfef04 100644 --- a/backend/escl/escl_png.c +++ b/backend/escl/escl_png.c @@ -49,19 +49,15 @@ * \return SANE_STATUS_GOOD (if everything is OK, otherwise, SANE_STATUS_NO_MEM/SANE_STATUS_INVAL) */ SANE_Status -get_PNG_data(capabilities_t *scanner, int *w, int *h, int *components) +get_PNG_data(capabilities_t *scanner, int *width, int *height, int *bps) { - unsigned int width = 0; /* largeur */ - unsigned int height = 0; /* hauteur */ - int bps = 3; /* composantes d'un texel */ - unsigned char *texels = NULL; /* données de l'image */ - unsigned char *surface = NULL; /* données de l'image */ + unsigned int w = 0; + unsigned int h = 0; + int components = 3; + unsigned char *surface = NULL; /* Image data */ unsigned int i = 0; png_byte magic[8]; - int x_off = 0, x = 0; - int wid = 0; - int y_off = 0, y = 0; - int hei = 0; + SANE_Status status = SANE_STATUS_GOOD; // read magic number fread (magic, 1, sizeof (magic), scanner->tmp); @@ -69,11 +65,8 @@ get_PNG_data(capabilities_t *scanner, int *w, int *h, int *components) if (!png_check_sig (magic, sizeof (magic))) { DBG( 1, "Escl Png : PNG error is not a valid PNG image!\n"); - if (scanner->tmp) { - fclose(scanner->tmp); - scanner->tmp = NULL; - } - return (SANE_STATUS_INVAL); + status = SANE_STATUS_INVAL; + goto close_file; } // create a png read struct png_structp png_ptr = png_create_read_struct @@ -81,12 +74,8 @@ get_PNG_data(capabilities_t *scanner, int *w, int *h, int *components) if (!png_ptr) { DBG( 1, "Escl Png : PNG error create a png read struct\n"); - if (scanner->tmp) - if (scanner->tmp) { - fclose(scanner->tmp); - scanner->tmp = NULL; - } - return (SANE_STATUS_INVAL); + status = SANE_STATUS_INVAL; + goto close_file; } // create a png info struct png_infop info_ptr = png_create_info_struct (png_ptr); @@ -94,26 +83,19 @@ get_PNG_data(capabilities_t *scanner, int *w, int *h, int *components) { DBG( 1, "Escl Png : PNG error create a png info struct\n"); png_destroy_read_struct (&png_ptr, NULL, NULL); - if (scanner->tmp) { - fclose(scanner->tmp); - scanner->tmp = NULL; - } - return (SANE_STATUS_INVAL); + status = SANE_STATUS_INVAL; + goto close_file; } // initialize the setjmp for returning properly after a libpng // error occured if (setjmp (png_jmpbuf (png_ptr))) { png_destroy_read_struct (&png_ptr, &info_ptr, NULL); - if (texels) - free (texels); - fprintf(stderr,"PNG read error.\n"); - if (scanner->tmp) { - fclose(scanner->tmp); - scanner->tmp = NULL; - } + if (surface) + free (surface); DBG( 1, "Escl Png : PNG read error.\n"); - return (SANE_STATUS_INVAL); + status = SANE_STATUS_INVAL; + goto close_file; } // setup libpng for using standard C fread() function // with our FILE pointer @@ -133,114 +115,79 @@ get_PNG_data(capabilities_t *scanner, int *w, int *h, int *components) png_set_palette_to_rgb (png_ptr); else if (color_type != PNG_COLOR_TYPE_RGB && color_type != PNG_COLOR_TYPE_RGB_ALPHA) { - fprintf(stderr,"PNG format not supported.\n"); - if (scanner->tmp) { - fclose(scanner->tmp); - scanner->tmp = NULL; - } - return (SANE_STATUS_INVAL); + DBG(1, "PNG format not supported.\n"); + status = SANE_STATUS_NO_MEM; + goto close_file; } - if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) - bps = 4; + + if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) + components = 4; else - bps = 3; - if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS)) - png_set_tRNS_to_alpha (png_ptr); - if (bit_depth == 16) - png_set_strip_16 (png_ptr); - else if (bit_depth < 8) - png_set_packing (png_ptr); - // update info structure to apply transformations - png_read_update_info (png_ptr, info_ptr); - // retrieve updated information - png_get_IHDR (png_ptr, info_ptr, - (png_uint_32*)(&width), - (png_uint_32*)(&height), - &bit_depth, &color_type, - NULL, NULL, NULL); + components = 3; - if (width < (unsigned int)scanner->width) - scanner->width = width; - if (scanner->pos_x < 0) - scanner->pos_x = 0; + if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS)) + png_set_tRNS_to_alpha (png_ptr); + if (bit_depth == 16) + png_set_strip_16 (png_ptr); + else if (bit_depth < 8) + png_set_packing (png_ptr); + // update info structure to apply transformations + png_read_update_info (png_ptr, info_ptr); + // retrieve updated information + png_get_IHDR (png_ptr, info_ptr, + (png_uint_32*)(&w), + (png_uint_32*)(&h), + &bit_depth, &color_type, + NULL, NULL, NULL); - if (height < (unsigned int)scanner->height) - scanner->height = height; - if (scanner->pos_x < 0) - scanner->pos_x = 0; - - x_off = scanner->pos_x; - wid = scanner->width - x_off; - y_off = scanner->pos_y; - hei = scanner->height - y_off; - *w = (int)wid; - *h = (int)hei; - *components = bps; + *bps = components; // we can now allocate memory for storing pixel data - texels = (unsigned char *)malloc (sizeof (unsigned char) * width - * height * bps); - if (!texels) { + surface = (unsigned char *)malloc (sizeof (unsigned char) * w + * h * components); + if (!surface) { DBG( 1, "Escl Png : texels Memory allocation problem\n"); - if (scanner->tmp) { - fclose(scanner->tmp); - scanner->tmp = NULL; - } - return (SANE_STATUS_NO_MEM); + status = SANE_STATUS_NO_MEM; + goto close_file; } png_bytep *row_pointers; // setup a pointer array. Each one points at the begening of a row. - row_pointers = (png_bytep *)malloc (sizeof (png_bytep) * height); + row_pointers = (png_bytep *)malloc (sizeof (png_bytep) * h); if (!row_pointers) { DBG( 1, "Escl Png : row_pointers Memory allocation problem\n"); - free(texels); - if (scanner->tmp) { - fclose(scanner->tmp); - scanner->tmp = NULL; - } - return (SANE_STATUS_NO_MEM); + free(surface); + status = SANE_STATUS_NO_MEM; + goto close_file; } - for (i = 0; i < height; ++i) + for (i = 0; i < h; ++i) { - row_pointers[i] = (png_bytep)(texels + - ((height - (i + 1)) * width * bps)); + row_pointers[i] = (png_bytep)(surface + + ((h - (i + 1)) * w * components)); } // read pixel data using row pointers png_read_image (png_ptr, row_pointers); - if (x_off > 0 || wid < scanner->width || - y_off > 0 || hei < scanner->height) { - surface = (unsigned char *)malloc (sizeof (unsigned char) * wid - * hei * bps); - if (surface) - { - for (y = 0; y < hei; y++) - { - for (x = 0; x < wid; x++) - { - surface[y * wid + x] = texels[(y + y_off) * width + x + x_off]; - } - } - free(texels); - } - else - surface = texels; + + // If necessary, trim the image. + surface = escl_crop_surface(scanner, surface, w, h, components, width, height); + if (!surface) { + DBG( 1, "Escl Png : Surface Memory allocation problem\n"); + status = SANE_STATUS_NO_MEM; + goto close_file; } - else - surface = texels; - // we don't need row pointers anymore - scanner->img_data = surface; - scanner->img_size = (int)(wid * hei * bps); - scanner->img_read = 0; + free (row_pointers); - fclose(scanner->tmp); + +close_file: + if (scanner->tmp) + fclose(scanner->tmp); scanner->tmp = NULL; - return (SANE_STATUS_GOOD); + return (status); } #else SANE_Status get_PNG_data(capabilities_t __sane_unused__ *scanner, - int __sane_unused__ *w, - int __sane_unused__ *h, + int __sane_unused__ *width, + int __sane_unused__ *height, int __sane_unused__ *bps) { return (SANE_STATUS_INVAL); diff --git a/backend/escl/escl_tiff.c b/backend/escl/escl_tiff.c index c6fff18ce..294cf84ba 100644 --- a/backend/escl/escl_tiff.c +++ b/backend/escl/escl_tiff.c @@ -50,100 +50,59 @@ * \return SANE_STATUS_GOOD (if everything is OK, otherwise, SANE_STATUS_NO_MEM/SANE_STATUS_INVAL) */ SANE_Status -get_TIFF_data(capabilities_t *scanner, int *w, int *h, int *components) +get_TIFF_data(capabilities_t *scanner, int *width, int *height, int *bps) { TIFF* tif = NULL; - uint32 width = 0; /* largeur */ - uint32 height = 0; /* hauteur */ - unsigned char *raster = NULL; /* données de l'image */ - unsigned char *surface = NULL; /* données de l'image */ - int bps = 4; + uint32 w = 0; + uint32 h = 0; + unsigned char *surface = NULL; /* image data*/ + int components = 4; uint32 npixels = 0; - int x_off = 0, x = 0; - int wid = 0; - int y_off = 0, y = 0; - int hei = 0; + SANE_Status status = SANE_STATUS_GOOD; lseek(fileno(scanner->tmp), 0, SEEK_SET); tif = TIFFFdOpen(fileno(scanner->tmp), "temp", "r"); if (!tif) { DBG( 1, "Escl Tiff : Can not open, or not a TIFF file.\n"); - if (scanner->tmp) { - fclose(scanner->tmp); - scanner->tmp = NULL; - } - return (SANE_STATUS_INVAL); + status = SANE_STATUS_INVAL; + goto close_file; } - TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width); - TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height); - npixels = width * height; - raster = (unsigned char*) malloc(npixels * sizeof (uint32)); - if (raster != NULL) + TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w); + TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h); + npixels = w * h; + surface = (unsigned char*) malloc(npixels * sizeof (uint32)); + if (surface != NULL) { DBG( 1, "Escl Tiff : raster Memory allocation problem.\n"); - if (scanner->tmp) { - fclose(scanner->tmp); - scanner->tmp = NULL; - } - return (SANE_STATUS_INVAL); + status = SANE_STATUS_INVAL; + goto close_tiff; } - if (!TIFFReadRGBAImage(tif, width, height, (uint32 *)raster, 0)) + if (!TIFFReadRGBAImage(tif, w, h, (uint32 *)surface, 0)) { DBG( 1, "Escl Tiff : Problem reading image data.\n"); - if (scanner->tmp) { - fclose(scanner->tmp); - scanner->tmp = NULL; - } - return (SANE_STATUS_INVAL); + status = SANE_STATUS_INVAL; + free(surface); + goto close_tiff; } - if (width < (unsigned int)scanner->width) - scanner->width = width; - if (scanner->pos_x < 0) - scanner->pos_x = 0; - - if (height < (unsigned int)scanner->height) - scanner->height = height; - if (scanner->pos_x < 0) - scanner->pos_x = 0; - - x_off = scanner->pos_x; - wid = scanner->width - x_off; - y_off = scanner->pos_y; - hei = scanner->height - y_off; - *w = (int)wid; - *h = (int)hei; - *components = bps; - if (x_off > 0 || wid < scanner->width || - y_off > 0 || hei < scanner->height) { - surface = (unsigned char *)malloc (sizeof (unsigned char) * wid - * hei * bps); - if (surface) - { - for (y = 0; y < hei; y++) - { - for (x = 0; x < wid; x++) - { - surface[y * wid + x] = raster[(y + y_off) * width + x + x_off]; - } - } - free(raster); - } - else - surface = raster; + *bps = components; + + // If necessary, trim the image. + surface = escl_crop_surface(scanner, surface, w, h, components, width, height); + if (!surface) { + DBG( 1, "Escl Tiff : Surface Memory allocation problem\n"); + status = SANE_STATUS_INVAL; } - else - surface = raster; - // we don't need row pointers anymore - scanner->img_data = raster; - scanner->img_size = (int)(width * height * bps); - scanner->img_read = 0; + +close_tiff: TIFFClose(tif); - fclose(scanner->tmp); +close_file: + if (scanner->tmp) + fclose(scanner->tmp); scanner->tmp = NULL; - return (SANE_STATUS_GOOD); + return (status); } #else From c3adeed4c46b30ce1f8185ee600ebb7b68b29d0f Mon Sep 17 00:00:00 2001 From: Thierry HUCHARD Date: Sun, 9 Feb 2020 09:36:16 +0100 Subject: [PATCH 09/14] Fix style. --- backend/Makefile.am | 2 +- backend/escl/escl_crop.c | 1 - backend/escl/escl_png.c | 6 +++--- backend/escl/escl_tiff.c | 8 ++++---- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/backend/Makefile.am b/backend/Makefile.am index dc11a8a59..642fa130f 100644 --- a/backend/Makefile.am +++ b/backend/Makefile.am @@ -437,7 +437,7 @@ EXTRA_DIST += dmc.conf.in if have_libavahi if have_libcurl if have_libxml2 -libescl_la_SOURCES = escl/escl.c escl/escl_capabilities.c escl/escl_devices.c escl/escl.h escl/escl_newjob.c escl/escl_reset.c escl/escl_scan.c escl/escl_status.c escl/escl_jpeg.c escl/escl_png.c escl/escl_tiff.c escl/escl_crop.c +libescl_la_SOURCES = escl/escl.c escl/escl_capabilities.c escl/escl_devices.c escl/escl.h escl/escl_newjob.c escl/escl_reset.c escl/escl_scan.c escl/escl_status.c escl/escl_jpeg.c escl/escl_png.c escl/escl_tiff.c escl/escl_crop.c libescl_la_CPPFLAGS = $(AM_CPPFLAGS) $(JPEG_CFLAGS) $(PNG_CFLAGS) $(TIFF_CFLAGS) $(XML_CFLAGS) $(libcurl_CFLAGS) $(AVAHI_CFLAGS) -DBACKEND_NAME=escl nodist_libsane_escl_la_SOURCES = escl-s.c diff --git a/backend/escl/escl_crop.c b/backend/escl/escl_crop.c index e6dbadf20..0d1b9444c 100644 --- a/backend/escl/escl_crop.c +++ b/backend/escl/escl_crop.c @@ -87,4 +87,3 @@ escl_crop_surface(capabilities_t *scanner, finish: return surface; } - diff --git a/backend/escl/escl_png.c b/backend/escl/escl_png.c index 188dfef04..662f8ea9b 100644 --- a/backend/escl/escl_png.c +++ b/backend/escl/escl_png.c @@ -165,15 +165,15 @@ get_PNG_data(capabilities_t *scanner, int *width, int *height, int *bps) } // read pixel data using row pointers png_read_image (png_ptr, row_pointers); - - // If necessary, trim the image. + + // If necessary, trim the image. surface = escl_crop_surface(scanner, surface, w, h, components, width, height); if (!surface) { DBG( 1, "Escl Png : Surface Memory allocation problem\n"); status = SANE_STATUS_NO_MEM; goto close_file; } - + free (row_pointers); close_file: diff --git a/backend/escl/escl_tiff.c b/backend/escl/escl_tiff.c index 294cf84ba..98bc5f31e 100644 --- a/backend/escl/escl_tiff.c +++ b/backend/escl/escl_tiff.c @@ -83,20 +83,20 @@ get_TIFF_data(capabilities_t *scanner, int *width, int *height, int *bps) { DBG( 1, "Escl Tiff : Problem reading image data.\n"); status = SANE_STATUS_INVAL; - free(surface); + free(surface); goto close_tiff; } *bps = components; - - // If necessary, trim the image. + + // If necessary, trim the image. surface = escl_crop_surface(scanner, surface, w, h, components, width, height); if (!surface) { DBG( 1, "Escl Tiff : Surface Memory allocation problem\n"); status = SANE_STATUS_INVAL; } -close_tiff: +close_tiff: TIFFClose(tif); close_file: if (scanner->tmp) From bae9f0a3d3bc83928e95e0cf5abb7c63b9552e75 Mon Sep 17 00:00:00 2001 From: Ordissimo Date: Mon, 10 Feb 2020 08:45:21 +0000 Subject: [PATCH 10/14] Apply suggestion to backend/escl/escl.h --- backend/escl/escl.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/escl/escl.h b/backend/escl/escl.h index f61561705..7f4964158 100644 --- a/backend/escl/escl.h +++ b/backend/escl/escl.h @@ -152,8 +152,8 @@ enum NUM_OPTIONS }; -#define PIXEL_TO_MM(pixels, dpi) SANE_FIX((SANE_UNFIX(pixels) * (25.4) / dpi)) -#define MM_TO_PIXEL(millimeters, dpi) SANE_FIX((SANE_UNFIX(millimeters) * (dpi) / 25.4)) +#define PIXEL_TO_MM(pixels, dpi) SANE_FIX(SANE_UNFIX(pixels) * 25.4 / (dpi)) +#define MM_TO_PIXEL(millimeters, dpi) SANE_FIX(SANE_UNFIX(millimeters) * (dpi) / 25.4) ESCL_Device *escl_devices(SANE_Status *status); SANE_Status escl_device_add(int port_nb, const char *model_name, From a771be0c6a1f11a1263f466be2ca4fb2749dc9a2 Mon Sep 17 00:00:00 2001 From: Ordissimo Date: Mon, 10 Feb 2020 08:45:38 +0000 Subject: [PATCH 11/14] Apply suggestion to backend/escl/escl_png.c --- backend/escl/escl_png.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/escl/escl_png.c b/backend/escl/escl_png.c index 662f8ea9b..cf92449c1 100644 --- a/backend/escl/escl_png.c +++ b/backend/escl/escl_png.c @@ -120,7 +120,7 @@ get_PNG_data(capabilities_t *scanner, int *width, int *height, int *bps) goto close_file; } - if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) + if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) components = 4; else components = 3; From dd4fb84de81bcfe0caf570958ee1d04ee63484ab Mon Sep 17 00:00:00 2001 From: Olaf Meeuwissen Date: Tue, 11 Feb 2020 01:45:08 +0000 Subject: [PATCH 12/14] Apply suggestion to backend/escl/escl.c --- backend/escl/escl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/escl/escl.c b/backend/escl/escl.c index 002206046..5ea3c76b9 100644 --- a/backend/escl/escl.c +++ b/backend/escl/escl.c @@ -770,7 +770,7 @@ sane_start(SANE_Handle h) return SANE_STATUS_INVAL; } - fprintf(stdout, "2-Size Image [%dx%d|%dx%d]\n", 0, 0, w, he); + DBG(10, "2-Size Image [%dx%d|%dx%d]\n", 0, 0, w, he); if (status != SANE_STATUS_GOOD) return (status); handler->ps.depth = 8; From 23cd9f5d6104c22cbd254445618bc627f6e16ba3 Mon Sep 17 00:00:00 2001 From: Olaf Meeuwissen Date: Tue, 11 Feb 2020 01:45:11 +0000 Subject: [PATCH 13/14] Apply suggestion to backend/escl/escl_jpeg.c --- backend/escl/escl_jpeg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/escl/escl_jpeg.c b/backend/escl/escl_jpeg.c index 178d9e5c6..bcf3a7c45 100644 --- a/backend/escl/escl_jpeg.c +++ b/backend/escl/escl_jpeg.c @@ -154,7 +154,7 @@ output_no_message(j_common_ptr __sane_unused__ cinfo) * \return SANE_STATUS_GOOD (if everything is OK, otherwise, SANE_STATUS_NO_MEM/SANE_STATUS_INVAL) */ SANE_Status - get_JPEG_data(capabilities_t *scanner, int *width, int *height, int *bps) +get_JPEG_data(capabilities_t *scanner, int *width, int *height, int *bps) { int start = 0; struct jpeg_decompress_struct cinfo; From 4466d51924975c3f34f2fee5f02419db24d63d5a Mon Sep 17 00:00:00 2001 From: thierry1970 Date: Tue, 11 Feb 2020 08:22:43 +0100 Subject: [PATCH 14/14] Fixed variable name. --- backend/escl/escl_jpeg.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/escl/escl_jpeg.c b/backend/escl/escl_jpeg.c index bcf3a7c45..eebe7d161 100644 --- a/backend/escl/escl_jpeg.c +++ b/backend/escl/escl_jpeg.c @@ -200,8 +200,8 @@ get_JPEG_data(capabilities_t *scanner, int *width, int *height, int *bps) if (cinfo.output_height < (unsigned int)scanner->height) scanner->height = cinfo.output_height; - if (scanner->pos_x < 0) - scanner->pos_x = 0; + if (scanner->pos_y < 0) + scanner->pos_y = 0; x_off = scanner->pos_x; w = scanner->width - x_off;