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)
289 /* this function accepts link==NULL */
290 BLI_insertlinkbefore(&ima->ibufs, link, ibuf);
292 /* now we don't want copies? */
293 if(link && ibuf->index==link->index)
294 image_remove_ibuf(ima, link);
298 /* empty image block, of similar type and filename */
299 Image *copy_image(Image *ima)
301 Image *nima= image_alloc(ima->id.name+2, ima->source, ima->type);
303 BLI_strncpy(nima->name, ima->name, sizeof(ima->name));
305 nima->flag= ima->flag;
306 nima->tpageflag= ima->tpageflag;
308 nima->gen_x= ima->gen_x;
309 nima->gen_y= ima->gen_y;
310 nima->gen_type= ima->gen_type;
312 nima->animspeed= ima->animspeed;
314 nima->aspx= ima->aspx;
315 nima->aspy= ima->aspy;
320 static void extern_local_image(Image *UNUSED(ima))
322 /* Nothing to do: images don't link to other IDs. This function exists to
323 match id_make_local pattern. */
326 void make_local_image(struct Image *ima)
332 int is_local= FALSE, is_lib= FALSE;
334 /* - only lib users: do nothing
335 * - only local users: set flag
339 if(ima->id.lib==NULL) return;
341 /* Can't take short cut here: must check meshes at least because of bogus
342 texface ID refs. - z0r */
345 id_clear_lib_data(bmain, &ima->id);
346 extern_local_image(ima);
351 for(tex= bmain->tex.first; tex; tex= tex->id.next) {
352 if(tex->ima == ima) {
353 if(tex->id.lib) is_lib= TRUE;
357 for(brush= bmain->brush.first; brush; brush= brush->id.next) {
358 if(brush->clone.image == ima) {
359 if(brush->id.lib) is_lib= TRUE;
363 for(me= bmain->mesh.first; me; me= me->id.next) {
368 for(i=0; i<me->fdata.totlayer; i++) {
369 if(me->fdata.layers[i].type == CD_MTFACE) {
370 tface= (MTFace*)me->fdata.layers[i].data;
372 for(a=0; a<me->totface; a++, tface++) {
373 if(tface->tpage == ima) {
374 if(me->id.lib) is_lib= TRUE;
383 if(is_local && is_lib == FALSE) {
384 id_clear_lib_data(bmain, &ima->id);
385 extern_local_image(ima);
387 else if(is_local && is_lib) {
388 char *bpath_user_data[2]= {bmain->name, ima->id.lib->filepath};
389 Image *iman= copy_image(ima);
393 /* Remap paths of new ID using old library as base. */
394 bpath_traverse_id(bmain, &iman->id, bpath_relocate_visitor, 0, bpath_user_data);
396 tex= bmain->tex.first;
398 if(tex->id.lib==NULL) {
407 brush= bmain->brush.first;
409 if(brush->id.lib==NULL) {
410 if(brush->clone.image==ima) {
411 brush->clone.image = iman;
416 brush= brush->id.next;
418 /* Transfer references in texfaces. Texfaces don't add to image ID
419 user count *unless* there are no other users. See
420 readfile.c:lib_link_mtface. */
421 me= bmain->mesh.first;
427 for(i=0; i<me->fdata.totlayer; i++) {
428 if(me->fdata.layers[i].type == CD_MTFACE) {
429 tface= (MTFace*)me->fdata.layers[i].data;
431 for(a=0; a<me->totface; a++, tface++) {
432 if(tface->tpage == ima) {
434 if(iman->id.us == 0) {
435 tface->tpage->id.us= 1;
437 id_lib_extern((ID*)iman);
448 void BKE_image_merge(Image *dest, Image *source)
453 if(dest && source && dest!=source) {
455 while((ibuf= source->ibufs.first)) {
456 BLI_remlink(&source->ibufs, ibuf);
457 image_assign_ibuf(dest, ibuf, IMA_INDEX_PASS(ibuf->index), IMA_INDEX_FRAME(ibuf->index));
460 free_libblock(&G.main->image, source);
465 /* checks if image was already loaded, then returns same image */
466 /* otherwise creates new. */
467 /* does not load ibuf itself */
468 /* pass on optional frame for #name images */
469 Image *BKE_add_image_file(const char *name)
474 char str[FILE_MAX], strtest[FILE_MAX];
476 BLI_strncpy(str, name, sizeof(str));
477 BLI_path_abs(str, G.main->name);
480 file= open(str, O_BINARY|O_RDONLY);
481 if(file== -1) return NULL;
484 /* first search an identical image */
485 for(ima= G.main->image.first; ima; ima= ima->id.next) {
486 if(ima->source!=IMA_SRC_VIEWER && ima->source!=IMA_SRC_GENERATED) {
487 BLI_strncpy(strtest, ima->name, sizeof(ima->name));
488 BLI_path_abs(strtest, G.main->name);
490 if( strcmp(strtest, str)==0 ) {
491 if(ima->anim==NULL || ima->id.us==0) {
492 BLI_strncpy(ima->name, name, sizeof(ima->name)); /* for stringcode */
493 ima->id.us++; /* officially should not, it doesn't link here! */
504 /* create a short library name */
507 while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\') len--;
510 ima= image_alloc(libname, IMA_SRC_FILE, IMA_TYPE_IMAGE);
511 BLI_strncpy(ima->name, name, sizeof(ima->name));
513 if(BLI_testextensie_array(name, imb_ext_movie))
514 ima->source= IMA_SRC_MOVIE;
519 static ImBuf *add_ibuf_size(unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short uvtestgrid, float color[4])
522 unsigned char *rect= NULL;
523 float *rect_float= NULL;
526 ibuf= IMB_allocImBuf(width, height, depth, IB_rectfloat);
527 rect_float= (float*)ibuf->rect_float;
530 ibuf= IMB_allocImBuf(width, height, depth, IB_rect);
531 rect= (unsigned char*)ibuf->rect;
534 BLI_strncpy(ibuf->name, name, sizeof(ibuf->name));
535 ibuf->userflags |= IB_BITMAPDIRTY;
539 BKE_image_buf_fill_checker(rect, rect_float, width, height);
542 BKE_image_buf_fill_checker_color(rect, rect_float, width, height);
545 BKE_image_buf_fill_color(rect, rect_float, width, height, color);
551 /* adds new image block, creates ImBuf and initializes color */
552 Image *BKE_add_image_size(unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short uvtestgrid, float color[4])
554 /* on save, type is changed to FILE in editsima.c */
555 Image *ima= image_alloc(name, IMA_SRC_GENERATED, IMA_TYPE_UV_TEST);
560 BLI_strncpy(ima->name, name, FILE_MAX);
563 ima->gen_type= uvtestgrid;
564 ima->gen_flag |= (floatbuf ? IMA_GEN_FLOAT : 0);
566 ibuf= add_ibuf_size(width, height, name, depth, floatbuf, uvtestgrid, color);
567 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
569 ima->ok= IMA_OK_LOADED;
575 /* creates an image image owns the imbuf passed */
576 Image *BKE_add_image_imbuf(ImBuf *ibuf)
578 /* on save, type is changed to FILE in editsima.c */
581 ima= image_alloc(BLI_path_basename(ibuf->name), IMA_SRC_FILE, IMA_TYPE_IMAGE);
584 BLI_strncpy(ima->name, ibuf->name, FILE_MAX);
585 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
586 ima->ok= IMA_OK_LOADED;
592 /* packs rect from memory as PNG */
593 void BKE_image_memorypack(Image *ima)
595 ImBuf *ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
599 if (ima->packedfile) {
600 freePackedFile(ima->packedfile);
601 ima->packedfile = NULL;
607 IMB_saveiff(ibuf, ibuf->name, IB_rect | IB_mem);
608 if(ibuf->encodedbuffer==NULL) {
609 printf("memory save for pack error\n");
612 PackedFile *pf = MEM_callocN(sizeof(*pf), "PackedFile");
614 pf->data = ibuf->encodedbuffer;
615 pf->size = ibuf->encodedsize;
617 ibuf->encodedbuffer= NULL;
618 ibuf->encodedsize= 0;
619 ibuf->userflags &= ~IB_BITMAPDIRTY;
621 if(ima->source==IMA_SRC_GENERATED) {
622 ima->source= IMA_SRC_FILE;
623 ima->type= IMA_TYPE_IMAGE;
628 void tag_image_time(Image *ima)
631 ima->lastused = (int)PIL_check_seconds_timer();
635 static void tag_all_images_time()
638 int ctime = (int)PIL_check_seconds_timer();
640 ima= G.main->image.first;
642 if(ima->bindcode || ima->repbind || ima->ibufs.first) {
643 ima->lastused = ctime;
649 void free_old_images(void)
652 static int lasttime = 0;
653 int ctime = (int)PIL_check_seconds_timer();
656 Run garbage collector once for every collecting period of time
657 if textimeout is 0, that's the option to NOT run the collector
659 if (U.textimeout == 0 || ctime % U.texcollectrate || ctime == lasttime)
668 ima= G.main->image.first;
670 if((ima->flag & IMA_NOCOLLECT)==0 && ctime - ima->lastused > U.textimeout) {
672 If it's in GL memory, deallocate and set time tag to current time
673 This gives textures a "second chance" to be used before dying.
675 if(ima->bindcode || ima->repbind) {
677 ima->lastused = ctime;
679 /* Otherwise, just kill the buffers */
680 else if (ima->ibufs.first) {
681 image_free_buffers(ima);
688 static uintptr_t image_mem_size(Image *ima)
696 /* viewers have memory depending on other rules, has no valid rect pointer */
697 if(ima->source==IMA_SRC_VIEWER)
700 for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next) {
701 if(ibuf->rect) size += MEM_allocN_len(ibuf->rect);
702 else if(ibuf->rect_float) size += MEM_allocN_len(ibuf->rect_float);
704 for(level=0; level<IB_MIPMAP_LEVELS; level++) {
705 ibufm= ibuf->mipmap[level];
707 if(ibufm->rect) size += MEM_allocN_len(ibufm->rect);
708 else if(ibufm->rect_float) size += MEM_allocN_len(ibufm->rect_float);
716 void BKE_image_print_memlist(void)
719 uintptr_t size, totsize= 0;
721 for(ima= G.main->image.first; ima; ima= ima->id.next)
722 totsize += image_mem_size(ima);
724 printf("\ntotal image memory len: %.3f MB\n", (double)totsize/(double)(1024*1024));
726 for(ima= G.main->image.first; ima; ima= ima->id.next) {
727 size= image_mem_size(ima);
730 printf("%s len: %.3f MB\n", ima->id.name+2, (double)size/(double)(1024*1024));
734 void BKE_image_free_all_textures(void)
738 /* unsigned int totsize= 0; */
740 for(ima= G.main->image.first; ima; ima= ima->id.next)
741 ima->id.flag &= ~LIB_DOIT;
743 for(tex= G.main->tex.first; tex; tex= tex->id.next)
745 tex->ima->id.flag |= LIB_DOIT;
747 for(ima= G.main->image.first; ima; ima= ima->id.next) {
748 if(ima->ibufs.first && (ima->id.flag & LIB_DOIT)) {
751 for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next) {
752 /* escape when image is painted on */
753 if(ibuf->userflags & IB_BITMAPDIRTY)
756 /* if(ibuf->mipmap[0])
757 totsize+= 1.33*ibuf->x*ibuf->y*4;
759 totsize+= ibuf->x*ibuf->y*4;*/
763 image_free_buffers(ima);
766 /* printf("freed total %d MB\n", totsize/(1024*1024)); */
769 /* except_frame is weak, only works for seqs without offset... */
770 void BKE_image_free_anim_ibufs(Image *ima, int except_frame)
774 for(ibuf= ima->ibufs.first; ibuf; ibuf= nbuf) {
776 if(ibuf->userflags & IB_BITMAPDIRTY)
778 if(ibuf->index==IMA_NO_INDEX)
780 if(except_frame!=IMA_INDEX_FRAME(ibuf->index)) {
781 BLI_remlink(&ima->ibufs, ibuf);
783 if (ibuf->userdata) {
784 MEM_freeN(ibuf->userdata);
785 ibuf->userdata = NULL;
792 void BKE_image_all_free_anim_ibufs(int cfra)
796 for(ima= G.main->image.first; ima; ima= ima->id.next)
797 if(ELEM(ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE))
798 BKE_image_free_anim_ibufs(ima, cfra);
802 /* *********** READ AND WRITE ************** */
804 int BKE_imtype_to_ftype(int imtype)
808 else if(imtype==R_RAWTGA)
810 else if(imtype== R_IRIS)
813 else if (imtype==R_RADHDR)
816 else if (imtype==R_PNG)
819 else if (imtype==R_DDS)
822 else if (imtype==R_BMP)
825 else if (imtype==R_TIFF)
828 else if (imtype==R_OPENEXR || imtype==R_MULTILAYER)
831 else if (imtype==R_CINEON)
833 else if (imtype==R_DPX)
837 else if(imtype==R_JP2)
844 int BKE_ftype_to_imtype(int ftype)
848 else if(ftype == IMAGIC)
851 else if (ftype & RADHDR)
854 else if (ftype & PNG)
857 else if (ftype & DDS)
860 else if (ftype & BMP)
863 else if (ftype & TIF)
866 else if (ftype & OPENEXR)
869 else if (ftype & CINEON)
871 else if (ftype & DPX)
874 else if (ftype & TGA)
876 else if(ftype & RAWTGA)
887 int BKE_imtype_is_movie(int imtype)
904 int BKE_add_image_extension(char *string, int imtype)
906 const char *extension= NULL;
908 if(imtype== R_IRIS) {
909 if(!BLI_testextensie(string, ".rgb"))
912 else if(imtype==R_IRIZ) {
913 if(!BLI_testextensie(string, ".rgb"))
917 else if(imtype==R_RADHDR) {
918 if(!BLI_testextensie(string, ".hdr"))
922 else if (ELEM5(imtype, R_PNG, R_FFMPEG, R_H264, R_THEORA, R_XVID)) {
923 if(!BLI_testextensie(string, ".png"))
927 else if(imtype==R_DDS) {
928 if(!BLI_testextensie(string, ".dds"))
932 else if(imtype==R_RAWTGA) {
933 if(!BLI_testextensie(string, ".tga"))
936 else if(imtype==R_BMP) {
937 if(!BLI_testextensie(string, ".bmp"))
941 else if(imtype==R_TIFF) {
942 if(!BLI_testextensie(string, ".tif") &&
943 !BLI_testextensie(string, ".tiff")) extension= ".tif";
947 else if( ELEM(imtype, R_OPENEXR, R_MULTILAYER)) {
948 if(!BLI_testextensie(string, ".exr"))
953 else if(imtype==R_CINEON){
954 if (!BLI_testextensie(string, ".cin"))
957 else if(imtype==R_DPX){
958 if (!BLI_testextensie(string, ".dpx"))
962 else if(imtype==R_TARGA) {
963 if(!BLI_testextensie(string, ".tga"))
967 else if(imtype==R_JP2) {
968 if(!BLI_testextensie(string, ".jp2"))
972 else { // R_AVICODEC, R_AVIRAW, R_AVIJPEG, R_JPEG90, R_QUICKTIME etc
973 if(!( BLI_testextensie(string, ".jpg") || BLI_testextensie(string, ".jpeg")))
978 /* prefer this in many cases to avoid .png.tga, but in certain cases it breaks */
979 /* remove any other known image extension */
980 if(BLI_testextensie_array(string, imb_ext_image)
981 || (G.have_quicktime && BLI_testextensie_array(string, imb_ext_image_qt))) {
982 return BLI_replace_extension(string, FILE_MAX, extension);
984 strcat(string, extension);
994 /* could allow access externally - 512 is for long names, 64 is for id names */
995 typedef struct StampData {
1003 char cameralens[64];
1006 char rendertime[64];
1009 static void stampdata(Scene *scene, Object *camera, StampData *stamp_data, int do_prefix)
1015 if (scene->r.stamp & R_STAMP_FILENAME) {
1016 BLI_snprintf(stamp_data->file, sizeof(stamp_data->file), do_prefix ? "File %s":"%s", G.relbase_valid ? G.main->name:"<untitled>");
1018 stamp_data->file[0] = '\0';
1021 if (scene->r.stamp & R_STAMP_NOTE) {
1022 /* Never do prefix for Note */
1023 BLI_snprintf(stamp_data->note, sizeof(stamp_data->note), "%s", scene->r.stamp_udata);
1025 stamp_data->note[0] = '\0';
1028 if (scene->r.stamp & R_STAMP_DATE) {
1031 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);
1032 BLI_snprintf(stamp_data->date, sizeof(stamp_data->date), do_prefix ? "Date %s":"%s", text);
1034 stamp_data->date[0] = '\0';
1037 if (scene->r.stamp & R_STAMP_MARKER) {
1038 char *name = scene_find_last_marker_name(scene, CFRA);
1040 if (name) BLI_strncpy(text, name, sizeof(text));
1041 else strcpy(text, "<none>");
1043 BLI_snprintf(stamp_data->marker, sizeof(stamp_data->marker), do_prefix ? "Marker %s":"%s", text);
1045 stamp_data->marker[0] = '\0';
1048 if (scene->r.stamp & R_STAMP_TIME) {
1049 int f = (int)(scene->r.cfra % scene->r.frs_sec);
1050 int s = (int)(scene->r.cfra / scene->r.frs_sec);
1064 if (scene->r.frs_sec < 100)
1065 BLI_snprintf(text, sizeof(text), "%02d:%02d:%02d.%02d", h, m, s, f);
1067 BLI_snprintf(text, sizeof(text), "%02d:%02d:%02d.%03d", h, m, s, f);
1069 BLI_snprintf(stamp_data->time, sizeof(stamp_data->time), do_prefix ? "Time %s":"%s", text);
1071 stamp_data->time[0] = '\0';
1074 if (scene->r.stamp & R_STAMP_FRAME) {
1079 digits= 1 + (int) log10(scene->r.efra);
1081 BLI_snprintf(format, sizeof(format), do_prefix ? "Frame %%0%di":"%%0%di", digits);
1082 BLI_snprintf (stamp_data->frame, sizeof(stamp_data->frame), format, scene->r.cfra);
1084 stamp_data->frame[0] = '\0';
1087 if (scene->r.stamp & R_STAMP_CAMERA) {
1088 BLI_snprintf(stamp_data->camera, sizeof(stamp_data->camera), do_prefix ? "Camera %s":"%s", camera ? camera->id.name+2 : "<none>");
1090 stamp_data->camera[0] = '\0';
1093 if (scene->r.stamp & R_STAMP_CAMERALENS) {
1094 if (camera && camera->type == OB_CAMERA) {
1095 BLI_snprintf(text, sizeof(text), "%.2f", ((Camera *)camera->data)->lens);
1097 else strcpy(text, "<none>");
1099 BLI_snprintf(stamp_data->cameralens, sizeof(stamp_data->cameralens), do_prefix ? "Lens %s":"%s", text);
1101 stamp_data->cameralens[0] = '\0';
1104 if (scene->r.stamp & R_STAMP_SCENE) {
1105 BLI_snprintf(stamp_data->scene, sizeof(stamp_data->scene), do_prefix ? "Scene %s":"%s", scene->id.name+2);
1107 stamp_data->scene[0] = '\0';
1110 if (scene->r.stamp & R_STAMP_SEQSTRIP) {
1111 Sequence *seq= seq_foreground_frame_get(scene, scene->r.cfra);
1113 if (seq) BLI_strncpy(text, seq->name+2, sizeof(text));
1114 else strcpy(text, "<none>");
1116 BLI_snprintf(stamp_data->strip, sizeof(stamp_data->strip), do_prefix ? "Strip %s":"%s", text);
1118 stamp_data->strip[0] = '\0';
1122 Render *re= RE_GetRender(scene->id.name);
1123 RenderStats *stats= re ? RE_GetStats(re):NULL;
1125 if (stats && (scene->r.stamp & R_STAMP_RENDERTIME)) {
1126 BLI_timestr(stats->lastframetime, text);
1128 BLI_snprintf(stamp_data->rendertime, sizeof(stamp_data->rendertime), do_prefix ? "RenderTime %s":"%s", text);
1130 stamp_data->rendertime[0] = '\0';
1135 void BKE_stamp_buf(Scene *scene, Object *camera, unsigned char *rect, float *rectf, int width, int height, int channels)
1137 struct StampData stamp_data;
1141 const int mono= blf_mono_font_render; // XXX
1143 #define BUFF_MARGIN_X 2
1144 #define BUFF_MARGIN_Y 1
1146 if (!rect && !rectf)
1149 stampdata(scene, camera, &stamp_data, 1);
1151 /* TODO, do_versions */
1152 if(scene->r.stamp_font_id < 8)
1153 scene->r.stamp_font_id= 12;
1155 /* set before return */
1156 BLF_size(mono, scene->r.stamp_font_id, 72);
1158 BLF_buffer(mono, rectf, rect, width, height, channels);
1159 BLF_buffer_col(mono, scene->r.fg_stamp[0], scene->r.fg_stamp[1], scene->r.fg_stamp[2], 1.0);
1160 pad= BLF_width_max(mono);
1162 /* use 'h_fixed' rather than 'h', aligns better */
1163 h_fixed= BLF_height_max(mono);
1164 y_ofs = -BLF_descender(mono);
1169 if (stamp_data.file[0]) {
1170 /* Top left corner */
1171 BLF_width_and_height(mono, stamp_data.file, &w, &h); h= h_fixed;
1174 /* also a little of space to the background. */
1175 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);
1177 /* and draw the text. */
1178 BLF_position(mono, x, y + y_ofs, 0.0);
1179 BLF_draw_buffer(mono, stamp_data.file);
1181 /* the extra pixel for background. */
1182 y -= BUFF_MARGIN_Y * 2;
1185 /* Top left corner, below File */
1186 if (stamp_data.note[0]) {
1187 BLF_width_and_height(mono, stamp_data.note, &w, &h); h= h_fixed;
1190 /* and space for background. */
1191 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);
1193 BLF_position(mono, x, y + y_ofs, 0.0);
1194 BLF_draw_buffer(mono, stamp_data.note);
1196 /* the extra pixel for background. */
1197 y -= BUFF_MARGIN_Y * 2;
1200 /* Top left corner, below File (or Note) */
1201 if (stamp_data.date[0]) {
1202 BLF_width_and_height(mono, stamp_data.date, &w, &h); h= h_fixed;
1205 /* and space for background. */
1206 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);
1208 BLF_position(mono, x, y + y_ofs, 0.0);
1209 BLF_draw_buffer(mono, stamp_data.date);
1211 /* the extra pixel for background. */
1212 y -= BUFF_MARGIN_Y * 2;
1215 /* Top left corner, below File, Date or Note */
1216 if (stamp_data.rendertime[0]) {
1217 BLF_width_and_height(mono, stamp_data.rendertime, &w, &h); h= h_fixed;
1220 /* and space for background. */
1221 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);
1223 BLF_position(mono, x, y + y_ofs, 0.0);
1224 BLF_draw_buffer(mono, stamp_data.rendertime);
1230 /* Bottom left corner, leaving space for timing */
1231 if (stamp_data.marker[0]) {
1232 BLF_width_and_height(mono, stamp_data.marker, &w, &h); h= h_fixed;
1234 /* extra space for background. */
1235 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);
1237 /* and pad the text. */
1238 BLF_position(mono, x, y + y_ofs, 0.0);
1239 BLF_draw_buffer(mono, stamp_data.marker);
1245 /* Left bottom corner */
1246 if (stamp_data.time[0]) {
1247 BLF_width_and_height(mono, stamp_data.time, &w, &h); h= h_fixed;
1249 /* extra space for background */
1250 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);
1252 /* and pad the text. */
1253 BLF_position(mono, x, y + y_ofs, 0.0);
1254 BLF_draw_buffer(mono, stamp_data.time);
1260 if (stamp_data.frame[0]) {
1261 BLF_width_and_height(mono, stamp_data.frame, &w, &h); h= h_fixed;
1263 /* extra space for background. */
1264 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);
1266 /* and pad the text. */
1267 BLF_position(mono, x, y + y_ofs, 0.0);
1268 BLF_draw_buffer(mono, stamp_data.frame);
1274 if (stamp_data.camera[0]) {
1275 BLF_width_and_height(mono, stamp_data.camera, &w, &h); h= h_fixed;
1277 /* extra space for background. */
1278 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);
1279 BLF_position(mono, x, y + y_ofs, 0.0);
1280 BLF_draw_buffer(mono, stamp_data.camera);
1286 if (stamp_data.cameralens[0]) {
1287 BLF_width_and_height(mono, stamp_data.cameralens, &w, &h); h= h_fixed;
1289 /* extra space for background. */
1290 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);
1291 BLF_position(mono, x, y + y_ofs, 0.0);
1292 BLF_draw_buffer(mono, stamp_data.cameralens);
1295 if (stamp_data.scene[0]) {
1296 BLF_width_and_height(mono, stamp_data.scene, &w, &h); h= h_fixed;
1298 /* Bottom right corner, with an extra space because blenfont is too strict! */
1301 /* extra space for background. */
1302 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);
1304 /* and pad the text. */
1305 BLF_position(mono, x, y+y_ofs, 0.0);
1306 BLF_draw_buffer(mono, stamp_data.scene);
1309 if (stamp_data.strip[0]) {
1310 BLF_width_and_height(mono, stamp_data.strip, &w, &h); h= h_fixed;
1312 /* Top right corner, with an extra space because blenfont is too strict! */
1316 /* extra space for background. */
1317 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);
1319 BLF_position(mono, x, y + y_ofs, 0.0);
1320 BLF_draw_buffer(mono, stamp_data.strip);
1323 /* cleanup the buffer. */
1324 BLF_buffer(mono, NULL, NULL, 0, 0, 0);
1326 #undef BUFF_MARGIN_X
1327 #undef BUFF_MARGIN_Y
1330 void BKE_stamp_info(Scene *scene, Object *camera, struct ImBuf *ibuf)
1332 struct StampData stamp_data;
1336 /* fill all the data values, no prefix */
1337 stampdata(scene, camera, &stamp_data, 0);
1339 if (stamp_data.file[0]) IMB_metadata_change_field (ibuf, "File", stamp_data.file);
1340 if (stamp_data.note[0]) IMB_metadata_change_field (ibuf, "Note", stamp_data.note);
1341 if (stamp_data.date[0]) IMB_metadata_change_field (ibuf, "Date", stamp_data.date);
1342 if (stamp_data.marker[0]) IMB_metadata_change_field (ibuf, "Marker", stamp_data.marker);
1343 if (stamp_data.time[0]) IMB_metadata_change_field (ibuf, "Time", stamp_data.time);
1344 if (stamp_data.frame[0]) IMB_metadata_change_field (ibuf, "Frame", stamp_data.frame);
1345 if (stamp_data.camera[0]) IMB_metadata_change_field (ibuf, "Camera", stamp_data.camera);
1346 if (stamp_data.cameralens[0]) IMB_metadata_change_field (ibuf, "Lens", stamp_data.cameralens);
1347 if (stamp_data.scene[0]) IMB_metadata_change_field (ibuf, "Scene", stamp_data.scene);
1348 if (stamp_data.strip[0]) IMB_metadata_change_field (ibuf, "Strip", stamp_data.strip);
1349 if (stamp_data.rendertime[0]) IMB_metadata_change_field (ibuf, "RenderTime", stamp_data.rendertime);
1352 int BKE_alphatest_ibuf(ImBuf *ibuf)
1355 if(ibuf->rect_float) {
1356 float *buf= ibuf->rect_float;
1357 for(tot= ibuf->x * ibuf->y; tot--; buf+=4) {
1363 else if (ibuf->rect) {
1364 unsigned char *buf= (unsigned char *)ibuf->rect;
1365 for(tot= ibuf->x * ibuf->y; tot--; buf+=4) {
1375 int BKE_write_ibuf(ImBuf *ibuf, const char *name, int imtype, int subimtype, int quality)
1378 (void)subimtype; /* quies unused warnings */
1381 /* use whatever existing image type is set by 'ibuf' */
1383 else if(imtype== R_IRIS) {
1384 ibuf->ftype= IMAGIC;
1387 else if (imtype==R_RADHDR) {
1388 ibuf->ftype= RADHDR;
1391 else if (ELEM5(imtype, R_PNG, R_FFMPEG, R_H264, R_THEORA, R_XVID)) {
1395 ibuf->ftype |= quality; /* quality is actually compression 0-100 --> 0-9 */
1399 else if (imtype==R_DDS) {
1403 else if (imtype==R_BMP) {
1407 else if (imtype==R_TIFF) {
1410 if(subimtype & R_TIFF_16BIT)
1411 ibuf->ftype |= TIF_16BIT;
1415 else if (imtype==R_OPENEXR || imtype==R_MULTILAYER) {
1416 ibuf->ftype= OPENEXR;
1417 if(subimtype & R_OPENEXR_HALF)
1418 ibuf->ftype |= OPENEXR_HALF;
1419 ibuf->ftype |= (quality & OPENEXR_COMPRESS);
1421 if(!(subimtype & R_OPENEXR_ZBUF))
1422 ibuf->zbuf_float = NULL; /* signal for exr saving */
1427 else if (imtype==R_CINEON) {
1428 ibuf->ftype = CINEON;
1430 else if (imtype==R_DPX) {
1434 else if (imtype==R_TARGA) {
1437 else if(imtype==R_RAWTGA) {
1438 ibuf->ftype= RAWTGA;
1440 #ifdef WITH_OPENJPEG
1441 else if(imtype==R_JP2) {
1442 if(quality < 10) quality= 90;
1443 ibuf->ftype= JP2|quality;
1445 if (subimtype & R_JPEG2K_16BIT) {
1446 ibuf->ftype |= JP2_16BIT;
1447 } else if (subimtype & R_JPEG2K_12BIT) {
1448 ibuf->ftype |= JP2_12BIT;
1451 if (subimtype & R_JPEG2K_YCC) {
1452 ibuf->ftype |= JP2_YCC;
1455 if (subimtype & R_JPEG2K_CINE_PRESET) {
1456 ibuf->ftype |= JP2_CINE;
1457 if (subimtype & R_JPEG2K_CINE_48FPS)
1458 ibuf->ftype |= JP2_CINE_48FPS;
1463 /* R_JPEG90, etc. default we save jpegs */
1464 if(quality < 10) quality= 90;
1465 ibuf->ftype= JPG|quality;
1466 if(ibuf->depth==32) ibuf->depth= 24; /* unsupported feature only confuses other s/w */
1469 BLI_make_existing_file(name);
1471 ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
1479 int BKE_write_ibuf_stamp(Scene *scene, struct Object *camera, ImBuf *ibuf, const char *name, int imtype, int subimtype, int quality)
1481 if(scene && scene->r.stamp & R_STAMP_ALL)
1482 BKE_stamp_info(scene, camera, ibuf);
1484 return BKE_write_ibuf(ibuf, name, imtype, subimtype, quality);
1488 void BKE_makepicstring(char *string, const char *base, int frame, int imtype, const short use_ext, const short use_frames)
1490 if (string==NULL) return;
1491 BLI_strncpy(string, base, FILE_MAX - 10); /* weak assumption */
1492 BLI_path_abs(string, G.main->name);
1495 BLI_path_frame(string, frame, 4);
1498 BKE_add_image_extension(string, imtype);
1502 /* used by sequencer too */
1503 struct anim *openanim(const char *name, int flags, int streamindex)
1508 anim = IMB_open_anim(name, flags, streamindex);
1509 if (anim == NULL) return NULL;
1511 ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
1513 if(BLI_exists(name))
1514 printf("not an anim: %s\n", name);
1516 printf("anim file doesn't exist: %s\n", name);
1517 IMB_free_anim(anim);
1520 IMB_freeImBuf(ibuf);
1525 /* ************************* New Image API *************** */
1528 /* Notes about Image storage
1530 -> written in .blend
1532 -> written in .blend
1534 -> comes from packedfile or filename
1536 -> comes from packedfile or filename
1538 -> ibufs from exrhandle
1540 -> ibufs come from movie, temporary renderresult or sequence
1542 -> comes from packedfile or filename or generated
1547 /* forces existence of 1 Image for renderout or nodes, returns Image */
1548 /* name is only for default, when making new one */
1549 Image *BKE_image_verify_viewer(int type, const char *name)
1553 for(ima=G.main->image.first; ima; ima= ima->id.next)
1554 if(ima->source==IMA_SRC_VIEWER)
1559 ima= image_alloc(name, IMA_SRC_VIEWER, type);
1561 /* happens on reload, imagewindow cannot be image user when hidden*/
1563 id_us_plus(&ima->id);
1568 void BKE_image_assign_ibuf(Image *ima, ImBuf *ibuf)
1570 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
1573 void BKE_image_signal(Image *ima, ImageUser *iuser, int signal)
1579 case IMA_SIGNAL_FREE:
1580 image_free_buffers(ima);
1584 case IMA_SIGNAL_SRC_CHANGE:
1585 if(ima->type == IMA_TYPE_UV_TEST)
1586 if(ima->source != IMA_SRC_GENERATED)
1587 ima->type= IMA_TYPE_IMAGE;
1589 if(ima->source==IMA_SRC_GENERATED) {
1590 if(ima->gen_x==0 || ima->gen_y==0) {
1591 ImBuf *ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
1593 ima->gen_x= ibuf->x;
1594 ima->gen_y= ibuf->y;
1599 /* force reload on first use, but not for multilayer, that makes nodes and buttons in ui drawing fail */
1600 if(ima->type!=IMA_TYPE_MULTILAYER)
1601 image_free_buffers(ima);
1608 case IMA_SIGNAL_RELOAD:
1609 /* try to repack file */
1610 if(ima->packedfile) {
1612 pf = newPackedFile(NULL, ima->name, ID_BLEND_PATH(G.main, &ima->id));
1614 freePackedFile(ima->packedfile);
1615 ima->packedfile = pf;
1616 image_free_buffers(ima);
1618 printf("ERROR: Image not available. Keeping packed image\n");
1622 image_free_buffers(ima);
1628 case IMA_SIGNAL_USER_NEW_IMAGE:
1631 if(ima->source==IMA_SRC_FILE || ima->source==IMA_SRC_SEQUENCE) {
1632 if(ima->type==IMA_TYPE_MULTILAYER) {
1633 iuser->multi_index= 0;
1634 iuser->layer= iuser->pass= 0;
1641 /* dont use notifiers because they are not 100% sure to succseed
1642 * this also makes sure all scenes are accounted for. */
1645 for(scene= G.main->scene.first; scene; scene= scene->id.next) {
1646 if(scene->nodetree) {
1647 nodeUpdateID(scene->nodetree, &ima->id);
1653 /* if layer or pass changes, we need an index for the imbufs list */
1654 /* note it is called for rendered results, but it doesnt use the index! */
1655 /* and because rendered results use fake layer/passes, don't correct for wrong indices here */
1656 RenderPass *BKE_image_multilayer_index(RenderResult *rr, ImageUser *iuser)
1659 RenderPass *rpass= NULL;
1665 short index= 0, rl_index= 0, rp_index;
1667 for(rl= rr->layers.first; rl; rl= rl->next, rl_index++) {
1669 for(rpass= rl->passes.first; rpass; rpass= rpass->next, index++, rp_index++)
1670 if(iuser->layer==rl_index && iuser->pass==rp_index)
1677 iuser->multi_index= index;
1679 iuser->multi_index= 0;
1682 rl= rr->layers.first;
1684 rpass= rl->passes.first;
1690 RenderResult *BKE_image_acquire_renderresult(Scene *scene, Image *ima)
1695 else if(ima->type==IMA_TYPE_R_RESULT) {
1696 if(ima->render_slot == ima->last_render_slot)
1697 return RE_AcquireResultRead(RE_GetRender(scene->id.name));
1699 return ima->renders[ima->render_slot];
1705 void BKE_image_release_renderresult(Scene *scene, Image *ima)
1708 else if(ima->type==IMA_TYPE_R_RESULT) {
1709 if(ima->render_slot == ima->last_render_slot)
1710 RE_ReleaseResult(RE_GetRender(scene->id.name));
1714 void BKE_image_backup_render(Scene *scene, Image *ima)
1716 /* called right before rendering, ima->renders contains render
1717 result pointers for everything but the current render */
1718 Render *re= RE_GetRender(scene->id.name);
1719 int slot= ima->render_slot, last= ima->last_render_slot;
1722 if(ima->renders[slot]) {
1723 RE_FreeRenderResult(ima->renders[slot]);
1724 ima->renders[slot]= NULL;
1727 ima->renders[last]= NULL;
1728 RE_SwapResult(re, &ima->renders[last]);
1731 ima->last_render_slot= slot;
1734 /* after imbuf load, openexr type can return with a exrhandle open */
1735 /* in that case we have to build a render-result */
1736 static void image_create_multilayer(Image *ima, ImBuf *ibuf, int framenr)
1739 ima->rr= RE_MultilayerConvert(ibuf->userdata, ibuf->x, ibuf->y);
1742 IMB_exr_close(ibuf->userdata);
1745 ibuf->userdata= NULL;
1747 ima->rr->framenr= framenr;
1750 /* common stuff to do with images after loading */
1751 static void image_initialize_after_load(Image *ima, ImBuf *ibuf)
1753 /* preview is NULL when it has never been used as an icon before */
1754 if(G.background==0 && ima->preview==NULL)
1755 BKE_icon_changed(BKE_icon_getid(&ima->id));
1758 if (ima->flag & IMA_FIELDS) {
1759 if(ima->flag & IMA_STD_FIELD) de_interlace_st(ibuf);
1760 else de_interlace_ng(ibuf);
1763 ima->lastused = clock() / CLOCKS_PER_SEC;
1765 ima->ok= IMA_OK_LOADED;
1769 static ImBuf *image_load_sequence_file(Image *ima, ImageUser *iuser, int frame)
1772 unsigned short numlen;
1773 char name[FILE_MAX], head[FILE_MAX], tail[FILE_MAX];
1776 /* XXX temp stuff? */
1777 if(ima->lastframe != frame)
1778 ima->tpageflag |= IMA_TPAGE_REFRESH;
1780 ima->lastframe= frame;
1781 BLI_strncpy(name, ima->name, sizeof(name));
1782 BLI_stringdec(name, head, tail, &numlen);
1783 BLI_stringenc(name, head, tail, numlen, frame);
1785 BLI_path_abs(name, ID_BLEND_PATH(G.main, &ima->id));
1787 flag= IB_rect|IB_multilayer;
1788 if(ima->flag & IMA_DO_PREMUL)
1792 ibuf = IMB_loadiffname(name, flag);
1796 printf(AT" loaded %s\n", name);
1798 printf(AT" missed %s\n", name);
1804 /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_get_ibuf */
1805 if (ibuf->ftype==OPENEXR && ibuf->userdata) {
1806 image_create_multilayer(ima, ibuf, frame);
1807 ima->type= IMA_TYPE_MULTILAYER;
1808 IMB_freeImBuf(ibuf);
1812 image_initialize_after_load(ima, ibuf);
1813 image_assign_ibuf(ima, ibuf, 0, frame);
1816 image_initialize_after_load(ima, ibuf);
1817 image_assign_ibuf(ima, ibuf, 0, frame);
1829 static ImBuf *image_load_sequence_multilayer(Image *ima, ImageUser *iuser, int frame)
1831 struct ImBuf *ibuf= NULL;
1833 /* either we load from RenderResult, or we have to load a new one */
1835 /* check for new RenderResult */
1836 if(ima->rr==NULL || frame!=ima->rr->framenr) {
1837 /* copy to survive not found multilayer image */
1838 RenderResult *oldrr= ima->rr;
1841 ibuf = image_load_sequence_file(ima, iuser, frame);
1843 if(ibuf) { /* actually an error */
1844 ima->type= IMA_TYPE_IMAGE;
1845 printf("error, multi is normal image\n");
1847 // printf("loaded new result %p\n", ima->rr);
1848 /* free result if new one found */
1850 // if(oldrr) printf("freed previous result %p\n", oldrr);
1851 if(oldrr) RE_FreeRenderResult(oldrr);
1859 RenderPass *rpass= BKE_image_multilayer_index(ima->rr, iuser);
1862 // printf("load from pass %s\n", rpass->name);
1863 /* since we free render results, we copy the rect */
1864 ibuf= IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0);
1865 ibuf->rect_float= MEM_dupallocN(rpass->rect);
1866 ibuf->flags |= IB_rectfloat;
1867 ibuf->mall= IB_rectfloat;
1868 ibuf->channels= rpass->channels;
1869 ibuf->profile = IB_PROFILE_LINEAR_RGB;
1871 image_initialize_after_load(ima, ibuf);
1872 image_assign_ibuf(ima, ibuf, iuser?iuser->multi_index:0, frame);
1875 // else printf("pass not found\n");
1887 static ImBuf *image_load_movie_file(Image *ima, ImageUser *iuser, int frame)
1889 struct ImBuf *ibuf= NULL;
1891 ima->lastframe= frame;
1893 if(ima->anim==NULL) {
1896 BLI_strncpy(str, ima->name, FILE_MAX);
1897 BLI_path_abs(str, ID_BLEND_PATH(G.main, &ima->id));
1899 /* FIXME: make several stream accessible in image editor, too*/
1900 ima->anim = openanim(str, IB_rect, 0);
1902 /* let's initialize this user */
1903 if(ima->anim && iuser && iuser->frames==0)
1904 iuser->frames= IMB_anim_get_duration(ima->anim,
1909 int dur = IMB_anim_get_duration(ima->anim,
1914 if(fra>(dur-1)) fra= dur-1;
1915 ibuf = IMB_makeSingleUser(
1916 IMB_anim_absolute(ima->anim, fra,
1921 image_initialize_after_load(ima, ibuf);
1922 image_assign_ibuf(ima, ibuf, 0, frame);
1936 /* warning, 'iuser' can be NULL */
1937 static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
1941 int assign = 0, flag;
1943 /* always ensure clean ima */
1944 image_free_buffers(ima);
1946 /* is there a PackedFile with this image ? */
1947 if (ima->packedfile) {
1948 flag = IB_rect|IB_multilayer;
1949 if(ima->flag & IMA_DO_PREMUL) flag |= IB_premul;
1951 ibuf = IMB_ibImageFromMemory((unsigned char*)ima->packedfile->data, ima->packedfile->size, flag, "<packed data>");
1954 flag= IB_rect|IB_multilayer|IB_metadata;
1955 if(ima->flag & IMA_DO_PREMUL)
1958 /* get the right string */
1959 BLI_strncpy(str, ima->name, sizeof(str));
1960 BLI_path_abs(str, ID_BLEND_PATH(G.main, &ima->id));
1963 ibuf = IMB_loadiffname(str, flag);
1967 /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_get_ibuf */
1968 if (ibuf->ftype==OPENEXR && ibuf->userdata) {
1969 image_create_multilayer(ima, ibuf, cfra);
1970 ima->type= IMA_TYPE_MULTILAYER;
1971 IMB_freeImBuf(ibuf);
1975 image_initialize_after_load(ima, ibuf);
1978 /* check if the image is a font image... */
1979 detectBitmapFont(ibuf);
1981 /* make packed file for autopack */
1982 if ((ima->packedfile == NULL) && (G.fileflags & G_AUTOPACK))
1983 ima->packedfile = newPackedFile(NULL, str, ID_BLEND_PATH(G.main, &ima->id));
1990 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
1998 static ImBuf *image_get_ibuf_multilayer(Image *ima, ImageUser *iuser)
2003 ibuf = image_load_image_file(ima, iuser, 0);
2004 if(ibuf) { /* actually an error */
2005 ima->type= IMA_TYPE_IMAGE;
2010 RenderPass *rpass= BKE_image_multilayer_index(ima->rr, iuser);
2013 ibuf= IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0);
2015 image_initialize_after_load(ima, ibuf);
2017 ibuf->rect_float= rpass->rect;
2018 ibuf->flags |= IB_rectfloat;
2019 ibuf->channels= rpass->channels;
2020 ibuf->profile = IB_PROFILE_LINEAR_RGB;
2022 image_assign_ibuf(ima, ibuf, iuser?iuser->multi_index:IMA_NO_INDEX, 0);
2035 /* showing RGBA result itself (from compo/sequence) or
2036 like exr, using layers etc */
2037 /* always returns a single ibuf, also during render progress */
2038 static ImBuf *image_get_render_result(Image *ima, ImageUser *iuser, void **lock_r)
2042 float *rectf, *rectz;
2045 int channels, layer, pass;
2047 int from_render= (ima->render_slot == ima->last_render_slot);
2049 if(!(iuser && iuser->scene))
2052 /* if we the caller is not going to release the lock, don't give the image */
2056 re= RE_GetRender(iuser->scene->id.name);
2059 layer= (iuser)? iuser->layer: 0;
2060 pass= (iuser)? iuser->pass: 0;
2063 RE_AcquireResultImage(re, &rres);
2065 else if(ima->renders[ima->render_slot]) {
2066 rres= *(ima->renders[ima->render_slot]);
2067 rres.have_combined= rres.rectf != NULL;
2070 memset(&rres, 0, sizeof(RenderResult));
2072 if(!(rres.rectx > 0 && rres.recty > 0)) {
2074 RE_ReleaseResultImage(re);
2078 /* release is done in BKE_image_release_ibuf using lock_r */
2080 BLI_lock_thread(LOCK_VIEWER);
2084 /* this gives active layer, composite or seqence result */
2085 rect= (unsigned int *)rres.rect32;
2088 dither= iuser->scene->r.dither_intensity;
2090 /* combined layer gets added as first layer */
2091 if(rres.have_combined && layer==0);
2092 else if(rres.layers.first) {
2093 RenderLayer *rl= BLI_findlink(&rres.layers, layer-(rres.have_combined?1:0));
2097 /* there's no combined pass, is in renderlayer itself */
2102 rpass= BLI_findlink(&rl->passes, pass-1);
2104 channels= rpass->channels;
2106 dither= 0.0f; /* don't dither passes */
2110 for(rpass= rl->passes.first; rpass; rpass= rpass->next)
2111 if(rpass->passtype == SCE_PASS_Z)
2116 ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
2118 /* make ibuf if needed, and initialize it */
2120 ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, 0);
2121 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
2124 ibuf->x= rres.rectx;
2125 ibuf->y= rres.recty;
2127 if(ibuf->rect_float!=rectf || rect) /* ensure correct redraw */
2128 imb_freerectImBuf(ibuf);
2134 ibuf->rect_float= rectf;
2135 ibuf->flags |= IB_rectfloat;
2136 ibuf->channels= channels;
2139 ibuf->rect_float= NULL;
2140 ibuf->flags &= ~IB_rectfloat;
2144 ibuf->zbuf_float= rectz;
2145 ibuf->flags |= IB_zbuffloat;
2148 ibuf->zbuf_float= NULL;
2149 ibuf->flags &= ~IB_zbuffloat;
2152 /* since its possible to access the buffer from the image directly, set the profile [#25073] */
2153 ibuf->profile= (iuser->scene->r.color_mgt_flag & R_COLOR_MANAGEMENT) ? IB_PROFILE_LINEAR_RGB : IB_PROFILE_NONE;
2155 ibuf->dither= dither;
2157 ima->ok= IMA_OK_LOADED;
2162 static ImBuf *image_get_ibuf_threadsafe(Image *ima, ImageUser *iuser, int *frame_r, int *index_r)
2165 int frame = 0, index = 0;
2167 /* see if we already have an appropriate ibuf, with image source and type */
2168 if(ima->source==IMA_SRC_MOVIE) {
2169 frame= iuser?iuser->framenr:ima->lastframe;
2170 ibuf= image_get_ibuf(ima, 0, frame);
2171 /* XXX temp stuff? */
2172 if(ima->lastframe != frame)
2173 ima->tpageflag |= IMA_TPAGE_REFRESH;
2174 ima->lastframe = frame;
2176 else if(ima->source==IMA_SRC_SEQUENCE) {
2177 if(ima->type==IMA_TYPE_IMAGE) {
2178 frame= iuser?iuser->framenr:ima->lastframe;
2179 ibuf= image_get_ibuf(ima, 0, frame);
2181 /* XXX temp stuff? */
2182 if(ima->lastframe != frame) {
2183 ima->tpageflag |= IMA_TPAGE_REFRESH;
2185 ima->lastframe = frame;
2187 else if(ima->type==IMA_TYPE_MULTILAYER) {
2188 frame= iuser?iuser->framenr:ima->lastframe;
2189 index= iuser?iuser->multi_index:IMA_NO_INDEX;
2190 ibuf= image_get_ibuf(ima, index, frame);
2193 else if(ima->source==IMA_SRC_FILE) {
2194 if(ima->type==IMA_TYPE_IMAGE)
2195 ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
2196 else if(ima->type==IMA_TYPE_MULTILAYER)
2197 ibuf= image_get_ibuf(ima, iuser?iuser->multi_index:IMA_NO_INDEX, 0);
2199 else if(ima->source == IMA_SRC_GENERATED) {
2200 ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
2202 else if(ima->source == IMA_SRC_VIEWER) {
2203 /* always verify entirely, not that this shouldn't happen
2204 * as part of texture sampling in rendering anyway, so not
2205 * a big bottleneck */
2214 /* Checks optional ImageUser and verifies/creates ImBuf. */
2215 /* use this one if you want to get a render result in progress,
2216 * if not, use BKE_image_get_ibuf which doesn't require a release */
2217 ImBuf *BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **lock_r)
2220 float color[] = {0, 0, 0, 1};
2221 int frame= 0, index= 0;
2223 /* This function is intended to be thread-safe. It postpones the mutex lock
2224 * until it needs to load the image, if the image is already there it
2225 * should just get the pointer and return. The reason is that a lot of mutex
2226 * locks appears to be very slow on certain multicore macs, causing a render
2227 * with image textures to actually slow down as more threads are used.
2229 * Note that all the image loading functions should also make sure they do
2230 * things in a threadsafe way for image_get_ibuf_threadsafe to work correct.
2231 * That means, the last two steps must be, 1) add the ibuf to the list and
2232 * 2) set ima/iuser->ok to 0 to IMA_OK_LOADED */
2237 /* quick reject tests */
2247 /* try to get the ibuf without locking */
2248 ibuf= image_get_ibuf_threadsafe(ima, iuser, &frame, &index);
2251 /* couldn't get ibuf and image is not ok, so let's lock and try to
2253 BLI_lock_thread(LOCK_IMAGE);
2255 /* need to check ok flag and loading ibuf again, because the situation
2256 * might have changed in the meantime */
2259 BLI_unlock_thread(LOCK_IMAGE);
2263 else if(ima->ok==0) {
2264 BLI_unlock_thread(LOCK_IMAGE);
2268 ibuf= image_get_ibuf_threadsafe(ima, iuser, &frame, &index);
2271 /* we are sure we have to load the ibuf, using source and type */
2272 if(ima->source==IMA_SRC_MOVIE) {
2273 /* source is from single file, use flipbook to store ibuf */
2274 ibuf= image_load_movie_file(ima, iuser, frame);
2276 else if(ima->source==IMA_SRC_SEQUENCE) {
2277 if(ima->type==IMA_TYPE_IMAGE) {
2278 /* regular files, ibufs in flipbook, allows saving */
2279 ibuf= image_load_sequence_file(ima, iuser, frame);
2281 /* no else; on load the ima type can change */
2282 if(ima->type==IMA_TYPE_MULTILAYER) {
2283 /* only 1 layer/pass stored in imbufs, no exrhandle anim storage, no saving */
2284 ibuf= image_load_sequence_multilayer(ima, iuser, frame);
2287 else if(ima->source==IMA_SRC_FILE) {
2289 if(ima->type==IMA_TYPE_IMAGE)
2290 ibuf= image_load_image_file(ima, iuser, frame); /* cfra only for '#', this global is OK */
2291 /* no else; on load the ima type can change */
2292 if(ima->type==IMA_TYPE_MULTILAYER)
2293 /* keeps render result, stores ibufs in listbase, allows saving */
2294 ibuf= image_get_ibuf_multilayer(ima, iuser);
2297 else if(ima->source == IMA_SRC_GENERATED) {
2298 /* generated is: ibuf is allocated dynamically */
2299 /* UV testgrid or black or solid etc */
2300 if(ima->gen_x==0) ima->gen_x= 1024;
2301 if(ima->gen_y==0) ima->gen_y= 1024;
2302 ibuf= add_ibuf_size(ima->gen_x, ima->gen_y, ima->name, 24, (ima->gen_flag & IMA_GEN_FLOAT) != 0, ima->gen_type, color);
2303 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
2304 ima->ok= IMA_OK_LOADED;
2306 else if(ima->source == IMA_SRC_VIEWER) {
2307 if(ima->type==IMA_TYPE_R_RESULT) {
2308 /* always verify entirely, and potentially
2309 returns pointer to release later */
2310 ibuf= image_get_render_result(ima, iuser, lock_r);
2312 else if(ima->type==IMA_TYPE_COMPOSITE) {
2313 /* requires lock/unlock, otherwise don't return image */
2315 /* unlock in BKE_image_release_ibuf */
2316 BLI_lock_thread(LOCK_VIEWER);
2319 /* XXX anim play for viewer nodes not yet supported */
2320 frame= 0; // XXX iuser?iuser->framenr:0;
2321 ibuf= image_get_ibuf(ima, 0, frame);
2324 /* Composite Viewer, all handled in compositor */
2325 /* fake ibuf, will be filled in compositor */
2326 ibuf= IMB_allocImBuf(256, 256, 32, IB_rect);
2327 image_assign_ibuf(ima, ibuf, 0, frame);
2334 BLI_unlock_thread(LOCK_IMAGE);
2337 tag_image_time(ima);
2342 void BKE_image_release_ibuf(Image *ima, void *lock)
2344 /* for getting image during threaded render / compositing, need to release */
2346 BLI_unlock_thread(LOCK_VIEWER); /* viewer image */
2349 RE_ReleaseResultImage(lock); /* render result */
2350 BLI_unlock_thread(LOCK_VIEWER); /* view image imbuf */
2354 /* warning, this can allocate generated images */
2355 ImBuf *BKE_image_get_ibuf(Image *ima, ImageUser *iuser)
2357 /* here (+fie_ima/2-1) makes sure that division happens correctly */
2358 return BKE_image_acquire_ibuf(ima, iuser, NULL);
2361 int BKE_image_user_get_frame(const ImageUser *iuser, int cfra, int fieldnr)
2363 const int len= (iuser->fie_ima*iuser->frames)/2;
2370 cfra= cfra - iuser->sfra+1;
2374 cfra= ( (cfra) % len );
2375 if(cfra < 0) cfra+= len;
2376 if(cfra==0) cfra= len;
2380 else if(cfra>len) cfra= len;
2382 /* convert current frame to current field */
2386 /* transform to images space */
2387 framenr= (cfra+iuser->fie_ima-2)/iuser->fie_ima;
2388 if(framenr>iuser->frames) framenr= iuser->frames;
2389 framenr+= iuser->offset;
2392 framenr= ( (framenr) % len );
2393 while(framenr < 0) framenr+= len;
2394 if(framenr==0) framenr= len;
2401 void BKE_image_user_calc_frame(ImageUser *iuser, int cfra, int fieldnr)
2403 const int framenr= BKE_image_user_get_frame(iuser, cfra, fieldnr);
2405 /* allows image users to handle redraws */
2406 if(iuser->flag & IMA_ANIM_ALWAYS)
2407 if(framenr!=iuser->framenr)
2408 iuser->flag |= IMA_ANIM_REFRESHED;
2410 iuser->framenr= framenr;
2411 if(iuser->ok==0) iuser->ok= 1;
2414 int BKE_image_has_alpha(struct Image *image)
2420 ibuf= BKE_image_acquire_ibuf(image, NULL, &lock);
2421 depth = (ibuf?ibuf->depth:0);
2422 BKE_image_release_ibuf(image, lock);