1/* sane - Scanner Access Now Easy.
2
3   pieusb_specific.h
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#ifndef PIEUSB_SPECIFIC_H
44#define	PIEUSB_SPECIFIC_H
45
46#include "../include/sane/sanei_ir.h"
47#include "../include/sane/sanei_backend.h"
48#include "pieusb_scancmd.h"
49#include "pieusb_buffer.h"
50
51/* Settings for scan modes available to SANE */
52/* In addition to those defined in sane.h */
53#define SANE_VALUE_SCAN_MODE_RGBI    "RGBI"
54
55/* Scanner settings for colors to scan */
56#define SCAN_ONE_PASS_RGBI           0x90
57#define SCAN_ONE_PASS_COLOR          0x80
58#define SCAN_FILTER_INFRARED         0x10
59#define SCAN_FILTER_BLUE             0x08
60#define SCAN_FILTER_GREEN            0x04
61#define SCAN_FILTER_RED              0x02
62#define SCAN_FILTER_NEUTRAL          0x01
63
64/* Settings for color depth of scan */
65#define SCAN_COLOR_DEPTH_16          0x20
66#define SCAN_COLOR_DEPTH_12          0x10
67#define SCAN_COLOR_DEPTH_10          0x08
68#define SCAN_COLOR_DEPTH_8           0x04
69#define SCAN_COLOR_DEPTH_4           0x02
70#define SCAN_COLOR_DEPTH_1           0x01
71
72/* Settings for format of the scanned data */
73#define SCAN_COLOR_FORMAT_INDEX      0x04
74#define SCAN_COLOR_FORMAT_LINE       0x02
75#define SCAN_COLOR_FORMAT_PIXEL      0x01
76
77/* Settings for calibration mode */
78#define SCAN_CALIBRATION_DEFAULT     "default values"
79#define SCAN_CALIBRATION_AUTO        "from internal test"
80#define SCAN_CALIBRATION_PREVIEW     "from preview"
81#define SCAN_CALIBRATION_OPTIONS     "from options"
82
83/* Settings for additional gain */
84#define SCAN_GAIN_ADJUST_03 "* 0.3"
85#define SCAN_GAIN_ADJUST_05 "* 0.5"
86#define SCAN_GAIN_ADJUST_08 "* 0.8"
87#define SCAN_GAIN_ADJUST_10 "* 1.0"
88#define SCAN_GAIN_ADJUST_12 "* 1.2"
89#define SCAN_GAIN_ADJUST_16 "* 1.6"
90#define SCAN_GAIN_ADJUST_19 "* 1.9"
91#define SCAN_GAIN_ADJUST_24 "* 2.4"
92#define SCAN_GAIN_ADJUST_30 "* 3.0"
93
94/* Post-processing */
95#define POST_SW_COLORS          (1 << 0)        /* gain, negatives, ..., can be done at any time */
96#define POST_SW_IRED            (1 << 1)        /* remove spectral overlap, needs complete scan */
97#define POST_SW_DIRT            (1 << 2)        /* our digital lavabo, needs complete scan */
98#define POST_SW_GRAIN           (1 << 3)        /* smoothen a bit */
99#define POST_SW_CROP            (1 << 4)        /* trim whole image in sane_start
100                                                   before sane_get_parameters() is answered */
101#define POST_SW_IRED_MASK       (POST_SW_IRED | POST_SW_DIRT)
102#define POST_SW_ACCUM_MASK      (POST_SW_IRED_MASK | POST_SW_GRAIN | POST_SW_CROP)
103
104#define DEFAULT_GAIN                    19   /* 0x13 */
105#define DEFAULT_EXPOSURE                2937 /* 0xb79 minimum value, see Pieusb_Settings */
106#define DEFAULT_OFFSET                  0
107#define DEFAULT_LIGHT                   4
108#define DEFAULT_ADDITIONAL_ENTRIES      1
109#define DEFAULT_DOUBLE_TIMES            0
110
111/* --------------------------------------------------------------------------
112 *
113 * DEVICE DEFINITION STRUCTURES
114 *
115 * --------------------------------------------------------------------------*/
116
117/* Options supported by the scanner */
118
119enum Pieusb_Option
120{
121    OPT_NUM_OPTS = 0,
122    /* ------------------------------------------- */
123    OPT_MODE_GROUP,
124    OPT_MODE,                   /* scan mode */
125    OPT_BIT_DEPTH,              /* number of bits to encode a color */
126    OPT_RESOLUTION,             /* number of pixels per inch */
127    OPT_HALFTONE_PATTERN,       /* halftone pattern to use (see halftone_list) */
128    OPT_THRESHOLD,              /* halftone threshold */
129    OPT_SHARPEN,                /* create a sharper scan at the cost of scan time */
130    OPT_SHADING_ANALYSIS,       /* do shading analysis before the scan */
131    OPT_FAST_INFRARED,          /* scan infrared channel faster but less accurate */
132    OPT_ADVANCE_SLIDE,          /* auto-advance slide after scan */
133    OPT_CALIBRATION_MODE,       /* use auto-calibarion settings for scan */
134    /* ------------------------------------------- */
135    OPT_GEOMETRY_GROUP,
136    OPT_TL_X,			/* top-left x */
137    OPT_TL_Y,			/* top-left y */
138    OPT_BR_X,			/* bottom-right x */
139    OPT_BR_Y,			/* bottom-right y */
140    /* ------------------------------------------- */
141    OPT_ENHANCEMENT_GROUP,
142    OPT_CORRECT_SHADING,        /* correct scanned data for lamp variations (shading) */
143    OPT_CORRECT_INFRARED,       /* correct infrared for red crosstalk */
144    OPT_CLEAN_IMAGE,            /* detect and remove dust and scratch artifacts */
145    OPT_GAIN_ADJUST,            /* adjust gain (a simpler option than setting gain, exposure and offset directly) */
146    OPT_CROP_IMAGE,             /* automatically crop image */
147    OPT_SMOOTH_IMAGE,           /* smoothen image */
148    OPT_TRANSFORM_TO_SRGB,      /* transform to approximate sRGB data */
149    OPT_INVERT_IMAGE,           /* transform negative to positive */
150    /* ------------------------------------------- */
151    OPT_ADVANCED_GROUP,
152    OPT_PREVIEW,                /* scan a preview before the actual scan */
153    OPT_SAVE_SHADINGDATA,       /* output shading data */
154    OPT_SAVE_CCDMASK,           /* output CCD mask */
155    OPT_LIGHT,
156    OPT_DOUBLE_TIMES,
157    OPT_SET_EXPOSURE_R,           /* exposure times for R */
158    OPT_SET_EXPOSURE_G,           /* exposure times for G */
159    OPT_SET_EXPOSURE_B,           /* exposure times for B */
160    OPT_SET_EXPOSURE_I,           /* exposure times for I */
161    OPT_SET_GAIN_R,               /* gain for R */
162    OPT_SET_GAIN_G,               /* gain for G */
163    OPT_SET_GAIN_B,               /* gain for B */
164    OPT_SET_GAIN_I,               /* gain for I */
165    OPT_SET_OFFSET_R,             /* offset for R */
166    OPT_SET_OFFSET_G,             /* offset for G */
167    OPT_SET_OFFSET_B,             /* offset for B */
168    OPT_SET_OFFSET_I,             /* offset for I */
169    /* must come last: */
170    NUM_OPTIONS
171};
172
173/* Forward declaration (see pieusb_scancmd.h) */
174struct Pieusb_Shading_Parameters;
175
176/* Device characteristics of a Pieusb USB scanner */
177struct Pieusb_Device_Definition
178{
179    struct Pieusb_Device_Definition *next;
180
181    SANE_Device sane;
182      /* name = string like "libusb:001:006" == NO! this should be "CrystalScan 7200" or "ProScan 7200"...
183       * vendor = "PIE/Pieusb"
184       * model = "CrystalScan 7200" or "ProScan 7200"
185       * type = "film scanner" */
186    /* char *devicename; => sane->name */
187    /* char *vendor; => sane->vendor */
188    /* char *product; => sane->model */
189    SANE_Word vendorId;
190    SANE_Word productId;
191      /* USB id's like 0x05e3 0x0145, see pieusb.conf */
192    SANE_String version; /* INQUIRY productRevision */
193    SANE_Byte model; /* INQUIRY model */
194    SANE_Byte flags; /* pieusb.conf flags */
195
196    /* Ranges for various quantities */
197    SANE_Range dpi_range;
198    SANE_Range x_range;
199    SANE_Range y_range;
200    SANE_Range exposure_range; /* Unit is a 8051 machine cycle, which is approximately 1 us. (Exactly: 12 cycles at 11.059 Mhz = 1.085 us.) */
201    SANE_Range dust_range;
202
203    SANE_Range shadow_range;
204    SANE_Range highlight_range;
205
206    /* Enumerated ranges vor various quantities */
207    SANE_String scan_mode_list[7]; /* names of scan modes (see saneopts.h) */
208    SANE_String calibration_mode_list[6]; /* names of calibration modes */
209    SANE_String gain_adjust_list[10]; /* gain adjustment values */
210    SANE_Word bpp_list[5];	   /* bit depths  */
211    SANE_String halftone_list[17]; /* names of the halftone patterns from the scanner */
212    SANE_String speed_list[9];	   /* names of available speeds */
213    SANE_String ir_sw_list[4];
214    SANE_String crop_sw_list[4];
215    SANE_Word grain_sw_list[6];
216
217    /* Maximum resolution values */
218    int maximum_resolution_x;	   /* maximum x-resolution */
219    int maximum_resolution_y;	   /* maximum y-resolution */
220    int maximum_resolution;
221
222    /* Geometry */
223    double scan_bed_width;	   /* flatbed width in inches (horizontal) */
224    double scan_bed_height;	   /* flatbed height in inches (vertical) */
225    int slide_top_left_x;          /* top-left location of slide w.r.t. scan bed */
226    int slide_top_left_y;          /* top-left location of slide w.r.t. scan bed */
227    double slide_width;	           /* transparency width in inches */
228    double slide_height;           /* transparency length in inches */
229
230    /* Integer and bit-encoded properties */
231    int halftone_patterns;	   /* number of halftones supported */
232    int color_filters;	           /* available colour filters: Infrared-0-0-OnePassColor-B-G-R-N */
233    int color_depths;	           /* available colour depths: 0-0-16-12-10-8-4-1 */
234    int color_formats;	           /* colour data format: 0-0-0-0-0-Index-Line-Pixel */
235    int image_formats;	           /* image data format: 0-0-0-0-OKLine-BlkOne-Motorola-Intel */
236    int scan_capabilities;         /* additional scanner features, number of speeds: PowerSave-ExtCal-0-FastPreview-DisableCal-[CalSpeeds=3] */
237    int optional_devices;          /* optional devices: MultiPageLoad-?-?-0-0-TransModule1-TransModule-AutoDocFeeder */
238    int enhancements;	           /* enhancements: unknown coding */
239    int gamma_bits;	           /* no of bits used for gamma table */
240    int fast_preview_resolution;   /* fast preview resolution */
241    int minimum_highlight;	   /* min highlight % that can be used */
242    int maximum_shadow;  	   /* max shadow % that can be used */
243    int calibration_equation;      /* which calibration equation to use */
244    int minimum_exposure;	   /* min exposure */
245    int maximum_exposure;	   /* max exposure */
246
247    struct Pieusb_Shading_Parameters_Info shading_parameters[4]; /* array with shading data parameters */
248
249    int x0, y0, x1, y1;
250    SANE_String production;
251    SANE_String timestamp;
252    SANE_String signature;
253};
254
255typedef struct Pieusb_Device_Definition Pieusb_Device_Definition;
256
257/* --------------------------------------------------------------------------
258 *
259 * CURRENTLY ACTIVE DEVICES
260 *
261 * --------------------------------------------------------------------------*/
262
263/* This structure holds information about an instance of an active scanner */
264
265struct Pieusb_Scanner
266{
267    struct Pieusb_Scanner *next;
268    struct Pieusb_Device_Definition *device; /* pointer to device definition */
269
270    int device_number; /* scanner device number (as determined by USB) */
271
272    /* SANE option descriptions and settings for this scanner instance */
273    SANE_Option_Descriptor opt[NUM_OPTIONS];
274    Option_Value val[NUM_OPTIONS];
275
276    /* Scan state */
277    struct Pieusb_Scanner_State state;
278    SANE_Int scanning; /* true if busy scanning */
279    SANE_Int cancel_request; /* if true, scanner should terminate a scan */
280
281    /* Scan settings */
282    struct Pieusb_Mode mode;
283    struct Pieusb_Settings settings;
284    struct Pieusb_Scan_Frame frame;
285    SANE_Parameters scan_parameters;
286
287    /* Shading data and CCD-mask */
288#define PIEUSB_CCD_MASK_SIZE 0x1a1d  /* pieusb 5340; */ /* cyberview: 6685 0x1a1d */
289    SANE_Byte *ccd_mask; /* malloc'ed in sane_open */
290    SANE_Int ccd_mask_size;
291    SANE_Bool shading_data_present; /* don't correct shading if not present */
292    SANE_Int shading_mean[SHADING_PARAMETERS_INFO_COUNT]; /* mean shading value for each color (average all 45 lines)  */
293    SANE_Int shading_max[SHADING_PARAMETERS_INFO_COUNT]; /* maximum shading value for each color (for all 45 lines)  */
294    SANE_Int* shading_ref[SHADING_PARAMETERS_INFO_COUNT]; /* 4 arrays of shading references for each pixel on a line and for each color */
295
296    /* Calibration using preview */
297
298    SANE_Bool preview_done;
299    SANE_Int preview_exposure[4];    /* exposure values used in preview */
300    SANE_Int preview_gain[4];        /* gain values used in preview */
301    SANE_Int preview_offset[4];      /* offset values used in preview */
302    SANE_Int preview_lower_bound[4]; /* lowest RGBI values in preview */
303    SANE_Int preview_upper_bound[4]; /* highest RGBI values in preview */
304
305    /* Post processing options */
306/*
307    SANE_Int processing;
308*/
309    double *ln_lut; /* logarithmic lookup table */
310
311    /* Reading buffer */
312    struct Pieusb_Read_Buffer buffer;
313};
314
315typedef struct Pieusb_Scanner Pieusb_Scanner;
316
317SANE_Status sanei_pieusb_parse_config_line(const char* config_line,
318                                           SANE_Word* vendor_id,
319                                           SANE_Word* product_id,
320                                           SANE_Int* model_number,
321                                           SANE_Int* flags);
322/* sub to sane_start() */
323SANE_Status sanei_pieusb_post (Pieusb_Scanner *scanner,  uint16_t **in_img, int planes);
324void sanei_pieusb_correct_shading(struct Pieusb_Scanner *scanner, struct Pieusb_Read_Buffer *buffer);
325SANE_Status sanei_pieusb_get_scan_data(Pieusb_Scanner * scanner, SANE_Int parameter_bytes);
326SANE_Status sanei_pieusb_get_parameters(Pieusb_Scanner * scanner, SANE_Int *parameter_bytes);
327SANE_Status sanei_pieusb_get_ccd_mask(Pieusb_Scanner * scanner);
328SANE_Status sanei_pieusb_get_shading_data(Pieusb_Scanner * scanner);
329SANE_Status sanei_pieusb_set_mode_from_options(Pieusb_Scanner * scanner);
330SANE_Status sanei_pieusb_set_gain_offset(Pieusb_Scanner * scanner, const char* calibration_mode);
331SANE_Status sanei_pieusb_set_frame_from_options(Pieusb_Scanner * scanner);
332void sanei_pieusb_print_options(struct Pieusb_Scanner *scanner);
333/* sub to sane_control_option() and sane_start() */
334int sanei_pieusb_analyse_options(struct Pieusb_Scanner *scanner);
335SANE_Bool sanei_pieusb_supported_device_list_contains(SANE_Word vendor_id, SANE_Word product_id, SANE_Int model_number, SANE_Int flags);
336SANE_Status sanei_pieusb_supported_device_list_add(SANE_Word vendor_id, SANE_Word product_id, SANE_Int model_number, SANE_Int flags);
337/* sub to sane_init() and sane_open() */
338SANE_Status sanei_pieusb_find_device_callback (const char *devicename);
339/* sub to sane_open() */
340SANE_Status sanei_pieusb_init_options (Pieusb_Scanner * scanner);
341/* sub to sane_start(), sane_read() and sane_close() */
342SANE_Status sanei_pieusb_on_cancel (Pieusb_Scanner * scanner);
343
344SANE_Status sanei_pieusb_wait_ready(Pieusb_Scanner *scanner, SANE_Int device_number);
345SANE_Status sanei_pieusb_analyze_preview(Pieusb_Scanner * scanner);
346
347
348
349#endif	/* PIEUSB_SPECIFIC_H */
350