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