1141cc406Sopenharmony_ci/* sane - Scanner Access Now Easy.
2141cc406Sopenharmony_ci
3141cc406Sopenharmony_ci   Copyright (C) 2019 Touboul Nathane
4141cc406Sopenharmony_ci   Copyright (C) 2019 Thierry HUCHARD <thierry@ordissimo.com>
5141cc406Sopenharmony_ci
6141cc406Sopenharmony_ci   This file is part of the SANE package.
7141cc406Sopenharmony_ci
8141cc406Sopenharmony_ci   SANE is free software; you can redistribute it and/or modify it under
9141cc406Sopenharmony_ci   the terms of the GNU General Public License as published by the Free
10141cc406Sopenharmony_ci   Software Foundation; either version 3 of the License, or (at your
11141cc406Sopenharmony_ci   option) any later version.
12141cc406Sopenharmony_ci
13141cc406Sopenharmony_ci   SANE is distributed in the hope that it will be useful, but WITHOUT
14141cc406Sopenharmony_ci   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15141cc406Sopenharmony_ci   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16141cc406Sopenharmony_ci   for more details.
17141cc406Sopenharmony_ci
18141cc406Sopenharmony_ci   You should have received a copy of the GNU General Public License
19141cc406Sopenharmony_ci   along with sane; see the file COPYING.
20141cc406Sopenharmony_ci   If not, see <https://www.gnu.org/licenses/>.
21141cc406Sopenharmony_ci
22141cc406Sopenharmony_ci   This file implements a SANE backend for eSCL scanners.  */
23141cc406Sopenharmony_ci
24141cc406Sopenharmony_ci#define DEBUG_DECLARE_ONLY
25141cc406Sopenharmony_ci#include "../include/sane/config.h"
26141cc406Sopenharmony_ci
27141cc406Sopenharmony_ci#include "escl.h"
28141cc406Sopenharmony_ci
29141cc406Sopenharmony_ci#include "../include/_stdint.h"
30141cc406Sopenharmony_ci
31141cc406Sopenharmony_ci#include "../include/sane/sanei.h"
32141cc406Sopenharmony_ci
33141cc406Sopenharmony_ci#include <stdio.h>
34141cc406Sopenharmony_ci#include <stdlib.h>
35141cc406Sopenharmony_ci#include <string.h>
36141cc406Sopenharmony_ci#include <unistd.h>
37141cc406Sopenharmony_ci
38141cc406Sopenharmony_ci#if(defined HAVE_TIFFIO_H)
39141cc406Sopenharmony_ci#include <tiffio.h>
40141cc406Sopenharmony_ci#endif
41141cc406Sopenharmony_ci
42141cc406Sopenharmony_ci#include <setjmp.h>
43141cc406Sopenharmony_ci
44141cc406Sopenharmony_ci
45141cc406Sopenharmony_ci#if(defined HAVE_TIFFIO_H)
46141cc406Sopenharmony_ci
47141cc406Sopenharmony_ci/**
48141cc406Sopenharmony_ci * \fn SANE_Status escl_sane_decompressor(escl_sane_t *handler)
49141cc406Sopenharmony_ci * \brief Function that aims to decompress the png image to SANE be able to read the image.
50141cc406Sopenharmony_ci *        This function is called in the "sane_read" function.
51141cc406Sopenharmony_ci *
52141cc406Sopenharmony_ci * \return SANE_STATUS_GOOD (if everything is OK, otherwise, SANE_STATUS_NO_MEM/SANE_STATUS_INVAL)
53141cc406Sopenharmony_ci */
54141cc406Sopenharmony_ciSANE_Status
55141cc406Sopenharmony_ciget_TIFF_data(capabilities_t *scanner, int *width, int *height, int *bps)
56141cc406Sopenharmony_ci{
57141cc406Sopenharmony_ci    TIFF* tif = NULL;
58141cc406Sopenharmony_ci    uint32_t w = 0;
59141cc406Sopenharmony_ci    uint32_t h = 0;
60141cc406Sopenharmony_ci    unsigned char *surface = NULL;         /*  image data*/
61141cc406Sopenharmony_ci    int components = 4;
62141cc406Sopenharmony_ci    uint32_t npixels = 0;
63141cc406Sopenharmony_ci    SANE_Status status = SANE_STATUS_GOOD;
64141cc406Sopenharmony_ci
65141cc406Sopenharmony_ci    lseek(fileno(scanner->tmp), 0, SEEK_SET);
66141cc406Sopenharmony_ci    tif = TIFFFdOpen(fileno(scanner->tmp), "temp", "r");
67141cc406Sopenharmony_ci    if (!tif) {
68141cc406Sopenharmony_ci        DBG( 1, "Escl Tiff : Can not open, or not a TIFF file.\n");
69141cc406Sopenharmony_ci        status = SANE_STATUS_INVAL;
70141cc406Sopenharmony_ci	goto close_file;
71141cc406Sopenharmony_ci    }
72141cc406Sopenharmony_ci
73141cc406Sopenharmony_ci    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
74141cc406Sopenharmony_ci    TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
75141cc406Sopenharmony_ci    npixels = w * h;
76141cc406Sopenharmony_ci    surface = (unsigned char*) malloc(npixels * sizeof (uint32_t));
77141cc406Sopenharmony_ci    if (surface == NULL)
78141cc406Sopenharmony_ci    {
79141cc406Sopenharmony_ci        DBG( 1, "Escl Tiff : raster Memory allocation problem.\n");
80141cc406Sopenharmony_ci        status = SANE_STATUS_INVAL;
81141cc406Sopenharmony_ci	goto close_tiff;
82141cc406Sopenharmony_ci    }
83141cc406Sopenharmony_ci
84141cc406Sopenharmony_ci    if (!TIFFReadRGBAImage(tif, w, h, (uint32_t *)surface, 0))
85141cc406Sopenharmony_ci    {
86141cc406Sopenharmony_ci        DBG( 1, "Escl Tiff : Problem reading image data.\n");
87141cc406Sopenharmony_ci        status = SANE_STATUS_INVAL;
88141cc406Sopenharmony_ci        free(surface);
89141cc406Sopenharmony_ci	goto close_tiff;
90141cc406Sopenharmony_ci    }
91141cc406Sopenharmony_ci
92141cc406Sopenharmony_ci    *bps = components;
93141cc406Sopenharmony_ci
94141cc406Sopenharmony_ci    // If necessary, trim the image.
95141cc406Sopenharmony_ci    surface = escl_crop_surface(scanner, surface, w, h, components, width, height);
96141cc406Sopenharmony_ci    if (!surface)  {
97141cc406Sopenharmony_ci        DBG( 1, "Escl Tiff : Surface Memory allocation problem\n");
98141cc406Sopenharmony_ci        status = SANE_STATUS_INVAL;
99141cc406Sopenharmony_ci    }
100141cc406Sopenharmony_ci
101141cc406Sopenharmony_ciclose_tiff:
102141cc406Sopenharmony_ci    TIFFClose(tif);
103141cc406Sopenharmony_ciclose_file:
104141cc406Sopenharmony_ci    if (scanner->tmp)
105141cc406Sopenharmony_ci       fclose(scanner->tmp);
106141cc406Sopenharmony_ci    scanner->tmp = NULL;
107141cc406Sopenharmony_ci    return (status);
108141cc406Sopenharmony_ci}
109141cc406Sopenharmony_ci#else
110141cc406Sopenharmony_ci
111141cc406Sopenharmony_ciSANE_Status
112141cc406Sopenharmony_ciget_TIFF_data(capabilities_t __sane_unused__ *scanner,
113141cc406Sopenharmony_ci              int __sane_unused__ *w,
114141cc406Sopenharmony_ci              int __sane_unused__ *h,
115141cc406Sopenharmony_ci              int __sane_unused__ *bps)
116141cc406Sopenharmony_ci{
117141cc406Sopenharmony_ci    return (SANE_STATUS_INVAL);
118141cc406Sopenharmony_ci}
119141cc406Sopenharmony_ci
120141cc406Sopenharmony_ci#endif
121