#ifndef __AMSCIMGLIB4_H__
#define __AMSCIMGLIB4_H__

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#ifdef _WIN32
    #ifdef EXPORT_AMSCIMGLIB4
        #define AMSCIMGLIB4_API __declspec(dllexport)
    #else
        #define AMSCIMGLIB4_API __declspec(dllimport)
    #endif
#else
    //#define AMSCPPDLL_API __attribute__((visibility("default")))
    #define AMSCIMGLIB4_API 
#endif

#ifdef __cplusplus
extern "C" { 
#endif

AMSCIMGLIB4_API int amscppdlltemplate_exportedtestfn(int a, int b);

static const int amscimglib4_success = 0;
static const int amscimglib4_failure = -1;

static const int amscimglib4_type_unknown = -1;
static const int amscimglib4_type_bmp = 1;
static const int amscimglib4_type_png = 2;
static const int amscimglib4_type_jpg = 3;



// Main Dynamic Image Structure
//bytebuf contans RGBARGBA information in the following order:
//to access pixel [I,J] color C, [I+J*sizex]*4+C
typedef struct amscimglib4_image
{
    int sizex;
    int sizey;
    unsigned char *data; //size 4*sizex*sizey, [color + I*4 + J*4*sizex]
} amscimglib4_image;

typedef struct amscimglib4_pixel
{
    unsigned char R;
    unsigned char G;
    unsigned char B;
    unsigned char A;
} amscimglib4_pixel;

// commonly known as an opaque pointer pattern or PIMPL pattern (Pointer to IMPLementation) for memory management.

AMSCIMGLIB4_API int amscimglib4_image_new(amscimglib4_image **imgptr, int _sizex, int _sizey);


AMSCIMGLIB4_API int amscimglib4_image_delete(amscimglib4_image **imgptr);


AMSCIMGLIB4_API int amscimglib4_image_resize(amscimglib4_image *imgptr, int _sizex, int _sizey);

//sets all pixels in the image to (0,0,0,0)
AMSCIMGLIB4_API void amscimglib4_image_clear(amscimglib4_image *imgptr);

//copies an image from one image struct to another
//resizes imgto
AMSCIMGLIB4_API int amscimglib4_image_copy(amscimglib4_image *imgto, const amscimglib4_image *imgfrom);

AMSCIMGLIB4_API int amscimglib4_set_pixel_RGBA(amscimglib4_image *img, int x, int y, 
    unsigned char R, unsigned char G, unsigned char B, unsigned char A);

AMSCIMGLIB4_API int amscimglib4_get_pixel_RGBA(amscimglib4_image *img, int x, int y, 
    unsigned char *R, unsigned char *G, unsigned char *B, unsigned char *A);

AMSCIMGLIB4_API int amscimglib4_set_pixel(amscimglib4_image *img, int x, int y, 
    const amscimglib4_pixel px);

AMSCIMGLIB4_API int amscimglib4_get_pixel(amscimglib4_image *img, int x, int y, 
    amscimglib4_pixel *px);



//////////////////////////////////////////////
// Some limited image manipulation routines //
//////////////////////////////////////////////

//transposes an image
AMSCIMGLIB4_API int amscimglib4_transpose_image(amscimglib4_image *img);

AMSCIMGLIB4_API void amscimglib4_image_flipx(amscimglib4_image *img);
AMSCIMGLIB4_API void amscimglib4_image_flipy(amscimglib4_image *img);

//////////////////////////////////////////////////
// Routines to load and save image file formats //
//////////////////////////////////////////////////

AMSCIMGLIB4_API int amscimglib4_magic_type(const char *fname);

AMSCIMGLIB4_API void amscimglib4_readimage(const char *fname, amscimglib4_image *img);

//writes an image to a file
//            amscimglib4_type_bmp - write as a BMP
//            amscimglib4_type_jpg - write as a JPG
//            anything else - default is PNG
AMSCIMGLIB4_API void amscimglib4_writeimage_select(const char *fname, amscimglib4_image *img, int formatspec);

//writes to a file of a given type based on filename extension
AMSCIMGLIB4_API void amscimglib4_writeimage(const char *fname, amscimglib4_image *img);

AMSCIMGLIB4_API void amscimglib4_readimage_bmp(const char *fname, amscimglib4_image *img);
AMSCIMGLIB4_API void amscimglib4_writeimage_bmp(const char *fname, amscimglib4_image *img);

AMSCIMGLIB4_API void amscimglib4_readimage_png(const char *fname, amscimglib4_image *img);
AMSCIMGLIB4_API void amscimglib4_writeimage_png(const char *fname, amscimglib4_image *img);

AMSCIMGLIB4_API void amscimglib4_readimage_jpeg(const char *fname, amscimglib4_image *img);
AMSCIMGLIB4_API void amscimglib4_writeimage_jpeg(const char *fname, amscimglib4_image *img, int quality);




#ifdef __cplusplus
};  // end extern "C"
#endif

#endif