Blender  V2.93
image.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
24 #include <fcntl.h>
25 #include <math.h>
26 #include <stdio.h>
27 #include <string.h>
28 #ifndef WIN32
29 # include <unistd.h>
30 #else
31 # include <io.h>
32 #endif
33 
34 #include <time.h>
35 
36 #include "CLG_log.h"
37 
38 #include "MEM_guardedalloc.h"
39 
40 #include "IMB_colormanagement.h"
41 #include "IMB_imbuf.h"
42 #include "IMB_imbuf_types.h"
43 #include "IMB_metadata.h"
44 #include "IMB_moviecache.h"
45 
46 #ifdef WITH_OPENEXR
48 #endif
49 
50 /* Allow using deprecated functionality for .blend file I/O. */
51 #define DNA_DEPRECATED_ALLOW
52 
53 #include "DNA_brush_types.h"
54 #include "DNA_camera_types.h"
55 #include "DNA_defaults.h"
56 #include "DNA_light_types.h"
57 #include "DNA_material_types.h"
58 #include "DNA_mesh_types.h"
59 #include "DNA_meshdata_types.h"
60 #include "DNA_object_types.h"
61 #include "DNA_packedFile_types.h"
62 #include "DNA_scene_types.h"
63 #include "DNA_sequence_types.h"
64 #include "DNA_simulation_types.h"
65 #include "DNA_world_types.h"
66 
67 #include "BLI_blenlib.h"
68 #include "BLI_math_vector.h"
69 #include "BLI_mempool.h"
70 #include "BLI_system.h"
71 #include "BLI_threads.h"
72 #include "BLI_timecode.h" /* For stamp time-code format. */
73 #include "BLI_utildefines.h"
74 
75 #include "BLT_translation.h"
76 
77 #include "BKE_colortools.h"
78 #include "BKE_global.h"
79 #include "BKE_icons.h"
80 #include "BKE_idtype.h"
81 #include "BKE_image.h"
82 #include "BKE_lib_id.h"
83 #include "BKE_main.h"
84 #include "BKE_node.h"
85 #include "BKE_packedFile.h"
86 #include "BKE_report.h"
87 #include "BKE_scene.h"
88 #include "BKE_workspace.h"
89 
90 #include "BLF_api.h"
91 
92 #include "PIL_time.h"
93 
94 #include "RE_pipeline.h"
95 
96 #include "SEQ_utils.h" /* SEQ_get_topmost_sequence() */
97 
98 #include "GPU_texture.h"
99 
100 #include "BLI_sys_types.h" /* for intptr_t support */
101 
102 #include "DEG_depsgraph.h"
103 #include "DEG_depsgraph_query.h"
104 
105 #include "BLO_read_write.h"
106 
107 /* for image user iteration */
108 #include "DNA_node_types.h"
109 #include "DNA_screen_types.h"
110 #include "DNA_space_types.h"
111 #include "DNA_view3d_types.h"
112 
113 static CLG_LogRef LOG = {"bke.image"};
115 
116 static void image_init(Image *ima, short source, short type);
117 static void image_free_packedfiles(Image *ima);
118 static void copy_image_packedfiles(ListBase *lb_dst, const ListBase *lb_src);
119 
120 static void image_init_data(ID *id)
121 {
122  Image *image = (Image *)id;
123 
124  if (image != NULL) {
126  }
127 }
128 
129 static void image_copy_data(Main *UNUSED(bmain), ID *id_dst, const ID *id_src, const int flag)
130 {
131  Image *image_dst = (Image *)id_dst;
132  const Image *image_src = (const Image *)id_src;
133 
135  &image_src->colorspace_settings);
136 
137  copy_image_packedfiles(&image_dst->packedfiles, &image_src->packedfiles);
138 
139  image_dst->stereo3d_format = MEM_dupallocN(image_src->stereo3d_format);
140  BLI_duplicatelist(&image_dst->views, &image_src->views);
141 
142  /* Cleanup stuff that cannot be copied. */
143  image_dst->cache = NULL;
144  image_dst->rr = NULL;
145 
146  BLI_duplicatelist(&image_dst->renderslots, &image_src->renderslots);
147  LISTBASE_FOREACH (RenderSlot *, slot, &image_dst->renderslots) {
148  slot->render = NULL;
149  }
150 
151  BLI_listbase_clear(&image_dst->anims);
152 
153  BLI_duplicatelist(&image_dst->tiles, &image_src->tiles);
154 
155  for (int eye = 0; eye < 2; eye++) {
156  for (int i = 0; i < TEXTARGET_COUNT; i++) {
157  image_dst->gputexture[i][eye] = NULL;
158  }
159  }
160 
161  if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {
162  BKE_previewimg_id_copy(&image_dst->id, &image_src->id);
163  }
164  else {
165  image_dst->preview = NULL;
166  }
167 }
168 
169 static void image_free_data(ID *id)
170 {
171  Image *image = (Image *)id;
172 
173  /* Also frees animdata. */
174  BKE_image_free_buffers(image);
175 
176  image_free_packedfiles(image);
177 
178  LISTBASE_FOREACH (RenderSlot *, slot, &image->renderslots) {
179  if (slot->render) {
180  RE_FreeRenderResult(slot->render);
181  slot->render = NULL;
182  }
183  }
184  BLI_freelistN(&image->renderslots);
185 
186  BKE_image_free_views(image);
188 
189  BKE_icon_id_delete(&image->id);
190  BKE_previewimg_free(&image->preview);
191 
192  BLI_freelistN(&image->tiles);
194 }
195 
196 static void image_foreach_cache(ID *id,
197  IDTypeForeachCacheFunctionCallback function_callback,
198  void *user_data)
199 {
200  Image *image = (Image *)id;
201  IDCacheKey key = {
202  .id_session_uuid = id->session_uuid,
203  .offset_in_ID = offsetof(Image, cache),
204  .cache_v = image->cache,
205  };
206  function_callback(id, &key, (void **)&image->cache, 0, user_data);
207 
208  for (int eye = 0; eye < 2; eye++) {
209  for (int a = 0; a < TEXTARGET_COUNT; a++) {
210  key.offset_in_ID = offsetof(Image, gputexture[a][eye]);
211  key.cache_v = image->gputexture[a][eye];
212  function_callback(id, &key, (void **)&image->gputexture[a][eye], 0, user_data);
213  }
214  }
215 
216  key.offset_in_ID = offsetof(Image, rr);
217  key.cache_v = image->rr;
218  function_callback(id, &key, (void **)&image->rr, 0, user_data);
219 
220  LISTBASE_FOREACH (RenderSlot *, slot, &image->renderslots) {
221  key.offset_in_ID = (size_t)BLI_ghashutil_strhash_p(slot->name);
222  key.cache_v = slot->render;
223  function_callback(id, &key, (void **)&slot->render, 0, user_data);
224  }
225 }
226 
227 static void image_blend_write(BlendWriter *writer, ID *id, const void *id_address)
228 {
229  Image *ima = (Image *)id;
230  const bool is_undo = BLO_write_is_undo(writer);
231  if (ima->id.us > 0 || is_undo) {
232  ImagePackedFile *imapf;
233 
234  BLI_assert(ima->packedfile == NULL);
235  /* Do not store packed files in case this is a library override ID. */
236  if (ID_IS_OVERRIDE_LIBRARY(ima) && !is_undo) {
238  }
239  else {
240  /* Some trickery to keep forward compatibility of packed images. */
241  if (ima->packedfiles.first != NULL) {
242  imapf = ima->packedfiles.first;
243  ima->packedfile = imapf->packedfile;
244  }
245  }
246 
247  /* write LibData */
248  BLO_write_id_struct(writer, Image, id_address, &ima->id);
249  BKE_id_blend_write(writer, &ima->id);
250 
251  for (imapf = ima->packedfiles.first; imapf; imapf = imapf->next) {
252  BLO_write_struct(writer, ImagePackedFile, imapf);
253  BKE_packedfile_blend_write(writer, imapf->packedfile);
254  }
255 
256  BKE_previewimg_blend_write(writer, ima->preview);
257 
258  LISTBASE_FOREACH (ImageView *, iv, &ima->views) {
259  BLO_write_struct(writer, ImageView, iv);
260  }
262 
263  BLO_write_struct_list(writer, ImageTile, &ima->tiles);
264 
265  ima->packedfile = NULL;
266 
268  }
269 }
270 
271 static void image_blend_read_data(BlendDataReader *reader, ID *id)
272 {
273  Image *ima = (Image *)id;
274  BLO_read_list(reader, &ima->tiles);
275 
276  BLO_read_list(reader, &(ima->renderslots));
277  if (!BLO_read_data_is_undo(reader)) {
278  /* We reset this last render slot index only when actually reading a file, not for undo. */
279  ima->last_render_slot = ima->render_slot;
280  }
281 
282  BLO_read_list(reader, &(ima->views));
283  BLO_read_list(reader, &(ima->packedfiles));
284 
285  if (ima->packedfiles.first) {
286  LISTBASE_FOREACH (ImagePackedFile *, imapf, &ima->packedfiles) {
287  BKE_packedfile_blend_read(reader, &imapf->packedfile);
288  }
289  ima->packedfile = NULL;
290  }
291  else {
292  BKE_packedfile_blend_read(reader, &ima->packedfile);
293  }
294 
295  BLI_listbase_clear(&ima->anims);
296  BLO_read_data_address(reader, &ima->preview);
297  BKE_previewimg_blend_read(reader, ima->preview);
298  BLO_read_data_address(reader, &ima->stereo3d_format);
299  LISTBASE_FOREACH (ImageTile *, tile, &ima->tiles) {
300  tile->ok = IMA_OK;
301  }
302  ima->gpuflag = 0;
304 }
305 
306 static void image_blend_read_lib(BlendLibReader *UNUSED(reader), ID *id)
307 {
308  Image *ima = (Image *)id;
309  /* Images have some kind of 'main' cache, when NULL we should also clear all others. */
310  /* Needs to be done *after* cache pointers are restored (call to
311  * `foreach_cache`/`blo_cache_storage_entry_restore_in_new`), easier for now to do it in
312  * lib_link... */
313  if (ima->cache == NULL) {
315  }
316 }
317 
319  .id_code = ID_IM,
320  .id_filter = FILTER_ID_IM,
321  .main_listbase_index = INDEX_ID_IM,
322  .struct_size = sizeof(Image),
323  .name = "Image",
324  .name_plural = "images",
325  .translation_context = BLT_I18NCONTEXT_ID_IMAGE,
326  .flags = IDTYPE_FLAGS_NO_ANIMDATA,
327 
329  .copy_data = image_copy_data,
330  .free_data = image_free_data,
331  .make_local = NULL,
332  .foreach_id = NULL,
333  .foreach_cache = image_foreach_cache,
334  .owner_get = NULL,
335 
336  .blend_write = image_blend_write,
337  .blend_read_data = image_blend_read_data,
338  .blend_read_lib = image_blend_read_lib,
339  .blend_read_expand = NULL,
340 
341  .blend_read_undo_preserve = NULL,
342 
343  .lib_override_apply_post = NULL,
344 };
345 
346 /* prototypes */
347 static int image_num_files(struct Image *ima);
348 static ImBuf *image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock);
349 static void image_update_views_format(Image *ima, ImageUser *iuser);
350 static void image_add_view(Image *ima, const char *viewname, const char *filepath);
351 
352 /* max int, to indicate we don't store sequences in ibuf */
353 #define IMA_NO_INDEX 0x7FEFEFEF
354 
355 /* quick lookup: supports 1 million entries, thousand passes */
356 #define IMA_MAKE_INDEX(entry, index) (((entry) << 10) + (index))
357 #define IMA_INDEX_ENTRY(index) ((index) >> 10)
358 #if 0
359 # define IMA_INDEX_PASS(index) (index & ~1023)
360 #endif
361 
362 /* ******** IMAGE CACHE ************* */
363 
364 typedef struct ImageCacheKey {
365  int index;
367 
368 static unsigned int imagecache_hashhash(const void *key_v)
369 {
370  const ImageCacheKey *key = key_v;
371  return key->index;
372 }
373 
374 static bool imagecache_hashcmp(const void *a_v, const void *b_v)
375 {
376  const ImageCacheKey *a = a_v;
377  const ImageCacheKey *b = b_v;
378 
379  return (a->index != b->index);
380 }
381 
382 static void imagecache_keydata(void *userkey, int *framenr, int *proxy, int *render_flags)
383 {
384  ImageCacheKey *key = userkey;
385 
386  *framenr = IMA_INDEX_ENTRY(key->index);
387  *proxy = IMB_PROXY_NONE;
388  *render_flags = 0;
389 }
390 
391 static void imagecache_put(Image *image, int index, ImBuf *ibuf)
392 {
393  ImageCacheKey key;
394 
395  if (image->cache == NULL) {
396  // char cache_name[64];
397  // SNPRINTF(cache_name, "Image Datablock %s", image->id.name);
398 
399  image->cache = IMB_moviecache_create(
400  "Image Datablock Cache", sizeof(ImageCacheKey), imagecache_hashhash, imagecache_hashcmp);
402  }
403 
404  key.index = index;
405 
406  IMB_moviecache_put(image->cache, &key, ibuf);
407 }
408 
409 static void imagecache_remove(Image *image, int index)
410 {
411  if (image->cache == NULL) {
412  return;
413  }
414 
415  ImageCacheKey key;
416  key.index = index;
417  IMB_moviecache_remove(image->cache, &key);
418 }
419 
420 static struct ImBuf *imagecache_get(Image *image, int index)
421 {
422  if (image->cache) {
423  ImageCacheKey key;
424  key.index = index;
425  return IMB_moviecache_get(image->cache, &key);
426  }
427 
428  return NULL;
429 }
430 
431 void BKE_images_init(void)
432 {
434 }
435 
436 void BKE_images_exit(void)
437 {
439 }
440 
441 /* ***************** ALLOC & FREE, DATA MANAGING *************** */
442 
443 static void image_free_cached_frames(Image *image)
444 {
445  if (image->cache) {
446  IMB_moviecache_free(image->cache);
447  image->cache = NULL;
448  }
449 }
450 
451 static void image_free_packedfiles(Image *ima)
452 {
453  while (ima->packedfiles.last) {
454  ImagePackedFile *imapf = ima->packedfiles.last;
455  if (imapf->packedfile) {
457  }
458  BLI_remlink(&ima->packedfiles, imapf);
459  MEM_freeN(imapf);
460  }
461 }
462 
464 {
466 }
467 
469 {
470  BLI_freelistN(&image->views);
471 }
472 
473 static void image_free_anims(Image *ima)
474 {
475  while (ima->anims.last) {
476  ImageAnim *ia = ima->anims.last;
477  if (ia->anim) {
478  IMB_free_anim(ia->anim);
479  ia->anim = NULL;
480  }
481  BLI_remlink(&ima->anims, ia);
482  MEM_freeN(ia);
483  }
484 }
485 
490 void BKE_image_free_buffers_ex(Image *ima, bool do_lock)
491 {
492  if (do_lock) {
494  }
496 
497  image_free_anims(ima);
498 
499  if (ima->rr) {
500  RE_FreeRenderResult(ima->rr);
501  ima->rr = NULL;
502  }
503 
505 
506  LISTBASE_FOREACH (ImageTile *, tile, &ima->tiles) {
507  tile->ok = IMA_OK;
508  }
509 
510  if (do_lock) {
512  }
513 }
514 
516 {
517  BKE_image_free_buffers_ex(ima, false);
518 }
519 
522 {
523  image_free_data(&ima->id);
524 }
525 
526 /* only image block itself */
527 static void image_init(Image *ima, short source, short type)
528 {
530 
532 
533  ima->source = source;
534  ima->type = type;
535 
536  if (source == IMA_SRC_VIEWER) {
537  ima->flag |= IMA_VIEW_AS_RENDER;
538  }
539 
540  ImageTile *tile = MEM_callocN(sizeof(ImageTile), "Image Tiles");
541  tile->ok = IMA_OK;
542  tile->tile_number = 1001;
543  BLI_addtail(&ima->tiles, tile);
544 
545  if (type == IMA_TYPE_R_RESULT) {
546  for (int i = 0; i < 8; i++) {
548  }
549  }
550 
552  ima->stereo3d_format = MEM_callocN(sizeof(Stereo3dFormat), "Image Stereo Format");
553 }
554 
555 static Image *image_alloc(Main *bmain, const char *name, short source, short type)
556 {
557  Image *ima;
558 
559  ima = BKE_libblock_alloc(bmain, ID_IM, name, 0);
560  if (ima) {
561  image_init(ima, source, type);
562  }
563 
564  return ima;
565 }
566 
567 /* Get the ibuf from an image cache by its index and entry.
568  * Local use here only.
569  *
570  * Returns referenced image buffer if it exists, callee is to
571  * call IMB_freeImBuf to de-reference the image buffer after
572  * it's done handling it.
573  */
575 {
576  if (index != IMA_NO_INDEX) {
577  index = IMA_MAKE_INDEX(entry, index);
578  }
579 
580  return imagecache_get(ima, index);
581 }
582 
583 /* no ima->ibuf anymore, but listbase */
584 static void image_assign_ibuf(Image *ima, ImBuf *ibuf, int index, int entry)
585 {
586  if (ibuf) {
587  if (index != IMA_NO_INDEX) {
588  index = IMA_MAKE_INDEX(entry, index);
589  }
590 
591  imagecache_put(ima, index, ibuf);
592  }
593 }
594 
595 static void image_remove_ibuf(Image *ima, int index, int entry)
596 {
597  if (index != IMA_NO_INDEX) {
598  index = IMA_MAKE_INDEX(entry, index);
599  }
600  imagecache_remove(ima, index);
601 }
602 
603 static void copy_image_packedfiles(ListBase *lb_dst, const ListBase *lb_src)
604 {
605  const ImagePackedFile *imapf_src;
606 
607  BLI_listbase_clear(lb_dst);
608  for (imapf_src = lb_src->first; imapf_src; imapf_src = imapf_src->next) {
609  ImagePackedFile *imapf_dst = MEM_mallocN(sizeof(ImagePackedFile), "Image Packed Files (copy)");
610  STRNCPY(imapf_dst->filepath, imapf_src->filepath);
611 
612  if (imapf_src->packedfile) {
613  imapf_dst->packedfile = BKE_packedfile_duplicate(imapf_src->packedfile);
614  }
615 
616  BLI_addtail(lb_dst, imapf_dst);
617  }
618 }
619 
620 void BKE_image_merge(Main *bmain, Image *dest, Image *source)
621 {
622  /* sanity check */
623  if (dest && source && dest != source) {
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);
631  imagecache_put(dest, key->index, ibuf);
633  }
635  }
637 
638  BKE_id_free(bmain, source);
639  }
640 }
641 
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)
644 {
645  ImBuf *ibuf;
646  void *lock;
647 
648  ibuf = BKE_image_acquire_ibuf(image, NULL, &lock);
649 
650  if (ibuf) {
651  IMB_scaleImBuf(ibuf, width, height);
652  BKE_image_mark_dirty(image, ibuf);
653  }
654 
655  BKE_image_release_ibuf(image, ibuf, lock);
656 
657  return (ibuf != NULL);
658 }
659 
661 {
662  for (int eye = 0; eye < 2; eye++) {
663  for (int i = 0; i < TEXTARGET_COUNT; i++) {
664  if (ima->gputexture[i][eye] != NULL) {
665  return true;
666  }
667  }
668  }
669  return false;
670 }
671 
672 ImageTile *BKE_image_get_tile(Image *ima, int tile_number)
673 {
674  if (ima == NULL) {
675  return NULL;
676  }
677 
678  /* Verify valid tile range. */
679  if ((tile_number != 0) && (tile_number < 1001 || tile_number > IMA_UDIM_MAX)) {
680  return NULL;
681  }
682 
683  /* Tile number 0 is a special case and refers to the first tile, typically
684  * coming from non-UDIM-aware code. */
685  if (ELEM(tile_number, 0, 1001)) {
686  return ima->tiles.first;
687  }
688 
689  if (ima->source != IMA_SRC_TILED) {
690  return NULL;
691  }
692 
693  LISTBASE_FOREACH (ImageTile *, tile, &ima->tiles) {
694  if (tile->tile_number == tile_number) {
695  return tile;
696  }
697  }
698 
699  return NULL;
700 }
701 
703 {
704  return BKE_image_get_tile(ima, (iuser && iuser->tile) ? iuser->tile : 1001);
705 }
706 
708  const float uv[2],
709  float r_uv[2],
710  float r_ofs[2])
711 {
712  float local_ofs[2];
713  if (r_ofs == NULL) {
714  r_ofs = local_ofs;
715  }
716 
717  copy_v2_v2(r_uv, uv);
718  zero_v2(r_ofs);
719 
720  if ((ima->source != IMA_SRC_TILED) || uv[0] < 0.0f || uv[1] < 0.0f || uv[0] >= 10.0f) {
721  return 0;
722  }
723 
724  int ix = (int)uv[0];
725  int iy = (int)uv[1];
726  int tile_number = 1001 + 10 * iy + ix;
727 
728  if (BKE_image_get_tile(ima, tile_number) == NULL) {
729  return 0;
730  }
731  r_ofs[0] = ix;
732  r_ofs[1] = iy;
733  sub_v2_v2(r_uv, r_ofs);
734 
735  return tile_number;
736 }
737 
741 int BKE_image_find_nearest_tile(const Image *image, const float co[2])
742 {
743  const float co_floor[2] = {floorf(co[0]), floorf(co[1])};
744  /* Distance to the closest UDIM tile. */
745  float dist_best_sq = FLT_MAX;
746  int tile_number_best = -1;
747 
748  LISTBASE_FOREACH (const ImageTile *, tile, &image->tiles) {
749  const int tile_index = tile->tile_number - 1001;
750  /* Coordinates of the current tile. */
751  const float tile_index_co[2] = {tile_index % 10, tile_index / 10};
752 
753  if (equals_v2v2(co_floor, tile_index_co)) {
754  return tile->tile_number;
755  }
756 
757  /* Distance between co[2] and UDIM tile. */
758  const float dist_sq = len_squared_v2v2(tile_index_co, co);
759 
760  if (dist_sq < dist_best_sq) {
761  dist_best_sq = dist_sq;
762  tile_number_best = tile->tile_number;
763  }
764  }
765 
766  return tile_number_best;
767 }
768 
770 {
771  ImBuf *ibuf;
772  char name[FILE_MAX];
773 
774  BKE_image_user_file_path(NULL, ima, name);
775 
776  /* will set input color space to image format default's */
778 
779  if (ibuf) {
780  if (ibuf->flags & IB_alphamode_premul) {
782  }
783  else if (ibuf->flags & IB_alphamode_channel_packed) {
785  }
786  else if (ibuf->flags & IB_alphamode_ignore) {
788  }
789  else {
791  }
792 
793  IMB_freeImBuf(ibuf);
794  }
795 }
796 
797 char BKE_image_alpha_mode_from_extension_ex(const char *filepath)
798 {
799  if (BLI_path_extension_check_n(filepath, ".exr", ".cin", ".dpx", ".hdr", NULL)) {
800  return IMA_ALPHA_PREMUL;
801  }
802 
803  return IMA_ALPHA_STRAIGHT;
804 }
805 
807 {
809 }
810 
811 Image *BKE_image_load(Main *bmain, const char *filepath)
812 {
813  Image *ima;
814  int file;
815  char str[FILE_MAX];
816 
817  STRNCPY(str, filepath);
819 
820  /* exists? */
821  file = BLI_open(str, O_BINARY | O_RDONLY, 0);
822  if (file == -1) {
823  return NULL;
824  }
825  close(file);
826 
827  ima = image_alloc(bmain, BLI_path_basename(filepath), IMA_SRC_FILE, IMA_TYPE_IMAGE);
828  STRNCPY(ima->filepath, filepath);
829 
831  ima->source = IMA_SRC_MOVIE;
832  }
833 
835 
836  return ima;
837 }
838 
839 /* checks if image was already loaded, then returns same image */
840 /* otherwise creates new. */
841 /* does not load ibuf itself */
842 /* pass on optional frame for #name images */
843 Image *BKE_image_load_exists_ex(Main *bmain, const char *filepath, bool *r_exists)
844 {
845  Image *ima;
846  char str[FILE_MAX], strtest[FILE_MAX];
847 
848  STRNCPY(str, filepath);
849  BLI_path_abs(str, bmain->name);
850 
851  /* first search an identical filepath */
852  for (ima = bmain->images.first; ima; ima = ima->id.next) {
854  STRNCPY(strtest, ima->filepath);
855  BLI_path_abs(strtest, ID_BLEND_PATH(bmain, &ima->id));
856 
857  if (BLI_path_cmp(strtest, str) == 0) {
858  if ((BKE_image_has_anim(ima) == false) || (ima->id.us == 0)) {
859  id_us_plus(&ima->id); /* officially should not, it doesn't link here! */
860  LISTBASE_FOREACH (ImageTile *, tile, &ima->tiles) {
861  if (tile->ok == 0) {
862  tile->ok = IMA_OK;
863  }
864  }
865  if (r_exists) {
866  *r_exists = true;
867  }
868  return ima;
869  }
870  }
871  }
872  }
873 
874  if (r_exists) {
875  *r_exists = false;
876  }
877  return BKE_image_load(bmain, filepath);
878 }
879 
880 Image *BKE_image_load_exists(Main *bmain, const char *filepath)
881 {
882  return BKE_image_load_exists_ex(bmain, filepath, NULL);
883 }
884 
885 static ImBuf *add_ibuf_size(unsigned int width,
886  unsigned int height,
887  const char *name,
888  int depth,
889  int floatbuf,
890  short gen_type,
891  const float color[4],
892  ColorManagedColorspaceSettings *colorspace_settings)
893 {
894  ImBuf *ibuf;
895  unsigned char *rect = NULL;
896  float *rect_float = NULL;
897  float fill_color[4];
898 
899  if (floatbuf) {
900  ibuf = IMB_allocImBuf(width, height, depth, IB_rectfloat);
901 
902  if (colorspace_settings->name[0] == '\0') {
903  const char *colorspace = IMB_colormanagement_role_colorspace_name_get(
905 
906  STRNCPY(colorspace_settings->name, colorspace);
907  }
908 
909  if (ibuf != NULL) {
910  rect_float = ibuf->rect_float;
911  IMB_colormanagement_check_is_data(ibuf, colorspace_settings->name);
912  }
913 
914  if (IMB_colormanagement_space_name_is_data(colorspace_settings->name)) {
915  copy_v4_v4(fill_color, color);
916  }
917  else {
918  /* The input color here should ideally be linear already, but for now
919  * we just convert and postpone breaking the API for later. */
920  srgb_to_linearrgb_v4(fill_color, color);
921  }
922  }
923  else {
924  ibuf = IMB_allocImBuf(width, height, depth, IB_rect);
925 
926  if (colorspace_settings->name[0] == '\0') {
927  const char *colorspace = IMB_colormanagement_role_colorspace_name_get(
929 
930  STRNCPY(colorspace_settings->name, colorspace);
931  }
932 
933  if (ibuf != NULL) {
934  rect = (unsigned char *)ibuf->rect;
935  IMB_colormanagement_assign_rect_colorspace(ibuf, colorspace_settings->name);
936  }
937 
938  copy_v4_v4(fill_color, color);
939  }
940 
941  if (!ibuf) {
942  return NULL;
943  }
944 
945  STRNCPY(ibuf->name, name);
946 
947  switch (gen_type) {
948  case IMA_GENTYPE_GRID:
949  BKE_image_buf_fill_checker(rect, rect_float, width, height);
950  break;
952  BKE_image_buf_fill_checker_color(rect, rect_float, width, height);
953  break;
954  default:
955  BKE_image_buf_fill_color(rect, rect_float, width, height, fill_color);
956  break;
957  }
958 
959  return ibuf;
960 }
961 
962 /* adds new image block, creates ImBuf and initializes color */
964  unsigned int width,
965  unsigned int height,
966  const char *name,
967  int depth,
968  int floatbuf,
969  short gen_type,
970  const float color[4],
971  const bool stereo3d,
972  const bool is_data,
973  const bool tiled)
974 {
975  /* on save, type is changed to FILE in editsima.c */
976  Image *ima;
977  if (tiled) {
978  ima = image_alloc(bmain, name, IMA_SRC_TILED, IMA_TYPE_IMAGE);
979  }
980  else {
981  ima = image_alloc(bmain, name, IMA_SRC_GENERATED, IMA_TYPE_UV_TEST);
982  }
983  if (ima == NULL) {
984  return NULL;
985  }
986 
987  int view_id;
988  const char *names[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME};
989 
990  /* STRNCPY(ima->filepath, name); */ /* don't do this, this writes in ain invalid filepath! */
991  ima->gen_x = width;
992  ima->gen_y = height;
993  ima->gen_type = gen_type;
994  ima->gen_flag |= (floatbuf ? IMA_GEN_FLOAT : 0);
995  ima->gen_depth = depth;
996  copy_v4_v4(ima->gen_color, color);
997 
998  if (is_data) {
1001  }
1002 
1003  for (view_id = 0; view_id < 2; view_id++) {
1004  ImBuf *ibuf;
1005  ibuf = add_ibuf_size(
1006  width, height, ima->filepath, depth, floatbuf, gen_type, color, &ima->colorspace_settings);
1007  int index = tiled ? 0 : IMA_NO_INDEX;
1008  int entry = tiled ? 1001 : 0;
1009  image_assign_ibuf(ima, ibuf, stereo3d ? view_id : index, entry);
1010 
1011  /* image_assign_ibuf puts buffer to the cache, which increments user counter. */
1012  IMB_freeImBuf(ibuf);
1013  if (!stereo3d) {
1014  break;
1015  }
1016 
1017  image_add_view(ima, names[view_id], "");
1018  }
1019 
1020  ImageTile *tile = BKE_image_get_tile(ima, 0);
1021  tile->ok = IMA_OK_LOADED;
1022 
1023  return ima;
1024 }
1025 
1026 /* Create an image image from ibuf. The refcount of ibuf is increased,
1027  * caller should take care to drop its reference by calling
1028  * IMB_freeImBuf if needed. */
1029 Image *BKE_image_add_from_imbuf(Main *bmain, ImBuf *ibuf, const char *name)
1030 {
1031  /* on save, type is changed to FILE in editsima.c */
1032  Image *ima;
1033 
1034  if (name == NULL) {
1035  name = BLI_path_basename(ibuf->name);
1036  }
1037 
1038  ima = image_alloc(bmain, name, IMA_SRC_FILE, IMA_TYPE_IMAGE);
1039 
1040  if (ima) {
1041  STRNCPY(ima->filepath, ibuf->name);
1042  image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
1043  ImageTile *tile = BKE_image_get_tile(ima, 0);
1044  tile->ok = IMA_OK_LOADED;
1045  }
1046 
1047  return ima;
1048 }
1049 
1050 /* Pack image buffer to memory as PNG or EXR. */
1051 static bool image_memorypack_imbuf(Image *ima, ImBuf *ibuf, const char *filepath)
1052 {
1053  ibuf->ftype = (ibuf->rect_float) ? IMB_FTYPE_OPENEXR : IMB_FTYPE_PNG;
1054 
1055  IMB_saveiff(ibuf, filepath, IB_rect | IB_mem);
1056 
1057  if (ibuf->encodedbuffer == NULL) {
1058  CLOG_STR_ERROR(&LOG, "memory save for pack error");
1059  IMB_freeImBuf(ibuf);
1061  return false;
1062  }
1063 
1064  ImagePackedFile *imapf;
1065  PackedFile *pf = MEM_callocN(sizeof(*pf), "PackedFile");
1066 
1067  pf->data = ibuf->encodedbuffer;
1068  pf->size = ibuf->encodedsize;
1069 
1070  imapf = MEM_mallocN(sizeof(ImagePackedFile), "Image PackedFile");
1071  STRNCPY(imapf->filepath, filepath);
1072  imapf->packedfile = pf;
1073  BLI_addtail(&ima->packedfiles, imapf);
1074 
1075  ibuf->encodedbuffer = NULL;
1076  ibuf->encodedsize = 0;
1077  ibuf->userflags &= ~IB_BITMAPDIRTY;
1078 
1079  return true;
1080 }
1081 
1082 /* Pack image to memory. */
1084 {
1085  bool ok = true;
1086 
1088 
1089  if (BKE_image_is_multiview(ima)) {
1090  /* Store each view as a separate packed files with R_IMF_VIEWS_INDIVIDUAL. */
1091  ImageView *iv;
1092  int i;
1093 
1094  for (i = 0, iv = ima->views.first; iv; iv = iv->next, i++) {
1095  ImBuf *ibuf = image_get_cached_ibuf_for_index_entry(ima, i, 0);
1096 
1097  if (!ibuf) {
1098  ok = false;
1099  break;
1100  }
1101 
1102  /* if the image was a R_IMF_VIEWS_STEREO_3D we force _L, _R suffices */
1103  if (ima->views_format == R_IMF_VIEWS_STEREO_3D) {
1104  const char *suffix[2] = {STEREO_LEFT_SUFFIX, STEREO_RIGHT_SUFFIX};
1105  BLI_path_suffix(iv->filepath, FILE_MAX, suffix[i], "");
1106  }
1107 
1108  ok = ok && image_memorypack_imbuf(ima, ibuf, iv->filepath);
1109  IMB_freeImBuf(ibuf);
1110  }
1111 
1113  }
1114  else {
1116 
1117  if (ibuf) {
1118  ok = ok && image_memorypack_imbuf(ima, ibuf, ibuf->name);
1119  IMB_freeImBuf(ibuf);
1120  }
1121  else {
1122  ok = false;
1123  }
1124  }
1125 
1126  if (ok && ima->source == IMA_SRC_GENERATED) {
1127  ima->source = IMA_SRC_FILE;
1128  ima->type = IMA_TYPE_IMAGE;
1129  }
1130 
1131  return ok;
1132 }
1133 
1134 void BKE_image_packfiles(ReportList *reports, Image *ima, const char *basepath)
1135 {
1136  const int totfiles = image_num_files(ima);
1137 
1138  if (totfiles == 1) {
1139  ImagePackedFile *imapf = MEM_mallocN(sizeof(ImagePackedFile), "Image packed file");
1140  BLI_addtail(&ima->packedfiles, imapf);
1141  imapf->packedfile = BKE_packedfile_new(reports, ima->filepath, basepath);
1142  if (imapf->packedfile) {
1143  STRNCPY(imapf->filepath, ima->filepath);
1144  }
1145  else {
1146  BLI_freelinkN(&ima->packedfiles, imapf);
1147  }
1148  }
1149  else {
1150  ImageView *iv;
1151  for (iv = ima->views.first; iv; iv = iv->next) {
1152  ImagePackedFile *imapf = MEM_mallocN(sizeof(ImagePackedFile), "Image packed file");
1153  BLI_addtail(&ima->packedfiles, imapf);
1154 
1155  imapf->packedfile = BKE_packedfile_new(reports, iv->filepath, basepath);
1156  if (imapf->packedfile) {
1157  STRNCPY(imapf->filepath, iv->filepath);
1158  }
1159  else {
1160  BLI_freelinkN(&ima->packedfiles, imapf);
1161  }
1162  }
1163  }
1164 }
1165 
1167  Image *ima,
1168  char *data,
1169  const size_t data_len)
1170 {
1171  const int totfiles = image_num_files(ima);
1172 
1173  if (totfiles != 1) {
1174  BKE_report(reports, RPT_ERROR, "Cannot pack multiview images from raw data currently...");
1175  }
1176  else {
1177  ImagePackedFile *imapf = MEM_mallocN(sizeof(ImagePackedFile), __func__);
1178  BLI_addtail(&ima->packedfiles, imapf);
1179  imapf->packedfile = BKE_packedfile_new_from_memory(data, data_len);
1180  STRNCPY(imapf->filepath, ima->filepath);
1181  }
1182 }
1183 
1185 {
1187 }
1188 
1190 {
1191  uintptr_t size = 0;
1192 
1193  /* viewers have memory depending on other rules, has no valid rect pointer */
1194  if (image->source == IMA_SRC_VIEWER) {
1195  return 0;
1196  }
1197 
1199  if (image->cache != NULL) {
1200  struct MovieCacheIter *iter = IMB_moviecacheIter_new(image->cache);
1201 
1202  while (!IMB_moviecacheIter_done(iter)) {
1203  ImBuf *ibuf = IMB_moviecacheIter_getImBuf(iter);
1204  ImBuf *ibufm;
1205  int level;
1206 
1207  if (ibuf->rect) {
1208  size += MEM_allocN_len(ibuf->rect);
1209  }
1210  if (ibuf->rect_float) {
1211  size += MEM_allocN_len(ibuf->rect_float);
1212  }
1213 
1214  for (level = 0; level < IMB_MIPMAP_LEVELS; level++) {
1215  ibufm = ibuf->mipmap[level];
1216  if (ibufm) {
1217  if (ibufm->rect) {
1218  size += MEM_allocN_len(ibufm->rect);
1219  }
1220  if (ibufm->rect_float) {
1221  size += MEM_allocN_len(ibufm->rect_float);
1222  }
1223  }
1224  }
1225 
1227  }
1229  }
1231 
1232  return size;
1233 }
1234 
1236 {
1237  Image *ima;
1238  uintptr_t size, totsize = 0;
1239 
1240  for (ima = bmain->images.first; ima; ima = ima->id.next) {
1241  totsize += image_mem_size(ima);
1242  }
1243 
1244  printf("\ntotal image memory len: %.3f MB\n", (double)totsize / (double)(1024 * 1024));
1245 
1246  for (ima = bmain->images.first; ima; ima = ima->id.next) {
1247  size = image_mem_size(ima);
1248 
1249  if (size) {
1250  printf("%s len: %.3f MB\n", ima->id.name + 2, (double)size / (double)(1024 * 1024));
1251  }
1252  }
1253 }
1254 
1255 static bool imagecache_check_dirty(ImBuf *ibuf, void *UNUSED(userkey), void *UNUSED(userdata))
1256 {
1257  return (ibuf->userflags & IB_BITMAPDIRTY) == 0;
1258 }
1259 
1261 {
1262 #undef CHECK_FREED_SIZE
1263 
1264  Tex *tex;
1265  Image *ima;
1266 #ifdef CHECK_FREED_SIZE
1267  uintptr_t tot_freed_size = 0;
1268 #endif
1269 
1270  for (ima = bmain->images.first; ima; ima = ima->id.next) {
1271  ima->id.tag &= ~LIB_TAG_DOIT;
1272  }
1273 
1274  for (tex = bmain->textures.first; tex; tex = tex->id.next) {
1275  if (tex->ima) {
1276  tex->ima->id.tag |= LIB_TAG_DOIT;
1277  }
1278  }
1279 
1280  for (ima = bmain->images.first; ima; ima = ima->id.next) {
1281  if (ima->cache && (ima->id.tag & LIB_TAG_DOIT)) {
1282 #ifdef CHECK_FREED_SIZE
1283  uintptr_t old_size = image_mem_size(ima);
1284 #endif
1285 
1287 
1288 #ifdef CHECK_FREED_SIZE
1289  tot_freed_size += old_size - image_mem_size(ima);
1290 #endif
1291  }
1292  }
1293 #ifdef CHECK_FREED_SIZE
1294  printf("%s: freed total %lu MB\n", __func__, tot_freed_size / (1024 * 1024));
1295 #endif
1296 }
1297 
1298 static bool imagecache_check_free_anim(ImBuf *ibuf, void *UNUSED(userkey), void *userdata)
1299 {
1300  int except_frame = *(int *)userdata;
1301  return (ibuf->userflags & IB_BITMAPDIRTY) == 0 && (ibuf->index != IMA_NO_INDEX) &&
1302  (except_frame != IMA_INDEX_ENTRY(ibuf->index));
1303 }
1304 
1305 /* except_frame is weak, only works for seqs without offset... */
1306 void BKE_image_free_anim_ibufs(Image *ima, int except_frame)
1307 {
1309  if (ima->cache != NULL) {
1311  }
1313 }
1314 
1315 void BKE_image_all_free_anim_ibufs(Main *bmain, int cfra)
1316 {
1317  Image *ima;
1318 
1319  for (ima = bmain->images.first; ima; ima = ima->id.next) {
1320  if (BKE_image_is_animated(ima)) {
1321  BKE_image_free_anim_ibufs(ima, cfra);
1322  }
1323  }
1324 }
1325 
1326 /* *********** READ AND WRITE ************** */
1327 
1328 int BKE_image_imtype_to_ftype(const char imtype, ImbFormatOptions *r_options)
1329 {
1330  memset(r_options, 0, sizeof(*r_options));
1331 
1332  if (imtype == R_IMF_IMTYPE_TARGA) {
1333  return IMB_FTYPE_TGA;
1334  }
1335  if (imtype == R_IMF_IMTYPE_RAWTGA) {
1336  r_options->flag = RAWTGA;
1337  return IMB_FTYPE_TGA;
1338  }
1339  if (imtype == R_IMF_IMTYPE_IRIS) {
1340  return IMB_FTYPE_IMAGIC;
1341  }
1342 #ifdef WITH_HDR
1343  if (imtype == R_IMF_IMTYPE_RADHDR) {
1344  return IMB_FTYPE_RADHDR;
1345  }
1346 #endif
1347  if (imtype == R_IMF_IMTYPE_PNG) {
1348  r_options->quality = 15;
1349  return IMB_FTYPE_PNG;
1350  }
1351 #ifdef WITH_DDS
1352  if (imtype == R_IMF_IMTYPE_DDS) {
1353  return IMB_FTYPE_DDS;
1354  }
1355 #endif
1356  if (imtype == R_IMF_IMTYPE_BMP) {
1357  return IMB_FTYPE_BMP;
1358  }
1359 #ifdef WITH_TIFF
1360  if (imtype == R_IMF_IMTYPE_TIFF) {
1361  return IMB_FTYPE_TIF;
1362  }
1363 #endif
1365  return IMB_FTYPE_OPENEXR;
1366  }
1367 #ifdef WITH_CINEON
1368  if (imtype == R_IMF_IMTYPE_CINEON) {
1369  return IMB_FTYPE_CINEON;
1370  }
1371  if (imtype == R_IMF_IMTYPE_DPX) {
1372  return IMB_FTYPE_DPX;
1373  }
1374 #endif
1375 #ifdef WITH_OPENJPEG
1376  if (imtype == R_IMF_IMTYPE_JP2) {
1377  r_options->flag |= JP2_JP2;
1378  r_options->quality = 90;
1379  return IMB_FTYPE_JP2;
1380  }
1381 #endif
1382 
1383  r_options->quality = 90;
1384  return IMB_FTYPE_JPG;
1385 }
1386 
1388 {
1389  if (ftype == IMB_FTYPE_NONE) {
1390  return R_IMF_IMTYPE_TARGA;
1391  }
1392  if (ftype == IMB_FTYPE_IMAGIC) {
1393  return R_IMF_IMTYPE_IRIS;
1394  }
1395 #ifdef WITH_HDR
1396  if (ftype == IMB_FTYPE_RADHDR) {
1397  return R_IMF_IMTYPE_RADHDR;
1398  }
1399 #endif
1400  if (ftype == IMB_FTYPE_PNG) {
1401  return R_IMF_IMTYPE_PNG;
1402  }
1403 #ifdef WITH_DDS
1404  if (ftype == IMB_FTYPE_DDS) {
1405  return R_IMF_IMTYPE_DDS;
1406  }
1407 #endif
1408  if (ftype == IMB_FTYPE_BMP) {
1409  return R_IMF_IMTYPE_BMP;
1410  }
1411 #ifdef WITH_TIFF
1412  if (ftype == IMB_FTYPE_TIF) {
1413  return R_IMF_IMTYPE_TIFF;
1414  }
1415 #endif
1416  if (ftype == IMB_FTYPE_OPENEXR) {
1417  return R_IMF_IMTYPE_OPENEXR;
1418  }
1419 #ifdef WITH_CINEON
1420  if (ftype == IMB_FTYPE_CINEON) {
1421  return R_IMF_IMTYPE_CINEON;
1422  }
1423  if (ftype == IMB_FTYPE_DPX) {
1424  return R_IMF_IMTYPE_DPX;
1425  }
1426 #endif
1427  if (ftype == IMB_FTYPE_TGA) {
1428  if (options && (options->flag & RAWTGA)) {
1429  return R_IMF_IMTYPE_RAWTGA;
1430  }
1431 
1432  return R_IMF_IMTYPE_TARGA;
1433  }
1434 #ifdef WITH_OPENJPEG
1435  if (ftype == IMB_FTYPE_JP2) {
1436  return R_IMF_IMTYPE_JP2;
1437  }
1438 #endif
1439 
1440  return R_IMF_IMTYPE_JPEG90;
1441 }
1442 
1443 bool BKE_imtype_is_movie(const char imtype)
1444 {
1445  switch (imtype) {
1446  case R_IMF_IMTYPE_AVIRAW:
1447  case R_IMF_IMTYPE_AVIJPEG:
1448  case R_IMF_IMTYPE_FFMPEG:
1449  case R_IMF_IMTYPE_H264:
1450  case R_IMF_IMTYPE_THEORA:
1451  case R_IMF_IMTYPE_XVID:
1452  return true;
1453  }
1454  return false;
1455 }
1456 
1457 bool BKE_imtype_supports_zbuf(const char imtype)
1458 {
1459  switch (imtype) {
1460  case R_IMF_IMTYPE_IRIZ:
1461  case R_IMF_IMTYPE_OPENEXR: /* but not R_IMF_IMTYPE_MULTILAYER */
1462  return true;
1463  }
1464  return false;
1465 }
1466 
1467 bool BKE_imtype_supports_compress(const char imtype)
1468 {
1469  switch (imtype) {
1470  case R_IMF_IMTYPE_PNG:
1471  return true;
1472  }
1473  return false;
1474 }
1475 
1476 bool BKE_imtype_supports_quality(const char imtype)
1477 {
1478  switch (imtype) {
1479  case R_IMF_IMTYPE_JPEG90:
1480  case R_IMF_IMTYPE_JP2:
1481  case R_IMF_IMTYPE_AVIJPEG:
1482  return true;
1483  }
1484  return false;
1485 }
1486 
1487 bool BKE_imtype_requires_linear_float(const char imtype)
1488 {
1489  switch (imtype) {
1490  case R_IMF_IMTYPE_CINEON:
1491  case R_IMF_IMTYPE_DPX:
1492  case R_IMF_IMTYPE_RADHDR:
1493  case R_IMF_IMTYPE_OPENEXR:
1495  return true;
1496  }
1497  return false;
1498 }
1499 
1500 char BKE_imtype_valid_channels(const char imtype, bool write_file)
1501 {
1502  char chan_flag = IMA_CHAN_FLAG_RGB; /* assume all support rgb */
1503 
1504  /* alpha */
1505  switch (imtype) {
1506  case R_IMF_IMTYPE_BMP:
1507  if (write_file) {
1508  break;
1509  }
1511  case R_IMF_IMTYPE_TARGA:
1512  case R_IMF_IMTYPE_RAWTGA:
1513  case R_IMF_IMTYPE_IRIS:
1514  case R_IMF_IMTYPE_PNG:
1515  case R_IMF_IMTYPE_TIFF:
1516  case R_IMF_IMTYPE_OPENEXR:
1518  case R_IMF_IMTYPE_DDS:
1519  case R_IMF_IMTYPE_JP2:
1520  case R_IMF_IMTYPE_DPX:
1521  chan_flag |= IMA_CHAN_FLAG_ALPHA;
1522  break;
1523  }
1524 
1525  /* bw */
1526  switch (imtype) {
1527  case R_IMF_IMTYPE_BMP:
1528  case R_IMF_IMTYPE_PNG:
1529  case R_IMF_IMTYPE_JPEG90:
1530  case R_IMF_IMTYPE_TARGA:
1531  case R_IMF_IMTYPE_RAWTGA:
1532  case R_IMF_IMTYPE_TIFF:
1533  case R_IMF_IMTYPE_IRIS:
1534  chan_flag |= IMA_CHAN_FLAG_BW;
1535  break;
1536  }
1537 
1538  return chan_flag;
1539 }
1540 
1541 char BKE_imtype_valid_depths(const char imtype)
1542 {
1543  switch (imtype) {
1544  case R_IMF_IMTYPE_RADHDR:
1545  return R_IMF_CHAN_DEPTH_32;
1546  case R_IMF_IMTYPE_TIFF:
1548  case R_IMF_IMTYPE_OPENEXR:
1552  /* eeh, cineon does some strange 10bits per channel */
1553  case R_IMF_IMTYPE_DPX:
1555  case R_IMF_IMTYPE_CINEON:
1556  return R_IMF_CHAN_DEPTH_10;
1557  case R_IMF_IMTYPE_JP2:
1559  case R_IMF_IMTYPE_PNG:
1561  /* most formats are 8bit only */
1562  default:
1563  return R_IMF_CHAN_DEPTH_8;
1564  }
1565 }
1566 
1567 /* string is from command line --render-format arg, keep in sync with
1568  * creator_args.c help info */
1569 char BKE_imtype_from_arg(const char *imtype_arg)
1570 {
1571  if (STREQ(imtype_arg, "TGA")) {
1572  return R_IMF_IMTYPE_TARGA;
1573  }
1574  if (STREQ(imtype_arg, "IRIS")) {
1575  return R_IMF_IMTYPE_IRIS;
1576  }
1577 #ifdef WITH_DDS
1578  if (STREQ(imtype_arg, "DDS")) {
1579  return R_IMF_IMTYPE_DDS;
1580  }
1581 #endif
1582  if (STREQ(imtype_arg, "JPEG")) {
1583  return R_IMF_IMTYPE_JPEG90;
1584  }
1585  if (STREQ(imtype_arg, "IRIZ")) {
1586  return R_IMF_IMTYPE_IRIZ;
1587  }
1588  if (STREQ(imtype_arg, "RAWTGA")) {
1589  return R_IMF_IMTYPE_RAWTGA;
1590  }
1591  if (STREQ(imtype_arg, "AVIRAW")) {
1592  return R_IMF_IMTYPE_AVIRAW;
1593  }
1594  if (STREQ(imtype_arg, "AVIJPEG")) {
1595  return R_IMF_IMTYPE_AVIJPEG;
1596  }
1597  if (STREQ(imtype_arg, "PNG")) {
1598  return R_IMF_IMTYPE_PNG;
1599  }
1600  if (STREQ(imtype_arg, "BMP")) {
1601  return R_IMF_IMTYPE_BMP;
1602  }
1603 #ifdef WITH_HDR
1604  if (STREQ(imtype_arg, "HDR")) {
1605  return R_IMF_IMTYPE_RADHDR;
1606  }
1607 #endif
1608 #ifdef WITH_TIFF
1609  if (STREQ(imtype_arg, "TIFF")) {
1610  return R_IMF_IMTYPE_TIFF;
1611  }
1612 #endif
1613 #ifdef WITH_OPENEXR
1614  if (STREQ(imtype_arg, "OPEN_EXR")) {
1615  return R_IMF_IMTYPE_OPENEXR;
1616  }
1617  if (STREQ(imtype_arg, "OPEN_EXR_MULTILAYER")) {
1618  return R_IMF_IMTYPE_MULTILAYER;
1619  }
1620  if (STREQ(imtype_arg, "EXR")) {
1621  return R_IMF_IMTYPE_OPENEXR;
1622  }
1623  if (STREQ(imtype_arg, "MULTILAYER")) {
1624  return R_IMF_IMTYPE_MULTILAYER;
1625  }
1626 #endif
1627  if (STREQ(imtype_arg, "FFMPEG")) {
1628  return R_IMF_IMTYPE_FFMPEG;
1629  }
1630 #ifdef WITH_CINEON
1631  if (STREQ(imtype_arg, "CINEON")) {
1632  return R_IMF_IMTYPE_CINEON;
1633  }
1634  if (STREQ(imtype_arg, "DPX")) {
1635  return R_IMF_IMTYPE_DPX;
1636  }
1637 #endif
1638 #ifdef WITH_OPENJPEG
1639  if (STREQ(imtype_arg, "JP2")) {
1640  return R_IMF_IMTYPE_JP2;
1641  }
1642 #endif
1643 
1644  return R_IMF_IMTYPE_INVALID;
1645 }
1646 
1647 static bool do_add_image_extension(char *string,
1648  const char imtype,
1649  const ImageFormatData *im_format)
1650 {
1651  const char *extension = NULL;
1652  const char *extension_test;
1653  (void)im_format; /* may be unused, depends on build options */
1654 
1655  if (imtype == R_IMF_IMTYPE_IRIS) {
1656  if (!BLI_path_extension_check(string, extension_test = ".rgb")) {
1657  extension = extension_test;
1658  }
1659  }
1660  else if (imtype == R_IMF_IMTYPE_IRIZ) {
1661  if (!BLI_path_extension_check(string, extension_test = ".rgb")) {
1662  extension = extension_test;
1663  }
1664  }
1665 #ifdef WITH_HDR
1666  else if (imtype == R_IMF_IMTYPE_RADHDR) {
1667  if (!BLI_path_extension_check(string, extension_test = ".hdr")) {
1668  extension = extension_test;
1669  }
1670  }
1671 #endif
1672  else if (ELEM(imtype,
1677  R_IMF_IMTYPE_XVID)) {
1678  if (!BLI_path_extension_check(string, extension_test = ".png")) {
1679  extension = extension_test;
1680  }
1681  }
1682 #ifdef WITH_DDS
1683  else if (imtype == R_IMF_IMTYPE_DDS) {
1684  if (!BLI_path_extension_check(string, extension_test = ".dds")) {
1685  extension = extension_test;
1686  }
1687  }
1688 #endif
1689  else if (ELEM(imtype, R_IMF_IMTYPE_TARGA, R_IMF_IMTYPE_RAWTGA)) {
1690  if (!BLI_path_extension_check(string, extension_test = ".tga")) {
1691  extension = extension_test;
1692  }
1693  }
1694  else if (imtype == R_IMF_IMTYPE_BMP) {
1695  if (!BLI_path_extension_check(string, extension_test = ".bmp")) {
1696  extension = extension_test;
1697  }
1698  }
1699 #ifdef WITH_TIFF
1700  else if (imtype == R_IMF_IMTYPE_TIFF) {
1701  if (!BLI_path_extension_check_n(string, extension_test = ".tif", ".tiff", NULL)) {
1702  extension = extension_test;
1703  }
1704  }
1705 #endif
1706 #ifdef WITH_OPENIMAGEIO
1707  else if (imtype == R_IMF_IMTYPE_PSD) {
1708  if (!BLI_path_extension_check(string, extension_test = ".psd")) {
1709  extension = extension_test;
1710  }
1711  }
1712 #endif
1713 #ifdef WITH_OPENEXR
1714  else if (ELEM(imtype, R_IMF_IMTYPE_OPENEXR, R_IMF_IMTYPE_MULTILAYER)) {
1715  if (!BLI_path_extension_check(string, extension_test = ".exr")) {
1716  extension = extension_test;
1717  }
1718  }
1719 #endif
1720 #ifdef WITH_CINEON
1721  else if (imtype == R_IMF_IMTYPE_CINEON) {
1722  if (!BLI_path_extension_check(string, extension_test = ".cin")) {
1723  extension = extension_test;
1724  }
1725  }
1726  else if (imtype == R_IMF_IMTYPE_DPX) {
1727  if (!BLI_path_extension_check(string, extension_test = ".dpx")) {
1728  extension = extension_test;
1729  }
1730  }
1731 #endif
1732 #ifdef WITH_OPENJPEG
1733  else if (imtype == R_IMF_IMTYPE_JP2) {
1734  if (im_format) {
1735  if (im_format->jp2_codec == R_IMF_JP2_CODEC_JP2) {
1736  if (!BLI_path_extension_check(string, extension_test = ".jp2")) {
1737  extension = extension_test;
1738  }
1739  }
1740  else if (im_format->jp2_codec == R_IMF_JP2_CODEC_J2K) {
1741  if (!BLI_path_extension_check(string, extension_test = ".j2c")) {
1742  extension = extension_test;
1743  }
1744  }
1745  else {
1746  BLI_assert(!"Unsupported jp2 codec was specified in im_format->jp2_codec");
1747  }
1748  }
1749  else {
1750  if (!BLI_path_extension_check(string, extension_test = ".jp2")) {
1751  extension = extension_test;
1752  }
1753  }
1754  }
1755 #endif
1756  else { // R_IMF_IMTYPE_AVIRAW, R_IMF_IMTYPE_AVIJPEG, R_IMF_IMTYPE_JPEG90 etc
1757  if (!(BLI_path_extension_check_n(string, extension_test = ".jpg", ".jpeg", NULL))) {
1758  extension = extension_test;
1759  }
1760  }
1761 
1762  if (extension) {
1763  /* prefer this in many cases to avoid .png.tga, but in certain cases it breaks */
1764  /* remove any other known image extension */
1766  return BLI_path_extension_replace(string, FILE_MAX, extension);
1767  }
1768 
1769  return BLI_path_extension_ensure(string, FILE_MAX, extension);
1770  }
1771 
1772  return false;
1773 }
1774 
1776 {
1777  return do_add_image_extension(string, im_format->imtype, im_format);
1778 }
1779 
1780 int BKE_image_path_ensure_ext_from_imtype(char *string, const char imtype)
1781 {
1782  return do_add_image_extension(string, imtype, NULL);
1783 }
1784 
1786 {
1787  memset(im_format, 0, sizeof(*im_format));
1788  im_format->planes = R_IMF_PLANES_RGBA;
1789  im_format->imtype = R_IMF_IMTYPE_PNG;
1790  im_format->depth = R_IMF_CHAN_DEPTH_8;
1791  im_format->quality = 90;
1792  im_format->compress = 15;
1793 
1796  &im_format->display_settings);
1797 }
1798 
1799 void BKE_imbuf_to_image_format(struct ImageFormatData *im_format, const ImBuf *imbuf)
1800 {
1801  int ftype = imbuf->ftype;
1802  int custom_flags = imbuf->foptions.flag;
1803  char quality = imbuf->foptions.quality;
1804 
1805  BKE_imformat_defaults(im_format);
1806 
1807  /* file type */
1808 
1809  if (ftype == IMB_FTYPE_IMAGIC) {
1810  im_format->imtype = R_IMF_IMTYPE_IRIS;
1811  }
1812 #ifdef WITH_HDR
1813  else if (ftype == IMB_FTYPE_RADHDR) {
1814  im_format->imtype = R_IMF_IMTYPE_RADHDR;
1815  }
1816 #endif
1817  else if (ftype == IMB_FTYPE_PNG) {
1818  im_format->imtype = R_IMF_IMTYPE_PNG;
1819 
1820  if (custom_flags & PNG_16BIT) {
1821  im_format->depth = R_IMF_CHAN_DEPTH_16;
1822  }
1823 
1824  im_format->compress = quality;
1825  }
1826 
1827 #ifdef WITH_DDS
1828  else if (ftype == IMB_FTYPE_DDS) {
1829  im_format->imtype = R_IMF_IMTYPE_DDS;
1830  }
1831 #endif
1832  else if (ftype == IMB_FTYPE_BMP) {
1833  im_format->imtype = R_IMF_IMTYPE_BMP;
1834  }
1835 #ifdef WITH_TIFF
1836  else if (ftype == IMB_FTYPE_TIF) {
1837  im_format->imtype = R_IMF_IMTYPE_TIFF;
1838  if (custom_flags & TIF_16BIT) {
1839  im_format->depth = R_IMF_CHAN_DEPTH_16;
1840  }
1841  if (custom_flags & TIF_COMPRESS_NONE) {
1842  im_format->tiff_codec = R_IMF_TIFF_CODEC_NONE;
1843  }
1844  if (custom_flags & TIF_COMPRESS_DEFLATE) {
1845  im_format->tiff_codec = R_IMF_TIFF_CODEC_DEFLATE;
1846  }
1847  if (custom_flags & TIF_COMPRESS_LZW) {
1848  im_format->tiff_codec = R_IMF_TIFF_CODEC_LZW;
1849  }
1850  if (custom_flags & TIF_COMPRESS_PACKBITS) {
1852  }
1853  }
1854 #endif
1855 
1856 #ifdef WITH_OPENEXR
1857  else if (ftype == IMB_FTYPE_OPENEXR) {
1858  im_format->imtype = R_IMF_IMTYPE_OPENEXR;
1859  if (custom_flags & OPENEXR_HALF) {
1860  im_format->depth = R_IMF_CHAN_DEPTH_16;
1861  }
1862  if (custom_flags & OPENEXR_COMPRESS) {
1863  im_format->exr_codec = R_IMF_EXR_CODEC_ZIP; /* Can't determine compression */
1864  }
1865  if (imbuf->zbuf_float) {
1866  im_format->flag |= R_IMF_FLAG_ZBUF;
1867  }
1868  }
1869 #endif
1870 
1871 #ifdef WITH_CINEON
1872  else if (ftype == IMB_FTYPE_CINEON) {
1873  im_format->imtype = R_IMF_IMTYPE_CINEON;
1874  }
1875  else if (ftype == IMB_FTYPE_DPX) {
1876  im_format->imtype = R_IMF_IMTYPE_DPX;
1877  }
1878 #endif
1879  else if (ftype == IMB_FTYPE_TGA) {
1880  if (custom_flags & RAWTGA) {
1881  im_format->imtype = R_IMF_IMTYPE_RAWTGA;
1882  }
1883  else {
1884  im_format->imtype = R_IMF_IMTYPE_TARGA;
1885  }
1886  }
1887 #ifdef WITH_OPENJPEG
1888  else if (ftype == IMB_FTYPE_JP2) {
1889  im_format->imtype = R_IMF_IMTYPE_JP2;
1890  im_format->quality = quality;
1891 
1892  if (custom_flags & JP2_16BIT) {
1893  im_format->depth = R_IMF_CHAN_DEPTH_16;
1894  }
1895  else if (custom_flags & JP2_12BIT) {
1896  im_format->depth = R_IMF_CHAN_DEPTH_12;
1897  }
1898 
1899  if (custom_flags & JP2_YCC) {
1900  im_format->jp2_flag |= R_IMF_JP2_FLAG_YCC;
1901  }
1902 
1903  if (custom_flags & JP2_CINE) {
1904  im_format->jp2_flag |= R_IMF_JP2_FLAG_CINE_PRESET;
1905  if (custom_flags & JP2_CINE_48FPS) {
1906  im_format->jp2_flag |= R_IMF_JP2_FLAG_CINE_48;
1907  }
1908  }
1909 
1910  if (custom_flags & JP2_JP2) {
1911  im_format->jp2_codec = R_IMF_JP2_CODEC_JP2;
1912  }
1913  else if (custom_flags & JP2_J2K) {
1914  im_format->jp2_codec = R_IMF_JP2_CODEC_J2K;
1915  }
1916  else {
1917  BLI_assert(!"Unsupported jp2 codec was specified in file type");
1918  }
1919  }
1920 #endif
1921 
1922  else {
1923  im_format->imtype = R_IMF_IMTYPE_JPEG90;
1924  im_format->quality = quality;
1925  }
1926 
1927  /* planes */
1928  im_format->planes = imbuf->planes;
1929 }
1930 
1931 #define STAMP_NAME_SIZE ((MAX_ID_NAME - 2) + 16)
1932 /* could allow access externally - 512 is for long names,
1933  * STAMP_NAME_SIZE is for id names, allowing them some room for description */
1934 typedef struct StampDataCustomField {
1936  /* TODO(sergey): Think of better size here, maybe dynamically allocated even. */
1937  char key[512];
1938  char *value;
1939  /* TODO(sergey): Support non-string values. */
1941 
1942 typedef struct StampData {
1943  char file[512];
1944  char note[512];
1945  char date[512];
1946  char marker[512];
1947  char time[512];
1948  char frame[512];
1949  char frame_range[512];
1956  char hostname[512];
1957 
1958  /* Custom fields are used to put extra meta information header from render
1959  * engine to the result image.
1960  *
1961  * NOTE: This fields are not stamped onto the image. At least for now.
1962  */
1965 #undef STAMP_NAME_SIZE
1966 
1971 static void stampdata(
1972  const Scene *scene, Object *camera, StampData *stamp_data, int do_prefix, bool use_dynamic)
1973 {
1974  char text[256];
1975  struct tm *tl;
1976  time_t t;
1977 
1978  if (scene->r.stamp & R_STAMP_FILENAME) {
1979  SNPRINTF(stamp_data->file,
1980  do_prefix ? "File %s" : "%s",
1981  G.relbase_valid ? BKE_main_blendfile_path_from_global() : "<untitled>");
1982  }
1983  else {
1984  stamp_data->file[0] = '\0';
1985  }
1986 
1987  if (scene->r.stamp & R_STAMP_NOTE) {
1988  /* Never do prefix for Note */
1989  SNPRINTF(stamp_data->note, "%s", scene->r.stamp_udata);
1990  }
1991  else {
1992  stamp_data->note[0] = '\0';
1993  }
1994 
1995  if (scene->r.stamp & R_STAMP_DATE) {
1996  t = time(NULL);
1997  tl = localtime(&t);
1998  SNPRINTF(text,
1999  "%04d/%02d/%02d %02d:%02d:%02d",
2000  tl->tm_year + 1900,
2001  tl->tm_mon + 1,
2002  tl->tm_mday,
2003  tl->tm_hour,
2004  tl->tm_min,
2005  tl->tm_sec);
2006  SNPRINTF(stamp_data->date, do_prefix ? "Date %s" : "%s", text);
2007  }
2008  else {
2009  stamp_data->date[0] = '\0';
2010  }
2011 
2012  if (use_dynamic && scene->r.stamp & R_STAMP_MARKER) {
2013  const char *name = BKE_scene_find_last_marker_name(scene, CFRA);
2014 
2015  if (name) {
2016  STRNCPY(text, name);
2017  }
2018  else {
2019  STRNCPY(text, "<none>");
2020  }
2021 
2022  SNPRINTF(stamp_data->marker, do_prefix ? "Marker %s" : "%s", text);
2023  }
2024  else {
2025  stamp_data->marker[0] = '\0';
2026  }
2027 
2028  if (use_dynamic && scene->r.stamp & R_STAMP_TIME) {
2029  const short timecode_style = USER_TIMECODE_SMPTE_FULL;
2031  text, sizeof(text), 0, FRA2TIME(scene->r.cfra), FPS, timecode_style);
2032  SNPRINTF(stamp_data->time, do_prefix ? "Timecode %s" : "%s", text);
2033  }
2034  else {
2035  stamp_data->time[0] = '\0';
2036  }
2037 
2038  if (use_dynamic && scene->r.stamp & R_STAMP_FRAME) {
2039  char fmtstr[32];
2040  int digits = 1;
2041 
2042  if (scene->r.efra > 9) {
2043  digits = integer_digits_i(scene->r.efra);
2044  }
2045 
2046  SNPRINTF(fmtstr, do_prefix ? "Frame %%0%di" : "%%0%di", digits);
2047  SNPRINTF(stamp_data->frame, fmtstr, scene->r.cfra);
2048  }
2049  else {
2050  stamp_data->frame[0] = '\0';
2051  }
2052 
2053  if (scene->r.stamp & R_STAMP_FRAME_RANGE) {
2054  SNPRINTF(stamp_data->frame_range,
2055  do_prefix ? "Frame Range %d:%d" : "%d:%d",
2056  scene->r.sfra,
2057  scene->r.efra);
2058  }
2059  else {
2060  stamp_data->frame_range[0] = '\0';
2061  }
2062 
2063  if (use_dynamic && scene->r.stamp & R_STAMP_CAMERA) {
2064  SNPRINTF(stamp_data->camera,
2065  do_prefix ? "Camera %s" : "%s",
2066  camera ? camera->id.name + 2 : "<none>");
2067  }
2068  else {
2069  stamp_data->camera[0] = '\0';
2070  }
2071 
2072  if (use_dynamic && scene->r.stamp & R_STAMP_CAMERALENS) {
2073  if (camera && camera->type == OB_CAMERA) {
2074  SNPRINTF(text, "%.2f", ((Camera *)camera->data)->lens);
2075  }
2076  else {
2077  STRNCPY(text, "<none>");
2078  }
2079 
2080  SNPRINTF(stamp_data->cameralens, do_prefix ? "Lens %s" : "%s", text);
2081  }
2082  else {
2083  stamp_data->cameralens[0] = '\0';
2084  }
2085 
2086  if (scene->r.stamp & R_STAMP_SCENE) {
2087  SNPRINTF(stamp_data->scene, do_prefix ? "Scene %s" : "%s", scene->id.name + 2);
2088  }
2089  else {
2090  stamp_data->scene[0] = '\0';
2091  }
2092 
2093  if (use_dynamic && scene->r.stamp & R_STAMP_SEQSTRIP) {
2095 
2096  if (seq) {
2097  STRNCPY(text, seq->name + 2);
2098  }
2099  else {
2100  STRNCPY(text, "<none>");
2101  }
2102 
2103  SNPRINTF(stamp_data->strip, do_prefix ? "Strip %s" : "%s", text);
2104  }
2105  else {
2106  stamp_data->strip[0] = '\0';
2107  }
2108 
2109  {
2111  RenderStats *stats = re ? RE_GetStats(re) : NULL;
2112 
2113  if (use_dynamic && stats && (scene->r.stamp & R_STAMP_RENDERTIME)) {
2114  BLI_timecode_string_from_time_simple(text, sizeof(text), stats->lastframetime);
2115 
2116  SNPRINTF(stamp_data->rendertime, do_prefix ? "RenderTime %s" : "%s", text);
2117  }
2118  else {
2119  stamp_data->rendertime[0] = '\0';
2120  }
2121 
2122  if (use_dynamic && stats && (scene->r.stamp & R_STAMP_MEMORY)) {
2123  SNPRINTF(stamp_data->memory, do_prefix ? "Peak Memory %.2fM" : "%.2fM", stats->mem_peak);
2124  }
2125  else {
2126  stamp_data->memory[0] = '\0';
2127  }
2128  }
2129  if (scene->r.stamp & R_STAMP_FRAME_RANGE) {
2130  SNPRINTF(stamp_data->frame_range,
2131  do_prefix ? "Frame Range %d:%d" : "%d:%d",
2132  scene->r.sfra,
2133  scene->r.efra);
2134  }
2135  else {
2136  stamp_data->frame_range[0] = '\0';
2137  }
2138 
2139  if (scene->r.stamp & R_STAMP_HOSTNAME) {
2140  char hostname[500]; /* sizeof(stamp_data->hostname) minus some bytes for a label. */
2141  BLI_hostname_get(hostname, sizeof(hostname));
2142  SNPRINTF(stamp_data->hostname, do_prefix ? "Hostname %s" : "%s", hostname);
2143  }
2144  else {
2145  stamp_data->hostname[0] = '\0';
2146  }
2147 }
2148 
2149 static void stampdata_from_template(StampData *stamp_data,
2150  const Scene *scene,
2151  const StampData *stamp_data_template,
2152  bool do_prefix)
2153 {
2154  if (scene->r.stamp & R_STAMP_FILENAME) {
2155  SNPRINTF(stamp_data->file, do_prefix ? "File %s" : "%s", stamp_data_template->file);
2156  }
2157  else {
2158  stamp_data->file[0] = '\0';
2159  }
2160  if (scene->r.stamp & R_STAMP_NOTE) {
2161  SNPRINTF(stamp_data->note, "%s", stamp_data_template->note);
2162  }
2163  else {
2164  stamp_data->note[0] = '\0';
2165  }
2166  if (scene->r.stamp & R_STAMP_DATE) {
2167  SNPRINTF(stamp_data->date, do_prefix ? "Date %s" : "%s", stamp_data_template->date);
2168  }
2169  else {
2170  stamp_data->date[0] = '\0';
2171  }
2172  if (scene->r.stamp & R_STAMP_MARKER) {
2173  SNPRINTF(stamp_data->marker, do_prefix ? "Marker %s" : "%s", stamp_data_template->marker);
2174  }
2175  else {
2176  stamp_data->marker[0] = '\0';
2177  }
2178  if (scene->r.stamp & R_STAMP_TIME) {
2179  SNPRINTF(stamp_data->time, do_prefix ? "Timecode %s" : "%s", stamp_data_template->time);
2180  }
2181  else {
2182  stamp_data->time[0] = '\0';
2183  }
2184  if (scene->r.stamp & R_STAMP_FRAME) {
2185  SNPRINTF(stamp_data->frame, do_prefix ? "Frame %s" : "%s", stamp_data_template->frame);
2186  }
2187  else {
2188  stamp_data->frame[0] = '\0';
2189  }
2190  if (scene->r.stamp & R_STAMP_CAMERA) {
2191  SNPRINTF(stamp_data->camera, do_prefix ? "Camera %s" : "%s", stamp_data_template->camera);
2192  }
2193  else {
2194  stamp_data->camera[0] = '\0';
2195  }
2196  if (scene->r.stamp & R_STAMP_CAMERALENS) {
2197  SNPRINTF(
2198  stamp_data->cameralens, do_prefix ? "Lens %s" : "%s", stamp_data_template->cameralens);
2199  }
2200  else {
2201  stamp_data->cameralens[0] = '\0';
2202  }
2203  if (scene->r.stamp & R_STAMP_SCENE) {
2204  SNPRINTF(stamp_data->scene, do_prefix ? "Scene %s" : "%s", stamp_data_template->scene);
2205  }
2206  else {
2207  stamp_data->scene[0] = '\0';
2208  }
2209  if (scene->r.stamp & R_STAMP_SEQSTRIP) {
2210  SNPRINTF(stamp_data->strip, do_prefix ? "Strip %s" : "%s", stamp_data_template->strip);
2211  }
2212  else {
2213  stamp_data->strip[0] = '\0';
2214  }
2215  if (scene->r.stamp & R_STAMP_RENDERTIME) {
2216  SNPRINTF(stamp_data->rendertime,
2217  do_prefix ? "RenderTime %s" : "%s",
2218  stamp_data_template->rendertime);
2219  }
2220  else {
2221  stamp_data->rendertime[0] = '\0';
2222  }
2223  if (scene->r.stamp & R_STAMP_MEMORY) {
2224  SNPRINTF(stamp_data->memory, do_prefix ? "Peak Memory %s" : "%s", stamp_data_template->memory);
2225  }
2226  else {
2227  stamp_data->memory[0] = '\0';
2228  }
2229  if (scene->r.stamp & R_STAMP_HOSTNAME) {
2230  SNPRINTF(
2231  stamp_data->hostname, do_prefix ? "Hostname %s" : "%s", stamp_data_template->hostname);
2232  }
2233  else {
2234  stamp_data->hostname[0] = '\0';
2235  }
2236 }
2237 
2239  Object *camera,
2240  const StampData *stamp_data_template,
2241  unsigned char *rect,
2242  float *rectf,
2243  int width,
2244  int height,
2245  int channels)
2246 {
2247  struct StampData stamp_data;
2248  float w, h, pad;
2249  int x, y, y_ofs;
2250  float h_fixed;
2251  const int mono = blf_mono_font_render; /* XXX */
2252  struct ColorManagedDisplay *display;
2253  const char *display_device;
2254 
2255  /* vars for calculating wordwrap */
2256  struct {
2257  struct ResultBLF info;
2258  rctf rect;
2259  } wrap;
2260 
2261  /* this could be an argument if we want to operate on non linear float imbuf's
2262  * for now though this is only used for renders which use scene settings */
2263 
2264 #define TEXT_SIZE_CHECK(str, w, h) \
2265  ((str[0]) && ((void)(h = h_fixed), (w = BLF_width(mono, str, sizeof(str)))))
2266 
2267  /* must enable BLF_WORD_WRAP before using */
2268 #define TEXT_SIZE_CHECK_WORD_WRAP(str, w, h) \
2269  ((str[0]) && (BLF_boundbox_ex(mono, str, sizeof(str), &wrap.rect, &wrap.info), \
2270  (void)(h = h_fixed * wrap.info.lines), \
2271  (w = BLI_rctf_size_x(&wrap.rect))))
2272 
2273 #define BUFF_MARGIN_X 2
2274 #define BUFF_MARGIN_Y 1
2275 
2276  if (!rect && !rectf) {
2277  return;
2278  }
2279 
2280  display_device = scene->display_settings.display_device;
2281  display = IMB_colormanagement_display_get_named(display_device);
2282 
2283  bool do_prefix = (scene->r.stamp & R_STAMP_HIDE_LABELS) == 0;
2284  if (stamp_data_template == NULL) {
2285  stampdata(scene, camera, &stamp_data, do_prefix, true);
2286  }
2287  else {
2288  stampdata_from_template(&stamp_data, scene, stamp_data_template, do_prefix);
2289  }
2290 
2291  /* TODO, do_versions */
2292  if (scene->r.stamp_font_id < 8) {
2293  scene->r.stamp_font_id = 12;
2294  }
2295 
2296  /* set before return */
2297  BLF_size(mono, scene->r.stamp_font_id, 72);
2298  BLF_wordwrap(mono, width - (BUFF_MARGIN_X * 2));
2299 
2300  BLF_buffer(mono, rectf, rect, width, height, channels, display);
2301  BLF_buffer_col(mono, scene->r.fg_stamp);
2302  pad = BLF_width_max(mono);
2303 
2304  /* use 'h_fixed' rather than 'h', aligns better */
2305  h_fixed = BLF_height_max(mono);
2306  y_ofs = -BLF_descender(mono);
2307 
2308  x = 0;
2309  y = height;
2310 
2311  if (TEXT_SIZE_CHECK(stamp_data.file, w, h)) {
2312  /* Top left corner */
2313  y -= h;
2314 
2315  /* also a little of space to the background. */
2316  buf_rectfill_area(rect,
2317  rectf,
2318  width,
2319  height,
2320  scene->r.bg_stamp,
2321  display,
2322  x - BUFF_MARGIN_X,
2323  y - BUFF_MARGIN_Y,
2324  w + BUFF_MARGIN_X,
2325  y + h + BUFF_MARGIN_Y);
2326 
2327  /* and draw the text. */
2328  BLF_position(mono, x, y + y_ofs, 0.0);
2329  BLF_draw_buffer(mono, stamp_data.file, BLF_DRAW_STR_DUMMY_MAX);
2330 
2331  /* the extra pixel for background. */
2332  y -= BUFF_MARGIN_Y * 2;
2333  }
2334 
2335  /* Top left corner, below File */
2336  if (TEXT_SIZE_CHECK(stamp_data.date, w, h)) {
2337  y -= h;
2338 
2339  /* and space for background. */
2340  buf_rectfill_area(rect,
2341  rectf,
2342  width,
2343  height,
2344  scene->r.bg_stamp,
2345  display,
2346  0,
2347  y - BUFF_MARGIN_Y,
2348  w + BUFF_MARGIN_X,
2349  y + h + BUFF_MARGIN_Y);
2350 
2351  BLF_position(mono, x, y + y_ofs, 0.0);
2352  BLF_draw_buffer(mono, stamp_data.date, BLF_DRAW_STR_DUMMY_MAX);
2353 
2354  /* the extra pixel for background. */
2355  y -= BUFF_MARGIN_Y * 2;
2356  }
2357 
2358  /* Top left corner, below File, Date */
2359  if (TEXT_SIZE_CHECK(stamp_data.rendertime, w, h)) {
2360  y -= h;
2361 
2362  /* and space for background. */
2363  buf_rectfill_area(rect,
2364  rectf,
2365  width,
2366  height,
2367  scene->r.bg_stamp,
2368  display,
2369  0,
2370  y - BUFF_MARGIN_Y,
2371  w + BUFF_MARGIN_X,
2372  y + h + BUFF_MARGIN_Y);
2373 
2374  BLF_position(mono, x, y + y_ofs, 0.0);
2376 
2377  /* the extra pixel for background. */
2378  y -= BUFF_MARGIN_Y * 2;
2379  }
2380 
2381  /* Top left corner, below File, Date, Rendertime */
2382  if (TEXT_SIZE_CHECK(stamp_data.memory, w, h)) {
2383  y -= h;
2384 
2385  /* and space for background. */
2386  buf_rectfill_area(rect,
2387  rectf,
2388  width,
2389  height,
2390  scene->r.bg_stamp,
2391  display,
2392  0,
2393  y - BUFF_MARGIN_Y,
2394  w + BUFF_MARGIN_X,
2395  y + h + BUFF_MARGIN_Y);
2396 
2397  BLF_position(mono, x, y + y_ofs, 0.0);
2398  BLF_draw_buffer(mono, stamp_data.memory, BLF_DRAW_STR_DUMMY_MAX);
2399 
2400  /* the extra pixel for background. */
2401  y -= BUFF_MARGIN_Y * 2;
2402  }
2403 
2404  /* Top left corner, below File, Date, Rendertime, Memory */
2405  if (TEXT_SIZE_CHECK(stamp_data.hostname, w, h)) {
2406  y -= h;
2407 
2408  /* and space for background. */
2409  buf_rectfill_area(rect,
2410  rectf,
2411  width,
2412  height,
2413  scene->r.bg_stamp,
2414  display,
2415  0,
2416  y - BUFF_MARGIN_Y,
2417  w + BUFF_MARGIN_X,
2418  y + h + BUFF_MARGIN_Y);
2419 
2420  BLF_position(mono, x, y + y_ofs, 0.0);
2421  BLF_draw_buffer(mono, stamp_data.hostname, BLF_DRAW_STR_DUMMY_MAX);
2422 
2423  /* the extra pixel for background. */
2424  y -= BUFF_MARGIN_Y * 2;
2425  }
2426 
2427  /* Top left corner, below File, Date, Memory, Rendertime, Hostname */
2428  BLF_enable(mono, BLF_WORD_WRAP);
2429  if (TEXT_SIZE_CHECK_WORD_WRAP(stamp_data.note, w, h)) {
2430  y -= h;
2431 
2432  /* and space for background. */
2433  buf_rectfill_area(rect,
2434  rectf,
2435  width,
2436  height,
2437  scene->r.bg_stamp,
2438  display,
2439  0,
2440  y - BUFF_MARGIN_Y,
2441  w + BUFF_MARGIN_X,
2442  y + h + BUFF_MARGIN_Y);
2443 
2444  BLF_position(mono, x, y + y_ofs + (h - h_fixed), 0.0);
2445  BLF_draw_buffer(mono, stamp_data.note, BLF_DRAW_STR_DUMMY_MAX);
2446  }
2447  BLF_disable(mono, BLF_WORD_WRAP);
2448 
2449  x = 0;
2450  y = 0;
2451 
2452  /* Bottom left corner, leaving space for timing */
2453  if (TEXT_SIZE_CHECK(stamp_data.marker, w, h)) {
2454 
2455  /* extra space for background. */
2456  buf_rectfill_area(rect,
2457  rectf,
2458  width,
2459  height,
2460  scene->r.bg_stamp,
2461  display,
2462  x - BUFF_MARGIN_X,
2463  y - BUFF_MARGIN_Y,
2464  w + BUFF_MARGIN_X,
2465  y + h + BUFF_MARGIN_Y);
2466 
2467  /* and pad the text. */
2468  BLF_position(mono, x, y + y_ofs, 0.0);
2469  BLF_draw_buffer(mono, stamp_data.marker, BLF_DRAW_STR_DUMMY_MAX);
2470 
2471  /* space width. */
2472  x += w + pad;
2473  }
2474 
2475  /* Left bottom corner */
2476  if (TEXT_SIZE_CHECK(stamp_data.time, w, h)) {
2477 
2478  /* extra space for background */
2479  buf_rectfill_area(rect,
2480  rectf,
2481  width,
2482  height,
2483  scene->r.bg_stamp,
2484  display,
2485  x - BUFF_MARGIN_X,
2486  y,
2487  x + w + BUFF_MARGIN_X,
2488  y + h + BUFF_MARGIN_Y);
2489 
2490  /* and pad the text. */
2491  BLF_position(mono, x, y + y_ofs, 0.0);
2492  BLF_draw_buffer(mono, stamp_data.time, BLF_DRAW_STR_DUMMY_MAX);
2493 
2494  /* space width. */
2495  x += w + pad;
2496  }
2497 
2498  if (TEXT_SIZE_CHECK(stamp_data.frame, w, h)) {
2499 
2500  /* extra space for background. */
2501  buf_rectfill_area(rect,
2502  rectf,
2503  width,
2504  height,
2505  scene->r.bg_stamp,
2506  display,
2507  x - BUFF_MARGIN_X,
2508  y - BUFF_MARGIN_Y,
2509  x + w + BUFF_MARGIN_X,
2510  y + h + BUFF_MARGIN_Y);
2511 
2512  /* and pad the text. */
2513  BLF_position(mono, x, y + y_ofs, 0.0);
2514  BLF_draw_buffer(mono, stamp_data.frame, BLF_DRAW_STR_DUMMY_MAX);
2515 
2516  /* space width. */
2517  x += w + pad;
2518  }
2519 
2520  if (TEXT_SIZE_CHECK(stamp_data.camera, w, h)) {
2521 
2522  /* extra space for background. */
2523  buf_rectfill_area(rect,
2524  rectf,
2525  width,
2526  height,
2527  scene->r.bg_stamp,
2528  display,
2529  x - BUFF_MARGIN_X,
2530  y - BUFF_MARGIN_Y,
2531  x + w + BUFF_MARGIN_X,
2532  y + h + BUFF_MARGIN_Y);
2533  BLF_position(mono, x, y + y_ofs, 0.0);
2534  BLF_draw_buffer(mono, stamp_data.camera, BLF_DRAW_STR_DUMMY_MAX);
2535 
2536  /* space width. */
2537  x += w + pad;
2538  }
2539 
2540  if (TEXT_SIZE_CHECK(stamp_data.cameralens, w, h)) {
2541 
2542  /* extra space for background. */
2543  buf_rectfill_area(rect,
2544  rectf,
2545  width,
2546  height,
2547  scene->r.bg_stamp,
2548  display,
2549  x - BUFF_MARGIN_X,
2550  y - BUFF_MARGIN_Y,
2551  x + w + BUFF_MARGIN_X,
2552  y + h + BUFF_MARGIN_Y);
2553  BLF_position(mono, x, y + y_ofs, 0.0);
2555  }
2556 
2557  if (TEXT_SIZE_CHECK(stamp_data.scene, w, h)) {
2558 
2559  /* Bottom right corner, with an extra space because blenfont is too strict! */
2560  x = width - w - 2;
2561 
2562  /* extra space for background. */
2563  buf_rectfill_area(rect,
2564  rectf,
2565  width,
2566  height,
2567  scene->r.bg_stamp,
2568  display,
2569  x - BUFF_MARGIN_X,
2570  y - BUFF_MARGIN_Y,
2571  x + w + BUFF_MARGIN_X,
2572  y + h + BUFF_MARGIN_Y);
2573 
2574  /* and pad the text. */
2575  BLF_position(mono, x, y + y_ofs, 0.0);
2576  BLF_draw_buffer(mono, stamp_data.scene, BLF_DRAW_STR_DUMMY_MAX);
2577  }
2578 
2579  if (TEXT_SIZE_CHECK(stamp_data.strip, w, h)) {
2580 
2581  /* Top right corner, with an extra space because blenfont is too strict! */
2582  x = width - w - pad;
2583  y = height - h;
2584 
2585  /* extra space for background. */
2586  buf_rectfill_area(rect,
2587  rectf,
2588  width,
2589  height,
2590  scene->r.bg_stamp,
2591  display,
2592  x - BUFF_MARGIN_X,
2593  y - BUFF_MARGIN_Y,
2594  x + w + BUFF_MARGIN_X,
2595  y + h + BUFF_MARGIN_Y);
2596 
2597  BLF_position(mono, x, y + y_ofs, 0.0);
2598  BLF_draw_buffer(mono, stamp_data.strip, BLF_DRAW_STR_DUMMY_MAX);
2599  }
2600 
2601  /* cleanup the buffer. */
2602  BLF_buffer(mono, NULL, NULL, 0, 0, 0, NULL);
2603  BLF_wordwrap(mono, 0);
2604 
2605 #undef TEXT_SIZE_CHECK
2606 #undef TEXT_SIZE_CHECK_WORD_WRAP
2607 #undef BUFF_MARGIN_X
2608 #undef BUFF_MARGIN_Y
2609 }
2610 
2612  Object *camera,
2613  struct RenderResult *rr,
2614  bool allocate_only)
2615 {
2616  struct StampData *stamp_data;
2617 
2618  if (!(scene && (scene->r.stamp & R_STAMP_ALL)) && !allocate_only) {
2619  return;
2620  }
2621 
2622  if (!rr->stamp_data) {
2623  stamp_data = MEM_callocN(sizeof(StampData), "RenderResult.stamp_data");
2624  }
2625  else {
2626  stamp_data = rr->stamp_data;
2627  }
2628 
2629  if (!allocate_only) {
2630  stampdata(scene, camera, stamp_data, 0, true);
2631  }
2632 
2633  if (!rr->stamp_data) {
2634  rr->stamp_data = stamp_data;
2635  }
2636 }
2637 
2639 {
2640  struct StampData *stamp_data;
2641 
2642  if (!(scene && (scene->r.stamp & R_STAMP_ALL))) {
2643  return NULL;
2644  }
2645 
2646  /* Memory is allocated here (instead of by the caller) so that the caller
2647  * doesn't have to know the size of the StampData struct. */
2648  stamp_data = MEM_callocN(sizeof(StampData), __func__);
2649  stampdata(scene, NULL, stamp_data, 0, false);
2650 
2651  return stamp_data;
2652 }
2653 
2654 static const char *stamp_metadata_fields[] = {
2655  "File",
2656  "Note",
2657  "Date",
2658  "Marker",
2659  "Time",
2660  "Frame",
2661  "FrameRange",
2662  "Camera",
2663  "Lens",
2664  "Scene",
2665  "Strip",
2666  "RenderTime",
2667  "Memory",
2668  "Hostname",
2669  NULL,
2670 };
2671 
2672 /* Check whether the given metadata field name translates to a known field of
2673  * a stamp. */
2674 bool BKE_stamp_is_known_field(const char *field_name)
2675 {
2676  int i = 0;
2677  while (stamp_metadata_fields[i] != NULL) {
2678  if (STREQ(field_name, stamp_metadata_fields[i])) {
2679  return true;
2680  }
2681  i++;
2682  }
2683  return false;
2684 }
2685 
2687  struct StampData *stamp_data,
2689  bool noskip)
2690 {
2691  if ((callback == NULL) || (stamp_data == NULL)) {
2692  return;
2693  }
2694 
2695 #define CALL(member, value_str) \
2696  if (noskip || stamp_data->member[0]) { \
2697  callback(data, value_str, stamp_data->member, sizeof(stamp_data->member)); \
2698  } \
2699  ((void)0)
2700 
2701  /* TODO(sergey): Use stamp_metadata_fields somehow, or make it more generic
2702  * meta information to avoid duplication. */
2703  CALL(file, "File");
2704  CALL(note, "Note");
2705  CALL(date, "Date");
2706  CALL(marker, "Marker");
2707  CALL(time, "Time");
2708  CALL(frame, "Frame");
2709  CALL(frame_range, "FrameRange");
2710  CALL(camera, "Camera");
2711  CALL(cameralens, "Lens");
2712  CALL(scene, "Scene");
2713  CALL(strip, "Strip");
2714  CALL(rendertime, "RenderTime");
2715  CALL(memory, "Memory");
2716  CALL(hostname, "Hostname");
2717 
2718  LISTBASE_FOREACH (StampDataCustomField *, custom_field, &stamp_data->custom_fields) {
2719  if (noskip || custom_field->value[0]) {
2720  callback(data, custom_field->key, custom_field->value, strlen(custom_field->value) + 1);
2721  }
2722  }
2723 
2724 #undef CALL
2725 }
2726 
2727 void BKE_render_result_stamp_data(RenderResult *rr, const char *key, const char *value)
2728 {
2729  StampData *stamp_data;
2730  if (rr->stamp_data == NULL) {
2731  rr->stamp_data = MEM_callocN(sizeof(StampData), "RenderResult.stamp_data");
2732  }
2733  stamp_data = rr->stamp_data;
2735  "StampData Custom Field");
2736  STRNCPY(field->key, key);
2737  field->value = BLI_strdup(value);
2738  BLI_addtail(&stamp_data->custom_fields, field);
2739 }
2740 
2742 {
2743  if (stamp_data == NULL) {
2744  return NULL;
2745  }
2746 
2747  StampData *stamp_datan = MEM_dupallocN(stamp_data);
2748  BLI_duplicatelist(&stamp_datan->custom_fields, &stamp_data->custom_fields);
2749 
2750  LISTBASE_FOREACH (StampDataCustomField *, custom_fieldn, &stamp_datan->custom_fields) {
2751  custom_fieldn->value = MEM_dupallocN(custom_fieldn->value);
2752  }
2753 
2754  return stamp_datan;
2755 }
2756 
2758 {
2759  if (stamp_data == NULL) {
2760  return;
2761  }
2762  LISTBASE_FOREACH (StampDataCustomField *, custom_field, &stamp_data->custom_fields) {
2763  MEM_freeN(custom_field->value);
2764  }
2765  BLI_freelistN(&stamp_data->custom_fields);
2766  MEM_freeN(stamp_data);
2767 }
2768 
2769 /* wrap for callback only */
2770 static void metadata_set_field(void *data, const char *propname, char *propvalue, int UNUSED(len))
2771 {
2772  /* We know it is an ImBuf* because that's what we pass to BKE_stamp_info_callback. */
2773  struct ImBuf *imbuf = data;
2774  IMB_metadata_set_field(imbuf->metadata, propname, propvalue);
2775 }
2776 
2777 static void metadata_get_field(void *data, const char *propname, char *propvalue, int len)
2778 {
2779  /* We know it is an ImBuf* because that's what we pass to BKE_stamp_info_callback. */
2780  struct ImBuf *imbuf = data;
2781  IMB_metadata_get_field(imbuf->metadata, propname, propvalue, len);
2782 }
2783 
2784 void BKE_imbuf_stamp_info(RenderResult *rr, struct ImBuf *ibuf)
2785 {
2786  struct StampData *stamp_data = rr->stamp_data;
2787  IMB_metadata_ensure(&ibuf->metadata);
2788  BKE_stamp_info_callback(ibuf, stamp_data, metadata_set_field, false);
2789 }
2790 
2791 static void metadata_copy_custom_fields(const char *field, const char *value, void *rr_v)
2792 {
2793  if (BKE_stamp_is_known_field(field)) {
2794  return;
2795  }
2796  RenderResult *rr = (RenderResult *)rr_v;
2797  BKE_render_result_stamp_data(rr, field, value);
2798 }
2799 
2801 {
2802  if (rr->stamp_data == NULL) {
2803  rr->stamp_data = MEM_callocN(sizeof(StampData), "RenderResult.stamp_data");
2804  }
2805  struct StampData *stamp_data = rr->stamp_data;
2806  IMB_metadata_ensure(&ibuf->metadata);
2807  BKE_stamp_info_callback(ibuf, stamp_data, metadata_get_field, true);
2808  /* Copy render engine specific settings. */
2810 }
2811 
2813 {
2814  int tot;
2815  if (ibuf->rect_float) {
2816  const float *buf = ibuf->rect_float;
2817  for (tot = ibuf->x * ibuf->y; tot--; buf += 4) {
2818  if (buf[3] < 1.0f) {
2819  return true;
2820  }
2821  }
2822  }
2823  else if (ibuf->rect) {
2824  unsigned char *buf = (unsigned char *)ibuf->rect;
2825  for (tot = ibuf->x * ibuf->y; tot--; buf += 4) {
2826  if (buf[3] != 255) {
2827  return true;
2828  }
2829  }
2830  }
2831 
2832  return false;
2833 }
2834 
2835 /* note: imf->planes is ignored here, its assumed the image channels
2836  * are already set */
2838 {
2839  char imtype = imf->imtype;
2840  char compress = imf->compress;
2841  char quality = imf->quality;
2842 
2843  /* initialize all from image format */
2844  ibuf->foptions.flag = 0;
2845 
2846  if (imtype == R_IMF_IMTYPE_IRIS) {
2847  ibuf->ftype = IMB_FTYPE_IMAGIC;
2848  }
2849 #ifdef WITH_HDR
2850  else if (imtype == R_IMF_IMTYPE_RADHDR) {
2851  ibuf->ftype = IMB_FTYPE_RADHDR;
2852  }
2853 #endif
2854  else if (ELEM(imtype,
2859  R_IMF_IMTYPE_XVID)) {
2860  ibuf->ftype = IMB_FTYPE_PNG;
2861 
2862  if (imtype == R_IMF_IMTYPE_PNG) {
2863  if (imf->depth == R_IMF_CHAN_DEPTH_16) {
2864  ibuf->foptions.flag |= PNG_16BIT;
2865  }
2866 
2867  ibuf->foptions.quality = compress;
2868  }
2869  }
2870 #ifdef WITH_DDS
2871  else if (imtype == R_IMF_IMTYPE_DDS) {
2872  ibuf->ftype = IMB_FTYPE_DDS;
2873  }
2874 #endif
2875  else if (imtype == R_IMF_IMTYPE_BMP) {
2876  ibuf->ftype = IMB_FTYPE_BMP;
2877  }
2878 #ifdef WITH_TIFF
2879  else if (imtype == R_IMF_IMTYPE_TIFF) {
2880  ibuf->ftype = IMB_FTYPE_TIF;
2881 
2882  if (imf->depth == R_IMF_CHAN_DEPTH_16) {
2883  ibuf->foptions.flag |= TIF_16BIT;
2884  }
2885  if (imf->tiff_codec == R_IMF_TIFF_CODEC_NONE) {
2886  ibuf->foptions.flag |= TIF_COMPRESS_NONE;
2887  }
2888  else if (imf->tiff_codec == R_IMF_TIFF_CODEC_DEFLATE) {
2889  ibuf->foptions.flag |= TIF_COMPRESS_DEFLATE;
2890  }
2891  else if (imf->tiff_codec == R_IMF_TIFF_CODEC_LZW) {
2892  ibuf->foptions.flag |= TIF_COMPRESS_LZW;
2893  }
2894  else if (imf->tiff_codec == R_IMF_TIFF_CODEC_PACKBITS) {
2895  ibuf->foptions.flag |= TIF_COMPRESS_PACKBITS;
2896  }
2897  }
2898 #endif
2899 #ifdef WITH_OPENEXR
2900  else if (ELEM(imtype, R_IMF_IMTYPE_OPENEXR, R_IMF_IMTYPE_MULTILAYER)) {
2901  ibuf->ftype = IMB_FTYPE_OPENEXR;
2902  if (imf->depth == R_IMF_CHAN_DEPTH_16) {
2903  ibuf->foptions.flag |= OPENEXR_HALF;
2904  }
2905  ibuf->foptions.flag |= (imf->exr_codec & OPENEXR_COMPRESS);
2906 
2907  if (!(imf->flag & R_IMF_FLAG_ZBUF)) {
2908  /* Signal for exr saving. */
2909  IMB_freezbuffloatImBuf(ibuf);
2910  }
2911  }
2912 #endif
2913 #ifdef WITH_CINEON
2914  else if (imtype == R_IMF_IMTYPE_CINEON) {
2915  ibuf->ftype = IMB_FTYPE_CINEON;
2916  if (imf->cineon_flag & R_IMF_CINEON_FLAG_LOG) {
2917  ibuf->foptions.flag |= CINEON_LOG;
2918  }
2919  if (imf->depth == R_IMF_CHAN_DEPTH_16) {
2920  ibuf->foptions.flag |= CINEON_16BIT;
2921  }
2922  else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
2923  ibuf->foptions.flag |= CINEON_12BIT;
2924  }
2925  else if (imf->depth == R_IMF_CHAN_DEPTH_10) {
2926  ibuf->foptions.flag |= CINEON_10BIT;
2927  }
2928  }
2929  else if (imtype == R_IMF_IMTYPE_DPX) {
2930  ibuf->ftype = IMB_FTYPE_DPX;
2931  if (imf->cineon_flag & R_IMF_CINEON_FLAG_LOG) {
2932  ibuf->foptions.flag |= CINEON_LOG;
2933  }
2934  if (imf->depth == R_IMF_CHAN_DEPTH_16) {
2935  ibuf->foptions.flag |= CINEON_16BIT;
2936  }
2937  else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
2938  ibuf->foptions.flag |= CINEON_12BIT;
2939  }
2940  else if (imf->depth == R_IMF_CHAN_DEPTH_10) {
2941  ibuf->foptions.flag |= CINEON_10BIT;
2942  }
2943  }
2944 #endif
2945  else if (imtype == R_IMF_IMTYPE_TARGA) {
2946  ibuf->ftype = IMB_FTYPE_TGA;
2947  }
2948  else if (imtype == R_IMF_IMTYPE_RAWTGA) {
2949  ibuf->ftype = IMB_FTYPE_TGA;
2950  ibuf->foptions.flag = RAWTGA;
2951  }
2952 #ifdef WITH_OPENJPEG
2953  else if (imtype == R_IMF_IMTYPE_JP2) {
2954  if (quality < 10) {
2955  quality = 90;
2956  }
2957  ibuf->ftype = IMB_FTYPE_JP2;
2958  ibuf->foptions.quality = quality;
2959 
2960  if (imf->depth == R_IMF_CHAN_DEPTH_16) {
2961  ibuf->foptions.flag |= JP2_16BIT;
2962  }
2963  else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
2964  ibuf->foptions.flag |= JP2_12BIT;
2965  }
2966 
2967  if (imf->jp2_flag & R_IMF_JP2_FLAG_YCC) {
2968  ibuf->foptions.flag |= JP2_YCC;
2969  }
2970 
2971  if (imf->jp2_flag & R_IMF_JP2_FLAG_CINE_PRESET) {
2972  ibuf->foptions.flag |= JP2_CINE;
2973  if (imf->jp2_flag & R_IMF_JP2_FLAG_CINE_48) {
2974  ibuf->foptions.flag |= JP2_CINE_48FPS;
2975  }
2976  }
2977 
2978  if (imf->jp2_codec == R_IMF_JP2_CODEC_JP2) {
2979  ibuf->foptions.flag |= JP2_JP2;
2980  }
2981  else if (imf->jp2_codec == R_IMF_JP2_CODEC_J2K) {
2982  ibuf->foptions.flag |= JP2_J2K;
2983  }
2984  else {
2985  BLI_assert(!"Unsupported jp2 codec was specified in im_format->jp2_codec");
2986  }
2987  }
2988 #endif
2989  else {
2990  /* R_IMF_IMTYPE_JPEG90, etc. default we save jpegs */
2991  if (quality < 10) {
2992  quality = 90;
2993  }
2994  ibuf->ftype = IMB_FTYPE_JPG;
2995  ibuf->foptions.quality = quality;
2996  }
2997 }
2998 
2999 int BKE_imbuf_write(ImBuf *ibuf, const char *name, const ImageFormatData *imf)
3000 {
3001  BKE_imbuf_write_prepare(ibuf, imf);
3002 
3003  BLI_make_existing_file(name);
3004 
3005  const bool ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
3006  if (ok == 0) {
3007  perror(name);
3008  }
3009 
3010  return ok;
3011 }
3012 
3013 /* same as BKE_imbuf_write() but crappy workaround not to permanently modify
3014  * _some_, values in the imbuf */
3015 int BKE_imbuf_write_as(ImBuf *ibuf, const char *name, ImageFormatData *imf, const bool save_copy)
3016 {
3017  ImBuf ibuf_back = *ibuf;
3018  int ok;
3019 
3020  /* all data is rgba anyway,
3021  * this just controls how to save for some formats */
3022  ibuf->planes = imf->planes;
3023 
3024  ok = BKE_imbuf_write(ibuf, name, imf);
3025 
3026  if (save_copy) {
3027  /* note that we are not restoring _all_ settings */
3028  ibuf->planes = ibuf_back.planes;
3029  ibuf->ftype = ibuf_back.ftype;
3030  ibuf->foptions = ibuf_back.foptions;
3031  }
3032 
3033  return ok;
3034 }
3035 
3037  struct RenderResult *rr,
3038  ImBuf *ibuf,
3039  const char *name,
3040  const struct ImageFormatData *imf)
3041 {
3042  if (scene && scene->r.stamp & R_STAMP_ALL) {
3043  BKE_imbuf_stamp_info(rr, ibuf);
3044  }
3045 
3046  return BKE_imbuf_write(ibuf, name, imf);
3047 }
3048 
3049 static void do_makepicstring(char *string,
3050  const char *base,
3051  const char *relbase,
3052  int frame,
3053  const char imtype,
3054  const ImageFormatData *im_format,
3055  const bool use_ext,
3056  const bool use_frames,
3057  const char *suffix)
3058 {
3059  if (string == NULL) {
3060  return;
3061  }
3062  BLI_strncpy(string, base, FILE_MAX - 10); /* weak assumption */
3063  BLI_path_abs(string, relbase);
3064 
3065  if (use_frames) {
3066  BLI_path_frame(string, frame, 4);
3067  }
3068 
3069  if (suffix) {
3070  BLI_path_suffix(string, FILE_MAX, suffix, "");
3071  }
3072 
3073  if (use_ext) {
3074  do_add_image_extension(string, imtype, im_format);
3075  }
3076 }
3077 
3079  const char *base,
3080  const char *relbase,
3081  int frame,
3082  const ImageFormatData *im_format,
3083  const bool use_ext,
3084  const bool use_frames,
3085  const char *suffix)
3086 {
3088  string, base, relbase, frame, im_format->imtype, im_format, use_ext, use_frames, suffix);
3089 }
3090 
3091 void BKE_image_path_from_imtype(char *string,
3092  const char *base,
3093  const char *relbase,
3094  int frame,
3095  const char imtype,
3096  const bool use_ext,
3097  const bool use_frames,
3098  const char *suffix)
3099 {
3100  do_makepicstring(string, base, relbase, frame, imtype, NULL, use_ext, use_frames, suffix);
3101 }
3102 
3103 struct anim *openanim_noload(const char *name,
3104  int flags,
3105  int streamindex,
3106  char colorspace[IMA_MAX_SPACE])
3107 {
3108  struct anim *anim;
3109 
3111  return anim;
3112 }
3113 
3114 /* used by sequencer too */
3115 struct anim *openanim(const char *name, int flags, int streamindex, char colorspace[IMA_MAX_SPACE])
3116 {
3117  struct anim *anim;
3118  struct ImBuf *ibuf;
3119 
3120  anim = IMB_open_anim(name, flags, streamindex, colorspace);
3121  if (anim == NULL) {
3122  return NULL;
3123  }
3124 
3126  if (ibuf == NULL) {
3127  if (BLI_exists(name)) {
3128  printf("not an anim: %s\n", name);
3129  }
3130  else {
3131  printf("anim file doesn't exist: %s\n", name);
3132  }
3134  return NULL;
3135  }
3136  IMB_freeImBuf(ibuf);
3137 
3138  return anim;
3139 }
3140 
3141 /* ************************* New Image API *************** */
3142 
3143 /* Notes about Image storage
3144  * - packedfile
3145  * -> written in .blend
3146  * - filename
3147  * -> written in .blend
3148  * - movie
3149  * -> comes from packedfile or filename
3150  * - renderresult
3151  * -> comes from packedfile or filename
3152  * - listbase
3153  * -> ibufs from exrhandle
3154  * - flipbook array
3155  * -> ibufs come from movie, temporary renderresult or sequence
3156  * - ibuf
3157  * -> comes from packedfile or filename or generated
3158  */
3159 
3160 /* forces existence of 1 Image for renderout or nodes, returns Image */
3161 /* name is only for default, when making new one */
3162 Image *BKE_image_ensure_viewer(Main *bmain, int type, const char *name)
3163 {
3164  Image *ima;
3165 
3166  for (ima = bmain->images.first; ima; ima = ima->id.next) {
3167  if (ima->source == IMA_SRC_VIEWER) {
3168  if (ima->type == type) {
3169  break;
3170  }
3171  }
3172  }
3173 
3174  if (ima == NULL) {
3175  ima = image_alloc(bmain, name, IMA_SRC_VIEWER, type);
3176  }
3177 
3178  /* happens on reload, imagewindow cannot be image user when hidden*/
3179  if (ima->id.us == 0) {
3180  id_us_ensure_real(&ima->id);
3181  }
3182 
3183  return ima;
3184 }
3185 
3186 static void image_viewer_create_views(const RenderData *rd, Image *ima)
3187 {
3188  if ((rd->scemode & R_MULTIVIEW) == 0) {
3189  image_add_view(ima, "", "");
3190  }
3191  else {
3192  SceneRenderView *srv;
3193  for (srv = rd->views.first; srv; srv = srv->next) {
3194  if (BKE_scene_multiview_is_render_view_active(rd, srv) == false) {
3195  continue;
3196  }
3197  image_add_view(ima, srv->name, "");
3198  }
3199  }
3200 }
3201 
3202 /* Reset the image cache and views when the Viewer Nodes views don't match the scene views */
3204 {
3205  bool do_reset;
3206  const bool is_multiview = (rd->scemode & R_MULTIVIEW) != 0;
3207 
3209 
3211  iuser->flag &= ~IMA_SHOW_STEREO;
3212  }
3213 
3214  /* see if all scene render views are in the image view list */
3215  do_reset = (BKE_scene_multiview_num_views_get(rd) != BLI_listbase_count(&ima->views));
3216 
3217  /* multiview also needs to be sure all the views are synced */
3218  if (is_multiview && !do_reset) {
3219  SceneRenderView *srv;
3220  ImageView *iv;
3221 
3222  for (iv = ima->views.first; iv; iv = iv->next) {
3223  srv = BLI_findstring(&rd->views, iv->name, offsetof(SceneRenderView, name));
3224  if ((srv == NULL) || (BKE_scene_multiview_is_render_view_active(rd, srv) == false)) {
3225  do_reset = true;
3226  break;
3227  }
3228  }
3229  }
3230 
3231  if (do_reset) {
3233 
3235  BKE_image_free_views(ima);
3236 
3237  /* add new views */
3238  image_viewer_create_views(rd, ima);
3239 
3241  }
3242 
3244 }
3245 
3247  bNodeTree *ntree,
3248  ID *id,
3249  void *customdata,
3250  void callback(Image *ima, ID *iuser_id, ImageUser *iuser, void *customdata))
3251 {
3252  switch (ntree->type) {
3253  case NTREE_SHADER:
3255  if (node->id) {
3256  if (node->type == SH_NODE_TEX_IMAGE) {
3257  NodeTexImage *tex = node->storage;
3258  Image *ima = (Image *)node->id;
3259  callback(ima, id, &tex->iuser, customdata);
3260  }
3261  if (node->type == SH_NODE_TEX_ENVIRONMENT) {
3262  NodeTexImage *tex = node->storage;
3263  Image *ima = (Image *)node->id;
3264  callback(ima, id, &tex->iuser, customdata);
3265  }
3266  }
3267  }
3268  break;
3269  case NTREE_TEXTURE:
3271  if (node->id && node->type == TEX_NODE_IMAGE) {
3272  Image *ima = (Image *)node->id;
3273  ImageUser *iuser = node->storage;
3274  callback(ima, id, iuser, customdata);
3275  }
3276  }
3277  break;
3278  case NTREE_COMPOSIT:
3280  if (node->id && node->type == CMP_NODE_IMAGE) {
3281  Image *ima = (Image *)node->id;
3282  ImageUser *iuser = node->storage;
3283  callback(ima, id, iuser, customdata);
3284  }
3285  }
3286  break;
3287  }
3288 }
3289 
3291  ID *id,
3292  bool skip_nested_nodes,
3293  void *customdata,
3294  void callback(Image *ima, ID *iuser_id, ImageUser *iuser, void *customdata))
3295 {
3296  switch (GS(id->name)) {
3297  case ID_OB: {
3298  Object *ob = (Object *)id;
3299  if (ob->empty_drawtype == OB_EMPTY_IMAGE && ob->data) {
3300  callback(ob->data, &ob->id, ob->iuser, customdata);
3301  }
3302  break;
3303  }
3304  case ID_MA: {
3305  Material *ma = (Material *)id;
3306  if (ma->nodetree && ma->use_nodes && !skip_nested_nodes) {
3307  image_walk_ntree_all_users(ma->nodetree, &ma->id, customdata, callback);
3308  }
3309  break;
3310  }
3311  case ID_LA: {
3312  Light *light = (Light *)id;
3313  if (light->nodetree && light->use_nodes && !skip_nested_nodes) {
3314  image_walk_ntree_all_users(light->nodetree, &light->id, customdata, callback);
3315  }
3316  break;
3317  }
3318  case ID_WO: {
3319  World *world = (World *)id;
3320  if (world->nodetree && world->use_nodes && !skip_nested_nodes) {
3322  }
3323  break;
3324  }
3325  case ID_TE: {
3326  Tex *tex = (Tex *)id;
3327  if (tex->type == TEX_IMAGE && tex->ima) {
3328  callback(tex->ima, &tex->id, &tex->iuser, customdata);
3329  }
3330  if (tex->nodetree && tex->use_nodes && !skip_nested_nodes) {
3332  }
3333  break;
3334  }
3335  case ID_NT: {
3336  bNodeTree *ntree = (bNodeTree *)id;
3338  break;
3339  }
3340  case ID_CA: {
3341  Camera *cam = (Camera *)id;
3342  LISTBASE_FOREACH (CameraBGImage *, bgpic, &cam->bg_images) {
3343  callback(bgpic->ima, NULL, &bgpic->iuser, customdata);
3344  }
3345  break;
3346  }
3347  case ID_WM: {
3348  wmWindowManager *wm = (wmWindowManager *)id;
3349  LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
3350  const bScreen *screen = BKE_workspace_active_screen_get(win->workspace_hook);
3351 
3352  LISTBASE_FOREACH (ScrArea *, area, &screen->areabase) {
3353  if (area->spacetype == SPACE_IMAGE) {
3354  SpaceImage *sima = area->spacedata.first;
3355  callback(sima->image, NULL, &sima->iuser, customdata);
3356  }
3357  }
3358  }
3359  break;
3360  }
3361  case ID_SCE: {
3362  Scene *scene = (Scene *)id;
3363  if (scene->nodetree && scene->use_nodes && !skip_nested_nodes) {
3365  }
3366  break;
3367  }
3368  case ID_SIM: {
3369  Simulation *simulation = (Simulation *)id;
3371  break;
3372  }
3373  default:
3374  break;
3375  }
3376 }
3377 
3379  const Main *mainp,
3380  void *customdata,
3381  void callback(Image *ima, ID *iuser_id, ImageUser *iuser, void *customdata))
3382 {
3383  for (Scene *scene = mainp->scenes.first; scene; scene = scene->id.next) {
3384  image_walk_id_all_users(&scene->id, false, customdata, callback);
3385  }
3386 
3387  for (Object *ob = mainp->objects.first; ob; ob = ob->id.next) {
3388  image_walk_id_all_users(&ob->id, false, customdata, callback);
3389  }
3390 
3391  for (bNodeTree *ntree = mainp->nodetrees.first; ntree; ntree = ntree->id.next) {
3392  image_walk_id_all_users(&ntree->id, false, customdata, callback);
3393  }
3394 
3395  for (Material *ma = mainp->materials.first; ma; ma = ma->id.next) {
3396  image_walk_id_all_users(&ma->id, false, customdata, callback);
3397  }
3398 
3399  for (Light *light = mainp->materials.first; light; light = light->id.next) {
3400  image_walk_id_all_users(&light->id, false, customdata, callback);
3401  }
3402 
3403  for (World *world = mainp->materials.first; world; world = world->id.next) {
3404  image_walk_id_all_users(&world->id, false, customdata, callback);
3405  }
3406 
3407  for (Tex *tex = mainp->textures.first; tex; tex = tex->id.next) {
3408  image_walk_id_all_users(&tex->id, false, customdata, callback);
3409  }
3410 
3411  for (Camera *cam = mainp->cameras.first; cam; cam = cam->id.next) {
3412  image_walk_id_all_users(&cam->id, false, customdata, callback);
3413  }
3414 
3415  for (wmWindowManager *wm = mainp->wm.first; wm; wm = wm->id.next) { /* only 1 wm */
3416  image_walk_id_all_users(&wm->id, false, customdata, callback);
3417  }
3418 }
3419 
3420 static void image_tag_frame_recalc(Image *ima, ID *iuser_id, ImageUser *iuser, void *customdata)
3421 {
3422  Image *changed_image = customdata;
3423 
3424  if (ima == changed_image && BKE_image_is_animated(ima)) {
3425  iuser->flag |= IMA_NEED_FRAME_RECALC;
3426  iuser->ok = 1;
3427 
3428  if (iuser_id) {
3429  /* Must copy image user changes to CoW datablock. */
3431  }
3432  }
3433 }
3434 
3435 static void image_tag_reload(Image *ima, ID *iuser_id, ImageUser *iuser, void *customdata)
3436 {
3437  Image *changed_image = customdata;
3438 
3439  if (ima == changed_image) {
3440  iuser->ok = 1;
3441  if (iuser->scene) {
3442  image_update_views_format(ima, iuser);
3443  }
3444  if (iuser_id) {
3445  /* Must copy image user changes to CoW datablock. */
3447  }
3448  }
3449 }
3450 
3452 {
3453  memset(iuser, 0, sizeof(ImageUser));
3454  iuser->ok = 1;
3455  iuser->frames = 100;
3456  iuser->sfra = 1;
3457 }
3458 
3460 {
3461  RenderResult *rr = ima->rr;
3462 
3463  iuser->multi_index = 0;
3464  iuser->layer = iuser->pass = iuser->view = 0;
3465 
3466  if (rr) {
3467  BKE_image_multilayer_index(rr, iuser);
3468  }
3469 }
3470 
3471 static void image_free_tile(Image *ima, ImageTile *tile)
3472 {
3473  for (int i = 0; i < TEXTARGET_COUNT; i++) {
3474  /* Only two textures depends on all tiles, so if this is a secondary tile we can keep the other
3475  * two. */
3476  if (tile != ima->tiles.first && !(ELEM(i, TEXTARGET_2D_ARRAY, TEXTARGET_TILE_MAPPING))) {
3477  continue;
3478  }
3479 
3480  for (int eye = 0; eye < 2; eye++) {
3481  if (ima->gputexture[i][eye] != NULL) {
3482  GPU_texture_free(ima->gputexture[i][eye]);
3483  ima->gputexture[i][eye] = NULL;
3484  }
3485  }
3486  }
3487 
3488  if (BKE_image_is_multiview(ima)) {
3489  const int totviews = BLI_listbase_count(&ima->views);
3490  for (int i = 0; i < totviews; i++) {
3491  image_remove_ibuf(ima, i, tile->tile_number);
3492  }
3493  }
3494  else {
3495  image_remove_ibuf(ima, 0, tile->tile_number);
3496  }
3497 }
3498 
3499 void BKE_image_signal(Main *bmain, Image *ima, ImageUser *iuser, int signal)
3500 {
3501  if (ima == NULL) {
3502  return;
3503  }
3504 
3506 
3507  switch (signal) {
3508  case IMA_SIGNAL_FREE:
3510 
3511  if (iuser) {
3512  iuser->ok = 1;
3513  if (iuser->scene) {
3514  image_update_views_format(ima, iuser);
3515  }
3516  }
3517  break;
3518  case IMA_SIGNAL_SRC_CHANGE:
3519  if (ima->type == IMA_TYPE_UV_TEST) {
3520  if (ima->source != IMA_SRC_GENERATED) {
3521  ima->type = IMA_TYPE_IMAGE;
3522  }
3523  }
3524 
3525  if (ima->source == IMA_SRC_GENERATED) {
3526  if (ima->gen_x == 0 || ima->gen_y == 0) {
3528  if (ibuf) {
3529  ima->gen_x = ibuf->x;
3530  ima->gen_y = ibuf->y;
3531  IMB_freeImBuf(ibuf);
3532  }
3533  }
3534 
3535  /* Changing source type to generated will likely change file format
3536  * used by generated image buffer. Saving different file format to
3537  * the old name might confuse other applications.
3538  *
3539  * Here we ensure original image path wouldn't be used when saving
3540  * generated image.
3541  */
3542  ima->filepath[0] = '\0';
3543  }
3544 
3545  if (ima->source != IMA_SRC_TILED) {
3546  /* Free all but the first tile. */
3547  ImageTile *base_tile = BKE_image_get_tile(ima, 0);
3548  BLI_assert(base_tile == ima->tiles.first);
3549  for (ImageTile *tile = base_tile->next, *tile_next; tile; tile = tile_next) {
3550  tile_next = tile->next;
3551  image_free_tile(ima, tile);
3552  MEM_freeN(tile);
3553  }
3554  base_tile->next = NULL;
3555  ima->tiles.last = base_tile;
3556  }
3557 
3558  /* image buffers for non-sequence multilayer will share buffers with RenderResult,
3559  * however sequence multilayer will own buffers. Such logic makes switching from
3560  * single multilayer file to sequence completely unstable
3561  * since changes in nodes seems this workaround isn't needed anymore, all sockets
3562  * are nicely detecting anyway, but freeing buffers always here makes multilayer
3563  * sequences behave stable
3564  */
3566 
3567  LISTBASE_FOREACH (ImageTile *, tile, &ima->tiles) {
3568  tile->ok = 1;
3569  }
3570 
3571  if (iuser) {
3572  image_tag_frame_recalc(ima, NULL, iuser, ima);
3573  }
3575 
3576  break;
3577 
3578  case IMA_SIGNAL_RELOAD:
3579  /* try to repack file */
3580  if (BKE_image_has_packedfile(ima)) {
3581  const int totfiles = image_num_files(ima);
3582 
3583  if (totfiles != BLI_listbase_count_at_most(&ima->packedfiles, totfiles + 1)) {
3584  /* in case there are new available files to be loaded */
3586  BKE_image_packfiles(NULL, ima, ID_BLEND_PATH(bmain, &ima->id));
3587  }
3588  else {
3589  ImagePackedFile *imapf;
3590  for (imapf = ima->packedfiles.first; imapf; imapf = imapf->next) {
3591  PackedFile *pf;
3592  pf = BKE_packedfile_new(NULL, imapf->filepath, ID_BLEND_PATH(bmain, &ima->id));
3593  if (pf) {
3595  imapf->packedfile = pf;
3596  }
3597  else {
3598  printf("ERROR: Image \"%s\" not available. Keeping packed image\n", imapf->filepath);
3599  }
3600  }
3601  }
3602 
3603  if (BKE_image_has_packedfile(ima)) {
3605  }
3606  }
3607  else {
3609  }
3610 
3611  if (iuser) {
3612  image_tag_reload(ima, NULL, iuser, ima);
3613  }
3615  break;
3617  if (iuser) {
3618  iuser->ok = 1;
3620  if (ima->type == IMA_TYPE_MULTILAYER) {
3621  BKE_image_init_imageuser(ima, iuser);
3622  }
3623  }
3624  }
3625  break;
3628 
3629  LISTBASE_FOREACH (ImageTile *, tile, &ima->tiles) {
3630  tile->ok = 1;
3631  }
3632 
3633  if (iuser) {
3634  iuser->ok = 1;
3635  }
3636 
3637  break;
3638  }
3639 
3641 
3642  /* don't use notifiers because they are not 100% sure to succeeded
3643  * this also makes sure all scenes are accounted for. */
3644  {
3645  Scene *scene;
3646  for (scene = bmain->scenes.first; scene; scene = scene->id.next) {
3647  if (scene->nodetree) {
3648  nodeUpdateID(scene->nodetree, &ima->id);
3649  }
3650  }
3651  }
3652 }
3653 
3654 /* return renderpass for a given pass index and active view */
3655 /* fallback to available if there are missing passes for active view */
3657  const int pass,
3658  const int view,
3659  int *r_passindex)
3660 {
3661  RenderPass *rpass_ret = NULL;
3662  RenderPass *rpass;
3663 
3664  int rp_index = 0;
3665  const char *rp_name = "";
3666 
3667  for (rpass = rl->passes.first; rpass; rpass = rpass->next, rp_index++) {
3668  if (rp_index == pass) {
3669  rpass_ret = rpass;
3670  if (view == 0) {
3671  /* no multiview or left eye */
3672  break;
3673  }
3674 
3675  rp_name = rpass->name;
3676  }
3677  /* multiview */
3678  else if (rp_name[0] && STREQ(rpass->name, rp_name) && (rpass->view_id == view)) {
3679  rpass_ret = rpass;
3680  break;
3681  }
3682  }
3683 
3684  /* fallback to the first pass in the layer */
3685  if (rpass_ret == NULL) {
3686  rp_index = 0;
3687  rpass_ret = rl->passes.first;
3688  }
3689 
3690  if (r_passindex) {
3691  *r_passindex = (rpass == rpass_ret ? rp_index : pass);
3692  }
3693 
3694  return rpass_ret;
3695 }
3696 
3697 void BKE_image_get_tile_label(Image *ima, ImageTile *tile, char *label, int len_label)
3698 {
3699  label[0] = '\0';
3700  if (ima == NULL || tile == NULL) {
3701  return;
3702  }
3703 
3704  if (tile->label[0]) {
3705  BLI_strncpy(label, tile->label, len_label);
3706  }
3707  else {
3708  BLI_snprintf(label, len_label, "%d", tile->tile_number);
3709  }
3710 }
3711 
3712 ImageTile *BKE_image_add_tile(struct Image *ima, int tile_number, const char *label)
3713 {
3714  if (ima->source != IMA_SRC_TILED) {
3715  return NULL;
3716  }
3717 
3718  if (tile_number < 1001 || tile_number > IMA_UDIM_MAX) {
3719  return NULL;
3720  }
3721 
3722  /* Search the first tile that has a higher number.
3723  * We then insert before that to keep the list sorted. */
3724  ImageTile *next_tile;
3725  for (next_tile = ima->tiles.first; next_tile; next_tile = next_tile->next) {
3726  if (next_tile->tile_number == tile_number) {
3727  /* Tile already exists. */
3728  return NULL;
3729  }
3730  if (next_tile->tile_number > tile_number) {
3731  break;
3732  }
3733  }
3734 
3735  ImageTile *tile = MEM_callocN(sizeof(ImageTile), "image new tile");
3736  tile->ok = IMA_OK;
3737  tile->tile_number = tile_number;
3738 
3739  if (next_tile) {
3740  BLI_insertlinkbefore(&ima->tiles, next_tile, tile);
3741  }
3742  else {
3743  BLI_addtail(&ima->tiles, tile);
3744  }
3745 
3746  if (label) {
3747  BLI_strncpy(tile->label, label, sizeof(tile->label));
3748  }
3749 
3750  for (int eye = 0; eye < 2; eye++) {
3751  /* Reallocate GPU tile array. */
3752  if (ima->gputexture[TEXTARGET_2D_ARRAY][eye] != NULL) {
3754  ima->gputexture[TEXTARGET_2D_ARRAY][eye] = NULL;
3755  }
3756  if (ima->gputexture[TEXTARGET_TILE_MAPPING][eye] != NULL) {
3758  ima->gputexture[TEXTARGET_TILE_MAPPING][eye] = NULL;
3759  }
3760  }
3761 
3762  return tile;
3763 }
3764 
3765 bool BKE_image_remove_tile(struct Image *ima, ImageTile *tile)
3766 {
3767  if (ima == NULL || tile == NULL || ima->source != IMA_SRC_TILED) {
3768  return false;
3769  }
3770 
3771  if (tile == ima->tiles.first) {
3772  /* Can't remove first tile. */
3773  return false;
3774  }
3775 
3776  image_free_tile(ima, tile);
3777  BLI_remlink(&ima->tiles, tile);
3778  MEM_freeN(tile);
3779 
3780  return true;
3781 }
3782 
3783 bool BKE_image_fill_tile(struct Image *ima,
3784  ImageTile *tile,
3785  int width,
3786  int height,
3787  const float color[4],
3788  int gen_type,
3789  int planes,
3790  bool is_float)
3791 {
3792  if (ima == NULL || tile == NULL || ima->source != IMA_SRC_TILED) {
3793  return false;
3794  }
3795 
3796  image_free_tile(ima, tile);
3797 
3798  ImBuf *tile_ibuf = add_ibuf_size(
3799  width, height, ima->filepath, planes, is_float, gen_type, color, &ima->colorspace_settings);
3800 
3801  if (tile_ibuf != NULL) {
3802  image_assign_ibuf(ima, tile_ibuf, 0, tile->tile_number);
3803  BKE_image_release_ibuf(ima, tile_ibuf, NULL);
3804  tile->ok = IMA_OK;
3805  return true;
3806  }
3807  return false;
3808 }
3809 
3810 /* if layer or pass changes, we need an index for the imbufs list */
3811 /* note it is called for rendered results, but it doesn't use the index! */
3812 /* and because rendered results use fake layer/passes, don't correct for wrong indices here */
3814 {
3815  RenderLayer *rl;
3816  RenderPass *rpass = NULL;
3817 
3818  if (rr == NULL) {
3819  return NULL;
3820  }
3821 
3822  if (iuser) {
3823  short index = 0, rv_index, rl_index = 0;
3824  bool is_stereo = (iuser->flag & IMA_SHOW_STEREO) && RE_RenderResult_is_stereo(rr);
3825 
3826  rv_index = is_stereo ? iuser->multiview_eye : iuser->view;
3827  if (RE_HasCombinedLayer(rr)) {
3828  rl_index += 1;
3829  }
3830 
3831  for (rl = rr->layers.first; rl; rl = rl->next, rl_index++) {
3832  if (iuser->layer == rl_index) {
3833  int rp_index;
3834  rpass = image_render_pass_get(rl, iuser->pass, rv_index, &rp_index);
3835  iuser->multi_index = index + rp_index;
3836  break;
3837  }
3838 
3839  index += BLI_listbase_count(&rl->passes);
3840  }
3841  }
3842 
3843  return rpass;
3844 }
3845 
3847 {
3848  if (iuser) {
3849  bool is_stereo = BKE_image_is_stereo(ima) && (iuser->flag & IMA_SHOW_STEREO);
3850  if (is_stereo) {
3851  iuser->multi_index = iuser->multiview_eye;
3852  }
3853  else {
3854  if ((iuser->view < 0) ||
3855  (iuser->view >= BLI_listbase_count_at_most(&ima->views, iuser->view + 1))) {
3856  iuser->multi_index = iuser->view = 0;
3857  }
3858  else {
3859  iuser->multi_index = iuser->view;
3860  }
3861  }
3862  }
3863 }
3864 
3865 /* if layer or pass changes, we need an index for the imbufs list */
3866 /* note it is called for rendered results, but it doesn't use the index! */
3867 /* and because rendered results use fake layer/passes, don't correct for wrong indices here */
3869 {
3871  if (ima->type == IMA_TYPE_MULTILAYER) {
3872  return true;
3873  }
3874  }
3875  else if (ima->source == IMA_SRC_VIEWER) {
3876  if (ima->type == IMA_TYPE_R_RESULT) {
3877  return true;
3878  }
3879  }
3880  return false;
3881 }
3882 
3884 {
3885  ImageView *view = ima->views.first;
3886  return (view && (view->next || view->name[0]));
3887 }
3888 
3890 {
3891  return BKE_image_is_multiview(ima) &&
3892  (BLI_findstring(&ima->views, STEREO_LEFT_NAME, offsetof(ImageView, name)) &&
3893  BLI_findstring(&ima->views, STEREO_RIGHT_NAME, offsetof(ImageView, name)));
3894 }
3895 
3897 {
3898  /* update image views from render views, but only if they actually changed,
3899  * to avoid invalid memory access during render. ideally these should always
3900  * be acquired with a mutex along with the render result, but there are still
3901  * some places with just an image pointer that need to access views */
3902  if (rr && BLI_listbase_count(&ima->views) == BLI_listbase_count(&rr->views)) {
3903  ImageView *iv = ima->views.first;
3904  RenderView *rv = rr->views.first;
3905  bool modified = false;
3906  for (; rv; rv = rv->next, iv = iv->next) {
3907  modified |= !STREQ(rv->name, iv->name);
3908  }
3909  if (!modified) {
3910  return;
3911  }
3912  }
3913 
3914  BKE_image_free_views(ima);
3915 
3916  if (rr) {
3917  LISTBASE_FOREACH (RenderView *, rv, &rr->views) {
3918  ImageView *iv = MEM_callocN(sizeof(ImageView), "Viewer Image View");
3919  STRNCPY(iv->name, rv->name);
3920  BLI_addtail(&ima->views, iv);
3921  }
3922  }
3923 }
3924 
3926 {
3927  RenderResult *rr = NULL;
3928  if (ima->rr) {
3929  rr = ima->rr;
3930  }
3931  else if (ima->type == IMA_TYPE_R_RESULT) {
3932  if (ima->render_slot == ima->last_render_slot) {
3934  }
3935  else {
3936  rr = BKE_image_get_renderslot(ima, ima->render_slot)->render;
3937  ima->gpuflag |= IMA_GPU_REFRESH;
3938  }
3939 
3940  /* set proper views */
3942  }
3943 
3944  return rr;
3945 }
3946 
3948 {
3949  if (ima->rr) {
3950  /* pass */
3951  }
3952  else if (ima->type == IMA_TYPE_R_RESULT) {
3953  if (ima->render_slot == ima->last_render_slot) {
3955  }
3956  }
3957 }
3958 
3959 bool BKE_image_is_openexr(struct Image *ima)
3960 {
3961 #ifdef WITH_OPENEXR
3963  return BLI_path_extension_check(ima->filepath, ".exr");
3964  }
3965 #else
3966  UNUSED_VARS(ima);
3967 #endif
3968  return false;
3969 }
3970 
3971 void BKE_image_backup_render(Scene *scene, Image *ima, bool free_current_slot)
3972 {
3973  /* called right before rendering, ima->renderslots contains render
3974  * result pointers for everything but the current render */
3976 
3977  /* Ensure we always have a valid render slot. */
3978  if (!ima->renderslots.first) {
3980  ima->render_slot = 0;
3981  ima->last_render_slot = 0;
3982  }
3983  else if (ima->render_slot >= BLI_listbase_count(&ima->renderslots)) {
3984  ima->render_slot = 0;
3985  ima->last_render_slot = 0;
3986  }
3987 
3988  RenderSlot *last_slot = BKE_image_get_renderslot(ima, ima->last_render_slot);
3989  RenderSlot *cur_slot = BKE_image_get_renderslot(ima, ima->render_slot);
3990 
3991  if (last_slot && ima->render_slot != ima->last_render_slot) {
3992  last_slot->render = NULL;
3993  RE_SwapResult(re, &last_slot->render);
3994 
3995  if (cur_slot->render) {
3996  if (free_current_slot) {
3998  }
3999  else {
4000  RE_SwapResult(re, &cur_slot->render);
4001  }
4002  }
4003  }
4004 
4005  ima->last_render_slot = ima->render_slot;
4006 }
4007 
4008 /**************************** multiview load openexr *********************************/
4009 
4010 static void image_add_view(Image *ima, const char *viewname, const char *filepath)
4011 {
4012  ImageView *iv;
4013 
4014  iv = MEM_mallocN(sizeof(ImageView), "Viewer Image View");
4015  STRNCPY(iv->name, viewname);
4016  STRNCPY(iv->filepath, filepath);
4017 
4018  /* For stereo drawing we need to ensure:
4019  * STEREO_LEFT_NAME == STEREO_LEFT_ID and
4020  * STEREO_RIGHT_NAME == STEREO_RIGHT_ID */
4021 
4022  if (STREQ(viewname, STEREO_LEFT_NAME)) {
4023  BLI_addhead(&ima->views, iv);
4024  }
4025  else if (STREQ(viewname, STEREO_RIGHT_NAME)) {
4026  ImageView *left_iv = BLI_findstring(&ima->views, STEREO_LEFT_NAME, offsetof(ImageView, name));
4027 
4028  if (left_iv == NULL) {
4029  BLI_addhead(&ima->views, iv);
4030  }
4031  else {
4032  BLI_insertlinkafter(&ima->views, left_iv, iv);
4033  }
4034  }
4035  else {
4036  BLI_addtail(&ima->views, iv);
4037  }
4038 }
4039 
4040 /* after imbuf load, openexr type can return with a exrhandle open */
4041 /* in that case we have to build a render-result */
4042 #ifdef WITH_OPENEXR
4043 static void image_create_multilayer(Image *ima, ImBuf *ibuf, int framenr)
4044 {
4045  const char *colorspace = ima->colorspace_settings.name;
4046  bool predivide = (ima->alpha_mode == IMA_ALPHA_PREMUL);
4047 
4048  /* only load rr once for multiview */
4049  if (!ima->rr) {
4050  ima->rr = RE_MultilayerConvert(ibuf->userdata, colorspace, predivide, ibuf->x, ibuf->y);
4051  }
4052 
4053  IMB_exr_close(ibuf->userdata);
4054 
4055  ibuf->userdata = NULL;
4056  if (ima->rr != NULL) {
4057  ima->rr->framenr = framenr;
4058  BKE_stamp_info_from_imbuf(ima->rr, ibuf);
4059  }
4060 
4061  /* set proper views */
4063 }
4064 #endif /* WITH_OPENEXR */
4065 
4066 /* common stuff to do with images after loading */
4067 static void image_init_after_load(Image *ima, ImageUser *iuser, ImBuf *UNUSED(ibuf))
4068 {
4069  /* Preview is NULL when it has never been used as an icon before.
4070  * Never handle previews/icons outside of main thread. */
4071  if (G.background == 0 && ima->preview == NULL && BLI_thread_is_main()) {
4073  }
4074 
4075  /* timer */
4076  BKE_image_tag_time(ima);
4077 
4078  ImageTile *tile = BKE_image_get_tile_from_iuser(ima, iuser);
4079  /* Images should never get loaded if the corresponding tile does not exist,
4080  * but we should at least not crash if it happens due to a bug elsewhere. */
4081  BLI_assert(tile != NULL);
4082  if (tile != NULL) {
4083  tile->ok = IMA_OK_LOADED;
4084  }
4085 }
4086 
4088 {
4089  switch (ima->alpha_mode) {
4090  case IMA_ALPHA_STRAIGHT:
4091  return 0;
4092  case IMA_ALPHA_PREMUL:
4093  return IB_alphamode_premul;
4096  case IMA_ALPHA_IGNORE:
4097  return IB_alphamode_ignore;
4098  }
4099 
4100  return 0;
4101 }
4102 
4103 /* the number of files will vary according to the stereo format */
4104 static int image_num_files(Image *ima)
4105 {
4106  const bool is_multiview = BKE_image_is_multiview(ima);
4107 
4108  if (!is_multiview) {
4109  return 1;
4110  }
4111  if (ima->views_format == R_IMF_VIEWS_STEREO_3D) {
4112  return 1;
4113  }
4114  /* R_IMF_VIEWS_INDIVIDUAL */
4115 
4116  return BLI_listbase_count(&ima->views);
4117 }
4118 
4120  Image *ima, ImageUser *iuser, int frame, const int view_id, bool *r_assign)
4121 {
4122  struct ImBuf *ibuf;
4123  char name[FILE_MAX];
4124  int flag;
4125  ImageUser iuser_t = {0};
4126 
4127  ima->lastframe = frame;
4128 
4129  if (iuser) {
4130  iuser_t = *iuser;
4131  }
4132  else {
4133  /* BKE_image_user_file_path() uses this value for file name for sequences. */
4134  iuser_t.framenr = frame;
4135  /* TODO(sergey): Do we need to initialize something else here? */
4136  }
4137 
4138  iuser_t.view = view_id;
4139  BKE_image_user_file_path(&iuser_t, ima, name);
4140 
4141  flag = IB_rect | IB_multilayer | IB_metadata;
4142  flag |= imbuf_alpha_flags_for_image(ima);
4143 
4144  /* read ibuf */
4145  ibuf = IMB_loadiffname(name, flag, ima->colorspace_settings.name);
4146 
4147 #if 0
4148  if (ibuf) {
4149  printf(AT " loaded %s\n", name);
4150  }
4151  else {
4152  printf(AT " missed %s\n", name);
4153  }
4154 #endif
4155 
4156  if (ibuf) {
4157 #ifdef WITH_OPENEXR
4158  if (ibuf->ftype == IMB_FTYPE_OPENEXR && ibuf->userdata) {
4159  /* Handle multilayer and multiview cases, don't assign ibuf here.
4160  * will be set layer in BKE_image_acquire_ibuf from ima->rr. */
4161  if (IMB_exr_has_multilayer(ibuf->userdata)) {
4162  image_create_multilayer(ima, ibuf, frame);
4163  ima->type = IMA_TYPE_MULTILAYER;
4164  IMB_freeImBuf(ibuf);
4165  ibuf = NULL;
4166  }
4167  }
4168  else {
4169  image_init_after_load(ima, iuser, ibuf);
4170  *r_assign = true;
4171  }
4172 #else
4173  image_init_after_load(ima, iuser, ibuf);
4174  *r_assign = true;
4175 #endif
4176  }
4177  else {
4178  ImageTile *tile = BKE_image_get_tile_from_iuser(ima, iuser);
4179  if (tile != NULL) {
4180  tile->ok = 0;
4181  }
4182  }
4183 
4184  return ibuf;
4185 }
4186 
4187 static ImBuf *image_load_sequence_file(Image *ima, ImageUser *iuser, int entry, int frame)
4188 {
4189  struct ImBuf *ibuf = NULL;
4190  const bool is_multiview = BKE_image_is_multiview(ima);
4191  const int totfiles = image_num_files(ima);
4192  bool assign = false;
4193 
4194  if (!is_multiview) {
4195  ibuf = load_sequence_single(ima, iuser, frame, 0, &assign);
4196  if (assign) {
4197  image_assign_ibuf(ima, ibuf, 0, entry);
4198  }
4199  }
4200  else {
4201  const int totviews = BLI_listbase_count(&ima->views);
4202  struct ImBuf **ibuf_arr;
4203 
4204  ibuf_arr = MEM_mallocN(sizeof(ImBuf *) * totviews, "Image Views Imbufs");
4205 
4206  for (int i = 0; i < totfiles; i++) {
4207  ibuf_arr[i] = load_sequence_single(ima, iuser, frame, i, &assign);
4208  }
4209 
4211  IMB_ImBufFromStereo3d(ima->stereo3d_format, ibuf_arr[0], &ibuf_arr[0], &ibuf_arr[1]);
4212  }
4213 
4214  /* return the original requested ImBuf */
4215  ibuf = ibuf_arr[(iuser ? iuser->multi_index : 0)];
4216 
4217  if (assign) {
4218  for (int i = 0; i < totviews; i++) {
4219  image_assign_ibuf(ima, ibuf_arr[i], i, entry);
4220  }
4221  }
4222 
4223  /* "remove" the others (decrease their refcount) */
4224  for (int i = 0; i < totviews; i++) {
4225  if (ibuf_arr[i] != ibuf) {
4226  IMB_freeImBuf(ibuf_arr[i]);
4227  }
4228  }
4229 
4230  /* cleanup */
4231  MEM_freeN(ibuf_arr);
4232  }
4233 
4234  return ibuf;
4235 }
4236 
4237 static ImBuf *image_load_sequence_multilayer(Image *ima, ImageUser *iuser, int entry, int frame)
4238 {
4239  struct ImBuf *ibuf = NULL;
4240  ImageTile *tile = BKE_image_get_tile_from_iuser(ima, iuser);
4241 
4242  /* either we load from RenderResult, or we have to load a new one */
4243 
4244  /* check for new RenderResult */
4245  if (ima->rr == NULL || frame != ima->rr->framenr) {
4246  if (ima->rr) {
4247  /* Cached image buffers shares pointers with render result,
4248  * need to ensure there's no image buffers are hanging around
4249  * with dead links after freeing the render result.
4250  */
4252  RE_FreeRenderResult(ima->rr);
4253  ima->rr = NULL;
4254  }
4255 
4256  ibuf = image_load_sequence_file(ima, iuser, entry, frame);
4257 
4258  if (ibuf) { /* actually an error */
4259  ima->type = IMA_TYPE_IMAGE;
4260  printf("error, multi is normal image\n");
4261  }
4262  }
4263  if (ima->rr) {
4264  RenderPass *rpass = BKE_image_multilayer_index(ima->rr, iuser);
4265 
4266  if (rpass) {
4267  // printf("load from pass %s\n", rpass->name);
4268  /* since we free render results, we copy the rect */
4269  ibuf = IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0);
4270  ibuf->rect_float = MEM_dupallocN(rpass->rect);
4271  ibuf->flags |= IB_rectfloat;
4272  ibuf->mall = IB_rectfloat;
4273  ibuf->channels = rpass->channels;
4274 
4275  BKE_imbuf_stamp_info(ima->rr, ibuf);
4276 
4277  image_init_after_load(ima, iuser, ibuf);
4278  image_assign_ibuf(ima, ibuf, iuser ? iuser->multi_index : 0, entry);
4279  }
4280  // else printf("pass not found\n");
4281  }
4282  else {
4283  tile->ok = 0;
4284  }
4285 
4286  if (iuser) {
4287  iuser->ok = tile->ok;
4288  }
4289 
4290  return ibuf;
4291 }
4292 
4293 static ImBuf *load_movie_single(Image *ima, ImageUser *iuser, int frame, const int view_id)
4294 {
4295  struct ImBuf *ibuf = NULL;
4296  ImageAnim *ia;
4297 
4298  ia = BLI_findlink(&ima->anims, view_id);
4299 
4300  ImageTile *tile = BKE_image_get_tile(ima, 0);
4301 
4302  if (ia->anim == NULL) {
4303  char str[FILE_MAX];
4304  int flags = IB_rect;
4305  ImageUser iuser_t;
4306 
4307  if (ima->flag & IMA_DEINTERLACE) {
4309  }
4310 
4311  if (iuser) {
4312  iuser_t = *iuser;
4313  }
4314 
4315  iuser_t.view = view_id;
4316 
4317  BKE_image_user_file_path(&iuser_t, ima, str);
4318 
4319  /* FIXME: make several stream accessible in image editor, too*/
4320  ia->anim = openanim(str, flags, 0, ima->colorspace_settings.name);
4321 
4322  /* let's initialize this user */
4323  if (ia->anim && iuser && iuser->frames == 0) {
4325  }
4326  }
4327 
4328  if (ia->anim) {
4330  int fra = frame - 1;
4331 
4332  if (fra < 0) {
4333  fra = 0;
4334  }
4335  if (fra > (dur - 1)) {
4336  fra = dur - 1;
4337  }
4339 
4340  if (ibuf) {
4341  image_init_after_load(ima, iuser, ibuf);
4342  }
4343  else {
4344  tile->ok = 0;
4345  }
4346  }
4347  else {
4348  tile->ok = 0;
4349  }
4350 
4351  return ibuf;
4352 }
4353 
4354 static ImBuf *image_load_movie_file(Image *ima, ImageUser *iuser, int frame)
4355 {
4356  struct ImBuf *ibuf = NULL;
4357  const bool is_multiview = BKE_image_is_multiview(ima);
4358  const int totfiles = image_num_files(ima);
4359  ImageTile *tile = BKE_image_get_tile(ima, 0);
4360 
4361  if (totfiles != BLI_listbase_count_at_most(&ima->anims, totfiles + 1)) {
4362  image_free_anims(ima);
4363 
4364  for (int i = 0; i < totfiles; i++) {
4365  /* allocate the ImageAnim */
4366  ImageAnim *ia = MEM_callocN(sizeof(ImageAnim), "Image Anim");
4367  BLI_addtail(&ima->anims, ia);
4368  }
4369  }
4370 
4371  if (!is_multiview) {
4372  ibuf = load_movie_single(ima, iuser, frame, 0);
4373  image_assign_ibuf(ima, ibuf, 0, frame);
4374  }
4375  else {
4376  struct ImBuf **ibuf_arr;
4377  const int totviews = BLI_listbase_count(&ima->views);
4378 
4379  ibuf_arr = MEM_mallocN(sizeof(ImBuf *) * totviews, "Image Views (movie) Imbufs");
4380 
4381  for (int i = 0; i < totfiles; i++) {
4382  ibuf_arr[i] = load_movie_single(ima, iuser, frame, i);
4383  }
4384 
4386  IMB_ImBufFromStereo3d(ima->stereo3d_format, ibuf_arr[0], &ibuf_arr[0], &ibuf_arr[1]);
4387  }
4388 
4389  for (int i = 0; i < totviews; i++) {
4390  if (ibuf_arr[i]) {
4391  image_assign_ibuf(ima, ibuf_arr[i], i, frame);
4392  }
4393  else {
4394  tile->ok = 0;
4395  }
4396  }
4397 
4398  /* return the original requested ImBuf */
4399  ibuf = ibuf_arr[(iuser ? iuser->multi_index : 0)];
4400 
4401  /* "remove" the others (decrease their refcount) */
4402  for (int i = 0; i < totviews; i++) {
4403  if (ibuf_arr[i] != ibuf) {
4404  IMB_freeImBuf(ibuf_arr[i]);
4405  }
4406  }
4407 
4408  /* cleanup */
4409  MEM_freeN(ibuf_arr);
4410  }
4411 
4412  if (iuser) {
4413  iuser->ok = tile->ok;
4414  }
4415 
4416  return ibuf;
4417 }
4418 
4420  ImageUser *iuser,
4421  int cfra,
4422  const int view_id,
4423  const bool has_packed,
4424  bool *r_assign)
4425 {
4426  char filepath[FILE_MAX];
4427  struct ImBuf *ibuf = NULL;
4428  int flag;
4429 
4430  /* is there a PackedFile with this image ? */
4431  if (has_packed) {
4432  ImagePackedFile *imapf;
4433 
4434  flag = IB_rect | IB_multilayer;
4435  flag |= imbuf_alpha_flags_for_image(ima);
4436 
4437  imapf = BLI_findlink(&ima->packedfiles, view_id);
4438  if (imapf->packedfile) {
4439  ibuf = IMB_ibImageFromMemory((unsigned char *)imapf->packedfile->data,
4440  imapf->packedfile->size,
4441  flag,
4443  "<packed data>");
4444  }
4445  }
4446  else {
4447  ImageUser iuser_t;
4448 
4449  flag = IB_rect | IB_multilayer | IB_metadata;
4450  flag |= imbuf_alpha_flags_for_image(ima);
4451 
4452  /* get the correct filepath */
4453  BKE_image_user_frame_calc(ima, iuser, cfra);
4454 
4455  if (iuser) {
4456  iuser_t = *iuser;
4457  }
4458  else {
4459  iuser_t.framenr = ima->lastframe;
4460  }
4461 
4462  iuser_t.view = view_id;
4463 
4464  BKE_image_user_file_path(&iuser_t, ima, filepath);
4465 
4466  /* read ibuf */
4467  ibuf = IMB_loadiffname(filepath, flag, ima->colorspace_settings.name);
4468  }
4469 
4470  if (ibuf) {
4471 #ifdef WITH_OPENEXR
4472  if (ibuf->ftype == IMB_FTYPE_OPENEXR && ibuf->userdata) {
4473  /* Handle multilayer and multiview cases, don't assign ibuf here.
4474  * will be set layer in BKE_image_acquire_ibuf from ima->rr. */
4475  if (IMB_exr_has_multilayer(ibuf->userdata)) {
4476  image_create_multilayer(ima, ibuf, cfra);
4477  ima->type = IMA_TYPE_MULTILAYER;
4478  IMB_freeImBuf(ibuf);
4479  ibuf = NULL;
4480  }
4481  }
4482  else
4483 #endif
4484  {
4485  image_init_after_load(ima, iuser, ibuf);
4486  *r_assign = true;
4487 
4488  /* make packed file for autopack */
4489  if ((has_packed == false) && (G.fileflags & G_FILE_AUTOPACK)) {
4490  ImagePackedFile *imapf = MEM_mallocN(sizeof(ImagePackedFile), "Image Pack-file");
4491  BLI_addtail(&ima->packedfiles, imapf);
4492 
4493  STRNCPY(imapf->filepath, filepath);
4494  imapf->packedfile = BKE_packedfile_new(
4495  NULL, filepath, ID_BLEND_PATH_FROM_GLOBAL(&ima->id));
4496  }
4497  }
4498  }
4499  else {
4500  ImageTile *tile = BKE_image_get_tile_from_iuser(ima, iuser);
4501  tile->ok = 0;
4502  }
4503 
4504  return ibuf;
4505 }
4506 
4507 /* warning, 'iuser' can be NULL
4508  * note: Image->views was already populated (in image_update_views_format)
4509  */
4510 static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
4511 {
4512  struct ImBuf *ibuf = NULL;
4513  bool assign = false;
4514  const bool is_multiview = BKE_image_is_multiview(ima);
4515  const int totfiles = image_num_files(ima);
4516  bool has_packed = BKE_image_has_packedfile(ima);
4517 
4518  /* always ensure clean ima */
4520 
4521  /* this should never happen, but just playing safe */
4522  if (has_packed) {
4523  if (totfiles != BLI_listbase_count_at_most(&ima->packedfiles, totfiles + 1)) {
4525  has_packed = false;
4526  }
4527  }
4528 
4529  if (!is_multiview) {
4530  ibuf = load_image_single(ima, iuser, cfra, 0, has_packed, &assign);
4531  if (assign) {
4532  image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
4533  }
4534  }
4535  else {
4536  struct ImBuf **ibuf_arr;
4537  const int totviews = BLI_listbase_count(&ima->views);
4538  BLI_assert(totviews > 0);
4539 
4540  ibuf_arr = MEM_callocN(sizeof(ImBuf *) * totviews, "Image Views Imbufs");
4541 
4542  for (int i = 0; i < totfiles; i++) {
4543  ibuf_arr[i] = load_image_single(ima, iuser, cfra, i, has_packed, &assign);
4544  }
4545 
4546  /* multi-views/multi-layers OpenEXR files directly populate ima, and return NULL ibuf... */
4547  if (BKE_image_is_stereo(ima) && ima->views_format == R_IMF_VIEWS_STEREO_3D && ibuf_arr[0] &&
4548  totfiles == 1 && totviews >= 2) {
4549  IMB_ImBufFromStereo3d(ima->stereo3d_format, ibuf_arr[0], &ibuf_arr[0], &ibuf_arr[1]);
4550  }
4551 
4552  /* return the original requested ImBuf */
4553  int i = (iuser && iuser->multi_index < totviews) ? iuser->multi_index : 0;
4554  ibuf = ibuf_arr[i];
4555 
4556  if (assign) {
4557  for (i = 0; i < totviews; i++) {
4558  image_assign_ibuf(ima, ibuf_arr[i], i, 0);
4559  }
4560  }
4561 
4562  /* "remove" the others (decrease their refcount) */
4563  for (i = 0; i < totviews; i++) {
4564  if (ibuf_arr[i] != ibuf) {
4565  IMB_freeImBuf(ibuf_arr[i]);
4566  }
4567  }
4568 
4569  /* cleanup */
4570  MEM_freeN(ibuf_arr);
4571  }
4572 
4573  if (iuser) {
4574  ImageTile *tile = BKE_image_get_tile(ima, 0);
4575  iuser->ok = tile->ok;
4576  }
4577 
4578  return ibuf;
4579 }
4580 
4582 {
4583  ImBuf *ibuf = NULL;
4584 
4585  if (ima->rr == NULL) {
4586  ibuf = image_load_image_file(ima, iuser, 0);
4587  if (ibuf) { /* actually an error */
4588  ima->type = IMA_TYPE_IMAGE;
4589  return ibuf;
4590  }
4591  }
4592  if (ima->rr) {
4593  RenderPass *rpass = BKE_image_multilayer_index(ima->rr, iuser);
4594 
4595  if (rpass) {
4596  ibuf = IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0);
4597 
4598  image_init_after_load(ima, iuser, ibuf);
4599 
4600  ibuf->rect_float = rpass->rect;
4601  ibuf->flags |= IB_rectfloat;
4602  ibuf->channels = rpass->channels;
4603 
4604  BKE_imbuf_stamp_info(ima->rr, ibuf);
4605 
4606  image_assign_ibuf(ima, ibuf, iuser ? iuser->multi_index : IMA_NO_INDEX, 0);
4607  }
4608  }
4609 
4610  ImageTile *tile = BKE_image_get_tile(ima, 0);
4611  if (ibuf == NULL) {
4612  tile->ok = 0;
4613  }
4614  if (iuser) {
4615  iuser->ok = tile->ok;
4616  }
4617 
4618  return ibuf;
4619 }
4620 
4621 /* showing RGBA result itself (from compo/sequence) or
4622  * like exr, using layers etc */
4623 /* always returns a single ibuf, also during render progress */
4624 static ImBuf *image_get_render_result(Image *ima, ImageUser *iuser, void **r_lock)
4625 {
4626  Render *re;
4627  RenderResult rres;
4628  RenderView *rv;
4629  float *rectf, *rectz;
4630  unsigned int *rect;
4631  float dither;
4632  int channels, layer, pass;
4633  ImBuf *ibuf;
4634  int from_render = (ima->render_slot == ima->last_render_slot);
4635  int actview;
4636 
4637  if (!(iuser && iuser->scene)) {
4638  return NULL;
4639  }
4640 
4641  /* if we the caller is not going to release the lock, don't give the image */
4642  if (!r_lock) {
4643  return NULL;
4644  }
4645 
4646  re = RE_GetSceneRender(iuser->scene);
4647 
4648  channels = 4;
4649  layer = iuser->layer;
4650  pass = iuser->pass;
4651  actview = iuser->view;
4652 
4653  if (BKE_image_is_stereo(ima) && (iuser->flag & IMA_SHOW_STEREO)) {
4654  actview = iuser->multiview_eye;
4655  }
4656 
4657  RenderSlot *slot;
4658  if (from_render) {
4659  RE_AcquireResultImage(re, &rres, actview);
4660  }
4661  else if ((slot = BKE_image_get_renderslot(ima, ima->render_slot))->render) {
4662  rres = *(slot->render);
4663  rres.have_combined = ((RenderView *)rres.views.first)->rectf != NULL;
4664  }
4665  else {
4666  memset(&rres, 0, sizeof(RenderResult));
4667  }
4668 
4669  if (!(rres.rectx > 0 && rres.recty > 0)) {
4670  if (from_render) {
4672  }
4673  return NULL;
4674  }
4675 
4676  /* release is done in BKE_image_release_ibuf using r_lock */
4677  if (from_render) {
4679  *r_lock = re;
4680  rv = NULL;
4681  }
4682  else {
4683  rv = BLI_findlink(&rres.views, actview);
4684  if (rv == NULL) {
4685  rv = rres.views.first;
4686  }
4687  }
4688 
4689  /* this gives active layer, composite or sequence result */
4690  if (rv == NULL) {
4691  rect = (unsigned int *)rres.rect32;
4692  rectf = rres.rectf;
4693  rectz = rres.rectz;
4694  }
4695  else {
4696  rect = (unsigned int *)rv->rect32;
4697  rectf = rv->rectf;
4698  rectz = rv->rectz;
4699  }
4700 
4701  dither = iuser->scene->r.dither_intensity;
4702 
4703  /* combined layer gets added as first layer */
4704  if (rres.have_combined && layer == 0) {
4705  /* pass */
4706  }
4707  else if (rect && layer == 0) {
4708  /* rect32 is set when there's a Sequence pass, this pass seems
4709  * to have layer=0 (this is from image_buttons.c)
4710  * in this case we ignore float buffer, because it could have
4711  * hung from previous pass which was float
4712  */
4713  rectf = NULL;
4714  }
4715  else if (rres.layers.first) {
4716  RenderLayer *rl = BLI_findlink(&rres.layers, layer - (rres.have_combined ? 1 : 0));
4717  if (rl) {
4718  RenderPass *rpass = image_render_pass_get(rl, pass, actview, NULL);
4719  if (rpass) {
4720  rectf = rpass->rect;
4721  if (pass != 0) {
4722  channels = rpass->channels;
4723  dither = 0.0f; /* don't dither passes */
4724  }
4725  }
4726 
4727  for (rpass = rl->passes.first; rpass; rpass = rpass->next) {
4728  if (STREQ(rpass->name, RE_PASSNAME_Z) && rpass->view_id == actview) {
4729  rectz = rpass->rect;
4730  }
4731  }
4732  }
4733  }
4734 
4736 
4737  /* make ibuf if needed, and initialize it */
4738  if (ibuf == NULL) {
4739  ibuf = IMB_allocImBuf(rres.rectx, rres.recty, 32, 0);
4740  image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
4741  }
4742 
4743  /* Set color space settings for a byte buffer.
4744  *
4745  * This is mainly to make it so color management treats byte buffer
4746  * from render result with Save Buffers enabled as final display buffer
4747  * and doesn't apply any color management on it.
4748  *
4749  * For other cases we need to be sure it stays to default byte buffer space.
4750  */
4751  if (ibuf->rect != rect) {
4754  }
4755 
4756  /* invalidate color managed buffers if render result changed */
4758  if (ibuf->x != rres.rectx || ibuf->y != rres.recty || ibuf->rect_float != rectf) {
4760  }
4761 
4762  ibuf->x = rres.rectx;
4763  ibuf->y = rres.recty;
4764 
4765  if (rect) {
4766  imb_freerectImBuf(ibuf);
4767  ibuf->rect = rect;
4768  }
4769  else {
4770  /* byte buffer of render result has been freed, make sure image buffers
4771  * does not reference to this buffer anymore
4772  * need check for whether byte buffer was allocated and owned by image itself
4773  * or if it's reusing buffer from render result
4774  */
4775  if ((ibuf->mall & IB_rect) == 0) {
4776  ibuf->rect = NULL;
4777  }
4778  }
4779 
4780  if (rectf) {
4781  ibuf->rect_float = rectf;
4782  ibuf->flags |= IB_rectfloat;
4783  ibuf->channels = channels;
4784  }
4785  else {
4786  ibuf->rect_float = NULL;
4787  ibuf->flags &= ~IB_rectfloat;
4788  }
4789 
4790  if (rectz) {
4791  ibuf->zbuf_float = rectz;
4792  ibuf->flags |= IB_zbuffloat;
4793  }
4794  else {
4795  ibuf->zbuf_float = NULL;
4796  ibuf->flags &= ~IB_zbuffloat;
4797  }
4798 
4799  /* TODO(sergey): Make this faster by either simply referencing the stamp
4800  * or by changing both ImBug and RenderResult to use same data type to
4801  * store metadata. */
4802  if (ibuf->metadata != NULL) {
4803  IMB_metadata_free(ibuf->metadata);
4804  ibuf->metadata = NULL;
4805  }
4806  BKE_imbuf_stamp_info(&rres, ibuf);
4807 
4809 
4810  ibuf->dither = dither;
4811 
4812  ImageTile *tile = BKE_image_get_tile(ima, 0);
4813  tile->ok = IMA_OK_LOADED;
4814 
4815  return ibuf;
4816 }
4817 
4819 {
4820  const bool is_multilayer = BKE_image_is_multilayer(ima);
4821  const bool is_backdrop = (ima->source == IMA_SRC_VIEWER) && (ima->type == IMA_TYPE_COMPOSITE) &&
4822  (iuser == NULL);
4824 
4825  if (is_multilayer) {
4826  return iuser ? iuser->multi_index : index;
4827  }
4828  if (is_backdrop) {
4829  if (BKE_image_is_stereo(ima)) {
4830  /* backdrop hackaround (since there is no iuser */
4831  return ima->eye;
4832  }
4833  }
4834  else if (BKE_image_is_multiview(ima)) {
4835  return iuser ? iuser->multi_index : index;
4836  }
4837 
4838  return index;
4839 }
4840 
4841 static void image_get_entry_and_index(Image *ima, ImageUser *iuser, int *r_entry, int *r_index)
4842 {
4843  int frame = 0, index = image_get_multiview_index(ima, iuser);
4844 
4845  /* see if we already have an appropriate ibuf, with image source and type */
4846  if (ima->source == IMA_SRC_MOVIE) {
4847  frame = iuser ? iuser->framenr : ima->lastframe;
4848  }
4849  else if (ima->source == IMA_SRC_SEQUENCE) {
4850  if (ima->type == IMA_TYPE_IMAGE) {
4851  frame = iuser ? iuser->framenr : ima->lastframe;
4852  }
4853  else if (ima->type == IMA_TYPE_MULTILAYER) {
4854  frame = iuser ? iuser->framenr : ima->lastframe;
4855  }
4856  }
4857  else if (ima->source == IMA_SRC_TILED) {
4858  frame = (iuser && iuser->tile) ? iuser->tile : 1001;
4859  }
4860 
4861  *r_entry = frame;
4862  *r_index = index;
4863 }
4864 
4865 /* Get the ibuf from an image cache for a given image user.
4866  *
4867  * Returns referenced image buffer if it exists, callee is to
4868  * call IMB_freeImBuf to de-reference the image buffer after
4869  * it's done handling it.
4870  */
4871 static ImBuf *image_get_cached_ibuf(Image *ima, ImageUser *iuser, int *r_entry, int *r_index)
4872 {
4873  ImBuf *ibuf = NULL;
4874  int entry = 0, index = image_get_multiview_index(ima, iuser);
4875 
4876  /* see if we already have an appropriate ibuf, with image source and type */
4877  if (ima->source == IMA_SRC_MOVIE) {
4878  entry = iuser ? iuser->framenr : ima->lastframe;
4879  ibuf = image_get_cached_ibuf_for_index_entry(ima, index, entry);
4880  ima->lastframe = entry;
4881  }
4882  else if (ima->source == IMA_SRC_SEQUENCE) {
4883  if (ima->type == IMA_TYPE_IMAGE) {
4884  entry = iuser ? iuser->framenr : ima->lastframe;
4885  ibuf = image_get_cached_ibuf_for_index_entry(ima, index, entry);
4886  ima->lastframe = entry;
4887 
4888  /* counter the fact that image is set as invalid when loading a frame
4889  * that is not in the cache (through image_acquire_ibuf for instance),
4890  * yet we have valid frames in the cache loaded */
4891  if (ibuf) {
4892  ImageTile *tile = BKE_image_get_tile(ima, 0);
4893  tile->ok = IMA_OK_LOADED;
4894 
4895  if (iuser) {
4896  iuser->ok = tile->ok;
4897  }
4898  }
4899  }
4900  else if (ima->type == IMA_TYPE_MULTILAYER) {
4901  entry = iuser ? iuser->framenr : ima->lastframe;
4902  ibuf = image_get_cached_ibuf_for_index_entry(ima, index, entry);
4903  }
4904  }
4905  else if (ima->source == IMA_SRC_FILE) {
4906  if (ima->type == IMA_TYPE_IMAGE) {
4908  }
4909  else if (ima->type == IMA_TYPE_MULTILAYER) {
4911  }
4912  }
4913  else if (ima->source == IMA_SRC_GENERATED) {
4915  }
4916  else if (ima->source == IMA_SRC_VIEWER) {
4917  /* always verify entirely, not that this shouldn't happen
4918  * as part of texture sampling in rendering anyway, so not
4919  * a big bottleneck */
4920  }
4921  else if (ima->source == IMA_SRC_TILED) {
4923  entry = (iuser && iuser->tile) ? iuser->tile : 1001;
4924  ibuf = image_get_cached_ibuf_for_index_entry(ima, index, entry);
4925 
4926  if ((ima->type == IMA_TYPE_IMAGE) && ibuf != NULL) {
4927  ImageTile *tile = BKE_image_get_tile(ima, entry);
4928  tile->ok = IMA_OK_LOADED;
4929 
4930  /* iuser->ok is useless for tiled images because iuser->tile changes all the time. */
4931  if (iuser != NULL) {
4932  iuser->ok = 1;
4933  }
4934  }
4935  }
4936  }
4937 
4938  if (r_entry) {
4939  *r_entry = entry;
4940  }
4941 
4942  if (r_index) {
4943  *r_index = index;
4944  }
4945 
4946  return ibuf;
4947 }
4948 
4949 BLI_INLINE bool image_quick_test(Image *ima, const ImageUser *iuser)
4950 {
4951  if (ima == NULL) {
4952  return false;
4953  }
4954 
4955  if (iuser) {
4956  if (iuser->ok == 0) {
4957  return false;
4958  }
4959  }
4960 
4961  ImageTile *tile = BKE_image_get_tile_from_iuser(ima, iuser);
4962  if (tile == NULL) {
4963  return false;
4964  }
4965  if (tile->ok == 0) {
4966  return false;
4967  }
4968 
4969  return true;
4970 }
4971 
4972 /* Checks optional ImageUser and verifies/creates ImBuf.
4973  *
4974  * not thread-safe, so callee should worry about thread locks
4975  */
4976 static ImBuf *image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
4977 {
4978  ImBuf *ibuf = NULL;
4979  int entry = 0, index = 0;
4980 
4981  if (r_lock) {
4982  *r_lock = NULL;
4983  }
4984 
4985  /* quick reject tests */
4986  if (!image_quick_test(ima, iuser)) {
4987  return NULL;
4988  }
4989 
4990  ibuf = image_get_cached_ibuf(ima, iuser, &entry, &index);
4991 
4992  if (ibuf == NULL) {
4993  /* we are sure we have to load the ibuf, using source and type */
4994  if (ima->source == IMA_SRC_MOVIE) {
4995  /* source is from single file, use flipbook to store ibuf */
4996  ibuf = image_load_movie_file(ima, iuser, entry);
4997  }
4998  else if (ima->source == IMA_SRC_SEQUENCE) {
4999  if (ima->type == IMA_TYPE_IMAGE) {
5000  /* regular files, ibufs in flipbook, allows saving */
5001  ibuf = image_load_sequence_file(ima, iuser, entry, entry);
5002  }
5003  /* no else; on load the ima type can change */
5004  if (ima->type == IMA_TYPE_MULTILAYER) {
5005  /* only 1 layer/pass stored in imbufs, no exrhandle anim storage, no saving */
5006  ibuf = image_load_sequence_multilayer(ima, iuser, entry, entry);
5007  }
5008  }
5009  else if (ima->source == IMA_SRC_TILED) {
5010  if (ima->type == IMA_TYPE_IMAGE) {
5011  /* regular files, ibufs in flipbook, allows saving */
5012  ibuf = image_load_sequence_file(ima, iuser, entry, 0);
5013  }
5014  /* no else; on load the ima type can change */
5015  if (ima->type == IMA_TYPE_MULTILAYER) {
5016  /* only 1 layer/pass stored in imbufs, no exrhandle anim storage, no saving */
5017  ibuf = image_load_sequence_multilayer(ima, iuser, entry, 0);
5018  }
5019  }
5020  else if (ima->source == IMA_SRC_FILE) {
5021 
5022  if (ima->type == IMA_TYPE_IMAGE) {
5023  ibuf = image_load_image_file(ima, iuser, entry); /* cfra only for '#', this global is OK */
5024  }
5025  /* no else; on load the ima type can change */
5026  if (ima->type == IMA_TYPE_MULTILAYER) {
5027  /* keeps render result, stores ibufs in listbase, allows saving */
5028  ibuf = image_get_ibuf_multilayer(ima, iuser);
5029  }
5030  }
5031  else if (ima->source == IMA_SRC_GENERATED) {
5032  /* generated is: ibuf is allocated dynamically */
5033  /* UV testgrid or black or solid etc */
5034  if (ima->gen_x == 0) {
5035  ima->gen_x = 1024;
5036  }
5037  if (ima->gen_y == 0) {
5038  ima->gen_y = 1024;
5039  }
5040  if (ima->gen_depth == 0) {
5041  ima->gen_depth = 24;
5042  }
5043  ibuf = add_ibuf_size(ima->gen_x,
5044  ima->gen_y,
5045  ima->filepath,
5046  ima->gen_depth,
5047  (ima->gen_flag & IMA_GEN_FLOAT) != 0,
5048  ima->gen_type,
5049  ima->gen_color,
5050  &ima->colorspace_settings);
5051  image_assign_ibuf(ima, ibuf, index, 0);
5052  ImageTile *tile = BKE_image_get_tile(ima, 0);
5053  tile->ok = IMA_OK_LOADED;
5054  }
5055  else if (ima->source == IMA_SRC_VIEWER) {
5056  if (ima->type == IMA_TYPE_R_RESULT) {
5057  /* always verify entirely, and potentially
5058  * returns pointer to release later */
5059  ibuf = image_get_render_result(ima, iuser, r_lock);
5060  }
5061  else if (ima->type == IMA_TYPE_COMPOSITE) {
5062  /* requires lock/unlock, otherwise don't return image */
5063  if (r_lock) {
5064  /* unlock in BKE_image_release_ibuf */
5066  *r_lock = ima;
5067 
5068  /* XXX anim play for viewer nodes not yet supported */
5069  entry = 0; // XXX iuser ? iuser->framenr : 0;
5070  ibuf = image_get_cached_ibuf_for_index_entry(ima, index, entry);
5071 
5072  if (!ibuf) {
5073  /* Composite Viewer, all handled in compositor */
5074  /* fake ibuf, will be filled in compositor */
5075  ibuf = IMB_allocImBuf(256, 256, 32, IB_rect | IB_rectfloat);
5076  image_assign_ibuf(ima, ibuf, index, entry);
5077  }
5078  }
5079  }
5080  }
5081 
5082  /* We only want movies and sequences to be memory limited. */
5083  if (ibuf != NULL && !ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
5084  ibuf->userflags |= IB_PERSISTENT;
5085  }
5086  }
5087 
5088  BKE_image_tag_time(ima);
5089 
5090  return ibuf;
5091 }
5092 
5093 /* return image buffer for given image and user
5094  *
5095  * - will lock render result if image type is render result and lock is not NULL
5096  * - will return NULL if image type if render or composite result and lock is NULL
5097  *
5098  * references the result, BKE_image_release_ibuf should be used to de-reference
5099  */
5100 ImBuf *BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
5101 {
5102  ImBuf *ibuf;
5103 
5105 
5106  ibuf = image_acquire_ibuf(ima, iuser, r_lock);
5107 
5109 
5110  return ibuf;
5111 }
5112 
5113 void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
5114 {
5115  if (lock != NULL) {
5116  /* for getting image during threaded render / compositing, need to release */
5117  if (lock == ima) {
5118  BLI_thread_unlock(LOCK_VIEWER); /* viewer image */
5119  }
5120  else {
5121  RE_ReleaseResultImage(lock); /* render result */
5122  BLI_thread_unlock(LOCK_VIEWER); /* view image imbuf */
5123  }
5124  }
5125 
5126  if (ibuf) {
5128  IMB_freeImBuf(ibuf);
5130  }
5131 }
5132 
5133 /* checks whether there's an image buffer for given image and user */
5135 {
5136  ImBuf *ibuf;
5137 
5138  /* quick reject tests */
5139  if (!image_quick_test(ima, iuser)) {
5140  return false;
5141  }
5142 
5144 
5145  ibuf = image_get_cached_ibuf(ima, iuser, NULL, NULL);
5146 
5147  if (!ibuf) {
5148  ibuf = image_acquire_ibuf(ima, iuser, NULL);
5149  }
5150 
5152 
5153  IMB_freeImBuf(ibuf);
5154 
5155  return ibuf != NULL;
5156 }
5157 
5158 /* ******** Pool for image buffers ******** */
5159 
5160 typedef struct ImagePoolItem {
5164  int index;
5165  int entry;
5167 
5168 typedef struct ImagePool {
5172 
5174 {
5175  ImagePool *pool = MEM_callocN(sizeof(ImagePool), "Image Pool");
5176  pool->memory_pool = BLI_mempool_create(sizeof(ImagePoolItem), 0, 128, BLI_MEMPOOL_NOP);
5177 
5178  return pool;
5179 }
5180 
5182 {
5183  /* Use single lock to dereference all the image buffers. */
5185  for (ImagePoolItem *item = pool->image_buffers.first; item != NULL; item = item->next) {
5186  if (item->ibuf != NULL) {
5187  IMB_freeImBuf(item->ibuf);
5188  }
5189  }
5191 
5192  BLI_mempool_destroy(pool->memory_pool);
5193  MEM_freeN(pool);
5194 }
5195 
5197  ImagePool *pool, Image *image, int entry, int index, bool *found)
5198 {
5199  ImagePoolItem *item;
5200 
5201  *found = false;
5202 
5203  for (item = pool->image_buffers.first; item; item = item->next) {
5204  if (item->image == image && item->entry == entry && item->index == index) {
5205  *found = true;
5206  return item->ibuf;
5207  }
5208  }
5209 
5210  return NULL;
5211 }
5212 
5214 {
5215  ImBuf *ibuf;
5216  int index, entry;
5217  bool found;
5218 
5219  if (!image_quick_test(ima, iuser)) {
5220  return NULL;
5221  }
5222 
5223  if (pool == NULL) {
5224  /* pool could be NULL, in this case use general acquire function */
5225  return BKE_image_acquire_ibuf(ima, iuser, NULL);
5226  }
5227 
5228  image_get_entry_and_index(ima, iuser, &entry, &index);
5229 
5230  ibuf = image_pool_find_item(pool, ima, entry, index, &found);
5231  if (found) {
5232  return ibuf;
5233  }
5234 
5236 
5237  ibuf = image_pool_find_item(pool, ima, entry, index, &found);
5238 
5239  /* will also create item even in cases image buffer failed to load,
5240  * prevents trying to load the same buggy file multiple times
5241  */
5242  if (!found) {
5243  ImagePoolItem *item;
5244 
5245  ibuf = image_acquire_ibuf(ima, iuser, NULL);
5246 
5247  item = BLI_mempool_alloc(pool->memory_pool);
5248  item->image = ima;
5249  item->entry = entry;
5250  item->index = index;
5251  item->ibuf = ibuf;
5252 
5253  BLI_addtail(&pool->image_buffers, item);
5254  }
5255 
5257 
5258  return ibuf;
5259 }
5260 
5262 {
5263  /* if pool wasn't actually used, use general release stuff,
5264  * for pools image buffers will be dereferenced on pool free
5265  */
5266  if (pool == NULL) {
5267  BKE_image_release_ibuf(ima, ibuf, NULL);
5268  }
5269 }
5270 
5271 int BKE_image_user_frame_get(const ImageUser *iuser, int cfra, bool *r_is_in_range)
5272 {
5273  const int len = iuser->frames;
5274 
5275  if (r_is_in_range) {
5276  *r_is_in_range = false;
5277  }
5278 
5279  if (len == 0) {
5280  return 0;
5281  }
5282 
5283  int framenr;
5284  cfra = cfra - iuser->sfra + 1;
5285 
5286  /* cyclic */
5287  if (iuser->cycl) {
5288  cfra = ((cfra) % len);
5289  if (cfra < 0) {
5290  cfra += len;
5291  }
5292  if (cfra == 0) {
5293  cfra = len;
5294  }
5295 
5296  if (r_is_in_range) {
5297  *r_is_in_range = true;
5298  }
5299  }
5300 
5301  if (cfra < 0) {
5302  cfra = 0;
5303  }
5304  else if (cfra > len) {
5305  cfra = len;
5306  }
5307  else {
5308  if (r_is_in_range) {
5309  *r_is_in_range = true;
5310  }
5311  }
5312 
5313  /* transform to images space */
5314  framenr = cfra;
5315  if (framenr > iuser->frames) {
5316  framenr = iuser->frames;
5317  }
5318 
5319  if (iuser->cycl) {
5320  framenr = ((framenr) % len);
5321  while (framenr < 0) {
5322  framenr += len;
5323  }
5324  if (framenr == 0) {
5325  framenr = len;
5326  }
5327  }
5328 
5329  /* important to apply after else we cant loop on frames 100 - 110 for eg. */
5330  framenr += iuser->offset;
5331 
5332  return framenr;
5333 }
5334 
5335 void BKE_image_user_frame_calc(Image *ima, ImageUser *iuser, int cfra)
5336 {
5337  if (iuser) {
5338  if (ima && BKE_image_is_animated(ima)) {
5339  /* Compute current frame for animated image. */
5340  bool is_in_range;
5341  const int framenr = BKE_image_user_frame_get(iuser, cfra, &is_in_range);
5342 
5343  if (is_in_range) {
5344  iuser->flag |= IMA_USER_FRAME_IN_RANGE;
5345  }
5346  else {
5347  iuser->flag &= ~IMA_USER_FRAME_IN_RANGE;
5348  }
5349 
5350  iuser->framenr = framenr;
5351  }
5352  else {
5353  /* Set fixed frame number for still image. */
5354  iuser->framenr = 0;
5355  iuser->flag |= IMA_USER_FRAME_IN_RANGE;
5356  }
5357 
5358  if (ima && ima->gpuframenr != iuser->framenr) {
5359  /* Note: a single texture and refresh doesn't really work when
5360  * multiple image users may use different frames, this is to
5361  * be improved with perhaps a GPU texture cache. */
5362  ima->gpuflag |= IMA_GPU_REFRESH;
5363  ima->gpuframenr = iuser->framenr;
5364  }
5365 
5366  if (iuser->ok == 0) {
5367  iuser->ok = 1;
5368  }
5369 
5370  if (ima) {
5371  LISTBASE_FOREACH (ImageTile *, tile, &ima->tiles) {
5372  if (tile->ok == 0) {
5373  tile->ok = IMA_OK;
5374  }
5375  }
5376  }
5377 
5378  iuser->flag &= ~IMA_NEED_FRAME_RECALC;
5379  }
5380 }
5381 
5382 /* goes over all ImageUsers, and sets frame numbers if auto-refresh is set */
5384  ID *UNUSED(iuser_id),
5385  ImageUser *iuser,
5386  void *customdata)
5387 {
5388  int cfra = *(int *)customdata;
5389 
5390  if ((iuser->flag & IMA_ANIM_ALWAYS) || (iuser->flag & IMA_NEED_FRAME_RECALC)) {
5391  BKE_image_user_frame_calc(ima, iuser, cfra);
5392  }
5393 }
5394 
5395 void BKE_image_editors_update_frame(const Main *bmain, int cfra)
5396 {
5397  /* This only updates images used by the user interface. For others the
5398  * dependency graph will call BKE_image_user_id_eval_animation. */
5399  wmWindowManager *wm = bmain->wm.first;
5401 }
5402 
5404  ID *UNUSED(iuser_id),
5405  ImageUser *UNUSED(iuser),
5406  void *customdata)
5407 {
5408  if (ima && BKE_image_is_animated(ima)) {
5409  *(bool *)customdata = true;
5410  }
5411 }
5412 
5414 {
5415  /* For the dependency graph, this does not consider nested node
5416  * trees as these are handled as their own datablock. */
5417  bool has_animation = false;
5418  bool skip_nested_nodes = true;
5419  image_walk_id_all_users(id, skip_nested_nodes, &has_animation, image_user_id_has_animation);
5420  return has_animation;
5421 }
5422 
5424  ID *UNUSED(iduser_id),
5425  ImageUser *iuser,
5426  void *customdata)
5427 {
5428  if (ima && BKE_image_is_animated(ima)) {
5429  Depsgraph *depsgraph = (Depsgraph *)customdata;
5430 
5431  if ((iuser->flag & IMA_ANIM_ALWAYS) || (iuser->flag & IMA_NEED_FRAME_RECALC) ||
5433  float cfra = DEG_get_ctime(depsgraph);
5434 
5435  BKE_image_user_frame_calc(ima, iuser, cfra);
5436  }
5437  }
5438 }
5439 
5441 {
5442  /* This is called from the dependency graph to update the image
5443  * users in data-blocks. It computes the current frame number
5444  * and tags the image to be refreshed.
5445  * This does not consider nested node trees as these are handled
5446  * as their own data-block. */
5447  bool skip_nested_nodes = true;
5449 }
5450 
5451 void BKE_image_user_file_path(ImageUser *iuser, Image *ima, char *filepath)
5452 {
5453  if (BKE_image_is_multiview(ima)) {
5454  ImageView *iv = BLI_findlink(&ima->views, iuser->view);
5455  if (iv->filepath[0]) {
5456  BLI_strncpy(filepath, iv->filepath, FILE_MAX);
5457  }
5458  else {
5459  BLI_strncpy(filepath, ima->filepath, FILE_MAX);
5460  }
5461  }
5462  else {
5463  BLI_strncpy(filepath, ima->filepath, FILE_MAX);
5464  }
5465 
5466  if (ELEM(ima->source, IMA_SRC_SEQUENCE, IMA_SRC_TILED)) {
5467  char head[FILE_MAX], tail[FILE_MAX];
5468  unsigned short numlen;
5469 
5470  int index;
5471  if (ima->source == IMA_SRC_SEQUENCE) {
5472  index = iuser ? iuser->framenr : ima->lastframe;
5473  }
5474  else {
5475  index = (iuser && iuser->tile) ? iuser->tile : 1001;
5476  }
5477 
5478  BLI_path_sequence_decode(filepath, head, tail, &numlen);
5479  BLI_path_sequence_encode(filepath, head, tail, numlen, index);
5480  }
5481 
5482  BLI_path_abs(filepath, ID_BLEND_PATH_FROM_GLOBAL(&ima->id));
5483 }
5484 
5485 bool BKE_image_has_alpha(struct Image *image)
5486 {
5487  ImBuf *ibuf;
5488  void *lock;
5489  int planes;
5490 
5491  ibuf = BKE_image_acquire_ibuf(image, NULL, &lock);
5492  planes = (ibuf ? ibuf->planes : 0);
5493  BKE_image_release_ibuf(image, ibuf, lock);
5494 
5495  if (planes == 32) {
5496  return true;
5497  }
5498 
5499  return false;
5500 }
5501 
5502 void BKE_image_get_size(Image *image, ImageUser *iuser, int *r_width, int *r_height)
5503 {
5504  ImBuf *ibuf = NULL;
5505  void *lock;
5506 
5507  if (image != NULL) {
5508  ibuf = BKE_image_acquire_ibuf(image, iuser, &lock);
5509  }
5510 
5511  if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
5512  *r_width = ibuf->x;
5513  *r_height = ibuf->y;
5514  }
5515  else if (image != NULL && image->type == IMA_TYPE_R_RESULT && iuser != NULL &&
5516  iuser->scene != NULL) {
5517  Scene *scene = iuser->scene;
5518  *r_width = (scene->r.xsch * scene->r.size) / 100;
5519  *r_height = (scene->r.ysch * scene->r.size) / 100;
5520  if ((scene->r.mode & R_BORDER) && (scene->r.mode & R_CROP)) {
5521  *r_width *= BLI_rctf_size_x(&scene->r.border);
5522  *r_height *= BLI_rctf_size_y(&scene->r.border);
5523  }
5524  }
5525  else {
5526  *r_width = IMG_SIZE_FALLBACK;
5527  *r_height = IMG_SIZE_FALLBACK;
5528  }
5529 
5530  if (image != NULL) {
5531  BKE_image_release_ibuf(image, ibuf, lock);
5532  }
5533 }
5534 
5535 void BKE_image_get_size_fl(Image *image, ImageUser *iuser, float r_size[2])
5536 {
5537  int width, height;
5538  BKE_image_get_size(image, iuser, &width, &height);
5539 
5540  r_size[0] = (float)width;
5541  r_size[1] = (float)height;
5542 }
5543 
5544 void BKE_image_get_aspect(Image *image, float *r_aspx, float *r_aspy)
5545 {
5546  *r_aspx = 1.0;
5547 
5548  /* x is always 1 */
5549  if (image) {
5550  *r_aspy = image->aspy / image->aspx;
5551  }
5552  else {
5553  *r_aspy = 1.0f;
5554  }
5555 }
5556 
5557 unsigned char *BKE_image_get_pixels_for_frame(struct Image *image, int frame, int tile)
5558 {
5559  ImageUser iuser;
5560  BKE_imageuser_default(&iuser);
5561  void *lock;
5562  ImBuf *ibuf;
5563  unsigned char *pixels = NULL;
5564 
5565  iuser.framenr = frame;
5566  iuser.tile = tile;
5567 
5568  ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
5569 
5570  if (ibuf) {
5571  pixels = (unsigned char *)ibuf->rect;
5572 
5573  if (pixels) {
5574  pixels = MEM_dupallocN(pixels);
5575  }
5576 
5577  BKE_image_release_ibuf(image, ibuf, lock);
5578  }
5579 
5580  if (!pixels) {
5581  return NULL;
5582  }
5583 
5584  return pixels;
5585 }
5586 
5587 float *BKE_image_get_float_pixels_for_frame(struct Image *image, int frame, int tile)
5588 {
5589  ImageUser iuser;
5590  BKE_imageuser_default(&iuser);
5591  void *lock;
5592  ImBuf *ibuf;
5593  float *pixels = NULL;
5594 
5595  iuser.framenr = frame;
5596  iuser.tile = tile;
5597 
5598  ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
5599 
5600  if (ibuf) {
5601  pixels = ibuf->rect_float;
5602 
5603  if (pixels) {
5604  pixels = MEM_dupallocN(pixels);
5605  }
5606 
5607  BKE_image_release_ibuf(image, ibuf, lock);
5608  }
5609 
5610  if (!pixels) {
5611  return NULL;
5612  }
5613 
5614  return pixels;
5615 }
5616 
5618 {
5619  return BLI_path_sequence_decode(image->filepath, NULL, NULL, NULL);
5620 }
5621 
5623 {
5624  return (BLI_listbase_is_empty(&ima->anims) == false);
5625 }
5626 
5628 {
5629  return (BLI_listbase_is_empty(&ima->packedfiles) == false);
5630 }
5631 
5633 {
5634  /* This could be improved to detect cases like //../../, currently path
5635  * remapping empty file paths empty. */
5636  return ima->filepath[0] != '\0';
5637 }
5638 
5639 /* Checks the image buffer changes with time (not keyframed values). */
5641 {
5642  return ELEM(image->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE);
5643 }
5644 
5645 /* Checks whether the image consists of multiple buffers. */
5647 {
5649 }
5650 
5651 /* Image modifications */
5652 bool BKE_image_is_dirty_writable(Image *image, bool *r_is_writable)
5653 {
5654  bool is_dirty = false;
5655  bool is_writable = false;
5656 
5658  if (image->cache != NULL) {
5659  struct MovieCacheIter *iter = IMB_moviecacheIter_new(image->cache);
5660 
5661  while (!IMB_moviecacheIter_done(iter)) {
5662  ImBuf *ibuf = IMB_moviecacheIter_getImBuf(iter);
5663  if (ibuf->userflags & IB_BITMAPDIRTY) {
5664  is_writable = BKE_image_buffer_format_writable(ibuf);
5665  is_dirty = true;
5666  break;
5667  }
5669  }
5671  }
5673 
5674  if (r_is_writable) {
5675  *r_is_writable = is_writable;
5676  }
5677 
5678  return is_dirty;
5679 }
5680 
5682 {
5683  return BKE_image_is_dirty_writable(image, NULL);
5684 }
5685 
5687 {
5688  ibuf->userflags |= IB_BITMAPDIRTY;
5689 }
5690 
5692 {
5693  ImageFormatData im_format;
5694  ImbFormatOptions options_dummy;
5695  BKE_imbuf_to_image_format(&im_format, ibuf);
5696  return (BKE_image_imtype_to_ftype(im_format.imtype, &options_dummy) == ibuf->ftype);
5697 }
5698 
5700 {
5702  if (image->cache != NULL) {
5703  struct MovieCacheIter *iter = IMB_moviecacheIter_new(image->cache);
5704 
5705  while (!IMB_moviecacheIter_done(iter)) {
5706  ImBuf *ibuf = IMB_moviecacheIter_getImBuf(iter);
5707  ibuf->ftype = ftype;
5708  ibuf->foptions = *options;
5710  }
5712  }
5714 }
5715 
5717 {
5718  bool has_loaded_ibuf = false;
5719 
5721  if (image->cache != NULL) {
5722  struct MovieCacheIter *iter = IMB_moviecacheIter_new(image->cache);
5723 
5724  while (!IMB_moviecacheIter_done(iter)) {
5725  has_loaded_ibuf = true;
5726  break;
5727  }
5729  }
5731 
5732  return has_loaded_ibuf;
5733 }
5734 
5739 ImBuf *BKE_image_get_ibuf_with_name(Image *image, const char *name)
5740 {
5741  ImBuf *ibuf = NULL;
5742 
5744  if (image->cache != NULL) {
5745  struct MovieCacheIter *iter = IMB_moviecacheIter_new(image->cache);
5746 
5747  while (!IMB_moviecacheIter_done(iter)) {
5748  ImBuf *current_ibuf = IMB_moviecacheIter_getImBuf(iter);
5749  if (STREQ(current_ibuf->name, name)) {
5750  ibuf = current_ibuf;
5751  IMB_refImBuf(ibuf);
5752  break;
5753  }
5755  }
5757  }
5759 
5760  return ibuf;
5761 }
5762 
5773 {
5774  ImBuf *ibuf = NULL;
5775 
5777  if (image->cache != NULL) {
5778  struct MovieCacheIter *iter = IMB_moviecacheIter_new(image->cache);
5779 
5780  while (!IMB_moviecacheIter_done(iter)) {
5781  ibuf = IMB_moviecacheIter_getImBuf(iter);
5782  IMB_refImBuf(ibuf);
5783  break;
5784  }
5786  }
5788 
5789  return ibuf;
5790 }
5791 
5792 static void image_update_views_format(Image *ima, ImageUser *iuser)
5793 {
5794  SceneRenderView *srv;
5795  ImageView *iv;
5796  Scene *scene = iuser->scene;
5797  const bool is_multiview = ((scene->r.scemode & R_MULTIVIEW) != 0) &&
5798  ((ima->flag & IMA_USE_VIEWS) != 0);
5799 
5800  /* reset the image views */
5801  BKE_image_free_views(ima);
5802 
5803  if (!is_multiview) {
5804  /* nothing to do */
5805  }
5806  else if (ima->views_format == R_IMF_VIEWS_STEREO_3D) {
5807  const char *names[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME};
5808 
5809  for (int i = 0; i < 2; i++) {
5810  image_add_view(ima, names[i], ima->filepath);
5811  }
5812  return;
5813  }
5814  else {
5815  /* R_IMF_VIEWS_INDIVIDUAL */
5816  char prefix[FILE_MAX] = {'\0'};
5817  char *name = ima->filepath;
5818  const char *ext = NULL;
5819 
5820  BKE_scene_multiview_view_prefix_get(scene, name, prefix, &ext);
5821 
5822  if (prefix[0] == '\0') {
5823  BKE_image_free_views(ima);
5824  return;
5825  }
5826 
5827  /* create all the image views */
5828  for (srv = scene->r.views.first; srv; srv = srv->next) {
5830  char filepath[FILE_MAX];
5831  SNPRINTF(filepath, "%s%s%s", prefix, srv->suffix, ext);
5832  image_add_view(ima, srv->name, filepath);
5833  }
5834  }
5835 
5836  /* check if the files are all available */
5837  iv = ima->views.last;
5838  while (iv) {
5839  int file;
5840  char str[FILE_MAX];
5841 
5842  STRNCPY(str, iv->filepath);
5844 
5845  /* exists? */
5846  file = BLI_open(str, O_BINARY | O_RDONLY, 0);
5847  if (file == -1) {
5848  ImageView *iv_del = iv;
5849  iv = iv->prev;
5850  BLI_remlink(&ima->views, iv_del);
5851  MEM_freeN(iv_del);
5852  }
5853  else {
5854  iv = iv->prev;
5855  close(file);
5856  }
5857  }
5858 
5859  /* all good */
5860  if (!BKE_image_is_multiview(ima)) {
5861  BKE_image_free_views(ima);
5862  }
5863  }
5864 }
5865 
5866 /**************************** Render Slots ***************************/
5867 
5869 {
5870  RenderSlot *slot = MEM_callocN(sizeof(RenderSlot), "Image new Render Slot");
5871  if (name && name[0]) {
5872  BLI_strncpy(slot->name, name, sizeof(slot->name));
5873  }
5874  else {
5875  int n = BLI_listbase_count(&ima->renderslots) + 1;
5876  BLI_snprintf(slot->name, sizeof(slot->name), "Slot %d", n);
5877  }
5878  BLI_addtail(&ima->renderslots, slot);
5879  return slot;
5880 }
5881 
5882 bool BKE_image_remove_renderslot(Image *ima, ImageUser *iuser, int index)
5883 {
5884  if (index == ima->last_render_slot) {
5885  /* Don't remove render slot while rendering to it. */
5886  if (G.is_rendering) {
5887  return false;
5888  }
5889  }
5890 
5891  int num_slots = BLI_listbase_count(&ima->renderslots);
5892  if (index >= num_slots || num_slots == 1) {
5893  return false;
5894  }
5895 
5896  RenderSlot *remove_slot = BLI_findlink(&ima->renderslots, index);
5897  RenderSlot *current_slot = BLI_findlink(&ima->renderslots, ima->render_slot);
5898  RenderSlot *current_last_slot = BLI_findlink(&ima->renderslots, ima->last_render_slot);
5899 
5900  RenderSlot *next_slot;
5901  if (current_slot == remove_slot) {
5902  next_slot = BLI_findlink(&ima->renderslots, (index == num_slots - 1) ? index - 1 : index + 1);
5903  }
5904  else {
5905  next_slot = current_slot;
5906  }
5907 
5908  /* If the slot to be removed is the slot with the last render,
5909  * make another slot the last render slot. */
5910  if (remove_slot == current_last_slot) {
5911  /* Choose the currently selected slot unless that one is being removed,
5912  * in that case take the next one. */
5913  RenderSlot *next_last_slot;
5914  if (current_slot == remove_slot) {
5915  next_last_slot = next_slot;
5916  }
5917  else {
5918  next_last_slot = current_slot;
5919  }
5920 
5921  if (!iuser) {
5922  return false;
5923  }
5924  Render *re = RE_GetSceneRender(iuser->scene);
5925  if (!re) {
5926  return false;
5927  }
5928  RE_SwapResult(re, &current_last_slot->render);
5929  RE_SwapResult(re, &next_last_slot->render);
5930  current_last_slot = next_last_slot;
5931  }
5932 
5933  current_slot = next_slot;
5934 
5935  BLI_remlink(&ima->renderslots, remove_slot);
5936 
5937  ima->render_slot = BLI_findindex(&ima->renderslots, current_slot);
5938  ima->last_render_slot = BLI_findindex(&ima->renderslots, current_last_slot);
5939 
5940  if (remove_slot->render) {
5941  RE_FreeRenderResult(remove_slot->render);
5942  }
5943  MEM_freeN(remove_slot);
5944 
5945  return true;
5946 }
5947 
5948 bool BKE_image_clear_renderslot(Image *ima, ImageUser *iuser, int index)
5949 {
5950  if (index == ima->last_render_slot) {
5951  if (!iuser) {
5952  return false;
5953  }
5954  if (G.is_rendering) {
5955  return false;
5956  }
5957  Render *re = RE_GetSceneRender(iuser->scene);
5958  if (!re) {
5959  return false;
5960  }
5961  RE_ClearResult(re);
5962  return true;
5963  }
5964 
5965  RenderSlot *slot = BLI_findlink(&ima->renderslots, index);
5966  if (!slot) {
5967  return false;
5968  }
5969  if (slot->render) {
5970  RE_FreeRenderResult(slot->render);
5971  slot->render = NULL;
5972  }
5973  return true;
5974 }
5975 
5977 {
5978  /* Can be NULL for images without render slots. */
5979  return BLI_findlink(&ima->renderslots, index);
5980 }
typedef float(TangentPoint)[2]
void BKE_color_managed_view_settings_init_default(struct ColorManagedViewSettings *settings, const struct ColorManagedDisplaySettings *display_settings)
Definition: colortools.c:1793
void BKE_color_managed_colorspace_settings_copy(struct ColorManagedColorspaceSettings *colorspace_settings, const struct ColorManagedColorspaceSettings *settings)
void BKE_color_managed_colorspace_settings_init(struct ColorManagedColorspaceSettings *colorspace_settings)
Definition: colortools.c:1846
void BKE_color_managed_display_settings_init(struct ColorManagedDisplaySettings *settings)
Definition: colortools.c:1753
@ G_FILE_AUTOPACK
Definition: BKE_global.h:166
void BKE_icon_id_delete(struct ID *id)
Definition: icons.cc:919
void BKE_icon_changed(const int icon_id)
Definition: icons.cc:678
void BKE_previewimg_free(struct PreviewImage **prv)
Definition: icons.cc:295
void BKE_previewimg_id_copy(struct ID *new_id, const struct ID *old_id)
void BKE_previewimg_blend_read(struct BlendDataReader *reader, struct PreviewImage *prv)
Definition: icons.cc:651
void BKE_previewimg_blend_write(struct BlendWriter *writer, const struct PreviewImage *prv)
int BKE_icon_id_ensure(struct ID *id)
Definition: icons.cc:740
@ IDTYPE_FLAGS_NO_ANIMDATA
Definition: BKE_idtype.h:51
void(* IDTypeForeachCacheFunctionCallback)(struct ID *id, const struct IDCacheKey *cache_key, void **cache_p, uint flags, void *user_data)
Definition: BKE_idtype.h:89
void() StampCallback(void *data, const char *propname, char *propvalue, int len)
Definition: BKE_image.h:61
#define IMA_OK_LOADED
Definition: BKE_image.h:158
#define IMA_CHAN_FLAG_RGB
Definition: BKE_image.h:171
#define IMA_MAX_SPACE
Definition: BKE_image.h:47
#define IMA_UDIM_MAX
Definition: BKE_image.h:48
#define IMA_OK
Definition: BKE_image.h:157
void BKE_image_buf_fill_checker_color(unsigned char *rect, float *rect_float, int width, int height)
Definition: image_gen.c:459
void BKE_image_buf_fill_checker(unsigned char *rect, float *rect_float, int width, int height)
Definition: image_gen.c:209
#define IMA_SIGNAL_COLORMANAGE
Definition: BKE_image.h:168
#define IMA_CHAN_FLAG_ALPHA
Definition: BKE_image.h:172
#define IMA_SIGNAL_FREE
Definition: BKE_image.h:163
#define IMA_CHAN_FLAG_BW
Definition: BKE_image.h:170
void BKE_image_free_gputextures(struct Image *ima)
Definition: image_gpu.c:517
#define IMA_SIGNAL_SRC_CHANGE
Definition: BKE_image.h:165
#define IMA_SIGNAL_USER_NEW_IMAGE
Definition: BKE_image.h:167
#define IMA_SIGNAL_RELOAD
Definition: BKE_image.h:162
void BKE_image_buf_fill_color(unsigned char *rect, float *rect_float, int width, int height, const float color[4])
Definition: image_gen.c:81
@ LIB_ID_COPY_NO_PREVIEW
Definition: BKE_lib_id.h:116
void * BKE_libblock_alloc(struct Main *bmain, short type, const char *name, const int flag) ATTR_WARN_UNUSED_RESULT
Definition: lib_id.c:1062
void BKE_id_free(struct Main *bmain, void *idv)
void id_us_ensure_real(struct ID *id)
Definition: lib_id.c:238
void id_us_plus(struct ID *id)
Definition: lib_id.c:288
void BKE_id_blend_write(struct BlendWriter *writer, struct ID *id)
Definition: lib_id.c:2395
const char * BKE_main_blendfile_path(const struct Main *bmain) ATTR_NONNULL()
const char * BKE_main_blendfile_path_from_global(void)
Definition: main.c:439
#define SH_NODE_TEX_ENVIRONMENT
Definition: BKE_node.h:1022
bool nodeUpdateID(struct bNodeTree *ntree, struct ID *id)
Definition: node.cc:4346
#define TEX_NODE_IMAGE
Definition: BKE_node.h:1333
struct PackedFile * BKE_packedfile_new_from_memory(void *mem, int memlen)
Definition: packedFile.c:181
struct PackedFile * BKE_packedfile_duplicate(const struct PackedFile *pf_src)
void BKE_packedfile_blend_write(struct BlendWriter *writer, struct PackedFile *pf)
Definition: packedFile.c:876
struct PackedFile * BKE_packedfile_new(struct ReportList *reports, const char *filename, const char *basepath)
Definition: packedFile.c:192
void BKE_packedfile_blend_read(struct BlendDataReader *reader, struct PackedFile **pf_p)
Definition: packedFile.c:885
void BKE_packedfile_free(struct PackedFile *pf)
Definition: packedFile.c:155
void BKE_report(ReportList *reports, ReportType type, const char *message)
Definition: report.c:104
bool BKE_scene_multiview_is_stereo3d(const struct RenderData *rd)
bool BKE_scene_multiview_is_render_view_active(const struct RenderData *rd, const struct SceneRenderView *srv)
void BKE_scene_multiview_view_prefix_get(struct Scene *scene, const char *name, char *r_prefix, const char **r_ext)
Definition: scene.c:3301
const char * BKE_scene_find_last_marker_name(const struct Scene *scene, int frame)
int BKE_scene_multiview_num_views_get(const struct RenderData *rd)
struct bScreen * BKE_workspace_active_screen_get(const struct WorkSpaceInstanceHook *hook) GETTER_ATTRS
float BLF_width_max(int fontid) ATTR_WARN_UNUSED_RESULT
Definition: blf.c:768
void BLF_draw_buffer(int fontid, const char *str, size_t len) ATTR_NONNULL(2)
Definition: blf.c:913
int blf_mono_font_render
Definition: blf.c:71
#define BLF_DRAW_STR_DUMMY_MAX
Definition: BLF_api.h:283
void BLF_disable(int fontid, int option)
Definition: blf.c:283
void BLF_buffer_col(int fontid, const float rgba[4]) ATTR_NONNULL(2)
Definition: blf.c:871
float BLF_descender(int fontid) ATTR_WARN_UNUSED_RESULT
Definition: blf.c:779
void BLF_buffer(int fontid, float *fbuf, unsigned char *cbuf, int w, int h, int nch, struct ColorManagedDisplay *display)
Definition: blf.c:851
void BLF_size(int fontid, int size, int dpi)
Definition: blf.c:367
void BLF_enable(int fontid, int option)
Definition: blf.c:274
int BLF_height_max(int fontid) ATTR_WARN_UNUSED_RESULT
Definition: blf.c:757
#define BLF_WORD_WRAP
Definition: BLF_api.h:275
void BLF_wordwrap(int fontid, int wrap_width)
Definition: blf.c:822
void BLF_position(int fontid, float x, float y, float z)
Definition: blf.c:312
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define ATTR_FALLTHROUGH
#define BLI_INLINE
int BLI_open(const char *filename, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: fileops.c:1017
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:349
#define O_BINARY
Definition: BLI_fileops.h:182
unsigned int BLI_ghashutil_strhash_p(const void *ptr)
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:124
void BLI_addhead(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:87
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
void BLI_freelinkN(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:281
void void void void void BLI_duplicatelist(struct ListBase *dst, const struct ListBase *src) ATTR_NONNULL(1
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
Definition: BLI_listbase.h:128
void BLI_insertlinkafter(struct ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition: listbase.c:352
int BLI_listbase_count_at_most(const struct ListBase *listbase, const int count_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:547
void * BLI_findstring(const struct ListBase *listbase, const char *id, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:133
int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BLI_insertlinkbefore(struct ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition: listbase.c:395
void * BLI_findlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
int BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
MINLINE int integer_digits_i(const int i)
MINLINE void srgb_to_linearrgb_v4(float linear[4], const float srgb[4])
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE float len_squared_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v2_v2(float r[2], const float a[2])
MINLINE void copy_v2_v2(float r[2], const float a[2])
MINLINE bool equals_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void zero_v2(float r[2])
@ BLI_MEMPOOL_NOP
Definition: BLI_mempool.h:77
BLI_mempool * BLI_mempool_create(unsigned int esize, unsigned int totelem, unsigned int pchunk, unsigned int flag) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition: BLI_mempool.c:268
void * BLI_mempool_alloc(BLI_mempool *pool) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition: BLI_mempool.c:334
void BLI_mempool_destroy(BLI_mempool *pool) ATTR_NONNULL(1)
Definition: BLI_mempool.c:757
bool BLI_path_extension_check_array(const char *str, const char **ext_array) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
Definition: path_util.c:1487
const char * BLI_path_basename(const char *path) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
Definition: path_util.c:1868
bool BLI_path_frame(char *path, int frame, int digits) ATTR_NONNULL()
Definition: path_util.c:802
bool BLI_make_existing_file(const char *name)
Definition: path_util.c:1347
#define FILE_MAX
bool BLI_path_extension_ensure(char *path, size_t maxlen, const char *ext) ATTR_NONNULL()
Definition: path_util.c:1601
int BLI_path_sequence_decode(const char *string, char *head, char *tail, unsigned short *r_num_len)
Definition: path_util.c:83
bool BLI_path_extension_replace(char *path, size_t maxlen, const char *ext) ATTR_NONNULL()
Definition: path_util.c:1571
bool BLI_path_extension_check(const char *str, const char *ext) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
Definition: path_util.c:1459
void BLI_path_sequence_encode(char *string, const char *head, const char *tail, unsigned short numlen, int pic)
Definition: path_util.c:154
bool BLI_path_extension_check_n(const char *str,...) ATTR_NONNULL(1) ATTR_SENTINEL(0)
Definition: path_util.c:1464
bool BLI_path_abs(char *path, const char *basepath) ATTR_NONNULL()
Definition: path_util.c:1016
#define BLI_path_cmp
bool BLI_path_suffix(char *string, size_t maxlen, const char *suffix, const char *sep) ATTR_NONNULL()
Definition: path_util.c:669
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition: BLI_rect.h:161
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition: BLI_rect.h:165
#define STRNCPY(dst, src)
Definition: BLI_string.h:163
#define SNPRINTF(dst, format,...)
Definition: BLI_string.h:165
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:70
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
void BLI_hostname_get(char *buffer, size_t bufsize)
Definition: system.c:166
void BLI_thread_unlock(int type)
Definition: threads.cc:389
void BLI_mutex_free(ThreadMutex *mutex)
Definition: threads.cc:428
void BLI_thread_lock(int type)
Definition: threads.cc:384
ThreadMutex * BLI_mutex_alloc(void)
Definition: threads.cc:421
int BLI_thread_is_main(void)
Definition: threads.cc:234
@ LOCK_DRAW_IMAGE
Definition: BLI_threads.h:68
@ LOCK_COLORMANAGE
Definition: BLI_threads.h:73
@ LOCK_VIEWER
Definition: BLI_threads.h:69
void BLI_mutex_lock(ThreadMutex *mutex)
Definition: threads.cc:401
void BLI_mutex_unlock(ThreadMutex *mutex)
Definition: threads.cc:406
pthread_mutex_t ThreadMutex
Definition: BLI_threads.h:83
size_t BLI_timecode_string_from_time(char *str, const size_t maxncpy, const int brevity_level, const float time_seconds, const double scene_fps, const short timecode_style) ATTR_NONNULL()
Definition: timecode.c:51
size_t BLI_timecode_string_from_time_simple(char *str, const size_t maxncpy, const double time_seconds) ATTR_NONNULL()
Definition: timecode.c:206
#define UNUSED_VARS(...)
#define UNUSED(x)
#define ELEM(...)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define AT
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define STREQ(a, b)
#define BLO_read_data_address(reader, ptr_p)
bool BLO_read_data_is_undo(BlendDataReader *reader)
Definition: readfile.c:5770
#define BLO_write_id_struct(writer, struct_name, id_address, id)
#define BLO_write_struct(writer, struct_name, data_ptr)
void BLO_read_list(BlendDataReader *reader, struct ListBase *list)
Definition: readfile.c:5654
#define BLO_write_struct_list(writer, struct_name, list_ptr)
bool BLO_write_is_undo(BlendWriter *writer)
Definition: writefile.c:1412
#define BLT_I18NCONTEXT_ID_IMAGE
#define CLOG_STR_ERROR(clg_ref, str)
Definition: CLG_log.h:210
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
@ DAG_EVAL_RENDER
Definition: DEG_depsgraph.h:62
void DEG_id_tag_update(struct ID *id, int flag)
float DEG_get_ctime(const Depsgraph *graph)
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
@ ID_RECALC_COPY_ON_WRITE
Definition: DNA_ID.h:654
@ LIB_TAG_DOIT
Definition: DNA_ID.h:554
#define ID_BLEND_PATH_FROM_GLOBAL(_id)
Definition: DNA_ID.h:421
#define ID_BLEND_PATH(_bmain, _id)
Definition: DNA_ID.h:419
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition: DNA_ID.h:445
#define FILTER_ID_IM
Definition: DNA_ID.h:712
@ INDEX_ID_IM
Definition: DNA_ID.h:808
@ ID_WM
Definition: DNA_ID_enums.h:84
@ ID_CA
Definition: DNA_ID_enums.h:68
@ ID_TE
Definition: DNA_ID_enums.h:64
@ ID_IM
Definition: DNA_ID_enums.h:65
@ ID_NT
Definition: DNA_ID_enums.h:80
@ ID_LA
Definition: DNA_ID_enums.h:67
@ ID_SCE
Definition: DNA_ID_enums.h:57
@ ID_WO
Definition: DNA_ID_enums.h:71
@ ID_SIM
Definition: DNA_ID_enums.h:96
@ ID_MA
Definition: DNA_ID_enums.h:63
@ ID_OB
Definition: DNA_ID_enums.h:59
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:44
struct Image Image
@ IMA_DEINTERLACE
@ IMA_USER_FRAME_IN_RANGE
@ IMA_USE_VIEWS
@ IMA_VIEW_AS_RENDER
@ IMA_GPU_REFRESH
#define IMA_NEED_FRAME_RECALC
#define IMA_ANIM_ALWAYS
@ IMA_GENTYPE_GRID_COLOR
@ IMA_GENTYPE_GRID
@ IMA_GEN_FLOAT
@ IMA_TYPE_MULTILAYER
@ IMA_TYPE_UV_TEST
@ IMA_TYPE_R_RESULT
@ IMA_TYPE_COMPOSITE
@ IMA_TYPE_IMAGE
@ IMA_ALPHA_IGNORE
@ IMA_ALPHA_STRAIGHT
@ IMA_ALPHA_PREMUL
@ IMA_ALPHA_CHANNEL_PACKED
@ IMA_SRC_FILE
@ IMA_SRC_MOVIE
@ IMA_SRC_GENERATED
@ IMA_SRC_VIEWER
@ IMA_SRC_TILED
@ IMA_SRC_SEQUENCE
#define IMA_SHOW_STEREO
@ TEXTARGET_2D_ARRAY
@ TEXTARGET_COUNT
@ TEXTARGET_TILE_MAPPING
#define NTREE_TEXTURE
#define NTREE_COMPOSIT
#define NTREE_SHADER
Object is a sort of wrapper for general info.
@ OB_EMPTY_IMAGE
@ OB_CAMERA
#define R_IMF_IMTYPE_TARGA
#define R_STAMP_ALL
#define R_IMF_IMTYPE_FFMPEG
#define R_IMF_IMTYPE_H264
#define STEREO_LEFT_NAME
#define R_STAMP_RENDERTIME
#define R_BORDER
#define R_STAMP_HOSTNAME
#define R_MULTIVIEW
#define R_IMF_IMTYPE_THEORA
#define R_IMF_IMTYPE_CINEON
#define R_CROP
#define CFRA
#define R_IMF_IMTYPE_AVIJPEG
#define R_STAMP_MARKER
#define R_IMF_IMTYPE_TIFF
#define R_STAMP_MEMORY
#define R_IMF_IMTYPE_JP2
#define R_IMF_JP2_CODEC_J2K
#define R_IMF_EXR_CODEC_ZIP
#define R_STAMP_FRAME_RANGE
#define R_IMF_IMTYPE_RAWTGA
#define R_IMF_IMTYPE_PNG
#define R_IMF_IMTYPE_IRIZ
#define R_IMF_IMTYPE_MULTILAYER
#define R_IMF_JP2_FLAG_CINE_PRESET
#define R_IMF_IMTYPE_BMP
#define R_IMF_IMTYPE_IRIS
@ R_IMF_CHAN_DEPTH_8
@ R_IMF_CHAN_DEPTH_16
@ R_IMF_CHAN_DEPTH_12
@ R_IMF_CHAN_DEPTH_10
@ R_IMF_CHAN_DEPTH_32
#define R_STAMP_HIDE_LABELS
#define R_STAMP_NOTE
#define R_IMF_IMTYPE_XVID
#define R_STAMP_FRAME
#define R_IMF_PLANES_RGBA
#define R_IMF_CINEON_FLAG_LOG
#define R_STAMP_CAMERA
#define R_STAMP_DATE
#define STEREO_LEFT_SUFFIX
#define R_STAMP_FILENAME
#define RE_PASSNAME_Z
#define R_IMF_IMTYPE_AVIRAW
#define R_IMF_IMTYPE_DPX
@ R_IMF_VIEWS_STEREO_3D
@ R_IMF_VIEWS_INDIVIDUAL
#define R_IMF_IMTYPE_DDS
#define STEREO_RIGHT_NAME
#define STEREO_RIGHT_SUFFIX
#define R_IMF_IMTYPE_RADHDR
#define R_STAMP_SCENE
#define FPS
#define R_IMF_JP2_FLAG_YCC
#define R_STAMP_SEQSTRIP
#define R_IMF_JP2_CODEC_JP2
#define R_IMF_JP2_FLAG_CINE_48
#define R_STAMP_CAMERALENS
#define R_IMF_IMTYPE_INVALID
#define R_IMF_FLAG_ZBUF
#define R_IMF_IMTYPE_OPENEXR
#define R_STAMP_TIME
@ R_IMF_TIFF_CODEC_PACKBITS
@ R_IMF_TIFF_CODEC_DEFLATE
@ R_IMF_TIFF_CODEC_NONE
@ R_IMF_TIFF_CODEC_LZW
#define FRA2TIME(a)
#define R_IMF_IMTYPE_PSD
#define R_IMF_IMTYPE_JPEG90
@ SPACE_IMAGE
#define IMG_SIZE_FALLBACK
@ USER_TIMECODE_SMPTE_FULL
static AppView * view
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
void GPU_texture_free(GPUTexture *tex)
Definition: gpu_texture.cc:508
bool IMB_colormanagement_space_name_is_data(const char *name)
void IMB_colormanagement_assign_rect_colorspace(struct ImBuf *ibuf, const char *name)
const char * IMB_colormanagement_role_colorspace_name_get(int role)
void IMB_colormanagement_check_is_data(struct ImBuf *ibuf, const char *name)
@ COLOR_ROLE_DEFAULT_FLOAT
@ COLOR_ROLE_DATA
@ COLOR_ROLE_DEFAULT_BYTE
struct ColorManagedDisplay * IMB_colormanagement_display_get_named(const char *name)
struct ImBuf * IMB_makeSingleUser(struct ImBuf *ibuf)
Definition: allocimbuf.c:246
void IMB_freezbuffloatImBuf(struct ImBuf *ibuf)
Definition: allocimbuf.c:186
bool IMB_scaleImBuf(struct ImBuf *ibuf, unsigned int newx, unsigned int newy)
Definition: scaling.c:1667
void IMB_free_anim(struct anim *anim)
Definition: anim_movie.c:207
struct ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, unsigned char planes, unsigned int flags)
Definition: allocimbuf.c:478
@ IMB_PROXY_NONE
Definition: IMB_imbuf.h:318
void IMB_freeImBuf(struct ImBuf *ibuf)
Definition: allocimbuf.c:211
void imb_freerectImBuf(struct ImBuf *ibuf)
Definition: allocimbuf.c:115
void IMB_ImBufFromStereo3d(struct Stereo3dFormat *s3d, struct ImBuf *ibuf_stereo, struct ImBuf **r_ibuf_left, struct ImBuf **r_ibuf_right)
Definition: stereoimbuf.c:1279
void IMB_refImBuf(struct ImBuf *ibuf)
Definition: allocimbuf.c:239
struct anim * IMB_open_anim(const char *name, int ib_flags, int streamindex, char colorspace[IM_MAX_SPACE])
Definition: anim_movie.c:282
void buf_rectfill_area(unsigned char *rect, float *rectf, int width, int height, const float col[4], struct ColorManagedDisplay *display, int x1, int y1, int x2, int y2)
Definition: rectop.c:1164
struct ImBuf * IMB_loadiffname(const char *filepath, int flags, char colorspace[IM_MAX_SPACE])
Definition: readimage.c:224
struct ImBuf * IMB_ibImageFromMemory(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE], const char *descr)
Definition: readimage.c:99
struct ImBuf * IMB_anim_absolute(struct anim *anim, int position, IMB_Timecode_Type tc, IMB_Proxy_Size preview_size)
Definition: anim_movie.c:1580
bool IMB_saveiff(struct ImBuf *ibuf, const char *filepath, int flags)
Definition: writeimage.c:44
int IMB_anim_get_duration(struct anim *anim, IMB_Timecode_Type tc)
Definition: anim_movie.c:1671
@ IMB_TC_NONE
Definition: IMB_imbuf.h:300
@ IMB_TC_RECORD_RUN
Definition: IMB_imbuf.h:304
Contains defines and structs used throughout the imbuf module.
#define OPENEXR_HALF
@ IB_PERSISTENT
@ IB_BITMAPDIRTY
@ IB_DISPLAY_BUFFER_INVALID
#define RAWTGA
#define PNG_16BIT
const char * imb_ext_movie[]
Definition: util.c:95
#define IMB_MIPMAP_LEVELS
@ IB_zbuf
@ IB_animdeinterlace
@ IB_alphamode_channel_packed
@ IB_alphamode_premul
@ IB_alphamode_ignore
@ IB_rectfloat
@ IB_metadata
@ IB_multilayer
@ IB_alphamode_detect
@ IB_zbuffloat
@ IB_mem
@ IB_test
@ IB_rect
const char * imb_ext_image[]
Definition: util.c:60
#define OPENEXR_COMPRESS
#define IMB_FTYPE_NONE
void IMB_metadata_set_field(struct IDProperty *metadata, const char *key, const char *value)
Definition: metadata.c:89
void IMB_metadata_foreach(struct ImBuf *ibuf, IMBMetadataForeachCb callback, void *userdata)
Definition: metadata.c:107
bool IMB_metadata_get_field(struct IDProperty *metadata, const char *key, char *value, const size_t len)
Definition: metadata.c:60
void IMB_metadata_ensure(struct IDProperty **metadata)
Definition: metadata.c:41
void IMB_metadata_free(struct IDProperty *metadata)
Definition: metadata.c:51
struct MovieCache * IMB_moviecache_create(const char *name, int keysize, GHashHashFP hashfp, GHashCmpFP cmpfp)
Definition: moviecache.c:262
void IMB_moviecache_set_getdata_callback(struct MovieCache *cache, MovieCacheGetKeyDataFP getdatafp)
Definition: moviecache.c:289
void IMB_moviecacheIter_free(struct MovieCacheIter *iter)
Definition: moviecache.c:580
void IMB_moviecache_remove(struct MovieCache *cache, void *userkey)
Definition: moviecache.c:390
void IMB_moviecache_cleanup(struct MovieCache *cache, bool(cleanup_check_cb)(struct ImBuf *ibuf, void *userkey, void *userdata), void *userdata)
struct MovieCacheIter * IMB_moviecacheIter_new(struct MovieCache *cache)
Definition: moviecache.c:570
struct ImBuf * IMB_moviecache_get(struct MovieCache *cache, void *userkey)
Definition: moviecache.c:398
void IMB_moviecache_put(struct MovieCache *cache, void *userkey, struct ImBuf *ibuf)
Definition: moviecache.c:364
void IMB_moviecache_free(struct MovieCache *cache)
Definition: moviecache.c:434
bool IMB_moviecacheIter_done(struct MovieCacheIter *iter)
Definition: moviecache.c:585
void IMB_moviecacheIter_step(struct MovieCacheIter *iter)
Definition: moviecache.c:590
void * IMB_moviecacheIter_getUserKey(struct MovieCacheIter *iter)
Definition: moviecache.c:601
struct ImBuf * IMB_moviecacheIter_getImBuf(struct MovieCacheIter *iter)
Definition: moviecache.c:595
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
static void init_data(ModifierData *md)
Group RGB to Bright Vector Camera Vector Combine Material Light Line Style Layer Add Ambient Diffuse Glossy Refraction Transparent Toon Principled Hair Volume Principled Light Particle Volume TEX_IMAGE
Group RGB to Bright Vector Camera Vector Combine Material Light Line Style Layer Add Ambient Diffuse Glossy Refraction Transparent Toon Principled Hair Volume Principled Light Particle Volume SH_NODE_TEX_IMAGE
Group RGB to Bright Vector Camera Vector Combine Material Light Line Style Layer Add Ambient Diffuse Glossy Refraction Transparent Toon Principled Hair Volume Principled Light Particle Volume Image Sky Noise Wave Voronoi Brick Texture Vector Combine Vertex Separate Vector White RGB Map Separate Set CMP_NODE_IMAGE
Platform independent time functions.
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
short gpuflag
struct MovieCache * cache
struct ListBase packedfiles
struct PreviewImage * preview
char gen_type
short last_render_slot
ListBase anims
ColorManagedColorspaceSettings colorspace_settings
int lastused
float aspy
ListBase renderslots
char views_format
char filepath[1024]
int gpuframenr
ListBase tiles
short type
struct GPUTexture * gputexture[3][2]
short source
short render_slot
float aspx
short gen_depth
struct RenderResult * rr
ListBase gpu_refresh_areas
char gen_flag
ListBase views
char alpha_mode
int lastframe
struct Stereo3dFormat * stereo3d_format
float gen_color[4]
CCL_NAMESPACE_BEGIN struct Options options
OperationNode * node
FILE * file
double time
const char * label
Scene scene
World world
Simulation simulation
const Depsgraph * depsgraph
void * user_data
DEGForeachIDComponentCallback callback
bNodeTree * ntree
#define str(s)
#define pf(_x, _i)
Prefetch 64.
Definition: gim_memory.h:48
@ IMB_FTYPE_BMP
@ IMB_FTYPE_IMAGIC
@ IMB_FTYPE_JPG
@ IMB_FTYPE_TGA
@ IMB_FTYPE_OPENEXR
@ IMB_FTYPE_PNG
static void image_blend_read_lib(BlendLibReader *UNUSED(reader), ID *id)
Definition: image.c:306
static void image_init_color_management(Image *ima)
Definition: image.c:769
ImBuf * BKE_image_get_first_ibuf(Image *image)
Definition: image.c:5772
static void image_free_tile(Image *ima, ImageTile *tile)
Definition: image.c:3471
static RenderPass * image_render_pass_get(RenderLayer *rl, const int pass, const int view, int *r_passindex)
Definition: image.c:3656
ImageTile * BKE_image_get_tile_from_iuser(Image *ima, const ImageUser *iuser)
Definition: image.c:702
void BKE_image_ensure_viewer_views(const RenderData *rd, Image *ima, ImageUser *iuser)
Definition: image.c:3203
static void image_walk_ntree_all_users(bNodeTree *ntree, ID *id, void *customdata, void callback(Image *ima, ID *iuser_id, ImageUser *iuser, void *customdata))
Definition: image.c:3246
bool BKE_imtype_supports_compress(const char imtype)
Definition: image.c:1467
void BKE_image_user_frame_calc(Image *ima, ImageUser *iuser, int cfra)
Definition: image.c:5335
RenderSlot * BKE_image_get_renderslot(Image *ima, int index)
Definition: image.c:5976
BLI_INLINE bool image_quick_test(Image *ima, const ImageUser *iuser)
Definition: image.c:4949
void BKE_image_alpha_mode_from_extension(Image *image)
Definition: image.c:806
static void image_viewer_create_views(const RenderData *rd, Image *ima)
Definition: image.c:3186
void BKE_image_get_size_fl(Image *image, ImageUser *iuser, float r_size[2])
Definition: image.c:5535
void BKE_image_free_buffers_ex(Image *ima, bool do_lock)
Definition: image.c:490
static void stampdata(const Scene *scene, Object *camera, StampData *stamp_data, int do_prefix, bool use_dynamic)
Definition: image.c:1971
bool BKE_image_user_id_has_animation(ID *id)
Definition: image.c:5413
static ImBuf * image_load_sequence_multilayer(Image *ima, ImageUser *iuser, int entry, int frame)
Definition: image.c:4237
ImageTile * BKE_image_get_tile(Image *ima, int tile_number)
Definition: image.c:672
void BKE_image_get_aspect(Image *image, float *r_aspx, float *r_aspy)
Definition: image.c:5544
bool BKE_image_fill_tile(struct Image *ima, ImageTile *tile, int width, int height, const float color[4], int gen_type, int planes, bool is_float)
Definition: image.c:3783
int BKE_image_imtype_to_ftype(const char imtype, ImbFormatOptions *r_options)
Definition: image.c:1328
int BKE_imbuf_write_stamp(Scene *scene, struct RenderResult *rr, ImBuf *ibuf, const char *name, const struct ImageFormatData *imf)
Definition: image.c:3036
static void imagecache_keydata(void *userkey, int *framenr, int *proxy, int *render_flags)
Definition: image.c:382
static void metadata_copy_custom_fields(const char *field, const char *value, void *rr_v)
Definition: image.c:2791
static void metadata_get_field(void *data, const char *propname, char *propvalue, int len)
Definition: image.c:2777
#define BUFF_MARGIN_X
bool BKE_image_has_multiple_ibufs(Image *image)
Definition: image.c:5646
static void image_remove_ibuf(Image *ima, int index, int entry)
Definition: image.c:595
void BKE_image_release_renderresult(Scene *scene, Image *ima)
Definition: image.c:3947
static void image_get_entry_and_index(Image *ima, ImageUser *iuser, int *r_entry, int *r_index)
Definition: image.c:4841
static void image_init_data(ID *id)
Definition: image.c:120
static ImBuf * add_ibuf_size(unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short gen_type, const float color[4], ColorManagedColorspaceSettings *colorspace_settings)
Definition: image.c:885
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
Definition: image.c:5113
bool BKE_imtype_supports_zbuf(const char imtype)
Definition: image.c:1457
static bool imagecache_check_free_anim(ImBuf *ibuf, void *UNUSED(userkey), void *userdata)
Definition: image.c:1298
void BKE_stamp_info_from_imbuf(RenderResult *rr, struct ImBuf *ibuf)
Definition: image.c:2800
bool BKE_imtype_requires_linear_float(const char imtype)
Definition: image.c:1487
struct ImagePoolItem ImagePoolItem
static unsigned int imagecache_hashhash(const void *key_v)
Definition: image.c:368
static uintptr_t image_mem_size(Image *image)
Definition: image.c:1189
bool BKE_image_clear_renderslot(Image *ima, ImageUser *iuser, int index)
Definition: image.c:5948
static void image_tag_reload(Image *ima, ID *iuser_id, ImageUser *iuser, void *customdata)
Definition: image.c:3435
void BKE_image_tag_time(Image *ima)
Definition: image.c:1184
static void image_init_multilayer_multiview(Image *ima, RenderResult *rr)
Definition: image.c:3896
void BKE_image_merge(Main *bmain, Image *dest, Image *source)
Definition: image.c:620
static void image_user_id_has_animation(Image *ima, ID *UNUSED(iuser_id), ImageUser *UNUSED(iuser), void *customdata)
Definition: image.c:5403
bool BKE_image_memorypack(Image *ima)
Definition: image.c:1083
void BKE_image_packfiles(ReportList *reports, Image *ima, const char *basepath)
Definition: image.c:1134
char BKE_imtype_valid_depths(const char imtype)
Definition: image.c:1541
#define IMA_MAKE_INDEX(entry, index)
Definition: image.c:356
Image * BKE_image_load(Main *bmain, const char *filepath)
Definition: image.c:811
void BKE_image_packfiles_from_mem(ReportList *reports, Image *ima, char *data, const size_t data_len)
Definition: image.c:1166
bool BKE_image_remove_tile(struct Image *ima, ImageTile *tile)
Definition: image.c:3765
bool BKE_image_is_dirty(Image *image)
Definition: image.c:5681
static const char * stamp_metadata_fields[]
Definition: image.c:2654
void BKE_imbuf_stamp_info(RenderResult *rr, struct ImBuf *ibuf)
Definition: image.c:2784
void BKE_stamp_data_free(StampData *stamp_data)
Definition: image.c:2757
struct StampData * BKE_stamp_info_from_scene_static(const Scene *scene)
Definition: image.c:2638
void BKE_image_free_anim_ibufs(Image *ima, int except_frame)
Definition: image.c:1306
Image * BKE_image_add_from_imbuf(Main *bmain, ImBuf *ibuf, const char *name)
Definition: image.c:1029
bool BKE_image_has_filepath(Image *ima)
Definition: image.c:5632
void BKE_image_pool_free(ImagePool *pool)
Definition: image.c:5181
int BKE_image_user_frame_get(const ImageUser *iuser, int cfra, bool *r_is_in_range)
Definition: image.c:5271
static void image_tag_frame_recalc(Image *ima, ID *iuser_id, ImageUser *iuser, void *customdata)
Definition: image.c:3420
static void stampdata_from_template(StampData *stamp_data, const Scene *scene, const StampData *stamp_data_template, bool do_prefix)
Definition: image.c:2149
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
Definition: image.c:5100
bool BKE_image_has_alpha(struct Image *image)
Definition: image.c:5485
ImagePool * BKE_image_pool_new(void)
Definition: image.c:5173
#define CALL(member, value_str)
static void image_user_id_eval_animation(Image *ima, ID *UNUSED(iduser_id), ImageUser *iuser, void *customdata)
Definition: image.c:5423
struct StampData StampData
struct anim * openanim_noload(const char *name, int flags, int streamindex, char colorspace[IMA_MAX_SPACE])
Definition: image.c:3103
Image * BKE_image_load_exists(Main *bmain, const char *filepath)
Definition: image.c:880
static ImBuf * image_load_sequence_file(Image *ima, ImageUser *iuser, int entry, int frame)
Definition: image.c:4187
static void image_editors_update_frame(Image *ima, ID *UNUSED(iuser_id), ImageUser *iuser, void *customdata)
Definition: image.c:5383
void BKE_image_free_packedfiles(Image *ima)
Definition: image.c:463
void BKE_image_multiview_index(Image *ima, ImageUser *iuser)
Definition: image.c:3846
static ImBuf * image_get_cached_ibuf(Image *ima, ImageUser *iuser, int *r_entry, int *r_index)
Definition: image.c:4871
static void image_walk_id_all_users(ID *id, bool skip_nested_nodes, void *customdata, void callback(Image *ima, ID *iuser_id, ImageUser *iuser, void *customdata))
Definition: image.c:3290
void BKE_image_free_buffers(Image *ima)
Definition: image.c:515
void BKE_imbuf_write_prepare(ImBuf *ibuf, const ImageFormatData *imf)
Definition: image.c:2837
void BKE_image_stamp_buf(Scene *scene, Object *camera, const StampData *stamp_data_template, unsigned char *rect, float *rectf, int width, int height, int channels)
Definition: image.c:2238
static void image_foreach_cache(ID *id, IDTypeForeachCacheFunctionCallback function_callback, void *user_data)
Definition: image.c:196
void BKE_image_backup_render(Scene *scene, Image *ima, bool free_current_slot)
Definition: image.c:3971
float * BKE_image_get_float_pixels_for_frame(struct Image *image, int frame, int tile)
Definition: image.c:5587
#define BUFF_MARGIN_Y
int BKE_image_path_ensure_ext_from_imformat(char *string, const ImageFormatData *im_format)
Definition: image.c:1775
char BKE_imtype_from_arg(const char *imtype_arg)
Definition: image.c:1569
void BKE_images_init(void)
Definition: image.c:431
StampData * BKE_stamp_data_copy(const StampData *stamp_data)
Definition: image.c:2741
#define IMA_NO_INDEX
Definition: image.c:353
bool BKE_image_has_opengl_texture(Image *ima)
Definition: image.c:660
static ImBuf * image_get_ibuf_multilayer(Image *ima, ImageUser *iuser)
Definition: image.c:4581
static void image_free_anims(Image *ima)
Definition: image.c:473
bool BKE_image_scale(Image *image, int width, int height)
Definition: image.c:643
BLI_INLINE ImBuf * image_pool_find_item(ImagePool *pool, Image *image, int entry, int index, bool *found)
Definition: image.c:5196
bool BKE_image_is_multilayer(Image *ima)
Definition: image.c:3868
static bool do_add_image_extension(char *string, const char imtype, const ImageFormatData *im_format)
Definition: image.c:1647
void BKE_image_free(Image *ima)
Definition: image.c:521
int BKE_imbuf_write_as(ImBuf *ibuf, const char *name, ImageFormatData *imf, const bool save_copy)
Definition: image.c:3015
int BKE_imbuf_write(ImBuf *ibuf, const char *name, const ImageFormatData *imf)
Definition: image.c:2999
struct anim * openanim(const char *name, int flags, int streamindex, char colorspace[IMA_MAX_SPACE])
Definition: image.c:3115
bool BKE_imbuf_alpha_test(ImBuf *ibuf)
Definition: image.c:2812
char BKE_imtype_valid_channels(const char imtype, bool write_file)
Definition: image.c:1500
static void image_assign_ibuf(Image *ima, ImBuf *ibuf, int index, int entry)
Definition: image.c:584
void BKE_image_path_from_imtype(char *string, const char *base, const char *relbase, int frame, const char imtype, const bool use_ext, const bool use_frames, const char *suffix)
Definition: image.c:3091
static bool image_memorypack_imbuf(Image *ima, ImBuf *ibuf, const char *filepath)
Definition: image.c:1051
void BKE_imageuser_default(ImageUser *iuser)
Definition: image.c:3451
bool BKE_image_has_anim(Image *ima)
Definition: image.c:5622
#define TEXT_SIZE_CHECK(str, w, h)
static void do_makepicstring(char *string, const char *base, const char *relbase, int frame, const char imtype, const ImageFormatData *im_format, const bool use_ext, const bool use_frames, const char *suffix)
Definition: image.c:3049
RenderSlot * BKE_image_add_renderslot(Image *ima, const char *name)
Definition: image.c:5868
static ImBuf * image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
Definition: image.c:4976
static struct ImBuf * imagecache_get(Image *image, int index)
Definition: image.c:420
void BKE_image_free_all_textures(Main *bmain)
Definition: image.c:1260
bool BKE_image_has_packedfile(Image *ima)
Definition: image.c:5627
ImBuf * BKE_image_get_ibuf_with_name(Image *image, const char *name)
Definition: image.c:5739
void BKE_image_get_tile_label(Image *ima, ImageTile *tile, char *label, int len_label)
Definition: image.c:3697
bool BKE_imtype_is_movie(const char imtype)
Definition: image.c:1443
static int imbuf_alpha_flags_for_image(Image *ima)
Definition: image.c:4087
struct ImageCacheKey ImageCacheKey
Image * BKE_image_load_exists_ex(Main *bmain, const char *filepath, bool *r_exists)
Definition: image.c:843
int BKE_image_sequence_guess_offset(Image *image)
Definition: image.c:5617
static ImBuf * image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
Definition: image.c:4510
static ImBuf * image_get_render_result(Image *ima, ImageUser *iuser, void **r_lock)
Definition: image.c:4624
void BKE_stamp_info_callback(void *data, struct StampData *stamp_data, StampCallback callback, bool noskip)
Definition: image.c:2686
void BKE_image_get_size(Image *image, ImageUser *iuser, int *r_width, int *r_height)
Definition: image.c:5502
static void imagecache_put(Image *image, int index, ImBuf *ibuf)
Definition: image.c:391
void BKE_render_result_stamp_info(Scene *scene, Object *camera, struct RenderResult *rr, bool allocate_only)
Definition: image.c:2611
static void image_free_data(ID *id)
Definition: image.c:169
void BKE_imformat_defaults(ImageFormatData *im_format)
Definition: image.c:1785
struct ImagePool ImagePool
static void image_update_views_format(Image *ima, ImageUser *iuser)
Definition: image.c:5792
bool BKE_image_is_openexr(struct Image *ima)
Definition: image.c:3959
static void image_free_packedfiles(Image *ima)
Definition: image.c:451
#define STAMP_NAME_SIZE
Definition: image.c:1931
static void image_init_after_load(Image *ima, ImageUser *iuser, ImBuf *UNUSED(ibuf))
Definition: image.c:4067
char BKE_image_alpha_mode_from_extension_ex(const char *filepath)
Definition: image.c:797
bool BKE_imtype_supports_quality(const char imtype)
Definition: image.c:1476
void BKE_image_free_views(Image *image)
Definition: image.c:468
static ImBuf * image_get_cached_ibuf_for_index_entry(Image *ima, int index, int entry)
Definition: image.c:574
void BKE_image_all_free_anim_ibufs(Main *bmain, int cfra)
Definition: image.c:1315
void BKE_image_walk_all_users(const Main *mainp, void *customdata, void callback(Image *ima, ID *iuser_id, ImageUser *iuser, void *customdata))
Definition: image.c:3378
void BKE_images_exit(void)
Definition: image.c:436
RenderResult * BKE_image_acquire_renderresult(Scene *scene, Image *ima)
Definition: image.c:3925
void BKE_image_signal(Main *bmain, Image *ima, ImageUser *iuser, int signal)
Definition: image.c:3499
void BKE_render_result_stamp_data(RenderResult *rr, const char *key, const char *value)
Definition: image.c:2727
static bool imagecache_check_dirty(ImBuf *ibuf, void *UNUSED(userkey), void *UNUSED(userdata))
Definition: image.c:1255
static void image_blend_read_data(BlendDataReader *reader, ID *id)
Definition: image.c:271
int BKE_image_get_tile_from_pos(struct Image *ima, const float uv[2], float r_uv[2], float r_ofs[2])
Definition: image.c:707
bool BKE_image_is_animated(Image *image)
Definition: image.c:5640
static ThreadMutex * image_mutex
Definition: image.c:114
IDTypeInfo IDType_ID_IM
Definition: image.c:318
bool BKE_stamp_is_known_field(const char *field_name)
Definition: image.c:2674
bool BKE_image_remove_renderslot(Image *ima, ImageUser *iuser, int index)
Definition: image.c:5882
RenderPass * BKE_image_multilayer_index(RenderResult *rr, ImageUser *iuser)
Definition: image.c:3813
static bool imagecache_hashcmp(const void *a_v, const void *b_v)
Definition: image.c:374
static ImBuf * image_load_movie_file(Image *ima, ImageUser *iuser, int frame)
Definition: image.c:4354
static void imagecache_remove(Image *image, int index)
Definition: image.c:409
void BKE_image_user_id_eval_animation(Depsgraph *depsgraph, ID *id)
Definition: image.c:5440
static void metadata_set_field(void *data, const char *propname, char *propvalue, int UNUSED(len))
Definition: image.c:2770
static CLG_LogRef LOG
Definition: image.c:113
void BKE_image_mark_dirty(Image *UNUSED(image), ImBuf *ibuf)
Definition: image.c:5686
int BKE_image_find_nearest_tile(const Image *image, const float co[2])
Definition: image.c:741
void BKE_image_editors_update_frame(const Main *bmain, int cfra)
Definition: image.c:5395
char BKE_image_ftype_to_imtype(const int ftype, const ImbFormatOptions *options)
Definition: image.c:1387
void BKE_image_init_imageuser(Image *ima, ImageUser *iuser)
Definition: image.c:3459
void BKE_image_pool_release_ibuf(Image *ima, ImBuf *ibuf, ImagePool *pool)
Definition: image.c:5261
#define IMA_INDEX_ENTRY(index)
Definition: image.c:357
#define TEXT_SIZE_CHECK_WORD_WRAP(str, w, h)
static ImBuf * load_image_single(Image *ima, ImageUser *iuser, int cfra, const int view_id, const bool has_packed, bool *r_assign)
Definition: image.c:4419
unsigned char * BKE_image_get_pixels_for_frame(struct Image *image, int frame, int tile)
Definition: image.c:5557
static void image_blend_write(BlendWriter *writer, ID *id, const void *id_address)
Definition: image.c:227
static Image * image_alloc(Main *bmain, const char *name, short source, short type)
Definition: image.c:555
Image * BKE_image_add_generated(Main *bmain, unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short gen_type, const float color[4], const bool stereo3d, const bool is_data, const bool tiled)
Definition: image.c:963
static void image_free_cached_frames(Image *image)
Definition: image.c:443
static void copy_image_packedfiles(ListBase *lb_dst, const ListBase *lb_src)
Definition: image.c:603
void BKE_image_file_format_set(Image *image, int ftype, const ImbFormatOptions *options)
Definition: image.c:5699
static void image_copy_data(Main *UNUSED(bmain), ID *id_dst, const ID *id_src, const int flag)
Definition: image.c:129
bool BKE_image_is_multiview(Image *ima)
Definition: image.c:3883
static ImBuf * load_sequence_single(Image *ima, ImageUser *iuser, int frame, const int view_id, bool *r_assign)
Definition: image.c:4119
void BKE_image_user_file_path(ImageUser *iuser, Image *ima, char *filepath)
Definition: image.c:5451
static int image_num_files(struct Image *ima)
Definition: image.c:4104
void BKE_image_path_from_imformat(char *string, const char *base, const char *relbase, int frame, const ImageFormatData *im_format, const bool use_ext, const bool use_frames, const char *suffix)
Definition: image.c:3078
ImageTile * BKE_image_add_tile(struct Image *ima, int tile_number, const char *label)
Definition: image.c:3712
static int image_get_multiview_index(Image *ima, ImageUser *iuser)
Definition: image.c:4818
ImBuf * BKE_image_pool_acquire_ibuf(Image *ima, ImageUser *iuser, ImagePool *pool)
Definition: image.c:5213
static void image_add_view(Image *ima, const char *viewname, const char *filepath)
Definition: image.c:4010
bool BKE_image_has_ibuf(Image *ima, ImageUser *iuser)
Definition: image.c:5134
struct StampDataCustomField StampDataCustomField
void BKE_imbuf_to_image_format(struct ImageFormatData *im_format, const ImBuf *imbuf)
Definition: image.c:1799
bool BKE_image_has_loaded_ibuf(Image *image)
Definition: image.c:5716
static ImBuf * load_movie_single(Image *ima, ImageUser *iuser, int frame, const int view_id)
Definition: image.c:4293
bool BKE_image_is_stereo(Image *ima)
Definition: image.c:3889
int BKE_image_path_ensure_ext_from_imtype(char *string, const char imtype)
Definition: image.c:1780
void BKE_image_print_memlist(Main *bmain)
Definition: image.c:1235
bool BKE_image_is_dirty_writable(Image *image, bool *r_is_writable)
Definition: image.c:5652
static void image_init(Image *ima, short source, short type)
Definition: image.c:527
Image * BKE_image_ensure_viewer(Main *bmain, int type, const char *name)
Definition: image.c:3162
bool BKE_image_buffer_format_writable(ImBuf *ibuf)
Definition: image.c:5691
#define GS(x)
Definition: iris.c:241
#define floorf(x)
static char ** names
Definition: makesdna.c:162
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:42
size_t(* MEM_allocN_len)(const void *vmemh)
Definition: mallocn.c:40
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
static unsigned a[3]
Definition: RandGen.cpp:92
static void area(int d1, int d2, int e1, int e2, float weights[2])
static GPUContext * wrap(Context *ctx)
bool IMB_exr_has_multilayer(void *handle)
void IMB_exr_close(void *handle)
RenderResult * RE_AcquireResultRead(Render *re)
Definition: pipeline.c:343
void RE_ReleaseResultImage(Render *re)
Definition: pipeline.c:512
Render * RE_GetSceneRender(const Scene *scene)
Definition: pipeline.c:591
void RE_AcquireResultImage(Render *re, RenderResult *rr, const int view_id)
Definition: pipeline.c:466
void RE_ClearResult(Render *re)
Definition: pipeline.c:363
void RE_SwapResult(Render *re, RenderResult **rr)
Definition: pipeline.c:371
void RE_FreeRenderResult(RenderResult *rr)
Definition: pipeline.c:265
void RE_ReleaseResult(Render *re)
Definition: pipeline.c:379
RenderResult * RE_MultilayerConvert(void *exrhandle, const char *colorspace, bool predivide, int rectx, int recty)
Definition: pipeline.c:290
RenderStats * RE_GetStats(Render *re)
Definition: pipeline.c:552
bool RE_HasCombinedLayer(RenderResult *rr)
bool RE_RenderResult_is_stereo(RenderResult *rr)
_W64 unsigned int uintptr_t
Definition: stdint.h:122
struct ListBase bg_images
unsigned int id_session_uuid
Definition: BKE_idtype.h:56
size_t offset_in_ID
Definition: BKE_idtype.h:59
void * cache_v
Definition: BKE_idtype.h:61
short id_code
Definition: BKE_idtype.h:120
Definition: DNA_ID.h:273
int tag
Definition: DNA_ID.h:292
int us
Definition: DNA_ID.h:293
void * next
Definition: DNA_ID.h:274
char name[66]
Definition: DNA_ID.h:283
struct IDProperty * metadata
void * userdata
struct ImBuf * mipmap[IMB_MIPMAP_LEVELS]
float * zbuf_float
int channels
int userflags
ImbFormatOptions foptions
unsigned char * encodedbuffer
float dither
unsigned char planes
char name[IMB_FILENAME_SIZE]
enum eImbFileType ftype
unsigned int * rect
unsigned int encodedsize
float * rect_float
struct anim * anim
int index
Definition: image.c:365
ColorManagedDisplaySettings display_settings
ColorManagedViewSettings view_settings
struct PackedFile * packedfile
struct ImagePackedFile * next
char filepath[1024]
struct ImagePoolItem * prev
Definition: image.c:5161
ImBuf * ibuf
Definition: image.c:5163
struct ImagePoolItem * next
Definition: image.c:5161
Image * image
Definition: image.c:5162
BLI_mempool * memory_pool
Definition: image.c:5170
ListBase image_buffers
Definition: image.c:5169
char label[64]
struct ImageTile * next
char multiview_eye
short multi_index
struct Scene * scene
char name[64]
char filepath[1024]
struct ImageView * next
struct ImageView * prev
short use_nodes
struct bNodeTree * nodetree
void * last
Definition: DNA_listBase.h:47
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
ListBase scenes
Definition: BKE_main.h:146
ListBase wm
Definition: BKE_main.h:175
ListBase textures
Definition: BKE_main.h:153
ListBase nodetrees
Definition: BKE_main.h:170
ListBase materials
Definition: BKE_main.h:152
ListBase cameras
Definition: BKE_main.h:157
char name[1024]
Definition: BKE_main.h:118
ListBase images
Definition: BKE_main.h:154
ListBase objects
Definition: BKE_main.h:148
struct bNodeTree * nodetree
char empty_drawtype
ImageUser * iuser
void * data
char stamp_udata[768]
float fg_stamp[4]
short stamp_font_id
float bg_stamp[4]
float dither_intensity
ListBase views
ListBase passes
Definition: RE_pipeline.h:108
struct RenderLayer * next
Definition: RE_pipeline.h:97
char name[64]
Definition: RE_pipeline.h:80
int channels
Definition: RE_pipeline.h:79
float * rect
Definition: RE_pipeline.h:82
struct RenderPass * next
Definition: RE_pipeline.h:78
ListBase views
Definition: RE_pipeline.h:138
ListBase layers
Definition: RE_pipeline.h:135
float * rectz
Definition: RE_pipeline.h:127
struct StampData * stamp_data
Definition: RE_pipeline.h:157
float * rectf
Definition: RE_pipeline.h:125
struct RenderResult * render
char name[64]
float mem_peak
Definition: RE_pipeline.h:166
double lastframetime
Definition: RE_pipeline.h:163
float * rectf
Definition: RE_pipeline.h:69
float * rectz
Definition: RE_pipeline.h:71
int * rect32
Definition: RE_pipeline.h:73
struct RenderView * next
Definition: RE_pipeline.h:65
char name[64]
Definition: RE_pipeline.h:66
struct SceneRenderView * next
struct bNodeTree * nodetree
struct RenderData r
char use_nodes
ColorManagedDisplaySettings display_settings
struct bNodeTree * nodetree
struct ImageUser iuser
struct Image * image
struct StampDataCustomField * prev
Definition: image.c:1935
struct StampDataCustomField * next
Definition: image.c:1935
char key[512]
Definition: image.c:1937
char memory[STAMP_NAME_SIZE]
Definition: image.c:1955
char date[512]
Definition: image.c:1945
char marker[512]
Definition: image.c:1946
char file[512]
Definition: image.c:1943
char strip[STAMP_NAME_SIZE]
Definition: image.c:1953
char note[512]
Definition: image.c:1944
char frame[512]
Definition: image.c:1948
char time[512]
Definition: image.c:1947
char rendertime[STAMP_NAME_SIZE]
Definition: image.c:1954
char camera[STAMP_NAME_SIZE]
Definition: image.c:1950
char frame_range[512]
Definition: image.c:1949
char hostname[512]
Definition: image.c:1956
char cameralens[STAMP_NAME_SIZE]
Definition: image.c:1951
char scene[STAMP_NAME_SIZE]
Definition: image.c:1952
ListBase custom_fields
Definition: image.c:1963
char use_nodes
struct ImageUser iuser
short type
struct bNodeTree * nodetree
struct Image * ima
struct bNodeTree * nodetree
short use_nodes
Definition: IMB_anim.h:87
char colorspace[64]
Definition: IMB_anim.h:149
int streamindex
Definition: IMB_anim.h:109
char name[1024]
Definition: IMB_anim.h:98
ListBase nodes
ListBase areabase
long int PIL_check_seconds_timer_i(void)
Definition: time.c:90
#define G(x, y, z)
const Sequence * SEQ_get_topmost_sequence(const Scene *scene, int frame)
Definition: utils.c:395
uint len