summaryrefslogtreecommitdiff
path: root/backend/escl/escl_jpeg.c
blob: 62c20c09a882d2d1ac256eb24f6b49778b674e55 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* 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>

#if(defined HAVE_LIBJPEG)
#  include <jpeglib.h>
#endif

#include <setjmp.h>

#define INPUT_BUFFER_SIZE 4096

#if(defined HAVE_LIBJPEG)
struct my_error_mgr
{
    struct jpeg_error_mgr errmgr;
    jmp_buf escape;
};

typedef struct
{
    struct jpeg_source_mgr pub;
    FILE *ctx;
    unsigned char buffer[INPUT_BUFFER_SIZE];
} my_source_mgr;

/**
 * \fn static boolean fill_input_buffer(j_decompress_ptr cinfo)
 * \brief Called in the "skip_input_data" function.
 *
 * \return TRUE (everything is OK)
 */
static boolean
fill_input_buffer(j_decompress_ptr cinfo)
{
    my_source_mgr *src = (my_source_mgr *) cinfo->src;
    int nbytes = 0;

    nbytes = fread(src->buffer, 1, INPUT_BUFFER_SIZE, src->ctx);
    if (nbytes <= 0) {
        src->buffer[0] = (unsigned char) 0xFF;
        src->buffer[1] = (unsigned char) JPEG_EOI;
        nbytes = 2;
    }
    src->pub.next_input_byte = src->buffer;
    src->pub.bytes_in_buffer = nbytes;
    return (TRUE);
}

/**
 * \fn static void skip_input_data(j_decompress_ptr cinfo, long num_bytes)
 * \brief Called in the "jpeg_RW_src" function.
 */
static void
skip_input_data(j_decompress_ptr cinfo, long num_bytes)
{
    my_source_mgr *src = (my_source_mgr *) cinfo->src;

    if (num_bytes > 0) {
        while (num_bytes > (long) src->pub.bytes_in_buffer) {
            num_bytes -= (long) src->pub.bytes_in_buffer;
            (void) src->pub.fill_input_buffer(cinfo);
        }
        src->pub.next_input_byte += (size_t) num_bytes;
        src->pub.bytes_in_buffer -= (size_t) num_bytes;
    }
}

static void
term_source(j_decompress_ptr __sane_unused__ cinfo)
{
    return;
}

static void
init_source(j_decompress_ptr __sane_unused__ cinfo)
{
    return;
}

/**
 * \fn static void jpeg_RW_src(j_decompress_ptr cinfo, FILE *ctx)
 * \brief Called in the "escl_sane_decompressor" function.
 */
static void
jpeg_RW_src(j_decompress_ptr cinfo, FILE *ctx)
{
    my_source_mgr *src;

    if (cinfo->src == NULL) {
        cinfo->src = (struct jpeg_source_mgr *)(*cinfo->mem->alloc_small)
            ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(my_source_mgr));
    }
    src = (my_source_mgr *) cinfo->src;
    src->pub.init_source = init_source;
    src->pub.fill_input_buffer = fill_input_buffer;
    src->pub.skip_input_data = skip_input_data;
    src->pub.resync_to_restart = jpeg_resync_to_restart;
    src->pub.term_source = term_source;
    src->ctx = ctx;
    src->pub.bytes_in_buffer = 0;
    src->pub.next_input_byte = NULL;
}

static void
my_error_exit(j_common_ptr cinfo)
{
    struct my_error_mgr *err = (struct my_error_mgr *)cinfo->err;

    longjmp(err->escape, 1);
}

static void
output_no_message(j_common_ptr __sane_unused__ cinfo)
{
}

/**
 * \fn SANE_Status escl_sane_decompressor(escl_sane_t *handler)
 * \brief Function that aims to decompress the jpeg 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_JPEG_data(capabilities_t *scanner, int *width, int *height, int *bps)
{
    int start = 0;
    struct jpeg_decompress_struct cinfo;
    JSAMPROW rowptr[1];
    unsigned char *surface = NULL;
    struct my_error_mgr jerr;
    int lineSize = 0;
    JDIMENSION x_off = 0;
    JDIMENSION y_off = 0;
    JDIMENSION w = 0;
    JDIMENSION h = 0;
    int pos = 0;

    if (scanner->tmp == NULL)
        return (SANE_STATUS_INVAL);
    fseek(scanner->tmp, SEEK_SET, 0);
    start = ftell(scanner->tmp);
    cinfo.err = jpeg_std_error(&jerr.errmgr);
    jerr.errmgr.error_exit = my_error_exit;
    jerr.errmgr.output_message = output_no_message;
    if (setjmp(jerr.escape)) {
        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);
           scanner->tmp = NULL;
        }
        return (SANE_STATUS_INVAL);
    }
    jpeg_create_decompress(&cinfo);
    jpeg_RW_src(&cinfo, scanner->tmp);
    jpeg_read_header(&cinfo, TRUE);
    cinfo.out_color_space = JCS_RGB;
    cinfo.quantize_colors = FALSE;
    jpeg_calc_output_dimensions(&cinfo);
    double ratio = (double)cinfo.output_width / (double)scanner->caps[scanner->source].width;
    int rw = (int)((double)scanner->caps[scanner->source].width * ratio);
    int rh = (int)((double)scanner->caps[scanner->source].height * ratio);
    int rx = (int)((double)scanner->caps[scanner->source].pos_x * ratio);
    int ry = (int)((double)scanner->caps[scanner->source].pos_y * ratio);


    if (cinfo.output_width < (unsigned int)rw)
          rw = cinfo.output_width;
    if (rx < 0)
          rx = 0;

    if (cinfo.output_height < (unsigned int)rh)
          rh = cinfo.output_height;
    if (ry < 0)
          ry = 0;
    DBG(10, "1-JPEF Geometry [%dx%d|%dx%d]\n",
	        rx,
	        ry,
	        rw,
	        rh);
    x_off = rx;
    if (x_off > (unsigned int)rw) {
       w = rw;
       x_off = 0;
    }
    else
       w = rw - x_off;
    y_off = ry;
    if(y_off > (unsigned int)rh) {
       h = rh;
       y_off = 0;
    }
    else
       h = rh - y_off;
    DBG(10, "2-JPEF Geometry [%dx%d|%dx%d]\n",
	        x_off,
	        y_off,
	        w,
	        h);
    jpeg_start_decompress(&cinfo);
    if (x_off > 0 || w < cinfo.output_width)
       jpeg_crop_scanline(&cinfo, &x_off, &w);
    lineSize = w * cinfo.output_components;
    if (y_off > 0)
        jpeg_skip_scanlines(&cinfo, y_off);
    surface = malloc(cinfo.output_width * cinfo.output_height * cinfo.output_components);
    if (surface == NULL) {
        jpeg_destroy_decompress(&cinfo);
        DBG( 1, "Escl Jpeg : Memory allocation problem\n");
        if (scanner->tmp) {
           fclose(scanner->tmp);
           scanner->tmp = NULL;
        }
        return (SANE_STATUS_NO_MEM);
    }
    pos = 0;
    while (cinfo.output_scanline < (unsigned int)rh) {
        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 * h;
    scanner->img_read = 0;
    *width = w;
    *height = h;
    *bps = cinfo.output_components;
    // jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);
    fclose(scanner->tmp);
    scanner->tmp = NULL;
    return (SANE_STATUS_GOOD);
}
#else

SANE_Status
get_JPEG_data(capabilities_t __sane_unused__ *scanner,
              int __sane_unused__ *width,
              int __sane_unused__ *height,
              int __sane_unused__ *bps)
{
    return (SANE_STATUS_INVAL);
}

#endif