2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * Contributor(s): Blender Foundation, 2006, full recode
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/blenkernel/intern/image.c
48 #include "MEM_guardedalloc.h"
50 #include "IMB_imbuf_types.h"
51 #include "IMB_imbuf.h"
54 #include "intern/openexr/openexr_multi.h"
57 #include "DNA_packedFile_types.h"
58 #include "DNA_scene_types.h"
59 #include "DNA_object_types.h"
60 #include "DNA_camera_types.h"
61 #include "DNA_sequence_types.h"
62 #include "DNA_userdef_types.h"
63 #include "DNA_brush_types.h"
64 #include "DNA_mesh_types.h"
65 #include "DNA_meshdata_types.h"
67 #include "BLI_blenlib.h"
68 #include "BLI_threads.h"
69 #include "BLI_utildefines.h"
70 #include "BLI_bpath.h"
72 #include "BKE_bmfont.h"
73 #include "BKE_global.h"
74 #include "BKE_icons.h"
75 #include "BKE_image.h"
76 #include "BKE_library.h"
78 #include "BKE_packedFile.h"
79 #include "BKE_scene.h"
81 #include "BKE_sequencer.h" /* seq_foreground_frame_get() */
82 #include "BKE_utildefines.h"
88 #include "RE_pipeline.h"
92 #include "BLO_sys_types.h" // for intptr_t support
94 /* max int, to indicate we don't store sequences in ibuf */
95 #define IMA_NO_INDEX 0x7FEFEFEF
97 /* quick lookup: supports 1 million frames, thousand passes */
98 #define IMA_MAKE_INDEX(frame, index) ((frame)<<10)+index
99 #define IMA_INDEX_FRAME(index) (index>>10)
100 #define IMA_INDEX_PASS(index) (index & ~1023)
102 /* ******** IMAGE PROCESSING ************* */
104 static void de_interlace_ng(struct ImBuf *ibuf) /* neogeo fields */
106 struct ImBuf * tbuf1, * tbuf2;
108 if (ibuf == NULL) return;
109 if (ibuf->flags & IB_fields) return;
110 ibuf->flags |= IB_fields;
114 tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect);
115 tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect);
119 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
120 IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
123 IMB_rectcpy(ibuf, tbuf1, 0, 0, 0, 0, tbuf1->x, tbuf1->y);
124 IMB_rectcpy(ibuf, tbuf2, 0, tbuf2->y, 0, 0, tbuf2->x, tbuf2->y);
126 IMB_freeImBuf(tbuf1);
127 IMB_freeImBuf(tbuf2);
132 static void de_interlace_st(struct ImBuf *ibuf) /* standard fields */
134 struct ImBuf * tbuf1, * tbuf2;
136 if (ibuf == NULL) return;
137 if (ibuf->flags & IB_fields) return;
138 ibuf->flags |= IB_fields;
142 tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect);
143 tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect);
147 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
148 IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
151 IMB_rectcpy(ibuf, tbuf2, 0, 0, 0, 0, tbuf2->x, tbuf2->y);
152 IMB_rectcpy(ibuf, tbuf1, 0, tbuf2->y, 0, 0, tbuf1->x, tbuf1->y);
154 IMB_freeImBuf(tbuf1);
155 IMB_freeImBuf(tbuf2);
160 void image_de_interlace(Image *ima, int odd)
162 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
165 de_interlace_st(ibuf);
167 de_interlace_ng(ibuf);
171 /* ***************** ALLOC & FREE, DATA MANAGING *************** */
173 static void image_free_buffers(Image *ima)
177 while((ibuf = ima->ibufs.first)) {
178 BLI_remlink(&ima->ibufs, ibuf);
180 if (ibuf->userdata) {
181 MEM_freeN(ibuf->userdata);
182 ibuf->userdata = NULL;
187 if(ima->anim) IMB_free_anim(ima->anim);
191 RE_FreeRenderResult(ima->rr);
200 /* called by library too, do not free ima itself */
201 void free_image(Image *ima)
205 image_free_buffers(ima);
206 if (ima->packedfile) {
207 freePackedFile(ima->packedfile);
208 ima->packedfile = NULL;
210 BKE_icon_delete(&ima->id);
213 BKE_previewimg_free(&ima->preview);
215 for(a=0; a<IMA_MAX_RENDER_SLOT; a++) {
216 if(ima->renders[a]) {
217 RE_FreeRenderResult(ima->renders[a]);
218 ima->renders[a]= NULL;
223 /* only image block itself */
224 static Image *image_alloc(const char *name, short source, short type)
228 ima= alloc_libblock(&G.main->image, ID_IM, name);
232 ima->xrep= ima->yrep= 1;
233 ima->aspx= ima->aspy= 1.0;
234 ima->gen_x= 1024; ima->gen_y= 1024;
235 ima->gen_type= 1; /* no defines yet? */
243 /* get the ibuf from an image cache, local use here only */
244 static ImBuf *image_get_ibuf(Image *ima, int index, int frame)
246 /* this function is intended to be thread safe. with IMA_NO_INDEX this
247 * should be OK, but when iterating over the list this is more tricky
249 if(index==IMA_NO_INDEX)
250 return ima->ibufs.first;
254 index= IMA_MAKE_INDEX(frame, index);
255 for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next)
256 if(ibuf->index==index)
263 /* no ima->ibuf anymore, but listbase */
264 static void image_remove_ibuf(Image *ima, ImBuf *ibuf)
267 BLI_remlink(&ima->ibufs, ibuf);
273 /* no ima->ibuf anymore, but listbase */
274 static void image_assign_ibuf(Image *ima, ImBuf *ibuf, int index, int frame)
279 if(index!=IMA_NO_INDEX)
280 index= IMA_MAKE_INDEX(frame, index);
282 /* insert based on index */
283 for(link= ima->ibufs.first; link; link= link->next)
284 if(link->index>=index)
288 if(ima->flag & IMA_CM_PREDIVIDE)
289 ibuf->flags |= IB_cm_predivide;
291 ibuf->flags &= ~IB_cm_predivide;
293 /* this function accepts link==NULL */
294 BLI_insertlinkbefore(&ima->ibufs, link, ibuf);
296 /* now we don't want copies? */
297 if(link && ibuf->index==link->index)
298 image_remove_ibuf(ima, link);
302 /* empty image block, of similar type and filename */
303 Image *copy_image(Image *ima)
305 Image *nima= image_alloc(ima->id.name+2, ima->source, ima->type);
307 BLI_strncpy(nima->name, ima->name, sizeof(ima->name));
309 nima->flag= ima->flag;
310 nima->tpageflag= ima->tpageflag;
312 nima->gen_x= ima->gen_x;
313 nima->gen_y= ima->gen_y;
314 nima->gen_type= ima->gen_type;
316 nima->animspeed= ima->animspeed;
318 nima->aspx= ima->aspx;
319 nima->aspy= ima->aspy;
324 static void extern_local_image(Image *UNUSED(ima))
326 /* Nothing to do: images don't link to other IDs. This function exists to
327 match id_make_local pattern. */
330 void make_local_image(struct Image *ima)
336 int is_local= FALSE, is_lib= FALSE;
338 /* - only lib users: do nothing
339 * - only local users: set flag
343 if(ima->id.lib==NULL) return;
345 /* Can't take short cut here: must check meshes at least because of bogus
346 texface ID refs. - z0r */
349 id_clear_lib_data(bmain, &ima->id);
350 extern_local_image(ima);
355 for(tex= bmain->tex.first; tex; tex= tex->id.next) {
356 if(tex->ima == ima) {
357 if(tex->id.lib) is_lib= TRUE;
361 for(brush= bmain->brush.first; brush; brush= brush->id.next) {
362 if(brush->clone.image == ima) {
363 if(brush->id.lib) is_lib= TRUE;
367 for(me= bmain->mesh.first; me; me= me->id.next) {
372 for(i=0; i<me->fdata.totlayer; i++) {
373 if(me->fdata.layers[i].type == CD_MTFACE) {
374 tface= (MTFace*)me->fdata.layers[i].data;
376 for(a=0; a<me->totface; a++, tface++) {
377 if(tface->tpage == ima) {
378 if(me->id.lib) is_lib= TRUE;
387 if(is_local && is_lib == FALSE) {
388 id_clear_lib_data(bmain, &ima->id);
389 extern_local_image(ima);
391 else if(is_local && is_lib) {
392 Image *ima_new= copy_image(ima);
396 /* Remap paths of new ID using old library as base. */
397 BKE_id_lib_local_paths(bmain, ima->id.lib, &ima_new->id);
399 tex= bmain->tex.first;
401 if(tex->id.lib==NULL) {
410 brush= bmain->brush.first;
412 if(brush->id.lib==NULL) {
413 if(brush->clone.image==ima) {
414 brush->clone.image = ima_new;
419 brush= brush->id.next;
421 /* Transfer references in texfaces. Texfaces don't add to image ID
422 user count *unless* there are no other users. See
423 readfile.c:lib_link_mtface. */
424 me= bmain->mesh.first;
430 for(i=0; i<me->fdata.totlayer; i++) {
431 if(me->fdata.layers[i].type == CD_MTFACE) {
432 tface= (MTFace*)me->fdata.layers[i].data;
434 for(a=0; a<me->totface; a++, tface++) {
435 if(tface->tpage == ima) {
436 tface->tpage = ima_new;
437 if(ima_new->id.us == 0) {
438 tface->tpage->id.us= 1;
440 id_lib_extern((ID*)ima_new);
451 void BKE_image_merge(Image *dest, Image *source)
456 if(dest && source && dest!=source) {
458 while((ibuf= source->ibufs.first)) {
459 BLI_remlink(&source->ibufs, ibuf);
460 image_assign_ibuf(dest, ibuf, IMA_INDEX_PASS(ibuf->index), IMA_INDEX_FRAME(ibuf->index));
463 free_libblock(&G.main->image, source);
468 /* checks if image was already loaded, then returns same image */
469 /* otherwise creates new. */
470 /* does not load ibuf itself */
471 /* pass on optional frame for #name images */
472 Image *BKE_add_image_file(const char *name)
477 char str[FILE_MAX], strtest[FILE_MAX];
479 BLI_strncpy(str, name, sizeof(str));
480 BLI_path_abs(str, G.main->name);
483 file= open(str, O_BINARY|O_RDONLY);
484 if(file== -1) return NULL;
487 /* first search an identical image */
488 for(ima= G.main->image.first; ima; ima= ima->id.next) {
489 if(ima->source!=IMA_SRC_VIEWER && ima->source!=IMA_SRC_GENERATED) {
490 BLI_strncpy(strtest, ima->name, sizeof(ima->name));
491 BLI_path_abs(strtest, G.main->name);
493 if( strcmp(strtest, str)==0 ) {
494 if(ima->anim==NULL || ima->id.us==0) {
495 BLI_strncpy(ima->name, name, sizeof(ima->name)); /* for stringcode */
496 ima->id.us++; /* officially should not, it doesn't link here! */
507 /* create a short library name */
510 while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\') len--;
513 ima= image_alloc(libname, IMA_SRC_FILE, IMA_TYPE_IMAGE);
514 BLI_strncpy(ima->name, name, sizeof(ima->name));
516 if(BLI_testextensie_array(name, imb_ext_movie))
517 ima->source= IMA_SRC_MOVIE;
522 static ImBuf *add_ibuf_size(unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short uvtestgrid, float color[4])
525 unsigned char *rect= NULL;
526 float *rect_float= NULL;
529 ibuf= IMB_allocImBuf(width, height, depth, IB_rectfloat);
530 rect_float= (float*)ibuf->rect_float;
533 ibuf= IMB_allocImBuf(width, height, depth, IB_rect);
534 rect= (unsigned char*)ibuf->rect;
537 BLI_strncpy(ibuf->name, name, sizeof(ibuf->name));
538 ibuf->userflags |= IB_BITMAPDIRTY;
542 BKE_image_buf_fill_checker(rect, rect_float, width, height);
545 BKE_image_buf_fill_checker_color(rect, rect_float, width, height);
548 BKE_image_buf_fill_color(rect, rect_float, width, height, color);
554 /* adds new image block, creates ImBuf and initializes color */
555 Image *BKE_add_image_size(unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short uvtestgrid, float color[4])
557 /* on save, type is changed to FILE in editsima.c */
558 Image *ima= image_alloc(name, IMA_SRC_GENERATED, IMA_TYPE_UV_TEST);
563 /* BLI_strncpy(ima->name, name, FILE_MAX); */ /* dont do this, this writes in ain invalid filepath! */
566 ima->gen_type= uvtestgrid;
567 ima->gen_flag |= (floatbuf ? IMA_GEN_FLOAT : 0);
569 ibuf= add_ibuf_size(width, height, ima->name, depth, floatbuf, uvtestgrid, color);
570 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
572 ima->ok= IMA_OK_LOADED;
578 /* creates an image image owns the imbuf passed */
579 Image *BKE_add_image_imbuf(ImBuf *ibuf)
581 /* on save, type is changed to FILE in editsima.c */
584 ima= image_alloc(BLI_path_basename(ibuf->name), IMA_SRC_FILE, IMA_TYPE_IMAGE);
587 BLI_strncpy(ima->name, ibuf->name, FILE_MAX);
588 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
589 ima->ok= IMA_OK_LOADED;
595 /* packs rect from memory as PNG */
596 void BKE_image_memorypack(Image *ima)
598 ImBuf *ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
602 if (ima->packedfile) {
603 freePackedFile(ima->packedfile);
604 ima->packedfile = NULL;
608 ibuf->planes= R_IMF_PLANES_RGBA;
610 IMB_saveiff(ibuf, ibuf->name, IB_rect | IB_mem);
611 if(ibuf->encodedbuffer==NULL) {
612 printf("memory save for pack error\n");
615 PackedFile *pf = MEM_callocN(sizeof(*pf), "PackedFile");
617 pf->data = ibuf->encodedbuffer;
618 pf->size = ibuf->encodedsize;
620 ibuf->encodedbuffer= NULL;
621 ibuf->encodedsize= 0;
622 ibuf->userflags &= ~IB_BITMAPDIRTY;
624 if(ima->source==IMA_SRC_GENERATED) {
625 ima->source= IMA_SRC_FILE;
626 ima->type= IMA_TYPE_IMAGE;
631 void tag_image_time(Image *ima)
634 ima->lastused = (int)PIL_check_seconds_timer();
638 static void tag_all_images_time()
641 int ctime = (int)PIL_check_seconds_timer();
643 ima= G.main->image.first;
645 if(ima->bindcode || ima->repbind || ima->ibufs.first) {
646 ima->lastused = ctime;
652 void free_old_images(void)
655 static int lasttime = 0;
656 int ctime = (int)PIL_check_seconds_timer();
659 Run garbage collector once for every collecting period of time
660 if textimeout is 0, that's the option to NOT run the collector
662 if (U.textimeout == 0 || ctime % U.texcollectrate || ctime == lasttime)
671 ima= G.main->image.first;
673 if((ima->flag & IMA_NOCOLLECT)==0 && ctime - ima->lastused > U.textimeout) {
675 If it's in GL memory, deallocate and set time tag to current time
676 This gives textures a "second chance" to be used before dying.
678 if(ima->bindcode || ima->repbind) {
680 ima->lastused = ctime;
682 /* Otherwise, just kill the buffers */
683 else if (ima->ibufs.first) {
684 image_free_buffers(ima);
691 static uintptr_t image_mem_size(Image *ima)
699 /* viewers have memory depending on other rules, has no valid rect pointer */
700 if(ima->source==IMA_SRC_VIEWER)
703 for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next) {
704 if(ibuf->rect) size += MEM_allocN_len(ibuf->rect);
705 else if(ibuf->rect_float) size += MEM_allocN_len(ibuf->rect_float);
707 for(level=0; level<IB_MIPMAP_LEVELS; level++) {
708 ibufm= ibuf->mipmap[level];
710 if(ibufm->rect) size += MEM_allocN_len(ibufm->rect);
711 else if(ibufm->rect_float) size += MEM_allocN_len(ibufm->rect_float);
719 void BKE_image_print_memlist(void)
722 uintptr_t size, totsize= 0;
724 for(ima= G.main->image.first; ima; ima= ima->id.next)
725 totsize += image_mem_size(ima);
727 printf("\ntotal image memory len: %.3f MB\n", (double)totsize/(double)(1024*1024));
729 for(ima= G.main->image.first; ima; ima= ima->id.next) {
730 size= image_mem_size(ima);
733 printf("%s len: %.3f MB\n", ima->id.name+2, (double)size/(double)(1024*1024));
737 void BKE_image_free_all_textures(void)
741 /* unsigned int totsize= 0; */
743 for(ima= G.main->image.first; ima; ima= ima->id.next)
744 ima->id.flag &= ~LIB_DOIT;
746 for(tex= G.main->tex.first; tex; tex= tex->id.next)
748 tex->ima->id.flag |= LIB_DOIT;
750 for(ima= G.main->image.first; ima; ima= ima->id.next) {
751 if(ima->ibufs.first && (ima->id.flag & LIB_DOIT)) {
754 for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next) {
755 /* escape when image is painted on */
756 if(ibuf->userflags & IB_BITMAPDIRTY)
759 /* if(ibuf->mipmap[0])
760 totsize+= 1.33*ibuf->x*ibuf->y*4;
762 totsize+= ibuf->x*ibuf->y*4;*/
766 image_free_buffers(ima);
769 /* printf("freed total %d MB\n", totsize/(1024*1024)); */
772 /* except_frame is weak, only works for seqs without offset... */
773 void BKE_image_free_anim_ibufs(Image *ima, int except_frame)
777 for(ibuf= ima->ibufs.first; ibuf; ibuf= nbuf) {
779 if(ibuf->userflags & IB_BITMAPDIRTY)
781 if(ibuf->index==IMA_NO_INDEX)
783 if(except_frame!=IMA_INDEX_FRAME(ibuf->index)) {
784 BLI_remlink(&ima->ibufs, ibuf);
786 if (ibuf->userdata) {
787 MEM_freeN(ibuf->userdata);
788 ibuf->userdata = NULL;
795 void BKE_image_all_free_anim_ibufs(int cfra)
799 for(ima= G.main->image.first; ima; ima= ima->id.next)
800 if(ELEM(ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE))
801 BKE_image_free_anim_ibufs(ima, cfra);
805 /* *********** READ AND WRITE ************** */
807 int BKE_imtype_to_ftype(const char imtype)
809 if(imtype==R_IMF_IMTYPE_TARGA)
811 else if(imtype==R_IMF_IMTYPE_RAWTGA)
813 else if(imtype== R_IMF_IMTYPE_IRIS)
816 else if (imtype==R_IMF_IMTYPE_RADHDR)
819 else if (imtype==R_IMF_IMTYPE_PNG)
822 else if (imtype==R_IMF_IMTYPE_DDS)
825 else if (imtype==R_IMF_IMTYPE_BMP)
828 else if (imtype==R_IMF_IMTYPE_TIFF)
831 else if (imtype==R_IMF_IMTYPE_OPENEXR || imtype==R_IMF_IMTYPE_MULTILAYER)
834 else if (imtype==R_IMF_IMTYPE_CINEON)
836 else if (imtype==R_IMF_IMTYPE_DPX)
840 else if(imtype==R_IMF_IMTYPE_JP2)
847 char BKE_ftype_to_imtype(const int ftype)
850 return R_IMF_IMTYPE_TARGA;
851 else if(ftype == IMAGIC)
852 return R_IMF_IMTYPE_IRIS;
854 else if (ftype & RADHDR)
855 return R_IMF_IMTYPE_RADHDR;
857 else if (ftype & PNG)
858 return R_IMF_IMTYPE_PNG;
860 else if (ftype & DDS)
861 return R_IMF_IMTYPE_DDS;
863 else if (ftype & BMP)
864 return R_IMF_IMTYPE_BMP;
866 else if (ftype & TIF)
867 return R_IMF_IMTYPE_TIFF;
869 else if (ftype & OPENEXR)
870 return R_IMF_IMTYPE_OPENEXR;
872 else if (ftype & CINEON)
873 return R_IMF_IMTYPE_CINEON;
874 else if (ftype & DPX)
875 return R_IMF_IMTYPE_DPX;
877 else if (ftype & TGA)
878 return R_IMF_IMTYPE_TARGA;
879 else if(ftype & RAWTGA)
880 return R_IMF_IMTYPE_RAWTGA;
883 return R_IMF_IMTYPE_JP2;
886 return R_IMF_IMTYPE_JPEG90;
890 int BKE_imtype_is_movie(const char imtype)
893 case R_IMF_IMTYPE_AVIRAW:
894 case R_IMF_IMTYPE_AVIJPEG:
895 case R_IMF_IMTYPE_AVICODEC:
896 case R_IMF_IMTYPE_QUICKTIME:
897 case R_IMF_IMTYPE_FFMPEG:
898 case R_IMF_IMTYPE_H264:
899 case R_IMF_IMTYPE_THEORA:
900 case R_IMF_IMTYPE_XVID:
901 case R_IMF_IMTYPE_FRAMESERVER:
907 int BKE_imtype_supports_zbuf(const char imtype)
910 case R_IMF_IMTYPE_IRIZ:
911 case R_IMF_IMTYPE_OPENEXR: /* but not R_IMF_IMTYPE_MULTILAYER */
917 int BKE_imtype_supports_compress(const char imtype)
920 case R_IMF_IMTYPE_PNG:
926 int BKE_imtype_supports_quality(const char imtype)
929 case R_IMF_IMTYPE_JPEG90:
930 case R_IMF_IMTYPE_JP2:
931 case R_IMF_IMTYPE_AVIJPEG:
937 char BKE_imtype_valid_channels(const char imtype)
939 char chan_flag= IMA_CHAN_FLAG_RGB; /* assume all support rgb */
943 case R_IMF_IMTYPE_TARGA:
944 case R_IMF_IMTYPE_IRIS:
945 case R_IMF_IMTYPE_PNG:
946 /* case R_IMF_IMTYPE_BMP: */ /* read but not write */
947 case R_IMF_IMTYPE_RADHDR:
948 case R_IMF_IMTYPE_TIFF:
949 case R_IMF_IMTYPE_OPENEXR:
950 case R_IMF_IMTYPE_MULTILAYER:
951 case R_IMF_IMTYPE_DDS:
952 case R_IMF_IMTYPE_JP2:
953 chan_flag |= IMA_CHAN_FLAG_ALPHA;
958 case R_IMF_IMTYPE_PNG:
959 case R_IMF_IMTYPE_JPEG90:
960 case R_IMF_IMTYPE_TARGA:
961 case R_IMF_IMTYPE_RAWTGA:
962 case R_IMF_IMTYPE_TIFF:
963 case R_IMF_IMTYPE_IRIS:
964 chan_flag |= IMA_CHAN_FLAG_BW;
970 char BKE_imtype_valid_depths(const char imtype)
973 case R_IMF_IMTYPE_RADHDR:
974 return R_IMF_CHAN_DEPTH_32;
975 case R_IMF_IMTYPE_TIFF:
976 return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_16;
977 case R_IMF_IMTYPE_OPENEXR:
978 return R_IMF_CHAN_DEPTH_16 | R_IMF_CHAN_DEPTH_32;
979 case R_IMF_IMTYPE_MULTILAYER:
980 return R_IMF_CHAN_DEPTH_32;
981 /* eeh, cineone does some strange 10bits per channel */
982 case R_IMF_IMTYPE_DPX:
983 case R_IMF_IMTYPE_CINEON:
984 return R_IMF_CHAN_DEPTH_12;
985 case R_IMF_IMTYPE_JP2:
986 return R_IMF_CHAN_DEPTH_8 | R_IMF_CHAN_DEPTH_12 | R_IMF_CHAN_DEPTH_16;
987 /* most formats are 8bit only */
989 return R_IMF_CHAN_DEPTH_8;
994 /* string is from command line --render-format arg, keep in sync with
995 * creator.c help info */
996 char BKE_imtype_from_arg(const char *imtype_arg)
998 if (!strcmp(imtype_arg,"TGA")) return R_IMF_IMTYPE_TARGA;
999 else if (!strcmp(imtype_arg,"IRIS")) return R_IMF_IMTYPE_IRIS;
1001 else if (!strcmp(imtype_arg,"DDS")) return R_IMF_IMTYPE_DDS;
1003 else if (!strcmp(imtype_arg,"JPEG")) return R_IMF_IMTYPE_JPEG90;
1004 else if (!strcmp(imtype_arg,"IRIZ")) return R_IMF_IMTYPE_IRIZ;
1005 else if (!strcmp(imtype_arg,"RAWTGA")) return R_IMF_IMTYPE_RAWTGA;
1006 else if (!strcmp(imtype_arg,"AVIRAW")) return R_IMF_IMTYPE_AVIRAW;
1007 else if (!strcmp(imtype_arg,"AVIJPEG")) return R_IMF_IMTYPE_AVIJPEG;
1008 else if (!strcmp(imtype_arg,"PNG")) return R_IMF_IMTYPE_PNG;
1009 else if (!strcmp(imtype_arg,"AVICODEC")) return R_IMF_IMTYPE_AVICODEC;
1010 else if (!strcmp(imtype_arg,"QUICKTIME")) return R_IMF_IMTYPE_QUICKTIME;
1011 else if (!strcmp(imtype_arg,"BMP")) return R_IMF_IMTYPE_BMP;
1013 else if (!strcmp(imtype_arg,"HDR")) return R_IMF_IMTYPE_RADHDR;
1016 else if (!strcmp(imtype_arg,"TIFF")) return R_IMF_IMTYPE_TIFF;
1019 else if (!strcmp(imtype_arg,"EXR")) return R_IMF_IMTYPE_OPENEXR;
1020 else if (!strcmp(imtype_arg,"MULTILAYER")) return R_IMF_IMTYPE_MULTILAYER;
1022 else if (!strcmp(imtype_arg,"MPEG")) return R_IMF_IMTYPE_FFMPEG;
1023 else if (!strcmp(imtype_arg,"FRAMESERVER")) return R_IMF_IMTYPE_FRAMESERVER;
1025 else if (!strcmp(imtype_arg,"CINEON")) return R_IMF_IMTYPE_CINEON;
1026 else if (!strcmp(imtype_arg,"DPX")) return R_IMF_IMTYPE_DPX;
1028 #ifdef WITH_OPENJPEG
1029 else if (!strcmp(imtype_arg,"JP2")) return R_IMF_IMTYPE_JP2;
1031 else return R_IMF_IMTYPE_INVALID;
1034 int BKE_add_image_extension(char *string, const char imtype)
1036 const char *extension= NULL;
1038 if(imtype== R_IMF_IMTYPE_IRIS) {
1039 if(!BLI_testextensie(string, ".rgb"))
1042 else if(imtype==R_IMF_IMTYPE_IRIZ) {
1043 if(!BLI_testextensie(string, ".rgb"))
1047 else if(imtype==R_IMF_IMTYPE_RADHDR) {
1048 if(!BLI_testextensie(string, ".hdr"))
1052 else if (ELEM5(imtype, R_IMF_IMTYPE_PNG, R_IMF_IMTYPE_FFMPEG, R_IMF_IMTYPE_H264, R_IMF_IMTYPE_THEORA, R_IMF_IMTYPE_XVID)) {
1053 if(!BLI_testextensie(string, ".png"))
1057 else if(imtype==R_IMF_IMTYPE_DDS) {
1058 if(!BLI_testextensie(string, ".dds"))
1062 else if(imtype==R_IMF_IMTYPE_RAWTGA) {
1063 if(!BLI_testextensie(string, ".tga"))
1066 else if(imtype==R_IMF_IMTYPE_BMP) {
1067 if(!BLI_testextensie(string, ".bmp"))
1071 else if(imtype==R_IMF_IMTYPE_TIFF) {
1072 if(!BLI_testextensie(string, ".tif") &&
1073 !BLI_testextensie(string, ".tiff")) extension= ".tif";
1077 else if( ELEM(imtype, R_IMF_IMTYPE_OPENEXR, R_IMF_IMTYPE_MULTILAYER)) {
1078 if(!BLI_testextensie(string, ".exr"))
1083 else if(imtype==R_IMF_IMTYPE_CINEON){
1084 if (!BLI_testextensie(string, ".cin"))
1087 else if(imtype==R_IMF_IMTYPE_DPX){
1088 if (!BLI_testextensie(string, ".dpx"))
1092 else if(imtype==R_IMF_IMTYPE_TARGA) {
1093 if(!BLI_testextensie(string, ".tga"))
1096 #ifdef WITH_OPENJPEG
1097 else if(imtype==R_IMF_IMTYPE_JP2) {
1098 if(!BLI_testextensie(string, ".jp2"))
1102 else { // R_IMF_IMTYPE_AVICODEC, R_IMF_IMTYPE_AVIRAW, R_IMF_IMTYPE_AVIJPEG, R_IMF_IMTYPE_JPEG90, R_IMF_IMTYPE_QUICKTIME etc
1103 if(!( BLI_testextensie(string, ".jpg") || BLI_testextensie(string, ".jpeg")))
1108 /* prefer this in many cases to avoid .png.tga, but in certain cases it breaks */
1109 /* remove any other known image extension */
1110 if(BLI_testextensie_array(string, imb_ext_image)
1111 || (G.have_quicktime && BLI_testextensie_array(string, imb_ext_image_qt))) {
1112 return BLI_replace_extension(string, FILE_MAX, extension);
1114 return BLI_ensure_extension(string, FILE_MAX, extension);
1124 /* could allow access externally - 512 is for long names, 64 is for id names */
1125 typedef struct StampData {
1133 char cameralens[64];
1136 char rendertime[64];
1139 static void stampdata(Scene *scene, Object *camera, StampData *stamp_data, int do_prefix)
1145 if (scene->r.stamp & R_STAMP_FILENAME) {
1146 BLI_snprintf(stamp_data->file, sizeof(stamp_data->file), do_prefix ? "File %s":"%s", G.relbase_valid ? G.main->name:"<untitled>");
1148 stamp_data->file[0] = '\0';
1151 if (scene->r.stamp & R_STAMP_NOTE) {
1152 /* Never do prefix for Note */
1153 BLI_snprintf(stamp_data->note, sizeof(stamp_data->note), "%s", scene->r.stamp_udata);
1155 stamp_data->note[0] = '\0';
1158 if (scene->r.stamp & R_STAMP_DATE) {
1161 BLI_snprintf(text, sizeof(text), "%04d/%02d/%02d %02d:%02d:%02d", tl->tm_year+1900, tl->tm_mon+1, tl->tm_mday, tl->tm_hour, tl->tm_min, tl->tm_sec);
1162 BLI_snprintf(stamp_data->date, sizeof(stamp_data->date), do_prefix ? "Date %s":"%s", text);
1164 stamp_data->date[0] = '\0';
1167 if (scene->r.stamp & R_STAMP_MARKER) {
1168 char *name = scene_find_last_marker_name(scene, CFRA);
1170 if (name) BLI_strncpy(text, name, sizeof(text));
1171 else BLI_strncpy(text, "<none>", sizeof(text));
1173 BLI_snprintf(stamp_data->marker, sizeof(stamp_data->marker), do_prefix ? "Marker %s":"%s", text);
1175 stamp_data->marker[0] = '\0';
1178 if (scene->r.stamp & R_STAMP_TIME) {
1179 int f = (int)(scene->r.cfra % scene->r.frs_sec);
1180 int s = (int)(scene->r.cfra / scene->r.frs_sec);
1194 if (scene->r.frs_sec < 100)
1195 BLI_snprintf(text, sizeof(text), "%02d:%02d:%02d.%02d", h, m, s, f);
1197 BLI_snprintf(text, sizeof(text), "%02d:%02d:%02d.%03d", h, m, s, f);
1199 BLI_snprintf(stamp_data->time, sizeof(stamp_data->time), do_prefix ? "Time %s":"%s", text);
1201 stamp_data->time[0] = '\0';
1204 if (scene->r.stamp & R_STAMP_FRAME) {
1209 digits= 1 + (int) log10(scene->r.efra);
1211 BLI_snprintf(fmtstr, sizeof(fmtstr), do_prefix ? "Frame %%0%di":"%%0%di", digits);
1212 BLI_snprintf (stamp_data->frame, sizeof(stamp_data->frame), fmtstr, scene->r.cfra);
1214 stamp_data->frame[0] = '\0';
1217 if (scene->r.stamp & R_STAMP_CAMERA) {
1218 BLI_snprintf(stamp_data->camera, sizeof(stamp_data->camera), do_prefix ? "Camera %s":"%s", camera ? camera->id.name+2 : "<none>");
1220 stamp_data->camera[0] = '\0';
1223 if (scene->r.stamp & R_STAMP_CAMERALENS) {
1224 if (camera && camera->type == OB_CAMERA) {
1225 BLI_snprintf(text, sizeof(text), "%.2f", ((Camera *)camera->data)->lens);
1227 else BLI_strncpy(text, "<none>", sizeof(text));
1229 BLI_snprintf(stamp_data->cameralens, sizeof(stamp_data->cameralens), do_prefix ? "Lens %s":"%s", text);
1231 stamp_data->cameralens[0] = '\0';
1234 if (scene->r.stamp & R_STAMP_SCENE) {
1235 BLI_snprintf(stamp_data->scene, sizeof(stamp_data->scene), do_prefix ? "Scene %s":"%s", scene->id.name+2);
1237 stamp_data->scene[0] = '\0';
1240 if (scene->r.stamp & R_STAMP_SEQSTRIP) {
1241 Sequence *seq= seq_foreground_frame_get(scene, scene->r.cfra);
1243 if (seq) BLI_strncpy(text, seq->name+2, sizeof(text));
1244 else BLI_strncpy(text, "<none>", sizeof(text));
1246 BLI_snprintf(stamp_data->strip, sizeof(stamp_data->strip), do_prefix ? "Strip %s":"%s", text);
1248 stamp_data->strip[0] = '\0';
1252 Render *re= RE_GetRender(scene->id.name);
1253 RenderStats *stats= re ? RE_GetStats(re):NULL;
1255 if (stats && (scene->r.stamp & R_STAMP_RENDERTIME)) {
1256 BLI_timestr(stats->lastframetime, text);
1258 BLI_snprintf(stamp_data->rendertime, sizeof(stamp_data->rendertime), do_prefix ? "RenderTime %s":"%s", text);
1260 stamp_data->rendertime[0] = '\0';
1265 void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rectf, int width, int height, int channels)
1267 struct StampData stamp_data;
1271 const int mono= blf_mono_font_render; // XXX
1273 #define BUFF_MARGIN_X 2
1274 #define BUFF_MARGIN_Y 1
1276 if (!rect && !rectf)
1279 stampdata(scene, camera, &stamp_data, 1);
1281 /* TODO, do_versions */
1282 if(scene->r.stamp_font_id < 8)
1283 scene->r.stamp_font_id= 12;
1285 /* set before return */
1286 BLF_size(mono, scene->r.stamp_font_id, 72);
1288 BLF_buffer(mono, rectf, rect, width, height, channels);
1289 BLF_buffer_col(mono, scene->r.fg_stamp[0], scene->r.fg_stamp[1], scene->r.fg_stamp[2], 1.0);
1290 pad= BLF_width_max(mono);
1292 /* use 'h_fixed' rather than 'h', aligns better */
1293 h_fixed= BLF_height_max(mono);
1294 y_ofs = -BLF_descender(mono);
1299 if (stamp_data.file[0]) {
1300 /* Top left corner */
1301 BLF_width_and_height(mono, stamp_data.file, &w, &h); h= h_fixed;
1304 /* also a little of space to the background. */
1305 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
1307 /* and draw the text. */
1308 BLF_position(mono, x, y + y_ofs, 0.0);
1309 BLF_draw_buffer(mono, stamp_data.file);
1311 /* the extra pixel for background. */
1312 y -= BUFF_MARGIN_Y * 2;
1315 /* Top left corner, below File */
1316 if (stamp_data.note[0]) {
1317 BLF_width_and_height(mono, stamp_data.note, &w, &h); h= h_fixed;
1320 /* and space for background. */
1321 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, 0, y-BUFF_MARGIN_Y, w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
1323 BLF_position(mono, x, y + y_ofs, 0.0);
1324 BLF_draw_buffer(mono, stamp_data.note);
1326 /* the extra pixel for background. */
1327 y -= BUFF_MARGIN_Y * 2;
1330 /* Top left corner, below File (or Note) */
1331 if (stamp_data.date[0]) {
1332 BLF_width_and_height(mono, stamp_data.date, &w, &h); h= h_fixed;
1335 /* and space for background. */
1336 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, 0, y-BUFF_MARGIN_Y, w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
1338 BLF_position(mono, x, y + y_ofs, 0.0);
1339 BLF_draw_buffer(mono, stamp_data.date);
1341 /* the extra pixel for background. */
1342 y -= BUFF_MARGIN_Y * 2;
1345 /* Top left corner, below File, Date or Note */
1346 if (stamp_data.rendertime[0]) {
1347 BLF_width_and_height(mono, stamp_data.rendertime, &w, &h); h= h_fixed;
1350 /* and space for background. */
1351 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, 0, y-BUFF_MARGIN_Y, w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
1353 BLF_position(mono, x, y + y_ofs, 0.0);
1354 BLF_draw_buffer(mono, stamp_data.rendertime);
1360 /* Bottom left corner, leaving space for timing */
1361 if (stamp_data.marker[0]) {
1362 BLF_width_and_height(mono, stamp_data.marker, &w, &h); h= h_fixed;
1364 /* extra space for background. */
1365 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
1367 /* and pad the text. */
1368 BLF_position(mono, x, y + y_ofs, 0.0);
1369 BLF_draw_buffer(mono, stamp_data.marker);
1375 /* Left bottom corner */
1376 if (stamp_data.time[0]) {
1377 BLF_width_and_height(mono, stamp_data.time, &w, &h); h= h_fixed;
1379 /* extra space for background */
1380 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y, x+w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
1382 /* and pad the text. */
1383 BLF_position(mono, x, y + y_ofs, 0.0);
1384 BLF_draw_buffer(mono, stamp_data.time);
1390 if (stamp_data.frame[0]) {
1391 BLF_width_and_height(mono, stamp_data.frame, &w, &h); h= h_fixed;
1393 /* extra space for background. */
1394 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, x+w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
1396 /* and pad the text. */
1397 BLF_position(mono, x, y + y_ofs, 0.0);
1398 BLF_draw_buffer(mono, stamp_data.frame);
1404 if (stamp_data.camera[0]) {
1405 BLF_width_and_height(mono, stamp_data.camera, &w, &h); h= h_fixed;
1407 /* extra space for background. */
1408 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, x+w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
1409 BLF_position(mono, x, y + y_ofs, 0.0);
1410 BLF_draw_buffer(mono, stamp_data.camera);
1416 if (stamp_data.cameralens[0]) {
1417 BLF_width_and_height(mono, stamp_data.cameralens, &w, &h); h= h_fixed;
1419 /* extra space for background. */
1420 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, x+w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
1421 BLF_position(mono, x, y + y_ofs, 0.0);
1422 BLF_draw_buffer(mono, stamp_data.cameralens);
1425 if (stamp_data.scene[0]) {
1426 BLF_width_and_height(mono, stamp_data.scene, &w, &h); h= h_fixed;
1428 /* Bottom right corner, with an extra space because blenfont is too strict! */
1431 /* extra space for background. */
1432 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, x+w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
1434 /* and pad the text. */
1435 BLF_position(mono, x, y+y_ofs, 0.0);
1436 BLF_draw_buffer(mono, stamp_data.scene);
1439 if (stamp_data.strip[0]) {
1440 BLF_width_and_height(mono, stamp_data.strip, &w, &h); h= h_fixed;
1442 /* Top right corner, with an extra space because blenfont is too strict! */
1446 /* extra space for background. */
1447 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x-BUFF_MARGIN_X, y-BUFF_MARGIN_Y, x+w+BUFF_MARGIN_X, y+h+BUFF_MARGIN_Y);
1449 BLF_position(mono, x, y + y_ofs, 0.0);
1450 BLF_draw_buffer(mono, stamp_data.strip);
1453 /* cleanup the buffer. */
1454 BLF_buffer(mono, NULL, NULL, 0, 0, 0);
1456 #undef BUFF_MARGIN_X
1457 #undef BUFF_MARGIN_Y
1460 void BKE_stamp_info(Scene *scene, Object *camera, struct ImBuf *ibuf)
1462 struct StampData stamp_data;
1466 /* fill all the data values, no prefix */
1467 stampdata(scene, camera, &stamp_data, 0);
1469 if (stamp_data.file[0]) IMB_metadata_change_field (ibuf, "File", stamp_data.file);
1470 if (stamp_data.note[0]) IMB_metadata_change_field (ibuf, "Note", stamp_data.note);
1471 if (stamp_data.date[0]) IMB_metadata_change_field (ibuf, "Date", stamp_data.date);
1472 if (stamp_data.marker[0]) IMB_metadata_change_field (ibuf, "Marker", stamp_data.marker);
1473 if (stamp_data.time[0]) IMB_metadata_change_field (ibuf, "Time", stamp_data.time);
1474 if (stamp_data.frame[0]) IMB_metadata_change_field (ibuf, "Frame", stamp_data.frame);
1475 if (stamp_data.camera[0]) IMB_metadata_change_field (ibuf, "Camera", stamp_data.camera);
1476 if (stamp_data.cameralens[0]) IMB_metadata_change_field (ibuf, "Lens", stamp_data.cameralens);
1477 if (stamp_data.scene[0]) IMB_metadata_change_field (ibuf, "Scene", stamp_data.scene);
1478 if (stamp_data.strip[0]) IMB_metadata_change_field (ibuf, "Strip", stamp_data.strip);
1479 if (stamp_data.rendertime[0]) IMB_metadata_change_field (ibuf, "RenderTime", stamp_data.rendertime);
1482 int BKE_alphatest_ibuf(ImBuf *ibuf)
1485 if(ibuf->rect_float) {
1486 float *buf= ibuf->rect_float;
1487 for(tot= ibuf->x * ibuf->y; tot--; buf+=4) {
1493 else if (ibuf->rect) {
1494 unsigned char *buf= (unsigned char *)ibuf->rect;
1495 for(tot= ibuf->x * ibuf->y; tot--; buf+=4) {
1505 /* note: imf->planes is ignored here, its assumed the image channels
1506 * are already set */
1507 int BKE_write_ibuf(ImBuf *ibuf, const char *name, ImageFormatData *imf)
1509 char imtype= imf->imtype;
1510 char compress= imf->compress;
1511 char quality= imf->quality;
1515 if(imtype== R_IMF_IMTYPE_IRIS) {
1516 ibuf->ftype= IMAGIC;
1519 else if (imtype==R_IMF_IMTYPE_RADHDR) {
1520 ibuf->ftype= RADHDR;
1523 else if (ELEM5(imtype, R_IMF_IMTYPE_PNG, R_IMF_IMTYPE_FFMPEG, R_IMF_IMTYPE_H264, R_IMF_IMTYPE_THEORA, R_IMF_IMTYPE_XVID)) {
1526 if(imtype==R_IMF_IMTYPE_PNG)
1527 ibuf->ftype |= compress;
1531 else if (imtype==R_IMF_IMTYPE_DDS) {
1535 else if (imtype==R_IMF_IMTYPE_BMP) {
1539 else if (imtype==R_IMF_IMTYPE_TIFF) {
1542 if(imf->depth == R_IMF_CHAN_DEPTH_16)
1543 ibuf->ftype |= TIF_16BIT;
1547 else if (imtype==R_IMF_IMTYPE_OPENEXR || imtype==R_IMF_IMTYPE_MULTILAYER) {
1548 ibuf->ftype= OPENEXR;
1549 if(imf->depth == R_IMF_CHAN_DEPTH_16)
1550 ibuf->ftype |= OPENEXR_HALF;
1551 ibuf->ftype |= (imf->exr_codec & OPENEXR_COMPRESS);
1553 if(!(imf->flag & R_IMF_FLAG_ZBUF))
1554 ibuf->zbuf_float = NULL; /* signal for exr saving */
1559 else if (imtype==R_IMF_IMTYPE_CINEON) {
1560 ibuf->ftype = CINEON;
1562 else if (imtype==R_IMF_IMTYPE_DPX) {
1566 else if (imtype==R_IMF_IMTYPE_TARGA) {
1569 else if(imtype==R_IMF_IMTYPE_RAWTGA) {
1570 ibuf->ftype= RAWTGA;
1572 #ifdef WITH_OPENJPEG
1573 else if(imtype==R_IMF_IMTYPE_JP2) {
1574 if(quality < 10) quality= 90;
1575 ibuf->ftype= JP2|quality;
1577 if (imf->depth == R_IMF_CHAN_DEPTH_16) {
1578 ibuf->ftype |= JP2_16BIT;
1579 } else if (imf->depth == R_IMF_CHAN_DEPTH_12) {
1580 ibuf->ftype |= JP2_12BIT;
1583 if (imf->jp2_flag & R_IMF_JP2_FLAG_YCC) {
1584 ibuf->ftype |= JP2_YCC;
1587 if (imf->jp2_flag & R_IMF_JP2_FLAG_CINE_PRESET) {
1588 ibuf->ftype |= JP2_CINE;
1589 if (imf->jp2_flag & R_IMF_JP2_FLAG_CINE_48)
1590 ibuf->ftype |= JP2_CINE_48FPS;
1595 /* R_IMF_IMTYPE_JPEG90, etc. default we save jpegs */
1596 if(quality < 10) quality= 90;
1597 ibuf->ftype= JPG|quality;
1600 BLI_make_existing_file(name);
1602 ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
1610 /* same as BKE_write_ibuf_as but crappy workaround not to perminantly modify
1611 * _some_, values in the imbuf */
1612 int BKE_write_ibuf_as(ImBuf *ibuf, const char *name, ImageFormatData *imf,
1613 const short save_copy)
1615 ImBuf ibuf_back= *ibuf;
1618 /* all data is rgba anyway,
1619 * this just controls how to save for some formats */
1620 ibuf->planes= imf->planes;
1622 ok= BKE_write_ibuf(ibuf, name, imf);
1625 /* note that we are not restoring _all_ settings */
1626 ibuf->planes= ibuf_back.planes;
1627 ibuf->ftype= ibuf_back.ftype;
1633 int BKE_write_ibuf_stamp(Scene *scene, struct Object *camera, ImBuf *ibuf, const char *name, struct ImageFormatData *imf)
1635 if(scene && scene->r.stamp & R_STAMP_ALL)
1636 BKE_stamp_info(scene, camera, ibuf);
1638 return BKE_write_ibuf(ibuf, name, imf);
1642 void BKE_makepicstring(char *string, const char *base, const char *relbase, int frame, const char imtype, const short use_ext, const short use_frames)
1644 if (string==NULL) return;
1645 BLI_strncpy(string, base, FILE_MAX - 10); /* weak assumption */
1646 BLI_path_abs(string, relbase);
1649 BLI_path_frame(string, frame, 4);
1652 BKE_add_image_extension(string, imtype);
1656 /* used by sequencer too */
1657 struct anim *openanim(const char *name, int flags, int streamindex)
1662 anim = IMB_open_anim(name, flags, streamindex);
1663 if (anim == NULL) return NULL;
1665 ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
1667 if(BLI_exists(name))
1668 printf("not an anim: %s\n", name);
1670 printf("anim file doesn't exist: %s\n", name);
1671 IMB_free_anim(anim);
1674 IMB_freeImBuf(ibuf);
1679 /* ************************* New Image API *************** */
1682 /* Notes about Image storage
1684 -> written in .blend
1686 -> written in .blend
1688 -> comes from packedfile or filename
1690 -> comes from packedfile or filename
1692 -> ibufs from exrhandle
1694 -> ibufs come from movie, temporary renderresult or sequence
1696 -> comes from packedfile or filename or generated
1701 /* forces existence of 1 Image for renderout or nodes, returns Image */
1702 /* name is only for default, when making new one */
1703 Image *BKE_image_verify_viewer(int type, const char *name)
1707 for(ima=G.main->image.first; ima; ima= ima->id.next)
1708 if(ima->source==IMA_SRC_VIEWER)
1713 ima= image_alloc(name, IMA_SRC_VIEWER, type);
1715 /* happens on reload, imagewindow cannot be image user when hidden*/
1717 id_us_plus(&ima->id);
1722 void BKE_image_assign_ibuf(Image *ima, ImBuf *ibuf)
1724 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
1727 void BKE_image_signal(Image *ima, ImageUser *iuser, int signal)
1733 case IMA_SIGNAL_FREE:
1734 image_free_buffers(ima);
1738 case IMA_SIGNAL_SRC_CHANGE:
1739 if(ima->type == IMA_TYPE_UV_TEST)
1740 if(ima->source != IMA_SRC_GENERATED)
1741 ima->type= IMA_TYPE_IMAGE;
1743 if(ima->source==IMA_SRC_GENERATED) {
1744 if(ima->gen_x==0 || ima->gen_y==0) {
1745 ImBuf *ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
1747 ima->gen_x= ibuf->x;
1748 ima->gen_y= ibuf->y;
1753 /* force reload on first use, but not for multilayer, that makes nodes and buttons in ui drawing fail */
1754 if(ima->type!=IMA_TYPE_MULTILAYER)
1755 image_free_buffers(ima);
1762 case IMA_SIGNAL_RELOAD:
1763 /* try to repack file */
1764 if(ima->packedfile) {
1766 pf = newPackedFile(NULL, ima->name, ID_BLEND_PATH(G.main, &ima->id));
1768 freePackedFile(ima->packedfile);
1769 ima->packedfile = pf;
1770 image_free_buffers(ima);
1772 printf("ERROR: Image not available. Keeping packed image\n");
1776 image_free_buffers(ima);
1782 case IMA_SIGNAL_USER_NEW_IMAGE:
1785 if(ima->source==IMA_SRC_FILE || ima->source==IMA_SRC_SEQUENCE) {
1786 if(ima->type==IMA_TYPE_MULTILAYER) {
1787 iuser->multi_index= 0;
1788 iuser->layer= iuser->pass= 0;
1795 /* dont use notifiers because they are not 100% sure to succseed
1796 * this also makes sure all scenes are accounted for. */
1799 for(scene= G.main->scene.first; scene; scene= scene->id.next) {
1800 if(scene->nodetree) {
1801 nodeUpdateID(scene->nodetree, &ima->id);
1807 /* if layer or pass changes, we need an index for the imbufs list */
1808 /* note it is called for rendered results, but it doesnt use the index! */
1809 /* and because rendered results use fake layer/passes, don't correct for wrong indices here */
1810 RenderPass *BKE_image_multilayer_index(RenderResult *rr, ImageUser *iuser)
1813 RenderPass *rpass= NULL;
1819 short index= 0, rl_index= 0, rp_index;
1821 for(rl= rr->layers.first; rl; rl= rl->next, rl_index++) {
1823 for(rpass= rl->passes.first; rpass; rpass= rpass->next, index++, rp_index++)
1824 if(iuser->layer==rl_index && iuser->pass==rp_index)
1831 iuser->multi_index= index;
1833 iuser->multi_index= 0;
1836 rl= rr->layers.first;
1838 rpass= rl->passes.first;
1844 RenderResult *BKE_image_acquire_renderresult(Scene *scene, Image *ima)
1849 else if(ima->type==IMA_TYPE_R_RESULT) {
1850 if(ima->render_slot == ima->last_render_slot)
1851 return RE_AcquireResultRead(RE_GetRender(scene->id.name));
1853 return ima->renders[ima->render_slot];
1859 void BKE_image_release_renderresult(Scene *scene, Image *ima)
1862 else if(ima->type==IMA_TYPE_R_RESULT) {
1863 if(ima->render_slot == ima->last_render_slot)
1864 RE_ReleaseResult(RE_GetRender(scene->id.name));
1868 void BKE_image_backup_render(Scene *scene, Image *ima)
1870 /* called right before rendering, ima->renders contains render
1871 result pointers for everything but the current render */
1872 Render *re= RE_GetRender(scene->id.name);
1873 int slot= ima->render_slot, last= ima->last_render_slot;
1876 if(ima->renders[slot]) {
1877 RE_FreeRenderResult(ima->renders[slot]);
1878 ima->renders[slot]= NULL;
1881 ima->renders[last]= NULL;
1882 RE_SwapResult(re, &ima->renders[last]);
1885 ima->last_render_slot= slot;
1888 /* after imbuf load, openexr type can return with a exrhandle open */
1889 /* in that case we have to build a render-result */
1890 static void image_create_multilayer(Image *ima, ImBuf *ibuf, int framenr)
1893 ima->rr= RE_MultilayerConvert(ibuf->userdata, ibuf->x, ibuf->y);
1896 IMB_exr_close(ibuf->userdata);
1899 ibuf->userdata= NULL;
1901 ima->rr->framenr= framenr;
1904 /* common stuff to do with images after loading */
1905 static void image_initialize_after_load(Image *ima, ImBuf *ibuf)
1907 /* preview is NULL when it has never been used as an icon before */
1908 if(G.background==0 && ima->preview==NULL)
1909 BKE_icon_changed(BKE_icon_getid(&ima->id));
1912 if (ima->flag & IMA_FIELDS) {
1913 if(ima->flag & IMA_STD_FIELD) de_interlace_st(ibuf);
1914 else de_interlace_ng(ibuf);
1917 ima->lastused = clock() / CLOCKS_PER_SEC;
1919 ima->ok= IMA_OK_LOADED;
1923 static ImBuf *image_load_sequence_file(Image *ima, ImageUser *iuser, int frame)
1926 unsigned short numlen;
1927 char name[FILE_MAX], head[FILE_MAX], tail[FILE_MAX];
1930 /* XXX temp stuff? */
1931 if(ima->lastframe != frame)
1932 ima->tpageflag |= IMA_TPAGE_REFRESH;
1934 ima->lastframe= frame;
1935 BLI_strncpy(name, ima->name, sizeof(name));
1936 BLI_stringdec(name, head, tail, &numlen);
1937 BLI_stringenc(name, head, tail, numlen, frame);
1939 BLI_path_abs(name, ID_BLEND_PATH(G.main, &ima->id));
1941 flag= IB_rect|IB_multilayer;
1942 if(ima->flag & IMA_DO_PREMUL)
1946 ibuf = IMB_loadiffname(name, flag);
1950 printf(AT" loaded %s\n", name);
1952 printf(AT" missed %s\n", name);
1958 /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_get_ibuf */
1959 if (ibuf->ftype==OPENEXR && ibuf->userdata) {
1960 image_create_multilayer(ima, ibuf, frame);
1961 ima->type= IMA_TYPE_MULTILAYER;
1962 IMB_freeImBuf(ibuf);
1966 image_initialize_after_load(ima, ibuf);
1967 image_assign_ibuf(ima, ibuf, 0, frame);
1970 image_initialize_after_load(ima, ibuf);
1971 image_assign_ibuf(ima, ibuf, 0, frame);
1983 static ImBuf *image_load_sequence_multilayer(Image *ima, ImageUser *iuser, int frame)
1985 struct ImBuf *ibuf= NULL;
1987 /* either we load from RenderResult, or we have to load a new one */
1989 /* check for new RenderResult */
1990 if(ima->rr==NULL || frame!=ima->rr->framenr) {
1991 /* copy to survive not found multilayer image */
1992 RenderResult *oldrr= ima->rr;
1995 ibuf = image_load_sequence_file(ima, iuser, frame);
1997 if(ibuf) { /* actually an error */
1998 ima->type= IMA_TYPE_IMAGE;
1999 printf("error, multi is normal image\n");
2001 // printf("loaded new result %p\n", ima->rr);
2002 /* free result if new one found */
2004 // if(oldrr) printf("freed previous result %p\n", oldrr);
2005 if(oldrr) RE_FreeRenderResult(oldrr);
2013 RenderPass *rpass= BKE_image_multilayer_index(ima->rr, iuser);
2016 // printf("load from pass %s\n", rpass->name);
2017 /* since we free render results, we copy the rect */
2018 ibuf= IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0);
2019 ibuf->rect_float= MEM_dupallocN(rpass->rect);
2020 ibuf->flags |= IB_rectfloat;
2021 ibuf->mall= IB_rectfloat;
2022 ibuf->channels= rpass->channels;
2023 ibuf->profile = IB_PROFILE_LINEAR_RGB;
2025 image_initialize_after_load(ima, ibuf);
2026 image_assign_ibuf(ima, ibuf, iuser?iuser->multi_index:0, frame);
2029 // else printf("pass not found\n");
2041 static ImBuf *image_load_movie_file(Image *ima, ImageUser *iuser, int frame)
2043 struct ImBuf *ibuf= NULL;
2045 ima->lastframe= frame;
2047 if(ima->anim==NULL) {
2050 BLI_strncpy(str, ima->name, FILE_MAX);
2051 BLI_path_abs(str, ID_BLEND_PATH(G.main, &ima->id));
2053 /* FIXME: make several stream accessible in image editor, too*/
2054 ima->anim = openanim(str, IB_rect, 0);
2056 /* let's initialize this user */
2057 if(ima->anim && iuser && iuser->frames==0)
2058 iuser->frames= IMB_anim_get_duration(ima->anim,
2063 int dur = IMB_anim_get_duration(ima->anim,
2068 if(fra>(dur-1)) fra= dur-1;
2069 ibuf = IMB_makeSingleUser(
2070 IMB_anim_absolute(ima->anim, fra,
2075 image_initialize_after_load(ima, ibuf);
2076 image_assign_ibuf(ima, ibuf, 0, frame);
2090 /* warning, 'iuser' can be NULL */
2091 static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
2095 int assign = 0, flag;
2097 /* always ensure clean ima */
2098 image_free_buffers(ima);
2100 /* is there a PackedFile with this image ? */
2101 if (ima->packedfile) {
2102 flag = IB_rect|IB_multilayer;
2103 if(ima->flag & IMA_DO_PREMUL) flag |= IB_premul;
2105 ibuf = IMB_ibImageFromMemory((unsigned char*)ima->packedfile->data, ima->packedfile->size, flag, "<packed data>");
2108 flag= IB_rect|IB_multilayer|IB_metadata;
2109 if(ima->flag & IMA_DO_PREMUL)
2112 /* get the right string */
2113 BLI_strncpy(str, ima->name, sizeof(str));
2114 BLI_path_abs(str, ID_BLEND_PATH(G.main, &ima->id));
2117 ibuf = IMB_loadiffname(str, flag);
2121 /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_get_ibuf */
2122 if (ibuf->ftype==OPENEXR && ibuf->userdata) {
2123 image_create_multilayer(ima, ibuf, cfra);
2124 ima->type= IMA_TYPE_MULTILAYER;
2125 IMB_freeImBuf(ibuf);
2129 image_initialize_after_load(ima, ibuf);
2132 /* check if the image is a font image... */
2133 detectBitmapFont(ibuf);
2135 /* make packed file for autopack */
2136 if ((ima->packedfile == NULL) && (G.fileflags & G_AUTOPACK))
2137 ima->packedfile = newPackedFile(NULL, str, ID_BLEND_PATH(G.main, &ima->id));
2144 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
2152 static ImBuf *image_get_ibuf_multilayer(Image *ima, ImageUser *iuser)
2157 ibuf = image_load_image_file(ima, iuser, 0);
2158 if(ibuf) { /* actually an error */
2159 ima->type= IMA_TYPE_IMAGE;
2164 RenderPass *rpass= BKE_image_multilayer_index(ima->rr, iuser);
2167 ibuf= IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0);
2169 image_initialize_after_load(ima, ibuf);
2171 ibuf->rect_float= rpass->rect;
2172 ibuf->flags |= IB_rectfloat;
2173 ibuf->channels= rpass->channels;
2174 ibuf->profile = IB_PROFILE_LINEAR_RGB;
2176 image_assign_ibuf(ima, ibuf, iuser?iuser->multi_index:IMA_NO_INDEX, 0);
2189 /* showing RGBA result itself (from compo/sequence) or
2190 like exr, using layers etc */
2191 /* always returns a single ibuf, also during render progress */
2192 static ImBuf *image_get_render_result(Image *ima, ImageUser *iuser, void **lock_r)
2196 float *rectf, *rectz;
2199 int channels, layer, pass;
2201 int from_render= (ima->render_slot == ima->last_render_slot);
2203 if(!(iuser && iuser->scene))
2206 /* if we the caller is not going to release the lock, don't give the image */
2210 re= RE_GetRender(iuser->scene->id.name);
2213 layer= (iuser)? iuser->layer: 0;
2214 pass= (iuser)? iuser->pass: 0;
2217 RE_AcquireResultImage(re, &rres);
2219 else if(ima->renders[ima->render_slot]) {
2220 rres= *(ima->renders[ima->render_slot]);
2221 rres.have_combined= rres.rectf != NULL;
2224 memset(&rres, 0, sizeof(RenderResult));
2226 if(!(rres.rectx > 0 && rres.recty > 0)) {
2228 RE_ReleaseResultImage(re);
2232 /* release is done in BKE_image_release_ibuf using lock_r */
2234 BLI_lock_thread(LOCK_VIEWER);
2238 /* this gives active layer, composite or seqence result */
2239 rect= (unsigned int *)rres.rect32;
2242 dither= iuser->scene->r.dither_intensity;
2244 /* combined layer gets added as first layer */
2245 if(rres.have_combined && layer==0);
2246 else if(rres.layers.first) {
2247 RenderLayer *rl= BLI_findlink(&rres.layers, layer-(rres.have_combined?1:0));
2251 /* there's no combined pass, is in renderlayer itself */
2256 rpass= BLI_findlink(&rl->passes, pass-1);
2258 channels= rpass->channels;
2260 dither= 0.0f; /* don't dither passes */
2264 for(rpass= rl->passes.first; rpass; rpass= rpass->next)
2265 if(rpass->passtype == SCE_PASS_Z)
2270 ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
2272 /* make ibuf if needed, and initialize it */
2274 ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, 0);
2275 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
2278 ibuf->x= rres.rectx;
2279 ibuf->y= rres.recty;
2281 /* free rect buffer if float buffer changes, so it can be recreated with
2282 the updated result, and also in case we got byte buffer from sequencer,
2283 so we don't keep reference to freed buffer */
2284 if(ibuf->rect_float!=rectf || rect || !rectf)
2285 imb_freerectImBuf(ibuf);
2291 ibuf->rect_float= rectf;
2292 ibuf->flags |= IB_rectfloat;
2293 ibuf->channels= channels;
2296 ibuf->rect_float= NULL;
2297 ibuf->flags &= ~IB_rectfloat;
2301 ibuf->zbuf_float= rectz;
2302 ibuf->flags |= IB_zbuffloat;
2305 ibuf->zbuf_float= NULL;
2306 ibuf->flags &= ~IB_zbuffloat;
2309 /* since its possible to access the buffer from the image directly, set the profile [#25073] */
2310 ibuf->profile= (iuser->scene->r.color_mgt_flag & R_COLOR_MANAGEMENT) ? IB_PROFILE_LINEAR_RGB : IB_PROFILE_NONE;
2311 ibuf->dither= dither;
2313 if(iuser->scene->r.color_mgt_flag & R_COLOR_MANAGEMENT_PREDIVIDE) {
2314 ibuf->flags |= IB_cm_predivide;
2315 ima->flag |= IMA_CM_PREDIVIDE;
2318 ibuf->flags &= ~IB_cm_predivide;
2319 ima->flag &= ~IMA_CM_PREDIVIDE;
2322 ima->ok= IMA_OK_LOADED;
2327 static ImBuf *image_get_ibuf_threadsafe(Image *ima, ImageUser *iuser, int *frame_r, int *index_r)
2330 int frame = 0, index = 0;
2332 /* see if we already have an appropriate ibuf, with image source and type */
2333 if(ima->source==IMA_SRC_MOVIE) {
2334 frame= iuser?iuser->framenr:ima->lastframe;
2335 ibuf= image_get_ibuf(ima, 0, frame);
2336 /* XXX temp stuff? */
2337 if(ima->lastframe != frame)
2338 ima->tpageflag |= IMA_TPAGE_REFRESH;
2339 ima->lastframe = frame;
2341 else if(ima->source==IMA_SRC_SEQUENCE) {
2342 if(ima->type==IMA_TYPE_IMAGE) {
2343 frame= iuser?iuser->framenr:ima->lastframe;
2344 ibuf= image_get_ibuf(ima, 0, frame);
2346 /* XXX temp stuff? */
2347 if(ima->lastframe != frame) {
2348 ima->tpageflag |= IMA_TPAGE_REFRESH;
2350 ima->lastframe = frame;
2352 else if(ima->type==IMA_TYPE_MULTILAYER) {
2353 frame= iuser?iuser->framenr:ima->lastframe;
2354 index= iuser?iuser->multi_index:IMA_NO_INDEX;
2355 ibuf= image_get_ibuf(ima, index, frame);
2358 else if(ima->source==IMA_SRC_FILE) {
2359 if(ima->type==IMA_TYPE_IMAGE)
2360 ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
2361 else if(ima->type==IMA_TYPE_MULTILAYER)
2362 ibuf= image_get_ibuf(ima, iuser?iuser->multi_index:IMA_NO_INDEX, 0);
2364 else if(ima->source == IMA_SRC_GENERATED) {
2365 ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
2367 else if(ima->source == IMA_SRC_VIEWER) {
2368 /* always verify entirely, not that this shouldn't happen
2369 * as part of texture sampling in rendering anyway, so not
2370 * a big bottleneck */
2379 /* Checks optional ImageUser and verifies/creates ImBuf. */
2380 /* use this one if you want to get a render result in progress,
2381 * if not, use BKE_image_get_ibuf which doesn't require a release */
2382 ImBuf *BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **lock_r)
2385 float color[] = {0, 0, 0, 1};
2386 int frame= 0, index= 0;
2388 /* This function is intended to be thread-safe. It postpones the mutex lock
2389 * until it needs to load the image, if the image is already there it
2390 * should just get the pointer and return. The reason is that a lot of mutex
2391 * locks appears to be very slow on certain multicore macs, causing a render
2392 * with image textures to actually slow down as more threads are used.
2394 * Note that all the image loading functions should also make sure they do
2395 * things in a threadsafe way for image_get_ibuf_threadsafe to work correct.
2396 * That means, the last two steps must be, 1) add the ibuf to the list and
2397 * 2) set ima/iuser->ok to 0 to IMA_OK_LOADED */
2402 /* quick reject tests */
2412 /* try to get the ibuf without locking */
2413 ibuf= image_get_ibuf_threadsafe(ima, iuser, &frame, &index);
2416 /* couldn't get ibuf and image is not ok, so let's lock and try to
2418 BLI_lock_thread(LOCK_IMAGE);
2420 /* need to check ok flag and loading ibuf again, because the situation
2421 * might have changed in the meantime */
2424 BLI_unlock_thread(LOCK_IMAGE);
2428 else if(ima->ok==0) {
2429 BLI_unlock_thread(LOCK_IMAGE);
2433 ibuf= image_get_ibuf_threadsafe(ima, iuser, &frame, &index);
2436 /* we are sure we have to load the ibuf, using source and type */
2437 if(ima->source==IMA_SRC_MOVIE) {
2438 /* source is from single file, use flipbook to store ibuf */
2439 ibuf= image_load_movie_file(ima, iuser, frame);
2441 else if(ima->source==IMA_SRC_SEQUENCE) {
2442 if(ima->type==IMA_TYPE_IMAGE) {
2443 /* regular files, ibufs in flipbook, allows saving */
2444 ibuf= image_load_sequence_file(ima, iuser, frame);
2446 /* no else; on load the ima type can change */
2447 if(ima->type==IMA_TYPE_MULTILAYER) {
2448 /* only 1 layer/pass stored in imbufs, no exrhandle anim storage, no saving */
2449 ibuf= image_load_sequence_multilayer(ima, iuser, frame);
2452 else if(ima->source==IMA_SRC_FILE) {
2454 if(ima->type==IMA_TYPE_IMAGE)
2455 ibuf= image_load_image_file(ima, iuser, frame); /* cfra only for '#', this global is OK */
2456 /* no else; on load the ima type can change */
2457 if(ima->type==IMA_TYPE_MULTILAYER)
2458 /* keeps render result, stores ibufs in listbase, allows saving */
2459 ibuf= image_get_ibuf_multilayer(ima, iuser);
2462 else if(ima->source == IMA_SRC_GENERATED) {
2463 /* generated is: ibuf is allocated dynamically */
2464 /* UV testgrid or black or solid etc */
2465 if(ima->gen_x==0) ima->gen_x= 1024;
2466 if(ima->gen_y==0) ima->gen_y= 1024;
2467 ibuf= add_ibuf_size(ima->gen_x, ima->gen_y, ima->name, 24, (ima->gen_flag & IMA_GEN_FLOAT) != 0, ima->gen_type, color);
2468 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
2469 ima->ok= IMA_OK_LOADED;
2471 else if(ima->source == IMA_SRC_VIEWER) {
2472 if(ima->type==IMA_TYPE_R_RESULT) {
2473 /* always verify entirely, and potentially
2474 returns pointer to release later */
2475 ibuf= image_get_render_result(ima, iuser, lock_r);
2477 else if(ima->type==IMA_TYPE_COMPOSITE) {
2478 /* requires lock/unlock, otherwise don't return image */
2480 /* unlock in BKE_image_release_ibuf */
2481 BLI_lock_thread(LOCK_VIEWER);
2484 /* XXX anim play for viewer nodes not yet supported */
2485 frame= 0; // XXX iuser?iuser->framenr:0;
2486 ibuf= image_get_ibuf(ima, 0, frame);
2489 /* Composite Viewer, all handled in compositor */
2490 /* fake ibuf, will be filled in compositor */
2491 ibuf= IMB_allocImBuf(256, 256, 32, IB_rect);
2492 image_assign_ibuf(ima, ibuf, 0, frame);
2499 BLI_unlock_thread(LOCK_IMAGE);
2502 tag_image_time(ima);
2507 void BKE_image_release_ibuf(Image *ima, void *lock)
2509 /* for getting image during threaded render / compositing, need to release */
2511 BLI_unlock_thread(LOCK_VIEWER); /* viewer image */
2514 RE_ReleaseResultImage(lock); /* render result */
2515 BLI_unlock_thread(LOCK_VIEWER); /* view image imbuf */
2519 /* warning, this can allocate generated images */
2520 ImBuf *BKE_image_get_ibuf(Image *ima, ImageUser *iuser)
2522 /* here (+fie_ima/2-1) makes sure that division happens correctly */
2523 return BKE_image_acquire_ibuf(ima, iuser, NULL);
2526 int BKE_image_user_get_frame(const ImageUser *iuser, int cfra, int fieldnr)
2528 const int len= (iuser->fie_ima*iuser->frames)/2;
2535 cfra= cfra - iuser->sfra+1;
2539 cfra= ( (cfra) % len );
2540 if(cfra < 0) cfra+= len;
2541 if(cfra==0) cfra= len;
2545 else if(cfra>len) cfra= len;
2547 /* convert current frame to current field */
2551 /* transform to images space */
2552 framenr= (cfra+iuser->fie_ima-2)/iuser->fie_ima;
2553 if(framenr>iuser->frames) framenr= iuser->frames;
2554 framenr+= iuser->offset;
2557 framenr= ( (framenr) % len );
2558 while(framenr < 0) framenr+= len;
2559 if(framenr==0) framenr= len;
2566 void BKE_image_user_calc_frame(ImageUser *iuser, int cfra, int fieldnr)
2568 const int framenr= BKE_image_user_get_frame(iuser, cfra, fieldnr);
2570 /* allows image users to handle redraws */
2571 if(iuser->flag & IMA_ANIM_ALWAYS)
2572 if(framenr!=iuser->framenr)
2573 iuser->flag |= IMA_ANIM_REFRESHED;
2575 iuser->framenr= framenr;
2576 if(iuser->ok==0) iuser->ok= 1;
2579 int BKE_image_has_alpha(struct Image *image)
2585 ibuf= BKE_image_acquire_ibuf(image, NULL, &lock);
2586 planes = (ibuf?ibuf->planes:0);
2587 BKE_image_release_ibuf(image, lock);