Blender  V3.3
COM_MovieDistortionOperation.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2011 Blender Foundation. */
3 
5 
6 #include "DNA_defaults.h"
7 
8 #include "BKE_movieclip.h"
9 
10 namespace blender::compositor {
11 
13 {
16  this->set_canvas_input_index(0);
17  input_operation_ = nullptr;
18  movie_clip_ = nullptr;
19  apply_ = distortion;
20 }
21 
23 {
24  if (movie_clip_) {
25  MovieTracking *tracking = &movie_clip_->tracking;
27  int calibration_width, calibration_height;
28 
30  BKE_movieclip_get_size(movie_clip_, &clip_user, &calibration_width, &calibration_height);
31 
32  float delta[2];
33  rcti full_frame;
34  full_frame.xmin = full_frame.ymin = 0;
35  full_frame.xmax = this->get_width();
36  full_frame.ymax = this->get_height();
38  tracking, this->get_width(), this->get_height(), &full_frame, !apply_, delta);
39 
40  /* 5 is just in case we didn't hit real max of distortion in
41  * BKE_tracking_max_undistortion_delta_across_bound
42  */
43  margin_[0] = delta[0] + 5;
44  margin_[1] = delta[1] + 5;
45 
46  calibration_width_ = calibration_width;
47  calibration_height_ = calibration_height;
48  pixel_aspect_ = tracking->camera.pixel_aspect;
49  }
50  else {
51  margin_[0] = margin_[1] = 0;
52  }
53 }
54 
56 {
57  input_operation_ = this->get_input_socket_reader(0);
58  if (movie_clip_) {
59  MovieTracking *tracking = &movie_clip_->tracking;
61  }
62  else {
63  distortion_ = nullptr;
64  }
65 }
66 
68 {
69  input_operation_ = nullptr;
70  movie_clip_ = nullptr;
71  if (distortion_ != nullptr) {
73  }
74 }
75 
77  float x,
78  float y,
79  PixelSampler /*sampler*/)
80 {
81  if (distortion_ != nullptr) {
82  /* float overscan = 0.0f; */
83  const float pixel_aspect = pixel_aspect_;
84  const float w = (float)this->get_width() /* / (1 + overscan) */;
85  const float h = (float)this->get_height() /* / (1 + overscan) */;
86  const float aspx = w / (float)calibration_width_;
87  const float aspy = h / (float)calibration_height_;
88  float in[2];
89  float out[2];
90 
91  in[0] = (x /* - 0.5 * overscan * w */) / aspx;
92  in[1] = (y /* - 0.5 * overscan * h */) / aspy / pixel_aspect;
93 
94  if (apply_) {
96  }
97  else {
99  }
100 
101  float u = out[0] * aspx /* + 0.5 * overscan * w */,
102  v = (out[1] * aspy /* + 0.5 * overscan * h */) * pixel_aspect;
103 
104  input_operation_->read_sampled(output, u, v, PixelSampler::Bilinear);
105  }
106  else {
107  input_operation_->read_sampled(output, x, y, PixelSampler::Bilinear);
108  }
109 }
110 
112  rcti *input, ReadBufferOperation *read_operation, rcti *output)
113 {
114  rcti new_input;
115  new_input.xmin = input->xmin - margin_[0];
116  new_input.ymin = input->ymin - margin_[1];
117  new_input.xmax = input->xmax + margin_[0];
118  new_input.ymax = input->ymax + margin_[1];
119  return NodeOperation::determine_depending_area_of_interest(&new_input, read_operation, output);
120 }
121 
123  const rcti &output_area,
124  rcti &r_input_area)
125 {
126  BLI_assert(input_idx == 0);
127  UNUSED_VARS_NDEBUG(input_idx);
128  r_input_area.xmin = output_area.xmin - margin_[0];
129  r_input_area.ymin = output_area.ymin - margin_[1];
130  r_input_area.xmax = output_area.xmax + margin_[0];
131  r_input_area.ymax = output_area.ymax + margin_[1];
132 }
133 
135  const rcti &area,
137 {
138  const MemoryBuffer *input_img = inputs[0];
139  if (distortion_ == nullptr) {
140  output->copy_from(input_img, area);
141  return;
142  }
143 
144  /* `float overscan = 0.0f;` */
145  const float pixel_aspect = pixel_aspect_;
146  const float w = (float)this->get_width() /* `/ (1 + overscan)` */;
147  const float h = (float)this->get_height() /* `/ (1 + overscan)` */;
148  const float aspx = w / (float)calibration_width_;
149  const float aspy = h / (float)calibration_height_;
150  float xy[2];
151  float distorted_xy[2];
152  for (BuffersIterator<float> it = output->iterate_with({}, area); !it.is_end(); ++it) {
153  xy[0] = (it.x /* `- 0.5 * overscan * w` */) / aspx;
154  xy[1] = (it.y /* `- 0.5 * overscan * h` */) / aspy / pixel_aspect;
155 
156  if (apply_) {
157  BKE_tracking_distortion_undistort_v2(distortion_, xy, distorted_xy);
158  }
159  else {
160  BKE_tracking_distortion_distort_v2(distortion_, xy, distorted_xy);
161  }
162 
163  const float u = distorted_xy[0] * aspx /* `+ 0.5 * overscan * w` */;
164  const float v = (distorted_xy[1] * aspy /* `+ 0.5 * overscan * h` */) * pixel_aspect;
165  input_img->read_elem_bilinear(u, v, it.out);
166  }
167 }
168 
169 } // namespace blender::compositor
typedef float(TangentPoint)[2]
void BKE_movieclip_user_set_frame(struct MovieClipUser *user, int framenr)
Definition: movieclip.c:1614
void BKE_movieclip_get_size(struct MovieClip *clip, struct MovieClipUser *user, int *width, int *height)
Definition: movieclip.c:1520
struct MovieDistortion * BKE_tracking_distortion_new(struct MovieTracking *tracking, int calibration_width, int calibration_height)
Definition: tracking.c:2303
void BKE_tracking_distortion_undistort_v2(struct MovieDistortion *distortion, const float co[2], float r_co[2])
Definition: tracking.c:2438
void BKE_tracking_distortion_distort_v2(struct MovieDistortion *distortion, const float co[2], float r_co[2])
Definition: tracking.c:2420
void BKE_tracking_distortion_free(struct MovieDistortion *distortion)
Definition: tracking.c:2450
void BKE_tracking_max_distortion_delta_across_bound(struct MovieTracking *tracking, int image_width, int image_height, struct rcti *rect, bool undistort, float delta[2])
Definition: tracking.c:2533
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define UNUSED_VARS_NDEBUG(...)
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:29
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
a MemoryBuffer contains access to the data of a chunk
bool determine_depending_area_of_interest(rcti *input, ReadBufferOperation *read_operation, rcti *output) override
void execute_pixel_sampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void update_memory_buffer_partial(MemoryBuffer *output, const rcti &area, Span< MemoryBuffer * > inputs) override
void get_area_of_interest(int input_idx, const rcti &output_area, rcti &r_input_area) override
Get input operation area being read by this operation on rendering given output area.
void add_output_socket(DataType datatype)
SocketReader * get_input_socket_reader(unsigned int index)
virtual bool determine_depending_area_of_interest(rcti *input, ReadBufferOperation *read_operation, rcti *output)
void read_sampled(float result[4], float x, float y, PixelSampler sampler)
void add_input_socket(DataType datatype, ResizeMode resize_mode=ResizeMode::Center)
void set_canvas_input_index(unsigned int index)
set the index of the input socket that will determine the canvas of this operation
ccl_global KernelShaderEvalInput ccl_global float * output
ccl_global KernelShaderEvalInput * input
static void area(int d1, int d2, int e1, int e2, float weights[2])
typename BuffersIteratorBuilder< T >::Iterator BuffersIterator
static const pxr::TfToken out("out", pxr::TfToken::Immortal)
static bNodeSocketTemplate inputs[]
struct MovieTracking tracking
MovieTrackingCamera camera
int ymin
Definition: DNA_vec_types.h:64
int ymax
Definition: DNA_vec_types.h:64
int xmin
Definition: DNA_vec_types.h:63
int xmax
Definition: DNA_vec_types.h:63
int xy[2]
Definition: wm_draw.c:135