2
* Copyright 2011, Blender Foundation.
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software Foundation,
16
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
* Contributor: Peter Schlaile
23
#include "MEM_guardedalloc.h"
25
#include "COM_InpaintOperation.h"
26
#include "COM_OpenCLDevice.h"
30
#define ASSERT_XY_RANGE(x, y) \
31
BLI_assert(x >= 0 && x < this->getWidth() && \
32
y >= 0 && y < this->getHeight())
35
// Inpaint (simple convolve using average of known pixels)
36
InpaintSimpleOperation::InpaintSimpleOperation() : NodeOperation()
38
this->addInputSocket(COM_DT_COLOR);
39
this->addOutputSocket(COM_DT_COLOR);
40
this->setComplex(true);
41
this->m_inputImageProgram = NULL;
42
this->m_pixelorder = NULL;
43
this->m_manhatten_distance = NULL;
44
this->m_cached_buffer = NULL;
45
this->m_cached_buffer_ready = false;
47
void InpaintSimpleOperation::initExecution()
49
this->m_inputImageProgram = this->getInputSocketReader(0);
51
this->m_cached_buffer = NULL;
52
this->m_pixelorder = NULL;
53
this->m_manhatten_distance = NULL;
54
this->m_cached_buffer = NULL;
55
this->m_cached_buffer_ready = false;
60
void InpaintSimpleOperation::clamp_xy(int &x, int &y)
62
int width = this->getWidth();
63
int height = this->getHeight();
68
else if (x >= width) {
75
else if (y >= height) {
80
float *InpaintSimpleOperation::get_pixel(int x, int y)
82
int width = this->getWidth();
84
ASSERT_XY_RANGE(x, y);
86
return &this->m_cached_buffer[
87
y * width * COM_NUMBER_OF_CHANNELS +
88
x * COM_NUMBER_OF_CHANNELS];
91
int InpaintSimpleOperation::mdist(int x, int y)
93
int width = this->getWidth();
95
ASSERT_XY_RANGE(x, y);
97
return this->m_manhatten_distance[y * width + x];
100
bool InpaintSimpleOperation::next_pixel(int &x, int &y, int & curr, int iters)
102
int width = this->getWidth();
104
if (curr >= this->m_area_size) {
108
int r = this->m_pixelorder[curr++];
113
if (this->mdist(x, y) > iters) {
120
void InpaintSimpleOperation::calc_manhatten_distance()
122
int width = this->getWidth();
123
int height = this->getHeight();
124
short *m = this->m_manhatten_distance = (short *)MEM_mallocN(sizeof(short) * width * height, __func__);
127
offsets = (int *)MEM_callocN(sizeof(int) * (width + height + 1), "InpaintSimpleOperation offsets");
129
for (int j = 0; j < height; j++) {
130
for (int i = 0; i < width; i++) {
132
/* no need to clamp here */
133
if (this->get_pixel(i, j)[3] < 1.0f) {
136
r = min_ii(r, m[j * width + i - 1] + 1);
138
r = min_ii(r, m[(j - 1) * width + i] + 1);
140
m[j * width + i] = r;
144
for (int j = height - 1; j >= 0; j--) {
145
for (int i = width - 1; i >= 0; i--) {
146
int r = m[j * width + i];
149
r = min_ii(r, m[j * width + i + 1] + 1);
151
r = min_ii(r, m[(j + 1) * width + i] + 1);
153
m[j * width + i] = r;
161
for (int i = 1; i < width + height + 1; i++) {
162
offsets[i] += offsets[i - 1];
165
this->m_area_size = offsets[width + height];
166
this->m_pixelorder = (int *)MEM_mallocN(sizeof(int) * this->m_area_size, __func__);
168
for (int i = 0; i < width * height; i++) {
170
this->m_pixelorder[offsets[m[i] - 1]++] = i;
177
void InpaintSimpleOperation::pix_step(int x, int y)
179
const int d = this->mdist(x, y);
180
float pix[3] = {0.0f, 0.0f, 0.0f};
181
float pix_divider = 0.0f;
183
for (int dx = -1; dx <= 1; dx++) {
184
for (int dy = -1; dy <= 1; dy++) {
185
/* changing to both != 0 gives dithering artifacts */
186
if (dx != 0 || dy != 0) {
190
this->clamp_xy(x_ofs, y_ofs);
192
if (this->mdist(x_ofs, y_ofs) < d) {
196
if (dx == 0 || dy == 0) {
200
weight = M_SQRT1_2; /* 1.0f / sqrt(2) */
203
madd_v3_v3fl(pix, this->get_pixel(x_ofs, y_ofs), weight);
204
pix_divider += weight;
210
float *output = this->get_pixel(x, y);
211
if (pix_divider != 0.0f) {
212
mul_v3_fl(pix, 1.0f / pix_divider);
213
/* use existing pixels alpha to blend into */
214
interp_v3_v3v3(output, pix, output, output[3]);
219
void *InpaintSimpleOperation::initializeTileData(rcti *rect)
221
if (this->m_cached_buffer_ready) {
222
return this->m_cached_buffer;
225
if (!this->m_cached_buffer_ready) {
226
MemoryBuffer *buf = (MemoryBuffer *)this->m_inputImageProgram->initializeTileData(rect);
227
this->m_cached_buffer = (float *)MEM_dupallocN(buf->getBuffer());
229
this->calc_manhatten_distance();
235
while (this->next_pixel(x, y, curr, this->m_iterations)) {
236
this->pix_step(x, y);
238
this->m_cached_buffer_ready = true;
242
return this->m_cached_buffer;
245
void InpaintSimpleOperation::executePixel(float output[4], int x, int y, void *data)
247
this->clamp_xy(x, y);
248
copy_v4_v4(output, this->get_pixel(x, y));
251
void InpaintSimpleOperation::deinitExecution()
253
this->m_inputImageProgram = NULL;
255
if (this->m_cached_buffer) {
256
MEM_freeN(this->m_cached_buffer);
257
this->m_cached_buffer = NULL;
260
if (this->m_pixelorder) {
261
MEM_freeN(this->m_pixelorder);
262
this->m_pixelorder = NULL;
265
if (this->m_manhatten_distance) {
266
MEM_freeN(this->m_manhatten_distance);
267
this->m_manhatten_distance = NULL;
269
this->m_cached_buffer_ready = false;
272
bool InpaintSimpleOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
274
if (this->m_cached_buffer_ready) {
280
newInput.xmax = getWidth();
282
newInput.ymax = getHeight();
285
return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);