Merge branch 'escl-page-crop' into 'master'

Escl page crop

Closes #230

See merge request sane-project/backends!316
merge-requests/340/head
Olaf Meeuwissen 2020-02-11 07:38:28 +00:00
commit 36f068a0ce
7 zmienionych plików z 300 dodań i 165 usunięć

Wyświetl plik

@ -437,13 +437,13 @@ EXTRA_DIST += dmc.conf.in
if have_libavahi if have_libavahi
if have_libcurl if have_libcurl
if have_libxml2 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 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 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_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_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 endif
endif endif

Wyświetl plik

@ -381,12 +381,12 @@ init_options(SANE_String_Const name, escl_sane_t *s)
s->opt[i].size = sizeof (SANE_Word); s->opt[i].size = sizeof (SANE_Word);
s->opt[i].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT; s->opt[i].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
} }
s->x_range.min = 0; s->x_range.min = PIXEL_TO_MM(s->scanner->MinWidth, 300.0);
s->x_range.max = s->scanner->MaxWidth - s->scanner->MinWidth; s->x_range.max = PIXEL_TO_MM(s->scanner->MaxWidth, 300.0);
s->x_range.quant = 1; s->x_range.quant = 0;
s->y_range.min = 0; s->y_range.min = PIXEL_TO_MM(s->scanner->MinHeight, 300.0);
s->y_range.max = s->scanner->MaxHeight - s->scanner->MinHeight; s->y_range.max = PIXEL_TO_MM(s->scanner->MaxHeight, 300.0);
s->y_range.quant = 1; s->y_range.quant = 0;
s->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS; 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].desc = SANE_DESC_NUM_OPTIONS;
s->opt[OPT_NUM_OPTS].type = SANE_TYPE_INT; s->opt[OPT_NUM_OPTS].type = SANE_TYPE_INT;
@ -442,7 +442,7 @@ init_options(SANE_String_Const name, escl_sane_t *s)
s->val[OPT_GRAY_PREVIEW].w = SANE_FALSE; s->val[OPT_GRAY_PREVIEW].w = SANE_FALSE;
s->opt[OPT_GEOMETRY_GROUP].title = SANE_TITLE_GEOMETRY; 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].type = SANE_TYPE_GROUP;
s->opt[OPT_GEOMETRY_GROUP].cap = SANE_CAP_ADVANCED; s->opt[OPT_GEOMETRY_GROUP].cap = SANE_CAP_ADVANCED;
s->opt[OPT_GEOMETRY_GROUP].constraint_type = SANE_CONSTRAINT_NONE; s->opt[OPT_GEOMETRY_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
@ -451,37 +451,41 @@ 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].title = SANE_TITLE_SCAN_TL_X;
s->opt[OPT_TL_X].desc = SANE_DESC_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].type = SANE_TYPE_FIXED;
s->opt[OPT_TL_X].unit = SANE_UNIT_PIXEL; 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_MM;
s->opt[OPT_TL_X].constraint_type = SANE_CONSTRAINT_RANGE; s->opt[OPT_TL_X].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_TL_X].constraint.range = &s->x_range; s->opt[OPT_TL_X].constraint.range = &s->x_range;
s->val[OPT_TL_X].w = s->scanner->RiskyLeftMargin;
s->opt[OPT_TL_Y].name = SANE_NAME_SCAN_TL_Y; 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].title = SANE_TITLE_SCAN_TL_Y;
s->opt[OPT_TL_Y].desc = SANE_DESC_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].type = SANE_TYPE_FIXED;
s->opt[OPT_TL_Y].unit = SANE_UNIT_PIXEL; 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_MM;
s->opt[OPT_TL_Y].constraint_type = SANE_CONSTRAINT_RANGE; s->opt[OPT_TL_Y].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_TL_Y].constraint.range = &s->y_range; s->opt[OPT_TL_Y].constraint.range = &s->y_range;
s->val[OPT_TL_Y].w = s->scanner->RiskyTopMargin;
s->opt[OPT_BR_X].name = SANE_NAME_SCAN_BR_X; 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].title = SANE_TITLE_SCAN_BR_X;
s->opt[OPT_BR_X].desc = SANE_DESC_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].type = SANE_TYPE_FIXED;
s->opt[OPT_BR_X].unit = SANE_UNIT_PIXEL; 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_MM;
s->opt[OPT_BR_X].constraint_type = SANE_CONSTRAINT_RANGE; s->opt[OPT_BR_X].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_BR_X].constraint.range = &s->x_range; s->opt[OPT_BR_X].constraint.range = &s->x_range;
s->val[OPT_BR_X].w = s->scanner->MaxWidth;
s->opt[OPT_BR_Y].name = SANE_NAME_SCAN_BR_Y; 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].title = SANE_TITLE_SCAN_BR_Y;
s->opt[OPT_BR_Y].desc = SANE_DESC_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].type = SANE_TYPE_FIXED;
s->opt[OPT_BR_Y].unit = SANE_UNIT_PIXEL; 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_MM;
s->opt[OPT_BR_Y].constraint_type = SANE_CONSTRAINT_RANGE; s->opt[OPT_BR_Y].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_BR_Y].constraint.range = &s->y_range; s->opt[OPT_BR_Y].constraint.range = &s->y_range;
s->val[OPT_BR_Y].w = s->scanner->MaxHeight;
return (status); return (status);
} }
@ -523,8 +527,8 @@ sane_open(SANE_String_Const name, SANE_Handle *h)
handler->ps.depth = 8; handler->ps.depth = 8;
handler->ps.last_frame = SANE_TRUE; handler->ps.last_frame = SANE_TRUE;
handler->ps.format = SANE_FRAME_RGB; handler->ps.format = SANE_FRAME_RGB;
handler->ps.pixels_per_line = handler->val[OPT_BR_X].w; handler->ps.pixels_per_line = MM_TO_PIXEL(handler->val[OPT_BR_X].w, 300.0);
handler->ps.lines = handler->val[OPT_BR_Y].w; 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; handler->ps.bytes_per_line = handler->ps.pixels_per_line * 3;
status = sane_get_parameters(handler, 0); status = sane_get_parameters(handler, 0);
if (status != SANE_STATUS_GOOD) if (status != SANE_STATUS_GOOD)
@ -588,7 +592,7 @@ sane_get_option_descriptor(SANE_Handle h, SANE_Int n)
if ((unsigned) n >= NUM_OPTIONS || n < 0) if ((unsigned) n >= NUM_OPTIONS || n < 0)
return (0); return (0);
return (s->opt + n); return (&s->opt[n]);
} }
/** /**
@ -616,12 +620,12 @@ sane_control_option(SANE_Handle h, SANE_Int n, SANE_Action a, void *v, SANE_Int
return (SANE_STATUS_INVAL); return (SANE_STATUS_INVAL);
if (a == SANE_ACTION_GET_VALUE) { if (a == SANE_ACTION_GET_VALUE) {
switch (n) { switch (n) {
case OPT_NUM_OPTS:
case OPT_RESOLUTION:
case OPT_TL_X: case OPT_TL_X:
case OPT_TL_Y: case OPT_TL_Y:
case OPT_BR_X: case OPT_BR_X:
case OPT_BR_Y: case OPT_BR_Y:
case OPT_NUM_OPTS:
case OPT_RESOLUTION:
case OPT_PREVIEW: case OPT_PREVIEW:
case OPT_GRAY_PREVIEW: case OPT_GRAY_PREVIEW:
*(SANE_Word *) v = handler->val[n].w; *(SANE_Word *) v = handler->val[n].w;
@ -641,14 +645,10 @@ sane_control_option(SANE_Handle h, SANE_Int n, SANE_Action a, void *v, SANE_Int
case OPT_TL_Y: case OPT_TL_Y:
case OPT_BR_X: case OPT_BR_X:
case OPT_BR_Y: case OPT_BR_Y:
case OPT_NUM_OPTS:
case OPT_RESOLUTION:
case OPT_PREVIEW: case OPT_PREVIEW:
case OPT_GRAY_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; handler->val[n].w = *(SANE_Word *) v;
if (i) if (i)
*i |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS | SANE_INFO_INEXACT; *i |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS | SANE_INFO_INEXACT;
@ -694,10 +694,6 @@ sane_start(SANE_Handle h)
handler->write_scan_data = SANE_FALSE; handler->write_scan_data = SANE_FALSE;
handler->decompress_scan_data = SANE_FALSE; handler->decompress_scan_data = SANE_FALSE;
handler->end_read = 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;
if(handler->scanner->default_color) if(handler->scanner->default_color)
free(handler->scanner->default_color); free(handler->scanner->default_color);
if (handler->val[OPT_PREVIEW].w == SANE_TRUE) if (handler->val[OPT_PREVIEW].w == SANE_TRUE)
@ -727,6 +723,15 @@ sane_start(SANE_Handle h)
else else
handler->scanner->default_color = strdup("RGB24"); handler->scanner->default_color = strdup("RGB24");
} }
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,
handler->scanner->height,
handler->scanner->width);
if (!handler->scanner->default_color) { if (!handler->scanner->default_color) {
DBG (10, "Default Color allocation failure.\n"); DBG (10, "Default Color allocation failure.\n");
return (SANE_STATUS_NO_MEM); return (SANE_STATUS_NO_MEM);
@ -749,9 +754,12 @@ sane_start(SANE_Handle h)
{ {
status = get_TIFF_data(handler->scanner, &w, &he, &bps); status = get_TIFF_data(handler->scanner, &w, &he, &bps);
} }
else else {
DBG(10, "Unknow image format\n");
return SANE_STATUS_INVAL; return SANE_STATUS_INVAL;
}
DBG(10, "2-Size Image [%dx%d|%dx%d]\n", 0, 0, w, he);
if (status != SANE_STATUS_GOOD) if (status != SANE_STATUS_GOOD)
return (status); return (status);
handler->ps.depth = 8; handler->ps.depth = 8;
@ -760,6 +768,7 @@ sane_start(SANE_Handle h)
handler->ps.bytes_per_line = w * bps; handler->ps.bytes_per_line = w * bps;
handler->ps.last_frame = SANE_TRUE; handler->ps.last_frame = SANE_TRUE;
handler->ps.format = SANE_FRAME_RGB; handler->ps.format = SANE_FRAME_RGB;
DBG(10, "Real Size Image [%dx%d|%dx%d]\n", 0, 0, w, he);
return (status); return (status);
} }

Wyświetl plik

@ -43,6 +43,7 @@
#include "../include/sane/sane.h" #include "../include/sane/sane.h"
#include <stdio.h> #include <stdio.h>
#include <math.h>
#ifndef BACKEND_NAME #ifndef BACKEND_NAME
#define BACKEND_NAME escl #define BACKEND_NAME escl
@ -151,21 +152,30 @@ enum
NUM_OPTIONS 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)
ESCL_Device *escl_devices(SANE_Status *status); 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); SANE_Status escl_status(SANE_String_Const name);
capabilities_t *escl_capabilities(SANE_String_Const name, SANE_Status *status); capabilities_t *escl_capabilities(SANE_String_Const name, SANE_Status *status);
char *escl_newjob(capabilities_t *scanner, SANE_String_Const name, SANE_Status *status); char *escl_newjob(capabilities_t *scanner, SANE_String_Const name,
SANE_Status escl_scan(capabilities_t *scanner, SANE_String_Const name, char *result); 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); 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 // 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 // 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 // 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 #endif

Wyświetl plik

@ -0,0 +1,89 @@
/* sane - Scanner Access Now Easy.
Copyright (C) 2020 Thierry HUCHARD <thierry@ordissimo.com>
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}

Wyświetl plik

@ -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) * \return SANE_STATUS_GOOD (if everything is OK, otherwise, SANE_STATUS_NO_MEM/SANE_STATUS_INVAL)
*/ */
SANE_Status 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; int start = 0;
struct jpeg_decompress_struct cinfo; struct jpeg_decompress_struct cinfo;
@ -162,6 +162,11 @@ get_JPEG_data(capabilities_t *scanner, int *w, int *h, int *bps)
unsigned char *surface = NULL; unsigned char *surface = NULL;
struct my_error_mgr jerr; struct my_error_mgr jerr;
int lineSize = 0; int lineSize = 0;
JDIMENSION x_off = 0;
JDIMENSION y_off = 0;
JDIMENSION w = 0;
JDIMENSION h = 0;
int pos = 0;
if (scanner->tmp == NULL) if (scanner->tmp == NULL)
return (SANE_STATUS_INVAL); return (SANE_STATUS_INVAL);
@ -174,6 +179,7 @@ get_JPEG_data(capabilities_t *scanner, int *w, int *h, int *bps)
jpeg_destroy_decompress(&cinfo); jpeg_destroy_decompress(&cinfo);
if (surface != NULL) if (surface != NULL)
free(surface); free(surface);
fseek(scanner->tmp, start, SEEK_SET);
DBG( 1, "Escl Jpeg : Error reading jpeg\n"); DBG( 1, "Escl Jpeg : Error reading jpeg\n");
if (scanner->tmp) { if (scanner->tmp) {
fclose(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.out_color_space = JCS_RGB;
cinfo.quantize_colors = FALSE; cinfo.quantize_colors = FALSE;
jpeg_calc_output_dimensions(&cinfo); 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_y < 0)
scanner->pos_y = 0;
x_off = scanner->pos_x;
w = scanner->width - x_off;
y_off = scanner->pos_y;
h = scanner->height - y_off;
surface = malloc(w * h * cinfo.output_components);
if (surface == NULL) { if (surface == NULL) {
jpeg_destroy_decompress(&cinfo); jpeg_destroy_decompress(&cinfo);
fseek(scanner->tmp, start, SEEK_SET);
DBG( 1, "Escl Jpeg : Memory allocation problem\n"); DBG( 1, "Escl Jpeg : Memory allocation problem\n");
if (scanner->tmp) { if (scanner->tmp) {
fclose(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); return (SANE_STATUS_NO_MEM);
} }
lineSize = cinfo.output_width * cinfo.output_components;
jpeg_start_decompress(&cinfo); jpeg_start_decompress(&cinfo);
while (cinfo.output_scanline < cinfo.output_height) { if (x_off > 0 || w < cinfo.output_width)
rowptr[0] = (JSAMPROW)surface + (lineSize * cinfo.output_scanline); jpeg_crop_scanline(&cinfo, &x_off, &w);
lineSize = w * 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); jpeg_read_scanlines(&cinfo, rowptr, (JDIMENSION) 1);
pos++;
} }
scanner->img_data = surface; scanner->img_data = surface;
scanner->img_size = lineSize * cinfo.output_height; scanner->img_size = lineSize * h;
scanner->img_read = 0; scanner->img_read = 0;
*w = cinfo.output_width; *width = w;
*h = cinfo.output_height; *height = h;
*bps = cinfo.output_components; *bps = cinfo.output_components;
jpeg_finish_decompress(&cinfo); jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo); jpeg_destroy_decompress(&cinfo);
@ -220,8 +245,8 @@ get_JPEG_data(capabilities_t *scanner, int *w, int *h, int *bps)
SANE_Status SANE_Status
get_JPEG_data(capabilities_t __sane_unused__ *scanner, get_JPEG_data(capabilities_t __sane_unused__ *scanner,
int __sane_unused__ *w, int __sane_unused__ *width,
int __sane_unused__ *h, int __sane_unused__ *height,
int __sane_unused__ *bps) int __sane_unused__ *bps)
{ {
return (SANE_STATUS_INVAL); return (SANE_STATUS_INVAL);

Wyświetl plik

@ -49,14 +49,15 @@
* \return SANE_STATUS_GOOD (if everything is OK, otherwise, SANE_STATUS_NO_MEM/SANE_STATUS_INVAL) * \return SANE_STATUS_GOOD (if everything is OK, otherwise, SANE_STATUS_NO_MEM/SANE_STATUS_INVAL)
*/ */
SANE_Status 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 w = 0;
unsigned int height = 0; /* hauteur */ unsigned int h = 0;
int bps = 3; /* composantes d'un texel */ int components = 3;
unsigned char *texels = NULL; /* données de l'image */ unsigned char *surface = NULL; /* Image data */
unsigned int i = 0; unsigned int i = 0;
png_byte magic[8]; png_byte magic[8];
SANE_Status status = SANE_STATUS_GOOD;
// read magic number // read magic number
fread (magic, 1, sizeof (magic), scanner->tmp); fread (magic, 1, sizeof (magic), scanner->tmp);
@ -64,11 +65,8 @@ get_PNG_data(capabilities_t *scanner, int *w, int *h, int *components)
if (!png_check_sig (magic, sizeof (magic))) if (!png_check_sig (magic, sizeof (magic)))
{ {
DBG( 1, "Escl Png : PNG error is not a valid PNG image!\n"); DBG( 1, "Escl Png : PNG error is not a valid PNG image!\n");
if (scanner->tmp) { status = SANE_STATUS_INVAL;
fclose(scanner->tmp); goto close_file;
scanner->tmp = NULL;
}
return (SANE_STATUS_INVAL);
} }
// create a png read struct // create a png read struct
png_structp png_ptr = png_create_read_struct png_structp png_ptr = png_create_read_struct
@ -76,12 +74,8 @@ get_PNG_data(capabilities_t *scanner, int *w, int *h, int *components)
if (!png_ptr) if (!png_ptr)
{ {
DBG( 1, "Escl Png : PNG error create a png read struct\n"); DBG( 1, "Escl Png : PNG error create a png read struct\n");
if (scanner->tmp) status = SANE_STATUS_INVAL;
if (scanner->tmp) { goto close_file;
fclose(scanner->tmp);
scanner->tmp = NULL;
}
return (SANE_STATUS_INVAL);
} }
// create a png info struct // create a png info struct
png_infop info_ptr = png_create_info_struct (png_ptr); png_infop info_ptr = png_create_info_struct (png_ptr);
@ -89,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"); DBG( 1, "Escl Png : PNG error create a png info struct\n");
png_destroy_read_struct (&png_ptr, NULL, NULL); png_destroy_read_struct (&png_ptr, NULL, NULL);
if (scanner->tmp) { status = SANE_STATUS_INVAL;
fclose(scanner->tmp); goto close_file;
scanner->tmp = NULL;
}
return (SANE_STATUS_INVAL);
} }
// initialize the setjmp for returning properly after a libpng // initialize the setjmp for returning properly after a libpng
// error occured // error occured
if (setjmp (png_jmpbuf (png_ptr))) if (setjmp (png_jmpbuf (png_ptr)))
{ {
png_destroy_read_struct (&png_ptr, &info_ptr, NULL); png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
if (texels) if (surface)
free (texels); free (surface);
fprintf(stderr,"PNG read error.\n");
if (scanner->tmp) {
fclose(scanner->tmp);
scanner->tmp = NULL;
}
DBG( 1, "Escl Png : PNG read error.\n"); 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 // setup libpng for using standard C fread() function
// with our FILE pointer // with our FILE pointer
@ -128,17 +115,16 @@ get_PNG_data(capabilities_t *scanner, int *w, int *h, int *components)
png_set_palette_to_rgb (png_ptr); png_set_palette_to_rgb (png_ptr);
else if (color_type != PNG_COLOR_TYPE_RGB && color_type != PNG_COLOR_TYPE_RGB_ALPHA) else if (color_type != PNG_COLOR_TYPE_RGB && color_type != PNG_COLOR_TYPE_RGB_ALPHA)
{ {
fprintf(stderr,"PNG format not supported.\n"); DBG(1, "PNG format not supported.\n");
if (scanner->tmp) { status = SANE_STATUS_NO_MEM;
fclose(scanner->tmp); goto close_file;
scanner->tmp = NULL;
}
return (SANE_STATUS_INVAL);
} }
if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
bps = 4; components = 4;
else else
bps = 3; components = 3;
if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS)) if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha (png_ptr); png_set_tRNS_to_alpha (png_ptr);
if (bit_depth == 16) if (bit_depth == 16)
@ -149,42 +135,59 @@ get_PNG_data(capabilities_t *scanner, int *w, int *h, int *components)
png_read_update_info (png_ptr, info_ptr); png_read_update_info (png_ptr, info_ptr);
// retrieve updated information // retrieve updated information
png_get_IHDR (png_ptr, info_ptr, png_get_IHDR (png_ptr, info_ptr,
(png_uint_32*)(&width), (png_uint_32*)(&w),
(png_uint_32*)(&height), (png_uint_32*)(&h),
&bit_depth, &color_type, &bit_depth, &color_type,
NULL, NULL, NULL); NULL, NULL, NULL);
*w = (int)width; *bps = components;
*h = (int)height;
*components = bps;
// we can now allocate memory for storing pixel data // we can now allocate memory for storing pixel data
texels = (unsigned char *)malloc (sizeof (unsigned char) * width surface = (unsigned char *)malloc (sizeof (unsigned char) * w
* height * bps); * h * components);
if (!surface) {
DBG( 1, "Escl Png : texels Memory allocation problem\n");
status = SANE_STATUS_NO_MEM;
goto close_file;
}
png_bytep *row_pointers; png_bytep *row_pointers;
// setup a pointer array. Each one points at the begening of a row. // 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);
for (i = 0; i < height; ++i) if (!row_pointers) {
DBG( 1, "Escl Png : row_pointers Memory allocation problem\n");
free(surface);
status = SANE_STATUS_NO_MEM;
goto close_file;
}
for (i = 0; i < h; ++i)
{ {
row_pointers[i] = (png_bytep)(texels + row_pointers[i] = (png_bytep)(surface +
((height - (i + 1)) * width * bps)); ((h - (i + 1)) * w * components));
} }
// read pixel data using row pointers // read pixel data using row pointers
png_read_image (png_ptr, row_pointers); png_read_image (png_ptr, row_pointers);
// we don't need row pointers anymore
scanner->img_data = texels; // If necessary, trim the image.
scanner->img_size = (int)(width * height * bps); surface = escl_crop_surface(scanner, surface, w, h, components, width, height);
scanner->img_read = 0; if (!surface) {
DBG( 1, "Escl Png : Surface Memory allocation problem\n");
status = SANE_STATUS_NO_MEM;
goto close_file;
}
free (row_pointers); free (row_pointers);
close_file:
if (scanner->tmp)
fclose(scanner->tmp); fclose(scanner->tmp);
scanner->tmp = NULL; scanner->tmp = NULL;
return (SANE_STATUS_GOOD); return (status);
} }
#else #else
SANE_Status SANE_Status
get_PNG_data(capabilities_t __sane_unused__ *scanner, get_PNG_data(capabilities_t __sane_unused__ *scanner,
int __sane_unused__ *w, int __sane_unused__ *width,
int __sane_unused__ *h, int __sane_unused__ *height,
int __sane_unused__ *bps) int __sane_unused__ *bps)
{ {
return (SANE_STATUS_INVAL); return (SANE_STATUS_INVAL);

Wyświetl plik

@ -50,60 +50,59 @@
* \return SANE_STATUS_GOOD (if everything is OK, otherwise, SANE_STATUS_NO_MEM/SANE_STATUS_INVAL) * \return SANE_STATUS_GOOD (if everything is OK, otherwise, SANE_STATUS_NO_MEM/SANE_STATUS_INVAL)
*/ */
SANE_Status 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; TIFF* tif = NULL;
uint32 width = 0; /* largeur */ uint32 w = 0;
uint32 height = 0; /* hauteur */ uint32 h = 0;
unsigned char *raster = NULL; /* données de l'image */ unsigned char *surface = NULL; /* image data*/
int bps = 4; int components = 4;
uint32 npixels = 0; uint32 npixels = 0;
SANE_Status status = SANE_STATUS_GOOD;
lseek(fileno(scanner->tmp), 0, SEEK_SET); lseek(fileno(scanner->tmp), 0, SEEK_SET);
tif = TIFFFdOpen(fileno(scanner->tmp), "temp", "r"); tif = TIFFFdOpen(fileno(scanner->tmp), "temp", "r");
if (!tif) { if (!tif) {
DBG( 1, "Escl Tiff : Can not open, or not a TIFF file.\n"); DBG( 1, "Escl Tiff : Can not open, or not a TIFF file.\n");
if (scanner->tmp) { status = SANE_STATUS_INVAL;
fclose(scanner->tmp); goto close_file;
scanner->tmp = NULL;
}
return (SANE_STATUS_INVAL);
} }
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width); TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height); TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
npixels = width * height; npixels = w * h;
raster = (unsigned char*) malloc(npixels * sizeof (uint32)); surface = (unsigned char*) malloc(npixels * sizeof (uint32));
if (raster != NULL) if (surface != NULL)
{ {
DBG( 1, "Escl Tiff : Memory allocation problem.\n"); DBG( 1, "Escl Tiff : raster Memory allocation problem.\n");
if (scanner->tmp) { status = SANE_STATUS_INVAL;
fclose(scanner->tmp); goto close_tiff;
scanner->tmp = NULL;
}
return (SANE_STATUS_INVAL);
} }
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"); DBG( 1, "Escl Tiff : Problem reading image data.\n");
if (scanner->tmp) { status = SANE_STATUS_INVAL;
fclose(scanner->tmp); free(surface);
scanner->tmp = NULL; goto close_tiff;
} }
return (SANE_STATUS_INVAL);
*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;
} }
*w = (int)width;
*h = (int)height; close_tiff:
*components = bps;
// we don't need row pointers anymore
scanner->img_data = raster;
scanner->img_size = (int)(width * height * bps);
scanner->img_read = 0;
TIFFClose(tif); TIFFClose(tif);
close_file:
if (scanner->tmp)
fclose(scanner->tmp); fclose(scanner->tmp);
scanner->tmp = NULL; scanner->tmp = NULL;
return (SANE_STATUS_GOOD); return (status);
} }
#else #else