/*
 * slb/tiff.h - Utility functions for the application,
 * when using the shared library.
 *
 * Copyright (C) 2019 Thorsten Otto
 */
#ifndef __SLB_TIFF_H__
#define __SLB_TIFF_H__ 1

#ifndef TIFF_SLB
#define TIFF_SLB 1
#endif

#include <tiffio.h>

#ifndef __CDECL
#define __CDECL
#endif

#include <mint/slb.h>

#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

#define TIFF_SHAREDLIB_NAME "tiff.slb"


/*
 * For Pure-C, we need do redirect some functions
 * that use double arguments and/or return values
 */
#if !defined(__GNUC__)

#if (defined(__68881__) || defined(_M68881) || defined(__M68881__) || defined(__MATH_68881__))

#define TIFFXYZToRGB(cielab, X, Y, Z, r, g, b) TIFF_881_XYZToRGB(cielab, X, Y, Z, r, g, b)
#define TIFFSetField TIFF_881_SetField
#define TIFFVSetField TIFF_881_VSetField
#define TIFFGetField TIFF_881_GetField
#define TIFFVGetField TIFF_881_VGetField
#define TIFFGetFieldDefaulted TIFF_881_GetFieldDefaulted
#define TIFFVGetFieldDefaulted TIFF_881_VGetFieldDefaulted
#define _TIFFClampDoubleToUInt32(d) _TIFF_881_ClampDoubleToUInt32(d)

#define slb_tiff_open slb_tiff_open881

#else

#define TIFFXYZToRGB(cielab, X, Y, Z, r, g, b) TIFF_pc_XYZToRGB(cielab, X, Y, Z, r, g, b)
#define TIFFSetField TIFF_pc_SetField
#define TIFFVSetField TIFF_pc_VSetField
#define TIFFGetField TIFF_pc_GetField
#define TIFFVGetField TIFF_pc_VGetField
#define TIFFGetFieldDefaulted TIFF_pc_GetFieldDefaulted
#define TIFFVGetFieldDefaulted TIFF_pc_VGetFieldDefaulted
#define _TIFFClampDoubleToUInt32(d) _TIFF_pc_ClampDoubleToUInt32(d)

#endif

void TIFF_API TIFFXYZToRGB(TIFFCIELabToRGB *cielab, float X, float Y, float Z, uint32_t *r, uint32_t *g, uint32_t *b);
tiff_int_t TIFF_API TIFFSetField(TIFF*, ttag_t, ...);
tiff_int_t TIFF_API TIFFVSetField(TIFF*, ttag_t, va_list);
tiff_int_t TIFF_API TIFFGetField(TIFF* tif, ttag_t tag, ...);
tiff_int_t TIFF_API TIFFVGetField(TIFF* tif, ttag_t tag, va_list ap);
tiff_int_t TIFF_API TIFFGetFieldDefaulted(TIFF* tif, ttag_t tag, ...);
tiff_int_t TIFF_API TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap);
uint32_t TIFF_API _TIFFClampDoubleToUInt32(double val);

#define LogL16toY unsupported_funcion
#define LogL10toY unsupported_funcion
#define LogL16fromY unsupported_funcion
#define LogL10fromY unsupported_funcion
#define uv_encode unsupported_funcion
#define uv_decode unsupported_funcion

#endif


long slb_tiff_open(const char *slbpath);
void slb_tiff_close(void);
SLB *slb_tiff_get(void);

/* internal helper function */
long __CDECL tiff_slb_control(long fn, void *arg);

/* Get the full path of the shared library */
#define tiff_slb_libpath() ((const char *)tiff_slb_control(4, 0))


#ifdef __cplusplus
}
#endif

#endif /* __SLB_TIFF_H__ */
