1 /* sane - Scanner Access Now Easy.
2 
3    Copyright (C) 2019 Touboul Nathane
4    Copyright (C) 2019 Thierry HUCHARD <thierry@ordissimo.com>
5 
6    This file is part of the SANE package.
7 
8    SANE is free software; you can redistribute it and/or modify it under
9    the terms of the GNU General Public License as published by the Free
10    Software Foundation; either version 3 of the License, or (at your
11    option) any later version.
12 
13    SANE is distributed in the hope that it will be useful, but WITHOUT
14    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16    for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with sane; see the file COPYING.
20    If not, see <https://www.gnu.org/licenses/>.
21 
22    This file implements a SANE backend for eSCL scanners.  */
23 
24 #define DEBUG_DECLARE_ONLY
25 #include "../include/sane/config.h"
26 
27 #include "escl.h"
28 
29 #include "../include/_stdint.h"
30 
31 #include "../include/sane/sanei.h"
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 
38 #if(defined HAVE_TIFFIO_H)
39 #include <tiffio.h>
40 #endif
41 
42 #include <setjmp.h>
43 
44 
45 #if(defined HAVE_TIFFIO_H)
46 
47 /**
48  * \fn SANE_Status escl_sane_decompressor(escl_sane_t *handler)
49  * \brief Function that aims to decompress the png image to SANE be able to read the image.
50  *        This function is called in the "sane_read" function.
51  *
52  * \return SANE_STATUS_GOOD (if everything is OK, otherwise, SANE_STATUS_NO_MEM/SANE_STATUS_INVAL)
53  */
54 SANE_Status
get_TIFF_data(capabilities_t *scanner, int *width, int *height, int *bps)55 get_TIFF_data(capabilities_t *scanner, int *width, int *height, int *bps)
56 {
57     TIFF* tif = NULL;
58     uint32_t w = 0;
59     uint32_t h = 0;
60     unsigned char *surface = NULL;         /*  image data*/
61     int components = 4;
62     uint32_t npixels = 0;
63     SANE_Status status = SANE_STATUS_GOOD;
64 
65     lseek(fileno(scanner->tmp), 0, SEEK_SET);
66     tif = TIFFFdOpen(fileno(scanner->tmp), "temp", "r");
67     if (!tif) {
68         DBG( 1, "Escl Tiff : Can not open, or not a TIFF file.\n");
69         status = SANE_STATUS_INVAL;
70 	goto close_file;
71     }
72 
73     TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
74     TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
75     npixels = w * h;
76     surface = (unsigned char*) malloc(npixels * sizeof (uint32_t));
77     if (surface == NULL)
78     {
79         DBG( 1, "Escl Tiff : raster Memory allocation problem.\n");
80         status = SANE_STATUS_INVAL;
81 	goto close_tiff;
82     }
83 
84     if (!TIFFReadRGBAImage(tif, w, h, (uint32_t *)surface, 0))
85     {
86         DBG( 1, "Escl Tiff : Problem reading image data.\n");
87         status = SANE_STATUS_INVAL;
88         free(surface);
89 	goto close_tiff;
90     }
91 
92     *bps = components;
93 
94     // If necessary, trim the image.
95     surface = escl_crop_surface(scanner, surface, w, h, components, width, height);
96     if (!surface)  {
97         DBG( 1, "Escl Tiff : Surface Memory allocation problem\n");
98         status = SANE_STATUS_INVAL;
99     }
100 
101 close_tiff:
102     TIFFClose(tif);
103 close_file:
104     if (scanner->tmp)
105        fclose(scanner->tmp);
106     scanner->tmp = NULL;
107     return (status);
108 }
109 #else
110 
111 SANE_Status
get_TIFF_data(capabilities_t __sane_unused__ *scanner, int __sane_unused__ *w, int __sane_unused__ *h, int __sane_unused__ *bps)112 get_TIFF_data(capabilities_t __sane_unused__ *scanner,
113               int __sane_unused__ *w,
114               int __sane_unused__ *h,
115               int __sane_unused__ *bps)
116 {
117     return (SANE_STATUS_INVAL);
118 }
119 
120 #endif
121