1 /* sane - Scanner Access Now Easy.
2
3 Copyright (C) 2019 Thierry HUCHARD <thierry@ordissimo.com>
4
5 This file is part of the SANE package.
6
7 SANE is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11
12 SANE is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with sane; see the file COPYING.
19 If not, see <https://www.gnu.org/licenses/>.
20
21 This file implements a SANE backend for eSCL scanners. */
22
23 #define DEBUG_DECLARE_ONLY
24 #include "../include/sane/config.h"
25
26 #include "escl.h"
27
28 #include "../include/sane/sanei.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <errno.h>
35
36 #if(defined HAVE_MUPDF)
37 #include <mupdf/fitz.h>
38 #endif
39
40 #include <setjmp.h>
41
42
43 #if(defined HAVE_MUPDF)
44
45 // TODO: WIN32: HANDLE CreateFileW(), etc.
46 // TODO: POSIX: int creat(), read(), write(), lseeko, etc.
47
48 typedef struct fz_file_stream_escl_s
49 {
50 FILE *file;
51 unsigned char buffer[4096];
52 } fz_file_stream_escl;
53
54 static int
next_file_escl(fz_context *ctx, fz_stream *stm, size_t n)55 next_file_escl(fz_context *ctx, fz_stream *stm, size_t n)
56 {
57 fz_file_stream_escl *state = stm->state;
58
59 /* n is only a hint, that we can safely ignore */
60 n = fread(state->buffer, 1, sizeof(state->buffer), state->file);
61 if (n < sizeof(state->buffer) && ferror(state->file))
62 fz_throw(ctx, FZ_ERROR_GENERIC, "read error: %s", strerror(errno));
63 stm->rp = state->buffer;
64 stm->wp = state->buffer + n;
65 stm->pos += (int64_t)n;
66
67 if (n == 0)
68 return EOF;
69 return *stm->rp++;
70 }
71
72 static void
drop_file_escl(fz_context *ctx, void *state_)73 drop_file_escl(fz_context *ctx, void *state_)
74 {
75 fz_file_stream_escl *state = state_;
76 int n = fclose(state->file);
77 if (n < 0)
78 fz_warn(ctx, "close error: %s", strerror(errno));
79 fz_free(ctx, state);
80 }
81
82 static void
seek_file_escl(fz_context *ctx, fz_stream *stm, int64_t offset, int whence)83 seek_file_escl(fz_context *ctx, fz_stream *stm, int64_t offset, int whence)
84 {
85 fz_file_stream_escl *state = stm->state;
86 #ifdef _WIN32
87 int64_t n = _fseeki64(state->file, offset, whence);
88 #else
89 int64_t n = fseeko(state->file, offset, whence);
90 #endif
91 if (n < 0)
92 fz_throw(ctx, FZ_ERROR_GENERIC, "cannot seek: %s", strerror(errno));
93 #ifdef _WIN32
94 stm->pos = _ftelli64(state->file);
95 #else
96 stm->pos = ftello(state->file);
97 #endif
98 stm->rp = state->buffer;
99 stm->wp = state->buffer;
100 }
101
102 static fz_stream *
fz_open_file_ptr_escl(fz_context *ctx, FILE *file)103 fz_open_file_ptr_escl(fz_context *ctx, FILE *file)
104 {
105 fz_stream *stm;
106 fz_file_stream_escl *state = fz_malloc_struct(ctx, fz_file_stream_escl);
107 state->file = file;
108
109 stm = fz_new_stream(ctx, state, next_file_escl, drop_file_escl);
110 stm->seek = seek_file_escl;
111
112 return stm;
113 }
114
115 /**
116 * \fn SANE_Status escl_sane_decompressor(escl_sane_t *handler)
117 * \brief Function that aims to decompress the pdf image to SANE be able
118 * to read the image.
119 * This function is called in the "sane_read" function.
120 *
121 * \return SANE_STATUS_GOOD (if everything is OK, otherwise,
122 * SANE_STATUS_NO_MEM/SANE_STATUS_INVAL)
123 */
124 SANE_Status
get_PDF_data(capabilities_t *scanner, int *width, int *height, int *bps)125 get_PDF_data(capabilities_t *scanner, int *width, int *height, int *bps)
126 {
127 int page_number = -1, page_count = -2;
128 fz_context *ctx;
129 fz_document *doc;
130 fz_pixmap *pix;
131 fz_matrix ctm;
132 fz_stream *stream;
133 unsigned char *surface = NULL; /* Image data */
134 SANE_Status status = SANE_STATUS_GOOD;
135
136 /* Create a context to hold the exception stack and various caches. */
137 ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
138 if (!ctx)
139 {
140 DBG(1, "cannot create mupdf context\n");
141 status = SANE_STATUS_INVAL;
142 goto close_file;
143 }
144
145 /* Register the default file types to handle. */
146 fz_try(ctx)
147 fz_register_document_handlers(ctx);
148 fz_catch(ctx)
149 {
150 DBG(1, "cannot register document handlers: %s\n", fz_caught_message(ctx));
151 status = SANE_STATUS_INVAL;
152 goto drop_context;
153 }
154
155 /* Open the stream. */
156 fz_try(ctx)
157 stream = fz_open_file_ptr_escl(ctx, scanner->tmp);
158 fz_catch(ctx)
159 {
160 DBG(1, "cannot open stream: %s\n", fz_caught_message(ctx));
161 status = SANE_STATUS_INVAL;
162 goto drop_context;
163 }
164
165 /* Seek stream. */
166 fz_try(ctx)
167 fz_seek(ctx, stream, 0, SEEK_SET);
168 fz_catch(ctx)
169 {
170 DBG(1, "cannot seek stream: %s\n", fz_caught_message(ctx));
171 status = SANE_STATUS_INVAL;
172 goto drop_stream;
173 }
174
175 /* Open the document. */
176 fz_try(ctx)
177 doc = fz_open_document_with_stream(ctx, "filename.pdf", stream);
178 fz_catch(ctx)
179 {
180 DBG(1, "cannot open document: %s\n", fz_caught_message(ctx));
181 status = SANE_STATUS_INVAL;
182 goto drop_stream;
183 }
184
185 /* Count the number of pages. */
186 fz_try(ctx)
187 page_count = fz_count_pages(ctx, doc);
188 fz_catch(ctx)
189 {
190 DBG(1, "cannot count number of pages: %s\n", fz_caught_message(ctx));
191 status = SANE_STATUS_INVAL;
192 goto drop_document;
193 }
194
195 if (page_number < 0 || page_number >= page_count)
196 {
197 DBG(1, "page number out of range: %d (page count %d)\n", page_number + 1, page_count);
198 status = SANE_STATUS_INVAL;
199 goto drop_document;
200 }
201
202 /* Compute a transformation matrix for the zoom and rotation desired. */
203 /* The default resolution without scaling is 72 dpi. */
204 fz_scale(&ctm, (float)1.0, (float)1.0);
205 fz_pre_rotate(&ctm, (float)0.0);
206
207 /* Render page to an RGB pixmap. */
208 fz_try(ctx)
209 pix = fz_new_pixmap_from_page_number(ctx, doc, 0, &ctm, fz_device_rgb(ctx), 0);
210 fz_catch(ctx)
211 {
212 DBG(1, "cannot render page: %s\n", fz_caught_message(ctx));
213 status = SANE_STATUS_INVAL;
214 goto drop_document;
215 }
216
217 surface = malloc(pix->h * pix->stride);
218 memcpy(surface, pix->samples, (pix->h * pix->stride));
219
220 // If necessary, trim the image.
221 surface = escl_crop_surface(scanner, surface, pix->w, pix->h, pix->n, width, height);
222 if (!surface) {
223 DBG( 1, "Escl Pdf : Surface Memory allocation problem\n");
224 status = SANE_STATUS_NO_MEM;
225 goto drop_pix;
226 }
227 *bps = pix->n;
228
229 /* Clean up. */
230 drop_pix:
231 fz_drop_pixmap(ctx, pix);
232 drop_document:
233 fz_drop_document(ctx, doc);
234 drop_stream:
235 fz_drop_stream(ctx, stream);
236 drop_context:
237 fz_drop_context(ctx);
238
239 close_file:
240 if (scanner->tmp)
241 fclose(scanner->tmp);
242 scanner->tmp = NULL;
243 return status;
244 }
245 #else
246
247 SANE_Status
get_PDF_data(capabilities_t __sane_unused__ *scanner, int __sane_unused__ *width, int __sane_unused__ *height, int __sane_unused__ *bps)248 get_PDF_data(capabilities_t __sane_unused__ *scanner,
249 int __sane_unused__ *width,
250 int __sane_unused__ *height,
251 int __sane_unused__ *bps)
252 {
253 return (SANE_STATUS_INVAL);
254 }
255
256 #endif
257