Blender V5.3
node_composite_crop.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_bounds.hh"
6#include "BLI_bounds_types.hh"
8
9#include "DNA_node_types.h"
10
11#include "BKE_node.hh"
12
13#include "GPU_shader.hh"
14
15#include "COM_node_operation.hh"
16#include "COM_utilities.hh"
17
19
21
23{
24 b.use_custom_socket_order();
25 b.allow_any_socket_order();
26 b.add_input<decl::Color>("Image"_ustr)
27 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
28 .hide_value()
29 .structure_type(StructureType::Dynamic);
30 b.add_output<decl::Color>("Image"_ustr)
31 .structure_type(StructureType::Dynamic)
32 .align_with_previous();
33
34 b.add_input<decl::Int>("X"_ustr)
35 .default_value(0)
36 .min(0)
37
38 .description("The X position of the lower left corner of the crop region");
39 b.add_input<decl::Int>("Y"_ustr)
40 .default_value(0)
41 .min(0)
42
43 .description("The Y position of the lower left corner of the crop region");
44 b.add_input<decl::Int>("Width"_ustr)
45 .default_value(1920)
47 .min(1)
48
49 .description("The width of the crop region");
50 b.add_input<decl::Int>("Height"_ustr)
51 .default_value(1080)
53 .min(1)
54
55 .description("The height of the crop region");
56 b.add_input<decl::Bool>("Alpha Crop"_ustr)
57 .default_value(false)
58
60 "Sets the areas outside of the crop region to be transparent instead of actually "
61 "cropping the size of the image");
62}
63
64using namespace blender::compositor;
65
67 public:
69
70 void execute() override
71 {
72 if (this->is_identity()) {
73 const Result &input = this->get_input("Image");
74 Result &output = this->get_result("Image");
75 output.share_data(input);
76 return;
77 }
78
79 if (this->is_alpha_crop()) {
80 this->execute_alpha_crop();
81 }
82 else {
83 this->execute_image_crop();
84 }
85 }
86
87 /* Crop by replacing areas outside of the cropping bounds with zero alpha. The output have the
88 * same domain as the input image. */
90 {
91 if (this->context().use_gpu()) {
93 }
94 else {
96 }
97 }
98
100 {
101 gpu::Shader *shader = this->context().get_shader("compositor_alpha_crop");
103
105 GPU_shader_uniform_2iv(shader, "lower_bound", bounds.min);
106 GPU_shader_uniform_2iv(shader, "upper_bound", bounds.max);
107
108 const Result &input_image = this->get_input("Image");
109 input_image.bind_as_texture(shader, "input_tx");
110
111 const Domain domain = this->compute_domain();
112
113 Result &output_image = this->get_result("Image");
114 output_image.allocate_texture(domain);
115 output_image.bind_as_image(shader, "output_img");
116
118
119 input_image.unbind_as_texture();
120 output_image.unbind_as_image();
122 }
123
125 {
127
128 const Result &input = this->get_input("Image");
129
130 const Domain domain = this->compute_domain();
131 Result &output = this->get_result("Image");
132 output.allocate_texture(domain);
133
134 parallel_for(domain.data_size, [&](const int2 texel) {
135 /* The lower bound is inclusive and upper bound is exclusive. */
136 bool is_inside = texel.x >= bounds.min.x && texel.y >= bounds.min.y &&
137 texel.x < bounds.max.x && texel.y < bounds.max.y;
138 /* Write the pixel color if it is inside the cropping region, otherwise, write zero. */
139 float4 color = is_inside ? float4(input.load_pixel<Color>(texel)) : float4(0.0f);
140 output.store_pixel(texel, Color(color));
141 });
142 }
143
144 /* Crop the image into a new size that matches the cropping bounds. */
146 {
147 if (this->context().use_gpu()) {
149 }
150 else {
152 }
153 }
154
156 {
158
159 gpu::Shader *shader = this->context().get_shader("compositor_image_crop_float4");
161
162 GPU_shader_uniform_2iv(shader, "lower_bound", bounds.min);
163
164 const Result &input_image = this->get_input("Image");
165 input_image.bind_as_texture(shader, "input_tx");
166
167 const int2 size = bounds.size();
168
169 Result &output_image = this->get_result("Image");
170 output_image.allocate_texture(Domain(size, this->compute_domain().transformation));
171 output_image.bind_as_image(shader, "output_img");
172
174
175 input_image.unbind_as_texture();
176 output_image.unbind_as_image();
178 }
179
181 {
183
184 const Result &input = this->get_input("Image");
185
186 const int2 size = bounds.size();
187 Result &output = this->get_result("Image");
188 output.allocate_texture(Domain(size, this->compute_domain().transformation));
189
190 parallel_for(size, [&](const int2 texel) {
191 output.store_pixel(texel, input.load_pixel<Color>(texel + bounds.min));
192 });
193 }
194
195 /* Returns true if the operation does nothing and the input can be passed through. */
197 {
198 const Result &input = this->get_input("Image");
199 /* Single value inputs can't be cropped and are returned as is. */
200 if (input.is_single_value()) {
201 return true;
202 }
203
205 const int2 input_size = input.domain().data_size;
206 /* The cropping bounds cover the whole image, so no cropping happens. */
207 if (bounds.min == int2(0) && bounds.max == input_size) {
208 return true;
209 }
210
211 return false;
212 }
213
215 {
216 const int2 input_size = this->get_input("Image").domain().data_size;
217
218 const int x = math::clamp(
219 this->get_input("X").get_single_value_default<int>(), 0, input_size.x - 1);
220 const int y = math::clamp(
221 this->get_input("Y").get_single_value_default<int>(), 0, input_size.y - 1);
222 const int width = math::max(1, this->get_input("Width").get_single_value_default<int>());
223 const int height = math::max(1, this->get_input("Height").get_single_value_default<int>());
224
225 const Bounds<int2> input_bounds = Bounds<int2>(int2(0), input_size);
226
227 const Bounds<int2> crop_bounds = Bounds<int2>(int2(x, y), int2(x + width, y + height));
228 return *bounds::intersect(crop_bounds, input_bounds);
229 }
230
231 /* If true, the region outside of the cropping bounds will be set to a zero alpha value instead
232 * of actually cropping the size of the image. */
234 {
235 return this->get_input("Alpha Crop").get_single_value_default<bool>();
236 }
237};
238
240{
241 return new CropOperation(context, node);
242}
243
244static void node_register()
245{
246 static bke::bNodeType ntype;
247
248 cmp_node_type_base(&ntype, "CompositorNodeCrop"_ustr, CMP_NODE_CROP);
249 ntype.ui_name = "Crop";
250 ntype.ui_description =
251 "Crops image to a smaller region, either making the cropped area transparent or resizing "
252 "the image";
253 ntype.enum_name_legacy = "CROP";
255 ntype.declare = node_declare;
257
259}
261
262} // namespace blender::nodes::node_composite_crop_cc
#define NODE_CLASS_DISTORT
Definition BKE_node.hh:486
#define CMP_NODE_CROP
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define output
gpu::Shader * get_shader(const char *info_name, ResultPrecision precision)
Domain(const int2 &size)
Definition domain.cc:20
NodeOperation(Context &context, const bNode &node)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Context & context() const
Definition operation.cc:57
Result & get_input(StringRef identifier) const
Definition operation.cc:35
virtual Domain compute_domain()
Definition operation.cc:62
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
Definition result.cc:554
void unbind_as_texture() const
Definition result.cc:964
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:854
T load_pixel(const int2 &texel) const
void unbind_as_image() const
Definition result.cc:990
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
Definition result.cc:951
NodeOperation(Context &context, const bNode &node)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
std::optional< Bounds< T > > intersect(const Bounds< T > &a, const Bounds< T > &b)
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
void parallel_for(const int2 range, const Function &function)
T clamp(const T &a, const T &min, const T &max)
T max(const T &a, const T &b)
static void node_declare(NodeDeclarationBuilder &b)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
void GPU_shader_uniform_2iv(gpu::Shader *sh, const char *name, const int data[2])
void GPU_shader_unbind()
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
Bounds()=default
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393