Blender V5.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
8
9#include <cstdlib>
10#include <cstring>
11#include <ctime>
12#include <fcntl.h>
13
14#include "BLI_path_utils.hh"
15
16#include "RNA_define.hh"
17#include "RNA_enum_types.hh"
18
19#include "BKE_packedFile.hh"
20
21#include "rna_internal.hh" /* own include */
22
23#ifdef RNA_RUNTIME
24
25# include "BLI_listbase.hh"
26# include "BLI_math_base_c.hh"
27# include "BLI_string.hh"
28
29# include "BKE_context.hh"
30# include "BKE_image.hh"
31# include "BKE_image_format.hh"
32# include "BKE_image_gpu.hh"
33# include "BKE_image_save.hh"
34# include "BKE_library.hh"
35# include "BKE_main.hh"
36
37# include "BKE_report.hh"
38# include "BKE_scene.hh"
39# include "GPU_texture.hh"
40
41# include "IMB_imbuf.hh"
42# include "IMB_partial_update.hh"
43
44# include "DNA_image_types.h"
45# include "DNA_scene_types.h"
46
47# include "MEM_guardedalloc.h"
48
49# include "WM_api.hh"
50
51namespace blender {
52
53static void rna_ImagePackedFile_save(ImagePackedFile *imapf, Main *bmain, ReportList *reports)
54{
56 reports, BKE_main_blendfile_path(bmain), imapf->filepath, imapf->packedfile) != RET_OK)
57 {
58 BKE_reportf(reports, RPT_ERROR, "Could not save packed file to disk as '%s'", imapf->filepath);
59 }
60}
61
62static void rna_Image_save_render(Image *image,
63 bContext *C,
64 ReportList *reports,
65 const char *path,
66 Scene *scene,
67 const int quality)
68{
69 Main *bmain = CTX_data_main(C);
70
71 if (scene == nullptr) {
72 scene = CTX_data_scene(C);
73 }
74
76
77 if (BKE_image_save_options_init(&opts, bmain, scene, image, nullptr, false, true)) {
78 opts.save_copy = true;
79 STRNCPY(opts.filepath, path);
80 if (quality != 0) {
81 opts.im_format.quality = clamp_i(quality, 0, 100);
82 }
83
84 if (!BKE_image_save(reports, bmain, image, nullptr, &opts)) {
86 reports, RPT_ERROR, "Image '%s' could not be saved to '%s'", image->id.name + 2, path);
87 }
88 }
89 else {
90 BKE_reportf(reports, RPT_ERROR, "Image '%s' does not have any image data", image->id.name + 2);
91 }
92
94
96}
97
98static void rna_Image_save(Image *image,
99 Main *bmain,
100 bContext *C,
101 ReportList *reports,
102 const char *path,
103 const int quality,
104 const bool save_copy)
105{
106 Scene *scene = CTX_data_scene(C);
107 ImageSaveOptions opts;
108
109 if (BKE_image_save_options_init(&opts, bmain, scene, image, nullptr, false, false)) {
110 if (path && path[0]) {
111 STRNCPY(opts.filepath, path);
112 }
113 if (quality != 0) {
114 opts.im_format.quality = clamp_i(quality, 0, 100);
115 }
116 opts.save_copy = save_copy;
117 if (!BKE_image_save(reports, bmain, image, nullptr, &opts)) {
118 BKE_reportf(reports,
119 RPT_ERROR,
120 "Image '%s' could not be saved to '%s'",
121 image->id.name + 2,
122 image->filepath);
123 }
124 }
125 else {
126 BKE_reportf(reports, RPT_ERROR, "Image '%s' does not have any image data", image->id.name + 2);
127 }
128
130
132}
133
134static void rna_Image_pack(
135 Image *image, Main *bmain, bContext *C, ReportList *reports, const char *data, int data_len)
136{
137 BKE_image_packfile_ensure(bmain, image, reports, data, data_len);
139}
140
141static void rna_Image_unpack(Image *image, Main *bmain, ReportList *reports, int method)
142{
144 BKE_report(reports, RPT_ERROR, "Image not packed");
145 return;
146 }
147
148 if (!ID_IS_EDITABLE(&image->id)) {
149 BKE_report(reports, RPT_ERROR, "Image is not editable");
150 return;
151 }
152
153 if (ELEM(image->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
154 BKE_report(reports, RPT_ERROR, "Unpacking movies or image sequences not supported");
155 return;
156 }
157
158 /* reports its own error on failure */
159 BKE_packedfile_unpack_image(bmain, reports, image, ePF_FileStatus(method));
160}
161
162static void rna_Image_reload(Image *image, Main *bmain)
163{
164 BKE_image_signal(bmain, image, nullptr, IMA_SIGNAL_RELOAD);
166}
167
168static void rna_Image_update(Image *image, ReportList *reports)
169{
170 ImBuf *ibuf = BKE_image_acquire_ibuf(image, nullptr, nullptr);
171
172 if (ibuf == nullptr) {
173 BKE_reportf(reports, RPT_ERROR, "Image '%s' does not have any image data", image->id.name + 2);
174 return;
175 }
176
177 if (ibuf->byte_data()) {
179 }
180
182
183 BKE_image_release_ibuf(image, ibuf, nullptr);
184}
185
186static void rna_Image_scale(
187 Image *image, ReportList *reports, int width, int height, int frame, int tile_index)
188{
189 ImageUser iuser{};
190 BKE_imageuser_default(&iuser);
191 iuser.framenr = frame;
192 if (image->source == IMA_SRC_TILED) {
193 const ImageTile *tile = static_cast<ImageTile *>(BLI_findlink(&image->tiles, tile_index));
194 if (tile != nullptr) {
195 iuser.tile = tile->tile_number;
196 }
197 }
198
199 if (!BKE_image_scale(image, width, height, &iuser)) {
200 BKE_reportf(reports, RPT_ERROR, "Image '%s' failed to load image buffer", image->id.name + 2);
201 return;
202 }
204}
205
206static int rna_Image_gl_load(
207 Image *image, ReportList *reports, int frame, int layer_index, int pass_index)
208{
209 ImageUser iuser;
210 BKE_imageuser_default(&iuser);
211 iuser.framenr = frame;
212 iuser.layer = layer_index;
213 iuser.pass = pass_index;
214 if (image->rr != nullptr) {
216 }
217
219
220 if (tex == nullptr) {
221 BKE_reportf(reports, RPT_ERROR, "Failed to load image texture '%s'", image->id.name + 2);
222 /* TODO(fclem): this error code makes no sense for vulkan. */
223 return 0x0502; /* GL_INVALID_OPERATION */
224 }
225
226 GPU_texture_free(tex);
227
228 return 0; /* GL_NO_ERROR */
229}
230
231static int rna_Image_gl_touch(
232 Image *image, ReportList *reports, int frame, int layer_index, int pass_index)
233{
234 /* Load tags as well, so this is the same and effectively was already since the
235 * initial implementation. */
236 return rna_Image_gl_load(image, reports, frame, layer_index, pass_index);
237}
238
239static void rna_Image_gl_free(Image *image)
240{
242
243 /* Remove the no-collect flag, image is available for garbage collection again. */
244 image->flag &= ~IMA_NOCOLLECT;
245}
246
247static void rna_Image_filepath_from_user(Image *image, ImageUser *image_user, char *filepath)
248{
249 BKE_image_user_file_path(image_user, image, filepath);
250}
251
252static void rna_Image_buffers_free(Image *image)
253{
255}
256
257} // namespace blender
258
259#else
260
261namespace blender {
262
264{
265 FunctionRNA *func;
266
267 func = RNA_def_function(srna, "save", "rna_ImagePackedFile_save");
268 RNA_def_function_ui_description(func, "Save the packed file to its filepath");
270}
271
273{
274 FunctionRNA *func;
275 PropertyRNA *parm;
276
277 func = RNA_def_function(srna, "save_render", "rna_Image_save_render");
279 "Save image to a specific path using a scenes render settings");
281 parm = RNA_def_string_file_path(func, "filepath", nullptr, 0, "", "Output path");
283 RNA_def_pointer(func, "scene", "Scene", "", "Scene to take image parameters from");
284 RNA_def_int(func,
285 "quality",
286 0,
287 0,
288 100,
289 "Quality",
290 "Quality for image formats that support lossy compression, uses default quality if "
291 "not specified",
292 0,
293 100);
294
295 func = RNA_def_function(srna, "save", "rna_Image_save");
296 RNA_def_function_ui_description(func, "Save image");
299 "filepath",
300 nullptr,
301 0,
302 "",
303 "Output path, uses image data-block filepath if not specified");
304 RNA_def_int(func,
305 "quality",
306 0,
307 0,
308 100,
309 "Quality",
310 "Quality for image formats that support lossy compression, uses default quality if "
311 "not specified",
312 0,
313 100);
314 RNA_def_boolean(func,
315 "save_copy",
316 false,
317 "Save Copy",
318 "Save the image as a copy, without updating current image's filepath");
319
320 func = RNA_def_function(srna, "pack", "rna_Image_pack");
321 RNA_def_function_ui_description(func, "Pack an image as embedded data into the .blend file");
323 parm = RNA_def_property(func, "data", PROP_STRING, PROP_BYTESTRING);
324 RNA_def_property_ui_text(parm, "data", "Raw data (bytes, exact content of the embedded file)");
325 RNA_def_int(func,
326 "data_len",
327 0,
328 0,
329 INT_MAX,
330 "data_len",
331 "length of given data (mandatory if data is provided)",
332 0,
333 INT_MAX);
334
335 func = RNA_def_function(srna, "unpack", "rna_Image_unpack");
336 RNA_def_function_ui_description(func, "Save an image packed in the .blend file to disk");
339 func, "method", rna_enum_unpack_method_items, PF_USE_LOCAL, "method", "How to unpack");
340
341 func = RNA_def_function(srna, "reload", "rna_Image_reload");
343 RNA_def_function_ui_description(func, "Reload the image from its source path");
344
345 func = RNA_def_function(srna, "update", "rna_Image_update");
346 RNA_def_function_ui_description(func, "Update the display image from the floating-point buffer");
348
349 func = RNA_def_function(srna, "scale", "rna_Image_scale");
350 RNA_def_function_ui_description(func, "Scale the buffer of the image, in pixels");
352 parm = RNA_def_int(func, "width", 1, 1, INT_MAX, "", "Width", 1, INT_MAX);
354 parm = RNA_def_int(func, "height", 1, 1, INT_MAX, "", "Height", 1, INT_MAX);
356 RNA_def_int(func, "frame", 0, 0, INT_MAX, "Frame", "Frame (for image sequences)", 0, INT_MAX);
358 func, "tile_index", 0, 0, INT_MAX, "Tile", "Tile index (for tiled images)", 0, INT_MAX);
359
360 func = RNA_def_function(srna, "gl_touch", "rna_Image_gl_touch");
362 func, "Delay the image from being cleaned from the cache due inactivity");
365 func, "frame", 0, 0, INT_MAX, "Frame", "Frame of image sequence or movie", 0, INT_MAX);
366 RNA_def_int(func,
367 "layer_index",
368 0,
369 0,
370 INT_MAX,
371 "Layer",
372 "Index of layer that should be loaded",
373 0,
374 INT_MAX);
375 RNA_def_int(func,
376 "pass_index",
377 0,
378 0,
379 INT_MAX,
380 "Pass",
381 "Index of pass that should be loaded",
382 0,
383 INT_MAX);
384 /* return value */
385 parm = RNA_def_int(
386 func, "error", 0, INT_MIN, INT_MAX, "Error", "OpenGL error value", INT_MIN, INT_MAX);
387 RNA_def_function_return(func, parm);
388
389 func = RNA_def_function(srna, "gl_load", "rna_Image_gl_load");
391 func,
392 "Load the image into an OpenGL texture. On success, image.bindcode will contain the "
393 "OpenGL texture bindcode. Colors read from the texture will be in scene linear color space "
394 "and have premultiplied or straight alpha matching the image alpha mode.");
397 func, "frame", 0, 0, INT_MAX, "Frame", "Frame of image sequence or movie", 0, INT_MAX);
398 RNA_def_int(func,
399 "layer_index",
400 0,
401 0,
402 INT_MAX,
403 "Layer",
404 "Index of layer that should be loaded",
405 0,
406 INT_MAX);
407 RNA_def_int(func,
408 "pass_index",
409 0,
410 0,
411 INT_MAX,
412 "Pass",
413 "Index of pass that should be loaded",
414 0,
415 INT_MAX);
416 /* return value */
417 parm = RNA_def_int(
418 func, "error", 0, INT_MIN, INT_MAX, "Error", "OpenGL error value", INT_MIN, INT_MAX);
419 RNA_def_function_return(func, parm);
420
421 func = RNA_def_function(srna, "gl_free", "rna_Image_gl_free");
422 RNA_def_function_ui_description(func, "Free the image from OpenGL graphics memory");
423
424 /* path to an frame specified by image user */
425 func = RNA_def_function(srna, "filepath_from_user", "rna_Image_filepath_from_user");
427 func,
428 "Return the absolute path to the filepath of an image frame specified by the image user");
430 func, "image_user", "ImageUser", "", "Image user of the image to get filepath for");
431 parm = RNA_def_string_file_path(func,
432 "filepath",
433 nullptr,
434 FILE_MAX,
435 "File Path",
436 "The resulting filepath from the image and its user");
438 parm, PROP_THICK_WRAP, ParameterFlag(0)); /* needed for string return value */
439 RNA_def_function_output(func, parm);
440
441 func = RNA_def_function(srna, "buffers_free", "rna_Image_buffers_free");
442 RNA_def_function_ui_description(func, "Free the image buffers from memory");
443
444 /* TODO: pack/unpack, maybe should be generic functions? */
445}
446
447} // namespace blender
448
449#endif
#define IMA_SIGNAL_RELOAD
Definition BKE_image.hh:178
#define RET_OK
#define FILE_MAX
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NA_EDITED
Definition WM_types.hh:634
#define NC_IMAGE
Definition WM_types.hh:397
#define ELEM(...)
#define INT_MAX
const int tile_index
const ccl_global KernelWorkTile * tile
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
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_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
char * STRNCPY(char(&dst)[N], const char *src)
bool BKE_image_scale(Image *image, int width, int height, ImageUser *iuser)
void RNA_def_function_output(FunctionRNA *, PropertyRNA *ret)
void RNA_api_image_packed_file(StructRNA *srna)
void BKE_image_signal(Main *bmain, Image *ima, ImageUser *iuser, int signal)
void BKE_image_free_gpu_texture_caches(Image *ima)
Definition image_gpu.cc:818
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
@ FUNC_USE_REPORTS
@ FUNC_USE_MAIN
@ FUNC_USE_CONTEXT
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
void BKE_image_user_file_path(const ImageUser *iuser, const Image *ima, char *filepath)
bool BKE_image_save_options_init(ImageSaveOptions *opts, Main *bmain, Scene *scene, Image *ima, ImageUser *iuser, bool guess_path, bool save_as_render)
Definition image_save.cc:51
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier_c_str, int type, int subtype)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
@ PROP_STRING
Definition RNA_types.hh:213
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
RenderPass * BKE_image_multilayer_index(RenderResult *rr, ImageUser *iuser)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
int BKE_packedfile_unpack_image(Main *bmain, ReportList *reports, Image *ima, enum ePF_FileStatus how)
BMesh const char void * data
void BKE_image_free_buffers_ex(Image *image, bool do_lock)
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
void IMB_partial_update_mark_full(ImBuf *ibuf)
const EnumPropertyItem rna_enum_unpack_method_items[]
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier_c_str, const char *call)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
void BKE_image_packfile_ensure(Main *bmain, Image *image, ReportList *reports, const char *data, int data_len)
void RNA_def_function_flag(FunctionRNA *func, FunctionFlag flag)
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 BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
Main * CTX_data_main(const bContext *C)
void IMB_byte_from_float(ImBuf *ibuf)
void GPU_texture_free(gpu::Texture *texture)
void BKE_image_save_options_free(ImageSaveOptions *opts)
@ PROP_THICK_WRAP
Definition RNA_types.hh:493
Scene * CTX_data_scene(const bContext *C)
gpu::Texture * BKE_image_acquire_gpu_texture(Image *image, ImageUser *iuser)
Definition image_gpu.cc:732
bool BKE_image_has_packedfile(const Image *image)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
@ PROP_BYTESTRING
Definition RNA_types.hh:295
@ PARM_REQUIRED
Definition RNA_types.hh:631
void RNA_api_image(StructRNA *srna)
void BKE_imageuser_default(ImageUser *iuser)
MINLINE int clamp_i(int value, int min, int max)
bool BKE_image_save(ReportList *reports, Main *bmain, Image *ima, ImageUser *iuser, const ImageSaveOptions *opts)
int BKE_packedfile_write_to_file(ReportList *reports, const char *ref_file_name, const char *filepath_rel, PackedFile *pf)
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)