Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <set></set>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tropcm.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tcolorstyles.h"
Toshihiro Shimizu 890ddd
#include "tpixelutils.h"
Toshihiro Shimizu 890ddd
#include "ttile.h"
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
#include "timage_io.h"
Toshihiro Shimizu 890ddd
#include "trasterimage.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//#include "tlevel.h"
Toshihiro Shimizu 890ddd
//#include "ttoonzimage.h"
Toshihiro Shimizu 890ddd
//#include "tgeometry.h"
Toshihiro Shimizu 890ddd
//#include "timage_io.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
extern "C" {
Toshihiro Shimizu 890ddd
#include "toonz4.6/raster.h"
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#define USE_SSE2
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef USE_SSE2
Toshihiro Shimizu 890ddd
#include <emmintrin.h></emmintrin.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
__declspec(align(16)) class TPixelFloat
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	TPixelFloat() : b(0), g(0), r(0), m(0) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPixelFloat(float rr, float gg, float bb, float mm)
Toshihiro Shimizu 890ddd
		: b(bb), g(gg), r(rr), m(mm) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPixelFloat(const TPixel32 &pix)
Toshihiro Shimizu 890ddd
		: b(pix.b), g(pix.g), r(pix.r), m(pix.m) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	float b, g, r, m;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // anonymous namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool renderRas32(const TTile &tileOut, const TTile &tileIn, const TPaletteP palette);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const TPixel32 c_transparencyCheckPaint = TPixel32(80, 80, 80, 255);
Toshihiro Shimizu 890ddd
const TPixel32 c_transparencyCheckInk = TPixel32::Black;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::convert(const TRaster32P &rasOut,
Toshihiro Shimizu 890ddd
				   const TRasterCM32P &rasIn,
Toshihiro Shimizu 890ddd
				   const TPaletteP palette,
Toshihiro Shimizu 890ddd
				   bool transparencyCheck)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int count = palette->getStyleCount();
Shinya Kitaoka 12c444
	int count2 = std::max({count, TPixelCM32::getMaxInk(), TPixelCM32::getMaxPaint()});
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// per poter utilizzare lo switch (piu' efficiente) si utilizza 255
Toshihiro Shimizu 890ddd
	// anziche' TPixelCM32::getMaxTone()
Toshihiro Shimizu 890ddd
	assert(TPixelCM32::getMaxTone() == 255);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int rasLx = rasOut->getLx();
Toshihiro Shimizu 890ddd
	int rasLy = rasOut->getLy();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	rasOut->lock();
Toshihiro Shimizu 890ddd
	rasIn->lock();
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
	if (TSystem::getCPUExtensions() & TSystem::CpuSupportsSse2) {
Toshihiro Shimizu 890ddd
		__m128i zeros = _mm_setzero_si128();
Toshihiro Shimizu 890ddd
		TPixelFloat *paints = (TPixelFloat *)_aligned_malloc(count2 * sizeof(TPixelFloat), 16);
Toshihiro Shimizu 890ddd
		TPixelFloat *inks = (TPixelFloat *)_aligned_malloc(count2 * sizeof(TPixelFloat), 16);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		std::vector<tpixel32> paints2(count2);</tpixel32>
Toshihiro Shimizu 890ddd
		std::vector<tpixel32> inks2(count2);</tpixel32>
Toshihiro Shimizu 890ddd
		if (transparencyCheck) {
Toshihiro Shimizu 890ddd
			for (int i = 0; i < palette->getStyleCount(); i++) {
Toshihiro Shimizu 890ddd
				paints2[i] = c_transparencyCheckPaint;
Toshihiro Shimizu 890ddd
				inks2[i] = c_transparencyCheckInk;
Toshihiro Shimizu 890ddd
				paints[i] = TPixelFloat(paints2[i]);
Toshihiro Shimizu 890ddd
				inks[i] = TPixelFloat(inks2[i]);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			paints2[0] = TPixel32::Transparent;
Toshihiro Shimizu 890ddd
			paints[0] = TPixelFloat(TPixel32::Transparent);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		/*
Toshihiro Shimizu 890ddd
   else if (true)
Toshihiro Shimizu 890ddd
      {
Toshihiro Shimizu 890ddd
			for(int i=0;i<palette->getStyleCount();i++)</palette->
Toshihiro Shimizu 890ddd
        {
Toshihiro Shimizu 890ddd
        paints2[i] = c_transparencyCheckPaint;
Toshihiro Shimizu 890ddd
				inks2[i]   = c_transparencyCheckInk;
Toshihiro Shimizu 890ddd
        paints[i]  = TPixelFloat(paints2[i]);
Toshihiro Shimizu 890ddd
				inks[i]    = TPixelFloat(inks2[i]);
Toshihiro Shimizu 890ddd
  			paints2[i] = TPixel32::Transparent;
Toshihiro Shimizu 890ddd
	  		paints[i] = TPixelFloat(TPixel32::Transparent);
Toshihiro Shimizu 890ddd
        }
Toshihiro Shimizu 890ddd
			paints2[0] = TPixel32::Transparent;
Toshihiro Shimizu 890ddd
			paints[0] = TPixelFloat(TPixel32::Transparent);
Toshihiro Shimizu 890ddd
     }
Toshihiro Shimizu 890ddd
     */
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			for (int i = 0; i < palette->getStyleCount(); i++) {
Toshihiro Shimizu 890ddd
				TPixel32 color = ::premultiply(palette->getStyle(i)->getAverageColor());
Toshihiro Shimizu 890ddd
				paints[i] = inks[i] = TPixelFloat(color);
Toshihiro Shimizu 890ddd
				paints2[i] = inks2[i] = color;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		float maxTone = (float)TPixelCM32::getMaxTone();
Toshihiro Shimizu 890ddd
		__m128 den_packed = _mm_load1_ps(&maxTone);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (int y = 0; y < rasLy; ++y) {
Toshihiro Shimizu 890ddd
			TPixel32 *pix32 = rasOut->pixels(y);
Toshihiro Shimizu 890ddd
			TPixelCM32 *pixIn = rasIn->pixels(y);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TPixelCM32 *endPixIn = pixIn + rasLx;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			while (pixIn < endPixIn) {
Toshihiro Shimizu 890ddd
				int tt = pixIn->getTone();
Toshihiro Shimizu 890ddd
				int p = pixIn->getPaint();
Toshihiro Shimizu 890ddd
				int i = pixIn->getInk();
Toshihiro Shimizu 890ddd
				switch (tt) {
Toshihiro Shimizu 890ddd
				case 255:
Toshihiro Shimizu 890ddd
					*pix32++ = paints2[p];
Toshihiro Shimizu 890ddd
					break;
Toshihiro Shimizu 890ddd
				case 0:
Toshihiro Shimizu 890ddd
					*pix32++ = inks2[i];
Toshihiro Shimizu 890ddd
					break;
Toshihiro Shimizu 890ddd
				default: {
Toshihiro Shimizu 890ddd
					float t = (float)tt;
Toshihiro Shimizu 890ddd
					__m128 a_packed = _mm_load_ps((float *)&(inks[i]));
Toshihiro Shimizu 890ddd
					__m128 b_packed = _mm_load_ps((float *)&(paints[p]));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					__m128 num_packed = _mm_load1_ps(&t);
Toshihiro Shimizu 890ddd
					__m128 diff_packed = _mm_sub_ps(den_packed, num_packed);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					// calcola in modo vettoriale out = ((den-num)*a + num*b)/den
Toshihiro Shimizu 890ddd
					__m128 outPix_packed = _mm_mul_ps(diff_packed, a_packed);
Toshihiro Shimizu 890ddd
					__m128 tmpPix_packed = _mm_mul_ps(num_packed, b_packed);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					outPix_packed = _mm_add_ps(outPix_packed, tmpPix_packed);
Toshihiro Shimizu 890ddd
					outPix_packed = _mm_div_ps(outPix_packed, den_packed);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					// converte i canali da float a char
Toshihiro Shimizu 890ddd
					__m128i outPix_packed_i = _mm_cvtps_epi32(outPix_packed);
Toshihiro Shimizu 890ddd
					outPix_packed_i = _mm_packs_epi32(outPix_packed_i, zeros);
Toshihiro Shimizu 890ddd
					outPix_packed_i = _mm_packus_epi16(outPix_packed_i, zeros);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					*(DWORD *)(pix32) = _mm_cvtsi128_si32(outPix_packed_i);
Toshihiro Shimizu 890ddd
					++pix32;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				++pixIn;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		_aligned_free(paints);
Toshihiro Shimizu 890ddd
		_aligned_free(inks);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	} else // SSE2 not supported
Shinya Kitaoka 9f5a1b
#endif	 // _WIN32
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		std::vector<tpixel32> paints(count2, TPixel32(255, 0, 0));</tpixel32>
Toshihiro Shimizu 890ddd
		std::vector<tpixel32> inks(count2, TPixel32(255, 0, 0));</tpixel32>
Toshihiro Shimizu 890ddd
		if (transparencyCheck) {
Toshihiro Shimizu 890ddd
			for (int i = 0; i < palette->getStyleCount(); i++) {
Toshihiro Shimizu 890ddd
				paints[i] = c_transparencyCheckPaint;
Toshihiro Shimizu 890ddd
				inks[i] = c_transparencyCheckInk;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			paints[0] = TPixel32::Transparent;
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			for (int i = 0; i < palette->getStyleCount(); i++)
Toshihiro Shimizu 890ddd
				paints[i] = inks[i] = ::premultiply(palette->getStyle(i)->getAverageColor());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (int y = 0; y < rasLy; ++y) {
Toshihiro Shimizu 890ddd
			TPixel32 *pix32 = rasOut->pixels(y);
Toshihiro Shimizu 890ddd
			TPixelCM32 *pixIn = rasIn->pixels(y);
Toshihiro Shimizu 890ddd
			TPixelCM32 *endPixIn = pixIn + rasLx;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			while (pixIn < endPixIn) {
Toshihiro Shimizu 890ddd
				int t = pixIn->getTone();
Toshihiro Shimizu 890ddd
				int p = pixIn->getPaint();
Toshihiro Shimizu 890ddd
				int i = pixIn->getInk();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (t == TPixelCM32::getMaxTone())
Toshihiro Shimizu 890ddd
					*pix32++ = paints[p];
Toshihiro Shimizu 890ddd
				else if (t == 0)
Toshihiro Shimizu 890ddd
					*pix32++ = inks[i];
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					*pix32++ = blend(inks[i], paints[p], t, TPixelCM32::getMaxTone());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				++pixIn;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	rasOut->unlock();
Toshihiro Shimizu 890ddd
	rasIn->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void do_convert(const TTile &dst, const TTile &src,
Toshihiro Shimizu 890ddd
				const TPaletteP palette, bool transparencyCheck,
Toshihiro Shimizu 890ddd
				bool applyFx)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//assert(palette);
Toshihiro Shimizu 890ddd
	//assert(_rasOut && _rasIn);
Toshihiro Shimizu 890ddd
	//assert(rasOut->getSize() == rasIn->getSize());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (applyFx && renderRas32(dst, src, palette))
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRaster32P rasOut;
Toshihiro Shimizu 890ddd
	TRasterCM32P rasIn;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (dst.getRaster()->getSize() != src.getRaster()->getSize()) {
Toshihiro Shimizu 890ddd
		TRect rect = TRect(convert(dst.m_pos), dst.getRaster()->getSize()) * TRect(convert(src.m_pos), src.getRaster()->getSize());
Toshihiro Shimizu 890ddd
		TRect rectOut = rect - convert(dst.m_pos);
Toshihiro Shimizu 890ddd
		rasOut = dst.getRaster()->extract(rectOut);
Toshihiro Shimizu 890ddd
		TRect rectIn = rect - convert(src.m_pos);
Toshihiro Shimizu 890ddd
		rasIn = src.getRaster()->extract(rectIn);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		rasOut = dst.getRaster();
Toshihiro Shimizu 890ddd
		rasIn = src.getRaster();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TRop::convert(rasOut, rasIn, palette, transparencyCheck);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::convert(const TRaster32P &rasOut,
Toshihiro Shimizu 890ddd
				   const TRasterCM32P &rasIn,
Toshihiro Shimizu 890ddd
				   TPaletteP palette,
Toshihiro Shimizu 890ddd
				   const TRect &theClipRect, // il rect su cui e' applicata la conversione
Toshihiro Shimizu 890ddd
				   bool transparencyCheck,
Toshihiro Shimizu 890ddd
				   bool applyFx)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(palette);
Toshihiro Shimizu 890ddd
	assert(rasIn && rasOut);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRect clipRect(theClipRect);
Toshihiro Shimizu 890ddd
	if (clipRect.isEmpty())
Toshihiro Shimizu 890ddd
		clipRect = rasIn->getBounds();
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		if (!clipRect.overlaps(rasIn->getBounds()))
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		clipRect = clipRect * rasIn->getBounds();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (clipRect.isEmpty())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRect clipRectIn, clipRectOut;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (applyFx && palette->getFxRects(clipRect, clipRectIn, clipRectOut)) {
Toshihiro Shimizu 890ddd
		TRect rAux = clipRectIn;
Toshihiro Shimizu 890ddd
		TRasterP rAuxIn = rasIn->extract(clipRectIn); //la extract modifica clipRectIn
Toshihiro Shimizu 890ddd
		if (rAux != clipRectIn && rAux != rasIn->getBounds()) {
Toshihiro Shimizu 890ddd
			TRasterCM32P rNew(rAux.getSize());
Toshihiro Shimizu 890ddd
			TRect tmpRect = clipRectIn - rAux.getP00();
Toshihiro Shimizu 890ddd
			rNew->extract(tmpRect)->copy(rAuxIn);
Toshihiro Shimizu 890ddd
			rAuxIn = rNew;
Toshihiro Shimizu 890ddd
			clipRectIn = rAux;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TTile tileIn(rAuxIn, ::convert(clipRectIn.getP00()));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		rAux = clipRectOut;
Toshihiro Shimizu 890ddd
		TRasterP rAuxOut = rasOut->extract(clipRectOut); //la extract modifica clipRectOut
Toshihiro Shimizu 890ddd
		TTile tileOut(rAuxOut, ::convert(clipRectOut.getP00()));
Toshihiro Shimizu 890ddd
		TRop::convert(tileOut, tileIn, palette, transparencyCheck, true);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		TRect clipRectIn = clipRect;
Toshihiro Shimizu 890ddd
		TRect clipRectOut = clipRect;
Toshihiro Shimizu 890ddd
		TRasterP _rasOut = rasOut->extract(clipRectOut);
Toshihiro Shimizu 890ddd
		TRasterP _rasIn = rasIn->extract(clipRectIn);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TTile t1(_rasOut, ::convert(clipRectOut.getP00()));
Toshihiro Shimizu 890ddd
		TTile t2(_rasIn, ::convert(clipRectIn.getP00()));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TRop::convert(t1, t2,
Toshihiro Shimizu 890ddd
					  palette, transparencyCheck, false);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::convert(const TTile &dst, const TTile &src, const TPaletteP plt, bool transparencyCheck, bool applyFxs)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//if (dst->getSize() != src->getSize())
Toshihiro Shimizu 890ddd
	//  throw TRopException("convert: size mismatch");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//assert(plt);
Toshihiro Shimizu 890ddd
	if ((TRaster32P)dst.getRaster())
Toshihiro Shimizu 890ddd
		do_convert(dst, src, plt, transparencyCheck, applyFxs);
Toshihiro Shimizu 890ddd
	else if ((TRaster64P)dst.getRaster()) {
Toshihiro Shimizu 890ddd
		TRaster32P aux(dst.getRaster()->getLx(), dst.getRaster()->getLy());
Toshihiro Shimizu 890ddd
		TTile taux(aux, dst.m_pos);
Toshihiro Shimizu 890ddd
		do_convert(taux, src, plt, transparencyCheck, applyFxs);
Toshihiro Shimizu 890ddd
		TRop::convert(dst.getRaster(), aux);
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		throw TRopException("unsupported pixel type");
Toshihiro Shimizu 890ddd
}
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
Toshihiro Shimizu 890ddd
TRasterP putSinglePaintInRaster(TRasterCM32P &rasIn, int paintId, TPixel32 color)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P rasOut;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int y = 0; y < rasIn->getLy(); y++) {
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixIn = rasIn->pixels(y);
Toshihiro Shimizu 890ddd
		TPixelCM32 *endPix = pixIn + rasIn->getLx();
Toshihiro Shimizu 890ddd
		TPixel32 *pixOut = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (rasOut)
Toshihiro Shimizu 890ddd
			pixOut = rasOut->pixels(y);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		while (pixIn < endPix) {
Toshihiro Shimizu 890ddd
			if (pixIn->getPaint() == paintId) {
Toshihiro Shimizu 890ddd
				if (!rasOut) {
Toshihiro Shimizu 890ddd
					rasOut = TRaster32P(rasIn->getLx(), rasIn->getLy());
Toshihiro Shimizu 890ddd
					rasOut->fill(TPixel32::Transparent);
Toshihiro Shimizu 890ddd
					pixOut = rasOut->pixels(y) + (pixIn - rasIn->pixels(y));
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (!pixIn->isPureInk())
Toshihiro Shimizu 890ddd
					*pixOut = color;
Toshihiro Shimizu 890ddd
				/*else if (!pixIn->isPureInk())
Toshihiro Shimizu 890ddd
			  {
Toshihiro Shimizu 890ddd
				assert(TPixelCM32::getMaxTone()==0xff);
Toshihiro Shimizu 890ddd
				pixOut->m = pixIn->getTone();
Toshihiro Shimizu 890ddd
				pixOut->r = 255*color.r/pixOut->m;
Toshihiro Shimizu 890ddd
				pixOut->g = 255*color.g/pixOut->m;
Toshihiro Shimizu 890ddd
				pixOut->b = 255*color.b/pixOut->m;
Toshihiro Shimizu 890ddd
        }*/
Toshihiro Shimizu 890ddd
				pixIn->setPaint(0);
Toshihiro Shimizu 890ddd
				//pixIn->setTone(0);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			pixIn++;
Toshihiro Shimizu 890ddd
			pixOut++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return rasOut;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TRasterP putSingleInkInRasterGR8(TRasterCM32P &rasIn, int inkId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRasterGR8P rasOut;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int y = 0; y < rasIn->getLy(); y++) {
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixIn = rasIn->pixels(y);
Toshihiro Shimizu 890ddd
		TPixelCM32 *endPix = pixIn + rasIn->getLx();
Toshihiro Shimizu 890ddd
		TPixelGR8 *pixOut;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (rasOut)
Toshihiro Shimizu 890ddd
			pixOut = rasOut->pixels(y);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		while (pixIn < endPix) {
Toshihiro Shimizu 890ddd
			if (pixIn->getInk() == inkId) {
Toshihiro Shimizu 890ddd
				assert(TPixelCM32::getMaxTone() == 0xff);
Toshihiro Shimizu 890ddd
				if (!rasOut) {
Toshihiro Shimizu 890ddd
					rasOut = TRasterGR8P(rasIn->getLx(), rasIn->getLy());
Toshihiro Shimizu 890ddd
					rasOut->fill(0);
Toshihiro Shimizu 890ddd
					pixOut = rasOut->pixels(y) + (pixIn - rasIn->pixels(y));
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				*pixOut = 255 - pixIn->getTone();
Toshihiro Shimizu 890ddd
				//pixIn->setInk(0);
Toshihiro Shimizu 890ddd
				pixIn->setTone(TPixelCM32::getMaxTone());
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			pixIn++;
Toshihiro Shimizu 890ddd
			pixOut++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return rasOut;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
TRasterP putSingleInkInRasterRGBM(TRasterCM32P &rasIn, int inkId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P rasOut;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int y = 0; y < rasIn->getLy(); y++) {
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixIn = rasIn->pixels(y);
Toshihiro Shimizu 890ddd
		TPixelCM32 *endPix = pixIn + rasIn->getLx();
Toshihiro Shimizu 890ddd
		TPixel *pixOut;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (rasOut)
Toshihiro Shimizu 890ddd
			pixOut = rasOut->pixels(y);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		while (pixIn < endPix) {
Toshihiro Shimizu 890ddd
			if (pixIn->getInk() == inkId) {
Toshihiro Shimizu 890ddd
				assert(TPixelCM32::getMaxTone() == 0xff);
Toshihiro Shimizu 890ddd
				if (!rasOut) {
Toshihiro Shimizu 890ddd
					rasOut = TRaster32P(rasIn->getLx(), rasIn->getLy());
Toshihiro Shimizu 890ddd
					rasOut->fill(TPixel32::Transparent);
Toshihiro Shimizu 890ddd
					pixOut = rasOut->pixels(y) + (pixIn - rasIn->pixels(y));
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				pixOut->r = pixOut->g = pixOut->b = pixOut->m = 255 - pixIn->getTone();
Toshihiro Shimizu 890ddd
				//pixIn->setInk(0);
Toshihiro Shimizu 890ddd
				pixIn->setTone(TPixelCM32::getMaxTone());
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			pixIn++;
Toshihiro Shimizu 890ddd
			if (rasOut)
Toshihiro Shimizu 890ddd
				pixOut++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return rasOut;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//filename!!
Toshihiro Shimizu 890ddd
//interactive!!
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
bool computePaletteFx(const std::vector<std::pair<tcolorstyle *,="" int="">> &fx,</std::pair<tcolorstyle>
Toshihiro Shimizu 890ddd
					  const TTile &tileOut, const TTile &tileIn, const TPaletteP plt)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	TRasterCM32P rasIn = tileIn.getRaster();
Toshihiro Shimizu 890ddd
	TRaster32P rAux32, rasOut = tileOut.getRaster();
Toshihiro Shimizu 890ddd
	TRasterGR8P rAuxGR;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int frame = plt->getFrame();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	std::vector<trasterp> paintLayers(fx.size());</trasterp>
Shinya Kitaoka 3bfa54
	std::vector<trasterp> inkLayers(fx.size());</trasterp>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//tolgo dal raster d'ingresso gli ink e i paint con gli effetti, mettendoli in dei raster layer
Toshihiro Shimizu 890ddd
	for (i = 0; i < (int)fx.size(); i++) {
Toshihiro Shimizu 890ddd
		TRasterStyleFx *rfx = fx[i].first->getRasterStyleFx();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (rfx->isPaintStyle())
Toshihiro Shimizu 890ddd
			paintLayers[i] = putSinglePaintInRaster(rasIn, fx[i].second, fx[i].first->getMainColor());
Toshihiro Shimizu 890ddd
		if (rfx->isInkStyle()) {
Toshihiro Shimizu 890ddd
			if (rfx->inkFxNeedRGBMRaster())
Toshihiro Shimizu 890ddd
				inkLayers[i] = putSingleInkInRasterRGBM(rasIn, fx[i].second);
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				inkLayers[i] = putSingleInkInRasterGR8(rasIn, fx[i].second);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//raster d'ingresso senza i colori fx, viene renderizzato nel raster d'uscita
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	do_convert(tileOut, tileIn, plt, false, false);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//ogni layer viene "effettato".; se il risultato e' non nullo, viene sovrapposto sul raster d'uscita
Toshihiro Shimizu 890ddd
	//prima vengono messi tutti i layer di paint, poi quelli di ink
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRect rectOut = TRect(convert(tileOut.m_pos), tileOut.getRaster()->getSize()) - convert(tileIn.m_pos);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < (int)fx.size(); i++)
Toshihiro Shimizu 890ddd
		if (paintLayers[i]) {
Toshihiro Shimizu 890ddd
			TRasterStyleFx::Params params(paintLayers[i], convert(tileIn.m_pos), rasIn, fx[i].second, frame);
Toshihiro Shimizu 890ddd
			if (fx[i].first->getRasterStyleFx()->compute(params))
Toshihiro Shimizu 890ddd
				TRop::over(rasOut, paintLayers[i]->extract(rectOut), rasOut);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < (int)fx.size(); i++)
Toshihiro Shimizu 890ddd
		if (inkLayers[i]) {
Toshihiro Shimizu 890ddd
			TRasterStyleFx *rfx = fx[i].first->getRasterStyleFx();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TRasterStyleFx::Params params(inkLayers[i], convert(tileIn.m_pos), rasIn, fx[i].second, frame);
Toshihiro Shimizu 890ddd
			if (rfx->compute(params)) {
Toshihiro Shimizu 890ddd
				if (rfx->inkFxNeedRGBMRaster())
Toshihiro Shimizu 890ddd
					TRop::over(rasOut, rasOut, inkLayers[i]->extract(rectOut));
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					TRop::over(rasOut, inkLayers[i]->extract(rectOut), fx[i].first->getMainColor());
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool renderRas32(const TTile &tileOut, const TTile &tileIn, const TPaletteP palette)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(TRect(convert(tileIn.m_pos), tileIn.getRaster()->getSize()).contains(TRect(convert(tileOut.m_pos), tileOut.getRaster()->getSize())));
Toshihiro Shimizu 890ddd
	assert((TRasterCM32P)tileIn.getRaster());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Shrink = shrink;
Toshihiro Shimizu 890ddd
	//INIT_TCM(rin)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* mark up are made apart */
Toshihiro Shimizu 890ddd
	//computeMarkup(rasIn, palette);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	std::vector<std::pair<tcolorstyle *,="" int="">> fxArray;</std::pair<tcolorstyle>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < palette->getStyleCount(); i++)
Toshihiro Shimizu 890ddd
		if (palette->getStyle(i)->isRasterStyle())
Shinya Kitaoka 3bfa54
			fxArray.push_back(std::pair<tcolorstyle *,="" int="">(palette->getStyle(i), i));</tcolorstyle>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (fxArray.empty())
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TTile _tileIn(tileIn.getRaster()->clone(), tileIn.m_pos);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	tileIn.getRaster()->lock();
Toshihiro Shimizu 890ddd
	tileOut.getRaster()->lock();
Toshihiro Shimizu 890ddd
	computePaletteFx(fxArray, tileOut, _tileIn, palette);
Toshihiro Shimizu 890ddd
	tileIn.getRaster()->unlock();
Toshihiro Shimizu 890ddd
	tileOut.getRaster()->unlock();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
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
Toshihiro Shimizu 890ddd
void addColor(TPaletteP plt, int index, const TPaletteP &upPlt, std::map<int, int=""> &usedInks)</int,>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TColorStyle *cs = upPlt->getStyle(index);
Toshihiro Shimizu 890ddd
	if (cs && cs->getMainColor() == plt->getStyle(index)->getMainColor()) {
Toshihiro Shimizu 890ddd
		usedInks[index] = index;
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int firstStyleId = plt->getFirstUnpagedStyle();
Toshihiro Shimizu 890ddd
	if (firstStyleId == -1)
Toshihiro Shimizu 890ddd
		firstStyleId = plt->getStyleCount();
Toshihiro Shimizu 890ddd
	usedInks[index] = firstStyleId;
Toshihiro Shimizu 890ddd
	plt->getPage(0)->addStyle(TPixel32::Red);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void addColor(TPaletteP plt, int index, std::map<int, int=""> &usedInks)</int,>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int firstStyleId = plt->getFirstUnpagedStyle();
Toshihiro Shimizu 890ddd
	if (firstStyleId == -1)
Toshihiro Shimizu 890ddd
		firstStyleId = plt->getStyleCount();
Toshihiro Shimizu 890ddd
	usedInks[index] = firstStyleId;
Toshihiro Shimizu 890ddd
	plt->getPage(0)->addStyle(TPixel32::Red);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
//std::map<int,int>& usedInk  upInkId -> downInkId</int,int>
Toshihiro Shimizu 890ddd
void doMergeCmapped(TRasterCM32P rasOut, const TRasterCM32P &rasUp, const TPaletteP &pltOut, bool onlyInks,
Toshihiro Shimizu 890ddd
					int matchlinePrevalence, std::map<int, int=""> &usedInks)</int,>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double val = matchlinePrevalence / 100.0; //matchlinePrevalence ==0 always ink down; matchlinePrevalence == 100 always ink up;
Toshihiro Shimizu 890ddd
	assert(rasOut && rasUp);
Toshihiro Shimizu 890ddd
	assert(rasOut->getSize() == rasUp->getSize());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPaletteP downPlt = pltOut->clone();
Toshihiro Shimizu 890ddd
	std::map<int, int="">::iterator it;</int,>
Toshihiro Shimizu 890ddd
	for (it = usedInks.begin(); it != usedInks.end(); it++)
Toshihiro Shimizu 890ddd
		downPlt->getPage(0)->addStyle(TPixel32::Red);
Toshihiro Shimizu 890ddd
	for (int y = 0; y < rasOut->getLy(); y++) {
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixDown = rasOut->pixels(y), *endPix = pixDown + rasOut->getLx();
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixUp = rasUp->pixels(y);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		while (pixDown < endPix) {
Toshihiro Shimizu 890ddd
			//there is lines in matchline
Toshihiro Shimizu 890ddd
			if (!pixUp->isPurePaint()) {
Toshihiro Shimizu 890ddd
				int toneDown = pixDown->getTone();
Toshihiro Shimizu 890ddd
				int toneUp = pixUp->getTone();
Toshihiro Shimizu 890ddd
				if (usedInks.find(pixUp->getInk()) == usedInks.end())
Toshihiro Shimizu 890ddd
					addColor(downPlt, pixUp->getInk(), usedInks);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (val == 1) { //Matchline is on top, with gap
Toshihiro Shimizu 890ddd
					pixDown->setTone(toneUp);
Toshihiro Shimizu 890ddd
					pixDown->setInk(usedInks[pixUp->getInk()]);
Toshihiro Shimizu 890ddd
				} else if (val == 0) { //Matchline is on bottom, with gap
Toshihiro Shimizu 890ddd
					if (pixDown->isPurePaint()) {
Toshihiro Shimizu 890ddd
						pixDown->setTone(toneUp);
Toshihiro Shimizu 890ddd
						pixDown->setInk(usedInks[pixUp->getInk()]);
Toshihiro Shimizu 890ddd
					} else {
Toshihiro Shimizu 890ddd
						//*pixOut = *pixDown;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				} else {
Toshihiro Shimizu 890ddd
					if ((val > 0 && toneUp < toneDown) || (val == 0 && toneDown == 255)) //(toneUp
Toshihiro Shimizu 890ddd
						pixDown->setTone(toneUp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					if ((255 - toneDown) * (1 - val) <= val * (255 - toneUp - 1)) //val==0 -> if (toneDown== 255)....
Toshihiro Shimizu 890ddd
						//val==0.5 -> if (toneup
Toshihiro Shimizu 890ddd
						//val==1 -> if (toneup<255)...
Toshihiro Shimizu 890ddd
						pixDown->setInk(usedInks[pixUp->getInk()]);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			int paintIndex;
Toshihiro Shimizu 890ddd
			if (!onlyInks && (paintIndex = pixUp->getPaint()) > 0) {
Toshihiro Shimizu 890ddd
				if (usedInks.find(paintIndex) == usedInks.end())
Toshihiro Shimizu 890ddd
					addColor(downPlt, paintIndex, usedInks);
Toshihiro Shimizu 890ddd
				pixDown->setPaint(usedInks[paintIndex]);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			pixUp++;
Toshihiro Shimizu 890ddd
			pixDown++;
Toshihiro Shimizu 890ddd
			//pixOut++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void addColors(const TPixelCM32 &color, TPaletteP plt, const TPaletteP &upPlt, std::map<int, int=""> &usedColors)</int,>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (usedColors.find(color.getInk()) == usedColors.end())
Toshihiro Shimizu 890ddd
		addColor(plt, color.getInk(), upPlt, usedColors);
Toshihiro Shimizu 890ddd
	if (usedColors.find(color.getPaint()) == usedColors.end())
Toshihiro Shimizu 890ddd
		addColor(plt, color.getPaint(), upPlt, usedColors);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
void addColors(const TPixelCM32 &color, TPaletteP plt, std::map<int, int=""> &usedColors)</int,>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (usedColors.find(color.getInk()) == usedColors.end())
Toshihiro Shimizu 890ddd
		addColor(plt, color.getInk(), usedColors);
Toshihiro Shimizu 890ddd
	if (usedColors.find(color.getPaint()) == usedColors.end())
Toshihiro Shimizu 890ddd
		addColor(plt, color.getPaint(), usedColors);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void doApplyMatchLines(TRasterCM32P rasOut, const TRasterCM32P &rasUp, int inkIndex, int matchlinePrevalence)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double val = matchlinePrevalence / 100.0; //matchlinePrevalence ==0->always ink down; matchlinePrevalence == 100 always ink up;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(rasOut && rasUp);
Toshihiro Shimizu 890ddd
	assert(rasOut->getSize() == rasUp->getSize());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int y = 0; y < rasOut->getLy(); y++) {
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixDown = rasOut->pixels(y), *endPix = pixDown + rasOut->getLx();
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixUp = rasUp->pixels(y);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		while (pixDown < endPix) {
Toshihiro Shimizu 890ddd
			if (!pixUp->isPurePaint()) {
Toshihiro Shimizu 890ddd
				int toneDown = pixDown->getTone();
Toshihiro Shimizu 890ddd
				int toneUp = pixUp->getTone();
Toshihiro Shimizu 890ddd
				if (val == 1) { //Matchline is on top, with gap
Toshihiro Shimizu 890ddd
					pixDown->setTone(toneUp);
Toshihiro Shimizu 890ddd
					pixDown->setInk(inkIndex);
Toshihiro Shimizu 890ddd
				} else if (val == 0) { //Matchline is on bottom, with gap
Toshihiro Shimizu 890ddd
					if (pixDown->isPurePaint()) {
Toshihiro Shimizu 890ddd
						pixDown->setTone(toneUp);
Toshihiro Shimizu 890ddd
						pixDown->setInk(inkIndex);
Toshihiro Shimizu 890ddd
					} else {
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					//*pixOut = *pixDown;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				if ((val > 0 && toneUp < toneDown) || (val == 0 && toneDown == 255))
Toshihiro Shimizu 890ddd
					pixDown->setTone(toneUp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if ((255 - toneDown) * (1 - val) <= val * (255 - toneUp - 1)) //val==0 -> if (toneDown == 255)....
Toshihiro Shimizu 890ddd
																			  //val==0.5 -> if (toneup
Toshihiro Shimizu 890ddd
																			  //val==1 -> if (toneup<255)...
Toshihiro Shimizu 890ddd
					pixDown->setInk(inkIndex);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			pixUp++;
Toshihiro Shimizu 890ddd
			pixDown++;
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
#ifdef LEVO
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::eraseColors(TRasterCM32P ras, vector<int> &colorIds, bool eraseInks, bool keepColor)</int>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(ras);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	vector<int> curColorIds;</int>
Toshihiro Shimizu 890ddd
	std::sort(colorIds.begin(), colorIds.end());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!keepColor)
Toshihiro Shimizu 890ddd
		curColorIds = colorIds;
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		//prendo tutti i colori ECCETTO quelli nel vettore colorIds
Toshihiro Shimizu 890ddd
		unsigned int count1 = 0, count2 = 0;
Toshihiro Shimizu 890ddd
		int size = eraseInks ? TPixelCM32::getMaxInk() : TPixelCM32::getMaxPaint();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		curColorIds.resize(size + 1 - colorIds.size());
Toshihiro Shimizu 890ddd
		for (int i = 0; i < size; i++)
Toshihiro Shimizu 890ddd
			if (count1 < colorIds.size() && colorIds[count1] == i)
Toshihiro Shimizu 890ddd
				count1++;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				curColorIds[count2++] = i;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int y = 0; y < ras->getLy(); y++) {
Toshihiro Shimizu 890ddd
		TPixelCM32 *pix = ras->pixels(y), *endPix = pix + ras->getLx();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		while (pix < endPix) {
Toshihiro Shimizu 890ddd
			unsigned int i;
Toshihiro Shimizu 890ddd
			int color = eraseInks ? pix->getInk() : pix->getPaint();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (color != 0)
Toshihiro Shimizu 890ddd
				for (i = 0; i < curColorIds.size() && curColorIds[i] <= color; i++)
Toshihiro Shimizu 890ddd
					if (color == curColorIds[i]) {
Toshihiro Shimizu 890ddd
						*pix = eraseInks ? TPixelCM32(0, pix->getPaint(), TPixelCM32::getMaxTone()) : TPixelCM32(pix->getInk(), 0, pix->getTone());
Toshihiro Shimizu 890ddd
						break;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
			pix++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
void TRop::eraseColors(TRasterCM32P ras, std::vector<int> *colorIds, bool eraseInks)</int>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (colorIds)
Toshihiro Shimizu 890ddd
		std::sort(colorIds->begin(), colorIds->end());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int y = 0; y < ras->getLy(); y++) {
Toshihiro Shimizu 890ddd
		TPixelCM32 *pix = ras->pixels(y), *endPix = pix + ras->getLx();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (; pix < endPix; pix++) {
Toshihiro Shimizu 890ddd
			unsigned int i = 0;
Toshihiro Shimizu 890ddd
			int color = eraseInks ? pix->getInk() : pix->getPaint();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (color == 0)
Toshihiro Shimizu 890ddd
				continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (colorIds) {
Toshihiro Shimizu 890ddd
				while (i < colorIds->size() && (*colorIds)[i] < color)
Toshihiro Shimizu 890ddd
					i++;
Toshihiro Shimizu 890ddd
				if (i == colorIds->size() || color != (*colorIds)[i])
Toshihiro Shimizu 890ddd
					continue;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (eraseInks) {
Toshihiro Shimizu 890ddd
				pix->setInk(0);
Toshihiro Shimizu 890ddd
				pix->setTone(TPixelCM32::getMaxTone());
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				pix->setPaint(0);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
void TRop::overaCmapped(TRasterCM32P rasOut, const TRasterCM32P& rasUp, const TPaletteP &pltOut, int matchlinePrevalence, std::map<int,int>& usedColors)</int,int>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
doMergeCmapped(rasOut, rasUp, pltOut, false, matchlinePrevalence, usedColors);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::applyMatchLines(TRasterCM32P rasOut, const TRasterCM32P &rasUp, const TPaletteP &pltOut, int inkIndex, int matchlinePrevalence, std::map<int, int=""> &usedInks)</int,>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(matchlinePrevalence >= 0);
Toshihiro Shimizu 890ddd
	if (inkIndex == -1)
Toshihiro Shimizu 890ddd
		doMergeCmapped(rasOut, rasUp, pltOut, true, matchlinePrevalence, usedInks);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		doApplyMatchLines(rasOut, rasUp, inkIndex, matchlinePrevalence);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
void TRop::eraseStyleIds(TToonzImage *image, const std::vector<int> styleIds)</int>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(image);
Toshihiro Shimizu 890ddd
	TRasterCM32P ras = image->getRaster();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < (int)styleIds.size(); i++) {
Toshihiro Shimizu 890ddd
		int styleId = styleIds[i];
Toshihiro Shimizu 890ddd
		ras->lock();
Toshihiro Shimizu 890ddd
		for (int y = 0; y < ras->getLy(); y++) {
Toshihiro Shimizu 890ddd
			TPixelCM32 *pix = ras->pixels(y), *endPix = pix + ras->getLx();
Toshihiro Shimizu 890ddd
			while (pix < endPix) {
Toshihiro Shimizu 890ddd
				bool isPaint = (pix->getPaint() == styleId);
Toshihiro Shimizu 890ddd
				bool isInk = (pix->getInk() == styleId);
Toshihiro Shimizu 890ddd
				if (!isPaint && !isInk) {
Toshihiro Shimizu 890ddd
					pix++;
Toshihiro Shimizu 890ddd
					continue;
Toshihiro Shimizu 890ddd
				} else if (isPaint && !isInk)
Toshihiro Shimizu 890ddd
					*pix = TPixelCM32(pix->getInk(), 0, pix->getTone());
Toshihiro Shimizu 890ddd
				else if (!isPaint && isInk)
Toshihiro Shimizu 890ddd
					*pix = TPixelCM32(0, pix->getPaint(), TPixelCM32::getMaxTone());
Toshihiro Shimizu 890ddd
				else if (isPaint && isInk)
Toshihiro Shimizu 890ddd
					*pix = TPixelCM32(0, 0, pix->getTone());
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					assert(0);
Toshihiro Shimizu 890ddd
				pix++;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		ras->unlock();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline bool isTransparent(TPixelCM32 *pix)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return (((*((ULONG *)pix)) & 0x000fffff) == 0xff);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::overlayCmapped(TRasterCM32P rasOut, const TRasterCM32P &rasUp, const TPaletteP &pltOut, const TPaletteP &upPlt, std::map<int, int=""> &usedColors)</int,>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(rasOut && rasUp);
Toshihiro Shimizu 890ddd
	assert(rasOut->getSize() == rasUp->getSize());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPaletteP downPlt = pltOut->clone();
Toshihiro Shimizu 890ddd
	std::map<int, int="">::iterator it;</int,>
Toshihiro Shimizu 890ddd
	for (it = usedColors.begin(); it != usedColors.end(); it++)
Toshihiro Shimizu 890ddd
		downPlt->getPage(0)->addStyle(TPixel32::Red);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int y = 0; y < rasOut->getLy(); y++) {
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixDown = rasOut->pixels(y);
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixUp = rasUp->pixels(y);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (int x = 0; x < rasOut->getLx(); x++, pixUp++, pixDown++) {
Toshihiro Shimizu 890ddd
			if (isTransparent(pixUp)) //WARNING! cannot check transparent pixels with *pixup==TPixelCM32() since also 0x183000ff i.e., is a valid transparent value
Toshihiro Shimizu 890ddd
				continue;
Toshihiro Shimizu 890ddd
			int tone = pixUp->getTone();
Toshihiro Shimizu 890ddd
			if (isTransparent(pixDown) || tone == 255 || tone == 0 || pixUp->getPaint() != 0) //up e' punto interno, o esterno non antialiasato
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				addColors(*pixUp, downPlt, upPlt, usedColors);
Toshihiro Shimizu 890ddd
				pixDown->setInk(usedColors[pixUp->getInk()]);
Toshihiro Shimizu 890ddd
				pixDown->setPaint(usedColors[pixUp->getPaint()]);
Toshihiro Shimizu 890ddd
				pixDown->setTone(tone);
Toshihiro Shimizu 890ddd
			} else if (tone <= pixDown->getTone() || tone < 128) //up e' bordo esterno antialiasato piu' opaco di down
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				int ink = pixUp->getInk();
Toshihiro Shimizu 890ddd
				if (usedColors.find(ink) == usedColors.end())
Toshihiro Shimizu 890ddd
					addColor(downPlt, ink, upPlt, usedColors);
Toshihiro Shimizu 890ddd
				pixDown->setInk(usedColors[ink]);
Toshihiro Shimizu 890ddd
				pixDown->setTone(tone < 128 ? 0 : tone);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
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
Toshihiro Shimizu 890ddd
#define MAGICFAC (257U * 256U + 1U)
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
#define PIX_CM32_PENMAP_IDX(PIX) ((PIX)>>12 & 0xfff00 | (PIX) & 0xff)
Toshihiro Shimizu 890ddd
#define PIX_CM32_COLMAP_IDX(PIX) ((PIX) & 0xfffff)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define PIX_CM32_PENMAP_COLMAP_TO_RGBM(PIX,PENMAP,COLMAP,RES) \
Toshihiro Shimizu 890ddd
  { \
Toshihiro Shimizu 890ddd
  ULONG _r = (PIX); \
Toshihiro Shimizu 890ddd
  ULONG _s = ((ULONG *)(PENMAP))[MY_PIX_CM32_PENMAP_IDX(_r)] + \
Toshihiro Shimizu 890ddd
             ((ULONG *)(COLMAP))[MY_PIX_CM32_COLMAP_IDX(_r)]; \
Toshihiro Shimizu 890ddd
  (RES) = *(LPIXEL *)(&_s); \
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
void fillCmapRamp (RAS_CMAP& cmap, const TPixel32& color, int index)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
index = index << cmap.info.tone_bits;
Toshihiro Shimizu 890ddd
int xedni = index + cmap.info.n_tones-1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPixel32 _color=color;
Toshihiro Shimizu 890ddd
	UINT fac = MAGICFAC * _color.m;
Toshihiro Shimizu 890ddd
	_color.r=(UINT)(_color.r * fac + (1U<<23))>>24;
Toshihiro Shimizu 890ddd
	_color.b=(UINT)(_color.b * fac + (1U<<23))>>24;
Toshihiro Shimizu 890ddd
	_color.g=(UINT)(_color.g * fac + (1U<<23))>>24;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
for (int tone = 0; tone < cmap.info.n_tones; tone++)
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
  LPIXEL val;
Toshihiro Shimizu 890ddd
  UINT magic_tone = tone * MAGICFAC;
Toshihiro Shimizu 890ddd
  val.r = (UCHAR)((_color.r * magic_tone + (1<<23)) >> 24);
Toshihiro Shimizu 890ddd
  val.g = (UCHAR)((_color.g * magic_tone + (1<<23)) >> 24);
Toshihiro Shimizu 890ddd
  val.b = (UCHAR)((_color.b * magic_tone + (1<<23)) >> 24);
Toshihiro Shimizu 890ddd
  val.m = (UCHAR)((_color.m * magic_tone + (1<<23)) >> 24);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  cmap.colbuffer[index++] = val;
Toshihiro Shimizu 890ddd
  cmap.penbuffer[xedni--] = val;
Toshihiro Shimizu 890ddd
  }
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
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// \b NOTE: Starting from Toonz 6.1, some important improvements are introduced:
Toshihiro Shimizu 890ddd
// a) The passed raster is now referenced by the returned _RASTER*, just the same way
Toshihiro Shimizu 890ddd
//    smartpointer to rasters do.
Toshihiro Shimizu 890ddd
// b) The cache is made aware of the passed raster, mainly because these old 4.6 raster
Toshihiro Shimizu 890ddd
//    structures are essentially used for memory-consuming operations with tlv fxs and may
Toshihiro Shimizu 890ddd
//    need to be shipped to hard disk on critical situations (a matter handled by the cache).
Toshihiro Shimizu 890ddd
// c) The lockRaster and unlockRaster functions are provided. They are meant to specify whether
Toshihiro Shimizu 890ddd
//    the raster is actively referenced by the raster pointer, or is rather in a lazy state -
Toshihiro Shimizu 890ddd
//    so that the cache may move it to hard disk if necessary.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
static const TCM_INFO Tcm_my_default_info  = {  8,  8, 12,  20, 12,
Toshihiro Shimizu 890ddd
                                               0x0000, 0x00ff,
Toshihiro Shimizu 890ddd
                                               256, 4096, 256
Toshihiro Shimizu 890ddd
                                             };*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline LPIXEL premultiplyLPIXEL(const TPixel32 &pix)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//int MAGICFAC = (257U * 256U + 1U);
Toshihiro Shimizu 890ddd
	UINT fac = MAGICFAC * pix.m;
Toshihiro Shimizu 890ddd
	LPIXEL out;
Toshihiro Shimizu 890ddd
	out.r = ((UINT)(pix.r * fac + (1U << 23)) >> 24);
Toshihiro Shimizu 890ddd
	out.g = ((UINT)(pix.g * fac + (1U << 23)) >> 24);
Toshihiro Shimizu 890ddd
	out.b = ((UINT)(pix.b * fac + (1U << 23)) >> 24);
Toshihiro Shimizu 890ddd
	out.m = pix.m;
Toshihiro Shimizu 890ddd
	return out;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
_RASTER *TRop::convertRaster50to46(const TRasterP &inRas, const TPaletteP &inPalette)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int lx = inRas->getLx();
Toshihiro Shimizu 890ddd
	int ly = inRas->getLy();
Toshihiro Shimizu 890ddd
	int wrap = inRas->getWrap();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(lx > 0 && ly > 0);
Toshihiro Shimizu 890ddd
	assert(inRas->getRawData());
Toshihiro Shimizu 890ddd
	TRasterGR8P rgr8 = (TRasterGR8P)inRas;
Toshihiro Shimizu 890ddd
	TRasterGR16P rgr16 = (TRasterGR16P)inRas;
Toshihiro Shimizu 890ddd
	TRaster32P r32 = (TRaster32P)inRas;
Toshihiro Shimizu 890ddd
	TRaster64P r64 = (TRaster64P)inRas;
Toshihiro Shimizu 890ddd
	TRasterCM32P rcm = (TRasterCM32P)inRas;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	RASTER *rout = new RASTER;
Toshihiro Shimizu 890ddd
	memset(rout, 0, sizeof(RASTER));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::string id(TImageCache::instance()->getUniqueId());
Toshihiro Shimizu 890ddd
	rout->cacheIdLength = id.size();
Toshihiro Shimizu 890ddd
	rout->cacheId = new char[rout->cacheIdLength];
Toshihiro Shimizu 890ddd
	memcpy(rout->cacheId, id.data(), sizeof(char) * rout->cacheIdLength);
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TImageP img;
Toshihiro Shimizu 890ddd
		if (rcm)
Toshihiro Shimizu 890ddd
			img = TToonzImageP(rcm, rcm->getBounds()); //saveBox is not considered in RASTER struct anyway
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			img = TRasterImageP(inRas);
Toshihiro Shimizu 890ddd
		TImageCache::instance()->add(id, img);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	inRas->addRef();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	rout->buffer = inRas->getRawData();
Toshihiro Shimizu 890ddd
	TRasterP parent = inRas->getParent();
Toshihiro Shimizu 890ddd
	rout->native_buffer = (parent) ? parent->getRawData() : inRas->getRawData();
Toshihiro Shimizu 890ddd
	rout->lx = lx;
Toshihiro Shimizu 890ddd
	rout->ly = ly;
Toshihiro Shimizu 890ddd
	rout->wrap = wrap;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (rgr8)
Toshihiro Shimizu 890ddd
		rout->type = RAS_GR8;
Toshihiro Shimizu 890ddd
	else if (rgr16)
Toshihiro Shimizu 890ddd
		rout->type = RAS_GR16;
Toshihiro Shimizu 890ddd
	else if (r32)
Toshihiro Shimizu 890ddd
		rout->type = RAS_RGBM;
Toshihiro Shimizu 890ddd
	else if (r64)
Toshihiro Shimizu 890ddd
		rout->type = RAS_RGBM64;
Toshihiro Shimizu 890ddd
	else if (rcm)
Toshihiro Shimizu 890ddd
		rout->type = RAS_CM32;
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		assert(!"raster type not convertible!");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (rout->type != RAS_CM32)
Toshihiro Shimizu 890ddd
		return rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!inPalette) {
Toshihiro Shimizu 890ddd
		assert(!"missing palette!");
Toshihiro Shimizu 890ddd
		return NULL;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	rout->cmap.info = Tcm_32_default_info;
Toshihiro Shimizu 890ddd
	rout->cmap.buffer = new LPIXEL[TCM_CMAP_PENBUFFER_SIZE(rout->cmap.info)];
Toshihiro Shimizu 890ddd
	//rout->cmap.penbuffer = new LPIXEL[TCM_CMAP_PENBUFFER_SIZE (rout->cmap.info)];
Toshihiro Shimizu 890ddd
	//rout->cmap.colbuffer = new LPIXEL[TCM_CMAP_COLBUFFER_SIZE (rout->cmap.info)];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < inPalette->getStyleCount(); i++)
Toshihiro Shimizu 890ddd
		rout->cmap.buffer[i] = ::premultiplyLPIXEL(inPalette->getStyle(i)->getAverageColor());
Toshihiro Shimizu 890ddd
	//fillCmapRamp (rout->cmap, inPalette->getStyle(i)->getMainColor(), i);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return rout;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::releaseRaster46(_RASTER *&r, bool doReleaseBuffer)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//Buffer release no more supported. These are now intended as smart
Toshihiro Shimizu 890ddd
	//pointers to rasters - they release themselves on their own.
Toshihiro Shimizu 890ddd
	assert(!doReleaseBuffer);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (r->type == RAS_CM32) {
Toshihiro Shimizu 890ddd
		delete[] r->cmap.buffer;
Toshihiro Shimizu 890ddd
		//delete [] r->cmap.penbuffer;
Toshihiro Shimizu 890ddd
		//delete [] r->cmap.colbuffer;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (doReleaseBuffer && r->native_buffer == r->buffer)
Toshihiro Shimizu 890ddd
		delete r->buffer; //Should not happen
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Unlock if locked, and remove the cache reference
Toshihiro Shimizu 890ddd
	if (r->buffer)
Toshihiro Shimizu 890ddd
		unlockRaster(r);
Toshihiro Shimizu 890ddd
	TImageCache::instance()->remove(std::string(r->cacheId, r->cacheIdLength));
Toshihiro Shimizu 890ddd
	delete[] r->cacheId;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	delete r;
Toshihiro Shimizu 890ddd
	r = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::lockRaster(RASTER *raster)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TImageP img(TImageCache::instance()->get(std::string(raster->cacheId, raster->cacheIdLength), true));
Toshihiro Shimizu 890ddd
	TRasterP cacheRas;
Toshihiro Shimizu 890ddd
	if (raster->type == RAS_CM32)
Toshihiro Shimizu 890ddd
		cacheRas = TToonzImageP(img)->getRaster();
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		cacheRas = TRasterImageP(img)->getRaster();
Toshihiro Shimizu 890ddd
	cacheRas->addRef();
Toshihiro Shimizu 890ddd
	raster->buffer = cacheRas->getRawData();
Toshihiro Shimizu 890ddd
	TRasterP parent = cacheRas->getParent();
Toshihiro Shimizu 890ddd
	raster->native_buffer = (parent) ? parent->getRawData() : cacheRas->getRawData();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::unlockRaster(RASTER *raster)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TImageP img(TImageCache::instance()->get(std::string(raster->cacheId, raster->cacheIdLength), true));
Toshihiro Shimizu 890ddd
	TRasterP cacheRas;
Toshihiro Shimizu 890ddd
	if (raster->type == RAS_CM32)
Toshihiro Shimizu 890ddd
		cacheRas = TToonzImageP(img)->getRaster();
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		cacheRas = TRasterImageP(img)->getRaster();
Toshihiro Shimizu 890ddd
	cacheRas->release();
Toshihiro Shimizu 890ddd
	raster->buffer = 0;
Toshihiro Shimizu 890ddd
	raster->native_buffer = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
_RASTER *TRop::readRaster46(const char *filename)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//No more called in Toonz...
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TImageP img;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TImageReader::load(TFilePath(filename), img);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if ((TToonzImageP)img)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterImageP rimg = (TRasterImageP)img;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!rimg)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	TRasterP ras = rimg->getRaster();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRop::convertRaster50to46(ras, 0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline TPixel32 getPix32(const TPixelCM32 &pixcm,
Toshihiro Shimizu 890ddd
						 const std::vector<tpixel32> &colors)</tpixel32>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int t = pixcm.getTone();
Toshihiro Shimizu 890ddd
	int p = pixcm.getPaint();
Toshihiro Shimizu 890ddd
	int i = pixcm.getInk();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (t == TPixelCM32::getMaxTone())
Toshihiro Shimizu 890ddd
		return colors[p];
Toshihiro Shimizu 890ddd
	else if (t == 0)
Toshihiro Shimizu 890ddd
		return colors[i];
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return blend(colors[i], colors[p], t, TPixelCM32::getMaxTone());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//from 4.6
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef LEVO
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::zoomOutCm32Rgbm(const TRasterCM32P &rin, TRaster32P &rout, const TPalette &plt,
Toshihiro Shimizu 890ddd
						   int x1, int y1, int x2, int y2,
Toshihiro Shimizu 890ddd
						   int newx, int newy, int absZoomLevel)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPixelCM32 *rowin, *pixin, *in, win;
Toshihiro Shimizu 890ddd
	TPixel32 *rowout, *pixout, valin, valout;
Toshihiro Shimizu 890ddd
	int tmp_r, tmp_g, tmp_b, tmp_m;
Toshihiro Shimizu 890ddd
	int wrapin, wrapout;
Toshihiro Shimizu 890ddd
	int x, y, lx, ly, xlast, ylast, xrest, yrest, i, j;
Toshihiro Shimizu 890ddd
	int factor, fac_fac_2_bits;
Toshihiro Shimizu 890ddd
	int fac_fac, yrest_fac, fac_xrest, yrest_xrest;
Toshihiro Shimizu 890ddd
	int fac_fac_2, yrest_fac_2, fac_xrest_2, yrest_xrest_2;
Toshihiro Shimizu 890ddd
	int fac_fac_4;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int count = plt.getStyleCount();
Shinya Kitaoka 12c444
	int count2 = std::max(count, TPixelCM32::getMaxInk(), TPixelCM32::getMaxPaint());
Toshihiro Shimizu 890ddd
	std::vector<tpixel32> colors(count2);</tpixel32>
Toshihiro Shimizu 890ddd
	for (i = 0; i < plt.getStyleCount(); i++)
Toshihiro Shimizu 890ddd
		colors[i] = ::premultiply(plt.getStyle(i)->getAverageColor());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	lx = x2 - x1 + 1;
Toshihiro Shimizu 890ddd
	ly = y2 - y1 + 1;
Toshihiro Shimizu 890ddd
	factor = 1 << absZoomLevel;
Toshihiro Shimizu 890ddd
	xrest = lx & (factor - 1);
Toshihiro Shimizu 890ddd
	yrest = ly & (factor - 1);
Toshihiro Shimizu 890ddd
	xlast = x2 - xrest + 1;
Toshihiro Shimizu 890ddd
	ylast = y2 - yrest + 1;
Toshihiro Shimizu 890ddd
	fac_fac = factor * factor;
Toshihiro Shimizu 890ddd
	fac_fac_2 = fac_fac >> 1;
Toshihiro Shimizu 890ddd
	fac_fac_4 = fac_fac >> 2;
Toshihiro Shimizu 890ddd
	fac_fac_2_bits = 2 * absZoomLevel - 1;
Toshihiro Shimizu 890ddd
	yrest_fac = yrest * factor;
Toshihiro Shimizu 890ddd
	yrest_fac_2 = yrest_fac >> 1;
Toshihiro Shimizu 890ddd
	fac_xrest = factor * xrest;
Toshihiro Shimizu 890ddd
	fac_xrest_2 = fac_xrest >> 1;
Toshihiro Shimizu 890ddd
	yrest_xrest = yrest * xrest;
Toshihiro Shimizu 890ddd
	yrest_xrest_2 = yrest_xrest >> 1;
Toshihiro Shimizu 890ddd
	wrapin = rin->getWrap();
Toshihiro Shimizu 890ddd
	wrapout = rout->getWrap();
Toshihiro Shimizu 890ddd
	valout.m = 0xff;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	rowin = (TPixelCM32 *)rin->getRawData() + wrapin * y1 + x1;
Toshihiro Shimizu 890ddd
	rowout = (TPixel32 *)rout->getRawData() + wrapout * newy + newx;
Toshihiro Shimizu 890ddd
	for (y = y1; y < ylast; y += factor) {
Toshihiro Shimizu 890ddd
		pixin = rowin;
Toshihiro Shimizu 890ddd
		pixout = rowout;
Toshihiro Shimizu 890ddd
		for (x = x1; x < xlast; x += factor) {
Toshihiro Shimizu 890ddd
			tmp_r = tmp_g = tmp_b = tmp_m = 0;
Toshihiro Shimizu 890ddd
			in = pixin;
Toshihiro Shimizu 890ddd
			for (j = 0; j < factor; j += 2) {
Toshihiro Shimizu 890ddd
				for (i = 0; i < factor; i += 2) {
Toshihiro Shimizu 890ddd
					win = *in;
Toshihiro Shimizu 890ddd
					valin = getPix32(win, colors);
Toshihiro Shimizu 890ddd
					tmp_r += valin.r;
Toshihiro Shimizu 890ddd
					tmp_g += valin.g;
Toshihiro Shimizu 890ddd
					tmp_b += valin.b;
Toshihiro Shimizu 890ddd
					tmp_m += valin.m;
Toshihiro Shimizu 890ddd
					in += wrapin;
Toshihiro Shimizu 890ddd
					in++;
Toshihiro Shimizu 890ddd
					win = *in;
Toshihiro Shimizu 890ddd
					valin = getPix32(win, colors);
Toshihiro Shimizu 890ddd
					tmp_r += valin.r;
Toshihiro Shimizu 890ddd
					tmp_g += valin.g;
Toshihiro Shimizu 890ddd
					tmp_b += valin.b;
Toshihiro Shimizu 890ddd
					tmp_m += valin.m;
Toshihiro Shimizu 890ddd
					in -= wrapin;
Toshihiro Shimizu 890ddd
					in++;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				in += 2 * wrapin - factor;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			valout.r = (tmp_r + fac_fac_4) >> fac_fac_2_bits;
Toshihiro Shimizu 890ddd
			valout.g = (tmp_g + fac_fac_4) >> fac_fac_2_bits;
Toshihiro Shimizu 890ddd
			valout.b = (tmp_b + fac_fac_4) >> fac_fac_2_bits;
Toshihiro Shimizu 890ddd
			valout.m = (tmp_m + fac_fac_4) >> fac_fac_2_bits;
Toshihiro Shimizu 890ddd
			*pixout++ = valout;
Toshihiro Shimizu 890ddd
			pixin += factor;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (xrest) {
Toshihiro Shimizu 890ddd
			tmp_r = tmp_g = tmp_b = tmp_m = 0;
Toshihiro Shimizu 890ddd
			for (j = 0; j < factor; j++)
Toshihiro Shimizu 890ddd
				for (i = 0; i < xrest; i++) {
Toshihiro Shimizu 890ddd
					win = pixin[i + j * wrapin];
Toshihiro Shimizu 890ddd
					valin = getPix32(win, colors);
Toshihiro Shimizu 890ddd
					tmp_r += valin.r;
Toshihiro Shimizu 890ddd
					tmp_g += valin.g;
Toshihiro Shimizu 890ddd
					tmp_b += valin.b;
Toshihiro Shimizu 890ddd
					tmp_m += valin.m;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			valout.r = (tmp_r + fac_xrest_2) / fac_xrest;
Toshihiro Shimizu 890ddd
			valout.g = (tmp_g + fac_xrest_2) / fac_xrest;
Toshihiro Shimizu 890ddd
			valout.b = (tmp_b + fac_xrest_2) / fac_xrest;
Toshihiro Shimizu 890ddd
			valout.m = (tmp_m + fac_xrest_2) / fac_xrest;
Toshihiro Shimizu 890ddd
			*pixout = valout;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		rowin += wrapin * factor;
Toshihiro Shimizu 890ddd
		rowout += wrapout;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (yrest) {
Toshihiro Shimizu 890ddd
		pixin = rowin;
Toshihiro Shimizu 890ddd
		pixout = rowout;
Toshihiro Shimizu 890ddd
		for (x = x1; x < xlast; x += factor) {
Toshihiro Shimizu 890ddd
			tmp_r = tmp_g = tmp_b = tmp_m = 0;
Toshihiro Shimizu 890ddd
			for (j = 0; j < yrest; j++)
Toshihiro Shimizu 890ddd
				for (i = 0; i < factor; i++) {
Toshihiro Shimizu 890ddd
					win = pixin[i + j * wrapin];
Toshihiro Shimizu 890ddd
					valin = getPix32(win, colors);
Toshihiro Shimizu 890ddd
					tmp_r += valin.r;
Toshihiro Shimizu 890ddd
					tmp_g += valin.g;
Toshihiro Shimizu 890ddd
					tmp_b += valin.b;
Toshihiro Shimizu 890ddd
					tmp_m += valin.m;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			valout.r = (tmp_r + yrest_fac_2) / yrest_fac;
Toshihiro Shimizu 890ddd
			valout.g = (tmp_g + yrest_fac_2) / yrest_fac;
Toshihiro Shimizu 890ddd
			valout.b = (tmp_b + yrest_fac_2) / yrest_fac;
Toshihiro Shimizu 890ddd
			valout.m = (tmp_m + yrest_fac_2) / yrest_fac;
Toshihiro Shimizu 890ddd
			*pixout++ = valout;
Toshihiro Shimizu 890ddd
			pixin += factor;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (xrest) {
Toshihiro Shimizu 890ddd
			tmp_r = tmp_g = tmp_b = tmp_m = 0;
Toshihiro Shimizu 890ddd
			for (j = 0; j < yrest; j++)
Toshihiro Shimizu 890ddd
				for (i = 0; i < xrest; i++) {
Toshihiro Shimizu 890ddd
					win = pixin[i + j * wrapin];
Toshihiro Shimizu 890ddd
					valin = getPix32(win, colors);
Toshihiro Shimizu 890ddd
					tmp_r += valin.r;
Toshihiro Shimizu 890ddd
					tmp_g += valin.g;
Toshihiro Shimizu 890ddd
					tmp_b += valin.b;
Toshihiro Shimizu 890ddd
					tmp_m += valin.m;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			valout.r = (tmp_r + yrest_xrest_2) / yrest_xrest;
Toshihiro Shimizu 890ddd
			valout.g = (tmp_g + yrest_xrest_2) / yrest_xrest;
Toshihiro Shimizu 890ddd
			valout.b = (tmp_b + yrest_xrest_2) / yrest_xrest;
Toshihiro Shimizu 890ddd
			valout.m = (tmp_m + yrest_xrest_2) / yrest_xrest;
Toshihiro Shimizu 890ddd
			*pixout = valout;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef LEVO
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::makeIcon(TRaster32P &_rout, const TRasterCM32P &rin, const TPaletteP &palette, bool onBlackBg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i, j;
Toshihiro Shimizu 890ddd
	int lx = rin->getLx();
Toshihiro Shimizu 890ddd
	int ly = rin->getLy();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int count = palette->getStyleCount();
Shinya Kitaoka 12c444
	int count2 = std::max(count, TPixelCM32::getMaxInk(), TPixelCM32::getMaxPaint());
Toshihiro Shimizu 890ddd
	std::vector<tpixel32> colors(count2);</tpixel32>
Toshihiro Shimizu 890ddd
	for (i = 0; i < palette->getStyleCount(); i++)
Toshihiro Shimizu 890ddd
		colors[i] = /*::premultiply(*/ palette->getStyle(i)->getAverageColor(); //);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TDimension dim(_rout->getSize());
Toshihiro Shimizu 890ddd
	TRaster32P rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double arIn = (double)lx / ly;
Toshihiro Shimizu 890ddd
	double arOut = (double)dim.lx / dim.ly;
Toshihiro Shimizu 890ddd
	if (!areAlmostEqual(arIn, arOut, 1e-2)) //do not want a stretched icon! I extract a subraster with same aspect ration of rin
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		int newlx, newly;
Toshihiro Shimizu 890ddd
		if (arOut < arIn) {
Toshihiro Shimizu 890ddd
			newlx = dim.lx;
Toshihiro Shimizu 890ddd
			newly = dim.lx / arIn;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			newly = dim.ly;
Toshihiro Shimizu 890ddd
			newlx = dim.ly * arIn;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		_rout->clear();
Toshihiro Shimizu 890ddd
		rout = _rout->extract((dim.lx - newlx) / 2, (dim.ly - newly) / 2,
Toshihiro Shimizu 890ddd
							  (dim.lx - newlx) / 2 + newlx - 1, (dim.ly - newly) / 2 + newly - 1);
Toshihiro Shimizu 890ddd
		dim = rout->getSize();
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		rout = _rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPixel32 *pixOut0 = (TPixel32 *)rout->getRawData();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int countY = 0;
Toshihiro Shimizu 890ddd
	bool newRow = true;
Toshihiro Shimizu 890ddd
	int currTone = TPixelCM32::getMaxTone();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < ly; i++) {
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixIn = (TPixelCM32 *)rin->pixels(i);
Toshihiro Shimizu 890ddd
		TPixel32 *pixOut = pixOut0;
Toshihiro Shimizu 890ddd
		int countX = 0;
Toshihiro Shimizu 890ddd
		for (j = 0; j < lx; j++) {
Toshihiro Shimizu 890ddd
			if (newRow || currTone > pixIn->getTone()) {
Toshihiro Shimizu 890ddd
				currTone = pixIn->getTone();
Toshihiro Shimizu 890ddd
				if (onBlackBg)
Toshihiro Shimizu 890ddd
					*pixOut = overPixOnBlack(getPix32(*pixIn, colors));
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					*pixOut = overPixOnWhite(getPix32(*pixIn, colors));
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			pixIn++;
Toshihiro Shimizu 890ddd
			countX += dim.lx;
Toshihiro Shimizu 890ddd
			if (countX >= lx)
Toshihiro Shimizu 890ddd
				countX -= lx, pixOut++, currTone = TPixelCM32::getMaxTone();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		countY += dim.ly;
Toshihiro Shimizu 890ddd
		if (countY >= ly) {
Toshihiro Shimizu 890ddd
			countY -= ly;
Toshihiro Shimizu 890ddd
			pixOut0 += rout->getWrap();
Toshihiro Shimizu 890ddd
			currTone = TPixelCM32::getMaxTone();
Toshihiro Shimizu 890ddd
			newRow = true;
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			newRow = false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::makeIcon(TRasterCM32P &_rout, const TRasterCM32P &rin)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i, j;
Toshihiro Shimizu 890ddd
	int lx = rin->getLx();
Toshihiro Shimizu 890ddd
	int ly = rin->getLy();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TDimension dim(_rout->getSize());
Toshihiro Shimizu 890ddd
	TRasterCM32P rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	rout = _rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	rout->lock();
Toshihiro Shimizu 890ddd
	rin->lock();
Toshihiro Shimizu 890ddd
	TPixelCM32 *pixOut0 = (TPixelCM32 *)rout->getRawData();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int countY = 0;
Toshihiro Shimizu 890ddd
	bool newRow = true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < ly; i++) {
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixIn = (TPixelCM32 *)rin->pixels(i);
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixOut = pixOut0;
Toshihiro Shimizu 890ddd
		int countX = 0;
Toshihiro Shimizu 890ddd
		bool newColumn = true;
Toshihiro Shimizu 890ddd
		for (j = 0; j < lx; j++) {
Toshihiro Shimizu 890ddd
			if ((newRow && newColumn) ||
Toshihiro Shimizu 890ddd
				pixOut->getTone() > pixIn->getTone())
Toshihiro Shimizu 890ddd
				*pixOut = *pixIn;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			pixIn++;
Toshihiro Shimizu 890ddd
			countX += dim.lx;
Toshihiro Shimizu 890ddd
			if (countX >= lx) {
Toshihiro Shimizu 890ddd
				countX -= lx, pixOut++;
Toshihiro Shimizu 890ddd
				newColumn = true;
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				newColumn = false;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		countY += dim.ly;
Toshihiro Shimizu 890ddd
		if (countY >= ly) {
Toshihiro Shimizu 890ddd
			countY -= ly;
Toshihiro Shimizu 890ddd
			pixOut0 += rout->getWrap();
Toshihiro Shimizu 890ddd
			newRow = true;
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			newRow = false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	rout->unlock();
Toshihiro Shimizu 890ddd
	rin->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/