/* sane - Scanner Access Now Easy.

   Copyright (C) 2006 -2006 Gerard Klaver (gerard at gkall dot hobby dot nl)
   The teco2 and gl646 backend (Frank Zago) are used as a template for 
   this backend.
   spyder	Yan Ke
   
   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.




*/

/* 
	$Id: colorvision.h,v 1.2 2007/03/19 19:59:53 gerard Exp $ */

/* Commands supported by the color meter. */

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

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. */
struct dpi_color_adjust
{
  int resolution_x;		/* x-resolution  */
  int resolution_y;		/* y-resolution  */

  int z1_color_0;		/* 0, 1 or 2 */
  int z1_color_1;		/* idem */
  int z1_color_2;		/* idem */
};

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

enum Colorvision_Option
{
  /* Must come first */
  OPT_NUM_OPTS = 0,

  OPT_MODE_GROUP,
  OPT_MODE,			/* colormeter modes */
  OPT_RESOLUTION,		/* X and Y resolution */
  OPT_BRIGHTNESS,		/* brightness   */

  OPT_ENHANCEMENT_GROUP,

  /* must come last: */
  OPT_NUM_OPTIONS
};

/* 
 * Colormeter scanner supported by this backend. 
 */
struct colormeter_hardware
{
  /* USB stuff */
  SANE_Word vendor;
  SANE_Word product;
  SANE_Word class;
  enum
  {
    Colorvision_Spyder2express,
    Colorvision_SPXX
  }
  colorvisionref;

  /* 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_API_STR			SANE_I18N("COLOR API")
#define COLOR_XCAM_API_STR            SANE_I18N("COLOR XCAM API")
/*--------------------------------------------------------------------------*/

/* Define a colormeter occurence. */
typedef struct Colorvision_Colormeter
{
  struct Colorvision_Colormeter *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 conversion */

  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 */
  SANE_Byte *image;		/* keep the raw image here */

  /* 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 colormeter_hardware *hw;	/* default options for that colormeter */

  SANE_Word *resolutions_list;
  SANE_Word *color_sequence_list;

  /* Scanning handling. */
  SANE_Bool scanning;		/* TRUE if a scan is running. */
  SANE_Bool deliver_eof;
  int x_resolution;		/* X resolution */
  int y_resolution;		/* Y resolution */
  int depth;			/* depth per color */
  unsigned int colour;
  int red_s;
  int green_s;
  int blue_s;

  SANE_Parameters s_params;
  enum
  {
    COLORVISION_API,
    COLORVISION_XCAM_API,
    COLORVISION_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 colormeter will return. */
  int bytes_pixel;

  const struct dpi_color_adjust *color_adjust;

  SANE_Parameters params;

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

  char picmsg_ps[50];

}
Colorvision_Colormeter;

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

/* 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

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

static SANE_Byte urb_outbuf_colors[0x12] = {
  0x00, 0x00, 0x00,	/* black point      */
  0x63, 0x29, 0x10,	/* red values   xx  */
  0x31, 0x42, 0x08,	/* green values xx  */
  0x00, 0x39, 0x4a,	/* blue values  xx  */
  0x21, 0x21, 0x21,	/* grey values  xx  */
  0xff, 0xff, 0xff	/* white point      */
};
