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