Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tstream.h"
Toshihiro Shimizu 890ddd
#include "timageinfo.h"
Toshihiro Shimizu 890ddd
#include "tmeshimage.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tiio_mesh.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TODO: Eccezioni. Vedi come funziona in toonzscene.cpp
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
//    TImageWriterMesh  definition
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TImageWriterMesh : public TImageWriter
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFrameId m_fid; //!< The frame id
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	TImageWriterMesh(const TFilePath &fp, const TFrameId &frameId);
Toshihiro Shimizu 890ddd
	~TImageWriterMesh() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	void save(const TImageP &);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	TImageWriterMesh(const TImageWriterMesh &);
Toshihiro Shimizu 890ddd
	TImageWriterMesh &operator=(const TImageWriterMesh &src);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TImageWriterMesh::TImageWriterMesh(const TFilePath &fp, const TFrameId &frameId)
Toshihiro Shimizu 890ddd
	: TImageWriter(fp), m_fid(frameId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TImageWriterMesh::save(const TImageP &img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath imagePath(this->m_path.withFrame(m_fid));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TOStream ostream(imagePath, true); // Use compression
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TMeshImageP mi = img;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Save Header
Toshihiro Shimizu 890ddd
	ostream.openChild("header");
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		// Save version
Toshihiro Shimizu 890ddd
		ostream.openChild("version");
Toshihiro Shimizu 890ddd
		ostream << 1 << 19;
Toshihiro Shimizu 890ddd
		ostream.closeChild();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// Save dpi
Toshihiro Shimizu 890ddd
		ostream.openChild("dpi");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		double dpiX, dpiY;
Toshihiro Shimizu 890ddd
		mi->getDpi(dpiX, dpiY);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		ostream << dpiX << dpiY;
Toshihiro Shimizu 890ddd
		ostream.closeChild();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	ostream.closeChild();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Save meshes
Toshihiro Shimizu 890ddd
	const std::vector<ttexturemeshp> &meshes = mi->meshes();</ttexturemeshp>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int m, mCount = meshes.size();
Toshihiro Shimizu 890ddd
	for (m = 0; m < mCount; ++m) {
Toshihiro Shimizu 890ddd
		ostream.openChild("mesh");
Toshihiro Shimizu 890ddd
		ostream << *meshes[m];
Toshihiro Shimizu 890ddd
		ostream.closeChild();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
//    TImageReaderMesh  definition
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TImageReaderMesh : public TImageReader
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFrameId m_fid;			   //
Toshihiro Shimizu 890ddd
	mutable TImageInfo m_info; //!< The image's infos
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	TImageReaderMesh(const TFilePath &fp, const TFrameId &frameId);
Toshihiro Shimizu 890ddd
	~TImageReaderMesh() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const TImageInfo *getImageInfo() const;
Toshihiro Shimizu 890ddd
	TImageP load();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	//! Reference to level reader
Toshihiro Shimizu 890ddd
	TLevelReaderMesh *m_lrp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	void readHeader(TIStream &is) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// not implemented
Toshihiro Shimizu 890ddd
	TImageReaderMesh(const TImageReaderMesh &);
Toshihiro Shimizu 890ddd
	TImageReaderMesh &operator=(const TImageReaderMesh &src);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TImageReaderMesh::TImageReaderMesh(const TFilePath &fp, const TFrameId &frameId)
Toshihiro Shimizu 890ddd
	: TImageReader(fp), m_fid(frameId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const TImageInfo *TImageReaderMesh::getImageInfo() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_info.m_valid) {
Toshihiro Shimizu 890ddd
		// Load info from file
Toshihiro Shimizu 890ddd
		TFilePath imagePath(this->m_path.withFrame(m_fid));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TIStream is(imagePath);
Toshihiro Shimizu 890ddd
		readHeader(is);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return &m_info;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TImageReaderMesh::readHeader(TIStream &is) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	std::string tagName;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Open header tag
Toshihiro Shimizu 890ddd
	is.openChild(tagName);
Toshihiro Shimizu 890ddd
	assert(tagName == "header");
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		// Read header entries
Toshihiro Shimizu 890ddd
		while (is.openChild(tagName)) {
Toshihiro Shimizu 890ddd
			if (tagName == "version") {
Toshihiro Shimizu 890ddd
				int major, minor;
Toshihiro Shimizu 890ddd
				is >> major >> minor;
Toshihiro Shimizu 890ddd
				is.setVersion(VersionNumber(major, minor));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				is.closeChild();
Toshihiro Shimizu 890ddd
			} else if (tagName == "dpi") {
Toshihiro Shimizu 890ddd
				is >> m_info.m_dpix >> m_info.m_dpiy;
Toshihiro Shimizu 890ddd
				assert(m_info.m_dpix > 0.0 && m_info.m_dpiy > 0.0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				is.closeChild();
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				is.skipCurrentTag();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	is.closeChild();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_info.m_valid = true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TImageP TImageReaderMesh::load()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TMeshImageP mi(new TMeshImage);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath imagePath(this->m_path.withFrame(m_fid));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TIStream is(imagePath);
Toshihiro Shimizu 890ddd
	readHeader(is);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	mi->setDpi(m_info.m_dpix, m_info.m_dpiy);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Meshes
Toshihiro Shimizu 890ddd
	std::vector<ttexturemeshp> &meshes = mi->meshes();</ttexturemeshp>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::string tagName;
Toshihiro Shimizu 890ddd
	while (is.openChild(tagName)) {
Toshihiro Shimizu 890ddd
		if (tagName == "mesh") {
Toshihiro Shimizu 890ddd
			meshes.push_back(new TTextureMesh);
Toshihiro Shimizu 890ddd
			is >> *meshes.back();
Toshihiro Shimizu 890ddd
			is.closeChild();
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			is.skipCurrentTag();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return mi;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
//    TLevelWriterMesh  implementation
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLevelWriterMesh::TLevelWriterMesh(const TFilePath &path, TPropertyGroup *winfo)
Toshihiro Shimizu 890ddd
	: TLevelWriter(path, winfo)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLevelWriterMesh::~TLevelWriterMesh()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TImageWriterP TLevelWriterMesh::getFrameWriter(TFrameId fid)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return new TImageWriterMesh(this->m_path, fid);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
//    TLevelReaderMesh  implementation
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLevelReaderMesh::TLevelReaderMesh(const TFilePath &path)
Toshihiro Shimizu 890ddd
	: TLevelReader(path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLevelReaderMesh::~TLevelReaderMesh()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TImageReaderP TLevelReaderMesh::getFrameReader(TFrameId fid)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return new TImageReaderMesh(this->m_path, fid);
Toshihiro Shimizu 890ddd
}