Removed last change in offset/gain calculation, which croaked for some

cases.  Give up early if we can't talk on the USB.
Added option to force scanner recalibration.
merge-requests/1/head
Nathaniel Rutman 2003-04-05 19:44:04 +00:00
rodzic 4d4f8a4c64
commit 2b0ea7786e
4 zmienionych plików z 119 dodań i 23 usunięć

9
ChangeLog-1.0.11 100644
Wyświetl plik

@ -0,0 +1,9 @@
2003-04-05 Nathan Rutman <nthn1@yahoo.com>
* backend/canon630u-common.c: Removed last change in offset/gain
calculation, which croaked for some cases. Give up early if we
can't talk on the USB.
* backend/canon630u.c: Added option to force scanner recalibration.
Older entries can be found in ChangeLog-1.0.10.

Wyświetl plik

@ -493,17 +493,25 @@ static const byte seq003[] =
/* Scanner init, called at calibration and scan time. Returns 1 if this
was the first time the scanner was plugged in, otherwise 0. */
was the first time the scanner was plugged in, 0 afterward, and
-1 on error. */
static int
init (int fd)
{
byte result, rv;
gl640WriteReq (fd, GL640_GPIO_OE, 0x71);
if (gl640WriteReq (fd, GL640_GPIO_OE, 0x71) != SANE_STATUS_GOOD) {
DBG(1, "Initial write request failed.\n");
return -1;
}
/* Gets 0x04 or 0x05 first run, gets 0x64 subsequent runs. */
gl640ReadReq (fd, GL640_GPIO_READ, &rv);
if (gl640ReadReq (fd, GL640_GPIO_READ, &rv) != SANE_STATUS_GOOD) {
DBG(1, "Initial read request failed.\n");
return -1;
}
gl640WriteReq (fd, GL640_GPIO_OE, 0x70);
DBG (2, "init query: %x\n", rv);
if (rv != 0x64)
{
gl640WriteReq (fd, GL640_GPIO_WRITE, 0x00);
@ -526,7 +534,7 @@ init (int fd)
/* parallel port noise filter */
write_byte (fd, 0x70, 0x73);
DBG (2, "init: %x\n", rv);
DBG (2, "init post-reset: %x\n", rv);
/* Returns 1 if this was the first time the scanner was plugged in. */
return (rv != 0x64);
}
@ -1127,6 +1135,14 @@ compute_ogn (char *calfilename)
255 : 1.5 times brighter
511 : 2 times brighter
1023: 3 times brighter */
#if 1
/* Original gain/offset */
gain = 512 * ((max_range[i / (width / 3)] /
(avg[i + width] - avg[i])) - 1);
offset = avg[i];
#else
/* This doesn't work for some people. For instance, a negative
offset would be bad. */
/* Enhanced offset and gain calculation by M.Reinelt <reinelt@eunet.at>
* These expressions were found by an iterative calibration process,
@ -1135,11 +1151,12 @@ compute_ogn (char *calfilename)
* formula.
* Note that offset is linear, but gain isn't!
*/
offset=(double)3.53*avg[i]-125;
gain=(double)3861.0*exp(-0.0168*(avg[i+width]-avg[i]));
offset = (double)3.53 * avg[i] - 125;
gain = (double)3861.0 * exp(-0.0168 * (avg[i + width] - avg[i]));
#endif
DBG (14, "%d wht=%f blk=%f diff=%f gain=%d\n", i,
avg[i + width], avg[i], avg[i + width] - avg[i], gain);
DBG (14, "%d wht=%f blk=%f diff=%f gain=%d offset=%d\n", i,
avg[i + width], avg[i], avg[i + width] - avg[i], gain, offset);
/* 10-bit gain, 6-bit offset (subtractor) in two bytes */
ogn[0] = (byte) (((offset << 2) + (gain >> 8)) & 0xFF);
ogn[1] = (byte) (gain & 0xFF);
@ -1266,7 +1283,8 @@ scan (CANON_Handle * opt)
buf = malloc (0x400);
for (temp = 0; temp < 0x0400; temp++)
/* gamma calculation by M.Reinelt <reinelt@eunet.at> */
buf[temp] = (double) 255.0 * exp(log((temp+0.5)/1023.0)/opt->gamma) + 0.5;
buf[temp] = (double) 255.0 * exp(log((temp + 0.5) / 1023.0) / opt->gamma)
+ 0.5;
/* Gamma R, write and verify */
write_byte (fd, DATAPORT_TARGET, DP_R | DP_GAMMA);
@ -1415,13 +1433,18 @@ scan (CANON_Handle * opt)
static SANE_Status
CANON_set_scan_parameters (CANON_Handle * scan,
const int forceCal,
const int gray,
const int left,
const int top,
const int right,
const int bottom, const int res, const int gain, const double gamma)
const int bottom,
const int res,
const int gain,
const double gamma)
{
DBG (2, "CANON_set_scan_parameters:\n");
DBG (2, "cal = %d\n", forceCal);
DBG (2, "gray = %d (ignored)\n", gray);
DBG (2, "res = %d\n", res);
DBG (2, "gain = %d\n", gain);
@ -1450,6 +1473,7 @@ CANON_set_scan_parameters (CANON_Handle * scan,
if (gamma <= 0.0)
return SANE_STATUS_INVAL;
/* Store params */
scan->resolution = res;
scan->x1 = left;
scan->x2 = right - /* subtract 1 pixel */ 600 / scan->resolution;
@ -1457,7 +1481,7 @@ CANON_set_scan_parameters (CANON_Handle * scan,
scan->y2 = bottom;
scan->gain = gain;
scan->gamma = gamma;
scan->flags = 0;
scan->flags = forceCal ? FLG_FORCE_CAL : 0;
return SANE_STATUS_GOOD;
}
@ -1552,6 +1576,7 @@ CANON_finish_scan (CANON_Handle * scanner)
static SANE_Status
CANON_start_scan (CANON_Handle * scanner)
{
int rv;
SANE_Status status;
DBG (3, "CANON_start_scan called\n");
@ -1561,13 +1586,18 @@ CANON_start_scan (CANON_Handle * scanner)
return SANE_STATUS_IO_ERROR;
/* calibrate if needed */
if (init (scanner->fd)
|| !check_ogn_file () || (scanner->flags & FLG_FORCE_CAL))
{
rv = init (scanner->fd);
if (rv < 0) {
DBG(1, "Can't talk on USB.\n");
return SANE_STATUS_IO_ERROR;
}
if ((rv == 1)
|| !check_ogn_file ()
|| (scanner->flags & FLG_FORCE_CAL)) {
plugin_cal (scanner);
wait_for_return (scanner->fd);
}
}
/* scan */
if ((status = scan (scanner)) != SANE_STATUS_GOOD)
{

Wyświetl plik

@ -96,6 +96,7 @@ static SANE_Parameters parms = {
8 /* Number of bits per sample. */
};
struct _SANE_Option
{
SANE_Option_Descriptor *descriptor;
@ -107,6 +108,8 @@ struct _SANE_Option
typedef struct _SANE_Option SANE_Option;
/*-----------------------------------------------------------------*/
static SANE_Word getNumberOfOptions (void); /* Forward declaration */
/*
@ -141,6 +144,51 @@ optionNumOptionsCallback (SANE_Option * option, SANE_Handle handle,
return SANE_STATUS_GOOD;
}
/*-----------------------------------------------------------------*/
/*
This option lets the user force scanner calibration. Normally, this is
done only once, at first scan after powerup.
*/
static SANE_Word optionCalibrateValue = SANE_FALSE;
static SANE_Option_Descriptor optionCalibrateDescriptor = {
SANE_I18N ("cal"),
SANE_I18N ("Calibrate Scanner"),
SANE_I18N ("Force scanner calibration before scan"),
SANE_TYPE_BOOL,
SANE_UNIT_NONE,
sizeof (SANE_Word),
SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT,
SANE_CONSTRAINT_NONE,
{NULL}
};
static SANE_Status
optionCalibrateCallback (SANE_Option * option, SANE_Handle handle,
SANE_Action action, void *value, SANE_Int * info)
{
handle = handle;
option = option; /* Eliminate warning about unused parameters */
switch (action)
{
case SANE_ACTION_SET_AUTO:
return SANE_STATUS_INVAL;
break;
case SANE_ACTION_SET_VALUE:
*info |= SANE_INFO_RELOAD_PARAMS;
optionCalibrateValue = *(SANE_Bool *) value;
break;
case SANE_ACTION_GET_VALUE:
*(SANE_Word *) value = optionCalibrateValue;
break;
}
return SANE_STATUS_GOOD;
}
/*-----------------------------------------------------------------*/
/*
This option lets the user select the scan resolution. The Canon fb630u
@ -197,6 +245,7 @@ optionResolutionCallback (SANE_Option * option, SANE_Handle handle,
return SANE_STATUS_GOOD;
}
/*-----------------------------------------------------------------*/
#ifdef GRAY
/*
@ -240,6 +289,7 @@ optionGrayscaleCallback (SANE_Option * option, SANE_Handle handle,
}
#endif /* GRAY */
/*-----------------------------------------------------------------*/
/* Analog Gain setting */
static const SANE_Range aGainRange = {
@ -286,7 +336,9 @@ optionAGainCallback (SANE_Option * option, SANE_Handle handle,
return SANE_STATUS_GOOD;
}
/*-----------------------------------------------------------------*/
/* Scanner gamma setting */
static SANE_Fixed optionGammaValue = SANE_FIX (1.6);
static SANE_Option_Descriptor optionGammaDescriptor = {
@ -326,6 +378,7 @@ optionGammaCallback (SANE_Option * option, SANE_Handle handle,
return SANE_STATUS_GOOD;
}
/*-----------------------------------------------------------------*/
/*
Scan range
@ -343,7 +396,7 @@ static const SANE_Range heightRange = {
0 /* quantization */
};
/*-----------------------------------------------------------------*/
/*
This option controls the top-left-x corner of the scan
*/
@ -386,7 +439,7 @@ optionTopLeftXCallback (SANE_Option * option, SANE_Handle handle,
return SANE_STATUS_GOOD;
}
/*-----------------------------------------------------------------*/
/*
This option controls the top-left-y corner of the scan
*/
@ -429,7 +482,7 @@ optionTopLeftYCallback (SANE_Option * option, SANE_Handle handle,
return SANE_STATUS_GOOD;
}
/*-----------------------------------------------------------------*/
/*
This option controls the bot-right-x corner of the scan
Default to 2 inches.
@ -473,7 +526,7 @@ optionBotRightXCallback (SANE_Option * option, SANE_Handle handle,
return SANE_STATUS_GOOD;
}
/*-----------------------------------------------------------------*/
/*
This option controls the bot-right-y corner of the scan
Default to 2 inches
@ -517,7 +570,7 @@ optionBotRightYCallback (SANE_Option * option, SANE_Handle handle,
return SANE_STATUS_GOOD;
}
/*-----------------------------------------------------------------*/
/*
The following array binds the option descriptors to
their respective callback routines
@ -526,6 +579,7 @@ their respective callback routines
static SANE_Option so[] = {
{&optionNumOptionsDescriptor, optionNumOptionsCallback},
{&optionResolutionDescriptor, optionResolutionCallback},
{&optionCalibrateDescriptor, optionCalibrateCallback},
#ifdef GRAY
{&optionGrayscaleDescriptor, optionGrayscaleCallback},
#endif
@ -927,6 +981,7 @@ sane_start (SANE_Handle handle)
DBG (3, "sane_start\n");
res = CANON_set_scan_parameters (&scanner->scan,
optionCalibrateValue,
#ifdef GRAY
optionGrayscaleValue,
#else
@ -940,8 +995,10 @@ sane_start (SANE_Handle handle)
MM_IN_INCH * 600,
SANE_UNFIX (optionBotRightYValue) /
MM_IN_INCH * 600,
optionResolutionValue, optionAGainValue,
optionResolutionValue,
optionAGainValue,
SANE_UNFIX (optionGammaValue));
if (res != SANE_STATUS_GOOD)
return res;

Wyświetl plik

@ -103,7 +103,7 @@ environment variable controls the debug level for this backend. Higher
debug levels increase the verbosity of the output.
Example:
export SANE_DEBUG_CANON630U=10
SANE_DEBUG_CANON630U=12 scanimage > /dev/null
.SH "SEE ALSO"
sane(7), sane\-usb(5), sane\-find\-scanner(1)
.br