Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef XPRESS
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tiio_pli.h"
Toshihiro Shimizu 890ddd
//#include "tstrokeoutline.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "pli_io.h"
Toshihiro Shimizu 890ddd
//#include "tstrokeutil.h"
Toshihiro Shimizu 890ddd
#include "tregion.h"
Toshihiro Shimizu 890ddd
#include "tsimplecolorstyles.h"
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
//#include "tspecialstyleid.h"
Toshihiro Shimizu 890ddd
#include "tiio.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
#include "tcontenthistory.h"
Toshihiro Shimizu 890ddd
#include "tstroke.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef TVectorImage::IntersectionBranch IntersectionBranch;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
const TSolidColorStyle ConstStyle(TPixel32::Black);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static TSolidColorStyle *CurrStyle = NULL;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class PliOuputStream : public TOutputStreamInterface
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::vector<tstyleparam> *m_stream;</tstyleparam>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 3bfa54
	PliOuputStream(std::vector<tstyleparam> *stream) : m_stream(stream) {}</tstyleparam>
Toshihiro Shimizu 890ddd
	TOutputStreamInterface &operator<<(double x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_stream->push_back(TStyleParam(x));
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TOutputStreamInterface &operator<<(int x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_stream->push_back(TStyleParam(x));
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Shinya Kitaoka 3bfa54
	TOutputStreamInterface &operator<<(std::string x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_stream->push_back(TStyleParam(x));
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TOutputStreamInterface &operator<<(USHORT x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_stream->push_back(TStyleParam(x));
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TOutputStreamInterface &operator<<(BYTE x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_stream->push_back(TStyleParam(x));
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TOutputStreamInterface &operator<<(const TRaster32P &x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_stream->push_back(TStyleParam(x));
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class PliInputStream : public TInputStreamInterface
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::vector<tstyleparam> *m_stream;</tstyleparam>
Toshihiro Shimizu 890ddd
	VersionNumber m_version;
Toshihiro Shimizu 890ddd
	int m_count;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 3bfa54
	PliInputStream(std::vector<tstyleparam> *stream,</tstyleparam>
Toshihiro Shimizu 890ddd
				   int majorVersion, int minorVersion)
Toshihiro Shimizu 890ddd
		: m_stream(stream), m_version(majorVersion, minorVersion), m_count(0) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TInputStreamInterface &operator>>(double &x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		assert((*m_stream)[m_count].m_type == TStyleParam::SP_DOUBLE);
Toshihiro Shimizu 890ddd
		x = (*m_stream)[m_count++].m_numericVal;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TInputStreamInterface &operator>>(int &x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		assert((*m_stream)[m_count].m_type == TStyleParam::SP_INT);
Toshihiro Shimizu 890ddd
		x = (int)(*m_stream)[m_count++].m_numericVal;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Shinya Kitaoka 3bfa54
	TInputStreamInterface &operator>>(std::string &x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if ((*m_stream)[m_count].m_type == TStyleParam::SP_INT)
Toshihiro Shimizu 890ddd
			x = toString((int)(*m_stream)[m_count++].m_numericVal);
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			assert((*m_stream)[m_count].m_type == TStyleParam::SP_STRING);
Toshihiro Shimizu 890ddd
			x = (*m_stream)[m_count++].m_string;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TInputStreamInterface &operator>>(BYTE &x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		assert((*m_stream)[m_count].m_type == TStyleParam::SP_BYTE);
Toshihiro Shimizu 890ddd
		x = (BYTE)(*m_stream)[m_count++].m_numericVal;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TInputStreamInterface &operator>>(USHORT &x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		assert((*m_stream)[m_count].m_type == TStyleParam::SP_USHORT);
Toshihiro Shimizu 890ddd
		x = (USHORT)(*m_stream)[m_count++].m_numericVal;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TInputStreamInterface &operator>>(TRaster32P &x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		assert((*m_stream)[m_count].m_type == TStyleParam::SP_RASTER);
Toshihiro Shimizu 890ddd
		x = (*m_stream)[m_count++].m_r;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	VersionNumber versionNumber() const { return m_version; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPixel32 getColor(const TStroke *stroke)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//const TStrokeStyle* style = stroke->getStyle();
Toshihiro Shimizu 890ddd
	//const TSolidColorStrokeStyle* style =   dynamic_cast<const tsolidcolorstrokestyle*="">(  );</const>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//if(style) return style->getAverageColor();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TPixel32::Transparent;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
  Crea la palette dei colori, in funzione di quelli 
Toshihiro Shimizu 890ddd
  trovati dalla funzione findColor
Toshihiro Shimizu 890ddd
  */
Shinya Kitaoka 3bfa54
UINT findColor(const TPixel32 &color, const std::vector<tpixel> &colorArray)</tpixel>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (UINT i = 0; i < colorArray.size(); i++)
Toshihiro Shimizu 890ddd
		if (colorArray[i] == color)
Toshihiro Shimizu 890ddd
			return i;
Toshihiro Shimizu 890ddd
	return colorArray.size();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void buildPalette(ParsedPli *pli, const TImageP img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!pli->m_palette_tags.empty())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	TVectorImageP tempVecImg = img;
Toshihiro Shimizu 890ddd
	TPalette *vPalette = tempVecImg->getPalette();
Toshihiro Shimizu 890ddd
	unsigned int i;
Toshihiro Shimizu 890ddd
	//if (pli->m_idWrittenColorsArray.empty())
Toshihiro Shimizu 890ddd
	//  {
Toshihiro Shimizu 890ddd
	//  assert(vPalette);
Toshihiro Shimizu 890ddd
	//  pli->m_idWrittenColorsArray.resize(vPalette->getStyleCount());
Toshihiro Shimizu 890ddd
	//   }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// se c'e' una reference image, uso il primo stile della palette per memorizzare il path
Toshihiro Shimizu 890ddd
	TFilePath fp;
Toshihiro Shimizu 890ddd
	if ((fp = vPalette->getRefImgPath()) != TFilePath()) {
Toshihiro Shimizu 890ddd
		TStyleParam styleParam("refimage" + toString(fp));
Toshihiro Shimizu 890ddd
		StyleTag *refImageTag = new StyleTag(0, 0, 1, &styleParam);
Toshihiro Shimizu 890ddd
		pli->m_palette_tags.push_back((PliObjectTag *)refImageTag);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//per scrivere le pages della palette, uso in modo improprio uno stile: il primo stile
Toshihiro Shimizu 890ddd
	//della paletta(o il secondo, se c'e' una refimage)  ha tutti parametri stringa, che coincidono con i nomi delle pages
Toshihiro Shimizu 890ddd
	//ilcampo m_id viene usato anche per mettere il frameIndex(per multi palette)
Toshihiro Shimizu 890ddd
	assert(vPalette->getPageCount());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	std::vector<tstyleparam> pageNames(vPalette->getPageCount());</tstyleparam>
Toshihiro Shimizu 890ddd
	for (i = 0; i < pageNames.size(); i++)
Toshihiro Shimizu 890ddd
		pageNames[i] = TStyleParam(toString(vPalette->getPage(i)->getName()));
Shinya Kitaoka 79e39b
	StyleTag *pageNamesTag = new StyleTag(0, 0, pageNames.size(), pageNames.data());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	pli->m_palette_tags.push_back((PliObjectTag *)pageNamesTag);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
  for(i=1 ; i<pli->m_idWrittenColorsArray.size(); i++ )</pli->
Toshihiro Shimizu 890ddd
    pli->m_idWrittenColorsArray[i]=false;
Toshihiro Shimizu 890ddd
  pli->m_idWrittenColorsArray[0]=true;
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 1; i < (unsigned)vPalette->getStyleCount(); i++) {
Toshihiro Shimizu 890ddd
		TColorStyle *style = vPalette->getStyle(i);
Toshihiro Shimizu 890ddd
		TPalette::Page *page = vPalette->getStylePage(i);
Toshihiro Shimizu 890ddd
		if (!page)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		int pageIndex = 65535;
Toshihiro Shimizu 890ddd
		//if (page)
Toshihiro Shimizu 890ddd
		pageIndex = page->getIndex();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// TColorStyle*style = tempVecImg->getPalette()->getStyle(styleId);
Shinya Kitaoka 3bfa54
		std::vector<tstyleparam> stream;</tstyleparam>
Toshihiro Shimizu 890ddd
		PliOuputStream chan(&stream);
Toshihiro Shimizu 890ddd
		style->save(chan); //viene riempito lo stream;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		assert(pageIndex >= 0 && pageIndex <= 65535);
Shinya Kitaoka 79e39b
		StyleTag *styleTag = new StyleTag(i, pageIndex, stream.size(), stream.data());
Toshihiro Shimizu 890ddd
		pli->m_palette_tags.push_back((PliObjectTag *)styleTag);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (vPalette->isAnimated()) {
Toshihiro Shimizu 890ddd
		std::set<int> keyFrames;</int>
Toshihiro Shimizu 890ddd
		for (i = 0; i < (unsigned)vPalette->getStyleCount(); i++)
Toshihiro Shimizu 890ddd
			for (int j = 0; j < vPalette->getKeyframeCount(i); j++)
Toshihiro Shimizu 890ddd
				keyFrames.insert(vPalette->getKeyframe(i, j));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		std::set<int>::const_iterator it = keyFrames.begin();</int>
Toshihiro Shimizu 890ddd
		for (; it != keyFrames.end(); ++it) {
Toshihiro Shimizu 890ddd
			int frame = *it;
Toshihiro Shimizu 890ddd
			vPalette->setFrame(frame);
Toshihiro Shimizu 890ddd
			StyleTag *pageNamesTag = new StyleTag(frame, 0, 0, 0); //lo so, e' orrendo. devo mettere un numero intero
Toshihiro Shimizu 890ddd
			pli->m_palette_tags.push_back((PliObjectTag *)pageNamesTag);
Toshihiro Shimizu 890ddd
			for (i = 1; i < (unsigned)vPalette->getStyleCount(); i++) {
Toshihiro Shimizu 890ddd
				if (vPalette->isKeyframe(i, frame)) {
Toshihiro Shimizu 890ddd
					TColorStyle *style = vPalette->getStyle(i);
Toshihiro Shimizu 890ddd
					TPalette::Page *page = vPalette->getStylePage(i);
Toshihiro Shimizu 890ddd
					if (!page)
Toshihiro Shimizu 890ddd
						continue;
Toshihiro Shimizu 890ddd
					int pageIndex = 65535;
Toshihiro Shimizu 890ddd
					//if (page)
Toshihiro Shimizu 890ddd
					pageIndex = page->getIndex();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					// TColorStyle*style = tempVecImg->getPalette()->getStyle(styleId);
Shinya Kitaoka 3bfa54
					std::vector<tstyleparam> stream;</tstyleparam>
Toshihiro Shimizu 890ddd
					PliOuputStream chan(&stream);
Toshihiro Shimizu 890ddd
					style->save(chan); //viene riempito lo stream;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					assert(pageIndex >= 0 && pageIndex <= 65535);
Shinya Kitaoka 79e39b
					StyleTag *styleTag = new StyleTag(i, pageIndex, stream.size(), stream.data());
Toshihiro Shimizu 890ddd
					pli->m_palette_tags.push_back((PliObjectTag *)styleTag);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // unnamed namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===========================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
Classe locale per la scrittura di un frame del livello.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
class TImageWriterPli : public TImageWriter
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	TImageWriterPli(const TFilePath &, const TFrameId &frameId, TLevelWriterPli *);
Toshihiro Shimizu 890ddd
	~TImageWriterPli() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	UCHAR m_precision;
Toshihiro Shimizu 890ddd
	//double m_maxThickness;
Toshihiro Shimizu 890ddd
	//not implemented
Toshihiro Shimizu 890ddd
	TImageWriterPli(const TImageWriterPli &);
Toshihiro Shimizu 890ddd
	TImageWriterPli &operator=(const TImageWriterPli &src);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	void save(const TImageP &);
Toshihiro Shimizu 890ddd
	TFrameId m_frameId;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	TLevelWriterPli *m_lwp;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TImageP TImageReaderPli::load()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_lrp->m_doesExist)
Toshihiro Shimizu 890ddd
		throw TImageException(getFilePath(), "Error file doesn't exist");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	UINT majorVersionNumber, minorVersionNumber;
Toshihiro Shimizu 890ddd
	m_lrp->m_pli->getVersion(majorVersionNumber, minorVersionNumber);
Toshihiro Shimizu 890ddd
	assert(majorVersionNumber > 5 || (majorVersionNumber == 5 && minorVersionNumber >= 5));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return doLoad();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===========================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void readRegionVersion4x(IntersectionDataTag *tag, TVectorImage *img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#ifndef NEW_REGION_FILL
Toshihiro Shimizu 890ddd
	img->setFillData(tag->m_branchArray, tag->m_branchCount);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
struct CreateStrokeData {
Toshihiro Shimizu 890ddd
	int m_styleId;
Toshihiro Shimizu 890ddd
	TStroke::OutlineOptions m_options;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	CreateStrokeData() : m_styleId(-1) {}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void createStroke(ThickQuadraticChainTag *quadTag, TVectorImage *outVectImage, const CreateStrokeData &data)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::vector<tthickquadratic *=""> chunks(quadTag->m_numCurves);</tthickquadratic>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (UINT k = 0; k < quadTag->m_numCurves; k++)
Toshihiro Shimizu 890ddd
		chunks[k] = &quadTag->m_curve[k];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TStroke *stroke = TStroke::create(chunks);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(data.m_styleId != -1);
Toshihiro Shimizu 890ddd
	stroke->setStyle(data.m_styleId);
Toshihiro Shimizu 890ddd
	stroke->outlineOptions() = data.m_options;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (quadTag->m_isLoop)
Toshihiro Shimizu 890ddd
		stroke->setSelfLoop();
Toshihiro Shimizu 890ddd
	//stroke->setSketchMode(groupTag->m_type==GroupTag::SKETCH_STROKE);
Toshihiro Shimizu 890ddd
	outVectImage->addStroke(stroke, false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void createGroup(GroupTag *groupTag, TVectorImage *vi, CreateStrokeData &data)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int count = vi->getStrokeCount();
Toshihiro Shimizu 890ddd
	for (int j = 0; j < groupTag->m_numObjects; j++) {
Toshihiro Shimizu 890ddd
		if (groupTag->m_object[j]->m_type == PliTag::COLOR_NGOBJ)
Toshihiro Shimizu 890ddd
			data.m_styleId = ((ColorTag *)groupTag->m_object[j])->m_color[0];
Toshihiro Shimizu 890ddd
		else if (groupTag->m_object[j]->m_type == PliTag::OUTLINE_OPTIONS_GOBJ)
Toshihiro Shimizu 890ddd
			data.m_options = ((StrokeOutlineOptionsTag *)groupTag->m_object[j])->m_options;
Toshihiro Shimizu 890ddd
		else if (groupTag->m_object[j]->m_type == PliTag::GROUP_GOBJ)
Toshihiro Shimizu 890ddd
			createGroup((GroupTag *)groupTag->m_object[j], vi, data);
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			assert(groupTag->m_object[j]->m_type == PliTag::THICK_QUADRATIC_CHAIN_GOBJ);
Toshihiro Shimizu 890ddd
			createStroke((ThickQuadraticChainTag *)groupTag->m_object[j], vi, data);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	vi->group(count, vi->getStrokeCount() - count);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TImageP TImageReaderPli::doLoad()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	CreateStrokeData strokeData;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// preparo l'immagine da restituire
Toshihiro Shimizu 890ddd
	TVectorImage
Toshihiro Shimizu 890ddd
		*outVectImage = new TVectorImage(true);
Toshihiro Shimizu 890ddd
	// fisso il colore di default a nero opaco
Toshihiro Shimizu 890ddd
	//TPixel currentColor=TPixel::Black;
Toshihiro Shimizu 890ddd
	//TStrokeStyle *currStyle = NULL;
Toshihiro Shimizu 890ddd
	// chiudo tutto dentro un blocco try per  cautelarmi
Toshihiro Shimizu 890ddd
	//  dalle eccezioni generate in lettura
Toshihiro Shimizu 890ddd
	//try
Toshihiro Shimizu 890ddd
	//{
Toshihiro Shimizu 890ddd
	// un contatore
Toshihiro Shimizu 890ddd
	UINT i;
Toshihiro Shimizu 890ddd
	outVectImage->setAutocloseTolerance(m_lrp->m_pli->getAutocloseTolerance());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ImageTag *imageTag;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	imageTag = m_lrp->m_pli->loadFrame(m_frameId);
Toshihiro Shimizu 890ddd
	if (!imageTag)
Toshihiro Shimizu 890ddd
		throw TImageException(m_path, "Corrupted or invalid image data");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_lrp->m_mapOfImage[m_frameId].second == false)
Toshihiro Shimizu 890ddd
		m_lrp->m_mapOfImage[m_frameId].second = true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// per tutti gli oggetti presenti nel tag
Toshihiro Shimizu 890ddd
	for (i = 0; i < imageTag->m_numObjects; i++) {
Toshihiro Shimizu 890ddd
		switch (imageTag->m_object[i]->m_type) {
Toshihiro Shimizu 890ddd
		case PliTag::GROUP_GOBJ: {
Toshihiro Shimizu 890ddd
			assert(((GroupTag *)imageTag->m_object[i])->m_type == GroupTag::STROKE);
Toshihiro Shimizu 890ddd
			createGroup((GroupTag *)imageTag->m_object[i], outVectImage, strokeData);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
			CASE PliTag::INTERSECTION_DATA_GOBJ : readRegionVersion4x((IntersectionDataTag *)imageTag->m_object[i], outVectImage);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			// aggiunge le stroke quadratiche
Toshihiro Shimizu 890ddd
			CASE PliTag::THICK_QUADRATIC_CHAIN_GOBJ : createStroke((ThickQuadraticChainTag *)imageTag->m_object[i], outVectImage, strokeData);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			// aggiunge curve quadratiche con spessore costante
Toshihiro Shimizu 890ddd
			CASE PliTag::COLOR_NGOBJ:
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				ColorTag *colorTag = (ColorTag *)imageTag->m_object[i];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				assert(colorTag->m_numColors == 1);
Toshihiro Shimizu 890ddd
				strokeData.m_styleId = colorTag->m_color[0];
Toshihiro Shimizu 890ddd
				// isSketch=(colorTag->m_color[0] < c_maxSketchColorNum);
Toshihiro Shimizu 890ddd
				// isSketch=(colorTag->m_color[0] < c_maxSketchColorNum);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			// adds outline options data
Toshihiro Shimizu 890ddd
			CASE PliTag::OUTLINE_OPTIONS_GOBJ : strokeData.m_options = ((StrokeOutlineOptionsTag *)imageTag->m_object[i])->m_options;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		DEFAULT:;
Toshihiro Shimizu 890ddd
		} // switch(groupTag->m_object[j]->m_type)
Toshihiro Shimizu 890ddd
	}	 // for (i=0; i<imagetag->m_numObjects; i++)</imagetag->
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//} // try
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//catch(...) // cosi' e' inutile o raccolgo qualcosa prima di rilanciare o lo elimino
Toshihiro Shimizu 890ddd
//{
Toshihiro Shimizu 890ddd
//  throw;
Toshihiro Shimizu 890ddd
// }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//  if (regionsComputed) //WARNING !!! la seedFill mette il flag a ValidRegion a TRUE
Toshihiro Shimizu 890ddd
//    outVectImage->seedFill(); //le vecchie immagini hanno il seed (version<3.1)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef _DEBUG
Toshihiro Shimizu 890ddd
	outVectImage->checkIntersections();
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TImageP(outVectImage);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TDimension TImageReaderPli::getSize() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return TDimension(-1, -1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TRect TImageReaderPli::getBBox() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return TRect();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TImageWriterPli::TImageWriterPli(
Toshihiro Shimizu 890ddd
	const TFilePath &f,
Toshihiro Shimizu 890ddd
	const TFrameId &frameId,
Toshihiro Shimizu 890ddd
	TLevelWriterPli *pli)
Toshihiro Shimizu 890ddd
	: TImageWriter(f), m_frameId(frameId), m_lwp(pli), m_precision(2)
Toshihiro Shimizu 890ddd
//, m_maxThickness(0)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
void putStroke(TStroke *stroke, int &currStyleId, std::vector<pliobjecttag *=""> &tags)</pliobjecttag>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double maxThickness = 0;
Toshihiro Shimizu 890ddd
	assert(stroke);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int chunkCount = stroke->getChunkCount();
Shinya Kitaoka 3bfa54
	std::vector<tthickquadratic> strokeChain(chunkCount);</tthickquadratic>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int styleId = stroke->getStyle();
Toshihiro Shimizu 890ddd
	assert(styleId >= 0);
Toshihiro Shimizu 890ddd
	if (currStyleId == -1 || styleId != currStyleId) {
Toshihiro Shimizu 890ddd
		currStyleId = styleId;
Shinya Kitaoka 79e39b
		std::unique_ptr<tuint32[]> color(new TUINT32[1]);</tuint32[]>
Toshihiro Shimizu 890ddd
		color[0] = (TUINT32)styleId;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 79e39b
		std::unique_ptr<colortag> colorTag(new ColorTag(ColorTag::SOLID, ColorTag::STROKE_COLOR, 1, std::move(color)));</colortag>
Shinya Kitaoka 79e39b
		tags.push_back(colorTag.release());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//If the outline options are non-standard (not round), add the outline infos
Toshihiro Shimizu 890ddd
	TStroke::OutlineOptions &options = stroke->outlineOptions();
Toshihiro Shimizu 890ddd
	if (options.m_capStyle != TStroke::OutlineOptions::ROUND_CAP ||
Toshihiro Shimizu 890ddd
		options.m_joinStyle != TStroke::OutlineOptions::ROUND_JOIN) {
Toshihiro Shimizu 890ddd
		StrokeOutlineOptionsTag *outlineOptionsTag = new StrokeOutlineOptionsTag(options);
Toshihiro Shimizu 890ddd
		tags.push_back((PliObjectTag *)outlineOptionsTag);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	UINT k;
Toshihiro Shimizu 890ddd
	for (k = 0; k < (UINT)chunkCount; ++k) {
Toshihiro Shimizu 890ddd
		const TThickQuadratic *q = stroke->getChunk(k);
Toshihiro Shimizu 890ddd
		maxThickness = tmax(maxThickness, q->getThickP0().thick, q->getThickP1().thick);
Toshihiro Shimizu 890ddd
		strokeChain[k] = *q;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	maxThickness = tmax(maxThickness, stroke->getChunk(chunkCount - 1)->getThickP2().thick);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ThickQuadraticChainTag *quadChainTag = new ThickQuadraticChainTag(k, &strokeChain[0], maxThickness);
Toshihiro Shimizu 890ddd
	quadChainTag->m_isLoop = stroke->isSelfLoop();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//pli->addTag((PliObjectTag *)quadChainTag);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	tags.push_back((PliObjectTag *)quadChainTag);
Toshihiro Shimizu 890ddd
	//pli->addTag(groupTag[count++]);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
GroupTag *makeGroup(TVectorImageP &vi, int &currStyleId, int &index, int currDepth);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TImageWriterPli::save(const TImageP &img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	// alloco un'immagine
Toshihiro Shimizu 890ddd
	TVectorImageP tempVecImg = img;
Toshihiro Shimizu 890ddd
	int currStyleId = -1;
Toshihiro Shimizu 890ddd
	if (!tempVecImg)
Toshihiro Shimizu 890ddd
		throw TImageException(m_path, "No data to save");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// controllo che il frame che sto per inserire non sia gia' presente
Toshihiro Shimizu 890ddd
	//  in modo da non incrementare il numero di frame correnti
Toshihiro Shimizu 890ddd
	++m_lwp->m_frameNumber;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 79e39b
	std::unique_ptr<intersectionbranch[]> v;</intersectionbranch[]>
Shinya Kitaoka 79e39b
	UINT intersectionSize = tempVecImg->getFillData(v);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// alloco l'oggetto m_lwp->m_pli ( di tipo ParsedPli ) che si occupa di costruire la struttura
Toshihiro Shimizu 890ddd
	if (!m_lwp->m_pli) {
Shinya Kitaoka 79e39b
		m_lwp->m_pli.reset(new ParsedPli(m_lwp->m_frameNumber, m_precision, 40, tempVecImg->getAutocloseTolerance()));
Toshihiro Shimizu 890ddd
		m_lwp->m_pli->setCreator(m_lwp->m_creator);
Toshihiro Shimizu 890ddd
	}
Shinya Kitaoka 79e39b
	buildPalette(m_lwp->m_pli.get(), img);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 79e39b
	ParsedPli *pli = m_lwp->m_pli.get();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* 
Toshihiro Shimizu 890ddd
  comunico che il numero di frame e' aumentato (il parsed lo riceve nel 
Toshihiro Shimizu 890ddd
  solo nel costruttore)
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
	pli->setFrameCount(m_lwp->m_frameNumber);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// alloco la struttura che conterra' i tag per la vector image corrente
Shinya Kitaoka 3bfa54
	std::vector<pliobjecttag *=""> tags;</pliobjecttag>
Shinya Kitaoka 3bfa54
	//tags = new   std::vector<pliobjecttag *="">;</pliobjecttag>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Store the precision scale to be used in saving the quadratics
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		int precisionScale = sq(128);
Toshihiro Shimizu 890ddd
		pli->precisionScale() = precisionScale;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		PliTag *tag = new PrecisionScaleTag(precisionScale);
Toshihiro Shimizu 890ddd
		tags.push_back((PliObjectTag *)tag);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// recupero il numero di stroke dall'immagine
Toshihiro Shimizu 890ddd
	int numStrokes = tempVecImg->getStrokeCount();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i = 0;
Toshihiro Shimizu 890ddd
	while (i < (UINT)numStrokes) {
Toshihiro Shimizu 890ddd
		if (tempVecImg->isStrokeGrouped(i))
Toshihiro Shimizu 890ddd
			tags.push_back(makeGroup(tempVecImg, currStyleId, i, 1));
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			putStroke(tempVecImg->getStroke(i++), currStyleId, tags);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (intersectionSize > 0) {
Shinya Kitaoka 79e39b
		PliTag *tag = new IntersectionDataTag(intersectionSize, std::move(v));
Toshihiro Shimizu 890ddd
		tags.push_back((PliObjectTag *)tag);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int tagsSize = tags.size();
Shinya Kitaoka 79e39b
	std::unique_ptr<imagetag> imageTagPtr(new ImageTag(m_frameId, tagsSize, (tagsSize > 0) ? tags.data() : nullptr));</imagetag>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 79e39b
	pli->addTag(imageTagPtr.release());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// il ritorno e' fissato a false in quanto la
Toshihiro Shimizu 890ddd
	//  scrittura avviene alla distruzione dello scrittore di livelli
Toshihiro Shimizu 890ddd
	return; // davedere false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
TLevelWriterPli::TLevelWriterPli(const TFilePath &path, TPropertyGroup *winfo)
Shinya Kitaoka 79e39b
	: TLevelWriter(path, winfo)
Shinya Kitaoka 79e39b
	, m_frameNumber(0)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLevelWriterPli::~TLevelWriterPli()
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 79e39b
	if (!m_pli) {
Shinya Kitaoka 79e39b
		return;
Shinya Kitaoka 79e39b
	}
Shinya Kitaoka 79e39b
	try {
Shinya Kitaoka 79e39b
		// aggiungo il tag della palette
Shinya Kitaoka 79e39b
		CurrStyle = NULL;
Shinya Kitaoka 79e39b
		assert(!m_pli->m_palette_tags.empty());
Shinya Kitaoka 79e39b
		std::unique_ptr<grouptag> groupTag(new GroupTag(GroupTag::PALETTE, m_pli->m_palette_tags.size(), m_pli->m_palette_tags.data()));</grouptag>
Shinya Kitaoka 79e39b
		m_pli->addTag(groupTag.release(), true);
Shinya Kitaoka 79e39b
		if (m_contentHistory) {
Shinya Kitaoka 79e39b
			QString his = m_contentHistory->serialize();
Shinya Kitaoka 79e39b
			std::unique_ptr<texttag> textTag(new TextTag(his.toStdString()));</texttag>
Shinya Kitaoka 79e39b
			m_pli->addTag(textTag.release(), true);
Toshihiro Shimizu 890ddd
		}
Shinya Kitaoka 79e39b
		m_pli->writePli(m_path);
Shinya Kitaoka 79e39b
	} catch (...) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TImageWriterP TLevelWriterPli::getFrameWriter(TFrameId fid)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TImageWriterPli *iwm = new TImageWriterPli(m_path, fid, this);
Toshihiro Shimizu 890ddd
	return TImageWriterP(iwm);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLevelReaderPli::TLevelReaderPli(const TFilePath &path)
Toshihiro Shimizu 890ddd
	: TLevelReader(path), m_palette(0), m_paletteCount(0), m_doesExist(false), m_pli(0), m_readPalette(true), m_level(), m_init(false)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!(m_doesExist = TFileStatus(path).doesExist()))
Toshihiro Shimizu 890ddd
		throw TImageException(getFilePath(), "Error file doesn't exist");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLevelReaderPli::~TLevelReaderPli()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	delete m_pli;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TImageReaderP TLevelReaderPli::getFrameReader(TFrameId fid)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TImageReaderPli *irm = new TImageReaderPli(m_path, fid, this);
Toshihiro Shimizu 890ddd
	return TImageReaderP(irm);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPalette *readPalette(GroupTag *paletteTag, int majorVersion, int minorVersion)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	bool newPli = (majorVersion > 5 || (majorVersion == 5 && minorVersion >= 6));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//wstring pageName(L"colors");
Toshihiro Shimizu 890ddd
	TPalette *palette = new TPalette();
Toshihiro Shimizu 890ddd
	//palette->addStyleToPage(0, pageName);
Toshihiro Shimizu 890ddd
	//palette->setStyle(0,TPixel32(255,255,255,0));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// palette->setVersion(isNew?0:-1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// tolgo dalla pagina lo stile #1
Toshihiro Shimizu 890ddd
	palette->getPage(0)->removeStyle(1);
Toshihiro Shimizu 890ddd
	int frame = -1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//int m = page->getStyleCount();
Toshihiro Shimizu 890ddd
	bool pagesRead = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//i primi due styletag della palette sono speciali;
Toshihiro Shimizu 890ddd
	//il primo, che potrebbe non esserci, contiene l'eventuale reference image path;
Toshihiro Shimizu 890ddd
	//il secondo, che c'e' sempre contiene i nomi delle pagine.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (unsigned int i = 0; i < paletteTag->m_numObjects; i++) {
Toshihiro Shimizu 890ddd
		StyleTag *styleTag = (StyleTag *)paletteTag->m_object[i];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (i == 0 && styleTag->m_numParams == 1 &&
Toshihiro Shimizu 890ddd
			strncmp(styleTag->m_param[0].m_string.c_str(), "refimage", 8) == 0) //questo stile contiene l'eventuale refimagepath
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			palette->setRefImgPath(TFilePath(styleTag->m_param[0].m_string.c_str() + 8));
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		assert(styleTag->m_type == PliTag::STYLE_NGOBJ);
Toshihiro Shimizu 890ddd
		int id = styleTag->m_id;
Toshihiro Shimizu 890ddd
		int pageIndex = styleTag->m_pageIndex;
Toshihiro Shimizu 890ddd
		if (!pagesRead && newPli) //quewsto stile contiene le stringhe dei nomi delle pagine della paletta!
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			pagesRead = true;
Toshihiro Shimizu 890ddd
			assert(id == 0 && pageIndex == 0);
Toshihiro Shimizu 890ddd
			assert(palette->getPageCount() == 1);
Toshihiro Shimizu 890ddd
			for (int j = 0; j < styleTag->m_numParams; j++) {
Toshihiro Shimizu 890ddd
				assert(styleTag->m_param[j].m_type == TStyleParam::SP_STRING);
Toshihiro Shimizu 890ddd
				if (j == 0)
Toshihiro Shimizu 890ddd
					palette->getPage(0)->setName(toWideString(styleTag->m_param[j].m_string));
Toshihiro Shimizu 890ddd
				else {
Toshihiro Shimizu 890ddd
					palette->addPage(toWideString(styleTag->m_param[j].m_string));
Toshihiro Shimizu 890ddd
					//palette->getPage(j)->addStyle(TPixel32::Red);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (styleTag->m_numParams == 0) //styletag contiene il frame di una multipalette!
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			frame = styleTag->m_id;
Toshihiro Shimizu 890ddd
			palette->setFrame(frame);
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		TPalette::Page *page = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (pageIndex < 65535) //questo valore pseciale significa che il colore non sta in alcuna pagina
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			page = palette->getPage(pageIndex);
Toshihiro Shimizu 890ddd
			assert(page);
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			continue; // i pli prima salvavano pure i colori non usati...evito di caricarli!
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
		std::vector<tstyleparam> params(styleTag->m_numParams);</tstyleparam>
Toshihiro Shimizu 890ddd
		for (int j = 0; j < styleTag->m_numParams; j++)
Toshihiro Shimizu 890ddd
			params[j] = styleTag->m_param[j];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		PliInputStream chan(¶ms, majorVersion, minorVersion);
Toshihiro Shimizu 890ddd
		TColorStyle *style = TColorStyle::load(chan); //leggo params
Toshihiro Shimizu 890ddd
		assert(id > 0);
Toshihiro Shimizu 890ddd
		if (id < palette->getStyleCount()) {
Toshihiro Shimizu 890ddd
			if (frame > -1) {
Toshihiro Shimizu 890ddd
				TColorStyle *oldStyle = palette->getStyle(id);
Toshihiro Shimizu 890ddd
				oldStyle->copy(*style);
Toshihiro Shimizu 890ddd
				palette->setKeyframe(id, frame);
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				palette->setStyle(id, style);
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			assert(frame == -1); //uno stile animato, ci deve gia' essere nella paletta!
Toshihiro Shimizu 890ddd
			while (palette->getStyleCount() < id)
Toshihiro Shimizu 890ddd
				palette->addStyle(TPixel32::Red);
Toshihiro Shimizu 890ddd
			if (page)
Toshihiro Shimizu 890ddd
				page->addStyle(palette->addStyle(style));
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				palette->addStyle(style);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (id > 0 && page && frame == -1)
Toshihiro Shimizu 890ddd
			page->addStyle(id);
Toshihiro Shimizu 890ddd
		//m = page->getStyleCount();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	palette->setFrame(0);
Toshihiro Shimizu 890ddd
	return palette;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
Carico le informazioni relative al livello
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TLevelReaderPli::doReadPalette(bool doReadIt)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_readPalette = doReadIt;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString TLevelReaderPli::getCreator()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	loadInfo();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_pli)
Toshihiro Shimizu 890ddd
		return m_pli->getCreator();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return "";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLevelP TLevelReaderPli::loadInfo()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_init)
Toshihiro Shimizu 890ddd
		return m_level;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_init = true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//m_level = TLevelP();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//TLevelP level;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// chiudo tutto dentro un blocco try per  cautelarmi
Toshihiro Shimizu 890ddd
	//  dalle eccezioni generate in lettura
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		// alloco l'oggetto parsed
Toshihiro Shimizu 890ddd
		assert(!m_pli);
Toshihiro Shimizu 890ddd
		m_pli = new ParsedPli(getFilePath());
Toshihiro Shimizu 890ddd
		UINT majorVersionNumber, minorVersionNumber;
Toshihiro Shimizu 890ddd
		m_pli->getVersion(majorVersionNumber, minorVersionNumber);
Toshihiro Shimizu 890ddd
		if (majorVersionNumber <= 5 && (majorVersionNumber != 5 || minorVersionNumber < 5))
Toshihiro Shimizu 890ddd
			return m_level;
Toshihiro Shimizu 890ddd
		TPalette *palette = 0;
Toshihiro Shimizu 890ddd
		m_pli->loadInfo(m_readPalette, palette, m_contentHistory);
Toshihiro Shimizu 890ddd
		if (palette)
Toshihiro Shimizu 890ddd
			m_level->setPalette(palette);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (int i = 0; i < m_pli->getFrameCount(); i++)
Toshihiro Shimizu 890ddd
			m_level->setFrame(m_pli->getFrameNumber(i), TVectorImageP());
Toshihiro Shimizu 890ddd
	} catch (std::exception &e) {
Toshihiro Shimizu 890ddd
		TSystem::outputDebug(e.what());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		throw TImageException(getFilePath(), "Unknow error on reading file");
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		throw;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return m_level;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TImageReaderPli::TImageReaderPli(
Toshihiro Shimizu 890ddd
	const TFilePath &f,
Toshihiro Shimizu 890ddd
	const TFrameId &frameId,
Toshihiro Shimizu 890ddd
	TLevelReaderPli *pli)
Toshihiro Shimizu 890ddd
	: TImageReader(f), m_frameId(frameId), m_lrp(pli)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
GroupTag *makeGroup(TVectorImageP &vi, int &currStyleId, int &index, int currDepth)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::vector<pliobjecttag *=""> tags;</pliobjecttag>
Toshihiro Shimizu 890ddd
	int i = index;
Toshihiro Shimizu 890ddd
	while (i < (UINT)vi->getStrokeCount() && vi->getCommonGroupDepth(i, index) >= currDepth) {
Toshihiro Shimizu 890ddd
		int strokeDepth = vi->getGroupDepth(i);
Toshihiro Shimizu 890ddd
		if (strokeDepth == currDepth)
Toshihiro Shimizu 890ddd
			putStroke(vi->getStroke(i++), currStyleId, tags);
Toshihiro Shimizu 890ddd
		else if (strokeDepth > currDepth)
Toshihiro Shimizu 890ddd
			tags.push_back(makeGroup(vi, currStyleId, i, currDepth + 1));
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			assert(false);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	index = i;
Shinya Kitaoka 79e39b
	return new GroupTag(GroupTag::STROKE, tags.size(), tags.data());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif