Blender V4.3
rna_image_api.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include <cstdio>
10#include <cstdlib>
11#include <cstring>
12#include <ctime>
13#include <fcntl.h>
14
16
17#include "BLI_path_utils.hh"
18#include "BLI_utildefines.h"
19
20#include "RNA_define.hh"
21#include "RNA_enum_types.hh"
22
23#include "BKE_packedFile.hh"
24
25#include "rna_internal.hh" /* own include */
26
27#ifdef RNA_RUNTIME
28
29# include "BKE_image.hh"
30# include "BKE_image_format.hh"
31# include "BKE_image_save.hh"
32# include "BKE_main.hh"
33# include "BKE_scene.hh"
34# include <errno.h>
35
36# include "IMB_imbuf.hh"
37
38# include "DNA_image_types.h"
39# include "DNA_scene_types.h"
40
41# include "MEM_guardedalloc.h"
42
43static void rna_ImagePackedFile_save(ImagePackedFile *imapf, Main *bmain, ReportList *reports)
44{
46 reports, BKE_main_blendfile_path(bmain), imapf->filepath, imapf->packedfile) != RET_OK)
47 {
48 BKE_reportf(reports, RPT_ERROR, "Could not save packed file to disk as '%s'", imapf->filepath);
49 }
50}
51
52static void rna_Image_save_render(Image *image,
53 bContext *C,
54 ReportList *reports,
55 const char *path,
56 Scene *scene,
57 const int quality)
58{
59 Main *bmain = CTX_data_main(C);
60
61 if (scene == nullptr) {
62 scene = CTX_data_scene(C);
63 }
64
66
67 if (BKE_image_save_options_init(&opts, bmain, scene, image, nullptr, false, true)) {
68 opts.save_copy = true;
69 STRNCPY(opts.filepath, path);
70 if (quality != 0) {
71 opts.im_format.quality = clamp_i(quality, 0, 100);
72 }
73
74 if (!BKE_image_save(reports, bmain, image, nullptr, &opts)) {
76 reports, RPT_ERROR, "Image '%s' could not be saved to '%s'", image->id.name + 2, path);
77 }
78 }
79 else {
80 BKE_reportf(reports, RPT_ERROR, "Image '%s' does not have any image data", image->id.name + 2);
81 }
82
84
86}
87
88static void rna_Image_save(Image *image,
89 Main *bmain,
90 bContext *C,
91 ReportList *reports,
92 const char *path,
93 const int quality)
94{
95 Scene *scene = CTX_data_scene(C);
97
98 if (BKE_image_save_options_init(&opts, bmain, scene, image, nullptr, false, false)) {
99 if (path && path[0]) {
100 STRNCPY(opts.filepath, path);
101 }
102 if (quality != 0) {
103 opts.im_format.quality = clamp_i(quality, 0, 100);
104 }
105 if (!BKE_image_save(reports, bmain, image, nullptr, &opts)) {
106 BKE_reportf(reports,
107 RPT_ERROR,
108 "Image '%s' could not be saved to '%s'",
109 image->id.name + 2,
110 image->filepath);
111 }
112 }
113 else {
114 BKE_reportf(reports, RPT_ERROR, "Image '%s' does not have any image data", image->id.name + 2);
115 }
116
118
120}
121
122static void rna_Image_pack(
123 Image *image, Main *bmain, bContext *C, ReportList *reports, const char *data, int data_len)
124{
126
127 if (data) {
128 char *data_dup = static_cast<char *>(
129 MEM_mallocN(sizeof(*data_dup) * (size_t)data_len, __func__));
130 memcpy(data_dup, data, size_t(data_len));
131 BKE_image_packfiles_from_mem(reports, image, data_dup, size_t(data_len));
132 }
133 else if (BKE_image_is_dirty(image)) {
135 }
136 else {
137 BKE_image_packfiles(reports, image, ID_BLEND_PATH(bmain, &image->id));
138 }
139
141}
142
143static void rna_Image_unpack(Image *image, Main *bmain, ReportList *reports, int method)
144{
145 if (!BKE_image_has_packedfile(image)) {
146 BKE_report(reports, RPT_ERROR, "Image not packed");
147 return;
148 }
149
150 if (!ID_IS_EDITABLE(&image->id)) {
151 BKE_report(reports, RPT_ERROR, "Image is not editable");
152 return;
153 }
154
155 if (ELEM(image->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
156 BKE_report(reports, RPT_ERROR, "Unpacking movies or image sequences not supported");
157 return;
158 }
159
160 /* reports its own error on failure */
161 BKE_packedfile_unpack_image(bmain, reports, image, ePF_FileStatus(method));
162}
163
164static void rna_Image_reload(Image *image, Main *bmain)
165{
166 BKE_image_signal(bmain, image, nullptr, IMA_SIGNAL_RELOAD);
168}
169
170static void rna_Image_update(Image *image, ReportList *reports)
171{
172 ImBuf *ibuf = BKE_image_acquire_ibuf(image, nullptr, nullptr);
173
174 if (ibuf == nullptr) {
175 BKE_reportf(reports, RPT_ERROR, "Image '%s' does not have any image data", image->id.name + 2);
176 return;
177 }
178
179 if (ibuf->byte_buffer.data) {
181 }
182
185
186 BKE_image_release_ibuf(image, ibuf, nullptr);
187}
188
189static void rna_Image_scale(
190 Image *image, ReportList *reports, int width, int height, int frame, int tile_index)
191{
192 ImageUser iuser{};
193 BKE_imageuser_default(&iuser);
194 iuser.framenr = frame;
195 if (image->source == IMA_SRC_TILED) {
196 const ImageTile *tile = static_cast<ImageTile *>(BLI_findlink(&image->tiles, tile_index));
197 if (tile != nullptr) {
198 iuser.tile = tile->tile_number;
199 }
200 }
201
202 if (!BKE_image_scale(image, width, height, &iuser)) {
203 BKE_reportf(reports, RPT_ERROR, "Image '%s' failed to load image buffer", image->id.name + 2);
204 return;
205 }
208}
209
210static int rna_Image_gl_load(
211 Image *image, ReportList *reports, int frame, int layer_index, int pass_index)
212{
213 ImageUser iuser;
214 BKE_imageuser_default(&iuser);
215 iuser.framenr = frame;
216 iuser.layer = layer_index;
217 iuser.pass = pass_index;
218 if (image->rr != nullptr) {
219 BKE_image_multilayer_index(image->rr, &iuser);
220 }
221
222 GPUTexture *tex = BKE_image_get_gpu_texture(image, &iuser);
223
224 if (tex == nullptr) {
225 BKE_reportf(reports, RPT_ERROR, "Failed to load image texture '%s'", image->id.name + 2);
226 /* TODO(fclem): this error code makes no sense for vulkan. */
227 return 0x0502; /* GL_INVALID_OPERATION */
228 }
229
230 return 0; /* GL_NO_ERROR */
231}
232
233static int rna_Image_gl_touch(
234 Image *image, ReportList *reports, int frame, int layer_index, int pass_index)
235{
236 int error = 0; /* GL_NO_ERROR */
237
238 BKE_image_tag_time(image);
239
240 if (image->gputexture[TEXTARGET_2D][0] == nullptr) {
241 error = rna_Image_gl_load(image, reports, frame, layer_index, pass_index);
242 }
243
244 return error;
245}
246
247static void rna_Image_gl_free(Image *image)
248{
250
251 /* Remove the no-collect flag, image is available for garbage collection again. */
252 image->flag &= ~IMA_NOCOLLECT;
253}
254
255static void rna_Image_filepath_from_user(Image *image, ImageUser *image_user, char *filepath)
256{
257 BKE_image_user_file_path(image_user, image, filepath);
258}
259
260static void rna_Image_buffers_free(Image *image)
261{
262 BKE_image_free_buffers_ex(image, true);
263}
264
265#else
266
268{
269 FunctionRNA *func;
270
271 func = RNA_def_function(srna, "save", "rna_ImagePackedFile_save");
272 RNA_def_function_ui_description(func, "Save the packed file to its filepath");
274}
275
277{
278 FunctionRNA *func;
279 PropertyRNA *parm;
280
281 func = RNA_def_function(srna, "save_render", "rna_Image_save_render");
283 "Save image to a specific path using a scenes render settings");
285 parm = RNA_def_string_file_path(func, "filepath", nullptr, 0, "", "Output path");
287 RNA_def_pointer(func, "scene", "Scene", "", "Scene to take image parameters from");
288 RNA_def_int(func,
289 "quality",
290 0,
291 0,
292 100,
293 "Quality",
294 "Quality for image formats that support lossy compression, uses default quality if "
295 "not specified",
296 0,
297 100);
298
299 func = RNA_def_function(srna, "save", "rna_Image_save");
300 RNA_def_function_ui_description(func, "Save image");
303 "filepath",
304 nullptr,
305 0,
306 "",
307 "Output path, uses image data-block filepath if not specified");
308 RNA_def_int(func,
309 "quality",
310 0,
311 0,
312 100,
313 "Quality",
314 "Quality for image formats that support lossy compression, uses default quality if "
315 "not specified",
316 0,
317 100);
318
319 func = RNA_def_function(srna, "pack", "rna_Image_pack");
320 RNA_def_function_ui_description(func, "Pack an image as embedded data into the .blend file");
322 parm = RNA_def_property(func, "data", PROP_STRING, PROP_BYTESTRING);
323 RNA_def_property_ui_text(parm, "data", "Raw data (bytes, exact content of the embedded file)");
324 RNA_def_int(func,
325 "data_len",
326 0,
327 0,
328 INT_MAX,
329 "data_len",
330 "length of given data (mandatory if data is provided)",
331 0,
332 INT_MAX);
333
334 func = RNA_def_function(srna, "unpack", "rna_Image_unpack");
335 RNA_def_function_ui_description(func, "Save an image packed in the .blend file to disk");
338 func, "method", rna_enum_unpack_method_items, PF_USE_LOCAL, "method", "How to unpack");
339
340 func = RNA_def_function(srna, "reload", "rna_Image_reload");
342 RNA_def_function_ui_description(func, "Reload the image from its source path");
343
344 func = RNA_def_function(srna, "update", "rna_Image_update");
345 RNA_def_function_ui_description(func, "Update the display image from the floating-point buffer");
347
348 func = RNA_def_function(srna, "scale", "rna_Image_scale");
349 RNA_def_function_ui_description(func, "Scale the buffer of the image, in pixels");
351 parm = RNA_def_int(func, "width", 1, 1, INT_MAX, "", "Width", 1, INT_MAX);
353 parm = RNA_def_int(func, "height", 1, 1, INT_MAX, "", "Height", 1, INT_MAX);
355 RNA_def_int(func, "frame", 0, 0, INT_MAX, "Frame", "Frame (for image sequences)", 0, INT_MAX);
357 func, "tile_index", 0, 0, INT_MAX, "Tile", "Tile index (for tiled images)", 0, INT_MAX);
358
359 func = RNA_def_function(srna, "gl_touch", "rna_Image_gl_touch");
361 func, "Delay the image from being cleaned from the cache due inactivity");
364 func, "frame", 0, 0, INT_MAX, "Frame", "Frame of image sequence or movie", 0, INT_MAX);
365 RNA_def_int(func,
366 "layer_index",
367 0,
368 0,
369 INT_MAX,
370 "Layer",
371 "Index of layer that should be loaded",
372 0,
373 INT_MAX);
374 RNA_def_int(func,
375 "pass_index",
376 0,
377 0,
378 INT_MAX,
379 "Pass",
380 "Index of pass that should be loaded",
381 0,
382 INT_MAX);
383 /* return value */
384 parm = RNA_def_int(
385 func, "error", 0, -INT_MAX, INT_MAX, "Error", "OpenGL error value", -INT_MAX, INT_MAX);
386 RNA_def_function_return(func, parm);
387
388 func = RNA_def_function(srna, "gl_load", "rna_Image_gl_load");
390 func,
391 "Load the image into an OpenGL texture. On success, image.bindcode will contain the "
392 "OpenGL texture bindcode. Colors read from the texture will be in scene linear color space "
393 "and have premultiplied or straight alpha matching the image alpha mode.");
396 func, "frame", 0, 0, INT_MAX, "Frame", "Frame of image sequence or movie", 0, INT_MAX);
397 RNA_def_int(func,
398 "layer_index",
399 0,
400 0,
401 INT_MAX,
402 "Layer",
403 "Index of layer that should be loaded",
404 0,
405 INT_MAX);
406 RNA_def_int(func,
407 "pass_index",
408 0,
409 0,
410 INT_MAX,
411 "Pass",
412 "Index of pass that should be loaded",
413 0,
414 INT_MAX);
415 /* return value */
416 parm = RNA_def_int(
417 func, "error", 0, -INT_MAX, INT_MAX, "Error", "OpenGL error value", -INT_MAX, INT_MAX);
418 RNA_def_function_return(func, parm);
419
420 func = RNA_def_function(srna, "gl_free", "rna_Image_gl_free");
421 RNA_def_function_ui_description(func, "Free the image from OpenGL graphics memory");
422
423 /* path to an frame specified by image user */
424 func = RNA_def_function(srna, "filepath_from_user", "rna_Image_filepath_from_user");
426 func,
427 "Return the absolute path to the filepath of an image frame specified by the image user");
429 func, "image_user", "ImageUser", "", "Image user of the image to get filepath for");
430 parm = RNA_def_string_file_path(func,
431 "filepath",
432 nullptr,
433 FILE_MAX,
434 "File Path",
435 "The resulting filepath from the image and its user");
437 parm, PROP_THICK_WRAP, ParameterFlag(0)); /* needed for string return value */
438 RNA_def_function_output(func, parm);
439
440 func = RNA_def_function(srna, "buffers_free", "rna_Image_buffers_free");
441 RNA_def_function_ui_description(func, "Free the image buffers from memory");
442
443 /* TODO: pack/unpack, maybe should be generic functions? */
444}
445
446#endif
Scene * CTX_data_scene(const bContext *C)
Main * CTX_data_main(const bContext *C)
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
bool BKE_image_scale(Image *image, int width, int height, ImageUser *iuser)
RenderPass * BKE_image_multilayer_index(RenderResult *rr, ImageUser *iuser)
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
void BKE_image_user_file_path(const ImageUser *iuser, const Image *ima, char *filepath)
void BKE_image_tag_time(Image *ima)
bool BKE_image_memorypack(Image *ima)
void BKE_image_packfiles(ReportList *reports, Image *ima, const char *basepath)
void BKE_image_free_buffers_ex(Image *image, bool do_lock)
bool BKE_image_is_dirty(Image *image)
void BKE_imageuser_default(ImageUser *iuser)
void BKE_image_free_gputextures(Image *ima)
Definition image_gpu.cc:556
#define IMA_SIGNAL_RELOAD
Definition BKE_image.hh:137
bool BKE_image_has_packedfile(const Image *image)
GPUTexture * BKE_image_get_gpu_texture(Image *image, ImageUser *iuser)
Definition image_gpu.cc:476
void BKE_image_signal(Main *bmain, Image *ima, ImageUser *iuser, int signal)
void BKE_image_packfiles_from_mem(ReportList *reports, Image *ima, char *data, size_t data_len)
void BKE_image_partial_update_mark_full_update(Image *image)
Mark the whole image to be updated.
void BKE_image_free_packedfiles(Image *image)
bool BKE_image_save(ReportList *reports, Main *bmain, Image *ima, ImageUser *iuser, const ImageSaveOptions *opts)
bool BKE_image_save_options_init(ImageSaveOptions *opts, Main *bmain, Scene *scene, Image *ima, ImageUser *iuser, const bool guess_path, const bool save_as_render)
Definition image_save.cc:82
void BKE_image_save_options_free(ImageSaveOptions *opts)
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:832
int BKE_packedfile_unpack_image(Main *bmain, ReportList *reports, Image *ima, enum ePF_FileStatus how)
#define RET_OK
int BKE_packedfile_write_to_file(ReportList *reports, const char *ref_file_name, const char *filepath_rel, PackedFile *pf)
ePF_FileStatus
@ PF_USE_LOCAL
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:125
void * BLI_findlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
MINLINE int clamp_i(int value, int min, int max)
#define FILE_MAX
#define STRNCPY(dst, src)
Definition BLI_string.h:593
#define ELEM(...)
#define ID_BLEND_PATH(_bmain, _id)
Definition DNA_ID.h:647
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:658
@ IMA_SRC_MOVIE
@ IMA_SRC_TILED
@ IMA_SRC_SEQUENCE
@ TEXTARGET_2D
void IMB_rect_from_float(ImBuf *ibuf)
Definition divers.cc:694
@ IB_DISPLAY_BUFFER_INVALID
Read Guarded memory(de)allocation.
ParameterFlag
Definition RNA_types.hh:396
@ PARM_REQUIRED
Definition RNA_types.hh:397
@ FUNC_USE_REPORTS
Definition RNA_types.hh:680
@ FUNC_USE_MAIN
Definition RNA_types.hh:678
@ FUNC_USE_CONTEXT
Definition RNA_types.hh:679
@ PROP_STRING
Definition RNA_types.hh:68
PropertyFlag
Definition RNA_types.hh:201
@ PROP_THICK_WRAP
Definition RNA_types.hh:312
@ PROP_BYTESTRING
Definition RNA_types.hh:143
#define NA_EDITED
Definition WM_types.hh:550
#define NC_IMAGE
Definition WM_types.hh:351
const int tile_index
ccl_global const KernelWorkTile * tile
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
static void error(const char *str)
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier, const char *call)
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
void RNA_def_function_flag(FunctionRNA *func, int flag)
PropertyRNA * RNA_def_string_file_path(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
void RNA_def_function_output(FunctionRNA *, PropertyRNA *ret)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
void RNA_api_image(StructRNA *srna)
void RNA_api_image_packed_file(StructRNA *srna)
const EnumPropertyItem rna_enum_unpack_method_items[]
ImBufByteBuffer byte_buffer
struct PackedFile * packedfile
char filepath[1024]
ImageFormatData im_format
void WM_main_add_notifier(uint type, void *reference)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)