Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Pattern.cpp: implementation of the CPattern class.
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
e280ae
#ifdef _MSC_VER
Toshihiro Shimizu 890ddd
#pragma warning(disable : 4996)
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <string.h></string.h>
Toshihiro Shimizu 890ddd
//#include <io.h></io.h>
Toshihiro Shimizu 890ddd
#include <fcntl.h></fcntl.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "Pattern.h"
Toshihiro Shimizu 890ddd
#include "toonz4.6/raster.h"
Toshihiro Shimizu 890ddd
//#include "toonz4.6/img.h"
Toshihiro Shimizu 890ddd
#include "toonz4.6/casm.h"
Toshihiro Shimizu 890ddd
//#include "toonz4.6/casm_msg.h"
Toshihiro Shimizu 890ddd
#include "toonz4.6/file.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
#include "toonz4_6staff.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "SDef.h"
Toshihiro Shimizu 890ddd
#include "SError.h"
Toshihiro Shimizu 890ddd
#include "STPic.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
Toshihiro Shimizu 890ddd
// Construction/Destruction
Toshihiro Shimizu 890ddd
//////////////////////////////////////////////////////////////////////
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//#define P(a) tmsg_warning("-- %d --",a)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
CPattern::CPattern(RASTER *imgContour) : m_lX(0), m_lY(0) {
Shinya Kitaoka 120a6e
  if (!readPattern(imgContour)) {
Shinya Kitaoka 120a6e
    throw SFileReadError();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    optimalizeSize();
Shinya Kitaoka 120a6e
  } catch (SMemAllocError) {
Shinya Kitaoka 120a6e
    throw;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
CPattern::~CPattern() { null(); }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CPattern::null() {
Shinya Kitaoka 120a6e
  m_lX = m_lY = 0;
Shinya Kitaoka 120a6e
  m_pat.reset();
Shinya Kitaoka 120a6e
  m_fn[0] = '\0';
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool CPattern::readTTT(const char *fn) {
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
  int fd;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if ( (fd=_open(fn, _O_RDONLY | _O_BINARY ))==-1 )
Shinya Kitaoka 120a6e
          return false;
Shinya Kitaoka 120a6e
  if ( _read(fd,&m_lX,sizeof(int))!=sizeof(int) ) {
Shinya Kitaoka 120a6e
          _close(fd);
Shinya Kitaoka 120a6e
          return false;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if ( _read(fd,&m_lY,sizeof(int))!=sizeof(int) ) {
Shinya Kitaoka 120a6e
          _close(fd);
Shinya Kitaoka 120a6e
          return false;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if ( m_lX<=0 || m_lY<=0 ) {
Shinya Kitaoka 120a6e
          _close(fd);
Shinya Kitaoka 120a6e
          return false;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  UCHAR *buffer=0;
Shinya Kitaoka 120a6e
  buffer=new UCHAR[sizeof(UCHAR)*4*m_lX*m_lY];
Shinya Kitaoka 120a6e
  if ( !buffer ) {
Shinya Kitaoka 120a6e
          _close(fd);
Shinya Kitaoka 120a6e
          return false;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_pat=new UC_PIXEL[m_lX*m_lY];
Shinya Kitaoka 120a6e
  if ( !m_pat ) {
Shinya Kitaoka 120a6e
          delete [] buffer;
Shinya Kitaoka 120a6e
          _close(fd);
Shinya Kitaoka 120a6e
          return false;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int length=4*m_lX*m_lY*sizeof(UCHAR);
Shinya Kitaoka 120a6e
  if ( _read(fd,buffer,length)!=length ) {
Shinya Kitaoka 120a6e
          delete [] buffer;
Shinya Kitaoka 120a6e
          _close(fd);
Shinya Kitaoka 120a6e
          return false;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int xy=m_lX*m_lY;
Shinya Kitaoka 120a6e
  UCHAR* pb=buffer;
Shinya Kitaoka 120a6e
  UC_PIXEL* pPat=m_pat;
Shinya Kitaoka 120a6e
  for( int i=0; i
Shinya Kitaoka 120a6e
          pPat->r=pb[0];
Shinya Kitaoka 120a6e
          pPat->g=pb[1];
Shinya Kitaoka 120a6e
          pPat->b=pb[2];
Shinya Kitaoka 120a6e
          pPat->m=pb[3];
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  delete [] buffer;
Shinya Kitaoka 120a6e
  _close(fd);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool CPattern::readPattern(RASTER *imgContour) {
Shinya Kitaoka 120a6e
  null();
Shinya Kitaoka 120a6e
  if (!imgContour) {
Shinya Kitaoka 120a6e
    return false;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    RASTER *r = imgContour;
Shinya Kitaoka 120a6e
    if (r) {
Shinya Kitaoka 120a6e
      CSTPic<uc_pixel> lpic;</uc_pixel>
Shinya Kitaoka 120a6e
      lpic.read(r);
Shinya Kitaoka 120a6e
      if (lpic.m_lX > 0 && lpic.m_lY > 0 && lpic.m_pic) {
Shinya Kitaoka 120a6e
        m_lX = lpic.m_lX;
Shinya Kitaoka 120a6e
        m_lY = lpic.m_lY;
Shinya Kitaoka 120a6e
        m_pat.reset(new UC_PIXEL[m_lX * m_lY]);
Shinya Kitaoka 120a6e
        if (!m_pat) {
Shinya Kitaoka 120a6e
          m_lX = m_lY = 0;
Shinya Kitaoka 120a6e
          lpic.null();
Shinya Kitaoka 120a6e
          //			TRop::releaseRaster46(r, true);
Shinya Kitaoka 120a6e
          return false;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        for (int y = 0; y < m_lY; y++)
Shinya Kitaoka 120a6e
          for (int x = 0; x < m_lX; x++) {
Shinya Kitaoka 120a6e
            UC_PIXEL *plp = lpic.m_pic + y * lpic.m_lX + x;
Shinya Kitaoka 120a6e
            UC_PIXEL *ucp = m_pat.get() + y * m_lX + x;
Shinya Kitaoka 120a6e
            ucp->r        = plp->r;
Shinya Kitaoka 120a6e
            ucp->g        = plp->g;
Shinya Kitaoka 120a6e
            ucp->b        = plp->b;
Shinya Kitaoka 120a6e
            ucp->m        = plp->m;
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
      } else {
Shinya Kitaoka 120a6e
        lpic.null();
Shinya Kitaoka 120a6e
        //	TRop::releaseRaster46(r, true);
Shinya Kitaoka 120a6e
        return false;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      // TRop::releaseRaster46(r, true);
Shinya Kitaoka 120a6e
    } else
Shinya Kitaoka 120a6e
      return false;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CPattern::getMapPixel(const int xx, const int yy, const double invScale,
Shinya Kitaoka 120a6e
                           const double si, const double co, UC_PIXEL *&pucp) {
Shinya Kitaoka 120a6e
  pucp        = 0;
Shinya Kitaoka 120a6e
  double dxx  = (double)xx * invScale;
Shinya Kitaoka 120a6e
  double dyy  = (double)yy * invScale;
Shinya Kitaoka 120a6e
  double d2xx = (dxx * co - dyy * si) + (double)(m_lX - 1) * 0.5;
Shinya Kitaoka 120a6e
  double d2yy = (dxx * si + dyy * co) + (double)(m_lY - 1) * 0.5;
Shinya Kitaoka 120a6e
  int x       = I_ROUND(d2xx);
Shinya Kitaoka 120a6e
  int y       = I_ROUND(d2yy);
Shinya Kitaoka 120a6e
  if (x >= 0 && x < m_lX && y >= 0 && y < m_lY) {
Shinya Kitaoka 120a6e
    pucp = m_pat.get() + y * m_lX + x;
Shinya Kitaoka 120a6e
    pucp = (pucp->m) == (UCHAR)0 ? 0 : pucp;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CPattern::getMapPixel(const int xx, const int yy, const double invScale,
Shinya Kitaoka 120a6e
                           UC_PIXEL *&pucp) {
Shinya Kitaoka 120a6e
  pucp       = 0;
Shinya Kitaoka 120a6e
  double dxx = (double)xx * invScale + (double)(m_lX - 1) * 0.5;
Shinya Kitaoka 120a6e
  double dyy = (double)yy * invScale + (double)(m_lY - 1) * 0.5;
Shinya Kitaoka 120a6e
  int x      = I_ROUND(dxx);
Shinya Kitaoka 120a6e
  int y      = I_ROUND(dyy);
Shinya Kitaoka 120a6e
  if (x >= 0 && x < m_lX && y >= 0 && y < m_lY) {
Shinya Kitaoka 120a6e
    pucp = m_pat.get() + y * m_lX + x;
Shinya Kitaoka 120a6e
    pucp = (pucp->m) == (UCHAR)0 ? 0 : pucp;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CPattern::getBBox(SRECT &bb) {
Shinya Kitaoka 120a6e
  bb.x0          = m_lX;
Shinya Kitaoka 120a6e
  bb.y0          = m_lY;
Shinya Kitaoka 120a6e
  bb.x1          = -1;
Shinya Kitaoka 120a6e
  bb.y1          = -1;
Shinya Kitaoka 120a6e
  UC_PIXEL *pPic = m_pat.get();
Shinya Kitaoka 120a6e
  for (int y = 0; y < m_lY; y++)
Shinya Kitaoka 120a6e
    for (int x = 0; x < m_lX; x++, pPic++)
Shinya Kitaoka 120a6e
      if (pPic->m > (UCHAR)0) {
Shinya Kitaoka 120a6e
        bb.x0 = std::min(bb.x0, x);
Shinya Kitaoka 120a6e
        bb.y0 = std::min(bb.y0, y);
Shinya Kitaoka 120a6e
        bb.x1 = std::max(bb.x1, x);
Shinya Kitaoka 120a6e
        bb.y1 = std::max(bb.y1, y);
Shinya Kitaoka 120a6e
      }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CPattern::optimalizeSize() {
Shinya Kitaoka 120a6e
  SRECT bb;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  getBBox(bb);
Shinya Kitaoka 120a6e
  if (bb.x0 <= bb.x1 && bb.y0 <= bb.y1) {
Shinya Kitaoka 120a6e
    int nLX = bb.x1 - bb.x0 + 1;
Shinya Kitaoka 120a6e
    int nLY = bb.y1 - bb.y0 + 1;
Shinya Kitaoka 120a6e
    std::unique_ptr<uc_pixel[]> nPat(new UC_PIXEL[nLX * nLY]);</uc_pixel[]>
Shinya Kitaoka 120a6e
    if (!nPat) {
Shinya Kitaoka 120a6e
      char s[200];
Rozhuk Ivan 823a31
      snprintf(s, sizeof(s), "in Pattern Optimalization \n");
Shinya Kitaoka 120a6e
      throw SMemAllocError(s);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    for (int y = bb.y0; y <= bb.y1; y++)
Shinya Kitaoka 120a6e
      for (int x = bb.x0; x <= bb.x1; x++) {
Shinya Kitaoka 120a6e
        UC_PIXEL *pPat  = m_pat.get() + y * m_lX + x;
Shinya Kitaoka 120a6e
        UC_PIXEL *pNPat = nPat.get() + (y - bb.y0) * nLX + x - bb.x0;
Shinya Kitaoka 120a6e
        pNPat->r        = pPat->r;
Shinya Kitaoka 120a6e
        pNPat->g        = pPat->g;
Shinya Kitaoka 120a6e
        pNPat->b        = pPat->b;
Shinya Kitaoka 120a6e
        pNPat->m        = pPat->m;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    m_lX  = nLX;
Shinya Kitaoka 120a6e
    m_lY  = nLY;
Shinya Kitaoka 120a6e
    m_pat = std::move(nPat);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CPattern::eraseBuffer(const int lX, const int lY, UC_PIXEL *buffer) {
Shinya Kitaoka 120a6e
  int xy       = lX * lY;
Shinya Kitaoka 120a6e
  UC_PIXEL *pb = buffer;
Shinya Kitaoka 120a6e
  for (int i = 0; i < xy; i++, pb++) pb->r = pb->g = pb->b = pb->m = (UCHAR)0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CPattern::rotate(const double angle) {
Shinya Kitaoka 120a6e
  if (m_lX <= 0 || m_lY <= 0 || !m_pat) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double sDiag = sqrt(double(m_lX * m_lX + m_lY * m_lY));
Shinya Kitaoka 120a6e
  int nLXY     = (int)sDiag + 5;
Shinya Kitaoka 120a6e
  int xx       = nLXY / 2;
Shinya Kitaoka 120a6e
  int yy       = xx;
Shinya Kitaoka 120a6e
  double co    = cos(-DEG2RAD(angle));
Shinya Kitaoka 120a6e
  double si    = sin(-DEG2RAD(angle));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  std::unique_ptr<uc_pixel[]> nPat(new UC_PIXEL[nLXY * nLXY]);</uc_pixel[]>
Shinya Kitaoka 120a6e
  if (!nPat) {
Shinya Kitaoka 120a6e
    char s[200];
Rozhuk Ivan 823a31
    snprintf(s, sizeof(s), "in Pattern Rotation \n");
Shinya Kitaoka 120a6e
    throw SMemAllocError(s);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  eraseBuffer(nLXY, nLXY, nPat.get());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  UC_PIXEL *pNPat = nPat.get();
Shinya Kitaoka 120a6e
  for (int y = 0; y < nLXY; y++)
Shinya Kitaoka 120a6e
    for (int x = 0; x < nLXY; x++, pNPat++) {
Shinya Kitaoka 120a6e
      UC_PIXEL *pucp = 0;
Shinya Kitaoka 120a6e
      getMapPixel(x - xx, y - yy, 1.0, si, co, pucp);
Shinya Kitaoka 120a6e
      if (pucp) {
Shinya Kitaoka 120a6e
        pNPat->r = pucp->r;
Shinya Kitaoka 120a6e
        pNPat->g = pucp->g;
Shinya Kitaoka 120a6e
        pNPat->b = pucp->b;
Shinya Kitaoka 120a6e
        pNPat->m = pucp->m;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  m_lX = m_lY = nLXY;
Shinya Kitaoka 120a6e
  m_pat       = std::move(nPat);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    optimalizeSize();
Shinya Kitaoka 120a6e
  } catch (SMemAllocError) {
Shinya Kitaoka 120a6e
    throw;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}