Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/ttilesaver.h"
Toshihiro Shimizu 890ddd
#include "toonz/ttileset.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define TILE_BIT_SIZE 6
Toshihiro Shimizu 890ddd
#define TILE_SIZE_MINUS_1 63
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//divisione per tileSize approssimata per eccesso
Toshihiro Shimizu 890ddd
inline int divSup(int x)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	// return (x & TILE_SIZE_MINUS_1)? (x>>TILE_BIT_SIZE)+1 : (x>>TILE_BIT_SIZE);
Toshihiro Shimizu 890ddd
	return ((x + TILE_SIZE_MINUS_1) >> TILE_BIT_SIZE);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TTileSaverCM32::TTileSaverCM32(const TRasterCM32P &raster, TTileSetCM32 *tileSet)
Toshihiro Shimizu 890ddd
	: m_raster(raster), m_tileSet(tileSet), m_rowSize(divSup(raster->getLx())), m_savedTiles(m_rowSize * divSup(raster->getLy()), 0)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTileSaverCM32::saveTile(int row, int col)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT index = m_rowSize * row + col;
Toshihiro Shimizu 890ddd
	assert(index < m_savedTiles.size());
Toshihiro Shimizu 890ddd
	if (m_savedTiles[index])
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	m_savedTiles[index] = 1;
Toshihiro Shimizu 890ddd
	int x = col << TILE_BIT_SIZE;
Toshihiro Shimizu 890ddd
	int y = row << TILE_BIT_SIZE;
Toshihiro Shimizu 890ddd
	TRect tileBounds(x, y, x + TILE_SIZE_MINUS_1, y + TILE_SIZE_MINUS_1);
Toshihiro Shimizu 890ddd
	m_tileSet->add(m_raster, tileBounds);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTileSaverCM32::save(TRect rect)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_raster->getBounds().overlaps(rect))
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	rect = rect * m_raster->getBounds();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPoint p0 = rect.getP00();
Toshihiro Shimizu 890ddd
	TPoint p1 = rect.getP11();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int col0 = p0.x >> TILE_BIT_SIZE;
Toshihiro Shimizu 890ddd
	int row0 = p0.y >> TILE_BIT_SIZE;
Toshihiro Shimizu 890ddd
	int col1 = p1.x >> TILE_BIT_SIZE;
Toshihiro Shimizu 890ddd
	int row1 = p1.y >> TILE_BIT_SIZE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int row = row0; row <= row1; ++row)
Toshihiro Shimizu 890ddd
		for (int col = col0; col <= col1; ++col)
Toshihiro Shimizu 890ddd
			saveTile(row, col);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTileSaverCM32::save(TPoint point)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_raster->getBounds().contains(point))
Toshihiro Shimizu 890ddd
		saveTile(point.y >> TILE_BIT_SIZE, point.x >> TILE_BIT_SIZE);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TTileSetCM32 *TTileSaverCM32::getTileSet() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_tileSet;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TTileSaverFullColor::TTileSaverFullColor(const TRasterP &raster, TTileSetFullColor *tileSet)
Toshihiro Shimizu 890ddd
	: m_raster(raster), m_tileSet(tileSet), m_rowSize(divSup(raster->getLx())), m_savedTiles(m_rowSize * divSup(raster->getLy()), 0)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTileSaverFullColor::saveTile(int row, int col)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UINT index = m_rowSize * row + col;
Toshihiro Shimizu 890ddd
	assert(index < m_savedTiles.size());
Toshihiro Shimizu 890ddd
	if (m_savedTiles[index])
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	m_savedTiles[index] = 1;
Toshihiro Shimizu 890ddd
	int x = col << TILE_BIT_SIZE;
Toshihiro Shimizu 890ddd
	int y = row << TILE_BIT_SIZE;
Toshihiro Shimizu 890ddd
	TRect tileBounds(x, y, x + TILE_SIZE_MINUS_1, y + TILE_SIZE_MINUS_1);
Toshihiro Shimizu 890ddd
	m_tileSet->add(m_raster, tileBounds);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTileSaverFullColor::save(TRect rect)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_raster->getBounds().overlaps(rect))
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	rect = rect * m_raster->getBounds();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPoint p0 = rect.getP00();
Toshihiro Shimizu 890ddd
	TPoint p1 = rect.getP11();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int col0 = p0.x >> TILE_BIT_SIZE;
Toshihiro Shimizu 890ddd
	int row0 = p0.y >> TILE_BIT_SIZE;
Toshihiro Shimizu 890ddd
	int col1 = p1.x >> TILE_BIT_SIZE;
Toshihiro Shimizu 890ddd
	int row1 = p1.y >> TILE_BIT_SIZE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int row = row0; row <= row1; ++row)
Toshihiro Shimizu 890ddd
		for (int col = col0; col <= col1; ++col)
Toshihiro Shimizu 890ddd
			saveTile(row, col);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TTileSaverFullColor::save(TPoint point)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_raster->getBounds().contains(point))
Toshihiro Shimizu 890ddd
		saveTile(point.y >> TILE_BIT_SIZE, point.x >> TILE_BIT_SIZE);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TTileSetFullColor *TTileSaverFullColor::getTileSet() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_tileSet;
Toshihiro Shimizu 890ddd
}