Ivan Mahonin bf1d82
#pragma once
Ivan Mahonin bf1d82
Ivan Mahonin bf1d82
#ifndef MYPAINTTOONZBRUSH_H
Ivan Mahonin bf1d82
#define MYPAINTTOONZBRUSH_H
Ivan Mahonin bf1d82
Ivan Mahonin bf1d82
#include <toonz/mypaint.h>
Ivan Mahonin bf1d82
#include "traster.h"
Ivan Mahonin bf1d82
#include "trastercm.h"
Ivan Mahonin bf1d82
#include "tcurves.h"
Ivan Mahonin bf1d82
#include <QPainter>
Ivan Mahonin bf1d82
#include <QImage>
Ivan Mahonin bf1d82
Ivan Mahonin bf1d82
class RasterController {
Ivan Mahonin bf1d82
public:
shun-iwasawa 98926d
  virtual ~RasterController() {}
Ivan Mahonin bf1d82
  virtual bool askRead(const TRect &rect) { return true; }
Ivan Mahonin bf1d82
  virtual bool askWrite(const TRect &rect) { return true; }
Ivan Mahonin bf1d82
};
Ivan Mahonin bf1d82
Ivan Mahonin bf1d82
//=======================================================
Ivan Mahonin bf1d82
//
Ivan Mahonin bf1d82
// Raster32PMyPaintSurface
Ivan Mahonin bf1d82
//
Ivan Mahonin bf1d82
//=======================================================
Ivan Mahonin bf1d82
shun-iwasawa 98926d
class Raster32PMyPaintSurface : public mypaint::Surface {
Ivan Mahonin bf1d82
private:
Ivan Mahonin bf1d82
  class Internal;
Ivan Mahonin bf1d82
Ivan Mahonin bf1d82
  TRaster32P m_ras;
Ivan Mahonin bf1d82
  RasterController *controller;
Ivan Mahonin bf1d82
  Internal *internal;
Ivan Mahonin bf1d82
shun-iwasawa 98926d
  inline static void readPixel(const void *pixelPtr, float &colorR,
shun-iwasawa 98926d
                               float &colorG, float &colorB, float &colorA) {
shun-iwasawa 98926d
    const TPixel32 &pixel = *(const TPixel32 *)pixelPtr;
shun-iwasawa 98926d
    colorR                = (float)pixel.r / (float)TPixel32::maxChannelValue;
shun-iwasawa 98926d
    colorG                = (float)pixel.g / (float)TPixel32::maxChannelValue;
shun-iwasawa 98926d
    colorB                = (float)pixel.b / (float)TPixel32::maxChannelValue;
shun-iwasawa 98926d
    colorA                = (float)pixel.m / (float)TPixel32::maxChannelValue;
Ivan Mahonin bf1d82
  }
Ivan Mahonin bf1d82
shun-iwasawa 98926d
  inline static void writePixel(void *pixelPtr, float colorR, float colorG,
shun-iwasawa 98926d
                                float colorB, float colorA) {
shun-iwasawa 98926d
    TPixel32 &pixel = *(TPixel32 *)pixelPtr;
Ivan Mahonin bf1d82
    pixel.r = (TPixel32::Channel)roundf(colorR * TPixel32::maxChannelValue);
Ivan Mahonin bf1d82
    pixel.g = (TPixel32::Channel)roundf(colorG * TPixel32::maxChannelValue);
Ivan Mahonin bf1d82
    pixel.b = (TPixel32::Channel)roundf(colorB * TPixel32::maxChannelValue);
Ivan Mahonin bf1d82
    pixel.m = (TPixel32::Channel)roundf(colorA * TPixel32::maxChannelValue);
Ivan Mahonin bf1d82
  }
Ivan Mahonin bf1d82
shun-iwasawa 98926d
  inline static bool askRead(void *surfaceController,
shun-iwasawa 98926d
                             const void * /* surfacePointer */, int x0, int y0,
shun-iwasawa 98926d
                             int x1, int y1) {
shun-iwasawa 98926d
    Raster32PMyPaintSurface &owner =
shun-iwasawa 98926d
        *((Raster32PMyPaintSurface *)surfaceController);
shun-iwasawa 98926d
    return !owner.controller ||
shun-iwasawa 98926d
           owner.controller->askRead(TRect(x0, y0, x1, y1));
Ivan Mahonin bf1d82
  }
Ivan Mahonin bf1d82
shun-iwasawa 98926d
  inline static bool askWrite(void *surfaceController,
shun-iwasawa 98926d
                              const void * /* surfacePointer */, int x0, int y0,
shun-iwasawa 98926d
                              int x1, int y1) {
shun-iwasawa 98926d
    Raster32PMyPaintSurface &owner =
shun-iwasawa 98926d
        *((Raster32PMyPaintSurface *)surfaceController);
shun-iwasawa 98926d
    return !owner.controller ||
shun-iwasawa 98926d
           owner.controller->askWrite(TRect(x0, y0, x1, y1));
Ivan Mahonin bf1d82
  }
Ivan Mahonin bf1d82
Ivan Mahonin bf1d82
public:
Ivan Mahonin bf1d82
  explicit Raster32PMyPaintSurface(const TRaster32P &ras);
shun-iwasawa 98926d
  explicit Raster32PMyPaintSurface(const TRaster32P &ras,
shun-iwasawa 98926d
                                   RasterController &controller);
Ivan Mahonin bf1d82
  ~Raster32PMyPaintSurface();
Ivan Mahonin bf1d82
shun-iwasawa 98926d
  bool getColor(float x, float y, float radius, float &colorR, float &colorG,
shun-iwasawa 98926d
                float &colorB, float &colorA) override;
Ivan Mahonin bf1d82
Ivan Mahonin bf1d82
  bool drawDab(const mypaint::Dab &dab) override;
Ivan Mahonin bf1d82
Ivan Mahonin bf1d82
  bool getAntialiasing() const;
Ivan Mahonin bf1d82
  void setAntialiasing(bool value);
Ivan Mahonin bf1d82
};
Ivan Mahonin bf1d82
Ivan Mahonin bf1d82
//=======================================================
Ivan Mahonin bf1d82
//
Ivan Mahonin bf1d82
// MyPaintToonzBrush
Ivan Mahonin bf1d82
//
Ivan Mahonin bf1d82
//=======================================================
Ivan Mahonin bf1d82
Ivan Mahonin bf1d82
class MyPaintToonzBrush {
Ivan Mahonin bf1d82
private:
Ivan Mahonin bf1d82
  struct Params {
Ivan Mahonin bf1d82
    union {
shun-iwasawa 98926d
      struct {
shun-iwasawa 98926d
        double x, y, pressure, time;
shun-iwasawa 98926d
      };
shun-iwasawa 98926d
      struct {
shun-iwasawa 98926d
        double values[4];
shun-iwasawa 98926d
      };
Ivan Mahonin bf1d82
    };
Ivan Mahonin bf1d82
shun-iwasawa 98926d
    inline explicit Params(double x = 0.0, double y = 0.0,
shun-iwasawa 98926d
                           double pressure = 0.0, double time = 0.0)
shun-iwasawa 98926d
        : x(x), y(y), pressure(pressure), time(time) {}
Ivan Mahonin bf1d82
Ivan Mahonin bf1d82
    inline void setMedian(Params &a, Params &b) {
shun-iwasawa 98926d
      for (int i  = 0; i < (int)sizeof(values) / sizeof(values[0]); ++i)
shun-iwasawa 98926d
        values[i] = 0.5 * (a.values[i] + b.values[i]);
Ivan Mahonin bf1d82
    }
Ivan Mahonin bf1d82
  };
Ivan Mahonin bf1d82
Ivan Mahonin bf1d82
  struct Segment {
Ivan Mahonin bf1d82
    Params p1, p2;
Ivan Mahonin bf1d82
  };
Ivan Mahonin bf1d82
Ivan Mahonin bf1d82
  TRaster32P m_ras;
Ivan Mahonin bf1d82
  Raster32PMyPaintSurface m_mypaintSurface;
Ivan Mahonin bf1d82
  mypaint::Brush brush;
Ivan Mahonin bf1d82
Ivan Mahonin bf1d82
  bool reset;
Ivan Mahonin bf1d82
  Params previous, current;
Ivan Mahonin d8eddc
  bool interpolation;
Ivan Mahonin bf1d82
Ivan Mahonin bf1d82
public:
Ivan Mahonin d8eddc
  MyPaintToonzBrush(
Ivan Mahonin d8eddc
    const TRaster32P &ras,
Ivan Mahonin d8eddc
    RasterController &controller,
Ivan Mahonin d8eddc
    const mypaint::Brush &brush,
Ivan Mahonin d8eddc
    bool interpolation = false );
Ivan Mahonin bf1d82
  void beginStroke();
Ivan Mahonin bf1d82
  void endStroke();
Ivan Mahonin bf1d82
  void strokeTo(const TPointD &p, double pressure, double dtime);
shun-iwasawa 98926d
shun-iwasawa 98926d
  // colormapped
shun-iwasawa 98926d
  void updateDrawing(const TRasterCM32P rasCM, const TRasterCM32P rasBackupCM,
manongjohn 6c9506
                     const TRect &bbox, int styleId, bool lockAlpha) const;
Ivan Mahonin bf1d82
};
Ivan Mahonin bf1d82
Ivan Mahonin bf1d82
#endif  // T_BLUREDBRUSH