/* === S Y N F I G ========================================================= */
/*!	\file trgt_bmp.cpp
**	\brief Bitmap Target
**
**	\legal
**	Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
**	Copyright (c) 2007 Chris Moore
**
**	This file is part of Synfig.
**
**	Synfig is free software: you can redistribute it and/or modify
**	it under the terms of the GNU General Public License as published by
**	the Free Software Foundation, either version 2 of the License, or
**	(at your option) any later version.
**
**	Synfig is distributed in the hope that it will be useful,
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**	GNU General Public License for more details.
**
**	You should have received a copy of the GNU General Public License
**	along with Synfig.  If not, see <https://www.gnu.org/licenses/>.
**	\endlegal
*/
/* ========================================================================= */

/* === H E A D E R S ======================================================= */

#ifdef USING_PCH
#	include "pch.h"
#else
#ifdef HAVE_CONFIG_H
#	include <config.h>
#endif

#include "trgt_bmp.h"
#include <synfig/general.h>
#include <synfig/localization.h>

#include <cstdio>
#include <algorithm>
#include <functional>

#endif

/* === U S I N G =========================================================== */

using namespace synfig;

/* === I N F O ============================================================= */

SYNFIG_TARGET_INIT(bmp);
SYNFIG_TARGET_SET_NAME(bmp,"bmp");
SYNFIG_TARGET_SET_EXT(bmp,"bmp");
SYNFIG_TARGET_SET_VERSION(bmp,"0.1");

/* === C L A S S E S & S T R U C T S ======================================= */
namespace synfig {

/*struct BITMAPFILEHEADER
{
	unsigned char	bfType[2];
	unsigned long	bfSize;
	unsigned short	bfReserved1;
	unsigned short	bfReserved2;
	unsigned long	bfOffsetBits;
};

struct BITMAPINFOHEADER
{
	unsigned long	biSize;
	long			biWidth;
	long			biHeight;
	unsigned short	biPlanes;
	unsigned short	biBitCount;
	unsigned long	biCompression;
	unsigned long	biSizeImage;
	long			biXPelsPerMeter;
	long			biYPelsPerMeter;
	unsigned long	biClrUsed;
	unsigned long	biClrImportant;
};*/

#pragma pack(push,1)
namespace BITMAP
{
	typedef unsigned char uint8_t;
	typedef unsigned short int uint16_t;
	typedef int int32_t;
	typedef unsigned int uint32_t;

	struct FILEHEADER
	{
		uint8_t		bfType[2];
		uint32_t	bfSize;
		uint16_t	bfReserved1;
		uint16_t	bfReserved2;
		uint32_t	bfOffsetBits;
	};

	struct INFOHEADER
	{
		uint32_t	biSize;
		int32_t		biWidth;
		int32_t		biHeight;
		uint16_t	biPlanes;
		uint16_t	biBitCount;
		uint32_t	biCompression;
		uint32_t	biSizeImage;
		int32_t		biXPelsPerMeter;
		int32_t		biYPelsPerMeter;
		uint32_t	biClrUsed;
		uint32_t	biClrImportant;
	};
}
#pragma pack(pop)


}
/* === M E T H O D S ======================================================= */

#ifdef WORDS_BIGENDIAN
inline long little_endian(const long &x)
{
	long ret;
	char *big_e=(char *)&ret;
	char *lit_e=(char *)&x;
	big_e[0]=lit_e[3];
	big_e[1]=lit_e[2];
	big_e[2]=lit_e[1];
	big_e[3]=lit_e[0];
	return ret;
}
inline short little_endian_short(const short &x)
{
	short ret;
	char *big_e=(char *)&ret;
	char *lit_e=(char *)&x;
	big_e[0]=lit_e[1];
	big_e[1]=lit_e[0];
	return ret;
}
#else
#define little_endian(x)	(x)
#define little_endian_short(x)	(x)
#endif

bmp::bmp(const synfig::filesystem::Path& Filename, const synfig::TargetParam& params):
	rowspan(),
	imagecount(),
	multi_image(false),
	filename(Filename),
	pf()
{
	set_alpha_mode(TARGET_ALPHA_MODE_FILL);
	sequence_separator=params.sequence_separator;
}

bmp::~bmp()
{
}

bool
bmp::set_rend_desc(RendDesc *given_desc)
{
	pf=PF_BGR;

    // Flip the image upside down,
	// because bitmaps are upside down.
    given_desc->set_flags(0);
	Point tl=given_desc->get_tl();
	Point br=given_desc->get_br();
	Point::value_type tmp;
	tmp=tl[1];
	tl[1]=br[1];
	br[1]=tmp;
	given_desc->set_tl(tl);
	given_desc->set_br(br);

	desc=*given_desc;
	if(desc.get_frame_end()-desc.get_frame_start()>0)
	{
		multi_image=true;
		imagecount=desc.get_frame_start();
	}
	else
		multi_image=false;

    return true;
}

void
bmp::end_frame()
{
	file.reset();
	color_buffer.clear();
	imagecount++;
}

bool
bmp::start_frame(synfig::ProgressCallback *callback)
{
	int w=desc.get_w(),h=desc.get_h();

	rowspan=4*((w*(pixel_size(pf)*8)+31)/32);

	synfig::filesystem::Path newfilename = filename;

	if (filename.u8string() == "-") {
		if (callback)
			callback->task(strprintf("(stdout) %d",imagecount));
		file = stdout;
	} else {
		if (multi_image) {
			newfilename.add_suffix(strprintf("%s%04d", sequence_separator.c_str(), imagecount));

			if (callback)
				callback->task(newfilename.u8string() + _(" (animated)"));
		} else {
			if (callback)
				callback->task(newfilename.u8string());
		}
		file = SmartFILE(newfilename, "wb");
	}

	if (!file) {
		if (callback)
			callback->error(_("Unable to open file"));
		else
			synfig::error(_("Unable to open file"));
		return false;
	}

	synfig::BITMAP::FILEHEADER fileheader;
	synfig::BITMAP::INFOHEADER infoheader;

	fileheader.bfType[0]='B';
	fileheader.bfType[1]='M';
	fileheader.bfSize=little_endian(sizeof(synfig::BITMAP::FILEHEADER)+sizeof(synfig::BITMAP::INFOHEADER)+rowspan*h);
	fileheader.bfReserved1=0;
	fileheader.bfReserved2=0;
	fileheader.bfOffsetBits=little_endian(sizeof(synfig::BITMAP::FILEHEADER)+sizeof(synfig::BITMAP::INFOHEADER));

	infoheader.biSize=little_endian(sizeof(synfig::BITMAP::INFOHEADER));
	infoheader.biWidth=little_endian(w);
	infoheader.biHeight=little_endian(h);
	infoheader.biPlanes=little_endian_short((short)1);
	infoheader.biBitCount=little_endian_short((short)(pixel_size(pf)*8));
	infoheader.biCompression=little_endian(0);
	infoheader.biSizeImage=little_endian(0);
	infoheader.biXPelsPerMeter=little_endian((int)rend_desc().get_x_res());
	infoheader.biYPelsPerMeter=little_endian((int)rend_desc().get_y_res()); // pels per meter...?
	infoheader.biClrUsed=little_endian(0);
	infoheader.biClrImportant=little_endian(0);

	//fprintf(file,"BM");

	//if (!fwrite(&fileheader.bfSize,sizeof(synfig::BITMAPFILEHEADER)-4,1,file))
	if (!fwrite(&fileheader, sizeof(synfig::BITMAP::FILEHEADER), 1, file.get()))
	{
		if(callback)callback->error(_("Unable to write file header to file"));
		else synfig::error(_("Unable to write file header to file"));
		return false;
	}

	if (!fwrite(&infoheader, sizeof(synfig::BITMAP::INFOHEADER), 1, file.get()))
	{
		if(callback)callback->error(_("Unable to write info header"));
		else synfig::error(_("Unable to write info header"));
		return false;
	}

	buffer.resize(rowspan);
	color_buffer.resize(desc.get_w());

	return true;
}

Color *
bmp::start_scanline(int /*scanline*/)
{
	return color_buffer.empty() ? nullptr : color_buffer.data();
}

bool
bmp::end_scanline()
{
	if(!file)
		return false;

	color_to_pixelformat(buffer.data(), color_buffer.data(), pf, 0, desc.get_w());

	if (!fwrite(buffer.data(), 1, rowspan, file.get()))
		return false;

	return true;
}
