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
class RasterController {
bf1d82
public:
shun-iwasawa 98926d
  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
shun-iwasawa 98926d
class Raster32PMyPaintSurface : public mypaint::Surface {
bf1d82
private:
bf1d82
  class Internal;
bf1d82
bf1d82
  TRaster32P m_ras;
bf1d82
  RasterController *controller;
bf1d82
  Internal *internal;
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;
bf1d82
  }
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;
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
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));
bf1d82
  }
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));
bf1d82
  }
bf1d82
bf1d82
public:
bf1d82
  explicit Raster32PMyPaintSurface(const TRaster32P &ras);
shun-iwasawa 98926d
  explicit Raster32PMyPaintSurface(const TRaster32P &ras,
shun-iwasawa 98926d
                                   RasterController &controller);
bf1d82
  ~Raster32PMyPaintSurface();
bf1d82
shun-iwasawa 98926d
  bool getColor(float x, float y, float radius, float &colorR, float &colorG,
shun-iwasawa 98926d
                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 {
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
      };
bf1d82
    };
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) {}
bf1d82
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]);
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:
shun-iwasawa 98926d
  MyPaintToonzBrush(const TRaster32P &ras, RasterController &controller,
shun-iwasawa 98926d
                    const mypaint::Brush &brush);
bf1d82
  void beginStroke();
bf1d82
  void endStroke();
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;
bf1d82
};
bf1d82
bf1d82
#endif  // T_BLUREDBRUSH