#pragma once

#define STB_IMAGE_IMPLEMENTATION
#if defined(_WIN32)
#define STBI_WINDOWS_UTF8
#endif
#include "stb_image.h"

#if !defined(_WIN32)
using COLORREF = uint32_t;
#define SUCCEEDED(x)  x == 0
#define RGB(r,g,b) ( ((COLORREF)(uint8_t)r)|((COLORREF)((uint8_t)g)<<8)|((COLORREF)((uint8_t)b)<<16) )
#endif

class CImage
{
		int width{ 0 };
		int height{ 0 };
		int channels{ 0 };
		stbi_uc *img{ nullptr };

	public:
		CImage()
		{
		}

		~CImage()
		{
			Destroy();
		}

		int Load(const char *fname)
		{
			img = stbi_load(fname, &width, &height, &channels, 0);

			if (img)
			{
				return 0;
			}

			return 1;
		}

		int GetHeight() const
		{
			return height;
		}
		int GetWidth() const
		{
			return width;
		}
		COLORREF GetPixel(const int x, const int y)
		{
			if (img)
			{
				return RGB(img[y * width * channels + x * channels + 0],
				           img[y * width * channels + x * channels + 1],
				           img[y * width * channels + x * channels + 2]);
			}

			return 0;
		}

		void Destroy()
		{
			if (img)
			{
				stbi_image_free(img);
			}

			width = 0;
			height = 0;
			img = nullptr;
		}
};


namespace BKBin2Obj
{
	void Usage();
	void PrintVersion();
	bool MakeObj(const fs::path &pathName, const std::_tstring &strExt);
	void ConvertBin();
	void ConvertImage();
	void convertBitmap1(uint8_t *destp, CImage *img, bool t);
	void convertBitmap2(uint8_t *destp, CImage *img, bool t);
}
