scanimage: some prep clean up work.

- reduce scope of some variables
- move full_optstring to global so that scanimage_exit can clean it up
- initialize global variables. Global statics are not initialized.
- some other more minor things
666-epsonds-has-issues-with-saned
Ralph Little 2022-12-04 09:29:02 -08:00
rodzic f9fe6d0ced
commit fc77717507
1 zmienionych plików z 80 dodań i 79 usunięć

Wyświetl plik

@ -132,26 +132,27 @@ static struct option basic_options[] = {
#define BASE_OPTSTRING "d:hi:Lf:o:B::nvVTAbp"
#define STRIP_HEIGHT 256 /* # lines we increment image height */
static struct option *all_options;
static int option_number_len;
static int *option_number;
static SANE_Handle device;
static int verbose;
static struct option *all_options = NULL;
static int option_number_len = 0;
static int *option_number = 0;
static SANE_Handle device = 0;
static int verbose = 0;
static int progress = 0;
static const char* output_file = NULL;
static int test;
static int all;
static int test = 0;
static int all = 0;
static int output_format = OUTPUT_UNKNOWN;
static int help;
static int help = 0;
static int dont_scan = 0;
static const char *prog_name;
static const char *prog_name = NULL;
static int resolution_optind = -1, resolution_value = 0;
/* window (area) related options */
static SANE_Option_Descriptor window_option[4]; /*updated descs for x,y,l,t*/
static int window[4]; /*index into backend options for x,y,l,t*/
static SANE_Word window_val[2]; /*the value for x,y options*/
static int window_val_user[2]; /* is x,y user-specified? */
static SANE_Option_Descriptor window_option[4] = {0}; /*updated descs for x,y,l,t*/
static int window[4] = {0}; /*index into backend options for x,y,l,t*/
static SANE_Word window_val[2] = {0}; /*the value for x,y options*/
static int window_val_user[2] = {0}; /* is x,y user-specified? */
static char *full_optstring = NULL;
static int accept_only_md5_auth = 0;
static const char *icc_profile = NULL;
@ -163,18 +164,16 @@ static SANE_Word tl_x = 0;
static SANE_Word tl_y = 0;
static SANE_Word br_x = 0;
static SANE_Word br_y = 0;
static SANE_Byte *buffer;
static size_t buffer_size;
static SANE_Byte *buffer = NULL;
static size_t buffer_size = 0;
static void
auth_callback (SANE_String_Const resource,
SANE_Char * username, SANE_Char * password)
{
char tmp[3 + 128 + SANE_MAX_USERNAME_LEN + SANE_MAX_PASSWORD_LEN], *wipe;
unsigned char md5digest[16];
char tmp[3 + 128 + SANE_MAX_USERNAME_LEN + SANE_MAX_PASSWORD_LEN];
int md5mode = 0, len, query_user = 1;
FILE *pass_file;
struct stat stat_buf;
char * uname = NULL;
@ -198,6 +197,7 @@ auth_callback (SANE_String_Const resource,
}
else
{
FILE *pass_file;
if ((pass_file = fopen (tmp, "r")) != NULL)
{
@ -287,6 +287,7 @@ auth_callback (SANE_String_Const resource,
if (query_user == 1)
{
#ifdef HAVE_GETPASS
char *wipe;
strcpy (password, (wipe = getpass ("Enter password: ")));
memset (wipe, 0, strlen (password));
#else
@ -296,6 +297,7 @@ auth_callback (SANE_String_Const resource,
if (md5mode)
{
unsigned char md5digest[16];
sprintf (tmp, "%.128s%.*s", (strstr (resource, "$MD5$")) + 5,
SANE_MAX_PASSWORD_LEN - 1, password);
@ -319,10 +321,10 @@ auth_callback (SANE_String_Const resource,
static void
sighandler (int signum)
{
static SANE_Bool first_time = SANE_TRUE;
if (device)
{
static SANE_Bool first_time = SANE_TRUE;
fprintf (stderr, "%s: received signal %d\n", prog_name, signum);
if (first_time)
{
@ -1205,18 +1207,17 @@ write_png_header (SANE_Frame format, int width, int height, int depth, int dpi,
/* There are nominally 39.3700787401575 inches in a meter. */
const double pixels_per_meter = dpi * 39.3700787401575;
size_t icc_size = 0;
void *icc_buffer;
*png_ptr = png_create_write_struct
(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!*png_ptr) {
fprintf(stderr, "png_create_write_struct failed\n");
exit(1);
scanimage_exit (1);
}
*info_ptr = png_create_info_struct(*png_ptr);
if (!*info_ptr) {
fprintf(stderr, "png_create_info_struct failed\n");
exit(1);
scanimage_exit (1);
}
png_init_io(*png_ptr, ofp);
@ -1244,7 +1245,7 @@ write_png_header (SANE_Frame format, int width, int height, int depth, int dpi,
if (icc_profile)
{
icc_buffer = sanei_load_icc_profile(icc_profile, &icc_size);
void *icc_buffer = sanei_load_icc_profile(icc_profile, &icc_size);
if (icc_size > 0)
{
/* libpng will abort if the profile and image colour spaces do not match*/
@ -1620,11 +1621,11 @@ scan_it (FILE *ofp, void* pw)
#ifdef HAVE_LIBPNG
if (output_format == OUTPUT_PNG)
{
int i = 0;
int idx = 0;
int left = len;
while(pngrow + left >= parm.bytes_per_line)
{
memcpy(pngbuf + pngrow, buffer + i, parm.bytes_per_line - pngrow);
memcpy(pngbuf + pngrow, buffer + idx, parm.bytes_per_line - pngrow);
if(parm.depth == 1)
{
int j;
@ -1647,11 +1648,11 @@ scan_it (FILE *ofp, void* pw)
}
#endif
png_write_row(png_ptr, pngbuf);
i += parm.bytes_per_line - pngrow;
idx += parm.bytes_per_line - pngrow;
left -= parm.bytes_per_line - pngrow;
pngrow = 0;
}
memcpy(pngbuf + pngrow, buffer + i, left);
memcpy(pngbuf + pngrow, buffer + idx, left);
pngrow += left;
}
else
@ -1659,28 +1660,28 @@ scan_it (FILE *ofp, void* pw)
#ifdef HAVE_LIBJPEG
if (output_format == OUTPUT_JPEG || output_format == OUTPUT_PDF)
{
int i = 0;
int idx = 0;
int left = len;
while(jpegrow + left >= parm.bytes_per_line)
{
memcpy(jpegbuf + jpegrow, buffer + i, parm.bytes_per_line - jpegrow);
memcpy(jpegbuf + jpegrow, buffer + idx, parm.bytes_per_line - jpegrow);
if(parm.depth == 1)
{
int col1, col8;
JSAMPLE *buf8 = malloc(parm.bytes_per_line * 8);
for(col1 = 0; col1 < parm.bytes_per_line; col1++)
for(col8 = 0; col8 < 8; col8++)
buf8[col1 * 8 + col8] = jpegbuf[col1] & (1 << (8 - col8 - 1)) ? 0 : 0xff;
buf8[col1 * 8 + col8] = (jpegbuf[col1] & (1 << (8 - col8 - 1))) ? 0 : 0xff;
jpeg_write_scanlines(&cinfo, &buf8, 1);
free(buf8);
} else {
jpeg_write_scanlines(&cinfo, &jpegbuf, 1);
}
i += parm.bytes_per_line - jpegrow;
idx += parm.bytes_per_line - jpegrow;
left -= parm.bytes_per_line - jpegrow;
jpegrow = 0;
}
memcpy(jpegbuf + jpegrow, buffer + i, left);
memcpy(jpegbuf + jpegrow, buffer + idx, left);
jpegrow += left;
}
else
@ -1690,7 +1691,7 @@ scan_it (FILE *ofp, void* pw)
else
{
#if !defined(WORDS_BIGENDIAN)
int i, start = 0;
int start = 0;
/* check if we have saved one byte from the last sane_read */
if (hang_over > -1)
@ -1704,12 +1705,12 @@ scan_it (FILE *ofp, void* pw)
}
}
/* now do the byte-swapping */
for (i = start; i < (len - 1); i += 2)
for (int idx = start; idx < (len - 1); idx += 2)
{
unsigned char LSB;
LSB = buffer[i];
buffer[i] = buffer[i + 1];
buffer[i + 1] = LSB;
LSB = buffer[idx];
buffer[idx] = buffer[idx + 1];
buffer[idx + 1] = LSB;
}
/* check if we have an odd number of bytes */
if (((len - start) % 2) != 0)
@ -1778,13 +1779,12 @@ scan_it (FILE *ofp, void* pw)
/* FIXME: other bit depths? */
if (output_format != OUTPUT_TIFF && parm.depth == 16)
{
int i;
for (i = 0; i < image.height * image.width; i += 2)
for (int idx = 0; idx < image.height * image.width; idx += 2)
{
unsigned char LSB;
LSB = image.data[i];
image.data[i] = image.data[i + 1];
image.data[i + 1] = LSB;
LSB = image.data[idx];
image.data[idx] = image.data[idx + 1];
image.data[idx + 1] = LSB;
}
}
#endif
@ -1991,6 +1991,8 @@ scanimage_exit (int status)
fprintf (stderr, "Calling sane_exit\n");
sane_exit ();
if (full_optstring)
free(full_optstring);
if (all_options)
free (all_options);
if (option_number)
@ -2008,15 +2010,12 @@ scanimage_exit (int status)
*/
static void print_options(SANE_Device * device, SANE_Int num_dev_options, SANE_Bool ro)
{
int i, j;
const SANE_Option_Descriptor *opt;
for (i = 1; i < num_dev_options; ++i)
for (int i = 1; i < num_dev_options; ++i)
{
opt = 0;
const SANE_Option_Descriptor *opt = 0;
/* scan area uses modified option struct */
for (j = 0; j < 4; ++j)
for (int j = 0; j < 4; ++j)
if (i == window[j])
opt = window_option + j;
@ -2063,23 +2062,18 @@ static int guess_output_format(const char* output_file)
}
}
// it would be very confusing if user makes a typo in the filename and the output format changes.
// This is most likely not what the user wanted.
fprintf(stderr, "Could not guess output format from the given path and no --format given.\n");
exit(1);
return OUTPUT_UNKNOWN;
}
int
main (int argc, char **argv)
{
int ch, i, index, all_options_len;
int ch, index;
const SANE_Device **device_list;
SANE_Int num_dev_options = 0;
const char *devname = 0;
const char *defdevname = 0;
const char *format = 0;
char readbuf[2];
char *readbuf2;
int batch = 0;
int batch_print = 0;
int batch_prompt = 0;
@ -2087,7 +2081,6 @@ main (int argc, char **argv)
int batch_start_at = 1;
int batch_increment = 1;
SANE_Status status;
char *full_optstring;
SANE_Int version_code;
void *pw = NULL;
FILE *ofp = NULL;
@ -2181,7 +2174,7 @@ main (int argc, char **argv)
output_format = OUTPUT_PNG;
#else
fprintf(stderr, "PNG support not compiled in\n");
exit(1);
scanimage_exit (1);
#endif
}
else if (strcmp (optarg, "jpeg") == 0)
@ -2190,7 +2183,7 @@ main (int argc, char **argv)
output_format = OUTPUT_JPEG;
#else
fprintf(stderr, "JPEG support not compiled in\n");
exit(1);
scanimage_exit (1);
#endif
}
else if (strcmp (optarg, "pdf") == 0)
@ -2199,7 +2192,7 @@ main (int argc, char **argv)
output_format = OUTPUT_PDF;
#else
fprintf(stderr, "PDF support not compiled in\n");
exit(1);
scanimage_exit (1);
#endif
}
else if (strcmp (optarg, "pnm") == 0)
@ -2217,7 +2210,7 @@ main (int argc, char **argv)
fprintf(stderr, ", jpeg");
#endif
fprintf(stderr, ".\n");
exit(1);
scanimage_exit (1);
}
break;
case OPTION_MD5:
@ -2247,14 +2240,14 @@ main (int argc, char **argv)
}
else
{
int i = 0, int_arg = 0;
const char *percent, *start;
int int_arg = 0;
const char *percent;
const char *text_arg = 0;
char ftype;
for (i = 0; device_list[i]; ++i)
for (int dev_num = 0; device_list[dev_num]; ++dev_num)
{
start = optarg;
const char *start = optarg;
while (*start && (percent = strchr (start, '%')))
{
int start_len = percent - start;
@ -2264,23 +2257,23 @@ main (int argc, char **argv)
switch (*percent)
{
case 'd':
text_arg = device_list[i]->name;
text_arg = device_list[dev_num]->name;
ftype = 's';
break;
case 'v':
text_arg = device_list[i]->vendor;
text_arg = device_list[dev_num]->vendor;
ftype = 's';
break;
case 'm':
text_arg = device_list[i]->model;
text_arg = device_list[dev_num]->model;
ftype = 's';
break;
case 't':
text_arg = device_list[i]->type;
text_arg = device_list[dev_num]->type;
ftype = 's';
break;
case 'i':
int_arg = i;
int_arg = dev_num;
ftype = 'i';
break;
case 'n':
@ -2392,12 +2385,20 @@ Parameters are separated by a blank from single-character options (e.g.\n\
if (batch && output_file != NULL)
{
fprintf(stderr, "--batch and --output-file can't be used together.\n");
exit(1);
scanimage_exit (1);
}
if (output_format == OUTPUT_UNKNOWN)
output_format = guess_output_format(output_file);
{
output_format = guess_output_format(output_file);
if (output_format == OUTPUT_UNKNOWN)
{
// it would be very confusing if user makes a typo in the filename and the output format changes.
// This is most likely not what the user wanted.
fprintf(stderr, "Could not guess output format from the given path and no --format given.\n");
scanimage_exit (1);
}
}
if (!devname)
{
/* If no device name was specified explicitly, we look at the
@ -2465,7 +2466,7 @@ Parameters are separated by a blank from single-character options (e.g.\n\
}
/* malloc global option lists */
all_options_len = num_dev_options + NELEMS (basic_options) + 1;
int all_options_len = num_dev_options + NELEMS (basic_options) + 1;
all_options = malloc (all_options_len * sizeof (all_options[0]));
option_number_len = num_dev_options;
option_number = malloc (option_number_len * sizeof (option_number[0]));
@ -2570,6 +2571,7 @@ Parameters are separated by a blank from single-character options (e.g.\n\
}
free (full_optstring);
full_optstring = NULL;
/* convert x/y to br_x/br_y */
for (index = 0; index < 2; ++index)
@ -2615,7 +2617,7 @@ List of available devices:", prog_name);
{
int column = 80;
for (i = 0; device_list[i]; ++i)
for (int i = 0; device_list[i]; ++i)
{
if (column + strlen (device_list[i]->name) + 1 >= 80)
{
@ -2732,13 +2734,12 @@ List of available devices:", prog_name);
if (output_format != OUTPUT_PDF)
#endif
strcat (part_path, ".part");
}
if (batch)
{
if (batch_prompt)
{
char readbuf[2];
char *readbuf2;
fprintf (stderr, "Place document no. %d on the scanner.\n",
n);
fprintf (stderr, "Press <RETURN> to continue.\n");