/* sane - Scanner Access Now Easy.

   Copyright (C) 2004 -2004 Gerard Klaver (gerard at gkall dot hobby dot nl)
   The teco2 and gl646 backend (Frank Zago) are used as a template for 
   this backend

   This file is part of the SANE package.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston,
   MA 02111-1307, USA.

   As a special exception, the authors of SANE give permission for
   additional uses of the libraries contained in this release of SANE.

   The exception is that, if you link a SANE library with other files
   to produce an executable, this does not by itself cause the
   resulting executable to be covered by the GNU General Public
   License.  Your use of that executable is in no way restricted on
   account of linking the SANE library code into it.

   This exception does not, however, invalidate any other reasons why
   the executable file might be covered by the GNU General Public
   License.

   If you submit changes to SANE to the maintainers to be included in
   a subsequent release, you agree by submitting the changes that
   those changes may be distributed with this exception intact.

   If you write modifications of your own for SANE, it is your choice
   whether to permit this exception to apply to your modifications.
   If you do not wish that, delete this exception notice. 
*/

/* 
	$Id: soundvision.h v 0.01 2004/09/30$
	update 28-03-2005
*/

/* Commands supported by the vidcam. */

typedef struct
{
  unsigned char data[16];
  int len;
}
CDB;

/*--------------------------------------------------------------------------*/

static inline int
getbitfield (unsigned char *pageaddr, int mask, int shift)
{
  return ((*pageaddr >> shift) & mask);
}


/*--------------------------------------------------------------------------*/

#include <stdio.h>

#define LIBUSB_TIMEOUT					1000 /* ms */

typedef unsigned char byte;

/*--------------------------------------------------------------------------*/

/* Black magic for color adjustment. Used only for ICM532B */
struct dpi_color_adjust
{
  int resolution_x;		/* x-resolution  */
  int resolution_y;             /* y-resolution  */

  int z3_color_0;		/* 0, 1 or 2 */
  int z3_color_1;		/* idem */
  int z3_color_2;		/* idem */
};

/*--------------------------------------------------------------------------*/

enum Soundvision_Option
{
  /* Must come first */
  OPT_NUM_OPTS = 0,
  
  OPT_MODE_GROUP,
  OPT_MODE,			/* vidcam modes */
  OPT_RESOLUTION,		/* X and Y resolution */
  OPT_BRIGHTNESS,               /* brightness   */
  OPT_CONTRAST,                 /* contrast     */
  OPT_HUE,			/* hue          */
  OPT_SATURATION,               /* saturation   */ 

  OPT_ENHANCEMENT_GROUP,
  OPT_THRESHOLD,		/* Threshold */
  
  OPT_BLACK_LEVEL,              /* black level correction */
  OPT_WHITE_LEVEL_R,		/*white level red correction */
  OPT_WHITE_LEVEL_G,		/*white level green correction */
  OPT_WHITE_LEVEL_B,		/*white level blue correction */

  /* must come last: */
  OPT_NUM_OPTIONS
};

/*--------------------------------------------------------------------------*/

/* 
 * Scanner supported by this backend. 
 */
struct vidcam_hardware
{
  /* USB stuff */
  SANE_Word vendor;
  SANE_Word product;
  SANE_Word class;
  
  /* Readable names */
  const char *vendor_name;		/* brand on the box */
  const char *product_name;		/* name on the box */

  /* Resolutions supported in color mode. */
  const struct dpi_color_adjust *color_adjust;
};


#define COLOR_RAW_STR			SANE_I18N("Color RAW")
#define COLOR_RGB_STR			SANE_I18N("Color RGB")
#define COLOR_RGB_TEXT_STR              SANE_I18N("Color RGB TEXT")
/*--------------------------------------------------------------------------*/

/* Define a vidcam occurence. */
typedef struct Soundvision_Vidcam
{
  struct Soundvision_Vidcam *next;
  SANE_Device sane;

  char *devicename;
  SANE_Int fd;			/* device handle */

  /* USB handling */
  size_t buffer_size;		/* size of the buffer */
  SANE_Byte *buffer;		/* for USB transfer. */
  
  /* Bayer handling */
  size_t output_size;		/* size of the output */
  SANE_Byte *output;		/* for bayer conversion */

  /* USB control messages handling */
  size_t windoww_size;		/* size of window write */
  size_t windowr_size;		/* size of window read  */
  SANE_Byte *windoww;		/* for window write     */
  SANE_Byte *windowr;		/* for window read      */

  /* Scanner infos. */
  const struct vidcam_hardware *hw;	/* default options for that vidcam */

  SANE_Word *resolutions_list;
  SANE_Word *color_sequence_list;

  /* Scanning handling. */
  int scanning;			/* TRUE if a scan is running. */
  int x_resolution;		/* X resolution in DPI */
  int y_resolution;		/* Y resolution in DPI */
  int depth;			/* depth per color */
  unsigned int colour;
  int red;
  int green;
  int blue;

  SANE_Parameters s_params;
  enum
  {
	  SOUNDVISION_COLOR_RAW,
	  SOUNDVISION_COLOR_RGB,
	  SOUNDVISION_COLOR_RGB_TEXT,
	  SOUNDVISION_COLOR
  }
  scan_mode;
  
  size_t bytes_left;		/* number of bytes left to give to the backend */

  size_t real_bytes_left;	/* number of bytes left the vidcam will return. */
  int bytes_pixel;
  SANE_Byte *image;		/* keep the raw image here */
  size_t image_size;		/* allocated size of image */
  size_t image_begin;		/* first significant byte in image */
  size_t image_end;		/* first free byte in image */

  const struct dpi_color_adjust *color_adjust;

  SANE_Parameters params;

  /* Options */
  SANE_Option_Descriptor opt[OPT_NUM_OPTIONS];
  Option_Value val[OPT_NUM_OPTIONS];
  

	unsigned int VideoMode;	/* 0x0100 = VGA, 0x0000 = CIF, 0x0300 = QVGA */
	int SupportedModes;
	int CIF;
	int VGA;
	int QVGA;
	unsigned int brightness;
	unsigned int whiteness;
	unsigned int contrast;
	unsigned int hue;
	unsigned int palette;

	int nullpackets;
        int init_loop_count;
}
Soundvision_Vidcam;

/* Commands for the SOUNDVISION. */
typedef enum
{
	SOUNDVISION_ISO_READ = 0x83
}
Soundvision_Request;

/*--------------------------------------------------------------------------*/

/* Debug levels. 
 * Should be common to all backends. */

#define DBG_error0  0
#define DBG_error   1
#define DBG_sense   2
#define DBG_warning 3
#define DBG_inquiry 4
#define DBG_info    5
#define DBG_info2   6
#define DBG_proc    7
#define DBG_read    8
#define DBG_sane_init   10
#define DBG_sane_proc   11
#define DBG_sane_info   12
#define DBG_sane_option 13

/*--------------------------------------------------------------------------*/

/* 32 bits from an array to an integer (eg ntohl). */
#define B32TOI(buf) \
	((((unsigned char *)buf)[0] << 24) | \
	 (((unsigned char *)buf)[1] << 16) | \
	 (((unsigned char *)buf)[2] <<  8) |  \
	 (((unsigned char *)buf)[3] <<  0))

#define B16TOI(buf) \
	((((unsigned char *)buf)[0] <<  8) | \
	 (((unsigned char *)buf)[1] <<  0))

/*--------------------------------------------------------*/


/* size of usb transfers */
#define SOUNDVISION_PACKETSIZE	4096

/* number of queued bulk transfers to use, may have problems if > 1 */
#define SOUNDVISION_NUMSBUF		1

/* number of frames supported by the v4l part */
#define SOUNDVISION_NUMFRAMES	2

/* scratch buffers for passing data to the decoders: 2 or 4 are good */
#define SOUNDVISION_NUMSCRATCH	2

/* number of nul sized packets to receive before kicking the camera */
#define SOUNDVISION_MAX_NULLPACKETS	200

/* number of decoding errors before kicking the camera */
#define SOUNDVISION_MAX_ERRORS	100

struct soundvision_sbuf {
	unsigned char *data;
};

enum {
	FRAME_UNUSED,		/* Unused (no MCAPTURE) */
	FRAME_READY,		/* Ready to start grabbing */
	FRAME_GRABBING,		/* In the process of being grabbed into */
	FRAME_DONE,		/* Finished grabbing, but not been synced yet */
	FRAME_ERROR		/* Something bad happened while processing */
};

enum {
	BUFFER_UNUSED,
	BUFFER_READY,
	BUFFER_BUSY,
	BUFFER_DONE
};

/* raw camera data <- sbuf (urb transfer buf) */
struct soundvision_scratch {
	unsigned char *data;
	volatile int state;
	int offset;
	int length;
};

/* processed data for display ends up here, after bayer */
struct soundvision_frame {
	unsigned char *data;	/* Frame buffer */
	volatile int grabstate;	/* State of grabbing */
	unsigned char *curline;
	int curlinepix;
	int curpix;
};

static unsigned char red[256] = { 
	0, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 
	18, 18, 18, 18, 18, 18, 18, 25, 30, 35, 38, 42, 
	44, 47, 50, 53, 54, 57, 59, 61, 63, 65, 67, 69, 
	71, 71, 73, 75, 77, 78, 80, 81, 82, 84, 85, 87, 
	88, 89, 90, 91, 93, 94, 95, 97, 98, 98, 99, 101, 
	102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 
	114, 115, 116, 116, 117, 118, 119, 120, 121, 122, 123, 124, 
	125, 125, 126, 127, 128, 129, 129, 130, 131, 132, 133, 134, 
	134, 135, 135, 136, 137, 138, 139, 140, 140, 141, 142, 143, 
	143, 143, 144, 145, 146, 147, 147, 148, 149, 150, 150, 151, 
	152, 152, 152, 153, 154, 154, 155, 156, 157, 157, 158, 159, 
	159, 160, 161, 161, 161, 162, 163, 163, 164, 165, 165, 166, 
	167, 167, 168, 168, 169, 170, 170, 170, 171, 171, 172, 173, 
	173, 174, 174, 175, 176, 176, 177, 178, 178, 179, 179, 179, 
	180, 180, 181, 181, 182, 183, 183, 184, 184, 185, 185, 186, 
	187, 187, 188, 188, 188, 188, 189, 190, 190, 191, 191, 192, 
	192, 193, 193, 194, 195, 195, 196, 196, 197, 197, 197, 197, 
	198, 198, 199, 199, 200, 201, 201, 202, 202, 203, 203, 204, 
	204, 205, 205, 206, 206, 206, 206, 207, 207, 208, 208, 209, 
	209, 210, 210, 211, 211, 212, 212, 213, 213, 214, 214, 215, 
	215, 215, 215, 216, 216, 217, 217, 218, 218, 218, 219, 219, 
	220, 220, 221, 221 
}; 

static unsigned char green[256] = {
	0, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 
	21, 21, 21, 21, 21, 21, 21, 28, 34, 39, 43, 47, 
	50, 53, 56, 59, 61, 64, 66, 68, 71, 73, 75, 77, 
	79, 80, 82, 84, 86, 87, 89, 91, 92, 94, 95, 97, 
	98, 100, 101, 102, 104, 105, 106, 108, 109, 110, 111, 113, 
	114, 115, 116, 117, 118, 120, 121, 122, 123, 124, 125, 126, 
	127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 
	139, 140, 141, 142, 143, 144, 144, 145, 146, 147, 148, 149, 
	150, 151, 151, 152, 153, 154, 155, 156, 156, 157, 158, 159, 
	160, 160, 161, 162, 163, 164, 164, 165, 166, 167, 167, 168, 
	169, 170, 170, 171, 172, 172, 173, 174, 175, 175, 176, 177, 
	177, 178, 179, 179, 180, 181, 182, 182, 183, 184, 184, 185, 
	186, 186, 187, 187, 188, 189, 189, 190, 191, 191, 192, 193, 
	193, 194, 194, 195, 196, 196, 197, 198, 198, 199, 199, 200, 
	201, 201, 202, 202, 203, 204, 204, 205, 205, 206, 206, 207, 
	208, 208, 209, 209, 210, 210, 211, 212, 212, 213, 213, 214, 
	214, 215, 215, 216, 217, 217, 218, 218, 219, 219, 220, 220, 
	221, 221, 222, 222, 223, 224, 224, 225, 225, 226, 226, 227, 
	227, 228, 228, 229, 229, 230, 230, 231, 231, 232, 232, 233, 
	233, 234, 234, 235, 235, 236, 236, 237, 237, 238, 238, 239, 
	239, 240, 240, 241, 241, 242, 242, 243, 243, 243, 244, 244, 
	245, 245, 246, 246 
}; 

static unsigned char blue[256] = {
	0, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 
	23, 23, 23, 23, 23, 23, 23, 30, 37, 42, 47, 51, 
	55, 58, 61, 64, 67, 70, 72, 74, 78, 80, 82, 84, 
	86, 88, 90, 92, 94, 95, 97, 100, 101, 103, 104, 106, 
	107, 110, 111, 112, 114, 115, 116, 118, 119, 121, 122, 124, 
	125, 126, 127, 128, 129, 132, 133, 134, 135, 136, 137, 138, 
	139, 140, 141, 143, 144, 145, 146, 147, 148, 149, 150, 151, 
	152, 154, 155, 156, 157, 158, 158, 159, 160, 161, 162, 163, 
	165, 166, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 
	176, 176, 177, 178, 179, 180, 180, 181, 182, 183, 183, 184, 
	185, 187, 187, 188, 189, 189, 190, 191, 192, 192, 193, 194, 
	194, 195, 196, 196, 198, 199, 200, 200, 201, 202, 202, 203, 
	204, 204, 205, 205, 206, 207, 207, 209, 210, 210, 211, 212, 
	212, 213, 213, 214, 215, 215, 216, 217, 217, 218, 218, 220, 
	221, 221, 222, 222, 223, 224, 224, 225, 225, 226, 226, 227, 
	228, 228, 229, 229, 231, 231, 232, 233, 233, 234, 234, 235, 
	235, 236, 236, 237, 238, 238, 239, 239, 240, 240, 242, 242, 
	243, 243, 244, 244, 245, 246, 246, 247, 247, 248, 248, 249, 
	249, 250, 250, 251, 251, 253, 253, 254, 254, 255, 255, 255, 
	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 
	255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 
	255, 255, 255, 255 
}; 


#include <sys/types.h>

FILE *sanei_config_open (const char *filename);

static SANE_Status snap_pic (void);

static SANE_Int read_dir (SANE_String dir, SANE_Bool read_files);

static void set_res (SANE_Int lowres);

static SANE_Int read_info (SANE_String_Const fname);

static SANE_Status converter_do_scan_complete_cleanup (void);

static SANE_Int converter_fill_buffer (void);

static SANE_Bool converter_scan_complete (void);

static SANE_Status converter_init (SANE_Handle handle);
