diff options
Diffstat (limited to 'source/blender/compositor/operations')
-rw-r--r-- | source/blender/compositor/operations/COM_DilateErodeOperation.cpp | 110 | ||||
-rw-r--r-- | source/blender/compositor/operations/COM_DilateErodeOperation.h | 41 |
2 files changed, 151 insertions, 0 deletions
diff --git a/source/blender/compositor/operations/COM_DilateErodeOperation.cpp b/source/blender/compositor/operations/COM_DilateErodeOperation.cpp index a73f76030ab..7bc49fa695c 100644 --- a/source/blender/compositor/operations/COM_DilateErodeOperation.cpp +++ b/source/blender/compositor/operations/COM_DilateErodeOperation.cpp @@ -23,6 +23,7 @@ #include "COM_DilateErodeOperation.h" #include "BLI_math.h" +// DilateErode Distance Threshold DilateErodeDistanceOperation::DilateErodeDistanceOperation(): NodeOperation() { this->addInputSocket(COM_DT_VALUE); @@ -158,6 +159,115 @@ bool DilateErodeDistanceOperation::determineDependingAreaOfInterest(rcti *input, return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output); } +// Dilate Distance +DilateDistanceOperation::DilateDistanceOperation(): NodeOperation() +{ + this->addInputSocket(COM_DT_VALUE); + this->addOutputSocket(COM_DT_VALUE); + this->setComplex(true); + this->inputProgram = NULL; + this->distance = 0.0f; +} +void DilateDistanceOperation::initExecution() +{ + this->inputProgram = this->getInputSocketReader(0); + this->scope = distance; + if (scope < 3) { + scope = 3; + } +} + +void *DilateDistanceOperation::initializeTileData(rcti *rect, MemoryBuffer **memoryBuffers) +{ + void *buffer = inputProgram->initializeTileData(NULL, memoryBuffers); + return buffer; +} + +void DilateDistanceOperation::executePixel(float *color, int x, int y, MemoryBuffer *inputBuffers[], void *data) +{ + const float distance = this->distance; + float mindist = distance * distance; + + MemoryBuffer *inputBuffer = (MemoryBuffer*)data; + float *buffer = inputBuffer->getBuffer(); + rcti *rect = inputBuffer->getRect(); + const int minx = max(x - scope, rect->xmin); + const int miny = max(y - scope, rect->ymin); + const int maxx = min(x + scope, rect->xmax); + const int maxy = min(y + scope, rect->ymax); + const int bufferWidth = rect->xmax-rect->xmin; + int offset; + + float value = 0.0f; + + for (int yi = miny ; yi<maxy;yi++) { + offset = ((yi-rect->ymin)*bufferWidth+(minx-rect->xmin))*4; + for (int xi = minx ; xi<maxx;xi++) { + const float dx = xi-x; + const float dy = yi-y; + const float dis = dx*dx+dy*dy; + if (dis <= mindist) { + value = max(buffer[offset], value); + } + offset +=4; + } + } + color[0] = value; +} + +void DilateDistanceOperation::deinitExecution() +{ + this->inputProgram = NULL; +} + +bool DilateDistanceOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output) +{ + rcti newInput; + + newInput.xmax = input->xmax + scope; + newInput.xmin = input->xmin - scope; + newInput.ymax = input->ymax + scope; + newInput.ymin = input->ymin - scope; + + return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output); +} +// Erode Distance +ErodeDistanceOperation::ErodeDistanceOperation() : DilateDistanceOperation() +{ +} + +void ErodeDistanceOperation::executePixel(float *color, int x, int y, MemoryBuffer *inputBuffers[], void *data) +{ + const float distance = this->distance; + float mindist = distance * distance; + + MemoryBuffer *inputBuffer = (MemoryBuffer*)data; + float *buffer = inputBuffer->getBuffer(); + rcti *rect = inputBuffer->getRect(); + const int minx = max(x - scope, rect->xmin); + const int miny = max(y - scope, rect->ymin); + const int maxx = min(x + scope, rect->xmax); + const int maxy = min(y + scope, rect->ymax); + const int bufferWidth = rect->xmax-rect->xmin; + int offset; + + float value = 1.0f; + + for (int yi = miny ; yi<maxy;yi++) { + offset = ((yi-rect->ymin)*bufferWidth+(minx-rect->xmin))*4; + for (int xi = minx ; xi<maxx;xi++) { + const float dx = xi-x; + const float dy = yi-y; + const float dis = dx*dx+dy*dy; + if (dis <= mindist) { + value = min(buffer[offset], value); + } + offset +=4; + } + } + color[0] = value; +} + // Dilate step DilateStepOperation::DilateStepOperation(): NodeOperation() { diff --git a/source/blender/compositor/operations/COM_DilateErodeOperation.h b/source/blender/compositor/operations/COM_DilateErodeOperation.h index 7f01ea94494..71bbab74a4b 100644 --- a/source/blender/compositor/operations/COM_DilateErodeOperation.h +++ b/source/blender/compositor/operations/COM_DilateErodeOperation.h @@ -68,6 +68,47 @@ public: }; +class DilateDistanceOperation : public NodeOperation { +private: + /** + * Cached reference to the inputProgram + */ + SocketReader * inputProgram; +protected: + float distance; + int scope; +public: + DilateDistanceOperation(); + + /** + * the inner loop of this program + */ + void executePixel(float *color, int x, int y, MemoryBuffer *inputBuffers[], void *data); + + /** + * Initialize the execution + */ + void initExecution(); + + void *initializeTileData(rcti *rect, MemoryBuffer **memoryBuffers); + /** + * Deinitialize the execution + */ + void deinitExecution(); + + void setDistance(float distance) {this->distance = distance;} + bool determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output); +}; +class ErodeDistanceOperation : public DilateDistanceOperation { +public: + ErodeDistanceOperation(); + + /** + * the inner loop of this program + */ + void executePixel(float *color, int x, int y, MemoryBuffer *inputBuffers[], void *data); +}; + class DilateStepOperation : public NodeOperation { protected: /** |