Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzFarm includes
Toshihiro Shimizu 890ddd
#include "tfarmcontroller.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzLib includes
Toshihiro Shimizu 890ddd
#include "toonz/txshleveltypes.h"
Toshihiro Shimizu 890ddd
#include "toonz/tpalettehandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/tscenehandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshlevelhandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/sceneproperties.h"
Toshihiro Shimizu 890ddd
#include "toonz/levelproperties.h"
Toshihiro Shimizu 890ddd
#include "toonz/levelupdater.h"
Toshihiro Shimizu 890ddd
#include "toonz/preferences.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzfolders.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzscene.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshchildlevel.h"
Toshihiro Shimizu 890ddd
#include "toonz/tproject.h"
Toshihiro Shimizu 890ddd
#include "toonz/tcleanupper.h"
Toshihiro Shimizu 890ddd
#include "toonz/txsheet.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshcell.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshcolumn.h"
Toshihiro Shimizu 890ddd
#include "toonz/tlog.h"
Toshihiro Shimizu 890ddd
#include "toonz/imagestyles.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzBase includes
Toshihiro Shimizu 890ddd
#include "tcli.h"
Toshihiro Shimizu 890ddd
#include "tenv.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 "tstopwatch.h"
Toshihiro Shimizu 890ddd
#include "tthread.h"
Toshihiro Shimizu 890ddd
#include "tthreadmessage.h"
Toshihiro Shimizu 890ddd
#include "timagecache.h"
Toshihiro Shimizu 890ddd
#include "tiio_std.h"
Toshihiro Shimizu 890ddd
#include "tnzimage.h"
Toshihiro Shimizu 890ddd
#include "tmsgcore.h"
Toshihiro Shimizu 890ddd
#include "tpluginmanager.h"
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
#include "tsimplecolorstyles.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Qt includes
Toshihiro Shimizu 890ddd
#include <qapplication></qapplication>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace TCli;
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline ostream &operator<<(ostream &out, const wstring &w)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return out << toString(w);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline ostream &operator<<(ostream &out, const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return out << fp.getWideString();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const char *applicationName = "OpenToonz";
Toshihiro Shimizu 890ddd
const char *applicationVersion = "1.0";
Toshihiro Shimizu 890ddd
const char *applicationFullName = "OpenToonz 1.0";
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
CleanupParameters GlobalParameters;
Toshihiro Shimizu 890ddd
TFilePath CurrentSettingsFile = TFilePath();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void loadSettings(const TFilePath &settingsFile, CleanupParameters *cp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (CurrentSettingsFile == TFilePath()) //the parameters were global ...I store them
Toshihiro Shimizu 890ddd
		GlobalParameters.assign(cp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	CurrentSettingsFile = settingsFile;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TIStream *is = new TIStream(settingsFile);
Toshihiro Shimizu 890ddd
	int minor, major;
Toshihiro Shimizu 890ddd
	string tagName;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Extract file version if any
Toshihiro Shimizu 890ddd
	is->matchTag(tagName);
Toshihiro Shimizu 890ddd
	if (tagName == "version") {
Toshihiro Shimizu 890ddd
		*is >> major >> minor;
Toshihiro Shimizu 890ddd
		is->matchEndTag();
Toshihiro Shimizu 890ddd
		is->setVersion(VersionNumber(major, minor));
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		delete is;
Toshihiro Shimizu 890ddd
		is = new TIStream(settingsFile);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	cp->loadData(*is, true);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	delete is;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void restoreGlobalSettings(CleanupParameters *cp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (CurrentSettingsFile == TFilePath())
Toshihiro Shimizu 890ddd
		return; //already global!
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	cp->assign(&GlobalParameters);
Toshihiro Shimizu 890ddd
	CurrentSettingsFile = TFilePath();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//========================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// fatalError
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void fatalError(string msg)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
	msg = "Application can't start:\n" + msg;
Shinya Kitaoka eabf18
	DVGui::error(QString::fromStdString(msg));
Toshihiro Shimizu 890ddd
	//MessageBox(0,msg.c_str(),"Fatal error",MB_ICONERROR);
Toshihiro Shimizu 890ddd
	exit(1);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	// TODO: Come si fa ad aggiungere un messaggio di errore qui?
Toshihiro Shimizu 890ddd
	std::cout << msg << std::endl;
Toshihiro Shimizu 890ddd
	abort();
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef MACOSX
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline bool isBlank(char c)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return c == ' ' || c == '\t' || c == '\n';
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//========================================================================
Toshihiro Shimizu 890ddd
// setToonzFolder
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Ritorna il path della variabile passata come secondo argomento
Toshihiro Shimizu 890ddd
// entrambe vengono lette da un file di testo (filename).
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath setToonzFolder(const TFilePath &filename, std::string toonzVar)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Tifstream is(filename);
Toshihiro Shimizu 890ddd
	if (!is)
Toshihiro Shimizu 890ddd
		return TFilePath();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	char buffer[1024];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (is.getline(buffer, sizeof(buffer))) {
Toshihiro Shimizu 890ddd
		// le righe dentro toonzenv.txt sono del tipo
Toshihiro Shimizu 890ddd
		// export set TOONZPROJECT="....."
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// devo trovare la linea che contiene toonzVar
Toshihiro Shimizu 890ddd
		char *s = buffer;
Toshihiro Shimizu 890ddd
		while (isBlank(*s))
Toshihiro Shimizu 890ddd
			s++;
Toshihiro Shimizu 890ddd
		// Se la riga  vuota, o inizia per # o ! salto alla prossima
Toshihiro Shimizu 890ddd
		if (*s == '\0' || *s == '#' || *s == '!')
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		if (*s == '=')
Toshihiro Shimizu 890ddd
			continue; // errore: nome variabile non c'
Toshihiro Shimizu 890ddd
		char *t = s;
Toshihiro Shimizu 890ddd
		// Mi prendo la sottoStringa fino all'=
Toshihiro Shimizu 890ddd
		while (*t && *t != '=')
Toshihiro Shimizu 890ddd
			t++;
Toshihiro Shimizu 890ddd
		if (*t != '=')
Toshihiro Shimizu 890ddd
			continue; // errore: manca '='
Toshihiro Shimizu 890ddd
		char *q = t;
Toshihiro Shimizu 890ddd
		// Torno indietro fino al primo blank
Toshihiro Shimizu 890ddd
		while (q > s && !isBlank(*(q - 1)))
Toshihiro Shimizu 890ddd
			q--;
Toshihiro Shimizu 890ddd
		if (q == s)
Toshihiro Shimizu 890ddd
			continue; // non dovrebbe mai succedere: prima di '=' tutti blanks
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		string toonzVarString(q, t);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// Confronto la stringa trovata con toonzVar, se   lei vado avanti.
Toshihiro Shimizu 890ddd
		if (toonzVar != toonzVarString)
Toshihiro Shimizu 890ddd
			continue; // errore: stringhe diverse
Toshihiro Shimizu 890ddd
		s = t + 1;
Toshihiro Shimizu 890ddd
		// Salto gli spazi
Toshihiro Shimizu 890ddd
		while (isBlank(*s))
Toshihiro Shimizu 890ddd
			s++;
Toshihiro Shimizu 890ddd
		if (*s == '\0')
Toshihiro Shimizu 890ddd
			continue; //errore: dst vuoto
Toshihiro Shimizu 890ddd
		t = s;
Toshihiro Shimizu 890ddd
		while (*t)
Toshihiro Shimizu 890ddd
			t++;
Toshihiro Shimizu 890ddd
		while (t > s && isBlank(*(t - 1)))
Toshihiro Shimizu 890ddd
			t--;
Toshihiro Shimizu 890ddd
		if (t == s)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		// ATTENZIONE : tolgo le virgolette !!
Toshihiro Shimizu 890ddd
		string pathName(s + 1, t - s - 2);
Toshihiro Shimizu 890ddd
		return TFilePath(pathName);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TFilePath();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void prepareToCleanup(TXshSimpleLevel *xl, TPalette *cleanupPalette)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(xl->getScene());
Toshihiro Shimizu 890ddd
	if (xl->getProperties()->getSubsampling() != 1) {
Toshihiro Shimizu 890ddd
		xl->getProperties()->setSubsampling(1);
Toshihiro Shimizu 890ddd
		xl->invalidateFrames();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* int ltype = xl->getType();
Toshihiro Shimizu 890ddd
    if(ltype != TZP_XSHLEVEL) 
Toshihiro Shimizu 890ddd
    xl->makeTlv(xl->getScene()->getDefaultParentDir(TZP_XSHLEVEL));
Toshihiro Shimizu 890ddd
  if(xl->getPalette()==0)
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
    //PaletteController* pc = TApp::instance()->getPaletteController();
Toshihiro Shimizu 890ddd
		xl->setPalette(cleanupPalette);
Toshihiro Shimizu 890ddd
		//if(xl == TApp::instance()->getCurrentLevel()->getLevel())
Toshihiro Shimizu 890ddd
		//	pc->getCurrentLevelPalette()->setPalette(xl->getPalette());
Toshihiro Shimizu 890ddd
    }*/
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
TStopWatch Sw1;
Toshihiro Shimizu 890ddd
TStopWatch Sw2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool UseRenderFarm = false;
Toshihiro Shimizu 890ddd
string FarmControllerName;
Toshihiro Shimizu 890ddd
int FarmControllerPort;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFarmController *FarmController = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
string TaskId;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//========================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// searchLevelsToCleanup
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// Restituisce l'elenco (ordinato per ordine alfabetico di nome)
Toshihiro Shimizu 890ddd
// dei livelli TLV(con scannedPath)/TZI/OVL presenti nell'xsheet (o nei sottoxsheet)
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// se selectedOnly = true salta i livelli presenti nelle colonne senza "occhietto"
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void searchLevelsToCleanup(
Toshihiro Shimizu 890ddd
	std::vector<std::pair<txshsimplelevel *,="" std::set<tframeid="">>> &levels,</std::pair<txshsimplelevel>
Toshihiro Shimizu 890ddd
	TXsheet *xsh,
Toshihiro Shimizu 890ddd
	bool selectedOnly)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	std::map<wstring, *="" txshsimplelevel=""> levelTable;</wstring,>
Toshihiro Shimizu 890ddd
	std::map<wstring, std::set<tframeid="">> framesTable;</wstring,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::set<txsheet *=""> visited;</txsheet>
Toshihiro Shimizu 890ddd
	std::vector<txsheet *=""> xsheets;</txsheet>
Toshihiro Shimizu 890ddd
	xsheets.push_back(xsh);
Toshihiro Shimizu 890ddd
	visited.insert(xsh);
Toshihiro Shimizu 890ddd
	while (!xsheets.empty()) {
Toshihiro Shimizu 890ddd
		TXsheet *xsh = xsheets.back();
Toshihiro Shimizu 890ddd
		xsheets.pop_back();
Toshihiro Shimizu 890ddd
		for (int c = 0; c < xsh->getColumnCount(); c++) {
Toshihiro Shimizu 890ddd
			TXshColumn *column = xsh->getColumn(c);
Toshihiro Shimizu 890ddd
			if (!column || column->isEmpty())
Toshihiro Shimizu 890ddd
				continue;
Toshihiro Shimizu 890ddd
			//if(selectedOnly && !column->isPreviewVisible()) continue;
Toshihiro Shimizu 890ddd
			int r0 = 0, r1 = -1;
Toshihiro Shimizu 890ddd
			xsh->getCellRange(c, r0, r1);
Toshihiro Shimizu 890ddd
			for (int r = r0; r <= r1; r++) {
Toshihiro Shimizu 890ddd
				TXshCell cell = xsh->getCell(r, c);
Toshihiro Shimizu 890ddd
				if (cell.isEmpty())
Toshihiro Shimizu 890ddd
					continue;
Toshihiro Shimizu 890ddd
				if (TXshSimpleLevel *sl = cell.m_level->getSimpleLevel()) {
Toshihiro Shimizu 890ddd
					if (selectedOnly && !column->isPreviewVisible()) //pezza: se questo "if" veniva fatto sopra(riga commentata)
Toshihiro Shimizu 890ddd
																	 // quando si fa save della Scene alla fine della cleanuppata,
Toshihiro Shimizu 890ddd
																	 //venivano backuppate le tif non processate e cancellati gli originali!
Toshihiro Shimizu 890ddd
																	 //Deliri del levelUPdater; evito di mettere le mani in quel pattume. vinz
Toshihiro Shimizu 890ddd
					{
Toshihiro Shimizu 890ddd
						sl->setDirtyFlag(false);
Toshihiro Shimizu 890ddd
						continue;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					int ltype = sl->getType();
Toshihiro Shimizu 890ddd
					if (ltype == TZP_XSHLEVEL && sl->getScannedPath() != TFilePath() || ltype == OVL_XSHLEVEL || ltype == TZI_XSHLEVEL) {
Toshihiro Shimizu 890ddd
						wstring levelName = sl->getName();
Toshihiro Shimizu 890ddd
						levelTable[levelName] = sl;
Toshihiro Shimizu 890ddd
						framesTable[levelName].insert(cell.m_frameId);
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				} else if (TXshChildLevel *cl = cell.m_level->getChildLevel()) {
Toshihiro Shimizu 890ddd
					TXsheet *subXsh = cl->getXsheet();
Toshihiro Shimizu 890ddd
					if (visited.count(subXsh) == 0) {
Toshihiro Shimizu 890ddd
						visited.insert(subXsh);
Toshihiro Shimizu 890ddd
						xsheets.push_back(subXsh);
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	assert(levelTable.size() == framesTable.size());
Toshihiro Shimizu 890ddd
	std::map<wstring, *="" txshsimplelevel="">::iterator it1 = levelTable.begin();</wstring,>
Toshihiro Shimizu 890ddd
	std::map<wstring, std::set<tframeid="">>::iterator it2 = framesTable.begin();</wstring,>
Toshihiro Shimizu 890ddd
	for (it1; it1 != levelTable.end(); ++it1, ++it2)
Toshihiro Shimizu 890ddd
		levels.push_back(std::make_pair(it1->second, it2->second));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*- CleanupDefaultパレットを追加する -*/
Toshihiro Shimizu 890ddd
void addCleanupDefaultPalette(TXshSimpleLevel *sl)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/*- 元となるパレットはStudioPaletteフォルダに置く -*/
Toshihiro Shimizu 890ddd
	TFilePath palettePath = ToonzFolder::getStudioPaletteFolder() + "cleanup_default.tpl";
Toshihiro Shimizu 890ddd
	TFileStatus pfs(palettePath);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!pfs.doesExist() || !pfs.isReadable()) {
Toshihiro Shimizu 890ddd
		wcout << L"CleanupDefaultPalette file: " << palettePath.getWideString() << L" is not found!" << endl;
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TIStream is(palettePath);
Toshihiro Shimizu 890ddd
	if (!is) {
Toshihiro Shimizu 890ddd
		cout << "CleanupDefaultPalette file: failed to get TIStream" << endl;
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	string tagName;
Toshihiro Shimizu 890ddd
	if (!is.matchTag(tagName) || tagName != "palette") {
Toshihiro Shimizu 890ddd
		cout << "CleanupDefaultPalette file: This is not palette file" << endl;
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	string gname;
Toshihiro Shimizu 890ddd
	is.getTagParam("name", gname);
Toshihiro Shimizu 890ddd
	TPalette *defaultPalette = new TPalette();
Toshihiro Shimizu 890ddd
	defaultPalette->loadData(is);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	sl->getPalette()->setIsCleanupPalette(false);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPalette::Page *dstPage = sl->getPalette()->getPage(0);
Toshihiro Shimizu 890ddd
	TPalette::Page *srcPage = defaultPalette->getPage(0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int srcIndexInPage = 0; srcIndexInPage < srcPage->getStyleCount(); srcIndexInPage++) {
Toshihiro Shimizu 890ddd
		int id = srcPage->getStyleId(srcIndexInPage);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		bool isUsedInCleanupPalette;
Toshihiro Shimizu 890ddd
		isUsedInCleanupPalette = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (int dstIndexInPage = 0; dstIndexInPage < dstPage->getStyleCount(); dstIndexInPage++) {
Toshihiro Shimizu 890ddd
			if (dstPage->getStyleId(dstIndexInPage) == id) {
Toshihiro Shimizu 890ddd
				isUsedInCleanupPalette = true;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (isUsedInCleanupPalette)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			int addedId = sl->getPalette()->addStyle(srcPage->getStyle(srcIndexInPage)->clone());
Toshihiro Shimizu 890ddd
			dstPage->addStyle(addedId);
Toshihiro Shimizu 890ddd
			/*- StudioPalette由来のDefaultPaletteの場合、GrobalNameを消去する -*/
Toshihiro Shimizu 890ddd
			sl->getPalette()->getStyle(addedId)->setGlobalName(L"");
Toshihiro Shimizu 890ddd
			sl->getPalette()->getStyle(addedId)->setOriginalName(L"");
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	delete defaultPalette;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//========================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// cleanupLevel
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// effettua il cleanup del livello. Se il livello e' un fullcolor
Toshihiro Shimizu 890ddd
// modifica tipo e parametri e crea la palette
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// se overwrite == false non fa il cleanup dei frames gia' cleanuppati
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void cleanupLevel(TXshSimpleLevel *xl, std::set<tframeid> fidsInXsheet,</tframeid>
Toshihiro Shimizu 890ddd
				  ToonzScene *scene, bool overwrite, TUserLogAppend &m_userLog)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	prepareToCleanup(xl, scene->getProperties()->getCleanupParameters()->m_cleanupPalette.getPointer());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TCleanupper *cl = TCleanupper::instance();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath fp = scene->decodeFilePath(xl->getPath());
Toshihiro Shimizu 890ddd
	TSystem::touchParentDir(fp);
Toshihiro Shimizu 890ddd
	cout << "cleanupping " << xl->getName() << " path=" << fp << endl;
Toshihiro Shimizu 890ddd
	string info = "cleanupping " + toString(xl->getPath());
Toshihiro Shimizu 890ddd
	LevelUpdater updater(xl);
Toshihiro Shimizu 890ddd
	m_userLog.info(info);
Shinya Kitaoka eabf18
	DVGui::info(QString::fromStdString(info));
Toshihiro Shimizu 890ddd
	bool firstImage = true;
Toshihiro Shimizu 890ddd
	std::set<tframeid>::iterator it = fidsInXsheet.begin();</tframeid>
Toshihiro Shimizu 890ddd
	for (it; it != fidsInXsheet.end(); it++) {
Toshihiro Shimizu 890ddd
		TFrameId fid = *it;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		cout << "  " << fid << endl;
Toshihiro Shimizu 890ddd
		info = "  " + fid.expand();
Toshihiro Shimizu 890ddd
		m_userLog.info(info);
Toshihiro Shimizu 890ddd
		int status = xl->getFrameStatus(fid);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (0 != (status & TXshSimpleLevel::Cleanupped) && !overwrite) {
Toshihiro Shimizu 890ddd
			cout << "  skipped" << endl;
Toshihiro Shimizu 890ddd
			m_userLog.info("  skipped");
Shinya Kitaoka eabf18
			DVGui::info(QString("--skipped frame ") + QString::fromStdString(fid.expand()));
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		TRasterImageP original = xl->getFrameToCleanup(fid);
Toshihiro Shimizu 890ddd
		if (!original) {
Toshihiro Shimizu 890ddd
			string err = "    *error* missed frame";
Toshihiro Shimizu 890ddd
			m_userLog.error(err);
Toshihiro Shimizu 890ddd
			cout << err << endl;
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		CleanupParameters *params = scene->getProperties()->getCleanupParameters();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (params->m_lineProcessingMode == lpNone) {
Toshihiro Shimizu 890ddd
			TRasterImageP ri;
Toshihiro Shimizu 890ddd
			if (params->m_autocenterType == CleanupTypes::AUTOCENTER_NONE)
Toshihiro Shimizu 890ddd
				ri = original;
Toshihiro Shimizu 890ddd
			else {
Toshihiro Shimizu 890ddd
				bool autocentered;
Toshihiro Shimizu 890ddd
				ri = cl->autocenterOnly(original, false, autocentered);
Toshihiro Shimizu 890ddd
				if (!autocentered) {
Toshihiro Shimizu 890ddd
					m_userLog.error("The autocentering failed on the current drawing.");
Toshihiro Shimizu 890ddd
					cout << "The autocentering failed on the current drawing." << endl;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			updater.update(fid, ri);
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		double dpix, dpiy;
Toshihiro Shimizu 890ddd
		original->getDpi(dpix, dpiy);
Toshihiro Shimizu 890ddd
		cl->setCustomDpi((dpix == 0 && dpiy == 0) ? xl->getProperties()->getDpi() : TPointD());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		CleanupPreprocessedImage *cpi;
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			TRasterImageP resampledImage;
Toshihiro Shimizu 890ddd
			cpi = cl->process(original, firstImage, resampledImage);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TToonzImageP timage = cl->finalize(cpi, true);
Toshihiro Shimizu 890ddd
		TPointD dpi(0, 0);
Toshihiro Shimizu 890ddd
		timage->getDpi(dpi.x, dpi.y);
Toshihiro Shimizu 890ddd
		if (dpi.x != 0 && dpi.y != 0)
Toshihiro Shimizu 890ddd
			xl->getProperties()->setDpi(dpi);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (firstImage)
Toshihiro Shimizu 890ddd
			addCleanupDefaultPalette(xl);
Toshihiro Shimizu 890ddd
		firstImage = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		timage->setPalette(xl->getPalette());
Toshihiro Shimizu 890ddd
		xl->setFrameStatus(fid, status | TXshSimpleLevel::Cleanupped);
Toshihiro Shimizu 890ddd
		xl->setFrame(fid, timage);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		updater.update(fid, timage);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/*- 1フレーム終わったら、そのフレームのキャッシュは消す -*/
Toshihiro Shimizu 890ddd
		xl->invalidateFrame(fid);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		delete cpi;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//========================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// main
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// usage: tcleanup filename.tnz [-selected][-overwrite]
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int main(int argc, char *argv[])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QApplication app(argc, argv);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// questo definisce la registry root e inizializza TEnv
Toshihiro Shimizu 890ddd
	TEnv::setApplication(applicationName, applicationVersion);
Toshihiro Shimizu 890ddd
	TEnv::setApplicationFullName(applicationFullName);
Toshihiro Shimizu 890ddd
	TEnv::setRootVarName(rootVarName);
Toshihiro Shimizu 890ddd
	TEnv::setSystemVarPrefix(systemVarPrefix);
Toshihiro Shimizu 890ddd
	TSystem::hasMainLoop(false);
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < argc; i++) //tmsg must be set as soon as it's possible
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		QString str = argv[i];
Toshihiro Shimizu 890ddd
		if (str == "-tmsg")
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			TMsgCore::instance()->connectTo(argv[i + 1]);
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (i == argc)
Toshihiro Shimizu 890ddd
		TMsgCore::instance()->connectTo("");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//TThread::init();  //For the ImageManager construction
Toshihiro Shimizu 890ddd
// controllo se la xxxroot e' definita e corrisponde ad un file esistente
Toshihiro Shimizu 890ddd
#ifdef MACOSX
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// StuffDir
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QFileInfo infoStuff(QString("Toonz 7.1 stuff"));
Toshihiro Shimizu 890ddd
	TFilePath stuffDirPath(infoStuff.absoluteFilePath().toStdString());
Toshihiro Shimizu 890ddd
	TEnv::setStuffDir(stuffDirPath);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
  #ifdef BRAVO
Toshihiro Shimizu 890ddd
    TFilePath stuffDir("/Applications/Toonz 7.1 Bravo/Toonz 7.1 Bravo stuff");
Toshihiro Shimizu 890ddd
  #else
Toshihiro Shimizu 890ddd
    TFilePath  stuffDir("/Applications/Toonz 7.1/Toonz 7.1 stuff");
Toshihiro Shimizu 890ddd
  #endif
Toshihiro Shimizu 890ddd
	
Toshihiro Shimizu 890ddd
  TEnv::setStuffDir(stuffDir);
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath fproot = TEnv::getStuffDir();
Toshihiro Shimizu 890ddd
	if (fproot == TFilePath())
Toshihiro Shimizu 890ddd
		fatalError(string("Undefined: \"") + toString(TEnv::getRootVarPath().getWideString()) + "\"");
Toshihiro Shimizu 890ddd
	if (!TFileStatus(fproot).isDirectory())
Toshihiro Shimizu 890ddd
		fatalError(string("Directory \"") + toString(fproot.getWideString()) + "\" not found or not readable");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath lRootDir = TEnv::getStuffDir() + "toonzfarm";
Toshihiro Shimizu 890ddd
	TFilePath logFilePath = lRootDir + "tcleanup.log";
Toshihiro Shimizu 890ddd
	TUserLogAppend m_userLog(logFilePath);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePathSet fps = ToonzFolder::getProjectsFolders();
Toshihiro Shimizu 890ddd
	TFilePathSet::iterator fpIt;
Toshihiro Shimizu 890ddd
	for (fpIt = fps.begin(); fpIt != fps.end(); ++fpIt)
Toshihiro Shimizu 890ddd
		TProjectManager::instance()->addProjectsRoot(*fpIt);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath libraryFolder = ToonzFolder::getLibraryFolder();
Toshihiro Shimizu 890ddd
	TRasterImagePatternStrokeStyle::setRootDir(libraryFolder);
Toshihiro Shimizu 890ddd
	TVectorImagePatternStrokeStyle::setRootDir(libraryFolder);
Toshihiro Shimizu 890ddd
	TPalette::setRootDir(libraryFolder);
Toshihiro Shimizu 890ddd
	TImageStyle::setLibraryDir(libraryFolder);
Toshihiro Shimizu 890ddd
	TFilePath cacheRoot = ToonzFolder::getCacheRootFolder();
Toshihiro Shimizu 890ddd
	if (cacheRoot.isEmpty())
Toshihiro Shimizu 890ddd
		cacheRoot = TEnv::getStuffDir() + "cache";
Toshihiro Shimizu 890ddd
	TImageCache::instance()->setRootDir(cacheRoot);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	FilePathArgument srcName("tnzFile", "Scene file");
Toshihiro Shimizu 890ddd
	SimpleQualifier selectedOnlyOption("-onlyvisible", "Selected column only");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	SimpleQualifier overwriteAllOption("-overwriteAll", "Overwrite all already cleanupped frames");
Toshihiro Shimizu 890ddd
	SimpleQualifier overwriteNoPaintOption("-overwriteNoPaint", "Overwrite only no-paint levels of already cleanupped frames");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	StringQualifier farmData("-farm data", "TFarm Controller");
Toshihiro Shimizu 890ddd
	StringQualifier idq("-id n", "id");
Toshihiro Shimizu 890ddd
	StringQualifier tmsg("-tmsg n", "Internal use only");
Toshihiro Shimizu 890ddd
	Usage usage(argv[0]);
Toshihiro Shimizu 890ddd
	usage.add(srcName + selectedOnlyOption + overwriteAllOption + overwriteNoPaintOption + farmData + idq + tmsg);
Toshihiro Shimizu 890ddd
	if (!usage.parse(argc, argv))
Toshihiro Shimizu 890ddd
		exit(1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TaskId = idq.getValue();
Toshihiro Shimizu 890ddd
	string fdata = farmData.getValue();
Toshihiro Shimizu 890ddd
	if (fdata.empty())
Toshihiro Shimizu 890ddd
		UseRenderFarm = false;
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		UseRenderFarm = true;
Toshihiro Shimizu 890ddd
		string::size_type pos = fdata.find('@');
Toshihiro Shimizu 890ddd
		if (pos == string::npos)
Toshihiro Shimizu 890ddd
			UseRenderFarm = false;
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			FarmControllerPort = toInt(fdata.substr(0, pos));
Toshihiro Shimizu 890ddd
			FarmControllerName = fdata.substr(pos + 1);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (UseRenderFarm) {
Toshihiro Shimizu 890ddd
		TFarmControllerFactory factory;
Toshihiro Shimizu 890ddd
		factory.create(QString::fromStdString(FarmControllerName), FarmControllerPort, &FarmController);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*- 画像Read/Writeの関数を登録 -*/
Toshihiro Shimizu 890ddd
	initImageIo();
Toshihiro Shimizu 890ddd
	Tiio::defineStd();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*- プロジェクトのロード -*/
Toshihiro Shimizu 890ddd
	TProjectManager *pm = TProjectManager::instance();
Toshihiro Shimizu 890ddd
	TProjectP project = pm->loadSceneProject(srcName);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!project) {
Toshihiro Shimizu 890ddd
		string err = "Couldn't find the project" + project->getName().getName();
Toshihiro Shimizu 890ddd
		m_userLog.error(err);
Toshihiro Shimizu 890ddd
		cerr << err << endl;
Toshihiro Shimizu 890ddd
		return -2;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	cout << "project:" << project->getName() << endl;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath fp = srcName;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*- CLNファイルを直接指定した場合 -*/
Toshihiro Shimizu 890ddd
	bool sourceFileIsCleanupSetting = (fp.getType() == "cln");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool selectedOnly = selectedOnlyOption;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ToonzScene *scene = new ToonzScene();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TImageStyle::setCurrentScene(scene);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*- シーンファイルパスを入力した場合 -*/
Toshihiro Shimizu 890ddd
	if (!sourceFileIsCleanupSetting) {
Toshihiro Shimizu 890ddd
		try {
Toshihiro Shimizu 890ddd
			scene->loadNoResources(fp);
Toshihiro Shimizu 890ddd
		} catch (...) {
Toshihiro Shimizu 890ddd
			string err = "can't read " + fp.getName();
Toshihiro Shimizu 890ddd
			m_userLog.error(err);
Toshihiro Shimizu 890ddd
			cerr << "can't read " << fp << endl;
Toshihiro Shimizu 890ddd
			TImageCache::instance()->clear(true);
Toshihiro Shimizu 890ddd
			return -3;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	/*-- CleanupSettingsファイルパスを直接入力した場合 --*/
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		try {
Toshihiro Shimizu 890ddd
			/*- CleanupSettingsファイルに対応するTIFファイルが有るかチェック -*/
Toshihiro Shimizu 890ddd
			TFilePath tifImagePath = fp.withType("tif");
Toshihiro Shimizu 890ddd
			std::wcout << L"tifImagePath : " << tifImagePath.getWideString() << std::endl;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			/*- 無ければ、連番画像がソースである可能性がある。tifImagePathを連番に差し替える -*/
Toshihiro Shimizu 890ddd
			if (!TFileStatus(tifImagePath).doesExist()) {
Toshihiro Shimizu 890ddd
				tifImagePath = tifImagePath.getParentDir() + (tifImagePath.getName() + "..tif");
Toshihiro Shimizu 890ddd
				std::wcout << "change the source path to : " << tifImagePath.getWideString() << std::endl;
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				std::cout << "tif single image found" << std::endl;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			/*- シーンファイルに対象のTIFファイルをロード -*/
Toshihiro Shimizu 890ddd
			TXshLevel *xl = scene->loadLevel(tifImagePath);
Toshihiro Shimizu 890ddd
			if (!xl) {
Toshihiro Shimizu 890ddd
				std::cout << "failed to load level" << std::endl;
Toshihiro Shimizu 890ddd
				throw TException("Failed to load level.");
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			std::vector<tframeid> dummy;</tframeid>
Toshihiro Shimizu 890ddd
			int frameLength = scene->getXsheet()->exposeLevel(0, 0, xl, dummy);
Toshihiro Shimizu 890ddd
			std::cout << "expose done. frameLength : " << frameLength << std::endl;
Toshihiro Shimizu 890ddd
		} catch (...) {
Toshihiro Shimizu 890ddd
			cerr << "can't read Cleanup Settings file " << fp << endl;
Toshihiro Shimizu 890ddd
			TImageCache::instance()->clear(true);
Toshihiro Shimizu 890ddd
			return -3;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Levels contiene i livelli e i rispettivi frames prsenti nell'xsheet.
Toshihiro Shimizu 890ddd
	std::vector<std::pair<txshsimplelevel *,="" std::set<tframeid="">>> levels;</std::pair<txshsimplelevel>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*- XsheetからCleanupするLevelのリストを得る -*/
Toshihiro Shimizu 890ddd
	searchLevelsToCleanup(levels, scene->getXsheet(), selectedOnly);
Toshihiro Shimizu 890ddd
	TSceneProperties *sprop = scene->getProperties();
Toshihiro Shimizu 890ddd
	CleanupParameters *params = scene->getProperties()->getCleanupParameters();
Toshihiro Shimizu 890ddd
	for (int i = 0; i < (int)levels.size(); i++) {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		bool overwrite = overwriteAllOption;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TXshSimpleLevel *xl = levels[i].first->getSimpleLevel();
Toshihiro Shimizu 890ddd
		if (!xl)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/*- CLNファイルパスを取得 -*/
Toshihiro Shimizu 890ddd
		TFilePath settingsFile = xl->getScannedPath().isEmpty() ? xl->getPath() : xl->getScannedPath();
Toshihiro Shimizu 890ddd
		settingsFile = scene->decodeFilePath(settingsFile).withNoFrame().withType("cln");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/*- CLNファイルがあればそれを用いる。無ければGlobal設定を読み込む -*/
Toshihiro Shimizu 890ddd
		//TFilePath settingsFile = scene->decodeFilePath(xl->getPath()).withNoFrame().withType("cln");
Toshihiro Shimizu 890ddd
		if (TFileStatus(settingsFile).doesExist())
Toshihiro Shimizu 890ddd
			loadSettings(settingsFile, params);
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			restoreGlobalSettings(params);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TCleanupper::instance()->setParameters(params);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		bool lineProcessing = (params->m_lineProcessingMode != lpNone);
Toshihiro Shimizu 890ddd
		TFilePath tmp = params->getPath(scene);
Toshihiro Shimizu 890ddd
		int ltype = xl->getType();
Toshihiro Shimizu 890ddd
		assert(ltype == TZP_XSHLEVEL || ltype == TZI_XSHLEVEL || ltype == OVL_XSHLEVEL);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// target path
Toshihiro Shimizu 890ddd
		TFilePath targetPath = xl->getPath();
Toshihiro Shimizu 890ddd
		if (ltype != TZP_XSHLEVEL) {
Toshihiro Shimizu 890ddd
			if (lineProcessing)
Toshihiro Shimizu 890ddd
				targetPath =
Toshihiro Shimizu 890ddd
					targetPath
Toshihiro Shimizu 890ddd
						.withParentDir(params->getPath(scene)) // scene->getDefaultParentDir(TZP_XSHLEVEL))
Toshihiro Shimizu 890ddd
						.withNoFrame()
Toshihiro Shimizu 890ddd
						.withType("tlv");
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				targetPath =
Toshihiro Shimizu 890ddd
					targetPath
Toshihiro Shimizu 890ddd
						.withParentDir(params->getPath(scene)); // scene->getDefaultParentDir(TZP_XSHLEVEL));
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			targetPath =
Toshihiro Shimizu 890ddd
				targetPath
Toshihiro Shimizu 890ddd
					.withParentDir(params->getPath(scene)); // scene->getDefaultParentDir(TZP_XSHLEVEL));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/*- NoPaintを作る設定のとき、NoPaintの方だけにOverwriteするようにする -*/
Toshihiro Shimizu 890ddd
		bool isReCleanup;
Toshihiro Shimizu 890ddd
		isReCleanup = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/*- 再Cleanupのときにパスを元に戻すために用いる -*/
Toshihiro Shimizu 890ddd
		TFilePath originalLevelPath = TFilePath();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TFilePath actualTargetPath = scene->decodeFilePath(targetPath);
Toshihiro Shimizu 890ddd
		/*- すでにLevelがある場合 -*/
Toshihiro Shimizu 890ddd
		if (TSystem::doesExistFileOrLevel(actualTargetPath) && Preferences::instance()->isSaveUnpaintedInCleanupEnable() && overwriteNoPaintOption) {
Toshihiro Shimizu 890ddd
			overwrite = true;
Toshihiro Shimizu 890ddd
			originalLevelPath = scene->codeFilePath(targetPath);
Toshihiro Shimizu 890ddd
			/*- パスを書き換え、再Cleanupのフラグを立てる -*/
Toshihiro Shimizu 890ddd
			isReCleanup = true;
Toshihiro Shimizu 890ddd
			/*- nopaintフォルダの作成 -*/
Toshihiro Shimizu 890ddd
			TFilePath nopaintDir = targetPath.getParentDir() + "nopaint";
Toshihiro Shimizu 890ddd
			if (!TFileStatus(nopaintDir).doesExist()) {
Toshihiro Shimizu 890ddd
				try {
Toshihiro Shimizu 890ddd
					TSystem::mkDir(nopaintDir);
Toshihiro Shimizu 890ddd
				} catch (...) {
Toshihiro Shimizu 890ddd
					return 0;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			/*- 保存先のパスをnopaintの方にする -*/
Toshihiro Shimizu 890ddd
			targetPath = targetPath.getParentDir() + "nopaint\\" + TFilePath(targetPath.getName() + "_np." + targetPath.getType());
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		std::wcout << L"targetPath = " << targetPath.getWideString() << std::endl;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (lineProcessing) {
Toshihiro Shimizu 890ddd
			if (ltype != TZP_XSHLEVEL) {
Toshihiro Shimizu 890ddd
				xl->makeTlv(targetPath);
Toshihiro Shimizu 890ddd
			} else if (targetPath != xl->getPath()) {
Toshihiro Shimizu 890ddd
				xl->setPath(targetPath, false);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			xl->setPalette(TCleanupper::instance()->createToonzPaletteFromCleanupPalette());
Toshihiro Shimizu 890ddd
		} else if (!params->getPath(scene).isEmpty()) {
Toshihiro Shimizu 890ddd
			xl->setScannedPath(xl->getPath());
Toshihiro Shimizu 890ddd
			xl->setPath(scene->codeFilePath(targetPath), false);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		std::set<tframeid> fidsInXsheet = levels[i].second;</tframeid>
Toshihiro Shimizu 890ddd
		assert(fidsInXsheet.size() > 0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		xl->load();
Toshihiro Shimizu 890ddd
		cleanupLevel(xl, fidsInXsheet, scene, overwrite, m_userLog);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/*- Cleanup完了後、Nopaintをnopaintフォルダに保存する -*/
Toshihiro Shimizu 890ddd
		if (Preferences::instance()->isSaveUnpaintedInCleanupEnable() && !isReCleanup) /*-- 再Cleanupのときはnopaintを作らない --*/
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			/*- nopaintフォルダの作成 -*/
Toshihiro Shimizu 890ddd
			TFilePath nopaintDir = actualTargetPath.getParentDir() + "nopaint";
Toshihiro Shimizu 890ddd
			if (!TFileStatus(nopaintDir).doesExist()) {
Toshihiro Shimizu 890ddd
				try {
Toshihiro Shimizu 890ddd
					TSystem::mkDir(nopaintDir);
Toshihiro Shimizu 890ddd
				} catch (...) {
Toshihiro Shimizu 890ddd
					string err = "Can't make directory  " + nopaintDir.getName();
Toshihiro Shimizu 890ddd
					m_userLog.error(err);
Toshihiro Shimizu 890ddd
					cerr << "Can't make directory" << endl;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			//Se sono nell'ultimo frame del livello salvo il livello cleanuppato
Toshihiro Shimizu 890ddd
			//in un file chiamato nome-unpainted (file di backup del cleanup).
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			/*- 保存先 -*/
Toshihiro Shimizu 890ddd
			TFilePath unpaintedLevelPath = actualTargetPath.getParentDir() + "nopaint\\" + TFilePath(targetPath.getName() + "_np." + targetPath.getType());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (TFileStatus(actualTargetPath).doesExist() && (!TFileStatus(unpaintedLevelPath).doesExist() || overwriteAllOption) /*- 全て上書きなら既存のNoPaintに上書きする -*/
Toshihiro Shimizu 890ddd
				&& xl) {
Toshihiro Shimizu 890ddd
				/*- GUI上でCleanupするときと同様に、Cleanup結果をコピーして作る -*/
Toshihiro Shimizu 890ddd
				TSystem::copyFile(unpaintedLevelPath, actualTargetPath);
Toshihiro Shimizu 890ddd
				/*- パレットのコピー -*/
Toshihiro Shimizu 890ddd
				TFilePath levelPalettePath = actualTargetPath.getParentDir() + TFilePath(actualTargetPath.getName() + ".tpl");
Toshihiro Shimizu 890ddd
				TFilePath unpaintedLevelPalettePath = levelPalettePath.getParentDir() + "nopaint\\" + TFilePath(levelPalettePath.getName() + "_np." + levelPalettePath.getType());
Toshihiro Shimizu 890ddd
				TSystem::copyFile(unpaintedLevelPalettePath, levelPalettePath);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		/*-- 再Cleanupの場合はXsheet上のパスをオリジナルのもの(NoPaintでないもの)に戻す --*/
Toshihiro Shimizu 890ddd
		else if (isReCleanup) {
Toshihiro Shimizu 890ddd
			xl->setPath(originalLevelPath);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*- CleanupParamをGrobalに戻す -*/
Toshihiro Shimizu 890ddd
	restoreGlobalSettings(params);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*- CleanupSettingsからCleanupを行った場合はシーンの保存は行わない -*/
Toshihiro Shimizu 890ddd
	if (sourceFileIsCleanupSetting) {
Toshihiro Shimizu 890ddd
		std::cout << "Cleanup from Setting file completed" << std::endl;
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	wstring wInfo = L"updating scene " + scene->getSceneName();
Toshihiro Shimizu 890ddd
	QString qInfo_ = QString::fromStdWString(wInfo);
Toshihiro Shimizu 890ddd
	string info = qInfo_.toStdString();
Toshihiro Shimizu 890ddd
	m_userLog.info(info);
Toshihiro Shimizu 890ddd
	cout << "updating scene " << scene->getSceneName() << endl;
Toshihiro Shimizu 890ddd
	/*- シーンを上書き保存 -*/
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		scene->save(fp);
Toshihiro Shimizu 890ddd
		TImageCache::instance()->clear(true);
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		wstring wErr = L"Can't save scene  " + fp.getWideName();
Toshihiro Shimizu 890ddd
		QString qErr = QString::fromStdWString(wErr);
Toshihiro Shimizu 890ddd
		string err = qErr.toStdString();
Toshihiro Shimizu 890ddd
		m_userLog.error(err);
Toshihiro Shimizu 890ddd
		cerr << "Can't save scene" << endl;
Toshihiro Shimizu 890ddd
		TImageCache::instance()->clear(true);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Shinya Kitaoka eabf18
	DVGui::info("Cleanup Done.");
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#ifdef BRAVO
Toshihiro Shimizu 890ddd
#ifdef BRAVODEMO
Toshihiro Shimizu 890ddd
const char *toonzVersion = "Bravo 7.1 (demo)";
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
const char *toonzVersion = "Bravo 7.1";
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
const char *toonzVersion = "Toonz 7.1";
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
string getToonzVersion()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return toonzVersion;
Toshihiro Shimizu 890ddd
}