/**************************************************************************
 *{@C
 *      Copyright:      1988-2024 Paul Obermeier (obermeier@poSoft.de)
 *
 *      Module:         Utilities
 *      Filename:       UT_Image.h
 *
 *      Author:         Paul Obermeier
 *
 *      Description:    Declaration of a vector font defining all printable
 *                      ASCII characters (see UT_Image.c), and some macros
 *                      for using the font.
 *
 *      Additional documentation:
 *                      None.
 *
 **************************************************************************/

#ifndef __UT_IMAGE_H__
#define __UT_IMAGE_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "UT_Compat.h"

extern POIMG_IMPORT_EXPORT void UT_ImgDitherTable (Int16 tbl[], Int32 sz, Int32 maxval);
extern POIMG_IMPORT_EXPORT void UT_ImgGammaTable  (Float32 gamma, Int32 n, Int32 maxval, Int16 g[]);

#define dtelement(tb,sz,u,v) \
        (tb)[((u) % (1<<(sz))) * (1<<(sz)) + (v) % (1<<(sz))]

/* A "noise generator" used as input for the "noise" function. */

#define UT_ImgNoiseBits    12
#define UT_ImgNoiseSize    (1 << UT_ImgNoiseBits)

typedef struct {
    UInt16  hashtable[UT_ImgNoiseSize];
    Float32 randtable[UT_ImgNoiseSize + 3];
} UT_ImgNoiseGen;

extern POIMG_IMPORT_EXPORT void    UT_ImgMakeNoiseGen (Int32 seed, UT_ImgNoiseGen *gen);
extern POIMG_IMPORT_EXPORT Float32 UT_ImgNoise (const UT_ImgNoiseGen *gen, Int32 p,
                            Float32 x, Float32 y, Float32 z);

#define UT_ImgRawTypeFloat "float"
#define UT_ImgRawTypeShort "short"
#define UT_ImgRawTypeByte  "byte"
#define UT_ImgRawTypeLen   20

extern POIMG_IMPORT_EXPORT UT_Bool UT_ImgRawReadHeader (
               FILE *fp, UInt32 *width, UInt32 *height,
               UInt32 *nChans, UT_Bool *isIntel, 
               UT_Bool *isTopDown, char pixelType[UT_ImgRawTypeLen]);

extern POIMG_IMPORT_EXPORT UT_Bool UT_ImgRawWriteHeader (
               FILE *fp, UInt32 width, UInt32 height,
               UInt32 nChans, UT_Bool isIntel, 
               UT_Bool isTopDown, const char *pixelType);
extern POIMG_IMPORT_EXPORT UT_Bool UT_ImgRawWriteUInt8Header (
               FILE *fp, UInt32 width, UInt32 height,
               UInt32 nChans, UT_Bool isTopDown);
extern POIMG_IMPORT_EXPORT UT_Bool UT_ImgRawWriteUInt16Header (
               FILE *fp, UInt32 width, UInt32 height,
               UInt32 nChans, UT_Bool isTopDown);
extern POIMG_IMPORT_EXPORT UT_Bool UT_ImgRawWriteFloat32Header (
               FILE *fp, UInt32 width, UInt32 height,
               UInt32 nChans, UT_Bool isTopDown);

extern POIMG_IMPORT_EXPORT UT_Bool UT_ImgRawWriteUInt8Scan (
               FILE *fp, const UInt8 *pixBuf, UInt32 width);
extern POIMG_IMPORT_EXPORT UT_Bool UT_ImgRawWriteUInt16Scan (
               FILE *fp, const UInt16 *pixBuf, UInt32 width);
extern POIMG_IMPORT_EXPORT UT_Bool UT_ImgRawWriteFloat32Scan (
               FILE *fp, const Float32 *pixBuf, UInt32 width);

extern POIMG_IMPORT_EXPORT UT_Bool UT_ImgRawWriteUInt8Chan (
               const char *fileName, const UInt8 *pixBuf,
               UInt32 width, UInt32 height,
               UT_Bool topDown, UT_Bool withHeader);
extern POIMG_IMPORT_EXPORT UT_Bool UT_ImgRawWriteUInt16Chan (
               const char *fileName, const UInt16 *pixBuf,
               UInt32 width, UInt32 height,
               UT_Bool topDown, UT_Bool withHeader);
extern POIMG_IMPORT_EXPORT UT_Bool UT_ImgRawWriteFloat32Chan (
               const char *fileName, const Float32 *pixBuf,
               UInt32 width, UInt32 height,
               UT_Bool topDown, UT_Bool withHeader);

/* A data type to describe colors according to the
Commission Internationale de L'Eclairage (CIE) */

typedef struct {Float32 x, y;} CIE_hue;

extern POIMG_IMPORT_EXPORT const char *vecf_font[256];

/* Determine the x and y coordinates of a point */

#define vecf_x(p)       ((*(p) & 0x70) >> 4)
#define vecf_y(p)       (*(p) & 0x0f)

/* Distinguish between "moves" and "draws" */

#define vecf_draw(p)    (*(p) & 0x80)

/* Find the string defining the shape of a character */

#define vecf_string(c)  ((UInt8 *)(vecf_font[(UInt8)(c)]))

#ifdef __cplusplus
}
#endif

#endif /* __UT_IMAGE_H__ */
