kopia lustrzana https://gitlab.com/sane-project/backends
Clarification and factoring of the code
rodzic
af6f6d4655
commit
6a9a0beee8
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -0,0 +1,90 @@
|
|||
/* 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;
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue