Welcome to mirror list, hosted at ThFree Co, Russian Federation.

COM_ScaleOperation.cc « operations « compositor « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1957c5eb5fc1692ed371b09309898cb3f1bd8265 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2011 Blender Foundation. */

#include "COM_ScaleOperation.h"
#include "COM_ConstantOperation.h"

namespace blender::compositor {

#define USE_FORCE_BILINEAR
/* XXX(campbell): ignore input and use default from old compositor,
 * could become an option like the transform node.
 *
 * NOTE: use bilinear because bicubic makes fuzzy even when not scaling at all (1:1)
 */

BaseScaleOperation::BaseScaleOperation()
{
#ifdef USE_FORCE_BILINEAR
  sampler_ = (int)PixelSampler::Bilinear;
#else
  sampler_ = -1;
#endif
  variable_size_ = false;
}

void BaseScaleOperation::set_scale_canvas_max_size(Size2f size)
{
  max_scale_canvas_size_ = size;
}

ScaleOperation::ScaleOperation() : ScaleOperation(DataType::Color)
{
}

ScaleOperation::ScaleOperation(DataType data_type) : BaseScaleOperation()
{
  this->add_input_socket(data_type, ResizeMode::None);
  this->add_input_socket(DataType::Value);
  this->add_input_socket(DataType::Value);
  this->add_output_socket(data_type);
  input_operation_ = nullptr;
  input_xoperation_ = nullptr;
  input_yoperation_ = nullptr;
}

float ScaleOperation::get_constant_scale(const int input_op_idx, const float factor)
{
  const bool is_constant = get_input_operation(input_op_idx)->get_flags().is_constant_operation;
  if (is_constant) {
    return ((ConstantOperation *)get_input_operation(input_op_idx))->get_constant_elem()[0] *
           factor;
  }

  return 1.0f;
}

float ScaleOperation::get_constant_scale_x(const float width)
{
  return get_constant_scale(X_INPUT_INDEX, get_relative_scale_x_factor(width));
}

float ScaleOperation::get_constant_scale_y(const float height)
{
  return get_constant_scale(Y_INPUT_INDEX, get_relative_scale_y_factor(height));
}

bool ScaleOperation::is_scaling_variable()
{
  return !get_input_operation(X_INPUT_INDEX)->get_flags().is_constant_operation ||
         !get_input_operation(Y_INPUT_INDEX)->get_flags().is_constant_operation;
}

void ScaleOperation::scale_area(rcti &area, float relative_scale_x, float relative_scale_y)
{
  const rcti src_area = area;
  const float center_x = BLI_rcti_size_x(&area) / 2.0f;
  const float center_y = BLI_rcti_size_y(&area) / 2.0f;
  area.xmin = floorf(scale_coord(area.xmin, center_x, relative_scale_x));
  area.xmax = ceilf(scale_coord(area.xmax, center_x, relative_scale_x));
  area.ymin = floorf(scale_coord(area.ymin, center_y, relative_scale_y));
  area.ymax = ceilf(scale_coord(area.ymax, center_y, relative_scale_y));

  float scale_offset_x, scale_offset_y;
  ScaleOperation::get_scale_offset(src_area, area, scale_offset_x, scale_offset_y);
  BLI_rcti_translate(&area, -scale_offset_x, -scale_offset_y);
}

void ScaleOperation::clamp_area_size_max(rcti &area, Size2f max_size)
{

  if (BLI_rcti_size_x(&area) > max_size.x) {
    area.xmax = area.xmin + max_size.x;
  }
  if (BLI_rcti_size_y(&area) > max_size.y) {
    area.ymax = area.ymin + max_size.y;
  }
}

void ScaleOperation::init_data()
{
  canvas_center_x_ = canvas_.xmin + get_width() / 2.0f;
  canvas_center_y_ = canvas_.ymin + get_height() / 2.0f;
}

void ScaleOperation::init_execution()
{
  input_operation_ = this->get_input_socket_reader(0);
  input_xoperation_ = this->get_input_socket_reader(1);
  input_yoperation_ = this->get_input_socket_reader(2);
}

void ScaleOperation::deinit_execution()
{
  input_operation_ = nullptr;
  input_xoperation_ = nullptr;
  input_yoperation_ = nullptr;
}

void ScaleOperation::get_scale_offset(const rcti &input_canvas,
                                      const rcti &scale_canvas,
                                      float &r_scale_offset_x,
                                      float &r_scale_offset_y)
{
  r_scale_offset_x = (BLI_rcti_size_x(&input_canvas) - BLI_rcti_size_x(&scale_canvas)) / 2.0f;
  r_scale_offset_y = (BLI_rcti_size_y(&input_canvas) - BLI_rcti_size_y(&scale_canvas)) / 2.0f;
}

void ScaleOperation::get_scale_area_of_interest(const rcti &input_canvas,
                                                const rcti &scale_canvas,
                                                const float relative_scale_x,
                                                const float relative_scale_y,
                                                const rcti &output_area,
                                                rcti &r_input_area)
{
  const float scale_center_x = BLI_rcti_size_x(&input_canvas) / 2.0f;
  const float scale_center_y = BLI_rcti_size_y(&input_canvas) / 2.0f;
  float scale_offset_x, scale_offset_y;
  ScaleOperation::get_scale_offset(input_canvas, scale_canvas, scale_offset_x, scale_offset_y);

  r_input_area.xmin = floorf(
      scale_coord_inverted(output_area.xmin + scale_offset_x, scale_center_x, relative_scale_x));
  r_input_area.xmax = ceilf(
      scale_coord_inverted(output_area.xmax + scale_offset_x, scale_center_x, relative_scale_x));
  r_input_area.ymin = floorf(
      scale_coord_inverted(output_area.ymin + scale_offset_y, scale_center_y, relative_scale_y));
  r_input_area.ymax = ceilf(
      scale_coord_inverted(output_area.ymax + scale_offset_y, scale_center_y, relative_scale_y));
}

void ScaleOperation::get_area_of_interest(const int input_idx,
                                          const rcti &output_area,
                                          rcti &r_input_area)
{
  r_input_area = output_area;
  if (input_idx != 0 || is_scaling_variable()) {
    return;
  }

  NodeOperation *image_op = get_input_operation(IMAGE_INPUT_INDEX);
  const float scale_x = get_constant_scale_x(image_op->get_width());
  const float scale_y = get_constant_scale_y(image_op->get_height());

  get_scale_area_of_interest(
      image_op->get_canvas(), this->get_canvas(), scale_x, scale_y, output_area, r_input_area);
  expand_area_for_sampler(r_input_area, (PixelSampler)sampler_);
}

void ScaleOperation::update_memory_buffer_partial(MemoryBuffer *output,
                                                  const rcti &area,
                                                  Span<MemoryBuffer *> inputs)
{
  NodeOperation *input_image_op = get_input_operation(IMAGE_INPUT_INDEX);
  const int input_image_width = input_image_op->get_width();
  const int input_image_height = input_image_op->get_height();
  const float scale_x_factor = get_relative_scale_x_factor(input_image_width);
  const float scale_y_factor = get_relative_scale_y_factor(input_image_height);
  const float scale_center_x = input_image_width / 2.0f;
  const float scale_center_y = input_image_height / 2.0f;
  float from_scale_offset_x, from_scale_offset_y;
  ScaleOperation::get_scale_offset(
      input_image_op->get_canvas(), this->get_canvas(), from_scale_offset_x, from_scale_offset_y);

  const MemoryBuffer *input_image = inputs[IMAGE_INPUT_INDEX];
  MemoryBuffer *input_x = inputs[X_INPUT_INDEX];
  MemoryBuffer *input_y = inputs[Y_INPUT_INDEX];
  BuffersIterator<float> it = output->iterate_with({input_x, input_y}, area);
  for (; !it.is_end(); ++it) {
    const float rel_scale_x = *it.in(0) * scale_x_factor;
    const float rel_scale_y = *it.in(1) * scale_y_factor;
    const float scaled_x = scale_coord_inverted(
        from_scale_offset_x + canvas_.xmin + it.x, scale_center_x, rel_scale_x);
    const float scaled_y = scale_coord_inverted(
        from_scale_offset_y + canvas_.ymin + it.y, scale_center_y, rel_scale_y);

    input_image->read_elem_sampled(
        scaled_x - canvas_.xmin, scaled_y - canvas_.ymin, (PixelSampler)sampler_, it.out);
  }
}

void ScaleOperation::determine_canvas(const rcti &preferred_area, rcti &r_area)
{
  if (execution_model_ == eExecutionModel::Tiled) {
    NodeOperation::determine_canvas(preferred_area, r_area);
    return;
  }

  const bool image_determined =
      get_input_socket(IMAGE_INPUT_INDEX)->determine_canvas(preferred_area, r_area);
  if (image_determined) {
    rcti image_canvas = r_area;
    rcti unused = COM_AREA_NONE;
    NodeOperationInput *x_socket = get_input_socket(X_INPUT_INDEX);
    NodeOperationInput *y_socket = get_input_socket(Y_INPUT_INDEX);
    x_socket->determine_canvas(image_canvas, unused);
    y_socket->determine_canvas(image_canvas, unused);
    if (is_scaling_variable()) {
      /* Do not scale canvas. */
      return;
    }

    /* Determine scaled canvas. */
    const float input_width = BLI_rcti_size_x(&r_area);
    const float input_height = BLI_rcti_size_y(&r_area);
    const float scale_x = get_constant_scale_x(input_width);
    const float scale_y = get_constant_scale_y(input_height);
    scale_area(r_area, scale_x, scale_y);
    const Size2f max_scale_size = {MAX2(input_width, max_scale_canvas_size_.x),
                                   MAX2(input_height, max_scale_canvas_size_.y)};
    clamp_area_size_max(r_area, max_scale_size);

    /* Re-determine canvases of x and y constant inputs with scaled canvas as preferred. */
    get_input_operation(X_INPUT_INDEX)->unset_canvas();
    get_input_operation(Y_INPUT_INDEX)->unset_canvas();
    x_socket->determine_canvas(r_area, unused);
    y_socket->determine_canvas(r_area, unused);
  }
}

ScaleRelativeOperation::ScaleRelativeOperation() : ScaleOperation()
{
}

ScaleRelativeOperation::ScaleRelativeOperation(DataType data_type) : ScaleOperation(data_type)
{
}

void ScaleRelativeOperation::execute_pixel_sampled(float output[4],
                                                   float x,
                                                   float y,
                                                   PixelSampler sampler)
{
  PixelSampler effective_sampler = get_effective_sampler(sampler);

  float scaleX[4];
  float scaleY[4];

  input_xoperation_->read_sampled(scaleX, x, y, effective_sampler);
  input_yoperation_->read_sampled(scaleY, x, y, effective_sampler);

  const float scx = scaleX[0];
  const float scy = scaleY[0];

  float nx = this->canvas_center_x_ + (x - this->canvas_center_x_) / scx;
  float ny = this->canvas_center_y_ + (y - this->canvas_center_y_) / scy;
  input_operation_->read_sampled(output, nx, ny, effective_sampler);
}

bool ScaleRelativeOperation::determine_depending_area_of_interest(
    rcti *input, ReadBufferOperation *read_operation, rcti *output)
{
  rcti new_input;
  if (!variable_size_) {
    float scaleX[4];
    float scaleY[4];

    input_xoperation_->read_sampled(scaleX, 0, 0, PixelSampler::Nearest);
    input_yoperation_->read_sampled(scaleY, 0, 0, PixelSampler::Nearest);

    const float scx = scaleX[0];
    const float scy = scaleY[0];

    new_input.xmax = this->canvas_center_x_ + (input->xmax - this->canvas_center_x_) / scx + 1;
    new_input.xmin = this->canvas_center_x_ + (input->xmin - this->canvas_center_x_) / scx - 1;
    new_input.ymax = this->canvas_center_y_ + (input->ymax - this->canvas_center_y_) / scy + 1;
    new_input.ymin = this->canvas_center_y_ + (input->ymin - this->canvas_center_y_) / scy - 1;
  }
  else {
    new_input.xmax = this->get_width();
    new_input.xmin = 0;
    new_input.ymax = this->get_height();
    new_input.ymin = 0;
  }
  return BaseScaleOperation::determine_depending_area_of_interest(
      &new_input, read_operation, output);
}

void ScaleAbsoluteOperation::execute_pixel_sampled(float output[4],
                                                   float x,
                                                   float y,
                                                   PixelSampler sampler)
{
  PixelSampler effective_sampler = get_effective_sampler(sampler);

  float scaleX[4];
  float scaleY[4];

  input_xoperation_->read_sampled(scaleX, x, y, effective_sampler);
  input_yoperation_->read_sampled(scaleY, x, y, effective_sampler);

  const float scx = scaleX[0]; /* Target absolute scale. */
  const float scy = scaleY[0]; /* Target absolute scale. */

  const float width = this->get_width();
  const float height = this->get_height();
  /* Divide. */
  float relative_xscale = scx / width;
  float relative_yscale = scy / height;

  float nx = this->canvas_center_x_ + (x - this->canvas_center_x_) / relative_xscale;
  float ny = this->canvas_center_y_ + (y - this->canvas_center_y_) / relative_yscale;

  input_operation_->read_sampled(output, nx, ny, effective_sampler);
}

bool ScaleAbsoluteOperation::determine_depending_area_of_interest(
    rcti *input, ReadBufferOperation *read_operation, rcti *output)
{
  rcti new_input;
  if (!variable_size_) {
    float scaleX[4];
    float scaleY[4];

    input_xoperation_->read_sampled(scaleX, 0, 0, PixelSampler::Nearest);
    input_yoperation_->read_sampled(scaleY, 0, 0, PixelSampler::Nearest);

    const float scx = scaleX[0];
    const float scy = scaleY[0];
    const float width = this->get_width();
    const float height = this->get_height();
    /* Divide. */
    float relateve_xscale = scx / width;
    float relateve_yscale = scy / height;

    new_input.xmax = this->canvas_center_x_ +
                     (input->xmax - this->canvas_center_x_) / relateve_xscale;
    new_input.xmin = this->canvas_center_x_ +
                     (input->xmin - this->canvas_center_x_) / relateve_xscale;
    new_input.ymax = this->canvas_center_y_ +
                     (input->ymax - this->canvas_center_y_) / relateve_yscale;
    new_input.ymin = this->canvas_center_y_ +
                     (input->ymin - this->canvas_center_y_) / relateve_yscale;
  }
  else {
    new_input.xmax = this->get_width();
    new_input.xmin = 0;
    new_input.ymax = this->get_height();
    new_input.ymin = 0;
  }
  return ScaleOperation::determine_depending_area_of_interest(&new_input, read_operation, output);
}

ScaleFixedSizeOperation::ScaleFixedSizeOperation() : BaseScaleOperation()
{
  this->add_input_socket(DataType::Color, ResizeMode::None);
  this->add_output_socket(DataType::Color);
  this->set_canvas_input_index(0);
  input_operation_ = nullptr;
  is_offset_ = false;
}

void ScaleFixedSizeOperation::init_data(const rcti &input_canvas)
{
  const int input_width = BLI_rcti_size_x(&input_canvas);
  const int input_height = BLI_rcti_size_y(&input_canvas);
  rel_x_ = input_width / (float)new_width_;
  rel_y_ = input_height / (float)new_height_;

  /* *** all the options below are for a fairly special case - camera framing *** */
  if (offset_x_ != 0.0f || offset_y_ != 0.0f) {
    is_offset_ = true;

    if (new_width_ > new_height_) {
      offset_x_ *= new_width_;
      offset_y_ *= new_width_;
    }
    else {
      offset_x_ *= new_height_;
      offset_y_ *= new_height_;
    }
  }

  if (is_aspect_) {
    /* apply aspect from clip */
    const float w_src = input_width;
    const float h_src = input_height;

    /* destination aspect is already applied from the camera frame */
    const float w_dst = new_width_;
    const float h_dst = new_height_;

    const float asp_src = w_src / h_src;
    const float asp_dst = w_dst / h_dst;

    if (fabsf(asp_src - asp_dst) >= FLT_EPSILON) {
      if ((asp_src > asp_dst) == (is_crop_ == true)) {
        /* fit X */
        const float div = asp_src / asp_dst;
        rel_x_ /= div;
        offset_x_ += ((w_src - (w_src * div)) / (w_src / w_dst)) / 2.0f;
        if (is_crop_ && execution_model_ == eExecutionModel::FullFrame) {
          int fit_width = new_width_ * div;
          if (fit_width > max_scale_canvas_size_.x) {
            fit_width = max_scale_canvas_size_.x;
          }

          const int added_width = fit_width - new_width_;
          new_width_ += added_width;
          offset_x_ += added_width / 2.0f;
        }
      }
      else {
        /* fit Y */
        const float div = asp_dst / asp_src;
        rel_y_ /= div;
        offset_y_ += ((h_src - (h_src * div)) / (h_src / h_dst)) / 2.0f;
        if (is_crop_ && execution_model_ == eExecutionModel::FullFrame) {
          int fit_height = new_height_ * div;
          if (fit_height > max_scale_canvas_size_.y) {
            fit_height = max_scale_canvas_size_.y;
          }

          const int added_height = fit_height - new_height_;
          new_height_ += added_height;
          offset_y_ += added_height / 2.0f;
        }
      }

      is_offset_ = true;
    }
  }
  /* *** end framing options *** */
}

void ScaleFixedSizeOperation::init_execution()
{
  input_operation_ = this->get_input_socket_reader(0);
}

void ScaleFixedSizeOperation::deinit_execution()
{
  input_operation_ = nullptr;
}

void ScaleFixedSizeOperation::execute_pixel_sampled(float output[4],
                                                    float x,
                                                    float y,
                                                    PixelSampler sampler)
{
  PixelSampler effective_sampler = get_effective_sampler(sampler);

  if (is_offset_) {
    float nx = ((x - offset_x_) * rel_x_);
    float ny = ((y - offset_y_) * rel_y_);
    input_operation_->read_sampled(output, nx, ny, effective_sampler);
  }
  else {
    input_operation_->read_sampled(output, x * rel_x_, y * rel_y_, effective_sampler);
  }
}

bool ScaleFixedSizeOperation::determine_depending_area_of_interest(
    rcti *input, ReadBufferOperation *read_operation, rcti *output)
{
  rcti new_input;

  new_input.xmax = (input->xmax - offset_x_) * rel_x_ + 1;
  new_input.xmin = (input->xmin - offset_x_) * rel_x_;
  new_input.ymax = (input->ymax - offset_y_) * rel_y_ + 1;
  new_input.ymin = (input->ymin - offset_y_) * rel_y_;

  return BaseScaleOperation::determine_depending_area_of_interest(
      &new_input, read_operation, output);
}

void ScaleFixedSizeOperation::determine_canvas(const rcti &preferred_area, rcti &r_area)
{
  rcti local_preferred = preferred_area;
  local_preferred.xmax = local_preferred.xmin + new_width_;
  local_preferred.ymax = local_preferred.ymin + new_height_;
  rcti input_canvas = COM_AREA_NONE;
  const bool input_determined = get_input_socket(0)->determine_canvas(local_preferred,
                                                                      input_canvas);
  if (input_determined) {
    init_data(input_canvas);
    r_area = input_canvas;
    if (execution_model_ == eExecutionModel::FullFrame) {
      r_area.xmin /= rel_x_;
      r_area.ymin /= rel_y_;
      r_area.xmin += offset_x_;
      r_area.ymin += offset_y_;
    }

    r_area.xmax = r_area.xmin + new_width_;
    r_area.ymax = r_area.ymin + new_height_;
  }
}

void ScaleFixedSizeOperation::get_area_of_interest(const int input_idx,
                                                   const rcti &output_area,
                                                   rcti &r_input_area)
{
  BLI_assert(input_idx == 0);
  UNUSED_VARS_NDEBUG(input_idx);

  r_input_area.xmax = ceilf((output_area.xmax - offset_x_) * rel_x_);
  r_input_area.xmin = floorf((output_area.xmin - offset_x_) * rel_x_);
  r_input_area.ymax = ceilf((output_area.ymax - offset_y_) * rel_y_);
  r_input_area.ymin = floorf((output_area.ymin - offset_y_) * rel_y_);
  expand_area_for_sampler(r_input_area, (PixelSampler)sampler_);
}

void ScaleFixedSizeOperation::update_memory_buffer_partial(MemoryBuffer *output,
                                                           const rcti &area,
                                                           Span<MemoryBuffer *> inputs)
{
  const MemoryBuffer *input_img = inputs[0];
  PixelSampler sampler = (PixelSampler)sampler_;
  BuffersIterator<float> it = output->iterate_with({}, area);
  if (is_offset_) {
    for (; !it.is_end(); ++it) {
      const float nx = (canvas_.xmin + it.x - offset_x_) * rel_x_;
      const float ny = (canvas_.ymin + it.y - offset_y_) * rel_y_;
      input_img->read_elem_sampled(nx - canvas_.xmin, ny - canvas_.ymin, sampler, it.out);
    }
  }
  else {
    for (; !it.is_end(); ++it) {
      input_img->read_elem_sampled((canvas_.xmin + it.x) * rel_x_ - canvas_.xmin,
                                   (canvas_.ymin + it.y) * rel_y_ - canvas_.ymin,
                                   sampler,
                                   it.out);
    }
  }
}

}  // namespace blender::compositor