Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <set></set>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tropcm.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tcolorstyles.h"
Toshihiro Shimizu 890ddd
#include "tpixelutils.h"
Toshihiro Shimizu 890ddd
#include "ttile.h"
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
#include "timage_io.h"
Toshihiro Shimizu 890ddd
#include "trasterimage.h"
shun-iwasawa 3a1040
#include "tsimplecolorstyles.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//#include "tlevel.h"
Toshihiro Shimizu 890ddd
//#include "ttoonzimage.h"
Toshihiro Shimizu 890ddd
//#include "tgeometry.h"
Toshihiro Shimizu 890ddd
//#include "timage_io.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
extern "C" {
Toshihiro Shimizu 890ddd
#include "toonz4.6/raster.h"
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
322341
#if defined(_WIN32) && defined(x64)
Toshihiro Shimizu 890ddd
#define USE_SSE2
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef USE_SSE2
Toshihiro Shimizu 890ddd
#include <emmintrin.h></emmintrin.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
2f0c77
DV_ALIGNED(16) class TPixelFloat {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TPixelFloat() : b(0), g(0), r(0), m(0) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPixelFloat(float rr, float gg, float bb, float mm)
Shinya Kitaoka 120a6e
      : b(bb), g(gg), r(rr), m(mm) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPixelFloat(const TPixel32 &pix) : b(pix.b), g(pix.g), r(pix.r), m(pix.m) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  float b, g, r, m;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // anonymous namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool renderRas32(const TTile &tileOut, const TTile &tileIn,
Shinya Kitaoka 120a6e
                 const TPaletteP palette);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const TPixel32 c_transparencyCheckPaint = TPixel32(80, 80, 80, 255);
Shinya Kitaoka 120a6e
const TPixel32 c_transparencyCheckInk   = TPixel32::Black;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TRop::convert(const TRaster32P &rasOut, const TRasterCM32P &rasIn,
Shinya Kitaoka 120a6e
                   const TPaletteP palette, bool transparencyCheck) {
Shinya Kitaoka 120a6e
  int count = palette->getStyleCount();
Shinya Kitaoka 120a6e
  int count2 =
Shinya Kitaoka 120a6e
      std::max({count, TPixelCM32::getMaxInk(), TPixelCM32::getMaxPaint()});
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // per poter utilizzare lo switch (piu' efficiente) si utilizza 255
Shinya Kitaoka 120a6e
  // anziche' TPixelCM32::getMaxTone()
Shinya Kitaoka 120a6e
  assert(TPixelCM32::getMaxTone() == 255);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int rasLx = rasOut->getLx();
Shinya Kitaoka 120a6e
  int rasLy = rasOut->getLy();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  rasOut->lock();
Shinya Kitaoka 120a6e
  rasIn->lock();
322341
#ifdef USE_SSE2
Shinya Kitaoka 120a6e
  if (TSystem::getCPUExtensions() & TSystem::CpuSupportsSse2) {
Shinya Kitaoka 120a6e
    __m128i zeros = _mm_setzero_si128();
Shinya Kitaoka 120a6e
    TPixelFloat *paints =
Shinya Kitaoka 120a6e
        (TPixelFloat *)_aligned_malloc(count2 * sizeof(TPixelFloat), 16);
Shinya Kitaoka 120a6e
    TPixelFloat *inks =
Shinya Kitaoka 120a6e
        (TPixelFloat *)_aligned_malloc(count2 * sizeof(TPixelFloat), 16);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    std::vector<tpixel32> paints2(count2);</tpixel32>
Shinya Kitaoka 120a6e
    std::vector<tpixel32> inks2(count2);</tpixel32>
Shinya Kitaoka 120a6e
    if (transparencyCheck) {
Shinya Kitaoka 120a6e
      for (int i = 0; i < palette->getStyleCount(); i++) {
Toshihiro Shimizu 890ddd
        paints2[i] = c_transparencyCheckPaint;
Shinya Kitaoka 120a6e
        inks2[i]   = c_transparencyCheckInk;
Toshihiro Shimizu 890ddd
        paints[i]  = TPixelFloat(paints2[i]);
Shinya Kitaoka 120a6e
        inks[i]    = TPixelFloat(inks2[i]);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      paints2[0] = TPixel32::Transparent;
Shinya Kitaoka 120a6e
      paints[0]  = TPixelFloat(TPixel32::Transparent);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    /*
Shinya Kitaoka 120a6e
else if (true)
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
            for(int i=0;i<palette->getStyleCount();i++)</palette->
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
paints2[i] = c_transparencyCheckPaint;
Shinya Kitaoka 120a6e
                    inks2[i]   = c_transparencyCheckInk;
Shinya Kitaoka 120a6e
paints[i]  = TPixelFloat(paints2[i]);
Shinya Kitaoka 120a6e
                    inks[i]    = TPixelFloat(inks2[i]);
Shinya Kitaoka 120a6e
            paints2[i] = TPixel32::Transparent;
Shinya Kitaoka 120a6e
            paints[i] = TPixelFloat(TPixel32::Transparent);
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
            paints2[0] = TPixel32::Transparent;
Shinya Kitaoka 120a6e
            paints[0] = TPixelFloat(TPixel32::Transparent);
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      for (int i = 0; i < palette->getStyleCount(); i++) {
Shinya Kitaoka 120a6e
        TPixel32 color = ::premultiply(palette->getStyle(i)->getAverageColor());
Shinya Kitaoka 120a6e
        paints[i] = inks[i] = TPixelFloat(color);
Shinya Kitaoka 120a6e
        paints2[i] = inks2[i] = color;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    float maxTone     = (float)TPixelCM32::getMaxTone();
Shinya Kitaoka 120a6e
    __m128 den_packed = _mm_load1_ps(&maxTone);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    for (int y = 0; y < rasLy; ++y) {
Shinya Kitaoka 120a6e
      TPixel32 *pix32   = rasOut->pixels(y);
Shinya Kitaoka 120a6e
      TPixelCM32 *pixIn = rasIn->pixels(y);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      TPixelCM32 *endPixIn = pixIn + rasLx;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      while (pixIn < endPixIn) {
Shinya Kitaoka 120a6e
        int tt = pixIn->getTone();
Shinya Kitaoka 120a6e
        int p  = pixIn->getPaint();
Shinya Kitaoka 120a6e
        int i  = pixIn->getInk();
Shinya Kitaoka 120a6e
        switch (tt) {
Shinya Kitaoka 120a6e
        case 255:
Shinya Kitaoka 120a6e
          *pix32++ = paints2[p];
Shinya Kitaoka 120a6e
          break;
Shinya Kitaoka 120a6e
        case 0:
Shinya Kitaoka 120a6e
          *pix32++ = inks2[i];
Shinya Kitaoka 120a6e
          break;
Shinya Kitaoka 120a6e
        default: {
Shinya Kitaoka 120a6e
          float t         = (float)tt;
Shinya Kitaoka 120a6e
          __m128 a_packed = _mm_load_ps((float *)&(inks[i]));
Shinya Kitaoka 120a6e
          __m128 b_packed = _mm_load_ps((float *)&(paints[p]));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          __m128 num_packed  = _mm_load1_ps(&t);
Shinya Kitaoka 120a6e
          __m128 diff_packed = _mm_sub_ps(den_packed, num_packed);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          // calcola in modo vettoriale out = ((den-num)*a + num*b)/den
Shinya Kitaoka 120a6e
          __m128 outPix_packed = _mm_mul_ps(diff_packed, a_packed);
Shinya Kitaoka 120a6e
          __m128 tmpPix_packed = _mm_mul_ps(num_packed, b_packed);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          outPix_packed = _mm_add_ps(outPix_packed, tmpPix_packed);
Shinya Kitaoka 120a6e
          outPix_packed = _mm_div_ps(outPix_packed, den_packed);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          // converte i canali da float a char
Shinya Kitaoka 120a6e
          __m128i outPix_packed_i = _mm_cvtps_epi32(outPix_packed);
Shinya Kitaoka 120a6e
          outPix_packed_i         = _mm_packs_epi32(outPix_packed_i, zeros);
Shinya Kitaoka 120a6e
          outPix_packed_i         = _mm_packus_epi16(outPix_packed_i, zeros);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          *(DWORD *)(pix32) = _mm_cvtsi128_si32(outPix_packed_i);
Shinya Kitaoka 120a6e
          ++pix32;
Toshihiro Shimizu 890ddd
        }
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        ++pixIn;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    _aligned_free(paints);
Shinya Kitaoka 120a6e
    _aligned_free(inks);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  } else  // SSE2 not supported
Shinya Kitaoka 120a6e
#endif    // _WIN32
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    std::vector<tpixel32> paints(count2, TPixel32(255, 0, 0));</tpixel32>
Shinya Kitaoka 120a6e
    std::vector<tpixel32> inks(count2, TPixel32(255, 0, 0));</tpixel32>
Shinya Kitaoka 120a6e
    if (transparencyCheck) {
Shinya Kitaoka 120a6e
      for (int i = 0; i < palette->getStyleCount(); i++) {
Shinya Kitaoka 120a6e
        paints[i] = c_transparencyCheckPaint;
Shinya Kitaoka 120a6e
        inks[i]   = c_transparencyCheckInk;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      paints[0] = TPixel32::Transparent;
Shinya Kitaoka 120a6e
    } else
shun-iwasawa 1a977b
      for (int i = 0; i < palette->getStyleCount(); i++)
Shinya Kitaoka 120a6e
        paints[i] = inks[i] =
Shinya Kitaoka 120a6e
            ::premultiply(palette->getStyle(i)->getAverageColor());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    for (int y = 0; y < rasLy; ++y) {
Shinya Kitaoka 120a6e
      TPixel32 *pix32      = rasOut->pixels(y);
Shinya Kitaoka 120a6e
      TPixelCM32 *pixIn    = rasIn->pixels(y);
Shinya Kitaoka 120a6e
      TPixelCM32 *endPixIn = pixIn + rasLx;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      while (pixIn < endPixIn) {
Shinya Kitaoka 120a6e
        int t = pixIn->getTone();
Shinya Kitaoka 120a6e
        int p = pixIn->getPaint();
Shinya Kitaoka 120a6e
        int i = pixIn->getInk();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        if (t == TPixelCM32::getMaxTone())
Shinya Kitaoka 120a6e
          *pix32++ = paints[p];
Shinya Kitaoka 120a6e
        else if (t == 0)
Shinya Kitaoka 120a6e
          *pix32++ = inks[i];
Shinya Kitaoka 120a6e
        else
Shinya Kitaoka 120a6e
          *pix32++ = blend(inks[i], paints[p], t, TPixelCM32::getMaxTone());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        ++pixIn;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  rasOut->unlock();
Shinya Kitaoka 120a6e
  rasIn->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 27b0cf
static void do_convert(const TTile &dst, const TTile &src,
shun-iwasawa 27b0cf
                       const TPaletteP palette, bool transparencyCheck,
shun-iwasawa 27b0cf
                       bool applyFx) {
Shinya Kitaoka 120a6e
  // assert(palette);
Shinya Kitaoka 120a6e
  // assert(_rasOut && _rasIn);
Shinya Kitaoka 120a6e
  // assert(rasOut->getSize() == rasIn->getSize());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (applyFx && renderRas32(dst, src, palette)) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRaster32P rasOut;
Shinya Kitaoka 120a6e
  TRasterCM32P rasIn;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (dst.getRaster()->getSize() != src.getRaster()->getSize()) {
Shinya Kitaoka 120a6e
    TRect rect = TRect(convert(dst.m_pos), dst.getRaster()->getSize()) *
Shinya Kitaoka 120a6e
                 TRect(convert(src.m_pos), src.getRaster()->getSize());
Shinya Kitaoka 120a6e
    TRect rectOut = rect - convert(dst.m_pos);
Shinya Kitaoka 120a6e
    rasOut        = dst.getRaster()->extract(rectOut);
Shinya Kitaoka 120a6e
    TRect rectIn  = rect - convert(src.m_pos);
Shinya Kitaoka 120a6e
    rasIn         = src.getRaster()->extract(rectIn);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    rasOut = dst.getRaster();
Shinya Kitaoka 120a6e
    rasIn  = src.getRaster();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TRop::convert(rasOut, rasIn, palette, transparencyCheck);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TRop::convert(
Shinya Kitaoka 120a6e
    const TRaster32P &rasOut, const TRasterCM32P &rasIn, TPaletteP palette,
Shinya Kitaoka 120a6e
    const TRect &theClipRect,  // il rect su cui e' applicata la conversione
Shinya Kitaoka 120a6e
    bool transparencyCheck, bool applyFx) {
Shinya Kitaoka 120a6e
  assert(palette);
Shinya Kitaoka 120a6e
  assert(rasIn && rasOut);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRect clipRect(theClipRect);
Shinya Kitaoka 120a6e
  if (clipRect.isEmpty())
Shinya Kitaoka 120a6e
    clipRect = rasIn->getBounds();
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    if (!clipRect.overlaps(rasIn->getBounds())) return;
Shinya Kitaoka 120a6e
    clipRect = clipRect * rasIn->getBounds();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (clipRect.isEmpty()) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRect clipRectIn, clipRectOut;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (applyFx && palette->getFxRects(clipRect, clipRectIn, clipRectOut)) {
Shinya Kitaoka 120a6e
    TRect rAux = clipRectIn;
Shinya Kitaoka 120a6e
    TRasterP rAuxIn =
Shinya Kitaoka 120a6e
        rasIn->extract(clipRectIn);  // la extract modifica clipRectIn
Shinya Kitaoka 120a6e
    if (rAux != clipRectIn && rAux != rasIn->getBounds()) {
Shinya Kitaoka 120a6e
      TRasterCM32P rNew(rAux.getSize());
Shinya Kitaoka 120a6e
      TRect tmpRect = clipRectIn - rAux.getP00();
Shinya Kitaoka 120a6e
      rNew->extract(tmpRect)->copy(rAuxIn);
Shinya Kitaoka 120a6e
      rAuxIn     = rNew;
Shinya Kitaoka 120a6e
      clipRectIn = rAux;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TTile tileIn(rAuxIn, ::convert(clipRectIn.getP00()));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    rAux = clipRectOut;
Shinya Kitaoka 120a6e
    TRasterP rAuxOut =
Shinya Kitaoka 120a6e
        rasOut->extract(clipRectOut);  // la extract modifica clipRectOut
Shinya Kitaoka 120a6e
    TTile tileOut(rAuxOut, ::convert(clipRectOut.getP00()));
Shinya Kitaoka 120a6e
    TRop::convert(tileOut, tileIn, palette, transparencyCheck, true);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    TRect clipRectIn  = clipRect;
Shinya Kitaoka 120a6e
    TRect clipRectOut = clipRect;
Shinya Kitaoka 120a6e
    TRasterP _rasOut  = rasOut->extract(clipRectOut);
Shinya Kitaoka 120a6e
    TRasterP _rasIn   = rasIn->extract(clipRectIn);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TTile t1(_rasOut, ::convert(clipRectOut.getP00()));
Shinya Kitaoka 120a6e
    TTile t2(_rasIn, ::convert(clipRectIn.getP00()));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TRop::convert(t1, t2, palette, transparencyCheck, false);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TRop::convert(const TTile &dst, const TTile &src, const TPaletteP plt,
Shinya Kitaoka 120a6e
                   bool transparencyCheck, bool applyFxs) {
Shinya Kitaoka 120a6e
  // if (dst->getSize() != src->getSize())
Shinya Kitaoka 120a6e
  //  throw TRopException("convert: size mismatch");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // assert(plt);
Shinya Kitaoka 120a6e
  if ((TRaster32P)dst.getRaster())
Shinya Kitaoka 120a6e
    do_convert(dst, src, plt, transparencyCheck, applyFxs);
Shinya Kitaoka 120a6e
  else if ((TRaster64P)dst.getRaster()) {
Shinya Kitaoka 120a6e
    TRaster32P aux(dst.getRaster()->getLx(), dst.getRaster()->getLy());
Shinya Kitaoka 120a6e
    TTile taux(aux, dst.m_pos);
Shinya Kitaoka 120a6e
    do_convert(taux, src, plt, transparencyCheck, applyFxs);
Shinya Kitaoka 120a6e
    TRop::convert(dst.getRaster(), aux);
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    throw TRopException("unsupported pixel type");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TRasterP putSinglePaintInRaster(TRasterCM32P &rasIn, int paintId,
Shinya Kitaoka 120a6e
                                TPixel32 color) {
Shinya Kitaoka 120a6e
  TRaster32P rasOut;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (int y = 0; y < rasIn->getLy(); y++) {
Shinya Kitaoka 120a6e
    TPixelCM32 *pixIn  = rasIn->pixels(y);
Shinya Kitaoka 120a6e
    TPixelCM32 *endPix = pixIn + rasIn->getLx();
Shinya Kitaoka 120a6e
    TPixel32 *pixOut   = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (rasOut) pixOut = rasOut->pixels(y);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    while (pixIn < endPix) {
Shinya Kitaoka 120a6e
      if (pixIn->getPaint() == paintId) {
Shinya Kitaoka 120a6e
        if (!rasOut) {
Shinya Kitaoka 120a6e
          rasOut = TRaster32P(rasIn->getLx(), rasIn->getLy());
Shinya Kitaoka 120a6e
          rasOut->fill(TPixel32::Transparent);
Shinya Kitaoka 120a6e
          pixOut = rasOut->pixels(y) + (pixIn - rasIn->pixels(y));
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        if (!pixIn->isPureInk()) *pixOut = color;
Shinya Kitaoka 120a6e
        /*else if (!pixIn->isPureInk())
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
        assert(TPixelCM32::getMaxTone()==0xff);
Shinya Kitaoka 120a6e
        pixOut->m = pixIn->getTone();
Shinya Kitaoka 120a6e
        pixOut->r = 255*color.r/pixOut->m;
Shinya Kitaoka 120a6e
        pixOut->g = 255*color.g/pixOut->m;
Shinya Kitaoka 120a6e
        pixOut->b = 255*color.b/pixOut->m;
Shinya Kitaoka 120a6e
}*/
Shinya Kitaoka 120a6e
        pixIn->setPaint(0);
Shinya Kitaoka 120a6e
        // pixIn->setTone(0);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      pixIn++;
Shinya Kitaoka 120a6e
      pixOut++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return rasOut;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TRasterP putSingleInkInRasterGR8(TRasterCM32P &rasIn, int inkId) {
Shinya Kitaoka 120a6e
  TRasterGR8P rasOut;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (int y = 0; y < rasIn->getLy(); y++) {
Shinya Kitaoka 120a6e
    TPixelCM32 *pixIn  = rasIn->pixels(y);
Shinya Kitaoka 120a6e
    TPixelCM32 *endPix = pixIn + rasIn->getLx();
Shinya Kitaoka 120a6e
    TPixelGR8 *pixOut;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (rasOut) pixOut = rasOut->pixels(y);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    while (pixIn < endPix) {
Shinya Kitaoka 120a6e
      if (pixIn->getInk() == inkId) {
Shinya Kitaoka 120a6e
        assert(TPixelCM32::getMaxTone() == 0xff);
Shinya Kitaoka 120a6e
        if (!rasOut) {
Shinya Kitaoka 120a6e
          rasOut = TRasterGR8P(rasIn->getLx(), rasIn->getLy());
Shinya Kitaoka 120a6e
          rasOut->fill(0);
Shinya Kitaoka 120a6e
          pixOut = rasOut->pixels(y) + (pixIn - rasIn->pixels(y));
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        *pixOut = 255 - pixIn->getTone();
Shinya Kitaoka 120a6e
        // pixIn->setInk(0);
Shinya Kitaoka 120a6e
        pixIn->setTone(TPixelCM32::getMaxTone());
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      pixIn++;
Shinya Kitaoka 120a6e
      pixOut++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return rasOut;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Shinya Kitaoka 120a6e
TRasterP putSingleInkInRasterRGBM(TRasterCM32P &rasIn, int inkId) {
Shinya Kitaoka 120a6e
  TRaster32P rasOut;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (int y = 0; y < rasIn->getLy(); y++) {
Shinya Kitaoka 120a6e
    TPixelCM32 *pixIn  = rasIn->pixels(y);
Shinya Kitaoka 120a6e
    TPixelCM32 *endPix = pixIn + rasIn->getLx();
Shinya Kitaoka 120a6e
    TPixel *pixOut;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (rasOut) pixOut = rasOut->pixels(y);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    while (pixIn < endPix) {
Shinya Kitaoka 120a6e
      if (pixIn->getInk() == inkId) {
Shinya Kitaoka 120a6e
        assert(TPixelCM32::getMaxTone() == 0xff);
Shinya Kitaoka 120a6e
        if (!rasOut) {
Shinya Kitaoka 120a6e
          rasOut = TRaster32P(rasIn->getLx(), rasIn->getLy());
Shinya Kitaoka 120a6e
          rasOut->fill(TPixel32::Transparent);
Shinya Kitaoka 120a6e
          pixOut = rasOut->pixels(y) + (pixIn - rasIn->pixels(y));
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        pixOut->r = pixOut->g = pixOut->b = pixOut->m = 255 - pixIn->getTone();
Shinya Kitaoka 120a6e
        // pixIn->setInk(0);
Shinya Kitaoka 120a6e
        pixIn->setTone(TPixelCM32::getMaxTone());
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      pixIn++;
Shinya Kitaoka 120a6e
      if (rasOut) pixOut++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return rasOut;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// filename!!
Shinya Kitaoka 120a6e
// interactive!!
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
bool computePaletteFx(const std::vector<std::pair<tcolorstyle *,="" int="">> &fx,</std::pair<tcolorstyle>
Shinya Kitaoka 120a6e
                      const TTile &tileOut, const TTile &tileIn,
Shinya Kitaoka 120a6e
                      const TPaletteP plt) {
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  TRasterCM32P rasIn = tileIn.getRaster();
Shinya Kitaoka 120a6e
  TRaster32P rAux32, rasOut = tileOut.getRaster();
Shinya Kitaoka 120a6e
  TRasterGR8P rAuxGR;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int frame = plt->getFrame();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::vector<trasterp> paintLayers(fx.size());</trasterp>
Shinya Kitaoka 120a6e
  std::vector<trasterp> inkLayers(fx.size());</trasterp>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // tolgo dal raster d'ingresso gli ink e i paint con gli effetti, mettendoli
Shinya Kitaoka 120a6e
  // in dei raster layer
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)fx.size(); i++) {
Shinya Kitaoka 120a6e
    TRasterStyleFx *rfx = fx[i].first->getRasterStyleFx();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (rfx->isPaintStyle())
Shinya Kitaoka 120a6e
      paintLayers[i] = putSinglePaintInRaster(rasIn, fx[i].second,
Shinya Kitaoka 120a6e
                                              fx[i].first->getMainColor());
Shinya Kitaoka 120a6e
    if (rfx->isInkStyle()) {
Shinya Kitaoka 120a6e
      if (rfx->inkFxNeedRGBMRaster())
Shinya Kitaoka 120a6e
        inkLayers[i] = putSingleInkInRasterRGBM(rasIn, fx[i].second);
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        inkLayers[i] = putSingleInkInRasterGR8(rasIn, fx[i].second);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // raster d'ingresso senza i colori fx, viene renderizzato nel raster d'uscita
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  do_convert(tileOut, tileIn, plt, false, false);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // ogni layer viene "effettato".; se il risultato e' non nullo, viene
Shinya Kitaoka 120a6e
  // sovrapposto sul raster d'uscita
Shinya Kitaoka 120a6e
  // prima vengono messi tutti i layer di paint, poi quelli di ink
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRect rectOut =
Shinya Kitaoka 120a6e
      TRect(convert(tileOut.m_pos), tileOut.getRaster()->getSize()) -
Shinya Kitaoka 120a6e
      convert(tileIn.m_pos);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)fx.size(); i++)
Shinya Kitaoka 120a6e
    if (paintLayers[i]) {
Shinya Kitaoka 120a6e
      TRasterStyleFx::Params params(paintLayers[i], convert(tileIn.m_pos),
Shinya Kitaoka 120a6e
                                    rasIn, fx[i].second, frame);
Shinya Kitaoka 120a6e
      if (fx[i].first->getRasterStyleFx()->compute(params))
Shinya Kitaoka 120a6e
        TRop::over(rasOut, paintLayers[i]->extract(rectOut), rasOut);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)fx.size(); i++)
Shinya Kitaoka 120a6e
    if (inkLayers[i]) {
Shinya Kitaoka 120a6e
      TRasterStyleFx *rfx = fx[i].first->getRasterStyleFx();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      TRasterStyleFx::Params params(inkLayers[i], convert(tileIn.m_pos), rasIn,
Shinya Kitaoka 120a6e
                                    fx[i].second, frame);
Shinya Kitaoka 120a6e
      if (rfx->compute(params)) {
Shinya Kitaoka 120a6e
        if (rfx->inkFxNeedRGBMRaster())
Shinya Kitaoka 120a6e
          TRop::over(rasOut, rasOut, inkLayers[i]->extract(rectOut));
Shinya Kitaoka 120a6e
        else
Shinya Kitaoka 120a6e
          TRop::over(rasOut, inkLayers[i]->extract(rectOut),
Shinya Kitaoka 120a6e
                     fx[i].first->getMainColor());
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool renderRas32(const TTile &tileOut, const TTile &tileIn,
Shinya Kitaoka 120a6e
                 const TPaletteP palette) {
Shinya Kitaoka 120a6e
  assert(TRect(convert(tileIn.m_pos), tileIn.getRaster()->getSize())
Shinya Kitaoka 120a6e
             .contains(TRect(convert(tileOut.m_pos),
Shinya Kitaoka 120a6e
                             tileOut.getRaster()->getSize())));
Shinya Kitaoka 120a6e
  assert((TRasterCM32P)tileIn.getRaster());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Shrink = shrink;
Shinya Kitaoka 120a6e
  // INIT_TCM(rin)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /* mark up are made apart */
Shinya Kitaoka 120a6e
  // computeMarkup(rasIn, palette);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  std::vector<std::pair<tcolorstyle *,="" int="">> fxArray;</std::pair<tcolorstyle>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  for (int i = 0; i < palette->getStyleCount(); i++)
Shinya Kitaoka 120a6e
    if (palette->getStyle(i)->isRasterStyle())
Shinya Kitaoka 120a6e
      fxArray.push_back(std::pair<tcolorstyle *,="" int="">(palette->getStyle(i), i));</tcolorstyle>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (fxArray.empty()) return false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TTile _tileIn(tileIn.getRaster()->clone(), tileIn.m_pos);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  tileIn.getRaster()->lock();
Shinya Kitaoka 120a6e
  tileOut.getRaster()->lock();
Shinya Kitaoka 120a6e
  computePaletteFx(fxArray, tileOut, _tileIn, palette);
Shinya Kitaoka 120a6e
  tileIn.getRaster()->unlock();
Shinya Kitaoka 120a6e
  tileOut.getRaster()->unlock();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void addColor(TPaletteP plt, int index, const TPaletteP &upPlt,
Shinya Kitaoka 120a6e
              std::map<int, int=""> &usedInks) {</int,>
Shinya Kitaoka 120a6e
  TColorStyle *cs = upPlt->getStyle(index);
Shinya Kitaoka 120a6e
  if (cs && cs->getMainColor() == plt->getStyle(index)->getMainColor()) {
Shinya Kitaoka 120a6e
    usedInks[index] = index;
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
shun-iwasawa 1a977b
  int firstStyleId = plt->getFirstUnpagedStyle();
Shinya Kitaoka 120a6e
  if (firstStyleId == -1) firstStyleId = plt->getStyleCount();
shun-iwasawa 1a977b
  usedInks[index] = firstStyleId;
Shinya Kitaoka 120a6e
  plt->getPage(0)->addStyle(TPixel32::Red);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void addColor(TPaletteP plt, int index, std::map<int, int=""> &usedInks) {</int,>
shun-iwasawa 1a977b
  int firstStyleId = plt->getFirstUnpagedStyle();
Shinya Kitaoka 120a6e
  if (firstStyleId == -1) firstStyleId = plt->getStyleCount();
shun-iwasawa 1a977b
  usedInks[index] = firstStyleId;
Shinya Kitaoka 120a6e
  plt->getPage(0)->addStyle(TPixel32::Red);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
shun-iwasawa 3a1040
// Check if the downPlt has the same style (same color, same index) and use it.
shun-iwasawa 3a1040
// If the same style is not found, then add it as a new style.
shun-iwasawa 3a1040
void tryMergeInkOrAddColor(TPaletteP downPlt, const TPaletteP matchPlt,
shun-iwasawa 3a1040
                           int index, std::map<int, int=""> &usedInks) {</int,>
shun-iwasawa 3a1040
  if (0 <= index && index < downPlt->getStyleCount() &&
shun-iwasawa 3a1040
      index < matchPlt->getStyleCount()) {
shun-iwasawa 3a1040
    TSolidColorStyle *solidDownStyle =
shun-iwasawa 3a1040
        dynamic_cast<tsolidcolorstyle *="">(downPlt->getStyle(index));</tsolidcolorstyle>
shun-iwasawa 3a1040
    TSolidColorStyle *solidMatchStyle =
shun-iwasawa 3a1040
        dynamic_cast<tsolidcolorstyle *="">(matchPlt->getStyle(index));</tsolidcolorstyle>
shun-iwasawa 3a1040
    if (solidDownStyle && solidMatchStyle &&
shun-iwasawa 3a1040
        solidDownStyle->getMainColor() == solidMatchStyle->getMainColor()) {
shun-iwasawa 3a1040
      usedInks[index] = index;
shun-iwasawa 3a1040
      return;
shun-iwasawa 3a1040
    }
shun-iwasawa 3a1040
  }
shun-iwasawa 3a1040
  addColor(downPlt, index, usedInks);
shun-iwasawa 3a1040
}
shun-iwasawa 3a1040
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Shinya Kitaoka 120a6e
// std::map<int,int>& usedInk  upInkId -> downInkId</int,int>
Shinya Kitaoka 120a6e
void doMergeCmapped(TRasterCM32P rasOut, const TRasterCM32P &rasUp,
shun-iwasawa 3a1040
                    const TPaletteP &pltOut, const TPaletteP &matchPlt,
shun-iwasawa 3a1040
                    bool onlyInks, int matchlinePrevalence,
shun-iwasawa 3a1040
                    std::map<int, int=""> &usedInks, bool mergePalette) {</int,>
Shinya Kitaoka 120a6e
  double val = matchlinePrevalence / 100.0;  // matchlinePrevalence ==0 always
Shinya Kitaoka 120a6e
                                             // ink down; matchlinePrevalence ==
Shinya Kitaoka 120a6e
                                             // 100 always ink up;
Shinya Kitaoka 120a6e
  assert(rasOut && rasUp);
Shinya Kitaoka 120a6e
  assert(rasOut->getSize() == rasUp->getSize());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TPaletteP downPlt = pltOut->clone();
Shinya Kitaoka 120a6e
  std::map<int, int="">::iterator it;</int,>
Shinya Kitaoka 120a6e
  for (it = usedInks.begin(); it != usedInks.end(); it++)
Shinya Kitaoka 120a6e
    downPlt->getPage(0)->addStyle(TPixel32::Red);
Shinya Kitaoka 120a6e
  for (int y = 0; y < rasOut->getLy(); y++) {
Shinya Kitaoka 120a6e
    TPixelCM32 *pixDown = rasOut->pixels(y),
Shinya Kitaoka 120a6e
               *endPix  = pixDown + rasOut->getLx();
Shinya Kitaoka 120a6e
    TPixelCM32 *pixUp   = rasUp->pixels(y);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    while (pixDown < endPix) {
Shinya Kitaoka 120a6e
      // there is lines in matchline
Shinya Kitaoka 120a6e
      if (!pixUp->isPurePaint()) {
Shinya Kitaoka 120a6e
        int toneDown = pixDown->getTone();
Shinya Kitaoka 120a6e
        int toneUp   = pixUp->getTone();
shun-iwasawa 3a1040
        if (usedInks.find(pixUp->getInk()) == usedInks.end()) {
shun-iwasawa 3a1040
          if (mergePalette)
shun-iwasawa 3a1040
            tryMergeInkOrAddColor(downPlt, matchPlt, pixUp->getInk(), usedInks);
shun-iwasawa 3a1040
          else
shun-iwasawa 3a1040
            addColor(downPlt, pixUp->getInk(), usedInks);
shun-iwasawa 3a1040
        }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        if (val == 1) {  // Matchline is on top, with gap
Shinya Kitaoka 120a6e
          pixDown->setTone(toneUp);
Shinya Kitaoka 120a6e
          pixDown->setInk(usedInks[pixUp->getInk()]);
Shinya Kitaoka 120a6e
        } else if (val == 0) {  // Matchline is on bottom, with gap
Shinya Kitaoka 120a6e
          if (pixDown->isPurePaint()) {
Shinya Kitaoka 120a6e
            pixDown->setTone(toneUp);
Shinya Kitaoka 120a6e
            pixDown->setInk(usedInks[pixUp->getInk()]);
Shinya Kitaoka 120a6e
          } else {
Shinya Kitaoka 120a6e
            //*pixOut = *pixDown;
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
        } else {
Shinya Kitaoka 120a6e
          if ((val > 0 && toneUp < toneDown) ||
Shinya Kitaoka 120a6e
              (val == 0 && toneDown == 255))  //(toneUp
Shinya Kitaoka 120a6e
            pixDown->setTone(toneUp);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          if ((255 - toneDown) * (1 - val) <=
Shinya Kitaoka 120a6e
              val * (255 - toneUp - 1))  // val==0 -> if (toneDown== 255)....
Shinya Kitaoka 120a6e
            // val==0.5 -> if (toneup
Shinya Kitaoka 120a6e
            // val==1 -> if (toneup<255)...
Shinya Kitaoka 120a6e
            pixDown->setInk(usedInks[pixUp->getInk()]);
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      int paintIndex;
Shinya Kitaoka 120a6e
      if (!onlyInks && (paintIndex = pixUp->getPaint()) > 0) {
Shinya Kitaoka 120a6e
        if (usedInks.find(paintIndex) == usedInks.end())
Shinya Kitaoka 120a6e
          addColor(downPlt, paintIndex, usedInks);
Shinya Kitaoka 120a6e
        pixDown->setPaint(usedInks[paintIndex]);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      pixUp++;
Shinya Kitaoka 120a6e
      pixDown++;
Shinya Kitaoka 120a6e
      // pixOut++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void addColors(const TPixelCM32 &color, TPaletteP plt, const TPaletteP &upPlt,
Shinya Kitaoka 120a6e
               std::map<int, int=""> &usedColors) {</int,>
Shinya Kitaoka 120a6e
  if (usedColors.find(color.getInk()) == usedColors.end())
Shinya Kitaoka 120a6e
    addColor(plt, color.getInk(), upPlt, usedColors);
Shinya Kitaoka 120a6e
  if (usedColors.find(color.getPaint()) == usedColors.end())
Shinya Kitaoka 120a6e
    addColor(plt, color.getPaint(), upPlt, usedColors);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Shinya Kitaoka 120a6e
void addColors(const TPixelCM32 &color, TPaletteP plt,
Shinya Kitaoka 120a6e
               std::map<int, int=""> &usedColors) {</int,>
Shinya Kitaoka 120a6e
  if (usedColors.find(color.getInk()) == usedColors.end())
Shinya Kitaoka 120a6e
    addColor(plt, color.getInk(), usedColors);
Shinya Kitaoka 120a6e
  if (usedColors.find(color.getPaint()) == usedColors.end())
Shinya Kitaoka 120a6e
    addColor(plt, color.getPaint(), usedColors);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void doApplyMatchLines(TRasterCM32P rasOut, const TRasterCM32P &rasUp,
Shinya Kitaoka 120a6e
                       int inkIndex, int matchlinePrevalence) {
Shinya Kitaoka 120a6e
  double val = matchlinePrevalence / 100.0;  // matchlinePrevalence ==0->always
Shinya Kitaoka 120a6e
                                             // ink down; matchlinePrevalence ==
Shinya Kitaoka 120a6e
                                             // 100 always ink up;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  assert(rasOut && rasUp);
Shinya Kitaoka 120a6e
  assert(rasOut->getSize() == rasUp->getSize());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (int y = 0; y < rasOut->getLy(); y++) {
Shinya Kitaoka 120a6e
    TPixelCM32 *pixDown = rasOut->pixels(y),
Shinya Kitaoka 120a6e
               *endPix  = pixDown + rasOut->getLx();
Shinya Kitaoka 120a6e
    TPixelCM32 *pixUp   = rasUp->pixels(y);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    while (pixDown < endPix) {
Shinya Kitaoka 120a6e
      if (!pixUp->isPurePaint()) {
Shinya Kitaoka 120a6e
        int toneDown = pixDown->getTone();
Shinya Kitaoka 120a6e
        int toneUp   = pixUp->getTone();
Shinya Kitaoka 120a6e
        if (val == 1) {  // Matchline is on top, with gap
Shinya Kitaoka 120a6e
          pixDown->setTone(toneUp);
Shinya Kitaoka 120a6e
          pixDown->setInk(inkIndex);
Shinya Kitaoka 120a6e
        } else if (val == 0) {  // Matchline is on bottom, with gap
Shinya Kitaoka 120a6e
          if (pixDown->isPurePaint()) {
Shinya Kitaoka 120a6e
            pixDown->setTone(toneUp);
Shinya Kitaoka 120a6e
            pixDown->setInk(inkIndex);
Shinya Kitaoka 120a6e
          } else {
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
          //*pixOut = *pixDown;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        if ((val > 0 && toneUp < toneDown) || (val == 0 && toneDown == 255))
Shinya Kitaoka 120a6e
          pixDown->setTone(toneUp);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        if ((255 - toneDown) * (1 - val) <=
Shinya Kitaoka 120a6e
            val * (255 - toneUp - 1))  // val==0 -> if (toneDown == 255)....
Shinya Kitaoka 120a6e
          // val==0.5 -> if (toneup
Shinya Kitaoka 120a6e
          // val==1 -> if (toneup<255)...
Shinya Kitaoka 120a6e
          pixDown->setInk(inkIndex);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      pixUp++;
Shinya Kitaoka 120a6e
      pixDown++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef LEVO
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TRop::eraseColors(TRasterCM32P ras, vector<int> &colorIds, bool eraseInks,</int>
Shinya Kitaoka 120a6e
                       bool keepColor) {
Shinya Kitaoka 120a6e
  assert(ras);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  vector<int> curColorIds;</int>
Shinya Kitaoka 120a6e
  std::sort(colorIds.begin(), colorIds.end());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!keepColor)
Shinya Kitaoka 120a6e
    curColorIds = colorIds;
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    // prendo tutti i colori ECCETTO quelli nel vettore colorIds
Shinya Kitaoka 120a6e
    unsigned int count1 = 0, count2 = 0;
Shinya Kitaoka 120a6e
    int size = eraseInks ? TPixelCM32::getMaxInk() : TPixelCM32::getMaxPaint();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    curColorIds.resize(size + 1 - colorIds.size());
Shinya Kitaoka 120a6e
    for (int i = 0; i < size; i++)
Shinya Kitaoka 120a6e
      if (count1 < colorIds.size() && colorIds[count1] == i)
Shinya Kitaoka 120a6e
        count1++;
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        curColorIds[count2++] = i;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (int y = 0; y < ras->getLy(); y++) {
Shinya Kitaoka 120a6e
    TPixelCM32 *pix = ras->pixels(y), *endPix = pix + ras->getLx();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    while (pix < endPix) {
Shinya Kitaoka 120a6e
      unsigned int i;
Shinya Kitaoka 120a6e
      int color = eraseInks ? pix->getInk() : pix->getPaint();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      if (color != 0)
Shinya Kitaoka 120a6e
        for (i = 0; i < curColorIds.size() && curColorIds[i] <= color; i++)
Shinya Kitaoka 120a6e
          if (color == curColorIds[i]) {
Shinya Kitaoka 120a6e
            *pix = eraseInks ? TPixelCM32(0, pix->getPaint(),
Shinya Kitaoka 120a6e
                                          TPixelCM32::getMaxTone())
Shinya Kitaoka 120a6e
                             : TPixelCM32(pix->getInk(), 0, pix->getTone());
Shinya Kitaoka 120a6e
            break;
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
      pix++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
shun-iwasawa 1a977b
namespace {
shun-iwasawa 1a977b
void expandAreaAndFillGaps(TRasterCM32P ras) {
shun-iwasawa 1a977b
  // expand neighbor pixels and fill the gaps
shun-iwasawa 1a977b
  while (1) {
shun-iwasawa 1a977b
    bool found = false;
shun-iwasawa 1a977b
    for (int y = 0; y < ras->getLy(); y++) {
shun-iwasawa 1a977b
      TPixelCM32 *pix   = ras->pixels(y);
shun-iwasawa 1a977b
      TPixelCM32 *upPix = (y == 0) ? ras->pixels(y) : ras->pixels(y - 1);
shun-iwasawa 1a977b
      TPixelCM32 *dnPix =
shun-iwasawa 1a977b
          (y == ras->getLy() - 1) ? ras->pixels(y) : ras->pixels(y + 1);
shun-iwasawa 1a977b
      for (int x = 0; x < ras->getLx(); x++, pix++, upPix++, dnPix++) {
shun-iwasawa 1a977b
        // pixels which were filled in the previous loop
shun-iwasawa 1a977b
        if (pix->getInk() == TPixelCM32::getMaxInk()) {
shun-iwasawa 1a977b
          pix->setInk(0);
shun-iwasawa 1a977b
          continue;
shun-iwasawa 1a977b
        }
shun-iwasawa 1a977b
        // pixels which are non-gap or already finished
shun-iwasawa 1a977b
        if (pix->getPaint() != TPixelCM32::getMaxPaint()) continue;
shun-iwasawa 1a977b
shun-iwasawa 1a977b
        int paint = TPixelCM32::getMaxPaint();
luz paz 6454c4
        // check the neighbor pixels and take the smallest styleId
shun-iwasawa 1a977b
        if (upPix != pix && upPix->getInk() != TPixelCM32::getMaxInk())
shun-iwasawa 1a977b
          paint = upPix->getPaint();
shun-iwasawa 1a977b
        if (dnPix != pix && dnPix->getInk() != TPixelCM32::getMaxInk())
shun-iwasawa 1a977b
          paint = std::min(paint, dnPix->getPaint());
shun-iwasawa 1a977b
        if (x != 0 && (pix - 1)->getInk() != TPixelCM32::getMaxInk())
shun-iwasawa 1a977b
          paint = std::min(paint, (pix - 1)->getPaint());
shun-iwasawa 1a977b
        if (x != ras->getLx() - 1 &&
shun-iwasawa 1a977b
            (pix + 1)->getInk() != TPixelCM32::getMaxInk())
shun-iwasawa 1a977b
          paint = std::min(paint, (pix + 1)->getPaint());
shun-iwasawa 1a977b
shun-iwasawa 1a977b
        if (paint != TPixelCM32::getMaxPaint()) {
shun-iwasawa 1a977b
          pix->setPaint(paint);
shun-iwasawa 1a977b
          // use inkId = 4095 as mark of newly-filled pixels
shun-iwasawa 1a977b
          // This pixel will be finalized in the next loop
shun-iwasawa 1a977b
          pix->setInk(TPixelCM32::getMaxInk());
shun-iwasawa 1a977b
        }
shun-iwasawa 1a977b
        found = true;
shun-iwasawa 1a977b
      }
shun-iwasawa 1a977b
    }
shun-iwasawa 1a977b
    if (!found) break;
shun-iwasawa 1a977b
  }
shun-iwasawa 1a977b
}
shun-iwasawa 1a977b
}  // namespace
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TRop::eraseColors(TRasterCM32P ras, std::vector<int> *colorIds,</int>
shun-iwasawa 1a977b
                       bool eraseInks, bool noGap) {
shun-iwasawa 1a977b
  // noGap option is available only when erasing Inks
shun-iwasawa 1a977b
  assert(eraseInks || !noGap);
Shinya Kitaoka 120a6e
  if (colorIds) std::sort(colorIds->begin(), colorIds->end());
Shinya Kitaoka 120a6e
shun-iwasawa 1a977b
  bool hasGapPixel = false;
shun-iwasawa 1a977b
Shinya Kitaoka 120a6e
  for (int y = 0; y < ras->getLy(); y++) {
Shinya Kitaoka 120a6e
    TPixelCM32 *pix = ras->pixels(y), *endPix = pix + ras->getLx();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    for (; pix < endPix; pix++) {
Shinya Kitaoka 120a6e
      unsigned int i = 0;
Shinya Kitaoka 120a6e
      int color      = eraseInks ? pix->getInk() : pix->getPaint();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      if (color == 0) continue;
Shinya Kitaoka 120a6e
shun-iwasawa 1a977b
      if (colorIds &&
shun-iwasawa 1a977b
          !std::binary_search(colorIds->begin(), colorIds->end(), color))
shun-iwasawa 1a977b
        continue;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      if (eraseInks) {
shun-iwasawa 1a977b
        // no-gap case.
shun-iwasawa 1a977b
        // even if some area is filled under the solid line, delete it and
shun-iwasawa 1a977b
        // expand-fill again to make sure the colors will be separated at the
shun-iwasawa 1a977b
        // center of the lines.
shun-iwasawa 1a977b
        if (noGap && pix->getTone() == 0) {
shun-iwasawa 1a977b
          pix->setPaint(
shun-iwasawa 1a977b
              TPixelCM32::getMaxPaint());  // use paintId = 4095 as mark of
shun-iwasawa 1a977b
                                           // pixels to be filled
shun-iwasawa 1a977b
          hasGapPixel = true;
shun-iwasawa 1a977b
        }
Shinya Kitaoka 120a6e
        pix->setInk(0);
Shinya Kitaoka 120a6e
        pix->setTone(TPixelCM32::getMaxTone());
Shinya Kitaoka 120a6e
      } else
Shinya Kitaoka 120a6e
        pix->setPaint(0);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
shun-iwasawa 1a977b
shun-iwasawa 1a977b
  if (hasGapPixel) expandAreaAndFillGaps(ras);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------
Toshihiro Shimizu 890ddd
/*
Shinya Kitaoka 120a6e
void TRop::overaCmapped(TRasterCM32P rasOut, const TRasterCM32P& rasUp, const
Shinya Kitaoka 120a6e
TPaletteP &pltOut, int matchlinePrevalence, std::map<int,int>& usedColors)</int,int>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
doMergeCmapped(rasOut, rasUp, pltOut, false, matchlinePrevalence, usedColors);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TRop::applyMatchLines(TRasterCM32P rasOut, const TRasterCM32P &rasUp,
shun-iwasawa 3a1040
                           const TPaletteP &pltOut, const TPaletteP &matchPlt,
shun-iwasawa 3a1040
                           int inkIndex, int matchlinePrevalence,
Shinya Kitaoka 120a6e
                           std::map<int, int=""> &usedInks) {</int,>
Shinya Kitaoka 120a6e
  assert(matchlinePrevalence >= 0);
Shinya Kitaoka 120a6e
  if (inkIndex == -1)
shun-iwasawa 3a1040
    doMergeCmapped(rasOut, rasUp, pltOut, matchPlt, true, matchlinePrevalence,
shun-iwasawa 3a1040
                   usedInks, false);
shun-iwasawa 3a1040
  else if (inkIndex == -2)
shun-iwasawa 3a1040
    doMergeCmapped(rasOut, rasUp, pltOut, matchPlt, true, matchlinePrevalence,
shun-iwasawa 3a1040
                   usedInks, true);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    doApplyMatchLines(rasOut, rasUp, inkIndex, matchlinePrevalence);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TRop::eraseStyleIds(TToonzImage *image, const std::vector<int> styleIds) {</int>
Shinya Kitaoka 120a6e
  assert(image);
Shinya Kitaoka 120a6e
  TRasterCM32P ras = image->getRaster();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)styleIds.size(); i++) {
Shinya Kitaoka 120a6e
    int styleId = styleIds[i];
Shinya Kitaoka 120a6e
    ras->lock();
Shinya Kitaoka 120a6e
    for (int y = 0; y < ras->getLy(); y++) {
Shinya Kitaoka 120a6e
      TPixelCM32 *pix = ras->pixels(y), *endPix = pix + ras->getLx();
Shinya Kitaoka 120a6e
      while (pix < endPix) {
Shinya Kitaoka 120a6e
        bool isPaint = (pix->getPaint() == styleId);
Shinya Kitaoka 120a6e
        bool isInk   = (pix->getInk() == styleId);
Shinya Kitaoka 120a6e
        if (!isPaint && !isInk) {
Shinya Kitaoka 120a6e
          pix++;
Shinya Kitaoka 120a6e
          continue;
Shinya Kitaoka 120a6e
        } else if (isPaint && !isInk)
Shinya Kitaoka 120a6e
          *pix = TPixelCM32(pix->getInk(), 0, pix->getTone());
Shinya Kitaoka 120a6e
        else if (!isPaint && isInk)
Shinya Kitaoka 120a6e
          *pix = TPixelCM32(0, pix->getPaint(), TPixelCM32::getMaxTone());
Shinya Kitaoka 120a6e
        else if (isPaint && isInk)
Shinya Kitaoka 120a6e
          *pix = TPixelCM32(0, 0, pix->getTone());
Shinya Kitaoka 120a6e
        else
Shinya Kitaoka 120a6e
          assert(0);
Shinya Kitaoka 120a6e
        pix++;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    ras->unlock();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
inline bool isTransparent(TPixelCM32 *pix) {
Shinya Kitaoka 120a6e
  return (((*((ULONG *)pix)) & 0x000fffff) == 0xff);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TRop::overlayCmapped(TRasterCM32P rasOut, const TRasterCM32P &rasUp,
Shinya Kitaoka 120a6e
                          const TPaletteP &pltOut, const TPaletteP &upPlt,
Shinya Kitaoka 120a6e
                          std::map<int, int=""> &usedColors) {</int,>
Shinya Kitaoka 120a6e
  assert(rasOut && rasUp);
Shinya Kitaoka 120a6e
  assert(rasOut->getSize() == rasUp->getSize());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TPaletteP downPlt = pltOut->clone();
Shinya Kitaoka 120a6e
  std::map<int, int="">::iterator it;</int,>
Shinya Kitaoka 120a6e
  for (it = usedColors.begin(); it != usedColors.end(); it++)
Shinya Kitaoka 120a6e
    downPlt->getPage(0)->addStyle(TPixel32::Red);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (int y = 0; y < rasOut->getLy(); y++) {
Shinya Kitaoka 120a6e
    TPixelCM32 *pixDown = rasOut->pixels(y);
Shinya Kitaoka 120a6e
    TPixelCM32 *pixUp   = rasUp->pixels(y);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    for (int x = 0; x < rasOut->getLx(); x++, pixUp++, pixDown++) {
Shinya Kitaoka 120a6e
      if (isTransparent(pixUp))  // WARNING! cannot check transparent pixels
Shinya Kitaoka 120a6e
                                 // with *pixup==TPixelCM32() since also
Shinya Kitaoka 120a6e
                                 // 0x183000ff i.e., is a valid transparent
Shinya Kitaoka 120a6e
                                 // value
Shinya Kitaoka 120a6e
        continue;
Shinya Kitaoka 120a6e
      int tone = pixUp->getTone();
Shinya Kitaoka 120a6e
      if (isTransparent(pixDown) || tone == 255 || tone == 0 ||
Shinya Kitaoka 120a6e
          pixUp->getPaint() !=
Shinya Kitaoka 120a6e
              0)  // up e' punto interno, o esterno non antialiasato
Shinya Kitaoka 120a6e
      {
Shinya Kitaoka 120a6e
        addColors(*pixUp, downPlt, upPlt, usedColors);
Shinya Kitaoka 120a6e
        pixDown->setInk(usedColors[pixUp->getInk()]);
Shinya Kitaoka 120a6e
        pixDown->setPaint(usedColors[pixUp->getPaint()]);
Shinya Kitaoka 120a6e
        pixDown->setTone(tone);
Shinya Kitaoka 120a6e
      } else if (tone <= pixDown->getTone() || tone < 128)  // up e' bordo
Shinya Kitaoka 120a6e
                                                            // esterno
Shinya Kitaoka 120a6e
                                                            // antialiasato piu'
Shinya Kitaoka 120a6e
                                                            // opaco di down
Shinya Kitaoka 120a6e
      {
Shinya Kitaoka 120a6e
        int ink = pixUp->getInk();
Shinya Kitaoka 120a6e
        if (usedColors.find(ink) == usedColors.end())
Shinya Kitaoka 120a6e
          addColor(downPlt, ink, upPlt, usedColors);
Shinya Kitaoka 120a6e
        pixDown->setInk(usedColors[ink]);
Shinya Kitaoka 120a6e
        pixDown->setTone(tone < 128 ? 0 : tone);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define MAGICFAC (257U * 256U + 1U)
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
#define PIX_CM32_PENMAP_IDX(PIX) ((PIX)>>12 & 0xfff00 | (PIX) & 0xff)
Toshihiro Shimizu 890ddd
#define PIX_CM32_COLMAP_IDX(PIX) ((PIX) & 0xfffff)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define PIX_CM32_PENMAP_COLMAP_TO_RGBM(PIX,PENMAP,COLMAP,RES) \
Toshihiro Shimizu 890ddd
  { \
Toshihiro Shimizu 890ddd
  ULONG _r = (PIX); \
Toshihiro Shimizu 890ddd
  ULONG _s = ((ULONG *)(PENMAP))[MY_PIX_CM32_PENMAP_IDX(_r)] + \
Toshihiro Shimizu 890ddd
             ((ULONG *)(COLMAP))[MY_PIX_CM32_COLMAP_IDX(_r)]; \
Toshihiro Shimizu 890ddd
  (RES) = *(LPIXEL *)(&_s); \
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
void fillCmapRamp (RAS_CMAP& cmap, const TPixel32& color, int index)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
index = index << cmap.info.tone_bits;
Toshihiro Shimizu 890ddd
int xedni = index + cmap.info.n_tones-1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPixel32 _color=color;
Shinya Kitaoka 120a6e
        UINT fac = MAGICFAC * _color.m;
Shinya Kitaoka 120a6e
        _color.r=(UINT)(_color.r * fac + (1U<<23))>>24;
Shinya Kitaoka 120a6e
        _color.b=(UINT)(_color.b * fac + (1U<<23))>>24;
Shinya Kitaoka 120a6e
        _color.g=(UINT)(_color.g * fac + (1U<<23))>>24;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
for (int tone = 0; tone < cmap.info.n_tones; tone++)
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
  LPIXEL val;
Toshihiro Shimizu 890ddd
  UINT magic_tone = tone * MAGICFAC;
Toshihiro Shimizu 890ddd
  val.r = (UCHAR)((_color.r * magic_tone + (1<<23)) >> 24);
Toshihiro Shimizu 890ddd
  val.g = (UCHAR)((_color.g * magic_tone + (1<<23)) >> 24);
Toshihiro Shimizu 890ddd
  val.b = (UCHAR)((_color.b * magic_tone + (1<<23)) >> 24);
Toshihiro Shimizu 890ddd
  val.m = (UCHAR)((_color.m * magic_tone + (1<<23)) >> 24);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  cmap.colbuffer[index++] = val;
Toshihiro Shimizu 890ddd
  cmap.penbuffer[xedni--] = val;
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// \b NOTE: Starting from Toonz 6.1, some important improvements are introduced:
Shinya Kitaoka 120a6e
// a) The passed raster is now referenced by the returned _RASTER*, just the
Shinya Kitaoka 120a6e
// same way
Toshihiro Shimizu 890ddd
//    smartpointer to rasters do.
Shinya Kitaoka 120a6e
// b) The cache is made aware of the passed raster, mainly because these old 4.6
Shinya Kitaoka 120a6e
// raster
Shinya Kitaoka 120a6e
//    structures are essentially used for memory-consuming operations with tlv
Shinya Kitaoka 120a6e
//    fxs and may
Shinya Kitaoka 120a6e
//    need to be shipped to hard disk on critical situations (a matter handled
Shinya Kitaoka 120a6e
//    by the cache).
Shinya Kitaoka 120a6e
// c) The lockRaster and unlockRaster functions are provided. They are meant to
Shinya Kitaoka 120a6e
// specify whether
Shinya Kitaoka 120a6e
//    the raster is actively referenced by the raster pointer, or is rather in a
Shinya Kitaoka 120a6e
//    lazy state -
Toshihiro Shimizu 890ddd
//    so that the cache may move it to hard disk if necessary.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
static const TCM_INFO Tcm_my_default_info  = {  8,  8, 12,  20, 12,
Toshihiro Shimizu 890ddd
                                               0x0000, 0x00ff,
Toshihiro Shimizu 890ddd
                                               256, 4096, 256
Toshihiro Shimizu 890ddd
                                             };*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
inline LPIXEL premultiplyLPIXEL(const TPixel32 &pix) {
Shinya Kitaoka 120a6e
  // int MAGICFAC = (257U * 256U + 1U);
Shinya Kitaoka 120a6e
  UINT fac = MAGICFAC * pix.m;
Shinya Kitaoka 120a6e
  LPIXEL out;
Shinya Kitaoka 120a6e
  out.r = ((UINT)(pix.r * fac + (1U << 23)) >> 24);
Shinya Kitaoka 120a6e
  out.g = ((UINT)(pix.g * fac + (1U << 23)) >> 24);
Shinya Kitaoka 120a6e
  out.b = ((UINT)(pix.b * fac + (1U << 23)) >> 24);
Shinya Kitaoka 120a6e
  out.m = pix.m;
Shinya Kitaoka 120a6e
  return out;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
_RASTER *TRop::convertRaster50to46(const TRasterP &inRas,
Shinya Kitaoka 120a6e
                                   const TPaletteP &inPalette) {
Shinya Kitaoka 120a6e
  int lx   = inRas->getLx();
Shinya Kitaoka 120a6e
  int ly   = inRas->getLy();
Shinya Kitaoka 120a6e
  int wrap = inRas->getWrap();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  assert(lx > 0 && ly > 0);
Shinya Kitaoka 120a6e
  assert(inRas->getRawData());
Shinya Kitaoka 120a6e
  TRasterGR8P rgr8   = (TRasterGR8P)inRas;
Shinya Kitaoka 120a6e
  TRasterGR16P rgr16 = (TRasterGR16P)inRas;
Shinya Kitaoka 120a6e
  TRaster32P r32     = (TRaster32P)inRas;
Shinya Kitaoka 120a6e
  TRaster64P r64     = (TRaster64P)inRas;
Shinya Kitaoka 120a6e
  TRasterCM32P rcm   = (TRasterCM32P)inRas;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  RASTER *rout = new RASTER;
Shinya Kitaoka 120a6e
  memset(rout, 0, sizeof(RASTER));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::string id(TImageCache::instance()->getUniqueId());
Shinya Kitaoka 120a6e
  rout->cacheIdLength = id.size();
Shinya Kitaoka 120a6e
  rout->cacheId       = new char[rout->cacheIdLength];
Shinya Kitaoka 120a6e
  memcpy(rout->cacheId, id.data(), sizeof(char) * rout->cacheIdLength);
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    TImageP img;
Shinya Kitaoka 120a6e
    if (rcm)
Shinya Kitaoka 120a6e
      img = TToonzImageP(rcm, rcm->getBounds());  // saveBox is not considered
Shinya Kitaoka 120a6e
                                                  // in RASTER struct anyway
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      img = TRasterImageP(inRas);
Shinya Kitaoka 120a6e
    TImageCache::instance()->add(id, img);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  inRas->addRef();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  rout->buffer        = inRas->getRawData();
Shinya Kitaoka 120a6e
  TRasterP parent     = inRas->getParent();
Shinya Kitaoka 120a6e
  rout->native_buffer = (parent) ? parent->getRawData() : inRas->getRawData();
Shinya Kitaoka 120a6e
  rout->lx            = lx;
Shinya Kitaoka 120a6e
  rout->ly            = ly;
Shinya Kitaoka 120a6e
  rout->wrap          = wrap;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (rgr8)
Shinya Kitaoka 120a6e
    rout->type = RAS_GR8;
Shinya Kitaoka 120a6e
  else if (rgr16)
Shinya Kitaoka 120a6e
    rout->type = RAS_GR16;
Shinya Kitaoka 120a6e
  else if (r32)
Shinya Kitaoka 120a6e
    rout->type = RAS_RGBM;
Shinya Kitaoka 120a6e
  else if (r64)
Shinya Kitaoka 120a6e
    rout->type = RAS_RGBM64;
Shinya Kitaoka 120a6e
  else if (rcm)
Shinya Kitaoka 120a6e
    rout->type = RAS_CM32;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    assert(!"raster type not convertible!");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (rout->type != RAS_CM32) return rout;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!inPalette) {
Shinya Kitaoka 120a6e
    assert(!"missing palette!");
Shinya Kitaoka 120a6e
    return NULL;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  rout->cmap.info   = Tcm_32_default_info;
Shinya Kitaoka 120a6e
  rout->cmap.buffer = new LPIXEL[TCM_CMAP_PENBUFFER_SIZE(rout->cmap.info)];
Shinya Kitaoka 120a6e
  // rout->cmap.penbuffer = new LPIXEL[TCM_CMAP_PENBUFFER_SIZE
Shinya Kitaoka 120a6e
  // (rout->cmap.info)];
Shinya Kitaoka 120a6e
  // rout->cmap.colbuffer = new LPIXEL[TCM_CMAP_COLBUFFER_SIZE
Shinya Kitaoka 120a6e
  // (rout->cmap.info)];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (int i = 0; i < inPalette->getStyleCount(); i++)
Shinya Kitaoka 120a6e
    rout->cmap.buffer[i] =
Shinya Kitaoka 120a6e
        ::premultiplyLPIXEL(inPalette->getStyle(i)->getAverageColor());
Shinya Kitaoka 120a6e
  // fillCmapRamp (rout->cmap, inPalette->getStyle(i)->getMainColor(), i);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return rout;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TRop::releaseRaster46(_RASTER *&r, bool doReleaseBuffer) {
Shinya Kitaoka 120a6e
  // Buffer release no more supported. These are now intended as smart
Shinya Kitaoka 120a6e
  // pointers to rasters - they release themselves on their own.
Shinya Kitaoka 120a6e
  assert(!doReleaseBuffer);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (r->type == RAS_CM32) {
Shinya Kitaoka 120a6e
    delete[] r->cmap.buffer;
Shinya Kitaoka 120a6e
    // delete [] r->cmap.penbuffer;
Shinya Kitaoka 120a6e
    // delete [] r->cmap.colbuffer;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (doReleaseBuffer && r->native_buffer == r->buffer)
Shinya Kitaoka 120a6e
    delete r->buffer;  // Should not happen
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Unlock if locked, and remove the cache reference
Shinya Kitaoka 120a6e
  if (r->buffer) unlockRaster(r);
Shinya Kitaoka 120a6e
  TImageCache::instance()->remove(std::string(r->cacheId, r->cacheIdLength));
Shinya Kitaoka 120a6e
  delete[] r->cacheId;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  delete r;
Shinya Kitaoka 120a6e
  r = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TRop::lockRaster(RASTER *raster) {
Shinya Kitaoka 120a6e
  TImageP img(TImageCache::instance()->get(
Shinya Kitaoka 120a6e
      std::string(raster->cacheId, raster->cacheIdLength), true));
Shinya Kitaoka 120a6e
  TRasterP cacheRas;
Shinya Kitaoka 120a6e
  if (raster->type == RAS_CM32)
Shinya Kitaoka 120a6e
    cacheRas = TToonzImageP(img)->getRaster();
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    cacheRas = TRasterImageP(img)->getRaster();
Shinya Kitaoka 120a6e
  cacheRas->addRef();
Shinya Kitaoka 120a6e
  raster->buffer  = cacheRas->getRawData();
Shinya Kitaoka 120a6e
  TRasterP parent = cacheRas->getParent();
Shinya Kitaoka 120a6e
  raster->native_buffer =
Shinya Kitaoka 120a6e
      (parent) ? parent->getRawData() : cacheRas->getRawData();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TRop::unlockRaster(RASTER *raster) {
Shinya Kitaoka 120a6e
  TImageP img(TImageCache::instance()->get(
Shinya Kitaoka 120a6e
      std::string(raster->cacheId, raster->cacheIdLength), true));
Shinya Kitaoka 120a6e
  TRasterP cacheRas;
Shinya Kitaoka 120a6e
  if (raster->type == RAS_CM32)
Shinya Kitaoka 120a6e
    cacheRas = TToonzImageP(img)->getRaster();
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    cacheRas = TRasterImageP(img)->getRaster();
Shinya Kitaoka 120a6e
  cacheRas->release();
Shinya Kitaoka 120a6e
  raster->buffer        = 0;
Shinya Kitaoka 120a6e
  raster->native_buffer = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
_RASTER *TRop::readRaster46(const char *filename) {
Shinya Kitaoka 120a6e
  // No more called in Toonz...
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TImageP img;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TImageReader::load(TFilePath(filename), img);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if ((TToonzImageP)img) return 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRasterImageP rimg = (TRasterImageP)img;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!rimg) return 0;
Shinya Kitaoka 120a6e
  TRasterP ras = rimg->getRaster();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return TRop::convertRaster50to46(ras, 0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline TPixel32 getPix32(const TPixelCM32 &pixcm,
Shinya Kitaoka 120a6e
                         const std::vector<tpixel32> &colors) {</tpixel32>
Shinya Kitaoka 120a6e
  int t = pixcm.getTone();
Shinya Kitaoka 120a6e
  int p = pixcm.getPaint();
Shinya Kitaoka 120a6e
  int i = pixcm.getInk();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (t == TPixelCM32::getMaxTone())
Shinya Kitaoka 120a6e
    return colors[p];
Shinya Kitaoka 120a6e
  else if (t == 0)
Shinya Kitaoka 120a6e
    return colors[i];
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return blend(colors[i], colors[p], t, TPixelCM32::getMaxTone());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// from 4.6
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef LEVO
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TRop::zoomOutCm32Rgbm(const TRasterCM32P &rin, TRaster32P &rout,
Shinya Kitaoka 120a6e
                           const TPalette &plt, int x1, int y1, int x2, int y2,
Shinya Kitaoka 120a6e
                           int newx, int newy, int absZoomLevel) {
Shinya Kitaoka 120a6e
  TPixelCM32 *rowin, *pixin, *in, win;
Shinya Kitaoka 120a6e
  TPixel32 *rowout, *pixout, valin, valout;
Shinya Kitaoka 120a6e
  int tmp_r, tmp_g, tmp_b, tmp_m;
Shinya Kitaoka 120a6e
  int wrapin, wrapout;
Shinya Kitaoka 120a6e
  int x, y, lx, ly, xlast, ylast, xrest, yrest, i, j;
Shinya Kitaoka 120a6e
  int factor, fac_fac_2_bits;
Shinya Kitaoka 120a6e
  int fac_fac, yrest_fac, fac_xrest, yrest_xrest;
Shinya Kitaoka 120a6e
  int fac_fac_2, yrest_fac_2, fac_xrest_2, yrest_xrest_2;
Shinya Kitaoka 120a6e
  int fac_fac_4;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int count = plt.getStyleCount();
Shinya Kitaoka 120a6e
  int count2 =
Shinya Kitaoka 120a6e
      std::max(count, TPixelCM32::getMaxInk(), TPixelCM32::getMaxPaint());
Shinya Kitaoka 120a6e
  std::vector<tpixel32> colors(count2);</tpixel32>
shun-iwasawa 1a977b
  for (i = 0; i < plt.getStyleCount(); i++)
Shinya Kitaoka 120a6e
    colors[i] = ::premultiply(plt.getStyle(i)->getAverageColor());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  lx             = x2 - x1 + 1;
Shinya Kitaoka 120a6e
  ly             = y2 - y1 + 1;
Shinya Kitaoka 120a6e
  factor         = 1 << absZoomLevel;
Shinya Kitaoka 120a6e
  xrest          = lx & (factor - 1);
Shinya Kitaoka 120a6e
  yrest          = ly & (factor - 1);
Shinya Kitaoka 120a6e
  xlast          = x2 - xrest + 1;
Shinya Kitaoka 120a6e
  ylast          = y2 - yrest + 1;
Shinya Kitaoka 120a6e
  fac_fac        = factor * factor;
Shinya Kitaoka 120a6e
  fac_fac_2      = fac_fac >> 1;
Shinya Kitaoka 120a6e
  fac_fac_4      = fac_fac >> 2;
Shinya Kitaoka 120a6e
  fac_fac_2_bits = 2 * absZoomLevel - 1;
Shinya Kitaoka 120a6e
  yrest_fac      = yrest * factor;
Shinya Kitaoka 120a6e
  yrest_fac_2    = yrest_fac >> 1;
Shinya Kitaoka 120a6e
  fac_xrest      = factor * xrest;
Shinya Kitaoka 120a6e
  fac_xrest_2    = fac_xrest >> 1;
Shinya Kitaoka 120a6e
  yrest_xrest    = yrest * xrest;
Shinya Kitaoka 120a6e
  yrest_xrest_2  = yrest_xrest >> 1;
Shinya Kitaoka 120a6e
  wrapin         = rin->getWrap();
Shinya Kitaoka 120a6e
  wrapout        = rout->getWrap();
Shinya Kitaoka 120a6e
  valout.m       = 0xff;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  rowin  = (TPixelCM32 *)rin->getRawData() + wrapin * y1 + x1;
Shinya Kitaoka 120a6e
  rowout = (TPixel32 *)rout->getRawData() + wrapout * newy + newx;
Shinya Kitaoka 120a6e
  for (y = y1; y < ylast; y += factor) {
Shinya Kitaoka 120a6e
    pixin  = rowin;
Shinya Kitaoka 120a6e
    pixout = rowout;
Shinya Kitaoka 120a6e
    for (x = x1; x < xlast; x += factor) {
Shinya Kitaoka 120a6e
      tmp_r = tmp_g = tmp_b = tmp_m = 0;
Shinya Kitaoka 120a6e
      in                            = pixin;
Shinya Kitaoka 120a6e
      for (j = 0; j < factor; j += 2) {
Shinya Kitaoka 120a6e
        for (i = 0; i < factor; i += 2) {
Shinya Kitaoka 120a6e
          win   = *in;
Shinya Kitaoka 120a6e
          valin = getPix32(win, colors);
Shinya Kitaoka 120a6e
          tmp_r += valin.r;
Shinya Kitaoka 120a6e
          tmp_g += valin.g;
Shinya Kitaoka 120a6e
          tmp_b += valin.b;
Shinya Kitaoka 120a6e
          tmp_m += valin.m;
Shinya Kitaoka 120a6e
          in += wrapin;
Shinya Kitaoka 120a6e
          in++;
Shinya Kitaoka 120a6e
          win   = *in;
Shinya Kitaoka 120a6e
          valin = getPix32(win, colors);
Shinya Kitaoka 120a6e
          tmp_r += valin.r;
Shinya Kitaoka 120a6e
          tmp_g += valin.g;
Shinya Kitaoka 120a6e
          tmp_b += valin.b;
Shinya Kitaoka 120a6e
          tmp_m += valin.m;
Shinya Kitaoka 120a6e
          in -= wrapin;
Shinya Kitaoka 120a6e
          in++;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        in += 2 * wrapin - factor;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      valout.r  = (tmp_r + fac_fac_4) >> fac_fac_2_bits;
Shinya Kitaoka 120a6e
      valout.g  = (tmp_g + fac_fac_4) >> fac_fac_2_bits;
Shinya Kitaoka 120a6e
      valout.b  = (tmp_b + fac_fac_4) >> fac_fac_2_bits;
Shinya Kitaoka 120a6e
      valout.m  = (tmp_m + fac_fac_4) >> fac_fac_2_bits;
Shinya Kitaoka 120a6e
      *pixout++ = valout;
Shinya Kitaoka 120a6e
      pixin += factor;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (xrest) {
Shinya Kitaoka 120a6e
      tmp_r = tmp_g = tmp_b = tmp_m = 0;
Shinya Kitaoka 120a6e
      for (j = 0; j < factor; j++)
Shinya Kitaoka 120a6e
        for (i = 0; i < xrest; i++) {
Shinya Kitaoka 120a6e
          win   = pixin[i + j * wrapin];
Shinya Kitaoka 120a6e
          valin = getPix32(win, colors);
Shinya Kitaoka 120a6e
          tmp_r += valin.r;
Shinya Kitaoka 120a6e
          tmp_g += valin.g;
Shinya Kitaoka 120a6e
          tmp_b += valin.b;
Shinya Kitaoka 120a6e
          tmp_m += valin.m;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      valout.r = (tmp_r + fac_xrest_2) / fac_xrest;
Shinya Kitaoka 120a6e
      valout.g = (tmp_g + fac_xrest_2) / fac_xrest;
Shinya Kitaoka 120a6e
      valout.b = (tmp_b + fac_xrest_2) / fac_xrest;
Shinya Kitaoka 120a6e
      valout.m = (tmp_m + fac_xrest_2) / fac_xrest;
Shinya Kitaoka 120a6e
      *pixout  = valout;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    rowin += wrapin * factor;
Shinya Kitaoka 120a6e
    rowout += wrapout;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (yrest) {
Shinya Kitaoka 120a6e
    pixin  = rowin;
Shinya Kitaoka 120a6e
    pixout = rowout;
Shinya Kitaoka 120a6e
    for (x = x1; x < xlast; x += factor) {
Shinya Kitaoka 120a6e
      tmp_r = tmp_g = tmp_b = tmp_m = 0;
Shinya Kitaoka 120a6e
      for (j = 0; j < yrest; j++)
Shinya Kitaoka 120a6e
        for (i = 0; i < factor; i++) {
Shinya Kitaoka 120a6e
          win   = pixin[i + j * wrapin];
Shinya Kitaoka 120a6e
          valin = getPix32(win, colors);
Shinya Kitaoka 120a6e
          tmp_r += valin.r;
Shinya Kitaoka 120a6e
          tmp_g += valin.g;
Shinya Kitaoka 120a6e
          tmp_b += valin.b;
Shinya Kitaoka 120a6e
          tmp_m += valin.m;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      valout.r  = (tmp_r + yrest_fac_2) / yrest_fac;
Shinya Kitaoka 120a6e
      valout.g  = (tmp_g + yrest_fac_2) / yrest_fac;
Shinya Kitaoka 120a6e
      valout.b  = (tmp_b + yrest_fac_2) / yrest_fac;
Shinya Kitaoka 120a6e
      valout.m  = (tmp_m + yrest_fac_2) / yrest_fac;
Shinya Kitaoka 120a6e
      *pixout++ = valout;
Shinya Kitaoka 120a6e
      pixin += factor;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (xrest) {
Shinya Kitaoka 120a6e
      tmp_r = tmp_g = tmp_b = tmp_m = 0;
Shinya Kitaoka 120a6e
      for (j = 0; j < yrest; j++)
Shinya Kitaoka 120a6e
        for (i = 0; i < xrest; i++) {
Shinya Kitaoka 120a6e
          win   = pixin[i + j * wrapin];
Shinya Kitaoka 120a6e
          valin = getPix32(win, colors);
Shinya Kitaoka 120a6e
          tmp_r += valin.r;
Shinya Kitaoka 120a6e
          tmp_g += valin.g;
Shinya Kitaoka 120a6e
          tmp_b += valin.b;
Shinya Kitaoka 120a6e
          tmp_m += valin.m;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      valout.r = (tmp_r + yrest_xrest_2) / yrest_xrest;
Shinya Kitaoka 120a6e
      valout.g = (tmp_g + yrest_xrest_2) / yrest_xrest;
Shinya Kitaoka 120a6e
      valout.b = (tmp_b + yrest_xrest_2) / yrest_xrest;
Shinya Kitaoka 120a6e
      valout.m = (tmp_m + yrest_xrest_2) / yrest_xrest;
Shinya Kitaoka 120a6e
      *pixout  = valout;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef LEVO
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TRop::makeIcon(TRaster32P &_rout, const TRasterCM32P &rin,
Shinya Kitaoka 120a6e
                    const TPaletteP &palette, bool onBlackBg) {
Shinya Kitaoka 120a6e
  int i, j;
Shinya Kitaoka 120a6e
  int lx = rin->getLx();
Shinya Kitaoka 120a6e
  int ly = rin->getLy();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int count = palette->getStyleCount();
Shinya Kitaoka 120a6e
  int count2 =
Shinya Kitaoka 120a6e
      std::max(count, TPixelCM32::getMaxInk(), TPixelCM32::getMaxPaint());
Shinya Kitaoka 120a6e
  std::vector<tpixel32> colors(count2);</tpixel32>
Shinya Kitaoka 120a6e
  for (i = 0; i < palette->getStyleCount(); i++)
Shinya Kitaoka 120a6e
    colors[i] =
Shinya Kitaoka 120a6e
        /*::premultiply(*/ palette->getStyle(i)->getAverageColor();  //);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TDimension dim(_rout->getSize());
Shinya Kitaoka 120a6e
  TRaster32P rout;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double arIn  = (double)lx / ly;
Shinya Kitaoka 120a6e
  double arOut = (double)dim.lx / dim.ly;
Shinya Kitaoka 120a6e
  if (!areAlmostEqual(arIn, arOut, 1e-2))  // do not want a stretched icon! I
Shinya Kitaoka 120a6e
                                           // extract a subraster with same
Shinya Kitaoka 120a6e
                                           // aspect ration of rin
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    int newlx, newly;
Shinya Kitaoka 120a6e
    if (arOut < arIn) {
Shinya Kitaoka 120a6e
      newlx = dim.lx;
Shinya Kitaoka 120a6e
      newly = dim.lx / arIn;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      newly = dim.ly;
Shinya Kitaoka 120a6e
      newlx = dim.ly * arIn;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    _rout->clear();
Shinya Kitaoka 120a6e
    rout = _rout->extract((dim.lx - newlx) / 2, (dim.ly - newly) / 2,
Shinya Kitaoka 120a6e
                          (dim.lx - newlx) / 2 + newlx - 1,
Shinya Kitaoka 120a6e
                          (dim.ly - newly) / 2 + newly - 1);
shun-iwasawa 1a977b
    dim  = rout->getSize();
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    rout = _rout;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TPixel32 *pixOut0 = (TPixel32 *)rout->getRawData();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int countY   = 0;
Shinya Kitaoka 120a6e
  bool newRow  = true;
Shinya Kitaoka 120a6e
  int currTone = TPixelCM32::getMaxTone();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = 0; i < ly; i++) {
Shinya Kitaoka 120a6e
    TPixelCM32 *pixIn = (TPixelCM32 *)rin->pixels(i);
Shinya Kitaoka 120a6e
    TPixel32 *pixOut  = pixOut0;
Shinya Kitaoka 120a6e
    int countX        = 0;
Shinya Kitaoka 120a6e
    for (j = 0; j < lx; j++) {
Shinya Kitaoka 120a6e
      if (newRow || currTone > pixIn->getTone()) {
Shinya Kitaoka 120a6e
        currTone = pixIn->getTone();
Shinya Kitaoka 120a6e
        if (onBlackBg)
Shinya Kitaoka 120a6e
          *pixOut = overPixOnBlack(getPix32(*pixIn, colors));
Shinya Kitaoka 120a6e
        else
Shinya Kitaoka 120a6e
          *pixOut = overPixOnWhite(getPix32(*pixIn, colors));
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      pixIn++;
Shinya Kitaoka 120a6e
      countX += dim.lx;
Shinya Kitaoka 120a6e
      if (countX >= lx)
Shinya Kitaoka 120a6e
        countX -= lx, pixOut++, currTone = TPixelCM32::getMaxTone();
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    countY += dim.ly;
Shinya Kitaoka 120a6e
    if (countY >= ly) {
Shinya Kitaoka 120a6e
      countY -= ly;
Shinya Kitaoka 120a6e
      pixOut0 += rout->getWrap();
Shinya Kitaoka 120a6e
      currTone = TPixelCM32::getMaxTone();
Shinya Kitaoka 120a6e
      newRow   = true;
Shinya Kitaoka 120a6e
    } else
Shinya Kitaoka 120a6e
      newRow = false;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TRop::makeIcon(TRasterCM32P &_rout, const TRasterCM32P &rin) {
Shinya Kitaoka 120a6e
  int i, j;
Shinya Kitaoka 120a6e
  int lx = rin->getLx();
Shinya Kitaoka 120a6e
  int ly = rin->getLy();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TDimension dim(_rout->getSize());
Shinya Kitaoka 120a6e
  TRasterCM32P rout;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  rout = _rout;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  rout->lock();
Shinya Kitaoka 120a6e
  rin->lock();
Shinya Kitaoka 120a6e
  TPixelCM32 *pixOut0 = (TPixelCM32 *)rout->getRawData();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int countY  = 0;
Shinya Kitaoka 120a6e
  bool newRow = true;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = 0; i < ly; i++) {
Shinya Kitaoka 120a6e
    TPixelCM32 *pixIn  = (TPixelCM32 *)rin->pixels(i);
Shinya Kitaoka 120a6e
    TPixelCM32 *pixOut = pixOut0;
Shinya Kitaoka 120a6e
    int countX         = 0;
Shinya Kitaoka 120a6e
    bool newColumn     = true;
Shinya Kitaoka 120a6e
    for (j = 0; j < lx; j++) {
Shinya Kitaoka 120a6e
      if ((newRow && newColumn) || pixOut->getTone() > pixIn->getTone())
Shinya Kitaoka 120a6e
        *pixOut = *pixIn;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      pixIn++;
Shinya Kitaoka 120a6e
      countX += dim.lx;
Shinya Kitaoka 120a6e
      if (countX >= lx) {
Shinya Kitaoka 120a6e
        countX -= lx, pixOut++;
Shinya Kitaoka 120a6e
        newColumn = true;
Shinya Kitaoka 120a6e
      } else
Shinya Kitaoka 120a6e
        newColumn = false;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    countY += dim.ly;
Shinya Kitaoka 120a6e
    if (countY >= ly) {
Shinya Kitaoka 120a6e
      countY -= ly;
Shinya Kitaoka 120a6e
      pixOut0 += rout->getWrap();
Shinya Kitaoka 120a6e
      newRow = true;
Shinya Kitaoka 120a6e
    } else
Shinya Kitaoka 120a6e
      newRow = false;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  rout->unlock();
Shinya Kitaoka 120a6e
  rin->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*-----------------------------------------------------------------------------*/