Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qstringlist.h></qstringlist.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tsimplecolorstyles.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "ttessellator.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
#include "drawutil.h"
Toshihiro Shimizu 890ddd
#include "tflash.h"
Toshihiro Shimizu 890ddd
#include "tpixelutils.h"
Toshihiro Shimizu 890ddd
#include "tlevel_io.h"
Toshihiro Shimizu 890ddd
#include "timage_io.h"
Toshihiro Shimizu 890ddd
#include "trandom.h"
Toshihiro Shimizu 890ddd
#include "tvectorimage.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzscene.h"
Toshihiro Shimizu 890ddd
#include "toonz/imagestyles.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*************************************************************************************
Toshihiro Shimizu 890ddd
//    TTextureStyle  implementation
Toshihiro Shimizu 890ddd
//*************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TRandom Random;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define QUAD_PRIMITIVE GL_QUAD_STRIP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef std::pair<tpointd, tpointd=""> PointPair;</tpointd,>
Toshihiro Shimizu 890ddd
typedef std::vector<tpointd> PointArray;</tpointd>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
PointPair computeTexParameters(const TPointD &p1,
Toshihiro Shimizu 890ddd
							   const TPointD &p2,
Toshihiro Shimizu 890ddd
							   const TPointD &tex1,
Toshihiro Shimizu 890ddd
							   const TPointD &tex2,
Toshihiro Shimizu 890ddd
							   const PointPair &newP,
Toshihiro Shimizu 890ddd
							   const TDimension &size)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	// texture points
Toshihiro Shimizu 890ddd
	static PointPair tex;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	// set vector of movement
Toshihiro Shimizu 890ddd
	TPointD v = (newP.first + newP.second) * 0.5 - (p2 + p1) * 0.5;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// compute length of opposite segment
Toshihiro Shimizu 890ddd
	double lenghtNextP1NextP2 = tdistance(newP.first, newP.second);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// compute parameter texture offset starting from 0.5
Toshihiro Shimizu 890ddd
	double texParameterOffset = lenghtNextP1NextP2 / size.lx * 0.5;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// fix value for s
Toshihiro Shimizu 890ddd
	tex.first.x = 0.5 - texParameterOffset;
Toshihiro Shimizu 890ddd
	tex.second.x = 0.5 + texParameterOffset;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// get length
Toshihiro Shimizu 890ddd
	double disty = norm(v);
Toshihiro Shimizu 890ddd
	assert(tex1.y == tex2.y);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// find parameter for t
Toshihiro Shimizu 890ddd
	texParameterOffset = disty / size.ly;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// fix values for t (oldValue + newValue)
Toshihiro Shimizu 890ddd
	tex.first.y =
Toshihiro Shimizu 890ddd
		tex.second.y = tex1.y + texParameterOffset;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return tex;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void setTexCoords(const TPointD tex, TOutlinePoint &p)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	p.u = tex.x;
Toshihiro Shimizu 890ddd
	p.v = tex.y;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPointD getTexCoords(const TOutlinePoint &p)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return TPointD(p.u, p.v);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
TTextureStyle::TTextureStyle(const TRasterP &ras, const TFilePath &texturePath)
Toshihiro Shimizu 890ddd
	: m_texture(ras), m_texturePath(texturePath), m_tessellator(new TglTessellator), m_params(), m_texturePathLoaded()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setAverageColor();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath TImageStyle::m_libraryDir;
Toshihiro Shimizu 890ddd
ToonzScene *TImageStyle::m_currentScene = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TTextureStyle::TTextureStyle(const TTextureStyle &other)
Toshihiro Shimizu 890ddd
	: TOutlineStyle(other), TRasterStyleFx(other), TImageStyle(other), m_texture(other.m_texture), m_texturePath(other.m_texturePath), m_texturePathLoaded(other.m_texturePathLoaded), m_params(other.m_params), m_tessellator(new TglTessellator)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setAverageColor();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TTextureStyle::~TTextureStyle()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	delete m_tessellator;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TColorStyle *TTextureStyle::clone() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return new TTextureStyle(*this);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString TTextureStyle::getDescription() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return "TextureStyle";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
int TTextureStyle::getTagId() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return 4;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TRasterP TTextureStyle::getTexture() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_texture;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTextureStyle::setAverageColor()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	loadTextureRaster();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRaster32P r32 = (TRaster32P)m_texture;
Toshihiro Shimizu 890ddd
	if (!r32) {
Toshihiro Shimizu 890ddd
		m_averageColor = TPixel::Black;
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (m_params.m_isPattern) {
Toshihiro Shimizu 890ddd
		m_averageColor = m_params.m_patternColor;
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TPixelD color(0, 0, 0, 0);
Toshihiro Shimizu 890ddd
	r32->lock();
Toshihiro Shimizu 890ddd
	for (int i = 0; i < r32->getLy(); i++) {
Toshihiro Shimizu 890ddd
		TPixel32 *buf = r32->pixels(i);
Toshihiro Shimizu 890ddd
		for (int j = 0; j < r32->getLx(); j++, buf++) {
Toshihiro Shimizu 890ddd
			color.r += buf->r;
Toshihiro Shimizu 890ddd
			color.g += buf->g;
Toshihiro Shimizu 890ddd
			color.b += buf->b;
Toshihiro Shimizu 890ddd
			color.m += buf->m;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	r32->unlock();
Toshihiro Shimizu 890ddd
	int pixels = r32->getLx() * r32->getLy();
Toshihiro Shimizu 890ddd
	m_averageColor.r = (int)(color.r / pixels);
Toshihiro Shimizu 890ddd
	m_averageColor.g = (int)(color.g / pixels);
Toshihiro Shimizu 890ddd
	m_averageColor.b = (int)(color.b / pixels);
Toshihiro Shimizu 890ddd
	m_averageColor.m = (int)(color.m / pixels);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTextureStyle::setTexture(const TRasterP &ras)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_texture = ras;
Toshihiro Shimizu 890ddd
	delete m_tessellator;
Toshihiro Shimizu 890ddd
	m_tessellator = new TglTessellator;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	setAverageColor();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
TPixel32 TTextureStyle::getAverageColor() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_averageColor;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTextureStyle::computeOutline(const TStroke *stroke,
Toshihiro Shimizu 890ddd
								   TStrokeOutline &outline,
Toshihiro Shimizu 890ddd
								   TOutlineUtil::OutlineParameter param) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TOutlineStyle::computeOutline(stroke, outline, param);
Shinya Kitaoka 3bfa54
	std::vector<toutlinepoint> &v = outline.getArray();</toutlinepoint>
Toshihiro Shimizu 890ddd
	PointPair newPnt;
Toshihiro Shimizu 890ddd
	TDimension size = m_texture->getSize();
Toshihiro Shimizu 890ddd
	UINT i = 0;
Toshihiro Shimizu 890ddd
	for (i = 2; i < v.size(); i += 2) {
Toshihiro Shimizu 890ddd
		newPnt.first = convert(v[i]);
Toshihiro Shimizu 890ddd
		newPnt.second = convert(v[i + 1]);
Toshihiro Shimizu 890ddd
		newPnt = computeTexParameters(convert(v[i - 2]), convert(v[i - 1]),
Toshihiro Shimizu 890ddd
									  getTexCoords(v[i - 2]), getTexCoords(v[i - 1]),
Toshihiro Shimizu 890ddd
									  newPnt,
Toshihiro Shimizu 890ddd
									  size);
Toshihiro Shimizu 890ddd
		setTexCoords(newPnt.first, v[i]);
Toshihiro Shimizu 890ddd
		setTexCoords(newPnt.second, v[i + 1]);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	for (i = 0; i < v.size(); i++) {
Toshihiro Shimizu 890ddd
		v[i].u = (i & 1) == 0 ? 0 : 1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
void TTextureStyle::drawStroke(const TColorFunction *cf, TStrokeOutline *outline, const TStroke *stroke) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/*struct locals {
Toshihiro Shimizu 890ddd
    static float adaptToGLTextureFunction( USHORT style )
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
      switch( style )
Toshihiro Shimizu 890ddd
      {
Toshihiro Shimizu 890ddd
      case TTextureStyle::DECAL:
Toshihiro Shimizu 890ddd
        return GL_DECAL;
Toshihiro Shimizu 890ddd
        break;
Toshihiro Shimizu 890ddd
      case TTextureStyle::MODULATE:
Toshihiro Shimizu 890ddd
        return GL_MODULATE;
Toshihiro Shimizu 890ddd
        break;
Toshihiro Shimizu 890ddd
      case TTextureStyle::BLEND:
Toshihiro Shimizu 890ddd
        return GL_BLEND;
Toshihiro Shimizu 890ddd
        break;
Toshihiro Shimizu 890ddd
      }
Toshihiro Shimizu 890ddd
      return GL_DECAL;
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
  };    // locals;*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	UINT i;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	std::vector<toutlinepoint> &v = outline->getArray();</toutlinepoint>
Toshihiro Shimizu 890ddd
	if (v.empty())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const TRasterP texture = m_texture;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(!v.empty() || !texture);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (v.empty() || !texture)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	static const int stride = sizeof(TOutlinePoint);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	glColor4d(1.0, 1.0, 1.0, 1.0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// information about vertex
Toshihiro Shimizu 890ddd
	glEnableClientState(GL_VERTEX_ARRAY);
Toshihiro Shimizu 890ddd
	glVertexPointer(2, GL_DOUBLE, stride, &v[0]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	glEnable(GL_TEXTURE_2D);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef _DEBUG
Toshihiro Shimizu 890ddd
	GLboolean blendEnabled;
Toshihiro Shimizu 890ddd
	glGetBooleanv(GL_BLEND, &blendEnabled);
Toshihiro Shimizu 890ddd
	assert(blendEnabled);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Toshihiro Shimizu 890ddd
	glTexCoordPointer(2, GL_DOUBLE, stride, &(v[0].u));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TextureInfoForGL texInfo;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_texture->lock();
Toshihiro Shimizu 890ddd
	TRasterP texImage = prepareTexture(m_texture, texInfo);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Generate a texture id and bind it.
Toshihiro Shimizu 890ddd
	GLuint texId;
Toshihiro Shimizu 890ddd
	glGenTextures(1, &texId);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	glBindTexture(GL_TEXTURE_2D, texId);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Specify texture parameters
Toshihiro Shimizu 890ddd
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
Toshihiro Shimizu 890ddd
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Toshihiro Shimizu 890ddd
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Toshihiro Shimizu 890ddd
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	glTexEnvf(GL_TEXTURE_ENV,
Toshihiro Shimizu 890ddd
			  GL_TEXTURE_ENV_MODE,
Toshihiro Shimizu 890ddd
			  GL_MODULATE); // change texture blending function
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	glPixelStorei(GL_UNPACK_ROW_LENGTH,
Toshihiro Shimizu 890ddd
				  texImage->getWrap() != texImage->getLx() ? texImage->getWrap() : 0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (texImage != m_texture)
Toshihiro Shimizu 890ddd
		texImage->lock();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Load texture
Toshihiro Shimizu 890ddd
	glTexImage2D(GL_TEXTURE_2D,
Toshihiro Shimizu 890ddd
				 0,						 // one level only
Toshihiro Shimizu 890ddd
				 texInfo.internalformat, // pixel channels count
Toshihiro Shimizu 890ddd
				 texInfo.width,			 // width
Toshihiro Shimizu 890ddd
				 texInfo.height,		 // height
Toshihiro Shimizu 890ddd
				 0,						 // border size
Toshihiro Shimizu 890ddd
				 texInfo.type,			 // pixel format           // crappy names
Toshihiro Shimizu 890ddd
				 texInfo.format,		 // pixel data type        // oh, SO much
Toshihiro Shimizu 890ddd
				 texImage->getRawData());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_texture->unlock();
Toshihiro Shimizu 890ddd
	if (texImage != m_texture)
Toshihiro Shimizu 890ddd
		texImage->lock();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Step 1: draw outline
Toshihiro Shimizu 890ddd
	glBegin(GL_LINE_STRIP);
Toshihiro Shimizu 890ddd
	for (i = 0; i < v.size(); i += 2)
Toshihiro Shimizu 890ddd
		glArrayElement(i);
Toshihiro Shimizu 890ddd
	glEnd();
Toshihiro Shimizu 890ddd
	glBegin(GL_LINE_STRIP);
Toshihiro Shimizu 890ddd
	for (i = 1; i < v.size(); i += 2)
Toshihiro Shimizu 890ddd
		glArrayElement(i);
Toshihiro Shimizu 890ddd
	glEnd();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Step 2: draw texturized stroke
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
  #ifdef MACOSX                   //NON SI CAPISCE PERCHE DRAWARRAY E DISPLAY LIST PROVOCHINO UN CRASH SU MAC
Toshihiro Shimizu 890ddd
  #warning "INDAGARE ANCORA!!!!"  //PER ADESSO CHIEDO SE STA REGISTANDO UNA DISPLAYLIST E IN QUEL CASO NON USO GLI ARRAY 
Toshihiro Shimizu 890ddd
  GLuint listId;
Toshihiro Shimizu 890ddd
  glGetIntegerv(GL_LIST_INDEX,(GLint*)&listId);   
Toshihiro Shimizu 890ddd
  if(listId==0)
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
  #endif  
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
	glDrawArrays(QUAD_PRIMITIVE, 0, v.size());
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
  #ifdef MACOSX      
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
  else
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
    glBegin( GL_QUAD_STRIP );
Toshihiro Shimizu 890ddd
    for(UINT i=0; i< v.size(); i++) 
Toshihiro Shimizu 890ddd
      glVertex2dv( &v[i].x );
Toshihiro Shimizu 890ddd
    glEnd();
Toshihiro Shimizu 890ddd
  } 
Toshihiro Shimizu 890ddd
  #endif
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Delete texture
Toshihiro Shimizu 890ddd
	glDeleteTextures(1, &texId);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	glDisableClientState(GL_VERTEX_ARRAY);
Toshihiro Shimizu 890ddd
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Toshihiro Shimizu 890ddd
	glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
Toshihiro Shimizu 890ddd
	glDisable(GL_TEXTURE_2D);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//drawRegion( const TVectorRenderData &rd,  TRegionOutline &boundary ) const
Toshihiro Shimizu 890ddd
void TTextureStyle::drawRegion(const TColorFunction *cf, const bool antiAliasing, TRegionOutline &boundary) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_tessellator)
Toshihiro Shimizu 890ddd
		m_tessellator->tessellate(cf, antiAliasing, boundary, m_texture);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
void TTextureStyle::drawRegion(TFlash &flash, const TRegion *r) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	flash.setTexture(m_texture);
Toshihiro Shimizu 890ddd
	flash.setFillStyleMatrix(TAffine());
Toshihiro Shimizu 890ddd
	flash.drawRegion(*r);
Toshihiro Shimizu 890ddd
	//rd.setTexture(m_colorStyle->getMainColor());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTextureStyle::setFill(TFlash &flash) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	flash.setTexture(getTexture());
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 tileRaster(const TRaster32P &tile, const TRaster32P &rout)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int x0, y0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (rout->getLy() > tile->getLy()) ///tile must be centered in rout
Toshihiro Shimizu 890ddd
		y0 = tile->getLy() - (((rout->getLy() - tile->getLy()) / 2) % tile->getLy());
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		y0 = (tile->getLy() - rout->getLy()) / 2;
Toshihiro Shimizu 890ddd
	if (rout->getLx() > tile->getLx()) ///tile must be centered in rout
Toshihiro Shimizu 890ddd
		x0 = tile->getLx() - (((rout->getLx() - tile->getLx()) / 2) % tile->getLx());
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		x0 = (tile->getLx() - rout->getLx()) / 2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//x0-=tround(offs.x);
Toshihiro Shimizu 890ddd
	//y0-=tround(offs.y);
Toshihiro Shimizu 890ddd
	while (x0 < 0)
Toshihiro Shimizu 890ddd
		x0 += tile->getLx();
Toshihiro Shimizu 890ddd
	while (y0 < 0)
Toshihiro Shimizu 890ddd
		y0 += tile->getLy();
Toshihiro Shimizu 890ddd
	x0 = x0 % tile->getLx();
Toshihiro Shimizu 890ddd
	y0 = y0 % tile->getLy();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int y = y0;
Toshihiro Shimizu 890ddd
	for (int i = 0; i < rout->getLy(); i++, y++) {
Toshihiro Shimizu 890ddd
		if (y == tile->getLy())
Toshihiro Shimizu 890ddd
			y = 0;
Toshihiro Shimizu 890ddd
		int x = x0;
Toshihiro Shimizu 890ddd
		TPixel32 *pixout = rout->pixels(i);
Toshihiro Shimizu 890ddd
		TPixel32 *pixin = tile->pixels(y) + x;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (int j = 0; j < rout->getLx(); j++, pixin++, pixout++, x++) {
Toshihiro Shimizu 890ddd
			if (x == tile->getLx()) {
Toshihiro Shimizu 890ddd
				x = 0, pixin = tile->pixels(y);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			*pixout = *pixin;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void doContrast(double contrast, const TRaster32P &rin)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPixel32 *row, *pix;
Toshihiro Shimizu 890ddd
	int i, j, lx, ly, wrap;
Toshihiro Shimizu 890ddd
	double line_avg_r, line_avg_g, line_avg_b;
Toshihiro Shimizu 890ddd
	double avg_r = 0, avg_g = 0, avg_b = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	lx = rin->getLx();
Toshihiro Shimizu 890ddd
	ly = rin->getLy();
Toshihiro Shimizu 890ddd
	wrap = rin->getWrap();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (j = 0, row = (TPixel32 *)rin->getRawData(); j < ly; j++, row += wrap) {
Toshihiro Shimizu 890ddd
		line_avg_r = 0;
Toshihiro Shimizu 890ddd
		line_avg_g = 0;
Toshihiro Shimizu 890ddd
		line_avg_b = 0;
Toshihiro Shimizu 890ddd
		for (i = 0, pix = row; i < lx; i++, pix++) {
Toshihiro Shimizu 890ddd
			line_avg_r += pix->r;
Toshihiro Shimizu 890ddd
			line_avg_g += pix->g;
Toshihiro Shimizu 890ddd
			line_avg_b += pix->b;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		avg_r += line_avg_r / lx;
Toshihiro Shimizu 890ddd
		avg_g += line_avg_g / lx;
Toshihiro Shimizu 890ddd
		avg_b += line_avg_b / lx;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	avg_r /= ly;
Toshihiro Shimizu 890ddd
	avg_g /= ly;
Toshihiro Shimizu 890ddd
	avg_b /= ly;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int ires;
Toshihiro Shimizu 890ddd
	double dres;
Toshihiro Shimizu 890ddd
	TPixel32 lut[256];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < 256; i++) {
Toshihiro Shimizu 890ddd
		dres = contrast * (i - avg_r) + avg_r;
Toshihiro Shimizu 890ddd
		ires = tround(dres);
Toshihiro Shimizu 890ddd
		lut[i].r = tcrop(ires, 0, 255);
Toshihiro Shimizu 890ddd
		dres = contrast * (i - avg_g) + avg_g;
Toshihiro Shimizu 890ddd
		ires = tround(dres);
Toshihiro Shimizu 890ddd
		lut[i].g = tcrop(ires, 0, 255);
Toshihiro Shimizu 890ddd
		dres = contrast * (i - avg_b) + avg_b;
Toshihiro Shimizu 890ddd
		ires = tround(dres);
Toshihiro Shimizu 890ddd
		lut[i].b = tcrop(ires, 0, 255);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (j = 0, row = (TPixel32 *)rin->getRawData(); j < ly; j++, row += wrap) {
Toshihiro Shimizu 890ddd
		for (i = 0, pix = row; i < lx; i++, pix++) {
Toshihiro Shimizu 890ddd
			pix->r = lut[pix->r].r;
Toshihiro Shimizu 890ddd
			pix->g = lut[pix->g].g;
Toshihiro Shimizu 890ddd
			pix->b = lut[pix->b].b;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (pix->r > pix->m)
Toshihiro Shimizu 890ddd
				pix->r = pix->m;
Toshihiro Shimizu 890ddd
			if (pix->g > pix->m)
Toshihiro Shimizu 890ddd
				pix->g = pix->m;
Toshihiro Shimizu 890ddd
			if (pix->b > pix->m)
Toshihiro Shimizu 890ddd
				pix->b = pix->m;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define BYTE_ROUND(A) ((UINT)((A) + (1U << 23)) >> 24)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define MAGICFAC (257U * 256U + 1U)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static void doPattern(const TRaster32P &r, TPixel32 color)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i, j, fac = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (TPixelGR8::from(color).value < 128)
Toshihiro Shimizu 890ddd
		fac = 255 - color.m * (255.0 - color.m) / 255.0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPixel32 *buf;
Toshihiro Shimizu 890ddd
	UINT b;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < r->getLy(); i++) {
Toshihiro Shimizu 890ddd
		buf = (TPixel32 *)r->pixels(i);
Toshihiro Shimizu 890ddd
		for (j = 0; j < r->getLx(); j++, buf++) {
Toshihiro Shimizu 890ddd
			if (buf->m > 0) {
Toshihiro Shimizu 890ddd
				b = TPixelGR8::from(*buf).value * MAGICFAC;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				buf->r = BYTE_ROUND(b * color.r + fac);
Toshihiro Shimizu 890ddd
				buf->g = BYTE_ROUND(b * color.g + fac);
Toshihiro Shimizu 890ddd
				buf->b = BYTE_ROUND(b * color.b + fac);
Toshihiro Shimizu 890ddd
				if (buf->m < 255 || color.m < 255) {
Toshihiro Shimizu 890ddd
					buf->m = BYTE_ROUND(buf->m * color.m / 255.0);
Toshihiro Shimizu 890ddd
					premult(*buf);
Toshihiro Shimizu 890ddd
				} else
Toshihiro Shimizu 890ddd
					buf->m = 255;
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 applyTexture(const TRaster32P &rTex, const TRaster32P &r, TPoint p)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	while (p.x < 0)
Toshihiro Shimizu 890ddd
		p.x += rTex->getLx();
Toshihiro Shimizu 890ddd
	while (p.y < 0)
Toshihiro Shimizu 890ddd
		p.y += rTex->getLy();
Toshihiro Shimizu 890ddd
	r->lock();
Toshihiro Shimizu 890ddd
	rTex->lock();
Toshihiro Shimizu 890ddd
	for (int y = 0, yTex = p.y; y < r->getLy(); y++, yTex++) {
Toshihiro Shimizu 890ddd
		TPixel32 *pixIn = r->pixels(y);
Toshihiro Shimizu 890ddd
		if (yTex >= rTex->getLy())
Toshihiro Shimizu 890ddd
			yTex -= rTex->getLy();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TPixel32 *pixTex = (TPixel32 *)(rTex->pixels(yTex)) + p.x;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (int x = 0, xTex = p.x; x < r->getLx(); x++, xTex++, pixIn++, pixTex++) {
Toshihiro Shimizu 890ddd
			if (xTex >= rTex->getLx())
Toshihiro Shimizu 890ddd
				xTex -= rTex->getLx(), pixTex -= rTex->getLx();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (pixIn->m == 0)
Toshihiro Shimizu 890ddd
				continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (pixIn->m == 255)
Toshihiro Shimizu 890ddd
				*pixIn = *pixTex;
Toshihiro Shimizu 890ddd
			else {
Toshihiro Shimizu 890ddd
				pixIn->r = pixIn->m * pixTex->r / 255;
Toshihiro Shimizu 890ddd
				pixIn->g = pixIn->m * pixTex->g / 255;
Toshihiro Shimizu 890ddd
				pixIn->b = pixIn->m * pixTex->b / 255;
Toshihiro Shimizu 890ddd
				pixIn->m = pixIn->m * pixTex->m / 255;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	r->unlock();
Toshihiro Shimizu 890ddd
	rTex->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPoint computeCentroid(const TRaster32P &r);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TTextureStyle::getParamCount() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return 8;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString TTextureStyle::getParamNames(int index) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	switch (index) {
Shinya Kitaoka d4642c
	case 0: return "Use As Pattern";
Shinya Kitaoka d4642c
	case 1: return "Position";
Shinya Kitaoka d4642c
	case 2: return "Scale";
Shinya Kitaoka d4642c
	case 3: return "Rotation(degrees)";
Shinya Kitaoka d4642c
	case 4: return "X displ";
Shinya Kitaoka d4642c
	case 5: return "Y displ";
Shinya Kitaoka d4642c
	case 6: return "Contrast";
Shinya Kitaoka d4642c
	case 7: return "Load From File";
Shinya Kitaoka d4642c
	default:
Toshihiro Shimizu 890ddd
		assert(false);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return QString("");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTextureStyle::getParamRange(int index, double &min, double &max) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(index > 1);
Toshihiro Shimizu 890ddd
	switch (index) {
Toshihiro Shimizu 890ddd
	case 2:
Toshihiro Shimizu 890ddd
		min = 0.15;
Toshihiro Shimizu 890ddd
		max = 10;
Shinya Kitaoka d4642c
		break;
Shinya Kitaoka d4642c
	case 3:
Shinya Kitaoka d4642c
		min = -180;
Toshihiro Shimizu 890ddd
		max = 180;
Shinya Kitaoka d4642c
		break;
Shinya Kitaoka d4642c
	case 4:
Shinya Kitaoka d4642c
		min = -500;
Toshihiro Shimizu 890ddd
		max = 500;
Shinya Kitaoka d4642c
		break;
Shinya Kitaoka d4642c
	case 5:
Shinya Kitaoka d4642c
		min = -500;
Toshihiro Shimizu 890ddd
		max = 500;
Shinya Kitaoka d4642c
		break;
Shinya Kitaoka d4642c
	case 6:
Shinya Kitaoka d4642c
		min = 0.01;
Toshihiro Shimizu 890ddd
		max = 10;
Shinya Kitaoka d4642c
		break;
Shinya Kitaoka d4642c
	default:
Toshihiro Shimizu 890ddd
		assert(false);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TColorStyle::ParamType TTextureStyle::getParamType(int index) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(0 <= index && index < getParamCount());
Toshihiro Shimizu 890ddd
	if (index == 0)
Toshihiro Shimizu 890ddd
		return TColorStyle::BOOL;
Toshihiro Shimizu 890ddd
	else if (index == 1)
Toshihiro Shimizu 890ddd
		return TColorStyle::ENUM;
Toshihiro Shimizu 890ddd
	else if (index == 7)
Toshihiro Shimizu 890ddd
		return TColorStyle::FILEPATH;
Toshihiro Shimizu 890ddd
	return TColorStyle::DOUBLE;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTextureStyle::getParamRange(int index, QStringList &enumItems) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(index == 1 || index == 7);
Toshihiro Shimizu 890ddd
	if (index == 1)
Toshihiro Shimizu 890ddd
		enumItems << "FIXED"
Toshihiro Shimizu 890ddd
				  << "AUTOMATIC"
Toshihiro Shimizu 890ddd
				  << "RANDOM";
Toshihiro Shimizu 890ddd
	else if (index == 7)
Toshihiro Shimizu 890ddd
		enumItems << "bmp"
Toshihiro Shimizu 890ddd
				  << "jpg"
Toshihiro Shimizu 890ddd
				  << "png"
Toshihiro Shimizu 890ddd
				  << "tif"
Toshihiro Shimizu 890ddd
				  << "tiff"
Toshihiro Shimizu 890ddd
				  << "gif";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TTextureStyle::getParamValue(TColorStyle::bool_tag, int index) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(index == 0);
Toshihiro Shimizu 890ddd
	return m_params.m_isPattern;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double TTextureStyle::getParamValue(TColorStyle::double_tag, int index) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(index > 1);
Toshihiro Shimizu 890ddd
	switch (index) {
Shinya Kitaoka d4642c
	case 2: return m_params.m_scale;
Shinya Kitaoka d4642c
	case 3: return m_params.m_rotation;
Shinya Kitaoka d4642c
	case 4: return m_params.m_displacement.x;
Shinya Kitaoka d4642c
	case 5: return m_params.m_displacement.y;
Shinya Kitaoka d4642c
	case 6: return m_params.m_contrast;
Shinya Kitaoka d4642c
	default:
Toshihiro Shimizu 890ddd
		assert(false);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath TTextureStyle::getParamValue(TColorStyle::TFilePath_tag, int index) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(index == 7);
Toshihiro Shimizu 890ddd
	return m_texturePath;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TTextureStyle::getParamValue(TColorStyle::int_tag, int index) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(index == 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return m_params.m_type;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTextureStyle::setParamValue(int index, const TFilePath &value)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(index == 7);
Toshihiro Shimizu 890ddd
	m_texturePath = value;
Toshihiro Shimizu 890ddd
	setAverageColor();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTextureStyle::setParamValue(int index, int value)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(index == 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_params.m_type = (TTextureParams::TYPE)value;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTextureStyle::setParamValue(int index, bool value)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(index == 0);
Toshihiro Shimizu 890ddd
	m_params.m_isPattern = value;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTextureStyle::setParamValue(int index, double value)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(index > 1);
Toshihiro Shimizu 890ddd
	switch (index) {
Toshihiro Shimizu 890ddd
	case 0:
Toshihiro Shimizu 890ddd
		m_params.m_isPattern = (((int)value == 0) ? false : true);
Shinya Kitaoka d4642c
		break;
Shinya Kitaoka d4642c
	case 1:
Shinya Kitaoka d4642c
		m_params.m_type = (((int)value == 0) ? TTextureParams::FIXED : ((int)value == 1) ? TTextureParams::AUTOMATIC : TTextureParams::RANDOM);
Shinya Kitaoka d4642c
		break;
Shinya Kitaoka d4642c
	case 2:
Shinya Kitaoka d4642c
		m_params.m_scale = value;
Shinya Kitaoka d4642c
		break;
Shinya Kitaoka d4642c
	case 3:
Shinya Kitaoka d4642c
		m_params.m_rotation = value;
Shinya Kitaoka d4642c
		break;
Shinya Kitaoka d4642c
	case 4:
Shinya Kitaoka d4642c
		m_params.m_displacement.x = value;
Shinya Kitaoka d4642c
		break;
Shinya Kitaoka d4642c
	case 5:
Shinya Kitaoka d4642c
		m_params.m_displacement.y = value;
Shinya Kitaoka d4642c
		break;
Shinya Kitaoka d4642c
	case 6:
Shinya Kitaoka d4642c
		m_params.m_contrast = value;
Shinya Kitaoka d4642c
		break;
Shinya Kitaoka d4642c
	default:
Toshihiro Shimizu 890ddd
		assert(false);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
bool TTextureStyle::loadTextureRaster()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_texturePathLoaded != TFilePath() && m_texturePath == m_texturePathLoaded)
Toshihiro Shimizu 890ddd
		return true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_texturePathLoaded = m_texturePath;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath path;
Toshihiro Shimizu 890ddd
	if (m_texturePath.getParentDir() != TFilePath()) //It's a custom texture
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		assert(m_currentScene);
Toshihiro Shimizu 890ddd
		path = m_currentScene->decodeFilePath(m_texturePath);
Toshihiro Shimizu 890ddd
		if (path.isLevelName()) {
Toshihiro Shimizu 890ddd
			TLevelReader lr(path);
Toshihiro Shimizu 890ddd
			path = path.withFrame(lr.loadInfo()->begin()->first);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else //is a library texture
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		path = m_texturePath.withParentDir(m_libraryDir + "textures");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterP aux;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!TImageReader::load(path, aux)) {
Toshihiro Shimizu 890ddd
		m_texture = TRaster32P(128, 128);
Toshihiro Shimizu 890ddd
		m_texture->clear();
Toshihiro Shimizu 890ddd
		m_texturePathLoaded = TFilePath();
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_texture = aux;
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
TRaster32P TTextureStyle::loadTextureRasterWithFrame(int frame) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_texturePathLoaded != TFilePath() && m_texturePath == m_texturePathLoaded && (!m_texturePath.isLevelName() || frame == 0))
Toshihiro Shimizu 890ddd
		return m_texture->clone();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath path;
Toshihiro Shimizu 890ddd
	if (m_texturePath.getParentDir() != TFilePath()) //It's a custom texture
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		assert(m_currentScene);
Toshihiro Shimizu 890ddd
		path = m_currentScene->decodeFilePath(m_texturePath);
Toshihiro Shimizu 890ddd
		if (path.isLevelName()) {
Toshihiro Shimizu 890ddd
			TLevelReader lr(path);
Toshihiro Shimizu 890ddd
			TLevelP info = lr.loadInfo();
Toshihiro Shimizu 890ddd
			TLevel::Iterator it = info->begin();
Toshihiro Shimizu 890ddd
			//frame = frame % (lr.loadInfo()->getFrameCount());
Toshihiro Shimizu 890ddd
			std::advance(it, frame % (info->getFrameCount()));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			//
Toshihiro Shimizu 890ddd
			path = path.withFrame(it->first);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else //is a library texture
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		path = m_texturePath.withParentDir(m_libraryDir + "textures");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterP aux;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!TImageReader::load(path, aux)) {
Toshihiro Shimizu 890ddd
		aux = TRaster32P(128, 128);
Toshihiro Shimizu 890ddd
		aux->clear();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return aux;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTextureStyle::loadData(TInputStreamInterface &is)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (is.versionNumber().first < 71) {
Toshihiro Shimizu 890ddd
		is >> m_texture;
Toshihiro Shimizu 890ddd
		setTexture(m_texture);
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	std::string path;
Toshihiro Shimizu 890ddd
	is >> path;
Toshihiro Shimizu 890ddd
	m_texturePath = TFilePath(path);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//TOutlineStyle::loadData(is);
Toshihiro Shimizu 890ddd
	//is >> m_texture;
Toshihiro Shimizu 890ddd
	loadTextureRaster();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	is >> m_params.m_patternColor;
Toshihiro Shimizu 890ddd
	m_averageColor = m_params.m_patternColor;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double value;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	is >> value;
Toshihiro Shimizu 890ddd
	m_params.m_isPattern = value == 1.0 ? true : false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	is >> value;
Toshihiro Shimizu 890ddd
	m_params.m_type = (((int)value == 0) ? TTextureParams::FIXED : ((int)value == 1) ? TTextureParams::AUTOMATIC : TTextureParams::RANDOM);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	is >> m_params.m_scale;
Toshihiro Shimizu 890ddd
	is >> m_params.m_rotation;
Toshihiro Shimizu 890ddd
	is >> m_params.m_displacement.x;
Toshihiro Shimizu 890ddd
	is >> m_params.m_displacement.y;
Toshihiro Shimizu 890ddd
	is >> m_params.m_contrast;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	delete m_tessellator;
Toshihiro Shimizu 890ddd
	m_tessellator = new TglTessellator;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	setAverageColor();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTextureStyle::saveData(TOutputStreamInterface &os) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//TOutlineStyle::saveData(os);
Toshihiro Shimizu 890ddd
	//os << m_texture;
Shinya Kitaoka 3bfa54
	std::wstring wstr = m_texturePath.getWideString();
Shinya Kitaoka 3bfa54
	std::string str;
Toshihiro Shimizu 890ddd
	str.assign(wstr.begin(), wstr.end());
Toshihiro Shimizu 890ddd
	os << str;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	os << m_params.m_patternColor;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	os << ((double)m_params.m_isPattern);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double value = (m_params.m_type == TTextureParams::FIXED) ? 0 : ((m_params.m_type == TTextureParams::AUTOMATIC) ? 1 : 2);
Toshihiro Shimizu 890ddd
	os << value;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	os << m_params.m_scale;
Toshihiro Shimizu 890ddd
	os << m_params.m_rotation;
Toshihiro Shimizu 890ddd
	os << m_params.m_displacement.x;
Toshihiro Shimizu 890ddd
	os << m_params.m_displacement.y;
Toshihiro Shimizu 890ddd
	os << m_params.m_contrast;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TTextureStyle::getColorParamCount() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPixel32 TTextureStyle::getColorParamValue(int index) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_params.m_patternColor;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTextureStyle::setColorParamValue(int index, const TPixel32 &color)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_params.m_patternColor = color;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
inline void drawdot(TPixel32 *pix, int wrap)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (int i = -wrap; i <= wrap; i += wrap)
Toshihiro Shimizu 890ddd
		for (int j = -1; j <= 1; j++)
Toshihiro Shimizu 890ddd
			*(pix + i + j) = TPixel::Red;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTextureStyle::fillCustomTextureIcon(const TRaster32P &r)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	r->fill(TPixel::White);
Toshihiro Shimizu 890ddd
	int x1, x2, x3;
Toshihiro Shimizu 890ddd
	x2 = r->getLx() / 2;
Toshihiro Shimizu 890ddd
	x1 = x2 + ((r->getLx() <= 64) ? 6 : 9);
Toshihiro Shimizu 890ddd
	x3 = x2 - ((r->getLx() <= 64) ? 6 : 9);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPixel32 *pix = r->pixels(r->getLy() / 4);
Toshihiro Shimizu 890ddd
	drawdot(pix + x1, r->getWrap());
Toshihiro Shimizu 890ddd
	drawdot(pix + x2, r->getWrap());
Toshihiro Shimizu 890ddd
	drawdot(pix + x3, r->getWrap());
Toshihiro Shimizu 890ddd
	// TImageWriter::save(TFilePath("C:\\temp\\boh.png"), r);
Toshihiro Shimizu 890ddd
	/**(pix+x1) = TPixel::Black;
Toshihiro Shimizu 890ddd
*(pix+x2) = TPixel::Black;
Toshihiro Shimizu 890ddd
*(pix+x3) = TPixel::Black;*/
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTextureStyle::makeIcon(const TDimension &outputRect)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!m_icon || m_icon->getSize() != outputRect) {
Toshihiro Shimizu 890ddd
		TRaster32P ras(outputRect);
Toshihiro Shimizu 890ddd
		ras->fill(TPixel::Red);
Toshihiro Shimizu 890ddd
		m_icon = ras;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!loadTextureRaster()) {
Toshihiro Shimizu 890ddd
		fillCustomTextureIcon(m_icon);
Toshihiro Shimizu 890ddd
		//m_icon->fill(TPixel::Green);
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TRaster32P rTex;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_params.m_contrast != 1.0 || m_params.m_isPattern) {
Toshihiro Shimizu 890ddd
		rTex = m_texture->clone();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (m_params.m_contrast != 1.0)
Toshihiro Shimizu 890ddd
			doContrast(m_params.m_contrast, rTex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (m_params.m_isPattern)
Toshihiro Shimizu 890ddd
			doPattern(rTex, m_params.m_patternColor);
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		rTex = m_texture;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 12c444
	double fitScale = std::min((double)(outputRect.lx) / m_texture->getLx(), (double)(outputRect.ly) / m_texture->getLy());
Toshihiro Shimizu 890ddd
	TAffine affine = TScale(m_params.m_scale * (fitScale)) * TRotation(-m_params.m_rotation);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (affine != TAffine()) {
Toshihiro Shimizu 890ddd
		int border = 2;
Toshihiro Shimizu 890ddd
		TRaster32P raux(m_icon->getLx() + 2 * border, m_icon->getLy() + 2 * border);
Toshihiro Shimizu 890ddd
		TRaster32P rin(convert(affine.inv() * TRectD(0, 0, raux->getLx() - 1, raux->getLy() - 1)).getSize());
Toshihiro Shimizu 890ddd
		tileRaster(rTex, rin);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TRop::resample(raux, rin, affine.place(rin->getCenterD(), raux->getCenterD()));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TRop::copy(m_icon, raux->extract(border, border, m_icon->getLx() + border - 1, m_icon->getLy() + border - 1));
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		applyTexture(rTex, m_icon, TPoint());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TTextureStyle::doCompute(const Params ¶ms) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P rTex = loadTextureRasterWithFrame(params.m_frame);
Toshihiro Shimizu 890ddd
	TRaster32P r = params.m_r;
Toshihiro Shimizu 890ddd
	//TRaster32P rTex = m_texture->clone();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(r);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_params.m_contrast != 1.0)
Toshihiro Shimizu 890ddd
		doContrast(m_params.m_contrast, rTex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_params.m_isPattern)
Toshihiro Shimizu 890ddd
		doPattern(rTex, m_params.m_patternColor);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPoint p = -convert(m_params.m_displacement);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_params.m_type == TTextureParams::FIXED)
Toshihiro Shimizu 890ddd
		p += params.m_p;
Toshihiro Shimizu 890ddd
	else if (m_params.m_type == TTextureParams::RANDOM)
Toshihiro Shimizu 890ddd
		p += TPoint(Random.getUInt(m_texture->getLx()), Random.getUInt(m_texture->getLy()));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_params.m_scale != 1.0 || m_params.m_rotation != 0.0) {
Toshihiro Shimizu 890ddd
		TAffine affine = TScale(m_params.m_scale) * TRotation(-m_params.m_rotation);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (m_params.m_type == TTextureParams::AUTOMATIC) {
Toshihiro Shimizu 890ddd
			TRect bBox;
Toshihiro Shimizu 890ddd
			TRop::computeBBox(r, bBox);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			r = r->extract(bBox);
Toshihiro Shimizu 890ddd
			p += r->getCenter() - computeCentroid(r);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		p.x = (p.x % m_texture->getLx());
Toshihiro Shimizu 890ddd
		p.y = (p.y % m_texture->getLy());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TRect rectIn = convert(affine.inv() * TRectD(0, 0, r->getLx() - 1, r->getLy() - 1));
Toshihiro Shimizu 890ddd
		TRaster32P rin(rectIn.getLx() + abs(p.x), rectIn.getLy() + abs(p.y));
Toshihiro Shimizu 890ddd
		TRaster32P rout(r->getSize());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		tileRaster(rTex, rin);
Toshihiro Shimizu 890ddd
		TRop::resample(rout, rin, affine.place(rin->getCenterD(), rout->getCenterD() - convert(p)));
Toshihiro Shimizu 890ddd
		rTex = rout;
Toshihiro Shimizu 890ddd
		p = TPoint();
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		if (m_params.m_type == TTextureParams::AUTOMATIC)
Toshihiro Shimizu 890ddd
			p += rTex->getCenter() - computeCentroid(r);
Toshihiro Shimizu 890ddd
		p.x = (p.x % m_texture->getLx());
Toshihiro Shimizu 890ddd
		p.y = (p.y % m_texture->getLy());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	applyTexture(rTex, r, p);
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
TRaster32P makeSimpleRaster()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRaster32P ras(2, 2);
Toshihiro Shimizu 890ddd
	ras->fill(TPixel32::White);
Toshihiro Shimizu 890ddd
	return ras;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TColorStyle::Declaration s2(new TTextureStyle(makeSimpleRaster(), TFilePath()));
Toshihiro Shimizu 890ddd
}