summaryrefslogtreecommitdiff
path: root/backend/escl/escl_tiff.c
blob: e33498ca510a2f2a900b22d468fc8529961d805c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* sane - Scanner Access Now Easy.

   Copyright (C) 2019 Touboul Nathane
   Copyright (C) 2019 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, see <https://www.gnu.org/licenses/>.

   This file implements a SANE backend for eSCL scanners.  */

#define DEBUG_DECLARE_ONLY
#include "../include/sane/config.h"

#include "escl.h"

#include "../include/sane/sanei.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#if(defined HAVE_TIFFIO_H)
#include <tiffio.h>
#endif

#include <setjmp.h>


#if(defined HAVE_TIFFIO_H)

/**
 * \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.
 *        This function is called in the "sane_read" function.
 *
 * \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 *width, int *height, int *bps)
{
    TIFF* tif = NULL;
    uint32  w = 0;
    uint32  h = 0;
    unsigned char *surface = NULL;         /*  image data*/
    int components = 4;
    uint32 npixels = 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");
        status = SANE_STATUS_INVAL;
	goto close_file;
    }

    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");
        status = SANE_STATUS_INVAL;
	goto close_tiff;
    }

    if (!TIFFReadRGBAImage(tif, w, h, (uint32 *)surface, 0))
    {
        DBG( 1, "Escl Tiff : Problem reading image data.\n");
        status = SANE_STATUS_INVAL;
        free(surface);
	goto close_tiff;
    }

    *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;
    }

close_tiff:
    TIFFClose(tif);
close_file:
    if (scanner->tmp)
       fclose(scanner->tmp);
    scanner->tmp = NULL;
    return (status);
}
#else

SANE_Status
get_TIFF_data(capabilities_t __sane_unused__ *scanner,
              int __sane_unused__ *w,
              int __sane_unused__ *h,
              int __sane_unused__ *bps)
{
    return (SANE_STATUS_INVAL);
}

#endif