Decoding files in one phase.

merge-requests/213/head
Thierry HUCHARD 2019-12-25 18:36:23 +01:00
rodzic c6e8d381e0
commit d3efc20a0b
4 zmienionych plików z 35 dodań i 161 usunięć

Wyświetl plik

@ -647,22 +647,23 @@ sane_start(SANE_Handle h)
if (status != SANE_STATUS_GOOD)
return (status);
status = escl_scan(handler->scanner, handler->name, handler->result);
if (status != SANE_STATUS_GOOD)
return (status);
fprintf(stderr, "DIM : [%s]\n", handler->scanner->default_format);
if (!strncmp(handler->scanner->default_format, "image/jpeg", 10))
{
get_JPEG_dimension(handler->scanner->tmp, &w, &he, &bps);
fprintf(stderr, "JPEG DIM : [%s]\n", handler->scanner->default_format);
status = get_JPEG_data(handler->scanner, &w, &he, &bps);
fprintf(stderr, "JPEG DIM : [%s]\n", handler->scanner->default_format);
}
else
{
get_PNG_dimension(handler->scanner->tmp, &w, &he, &bps);
fprintf(stderr, "PNG DIM : [%s]\n", handler->scanner->default_format);
status = get_PNG_data(handler->scanner->tmp, &w, &he, &bps);
fprintf(stderr, "PNG DIM : [%s]\n", handler->scanner->default_format);
}
if (status != SANE_STATUS_GOOD)
return (status);
fprintf(stderr, "SIZE [%dx%dx%d]\n", w, he, bps);
if (bps == 0)
return SANE_STATUS_INVAL;
fprintf(stderr, "2-SIZE [%dx%dx%d]\n", w, he, bps);
fseek(handler->scanner->tmp, SEEK_SET, 0);
handler->ps.depth = 8;
handler->ps.pixels_per_line = w;
handler->ps.lines = he;
@ -728,17 +729,7 @@ sane_read(SANE_Handle h, SANE_Byte *buf, SANE_Int maxlen, SANE_Int *len)
if (!handler->write_scan_data)
handler->write_scan_data = SANE_TRUE;
if (!handler->decompress_scan_data) {
if (handler->scanner->tmp == NULL)
return (SANE_STATUS_INVAL);
fprintf(stderr, "READ : [%s]\n", handler->scanner->default_format);
if (!strncmp(handler->scanner->default_format, "image/jpeg", 10)){
fprintf(stderr, "JPEG READ : [%s]\n", handler->scanner->default_format);
status = get_JPEG_data(handler->scanner);
}
else{
fprintf(stderr, "PNG READ : [%s]\n", handler->scanner->default_format);
status = get_PNG_data(handler->scanner);
}
if (status != SANE_STATUS_GOOD)
return (status);
handler->decompress_scan_data = SANE_TRUE;

Wyświetl plik

@ -153,11 +153,9 @@ SANE_Status escl_scan(capabilities_t *scanner, SANE_String_Const name, char *res
void escl_scanner(SANE_String_Const name, char *result);
// JPEG
void get_JPEG_dimension(FILE *fp, int *w, int *h, int *bps);
SANE_Status get_JPEG_data(capabilities_t *scanner);
SANE_Status get_JPEG_data(capabilities_t *scanner, int *w, int *h, int *bps);
// PNG
void get_PNG_dimension(FILE *fp, int *w, int *h, int *bps);
SANE_Status get_PNG_data(capabilities_t *scanner);
SANE_Status get_PNG_data(capabilities_t *scanner, int *w, int *h, int *bps);
#endif

Wyświetl plik

@ -35,37 +35,6 @@ error_exit(j_common_ptr cinfo)
longjmp(cinfo->client_data, 1);
}
/**
* \fn static void get_JPEG_dimension(FILE *fp, int *w, int *h)
* \brief Function that aims to get the dimensions of the jpeg image wich will be scanned.
* This function is called in the "sane_start" function.
*/
void
get_JPEG_dimension(FILE *fp, int *w, int *h, int *bps)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
jmp_buf env;
cinfo.err = jpeg_std_error(&jerr);
jerr.error_exit = error_exit;
cinfo.client_data = env;
if (setjmp(env))
return;
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, fp);
jpeg_read_header(&cinfo, TRUE);
cinfo.out_color_space = JCS_RGB;
jpeg_start_decompress(&cinfo);
*w = cinfo.output_width;
*h = cinfo.output_height;
*bps = 3;
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fseek(fp, SEEK_SET, 0);
}
/**
* \fn static boolean fill_input_buffer(j_decompress_ptr cinfo)
* \brief Called in the "skip_input_data" function.
@ -166,7 +135,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)
get_JPEG_data(capabilities_t *scanner, int *w, int *h, int *bps)
{
int start = 0;
struct jpeg_decompress_struct cinfo;
@ -209,6 +178,9 @@ get_JPEG_data(capabilities_t *scanner)
scanner->img_data = surface;
scanner->img_size = lineSize * cinfo.output_height;
scanner->img_read = 0;
*w = cinfo.output_width;
*h = cinfo.output_height;
*bps = cinfo.output_components;
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(scanner->tmp);
@ -217,14 +189,11 @@ get_JPEG_data(capabilities_t *scanner)
}
#else
void
get_JPEG_dimension(FILE __sane_unused__ *fp, int __sane_unused__ *w,
int __sane_unused__ *h, int __sane_unused__ *bps)
{
}
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__ *h,
int __sane_unused__ *bps)
{
}

Wyświetl plik

@ -15,91 +15,6 @@
#if(defined HAVE_LIBPNG)
/**
* \fn static void get_PNG_dimension(FILE *fp, int *w, int *h)
* \brief Function that aims to get the dimensions of the png image wich will be scanned.
* This function is called in the "sane_start" function.
*/
void
get_PNG_dimension(FILE *fp, int *w, int *h, int *bps)
{
unsigned int width, height;
png_byte magic[8];
fread (magic, 1, sizeof (magic), fp);
/* check for valid magic number */
if (!png_check_sig (magic, sizeof (magic)))
{
fprintf (stderr, "error: is not a valid PNG image!\n");
return;
}
/* create a png read struct */
png_structp png_ptr = png_create_read_struct
(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
{
return;
}
/* create a png info struct */
png_infop info_ptr = png_create_info_struct (png_ptr);
if (!info_ptr)
{
png_destroy_read_struct (&png_ptr, NULL, NULL);
return;
}
/* 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);
return;
}
/* setup libpng for using standard C fread() function
with our FILE pointer */
png_init_io (png_ptr, fp);
/* tell libpng that we have already read the magic number */
png_set_sig_bytes (png_ptr, sizeof (magic));
/* read png info */
png_read_info (png_ptr, info_ptr);
int bit_depth, color_type;
/* get some usefull information from header */
bit_depth = png_get_bit_depth (png_ptr, info_ptr);
color_type = png_get_color_type (png_ptr, info_ptr);
/* convert index color images to RGB images */
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb (png_ptr);
else if (color_type != PNG_COLOR_TYPE_RGB && color_type != PNG_COLOR_TYPE_RGB_ALPHA)
{
fprintf(stderr, "Format non pris en charge.\n");
return;
}
if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
*bps = 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);
*w = (int)width;
*h = (int)height;
/* finish decompression and release memory */
png_read_end (png_ptr, NULL);
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
fseek(fp, SEEK_SET, 0);
}
/**
* \fn SANE_Status escl_sane_decompressor(escl_sane_t *handler)
* \brief Function that aims to decompress the png image to SANE be able to read the image.
@ -108,7 +23,7 @@ get_PNG_dimension(FILE *fp, int *w, int *h, int *bps)
* \return SANE_STATUS_GOOD (if everything is OK, otherwise, SANE_STATUS_NO_MEM/SANE_STATUS_INVAL)
*/
SANE_Status
get_PNG_data(capabilities_t *scanner)
get_PNG_data(capabilities_t *scanner, int *w, int *h, int *components)
{
unsigned int width = 0; /* largeur */
unsigned int height = 0; /* hauteur */
@ -170,9 +85,9 @@ get_PNG_data(capabilities_t *scanner)
fprintf(stderr,"PNG format not supported.\n");
return (SANE_STATUS_INVAL);
}
if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
bps = 4;
else
if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
bps = 4;
else
bps = 3;
if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha (png_ptr);
@ -188,6 +103,10 @@ get_PNG_data(capabilities_t *scanner)
(png_uint_32*)(&height),
&bit_depth, &color_type,
NULL, NULL, NULL);
*w = (int)width;
*h = (int)height;
*components = bps;
// we can now allocate memory for storing pixel data
texels = (unsigned char *)malloc (sizeof (unsigned char) * width
* height * bps);
@ -203,23 +122,20 @@ get_PNG_data(capabilities_t *scanner)
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);
scanner->img_read = 0;
scanner->img_size = (int)(width * height * bps);
scanner->img_read = 0;
free (row_pointers);
fclose(scanner->tmp);
scanner->tmp = NULL;
return (SANE_STATUS_GOOD);
fclose(scanner->tmp);
scanner->tmp = NULL;
return (SANE_STATUS_GOOD);
}
#else
void
get_PNG_dimension(FILE __sane_unused__ *fp, int __sane_unused__ *w,
int __sane_unused__ *h, int __sane_unused__ *bps)
{
}
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__ *h,
int __sane_unused__ *bps)
{
}