Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzLib includes
Toshihiro Shimizu 890ddd
#include "toonz/toonzscene.h"
Toshihiro Shimizu 890ddd
#include "toonz/preferences.h"
Toshihiro Shimizu 890ddd
#include "toonz/sceneproperties.h"
Toshihiro Shimizu 890ddd
#include "toutputproperties.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzBase includes
Toshihiro Shimizu 890ddd
#include "tcli.h"
Toshihiro Shimizu 890ddd
#include "tenv.h"
Toshihiro Shimizu 890ddd
#include "tpluginmanager.h"
Toshihiro Shimizu 890ddd
#include "trasterfx.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "tstream.h"
Toshihiro Shimizu 890ddd
#include "tfilepath_io.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
#include "tiio_std.h"
Toshihiro Shimizu 890ddd
#include "timage_io.h"
Toshihiro Shimizu 890ddd
#include "tnzimage.h"
Toshihiro Shimizu 890ddd
#include "tlevel.h"
Toshihiro Shimizu 890ddd
#include "tlevel_io.h"
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
#include "tropcm.h"
Toshihiro Shimizu 890ddd
#include "trasterimage.h"
Toshihiro Shimizu 890ddd
#include "tvectorimage.h"
Toshihiro Shimizu 890ddd
#include "tvectorrenderdata.h"
Toshihiro Shimizu 890ddd
#include "tofflinegl.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
using namespace TCli;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef ArgumentT<tfilepath> FilePathArgument;</tfilepath>
Toshihiro Shimizu 890ddd
typedef QualifierT<tfilepath> FilePathQualifier;</tfilepath>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define RENDER_LICENSE_NOT_FOUND 888
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const char *applicationVersion = "1.0";
Toshihiro Shimizu 890ddd
const char *applicationName = "OpenToonz";
Toshihiro Shimizu 890ddd
const char *rootVarName = "TOONZROOT";
Toshihiro Shimizu 890ddd
const char *systemVarPrefix = "TOONZ";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void doesExist(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	string msg;
Toshihiro Shimizu 890ddd
	TFilePath path = fp.getParentDir() + (fp.getName() + "." + fp.getDottedType());
Toshihiro Shimizu 890ddd
	if (TSystem::doesExistFileOrLevel(fp) || TSystem::doesExistFileOrLevel(path)) {
Toshihiro Shimizu 890ddd
		msg = "File " + fp.getLevelName() + " already exists:";
Toshihiro Shimizu 890ddd
		cout << endl
Toshihiro Shimizu 890ddd
			 << msg << endl;
Toshihiro Shimizu 890ddd
		char answer = ' ';
Toshihiro Shimizu 890ddd
		while (answer != 'Y' && answer != 'N' && answer != 'y' && answer != 'n') {
Toshihiro Shimizu 890ddd
			msg = "do you want to replace it? [Y/N] ";
Toshihiro Shimizu 890ddd
			cout << msg;
Toshihiro Shimizu 890ddd
			cin >> answer;
Toshihiro Shimizu 890ddd
			if (answer == 'N' || answer == 'n') {
Toshihiro Shimizu 890ddd
				msg = "Conversion aborted.";
Toshihiro Shimizu 890ddd
				cout << endl
Toshihiro Shimizu 890ddd
					 << msg << endl;
Toshihiro Shimizu 890ddd
				exit(1);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//Ritorna un vettore contenete i soli TFrameId corrispondenti al range inserito dall'utente
Toshihiro Shimizu 890ddd
vector<tframeid> getFrameIds(const RangeQualifier &range, const TLevelP &level)</tframeid>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	string msg;
Toshihiro Shimizu 890ddd
	TFrameId r0, r1, lastFrame;
Toshihiro Shimizu 890ddd
	TLevel::Iterator begin = level->begin();
Toshihiro Shimizu 890ddd
	TLevel::Iterator end = level->end();
Toshihiro Shimizu 890ddd
	end--;
Toshihiro Shimizu 890ddd
	lastFrame = end->first;
Toshihiro Shimizu 890ddd
	vector<tframeid> frames;</tframeid>
Toshihiro Shimizu 890ddd
	if (range.isSelected()) {
Toshihiro Shimizu 890ddd
		r0 = TFrameId(range.getFrom());
Toshihiro Shimizu 890ddd
		r1 = TFrameId(range.getTo());
Toshihiro Shimizu 890ddd
		if (r0 > r1) {
Toshihiro Shimizu 890ddd
			TFrameId app = r0;
Toshihiro Shimizu 890ddd
			r0 = r1;
Toshihiro Shimizu 890ddd
			r1 = app;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		r0 = begin->first;
Toshihiro Shimizu 890ddd
		r1 = end->first;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	//cerco il primo TFrameId
Toshihiro Shimizu 890ddd
	TLevel::Iterator it = begin;
Toshihiro Shimizu 890ddd
	if (r0 <= end->first)
Toshihiro Shimizu 890ddd
		while (it->first < r0)
Toshihiro Shimizu 890ddd
			++it;
Toshihiro Shimizu 890ddd
	while (it != level->end() && r1 >= it->first) {
Toshihiro Shimizu 890ddd
		//Riempio il vettore fino all'ultimo TFrameId che mi serve
Toshihiro Shimizu 890ddd
		frames.push_back(it->first);
Toshihiro Shimizu 890ddd
		++it;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return frames;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void convertFromCM(const TLevelReaderP &lr, const TPaletteP &plt, const TLevelWriterP &lw, const vector<tframeid> &frames,</tframeid>
Toshihiro Shimizu 890ddd
				   const TAffine &aff, const TRop::ResampleFilterType &resType)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TDimension dim(0, 0); //Serve per controllare che non ci siano frame di diverse dimensioni
Toshihiro Shimizu 890ddd
	for (int i = 0; i < (int)frames.size(); i++) {
Toshihiro Shimizu 890ddd
		try {
Toshihiro Shimizu 890ddd
			TImageReaderP ir = lr->getFrameReader(frames[i]);
Toshihiro Shimizu 890ddd
			TImageP img = ir->load();
Toshihiro Shimizu 890ddd
			TToonzImageP toonzImage(img);
Toshihiro Shimizu 890ddd
			double xdpi, ydpi;
Toshihiro Shimizu 890ddd
			toonzImage->getDpi(xdpi, ydpi);
Toshihiro Shimizu 890ddd
			assert(toonzImage);
Toshihiro Shimizu 890ddd
			if (toonzImage) {
Toshihiro Shimizu 890ddd
				TRasterCM32P rasCMImage = toonzImage->getRaster();
Toshihiro Shimizu 890ddd
				if (i == 0)
Toshihiro Shimizu 890ddd
					dim = rasCMImage->getSize();
Toshihiro Shimizu 890ddd
				else if (dim != rasCMImage->getSize()) {
Toshihiro Shimizu 890ddd
					//dimensioni diverse dei frame
Toshihiro Shimizu 890ddd
					string msg = "Cannot continue to convert: not valid level!";
Toshihiro Shimizu 890ddd
					cout << msg << endl;
Toshihiro Shimizu 890ddd
					exit(1);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				TRaster32P ras(convert(aff * convert(rasCMImage->getBounds())).getSize());
Toshihiro Shimizu 890ddd
				if (!aff.isIdentity())
Toshihiro Shimizu 890ddd
					TRop::resample(ras, rasCMImage, plt, aff, resType);
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					TRop::convert(ras, rasCMImage, plt);
Toshihiro Shimizu 890ddd
				TRasterImageP rasImage(ras);
Toshihiro Shimizu 890ddd
				rasImage->setDpi(xdpi, ydpi);
Toshihiro Shimizu 890ddd
				TImageWriterP iw = lw->getFrameWriter(frames[i]);
Toshihiro Shimizu 890ddd
				iw->save(rasImage);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} catch (...) {
Toshihiro Shimizu 890ddd
			string msg = "Frame " + toString(frames[i].getNumber()) + ": conversion failed!";
Toshihiro Shimizu 890ddd
			cout << msg << endl;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void convertFromVI(const TLevelReaderP &lr, const TPaletteP &plt, const TLevelWriterP &lw, const vector<tframeid> &frames,</tframeid>
Toshihiro Shimizu 890ddd
				   const TRop::ResampleFilterType &resType, int width)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	vector<tvectorimagep> images;</tvectorimagep>
Toshihiro Shimizu 890ddd
	TRectD maxBbox;
Toshihiro Shimizu 890ddd
	TAffine aff;
Toshihiro Shimizu 890ddd
	for (i = 0; i < (int)frames.size(); i++) { //trovo la bbox che possa contenere tutte le immagini
Toshihiro Shimizu 890ddd
		try {
Toshihiro Shimizu 890ddd
			TImageReaderP ir = lr->getFrameReader(frames[i]);
Toshihiro Shimizu 890ddd
			TVectorImageP img = ir->load();
Toshihiro Shimizu 890ddd
			images.push_back(img);
Toshihiro Shimizu 890ddd
			maxBbox += img->getBBox();
Toshihiro Shimizu 890ddd
		} catch (...) {
Toshihiro Shimizu 890ddd
			string msg = "Frame " + toString(frames[i].getNumber()) + ": conversion failed!";
Toshihiro Shimizu 890ddd
			cout << msg << endl;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	maxBbox = maxBbox.enlarge(2);
Toshihiro Shimizu 890ddd
	if (width) //calcolo l'affine
Toshihiro Shimizu 890ddd
		aff = TScale((double)width / maxBbox.getLx());
Toshihiro Shimizu 890ddd
	maxBbox = aff * maxBbox;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < (int)images.size(); i++) {
Toshihiro Shimizu 890ddd
		try {
Toshihiro Shimizu 890ddd
			TVectorImageP vectorImage = images[i];
Toshihiro Shimizu 890ddd
			assert(vectorImage);
Toshihiro Shimizu 890ddd
			if (vectorImage) {
Toshihiro Shimizu 890ddd
				//faccio il render dell'immagine
Toshihiro Shimizu 890ddd
				vectorImage->transform(aff, true);
Toshihiro Shimizu 890ddd
				const TVectorRenderData rd(TTranslation(-maxBbox.getP00()), TRect(), plt.getPointer(), 0, true, true);
Toshihiro Shimizu 890ddd
				TOfflineGL *glContext = new TOfflineGL(convert(maxBbox).getSize());
Toshihiro Shimizu 890ddd
				glContext->clear(TPixel32::Transparent);
Toshihiro Shimizu 890ddd
				glContext->draw(vectorImage, rd);
Toshihiro Shimizu 890ddd
				TRaster32P rasImage = (glContext->getRaster());
Toshihiro Shimizu 890ddd
				TImageWriterP iw = lw->getFrameWriter(frames[i]);
Toshihiro Shimizu 890ddd
				iw->save(TRasterImageP(rasImage));
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} catch (...) {
Toshihiro Shimizu 890ddd
			string msg = "Frame " + frames[i].expand() + ": conversion failed!";
Toshihiro Shimizu 890ddd
			cout << msg << endl;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void convertFromFullRaster(const TLevelReaderP &lr, const TLevelWriterP &lw, const vector<tframeid> &frames,</tframeid>
Toshihiro Shimizu 890ddd
						   const TAffine &aff, const TRop::ResampleFilterType &resType)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (int i = 0; i < (int)frames.size(); i++) {
Toshihiro Shimizu 890ddd
		try {
Toshihiro Shimizu 890ddd
			TImageReaderP ir = lr->getFrameReader(frames[i]);
Toshihiro Shimizu 890ddd
			TRasterImageP img = ir->load();
Toshihiro Shimizu 890ddd
			TRaster32P raster(convert(aff * img->getBBox()).getSize());
Toshihiro Shimizu 890ddd
			if (!aff.isIdentity())
Toshihiro Shimizu 890ddd
				TRop::resample(raster, img->getRaster(), aff, resType);
Toshihiro Shimizu 890ddd
			else {
Toshihiro Shimizu 890ddd
				if ((TRaster32P)img->getRaster())
Toshihiro Shimizu 890ddd
					raster = img->getRaster();
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					TRop::convert(raster, img->getRaster());
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			TImageWriterP iw = lw->getFrameWriter(frames[i]);
Toshihiro Shimizu 890ddd
			iw->save(TRasterImageP(raster));
Toshihiro Shimizu 890ddd
		} catch (...) {
Toshihiro Shimizu 890ddd
			string msg = "Frame " + frames[i].expand() + ": conversion failed!";
Toshihiro Shimizu 890ddd
			cout << msg << endl;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void convertFromFullRasterToCm(const TLevelReaderP &lr, const TLevelWriterP &lw, const vector<tframeid> &frames,</tframeid>
Toshihiro Shimizu 890ddd
							   const TAffine &aff, const TRop::ResampleFilterType &resType)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPalette *plt = new TPalette();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < (int)frames.size(); i++) {
Toshihiro Shimizu 890ddd
		try {
Toshihiro Shimizu 890ddd
			TImageReaderP ir = lr->getFrameReader(frames[i]);
Toshihiro Shimizu 890ddd
			TRasterImageP img = ir->load();
Toshihiro Shimizu 890ddd
			double dpix, dpiy;
Toshihiro Shimizu 890ddd
			img->getDpi(dpix, dpiy);
Toshihiro Shimizu 890ddd
			if (dpix == 0 && dpiy == 0)
Toshihiro Shimizu 890ddd
				dpix = dpiy = Preferences::instance()->getDefLevelDpi();
Toshihiro Shimizu 890ddd
			TRasterCM32P raster(convert(aff * img->getBBox()).getSize());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (!aff.isIdentity()) {
Toshihiro Shimizu 890ddd
				TRaster32P raux(raster->getSize());
Toshihiro Shimizu 890ddd
				TRop::resample(raux, img->getRaster(), aff, resType);
Toshihiro Shimizu 890ddd
				TRop::convert(raster, raux);
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				TRop::convert(raster, img->getRaster());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TImageWriterP iw = lw->getFrameWriter(frames[i]);
Toshihiro Shimizu 890ddd
			TToonzImageP outimg(raster, raster->getBounds());
Toshihiro Shimizu 890ddd
			outimg->setDpi(dpix, dpiy);
Toshihiro Shimizu 890ddd
			outimg->setPalette(plt);
Toshihiro Shimizu 890ddd
			iw->save(outimg);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		} catch (...) {
Toshihiro Shimizu 890ddd
			string msg = "Frame " + frames[i].expand() + ": conversion failed!";
Toshihiro Shimizu 890ddd
			cout << msg << endl;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath pltPath = lw->getFilePath().withNoFrame().withType("tpl");
Toshihiro Shimizu 890ddd
	if (TSystem::touchParentDir(pltPath)) {
Toshihiro Shimizu 890ddd
		if (TSystem::doesExistFileOrLevel(pltPath))
Toshihiro Shimizu 890ddd
			TSystem::removeFileOrLevel(pltPath);
Toshihiro Shimizu 890ddd
		TOStream os(pltPath);
Toshihiro Shimizu 890ddd
		os << plt;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void convert(const TFilePath &source, const TFilePath &dest, const RangeQualifier &range,
Toshihiro Shimizu 890ddd
			 const IntQualifier &width, TPropertyGroup *prop,
Toshihiro Shimizu 890ddd
			 const TRenderSettings::ResampleQuality &resQuality)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	string msg;
Toshihiro Shimizu 890ddd
	//Carico le informazione del livello
Toshihiro Shimizu 890ddd
	TLevelReaderP lr(source);
Toshihiro Shimizu 890ddd
	TLevelP level = lr->loadInfo();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Trovo i TFrameId corrispondenti al range
Toshihiro Shimizu 890ddd
	vector<tframeid> frames = getFrameIds(range, level);</tframeid>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	doesExist(dest);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	msg = "Level loaded";
Toshihiro Shimizu 890ddd
	cout << msg << endl;
Toshihiro Shimizu 890ddd
	msg = "Conversion in progress: wait please...";
Toshihiro Shimizu 890ddd
	cout << msg << endl;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TAffine aff;
Toshihiro Shimizu 890ddd
	if (width.isSelected()) {
Toshihiro Shimizu 890ddd
		//calcolo un affine per fare la resample
Toshihiro Shimizu 890ddd
		int imgLx = lr->getImageInfo()->m_lx;
Toshihiro Shimizu 890ddd
		aff = TScale((double)width / (double)imgLx);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//setto il FilterResempleType giusto
Toshihiro Shimizu 890ddd
	TRop::ResampleFilterType resType;
Toshihiro Shimizu 890ddd
	if (resQuality == TRenderSettings::StandardResampleQuality)
Toshihiro Shimizu 890ddd
		resType = TRop::Triangle;
Toshihiro Shimizu 890ddd
	else if (resQuality == TRenderSettings::ImprovedResampleQuality)
Toshihiro Shimizu 890ddd
		resType = TRop::Hann2;
Toshihiro Shimizu 890ddd
	else if (resQuality == TRenderSettings::HighResampleQuality)
Toshihiro Shimizu 890ddd
		resType = TRop::Hamming3;
Toshihiro Shimizu 890ddd
	else if (resQuality == TRenderSettings::Triangle_FilterResampleQuality)
Toshihiro Shimizu 890ddd
		resType = TRop::Triangle;
Toshihiro Shimizu 890ddd
	else if (resQuality == TRenderSettings::Mitchell_FilterResampleQuality)
Toshihiro Shimizu 890ddd
		resType = TRop::Mitchell;
Toshihiro Shimizu 890ddd
	else if (resQuality == TRenderSettings::Cubic5_FilterResampleQuality)
Toshihiro Shimizu 890ddd
		resType = TRop::Cubic5;
Toshihiro Shimizu 890ddd
	else if (resQuality == TRenderSettings::Cubic75_FilterResampleQuality)
Toshihiro Shimizu 890ddd
		resType = TRop::Cubic75;
Toshihiro Shimizu 890ddd
	else if (resQuality == TRenderSettings::Cubic1_FilterResampleQuality)
Toshihiro Shimizu 890ddd
		resType = TRop::Cubic1;
Toshihiro Shimizu 890ddd
	else if (resQuality == TRenderSettings::Hann2_FilterResampleQuality)
Toshihiro Shimizu 890ddd
		resType = TRop::Hann2;
Toshihiro Shimizu 890ddd
	else if (resQuality == TRenderSettings::Hann3_FilterResampleQuality)
Toshihiro Shimizu 890ddd
		resType = TRop::Hann3;
Toshihiro Shimizu 890ddd
	else if (resQuality == TRenderSettings::Hamming2_FilterResampleQuality)
Toshihiro Shimizu 890ddd
		resType = TRop::Hamming2;
Toshihiro Shimizu 890ddd
	else if (resQuality == TRenderSettings::Hamming3_FilterResampleQuality)
Toshihiro Shimizu 890ddd
		resType = TRop::Hamming3;
Toshihiro Shimizu 890ddd
	else if (resQuality == TRenderSettings::Lanczos2_FilterResampleQuality)
Toshihiro Shimizu 890ddd
		resType = TRop::Lanczos2;
Toshihiro Shimizu 890ddd
	else if (resQuality == TRenderSettings::Lanczos3_FilterResampleQuality)
Toshihiro Shimizu 890ddd
		resType = TRop::Lanczos3;
Toshihiro Shimizu 890ddd
	else if (resQuality == TRenderSettings::Gauss_FilterResampleQuality)
Toshihiro Shimizu 890ddd
		resType = TRop::Gauss;
Toshihiro Shimizu 890ddd
	else if (resQuality == TRenderSettings::ClosestPixel_FilterResampleQuality)
Toshihiro Shimizu 890ddd
		resType = TRop::ClosestPixel;
Toshihiro Shimizu 890ddd
	else if (resQuality == TRenderSettings::Bilinear_FilterResampleQuality)
Toshihiro Shimizu 890ddd
		resType = TRop::Bilinear;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	string ext = source.getType();
Toshihiro Shimizu 890ddd
	TLevelWriterP lw(dest, prop);
Toshihiro Shimizu 890ddd
	if (ext != "tlv" && ext != "pli") {
Toshihiro Shimizu 890ddd
		if (dest.getType() == "tlv")
Toshihiro Shimizu 890ddd
			convertFromFullRasterToCm(lr, lw, frames, aff, resType);
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			convertFromFullRaster(lr, lw, frames, aff, resType);
Toshihiro Shimizu 890ddd
	} else if (ext == "tlv") //ToonzImage
Toshihiro Shimizu 890ddd
		convertFromCM(lr, level->getPalette(), lw, frames, aff, resType);
Toshihiro Shimizu 890ddd
	else if (ext == "pli") //VectorImage
Toshihiro Shimizu 890ddd
		convertFromVI(lr, level->getPalette(), lw, frames, resType, width.getValue());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int main(int argc, char *argv[])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TEnv::setApplication(applicationName, applicationVersion);
Toshihiro Shimizu 890ddd
	TEnv::setRootVarName(rootVarName);
Toshihiro Shimizu 890ddd
	TEnv::setSystemVarPrefix(systemVarPrefix);
Toshihiro Shimizu 890ddd
	TFilePath fp = TEnv::getStuffDir();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	string msg;
Toshihiro Shimizu 890ddd
	//Inizializzo i qualificatori
Toshihiro Shimizu 890ddd
	TCli::FilePathArgument srcName("srcName", "Source file");
Toshihiro Shimizu 890ddd
	TCli::FilePathArgument dstName("dstName", "Target file");
Toshihiro Shimizu 890ddd
	FilePathQualifier tnzName("-s sceneName", "Scene file");
Toshihiro Shimizu 890ddd
	RangeQualifier range;
Toshihiro Shimizu 890ddd
	IntQualifier width("-w width", "Image width");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Usage usage(argv[0]);
Toshihiro Shimizu 890ddd
	usage.add(srcName + dstName + width + tnzName + range);
Toshihiro Shimizu 890ddd
	if (!usage.parse(argc, argv))
Toshihiro Shimizu 890ddd
		exit(1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		Tiio::defineStd();
Toshihiro Shimizu 890ddd
		//TPluginManager::instance()->loadStandardPlugins();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TSystem::hasMainLoop(false);
Toshihiro Shimizu 890ddd
		TPropertyGroup *prop = 0;
Toshihiro Shimizu 890ddd
		initImageIo();
Toshihiro Shimizu 890ddd
		TRenderSettings::ResampleQuality resQuality = TRenderSettings::StandardResampleQuality;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TFilePath dstFilePath = dstName.getValue();
Toshihiro Shimizu 890ddd
		TFilePath srcFilePath = srcName.getValue();
Toshihiro Shimizu 890ddd
		if (!TSystem::doesExistFileOrLevel(srcFilePath)) {
Toshihiro Shimizu 890ddd
			msg = srcFilePath.getLevelName() + " level doesn't exist.";
Toshihiro Shimizu 890ddd
			cout << endl
Toshihiro Shimizu 890ddd
				 << msg << endl;
Toshihiro Shimizu 890ddd
			exit(1);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		msg = "Loading " + srcFilePath.getLevelName();
Toshihiro Shimizu 890ddd
		cout << endl
Toshihiro Shimizu 890ddd
			 << msg << endl;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		string ext = dstFilePath.getType();
Toshihiro Shimizu 890ddd
		//controllo che ci sia un'estensione
Toshihiro Shimizu 890ddd
		if (ext == "") {
Toshihiro Shimizu 890ddd
			ext = toString(dstFilePath.getWideString());
Toshihiro Shimizu 890ddd
			if (ext == "") {
Toshihiro Shimizu 890ddd
				msg = "Invalid extension!";
Toshihiro Shimizu 890ddd
				cout << msg << endl;
Toshihiro Shimizu 890ddd
				exit(1);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (dstFilePath.getParentDir().isEmpty()) //ho specificato solo l'estensione
Toshihiro Shimizu 890ddd
			dstFilePath = srcFilePath.getParentDir() + (srcFilePath.getName() + "." + ext);
Toshihiro Shimizu 890ddd
		if (tnzName.isSelected()) {
Toshihiro Shimizu 890ddd
			//Devo prendermi i settaggi degli "output setting" dalla scena!
Toshihiro Shimizu 890ddd
			TFilePath tnzFilePath = tnzName.getValue();
Toshihiro Shimizu 890ddd
			if (tnzFilePath.getType() != "tnz") {
Toshihiro Shimizu 890ddd
				msg = "Invalid scene file: conversion terminated!";
Toshihiro Shimizu 890ddd
				cout << msg << endl;
Toshihiro Shimizu 890ddd
				exit(1);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (!TSystem::doesExistFileOrLevel(tnzFilePath))
Toshihiro Shimizu 890ddd
				return false;
Toshihiro Shimizu 890ddd
			ToonzScene *scene = new ToonzScene();
Toshihiro Shimizu 890ddd
			try {
Toshihiro Shimizu 890ddd
				scene->loadTnzFile(tnzFilePath);
Toshihiro Shimizu 890ddd
			} catch (...) {
Toshihiro Shimizu 890ddd
				string msg;
Toshihiro Shimizu 890ddd
				msg = "There were problems loading the scene " + toString(srcFilePath.getWideString()) +
Toshihiro Shimizu 890ddd
					  ".\n Some files may be missing.";
Toshihiro Shimizu 890ddd
				cout << msg << endl;
Toshihiro Shimizu 890ddd
				//return false;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (scene) {
Toshihiro Shimizu 890ddd
				resQuality = scene->getProperties()->getOutputProperties()->getRenderSettings().m_quality;
Toshihiro Shimizu 890ddd
				prop = scene->getProperties()->getOutputProperties()->getFileFormatProperties(ext);
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				msg = "Invalid scene file: conversion terminated!";
Toshihiro Shimizu 890ddd
				cout << msg << endl;
Toshihiro Shimizu 890ddd
				exit(1);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (ext != "3gp" && ext != "pli") {
Toshihiro Shimizu 890ddd
			//assert(ext!="3gp" && ext!="pli" && ext!="tlv");
Toshihiro Shimizu 890ddd
			convert(srcFilePath, dstFilePath, range, width, prop, resQuality);
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			msg = "Cannot convert to ." + ext + " format.";
Toshihiro Shimizu 890ddd
			cout << msg << endl;
Toshihiro Shimizu 890ddd
			exit(1);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} catch (TException &e) {
Toshihiro Shimizu 890ddd
		msg = "Untrapped exception: " + toString(e.getMessage());
Toshihiro Shimizu 890ddd
		cout << msg << endl;
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	msg = "Conversion terminated!";
Toshihiro Shimizu 890ddd
	cout << endl
Toshihiro Shimizu 890ddd
		 << msg << endl;
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}