Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#pragma warning(disable : 4533)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tiio_std.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tlevel.h"
Toshihiro Shimizu 890ddd
#include "trasterimage.h"
Toshihiro Shimizu 890ddd
#include "ttoonzimage.h"
Toshihiro Shimizu 890ddd
#include "trastercm.h"
Toshihiro Shimizu 890ddd
#include "tnzimage.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
#include "toonz/fill.h"
Toshihiro Shimizu 890ddd
#include "toonz/autoclose.h"
Toshihiro Shimizu 890ddd
#include "tenv.h"
Toshihiro Shimizu 890ddd
#include "convert2tlv.h"
Toshihiro Shimizu 890ddd
#include "tstream.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <map></map>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/toonzfolders.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// gmt, 14/11/2013 removed a commented out blocks of code (void buildInks1(), void buildPalette() )
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
extern TEnv::DoubleVar AutocloseDistance;
Toshihiro Shimizu 890ddd
extern TEnv::DoubleVar AutocloseAngle;
Toshihiro Shimizu 890ddd
extern TEnv::IntVar AutocloseOpacity;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
//----------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline TPixel unmultiply(const TPixel &in)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (in.r == 255)
Toshihiro Shimizu 890ddd
		return in;
Toshihiro Shimizu 890ddd
	TPixel out;
Toshihiro Shimizu 890ddd
	int val = in.r * 255 / in.m;
Toshihiro Shimizu 890ddd
	out.r = tcrop(val, 0, 255);
Toshihiro Shimizu 890ddd
	val = in.g * 255 / in.m;
Toshihiro Shimizu 890ddd
	out.g = tcrop(val, 0, 255);
Toshihiro Shimizu 890ddd
	val = in.b * 255 / in.m;
Toshihiro Shimizu 890ddd
	out.b = tcrop(val, 0, 255);
Toshihiro Shimizu 890ddd
	out.m = 255;
Toshihiro Shimizu 890ddd
	return out;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline int distance(const TPixel &c1, const TPixel &c2)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return (c1.r - c2.r) * (c1.r - c2.r) + (c1.g - c2.g) * (c1.g - c2.g) + (c1.b - c2.b) * (c1.b - c2.b);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int findClosest(const map<tpixel, int=""> &colorMap, TPixel &curPixColor)</tpixel,>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	map<tpixel, int="">::const_iterator it = colorMap.begin();</tpixel,>
Toshihiro Shimizu 890ddd
	int minDistance = 1000000000;
Toshihiro Shimizu 890ddd
	int index = -1;
Toshihiro Shimizu 890ddd
	for (; it != colorMap.end(); ++it) {
Toshihiro Shimizu 890ddd
		int dist = distance(it->first, curPixColor);
Toshihiro Shimizu 890ddd
		if (dist < minDistance) {
Toshihiro Shimizu 890ddd
			minDistance = dist;
Toshihiro Shimizu 890ddd
			index = it->second;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	assert(index != -1);
Toshihiro Shimizu 890ddd
	return index;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define CHECKCOLOR(r, x, y, tone)                               \
Toshihiro Shimizu 890ddd
	{                                                           \
Toshihiro Shimizu 890ddd
		TPixelCM32 color = *((TPixelCM32 *)r->pixels(y) + (x)); \
Toshihiro Shimizu 890ddd
		if (color.getTone() == tone /*&& color.getPaint()!=0*/) \
Toshihiro Shimizu 890ddd
			return TPoint(x, y);                                \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//cerca in quadrati concentrici di raggio rad intorno al pixel in  (x, y)
Toshihiro Shimizu 890ddd
//il primo pixel di paint puro e ritorna il suo indice di paint
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPoint getClosestToneValue(const TRasterCM32P &r, int y, int x, int tone)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int maxRad = tmin(x, r->getLx() - x - 1, y, r->getLy() - y - 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int rad = 1; rad < maxRad; rad++) {
Toshihiro Shimizu 890ddd
		CHECKCOLOR(r, x, y - rad, tone)
Toshihiro Shimizu 890ddd
		CHECKCOLOR(r, x, y + rad, tone)
Toshihiro Shimizu 890ddd
		CHECKCOLOR(r, x - rad, y, tone)
Toshihiro Shimizu 890ddd
		CHECKCOLOR(r, x + rad, y, tone)
Toshihiro Shimizu 890ddd
		for (int j = 1; j <= rad; j++) {
Toshihiro Shimizu 890ddd
			CHECKCOLOR(r, x - j, y - rad, tone)
Toshihiro Shimizu 890ddd
			CHECKCOLOR(r, x + j, y - rad, tone)
Toshihiro Shimizu 890ddd
			CHECKCOLOR(r, x - j, y + rad, tone)
Toshihiro Shimizu 890ddd
			CHECKCOLOR(r, x + j, y + rad, tone)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CHECKCOLOR(r, x - rad, y - j, tone)
Toshihiro Shimizu 890ddd
			CHECKCOLOR(r, x - rad, y + j, tone)
Toshihiro Shimizu 890ddd
			CHECKCOLOR(r, x + rad, y - j, tone)
Toshihiro Shimizu 890ddd
			CHECKCOLOR(r, x + rad, y + j, tone)
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return TPoint(-1, -1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPoint getClosestPurePaint(const TRasterCM32P &r, int y, int x)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return getClosestToneValue(r, y, x, 255);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPoint getClosestPureInk(const TRasterCM32P &r, int y, int x)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return getClosestToneValue(r, y, x, 0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool firstIsUnpainted(const TRaster32P &r1, const TRaster32P &r2)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (int i = 0; i < r1->getLy(); i++) {
Toshihiro Shimizu 890ddd
		TPixel32 *pix1 = r1->pixels(i);
Toshihiro Shimizu 890ddd
		TPixel32 *pix2 = r2->pixels(i);
Toshihiro Shimizu 890ddd
		for (int j = 0; j < r1->getLx(); j++, pix1++, pix2++) {
Toshihiro Shimizu 890ddd
			if (pix1->m == 255 && pix2->m == 0)
Toshihiro Shimizu 890ddd
				return false;
Toshihiro Shimizu 890ddd
			else if (pix1->m == 0 && pix2->m == 255)
Toshihiro Shimizu 890ddd
				return true;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------
Toshihiro Shimizu 890ddd
//ritorna -1 se non ha il canale di matte (tutti i pixel a 255)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int getMaxMatte(const TRaster32P &r)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int maxMatte = -1;
Toshihiro Shimizu 890ddd
	bool withMatte = false;
Toshihiro Shimizu 890ddd
	for (int i = 0; i < r->getLy(); i++) {
Toshihiro Shimizu 890ddd
		TPixel32 *pix = r->pixels(i);
Toshihiro Shimizu 890ddd
		for (int j = 0; j < r->getLx(); j++, pix++) {
Toshihiro Shimizu 890ddd
			maxMatte = tmax(maxMatte, (int)pix->m);
Toshihiro Shimizu 890ddd
			if (pix->m != 255)
Toshihiro Shimizu 890ddd
				withMatte = true;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return withMatte ? maxMatte : -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void normalize(const TRaster32P &r, int maxMatte)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int val;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < r->getLy(); i++) {
Toshihiro Shimizu 890ddd
		TPixel32 *pix = r->pixels(i);
Toshihiro Shimizu 890ddd
		for (int j = 0; j < r->getLx(); j++, pix++) {
Toshihiro Shimizu 890ddd
			val = pix->r * 255 / maxMatte;
Toshihiro Shimizu 890ddd
			pix->r = tcrop(val, 0, 255);
Toshihiro Shimizu 890ddd
			val = pix->g * 255 / maxMatte;
Toshihiro Shimizu 890ddd
			pix->g = tcrop(val, 0, 255);
Toshihiro Shimizu 890ddd
			val = pix->b * 255 / maxMatte;
Toshihiro Shimizu 890ddd
			pix->b = tcrop(val, 0, 255);
Toshihiro Shimizu 890ddd
			val = pix->m * 255 / maxMatte;
Toshihiro Shimizu 890ddd
			pix->m = tcrop(val, 0, 255);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int getFramesCount(const TLevelP &l, int from, int to)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (from == -1)
Toshihiro Shimizu 890ddd
		return l->getFrameCount();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int count = 0;
Toshihiro Shimizu 890ddd
	TLevel::Iterator it = l->begin();
Toshihiro Shimizu 890ddd
	while (it != l->end() && it->first.getNumber() < from)
Toshihiro Shimizu 890ddd
		it++;
Toshihiro Shimizu 890ddd
	while (it != l->end() && it->first.getNumber() <= to)
Toshihiro Shimizu 890ddd
		it++, count++;
Toshihiro Shimizu 890ddd
	return count;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
map<tpixel, int="">::const_iterator Convert2Tlv::findNearestColor(const TPixel &color)</tpixel,>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//assert((int)colorMap.size()>toIndex);
Toshihiro Shimizu 890ddd
	//assert((int)colorMap.size()>fromIndex);
Toshihiro Shimizu 890ddd
	map<tpixel, int="">::const_iterator ret = m_colorMap.end(), it = m_colorMap.begin();</tpixel,>
Toshihiro Shimizu 890ddd
	//std::advance(it, fromIndex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int mindist = 1000;
Toshihiro Shimizu 890ddd
	for (; it != m_colorMap.end(); ++it) {
Toshihiro Shimizu 890ddd
		const TPixel &curr = it->first;
Toshihiro Shimizu 890ddd
		int dr = abs(curr.r - color.r);
Toshihiro Shimizu 890ddd
		if (dr > m_colorTolerance)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		int dg = abs(curr.g - color.g);
Toshihiro Shimizu 890ddd
		if (dg > m_colorTolerance)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		int db = abs(curr.b - color.b);
Toshihiro Shimizu 890ddd
		if (db > m_colorTolerance)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		int dist = dr + dg + db;
Toshihiro Shimizu 890ddd
		if (dist < mindist) {
Toshihiro Shimizu 890ddd
			mindist = dist;
Toshihiro Shimizu 890ddd
			ret = it;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return ret;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Convert2Tlv::buildInks(TRasterCM32P &rout, const TRaster32P &rin)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	map<tpixel, int="">::const_iterator it;</tpixel,>
Toshihiro Shimizu 890ddd
	TPixel curColor = TPixel::Transparent;
Toshihiro Shimizu 890ddd
	int i, j;
Toshihiro Shimizu 890ddd
	int curIndex;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//prima passata: identifico i colori di inchiostro e metto in rout i pixel di inchiostro puro
Toshihiro Shimizu 890ddd
	for (i = 0; i < rin->getLy(); i++) {
Toshihiro Shimizu 890ddd
		TPixel *pixin = rin->pixels(i);
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixout = rout->pixels(i);
Toshihiro Shimizu 890ddd
		for (j = 0; j < rin->getLx(); j++, pixin++, pixout++) {
Toshihiro Shimizu 890ddd
			TPixel colorIn;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (pixin->m != 255)
Toshihiro Shimizu 890ddd
				continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (curColor != *pixin) {
Toshihiro Shimizu 890ddd
				curColor = *pixin;
Toshihiro Shimizu 890ddd
				if ((it = m_colorMap.find(curColor)) == m_colorMap.end()) {
Toshihiro Shimizu 890ddd
					if (m_colorTolerance > 0)
Toshihiro Shimizu 890ddd
						it = findNearestColor(curColor);
Toshihiro Shimizu 890ddd
					//if (it==colorMap.end() && (int)colorMap.size()>origColorCount)
Toshihiro Shimizu 890ddd
					//	it  = findNearestColor(curColor, colorMap, colorTolerance, origColorCount, colorMap.size()-1);
Toshihiro Shimizu 890ddd
					if (it == m_colorMap.end() && m_lastIndex < 4095) {
Toshihiro Shimizu 890ddd
						m_colorMap[curColor] = ++m_lastIndex;
Toshihiro Shimizu 890ddd
						curIndex = m_lastIndex;
Toshihiro Shimizu 890ddd
					} else if (it != m_colorMap.end()) {
Toshihiro Shimizu 890ddd
						m_colorMap[curColor] = it->second;
Toshihiro Shimizu 890ddd
						curIndex = it->second;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				} else
Toshihiro Shimizu 890ddd
					curIndex = it->second;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			*pixout = TPixelCM32(curIndex, 0, 0);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//seconda  passata: metto gli inchiostri di antialiasing
Toshihiro Shimizu 890ddd
	curColor = TPixel::Transparent;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < rin->getLy(); i++) {
Toshihiro Shimizu 890ddd
		TPixel *pixin = rin->pixels(i);
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixout = rout->pixels(i);
Toshihiro Shimizu 890ddd
		for (j = 0; j < rin->getLx(); j++, pixin++, pixout++) {
Toshihiro Shimizu 890ddd
			TPixel colorIn;
Toshihiro Shimizu 890ddd
			if (pixin->m == 255) //gia' messo nel ciclo precedente
Toshihiro Shimizu 890ddd
				continue;
Toshihiro Shimizu 890ddd
			if (pixin->m == 0)
Toshihiro Shimizu 890ddd
				continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			colorIn = unmultiply(*pixin); //findClosestOpaque(rin, i, j);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (curColor != colorIn) {
Toshihiro Shimizu 890ddd
				curColor = colorIn;
Toshihiro Shimizu 890ddd
				if ((it = m_colorMap.find(curColor)) != m_colorMap.end())
Toshihiro Shimizu 890ddd
					curIndex = it->second;
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					curIndex = findClosest(m_colorMap, curColor);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			*pixout = TPixelCM32(curIndex, 0, 255 - pixin->m);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Convert2Tlv::removeAntialias(TRasterCM32P &r)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int threshold = (int)(m_antialiasValue * 255.0 / 100.0);
Toshihiro Shimizu 890ddd
	int tone;
Toshihiro Shimizu 890ddd
	for (int i = 0; i < r->getLy(); i++) {
Toshihiro Shimizu 890ddd
		TPixelCM32 *pix = r->pixels(i);
Toshihiro Shimizu 890ddd
		for (int j = 0; j < r->getLx(); j++, pix++)
Toshihiro Shimizu 890ddd
			if ((tone = pix->getTone()) != 0xff) //tone==ff e tone==0 non vanno toccati mai
Toshihiro Shimizu 890ddd
				pix->setTone(tone > threshold ? 0xff : 0);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Convert2Tlv::buildInksFromGrayTones(TRasterCM32P &rout, const TRasterP &rin)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i, j;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterGR8P r8 = (TRasterGR8P)rin;
Toshihiro Shimizu 890ddd
	TRaster32P r32 = (TRaster32P)rin;
Toshihiro Shimizu 890ddd
	if (r8)
Toshihiro Shimizu 890ddd
		for (i = 0; i < rin->getLy(); i++) {
Toshihiro Shimizu 890ddd
			TPixelGR8 *pixin = r8->pixels(i);
Toshihiro Shimizu 890ddd
			TPixelCM32 *pixout = rout->pixels(i);
Toshihiro Shimizu 890ddd
			for (j = 0; j < rin->getLx(); j++, pixin++, pixout++)
Toshihiro Shimizu 890ddd
				*pixout = TPixelCM32(1, 0, pixin->value);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		for (i = 0; i < rin->getLy(); i++) {
Toshihiro Shimizu 890ddd
			TPixel *pixin = r32->pixels(i);
Toshihiro Shimizu 890ddd
			TPixelCM32 *pixout = rout->pixels(i);
Toshihiro Shimizu 890ddd
			for (j = 0; j < rin->getLx(); j++, pixin++, pixout++)
Toshihiro Shimizu 890ddd
				*pixout = TPixelCM32(1, 0, TPixelGR8::from(*pixin).value);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Convert2Tlv::buildInksForNAAImage(TRasterCM32P &rout, const TRaster32P &rin)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	map<tpixel, int="">::iterator it;</tpixel,>
Toshihiro Shimizu 890ddd
	TPixel curColor = TPixel::Transparent;
Toshihiro Shimizu 890ddd
	int i, j;
Toshihiro Shimizu 890ddd
	int curIndex;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//prima passata: identifico i colori di inchiostro e metto in rout i pixel di inchiostro puro
Toshihiro Shimizu 890ddd
	for (i = 0; i < rin->getLy(); i++) {
Toshihiro Shimizu 890ddd
		TPixel *pixin = rin->pixels(i);
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixout = rout->pixels(i);
Toshihiro Shimizu 890ddd
		for (j = 0; j < rin->getLx(); j++, pixin++, pixout++) {
Toshihiro Shimizu 890ddd
			TPixel colorIn;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			/*-白ピクセルを透明として扱う-*/
Toshihiro Shimizu 890ddd
			if (*pixin == TPixel(255, 255, 255)) {
Toshihiro Shimizu 890ddd
				*pixout = TPixelCM32(0, 0, 255);
Toshihiro Shimizu 890ddd
				continue;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (curColor != *pixin) {
Toshihiro Shimizu 890ddd
				curColor = *pixin;
Toshihiro Shimizu 890ddd
				if ((it = m_colorMap.find(curColor)) == m_colorMap.end()) {
Toshihiro Shimizu 890ddd
					if (m_lastIndex < 4095)
Toshihiro Shimizu 890ddd
						m_colorMap[curColor] = ++m_lastIndex;
Toshihiro Shimizu 890ddd
					curIndex = m_lastIndex;
Toshihiro Shimizu 890ddd
				} else
Toshihiro Shimizu 890ddd
					curIndex = it->second;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			*pixout = TPixelCM32(curIndex, 0, 0);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_colorMap.empty())
Toshihiro Shimizu 890ddd
		m_colorMap[TPixel::Black] = ++m_lastIndex;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Convert2Tlv::doFill(TRasterCM32P &rout, const TRaster32P &rin)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//prima passata: si filla  solo partendo da pixel senza inchiostro, senza antialiasing(tone==255)
Toshihiro Shimizu 890ddd
	for (int i = 0; i < rin->getLy(); i++) {
Toshihiro Shimizu 890ddd
		TPixel *pixin = rin->pixels(i);
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixout = rout->pixels(i);
Toshihiro Shimizu 890ddd
		for (int j = 0; j < rin->getLx(); j++, pixin++, pixout++) {
Toshihiro Shimizu 890ddd
			if (!(pixout->getTone() == 255 && pixout->getPaint() == 0 && pixin->m == 255))
Toshihiro Shimizu 890ddd
				continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			map<tpixel, int="">::const_iterator it;</tpixel,>
Toshihiro Shimizu 890ddd
			int paintIndex;
Toshihiro Shimizu 890ddd
			if ((it = m_colorMap.find(*pixin)) == m_colorMap.end()) {
Toshihiro Shimizu 890ddd
				if (m_colorTolerance > 0)
Toshihiro Shimizu 890ddd
					it = findNearestColor(*pixin);
Toshihiro Shimizu 890ddd
				// if (it==colorMap.end() && (int)colorMap.size()>origColorCount) //se non l'ho trovato tra i colori origari, lo cerco in quelli nuovi, ma in questo caso deve essere esattamente uguale(tolerance = 0)
Toshihiro Shimizu 890ddd
				//	 it  = findNearestColor(*pixin, colorMap, colorTolerance, origColorCount, colorMap.size()-1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (it == m_colorMap.end() && m_lastIndex < 4096) {
Toshihiro Shimizu 890ddd
					m_colorMap[*pixin] = ++m_lastIndex;
Toshihiro Shimizu 890ddd
					paintIndex = m_lastIndex;
Toshihiro Shimizu 890ddd
				} else if (it != m_colorMap.end()) {
Toshihiro Shimizu 890ddd
					m_colorMap[*pixin] = it->second;
Toshihiro Shimizu 890ddd
					paintIndex = it->second;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				paintIndex = it->second;
Toshihiro Shimizu 890ddd
			FillParameters params;
Toshihiro Shimizu 890ddd
			params.m_p = TPoint(j, i);
Toshihiro Shimizu 890ddd
			params.m_styleId = paintIndex;
Toshihiro Shimizu 890ddd
			params.m_emptyOnly = true;
Toshihiro Shimizu 890ddd
			fill(rout, params);
Toshihiro Shimizu 890ddd
			//if (*((ULONG *)rout->getRawData())!=0xff)
Toshihiro Shimizu 890ddd
			//  {
Toshihiro Shimizu 890ddd
			//  int cavolo=0;
Toshihiro Shimizu 890ddd
			//  }
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//seconda passata: se son rimasti pixel antialiasati non fillati, si fillano, cercando nelle vicinanze un pixel di paint puro per capire il colore da usare
Toshihiro Shimizu 890ddd
	for (int i = 0; i < rin->getLy(); i++) {
Toshihiro Shimizu 890ddd
		TPixel *pixin = rin->pixels(i);
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixout = rout->pixels(i);
Toshihiro Shimizu 890ddd
		for (int j = 0; j < rin->getLx(); j++, pixin++, pixout++) {
Toshihiro Shimizu 890ddd
			if (!(pixout->getTone() > 0 && pixout->getTone() < 255 && pixout->getPaint() == 0 && pixin->m == 255))
Toshihiro Shimizu 890ddd
				continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TPoint p = getClosestPurePaint(rout, i, j);
Toshihiro Shimizu 890ddd
			if (p.x == -1)
Toshihiro Shimizu 890ddd
				continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			//pixout->setPaint( paintIndex);
Toshihiro Shimizu 890ddd
			FillParameters params;
Toshihiro Shimizu 890ddd
			params.m_p = TPoint(j, i);
Toshihiro Shimizu 890ddd
			params.m_styleId = (rout->pixels(p.y) + p.x)->getPaint();
Toshihiro Shimizu 890ddd
			params.m_emptyOnly = true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			fill(rout, params);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//infine, si filla di trasparente lo sfondo, percorrendo il bordo, nel caso di trasbordamenti di colore
Toshihiro Shimizu 890ddd
	TPixelCM32 *pixCm;
Toshihiro Shimizu 890ddd
	TPixel *pix;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	pixCm = rout->pixels(0);
Toshihiro Shimizu 890ddd
	pix = rin->pixels(0);
Toshihiro Shimizu 890ddd
	FillParameters params;
Toshihiro Shimizu 890ddd
	params.m_styleId = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < rout->getLx(); i++, pixCm++, pix++)
Toshihiro Shimizu 890ddd
		if (pixCm->getTone() == 255 && pixCm->getPaint() != 0 && pix->m == 0) {
Toshihiro Shimizu 890ddd
			params.m_p = TPoint(i, 0);
Toshihiro Shimizu 890ddd
			fill(rout, params);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	pixCm = rout->pixels(rout->getLy() - 1);
Toshihiro Shimizu 890ddd
	pix = rin->pixels(rout->getLy() - 1);
Toshihiro Shimizu 890ddd
	for (int i = 0; i < rout->getLx(); i++, pixCm++, pix++)
Toshihiro Shimizu 890ddd
		if (pixCm->getTone() == 255 && pixCm->getPaint() != 0 && pix->m == 0) {
Toshihiro Shimizu 890ddd
			params.m_p = TPoint(i, rout->getLy() - 1);
Toshihiro Shimizu 890ddd
			fill(rout, params);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	int wrapCM = rout->getWrap();
Toshihiro Shimizu 890ddd
	int wrap = rin->getWrap();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	pixCm = rout->pixels(0);
Toshihiro Shimizu 890ddd
	pix = rin->pixels(0);
Toshihiro Shimizu 890ddd
	for (int i = 0; i < rin->getLy(); i++, pixCm += wrapCM, pix += wrap)
Toshihiro Shimizu 890ddd
		if (pixCm->getTone() == 255 && pixCm->getPaint() != 0 && pix->m == 0) {
Toshihiro Shimizu 890ddd
			params.m_p = TPoint(0, i);
Toshihiro Shimizu 890ddd
			fill(rout, params);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	pixCm = rout->pixels(0) + rout->getLx() - 1;
Toshihiro Shimizu 890ddd
	pix = rin->pixels(0) + rin->getLx() - 1;
Toshihiro Shimizu 890ddd
	for (int i = 0; i < rin->getLy(); i++, pixCm += wrapCM, pix += wrap)
Toshihiro Shimizu 890ddd
		if (pixCm->getTone() == 255 && pixCm->getPaint() != 0 && pix->m == 0) {
Toshihiro Shimizu 890ddd
			params.m_p = TPoint(rout->getLx() - 1, i);
Toshihiro Shimizu 890ddd
			fill(rout, params);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Convert2Tlv::buildToonzRaster(TRasterCM32P &rout, const TRasterP &rin1, const TRasterP &rin2)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (rin2)
Toshihiro Shimizu 890ddd
		assert(rin1->getSize() == rin2->getSize());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	rout->clear();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::cout << "      computing inks...\n";
Toshihiro Shimizu 890ddd
	TRaster32P r1 = (TRaster32P)rin1;
Toshihiro Shimizu 890ddd
	TRasterGR8P r1gr = (TRasterGR8P)rin1;
Toshihiro Shimizu 890ddd
	TRaster32P r2 = (TRaster32P)rin2;
Toshihiro Shimizu 890ddd
	TRasterGR8P r2gr = (TRasterGR8P)rin2;
Toshihiro Shimizu 890ddd
	TRasterP rU, rP;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (r1gr) {
Toshihiro Shimizu 890ddd
		rU = r1gr;
Toshihiro Shimizu 890ddd
		rP = r2;
Toshihiro Shimizu 890ddd
	} else if (r2gr) {
Toshihiro Shimizu 890ddd
		rU = r2gr;
Toshihiro Shimizu 890ddd
		rP = r1;
Toshihiro Shimizu 890ddd
	} else if (!r1)
Toshihiro Shimizu 890ddd
		rU = r2;
Toshihiro Shimizu 890ddd
	else if (!r2)
Toshihiro Shimizu 890ddd
		rU = r1;
Toshihiro Shimizu 890ddd
	else if (firstIsUnpainted(r1, r2)) {
Toshihiro Shimizu 890ddd
		rU = r1;
Toshihiro Shimizu 890ddd
		rP = r2;
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		rU = r2;
Toshihiro Shimizu 890ddd
		rP = r1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterCM32P r;
Toshihiro Shimizu 890ddd
	if (rout->getSize() != rU->getSize()) {
Toshihiro Shimizu 890ddd
		int dx = rout->getLx() - rU->getLx();
Toshihiro Shimizu 890ddd
		int dy = rout->getLy() - rU->getLy();
Toshihiro Shimizu 890ddd
		assert(dx >= 0 && dy >= 0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		r = rout->extract(dx / 2, dy / 2, dx / 2 + rU->getLx() - 1, dy / 2 + rU->getLy() - 1);
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		r = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if ((TRasterGR8P)rU)
Toshihiro Shimizu 890ddd
		buildInksFromGrayTones(r, rU);
Toshihiro Shimizu 890ddd
	else if (m_isUnpaintedFromNAA)
Toshihiro Shimizu 890ddd
		buildInksForNAAImage(r, (TRaster32P)rU);
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		int maxMatte = getMaxMatte((TRaster32P)rU);
Toshihiro Shimizu 890ddd
		if (maxMatte == -1)
Toshihiro Shimizu 890ddd
			buildInksFromGrayTones(r, rU);
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			if (maxMatte < 255)
Toshihiro Shimizu 890ddd
				normalize(rU, maxMatte);
Toshihiro Shimizu 890ddd
			buildInks(r, (TRaster32P)rU /*rP,*/);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_autoclose)
Toshihiro Shimizu 890ddd
		TAutocloser(r, AutocloseDistance, AutocloseAngle, 1, AutocloseOpacity).exec();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (rP) {
Toshihiro Shimizu 890ddd
		std::cout << "      computing paints...\n";
Toshihiro Shimizu 890ddd
		doFill(r, rP);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (m_antialiasType == 2) //remove antialias
Toshihiro Shimizu 890ddd
		removeAntialias(r);
Toshihiro Shimizu 890ddd
	else if (m_antialiasType == 1) //add antialias
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TRasterCM32P raux(r->getSize());
Toshihiro Shimizu 890ddd
		TRop::antialias(r, raux, 10, m_antialiasValue);
Toshihiro Shimizu 890ddd
		rout = raux;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPalette *Convert2Tlv::buildPalette()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	map<tpixel, int="">::const_iterator it = m_colorMap.begin();</tpixel,>
Toshihiro Shimizu 890ddd
	TPalette::Page *page = m_palette->getPage(0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QList<int> stylesToBeAddedToPage;</int>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (; it != m_colorMap.end(); ++it) {
Toshihiro Shimizu 890ddd
		if (it->second > m_maxPaletteIndex) //colore nuovo da aggiungere alla paletta)
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			if (m_palette->getStyleCount() > it->second)
Toshihiro Shimizu 890ddd
				m_palette->setStyle(it->second, it->first);
Toshihiro Shimizu 890ddd
			else {
Toshihiro Shimizu 890ddd
				while (m_palette->getStyleCount() < it->second)
Toshihiro Shimizu 890ddd
					m_palette->addStyle(TPixel::Transparent);
Toshihiro Shimizu 890ddd
				int id = m_palette->addStyle(it->first);
Toshihiro Shimizu 890ddd
				assert(id == it->second);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (!m_palette->getStylePage(it->second))
Toshihiro Shimizu 890ddd
			stylesToBeAddedToPage.push_back(it->second);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*- インデックス順にページに格納する -*/
Toshihiro Shimizu 890ddd
	if (!stylesToBeAddedToPage.isEmpty()) {
Toshihiro Shimizu 890ddd
		qSort(stylesToBeAddedToPage.begin(), stylesToBeAddedToPage.end());
Toshihiro Shimizu 890ddd
		for (int s = 0; s < stylesToBeAddedToPage.size(); s++)
Toshihiro Shimizu 890ddd
			page->addStyle(stylesToBeAddedToPage.at(s));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*-- Cleanupデフォルトパレットを追加する --*/
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
		return m_palette;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TIStream is(palettePath);
Toshihiro Shimizu 890ddd
	if (!is)
Toshihiro Shimizu 890ddd
		return m_palette;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	string tagName;
Toshihiro Shimizu 890ddd
	if (!is.matchTag(tagName) || tagName != "palette")
Toshihiro Shimizu 890ddd
		return m_palette;
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
	m_palette->setIsCleanupPalette(false);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPalette::Page *dstPage = m_palette->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 isUsedInDstPalette = 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
				isUsedInDstPalette = true;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (isUsedInDstPalette)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			int addedId = m_palette->addStyle(srcPage->getStyle(srcIndexInPage)->clone());
Toshihiro Shimizu 890ddd
			dstPage->addStyle(addedId);
Toshihiro Shimizu 890ddd
			/*-- StudioPalette由来のDefaultPaletteの場合、GrobalNameを消去する --*/
Toshihiro Shimizu 890ddd
			m_palette->getStyle(addedId)->setGlobalName(L"");
Toshihiro Shimizu 890ddd
			m_palette->getStyle(addedId)->setOriginalName(L"");
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	delete defaultPalette;
Toshihiro Shimizu 890ddd
	/*-- Cleanupデフォルトパレットを追加する ここまで --*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return m_palette;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Convert2Tlv::Convert2Tlv(const TFilePath &filepath1, const TFilePath &filepath2, const TFilePath &outFolder, const QString &outName,
Toshihiro Shimizu 890ddd
						 int from, int to, bool doAutoclose, const TFilePath &palettePath, int colorTolerance,
Toshihiro Shimizu 890ddd
						 int antialiasType, int antialiasValue, bool isUnpaintedFromNAA)
Toshihiro Shimizu 890ddd
	: m_size(0, 0), m_level1(), m_levelIn1(), m_levelIn2(), m_levelOut(), m_autoclose(doAutoclose), m_premultiply(false), m_count(0), m_from(from), m_to(to), m_palettePath(palettePath), m_colorTolerance(colorTolerance), m_palette(0), m_antialiasType(antialiasType), m_antialiasValue(antialiasValue), m_isUnpaintedFromNAA(isUnpaintedFromNAA)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (filepath1 != TFilePath()) {
Toshihiro Shimizu 890ddd
		m_levelIn1 = filepath1.getParentDir() + filepath1.getLevelName();
Toshihiro Shimizu 890ddd
		if (outFolder != TFilePath())
Toshihiro Shimizu 890ddd
			m_levelOut = m_levelIn1.withParentDir(outFolder).withNoFrame().withType("tlv");
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			m_levelOut = m_levelIn1.withNoFrame().withType("tlv"); //filePaths[0].getParentDir() + TFilePath(filePaths[0].getWideName() + L".tlv");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (outName != "")
Toshihiro Shimizu 890ddd
			m_levelOut = m_levelOut.withName(outName.toStdString());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (filepath2 != TFilePath())
Toshihiro Shimizu 890ddd
		m_levelIn2 = filepath2.getParentDir() + filepath2.getLevelName();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int Convert2Tlv::getFramesToConvertCount()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_level1 && m_level1->getFrameCount() > 0)
Toshihiro Shimizu 890ddd
		return getFramesCount(m_level1, m_from, m_to); //m_level1->getFrameCount();
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		try {
Toshihiro Shimizu 890ddd
			TLevelReaderP lr = TLevelReaderP(m_levelIn1);
Toshihiro Shimizu 890ddd
			if (lr) {
Toshihiro Shimizu 890ddd
				TLevelP l = lr->loadInfo();
Toshihiro Shimizu 890ddd
				if (l) {
Toshihiro Shimizu 890ddd
					return getFramesCount(l, m_from, m_to);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} catch (...) {
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool Convert2Tlv::init(string &errorMessage)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_lastIndex = m_maxPaletteIndex = 0;
Toshihiro Shimizu 890ddd
	m_colorMap.clear();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		m_lr1 = TLevelReaderP(m_levelIn1);
Toshihiro Shimizu 890ddd
		if (m_lr1)
Toshihiro Shimizu 890ddd
			m_level1 = m_lr1->loadInfo();
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		errorMessage = "Error: can't read level " + toString(m_levelIn1.getWideString());
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_level1->getFrameCount() == 0) {
Toshihiro Shimizu 890ddd
		errorMessage = "Error: can't find level " + toString(m_levelIn1.getWideString());
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TLevelP level2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_levelIn2 != TFilePath()) {
Toshihiro Shimizu 890ddd
		try {
Toshihiro Shimizu 890ddd
			m_lr2 = TLevelReaderP(m_levelIn2);
Toshihiro Shimizu 890ddd
			if (m_lr2)
Toshihiro Shimizu 890ddd
				level2 = m_lr2->loadInfo();
Toshihiro Shimizu 890ddd
		} catch (...) {
Toshihiro Shimizu 890ddd
			errorMessage = "Error: can't read level " + toString(m_levelIn2.getWideString());
Toshihiro Shimizu 890ddd
			return false;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (level2->getFrameCount() == 0) {
Toshihiro Shimizu 890ddd
			errorMessage = "Error: can't find level " + toString(m_levelIn2.getWideString());
Toshihiro Shimizu 890ddd
			return false;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (m_level1->getFrameCount() != level2->getFrameCount()) {
Toshihiro Shimizu 890ddd
			errorMessage = "Error: the two input levels must have same frame number";
Toshihiro Shimizu 890ddd
			return false;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_size = TDimension();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_lw = TLevelWriterP(m_levelOut);
Toshihiro Shimizu 890ddd
	m_it = m_level1->begin();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TLevel::Iterator it2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (level2->getFrameCount() > 0)
Toshihiro Shimizu 890ddd
		it2 = level2->begin();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (; m_it != m_level1->end(); ++m_it) {
Toshihiro Shimizu 890ddd
		TImageReaderP ir1 = m_lr1->getFrameReader(m_it->first);
Toshihiro Shimizu 890ddd
		const TImageInfo *info1 = ir1->getImageInfo();
Toshihiro Shimizu 890ddd
		if (!info1) {
Toshihiro Shimizu 890ddd
			errorMessage = "Error: can't read frame " + toString(m_it->first.getNumber()) + " of level  " + toString(m_levelIn1.getWideString());
Toshihiro Shimizu 890ddd
			return false;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (info1->m_bitsPerSample != 8) {
Toshihiro Shimizu 890ddd
			errorMessage = "Error: all frames must have 8 bits per channel!\n";
Toshihiro Shimizu 890ddd
			return false;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		m_size.lx = tmax(m_size.lx, info1->m_lx);
Toshihiro Shimizu 890ddd
		m_size.ly = tmax(m_size.ly, info1->m_ly);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (m_lr2 != TLevelReaderP()) {
Toshihiro Shimizu 890ddd
			TImageReaderP ir2 = m_lr2->getFrameReader(it2->first);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (ir2) {
Toshihiro Shimizu 890ddd
				const TImageInfo *info2 = ir2->getImageInfo();
Toshihiro Shimizu 890ddd
				if (!info1) {
Toshihiro Shimizu 890ddd
					errorMessage = "Error: can't read frame " + toString(it2->first.getNumber()) + " of level  " + toString(m_levelIn2.getWideString());
Toshihiro Shimizu 890ddd
					;
Toshihiro Shimizu 890ddd
					return false;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (info1->m_lx != info2->m_lx || info1->m_ly != info2->m_ly) {
Toshihiro Shimizu 890ddd
					errorMessage = "Error: painted frames must have same resolution of matching unpainted frames!\n";
Toshihiro Shimizu 890ddd
					return false;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				if (info2->m_bitsPerSample != 8) {
Toshihiro Shimizu 890ddd
					errorMessage = "Error: all frames must have 8 bits per channel!\n";
Toshihiro Shimizu 890ddd
					return false;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			++it2;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_palette = new TPalette();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_palettePath != TFilePath()) {
Toshihiro Shimizu 890ddd
		TIStream is(m_palettePath);
Toshihiro Shimizu 890ddd
		is >> m_palette;
Toshihiro Shimizu 890ddd
		if (m_palette->getStyleInPagesCount() == 0) {
Toshihiro Shimizu 890ddd
			errorMessage = "Error: invalid palette!\n";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			return false;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		for (int i = 0; i < m_palette->getStyleCount(); i++)
Toshihiro Shimizu 890ddd
			if (m_palette->getStylePage(i)) {
Toshihiro Shimizu 890ddd
				m_colorMap[m_palette->getStyle(i)->getMainColor()] = i;
Toshihiro Shimizu 890ddd
				if (i > m_lastIndex)
Toshihiro Shimizu 890ddd
					m_lastIndex = i;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		assert(m_colorMap.size() == m_palette->getStyleInPagesCount());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_maxPaletteIndex = m_lastIndex;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_it = m_level1->begin();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*- 既定のパレットを作る。黒、赤、青、緑の順 -*/
Toshihiro Shimizu 890ddd
	m_colorMap[TPixel::Black] = ++m_lastIndex;
Toshihiro Shimizu 890ddd
	m_colorMap[TPixel::Red] = ++m_lastIndex;
Toshihiro Shimizu 890ddd
	m_colorMap[TPixel::Blue] = ++m_lastIndex;
Toshihiro Shimizu 890ddd
	m_colorMap[TPixel::Green] = ++m_lastIndex;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool Convert2Tlv::convertNext(string &errorMessage)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_count == 0 && m_from != -1)
Toshihiro Shimizu 890ddd
		while (m_it != m_level1->end() && m_it->first.getNumber() < m_from)
Toshihiro Shimizu 890ddd
			m_it++;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::cout << "Processing image " << ++m_count << " of " << getFramesCount(m_level1, m_from, m_to) << "...\n";
Toshihiro Shimizu 890ddd
	std::cout << "      Loading frame " << m_it->first.getNumber() << "...\n";
Toshihiro Shimizu 890ddd
	TImageReaderP ir1 = m_lr1->getFrameReader(m_it->first);
Toshihiro Shimizu 890ddd
	TRasterImageP imgIn1 = (TRasterImageP)ir1->load();
Toshihiro Shimizu 890ddd
	if (!imgIn1) {
Toshihiro Shimizu 890ddd
		errorMessage = "Error: cannot read frame" + toString(m_it->first.getNumber()) + " of " + toString(m_levelIn1.getWideString()) + "!";
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TRasterP rin1 = imgIn1->getRaster();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert((TRaster32P)rin1 || (TRasterGR8P)rin1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterP rin2;
Toshihiro Shimizu 890ddd
	TRasterImageP imgIn2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_lr2) {
Toshihiro Shimizu 890ddd
		TImageReaderP ir2 = m_lr2->getFrameReader(m_it->first);
Toshihiro Shimizu 890ddd
		imgIn2 = (TRasterImageP)ir2->load();
Toshihiro Shimizu 890ddd
		if (!imgIn2) {
Toshihiro Shimizu 890ddd
			errorMessage = "Error: cannot read frame " + toString(m_it->first.getNumber()) + " of " + toString(m_levelIn2.getWideString()) + "!";
Toshihiro Shimizu 890ddd
			return false;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		rin2 = imgIn2->getRaster();
Toshihiro Shimizu 890ddd
		assert((TRaster32P)rin2 || (TRasterGR8P)rin2);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterCM32P rout(m_size);
Toshihiro Shimizu 890ddd
	buildToonzRaster(rout, rin1, rin2);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::cout << "      saving frame in level \'" << m_levelOut.getLevelName() << "\'...\n\n";
Toshihiro Shimizu 890ddd
	TImageWriterP iw = m_lw->getFrameWriter(m_it->first);
Toshihiro Shimizu 890ddd
	TToonzImageP timg = TToonzImageP(rout, rout->getBounds());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRect bbox;
Toshihiro Shimizu 890ddd
	TRop::computeBBox(rout, bbox);
Toshihiro Shimizu 890ddd
	timg->setSavebox(bbox);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double dpix, dpiy;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	imgIn1->getDpi(dpix, dpiy);
Toshihiro Shimizu 890ddd
	timg->setDpi(dpix, dpiy);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TLevel::Iterator itaux = m_it;
Toshihiro Shimizu 890ddd
	itaux++;
Toshihiro Shimizu 890ddd
	if (itaux == m_level1->end()) // ultimo frame da scrivere.
Toshihiro Shimizu 890ddd
		timg->setPalette(buildPalette());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	iw->save(timg);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	++m_it;
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool Convert2Tlv::abort()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		m_lr1 = TLevelReaderP();
Toshihiro Shimizu 890ddd
		m_lr2 = TLevelReaderP();
Toshihiro Shimizu 890ddd
		m_lw = TLevelWriterP();
Toshihiro Shimizu 890ddd
		m_level1 = TLevelP();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		std::cout << "No output generated\n";
Toshihiro Shimizu 890ddd
		TSystem::deleteFile(m_levelOut);
Toshihiro Shimizu 890ddd
		TSystem::deleteFile(m_levelOut.withType("tpl"));
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// RasterToToonzRasterConverter
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
RasterToToonzRasterConverter::RasterToToonzRasterConverter()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_palette = new TPalette();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
RasterToToonzRasterConverter::~RasterToToonzRasterConverter()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void RasterToToonzRasterConverter::setPalette(const TPaletteP &palette)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_palette = palette;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TRasterCM32P RasterToToonzRasterConverter::convert(const TRasterP &inputRaster)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int lx = inputRaster->getLx();
Toshihiro Shimizu 890ddd
	int ly = inputRaster->getLy();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRaster32P r = inputRaster;
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
  TRasterGR8P r1gr = (TRasterGR8P)inputRaster;
Toshihiro Shimizu 890ddd
  TRasterP rU, rP;
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterCM32P rout(lx, ly);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int y = 0; y < ly; y++) {
Toshihiro Shimizu 890ddd
		TPixel32 *pixin = r->pixels(y);
Toshihiro Shimizu 890ddd
		TPixel32 *pixinEnd = pixin + lx;
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixout = rout->pixels(y);
Toshihiro Shimizu 890ddd
		while (pixin < pixinEnd) {
Toshihiro Shimizu 890ddd
			int v = (pixin->r + pixin->g + pixin->b) / 3;
Toshihiro Shimizu 890ddd
			++pixin;
Toshihiro Shimizu 890ddd
			*pixout++ = TPixelCM32(1, 0, v);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return rout;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TRasterCM32P RasterToToonzRasterConverter::convert(const TRasterP &inksInputRaster, const TRasterP &paintInputRaster)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return TRasterCM32P();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TToonzImageP RasterToToonzRasterConverter::convert(const TRasterImageP &ri)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRasterCM32P ras = convert(ri->getRaster());
Toshihiro Shimizu 890ddd
	if (ras)
Toshihiro Shimizu 890ddd
		return TToonzImageP(ras, TRect(ras->getBounds()));
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return TToonzImageP();
Toshihiro Shimizu 890ddd
}