#include <stdio.h>
#include <scanner.h>

struct scanner_info *scanners[] = { SCANNERS NULL };
struct scanner_info *scanner;

#ifdef HAVE_PNG
#define NAME	"out.png"
#include "png.c"
#else
#define NAME	"out.pgm"
int generate_img(char *name, char *img, int width, int height)
{
	FILE *f;

	f = fopen(name, "w");
	fprintf(f, "P5\n%d %d %d\n", width, height, 255);
	fwrite(img, width, height, f);

	fclose(f);

	return 0;
}
#endif

int main(void)
{
	int ret;
	int x, y;
	char *img;
	int i;

	scanner = scanners[0];
	if (scanner == NULL)
	{
		return 1;
	}

	scanner->timeout = 2000;
	ret = scanner->init();
	printf("scanner %s version:%d dpi:%d initialization %s %d\n",
		scanner->name, scanner->version, scanner->dpi,
		(ret == 0) ? "OK" : "fail", ret);
	if (ret != 0)
	{
		return -1;
	}

	printf("scanning: ");
	fflush(stdout);
	for (i = 0; i < 10; i++)
	{
		img = (char *)scanner->read(&x, &y);
		if (img == NULL)
		{
			printf("-");
			fflush(stdout);
		}
		else
		{
			printf("+\n");
			break;
		}
	}

	if (img)
	{
		printf("image scanned successfully\n");
		generate_img(NAME, img, x, y);
	}
	else
	{
		printf("\nimage scan failed\n");
	}

	scanner->close();

	return 0;
}
