/****************************************************************************
*
*  xxx.h
*
*  $Date: 18-Apr-2020 08:36 $
*
*  Copyright 1993-2020 re Recognition GmbH 
*  Höhenstrasse 5a, 8280 Kreuzlingen, Switzerland
*  +41 (0)71 6725100
*  info@reRecognition.com * www.reRecognition.com
*
****************************************************************************/

#ifndef INC_XXX

#define INC_XXX "1.0a"
#define XXX_MAJOR 0x10
#define XXX_MINOR 'a'

#define XXX_MKVER(major, minor) (((major)2<8) | (minor))
#define XXX_VERSION XXX_MKVER(XXX_MAJOR, XXX_MINOR)
/* sample usage: #if XXX_VERSION >= XXX_MKVER(0x10,'a') */

#ifdef __cplusplus
extern "C" {
#endif

#if !defined(_MSC_VER) || (_MSC_VER<1400)
  #if !defined(INT_PTR)
    #if defined(_WIN64) || defined(_LINUX64)
      typedef unsigned long long INT_PTR;
    #else
      typedef unsigned long INT_PTR;
    #endif
  #endif
#endif

/****************************************************************************
*
*  definition of XXX_API
*
****************************************************************************/

#ifndef XXX_API
  #if defined(_WIN32) || defined(_WIN64)
    #define XXX_API __stdcall
  #else
    #define XXX_API
  #endif
#endif

extern int licfile_authorised(const char *file_title); // DLL version only; int==BOOL

/****************************************************************************
*
*  functions for break and error control
*
****************************************************************************/

#define XXX_ERROR long

#if !defined RE_ERRORHANDLING
#define RE_ERRORHANDLING

/*  *** with multithreading, the following settings and functions are thread-specific */
/*  *** they have an impact only on the related thread */

/*  set/get configuration of the KADMOS error handling */
extern long XXX_API re_SetErrorConfig(long config);
extern long XXX_API re_GetErrorConfig(void);

/*** macros for error handling configuration ('config' values) */

/*  enable/disable standard program termination by */
/*  Ctrl+Break, ESC, ALT+F4 (consol app. and Windows) or Ctrl+C (consol app. only): */
#define RE_QUITENABLED       0x80000000  /* default */
/*  enable/disable console output of error texts and warnings: */
#define RE_ERRORDISPLAY      0x20000000  /* default */
/* if RE_ERRORDISPLAY is disabled, a private error handler should be installed to retrieve all error messages */
/*  enable/disable program termination after errors: */
#define RE_ERROREXIT         0x10000000
/*  enable/disable treatment of warnings like errors (except displaying them): */
#define RE_ENABLEWARNINGS    0x08000000

/*  to disable any of the former settings RE_XXXXXXXX, call the following: */
/*    re_SetErrorConfig(re_GetErrorConfig()&~RE_XXXXXXXX); */

/*  disable any error handling, exept return on error: */
#define RE_NOERRORHANDLING   0x00000000 

/*  set/get private_data of the KADMOS error text */
extern XXX_ERROR XXX_API re_SetPrivateData(long private_data);
extern long XXX_API re_GetPrivateData(void);

#define RE_ERROR_PROGRAM_LEN  64
#define RE_ERROR_TEXT_LEN    512

typedef struct re_ErrorText_tag {
  XXX_ERROR ident;
  char program[RE_ERROR_PROGRAM_LEN];
  char text[RE_ERROR_TEXT_LEN];
  char systext[RE_ERROR_TEXT_LEN];
  long private_data;
} re_ErrorText;

typedef struct re_wErrorText_tag {
  XXX_ERROR ident;
  wchar_t program[RE_ERROR_PROGRAM_LEN];
  wchar_t text[RE_ERROR_TEXT_LEN];
  wchar_t systext[RE_ERROR_TEXT_LEN];
  long private_data;
} re_wErrorText;

/* error types (values of ident) */
#define RE_SUCCESS           0x00000000
#define RE_ERROR             0x80000000
#define RE_QUIT              0x40000000
#define RE_WARNING           0x20000000
#define RE_INFO              0x10000000

#define RE_PARAMETERERROR    0x88000000
#define RE_MEMORYERROR       0x84000000
#define RE_FILEERROR         0x82000000
#define RE_PROTECTERROR      0x81000000

/*  retrieve error text, call with NULL for error status (ident) only */
extern XXX_ERROR XXX_API re_GetErrorText(re_ErrorText*);
extern XXX_ERROR XXX_API re_wGetErrorText(re_wErrorText*);

/*  set the error text and error status (ident) */
extern void XXX_API re_SetErrorText(const re_ErrorText*);
extern void XXX_API re_wSetErrorText(const re_wErrorText*);

/*  call with NULL to display KADMOS error text */
extern void XXX_API re_DisplayErrorText(const re_ErrorText*);
extern void XXX_API re_wDisplayErrorText(const re_wErrorText*);

/*  clear error status (ident) */
extern void XXX_API re_ClearError(void);

/*  *** with multithreading, the following settings for error handling */
/*  *** have an impact on all threads */

/*  declaration for private error handling */
typedef void (XXX_API *re_ErrorHandler)(const re_ErrorText*);
typedef void (XXX_API *re_wErrorHandler)(const re_wErrorText*);

/*  call to install a private error handler, call with NULL to deinstall */
extern re_ErrorHandler XXX_API re_SetErrorHandler(re_ErrorHandler);
extern re_wErrorHandler XXX_API re_wSetErrorHandler(re_wErrorHandler);

/*  call to retrieve the installed error handler (NULL == KADMOS error handler) */
extern re_ErrorHandler XXX_API re_GetErrorHandler(void);
extern re_wErrorHandler XXX_API re_wGetErrorHandler(void);
#endif /* RE_ERRORHANDLING */


/****************************************************************************
*
*  structure for initialization
*
****************************************************************************/

typedef struct XXX_Init_tag {
  char  version[8];        /* to check correct XXX version */

  /*** used internally by xxx_init(), xxx_do(), and xxx_end() */
  INT_PTR interna1[4];     /* do not change */
  INT_PTR interna2[4];     /* do not change */
} XXX_Init;


#ifndef INC_KADMOS
// same declarations in kadmos.h

/****************************************************************************
*
*  structures and macros for image description
*
****************************************************************************/

typedef struct ReRect_tag {
  long left, top;
  long width, height;
} ReRect; 

typedef struct ReStroke_tag {
  long color;
  long thickness;
  long timestamp;
  void *user_data;
  long x0,y0;
  long x1,y1;
} ReStroke;

typedef struct ReImage_tag {
  void           *data;       /* pointer to image/data of type 'imgtype' */
  unsigned long  imgtype;     /* see IMGTYPE_* (pixel, pointer, stroke, ...) */
  unsigned long  width;       /* for IMGTYPE_PIXELARRAY, -_POINTERARRAY, -_FEATURE only */ 
  unsigned long  height;      /* for IMGTYPE_PIXELARRAY, -_POINTERARRAY, -_FEATURE only */
  unsigned long  strokecount;    /* for IMGTYPE_STROKE only */
  unsigned long  max_strokecount;/* for IMGTYPE_STROKE only */
  ReRect         subimage;    /* for all IMGTYPEs except IMGTYPE_FEATURE */
  unsigned short bitsperpixel;/* size of 'data' items (0(=1), 1, 8, 24) */
  unsigned short color;       /* see COLOR_* (binary, gray, rgb) */
  unsigned short photometric; /* see PHOTOMETRIC_* (min is white, ...) */
  unsigned short alignment;   /* alignment of pixel data, 0, 1, 2, 4 */
  unsigned short orientation; /* see ORIENTATION_* (topleft, ...) */
  unsigned short fillorder;   /* see FILLORDER_* (MSB2LSB, LSB2MSB) */
  unsigned short filler;      /* 8 byte alignment */
  unsigned short resunit;     /* see RESUNIT_* (inch, cm, ...) */
  unsigned long  xresolution;
  unsigned long  yresolution;
} ReImage;

/*** for every following group, the first item is default */

#define IMGTYPE_PIXELARRAY    0
#define IMGTYPE_POINTERARRAY  1
#define IMGTYPE_STROKE        2

#define IMGTYPE_FEATURE       4
#define IMGTYPE_FEATURE_INT   5
#define IMGTYPE_FEATURE_UINT  6
#define IMGTYPE_FEATURE_FLOAT 7

/* *** for IMGTYPE_FEATURE_* the following exceptions are valid: 
*    - the feature vector is given under 'data'
*    - height*width is the dimension of the feature vector
*    - bitsperpixel (size of the feature data items) can be 8, 16, 32, 64
*    - subimage and all other items of ReImage are unused
* after loading a feature rec file (rec_init()), imgtype, width, height,
* and bitsperpixel are set to the related classifiers default values */

#define COLOR_BINARY 0
#define COLOR_GRAY   1
#define COLOR_RGB    2

#ifndef TIFFMACROS_INCLUDED      /* define this before if already included */       
  /*** macros according to tiff.h from Sam Leffler (Silicon Graphics Inc.) */
  #define PHOTOMETRIC_MINISWHITE 0
  #define PHOTOMETRIC_MINISBLACK 1

  /* not for IMGTYPE_STROKE */
  #define ORIENTATION_TOPLEFT 1
  #define ORIENTATION_BOTLEFT 4

  /* for COLOR_BINARY only, not for IMGTYPE_STROKE */
  #define FILLORDER_MSB2LSB 1    /* from most to least significant bit */
  #define FILLORDER_LSB2MSB 2    /* from least to most significant bit */

  #define RESUNIT_NONE       1
  #define RESUNIT_INCH       2
  #define RESUNIT_CENTIMETER 3
#endif /* TIFFMACROS_INCLUDED */

#define RESUNIT_METER 4

typedef void *ReImageHandle;

#endif // defined INC_KADMOS


/****************************************************************************
*
*  parameters for XXX
*
****************************************************************************/

/****************************************************************************
*  As part of XXX_Data all items of the structure XXX_Parm are cleared by a 
*  call to xxx_init() and set to default values. Set new values after xxx_init().
****************************************************************************/

typedef struct XXX_Parm_tag {
  long  general;                 /* see GENERAL_* macros */
  /*** preprocessing */
  long  prep;                    /* see PREP_* macros */

  short deskew_min;              /* minimum line skew to do de-skewing */
                                 /* zero means no de-skewing (default) */
  short filler1;                 /* 8 byte alignment */
  short noise_diameter_percent;  /* noise reduction */
  short noise_diameter_pixel;    /* noise reduction */

  short noise_width_percent;     /* noise reduction */
  short noise_length_percent;    /* noise reduction */
  short noise_width_pixel;       /* noise reduction */
  short noise_length_pixel;      /* noise reduction */

  INT_PTR hookparm;              /* hook - for private use */
              
} XXX_Parm;


/****************************************************************************
*
*  macros for preprocessing control
*
****************************************************************************/

#define GENERAL_XXXCALL             0x00200000 /* set by xxx_init(), xxx_do(),.. */

#ifndef INC_KADMOS
// same declarations in kadmos.h

#define PREP_NOSPOTREMOVAL          0x00000002 /* don't remove spots */
#define PREP_AUTO_NOISEREDUCTION    0x00000004 /* controlled noise reduction */

#define PREP_RGBTOGRAY_COLORGRAY    0x00000000 /* rgb to gray normal (default) */
#define PREP_RGBTOGRAY_COLORMIN     0x00000010 /* rgb to gray remove color */
#define PREP_RGBTOGRAY_COLORMAX     0x00000020 /* rgb to gray enhance color */

#define PREP_GRAYTOBIN_VERY_THIN    0x00000100 /* gray to bin very low level */
#define PREP_GRAYTOBIN_THIN         0x00000200 /* gray to bin low level */
#define PREP_GRAYTOBIN_MEDIUM       0x00000300 /* gray to bin medium level (default) */
#define PREP_GRAYTOBIN_THICK        0x00000400 /* gray to bin high level */
#define PREP_GRAYTOBIN_VERY_THICK   0x00000500 /* gray to bin very high level */

#define PREP_GRAYTOBIN_UNIFORM      0x00000800 /* gray to bin with one threshold only */

#define PREP_BINFILTER_VERY_THIN    0x00001000 /* smoothing very thin pixels */
#define PREP_BINFILTER_THIN         0x00002000 /* smoothing thin pixels */
#define PREP_BINFILTER_MEDIUM       0x00003000 /* smoothing normal pixels */
#define PREP_BINFILTER_THICK        0x00004000 /* smoothing thick pixels */
#define PREP_BINFILTER_VERY_THICK   0x00005000 /* smoothing very thick pixels */
#define PREP_BINFILTER              0x0000f000

#define PREP_REMOVE_BACKGROUND_AUTO 0x00010000  /* remove image background using histogram - (for color images only) */
#define PREP_REMOVE_BACKGROUND      0x00020000  /* remove image background - (for color images only) */

#define PREP_INPLACE                0x80000000 /* overwrite image in preprocessing */

#endif // defined INC_KADMOS


/****************************************************************************
*
*  module XXX
*
****************************************************************************/

#define PLATINUM_WCHAR_SIZE  32  /* size of platinum_wchar in byte */
#define PLATINUM_WCHAR_COUNT 16  /* count of wchars in platinum_wchar */
#define PLATINUM_ALT          8  /* maximum number of recognition alternatives */

#define PLATINUM_VALUE_MAX  140  /* limit for recognized platinum label */
#define PLATINUM_VALUE_OK    80  /* limit for reliable recognized platinum label */

typedef struct XXX_Data_tag {
  XXX_Init init;             /* set before calling rec_init() */

  /*** parameters for xxx_do() - set after calling xxx_init() */
  XXX_Parm parm;             /* defaults will be set by xxx_init() */

  /*** representation of image data */
  ReImage gold_image;        /* signature image from database */
  ReImage probe_image;       /* signature image to be compared */

  /*** recognition control */
  long  busy;                /* !=0: recognition under progress */

  /*** intermediate results of xxx_do() */
  short platinum_wchar[PLATINUM_ALT][PLATINUM_WCHAR_COUNT]; /* platinum recognition results */
  unsigned char platinum_value[PLATINUM_ALT];  /* uncertainty of platinum recognition 0...255 */
  unsigned char comparison_value_e; /* euclidian distance to gold signature. 0 means best fit */
  unsigned char comparison_value_m; /* mahalanobis distance to gold signature. 0 means best fit */

  unsigned char comparison_value_s; /* SVM + Turbo distance to gold signature. 0 means best fit */

  /*** final result of xxx_do() */
  unsigned char comparison_final_value; /* combined distance to gold signature. 0...255 */
} XXX_Data;

extern XXX_ERROR XXX_API xxx_init         (XXX_Data *, const char *file_title);
extern XXX_ERROR XXX_API xxx_winit        (XXX_Data *, const wchar_t *file_title);
extern XXX_ERROR XXX_API xxx_filetitle    (const XXX_Data *, char *file_title); /* char, wchar_t */
extern XXX_ERROR XXX_API xxx_do           (XXX_Data *);
extern XXX_ERROR XXX_API xxx_end          (XXX_Data *);


/****************************************************************************
*
*  API to config preprocessing (Windows only, reconfig.dll is required):
*    xxx_config(), xxx_config_close()
*  APIs to read and write image files (kadmos.dll or reimageio.lib is required):
*    re_openimagefile(), re_wopenimagefile() - open an image file
*    re_readimagefile() - read from an open image file into ReImage data
*    re_writeimagefile() - write a ReImage into an open image file
*    re_endofimagefile() - check end of file
*    re_closeimagefile() - close the image file
*    re_readimage(), re_wreadimage() - read single/first image into ReImage data
*    re_writeimage(), re_wwriteimage() - write one single ReImage into an image file
*  APIs to convert images from and to bitmap data:
*    re_image2bmp() - to convert ReImage data into bitmaps
*    re_bmp2image() - to convert bitmaps into ReImage data
*  APIs to read/write images from/to the clipboard:
*    re_clipboard2image() - get the clipboard content into ReImage data
*    re_image2clipboard() - place a ReImage on the clipboard
*  APIs to convert images from and to HBITMAP data:
*    re_image2hbitmap() - to convert ReImage data into HBITMAP data
*    re_hbitmap2image() - to convert HBITMAP data into ReImage data
*  API to get the image from an image handle:
*    re_imagehandle2image() - extract the content of an image handle  
*  API to free allocated memory from all the image functions above:
*    re_freeimage() - free allocated memory of images
*  APIs to manipulate a ReImage:
*    re_createimage()
*    re_cloneimage()
*    re_subimage()
*    re_rotateimage()
*    re_getpixel()
*    re_setpixel()
*    re_fillimage()
*
****************************************************************************/


/****************************************************************************
*
*  For PNG support, this software (kadmos.dll, reimageio.lib) uses
*  portions of libpng. Copyright 2000-2002, Glenn-Randers
*
*  For JPEG support, this software (kadmos.dll, reimageio.lib) is based in
*  part on the work of the Independent JPEG Group.
*
*  For TIFF support, this software (kadmos.dll, reimageio.lib) is based in part 
*  on work of Sam Leffler, Silicon Graphics Inc., and the University of California.
*    Copyright (c) 1988-1997 Sam Leffler 
*    Copyright (c) 1991-1997 Silicon Graphics, Inc.
*    Copyright (c) 1985, 1986 The Regents of the University of California
*
****************************************************************************/

#if (defined(_WIN32) || defined(_WIN64))
/* dialog to set preprocessing parameters */
extern XXX_ERROR XXX_API xxx_config(XXX_Data *);
extern void XXX_API xxx_config_close(void);
extern XXX_ERROR XXX_API about_xxx(void);
/* the above functions are implemented in a separate DLL, */
/* no error-handling is supported (except the return value) */
#endif

#ifndef INC_KADMOS

/* read/write images from/to files */
typedef void *ReFileHandle;

#if ((defined(_WIN32) || defined(_WIN64)) && !defined(_WIN32_WCE)) || defined(_CONSOLE)
  extern XXX_ERROR XXX_API GetPrivateFileName(char *file_title, 
                                   const char *filter, unsigned int mode, const char *boxtext, 
                                   const char *inifile, const char *section, const char *entry);
  /* filter, mode, boxtext=Title  see Windows function GetOpenFileName() */
  /* inifile, section, entry      see Windows function GetPrivateProfileString() */
  #if !defined(_WIN32) && !defined(_WIN64) && !defined(OF_READ)
    /* values of mode */
    #define OF_READ             0x00000000
    #define OF_CREATE           0x00001000 /* has to be set if the file doesn't exist! */
    #define OF_PROMPT           0x00002000
    #define OF_EXIST            0x00004000
  #endif
#endif

extern ReFileHandle XXX_API re_openimagefile(const char *file_title, const char *mode);  
extern ReFileHandle XXX_API re_wopenimagefile(const wchar_t *file_title, const char *mode);  
       /* mode: "r" for read, "w" for write, "a" for append */

extern ReImageHandle XXX_API re_readimagefile(ReFileHandle, long image_number, ReImage *image);
       /* image_number=0 for first image in file */

extern XXX_ERROR XXX_API re_writeimagefile(ReFileHandle, const ReImage *image);
       /* images are always written at the end of the file */

extern int XXX_API re_endofimagefile(ReFileHandle);
       /* returns 1 in case of eof, 0 else */

extern XXX_ERROR XXX_API re_closeimagefile(ReFileHandle);

/* read one or first image from a file */
extern ReImageHandle XXX_API re_readimage(const char *file_title, ReImage *image);
extern ReImageHandle XXX_API re_wreadimage(const wchar_t *file_title, ReImage *image);

/* write images from ReImage data into image files */
extern XXX_ERROR XXX_API re_writeimage(const ReImage *image, const char *file_title);
extern XXX_ERROR XXX_API re_wwriteimage(const ReImage *image, const wchar_t *file_title);

/* convert images from ReImage data into bitmap data */
  /* on return, bmp_buffer points to a BITMAPINFOHEADER structure */
  /* the bitmap bits are returned at position: */
  /* (char *)bmp_buffer + (BITMAPINFOHEADER*)bmp_buffer->biSize + (BITMAPINFOHEADER*)bmp_buffer->>biClrUsed * sizeof(RGBQUAD) */
extern ReImageHandle XXX_API re_image2bmp(const ReImage *image, void **bmp_buffer);

/* convert images from bitmap data into ReImage data */
  /* bmp_buffer is expected to point to a BITMAPINFOHEADER structure */
  /* for bmp_bits==NULL the bits are expected at position: */
  /* (char *)bmp_buffer + ((BITMAPINFOHEADER*)bmp_buffer)->biSize + ((BITMAPINFOHEADER*)bmp_buffer)->biClrUsed * sizeof(RGBQUAD) */
extern ReImageHandle XXX_API re_bmp2image(const void *bmp_buffer, const void *bmp_bits, ReImage *image);

#if (defined(_WIN32) || defined(_WIN64)) && !defined(_WIN32_WCE)
  /* get clipboard content */
  extern ReImageHandle XXX_API re_clipboard2image(ReImage *image);
  /* place an image on the clipboard */
  extern XXX_ERROR XXX_API re_image2clipboard(const ReImage *image);

  /* convert images from HBITMAP handles to ReImage data */
  extern ReImageHandle XXX_API re_hbitmap2image(HBITMAP hbmp, ReImage *image);
  /* convert images from ReImage data to HBITMAP handles */
  extern XXX_ERROR XXX_API re_image2hbitmap(const ReImage *image, HBITMAP *hbmp);
  /* to free the HBITMAP handle hbmp after usage, call DeleteObject(hbmp) */
#endif
 
/* get the image from an image handle */
extern XXX_ERROR XXX_API re_imagehandle2image(ReImageHandle, ReImage *image);

/* free the memory allocated from above functions */
extern XXX_ERROR XXX_API re_freeimage(ReImageHandle);

/* create/allocate an empty image based on the specification in the parameter 'image' */
extern ReImageHandle XXX_API re_createimage(ReImage *image);

/* duplicate an existing image */
extern ReImageHandle XXX_API re_cloneimage(const ReImage *image, ReImage *cloneimage);

/* generate a subimage of an existing image */
extern ReImageHandle XXX_API re_subimage(const ReImage *image, const ReRect *rect, ReImage *subimage);

/* rotate an image */
extern ReImageHandle XXX_API re_rotateimage(const ReImage *image, ReImage *rotateimage, int angle);

/* set or get the pixel color in pixel images */
extern XXX_ERROR XXX_API re_setpixel(ReImage *image, long x, long y, long color);
extern XXX_ERROR XXX_API re_getpixel(const ReImage *image, long x, long y, long *color);
/* fill a pixel image (or image->subimage, if specified) with color */
extern XXX_ERROR XXX_API re_fillimage(ReImage *image, long color);

#endif // defined INC_KADMOS

#ifdef __cplusplus
}
#endif

#endif /* INC_XXX */