Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
#include "tpixel.h"
Toshihiro Shimizu 890ddd
#include "tpixelutils.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#include <emmintrin.h> // per SSE2</emmintrin.h>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
inline double luminance(TPixel32 *pix)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return 0.2126 * pix->r + 0.7152 * pix->g + 0.0722 * pix->b;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
inline double luminance(TPixel64 *pix)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return 0.2126 * pix->r + 0.7152 * pix->g + 0.0722 * pix->b;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define FOR_EACH_PIXEL_BEGIN_LOOP(UpType, up, DownType, down, OutType, out) \
Toshihiro Shimizu 890ddd
	{                                                                       \
Toshihiro Shimizu 890ddd
		int upWrap = up->getWrap();                                         \
Toshihiro Shimizu 890ddd
		int downWrap = down->getWrap();                                     \
Toshihiro Shimizu 890ddd
		int outWrap = out->getWrap();                                       \
Toshihiro Shimizu 890ddd
                                                                            \
Toshihiro Shimizu 890ddd
		up->lock();                                                         \
Toshihiro Shimizu 890ddd
		down->lock();                                                       \
Toshihiro Shimizu 890ddd
		out->lock();                                                        \
Toshihiro Shimizu 890ddd
		UpType *upPix = 0, *upRow = up->pixels();                           \
Toshihiro Shimizu 890ddd
		DownType *downPix, *downRow = down->pixels();                       \
Toshihiro Shimizu 890ddd
		OutType *outPix, *outRow = out->pixels();                           \
Toshihiro Shimizu 890ddd
		UpType *endPix;                                                     \
Toshihiro Shimizu 890ddd
		int upLx = up->getLx();                                             \
Toshihiro Shimizu 890ddd
		UpType *lastPix = upRow + upWrap * (up->getLy() - 1) + upLx;        \
Toshihiro Shimizu 890ddd
		while (upPix < lastPix) {                                           \
Toshihiro Shimizu 890ddd
			upPix = upRow;                                                  \
Toshihiro Shimizu 890ddd
			downPix = downRow;                                              \
Toshihiro Shimizu 890ddd
			outPix = outRow;                                                \
Toshihiro Shimizu 890ddd
			endPix = upPix + upLx;                                          \
Toshihiro Shimizu 890ddd
			while (upPix < endPix) {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define FOR_EACH_PIXEL_END_LOOP(up, down, out) \
Toshihiro Shimizu 890ddd
	++upPix;                                   \
Toshihiro Shimizu 890ddd
	++downPix;                                 \
Toshihiro Shimizu 890ddd
	++outPix;                                  \
Toshihiro Shimizu 890ddd
	}                                          \
Toshihiro Shimizu 890ddd
	upRow += upWrap;                           \
Toshihiro Shimizu 890ddd
	downRow += downWrap;                       \
Toshihiro Shimizu 890ddd
	outRow += outWrap;                         \
Toshihiro Shimizu 890ddd
	}                                          \
Toshihiro Shimizu 890ddd
	up->unlock();                              \
Toshihiro Shimizu 890ddd
	down->unlock();                            \
Toshihiro Shimizu 890ddd
	out->unlock();                             \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define FOR_EACH_PIXEL_32_BEGIN_LOOP \
Toshihiro Shimizu 890ddd
	assert(up32 &&down32 &&out32);   \
Toshihiro Shimizu 890ddd
	FOR_EACH_PIXEL_BEGIN_LOOP(TPixelRGBM32, up32, TPixelRGBM32, down32, TPixelRGBM32, out32)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define FOR_EACH_PIXEL_32_END_LOOP \
Toshihiro Shimizu 890ddd
	assert(up32 &&down32 &&out32); \
Toshihiro Shimizu 890ddd
	FOR_EACH_PIXEL_END_LOOP(up32, down32, out32)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define FOR_EACH_PIXEL_64_BEGIN_LOOP \
Toshihiro Shimizu 890ddd
	assert(up64 &&down64 &&out64);   \
Toshihiro Shimizu 890ddd
	FOR_EACH_PIXEL_BEGIN_LOOP(TPixelRGBM64, up64, TPixelRGBM64, down64, TPixelRGBM64, out64)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define FOR_EACH_PIXEL_64_END_LOOP \
Toshihiro Shimizu 890ddd
	assert(up64 &&down64 &&out64); \
Toshihiro Shimizu 890ddd
	FOR_EACH_PIXEL_END_LOOP(up64, down64, out64)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define FOR_EACH_PIXEL_8_BEGIN_LOOP \
Toshihiro Shimizu 890ddd
	assert(up8 &&down8 &&out8);     \
Toshihiro Shimizu 890ddd
	FOR_EACH_PIXEL_BEGIN_LOOP(TPixelGR8, up8, TPixelGR8, down8, TPixelGR8, out8)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define FOR_EACH_PIXEL_8_END_LOOP \
Toshihiro Shimizu 890ddd
	assert(up8 &&down8 &&out8);   \
Toshihiro Shimizu 890ddd
	FOR_EACH_PIXEL_END_LOOP(up32, down32, out32)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::add(const TRasterP &rup, const TRasterP &rdown, const TRasterP &rout, double v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P up32 = rup;
Toshihiro Shimizu 890ddd
	TRaster32P down32 = rdown;
Toshihiro Shimizu 890ddd
	TRaster32P out32 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (up32 && down32 && out32) {
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TINT32 r, g, b, m;
Toshihiro Shimizu 890ddd
		if (upPix->m == 0)
Toshihiro Shimizu 890ddd
			*outPix = *downPix;
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			r = downPix->r + tround(upPix->r * v);
Toshihiro Shimizu 890ddd
			g = downPix->g + tround(upPix->g * v);
Toshihiro Shimizu 890ddd
			b = downPix->b + tround(upPix->b * v);
Toshihiro Shimizu 890ddd
			m = downPix->m + tround(upPix->m * v);
Toshihiro Shimizu 890ddd
			outPix->r = (UCHAR)tcrop<tint32>(r, (TINT32)0, (TINT32)255);</tint32>
Toshihiro Shimizu 890ddd
			outPix->g = (UCHAR)tcrop<tint32>(g, (TINT32)0, (TINT32)255);</tint32>
Toshihiro Shimizu 890ddd
			outPix->b = (UCHAR)tcrop<tint32>(b, (TINT32)0, (TINT32)255);</tint32>
Toshihiro Shimizu 890ddd
			outPix->m = (UCHAR)tcrop<tint32>(m, (TINT32)0, (TINT32)255);</tint32>
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		TRaster64P up64 = rup;
Toshihiro Shimizu 890ddd
		TRaster64P down64 = rdown;
Toshihiro Shimizu 890ddd
		TRaster64P out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (up64 && down64 && out64) {
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TINT32 r, g, b, m;
Toshihiro Shimizu 890ddd
			r = downPix->r + tround(upPix->r * v);
Toshihiro Shimizu 890ddd
			g = downPix->g + tround(upPix->g * v);
Toshihiro Shimizu 890ddd
			b = downPix->b + tround(upPix->b * v);
Toshihiro Shimizu 890ddd
			m = downPix->m + tround(upPix->m * v);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			outPix->r = (USHORT)tcrop<tint32>(r, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
			outPix->g = (USHORT)tcrop<tint32>(g, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
			outPix->b = (USHORT)tcrop<tint32>(b, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
			outPix->m = (USHORT)tcrop<tint32>(m, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			TRasterGR8P up8 = rup;
Toshihiro Shimizu 890ddd
			TRasterGR8P down8 = rdown;
Toshihiro Shimizu 890ddd
			TRasterGR8P out8 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (up8 && down8 && out8) {
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_8_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				USHORT value = troundp(upPix->value * v) + downPix->value;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				outPix->value = (UCHAR)tcrop<ushort>(value, 0, 255);</ushort>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_8_END_LOOP
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				throw TRopException("TRop::add invalid raster combination");
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::add(const TRasterP &rup, const TRasterP &rdown, const TRasterP &rout)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P up32 = rup;
Toshihiro Shimizu 890ddd
	TRaster32P down32 = rdown;
Toshihiro Shimizu 890ddd
	TRaster32P out32 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (up32 && down32 && out32) {
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		USHORT r, g, b, m;
Toshihiro Shimizu 890ddd
		r = downPix->r + upPix->r;
Toshihiro Shimizu 890ddd
		g = downPix->g + upPix->g;
Toshihiro Shimizu 890ddd
		b = downPix->b + upPix->b;
Toshihiro Shimizu 890ddd
		m = downPix->m + upPix->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		outPix->r = (UCHAR)tcrop<ushort>(r, 0, 255);</ushort>
Toshihiro Shimizu 890ddd
		outPix->g = (UCHAR)tcrop<ushort>(g, 0, 255);</ushort>
Toshihiro Shimizu 890ddd
		outPix->b = (UCHAR)tcrop<ushort>(b, 0, 255);</ushort>
Toshihiro Shimizu 890ddd
		outPix->m = (UCHAR)tcrop<ushort>(m, 0, 255);</ushort>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		TRaster64P up64 = rup;
Toshihiro Shimizu 890ddd
		TRaster64P down64 = rdown;
Toshihiro Shimizu 890ddd
		TRaster64P out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (up64 && down64 && out64) {
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TINT32 r, g, b, m;
Toshihiro Shimizu 890ddd
			r = downPix->r + upPix->r;
Toshihiro Shimizu 890ddd
			g = downPix->g + upPix->g;
Toshihiro Shimizu 890ddd
			b = downPix->b + upPix->b;
Toshihiro Shimizu 890ddd
			m = downPix->m + upPix->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			outPix->r = (USHORT)tcrop<tint32>(r, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
			outPix->g = (USHORT)tcrop<tint32>(g, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
			outPix->b = (USHORT)tcrop<tint32>(b, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
			outPix->m = (USHORT)tcrop<tint32>(m, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			TRasterGR8P up8 = rup;
Toshihiro Shimizu 890ddd
			TRasterGR8P down8 = rdown;
Toshihiro Shimizu 890ddd
			TRasterGR8P out8 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (up8 && down8 && out8) {
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_8_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				USHORT value = upPix->value + downPix->value;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				outPix->value = (UCHAR)tcrop<ushort>(value, 0, 255);</ushort>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_8_END_LOOP
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				throw TRopException("TRop::add invalid raster combination");
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::colordodge(const TRasterP &rup, const TRasterP &rdown, const TRasterP &rout)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P up32 = rup;
Toshihiro Shimizu 890ddd
	TRaster32P down32 = rdown;
Toshihiro Shimizu 890ddd
	TRaster32P out32 = rout;
Toshihiro Shimizu 890ddd
	if (up32 && down32 && out32) {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		USHORT r, g, b, m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		r = (USHORT)((downPix->r << 8) / (256.0 - upPix->r));
Toshihiro Shimizu 890ddd
		g = (USHORT)((downPix->g << 8) / (256.0 - upPix->g));
Toshihiro Shimizu 890ddd
		b = (USHORT)((downPix->b << 8) / (256.0 - upPix->b));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m = downPix->m + upPix->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		outPix->r = (UCHAR)tcrop<ushort>(r, 0, 255);</ushort>
Toshihiro Shimizu 890ddd
		outPix->g = (UCHAR)tcrop<ushort>(g, 0, 255);</ushort>
Toshihiro Shimizu 890ddd
		outPix->b = (UCHAR)tcrop<ushort>(b, 0, 255);</ushort>
Toshihiro Shimizu 890ddd
		outPix->m = (UCHAR)tcrop<ushort>(m, 0, 255);</ushort>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		TRaster64P up64 = rup;
Toshihiro Shimizu 890ddd
		TRaster64P down64 = rdown;
Toshihiro Shimizu 890ddd
		TRaster64P out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (up64 && down64 && out64) {
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TINT32 r, g, b, m;
Toshihiro Shimizu 890ddd
			r = (TINT32)(65536.0 * (downPix->r / (65536.0 - upPix->r)));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			g = (TINT32)(65536.0 * (downPix->g / (65536.0 - upPix->g)));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			b = (TINT32)(65536.0 * (downPix->b / (65536.0 - upPix->b)));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			m = downPix->m + upPix->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			outPix->r = (USHORT)tcrop<tint32>(r, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
			outPix->g = (USHORT)tcrop<tint32>(g, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
			outPix->b = (USHORT)tcrop<tint32>(b, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
			outPix->m = (USHORT)tcrop<tint32>(m, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			TRasterGR8P up8 = rup;
Toshihiro Shimizu 890ddd
			TRasterGR8P down8 = rdown;
Toshihiro Shimizu 890ddd
			TRasterGR8P out8 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (up8 && down8 && out8) {
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_8_BEGIN_LOOP
Toshihiro Shimizu 890ddd
				USHORT value;
Toshihiro Shimizu 890ddd
				if (downPix->value)
Toshihiro Shimizu 890ddd
					value = (USHORT)((downPix->value << 8) / (255.0 - upPix->value));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				outPix->value = (UCHAR)tcrop<ushort>(value, 0, 255);</ushort>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_8_END_LOOP
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				throw TRopException("TRop::color dodge invalid raster combination");
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::colorburn(const TRasterP &rup, const TRasterP &rdown, const TRasterP &rout)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P up32 = rup;
Toshihiro Shimizu 890ddd
	TRaster32P down32 = rdown;
Toshihiro Shimizu 890ddd
	TRaster32P out32 = rout;
Toshihiro Shimizu 890ddd
	if (up32 && down32 && out32) {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
		//  downPix->r=0;
Toshihiro Shimizu 890ddd
		//  downPix->g=255;
Toshihiro Shimizu 890ddd
		//  downPix->b=0;
Toshihiro Shimizu 890ddd
		double r, g, b;
Toshihiro Shimizu 890ddd
		if (upPix->m) {
Toshihiro Shimizu 890ddd
			if (downPix->r == 0 || downPix->r == 255)
Toshihiro Shimizu 890ddd
				r = downPix->r;
Toshihiro Shimizu 890ddd
			else if (upPix->r)
Toshihiro Shimizu 890ddd
				r = 255 - (((255 - downPix->r) << 8) / (double)upPix->r);
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				r = 0;
Toshihiro Shimizu 890ddd
			if (downPix->g == 0 || downPix->g == 255)
Toshihiro Shimizu 890ddd
				g = downPix->g;
Toshihiro Shimizu 890ddd
			else if (upPix->g)
Toshihiro Shimizu 890ddd
				g = 255 - (((255 - downPix->g) << 8) / (double)upPix->g);
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				g = 0;
Toshihiro Shimizu 890ddd
			if (downPix->b == 0 || downPix->b == 255)
Toshihiro Shimizu 890ddd
				b = downPix->b;
Toshihiro Shimizu 890ddd
			else if (upPix->b)
Toshihiro Shimizu 890ddd
				b = 255 - (((255 - downPix->b) << 8) / (double)upPix->b);
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				b = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (upPix->m != 255) {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				TPixel32 tmpPix;
Toshihiro Shimizu 890ddd
				tmpPix.r = (UCHAR)tcrop<double>(r, .0, 255.0);</double>
Toshihiro Shimizu 890ddd
				tmpPix.g = (UCHAR)tcrop<double>(g, .0, 255.0);</double>
Toshihiro Shimizu 890ddd
				tmpPix.b = (UCHAR)tcrop<double>(b, .0, 255.0);</double>
Toshihiro Shimizu 890ddd
				tmpPix.m = upPix->m;
Toshihiro Shimizu 890ddd
				;
Toshihiro Shimizu 890ddd
				overPix<tpixel32, uchar="">(*outPix, *downPix, tmpPix);</tpixel32,>
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				outPix->r = (UCHAR)tcrop<double>(r, .0, 255.0);</double>
Toshihiro Shimizu 890ddd
				outPix->g = (UCHAR)tcrop<double>(g, .0, 255.0);</double>
Toshihiro Shimizu 890ddd
				outPix->b = (UCHAR)tcrop<double>(b, .0, 255.0);</double>
Toshihiro Shimizu 890ddd
				outPix->m = downPix->m;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			outPix = downPix;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		TRaster64P up64 = rup;
Toshihiro Shimizu 890ddd
		TRaster64P down64 = rdown;
Toshihiro Shimizu 890ddd
		TRaster64P out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (up64 && down64 && out64) {
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
			double r, g, b;
Toshihiro Shimizu 890ddd
			if (upPix->m) {
Toshihiro Shimizu 890ddd
				if (downPix->r == 0 || downPix->r == 65535)
Toshihiro Shimizu 890ddd
					r = downPix->r;
Toshihiro Shimizu 890ddd
				else if (upPix->r)
Toshihiro Shimizu 890ddd
					r = 65535 - 65536 * ((65535.0 - downPix->r) / (double)upPix->r);
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					r = 0;
Toshihiro Shimizu 890ddd
				if (downPix->g == 0 || downPix->g == 65535)
Toshihiro Shimizu 890ddd
					g = downPix->g;
Toshihiro Shimizu 890ddd
				else if (upPix->g)
Toshihiro Shimizu 890ddd
					g = 65535 - 65536 * ((65535.0 - downPix->g) / (double)upPix->g);
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					g = 0;
Toshihiro Shimizu 890ddd
				if (downPix->b == 0 || downPix->b == 65535)
Toshihiro Shimizu 890ddd
					b = downPix->b;
Toshihiro Shimizu 890ddd
				else if (upPix->b)
Toshihiro Shimizu 890ddd
					b = 65535 - 65536 * ((65535.0 - downPix->b) / (double)upPix->b);
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					b = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (upPix->m != 65535) {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					TPixel64 tmpPix;
Toshihiro Shimizu 890ddd
					tmpPix.r = (USHORT)tcrop<double>(r, .0, 65535.0);</double>
Toshihiro Shimizu 890ddd
					tmpPix.g = (USHORT)tcrop<double>(g, .0, 65535.0);</double>
Toshihiro Shimizu 890ddd
					tmpPix.b = (USHORT)tcrop<double>(b, .0, 65535.0);</double>
Toshihiro Shimizu 890ddd
					tmpPix.m = upPix->m;
Toshihiro Shimizu 890ddd
					overPix<tpixel64, ushort="">(*outPix, *downPix, tmpPix);</tpixel64,>
Toshihiro Shimizu 890ddd
				} else {
Toshihiro Shimizu 890ddd
					outPix->r = (USHORT)tcrop<double>(r, .0, 65535.0);</double>
Toshihiro Shimizu 890ddd
					outPix->g = (USHORT)tcrop<double>(g, .0, 65535.0);</double>
Toshihiro Shimizu 890ddd
					outPix->b = (USHORT)tcrop<double>(b, .0, 65535.0);</double>
Toshihiro Shimizu 890ddd
					outPix->m = downPix->m;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				outPix = downPix;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			throw TRopException("TRop::color burn invalid raster combination");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::screen(const TRasterP &rup, const TRasterP &rdown, const TRasterP &rout)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P up32 = rup;
Toshihiro Shimizu 890ddd
	TRaster32P down32 = rdown;
Toshihiro Shimizu 890ddd
	TRaster32P out32 = rout;
Toshihiro Shimizu 890ddd
	if (up32 && down32 && out32) {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		double r, g, b;
Toshihiro Shimizu 890ddd
		r = 256 - ((256 - upPix->r) * (256 - downPix->r) >> 8);
Toshihiro Shimizu 890ddd
		g = 256 - ((256 - upPix->g) * (256 - downPix->g) >> 8);
Toshihiro Shimizu 890ddd
		b = 256 - ((256 - upPix->b) * (256 - downPix->b) >> 8);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (upPix->m != 255) {
Toshihiro Shimizu 890ddd
			double m;
Toshihiro Shimizu 890ddd
			m = 256 - ((256 - upPix->m) * (256 - downPix->m) >> 8);
Toshihiro Shimizu 890ddd
			TPixel32 tmpPix;
Toshihiro Shimizu 890ddd
			tmpPix.r = (UCHAR)tcrop<double>(r, 0, 255);</double>
Toshihiro Shimizu 890ddd
			tmpPix.g = (UCHAR)tcrop<double>(g, 0, 255);</double>
Toshihiro Shimizu 890ddd
			tmpPix.b = (UCHAR)tcrop<double>(b, 0, 255);</double>
Toshihiro Shimizu 890ddd
			tmpPix.m = (UCHAR)tcrop<double>(m, 0, 255);</double>
Toshihiro Shimizu 890ddd
			overPix<tpixel32, uchar="">(*outPix, *downPix, tmpPix);</tpixel32,>
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			outPix->r = (UCHAR)tcrop<double>(r, 0, 255);</double>
Toshihiro Shimizu 890ddd
			outPix->g = (UCHAR)tcrop<double>(g, 0, 255);</double>
Toshihiro Shimizu 890ddd
			outPix->b = (UCHAR)tcrop<double>(b, 0, 255);</double>
Toshihiro Shimizu 890ddd
			outPix->m = upPix->m;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		TRaster64P up64 = rup;
Toshihiro Shimizu 890ddd
		TRaster64P down64 = rdown;
Toshihiro Shimizu 890ddd
		TRaster64P out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (up64 && down64 && out64) {
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			double r, g, b;
Toshihiro Shimizu 890ddd
			r = 65536 - (65536 - upPix->r) * ((65536 - downPix->r) / 65536.0);
Toshihiro Shimizu 890ddd
			g = 65536 - (65536 - upPix->g) * ((65536 - downPix->g) / 65536.0);
Toshihiro Shimizu 890ddd
			b = 65536 - (65536 - upPix->b) * ((65536 - downPix->b) / 65536.0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (upPix->m != 65535) {
Toshihiro Shimizu 890ddd
				double m;
Toshihiro Shimizu 890ddd
				m = 65536 - (65536 - upPix->m) * ((65536 - downPix->m) / 65536.0);
Toshihiro Shimizu 890ddd
				TPixel64 tmpPix;
Toshihiro Shimizu 890ddd
				tmpPix.r = (USHORT)tcrop<double>(r, 0, 65535);</double>
Toshihiro Shimizu 890ddd
				tmpPix.g = (USHORT)tcrop<double>(g, 0, 65535);</double>
Toshihiro Shimizu 890ddd
				tmpPix.b = (USHORT)tcrop<double>(b, 0, 65535);</double>
Toshihiro Shimizu 890ddd
				tmpPix.m = (USHORT)tcrop<double>(m, 0, 65535);</double>
Toshihiro Shimizu 890ddd
				overPix<tpixel64, ushort="">(*outPix, *downPix, tmpPix);</tpixel64,>
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				outPix->r = (USHORT)tcrop<double>(r, 0, 65535);</double>
Toshihiro Shimizu 890ddd
				outPix->g = (USHORT)tcrop<double>(g, 0, 65535);</double>
Toshihiro Shimizu 890ddd
				outPix->b = (USHORT)tcrop<double>(b, 0, 65535);</double>
Toshihiro Shimizu 890ddd
				outPix->m = upPix->m;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			TRasterGR8P up8 = rup;
Toshihiro Shimizu 890ddd
			TRasterGR8P down8 = rdown;
Toshihiro Shimizu 890ddd
			TRasterGR8P out8 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (up8 && down8 && out8) {
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_8_BEGIN_LOOP
Toshihiro Shimizu 890ddd
				USHORT value;
Toshihiro Shimizu 890ddd
				if (downPix->value)
Toshihiro Shimizu 890ddd
					value = (USHORT)((downPix->value << 8) / (255.0 - upPix->value));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				outPix->value = (UCHAR)tcrop<ushort>(value, 0, 255);</ushort>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_8_END_LOOP
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				throw TRopException("TRop::color dodge invalid raster combination");
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::sub(const TRasterP &rup, const TRasterP &rdown, const TRasterP &rout, bool matte)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P up32 = rup;
Toshihiro Shimizu 890ddd
	TRaster32P down32 = rdown;
Toshihiro Shimizu 890ddd
	TRaster32P out32 = rout;
Toshihiro Shimizu 890ddd
	if (matte) {
Toshihiro Shimizu 890ddd
		if (up32 && down32 && out32) {
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			SHORT r = downPix->r - upPix->r;
Toshihiro Shimizu 890ddd
			SHORT g = downPix->g - upPix->g;
Toshihiro Shimizu 890ddd
			SHORT b = downPix->b - upPix->b;
Toshihiro Shimizu 890ddd
			SHORT m = downPix->m - upPix->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			outPix->r = (UCHAR)tcrop<short>(r, 0, 255);</short>
Toshihiro Shimizu 890ddd
			outPix->g = (UCHAR)tcrop<short>(g, 0, 255);</short>
Toshihiro Shimizu 890ddd
			outPix->b = (UCHAR)tcrop<short>(b, 0, 255);</short>
Toshihiro Shimizu 890ddd
			outPix->m = (UCHAR)tcrop<short>(m, 0, 255);</short>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			TRaster64P up64 = rup;
Toshihiro Shimizu 890ddd
			TRaster64P down64 = rdown;
Toshihiro Shimizu 890ddd
			TRaster64P out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (up64 && down64 && out64) {
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				TINT32 r = downPix->r - upPix->r;
Toshihiro Shimizu 890ddd
				TINT32 g = downPix->g - upPix->g;
Toshihiro Shimizu 890ddd
				TINT32 b = downPix->b - upPix->b;
Toshihiro Shimizu 890ddd
				TINT32 m = downPix->m - upPix->m;
Toshihiro Shimizu 890ddd
				outPix->r = (USHORT)tcrop<tint32>(r, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
				outPix->g = (USHORT)tcrop<tint32>(g, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
				outPix->b = (USHORT)tcrop<tint32>(b, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
				outPix->m = (USHORT)tcrop<tint32>(m, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				TRasterGR8P up8 = rup;
Toshihiro Shimizu 890ddd
				TRasterGR8P down8 = rdown;
Toshihiro Shimizu 890ddd
				TRasterGR8P out8 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (up8 && down8 && out8) {
Toshihiro Shimizu 890ddd
					FOR_EACH_PIXEL_8_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					SHORT value = upPix->value - downPix->value;
Toshihiro Shimizu 890ddd
					outPix->value = (UCHAR)tcrop<short>(value, 0, 255);</short>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					FOR_EACH_PIXEL_8_END_LOOP
Toshihiro Shimizu 890ddd
				} else
Toshihiro Shimizu 890ddd
					throw TRopException("TRop::sub invalid raster combination");
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		if (up32 && down32 && out32) {
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			SHORT r = downPix->r - upPix->r;
Toshihiro Shimizu 890ddd
			SHORT g = downPix->g - upPix->g;
Toshihiro Shimizu 890ddd
			SHORT b = downPix->b - upPix->b;
Toshihiro Shimizu 890ddd
			SHORT m = downPix->m; // - upPix->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			outPix->r = (UCHAR)tcrop<short>(r, 0, 255);</short>
Toshihiro Shimizu 890ddd
			outPix->g = (UCHAR)tcrop<short>(g, 0, 255);</short>
Toshihiro Shimizu 890ddd
			outPix->b = (UCHAR)tcrop<short>(b, 0, 255);</short>
Toshihiro Shimizu 890ddd
			outPix->m = (UCHAR)tcrop<short>(m, 0, 255);</short>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			TRaster64P up64 = rup;
Toshihiro Shimizu 890ddd
			TRaster64P down64 = rdown;
Toshihiro Shimizu 890ddd
			TRaster64P out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (up64 && down64 && out64) {
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				TINT32 r = downPix->r - upPix->r;
Toshihiro Shimizu 890ddd
				TINT32 g = downPix->g - upPix->g;
Toshihiro Shimizu 890ddd
				TINT32 b = downPix->b - upPix->b;
Toshihiro Shimizu 890ddd
				TINT32 m = downPix->m; // - upPix->m;
Toshihiro Shimizu 890ddd
				outPix->r = (USHORT)tcrop<tint32>(r, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
				outPix->g = (USHORT)tcrop<tint32>(g, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
				outPix->b = (USHORT)tcrop<tint32>(b, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
				outPix->m = (USHORT)tcrop<tint32>(m, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				TRasterGR8P up8 = rup;
Toshihiro Shimizu 890ddd
				TRasterGR8P down8 = rdown;
Toshihiro Shimizu 890ddd
				TRasterGR8P out8 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (up8 && down8 && out8) {
Toshihiro Shimizu 890ddd
					FOR_EACH_PIXEL_8_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					SHORT value = upPix->value - downPix->value;
Toshihiro Shimizu 890ddd
					outPix->value = (UCHAR)tcrop<short>(value, 0, 255);</short>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					FOR_EACH_PIXEL_8_END_LOOP
Toshihiro Shimizu 890ddd
				} else
Toshihiro Shimizu 890ddd
					throw TRopException("TRop::sub invalid raster combination");
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::mult(const TRasterP &rup, const TRasterP &rdown, const TRasterP &rout, int v, bool matte)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
    Let U be 'up', D be 'down' and M be 'multiplied' (the result), and suppose for the moment
Toshihiro Shimizu 890ddd
    that pixels are both NOT premultiplied and normalized to [0, 1].
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    The additional value v is used to add to RGB components of the U pixel:
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
      U'_rgb = (U_rgb + v / 255)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    The matte component is either U_m D_m in case (matte == true), or D_m in case (matte == false).
Toshihiro Shimizu 890ddd
    Please, observe that in the case (matte == false) that choice makes the product NOT COMMUTATIVE,
Toshihiro Shimizu 890ddd
    but I think that's justified - it's simply the most obvious use case.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    In case (matte == true), each channel is multiplied independently, and that's it.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    The matter is more complicated when (matte == false). The problem in this case is dealing with rgb
Toshihiro Shimizu 890ddd
    components when U and D both have some transparency.  When U is fully transparent, we expect the
Toshihiro Shimizu 890ddd
    result to be D, and vice-versa, which is non-trivial.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    We REQUIRE that (let's denote r only here):
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
      M_r = M_r_u = U_r (1 - D_m) + U_r D_r D_m,   when  U_m == 1    (When U is fully opaque, M_r is a D_m-linear
Toshihiro Shimizu 890ddd
                                                                      combination of U_r and U_r D_r)
Toshihiro Shimizu 890ddd
      M_r = M_r_d = D_r (1 - U_m) + U_r D_r U_m,   when  D_m == 1    (Vice-versa, when it's D that is fully opaque)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    Finally, we're building a weighted sum, by U_m and D_m of the two above:
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
      M_r = (M_r_u * U_m + M_r_d * D_m) / (U_m + D_m) =
Toshihiro Shimizu 890ddd
          = (...) =
Toshihiro Shimizu 890ddd
          = [ U_r U_m (1 - D_m) + D_r D_m (1 - U_m) + 2 U_r U_m  D_r D_m ] / (U_m + D_m)
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// 32-bit images case
Toshihiro Shimizu 890ddd
	TRaster32P up32 = rup,
Toshihiro Shimizu 890ddd
			   down32 = rdown,
Toshihiro Shimizu 890ddd
			   out32 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (up32 && down32 && out32) {
Toshihiro Shimizu 890ddd
		static const float maxChannelF = float(TPixel32::maxChannelValue);
Toshihiro Shimizu 890ddd
		static const UCHAR maxChannelC = UCHAR(TPixel32::maxChannelValue);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		float vf = v;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (matte) {
Toshihiro Shimizu 890ddd
			float dnMf, upMf_norm, outMf;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_32_BEGIN_LOOP // Awful... should be explicit...
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				dnMf = downPix->m;
Toshihiro Shimizu 890ddd
			upMf_norm = upPix->m / maxChannelF;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			outMf = downPix->m * upMf_norm;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			outPix->r = tcrop((upPix->r / upMf_norm + vf) * (downPix->r / dnMf), 0.0f, outMf);
Toshihiro Shimizu 890ddd
			outPix->g = tcrop((upPix->g / upMf_norm + vf) * (downPix->g / dnMf), 0.0f, outMf);
Toshihiro Shimizu 890ddd
			outPix->b = tcrop((upPix->b / upMf_norm + vf) * (downPix->b / dnMf), 0.0f, outMf);
Toshihiro Shimizu 890ddd
			outPix->m = outMf;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			// NOTE:  + 0.5f in the crop arguments could take care of rounding...
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			float umf_norm, dmf_norm, umdmf_norm, outMf;
Toshihiro Shimizu 890ddd
			float mSumf, uf, df, ufdf, normalizer;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			mSumf = upPix->m + float(downPix->m);
Toshihiro Shimizu 890ddd
			if (mSumf > 0.0f) {
Toshihiro Shimizu 890ddd
				umf_norm = upPix->m / maxChannelF, dmf_norm = downPix->m / maxChannelF;
Toshihiro Shimizu 890ddd
				outMf = upPix->m + (1.0f - umf_norm) * downPix->m; // umf_norm should be ensured in [0.0, 1.0].
Toshihiro Shimizu 890ddd
																   // Convex combination should be in the conversion range.
Toshihiro Shimizu 890ddd
				normalizer = outMf / (maxChannelF * mSumf);
Toshihiro Shimizu 890ddd
				umdmf_norm = umf_norm * dmf_norm;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				uf = upPix->r + vf * umdmf_norm, df = downPix->r, ufdf = uf * df;
Toshihiro Shimizu 890ddd
				outPix->r = tcrop((uf * (maxChannelC - downPix->m) + df * (maxChannelC - upPix->m) + ufdf + ufdf) * normalizer, 0.0f, outMf);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				uf = upPix->g + vf * umdmf_norm, df = downPix->g, ufdf = uf * df;
Toshihiro Shimizu 890ddd
				outPix->g = tcrop((uf * (maxChannelC - downPix->m) + df * (maxChannelC - upPix->m) + ufdf + ufdf) * normalizer, 0.0f, outMf);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				uf = upPix->b + vf * umdmf_norm, df = downPix->b, ufdf = uf * df;
Toshihiro Shimizu 890ddd
				outPix->b = tcrop((uf * (maxChannelC - downPix->m) + df * (maxChannelC - upPix->m) + ufdf + ufdf) * normalizer, 0.0f, outMf);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				outPix->m = outMf;
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				*outPix = TPixel32::Transparent;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// 64-bit images case
Toshihiro Shimizu 890ddd
	TRaster64P up64 = rup,
Toshihiro Shimizu 890ddd
			   down64 = rdown,
Toshihiro Shimizu 890ddd
			   out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (up64 && down64 && out64) {
Toshihiro Shimizu 890ddd
		static const double maxChannelF = double(TPixel64::maxChannelValue);
Toshihiro Shimizu 890ddd
		static const USHORT maxChannelC = USHORT(TPixel64::maxChannelValue);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		double vf = v * (TPixel64::maxChannelValue / double(TPixel32::maxChannelValue));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (matte) {
Toshihiro Shimizu 890ddd
			double dnMf, upMf_norm, outMf;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			dnMf = downPix->m;
Toshihiro Shimizu 890ddd
			upMf_norm = upPix->m / maxChannelF;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			outMf = downPix->m * upMf_norm;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			outPix->r = tcrop((upPix->r / upMf_norm + vf) * (downPix->r / dnMf), 0.0, outMf);
Toshihiro Shimizu 890ddd
			outPix->g = tcrop((upPix->g / upMf_norm + vf) * (downPix->g / dnMf), 0.0, outMf);
Toshihiro Shimizu 890ddd
			outPix->b = tcrop((upPix->b / upMf_norm + vf) * (downPix->b / dnMf), 0.0, outMf);
Toshihiro Shimizu 890ddd
			outPix->m = outMf;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			double umf_norm, dmf_norm, umdmf_norm, outMf;
Toshihiro Shimizu 890ddd
			double mSumf, uf, df, ufdf, normalizer;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			mSumf = upPix->m + double(downPix->m);
Toshihiro Shimizu 890ddd
			if (mSumf > 0.0) {
Toshihiro Shimizu 890ddd
				umf_norm = upPix->m / maxChannelF, dmf_norm = downPix->m / maxChannelF;
Toshihiro Shimizu 890ddd
				outMf = upPix->m + (1.0 - umf_norm) * downPix->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				normalizer = outMf / (maxChannelF * mSumf);
Toshihiro Shimizu 890ddd
				umdmf_norm = umf_norm * dmf_norm;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				uf = upPix->r + vf * umdmf_norm, df = downPix->r, ufdf = uf * df;
Toshihiro Shimizu 890ddd
				outPix->r = tcrop((uf * (maxChannelC - downPix->m) + df * (maxChannelC - upPix->m) + ufdf + ufdf) * normalizer, 0.0, outMf);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				uf = upPix->g + vf * umdmf_norm, df = downPix->g, ufdf = uf * df;
Toshihiro Shimizu 890ddd
				outPix->g = tcrop((uf * (maxChannelC - downPix->m) + df * (maxChannelC - upPix->m) + ufdf + ufdf) * normalizer, 0.0, outMf);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				uf = upPix->b + vf * umdmf_norm, df = downPix->b, ufdf = uf * df;
Toshihiro Shimizu 890ddd
				outPix->b = tcrop((uf * (maxChannelC - downPix->m) + df * (maxChannelC - upPix->m) + ufdf + ufdf) * normalizer, 0.0, outMf);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				outPix->m = outMf;
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				*outPix = TPixel64::Transparent;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// According to the specifics, throw an exception. I think it's not appropriate, though.
Toshihiro Shimizu 890ddd
	throw TRopException("TRop::mult invalid raster combination");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::ropin(const TRasterP &source, const TRasterP &matte, const TRasterP &rout)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P source32 = source;
Toshihiro Shimizu 890ddd
	TRaster32P matte32 = matte;
Toshihiro Shimizu 890ddd
	TRaster32P out32 = rout;
Toshihiro Shimizu 890ddd
	TRaster64P source64 = source;
Toshihiro Shimizu 890ddd
	TRaster64P matte64 = matte;
Toshihiro Shimizu 890ddd
	TRaster64P out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (source32 && matte32 && out32) {
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_BEGIN_LOOP(TPixelRGBM32, source32, TPixelRGBM32, matte32, TPixelRGBM32, out32)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (downPix->m == 0)
Toshihiro Shimizu 890ddd
			outPix->r = outPix->g = outPix->b = outPix->m = 0;
Toshihiro Shimizu 890ddd
		else if (downPix->m == 255)
Toshihiro Shimizu 890ddd
			*outPix = *upPix;
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			/*
Toshihiro Shimizu 890ddd
      __m128i zeros = _mm_setzero_si128();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	    __m128i upPix_packed_i= _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(DWORD*)upPix), zeros);
Toshihiro Shimizu 890ddd
	    __m128  upPix_packed  = _mm_cvtepi32_ps(_mm_unpacklo_epi16(upPix_packed_i, zeros));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
      float fac = downPix->m / 255.0;
Toshihiro Shimizu 890ddd
      __m128 fac_packed = _mm_load1_ps(&fac);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
      upPix_packed = _mm_mul_ps(upPix_packed, fac_packed);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
      __m128i outPix_packed_i = _mm_cvtps_epi32(upPix_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
      *(DWORD*)(outPix) = _mm_cvtsi128_si32(outPix_packed_i);      
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			const int MAGICFAC = (257U * 256U + 1U);
Toshihiro Shimizu 890ddd
			UINT fac = MAGICFAC * downPix->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			outPix->r = (UINT)(upPix->r * fac + (1U << 23)) >> 24;
Toshihiro Shimizu 890ddd
			outPix->g = (UINT)(upPix->g * fac + (1U << 23)) >> 24;
Toshihiro Shimizu 890ddd
			outPix->b = (UINT)(upPix->b * fac + (1U << 23)) >> 24;
Toshihiro Shimizu 890ddd
			outPix->m = (UINT)(upPix->m * fac + (1U << 23)) >> 24;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_END_LOOP(source32, matte32, out32)
Toshihiro Shimizu 890ddd
	} else if (source64 && matte64 && out64)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_BEGIN_LOOP(TPixelRGBM64, source64, TPixelRGBM64, matte64, TPixelRGBM64, out64)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (downPix->m == 0)
Toshihiro Shimizu 890ddd
			outPix->r = outPix->g = outPix->b = outPix->m = 0;
Toshihiro Shimizu 890ddd
		else if (downPix->m == 65535)
Toshihiro Shimizu 890ddd
			*outPix = *upPix;
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			/*
Toshihiro Shimizu 890ddd
      __m128i zeros = _mm_setzero_si128();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	    __m128i upPix_packed_i= _mm_unpacklo_epi8(_mm_cvtsi32_si128(*(DWORD*)upPix), zeros);
Toshihiro Shimizu 890ddd
	    __m128  upPix_packed  = _mm_cvtepi32_ps(_mm_unpacklo_epi16(upPix_packed_i, zeros));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
      float fac = downPix->m / 255.0;
Toshihiro Shimizu 890ddd
      __m128 fac_packed = _mm_load1_ps(&fac);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
      upPix_packed = _mm_mul_ps(upPix_packed, fac_packed);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
      __m128i outPix_packed_i = _mm_cvtps_epi32(upPix_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
      *(DWORD*)(outPix) = _mm_cvtsi128_si32(outPix_packed_i);      
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
			double fac = downPix->m / 65535.0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			outPix->r = (USHORT)(upPix->r * fac);
Toshihiro Shimizu 890ddd
			outPix->g = (USHORT)(upPix->g * fac);
Toshihiro Shimizu 890ddd
			outPix->b = (USHORT)(upPix->b * fac);
Toshihiro Shimizu 890ddd
			outPix->m = (USHORT)(upPix->m * fac);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_END_LOOP(source64, matte64, out64)
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		throw TRopException("TRop::in invalid raster combination");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::ropout(const TRasterP &source, const TRasterP &matte, const TRasterP &rout)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P source32 = source;
Toshihiro Shimizu 890ddd
	TRaster32P matte32 = matte;
Toshihiro Shimizu 890ddd
	TRaster32P out32 = rout;
Toshihiro Shimizu 890ddd
	TRaster64P source64 = source;
Toshihiro Shimizu 890ddd
	TRaster64P matte64 = matte;
Toshihiro Shimizu 890ddd
	TRaster64P out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (source32 && matte32 && out32) {
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_BEGIN_LOOP(TPixelRGBM32, source32, TPixelRGBM32, matte32, TPixelRGBM32, out32)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (downPix->m == 255)
Toshihiro Shimizu 890ddd
			outPix->r = outPix->g = outPix->b = outPix->m = 0;
Toshihiro Shimizu 890ddd
		else if (downPix->m == 0)
Toshihiro Shimizu 890ddd
			*outPix = *upPix;
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			const int MAGICFAC = (257U * 256U + 1U);
Toshihiro Shimizu 890ddd
			UINT fac = MAGICFAC * (255 - downPix->m);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			outPix->r = (UINT)(upPix->r * fac + (1U << 23)) >> 24;
Toshihiro Shimizu 890ddd
			outPix->g = (UINT)(upPix->g * fac + (1U << 23)) >> 24;
Toshihiro Shimizu 890ddd
			outPix->b = (UINT)(upPix->b * fac + (1U << 23)) >> 24;
Toshihiro Shimizu 890ddd
			outPix->m = (UINT)(upPix->m * fac + (1U << 23)) >> 24;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_END_LOOP(source32, matte32, out32)
Toshihiro Shimizu 890ddd
	} else if (source64 && matte64 && out64) {
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_BEGIN_LOOP(TPixelRGBM64, source64, TPixelRGBM64, matte64, TPixelRGBM64, out64)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (downPix->m == 65535)
Toshihiro Shimizu 890ddd
			outPix->r = outPix->g = outPix->b = outPix->m = 0;
Toshihiro Shimizu 890ddd
		else if (downPix->m == 0)
Toshihiro Shimizu 890ddd
			*outPix = *upPix;
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			double fac = (65535 - downPix->m) / 65535.0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			outPix->r = (USHORT)(upPix->r * fac);
Toshihiro Shimizu 890ddd
			outPix->g = (USHORT)(upPix->g * fac);
Toshihiro Shimizu 890ddd
			outPix->b = (USHORT)(upPix->b * fac);
Toshihiro Shimizu 890ddd
			outPix->m = (USHORT)(upPix->m * fac);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_END_LOOP(source64, matte64, out64)
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		throw TRopException("TRop::out invalid raster combination");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::atop(const TRasterP &rup, const TRasterP &rdown, const TRasterP &rout)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	// calcola rup ATOP rdown
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// da ottimizzare...
Toshihiro Shimizu 890ddd
	TRaster32P up32 = rup;
Toshihiro Shimizu 890ddd
	TRaster32P down32 = rdown;
Toshihiro Shimizu 890ddd
	TRaster32P out32 = rout;
Toshihiro Shimizu 890ddd
	TRaster64P up64 = rup;
Toshihiro Shimizu 890ddd
	TRaster64P down64 = rdown;
Toshihiro Shimizu 890ddd
	TRaster64P out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (up32 && down32 && out32) {
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TPixel32 tmpPix(0, 0, 0, 0);
Toshihiro Shimizu 890ddd
		if (downPix->m != 0) {
Toshihiro Shimizu 890ddd
			const int MAGICFAC = (257U * 256U + 1U);
Toshihiro Shimizu 890ddd
			UINT fac = MAGICFAC * downPix->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			tmpPix.r = (UINT)(upPix->r * fac + (1U << 23)) >> 24;
Toshihiro Shimizu 890ddd
			tmpPix.g = (UINT)(upPix->g * fac + (1U << 23)) >> 24;
Toshihiro Shimizu 890ddd
			tmpPix.b = (UINT)(upPix->b * fac + (1U << 23)) >> 24;
Toshihiro Shimizu 890ddd
			tmpPix.m = (UINT)(upPix->m * fac + (1U << 23)) >> 24;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		overPix<tpixel32, uchar="">(*outPix, *downPix, tmpPix);</tpixel32,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
	} else if (up64 && down64 && out64) {
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TPixel64 tmpPix(0, 0, 0, 0);
Toshihiro Shimizu 890ddd
		if (downPix->m != 0) {
Toshihiro Shimizu 890ddd
			double fac = downPix->m / 65535.0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			tmpPix.r = (USHORT)(upPix->r * fac);
Toshihiro Shimizu 890ddd
			tmpPix.g = (USHORT)(upPix->g * fac);
Toshihiro Shimizu 890ddd
			tmpPix.b = (USHORT)(upPix->b * fac);
Toshihiro Shimizu 890ddd
			tmpPix.m = (USHORT)(upPix->m * fac);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		overPix<tpixel64, ushort="">(*outPix, *downPix, tmpPix);</tpixel64,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		throw TRopException("TRop::atop invalid raster combination");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::txor(const TRasterP &rup, const TRasterP &rdown, const TRasterP &rout)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	// da ottimizzare...
Toshihiro Shimizu 890ddd
	TRaster32P up32 = rup;
Toshihiro Shimizu 890ddd
	TRaster32P down32 = rdown;
Toshihiro Shimizu 890ddd
	TRaster32P out32 = rout;
Toshihiro Shimizu 890ddd
	TRaster64P up64 = rup;
Toshihiro Shimizu 890ddd
	TRaster64P down64 = rdown;
Toshihiro Shimizu 890ddd
	TRaster64P out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (up32 && down32 && out32) {
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TUINT32 notUpM = 255 - upPix->m;
Toshihiro Shimizu 890ddd
		TUINT32 notDownM = 255 - downPix->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TUINT32 r = notUpM + upPix->r * (notDownM);
Toshihiro Shimizu 890ddd
		TUINT32 g = notUpM + upPix->g * (notDownM);
Toshihiro Shimizu 890ddd
		TUINT32 b = notUpM + upPix->b * (notDownM);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		outPix->r = (UCHAR)tcrop<tuint32>(0, 255, r);</tuint32>
Toshihiro Shimizu 890ddd
		outPix->g = (UCHAR)tcrop<tuint32>(0, 255, g);</tuint32>
Toshihiro Shimizu 890ddd
		outPix->b = (UCHAR)tcrop<tuint32>(0, 255, b);</tuint32>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
	} else if (up64 && down64 && out64) {
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TUINT32 notUpM = 65535 - upPix->m;
Toshihiro Shimizu 890ddd
		TUINT32 notDownM = 65535 - downPix->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TUINT32 r = notUpM + upPix->r * (notDownM);
Toshihiro Shimizu 890ddd
		TUINT32 g = notUpM + upPix->g * (notDownM);
Toshihiro Shimizu 890ddd
		TUINT32 b = notUpM + upPix->b * (notDownM);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		outPix->r = (USHORT)tcrop<tuint32>(0, 65535, r);</tuint32>
Toshihiro Shimizu 890ddd
		outPix->g = (USHORT)tcrop<tuint32>(0, 65535, g);</tuint32>
Toshihiro Shimizu 890ddd
		outPix->b = (USHORT)tcrop<tuint32>(0, 65535, b);</tuint32>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		throw TRopException("TRop::xor invalid raster combination");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::crossDissolve(const TRasterP &rup, const TRasterP &rdown, const TRasterP &rout, UCHAR v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P up32 = rup;
Toshihiro Shimizu 890ddd
	TRaster32P down32 = rdown;
Toshihiro Shimizu 890ddd
	TRaster32P out32 = rout;
Toshihiro Shimizu 890ddd
	TRaster64P up64 = rup;
Toshihiro Shimizu 890ddd
	TRaster64P down64 = rdown;
Toshihiro Shimizu 890ddd
	TRaster64P out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (up32 && down32 && out32) {
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		outPix->r = (upPix->r * v + downPix->r * (255 - v)) / 255;
Toshihiro Shimizu 890ddd
		outPix->g = (upPix->g * v + downPix->g * (255 - v)) / 255;
Toshihiro Shimizu 890ddd
		outPix->b = (upPix->b * v + downPix->b * (255 - v)) / 255;
Toshihiro Shimizu 890ddd
		outPix->m = (upPix->m * v + downPix->m * (255 - v)) / 255;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
	} else if (up64 && down64 && out64) {
Toshihiro Shimizu 890ddd
		USHORT vv = v * 257;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		outPix->r = (upPix->r * vv + downPix->r * (65535 - vv)) / 65535;
Toshihiro Shimizu 890ddd
		outPix->g = (upPix->g * vv + downPix->g * (65535 - vv)) / 65535;
Toshihiro Shimizu 890ddd
		outPix->b = (upPix->b * vv + downPix->b * (65535 - vv)) / 65535;
Toshihiro Shimizu 890ddd
		outPix->m = (upPix->m * vv + downPix->m * (65535 - vv)) / 65535;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		throw TRopException("TRop::crossDissolve invalid raster combination");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::darken(const TRasterP &rup, const TRasterP &rdown, const TRasterP &rout)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P up32 = rup;
Toshihiro Shimizu 890ddd
	TRaster32P down32 = rdown;
Toshihiro Shimizu 890ddd
	TRaster32P out32 = rout;
Toshihiro Shimizu 890ddd
	TRaster64P up64 = rup;
Toshihiro Shimizu 890ddd
	TRaster64P down64 = rdown;
Toshihiro Shimizu 890ddd
	TRaster64P out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (up32 && down32 && out32) {
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		double value0 = luminance(upPix);
Toshihiro Shimizu 890ddd
		double value1 = luminance(downPix);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (value0 < value1)
Toshihiro Shimizu 890ddd
			*outPix = *upPix;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			*outPix = *downPix;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
	} else if (up64 && down64 && out64) {
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		double value0 = luminance(upPix);
Toshihiro Shimizu 890ddd
		double value1 = luminance(downPix);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (value0 < value1)
Toshihiro Shimizu 890ddd
			*outPix = *upPix;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			*outPix = *downPix;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		throw TRopException("TRop::darken invalid raster combination");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::lighten(const TRasterP &rup, const TRasterP &rdown, const TRasterP &rout)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P up32 = rup;
Toshihiro Shimizu 890ddd
	TRaster32P down32 = rdown;
Toshihiro Shimizu 890ddd
	TRaster32P out32 = rout;
Toshihiro Shimizu 890ddd
	TRaster64P up64 = rup;
Toshihiro Shimizu 890ddd
	TRaster64P down64 = rdown;
Toshihiro Shimizu 890ddd
	TRaster64P out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (up32 && down32 && out32) {
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		double value0 = luminance(upPix);
Toshihiro Shimizu 890ddd
		double value1 = luminance(downPix);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (value0 > value1) {
Toshihiro Shimizu 890ddd
			TINT32 r, g, b, m;
Toshihiro Shimizu 890ddd
			if (upPix->m == 0)
Toshihiro Shimizu 890ddd
				*outPix = *downPix;
Toshihiro Shimizu 890ddd
			else {
Toshihiro Shimizu 890ddd
				r = downPix->r + upPix->r;
Toshihiro Shimizu 890ddd
				g = downPix->g + upPix->g;
Toshihiro Shimizu 890ddd
				b = downPix->b + upPix->b;
Toshihiro Shimizu 890ddd
				m = downPix->m + upPix->m;
Toshihiro Shimizu 890ddd
				outPix->r = (UCHAR)tcrop<tint32>(r, (TINT32)0, (TINT32)255);</tint32>
Toshihiro Shimizu 890ddd
				outPix->g = (UCHAR)tcrop<tint32>(g, (TINT32)0, (TINT32)255);</tint32>
Toshihiro Shimizu 890ddd
				outPix->b = (UCHAR)tcrop<tint32>(b, (TINT32)0, (TINT32)255);</tint32>
Toshihiro Shimizu 890ddd
				outPix->m = (UCHAR)tcrop<tint32>(m, (TINT32)0, (TINT32)255);</tint32>
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			*outPix = *downPix;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
	} else if (up64 && down64 && out64) {
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		double value0 = luminance(upPix);
Toshihiro Shimizu 890ddd
		double value1 = luminance(downPix);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (value0 > value1) {
Toshihiro Shimizu 890ddd
			TINT32 r, g, b, m;
Toshihiro Shimizu 890ddd
			if (upPix->m == 0)
Toshihiro Shimizu 890ddd
				*outPix = *downPix;
Toshihiro Shimizu 890ddd
			else {
Toshihiro Shimizu 890ddd
				r = downPix->r + upPix->r;
Toshihiro Shimizu 890ddd
				g = downPix->g + upPix->g;
Toshihiro Shimizu 890ddd
				b = downPix->b + upPix->b;
Toshihiro Shimizu 890ddd
				m = downPix->m + upPix->m;
Toshihiro Shimizu 890ddd
				outPix->r = (USHORT)tcrop<tint32>(r, (TINT32)0, (TINT32)65535);</tint32>
Toshihiro Shimizu 890ddd
				outPix->g = (USHORT)tcrop<tint32>(g, (TINT32)0, (TINT32)65535);</tint32>
Toshihiro Shimizu 890ddd
				outPix->b = (USHORT)tcrop<tint32>(b, (TINT32)0, (TINT32)65535);</tint32>
Toshihiro Shimizu 890ddd
				outPix->m = (USHORT)tcrop<tint32>(m, (TINT32)0, (TINT32)65535);</tint32>
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			*outPix = *downPix;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		throw TRopException("TRop::lighten invalid raster combination");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::ropmin(const TRasterP &rup, const TRasterP &rdown, const TRasterP &rout, bool matte)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P up32 = rup;
Toshihiro Shimizu 890ddd
	TRaster32P down32 = rdown;
Toshihiro Shimizu 890ddd
	TRaster32P out32 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (up32 && down32 && out32) {
Toshihiro Shimizu 890ddd
		if (matte) {
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			outPix->r = upPix->r < downPix->r ? upPix->r : downPix->r;
Toshihiro Shimizu 890ddd
			outPix->g = upPix->g < downPix->g ? upPix->g : downPix->g;
Toshihiro Shimizu 890ddd
			outPix->b = upPix->b < downPix->b ? upPix->b : downPix->b;
Toshihiro Shimizu 890ddd
			outPix->m = upPix->m < downPix->m ? upPix->m : downPix->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
			if (upPix->m >= 255) {
Toshihiro Shimizu 890ddd
				outPix->r = upPix->r < downPix->r ? upPix->r : downPix->r;
Toshihiro Shimizu 890ddd
				outPix->g = upPix->g < downPix->g ? upPix->g : downPix->g;
Toshihiro Shimizu 890ddd
				outPix->b = upPix->b < downPix->b ? upPix->b : downPix->b;
Toshihiro Shimizu 890ddd
				outPix->m = upPix->m < downPix->m ? upPix->m : downPix->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			} else if (upPix->m) {
Toshihiro Shimizu 890ddd
				TPixel32 tmp;
Toshihiro Shimizu 890ddd
				tmp.r = upPix->r < downPix->r ? upPix->r : downPix->r;
Toshihiro Shimizu 890ddd
				tmp.g = upPix->g < downPix->g ? upPix->g : downPix->g;
Toshihiro Shimizu 890ddd
				tmp.b = upPix->b < downPix->b ? upPix->b : downPix->b;
Toshihiro Shimizu 890ddd
				//tmp.m = upPix->m < downPix->m ? upPix->m : downPix->m;
Toshihiro Shimizu 890ddd
				outPix->r = upPix->m * (tmp.r - downPix->r) / 255.0 + downPix->r;
Toshihiro Shimizu 890ddd
				outPix->g = upPix->m * (tmp.g - downPix->g) / 255.0 + downPix->g;
Toshihiro Shimizu 890ddd
				outPix->b = upPix->m * (tmp.b - downPix->b) / 255.0 + downPix->b;
Toshihiro Shimizu 890ddd
				outPix->m = upPix->m * (tmp.m - downPix->m) / 255.0 + downPix->m;
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				*outPix = *downPix;
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		TRaster64P up64 = rup;
Toshihiro Shimizu 890ddd
		TRaster64P down64 = rdown;
Toshihiro Shimizu 890ddd
		TRaster64P out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (up64 && down64 && out64) {
Toshihiro Shimizu 890ddd
			if (matte) {
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				outPix->r = upPix->r < downPix->r ? upPix->r : downPix->r;
Toshihiro Shimizu 890ddd
				outPix->g = upPix->g < downPix->g ? upPix->g : downPix->g;
Toshihiro Shimizu 890ddd
				outPix->b = upPix->b < downPix->b ? upPix->b : downPix->b;
Toshihiro Shimizu 890ddd
				outPix->m = upPix->m < downPix->m ? upPix->m : downPix->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
				if (upPix->m >= 65535) {
Toshihiro Shimizu 890ddd
					outPix->r = upPix->r < downPix->r ? upPix->r : downPix->r;
Toshihiro Shimizu 890ddd
					outPix->g = upPix->g < downPix->g ? upPix->g : downPix->g;
Toshihiro Shimizu 890ddd
					outPix->b = upPix->b < downPix->b ? upPix->b : downPix->b;
Toshihiro Shimizu 890ddd
					outPix->m = upPix->m < downPix->m ? upPix->m : downPix->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				} else if (upPix->m) {
Toshihiro Shimizu 890ddd
					TPixel32 tmp;
Toshihiro Shimizu 890ddd
					tmp.r = upPix->r < downPix->r ? upPix->r : downPix->r;
Toshihiro Shimizu 890ddd
					tmp.g = upPix->g < downPix->g ? upPix->g : downPix->g;
Toshihiro Shimizu 890ddd
					tmp.b = upPix->b < downPix->b ? upPix->b : downPix->b;
Toshihiro Shimizu 890ddd
					//tmp.m = upPix->m < downPix->m ? upPix->m : downPix->m;
Toshihiro Shimizu 890ddd
					outPix->r = upPix->m * (tmp.r - downPix->r) / 65535.0 + downPix->r;
Toshihiro Shimizu 890ddd
					outPix->g = upPix->m * (tmp.g - downPix->g) / 65535.0 + downPix->g;
Toshihiro Shimizu 890ddd
					outPix->b = upPix->m * (tmp.b - downPix->b) / 65535.0 + downPix->b;
Toshihiro Shimizu 890ddd
					outPix->m = upPix->m * (tmp.m - downPix->m) / 65535.0 + downPix->m;
Toshihiro Shimizu 890ddd
				} else
Toshihiro Shimizu 890ddd
					*outPix = *downPix;
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			TRasterGR8P up8 = rup;
Toshihiro Shimizu 890ddd
			TRasterGR8P down8 = rdown;
Toshihiro Shimizu 890ddd
			TRasterGR8P out8 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (up8 && down8 && out8) {
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_8_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				outPix->value = upPix->value < downPix->value ? upPix->value : downPix->value;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_8_END_LOOP
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				throw TRopException("TRop::min invalid raster combination");
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::ropmax(const TRasterP &rup, const TRasterP &rdown, const TRasterP &rout)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P up32 = rup;
Toshihiro Shimizu 890ddd
	TRaster32P down32 = rdown;
Toshihiro Shimizu 890ddd
	TRaster32P out32 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (up32 && down32 && out32) {
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		outPix->r = upPix->r > downPix->r ? upPix->r : downPix->r;
Toshihiro Shimizu 890ddd
		outPix->g = upPix->g > downPix->g ? upPix->g : downPix->g;
Toshihiro Shimizu 890ddd
		outPix->b = upPix->b > downPix->b ? upPix->b : downPix->b;
Toshihiro Shimizu 890ddd
		outPix->m = upPix->m > downPix->m ? upPix->m : downPix->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		TRaster64P up64 = rup;
Toshihiro Shimizu 890ddd
		TRaster64P down64 = rdown;
Toshihiro Shimizu 890ddd
		TRaster64P out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (up64 && down64 && out64) {
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			outPix->r = upPix->r > downPix->r ? upPix->r : downPix->r;
Toshihiro Shimizu 890ddd
			outPix->g = upPix->g > downPix->g ? upPix->g : downPix->g;
Toshihiro Shimizu 890ddd
			outPix->b = upPix->b > downPix->b ? upPix->b : downPix->b;
Toshihiro Shimizu 890ddd
			outPix->m = upPix->m > downPix->m ? upPix->m : downPix->m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			TRasterGR8P up8 = rup;
Toshihiro Shimizu 890ddd
			TRasterGR8P down8 = rdown;
Toshihiro Shimizu 890ddd
			TRasterGR8P out8 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (up8 && down8 && out8) {
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_8_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				outPix->value = upPix->value > downPix->value ? upPix->value : downPix->value;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_8_END_LOOP
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				throw TRopException("TRop::max invalid raster combination");
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::linearburn(const TRasterP &rup, const TRasterP &rdown, const TRasterP &rout)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P up32 = rup;
Toshihiro Shimizu 890ddd
	TRaster32P down32 = rdown;
Toshihiro Shimizu 890ddd
	TRaster32P out32 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (up32 && down32 && out32) {
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
		if (upPix->m) {
Toshihiro Shimizu 890ddd
			TPixel32 app;
Toshihiro Shimizu 890ddd
			if (downPix->m) {
Toshihiro Shimizu 890ddd
				TINT32 r, g, b, m;
Toshihiro Shimizu 890ddd
				TPixel32 tmpPix;
Toshihiro Shimizu 890ddd
				tmpPix = depremultiply(*downPix);
Toshihiro Shimizu 890ddd
				r = tmpPix.r + upPix->r - 255;
Toshihiro Shimizu 890ddd
				g = tmpPix.g + upPix->g - 255;
Toshihiro Shimizu 890ddd
				b = tmpPix.b + upPix->b - 255;
Toshihiro Shimizu 890ddd
				m = tmpPix.m + upPix->m - 255;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				app.r = (UCHAR)tcrop<tint32>(r, (TINT32)0, (TINT32)255);</tint32>
Toshihiro Shimizu 890ddd
				app.g = (UCHAR)tcrop<tint32>(g, (TINT32)0, (TINT32)255);</tint32>
Toshihiro Shimizu 890ddd
				app.b = (UCHAR)tcrop<tint32>(b, (TINT32)0, (TINT32)255);</tint32>
Toshihiro Shimizu 890ddd
				//app.m = (UCHAR)tcrop<tint32> (m, (TINT32)0, (TINT32)255);</tint32>
Toshihiro Shimizu 890ddd
				app.m = upPix->m;
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				app = *upPix;
Toshihiro Shimizu 890ddd
			overPix<tpixel32, uchar="">(*outPix, *downPix, app);</tpixel32,>
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		TRaster64P up64 = rup;
Toshihiro Shimizu 890ddd
		TRaster64P down64 = rdown;
Toshihiro Shimizu 890ddd
		TRaster64P out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (up64 && down64 && out64) {
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (upPix->m) {
Toshihiro Shimizu 890ddd
				TPixel64 app;
Toshihiro Shimizu 890ddd
				if (downPix->m) {
Toshihiro Shimizu 890ddd
					TINT32 r, g, b, m;
Toshihiro Shimizu 890ddd
					TPixel64 tmpPix;
Toshihiro Shimizu 890ddd
					tmpPix = depremultiply(*downPix);
Toshihiro Shimizu 890ddd
					r = tmpPix.r + upPix->r - 65535;
Toshihiro Shimizu 890ddd
					g = tmpPix.g + upPix->g - 65535;
Toshihiro Shimizu 890ddd
					b = tmpPix.b + upPix->b - 65535;
Toshihiro Shimizu 890ddd
					m = tmpPix.m + upPix->m - 65535;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					app.r = (USHORT)tcrop<tint32>(r, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
					app.g = (USHORT)tcrop<tint32>(g, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
					app.b = (USHORT)tcrop<tint32>(b, 0, 0xffff);</tint32>
Toshihiro Shimizu 890ddd
					//app.m = (UCHAR)tcrop<tint32> (m, (TINT32)0, (TINT32)255);</tint32>
Toshihiro Shimizu 890ddd
					app.m = upPix->m;
Toshihiro Shimizu 890ddd
				} else
Toshihiro Shimizu 890ddd
					app = *upPix;
Toshihiro Shimizu 890ddd
				overPix<tpixel64, ushort="">(*outPix, *downPix, app);</tpixel64,>
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			TRasterGR8P up8 = rup;
Toshihiro Shimizu 890ddd
			TRasterGR8P down8 = rdown;
Toshihiro Shimizu 890ddd
			TRasterGR8P out8 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (up8 && down8 && out8) {
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_8_BEGIN_LOOP
Toshihiro Shimizu 890ddd
				USHORT value = troundp(upPix->value + downPix->value - 255);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				outPix->value = (UCHAR)tcrop<ushort>(value, 0, 255);</ushort>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_8_END_LOOP
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				throw TRopException("TRop::max invalid raster combination");
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::overlay(const TRasterP &rup, const TRasterP &rdown, const TRasterP &rout)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P up32 = rup;
Toshihiro Shimizu 890ddd
	TRaster32P down32 = rdown;
Toshihiro Shimizu 890ddd
	TRaster32P out32 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (up32 && down32 && out32) {
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
		if (upPix->m) {
Toshihiro Shimizu 890ddd
			TPixel32 app;
Toshihiro Shimizu 890ddd
			TPixel32 tmpPix, tmp2Pix;
Toshihiro Shimizu 890ddd
			if (downPix->m) {
Toshihiro Shimizu 890ddd
				tmpPix = *downPix;
Toshihiro Shimizu 890ddd
				tmp2Pix = depremultiply(*upPix);
Toshihiro Shimizu 890ddd
				if (tmpPix.r < 128)
Toshihiro Shimizu 890ddd
					app.r = troundp(2 * tmp2Pix.r * (tmpPix.r / 255.0));
Toshihiro Shimizu 890ddd
				else {
Toshihiro Shimizu 890ddd
					SHORT r = 255 * (1 - 2 * (1.0 - tmpPix.r / 255.0) * (1.0 - tmp2Pix.r / 255.0));
Toshihiro Shimizu 890ddd
					app.r = (UCHAR)tcrop<short>(r, 0, 255);</short>
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				if (tmpPix.g < 128)
Toshihiro Shimizu 890ddd
					app.g = troundp(2 * tmp2Pix.g * (tmpPix.g / 255.0));
Toshihiro Shimizu 890ddd
				else {
Toshihiro Shimizu 890ddd
					SHORT g = 255 * (1 - 2 * (1.0 - tmpPix.g / 255.0) * (1.0 - tmp2Pix.g / 255.0));
Toshihiro Shimizu 890ddd
					app.g = (UCHAR)tcrop<short>(g, 0, 255);</short>
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				if (tmpPix.b < 128)
Toshihiro Shimizu 890ddd
					app.b = troundp(2 * tmp2Pix.b * (tmpPix.b / 255.0));
Toshihiro Shimizu 890ddd
				else {
Toshihiro Shimizu 890ddd
					SHORT b = 255 * (1 - 2 * (1.0 - tmpPix.b / 255.0) * (1.0 - tmp2Pix.b / 255.0));
Toshihiro Shimizu 890ddd
					app.b = (UCHAR)tcrop<short>(b, 0, 255);</short>
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				app.m = tmp2Pix.m;
Toshihiro Shimizu 890ddd
				app = premultiply(app);
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				app = *upPix;
Toshihiro Shimizu 890ddd
			overPix<tpixel32, uchar="">(*outPix, *downPix, app);</tpixel32,>
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		TRaster64P up64 = rup;
Toshihiro Shimizu 890ddd
		TRaster64P down64 = rdown;
Toshihiro Shimizu 890ddd
		TRaster64P out64 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (up64 && down64 && out64) {
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (upPix->m) {
Toshihiro Shimizu 890ddd
				TPixel64 app;
Toshihiro Shimizu 890ddd
				TPixel64 tmpPix, tmp2Pix;
Toshihiro Shimizu 890ddd
				if (downPix->m) {
Toshihiro Shimizu 890ddd
					tmpPix = *downPix;
Toshihiro Shimizu 890ddd
					tmp2Pix = depremultiply(*upPix);
Toshihiro Shimizu 890ddd
					if (tmpPix.r < 32768)
Toshihiro Shimizu 890ddd
						app.r = troundp(2 * tmp2Pix.r * (tmpPix.r / 65535.0));
Toshihiro Shimizu 890ddd
					else {
Toshihiro Shimizu 890ddd
						SHORT r = 65535 * (1 - 2 * (1.0 - tmpPix.r / 65535.0) * (1.0 - tmp2Pix.r / 65535.0));
Toshihiro Shimizu 890ddd
						app.r = (USHORT)tcrop<tint32>(r, 0, 65535);</tint32>
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					if (tmpPix.g < 32768)
Toshihiro Shimizu 890ddd
						app.g = troundp(2 * tmp2Pix.g * (tmpPix.g / 65535.0));
Toshihiro Shimizu 890ddd
					else {
Toshihiro Shimizu 890ddd
						SHORT g = 65535 * (1 - 2 * (1.0 - tmpPix.g / 65535.0) * (1.0 - tmp2Pix.g / 65535.0));
Toshihiro Shimizu 890ddd
						app.g = (USHORT)tcrop<tint32>(g, 0, 65535);</tint32>
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					if (tmpPix.b < 32768)
Toshihiro Shimizu 890ddd
						app.b = troundp(2 * tmp2Pix.b * (tmpPix.b / 65535.0));
Toshihiro Shimizu 890ddd
					else {
Toshihiro Shimizu 890ddd
						SHORT b = 65535 * (1 - 2 * (1.0 - tmpPix.b / 65535.0) * (1.0 - tmp2Pix.b / 65535.0));
Toshihiro Shimizu 890ddd
						app.b = (USHORT)tcrop<tint32>(b, 0, 65535);</tint32>
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					app.m = tmp2Pix.m;
Toshihiro Shimizu 890ddd
					app = premultiply(app);
Toshihiro Shimizu 890ddd
				} else
Toshihiro Shimizu 890ddd
					app = *upPix;
Toshihiro Shimizu 890ddd
				overPix<tpixel64, ushort="">(*outPix, *downPix, app);</tpixel64,>
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			TRasterGR8P up8 = rup;
Toshihiro Shimizu 890ddd
			TRasterGR8P down8 = rdown;
Toshihiro Shimizu 890ddd
			TRasterGR8P out8 = rout;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (up8 && down8 && out8) {
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_8_BEGIN_LOOP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				USHORT value;
Toshihiro Shimizu 890ddd
				if (downPix->value < 128)
Toshihiro Shimizu 890ddd
					value = troundp(2 * upPix->value * (downPix->value / 255.0));
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					value = 255 * (1 - 2 * (1.0 - downPix->value / 255.0) * (1.0 - upPix->value / 255.0));
Toshihiro Shimizu 890ddd
				outPix->value = (UCHAR)tcrop<ushort>(value, 0, 255);</ushort>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				FOR_EACH_PIXEL_8_END_LOOP
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				throw TRopException("TRop::max invalid raster combination");
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TRop::premultiply(const TRasterP &ras)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	ras->lock();
Toshihiro Shimizu 890ddd
	TRaster32P ras32 = ras;
Toshihiro Shimizu 890ddd
	if (ras32) {
Toshihiro Shimizu 890ddd
		TPixel32 *endPix, *upPix = 0, *upRow = ras32->pixels();
Toshihiro Shimizu 890ddd
		TPixel32 *lastPix = upRow + ras32->getWrap() * (ras32->getLy() - 1) + ras32->getLx();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		while (upPix < lastPix) {
Toshihiro Shimizu 890ddd
			upPix = upRow;
Toshihiro Shimizu 890ddd
			endPix = upPix + ras32->getLx();
Toshihiro Shimizu 890ddd
			while (upPix < endPix) {
Toshihiro Shimizu 890ddd
				premult(*upPix);
Toshihiro Shimizu 890ddd
				++upPix;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			upRow += ras32->getWrap();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		TRaster64P ras64 = ras;
Toshihiro Shimizu 890ddd
		if (ras64) {
Toshihiro Shimizu 890ddd
			TPixel64 *endPix, *upPix = 0, *upRow = ras64->pixels();
Toshihiro Shimizu 890ddd
			TPixel64 *lastPix = upRow + ras64->getWrap() * (ras64->getLy() - 1) + ras64->getLx();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			while (upPix < lastPix) {
Toshihiro Shimizu 890ddd
				upPix = upRow;
Toshihiro Shimizu 890ddd
				endPix = upPix + ras64->getLx();
Toshihiro Shimizu 890ddd
				while (upPix < endPix) {
Toshihiro Shimizu 890ddd
					premult(*upPix);
Toshihiro Shimizu 890ddd
					++upPix;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				upRow += ras64->getWrap();
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			ras->unlock();
Toshihiro Shimizu 890ddd
			throw TException("TRop::premultiply invalid raster type");
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
void TRop::depremultiply(const TRasterP &ras)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	ras->lock();
Toshihiro Shimizu 890ddd
	TRaster32P ras32 = ras;
Toshihiro Shimizu 890ddd
	if (ras32) {
Toshihiro Shimizu 890ddd
		TPixel32 *endPix, *upPix = 0, *upRow = ras32->pixels();
Toshihiro Shimizu 890ddd
		TPixel32 *lastPix = upRow + ras32->getWrap() * (ras32->getLy() - 1) + ras32->getLx();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		while (upPix < lastPix) {
Toshihiro Shimizu 890ddd
			upPix = upRow;
Toshihiro Shimizu 890ddd
			endPix = upPix + ras32->getLx();
Toshihiro Shimizu 890ddd
			while (upPix < endPix) {
Toshihiro Shimizu 890ddd
				depremult(*upPix);
Toshihiro Shimizu 890ddd
				++upPix;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			upRow += ras32->getWrap();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		TRaster64P ras64 = ras;
Toshihiro Shimizu 890ddd
		if (ras64) {
Toshihiro Shimizu 890ddd
			TPixel64 *endPix, *upPix = 0, *upRow = ras64->pixels();
Toshihiro Shimizu 890ddd
			TPixel64 *lastPix = upRow + ras64->getWrap() * (ras64->getLy() - 1) + ras64->getLx();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			while (upPix < lastPix) {
Toshihiro Shimizu 890ddd
				upPix = upRow;
Toshihiro Shimizu 890ddd
				endPix = upPix + ras64->getLx();
Toshihiro Shimizu 890ddd
				while (upPix < endPix) {
Toshihiro Shimizu 890ddd
					depremult(*upPix);
Toshihiro Shimizu 890ddd
					++upPix;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				upRow += ras64->getWrap();
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			ras->unlock();
Toshihiro Shimizu 890ddd
			throw TException("TRop::depremultiply invalid raster type");
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
void TRop::whiteTransp(const TRasterP &ras)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	ras->lock();
Toshihiro Shimizu 890ddd
	TRaster32P ras32 = ras;
Toshihiro Shimizu 890ddd
	if (ras32) {
Toshihiro Shimizu 890ddd
		TPixel32 *endPix, *upPix = 0, *upRow = ras32->pixels();
Toshihiro Shimizu 890ddd
		TPixel32 *lastPix = upRow + ras32->getWrap() * (ras32->getLy() - 1) + ras32->getLx();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		while (upPix < lastPix) {
Toshihiro Shimizu 890ddd
			upPix = upRow;
Toshihiro Shimizu 890ddd
			endPix = upPix + ras32->getLx();
Toshihiro Shimizu 890ddd
			while (upPix < endPix) {
Toshihiro Shimizu 890ddd
				if (*upPix == TPixel::White)
Toshihiro Shimizu 890ddd
					*upPix = TPixel::Transparent;
Toshihiro Shimizu 890ddd
				++upPix;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			upRow += ras32->getWrap();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		TRaster64P ras64 = ras;
Toshihiro Shimizu 890ddd
		if (ras64) {
Toshihiro Shimizu 890ddd
			TPixel64 *endPix, *upPix = 0, *upRow = ras64->pixels();
Toshihiro Shimizu 890ddd
			TPixel64 *lastPix = upRow + ras64->getWrap() * (ras64->getLy() - 1) + ras64->getLx();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			while (upPix < lastPix) {
Toshihiro Shimizu 890ddd
				upPix = upRow;
Toshihiro Shimizu 890ddd
				endPix = upPix + ras64->getLx();
Toshihiro Shimizu 890ddd
				while (upPix < endPix) {
Toshihiro Shimizu 890ddd
					if (*upPix == TPixel64::White)
Toshihiro Shimizu 890ddd
						*upPix = TPixel64::Transparent;
Toshihiro Shimizu 890ddd
					++upPix;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				upRow += ras64->getWrap();
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			ras->unlock();
Toshihiro Shimizu 890ddd
			throw TException("TRop::premultiply invalid raster type");
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
template <typename chan=""></typename>
Toshihiro Shimizu 890ddd
const double *premultiplyTable()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	static double *table = 0;
Toshihiro Shimizu 890ddd
	if (!table) {
Toshihiro Shimizu 890ddd
		int maxChannelValue = (std::numeric_limits<chan>::max)();</chan>
Toshihiro Shimizu 890ddd
		int chanValuesCount = maxChannelValue + 1;
Toshihiro Shimizu 890ddd
		double maxD = maxChannelValue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		table = new double[chanValuesCount];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (int i = 0; i < chanValuesCount; ++i)
Toshihiro Shimizu 890ddd
			table[i] = i / maxD;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return table;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template DVAPI const double *premultiplyTable<uchar>();</uchar>
Toshihiro Shimizu 890ddd
template DVAPI const double *premultiplyTable<ushort>();</ushort>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename chan=""></typename>
Toshihiro Shimizu 890ddd
const double *depremultiplyTable()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	static double *table = 0;
Toshihiro Shimizu 890ddd
	if (!table) {
Toshihiro Shimizu 890ddd
		int maxChannelValue = (std::numeric_limits<chan>::max)();</chan>
Toshihiro Shimizu 890ddd
		int chanValuesCount = maxChannelValue + 1;
Toshihiro Shimizu 890ddd
		double maxD = maxChannelValue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		table = new double[chanValuesCount];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		table[0] = 0.0;
Toshihiro Shimizu 890ddd
		for (int i = 1; i < chanValuesCount; ++i)
Toshihiro Shimizu 890ddd
			table[i] = maxD / i;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return table;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template DVAPI const double *depremultiplyTable<uchar>();</uchar>
Toshihiro Shimizu 890ddd
template DVAPI const double *depremultiplyTable<ushort>();</ushort>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef FOR_EACH_PIXEL_BEGIN_LOOP
Toshihiro Shimizu 890ddd
#undef FOR_EACH_PIXEL_32_BEGIN_LOOP
Toshihiro Shimizu 890ddd
#undef FOR_EACH_PIXEL_64_BEGIN_LOOP
Toshihiro Shimizu 890ddd
#undef FOR_EACH_PIXEL_8_BEGIN_LOOP
Toshihiro Shimizu 890ddd
#undef FOR_EACH_PIXEL_END_LOOP
Toshihiro Shimizu 890ddd
#undef FOR_EACH_PIXEL_32_END_LOOP
Toshihiro Shimizu 890ddd
#undef FOR_EACH_PIXEL_64_END_LOOP
Toshihiro Shimizu 890ddd
#undef FOR_EACH_PIXEL_8_END_LOOP