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) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * Contributor(s): Blender Foundation, 2006, full recode
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/blenkernel/intern/image.c
43 #include "MEM_guardedalloc.h"
45 #include "IMB_colormanagement.h"
46 #include "IMB_imbuf_types.h"
47 #include "IMB_imbuf.h"
48 #include "IMB_moviecache.h"
51 # include "intern/openexr/openexr_multi.h"
54 #include "DNA_packedFile_types.h"
55 #include "DNA_scene_types.h"
56 #include "DNA_object_types.h"
57 #include "DNA_camera_types.h"
58 #include "DNA_sequence_types.h"
59 #include "DNA_brush_types.h"
60 #include "DNA_mesh_types.h"
61 #include "DNA_meshdata_types.h"
63 #include "BLI_blenlib.h"
64 #include "BLI_math_vector.h"
65 #include "BLI_threads.h"
66 #include "BLI_timecode.h" /* for stamp timecode format */
67 #include "BLI_utildefines.h"
69 #include "BKE_bmfont.h"
70 #include "BKE_colortools.h"
71 #include "BKE_global.h"
72 #include "BKE_icons.h"
73 #include "BKE_image.h"
74 #include "BKE_library.h"
76 #include "BKE_packedFile.h"
77 #include "BKE_report.h"
78 #include "BKE_scene.h"
80 #include "BKE_sequencer.h" /* seq_foreground_frame_get() */
86 #include "RE_pipeline.h"
90 #include "BLI_sys_types.h" // for intptr_t support
92 /* for image user iteration */
93 #include "DNA_node_types.h"
94 #include "DNA_space_types.h"
95 #include "DNA_screen_types.h"
96 #include "DNA_view3d_types.h"
100 static SpinLock image_spin;
103 static int image_num_files(struct Image *ima);
104 static ImBuf *image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock);
105 static void image_update_views_format(Image *ima, ImageUser *iuser);
106 static void image_add_view(Image *ima, const char *viewname, const char *filepath);
108 /* max int, to indicate we don't store sequences in ibuf */
109 #define IMA_NO_INDEX 0x7FEFEFEF
111 /* quick lookup: supports 1 million frames, thousand passes */
112 #define IMA_MAKE_INDEX(frame, index) ((frame) << 10) + index
113 #define IMA_INDEX_FRAME(index) (index >> 10)
115 #define IMA_INDEX_PASS(index) (index & ~1023)
118 /* ******** IMAGE CACHE ************* */
120 typedef struct ImageCacheKey {
124 static unsigned int imagecache_hashhash(const void *key_v)
126 const ImageCacheKey *key = key_v;
130 static bool imagecache_hashcmp(const void *a_v, const void *b_v)
132 const ImageCacheKey *a = a_v;
133 const ImageCacheKey *b = b_v;
135 return (a->index != b->index);
138 static void imagecache_keydata(void *userkey, int *framenr, int *proxy, int *render_flags)
140 ImageCacheKey *key = userkey;
142 *framenr = IMA_INDEX_FRAME(key->index);
143 *proxy = IMB_PROXY_NONE;
147 static void imagecache_put(Image *image, int index, ImBuf *ibuf)
151 if (image->cache == NULL) {
152 // char cache_name[64];
153 // BLI_snprintf(cache_name, sizeof(cache_name), "Image Datablock %s", image->id.name);
155 image->cache = IMB_moviecache_create("Image Datablock Cache", sizeof(ImageCacheKey),
156 imagecache_hashhash, imagecache_hashcmp);
157 IMB_moviecache_set_getdata_callback(image->cache, imagecache_keydata);
162 IMB_moviecache_put(image->cache, &key, ibuf);
165 static struct ImBuf *imagecache_get(Image *image, int index)
170 return IMB_moviecache_get(image->cache, &key);
176 void BKE_images_init(void)
178 BLI_spin_init(&image_spin);
181 void BKE_images_exit(void)
183 BLI_spin_end(&image_spin);
186 /* ******** IMAGE PROCESSING ************* */
188 static void de_interlace_ng(struct ImBuf *ibuf) /* neogeo fields */
190 struct ImBuf *tbuf1, *tbuf2;
192 if (ibuf == NULL) return;
193 if (ibuf->flags & IB_fields) return;
194 ibuf->flags |= IB_fields;
198 tbuf1 = IMB_allocImBuf(ibuf->x, (ibuf->y >> 1), (unsigned char)32, (int)IB_rect);
199 tbuf2 = IMB_allocImBuf(ibuf->x, (ibuf->y >> 1), (unsigned char)32, (int)IB_rect);
203 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
204 IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
207 IMB_rectcpy(ibuf, tbuf1, 0, 0, 0, 0, tbuf1->x, tbuf1->y);
208 IMB_rectcpy(ibuf, tbuf2, 0, tbuf2->y, 0, 0, tbuf2->x, tbuf2->y);
210 IMB_freeImBuf(tbuf1);
211 IMB_freeImBuf(tbuf2);
216 static void de_interlace_st(struct ImBuf *ibuf) /* standard fields */
218 struct ImBuf *tbuf1, *tbuf2;
220 if (ibuf == NULL) return;
221 if (ibuf->flags & IB_fields) return;
222 ibuf->flags |= IB_fields;
226 tbuf1 = IMB_allocImBuf(ibuf->x, (ibuf->y >> 1), (unsigned char)32, IB_rect);
227 tbuf2 = IMB_allocImBuf(ibuf->x, (ibuf->y >> 1), (unsigned char)32, IB_rect);
231 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
232 IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
235 IMB_rectcpy(ibuf, tbuf2, 0, 0, 0, 0, tbuf2->x, tbuf2->y);
236 IMB_rectcpy(ibuf, tbuf1, 0, tbuf2->y, 0, 0, tbuf1->x, tbuf1->y);
238 IMB_freeImBuf(tbuf1);
239 IMB_freeImBuf(tbuf2);
244 void BKE_image_de_interlace(Image *ima, int odd)
246 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
249 de_interlace_st(ibuf);
251 de_interlace_ng(ibuf);
253 BKE_image_release_ibuf(ima, ibuf, NULL);
256 /* ***************** ALLOC & FREE, DATA MANAGING *************** */
258 static void image_free_cached_frames(Image *image)
261 IMB_moviecache_free(image->cache);
266 static void image_free_packedfiles(Image *ima)
268 while (ima->packedfiles.last) {
269 ImagePackedFile *imapf = ima->packedfiles.last;
270 if (imapf->packedfile) {
271 freePackedFile(imapf->packedfile);
273 BLI_remlink(&ima->packedfiles, imapf);
278 void BKE_image_free_packedfiles(Image *ima)
280 image_free_packedfiles(ima);
283 void BKE_image_free_views(Image *image)
285 BLI_freelistN(&image->views);
288 static void image_free_anims(Image *ima)
290 while (ima->anims.last) {
291 ImageAnim *ia = ima->anims.last;
293 IMB_free_anim(ia->anim);
296 BLI_remlink(&ima->anims, ia);
302 * Simply free the image data from memory,
303 * on display the image can load again (except for render buffers).
305 void BKE_image_free_buffers(Image *ima)
307 image_free_cached_frames(ima);
309 image_free_anims(ima);
312 RE_FreeRenderResult(ima->rr);
317 /* Background mode doesn't use opnegl,
318 * so we can avoid freeing GPU images and save some
319 * time by skipping mutex lock.
327 /* called by library too, do not free ima itself */
328 void BKE_image_free(Image *ima)
332 BKE_image_free_buffers(ima);
334 image_free_packedfiles(ima);
336 BKE_icon_id_delete(&ima->id);
339 BKE_previewimg_free(&ima->preview);
341 for (a = 0; a < IMA_MAX_RENDER_SLOT; a++) {
342 if (ima->renders[a]) {
343 RE_FreeRenderResult(ima->renders[a]);
344 ima->renders[a] = NULL;
348 BKE_image_free_views(ima);
349 MEM_freeN(ima->stereo3d_format);
352 /* only image block itself */
353 static Image *image_alloc(Main *bmain, const char *name, short source, short type)
357 ima = BKE_libblock_alloc(bmain, ID_IM, name);
361 ima->xrep = ima->yrep = 1;
362 ima->aspx = ima->aspy = 1.0;
363 ima->gen_x = 1024; ima->gen_y = 1024;
364 ima->gen_type = 1; /* no defines yet? */
366 ima->source = source;
369 if (source == IMA_SRC_VIEWER)
370 ima->flag |= IMA_VIEW_AS_RENDER;
372 BKE_color_managed_colorspace_settings_init(&ima->colorspace_settings);
373 ima->stereo3d_format = MEM_callocN(sizeof(Stereo3dFormat), "Image Stereo Format");
379 /* Get the ibuf from an image cache by it's index and frame.
380 * Local use here only.
382 * Returns referenced image buffer if it exists, callee is to
383 * call IMB_freeImBuf to de-reference the image buffer after
384 * it's done handling it.
386 static ImBuf *image_get_cached_ibuf_for_index_frame(Image *ima, int index, int frame)
388 if (index != IMA_NO_INDEX) {
389 index = IMA_MAKE_INDEX(frame, index);
392 return imagecache_get(ima, index);
395 /* no ima->ibuf anymore, but listbase */
396 static void image_assign_ibuf(Image *ima, ImBuf *ibuf, int index, int frame)
399 if (index != IMA_NO_INDEX)
400 index = IMA_MAKE_INDEX(frame, index);
402 imagecache_put(ima, index, ibuf);
406 static void copy_image_packedfiles(ListBase *lb_dst, const ListBase *lb_src)
408 const ImagePackedFile *imapf_src;
410 BLI_listbase_clear(lb_dst);
411 for (imapf_src = lb_src->first; imapf_src; imapf_src = imapf_src->next) {
412 ImagePackedFile *imapf_dst = MEM_mallocN(sizeof(ImagePackedFile), "Image Packed Files (copy)");
413 BLI_strncpy(imapf_dst->filepath, imapf_src->filepath, sizeof(imapf_dst->filepath));
415 if (imapf_src->packedfile)
416 imapf_dst->packedfile = dupPackedFile(imapf_src->packedfile);
418 BLI_addtail(lb_dst, imapf_dst);
422 /* empty image block, of similar type and filename */
423 Image *BKE_image_copy(Main *bmain, Image *ima)
425 Image *nima = image_alloc(bmain, ima->id.name + 2, ima->source, ima->type);
427 BLI_strncpy(nima->name, ima->name, sizeof(ima->name));
429 nima->flag = ima->flag;
430 nima->tpageflag = ima->tpageflag;
432 nima->gen_x = ima->gen_x;
433 nima->gen_y = ima->gen_y;
434 nima->gen_type = ima->gen_type;
435 copy_v4_v4(nima->gen_color, ima->gen_color);
437 nima->animspeed = ima->animspeed;
439 nima->aspx = ima->aspx;
440 nima->aspy = ima->aspy;
442 BKE_color_managed_colorspace_settings_copy(&nima->colorspace_settings, &ima->colorspace_settings);
444 copy_image_packedfiles(&nima->packedfiles, &ima->packedfiles);
446 nima->stereo3d_format = MEM_dupallocN(ima->stereo3d_format);
447 BLI_duplicatelist(&nima->views, &ima->views);
450 BKE_id_lib_local_paths(bmain, ima->id.lib, &nima->id);
456 static void extern_local_image(Image *UNUSED(ima))
458 /* Nothing to do: images don't link to other IDs. This function exists to
459 * match id_make_local pattern. */
462 void BKE_image_make_local(struct Image *ima)
464 Main *bmain = G.main;
468 bool is_local = false, is_lib = false;
470 /* - only lib users: do nothing
471 * - only local users: set flag
475 if (ima->id.lib == NULL) return;
477 /* Can't take short cut here: must check meshes at least because of bogus
478 * texface ID refs. - z0r */
480 if (ima->id.us == 1) {
481 id_clear_lib_data(bmain, &ima->id);
482 extern_local_image(ima);
487 for (tex = bmain->tex.first; tex; tex = tex->id.next) {
488 if (tex->ima == ima) {
489 if (tex->id.lib) is_lib = true;
490 else is_local = true;
493 for (brush = bmain->brush.first; brush; brush = brush->id.next) {
494 if (brush->clone.image == ima) {
495 if (brush->id.lib) is_lib = true;
496 else is_local = true;
499 for (me = bmain->mesh.first; me; me = me->id.next) {
504 for (i = 0; i < me->fdata.totlayer; i++) {
505 if (me->fdata.layers[i].type == CD_MTFACE) {
506 tface = (MTFace *)me->fdata.layers[i].data;
508 for (a = 0; a < me->totface; a++, tface++) {
509 if (tface->tpage == ima) {
510 if (me->id.lib) is_lib = true;
511 else is_local = true;
522 for (i = 0; i < me->pdata.totlayer; i++) {
523 if (me->pdata.layers[i].type == CD_MTEXPOLY) {
524 mtpoly = (MTexPoly *)me->pdata.layers[i].data;
526 for (a = 0; a < me->totpoly; a++, mtpoly++) {
527 if (mtpoly->tpage == ima) {
528 if (me->id.lib) is_lib = true;
529 else is_local = true;
538 if (is_local && is_lib == false) {
539 id_clear_lib_data(bmain, &ima->id);
540 extern_local_image(ima);
542 else if (is_local && is_lib) {
543 Image *ima_new = BKE_image_copy(bmain, ima);
547 /* Remap paths of new ID using old library as base. */
548 BKE_id_lib_local_paths(bmain, ima->id.lib, &ima_new->id);
550 tex = bmain->tex.first;
552 if (tex->id.lib == NULL) {
553 if (tex->ima == ima) {
555 id_us_plus(&ima_new->id);
561 brush = bmain->brush.first;
563 if (brush->id.lib == NULL) {
564 if (brush->clone.image == ima) {
565 brush->clone.image = ima_new;
566 id_us_plus(&ima_new->id);
570 brush = brush->id.next;
572 /* Transfer references in texfaces. Texfaces don't add to image ID
573 * user count *unless* there are no other users. See
574 * readfile.c:lib_link_mtface. */
575 me = bmain->mesh.first;
581 for (i = 0; i < me->fdata.totlayer; i++) {
582 if (me->fdata.layers[i].type == CD_MTFACE) {
583 tface = (MTFace *)me->fdata.layers[i].data;
585 for (a = 0; a < me->totface; a++, tface++) {
586 if (tface->tpage == ima) {
587 tface->tpage = ima_new;
588 id_us_ensure_real((ID *)ima_new);
589 id_lib_extern((ID *)ima_new);
600 for (i = 0; i < me->pdata.totlayer; i++) {
601 if (me->pdata.layers[i].type == CD_MTEXPOLY) {
602 mtpoly = (MTexPoly *)me->pdata.layers[i].data;
604 for (a = 0; a < me->totpoly; a++, mtpoly++) {
605 if (mtpoly->tpage == ima) {
606 mtpoly->tpage = ima_new;
607 id_us_ensure_real((ID *)ima_new);
608 id_lib_extern((ID *)ima_new);
620 void BKE_image_merge(Image *dest, Image *source)
623 if (dest && source && dest != source) {
624 BLI_spin_lock(&image_spin);
625 if (source->cache != NULL) {
626 struct MovieCacheIter *iter;
627 iter = IMB_moviecacheIter_new(source->cache);
628 while (!IMB_moviecacheIter_done(iter)) {
629 ImBuf *ibuf = IMB_moviecacheIter_getImBuf(iter);
630 ImageCacheKey *key = IMB_moviecacheIter_getUserKey(iter);
631 imagecache_put(dest, key->index, ibuf);
632 IMB_moviecacheIter_step(iter);
634 IMB_moviecacheIter_free(iter);
636 BLI_spin_unlock(&image_spin);
638 BKE_libblock_free(G.main, source);
642 /* note, we could be clever and scale all imbuf's but since some are mipmaps its not so simple */
643 bool BKE_image_scale(Image *image, int width, int height)
648 ibuf = BKE_image_acquire_ibuf(image, NULL, &lock);
651 IMB_scaleImBuf(ibuf, width, height);
652 ibuf->userflags |= IB_BITMAPDIRTY;
655 BKE_image_release_ibuf(image, ibuf, lock);
657 return (ibuf != NULL);
660 static void image_init_color_management(Image *ima)
665 BKE_image_user_file_path(NULL, ima, name);
667 /* will set input color space to image format default's */
668 ibuf = IMB_loadiffname(name, IB_test | IB_alphamode_detect, ima->colorspace_settings.name);
671 if (ibuf->flags & IB_alphamode_premul)
672 ima->alpha_mode = IMA_ALPHA_PREMUL;
674 ima->alpha_mode = IMA_ALPHA_STRAIGHT;
680 char BKE_image_alpha_mode_from_extension_ex(const char *filepath)
682 if (BLI_testextensie_n(filepath, ".exr", ".cin", ".dpx", ".hdr", NULL)) {
683 return IMA_ALPHA_PREMUL;
686 return IMA_ALPHA_STRAIGHT;
690 void BKE_image_alpha_mode_from_extension(Image *image)
692 image->alpha_mode = BKE_image_alpha_mode_from_extension_ex(image->name);
695 Image *BKE_image_load(Main *bmain, const char *filepath)
701 BLI_strncpy(str, filepath, sizeof(str));
702 BLI_path_abs(str, bmain->name);
705 file = BLI_open(str, O_BINARY | O_RDONLY, 0);
710 ima = image_alloc(bmain, BLI_path_basename(filepath), IMA_SRC_FILE, IMA_TYPE_IMAGE);
711 BLI_strncpy(ima->name, filepath, sizeof(ima->name));
713 if (BLI_testextensie_array(filepath, imb_ext_movie))
714 ima->source = IMA_SRC_MOVIE;
716 image_init_color_management(ima);
721 /* checks if image was already loaded, then returns same image */
722 /* otherwise creates new. */
723 /* does not load ibuf itself */
724 /* pass on optional frame for #name images */
725 Image *BKE_image_load_exists_ex(const char *filepath, bool *r_exists)
728 char str[FILE_MAX], strtest[FILE_MAX];
730 BLI_strncpy(str, filepath, sizeof(str));
731 BLI_path_abs(str, G.main->name);
733 /* first search an identical filepath */
734 for (ima = G.main->image.first; ima; ima = ima->id.next) {
735 if (ima->source != IMA_SRC_VIEWER && ima->source != IMA_SRC_GENERATED) {
736 BLI_strncpy(strtest, ima->name, sizeof(ima->name));
737 BLI_path_abs(strtest, ID_BLEND_PATH(G.main, &ima->id));
739 if (BLI_path_cmp(strtest, str) == 0) {
740 if ((BKE_image_has_anim(ima) == false) ||
743 id_us_plus(&ima->id); /* officially should not, it doesn't link here! */
756 return BKE_image_load(G.main, filepath);
759 Image *BKE_image_load_exists(const char *filepath)
761 return BKE_image_load_exists_ex(filepath, NULL);
764 static ImBuf *add_ibuf_size(unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short gen_type,
765 const float color[4], ColorManagedColorspaceSettings *colorspace_settings)
768 unsigned char *rect = NULL;
769 float *rect_float = NULL;
772 ibuf = IMB_allocImBuf(width, height, depth, IB_rectfloat);
774 if (colorspace_settings->name[0] == '\0') {
775 const char *colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_DEFAULT_FLOAT);
777 BLI_strncpy(colorspace_settings->name, colorspace, sizeof(colorspace_settings->name));
781 rect_float = ibuf->rect_float;
782 IMB_colormanagement_check_is_data(ibuf, colorspace_settings->name);
786 ibuf = IMB_allocImBuf(width, height, depth, IB_rect);
788 if (colorspace_settings->name[0] == '\0') {
789 const char *colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_DEFAULT_BYTE);
791 BLI_strncpy(colorspace_settings->name, colorspace, sizeof(colorspace_settings->name));
795 rect = (unsigned char *)ibuf->rect;
796 IMB_colormanagement_assign_rect_colorspace(ibuf, colorspace_settings->name);
804 BLI_strncpy(ibuf->name, name, sizeof(ibuf->name));
805 ibuf->userflags |= IB_BITMAPDIRTY;
808 case IMA_GENTYPE_GRID:
809 BKE_image_buf_fill_checker(rect, rect_float, width, height);
811 case IMA_GENTYPE_GRID_COLOR:
812 BKE_image_buf_fill_checker_color(rect, rect_float, width, height);
815 BKE_image_buf_fill_color(rect, rect_float, width, height, color);
820 /* both byte and float buffers are filling in sRGB space, need to linearize float buffer after BKE_image_buf_fill* functions */
822 IMB_buffer_float_from_float(rect_float, rect_float, ibuf->channels, IB_PROFILE_LINEAR_RGB, IB_PROFILE_SRGB,
823 true, ibuf->x, ibuf->y, ibuf->x, ibuf->x);
829 /* adds new image block, creates ImBuf and initializes color */
830 Image *BKE_image_add_generated(
831 Main *bmain, unsigned int width, unsigned int height, const char *name,
832 int depth, int floatbuf, short gen_type, const float color[4], const bool stereo3d)
834 /* on save, type is changed to FILE in editsima.c */
835 Image *ima = image_alloc(bmain, name, IMA_SRC_GENERATED, IMA_TYPE_UV_TEST);
839 const char *names[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME};
841 /* BLI_strncpy(ima->name, name, FILE_MAX); */ /* don't do this, this writes in ain invalid filepath! */
844 ima->gen_type = gen_type;
845 ima->gen_flag |= (floatbuf ? IMA_GEN_FLOAT : 0);
846 ima->gen_depth = depth;
847 copy_v4_v4(ima->gen_color, color);
849 for (view_id = 0; view_id < 2; view_id++) {
851 ibuf = add_ibuf_size(width, height, ima->name, depth, floatbuf, gen_type, color, &ima->colorspace_settings);
852 image_assign_ibuf(ima, ibuf, stereo3d ? view_id : IMA_NO_INDEX, 0);
854 /* image_assign_ibuf puts buffer to the cache, which increments user counter. */
856 if (!stereo3d) break;
858 image_add_view(ima, names[view_id], "");
861 ima->ok = IMA_OK_LOADED;
867 /* Create an image image from ibuf. The refcount of ibuf is increased,
868 * caller should take care to drop its reference by calling
869 * IMB_freeImBuf if needed. */
870 Image *BKE_image_add_from_imbuf(ImBuf *ibuf, const char *name)
872 /* on save, type is changed to FILE in editsima.c */
876 name = BLI_path_basename(ibuf->name);
879 ima = image_alloc(G.main, name, IMA_SRC_FILE, IMA_TYPE_IMAGE);
882 BLI_strncpy(ima->name, ibuf->name, FILE_MAX);
883 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
884 ima->ok = IMA_OK_LOADED;
890 /* packs rects from memory as PNG
891 * convert multiview images to R_IMF_VIEWS_INDIVIDUAL
893 static void image_memorypack_multiview(Image *ima)
898 image_free_packedfiles(ima);
900 for (i = 0, iv = ima->views.first; iv; iv = iv->next, i++) {
901 ImBuf *ibuf = image_get_cached_ibuf_for_index_frame(ima, i, 0);
903 ibuf->ftype = IMB_FTYPE_PNG;
904 ibuf->planes = R_IMF_PLANES_RGBA;
906 /* if the image was a R_IMF_VIEWS_STEREO_3D we force _L, _R suffices */
907 if (ima->views_format == R_IMF_VIEWS_STEREO_3D) {
908 const char *suffix[2] = {STEREO_LEFT_SUFFIX, STEREO_RIGHT_SUFFIX};
909 BLI_path_suffix(iv->filepath, FILE_MAX, suffix[i], "");
912 IMB_saveiff(ibuf, iv->filepath, IB_rect | IB_mem);
914 if (ibuf->encodedbuffer == NULL) {
915 printf("memory save for pack error\n");
917 image_free_packedfiles(ima);
921 ImagePackedFile *imapf;
922 PackedFile *pf = MEM_callocN(sizeof(*pf), "PackedFile");
924 pf->data = ibuf->encodedbuffer;
925 pf->size = ibuf->encodedsize;
927 imapf = MEM_mallocN(sizeof(ImagePackedFile), "Image PackedFile");
928 BLI_strncpy(imapf->filepath, iv->filepath, sizeof(imapf->filepath));
929 imapf->packedfile = pf;
930 BLI_addtail(&ima->packedfiles, imapf);
932 ibuf->encodedbuffer = NULL;
933 ibuf->encodedsize = 0;
934 ibuf->userflags &= ~IB_BITMAPDIRTY;
939 if (ima->source == IMA_SRC_GENERATED) {
940 ima->source = IMA_SRC_FILE;
941 ima->type = IMA_TYPE_IMAGE;
943 ima->views_format = R_IMF_VIEWS_INDIVIDUAL;
946 /* packs rect from memory as PNG */
947 void BKE_image_memorypack(Image *ima)
951 if (BKE_image_is_multiview(ima)) {
952 image_memorypack_multiview(ima);
956 ibuf = image_get_cached_ibuf_for_index_frame(ima, IMA_NO_INDEX, 0);
961 image_free_packedfiles(ima);
963 ibuf->ftype = IMB_FTYPE_PNG;
964 ibuf->planes = R_IMF_PLANES_RGBA;
966 IMB_saveiff(ibuf, ibuf->name, IB_rect | IB_mem);
967 if (ibuf->encodedbuffer == NULL) {
968 printf("memory save for pack error\n");
971 ImagePackedFile *imapf;
972 PackedFile *pf = MEM_callocN(sizeof(*pf), "PackedFile");
974 pf->data = ibuf->encodedbuffer;
975 pf->size = ibuf->encodedsize;
977 imapf = MEM_mallocN(sizeof(ImagePackedFile), "Image PackedFile");
978 BLI_strncpy(imapf->filepath, ima->name, sizeof(imapf->filepath));
979 imapf->packedfile = pf;
980 BLI_addtail(&ima->packedfiles, imapf);
982 ibuf->encodedbuffer = NULL;
983 ibuf->encodedsize = 0;
984 ibuf->userflags &= ~IB_BITMAPDIRTY;
986 if (ima->source == IMA_SRC_GENERATED) {
987 ima->source = IMA_SRC_FILE;
988 ima->type = IMA_TYPE_IMAGE;
995 void BKE_image_packfiles(ReportList *reports, Image *ima, const char *basepath)
997 const int totfiles = image_num_files(ima);
1000 ImagePackedFile *imapf = MEM_mallocN(sizeof(ImagePackedFile), "Image packed file");
1001 BLI_addtail(&ima->packedfiles, imapf);
1002 imapf->packedfile = newPackedFile(reports, ima->name, basepath);
1003 if (imapf->packedfile) {
1004 BLI_strncpy(imapf->filepath, ima->name, sizeof(imapf->filepath));
1007 BLI_freelinkN(&ima->packedfiles, imapf);
1012 for (iv = ima->views.first; iv; iv = iv->next) {
1013 ImagePackedFile *imapf = MEM_mallocN(sizeof(ImagePackedFile), "Image packed file");
1014 BLI_addtail(&ima->packedfiles, imapf);
1016 imapf->packedfile = newPackedFile(reports, iv->filepath, basepath);
1017 if (imapf->packedfile) {
1018 BLI_strncpy(imapf->filepath, iv->filepath, sizeof(imapf->filepath));
1021 BLI_freelinkN(&ima->packedfiles, imapf);
1027 void BKE_image_packfiles_from_mem(ReportList *reports, Image *ima, char *data, const size_t data_len)
1029 const int totfiles = image_num_files(ima);
1031 if (totfiles != 1) {
1032 BKE_report(reports, RPT_ERROR, "Cannot pack multiview images from raw data currently...");
1035 ImagePackedFile *imapf = MEM_mallocN(sizeof(ImagePackedFile), __func__);
1036 BLI_addtail(&ima->packedfiles, imapf);
1037 imapf->packedfile = newPackedFileMemory(data, data_len);
1038 BLI_strncpy(imapf->filepath, ima->name, sizeof(imapf->filepath));
1042 void BKE_image_tag_time(Image *ima)
1044 ima->lastused = PIL_check_seconds_timer_i();
1048 static void tag_all_images_time()
1051 int ctime = PIL_check_seconds_timer_i();
1053 ima = G.main->image.first;
1055 if (ima->bindcode || ima->repbind || ima->ibufs.first) {
1056 ima->lastused = ctime;
1062 static uintptr_t image_mem_size(Image *image)
1066 /* viewers have memory depending on other rules, has no valid rect pointer */
1067 if (image->source == IMA_SRC_VIEWER)
1070 BLI_spin_lock(&image_spin);
1071 if (image->cache != NULL) {
1072 struct MovieCacheIter *iter = IMB_moviecacheIter_new(image->cache);
1074 while (!IMB_moviecacheIter_done(iter)) {
1075 ImBuf *ibuf = IMB_moviecacheIter_getImBuf(iter);
1080 size += MEM_allocN_len(ibuf->rect);
1082 if (ibuf->rect_float) {
1083 size += MEM_allocN_len(ibuf->rect_float);
1086 for (level = 0; level < IMB_MIPMAP_LEVELS; level++) {
1087 ibufm = ibuf->mipmap[level];
1090 size += MEM_allocN_len(ibufm->rect);
1092 if (ibufm->rect_float) {
1093 size += MEM_allocN_len(ibufm->rect_float);
1098 IMB_moviecacheIter_step(iter);
1100 IMB_moviecacheIter_free(iter);
1102 BLI_spin_unlock(&image_spin);
1107 void BKE_image_print_memlist(void)
1110 uintptr_t size, totsize = 0;
1112 for (ima = G.main->image.first; ima; ima = ima->id.next)
1113 totsize += image_mem_size(ima);
1115 printf("\ntotal image memory len: %.3f MB\n", (double)totsize / (double)(1024 * 1024));
1117 for (ima = G.main->image.first; ima; ima = ima->id.next) {
1118 size = image_mem_size(ima);
1121 printf("%s len: %.3f MB\n", ima->id.name + 2, (double)size / (double)(1024 * 1024));
1125 static bool imagecache_check_dirty(ImBuf *ibuf, void *UNUSED(userkey), void *UNUSED(userdata))
1127 return (ibuf->userflags & IB_BITMAPDIRTY) == 0;
1130 void BKE_image_free_all_textures(void)
1132 #undef CHECK_FREED_SIZE
1136 #ifdef CHECK_FREED_SIZE
1137 uintptr_t tot_freed_size = 0;
1140 for (ima = G.main->image.first; ima; ima = ima->id.next)
1141 ima->id.tag &= ~LIB_TAG_DOIT;
1143 for (tex = G.main->tex.first; tex; tex = tex->id.next)
1145 tex->ima->id.tag |= LIB_TAG_DOIT;
1147 for (ima = G.main->image.first; ima; ima = ima->id.next) {
1148 if (ima->cache && (ima->id.tag & LIB_TAG_DOIT)) {
1149 #ifdef CHECK_FREED_SIZE
1150 uintptr_t old_size = image_mem_size(ima);
1153 IMB_moviecache_cleanup(ima->cache, imagecache_check_dirty, NULL);
1155 #ifdef CHECK_FREED_SIZE
1156 tot_freed_size += old_size - image_mem_size(ima);
1160 #ifdef CHECK_FREED_SIZE
1161 printf("%s: freed total %lu MB\n", __func__, tot_freed_size / (1024 * 1024));
1165 static bool imagecache_check_free_anim(ImBuf *ibuf, void *UNUSED(userkey), void *userdata)
1167 int except_frame = *(int *)userdata;
1168 return (ibuf->userflags & IB_BITMAPDIRTY) == 0 &&
1169 (ibuf->index != IMA_NO_INDEX) &&
1170 (except_frame != IMA_INDEX_FRAME(ibuf->index));
1173 /* except_frame is weak, only works for seqs without offset... */
1174 void BKE_image_free_anim_ibufs(Image *ima, int except_frame)
1176 BLI_spin_lock(&image_spin);
1177 if (ima->cache != NULL) {
1178 IMB_moviecache_cleanup(ima->cache, imagecache_check_free_anim, &except_frame);
1180 BLI_spin_unlock(&image_spin);
1183 void BKE_image_all_free_anim_ibufs(int cfra)
1187 for (ima = G.main->image.first; ima; ima = ima->id.next)
1188 if (BKE_image_is_animated(ima))
1189 BKE_image_free_anim_ibufs(ima, cfra);
1193 /* *********** READ AND WRITE ************** */
1195 int BKE_image_imtype_to_ftype(const char imtype, ImbFormatOptions *r_options)
1197 memset(r_options, 0, sizeof(*r_options));
1199 if (imtype == R_IMF_IMTYPE_TARGA)
1200 return IMB_FTYPE_TGA;
1201 else if (imtype == R_IMF_IMTYPE_RAWTGA) {
1202 r_options->flag = RAWTGA;
1203 return IMB_FTYPE_TGA;
1205 else if (imtype == R_IMF_IMTYPE_IRIS)
1206 return IMB_FTYPE_IMAGIC;
1208 else if (imtype == R_IMF_IMTYPE_RADHDR)
1209 return IMB_FTYPE_RADHDR;
1211 else if (imtype == R_IMF_IMTYPE_PNG) {
1212 r_options->quality = 15;
1213 return IMB_FTYPE_PNG;
1216 else if (imtype == R_IMF_IMTYPE_DDS)
1217 return IMB_FTYPE_DDS;
1219 else if (imtype == R_IMF_IMTYPE_BMP)
1220 return IMB_FTYPE_BMP;
1222 else if (imtype == R_IMF_IMTYPE_TIFF)
1223 return IMB_FTYPE_TIF;
1225 else if (imtype == R_IMF_IMTYPE_OPENEXR || imtype == R_IMF_IMTYPE_MULTILAYER)
1226 return IMB_FTYPE_OPENEXR;
1228 else if (imtype == R_IMF_IMTYPE_CINEON)
1229 return IMB_FTYPE_CINEON;
1230 else if (imtype == R_IMF_IMTYPE_DPX)
1231 return IMB_FTYPE_DPX;
1233 #ifdef WITH_OPENJPEG
1234 else if (imtype == R_IMF_IMTYPE_JP2)
1235 return IMB_FTYPE_JP2;
1238 r_options->quality = 90;
1239 return IMB_FTYPE_JPG;
1243 char BKE_image_ftype_to_imtype(const int ftype, const ImbFormatOptions *options)
1246 return R_IMF_IMTYPE_TARGA;
1247 else if (ftype == IMB_FTYPE_IMAGIC)
1248 return R_IMF_IMTYPE_IRIS;
1250 else if (ftype == IMB_FTYPE_RADHDR)
1251 return R_IMF_IMTYPE_RADHDR;
1253 else if (ftype == IMB_FTYPE_PNG)
1254 return R_IMF_IMTYPE_PNG;
1256 else if (ftype == IMB_FTYPE_DDS)
1257 return R_IMF_IMTYPE_DDS;
1259 else if (ftype == IMB_FTYPE_BMP)
1260 return R_IMF_IMTYPE_BMP;
1262 else if (ftype == IMB_FTYPE_TIF)
1263 return R_IMF_IMTYPE_TIFF;
1265 else if (ftype == IMB_FTYPE_OPENEXR)
1266 return R_IMF_IMTYPE_OPENEXR;
1268 else if (ftype == IMB_FTYPE_CINEON)
1269 return R_IMF_IMTYPE_CINEON;
1270 else if (ftype == IMB_FTYPE_DPX)
1271 return R_IMF_IMTYPE_DPX;
1273 else if (ftype == IMB_FTYPE_TGA) {
1274 if (options && (options->flag & RAWTGA))
1275 return R_IMF_IMTYPE_RAWTGA;
1277 return R_IMF_IMTYPE_TARGA;
1279 #ifdef WITH_OPENJPEG
1280 else if (ftype == IMB_FTYPE_JP2)
1281 return R_IMF_IMTYPE_JP2;
1284 return R_IMF_IMTYPE_JPEG90;
1288 bool BKE_imtype_is_movie(const char imtype)
1291 case R_IMF_IMTYPE_AVIRAW:
1292 case R_IMF_IMTYPE_AVIJPEG:
1293 case R_IMF_IMTYPE_QUICKTIME:
1294 case R_IMF_IMTYPE_FFMPEG:
1295 case R_IMF_IMTYPE_H264:
1296 case R_IMF_IMTYPE_THEORA:
1297 case R_IMF_IMTYPE_XVID:
1298 case R_IMF_IMTYPE_FRAMESERVER:
1304 int BKE_imtype_supports_zbuf(const char imtype)
1307 case R_IMF_IMTYPE_IRIZ:
1308 case R_IMF_IMTYPE_OPENEXR: /* but not R_IMF_IMTYPE_MULTILAYER */
1314 int BKE_imtype_supports_compress(const char imtype)
1317 case R_IMF_IMTYPE_PNG:
1323 int BKE_imtype_supports_quality(const char imtype)
1326 case R_IMF_IMTYPE_JPEG90:
1327 case R_IMF_IMTYPE_JP2:
1328 case R_IMF_IMTYPE_AVIJPEG:
1334 int BKE_imtype_requires_linear_float(const char imtype)
1337 case R_IMF_IMTYPE_CINEON:
1338 case R_IMF_IMTYPE_DPX:
1339 case R_IMF_IMTYPE_RADHDR:
1340 case R_IMF_IMTYPE_OPENEXR:
1341 case R_IMF_IMTYPE_MULTILAYER:
1347 char BKE_imtype_valid_channels(const char imtype, bool write_file)
1349 char chan_flag = IMA_CHAN_FLAG_RGB; /* assume all support rgb */
1353 case R_IMF_IMTYPE_BMP:
1354 if (write_file) break;
1356 case R_IMF_IMTYPE_TARGA:
1357 case R_IMF_IMTYPE_RAWTGA:
1358 case R_IMF_IMTYPE_IRIS:
1359 case R_IMF_IMTYPE_PNG:
1360 case R_IMF_IMTYPE_RADHDR:
1361 case R_IMF_IMTYPE_TIFF:
1362 case R_IMF_IMTYPE_OPENEXR:
1363 case R_IMF_IMTYPE_MULTILAYER:
1364 case R_IMF_IMTYPE_DDS:
1365 case R_IMF_IMTYPE_JP2:
1366 case R_IMF_IMTYPE_QUICKTIME:
1367 case R_IMF_IMTYPE_DPX:
1368 chan_flag |= IMA_CHAN_FLAG_ALPHA;
1374 case R_IMF_IMTYPE_PNG:
1375 case R_IMF_IMTYPE_JPEG90:
1376 case R_IMF_IMTYPE_TARGA:
1377 case R_IMF_IMTYPE_RAWTGA:
1378 case R_IMF_IMTYPE_TIFF:
1379 case R_IMF_IMTYPE_IRIS:
1380 chan_flag |= IMA_CHAN_FLAG_BW;
1387 char BKE_imtype_valid_depths(const char imtype)
1390 case R_IMF_IMTYPE_RADHDR:
1391 return R_IMF_CHAN_DEPTH_32;
1392 case R_IMF_IMTYPE_TIFF:
1393 return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_16;
1394 case R_IMF_IMTYPE_OPENEXR:
1395 return R_IMF_CHAN_DEPTH_16 | R_IMF_CHAN_DEPTH_32;
1396 case R_IMF_IMTYPE_MULTILAYER:
1397 return R_IMF_CHAN_DEPTH_16 | R_IMF_CHAN_DEPTH_32;
1398 /* eeh, cineon does some strange 10bits per channel */
1399 case R_IMF_IMTYPE_DPX:
1400 return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_10 | R_IMF_CHAN_DEPTH_12 | R_IMF_CHAN_DEPTH_16;
1401 case R_IMF_IMTYPE_CINEON:
1402 return R_IMF_CHAN_DEPTH_10;
1403 case R_IMF_IMTYPE_JP2:
1404 return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_12 | R_IMF_CHAN_DEPTH_16;
1405 case R_IMF_IMTYPE_PNG:
1406 return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_16;
1407 /* most formats are 8bit only */
1409 return R_IMF_CHAN_DEPTH_8;
1414 /* string is from command line --render-format arg, keep in sync with
1415 * creator.c help info */
1416 char BKE_imtype_from_arg(const char *imtype_arg)
1418 if (STREQ(imtype_arg, "TGA")) return R_IMF_IMTYPE_TARGA;
1419 else if (STREQ(imtype_arg, "IRIS")) return R_IMF_IMTYPE_IRIS;
1421 else if (STREQ(imtype_arg, "DDS")) return R_IMF_IMTYPE_DDS;
1423 else if (STREQ(imtype_arg, "JPEG")) return R_IMF_IMTYPE_JPEG90;
1424 else if (STREQ(imtype_arg, "IRIZ")) return R_IMF_IMTYPE_IRIZ;
1425 else if (STREQ(imtype_arg, "RAWTGA")) return R_IMF_IMTYPE_RAWTGA;
1426 else if (STREQ(imtype_arg, "AVIRAW")) return R_IMF_IMTYPE_AVIRAW;
1427 else if (STREQ(imtype_arg, "AVIJPEG")) return R_IMF_IMTYPE_AVIJPEG;
1428 else if (STREQ(imtype_arg, "PNG")) return R_IMF_IMTYPE_PNG;
1429 else if (STREQ(imtype_arg, "QUICKTIME")) return R_IMF_IMTYPE_QUICKTIME;
1430 else if (STREQ(imtype_arg, "BMP")) return R_IMF_IMTYPE_BMP;
1432 else if (STREQ(imtype_arg, "HDR")) return R_IMF_IMTYPE_RADHDR;
1435 else if (STREQ(imtype_arg, "TIFF")) return R_IMF_IMTYPE_TIFF;
1438 else if (STREQ(imtype_arg, "EXR")) return R_IMF_IMTYPE_OPENEXR;
1439 else if (STREQ(imtype_arg, "MULTILAYER")) return R_IMF_IMTYPE_MULTILAYER;
1441 else if (STREQ(imtype_arg, "MPEG")) return R_IMF_IMTYPE_FFMPEG;
1442 else if (STREQ(imtype_arg, "FRAMESERVER")) return R_IMF_IMTYPE_FRAMESERVER;
1444 else if (STREQ(imtype_arg, "CINEON")) return R_IMF_IMTYPE_CINEON;
1445 else if (STREQ(imtype_arg, "DPX")) return R_IMF_IMTYPE_DPX;
1447 #ifdef WITH_OPENJPEG
1448 else if (STREQ(imtype_arg, "JP2")) return R_IMF_IMTYPE_JP2;
1450 else return R_IMF_IMTYPE_INVALID;
1453 static bool do_add_image_extension(char *string, const char imtype, const ImageFormatData *im_format)
1455 const char *extension = NULL;
1456 const char *extension_test;
1457 (void)im_format; /* may be unused, depends on build options */
1459 if (imtype == R_IMF_IMTYPE_IRIS) {
1460 if (!BLI_testextensie(string, extension_test = ".rgb"))
1461 extension = extension_test;
1463 else if (imtype == R_IMF_IMTYPE_IRIZ) {
1464 if (!BLI_testextensie(string, extension_test = ".rgb"))
1465 extension = extension_test;
1468 else if (imtype == R_IMF_IMTYPE_RADHDR) {
1469 if (!BLI_testextensie(string, extension_test = ".hdr"))
1470 extension = extension_test;
1473 else if (ELEM(imtype, R_IMF_IMTYPE_PNG, R_IMF_IMTYPE_FFMPEG, R_IMF_IMTYPE_H264, R_IMF_IMTYPE_THEORA, R_IMF_IMTYPE_XVID)) {
1474 if (!BLI_testextensie(string, extension_test = ".png"))
1475 extension = extension_test;
1478 else if (imtype == R_IMF_IMTYPE_DDS) {
1479 if (!BLI_testextensie(string, extension_test = ".dds"))
1480 extension = extension_test;
1483 else if (ELEM(imtype, R_IMF_IMTYPE_TARGA, R_IMF_IMTYPE_RAWTGA)) {
1484 if (!BLI_testextensie(string, extension_test = ".tga"))
1485 extension = extension_test;
1487 else if (imtype == R_IMF_IMTYPE_BMP) {
1488 if (!BLI_testextensie(string, extension_test = ".bmp"))
1489 extension = extension_test;
1492 else if (imtype == R_IMF_IMTYPE_TIFF) {
1493 if (!BLI_testextensie_n(string, extension_test = ".tif", ".tiff", NULL)) {
1494 extension = extension_test;
1498 #ifdef WITH_OPENIMAGEIO
1499 else if (imtype == R_IMF_IMTYPE_PSD) {
1500 if (!BLI_testextensie(string, extension_test = ".psd"))
1501 extension = extension_test;
1505 else if (imtype == R_IMF_IMTYPE_OPENEXR || imtype == R_IMF_IMTYPE_MULTILAYER) {
1506 if (!BLI_testextensie(string, extension_test = ".exr"))
1507 extension = extension_test;
1511 else if (imtype == R_IMF_IMTYPE_CINEON) {
1512 if (!BLI_testextensie(string, extension_test = ".cin"))
1513 extension = extension_test;
1515 else if (imtype == R_IMF_IMTYPE_DPX) {
1516 if (!BLI_testextensie(string, extension_test = ".dpx"))
1517 extension = extension_test;
1520 #ifdef WITH_OPENJPEG
1521 else if (imtype == R_IMF_IMTYPE_JP2) {
1523 if (im_format->jp2_codec == R_IMF_JP2_CODEC_JP2) {
1524 if (!BLI_testextensie(string, extension_test = ".jp2"))
1525 extension = extension_test;
1527 else if (im_format->jp2_codec == R_IMF_JP2_CODEC_J2K) {
1528 if (!BLI_testextensie(string, extension_test = ".j2c"))
1529 extension = extension_test;
1532 BLI_assert(!"Unsupported jp2 codec was specified in im_format->jp2_codec");
1535 if (!BLI_testextensie(string, extension_test = ".jp2"))
1536 extension = extension_test;
1540 else { // R_IMF_IMTYPE_AVIRAW, R_IMF_IMTYPE_AVIJPEG, R_IMF_IMTYPE_JPEG90, R_IMF_IMTYPE_QUICKTIME etc
1541 if (!(BLI_testextensie_n(string, extension_test = ".jpg", ".jpeg", NULL)))
1542 extension = extension_test;
1546 /* prefer this in many cases to avoid .png.tga, but in certain cases it breaks */
1547 /* remove any other known image extension */
1548 if (BLI_testextensie_array(string, imb_ext_image) ||
1549 (G.have_quicktime && BLI_testextensie_array(string, imb_ext_image_qt)))
1551 return BLI_replace_extension(string, FILE_MAX, extension);
1554 return BLI_ensure_extension(string, FILE_MAX, extension);
1563 int BKE_image_path_ensure_ext_from_imformat(char *string, const ImageFormatData *im_format)
1565 return do_add_image_extension(string, im_format->imtype, im_format);
1568 int BKE_image_path_ensure_ext_from_imtype(char *string, const char imtype)
1570 return do_add_image_extension(string, imtype, NULL);
1573 void BKE_imformat_defaults(ImageFormatData *im_format)
1575 memset(im_format, 0, sizeof(*im_format));
1576 im_format->planes = R_IMF_PLANES_RGBA;
1577 im_format->imtype = R_IMF_IMTYPE_PNG;
1578 im_format->depth = R_IMF_CHAN_DEPTH_8;
1579 im_format->quality = 90;
1580 im_format->compress = 15;
1582 BKE_color_managed_display_settings_init(&im_format->display_settings);
1583 BKE_color_managed_view_settings_init(&im_format->view_settings);
1586 void BKE_imbuf_to_image_format(struct ImageFormatData *im_format, const ImBuf *imbuf)
1588 int ftype = imbuf->ftype;
1589 int custom_flags = imbuf->foptions.flag;
1590 char quality = imbuf->foptions.quality;
1592 BKE_imformat_defaults(im_format);
1596 if (ftype == IMB_FTYPE_IMAGIC)
1597 im_format->imtype = R_IMF_IMTYPE_IRIS;
1600 else if (ftype == IMB_FTYPE_RADHDR)
1601 im_format->imtype = R_IMF_IMTYPE_RADHDR;
1604 else if (ftype == IMB_FTYPE_PNG) {
1605 im_format->imtype = R_IMF_IMTYPE_PNG;
1607 if (custom_flags & PNG_16BIT)
1608 im_format->depth = R_IMF_CHAN_DEPTH_16;
1610 im_format->compress = quality;
1614 else if (ftype == IMB_FTYPE_DDS)
1615 im_format->imtype = R_IMF_IMTYPE_DDS;
1618 else if (ftype == IMB_FTYPE_BMP)
1619 im_format->imtype = R_IMF_IMTYPE_BMP;
1622 else if (ftype == IMB_FTYPE_TIF) {
1623 im_format->imtype = R_IMF_IMTYPE_TIFF;
1624 if (custom_flags & TIF_16BIT)
1625 im_format->depth = R_IMF_CHAN_DEPTH_16;
1626 if (custom_flags & TIF_COMPRESS_NONE)
1627 im_format->tiff_codec = R_IMF_TIFF_CODEC_NONE;
1628 if (custom_flags & TIF_COMPRESS_DEFLATE)
1629 im_format->tiff_codec = R_IMF_TIFF_CODEC_DEFLATE;
1630 if (custom_flags & TIF_COMPRESS_LZW)
1631 im_format->tiff_codec = R_IMF_TIFF_CODEC_LZW;
1632 if (custom_flags & TIF_COMPRESS_PACKBITS)
1633 im_format->tiff_codec = R_IMF_TIFF_CODEC_PACKBITS;
1638 else if (ftype == IMB_FTYPE_OPENEXR) {
1639 im_format->imtype = R_IMF_IMTYPE_OPENEXR;
1640 if (custom_flags & OPENEXR_HALF)
1641 im_format->depth = R_IMF_CHAN_DEPTH_16;
1642 if (custom_flags & OPENEXR_COMPRESS)
1643 im_format->exr_codec = R_IMF_EXR_CODEC_ZIP; // Can't determine compression
1644 if (imbuf->zbuf_float)
1645 im_format->flag |= R_IMF_FLAG_ZBUF;
1650 else if (ftype == IMB_FTYPE_CINEON)
1651 im_format->imtype = R_IMF_IMTYPE_CINEON;
1652 else if (ftype == IMB_FTYPE_DPX)
1653 im_format->imtype = R_IMF_IMTYPE_DPX;
1656 else if (ftype == IMB_FTYPE_TGA) {
1657 if (custom_flags & RAWTGA)
1658 im_format->imtype = R_IMF_IMTYPE_RAWTGA;
1660 im_format->imtype = R_IMF_IMTYPE_TARGA;
1662 #ifdef WITH_OPENJPEG
1663 else if (ftype == IMB_FTYPE_JP2) {
1664 im_format->imtype = R_IMF_IMTYPE_JP2;
1665 im_format->quality = quality;
1667 if (custom_flags & JP2_16BIT)
1668 im_format->depth = R_IMF_CHAN_DEPTH_16;
1669 else if (custom_flags & JP2_12BIT)
1670 im_format->depth = R_IMF_CHAN_DEPTH_12;
1672 if (custom_flags & JP2_YCC)
1673 im_format->jp2_flag |= R_IMF_JP2_FLAG_YCC;
1675 if (custom_flags & JP2_CINE) {
1676 im_format->jp2_flag |= R_IMF_JP2_FLAG_CINE_PRESET;
1677 if (custom_flags & JP2_CINE_48FPS)
1678 im_format->jp2_flag |= R_IMF_JP2_FLAG_CINE_48;
1681 if (custom_flags & JP2_JP2)
1682 im_format->jp2_codec = R_IMF_JP2_CODEC_JP2;
1683 else if (custom_flags & JP2_J2K)
1684 im_format->jp2_codec = R_IMF_JP2_CODEC_J2K;
1686 BLI_assert(!"Unsupported jp2 codec was specified in file type");
1691 im_format->imtype = R_IMF_IMTYPE_JPEG90;
1692 im_format->quality = quality;
1696 /* TODO(sergey): Channels doesn't correspond actual planes used for image buffer
1697 * For example byte buffer will have 4 channels but it might easily
1698 * be BW or RGB image.
1700 * Need to use im_format->planes = imbuf->planes instead?
1702 switch (imbuf->channels) {
1704 case 4: im_format->planes = R_IMF_PLANES_RGBA;
1706 case 3: im_format->planes = R_IMF_PLANES_RGB;
1708 case 1: im_format->planes = R_IMF_PLANES_BW;
1710 default: im_format->planes = R_IMF_PLANES_RGB;
1717 #define STAMP_NAME_SIZE ((MAX_ID_NAME - 2) + 16)
1718 /* could allow access externally - 512 is for long names,
1719 * STAMP_NAME_SIZE is for id names, allowing them some room for description */
1720 typedef struct StampData {
1727 char camera[STAMP_NAME_SIZE];
1728 char cameralens[STAMP_NAME_SIZE];
1729 char scene[STAMP_NAME_SIZE];
1730 char strip[STAMP_NAME_SIZE];
1731 char rendertime[STAMP_NAME_SIZE];
1733 #undef STAMP_NAME_SIZE
1735 static void stampdata(Scene *scene, Object *camera, StampData *stamp_data, int do_prefix)
1741 if (scene->r.stamp & R_STAMP_FILENAME) {
1742 BLI_snprintf(stamp_data->file, sizeof(stamp_data->file), do_prefix ? "File %s" : "%s", G.relbase_valid ? G.main->name : "<untitled>");
1745 stamp_data->file[0] = '\0';
1748 if (scene->r.stamp & R_STAMP_NOTE) {
1749 /* Never do prefix for Note */
1750 BLI_snprintf(stamp_data->note, sizeof(stamp_data->note), "%s", scene->r.stamp_udata);
1753 stamp_data->note[0] = '\0';
1756 if (scene->r.stamp & R_STAMP_DATE) {
1759 BLI_snprintf(text, sizeof(text), "%04d/%02d/%02d %02d:%02d:%02d", tl->tm_year + 1900, tl->tm_mon + 1, tl->tm_mday, tl->tm_hour, tl->tm_min, tl->tm_sec);
1760 BLI_snprintf(stamp_data->date, sizeof(stamp_data->date), do_prefix ? "Date %s" : "%s", text);
1763 stamp_data->date[0] = '\0';
1766 if (scene->r.stamp & R_STAMP_MARKER) {
1767 const char *name = BKE_scene_find_last_marker_name(scene, CFRA);
1769 if (name) BLI_strncpy(text, name, sizeof(text));
1770 else BLI_strncpy(text, "<none>", sizeof(text));
1772 BLI_snprintf(stamp_data->marker, sizeof(stamp_data->marker), do_prefix ? "Marker %s" : "%s", text);
1775 stamp_data->marker[0] = '\0';
1778 if (scene->r.stamp & R_STAMP_TIME) {
1779 const short timecode_style = USER_TIMECODE_SMPTE_FULL;
1780 BLI_timecode_string_from_time(text, sizeof(text), 0, FRA2TIME(scene->r.cfra), FPS, timecode_style);
1781 BLI_snprintf(stamp_data->time, sizeof(stamp_data->time), do_prefix ? "Timecode %s" : "%s", text);
1784 stamp_data->time[0] = '\0';
1787 if (scene->r.stamp & R_STAMP_FRAME) {
1791 if (scene->r.efra > 9)
1792 digits = 1 + (int) log10(scene->r.efra);
1794 BLI_snprintf(fmtstr, sizeof(fmtstr), do_prefix ? "Frame %%0%di" : "%%0%di", digits);
1795 BLI_snprintf(stamp_data->frame, sizeof(stamp_data->frame), fmtstr, scene->r.cfra);
1798 stamp_data->frame[0] = '\0';
1801 if (scene->r.stamp & R_STAMP_CAMERA) {
1802 BLI_snprintf(stamp_data->camera, sizeof(stamp_data->camera), do_prefix ? "Camera %s" : "%s", camera ? camera->id.name + 2 : "<none>");
1805 stamp_data->camera[0] = '\0';
1808 if (scene->r.stamp & R_STAMP_CAMERALENS) {
1809 if (camera && camera->type == OB_CAMERA) {
1810 BLI_snprintf(text, sizeof(text), "%.2f", ((Camera *)camera->data)->lens);
1813 BLI_strncpy(text, "<none>", sizeof(text));
1816 BLI_snprintf(stamp_data->cameralens, sizeof(stamp_data->cameralens), do_prefix ? "Lens %s" : "%s", text);
1819 stamp_data->cameralens[0] = '\0';
1822 if (scene->r.stamp & R_STAMP_SCENE) {
1823 BLI_snprintf(stamp_data->scene, sizeof(stamp_data->scene), do_prefix ? "Scene %s" : "%s", scene->id.name + 2);
1826 stamp_data->scene[0] = '\0';
1829 if (scene->r.stamp & R_STAMP_SEQSTRIP) {
1830 Sequence *seq = BKE_sequencer_foreground_frame_get(scene, scene->r.cfra);
1832 if (seq) BLI_strncpy(text, seq->name + 2, sizeof(text));
1833 else BLI_strncpy(text, "<none>", sizeof(text));
1835 BLI_snprintf(stamp_data->strip, sizeof(stamp_data->strip), do_prefix ? "Strip %s" : "%s", text);
1838 stamp_data->strip[0] = '\0';
1842 Render *re = RE_GetRender(scene->id.name);
1843 RenderStats *stats = re ? RE_GetStats(re) : NULL;
1845 if (stats && (scene->r.stamp & R_STAMP_RENDERTIME)) {
1846 BLI_timecode_string_from_time_simple(text, sizeof(text), stats->lastframetime);
1848 BLI_snprintf(stamp_data->rendertime, sizeof(stamp_data->rendertime), do_prefix ? "RenderTime %s" : "%s", text);
1851 stamp_data->rendertime[0] = '\0';
1856 void BKE_image_stamp_buf(
1857 Scene *scene, Object *camera,
1858 unsigned char *rect, float *rectf, int width, int height, int channels)
1860 struct StampData stamp_data;
1864 const int mono = blf_mono_font_render; // XXX
1865 struct ColorManagedDisplay *display;
1866 const char *display_device;
1868 /* vars for calculating wordwrap */
1870 struct ResultBLF info;
1874 /* this could be an argument if we want to operate on non linear float imbuf's
1875 * for now though this is only used for renders which use scene settings */
1877 #define TEXT_SIZE_CHECK(str, w, h) \
1878 ((str[0]) && ((void)(h = h_fixed), (w = BLF_width(mono, str, sizeof(str)))))
1880 /* must enable BLF_WORD_WRAP before using */
1881 #define TEXT_SIZE_CHECK_WORD_WRAP(str, w, h) \
1882 ((str[0]) && (BLF_boundbox_ex(mono, str, sizeof(str), &wrap.rect, &wrap.info), \
1883 (void)(h = h_fixed * wrap.info.lines), (w = BLI_rctf_size_x(&wrap.rect))))
1885 #define BUFF_MARGIN_X 2
1886 #define BUFF_MARGIN_Y 1
1888 if (!rect && !rectf)
1891 display_device = scene->display_settings.display_device;
1892 display = IMB_colormanagement_display_get_named(display_device);
1894 stampdata(scene, camera, &stamp_data, 1);
1896 /* TODO, do_versions */
1897 if (scene->r.stamp_font_id < 8)
1898 scene->r.stamp_font_id = 12;
1900 /* set before return */
1901 BLF_size(mono, scene->r.stamp_font_id, 72);
1902 BLF_wordwrap(mono, width - (BUFF_MARGIN_X * 2));
1904 BLF_buffer(mono, rectf, rect, width, height, channels, display);
1905 BLF_buffer_col(mono, scene->r.fg_stamp[0], scene->r.fg_stamp[1], scene->r.fg_stamp[2], 1.0);
1906 pad = BLF_width_max(mono);
1908 /* use 'h_fixed' rather than 'h', aligns better */
1909 h_fixed = BLF_height_max(mono);
1910 y_ofs = -BLF_descender(mono);
1915 if (TEXT_SIZE_CHECK(stamp_data.file, w, h)) {
1916 /* Top left corner */
1919 /* also a little of space to the background. */
1920 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
1921 x - BUFF_MARGIN_X, y - BUFF_MARGIN_Y, w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
1923 /* and draw the text. */
1924 BLF_position(mono, x, y + y_ofs, 0.0);
1925 BLF_draw_buffer(mono, stamp_data.file, BLF_DRAW_STR_DUMMY_MAX);
1927 /* the extra pixel for background. */
1928 y -= BUFF_MARGIN_Y * 2;
1931 /* Top left corner, below File */
1932 if (TEXT_SIZE_CHECK(stamp_data.date, w, h)) {
1935 /* and space for background. */
1936 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
1937 0, y - BUFF_MARGIN_Y, w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
1939 BLF_position(mono, x, y + y_ofs, 0.0);
1940 BLF_draw_buffer(mono, stamp_data.date, BLF_DRAW_STR_DUMMY_MAX);
1942 /* the extra pixel for background. */
1943 y -= BUFF_MARGIN_Y * 2;
1946 /* Top left corner, below File, Date */
1947 if (TEXT_SIZE_CHECK(stamp_data.rendertime, w, h)) {
1950 /* and space for background. */
1951 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
1952 0, y - BUFF_MARGIN_Y, w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
1954 BLF_position(mono, x, y + y_ofs, 0.0);
1955 BLF_draw_buffer(mono, stamp_data.rendertime, BLF_DRAW_STR_DUMMY_MAX);
1957 /* the extra pixel for background. */
1958 y -= BUFF_MARGIN_Y * 2;
1961 /* Top left corner, below File, Date, Rendertime */
1962 BLF_enable(mono, BLF_WORD_WRAP);
1963 if (TEXT_SIZE_CHECK_WORD_WRAP(stamp_data.note, w, h)) {
1966 /* and space for background. */
1967 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
1968 0, y - BUFF_MARGIN_Y, w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
1970 BLF_position(mono, x, y + y_ofs + (h - h_fixed), 0.0);
1971 BLF_draw_buffer(mono, stamp_data.note, BLF_DRAW_STR_DUMMY_MAX);
1973 BLF_disable(mono, BLF_WORD_WRAP);
1978 /* Bottom left corner, leaving space for timing */
1979 if (TEXT_SIZE_CHECK(stamp_data.marker, w, h)) {
1981 /* extra space for background. */
1982 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
1983 x - BUFF_MARGIN_X, y - BUFF_MARGIN_Y, w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
1985 /* and pad the text. */
1986 BLF_position(mono, x, y + y_ofs, 0.0);
1987 BLF_draw_buffer(mono, stamp_data.marker, BLF_DRAW_STR_DUMMY_MAX);
1993 /* Left bottom corner */
1994 if (TEXT_SIZE_CHECK(stamp_data.time, w, h)) {
1996 /* extra space for background */
1997 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
1998 x - BUFF_MARGIN_X, y, x + w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
2000 /* and pad the text. */
2001 BLF_position(mono, x, y + y_ofs, 0.0);
2002 BLF_draw_buffer(mono, stamp_data.time, BLF_DRAW_STR_DUMMY_MAX);
2008 if (TEXT_SIZE_CHECK(stamp_data.frame, w, h)) {
2010 /* extra space for background. */
2011 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
2012 x - BUFF_MARGIN_X, y - BUFF_MARGIN_Y, x + w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
2014 /* and pad the text. */
2015 BLF_position(mono, x, y + y_ofs, 0.0);
2016 BLF_draw_buffer(mono, stamp_data.frame, BLF_DRAW_STR_DUMMY_MAX);
2022 if (TEXT_SIZE_CHECK(stamp_data.camera, w, h)) {
2024 /* extra space for background. */
2025 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
2026 x - BUFF_MARGIN_X, y - BUFF_MARGIN_Y, x + w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
2027 BLF_position(mono, x, y + y_ofs, 0.0);
2028 BLF_draw_buffer(mono, stamp_data.camera, BLF_DRAW_STR_DUMMY_MAX);
2034 if (TEXT_SIZE_CHECK(stamp_data.cameralens, w, h)) {
2036 /* extra space for background. */
2037 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
2038 x - BUFF_MARGIN_X, y - BUFF_MARGIN_Y, x + w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
2039 BLF_position(mono, x, y + y_ofs, 0.0);
2040 BLF_draw_buffer(mono, stamp_data.cameralens, BLF_DRAW_STR_DUMMY_MAX);
2043 if (TEXT_SIZE_CHECK(stamp_data.scene, w, h)) {
2045 /* Bottom right corner, with an extra space because blenfont is too strict! */
2048 /* extra space for background. */
2049 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
2050 x - BUFF_MARGIN_X, y - BUFF_MARGIN_Y, x + w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
2052 /* and pad the text. */
2053 BLF_position(mono, x, y + y_ofs, 0.0);
2054 BLF_draw_buffer(mono, stamp_data.scene, BLF_DRAW_STR_DUMMY_MAX);
2057 if (TEXT_SIZE_CHECK(stamp_data.strip, w, h)) {
2059 /* Top right corner, with an extra space because blenfont is too strict! */
2060 x = width - w - pad;
2063 /* extra space for background. */
2064 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, display,
2065 x - BUFF_MARGIN_X, y - BUFF_MARGIN_Y, x + w + BUFF_MARGIN_X, y + h + BUFF_MARGIN_Y);
2067 BLF_position(mono, x, y + y_ofs, 0.0);
2068 BLF_draw_buffer(mono, stamp_data.strip, BLF_DRAW_STR_DUMMY_MAX);
2071 /* cleanup the buffer. */
2072 BLF_buffer(mono, NULL, NULL, 0, 0, 0, NULL);
2073 BLF_wordwrap(mono, 0);
2075 #undef TEXT_SIZE_CHECK
2076 #undef TEXT_SIZE_CHECK_WORD_WRAP
2077 #undef BUFF_MARGIN_X
2078 #undef BUFF_MARGIN_Y
2081 void BKE_render_result_stamp_info(Scene *scene, Object *camera, struct RenderResult *rr, bool allocate_only)
2083 struct StampData *stamp_data;
2085 if (!(scene && (scene->r.stamp & R_STAMP_ALL)) && !allocate_only)
2088 if (!rr->stamp_data) {
2089 stamp_data = MEM_callocN(sizeof(StampData), "RenderResult.stamp_data");
2092 stamp_data = rr->stamp_data;
2096 stampdata(scene, camera, stamp_data, 0);
2098 if (!rr->stamp_data) {
2099 rr->stamp_data = stamp_data;
2103 void BKE_stamp_info_callback(void *data, struct StampData *stamp_data, StampCallback callback, bool noskip)
2105 if ((callback == NULL) || (stamp_data == NULL)) {
2109 #define CALL(member, value_str) \
2110 if (noskip || stamp_data->member[0]) { \
2111 callback(data, value_str, stamp_data->member, sizeof(stamp_data->member)); \
2117 CALL(marker, "Marker");
2119 CALL(frame, "Frame");
2120 CALL(camera, "Camera");
2121 CALL(cameralens, "Lens");
2122 CALL(scene, "Scene");
2123 CALL(strip, "Strip");
2124 CALL(rendertime, "RenderTime");
2129 /* wrap for callback only */
2130 static void metadata_change_field(void *data, const char *propname, char *propvalue, int UNUSED(len))
2132 IMB_metadata_change_field(data, propname, propvalue);
2135 static void metadata_get_field(void *data, const char *propname, char *propvalue, int len)
2137 IMB_metadata_get_field(data, propname, propvalue, len);
2140 void BKE_imbuf_stamp_info(RenderResult *rr, struct ImBuf *ibuf)
2142 struct StampData *stamp_data = rr->stamp_data;
2144 BKE_stamp_info_callback(ibuf, stamp_data, metadata_change_field, false);
2147 void BKE_stamp_info_from_imbuf(RenderResult *rr, struct ImBuf *ibuf)
2149 struct StampData *stamp_data = rr->stamp_data;
2151 BKE_stamp_info_callback(ibuf, stamp_data, metadata_get_field, true);
2154 bool BKE_imbuf_alpha_test(ImBuf *ibuf)
2157 if (ibuf->rect_float) {
2158 const float *buf = ibuf->rect_float;
2159 for (tot = ibuf->x * ibuf->y; tot--; buf += 4) {
2160 if (buf[3] < 1.0f) {
2165 else if (ibuf->rect) {
2166 unsigned char *buf = (unsigned char *)ibuf->rect;
2167 for (tot = ibuf->x * ibuf->y; tot--; buf += 4) {
2168 if (buf[3] != 255) {
2177 /* note: imf->planes is ignored here, its assumed the image channels
2178 * are already set */
2179 void BKE_imbuf_write_prepare(ImBuf *ibuf, const ImageFormatData *imf)
2181 char imtype = imf->imtype;
2182 char compress = imf->compress;
2183 char quality = imf->quality;
2185 /* initialize all from image format */
2186 ibuf->foptions.flag = 0;
2188 if (imtype == R_IMF_IMTYPE_IRIS) {
2189 ibuf->ftype = IMB_FTYPE_IMAGIC;
2192 else if (imtype == R_IMF_IMTYPE_RADHDR) {
2193 ibuf->ftype = IMB_FTYPE_RADHDR;
2196 else if (ELEM(imtype, R_IMF_IMTYPE_PNG, R_IMF_IMTYPE_FFMPEG, R_IMF_IMTYPE_H264, R_IMF_IMTYPE_THEORA, R_IMF_IMTYPE_XVID)) {
2197 ibuf->ftype = IMB_FTYPE_PNG;
2199 if (imtype == R_IMF_IMTYPE_PNG) {
2200 if (imf->depth == R_IMF_CHAN_DEPTH_16)
2201 ibuf->foptions.flag |= PNG_16BIT;
2203 ibuf->foptions.quality = compress;
2208 else if (imtype == R_IMF_IMTYPE_DDS) {
2209 ibuf->ftype = IMB_FTYPE_DDS;
2212 else if (imtype == R_IMF_IMTYPE_BMP) {
2213 ibuf->ftype = IMB_FTYPE_BMP;
2216 else if (imtype == R_IMF_IMTYPE_TIFF) {
2217 ibuf->ftype = IMB_FTYPE_TIF;
2219 if (imf->depth == R_IMF_CHAN_DEPTH_16) {
2220 ibuf->foptions.flag |= TIF_16BIT;
2222 if (imf->tiff_codec == R_IMF_TIFF_CODEC_NONE) {
2223 ibuf->foptions.flag |= TIF_COMPRESS_NONE;
2225 else if (imf->tiff_codec == R_IMF_TIFF_CODEC_DEFLATE) {
2226 ibuf->foptions.flag |= TIF_COMPRESS_DEFLATE;
2228 else if (imf->tiff_codec == R_IMF_TIFF_CODEC_LZW) {
2229 ibuf->foptions.flag |= TIF_COMPRESS_LZW;
2231 else if (imf->tiff_codec == R_IMF_TIFF_CODEC_PACKBITS) {
2232 ibuf->foptions.flag |= TIF_COMPRESS_PACKBITS;
2237 else if (ELEM(imtype, R_IMF_IMTYPE_OPENEXR, R_IMF_IMTYPE_MULTILAYER)) {
2238 ibuf->ftype = IMB_FTYPE_OPENEXR;
2239 if (imf->depth == R_IMF_CHAN_DEPTH_16)
2240 ibuf->foptions.flag |= OPENEXR_HALF;
2241 ibuf->foptions.flag |= (imf->exr_codec & OPENEXR_COMPRESS);
2243 if (!(imf->flag & R_IMF_FLAG_ZBUF))
2244 ibuf->zbuf_float = NULL; /* signal for exr saving */
2249 else if (imtype == R_IMF_IMTYPE_CINEON) {
2250 ibuf->ftype = IMB_FTYPE_CINEON;
2251 if (imf->cineon_flag & R_IMF_CINEON_FLAG_LOG) {
2252 ibuf->foptions.flag |= CINEON_LOG;
2254 if (imf->depth == R_IMF_CHAN_DEPTH_16) {
2255 ibuf->foptions.flag |= CINEON_16BIT;
2257 else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
2258 ibuf->foptions.flag |= CINEON_12BIT;
2260 else if (imf->depth == R_IMF_CHAN_DEPTH_10) {
2261 ibuf->foptions.flag |= CINEON_10BIT;
2264 else if (imtype == R_IMF_IMTYPE_DPX) {
2265 ibuf->ftype = IMB_FTYPE_DPX;
2266 if (imf->cineon_flag & R_IMF_CINEON_FLAG_LOG) {
2267 ibuf->foptions.flag |= CINEON_LOG;
2269 if (imf->depth == R_IMF_CHAN_DEPTH_16) {
2270 ibuf->foptions.flag |= CINEON_16BIT;
2272 else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
2273 ibuf->foptions.flag |= CINEON_12BIT;
2275 else if (imf->depth == R_IMF_CHAN_DEPTH_10) {
2276 ibuf->foptions.flag |= CINEON_10BIT;
2280 else if (imtype == R_IMF_IMTYPE_TARGA) {
2281 ibuf->ftype = IMB_FTYPE_TGA;
2283 else if (imtype == R_IMF_IMTYPE_RAWTGA) {
2284 ibuf->ftype = IMB_FTYPE_TGA;
2285 ibuf->foptions.flag = RAWTGA;
2287 #ifdef WITH_OPENJPEG
2288 else if (imtype == R_IMF_IMTYPE_JP2) {
2289 if (quality < 10) quality = 90;
2290 ibuf->ftype = IMB_FTYPE_JP2;
2291 ibuf->foptions.quality = quality;
2293 if (imf->depth == R_IMF_CHAN_DEPTH_16) {
2294 ibuf->foptions.flag |= JP2_16BIT;
2296 else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
2297 ibuf->foptions.flag |= JP2_12BIT;
2300 if (imf->jp2_flag & R_IMF_JP2_FLAG_YCC) {
2301 ibuf->foptions.flag |= JP2_YCC;
2304 if (imf->jp2_flag & R_IMF_JP2_FLAG_CINE_PRESET) {
2305 ibuf->foptions.flag |= JP2_CINE;
2306 if (imf->jp2_flag & R_IMF_JP2_FLAG_CINE_48)
2307 ibuf->foptions.flag |= JP2_CINE_48FPS;
2310 if (imf->jp2_codec == R_IMF_JP2_CODEC_JP2)
2311 ibuf->foptions.flag |= JP2_JP2;
2312 else if (imf->jp2_codec == R_IMF_JP2_CODEC_J2K)
2313 ibuf->foptions.flag |= JP2_J2K;
2315 BLI_assert(!"Unsupported jp2 codec was specified in im_format->jp2_codec");
2319 /* R_IMF_IMTYPE_JPEG90, etc. default we save jpegs */
2320 if (quality < 10) quality = 90;
2321 ibuf->ftype = IMB_FTYPE_JPG;
2322 ibuf->foptions.quality = quality;
2326 int BKE_imbuf_write(ImBuf *ibuf, const char *name, const ImageFormatData *imf)
2330 BKE_imbuf_write_prepare(ibuf, imf);
2332 BLI_make_existing_file(name);
2334 ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
2342 /* same as BKE_imbuf_write() but crappy workaround not to permanently modify
2343 * _some_, values in the imbuf */
2344 int BKE_imbuf_write_as(ImBuf *ibuf, const char *name, ImageFormatData *imf,
2345 const bool save_copy)
2347 ImBuf ibuf_back = *ibuf;
2350 /* all data is rgba anyway,
2351 * this just controls how to save for some formats */
2352 ibuf->planes = imf->planes;
2354 ok = BKE_imbuf_write(ibuf, name, imf);
2357 /* note that we are not restoring _all_ settings */
2358 ibuf->planes = ibuf_back.planes;
2359 ibuf->ftype = ibuf_back.ftype;
2360 ibuf->foptions = ibuf_back.foptions;
2366 int BKE_imbuf_write_stamp(
2367 Scene *scene, struct RenderResult *rr, ImBuf *ibuf, const char *name,
2368 const struct ImageFormatData *imf)
2370 if (scene && scene->r.stamp & R_STAMP_ALL)
2371 BKE_imbuf_stamp_info(rr, ibuf);
2373 return BKE_imbuf_write(ibuf, name, imf);
2376 static void do_makepicstring(
2377 char *string, const char *base, const char *relbase, int frame, const char imtype,
2378 const ImageFormatData *im_format, const short use_ext, const short use_frames,
2381 if (string == NULL) return;
2382 BLI_strncpy(string, base, FILE_MAX - 10); /* weak assumption */
2383 BLI_path_abs(string, relbase);
2386 BLI_path_frame(string, frame, 4);
2389 BLI_path_suffix(string, FILE_MAX, suffix, "");
2392 do_add_image_extension(string, imtype, im_format);
2395 void BKE_image_path_from_imformat(
2396 char *string, const char *base, const char *relbase, int frame,
2397 const ImageFormatData *im_format, const bool use_ext, const bool use_frames, const char *suffix)
2399 do_makepicstring(string, base, relbase, frame, im_format->imtype, im_format, use_ext, use_frames, suffix);
2402 void BKE_image_path_from_imtype(
2403 char *string, const char *base, const char *relbase, int frame,
2404 const char imtype, const bool use_ext, const bool use_frames, const char *view)
2406 do_makepicstring(string, base, relbase, frame, imtype, NULL, use_ext, use_frames, view);
2409 struct anim *openanim_noload(const char *name, int flags, int streamindex, char colorspace[IMA_MAX_SPACE])
2413 anim = IMB_open_anim(name, flags, streamindex, colorspace);
2417 /* used by sequencer too */
2418 struct anim *openanim(const char *name, int flags, int streamindex, char colorspace[IMA_MAX_SPACE])
2423 anim = IMB_open_anim(name, flags, streamindex, colorspace);
2424 if (anim == NULL) return NULL;
2426 ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
2428 if (BLI_exists(name))
2429 printf("not an anim: %s\n", name);
2431 printf("anim file doesn't exist: %s\n", name);
2432 IMB_free_anim(anim);
2435 IMB_freeImBuf(ibuf);
2440 /* ************************* New Image API *************** */
2443 /* Notes about Image storage
2445 * -> written in .blend
2447 * -> written in .blend
2449 * -> comes from packedfile or filename
2451 * -> comes from packedfile or filename
2453 * -> ibufs from exrhandle
2455 * -> ibufs come from movie, temporary renderresult or sequence
2457 * -> comes from packedfile or filename or generated
2461 /* forces existence of 1 Image for renderout or nodes, returns Image */
2462 /* name is only for default, when making new one */
2463 Image *BKE_image_verify_viewer(int type, const char *name)
2467 for (ima = G.main->image.first; ima; ima = ima->id.next)
2468 if (ima->source == IMA_SRC_VIEWER)
2469 if (ima->type == type)
2473 ima = image_alloc(G.main, name, IMA_SRC_VIEWER, type);
2475 /* happens on reload, imagewindow cannot be image user when hidden*/
2476 if (ima->id.us == 0)
2477 id_us_plus(&ima->id);
2482 static void image_viewer_create_views(const RenderData *rd, Image *ima)
2484 if ((rd->scemode & R_MULTIVIEW) == 0) {
2485 image_add_view(ima, "", "");
2488 SceneRenderView *srv;
2489 for (srv = rd->views.first; srv; srv = srv->next) {
2490 if (BKE_scene_multiview_is_render_view_active(rd, srv) == false)
2492 image_add_view(ima, srv->name, "");
2497 /* Reset the image cache and views when the Viewer Nodes views don't match the scene views */
2498 void BKE_image_verify_viewer_views(const RenderData *rd, Image *ima, ImageUser *iuser)
2501 const bool is_multiview = (rd->scemode & R_MULTIVIEW) != 0;
2503 BLI_lock_thread(LOCK_DRAW_IMAGE);
2505 if (!BKE_scene_multiview_is_stereo3d(rd))
2506 iuser->flag &= ~IMA_SHOW_STEREO;
2508 /* see if all scene render views are in the image view list */
2509 do_reset = (BKE_scene_multiview_num_views_get(rd) != BLI_listbase_count(&ima->views));
2511 /* multiview also needs to be sure all the views are synced */
2512 if (is_multiview && !do_reset) {
2513 SceneRenderView *srv;
2516 for (iv = ima->views.first; iv; iv = iv->next) {
2517 srv = BLI_findstring(&rd->views, iv->name, offsetof(SceneRenderView, name));
2518 if ((srv == NULL) || (BKE_scene_multiview_is_render_view_active(rd, srv) == false)) {
2526 BLI_spin_lock(&image_spin);
2528 image_free_cached_frames(ima);
2529 BKE_image_free_views(ima);
2532 image_viewer_create_views(rd, ima);
2534 BLI_spin_unlock(&image_spin);
2537 BLI_unlock_thread(LOCK_DRAW_IMAGE);
2540 void BKE_image_walk_all_users(const Main *mainp, void *customdata,
2541 void callback(Image *ima, ImageUser *iuser, void *customdata))
2543 wmWindowManager *wm;
2548 for (tex = mainp->tex.first; tex; tex = tex->id.next) {
2549 if (tex->type == TEX_IMAGE && tex->ima) {
2550 callback(tex->ima, &tex->iuser, customdata);
2553 if (tex->nodetree) {
2555 for (node = tex->nodetree->nodes.first; node; node = node->next) {
2556 if (node->id && node->type == TEX_NODE_IMAGE) {
2557 Image *ima = (Image *)node->id;
2558 ImageUser *iuser = node->storage;
2559 callback(ima, iuser, customdata);
2565 /* image window, compo node users */
2566 for (wm = mainp->wm.first; wm; wm = wm->id.next) { /* only 1 wm */
2567 for (win = wm->windows.first; win; win = win->next) {
2569 for (sa = win->screen->areabase.first; sa; sa = sa->next) {
2570 if (sa->spacetype == SPACE_VIEW3D) {
2571 View3D *v3d = sa->spacedata.first;
2573 for (bgpic = v3d->bgpicbase.first; bgpic; bgpic = bgpic->next) {
2574 callback(bgpic->ima, &bgpic->iuser, customdata);
2577 else if (sa->spacetype == SPACE_IMAGE) {
2578 SpaceImage *sima = sa->spacedata.first;
2579 callback(sima->image, &sima->iuser, customdata);
2581 else if (sa->spacetype == SPACE_NODE) {
2582 SpaceNode *snode = sa->spacedata.first;
2583 if (snode->nodetree && snode->nodetree->type == NTREE_COMPOSIT) {
2585 for (node = snode->nodetree->nodes.first; node; node = node->next) {
2586 if (node->id && node->type == CMP_NODE_IMAGE) {
2587 Image *ima = (Image *)node->id;
2588 ImageUser *iuser = node->storage;
2589 callback(ima, iuser, customdata);
2599 static void image_tag_frame_recalc(Image *ima, ImageUser *iuser, void *customdata)
2601 Image *changed_image = customdata;
2603 if (ima == changed_image && BKE_image_is_animated(ima)) {
2604 iuser->flag |= IMA_NEED_FRAME_RECALC;
2608 static void image_init_imageuser(Image *ima, ImageUser *iuser)
2610 RenderResult *rr = ima->rr;
2612 iuser->multi_index = 0;
2613 iuser->layer = iuser->pass = iuser->view = 0;
2616 BKE_image_multilayer_index(rr, iuser);
2619 void BKE_image_init_imageuser(Image *ima, ImageUser *iuser)
2621 image_init_imageuser(ima, iuser);
2624 void BKE_image_signal(Image *ima, ImageUser *iuser, int signal)
2629 BLI_spin_lock(&image_spin);
2632 case IMA_SIGNAL_FREE:
2633 BKE_image_free_buffers(ima);
2638 image_update_views_format(ima, iuser);
2642 case IMA_SIGNAL_SRC_CHANGE:
2643 if (ima->type == IMA_TYPE_UV_TEST)
2644 if (ima->source != IMA_SRC_GENERATED)
2645 ima->type = IMA_TYPE_IMAGE;
2647 if (ima->source == IMA_SRC_GENERATED) {
2648 if (ima->gen_x == 0 || ima->gen_y == 0) {
2649 ImBuf *ibuf = image_get_cached_ibuf_for_index_frame(ima, IMA_NO_INDEX, 0);
2651 ima->gen_x = ibuf->x;
2652 ima->gen_y = ibuf->y;
2653 IMB_freeImBuf(ibuf);
2657 /* Changing source type to generated will likely change file format
2658 * used by generated image buffer. Saving different file format to
2659 * the old name might confuse other applications.
2661 * Here we ensure original image path wouldn't be used when saving
2664 ima->name[0] = '\0';
2668 /* force reload on first use, but not for multilayer, that makes nodes and buttons in ui drawing fail */
2669 if (ima->type != IMA_TYPE_MULTILAYER)
2670 BKE_image_free_buffers(ima);
2672 /* image buffers for non-sequence multilayer will share buffers with RenderResult,
2673 * however sequence multilayer will own buffers. Such logic makes switching from
2674 * single multilayer file to sequence completely unstable
2675 * since changes in nodes seems this workaround isn't needed anymore, all sockets
2676 * are nicely detecting anyway, but freeing buffers always here makes multilayer
2677 * sequences behave stable
2679 BKE_image_free_buffers(ima);
2686 BKE_image_walk_all_users(G.main, ima, image_tag_frame_recalc);
2690 case IMA_SIGNAL_RELOAD:
2691 /* try to repack file */
2692 if (BKE_image_has_packedfile(ima)) {
2693 const int totfiles = image_num_files(ima);
2695 if (totfiles != BLI_listbase_count_ex(&ima->packedfiles, totfiles + 1)) {
2696 /* in case there are new available files to be loaded */
2697 image_free_packedfiles(ima);
2698 BKE_image_packfiles(NULL, ima, ID_BLEND_PATH(G.main, &ima->id));
2701 ImagePackedFile *imapf;
2702 for (imapf = ima->packedfiles.first; imapf; imapf = imapf->next) {
2704 pf = newPackedFile(NULL, imapf->filepath, ID_BLEND_PATH(G.main, &ima->id));
2706 freePackedFile(imapf->packedfile);
2707 imapf->packedfile = pf;
2710 printf("ERROR: Image \"%s\" not available. Keeping packed image\n", imapf->filepath);
2715 if (BKE_image_has_packedfile(ima))
2716 BKE_image_free_buffers(ima);
2719 BKE_image_free_buffers(ima);
2724 image_update_views_format(ima, iuser);
2729 case IMA_SIGNAL_USER_NEW_IMAGE:
2732 if (ima->source == IMA_SRC_FILE || ima->source == IMA_SRC_SEQUENCE) {
2733 if (ima->type == IMA_TYPE_MULTILAYER) {
2734 image_init_imageuser(ima, iuser);
2739 case IMA_SIGNAL_COLORMANAGE:
2740 BKE_image_free_buffers(ima);
2750 BLI_spin_unlock(&image_spin);
2752 /* don't use notifiers because they are not 100% sure to succeeded
2753 * this also makes sure all scenes are accounted for. */
2756 for (scene = G.main->scene.first; scene; scene = scene->id.next) {
2757 if (scene->nodetree) {
2758 nodeUpdateID(scene->nodetree, &ima->id);
2764 #define PASSTYPE_UNSET -1
2765 /* return renderpass for a given pass index and active view */
2766 /* fallback to available if there are missing passes for active view */
2767 static RenderPass *image_render_pass_get(RenderLayer *rl, const int pass, const int view, int *r_passindex)
2769 RenderPass *rpass_ret = NULL;
2773 int rp_passtype = PASSTYPE_UNSET;
2775 for (rpass = rl->passes.first; rpass; rpass = rpass->next, rp_index++) {
2776 if (rp_index == pass) {
2779 /* no multiview or left eye */
2783 rp_passtype = rpass->passtype;
2787 else if ((rp_passtype != PASSTYPE_UNSET) &&
2788 (rpass->passtype == rp_passtype) &&
2789 (rpass->view_id == view))
2796 /* fallback to the first pass in the layer */
2797 if (rpass_ret == NULL) {
2799 rpass_ret = rl->passes.first;
2803 *r_passindex = (rpass == rpass_ret ? rp_index : pass);
2808 #undef PASSTYPE_UNSET
2810 /* if layer or pass changes, we need an index for the imbufs list */
2811 /* note it is called for rendered results, but it doesnt use the index! */
2812 /* and because rendered results use fake layer/passes, don't correct for wrong indices here */
2813 RenderPass *BKE_image_multilayer_index(RenderResult *rr, ImageUser *iuser)
2816 RenderPass *rpass = NULL;
2822 short index = 0, rv_index, rl_index = 0;
2823 bool is_stereo = (iuser->flag & IMA_SHOW_STEREO) && RE_RenderResult_is_stereo(rr);
2825 rv_index = is_stereo ? iuser->multiview_eye : iuser->view;
2826 if (RE_HasFakeLayer(rr)) rl_index += 1;
2828 for (rl = rr->layers.first; rl; rl = rl->next, rl_index++) {
2829 if (iuser->layer == rl_index) {
2831 rpass = image_render_pass_get(rl, iuser->pass, rv_index, &rp_index);
2832 iuser->multi_index = index + rp_index;
2836 index += BLI_listbase_count(&rl->passes);
2844 void BKE_image_multiview_index(Image *ima, ImageUser *iuser)
2847 bool is_stereo = BKE_image_is_stereo(ima) && (iuser->flag & IMA_SHOW_STEREO);
2849 iuser->multi_index = iuser->multiview_eye;
2852 if ((iuser->view < 0) || (iuser->view >= BLI_listbase_count_ex(&ima->views, iuser->view + 1))) {
2853 iuser->multi_index = iuser->view = 0;
2856 iuser->multi_index = iuser->view;
2862 /* if layer or pass changes, we need an index for the imbufs list */
2863 /* note it is called for rendered results, but it doesnt use the index! */
2864 /* and because rendered results use fake layer/passes, don't correct for wrong indices here */
2865 bool BKE_image_is_multilayer(Image *ima)
2867 if (ELEM(ima->source, IMA_SRC_FILE, IMA_SRC_SEQUENCE)) {
2868 if (ima->type == IMA_TYPE_MULTILAYER) {
2872 else if (ima->source == IMA_SRC_VIEWER) {
2873 if (ima->type == IMA_TYPE_R_RESULT) {
2880 bool BKE_image_is_multiview(Image *ima)
2882 return (BLI_listbase_count_ex(&ima->views, 2) > 1);
2885 bool BKE_image_is_stereo(Image *ima)
2887 return BKE_image_is_multiview(ima) &&
2888 (BLI_findstring(&ima->views, STEREO_LEFT_NAME, offsetof(ImageView, name)) &&
2889 BLI_findstring(&ima->views, STEREO_RIGHT_NAME, offsetof(ImageView, name)));
2892 static void image_init_multilayer_multiview(Image *ima, RenderResult *rr)
2894 /* update image views from render views, but only if they actually changed,
2895 * to avoid invalid memory access during render. ideally these should always
2896 * be acquired with a mutex along with the render result, but there are still
2897 * some places with just an image pointer that need to access views */
2898 if (rr && BLI_listbase_count(&ima->views) == BLI_listbase_count(&rr->views)) {
2899 ImageView *iv = ima->views.first;
2900 RenderView *rv = rr->views.first;
2901 bool modified = false;
2902 for (; rv; rv = rv->next, iv = iv->next) {
2903 modified |= !STREQ(rv->name, iv->name);
2909 BKE_image_free_views(ima);
2912 for (RenderView *rv = rr->views.first; rv; rv = rv->next) {
2913 ImageView *iv = MEM_callocN(sizeof(ImageView), "Viewer Image View");
2914 BLI_strncpy(iv->name, rv->name, sizeof(iv->name));
2915 BLI_addtail(&ima->views, iv);
2921 RenderResult *BKE_image_acquire_renderresult(Scene *scene, Image *ima)
2923 RenderResult *rr = NULL;
2927 else if (ima->type == IMA_TYPE_R_RESULT) {
2928 if (ima->render_slot == ima->last_render_slot)
2929 rr = RE_AcquireResultRead(RE_GetRender(scene->id.name));
2931 rr = ima->renders[ima->render_slot];
2933 /* set proper views */
2934 image_init_multilayer_multiview(ima, rr);
2940 void BKE_image_release_renderresult(Scene *scene, Image *ima)
2945 else if (ima->type == IMA_TYPE_R_RESULT) {
2946 if (ima->render_slot == ima->last_render_slot)
2947 RE_ReleaseResult(RE_GetRender(scene->id.name));
2951 bool BKE_image_is_openexr(struct Image *ima)
2954 if (ELEM(ima->source, IMA_SRC_FILE, IMA_SRC_SEQUENCE)) {
2955 return BLI_testextensie(ima->name, ".exr");
2963 void BKE_image_backup_render(Scene *scene, Image *ima, bool free_current_slot)
2965 /* called right before rendering, ima->renders contains render
2966 * result pointers for everything but the current render */
2967 Render *re = RE_GetRender(scene->id.name);
2968 int slot = ima->render_slot, last = ima->last_render_slot;
2971 ima->renders[last] = NULL;
2972 RE_SwapResult(re, &ima->renders[last]);
2974 if (ima->renders[slot]) {
2975 if (free_current_slot) {
2976 RE_FreeRenderResult(ima->renders[slot]);
2977 ima->renders[slot] = NULL;
2980 RE_SwapResult(re, &ima->renders[slot]);
2985 ima->last_render_slot = slot;
2988 /**************************** multiview save openexr *********************************/
2990 static const char *image_get_view_cb(void *base, const int view_id)
2993 ImageView *iv = BLI_findlink(&ima->views, view_id);
2994 return iv ? iv->name : "";
2996 #endif /* WITH_OPENEXR */
2999 static ImBuf *image_get_buffer_cb(void *base, const int view_id)
3002 ImageUser iuser = {0};
3004 iuser.view = view_id;
3007 BKE_image_multiview_index(ima, &iuser);
3009 return image_acquire_ibuf(ima, &iuser, NULL);
3011 #endif /* WITH_OPENEXR */
3013 bool BKE_image_save_openexr_multiview(Image *ima, ImBuf *ibuf, const char *filepath, const int flags)
3016 char name[FILE_MAX];
3019 BLI_strncpy(name, filepath, sizeof(name));
3020 BLI_path_abs(name, G.main->name);
3022 ibuf->userdata = ima;
3023 ok = IMB_exr_multiview_save(ibuf, name, flags, BLI_listbase_count(&ima->views), image_get_view_cb, image_get_buffer_cb);
3024 ibuf->userdata = NULL;
3028 UNUSED_VARS(ima, ibuf, filepath, flags);
3033 /**************************** multiview load openexr *********************************/
3035 static void image_add_view(Image *ima, const char *viewname, const char *filepath)
3039 iv = MEM_mallocN(sizeof(ImageView), "Viewer Image View");
3040 BLI_strncpy(iv->name, viewname, sizeof(iv->name));
3041 BLI_strncpy(iv->filepath, filepath, sizeof(iv->filepath));
3043 /* For stereo drawing we need to ensure:
3044 * STEREO_LEFT_NAME == STEREO_LEFT_ID and
3045 * STEREO_RIGHT_NAME == STEREO_RIGHT_ID */
3047 if (STREQ(viewname, STEREO_LEFT_NAME)) {
3048 BLI_addhead(&ima->views, iv);
3050 else if (STREQ(viewname, STEREO_RIGHT_NAME)) {
3051 ImageView *left_iv = BLI_findstring(&ima->views, STEREO_LEFT_NAME, offsetof(ImageView, name));
3053 if (left_iv == NULL) {
3054 BLI_addhead(&ima->views, iv);
3057 BLI_insertlinkafter(&ima->views, left_iv, iv);
3061 BLI_addtail(&ima->views, iv);
3066 static void image_add_view_cb(void *base, const char *str)
3069 image_add_view(ima, str, ima->name);
3072 static void image_add_buffer_cb(void *base, const char *str, ImBuf *ibuf, const int frame)
3076 bool predivide = (ima->alpha_mode == IMA_ALPHA_PREMUL);
3077 const char *colorspace = ima->colorspace_settings.name;
3078 const char *to_colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_SCENE_LINEAR);
3083 id = BLI_findstringindex(&ima->views, str, offsetof(ImageView, name));
3088 if (ibuf->channels >= 3)
3089 IMB_colormanagement_transform(ibuf->rect_float, ibuf->x, ibuf->y, ibuf->channels,
3090 colorspace, to_colorspace, predivide);
3092 image_assign_ibuf(ima, ibuf, id, frame);
3093 IMB_freeImBuf(ibuf);
3095 #endif /* WITH_OPENEXR */
3097 /* after imbuf load, openexr type can return with a exrhandle open */
3098 /* in that case we have to build a render-result */
3100 static void image_create_multiview(Image *ima, ImBuf *ibuf, const int frame)
3102 BKE_image_free_views(ima);
3104 IMB_exr_multiview_convert(ibuf->userdata, ima, image_add_view_cb, image_add_buffer_cb, frame);
3106 IMB_exr_close(ibuf->userdata);
3108 #endif /* WITH_OPENEXR */
3110 /* after imbuf load, openexr type can return with a exrhandle open */
3111 /* in that case we have to build a render-result */
3113 static void image_create_multilayer(Image *ima, ImBuf *ibuf, int framenr)
3115 const char *colorspace = ima->colorspace_settings.name;
3116 bool predivide = (ima->alpha_mode == IMA_ALPHA_PREMUL);
3118 /* only load rr once for multiview */
3120 ima->rr = RE_MultilayerConvert(ibuf->userdata, colorspace, predivide, ibuf->x, ibuf->y);
3122 IMB_exr_close(ibuf->userdata);
3124 ibuf->userdata = NULL;
3126 ima->rr->framenr = framenr;
3128 /* set proper views */
3129 image_init_multilayer_multiview(ima, ima->rr);
3131 #endif /* WITH_OPENEXR */
3133 /* common stuff to do with images after loading */
3134 static void image_initialize_after_load(Image *ima, ImBuf *ibuf)
3136 /* preview is NULL when it has never been used as an icon before */
3137 if (G.background == 0 && ima->preview == NULL)
3138 BKE_icon_changed(BKE_icon_id_ensure(&ima->id));
3141 if (ima->flag & IMA_FIELDS) {
3142 if (ima->flag & IMA_STD_FIELD) de_interlace_st(ibuf);
3143 else de_interlace_ng(ibuf);
3146 BKE_image_tag_time(ima);
3148 ima->ok = IMA_OK_LOADED;
3152 static int imbuf_alpha_flags_for_image(Image *ima)
3156 if (ima->flag & IMA_IGNORE_ALPHA)
3157 flag |= IB_ignore_alpha;
3158 else if (ima->alpha_mode == IMA_ALPHA_PREMUL)
3159 flag |= IB_alphamode_premul;
3164 /* the number of files will vary according to the stereo format */
3165 static int image_num_files(Image *ima)
3167 const bool is_multiview = BKE_image_is_multiview(ima);
3169 if (!is_multiview) {
3172 else if (ima->views_format == R_IMF_VIEWS_STEREO_3D) {
3175 /* R_IMF_VIEWS_INDIVIDUAL */
3177 return BLI_listbase_count(&ima->views);
3181 static ImBuf *load_sequence_single(Image *ima, ImageUser *iuser, int frame, const int view_id, bool *r_assign)
3184 char name[FILE_MAX];
3188 /* XXX temp stuff? */
3189 if (ima->lastframe != frame)
3190 ima->tpageflag |= IMA_TPAGE_REFRESH;
3192 ima->lastframe = frame;
3197 iuser_t.view = view_id;
3198 BKE_image_user_file_path(&iuser_t, ima, name);
3200 flag = IB_rect | IB_multilayer;
3201 flag |= imbuf_alpha_flags_for_image(ima);
3204 ibuf = IMB_loadiffname(name, flag, ima->colorspace_settings.name);
3208 printf(AT " loaded %s\n", name);
3211 printf(AT " missed %s\n", name);
3217 /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_acquire_ibuf */
3218 if (ibuf->ftype == IMB_FTYPE_OPENEXR && ibuf->userdata) {
3219 /* handle singlelayer multiview case assign ibuf based on available views */
3220 if (IMB_exr_has_singlelayer_multiview(ibuf->userdata)) {
3221 image_create_multiview(ima, ibuf, frame);
3222 IMB_freeImBuf(ibuf);
3225 else if (IMB_exr_has_multilayer(ibuf->userdata)) {
3226 /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_acquire_ibuf */
3227 image_create_multilayer(ima, ibuf, frame);
3228 ima->type = IMA_TYPE_MULTILAYER;
3229 IMB_freeImBuf(ibuf);
3234 image_initialize_after_load(ima, ibuf);
3238 image_initialize_after_load(ima, ibuf);
3246 static ImBuf *image_load_sequence_file(Image *ima, ImageUser *iuser, int frame)
3248 struct ImBuf *ibuf = NULL;
3249 const bool is_multiview = BKE_image_is_multiview(ima);
3250 const int totfiles = image_num_files(ima);
3251 bool assign = false;
3253 if (!is_multiview) {
3254 ibuf = load_sequence_single(ima, iuser, frame, 0, &assign);
3256 image_assign_ibuf(ima, ibuf, 0, frame);
3260 const int totviews = BLI_listbase_count(&ima->views);
3262 struct ImBuf **ibuf_arr;
3264 ibuf_arr = MEM_mallocN(sizeof(ImBuf *) * totviews, "Image Views Imbufs");
3266 for (i = 0; i < totfiles; i++)
3267 ibuf_arr[i] = load_sequence_single(ima, iuser, frame, i, &assign);
3269 if (BKE_image_is_stereo(ima) && ima->views_format == R_IMF_VIEWS_STEREO_3D)
3270 IMB_ImBufFromStereo3d(ima->stereo3d_format, ibuf_arr[0], &ibuf_arr[0], &ibuf_arr[1]);
3272 /* return the original requested ImBuf */
3273 ibuf = ibuf_arr[(iuser ? iuser->multi_index : 0)];
3276 for (i = 0; i < totviews; i++) {
3277 image_assign_ibuf(ima, ibuf_arr[i], i, frame);
3281 /* "remove" the others (decrease their refcount) */
3282 for (i = 0; i < totviews; i++) {
3283 if (ibuf_arr[i] != ibuf) {
3284 IMB_freeImBuf(ibuf_arr[i]);
3289 MEM_freeN(ibuf_arr);
3295 static ImBuf *image_load_sequence_multilayer(Image *ima, ImageUser *iuser, int frame)
3297 struct ImBuf *ibuf = NULL;
3299 /* either we load from RenderResult, or we have to load a new one */
3301 /* check for new RenderResult */
3302 if (ima->rr == NULL || frame != ima->rr->framenr) {
3304 /* Cached image buffers shares pointers with render result,