Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tools/stylepicker.h"
Toshihiro Shimizu 890ddd
#include "tcolorstyles.h"
Toshihiro Shimizu 890ddd
#include "tstroke.h"
Toshihiro Shimizu 890ddd
#include "tofflinegl.h"
Toshihiro Shimizu 890ddd
#include "tvectorrenderdata.h"
Toshihiro Shimizu 890ddd
#include "tvectorimage.h"
Toshihiro Shimizu 890ddd
#include "ttoonzimage.h"
Toshihiro Shimizu 890ddd
#include "trasterimage.h"
Toshihiro Shimizu 890ddd
#include "toonz/dpiscale.h"
Toshihiro Shimizu 890ddd
#include "tpixelutils.h"
Toshihiro Shimizu 890ddd
#include "tregion.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
StylePicker::StylePicker(const TImageP &image)
Shinya Kitaoka 120a6e
    : m_image(image), m_palette(image->getPalette()) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
StylePicker::StylePicker(const TImageP &image, const TPaletteP &palette)
Shinya Kitaoka 120a6e
    : m_image(image), m_palette(palette) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TPoint StylePicker::getRasterPoint(const TPointD &p) const {
Shinya Kitaoka 120a6e
  if (TToonzImageP ti = m_image) {
Shinya Kitaoka 120a6e
    // DpiScale dpiScale(ti);
Shinya Kitaoka 120a6e
    TDimension size = ti->getSize();
Shinya Kitaoka 120a6e
    return TPoint(tround(0.5 * size.lx + p.x),   /// dpiScale.getSx()),
Shinya Kitaoka 120a6e
                  tround(0.5 * size.ly + p.y));  /// dpiScale.getSy()));
Shinya Kitaoka 120a6e
  } else if (TRasterImageP ri = m_image) {
Shinya Kitaoka 120a6e
    // DpiScale dpiScale(ri);
Shinya Kitaoka 120a6e
    TDimension size = ri->getRaster()->getSize();
Shinya Kitaoka 120a6e
    return TPoint(tround(0.5 * size.lx + p.x),   // /dpiScale.getSx()),
Shinya Kitaoka 120a6e
                  tround(0.5 * size.ly + p.y));  // /dpiScale.getSy()));
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    return TPoint(tround(p.x), tround(p.y));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Shinya Kitaoka 120a6e
/*-- (StylePickerTool内で)LineとAreaを切り替えてPickできる。mode: 0=Area,
Shinya Kitaoka 120a6e
 * 1=Line, 2=Line&Areas(default)  --*/
Shinya Kitaoka 120a6e
int StylePicker::pickStyleId(const TPointD &pos, double radius2,
Shinya Kitaoka 120a6e
                             int mode) const {
Shinya Kitaoka 120a6e
  int styleId = 0;
Shinya Kitaoka 120a6e
  if (TToonzImageP ti = m_image) {
Shinya Kitaoka 120a6e
    TRasterCM32P ras = ti->getRaster();
Shinya Kitaoka 120a6e
    TPoint point     = getRasterPoint(pos);
Shinya Kitaoka 120a6e
    if (!ras->getBounds().contains(point)) return -1;
Shinya Kitaoka 120a6e
    TPixelCM32 col = ras->pixels(point.y)[point.x];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    switch (mode) {
Shinya Kitaoka 120a6e
    case 0:  // AREAS
Shinya Kitaoka 120a6e
      styleId = col.getPaint();
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case 1:  // LINES
Shinya Kitaoka 120a6e
      styleId = col.getInk();
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case 2:  // ALL (Line & Area)
Shinya Kitaoka 120a6e
    default:
Shinya Kitaoka 120a6e
      styleId = col.isPurePaint() ? col.getPaint() : col.getInk();
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else if (TRasterImageP ri = m_image) {
Shinya Kitaoka 120a6e
    const TPalette *palette = m_palette.getPointer();
Shinya Kitaoka 120a6e
    if (!palette) return -1;
Shinya Kitaoka 120a6e
    TRaster32P ras = ri->getRaster();
Shinya Kitaoka 120a6e
    if (!ras) return -1;
Shinya Kitaoka 120a6e
    TPoint point = getRasterPoint(pos);
Shinya Kitaoka 120a6e
    if (!ras->getBounds().contains(point)) return -1;
Shinya Kitaoka 120a6e
    TPixel32 col = ras->pixels(point.y)[point.x];
Shinya Kitaoka 120a6e
    styleId      = palette->getClosestStyle(col);
Shinya Kitaoka 120a6e
  } else if (TVectorImageP vi = m_image) {
Shinya Kitaoka 120a6e
    // prima cerca lo stile della regione piu' vicina
Shinya Kitaoka 120a6e
    TRegion *r     = vi->getRegion(pos);
Shinya Kitaoka 120a6e
    if (r) styleId = r->getStyle();
Shinya Kitaoka 120a6e
    // poi cerca quello della stroke, ma se prima aveva trovato una regione,
Shinya Kitaoka 120a6e
    // richiede che
Shinya Kitaoka 120a6e
    // il click sia proprio sopra la stroke, altrimenti cerca la stroke piu'
Shinya Kitaoka 120a6e
    // vicina (max circa 10 pixel)
Shinya Kitaoka 120a6e
    const double maxDist2 = (styleId == 0) ? 100.0 * radius2 : 0;
Shinya Kitaoka 120a6e
    bool strokeFound;
Shinya Kitaoka 120a6e
    double dist2, w, thick;
Shinya Kitaoka 120a6e
    UINT index;
Shinya Kitaoka 120a6e
    //! funzionerebbe ancora meglio con un getNearestStroke che considera
Shinya Kitaoka 120a6e
    // la thickness, cioe' la min distance dalla outline e non dalla centerLine
Shinya Kitaoka 120a6e
    strokeFound = vi->getNearestStroke(pos, w, index, dist2);
Shinya Kitaoka 120a6e
    if (strokeFound) {
Shinya Kitaoka 120a6e
      TStroke *stroke = vi->getStroke(index);
Shinya Kitaoka 120a6e
      thick           = stroke->getThickPoint(w).thick;
Shinya Kitaoka 120a6e
      if (dist2 - thick * thick < maxDist2) {
Shinya Kitaoka 120a6e
        assert(stroke);
Shinya Kitaoka 120a6e
        styleId = stroke->getStyle();
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return styleId;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
/*--- Toonz Raster LevelのToneを拾う。 ---*/
Shinya Kitaoka 120a6e
int StylePicker::pickTone(const TPointD &pos) {
Shinya Kitaoka 120a6e
  if (TToonzImageP ti = m_image) {
Shinya Kitaoka 120a6e
    TRasterCM32P ras = ti->getRaster();
Shinya Kitaoka 120a6e
    if (!ras) return -1;
Shinya Kitaoka 120a6e
    TPoint point = getRasterPoint(pos);
Shinya Kitaoka 120a6e
    if (!ras->getBounds().contains(point)) return -1;
Shinya Kitaoka 120a6e
    TPixelCM32 col = ras->pixels(point.y)[point.x];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    return col.getTone();
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    return -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TPixel32 StylePicker::pickColor(const TPointD &pos, double radius2) const {
Shinya Kitaoka 120a6e
  TToonzImageP ti  = m_image;
Shinya Kitaoka 120a6e
  TRasterImageP ri = m_image;
Shinya Kitaoka 120a6e
  if (!!ri)  // !!ti || !!ri)
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    TRasterP raster;
Shinya Kitaoka 120a6e
    // if(ti)
Shinya Kitaoka 120a6e
    //  raster = ti->getRGBM(true);
Shinya Kitaoka 120a6e
    // else
Shinya Kitaoka 120a6e
    raster = ri->getRaster();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TPoint point = getRasterPoint(pos);
Shinya Kitaoka 120a6e
    if (!raster->getBounds().contains(point)) return TPixel32::Transparent;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TRaster32P raster32 = raster;
Shinya Kitaoka 120a6e
    if (raster32) return raster32->pixels(point.y)[point.x];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TRasterGR8P rasterGR8 = raster;
Shinya Kitaoka 120a6e
    if (rasterGR8) return toPixel32(rasterGR8->pixels(point.y)[point.x]);
Shinya Kitaoka 120a6e
  } else if (TVectorImageP vi = m_image) {
Shinya Kitaoka 120a6e
    const TPalette *palette = m_palette.getPointer();
Shinya Kitaoka 120a6e
    if (!palette) return TPixel32::Transparent;
Shinya Kitaoka 120a6e
    int styleId = pickStyleId(pos, radius2);
Shinya Kitaoka 120a6e
    if (0 <= styleId && styleId < palette->getStyleCount())
Shinya Kitaoka 120a6e
      return palette->getStyle(styleId)->getAverageColor();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return TPixel32::Transparent;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TPixel32 getAverageColor(const TRect &rect) {
Shinya Kitaoka 120a6e
  GLenum fmt =
Campbell Barton d869b5
#if defined(TNZ_MACHINE_CHANNEL_ORDER_BGRM)
Shinya Kitaoka 120a6e
      GL_RGBA;
Campbell Barton d869b5
#elif defined(TNZ_MACHINE_CHANNEL_ORDER_MBGR)
Shinya Kitaoka 120a6e
      GL_ABGR_EXT;
Campbell Barton d869b5
#elif defined(TNZ_MACHINE_CHANNEL_ORDER_RGBM)
Shinya Kitaoka 120a6e
      GL_RGBA;
Campbell Barton d869b5
#elif defined(TNZ_MACHINE_CHANNEL_ORDER_MRGB)
Shinya Kitaoka 120a6e
      GL_BGRA;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
//   Error  PLATFORM NOT SUPPORTED
Shinya Kitaoka 120a6e
#error "unknown channel order!"
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  UINT r = 0, g = 0, b = 0, m = 0;
Shinya Kitaoka 120a6e
  std::vector<tpixel32> buffer(rect.getLx() * rect.getLy());</tpixel32>
Shinya Kitaoka 120a6e
  glReadPixels(rect.x0, rect.y0, rect.getLx(), rect.getLy(), fmt,
Shinya Kitaoka 120a6e
               GL_UNSIGNED_BYTE, &buffer[0]);
Shinya Kitaoka 120a6e
  int size = rect.getLx() * rect.getLy();
Shinya Kitaoka 120a6e
  for (int i = 0; i < size; i++) {
Shinya Kitaoka 120a6e
    r += buffer[i].r;
Shinya Kitaoka 120a6e
    g += buffer[i].g;
Shinya Kitaoka 120a6e
    b += buffer[i].b;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return TPixel32(b / size, g / size, r / size, 255);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TPixel32 getAverageColor(TStroke *stroke) {
Shinya Kitaoka 120a6e
  GLenum fmt =
Campbell Barton d869b5
#if defined(TNZ_MACHINE_CHANNEL_ORDER_BGRM)
Shinya Kitaoka 120a6e
      GL_RGBA;
Campbell Barton d869b5
#elif defined(TNZ_MACHINE_CHANNEL_ORDER_MBGR)
Shinya Kitaoka 120a6e
      GL_ABGR_EXT;
Campbell Barton d869b5
#elif defined(TNZ_MACHINE_CHANNEL_ORDER_RGBM)
Shinya Kitaoka 120a6e
      GL_RGBA;
Campbell Barton d869b5
#elif defined(TNZ_MACHINE_CHANNEL_ORDER_MRGB)
Shinya Kitaoka 120a6e
      GL_BGRA;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
//   Error  PLATFORM NOT SUPPORTED
Shinya Kitaoka 120a6e
#error "unknown channel order"
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // leggo il buffer e mi prendo i pixels
Shinya Kitaoka 120a6e
  UINT r = 0, g = 0, b = 0, m = 0;
Shinya Kitaoka 120a6e
  TRect rect = convert(stroke->getBBox());
Shinya Kitaoka 120a6e
  std::vector<tpixel32> buffer(rect.getLx() * rect.getLy());</tpixel32>
Shinya Kitaoka 120a6e
  glReadPixels(rect.x0, rect.y0, rect.getLx(), rect.getLy(), fmt,
Shinya Kitaoka 120a6e
               GL_UNSIGNED_BYTE, &buffer[0]);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // calcolo le regioni dello stroke
Shinya Kitaoka 120a6e
  TVectorImage aux;
Shinya Kitaoka 120a6e
  aux.addStroke(stroke);
Shinya Kitaoka 120a6e
  aux.transform(TTranslation(convert(-rect.getP00())));
Shinya Kitaoka 120a6e
  aux.findRegions();
Shinya Kitaoka 120a6e
  int regionCount = aux.getRegionCount();
Shinya Kitaoka 120a6e
  int size = 0, lx = rect.getLx();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (int j = 0; j < regionCount; j++) {
Shinya Kitaoka 120a6e
    TRegion *reg  = aux.getRegion(j);
Shinya Kitaoka 120a6e
    TRect regRect = convert(reg->getBBox());
Shinya Kitaoka 120a6e
    for (int y = regRect.y0; y < regRect.y1; y++) {
Shinya Kitaoka 120a6e
      std::vector<double> intersections;</double>
Shinya Kitaoka 120a6e
      reg->computeScanlineIntersections(y, intersections);
Shinya Kitaoka 120a6e
      assert(!(intersections.size() & 0x1));
Shinya Kitaoka 120a6e
      for (UINT i = 0; i < intersections.size(); i += 2) {
Shinya Kitaoka 120a6e
        if (intersections[i] == intersections[i + 1]) continue;
Shinya Kitaoka 120a6e
        int firstInters  = (int)intersections[i];
Shinya Kitaoka 120a6e
        int secondInters = (int)intersections[i + 1];
Shinya Kitaoka 120a6e
        for (int x = firstInters + 1; x < secondInters - 1; x++) {
Shinya Kitaoka 120a6e
          r += buffer[y * lx + x].r;
Shinya Kitaoka 120a6e
          g += buffer[y * lx + x].g;
Shinya Kitaoka 120a6e
          b += buffer[y * lx + x].b;
Shinya Kitaoka 120a6e
          size++;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (size != 0)
Shinya Kitaoka 120a6e
    return TPixel32(b / size, g / size, r / size, 255);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return TPixel32(buffer[0].b, buffer[0].g, buffer[0].r, 255);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namspace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TPixel32 StylePicker::pickColor(const TRectD &area) const {
Shinya Kitaoka 120a6e
  // TRectD rect=area.enlarge(-1,-1);
Shinya Kitaoka 120a6e
  return getAverageColor(convert(area));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TPixel32 StylePicker::pickColor(TStroke *stroke) const {
Shinya Kitaoka 120a6e
  return getAverageColor(stroke);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------