/***************************************************************************
*                                                                          *
*  Copyright (C) Golaem S.A.  All Rights Reserved.                         *
*                                                                          *
***************************************************************************/

#pragma once

#include "glmCoreAPI.h"

#include "lodepng.h"

//This file is just an encapsulation to the functions in lodepng.h with the same name, but here they are exported in the glmCrowdIO library
//Original source files are at https://github.com/lvandeve/lodepng

namespace glm
{

	/*Returns an English description of the numerical error code.*/
	extern GLM_CORE_API const char* lodepng_error_text(unsigned code);

	/*
	Converts raw pixel data into a PNG image in memory. The colortype and bitdepth
		of the output PNG image cannot be chosen, they are automatically determined
		by the colortype, bitdepth and content of the input pixel data.
		Note: for 16-bit per channel colors, needs big endian format like PNG does.
	out: Output parameter. Pointer to buffer that will contain the PNG image data.
			Must be freed after usage with free(*out).
	outsize: Output parameter. Pointer to the size in bytes of the out buffer.
	image: The raw pixel data to encode. The size of this buffer should be
			w * h * (bytes per pixel), bytes per pixel depends on colortype and bitdepth.
	w: width of the raw pixel data in pixels.
	h: height of the raw pixel data in pixels.
	colortype: the color type of the raw input image. See explanation on PNG color types.
	bitdepth: the bit depth of the raw input image. See explanation on PNG color types.
	Return value: LodePNG error code (0 means no error).
	*/
	extern GLM_CORE_API unsigned lodepng_encode_memory(unsigned char** out, size_t* outsize,
		const unsigned char* image, unsigned w, unsigned h,
		LodePNGColorType colortype, unsigned bitdepth);

	/*Same as lodepng_encode_memory, but always encodes from 32-bit RGBA raw image.*/
	extern GLM_CORE_API unsigned lodepng_encode32(unsigned char** out, size_t* outsize,
		const unsigned char* image, unsigned w, unsigned h);

	/*Same as lodepng_encode_memory, but always encodes from 24-bit RGB raw image.*/
	extern GLM_CORE_API unsigned lodepng_encode24(unsigned char** out, size_t* outsize,
		const unsigned char* image, unsigned w, unsigned h);

	/*
	Converts raw pixel data into a PNG file on disk.
	Same as the other encode functions, but instead takes a filename as output.
	NOTE: This overwrites existing files without warning!
	*/
	extern GLM_CORE_API unsigned lodepng_encode_file(const char* filename,
		const unsigned char* image, unsigned w, unsigned h,
		LodePNGColorType colortype, unsigned bitdepth);

	/*Same as lodepng_encode_file, but always encodes from 32-bit RGBA raw image.*/
	extern GLM_CORE_API unsigned lodepng_encode32_file(const char* filename,
		const unsigned char* image, unsigned w, unsigned h);

	/*Same as lodepng_encode_file, but always encodes from 24-bit RGB raw image.*/
	extern GLM_CORE_API unsigned lodepng_encode24_file(const char* filename,
		const unsigned char* image, unsigned w, unsigned h);

	/*
	Converts PNG data in memory to raw pixel data.
	out: Output parameter. Pointer to buffer that will contain the raw pixel data.
		 After decoding, its size is w * h * (bytes per pixel) bytes larger than
		 initially. Bytes per pixel depends on colortype and bitdepth.
		 Must be freed after usage with free(*out).
		 Note: for 16-bit per channel colors, uses big endian format like PNG does.
	w: Output parameter. Pointer to width of pixel data.
	h: Output parameter. Pointer to height of pixel data.
	in: Memory buffer with the PNG file.
	insize: size of the in buffer.
	colortype: the desired color type for the raw output image. See explanation on PNG color types.
	bitdepth: the desired bit depth for the raw output image. See explanation on PNG color types.
	Return value: LodePNG error code (0 means no error).
	*/
	extern GLM_CORE_API unsigned lodepng_decode_memory(unsigned char** out, unsigned* w, unsigned* h,
		const unsigned char* in, size_t insize,
		LodePNGColorType colortype, unsigned bitdepth);

	/*Same as lodepng_decode_memory, but always decodes to 32-bit RGBA raw image*/
	extern GLM_CORE_API unsigned lodepng_decode32(unsigned char** out, unsigned* w, unsigned* h,
		const unsigned char* in, size_t insize);

	/*Same as lodepng_decode_memory, but always decodes to 24-bit RGB raw image*/
	extern GLM_CORE_API unsigned lodepng_decode24(unsigned char** out, unsigned* w, unsigned* h,
		const unsigned char* in, size_t insize);

	/*
	Load PNG from disk, from file with given name.
	Same as the other decode functions, but instead takes a filename as input.
	*/
	extern GLM_CORE_API unsigned lodepng_decode_file(unsigned char** out, unsigned* w, unsigned* h,
		const char* filename,
		LodePNGColorType colortype, unsigned bitdepth);

	/*Same as lodepng_decode_file, but always decodes to 32-bit RGBA raw image.*/
	extern GLM_CORE_API unsigned lodepng_decode32_file(unsigned char** out, unsigned* w, unsigned* h,
		const char* filename);

	/*Same as lodepng_decode_file, but always decodes to 24-bit RGB raw image.*/
	extern GLM_CORE_API unsigned lodepng_decode24_file(unsigned char** out, unsigned* w, unsigned* h,
		const char* filename);
}