1 /* sane - Scanner Access Now Easy.
2
3 pieusb.c
4
5 Copyright (C) 2012-2015 Jan Vleeshouwers, Michael Rickmann, Klaus Kaempf
6
7 This file is part of the SANE package.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <https://www.gnu.org/licenses/>.
21
22 As a special exception, the authors of SANE give permission for
23 additional uses of the libraries contained in this release of SANE.
24
25 The exception is that, if you link a SANE library with other files
26 to produce an executable, this does not by itself cause the
27 resulting executable to be covered by the GNU General Public
28 License. Your use of that executable is in no way restricted on
29 account of linking the SANE library code into it.
30
31 This exception does not, however, invalidate any other reasons why
32 the executable file might be covered by the GNU General Public
33 License.
34
35 If you submit changes to SANE to the maintainers to be included in
36 a subsequent release, you agree by submitting the changes that
37 those changes may be distributed with this exception intact.
38
39 If you write modifications of your own for SANE, it is your choice
40 whether to permit this exception to apply to your modifications.
41 If you do not wish that, delete this exception notice. */
42
43 /* =========================================================================
44 *
45 * SANE interface to three Reflecta USB scanners with USB-id 0x05e3/0x0145:
46 * - Reflecta CrystalScan 7200 (model id 0x30)
47 * - Reflecta ProScan 7200 (model id 0x36)
48 * - Reflecta 6000 Multiple Slide Scanner (model id 0x3A)
49 *
50 * ========================================================================= */
51
52 #define DEBUG_NOT_STATIC
53 /* --------------------------------------------------------------------------
54 *
55 * INCLUDES
56 *
57 * --------------------------------------------------------------------------*/
58
59 #include "../include/sane/config.h"
60 /* Standard includes for various utiliy functions */
61 #include <stdio.h> /* for FILE */
62 #include <string.h> /* for strlen */
63 #include <stdlib.h> /* for NULL */
64 #include <stdint.h>
65 #include <math.h>
66
67 /* Configuration defines */
68 #include "../include/sane/config.h"
69
70 /* SANE includes */
71 #include "../include/sane/sane.h"
72 #include "../include/sane/saneopts.h"
73 #include "../include/sane/sanei_usb.h"
74 #include "../include/sane/sanei_config.h"
75
76 /* Backend includes */
77 #define BACKEND_NAME pieusb
78 #include "../include/sane/sanei_backend.h"
79 #include "pieusb.h"
80 #include "pieusb_specific.h"
81
82 #define CAN_DO_4_CHANNEL_TIFF
83
84 #ifdef CAN_DO_4_CHANNEL_TIFF
85 extern void write_tiff_rgbi_header (FILE *fptr, int width, int height, int depth, int resolution, const char *icc_profile);
86 #endif
87
88 /* --------------------------------------------------------------------------
89 *
90 * DEFINES
91 *
92 * --------------------------------------------------------------------------*/
93
94 /* Build number of this backend */
95 #define BUILD 1
96
97 /* Configuration filename */
98 #define PIEUSB_CONFIG_FILE "pieusb.conf"
99
100 /* Debug error levels */
101 #define DBG_error 1 /* errors */
102 #define DBG_warning 3 /* warnings */
103 #define DBG_info 5 /* information */
104 #define DBG_info_sane 7 /* information sane interface level */
105 #define DBG_inquiry 8 /* inquiry data */
106 #define DBG_info_proc 9 /* information pieusb backend functions */
107 #define DBG_info_scan 11 /* information scanner commands */
108 #define DBG_info_usb 13 /* information usb level functions */
109
110 /* device flags */
111
112 #define FLAG_SLIDE_TRANSPORT 0x01
113
114 /* --------------------------------------------------------------------------
115 *
116 * SUPPORTED DEVICES SPECIFICS
117 *
118 * --------------------------------------------------------------------------*/
119
120 struct Pieusb_USB_Device_Entry* pieusb_supported_usb_device_list = NULL;
121 struct Pieusb_USB_Device_Entry pieusb_supported_usb_device; /* for searching */
122
123 /* --------------------------------------------------------------------------
124 *
125 * LISTS OF ACTIVE DEVICE DEFINITIONS AND SCANNERS
126 *
127 * --------------------------------------------------------------------------*/
128
129 Pieusb_Device_Definition *pieusb_definition_list_head = NULL;
130 static Pieusb_Scanner *first_handle = NULL;
131 static const SANE_Device **devlist = NULL;
132
133 /* --------------------------------------------------------------------------
134 *
135 * SANE INTERFACE
136 *
137 * --------------------------------------------------------------------------*/
138
139 /**
140 * Initializes the debugging system, the USB system, the version code and
141 * 'attaches' available scanners, i.e. creates device definitions for all
142 * scanner devices found.
143 *
144 * @param version_code
145 * @param authorize
146 * @return SANE_STATUS_GOOD
147 */
148 SANE_Status
sane_init(SANE_Int * version_code,SANE_Auth_Callback __sane_unused__ authorize)149 sane_init (SANE_Int * version_code, SANE_Auth_Callback __sane_unused__ authorize)
150 {
151 FILE *fp;
152 char config_line[PATH_MAX];
153 SANE_Word vendor_id;
154 SANE_Word product_id;
155 SANE_Int model_number;
156 SANE_Int flags;
157 SANE_Status status;
158 int i;
159
160 /* Initialize debug logging */
161 DBG_INIT ();
162
163 DBG (DBG_info_sane, "sane_init() build %d\n", BUILD);
164
165 /* Set version code to current major, minor and build number */
166 /* TODO: use V_MINOR instead or SANE_CURRENT_MINOR? If so, why? */
167 if (version_code)
168 *version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR, BUILD);
169
170 /* Initialize usb */
171 sanei_usb_init ();
172 sanei_usb_set_timeout (30 * 1000); /* 30 sec timeout */
173
174 /* There are currently 3 scanners hardcoded into this backend, see below.
175 * The config file may add other scanners using a line like
176 * usb 0x1234 0x5678 0x9A
177 * where the first two hex numbers are vendor and product id, and the last
178 * number is the model number. Unfortunately, this is not according to the
179 * config file standard. Anyone any suggestions? */
180
181 /* Create default list */
182 pieusb_supported_usb_device_list = calloc (4, sizeof(struct Pieusb_USB_Device_Entry));
183 if (pieusb_supported_usb_device_list == NULL)
184 return SANE_STATUS_NO_MEM;
185 /* Reflecta CrystalScan 7200, model number 0x30 */
186 pieusb_supported_usb_device_list[0].vendor = 0x05e3;
187 pieusb_supported_usb_device_list[0].product = 0x0145;
188 pieusb_supported_usb_device_list[0].model = 0x30;
189 pieusb_supported_usb_device_list[0].flags = 0;
190 /* Reflecta ProScan 7200, model number 0x36 */
191 pieusb_supported_usb_device_list[1].vendor = 0x05e3;
192 pieusb_supported_usb_device_list[1].product = 0x0145;
193 pieusb_supported_usb_device_list[1].model = 0x36;
194 pieusb_supported_usb_device_list[1].flags = 0;
195 /* Reflecta 6000 Multiple Slide Scanner, model number 0x3a */
196 pieusb_supported_usb_device_list[2].vendor = 0x05e3;
197 pieusb_supported_usb_device_list[2].product = 0x0142;
198 pieusb_supported_usb_device_list[2].model = 0x3a;
199 pieusb_supported_usb_device_list[2].flags = FLAG_SLIDE_TRANSPORT;
200 /* end of list */
201 pieusb_supported_usb_device_list[3].vendor = 0;
202 pieusb_supported_usb_device_list[3].product = 0;
203 pieusb_supported_usb_device_list[3].model = 0;
204 pieusb_supported_usb_device_list[3].flags = 0;
205
206 /* Add entries from config file */
207 fp = sanei_config_open (PIEUSB_CONFIG_FILE);
208 if (!fp) {
209 DBG (DBG_info_sane, "sane_init() did not find a config file, using default list of supported devices\n");
210 } else {
211 while (sanei_config_read (config_line, sizeof (config_line), fp)) {
212 /* Ignore line comments and empty lines */
213 if (config_line[0] == '#') continue;
214 if (strlen (config_line) == 0) continue;
215 /* Ignore lines which do not begin with 'usb ' */
216 if (strncmp (config_line, "usb ", 4) != 0) continue;
217 /* Parse vendor-id, product-id and model number and add to list */
218 DBG (DBG_info_sane, "sane_init() config file parsing %s\n", config_line);
219 status = sanei_pieusb_parse_config_line(config_line, &vendor_id, &product_id, &model_number, &flags);
220 if (status == SANE_STATUS_GOOD) {
221 DBG (DBG_info_sane, "sane_init() config file lists device %04x %04x %02x %02x\n",vendor_id, product_id, model_number, flags);
222 if (!sanei_pieusb_supported_device_list_contains(vendor_id, product_id, model_number, flags)) {
223 DBG (DBG_info_sane, "sane_init() adding device %04x %04x %02x %02x\n",vendor_id, product_id, model_number, flags);
224 sanei_pieusb_supported_device_list_add(vendor_id, product_id, model_number, flags);
225 } else {
226 DBG (DBG_info_sane, "sane_init() list already contains %04x %04x %02x %02x\n", vendor_id, product_id, model_number, flags);
227 }
228 } else {
229 DBG (DBG_info_sane, "sane_init() config file parsing %s: error\n", config_line);
230 }
231 }
232 fclose (fp);
233 }
234
235 /* Loop through supported device list */
236 i = 0;
237 while (pieusb_supported_usb_device_list[i].vendor != 0) {
238 /* Check if the supported device is present. If so, create a device
239 * definition structure for it.
240 * The variable pieusb_supported_usb_device is set to current values,
241 * which are used in the callback. */
242 pieusb_supported_usb_device.vendor = pieusb_supported_usb_device_list[i].vendor;
243 pieusb_supported_usb_device.product = pieusb_supported_usb_device_list[i].product;
244 pieusb_supported_usb_device.model = pieusb_supported_usb_device_list[i].model;
245 pieusb_supported_usb_device.flags = pieusb_supported_usb_device_list[i].flags;
246 pieusb_supported_usb_device.device_number = -1; /* No device number (yet) */
247 DBG( DBG_info_sane, "sane_init() looking for scanner %04x %04x model %02x, flags %02x\n",
248 pieusb_supported_usb_device.vendor,
249 pieusb_supported_usb_device.product,
250 pieusb_supported_usb_device.model,
251 pieusb_supported_usb_device.flags);
252 sanei_usb_find_devices (pieusb_supported_usb_device.vendor, pieusb_supported_usb_device.product, sanei_pieusb_find_device_callback);
253 i++;
254 }
255 return SANE_STATUS_GOOD;
256 }
257
258 /**
259 * Backend exit.
260 * Clean up allocated memory.
261 */
262 void
sane_exit(void)263 sane_exit (void)
264 {
265 Pieusb_Device_Definition *dev, *next;
266
267 DBG (DBG_info_sane, "sane_exit()\n");
268
269 for (dev = pieusb_definition_list_head; dev; dev = next) {
270 next = dev->next;
271 free((void *)dev->sane.name);
272 free((void *)dev->sane.vendor);
273 free((void *)dev->sane.model);
274 free (dev->version);
275 free (dev);
276 }
277 pieusb_definition_list_head = NULL;
278
279 if (devlist) {
280 free (devlist);
281 devlist = NULL;
282 }
283 }
284
285 /**
286 * Create a SANE device list from the device list generated by sane_init().
287 *
288 * @param device_list List of SANE_Device elements
289 * @param local_only If true, disregard network scanners. Not applicable for USB scanners.
290 * @return SANE_STATUS_GOOD, or SANE_STATUS_NO_MEM if the list cannot be allocated
291 */
292 SANE_Status
sane_get_devices(const SANE_Device *** device_list,SANE_Bool __sane_unused__ local_only)293 sane_get_devices (const SANE_Device *** device_list, SANE_Bool __sane_unused__ local_only)
294 {
295 Pieusb_Device_Definition *dev;
296 int i;
297
298 DBG (DBG_info_sane, "sane_get_devices\n");
299
300 /* Create SANE_DEVICE list from device list created in sane_init() */
301 i = 0;
302 for (dev = pieusb_definition_list_head; dev; dev = dev->next) {
303 i++;
304 }
305 if (devlist) {
306 free (devlist);
307 }
308 devlist = malloc ((i + 1) * sizeof (devlist[0]));
309 if (!devlist) {
310 return SANE_STATUS_NO_MEM;
311 }
312 i = 0;
313 for (dev = pieusb_definition_list_head; dev; dev = dev->next) {
314 devlist[i++] = &dev->sane;
315 }
316 devlist[i] = NULL;
317 *device_list = devlist;
318 return SANE_STATUS_GOOD;
319 }
320
321 /**
322 * Open the scanner with the given devicename and return a handle to it, which
323 * is a pointer to a Pieusb_Scanner struct. The handle will be an input to
324 * a couple of other functions of the SANE interface.
325 *
326 * @param devicename Name of the device, corresponds to SANE_Device.name
327 * @param handle handle to scanner (pointer to a Pieusb_Scanner struct)
328 * @return SANE_STATUS_GOOD if the device has been opened
329 */
330 SANE_Status
sane_open(SANE_String_Const devicename,SANE_Handle * handle)331 sane_open (SANE_String_Const devicename, SANE_Handle * handle)
332 {
333 Pieusb_Device_Definition *dev;
334 SANE_Status status;
335 Pieusb_Scanner *scanner, *s;
336
337 DBG (DBG_info_sane, "sane_open(%s)\n", devicename);
338
339 /* Search for devicename */
340 if (devicename[0]) {
341 for (dev = pieusb_definition_list_head; dev; dev = dev->next) {
342 if (strcmp (dev->sane.name, devicename) == 0) {
343 break;
344 }
345 }
346 if (!dev) {
347 /* Is it a valid USB device? */
348 SANE_Word vendor;
349 SANE_Word product;
350 int i = 0;
351
352 status = sanei_usb_get_vendor_product_byname (devicename, &vendor, &product);
353 if (status != SANE_STATUS_GOOD) {
354 DBG (DBG_error, "sane_open: sanei_usb_get_vendor_product_byname failed %s\n", devicename);
355 return status;
356 }
357 /* Get vendor-product-model & verify that is is supported */
358 /* Loop through supported device list */
359 while (pieusb_supported_usb_device_list[i].vendor != 0) {
360 /* Check if vendor and product match */
361 if (pieusb_supported_usb_device_list[i].vendor == vendor
362 && pieusb_supported_usb_device_list[i].product == product) {
363 /* Check if a supported device is present
364 * If so, create a device definition structure for it. */
365 /* Set pieusb_supported_usb_device to current values: these are used in callback */
366 pieusb_supported_usb_device.vendor = vendor;
367 pieusb_supported_usb_device.product = product;
368 pieusb_supported_usb_device.model = pieusb_supported_usb_device_list[i].model;
369 pieusb_supported_usb_device.flags = pieusb_supported_usb_device_list[i].flags;
370 pieusb_supported_usb_device.device_number = -1;
371 sanei_usb_find_devices (vendor, product, sanei_pieusb_find_device_callback);
372 if (pieusb_supported_usb_device.device_number == -1) {
373 /* Did not succeed in opening the USB device, which is an error.
374 * This error is not caught by sanei_usb_find_devices(), so handle
375 * it here. */
376 DBG (DBG_error, "sane_open: sanei_usb_find_devices did not open device %s\n", devicename);
377 return SANE_STATUS_INVAL;
378 }
379 }
380 i++;
381 }
382 /* Now rescan the device list to see if it is present */
383 for (dev = pieusb_definition_list_head; dev; dev = dev->next) {
384 if (strcmp (dev->sane.name, devicename) == 0) {
385 break;
386 }
387 }
388 }
389 } else {
390 /* empty devicename -> use first device */
391 dev = pieusb_definition_list_head;
392 }
393 /* If no device found, return error */
394 if (!dev) {
395 return SANE_STATUS_INVAL;
396 }
397
398 /* Now create a scanner structure to return */
399
400 /* Check if we are not opening the same scanner again. */
401 for (s = first_handle; s; s = s->next) {
402 if (s->device->sane.name == devicename) {
403 *handle = s;
404 return SANE_STATUS_GOOD;
405 }
406 }
407
408 /* Create a new scanner instance */
409 scanner = malloc (sizeof (*scanner));
410 if (!scanner) {
411 return SANE_STATUS_NO_MEM;
412 }
413 memset (scanner, 0, sizeof (*scanner));
414 scanner->device = dev;
415 sanei_usb_open (dev->sane.name, &scanner->device_number);
416 scanner->cancel_request = 0;
417 scanner->shading_data_present = SANE_FALSE;
418 /* Options and buffers */
419 (void)sanei_pieusb_init_options (scanner);
420
421 /* wait for warmup */
422 status = sanei_pieusb_wait_ready (scanner, 0);
423 if (status != SANE_STATUS_GOOD) {
424 sanei_usb_close(scanner->device_number);
425 free (scanner);
426 DBG (DBG_error, "sane_open: scanner not ready\n");
427 return status;
428 }
429
430 /* First time settings */
431 /* ? */
432 /* Insert newly opened handle into list of open handles: */
433 scanner->next = first_handle;
434 first_handle = scanner;
435
436 *handle = scanner;
437 return SANE_STATUS_GOOD;
438 }
439
440 /**
441 * Close the scanner and remove the scanner from the list of active scanners.
442 *
443 * @param handle Scanner handle
444 */
445 void
sane_close(SANE_Handle handle)446 sane_close (SANE_Handle handle)
447 {
448 Pieusb_Scanner *prev, *scanner;
449 SANE_Int k;
450
451 DBG (DBG_info_sane, "sane_close()\n");
452
453 /* Find handle in list of open handles: */
454 prev = 0;
455 for (scanner = first_handle; scanner; scanner = scanner->next) {
456 if (scanner == handle) {
457 break;
458 }
459 prev = scanner;
460 }
461 /* Not a handle we know about. This may happen since all different backend
462 * scanner instances are all cast to SANE_Handle (a void pointer) */
463 if (!scanner) {
464 DBG (DBG_error, "sane_close(): invalid handle %p\n", handle);
465 return;
466 }
467
468 /* Stop scan if still scanning */
469 if (scanner->scanning) {
470 sanei_pieusb_on_cancel(scanner);
471 }
472
473 /* USB scanners may be still open here */
474 if (scanner->device_number >= 0) {
475 sanei_usb_reset (scanner->device_number);
476 sanei_usb_close (scanner->device_number);
477 }
478 /* Remove handle from list */
479 if (prev) {
480 prev->next = scanner->next;
481 } else {
482 first_handle = scanner->next;
483 }
484
485 /* Free scanner related allocated memory and the scanner itself */
486 /*TODO: check if complete */
487 if (scanner->buffer.data) sanei_pieusb_buffer_delete(&scanner->buffer);
488 free (scanner->ccd_mask);
489 for (k=0; k<4; k++) free (scanner->shading_ref[k]);
490 free (scanner->val[OPT_MODE].s);
491 free (scanner->val[OPT_HALFTONE_PATTERN].s);
492 free (scanner);
493 }
494
495 /**
496 * Get option descriptor. Return the option descriptor with the given index
497 *
498 * @param handle Scanner handle
499 * @param option Index of option descriptor to return
500 * @return The option descriptor
501 */
502 const SANE_Option_Descriptor *
sane_get_option_descriptor(SANE_Handle handle,SANE_Int option)503 sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
504 {
505 Pieusb_Scanner *scanner = handle;
506
507 DBG (DBG_info_proc, "sane_get_option_descriptor() option=%d\n", option);
508
509 if ((unsigned) option >= NUM_OPTIONS)
510 {
511 return 0;
512 }
513
514 return scanner->opt + option;
515 }
516
517 /**
518 * Set or inquire the current value of option number 'option' of the device
519 * represented by the given handle.
520 *
521 * @param handle Scanner handle
522 * @param option Index of option to set or get
523 * @param action Determines if the option value is read or set
524 * @param val Pointer to value to set or get
525 * @param info About set result. May be NULL.
526 * @return SANE_STATUS_GOOD, or SANE_STATUS_INVAL if a parameter cannot be set
527 */
528 SANE_Status
sane_control_option(SANE_Handle handle,SANE_Int option,SANE_Action action,void * val,SANE_Int * info)529 sane_control_option (SANE_Handle handle, SANE_Int option, SANE_Action action,
530 void *val, SANE_Int * info)
531 {
532 Pieusb_Scanner *scanner = handle;
533 SANE_Status status;
534 SANE_Word cap;
535 SANE_String_Const name;
536
537 DBG(DBG_info_sane,"sane_control_option()\n");
538 if (info) {
539 *info = 0;
540 }
541
542 /* Don't set or get options while the scanner is busy */
543 if (scanner->scanning) {
544 DBG(DBG_error,"Device busy scanning, no option returned\n");
545 return SANE_STATUS_DEVICE_BUSY;
546 }
547
548 /* Check if option index is between bounds */
549 if ((unsigned) option >= NUM_OPTIONS) {
550 DBG(DBG_error,"Index too large, no option returned\n");
551 return SANE_STATUS_INVAL;
552 }
553
554 /* Check if option is switched on */
555 cap = scanner->opt[option].cap;
556 if (!SANE_OPTION_IS_ACTIVE (cap))
557 {
558 DBG(DBG_error,"Option inactive (%s)\n", scanner->opt[option].name);
559 return SANE_STATUS_INVAL;
560 }
561
562 /* Get name of option */
563 name = scanner->opt[option].name;
564 if (!name)
565 {
566 name = "(no name)";
567 }
568
569 /* */
570 switch (action) {
571 case SANE_ACTION_GET_VALUE:
572
573 DBG (DBG_info_sane, "get %s [#%d]\n", name, option);
574
575 switch (option) {
576
577 /* word options: */
578 case OPT_NUM_OPTS:
579 case OPT_BIT_DEPTH:
580 case OPT_RESOLUTION:
581 case OPT_TL_X:
582 case OPT_TL_Y:
583 case OPT_BR_X:
584 case OPT_BR_Y:
585 case OPT_THRESHOLD:
586 case OPT_SHARPEN:
587 case OPT_SHADING_ANALYSIS:
588 case OPT_FAST_INFRARED:
589 case OPT_ADVANCE_SLIDE:
590 case OPT_CORRECT_SHADING:
591 case OPT_CORRECT_INFRARED:
592 case OPT_CLEAN_IMAGE:
593 case OPT_SMOOTH_IMAGE:
594 case OPT_TRANSFORM_TO_SRGB:
595 case OPT_INVERT_IMAGE:
596 case OPT_PREVIEW:
597 case OPT_SAVE_SHADINGDATA:
598 case OPT_SAVE_CCDMASK:
599 case OPT_LIGHT:
600 case OPT_DOUBLE_TIMES:
601 case OPT_SET_EXPOSURE_R:
602 case OPT_SET_EXPOSURE_G:
603 case OPT_SET_EXPOSURE_B:
604 case OPT_SET_EXPOSURE_I:
605 case OPT_SET_GAIN_R:
606 case OPT_SET_GAIN_G:
607 case OPT_SET_GAIN_B:
608 case OPT_SET_GAIN_I:
609 case OPT_SET_OFFSET_R:
610 case OPT_SET_OFFSET_G:
611 case OPT_SET_OFFSET_B:
612 case OPT_SET_OFFSET_I:
613 *(SANE_Word *) val = scanner->val[option].w;
614 DBG (DBG_info_sane, "get %s [#%d] val=%d\n", name, option,scanner->val[option].w);
615 return SANE_STATUS_GOOD;
616
617 /* word-array options: => for exposure gain offset? */
618 case OPT_CROP_IMAGE:
619 memcpy (val, scanner->val[option].wa, scanner->opt[option].size);
620 return SANE_STATUS_GOOD;
621
622 /* string options */
623 case OPT_MODE:
624 case OPT_CALIBRATION_MODE:
625 case OPT_GAIN_ADJUST:
626 case OPT_HALFTONE_PATTERN:
627 strcpy (val, scanner->val[option].s);
628 DBG (DBG_info_sane, "get %s [#%d] val=%s\n", name, option,scanner->val[option].s);
629 return SANE_STATUS_GOOD;
630 }
631 break;
632
633 case SANE_ACTION_SET_VALUE:
634
635 switch (scanner->opt[option].type) {
636 case SANE_TYPE_INT:
637 DBG (DBG_info_sane, "set %s [#%d] to %d, size=%d\n", name, option, *(SANE_Word *) val, scanner->opt[option].size);
638 break;
639 case SANE_TYPE_FIXED:
640 DBG (DBG_info_sane, "set %s [#%d] to %f\n", name, option, SANE_UNFIX (*(SANE_Word *) val));
641 break;
642 case SANE_TYPE_STRING:
643 DBG (DBG_info_sane, "set %s [#%d] to %s\n", name, option, (char *) val);
644 break;
645 case SANE_TYPE_BOOL:
646 DBG (DBG_info_sane, "set %s [#%d] to %d\n", name, option, *(SANE_Word *) val);
647 break;
648 default:
649 DBG (DBG_info_sane, "set %s [#%d]\n", name, option);
650 }
651 /* Check if option can be set */
652 if (!SANE_OPTION_IS_SETTABLE (cap)) {
653 return SANE_STATUS_INVAL;
654 }
655 /* Check if new value within bounds */
656 status = sanei_constrain_value (scanner->opt + option, val, info);
657 if (status != SANE_STATUS_GOOD) {
658 return status;
659 }
660 /* Set option and handle info return */
661 switch (option)
662 {
663 /* (mostly) side-effect-free word options: */
664 case OPT_BIT_DEPTH:
665 case OPT_RESOLUTION:
666 case OPT_TL_X:
667 case OPT_TL_Y:
668 case OPT_BR_X:
669 case OPT_BR_Y:
670 case OPT_SHARPEN:
671 case OPT_SHADING_ANALYSIS:
672 case OPT_FAST_INFRARED:
673 if (info) {
674 *info |= SANE_INFO_RELOAD_PARAMS;
675 }
676 /* fall through */
677 case OPT_NUM_OPTS:
678 case OPT_PREVIEW:
679 case OPT_ADVANCE_SLIDE:
680 case OPT_CORRECT_SHADING:
681 case OPT_CORRECT_INFRARED:
682 case OPT_CLEAN_IMAGE:
683 case OPT_SMOOTH_IMAGE:
684 case OPT_TRANSFORM_TO_SRGB:
685 case OPT_INVERT_IMAGE:
686 case OPT_SAVE_SHADINGDATA:
687 case OPT_SAVE_CCDMASK:
688 case OPT_THRESHOLD:
689 case OPT_LIGHT:
690 case OPT_DOUBLE_TIMES:
691 case OPT_SET_GAIN_R:
692 case OPT_SET_GAIN_G:
693 case OPT_SET_GAIN_B:
694 case OPT_SET_GAIN_I:
695 case OPT_SET_OFFSET_R:
696 case OPT_SET_OFFSET_G:
697 case OPT_SET_OFFSET_B:
698 case OPT_SET_OFFSET_I:
699 case OPT_SET_EXPOSURE_R:
700 case OPT_SET_EXPOSURE_G:
701 case OPT_SET_EXPOSURE_B:
702 case OPT_SET_EXPOSURE_I:
703 scanner->val[option].w = *(SANE_Word *) val;
704 break;
705
706 /* side-effect-free word-array options: */
707 case OPT_CROP_IMAGE:
708 memcpy (scanner->val[option].wa, val, scanner->opt[option].size);
709 break;
710
711 /* options with side-effects: */
712 case OPT_MODE:
713 {
714 /* Free current setting */
715 if (scanner->val[option].s) {
716 free (scanner->val[option].s);
717 }
718 /* New setting */
719 scanner->val[option].s = (SANE_Char *) strdup (val);
720 /* Info */
721 if (info) {
722 *info |= SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS;
723 }
724 break;
725 }
726
727 case OPT_CALIBRATION_MODE:
728 case OPT_GAIN_ADJUST:
729 case OPT_HALFTONE_PATTERN:
730 {
731 /* Free current setting */
732 if (scanner->val[option].s) {
733 free (scanner->val[option].s);
734 }
735 /* New setting */
736 scanner->val[option].s = (SANE_Char *) strdup (val);
737 break;
738 }
739
740 }
741
742 /* Check the whole set */
743 if (sanei_pieusb_analyse_options(scanner)) {
744 return SANE_STATUS_GOOD;
745 } else {
746 return SANE_STATUS_INVAL;
747 }
748
749 break;
750 default:
751 return SANE_STATUS_INVAL;
752 break;
753 }
754 return SANE_STATUS_INVAL;
755 }
756
757 /**
758 * Obtain the current scan parameters. The returned parameters are guaranteed
759 * to be accurate between the time a scan has been started (sane start() has
760 * been called) and the completion of that request. Outside of that window, the
761 * returned values are best-effort estimates of what the parameters will be when
762 * sane start() gets invoked. - says the SANE standard.
763 *
764 * @param handle Scanner handle
765 * @param params Scan parameters
766 * @return SANE_STATUS_GOOD
767 */
768 SANE_Status
sane_get_parameters(SANE_Handle handle,SANE_Parameters * params)769 sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
770 {
771 Pieusb_Scanner *scanner = handle;
772 const char *mode;
773 double resolution, width, height;
774 SANE_Int colors;
775
776 DBG (DBG_info_sane, "sane_get_parameters\n");
777
778 if (params) {
779
780 if (scanner->scanning) {
781 /* sane_start() initialized a SANE_Parameters struct in the scanner */
782 DBG (DBG_info_sane, "sane_get_parameters from scanner values\n");
783 params->bytes_per_line = scanner->scan_parameters.bytes_per_line;
784 params->depth = scanner->scan_parameters.depth;
785 params->format = scanner->scan_parameters.format;
786 params->last_frame = scanner->scan_parameters.last_frame;
787 params->lines = scanner->scan_parameters.lines;
788 params->pixels_per_line = scanner->scan_parameters.pixels_per_line;
789 } else {
790 /* Calculate appropriate values from option settings */
791 DBG (DBG_info_sane, "sane_get_parameters from option values\n");
792 if (scanner->val[OPT_PREVIEW].b) {
793 resolution = scanner->device->fast_preview_resolution;
794 } else {
795 resolution = SANE_UNFIX(scanner->val[OPT_RESOLUTION].w);
796 }
797 DBG (DBG_info_sane, " resolution %f\n", resolution);
798 width = SANE_UNFIX(scanner->val[OPT_BR_X].w)-SANE_UNFIX(scanner->val[OPT_TL_X].w);
799 height = SANE_UNFIX(scanner->val[OPT_BR_Y].w)-SANE_UNFIX(scanner->val[OPT_TL_Y].w);
800 DBG (DBG_info_sane, " width x height: %f x %f\n", width, height);
801 params->lines = height / MM_PER_INCH * resolution;
802 params->pixels_per_line = width / MM_PER_INCH * resolution;
803 mode = scanner->val[OPT_MODE].s;
804 if (strcmp(mode, SANE_VALUE_SCAN_MODE_LINEART) == 0) {
805 params->format = SANE_FRAME_GRAY;
806 params->depth = 1;
807 colors = 1;
808 } else if(strcmp(mode, SANE_VALUE_SCAN_MODE_HALFTONE) == 0) {
809 params->format = SANE_FRAME_GRAY;
810 params->depth = 1;
811 colors = 1;
812 } else if(strcmp(mode, SANE_VALUE_SCAN_MODE_GRAY) == 0) {
813 params->format = SANE_FRAME_GRAY;
814 params->depth = scanner->val[OPT_BIT_DEPTH].w;
815 colors = 1;
816 } else if(strcmp(mode, SANE_VALUE_SCAN_MODE_RGBI) == 0) {
817 params->format = SANE_FRAME_RGB; /* was: SANE_FRAME_RGBI */
818 params->depth = scanner->val[OPT_BIT_DEPTH].w;
819 colors = 4;
820 } else { /* SANE_VALUE_SCAN_MODE_COLOR */
821 params->format = SANE_FRAME_RGB;
822 params->depth = scanner->val[OPT_BIT_DEPTH].w;
823 colors = 3;
824 }
825 DBG (DBG_info_sane, " colors: %d\n", colors);
826 if (params->depth == 1) {
827 params->bytes_per_line = colors * (params->pixels_per_line + 7)/8;
828 } else if (params->depth <= 8) {
829 params->bytes_per_line = colors * params->pixels_per_line;
830 } else if (params->depth <= 16) {
831 params->bytes_per_line = 2 * colors * params->pixels_per_line;
832 }
833 params->last_frame = SANE_TRUE;
834 }
835
836 DBG(DBG_info_sane,"sane_get_parameters(): SANE parameters\n");
837 DBG(DBG_info_sane," format = %d\n",params->format);
838 DBG(DBG_info_sane," last_frame = %d\n",params->last_frame);
839 DBG(DBG_info_sane," bytes_per_line = %d\n",params->bytes_per_line);
840 DBG(DBG_info_sane," pixels_per_line = %d\n",params->pixels_per_line);
841 DBG(DBG_info_sane," lines = %d\n",params->lines);
842 DBG(DBG_info_sane," depth = %d\n",params->depth);
843
844 } else {
845
846 DBG(DBG_info_sane," no params argument, no values returned\n");
847
848 }
849
850 return SANE_STATUS_GOOD;
851 }
852
853 /**
854 * Initiates acquisition of an image from the scanner.
855 * SCAN Phase 1: initialization and calibration
856 * (SCAN Phase 2: line-by-line scan & read is not implemented)
857 * SCAN Phase 3: get CCD-mask
858 * SCAN phase 4: scan slide and save data in scanner buffer
859
860 * @param handle Scanner handle
861 * @return
862 */
863 SANE_Status
sane_start(SANE_Handle handle)864 sane_start (SANE_Handle handle)
865 {
866 struct Pieusb_Scanner *scanner = handle;
867 struct Pieusb_Command_Status status;
868 SANE_Byte colors;
869 const char *mode;
870 SANE_Bool shading_correction_relevant;
871 SANE_Bool infrared_post_processing_relevant;
872 SANE_Status st;
873 SANE_Int bytes_per_line;
874
875 SANE_Int shading_width;
876 SANE_Int shading_idx;
877
878 struct Pieusb_Exposure_Time exptime = {
879 0x93, /* code 0x93 */
880 3 * 2 * sizeof(SANE_Int), /* number of bytes in rest of structure */
881 { { 0x02, 100 }, { 0x04, 100 }, { 0x08, 100 } }
882 };
883
884 struct Pieusb_Highlight_Shadow shadow = {
885 0x94, /* code 0x94 */
886 3 * 2 * sizeof(SANE_Int), /* number of bytes in rest of structure */
887 { { 0x02, 100 }, { 0x04, 100 }, { 0x08, 100 } }
888 };
889
890 DBG (DBG_info_sane, "sane_start()\n");
891
892 /* ----------------------------------------------------------------------
893 *
894 * Exit if currently scanning
895 *
896 * ---------------------------------------------------------------------- */
897 if (scanner->scanning) {
898 DBG (DBG_error, "sane_start(): scanner is already scanning, exiting\n");
899 return SANE_STATUS_DEVICE_BUSY;
900 }
901
902 /* ----------------------------------------------------------------------
903 *
904 * Exit with pause if not warmed up
905 *
906 * ---------------------------------------------------------------------- */
907
908 sanei_pieusb_cmd_read_state (scanner->device_number, &(scanner->state), &status);
909 if (status.pieusb_status != PIEUSB_STATUS_GOOD) {
910 if (status.pieusb_status == PIEUSB_STATUS_DEVICE_BUSY)
911 return SANE_STATUS_DEVICE_BUSY; /* was: SANE_STATUS_WARMING_UP */
912 DBG (DBG_error, "sane_start(): warmed up check returns status: %s\n", sane_strstatus (sanei_pieusb_convert_status(status.pieusb_status)));
913 return SANE_STATUS_IO_ERROR;
914 }
915 if (scanner->state.warmingUp) {
916 DBG (DBG_error, "sane_start(): warming up, exiting\n");
917 /* Seen SANE_STATUS_WARMING_UP in scanimage => enabled */
918 sleep (10); /* scanimage does not pause, so do it here */
919 return SANE_STATUS_DEVICE_BUSY; /* was: SANE_STATUS_WARMING_UP */
920 }
921
922 /* ----------------------------------------------------------------------
923 * set exposure time
924 * ---------------------------------------------------------------------- */
925
926 sanei_pieusb_cmd_set_exposure_time (scanner->device_number, &exptime, &status);
927 if (status.pieusb_status != PIEUSB_STATUS_GOOD) {
928 DBG (DBG_error, "sane_start(): sanei_pieusb_cmd_set_exposure_time failed: %d\n", status.pieusb_status);
929 return SANE_STATUS_IO_ERROR;
930 }
931
932 /* ----------------------------------------------------------------------
933 * set highlight shadow
934 * ---------------------------------------------------------------------- */
935
936 sanei_pieusb_cmd_set_highlight_shadow (scanner->device_number, &shadow, &status);
937 if (status.pieusb_status != PIEUSB_STATUS_GOOD) {
938 DBG (DBG_error, "sane_start(): sanei_pieusb_cmd_set_highlight_shadow failed: %d\n", status.pieusb_status);
939 return SANE_STATUS_IO_ERROR;
940 }
941
942 /* ----------------------------------------------------------------------
943 * get calibration info
944 * ---------------------------------------------------------------------- */
945
946 sanei_pieusb_cmd_get_shading_parms (scanner->device_number, scanner->device->shading_parameters, &status);
947 if (status.pieusb_status != PIEUSB_STATUS_GOOD) {
948 DBG (DBG_error, "sane_scan: sanei_pieusb_cmd_get_shading_parms failed: %d\n", status.pieusb_status);
949 return SANE_STATUS_INVAL;
950 }
951 shading_width = scanner->device->shading_parameters[0].pixelsPerLine;
952 DBG (DBG_info, "shading_width %d\n", shading_width);
953 for (shading_idx = 0; shading_idx < SHADING_PARAMETERS_INFO_COUNT; shading_idx++) {
954 scanner->shading_ref[shading_idx] =
955 realloc(scanner->shading_ref[shading_idx], 2 * shading_width * sizeof(SANE_Int));
956 if (scanner->shading_ref[shading_idx] == NULL) {
957 return SANE_STATUS_NO_MEM;
958 }
959 }
960 scanner->ccd_mask = realloc (scanner->ccd_mask, shading_width);
961 scanner->ccd_mask_size = shading_width;
962 if (scanner->ccd_mask == NULL) {
963 return SANE_STATUS_NO_MEM;
964 }
965
966 /* ----------------------------------------------------------------------
967 *
968 * Standard run does;
969 * - set exposure time 0x0A/0x13
970 * - set highlight shadow 0x0A/0x14
971 * - read shading parameters 0x0A/0x95/0x08
972 * - set scan frame 0x0A/0x12
973 * "12 00 0a00 80 00 0300 0000 b829 e31a"
974 * => 0:12 1:0 2:10 4:80 5:0 6:3 8:0 10:10680 12:6883
975 * - read gain offset 0xD7
976 * - set gain offset 0xDC
977 * - set mode 0x15
978 * "00 0f 2c01 80 04 04 00 01 0a 00 00 00 80 10 00"
979 * size res pass dpt frm ord bitmap ptn thr
980 * 15 300 RGB 8 inx intel 1=sharpen 0 128
981 * 3=skipshad
982 *
983 * ---------------------------------------------------------------------- */
984
985 /* ----------------------------------------------------------------------
986 *
987 * Show and check options
988 *
989 * ---------------------------------------------------------------------- */
990 sanei_pieusb_print_options (scanner);
991 if (!sanei_pieusb_analyse_options (scanner)) {
992 return SANE_STATUS_IO_ERROR;
993 }
994
995 /* ----------------------------------------------------------------------
996 *
997 * Set scan frame
998 *
999 * ---------------------------------------------------------------------- */
1000 if (sanei_pieusb_set_frame_from_options (scanner) != SANE_STATUS_GOOD) {
1001 return SANE_STATUS_IO_ERROR;
1002 }
1003
1004 /* ----------------------------------------------------------------------
1005 *
1006 * Function 17
1007 *
1008 * ---------------------------------------------------------------------- */
1009
1010 if (scanner->device->flags & FLAG_SLIDE_TRANSPORT) {
1011 sanei_pieusb_cmd_17 (scanner->device_number, 1, &status);
1012 if (status.pieusb_status != PIEUSB_STATUS_GOOD) {
1013 DBG (DBG_error, "sane_start(): sanei_pieusb_cmd_17 failed: %d\n", status.pieusb_status);
1014 return SANE_STATUS_IO_ERROR;
1015 }
1016 st = sanei_pieusb_wait_ready (scanner, 0);
1017 if (st != SANE_STATUS_GOOD) {
1018 DBG (DBG_error, "sane_start(): scanner not ready after sanei_pieusb_cmd_17: %d\n", st);
1019 return st;
1020 }
1021 }
1022 /* ----------------------------------------------------------------------
1023 *
1024 * Get & set initial gains and offsets
1025 *
1026 * There does not seem to be much reason to set exposure/gain/offset
1027 * now, but it does make a large difference in speed, because it
1028 * creates a small BADF-table. This is probably because without SET GAIN
1029 * OFFSET, extraEntries has a random value (it is not initialised).
1030 *
1031 * TODO: test if this may be done just once, in sane_open().
1032 *
1033 * ---------------------------------------------------------------------- */
1034
1035 if (sanei_pieusb_set_gain_offset (scanner, scanner->val[OPT_CALIBRATION_MODE].s) != SANE_STATUS_GOOD) {
1036 return SANE_STATUS_IO_ERROR;
1037 }
1038
1039 st = sanei_pieusb_wait_ready (scanner, 0);
1040 if (st != SANE_STATUS_GOOD) {
1041 DBG (DBG_error, "sane_start: scanner not ready %d\n", st);
1042 return st;
1043 }
1044
1045 /* ----------------------------------------------------------------------
1046 *
1047 * Set mode
1048 *
1049 * ---------------------------------------------------------------------- */
1050 if (sanei_pieusb_set_mode_from_options (scanner) != SANE_STATUS_GOOD) {
1051 return SANE_STATUS_IO_ERROR;
1052 }
1053
1054 /* ----------------------------------------------------------------------
1055 *
1056 * Init slide transport
1057 *
1058 * ---------------------------------------------------------------------- */
1059 if (scanner->device->flags & FLAG_SLIDE_TRANSPORT) {
1060 sanei_pieusb_cmd_slide (scanner->device_number, SLIDE_INIT, &status);
1061 if (status.pieusb_status != PIEUSB_STATUS_GOOD) {
1062 DBG (DBG_error, "sane_start(): sanei_pieusb_cmd_slide failed: %d\n", status.pieusb_status);
1063 return SANE_STATUS_IO_ERROR;
1064 }
1065 st = sanei_pieusb_wait_ready (scanner, 0);
1066 if (st != SANE_STATUS_GOOD) {
1067 DBG (DBG_error, "sane_start: scanner not ready %d\n", st);
1068 return st;
1069 }
1070 }
1071 /* Enter SCAN phase 1 */
1072 DBG (DBG_info_sane, "sane_start(): scan phase 1\n");
1073
1074 /* ----------------------------------------------------------------------
1075 *
1076 * Start scan & wait until device ready
1077 *
1078 * ---------------------------------------------------------------------- */
1079 scanner->scanning = SANE_TRUE;
1080 scanner->cancel_request = SANE_FALSE;
1081 for (;;) {
1082 sanei_pieusb_cmd_start_scan (scanner->device_number, &status);
1083 if (status.pieusb_status != PIEUSB_STATUS_WARMING_UP)
1084 break;
1085 sleep(5);
1086 }
1087 sanei_pieusb_wait_ready (scanner, 0);
1088 if ((status.pieusb_status == PIEUSB_STATUS_MUST_CALIBRATE)
1089 || (scanner->val[OPT_SHADING_ANALYSIS].b != 0)) {
1090
1091 /* Overriding skip calibration */
1092 DBG (DBG_info_sane, "sane_start(): process shading data\n");
1093
1094 /* ------------------------------------------------------------------
1095 *
1096 * Get and set gain and offset
1097 * Get settings from scanner, from preview data, from options,
1098 * or use defaults.
1099 *
1100 * ------------------------------------------------------------------ */
1101 if (sanei_pieusb_set_gain_offset (scanner, scanner->val[OPT_CALIBRATION_MODE].s) != SANE_STATUS_GOOD) {
1102 sanei_pieusb_cmd_stop_scan (scanner->device_number, &status);
1103 scanner->scanning = SANE_FALSE;
1104 return SANE_STATUS_IO_ERROR;
1105 }
1106 /* ------------------------------------------------------------------
1107 *
1108 * Obtain shading data
1109 * Get parameters from scanner->device->shading_parameters[0] although
1110 * it's 45 lines, scanner->ccd_mask_size pixels, 16 bit depth in all cases.
1111 *
1112 * ------------------------------------------------------------------ */
1113 if (sanei_pieusb_get_shading_data (scanner) != SANE_STATUS_GOOD) {
1114 sanei_pieusb_cmd_stop_scan (scanner->device_number, &status);
1115 scanner->scanning = SANE_FALSE;
1116 return SANE_STATUS_IO_ERROR;
1117 }
1118 }
1119
1120 /* Enter SCAN phase 2 */
1121 DBG (DBG_info_sane, "sane_start(): scan phase 2\n");
1122
1123 /* SCAN phase 2 (line-by-line scan) not implemented */
1124
1125 st = sanei_pieusb_wait_ready (scanner, 0);
1126 if (st != SANE_STATUS_GOOD) {
1127 DBG (DBG_error, "sane_start: scanner not ready %d\n", st);
1128 return st;
1129 }
1130
1131 /* Enter SCAN phase 3 */
1132
1133 DBG (DBG_info_sane, "sane_start(): scan phase 3\n");
1134
1135 /* Handle cancel request */
1136 if (scanner->cancel_request) {
1137 return sanei_pieusb_on_cancel (scanner);
1138 }
1139
1140 /* ----------------------------------------------------------------------
1141 *
1142 * Get CCD mask
1143 *
1144 * ---------------------------------------------------------------------- */
1145
1146 if (sanei_pieusb_get_ccd_mask (scanner) != SANE_STATUS_GOOD) {
1147 sanei_pieusb_cmd_stop_scan (scanner->device_number, &status);
1148 scanner->scanning = SANE_FALSE;
1149 return SANE_STATUS_IO_ERROR;
1150 }
1151
1152 /* Enter SCAN phase 4 */
1153
1154 /* ----------------------------------------------------------------------
1155 *
1156 * Read scan parameters & wait until ready for reading
1157 *
1158 * ---------------------------------------------------------------------- */
1159 if (sanei_pieusb_get_parameters (scanner, &bytes_per_line) != SANE_STATUS_GOOD) {
1160 sanei_pieusb_cmd_stop_scan (scanner->device_number, &status);
1161 scanner->scanning = SANE_FALSE;
1162 return SANE_STATUS_IO_ERROR;
1163 }
1164
1165 st = sanei_pieusb_wait_ready (scanner, 0);
1166 if (st != SANE_STATUS_GOOD) {
1167 DBG (DBG_error, "sane_start: scanner not ready %d\n", st);
1168 return st;
1169 }
1170
1171 /* ----------------------------------------------------------------------
1172 *
1173 * Prepare read buffer
1174 * Currently this buffer is always a memory mapped buffer
1175 * Might be faster to use RAM buffers for small images (such as preview)
1176 *
1177 * ---------------------------------------------------------------------- */
1178 colors = 0x00;
1179 switch (scanner->mode.passes) {
1180 case SCAN_FILTER_RED: colors = 0x01; break;
1181 case SCAN_FILTER_GREEN: colors = 0x02; break;
1182 case SCAN_FILTER_BLUE: colors = 0x04; break;
1183 case SCAN_FILTER_INFRARED: colors = 0x08; break;
1184 case SCAN_ONE_PASS_COLOR: colors = 0x07; break;
1185 case SCAN_ONE_PASS_RGBI: colors = 0x0F; break;
1186 }
1187 if (scanner->buffer.data) sanei_pieusb_buffer_delete(&scanner->buffer); /* free resources from previous invocation */
1188 st = sanei_pieusb_buffer_create (&(scanner->buffer), scanner->scan_parameters.pixels_per_line,
1189 scanner->scan_parameters.lines, colors,
1190 scanner->scan_parameters.depth);
1191 if (st != SANE_STATUS_GOOD) {
1192 scanner->scanning = SANE_FALSE;
1193 return st;
1194 }
1195
1196 /* ----------------------------------------------------------------------
1197 *
1198 * Read all image data into the buffer
1199 *
1200 * ---------------------------------------------------------------------- */
1201 if (sanei_pieusb_get_scan_data (scanner, bytes_per_line) != SANE_STATUS_GOOD) {
1202 scanner->scanning = SANE_FALSE;
1203 return SANE_STATUS_IO_ERROR;
1204 }
1205 sleep(2);
1206 st = sanei_pieusb_wait_ready (scanner, 0);
1207 if (st != SANE_STATUS_GOOD) {
1208 DBG (DBG_error, "sane_start(): scanner not ready after sanei_pieusb_get_scan_data: %d\n", st);
1209 scanner->scanning = SANE_FALSE;
1210 return st;
1211 }
1212
1213 /* ----------------------------------------------------------------------
1214 *
1215 * Advance to next slide (except for preview)
1216 *
1217 * ---------------------------------------------------------------------- */
1218
1219 if (scanner->device->flags & FLAG_SLIDE_TRANSPORT) {
1220 if (scanner->val[OPT_ADVANCE_SLIDE].b && !scanner->val[OPT_PREVIEW].b) {
1221 sanei_pieusb_cmd_slide (scanner->device_number, SLIDE_NEXT, &status);
1222 if (status.pieusb_status != PIEUSB_STATUS_GOOD) {
1223 DBG (DBG_error, "sane_start(): sanei_pieusb_cmd_slide failed: %d\n", status.pieusb_status);
1224 }
1225 }
1226 }
1227
1228 /* ----------------------------------------------------------------------
1229 *
1230 * Post processing:
1231 * 1. Correct for shading
1232 * 2. Remove R-component from IR data
1233 * 3. Remove dust
1234 *
1235 * ---------------------------------------------------------------------- */
1236
1237 mode = scanner->val[OPT_MODE].s;
1238 if (strcmp(mode, SANE_VALUE_SCAN_MODE_LINEART) == 0) {
1239 shading_correction_relevant = SANE_FALSE; /* Shading correction irrelavant at bit depth 1 */
1240 infrared_post_processing_relevant = SANE_FALSE; /* No infrared, no postprocessing */
1241 } else if (strcmp(mode, SANE_VALUE_SCAN_MODE_HALFTONE) == 0) {
1242 shading_correction_relevant = SANE_FALSE; /* Shading correction irrelavant at bit depth 1 */
1243 infrared_post_processing_relevant = SANE_FALSE; /* No infrared, no postprocessing */
1244 } else if (strcmp(mode, SANE_VALUE_SCAN_MODE_GRAY) == 0) {
1245 shading_correction_relevant = SANE_TRUE;
1246 infrared_post_processing_relevant = SANE_FALSE; /* No infrared, no postprocessing */
1247 } else if (scanner->val[OPT_PREVIEW].b) {
1248 /* Catch preview here, otherwise next ifs get complicated */
1249 shading_correction_relevant = SANE_TRUE;
1250 infrared_post_processing_relevant = SANE_FALSE;
1251 } else if (strcmp(mode, SANE_VALUE_SCAN_MODE_RGBI) == 0) {
1252 shading_correction_relevant = SANE_TRUE;
1253 infrared_post_processing_relevant = SANE_TRUE;
1254 } else if (strcmp(mode, SANE_VALUE_SCAN_MODE_COLOR) == 0 && scanner->val[OPT_CLEAN_IMAGE].b) {
1255 shading_correction_relevant = SANE_TRUE;
1256 infrared_post_processing_relevant = SANE_TRUE;
1257 } else { /* SANE_VALUE_SCAN_MODE_COLOR */
1258 shading_correction_relevant = SANE_TRUE;
1259 infrared_post_processing_relevant = SANE_TRUE;
1260 }
1261 if (scanner->val[OPT_CORRECT_SHADING].b && shading_correction_relevant) {
1262 if (scanner->shading_data_present) {
1263 sanei_pieusb_correct_shading (scanner, &scanner->buffer);
1264 } else {
1265 DBG(DBG_warning, "sane_start(): unable to correct for shading, no shading data available\n");
1266 }
1267 }
1268 if ((scanner->val[OPT_CORRECT_INFRARED].b || scanner->val[OPT_CLEAN_IMAGE].b) && !scanner->val[OPT_PREVIEW].b && infrared_post_processing_relevant) {
1269 /* Create array of pointers to color planes R, G, B, I */
1270 SANE_Uint *planes[PLANES];
1271 SANE_Int N;
1272 N = scanner->buffer.width * scanner->buffer.height;
1273 planes[0] = scanner->buffer.data;
1274 planes[1] = scanner->buffer.data + N;
1275 planes[2] = scanner->buffer.data + 2 * N;
1276 planes[3] = scanner->buffer.data + 3 * N;
1277 sanei_ir_init ();
1278 sanei_pieusb_post (scanner, planes, scanner->buffer.colors);
1279 }
1280
1281 /* Save preview data. Preview data only used once to set gain and offset. */
1282 if (scanner->val[OPT_PREVIEW].b) {
1283 sanei_pieusb_analyze_preview(scanner);
1284 } else {
1285 scanner->preview_done = SANE_FALSE;
1286 }
1287
1288 /* Modify buffer in case the buffer has infrared, but no infrared should be returned */
1289 if (scanner->buffer.colors == PLANES && (strcmp(mode,SANE_VALUE_SCAN_MODE_COLOR) == 0 && scanner->val[OPT_CLEAN_IMAGE].b)) {
1290 DBG(DBG_info_sane, "sane_start(): modifying buffer to ignore I\n");
1291 /* Base buffer parameters */
1292 scanner->buffer.colors = 3;
1293 /* Derived quantities */
1294 scanner->buffer.image_size_bytes = scanner->buffer.colors * scanner->buffer.height * scanner->buffer.line_size_bytes;
1295 scanner->buffer.color_index_infrared = -1;
1296 scanner->buffer.bytes_unread = scanner->buffer.bytes_unread * 3 / 4;
1297 scanner->buffer.bytes_written = scanner->buffer.bytes_written * 3 / 4;
1298 }
1299
1300 return SANE_STATUS_GOOD;
1301
1302 }
1303
1304 /**
1305 * Read image data from the scanner buffer.
1306 *
1307 * @param handle
1308 * @param buf
1309 * @param max_len
1310 * @param len
1311 * @return
1312 */
1313 SANE_Status
sane_read(SANE_Handle handle,SANE_Byte * buf,SANE_Int max_len,SANE_Int * len)1314 sane_read (SANE_Handle handle, SANE_Byte * buf, SANE_Int max_len, SANE_Int * len)
1315 {
1316
1317 struct Pieusb_Scanner *scanner = handle;
1318 SANE_Int return_size;
1319
1320 DBG(DBG_info_sane, "sane_read(): requested %d bytes\n", max_len);
1321
1322 /* No reading if not scanning */
1323 if (!scanner->scanning) {
1324 *len = 0;
1325 return SANE_STATUS_IO_ERROR; /* SANE standard does not allow a SANE_STATUS_INVAL return */
1326 }
1327
1328 /* Handle cancel request */
1329 if (scanner->cancel_request) {
1330 return sanei_pieusb_on_cancel(scanner);
1331 }
1332 #if 0
1333 /* Return image data, just read from scanner buffer */
1334 DBG(DBG_info_sane, "sane_read():\n");
1335 DBG(DBG_info_sane, " image size %d\n", scanner->buffer.image_size_bytes);
1336 DBG(DBG_info_sane, " unread %d\n", scanner->buffer.bytes_unread);
1337 DBG(DBG_info_sane, " read %d\n", scanner->buffer.bytes_read);
1338 DBG(DBG_info_sane, " max_len %d\n", max_len);
1339 #endif
1340 if (scanner->buffer.bytes_read > scanner->buffer.image_size_bytes) {
1341 /* Test if not reading past buffer boundaries */
1342 DBG(DBG_error, "sane_read(): reading past buffer boundaries (contains %d, read %d)\n", scanner->buffer.image_size_bytes, scanner->buffer.bytes_read);
1343 *len = 0;
1344 sanei_pieusb_on_cancel(scanner);
1345 return SANE_STATUS_EOF;
1346 } else if (scanner->buffer.bytes_read == scanner->buffer.image_size_bytes) {
1347 /* Return EOF since all data of this frame has already been read. */
1348 *len = 0;
1349 scanner->scanning = SANE_FALSE;
1350 return SANE_STATUS_EOF;
1351 } else if (scanner->buffer.bytes_unread >= max_len) {
1352 /* Already enough data to return, do not read */
1353 DBG(DBG_info_sane, "sane_read(): buffer suffices (contains %d, requested %d)\n", scanner->buffer.bytes_unread, max_len);
1354 return_size = max_len;
1355 } else if (scanner->buffer.bytes_read + scanner->buffer.bytes_unread == scanner->buffer.image_size_bytes) {
1356 /* All the remaining data is in the buffer, do not read */
1357 DBG(DBG_info_sane, "sane_read(): buffer suffices (contains %d, requested %d, last batch though)\n", scanner->buffer.bytes_unread, max_len);
1358 return_size = scanner->buffer.bytes_unread;
1359 } else {
1360 /* Should not happen in this implementation - all data read by sane_start() */
1361 DBG(DBG_error, "sane_read(): shouldn't be here...\n");
1362 return SANE_STATUS_IO_ERROR;
1363 }
1364
1365 /* Check */
1366 if (return_size == 0 && scanner->buffer.bytes_read < scanner->buffer.image_size_bytes) {
1367 DBG(DBG_error, "sane_read(): unable to service read request, %d bytes in frame, %d read\n", scanner->buffer.image_size_bytes, scanner->buffer.bytes_read);
1368 }
1369
1370 /* Return the available data: Output return_size bytes from buffer */
1371 sanei_pieusb_buffer_get(&scanner->buffer, buf, max_len, len);
1372 #if 0
1373 DBG(DBG_info_sane, "sane_read(): currently read %.2f lines of %d\n",
1374 (double)scanner->buffer.bytes_written/(scanner->buffer.line_size_bytes*scanner->buffer.colors),
1375 scanner->buffer.height);
1376 DBG(DBG_info_sane, "sane_read(): returning %d bytes (requested %d), returned %d of %d \n",
1377 *len, max_len,scanner->buffer.bytes_read, scanner->buffer.image_size_bytes);
1378 #endif
1379 return SANE_STATUS_GOOD;
1380
1381 }
1382
1383 /**
1384 * Request cancellation of current scanning process.
1385 *
1386 * @param handle Scanner handle
1387 */
1388 void
sane_cancel(SANE_Handle handle)1389 sane_cancel (SANE_Handle handle)
1390 {
1391 struct Pieusb_Scanner *scanner = handle;
1392
1393 DBG (DBG_info_sane, "sane_cancel\n");
1394
1395 if (scanner->scanning) {
1396 scanner->cancel_request = 1;
1397 }
1398 }
1399
1400 /**
1401 * Set the I/O mode of handle h. The I/O mode can be either blocking or
1402 * non-blocking, but for USB devices, only blocking mode is supported.
1403 *
1404 * @param handle Scanner handle
1405 * @param non_blocking
1406 * @return SANE_STATUS_UNSUPPORTED;
1407 */
1408 SANE_Status
sane_set_io_mode(SANE_Handle handle,SANE_Bool non_blocking)1409 sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking)
1410 {
1411 /* Pieusb_Scanner *scanner = handle; */
1412
1413 DBG (DBG_info_sane, "sane_set_io_mode: handle = %p, non_blocking = %s\n", handle, non_blocking == SANE_TRUE ? "true" : "false");
1414
1415 if (non_blocking) {
1416 return SANE_STATUS_UNSUPPORTED;
1417 }
1418
1419 return SANE_STATUS_GOOD;
1420 }
1421
1422 /**
1423 * Obtain a file-descriptor for the scanner that is readable if image data is
1424 * available. The select file-descriptor is returned in *fd.
1425 * The function has not been implemented since USB-device only operate in
1426 * blocking mode.
1427 *
1428 * @param handle Scanner handle
1429 * @param fd File descriptor with imae data
1430 * @return SANE_STATUS_INVAL
1431 */
1432 SANE_Status
sane_get_select_fd(SANE_Handle handle,SANE_Int * fd)1433 sane_get_select_fd (SANE_Handle handle, SANE_Int * fd)
1434 {
1435 DBG(DBG_info_sane,"sane_get_select_fd(): not supported (only for non-blocking IO)\n");
1436 handle = handle;
1437 fd = fd;
1438 return SANE_STATUS_UNSUPPORTED;
1439 }
1440