Setting up the cut-out of the received image.

merge-requests/332/merge
Thierry HUCHARD 2020-01-31 18:28:04 +01:00
rodzic d42521dfd4
commit 5298b0e018
3 zmienionych plików z 152 dodań i 31 usunięć

Wyświetl plik

@ -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);

Wyświetl plik

@ -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);

Wyświetl plik

@ -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);