Blender  V3.3
rna_image_api.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2009 Blender Foundation. All rights reserved. */
3 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12 
13 #include "DNA_packedFile_types.h"
14 
15 #include "BLI_path_util.h"
16 #include "BLI_utildefines.h"
17 
18 #include "RNA_define.h"
19 #include "RNA_enum_types.h"
20 
21 #include "BKE_packedFile.h"
22 
23 #include "rna_internal.h" /* own include */
24 
25 #ifdef RNA_RUNTIME
26 
27 # include "BKE_image.h"
28 # include "BKE_image_format.h"
29 # include "BKE_image_save.h"
30 # include "BKE_main.h"
31 # include "BKE_scene.h"
32 # include <errno.h>
33 
34 # include "IMB_colormanagement.h"
35 # include "IMB_imbuf.h"
36 
37 # include "DNA_image_types.h"
38 # include "DNA_scene_types.h"
39 
40 # include "MEM_guardedalloc.h"
41 
42 static void rna_ImagePackedFile_save(ImagePackedFile *imapf, Main *bmain, ReportList *reports)
43 {
45  reports, BKE_main_blendfile_path(bmain), imapf->filepath, imapf->packedfile, 0) !=
46  RET_OK) {
47  BKE_reportf(reports, RPT_ERROR, "Could not save packed file to disk as '%s'", imapf->filepath);
48  }
49 }
50 
51 static void rna_Image_save_render(
52  Image *image, bContext *C, ReportList *reports, const char *path, Scene *scene)
53 {
54  Main *bmain = CTX_data_main(C);
55 
56  if (scene == NULL) {
58  }
59 
60  ImageSaveOptions opts;
61 
62  if (BKE_image_save_options_init(&opts, bmain, scene, image, NULL, false, true)) {
63  opts.save_copy = true;
64  STRNCPY(opts.filepath, path);
65 
66  if (!BKE_image_save(reports, bmain, image, NULL, &opts)) {
68  reports, RPT_ERROR, "Image '%s' could not be saved to '%s'", image->id.name + 2, path);
69  }
70  }
71  else {
72  BKE_reportf(reports, RPT_ERROR, "Image '%s' does not have any image data", image->id.name + 2);
73  }
74 
76 
78 }
79 
80 static void rna_Image_save(Image *image, Main *bmain, bContext *C, ReportList *reports)
81 {
83  ImageSaveOptions opts;
84 
85  if (BKE_image_save_options_init(&opts, bmain, scene, image, NULL, false, false)) {
86  if (!BKE_image_save(reports, bmain, image, NULL, &opts)) {
87  BKE_reportf(reports,
88  RPT_ERROR,
89  "Image '%s' could not be saved to '%s'",
90  image->id.name + 2,
91  image->filepath);
92  }
93  }
94  else {
95  BKE_reportf(reports, RPT_ERROR, "Image '%s' does not have any image data", image->id.name + 2);
96  }
97 
99 
101 }
102 
103 static void rna_Image_pack(
104  Image *image, Main *bmain, bContext *C, ReportList *reports, const char *data, int data_len)
105 {
107 
108  if (data) {
109  char *data_dup = MEM_mallocN(sizeof(*data_dup) * (size_t)data_len, __func__);
110  memcpy(data_dup, data, (size_t)data_len);
111  BKE_image_packfiles_from_mem(reports, image, data_dup, (size_t)data_len);
112  }
113  else if (BKE_image_is_dirty(image)) {
115  }
116  else {
117  BKE_image_packfiles(reports, image, ID_BLEND_PATH(bmain, &image->id));
118  }
119 
121 }
122 
123 static void rna_Image_unpack(Image *image, Main *bmain, ReportList *reports, int method)
124 {
126  BKE_report(reports, RPT_ERROR, "Image not packed");
127  }
128  else if (ELEM(image->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
129  BKE_report(reports, RPT_ERROR, "Unpacking movies or image sequences not supported");
130  return;
131  }
132  else {
133  /* reports its own error on failure */
134  BKE_packedfile_unpack_image(bmain, reports, image, method);
135  }
136 }
137 
138 static void rna_Image_reload(Image *image, Main *bmain)
139 {
142 }
143 
144 static void rna_Image_update(Image *image, ReportList *reports)
145 {
147 
148  if (ibuf == NULL) {
149  BKE_reportf(reports, RPT_ERROR, "Image '%s' does not have any image data", image->id.name + 2);
150  return;
151  }
152 
153  if (ibuf->rect) {
154  IMB_rect_from_float(ibuf);
155  }
156 
159 
161 }
162 
163 static void rna_Image_scale(Image *image, ReportList *reports, int width, int height)
164 {
165  if (!BKE_image_scale(image, width, height)) {
166  BKE_reportf(reports, RPT_ERROR, "Image '%s' does not have any image data", image->id.name + 2);
167  return;
168  }
171 }
172 
173 static int rna_Image_gl_load(
174  Image *image, ReportList *reports, int frame, int layer_index, int pass_index)
175 {
176  ImageUser iuser;
177  BKE_imageuser_default(&iuser);
178  iuser.framenr = frame;
179  iuser.layer = layer_index;
180  iuser.pass = pass_index;
181  if (image->rr != NULL) {
182  BKE_image_multilayer_index(image->rr, &iuser);
183  }
184 
186 
187  if (tex == NULL) {
188  BKE_reportf(reports, RPT_ERROR, "Failed to load image texture '%s'", image->id.name + 2);
189  /* TODO(fclem): this error code makes no sense for vulkan. */
190  return 0x0502; /* GL_INVALID_OPERATION */
191  }
192 
193  return 0; /* GL_NO_ERROR */
194 }
195 
196 static int rna_Image_gl_touch(
197  Image *image, ReportList *reports, int frame, int layer_index, int pass_index)
198 {
199  int error = 0; /* GL_NO_ERROR */
200 
202 
203  if (image->gputexture[TEXTARGET_2D][0] == NULL) {
204  error = rna_Image_gl_load(image, reports, frame, layer_index, pass_index);
205  }
206 
207  return error;
208 }
209 
210 static void rna_Image_gl_free(Image *image)
211 {
213 
214  /* remove the nocollect flag, image is available for garbage collection again */
215  image->flag &= ~IMA_NOCOLLECT;
216 }
217 
218 static void rna_Image_filepath_from_user(Image *image, ImageUser *image_user, char *filepath)
219 {
220  BKE_image_user_file_path(image_user, image, filepath);
221 }
222 
223 static void rna_Image_buffers_free(Image *image)
224 {
226 }
227 
228 #else
229 
231 {
232  FunctionRNA *func;
233 
234  func = RNA_def_function(srna, "save", "rna_ImagePackedFile_save");
235  RNA_def_function_ui_description(func, "Save the packed file to its filepath");
237 }
238 
240 {
241  FunctionRNA *func;
242  PropertyRNA *parm;
243 
244  func = RNA_def_function(srna, "save_render", "rna_Image_save_render");
246  "Save image to a specific path using a scenes render settings");
248  parm = RNA_def_string_file_path(func, "filepath", NULL, 0, "", "Save path");
250  RNA_def_pointer(func, "scene", "Scene", "", "Scene to take image parameters from");
251 
252  func = RNA_def_function(srna, "save", "rna_Image_save");
253  RNA_def_function_ui_description(func, "Save image to its source path");
255 
256  func = RNA_def_function(srna, "pack", "rna_Image_pack");
257  RNA_def_function_ui_description(func, "Pack an image as embedded data into the .blend file");
259  parm = RNA_def_property(func, "data", PROP_STRING, PROP_BYTESTRING);
260  RNA_def_property_ui_text(parm, "data", "Raw data (bytes, exact content of the embedded file)");
261  RNA_def_int(func,
262  "data_len",
263  0,
264  0,
265  INT_MAX,
266  "data_len",
267  "length of given data (mandatory if data is provided)",
268  0,
269  INT_MAX);
270 
271  func = RNA_def_function(srna, "unpack", "rna_Image_unpack");
272  RNA_def_function_ui_description(func, "Save an image packed in the .blend file to disk");
274  RNA_def_enum(
275  func, "method", rna_enum_unpack_method_items, PF_USE_LOCAL, "method", "How to unpack");
276 
277  func = RNA_def_function(srna, "reload", "rna_Image_reload");
279  RNA_def_function_ui_description(func, "Reload the image from its source path");
280 
281  func = RNA_def_function(srna, "update", "rna_Image_update");
282  RNA_def_function_ui_description(func, "Update the display image from the floating-point buffer");
284 
285  func = RNA_def_function(srna, "scale", "rna_Image_scale");
286  RNA_def_function_ui_description(func, "Scale the buffer of the image, in pixels");
288  parm = RNA_def_int(func, "width", 1, 1, INT_MAX, "", "Width", 1, INT_MAX);
290  parm = RNA_def_int(func, "height", 1, 1, INT_MAX, "", "Height", 1, INT_MAX);
292 
293  func = RNA_def_function(srna, "gl_touch", "rna_Image_gl_touch");
295  func, "Delay the image from being cleaned from the cache due inactivity");
297  RNA_def_int(
298  func, "frame", 0, 0, INT_MAX, "Frame", "Frame of image sequence or movie", 0, INT_MAX);
299  RNA_def_int(func,
300  "layer_index",
301  0,
302  0,
303  INT_MAX,
304  "Layer",
305  "Index of layer that should be loaded",
306  0,
307  INT_MAX);
308  RNA_def_int(func,
309  "pass_index",
310  0,
311  0,
312  INT_MAX,
313  "Pass",
314  "Index of pass that should be loaded",
315  0,
316  INT_MAX);
317  /* return value */
318  parm = RNA_def_int(
319  func, "error", 0, -INT_MAX, INT_MAX, "Error", "OpenGL error value", -INT_MAX, INT_MAX);
320  RNA_def_function_return(func, parm);
321 
322  func = RNA_def_function(srna, "gl_load", "rna_Image_gl_load");
324  func,
325  "Load the image into an OpenGL texture. On success, image.bindcode will contain the "
326  "OpenGL texture bindcode. Colors read from the texture will be in scene linear color space "
327  "and have premultiplied or straight alpha matching the image alpha mode");
329  RNA_def_int(
330  func, "frame", 0, 0, INT_MAX, "Frame", "Frame of image sequence or movie", 0, INT_MAX);
331  RNA_def_int(func,
332  "layer_index",
333  0,
334  0,
335  INT_MAX,
336  "Layer",
337  "Index of layer that should be loaded",
338  0,
339  INT_MAX);
340  RNA_def_int(func,
341  "pass_index",
342  0,
343  0,
344  INT_MAX,
345  "Pass",
346  "Index of pass that should be loaded",
347  0,
348  INT_MAX);
349  /* return value */
350  parm = RNA_def_int(
351  func, "error", 0, -INT_MAX, INT_MAX, "Error", "OpenGL error value", -INT_MAX, INT_MAX);
352  RNA_def_function_return(func, parm);
353 
354  func = RNA_def_function(srna, "gl_free", "rna_Image_gl_free");
355  RNA_def_function_ui_description(func, "Free the image from OpenGL graphics memory");
356 
357  /* path to an frame specified by image user */
358  func = RNA_def_function(srna, "filepath_from_user", "rna_Image_filepath_from_user");
360  func,
361  "Return the absolute path to the filepath of an image frame specified by the image user");
363  func, "image_user", "ImageUser", "", "Image user of the image to get filepath for");
364  parm = RNA_def_string_file_path(func,
365  "filepath",
366  NULL,
367  FILE_MAX,
368  "File Path",
369  "The resulting filepath from the image and its user");
370  RNA_def_parameter_flags(parm, PROP_THICK_WRAP, 0); /* needed for string return value */
371  RNA_def_function_output(func, parm);
372 
373  func = RNA_def_function(srna, "buffers_free", "rna_Image_buffers_free");
374  RNA_def_function_ui_description(func, "Free the image buffers from memory");
375 
376  /* TODO: pack/unpack, maybe should be generic functions? */
377 }
378 
379 #endif
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1090
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1074
void BKE_image_release_ibuf(struct Image *ima, struct ImBuf *ibuf, void *lock)
void BKE_image_free_buffers_ex(struct Image *image, bool do_lock)
void BKE_image_user_file_path(const struct ImageUser *iuser, const struct Image *ima, char *path)
struct ImBuf * BKE_image_acquire_ibuf(struct Image *ima, struct ImageUser *iuser, void **r_lock)
void BKE_image_partial_update_mark_full_update(struct Image *image)
Mark the whole image to be updated.
bool BKE_image_scale(struct Image *image, int width, int height)
void BKE_image_packfiles(struct ReportList *reports, struct Image *ima, const char *basepath)
bool BKE_image_memorypack(struct Image *ima)
struct RenderPass * BKE_image_multilayer_index(struct RenderResult *rr, struct ImageUser *iuser)
void BKE_image_free_gputextures(struct Image *ima)
Definition: image_gpu.cc:516
bool BKE_image_has_packedfile(const struct Image *image)
struct GPUTexture * BKE_image_get_gpu_texture(struct Image *image, struct ImageUser *iuser, struct ImBuf *ibuf)
Definition: image_gpu.cc:438
bool BKE_image_is_dirty(struct Image *image)
#define IMA_SIGNAL_RELOAD
Definition: BKE_image.h:129
void BKE_image_signal(struct Main *bmain, struct Image *ima, struct ImageUser *iuser, int signal)
void BKE_image_packfiles_from_mem(struct ReportList *reports, struct Image *ima, char *data, size_t data_len)
void BKE_image_free_packedfiles(struct Image *image)
void BKE_imageuser_default(struct ImageUser *iuser)
void BKE_image_tag_time(struct Image *ima)
bool BKE_image_save(struct ReportList *reports, struct Main *bmain, struct Image *ima, struct ImageUser *iuser, const struct ImageSaveOptions *opts)
bool BKE_image_save_options_init(ImageSaveOptions *opts, struct Main *bmain, struct Scene *scene, struct Image *ima, struct ImageUser *iuser, const bool guess_path, const bool save_as_render)
Definition: image_save.cc:76
void BKE_image_save_options_free(struct ImageSaveOptions *opts)
Definition: image_save.cc:241
const char * BKE_main_blendfile_path(const struct Main *bmain) ATTR_NONNULL()
int BKE_packedfile_write_to_file(struct ReportList *reports, const char *ref_file_name, const char *filepath, struct PackedFile *pf, bool guimode)
Definition: packedFile.c:286
int BKE_packedfile_unpack_image(struct Main *bmain, struct ReportList *reports, struct Image *ima, enum ePF_FileStatus how)
Definition: packedFile.c:620
#define RET_OK
@ 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.c:83
#define FILE_MAX
#define STRNCPY(dst, src)
Definition: BLI_string.h:483
#define ELEM(...)
#define ID_BLEND_PATH(_bmain, _id)
Definition: DNA_ID.h:559
@ IMA_NOCOLLECT
@ IMA_SRC_MOVIE
@ IMA_SRC_SEQUENCE
@ TEXTARGET_2D
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
_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 GLsizei width
struct GPUTexture GPUTexture
Definition: GPU_texture.h:17
void IMB_rect_from_float(struct ImBuf *ibuf)
Definition: divers.c:696
@ IB_DISPLAY_BUFFER_INVALID
Read Guarded memory(de)allocation.
@ PARM_REQUIRED
Definition: RNA_types.h:352
@ FUNC_USE_REPORTS
Definition: RNA_types.h:663
@ FUNC_USE_MAIN
Definition: RNA_types.h:661
@ FUNC_USE_CONTEXT
Definition: RNA_types.h:662
@ PROP_STRING
Definition: RNA_types.h:62
@ PROP_THICK_WRAP
Definition: RNA_types.h:285
@ PROP_BYTESTRING
Definition: RNA_types.h:133
#define C
Definition: RandGen.cpp:25
#define NA_EDITED
Definition: WM_types.h:523
#define NC_IMAGE
Definition: WM_types.h:334
Scene scene
depth_tx normal_tx diffuse_light_tx specular_light_tx volume_light_tx environment_tx ambient_occlusion_tx aov_value_tx in_weight_img image(1, GPU_R32F, Qualifier::WRITE, ImageType::FLOAT_2D_ARRAY, "out_weight_img") .image(3
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
static void error(const char *str)
Definition: meshlaplacian.c:51
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
Definition: rna_define.c:4170
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
Definition: rna_define.c:4312
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
Definition: rna_define.c:1645
PropertyRNA * RNA_def_string_file_path(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, int maxlen, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3711
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier, const char *call)
Definition: rna_define.c:4273
void RNA_def_function_output(FunctionRNA *UNUSED(func), PropertyRNA *ret)
Definition: rna_define.c:4337
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
Definition: rna_define.c:4347
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
Definition: rna_define.c:1257
void RNA_def_function_flag(FunctionRNA *func, int flag)
Definition: rna_define.c:4342
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, int default_value, int hardmin, int hardmax, const char *ui_name, const char *ui_description, int softmin, int softmax)
Definition: rna_define.c:3597
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3783
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
Definition: rna_define.c:1518
void RNA_api_image(StructRNA *srna)
void RNA_api_image_packed_file(StructRNA *srna)
const EnumPropertyItem rna_enum_unpack_method_items[]
int userflags
unsigned int * rect
struct PackedFile * packedfile
char filepath[1024]
char filepath[1024]
Definition: BKE_main.h:121
void WM_main_add_notifier(unsigned int type, void *reference)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)