module rip.io.formats.tga;

private {
	import std.stdio;
	import std..string;

	import rip.concepts;
	import rip.io.interfaces;

	import rip.io.dlib;
}

public:
static const auto TGA = new TGAWorker;

private:

static final class TGAWorker : FormatWorker!(Surface) {

	override Surface save(in Surface surface, in string filename) const {
		import rip.io.dlib;

		saveImage(surfaceToSuperImage(surface), filename);

		return cast(Surface)surface;
	}
	//вернёт false, если всё прошло успешно

	override Surface decode(File file) const {
		return this.load(file.name);
	}

	override bool checkOnHeader(File file) const {
		ubyte[17] buff;
		file.seek(-18, SEEK_END);
		file.rawRead(buff);
		file.seek(0);
		return buff == "TRUEVISION-XFILE.";
	}

	override Surface load(in string filename) const {
		import rip.io.dlib;

		return superImageToSurface(loadTGA(filename));
	}
}