2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2009 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Arystanbek Dyussenov
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/makesrna/intern/rna_image_api.c
37 #include "DNA_packedFile_types.h"
39 #include "BLI_utildefines.h"
40 #include "BLI_path_util.h"
44 #include "RNA_define.h"
45 #include "RNA_enum_types.h"
47 #include "rna_internal.h" /* own include */
52 #include "BKE_image.h"
53 #include "BKE_packedFile.h"
56 #include "IMB_imbuf.h"
57 #include "IMB_colormanagement.h"
59 #include "DNA_image_types.h"
60 #include "DNA_scene_types.h"
62 #include "MEM_guardedalloc.h"
64 static void rna_ImagePackedFile_save(ImagePackedFile *imapf, Main *bmain, ReportList *reports)
66 if (writePackedFile(reports, BKE_main_blendfile_path(bmain), imapf->filepath, imapf->packedfile, 0) != RET_OK) {
67 BKE_reportf(reports, RPT_ERROR, "Could not save packed file to disk as '%s'",
72 static void rna_Image_save_render(Image *image, bContext *C, ReportList *reports, const char *path, Scene *scene)
77 scene = CTX_data_scene(C);
81 ImageUser iuser = {NULL};
87 ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
90 BKE_report(reports, RPT_ERROR, "Could not acquire buffer from image");
95 write_ibuf = IMB_colormanagement_imbuf_for_write(ibuf, true, true, &scene->view_settings,
96 &scene->display_settings, &scene->r.im_format);
98 write_ibuf->planes = scene->r.im_format.planes;
99 write_ibuf->dither = scene->r.dither_intensity;
101 if (!BKE_imbuf_write(write_ibuf, path, &scene->r.im_format)) {
102 BKE_reportf(reports, RPT_ERROR, "Could not write image: %s, '%s'", strerror(errno), path);
105 if (write_ibuf != ibuf)
106 IMB_freeImBuf(write_ibuf);
109 BKE_image_release_ibuf(image, ibuf, lock);
112 BKE_report(reports, RPT_ERROR, "Scene not in context, could not get save parameters");
116 static void rna_Image_save(Image *image, Main *bmain, bContext *C, ReportList *reports)
120 ImBuf *ibuf = BKE_image_acquire_ibuf(image, NULL, &lock);
122 char filename[FILE_MAX];
123 BLI_strncpy(filename, image->name, sizeof(filename));
124 BLI_path_abs(filename, ID_BLEND_PATH(bmain, &image->id));
126 /* note, we purposefully ignore packed files here,
127 * developers need to explicitly write them via 'packed_files' */
129 if (IMB_saveiff(ibuf, filename, ibuf->flags)) {
130 image->type = IMA_TYPE_IMAGE;
132 if (image->source == IMA_SRC_GENERATED)
133 image->source = IMA_SRC_FILE;
135 IMB_colormanagment_colorspace_from_ibuf_ftype(&image->colorspace_settings, ibuf);
137 ibuf->userflags &= ~IB_BITMAPDIRTY;
140 BKE_reportf(reports, RPT_ERROR, "Image '%s' could not be saved to '%s'", image->id.name + 2, image->name);
144 BKE_reportf(reports, RPT_ERROR, "Image '%s' does not have any image data", image->id.name + 2);
147 BKE_image_release_ibuf(image, ibuf, lock);
148 WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, image);
151 static void rna_Image_pack(
152 Image *image, Main *bmain, bContext *C, ReportList *reports,
153 bool as_png, const char *data, int data_len)
155 ImBuf *ibuf = BKE_image_acquire_ibuf(image, NULL, NULL);
157 if (!as_png && (ibuf && (ibuf->userflags & IB_BITMAPDIRTY))) {
158 BKE_report(reports, RPT_ERROR, "Cannot pack edited image from disk, only as internal PNG");
161 BKE_image_free_packedfiles(image);
163 BKE_image_memorypack(image);
166 char *data_dup = MEM_mallocN(sizeof(*data_dup) * (size_t)data_len, __func__);
167 memcpy(data_dup, data, (size_t)data_len);
168 BKE_image_packfiles_from_mem(reports, image, data_dup, (size_t)data_len);
171 BKE_image_packfiles(reports, image, ID_BLEND_PATH(bmain, &image->id));
175 BKE_image_release_ibuf(image, ibuf, NULL);
176 WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, image);
179 static void rna_Image_unpack(Image *image, Main *bmain, ReportList *reports, int method)
181 if (!BKE_image_has_packedfile(image)) {
182 BKE_report(reports, RPT_ERROR, "Image not packed");
184 else if (BKE_image_is_animated(image)) {
185 BKE_report(reports, RPT_ERROR, "Unpacking movies or image sequences not supported");
189 /* reports its own error on failure */
190 unpackImage(bmain, reports, image, method);
194 static void rna_Image_reload(Image *image, Main *bmain)
196 BKE_image_signal(bmain, image, NULL, IMA_SIGNAL_RELOAD);
199 static void rna_Image_update(Image *image, ReportList *reports)
201 ImBuf *ibuf = BKE_image_acquire_ibuf(image, NULL, NULL);
204 BKE_reportf(reports, RPT_ERROR, "Image '%s' does not have any image data", image->id.name + 2);
209 IMB_rect_from_float(ibuf);
211 ibuf->userflags |= IB_DISPLAY_BUFFER_INVALID;
213 BKE_image_release_ibuf(image, ibuf, NULL);
216 static void rna_Image_scale(Image *image, ReportList *reports, int width, int height)
218 if (!BKE_image_scale(image, width, height)) {
219 BKE_reportf(reports, RPT_ERROR, "Image '%s' does not have any image data", image->id.name + 2);
223 static int rna_Image_gl_load(Image *image, ReportList *reports, int frame, int filter, int mag)
225 GPUTexture *tex = image->gputexture[TEXTARGET_TEXTURE_2D];
226 int error = GL_NO_ERROR;
231 ImageUser iuser = {NULL};
232 iuser.framenr = frame;
236 ImBuf *ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
238 /* clean glError buffer */
239 while (glGetError() != GL_NO_ERROR) {}
241 if (ibuf == NULL || ibuf->rect == NULL) {
242 BKE_reportf(reports, RPT_ERROR, "Image '%s' does not have any image data", image->id.name + 2);
243 BKE_image_release_ibuf(image, ibuf, lock);
244 return (int)GL_INVALID_OPERATION;
247 unsigned int bindcode = 0;
248 GPU_create_gl_tex(&bindcode, ibuf->rect, ibuf->rect_float, ibuf->x, ibuf->y, GL_TEXTURE_2D,
249 (filter != GL_NEAREST && filter != GL_LINEAR), false, image);
251 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (GLint)filter);
252 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLint)mag);
254 /* TODO(merwin): validate input (dimensions, filter, mag) before calling OpenGL
255 * instead of trusting input & testing for error after */
256 error = glGetError();
259 glDeleteTextures(1, (GLuint *)&bindcode);
262 image->gputexture[TEXTARGET_TEXTURE_2D] = GPU_texture_from_bindcode(GL_TEXTURE_2D, bindcode);
265 BKE_image_release_ibuf(image, ibuf, lock);
270 static int rna_Image_gl_touch(Image *image, ReportList *reports, int frame, int filter, int mag)
272 int error = GL_NO_ERROR;
274 BKE_image_tag_time(image);
276 if (image->gputexture[TEXTARGET_TEXTURE_2D] == NULL)
277 error = rna_Image_gl_load(image, reports, frame, filter, mag);
282 static void rna_Image_gl_free(Image *image)
284 GPU_free_image(image);
286 /* remove the nocollect flag, image is available for garbage collection again */
287 image->flag &= ~IMA_NOCOLLECT;
290 static void rna_Image_filepath_from_user(Image *image, ImageUser *image_user, char *filepath)
292 BKE_image_user_file_path(image_user, image, filepath);
295 static void rna_Image_buffers_free(Image *image)
297 BKE_image_free_buffers_ex(image, true);
302 void RNA_api_image_packed_file(StructRNA *srna)
306 func = RNA_def_function(srna, "save", "rna_ImagePackedFile_save");
307 RNA_def_function_ui_description(func, "Save the packed file to its filepath");
308 RNA_def_function_flag(func, FUNC_USE_MAIN | FUNC_USE_REPORTS);
311 void RNA_api_image(StructRNA *srna)
316 func = RNA_def_function(srna, "save_render", "rna_Image_save_render");
317 RNA_def_function_ui_description(func, "Save image to a specific path using a scenes render settings");
318 RNA_def_function_flag(func, FUNC_USE_CONTEXT | FUNC_USE_REPORTS);
319 parm = RNA_def_string_file_path(func, "filepath", NULL, 0, "", "Save path");
320 RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
321 RNA_def_pointer(func, "scene", "Scene", "", "Scene to take image parameters from");
323 func = RNA_def_function(srna, "save", "rna_Image_save");
324 RNA_def_function_ui_description(func, "Save image to its source path");
325 RNA_def_function_flag(func, FUNC_USE_MAIN | FUNC_USE_CONTEXT | FUNC_USE_REPORTS);
327 func = RNA_def_function(srna, "pack", "rna_Image_pack");
328 RNA_def_function_ui_description(func, "Pack an image as embedded data into the .blend file");
329 RNA_def_function_flag(func, FUNC_USE_MAIN | FUNC_USE_CONTEXT | FUNC_USE_REPORTS);
330 RNA_def_boolean(func, "as_png", 0, "as_png", "Pack the image as PNG (needed for generated/dirty images)");
331 parm = RNA_def_property(func, "data", PROP_STRING, PROP_BYTESTRING);
332 RNA_def_property_ui_text(parm, "data", "Raw data (bytes, exact content of the embedded file)");
333 RNA_def_int(func, "data_len", 0, 0, INT_MAX,
334 "data_len", "length of given data (mandatory if data is provided)", 0, INT_MAX);
336 func = RNA_def_function(srna, "unpack", "rna_Image_unpack");
337 RNA_def_function_ui_description(func, "Save an image packed in the .blend file to disk");
338 RNA_def_function_flag(func, FUNC_USE_MAIN | FUNC_USE_REPORTS);
339 RNA_def_enum(func, "method", rna_enum_unpack_method_items, PF_USE_LOCAL, "method", "How to unpack");
341 func = RNA_def_function(srna, "reload", "rna_Image_reload");
342 RNA_def_function_flag(func, FUNC_USE_MAIN);
343 RNA_def_function_ui_description(func, "Reload the image from its source path");
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");
347 RNA_def_function_flag(func, FUNC_USE_REPORTS);
349 func = RNA_def_function(srna, "scale", "rna_Image_scale");
350 RNA_def_function_ui_description(func, "Scale the image in pixels");
351 RNA_def_function_flag(func, FUNC_USE_REPORTS);
352 parm = RNA_def_int(func, "width", 1, 1, 10000, "", "Width", 1, 10000);
353 RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
354 parm = RNA_def_int(func, "height", 1, 1, 10000, "", "Height", 1, 10000);
355 RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
357 func = RNA_def_function(srna, "gl_touch", "rna_Image_gl_touch");
358 RNA_def_function_ui_description(func, "Delay the image from being cleaned from the cache due inactivity");
359 RNA_def_function_flag(func, FUNC_USE_REPORTS);
360 RNA_def_int(func, "frame", 0, 0, INT_MAX, "Frame",
361 "Frame of image sequence or movie", 0, INT_MAX);
362 RNA_def_int(func, "filter", GL_LINEAR_MIPMAP_NEAREST, -INT_MAX, INT_MAX, "Filter",
363 "The texture minifying function to use if the image wasn't loaded", -INT_MAX, INT_MAX);
364 RNA_def_int(func, "mag", GL_LINEAR, -INT_MAX, INT_MAX, "Magnification",
365 "The texture magnification function to use if the image wasn't loaded", -INT_MAX, INT_MAX);
367 parm = RNA_def_int(func, "error", 0, -INT_MAX, INT_MAX, "Error", "OpenGL error value", -INT_MAX, INT_MAX);
368 RNA_def_function_return(func, parm);
370 func = RNA_def_function(srna, "gl_load", "rna_Image_gl_load");
371 RNA_def_function_ui_description(func, "Load the image into OpenGL graphics memory");
372 RNA_def_function_flag(func, FUNC_USE_REPORTS);
373 RNA_def_int(func, "frame", 0, 0, INT_MAX, "Frame",
374 "Frame of image sequence or movie", 0, INT_MAX);
375 RNA_def_int(func, "filter", GL_LINEAR_MIPMAP_NEAREST, -INT_MAX, INT_MAX, "Filter",
376 "The texture minifying function", -INT_MAX, INT_MAX);
377 RNA_def_int(func, "mag", GL_LINEAR, -INT_MAX, INT_MAX, "Magnification",
378 "The texture magnification function", -INT_MAX, INT_MAX);
380 parm = RNA_def_int(func, "error", 0, -INT_MAX, INT_MAX, "Error", "OpenGL error value", -INT_MAX, INT_MAX);
381 RNA_def_function_return(func, parm);
383 func = RNA_def_function(srna, "gl_free", "rna_Image_gl_free");
384 RNA_def_function_ui_description(func, "Free the image from OpenGL graphics memory");
386 /* path to an frame specified by image user */
387 func = RNA_def_function(srna, "filepath_from_user", "rna_Image_filepath_from_user");
388 RNA_def_function_ui_description(func, "Return the absolute path to the filepath of an image frame specified by the image user");
389 RNA_def_pointer(func, "image_user", "ImageUser", "", "Image user of the image to get filepath for");
390 parm = RNA_def_string_file_path(func, "filepath", NULL, FILE_MAX, "File Path",
391 "The resulting filepath from the image and it's user");
392 RNA_def_parameter_flags(parm, PROP_THICK_WRAP, 0); /* needed for string return value */
393 RNA_def_function_output(func, parm);
395 func = RNA_def_function(srna, "buffers_free", "rna_Image_buffers_free");
396 RNA_def_function_ui_description(func, "Free the image buffers from memory");
398 /* TODO, pack/unpack, maybe should be generic functions? */