5 * ***** BEGIN GPL LICENSE BLOCK *****
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
22 * All rights reserved.
24 * Contributor(s): Blender Foundation, 2006, full recode
26 * ***** END GPL LICENSE BLOCK *****
46 #include "MEM_guardedalloc.h"
48 #include "IMB_imbuf_types.h"
49 #include "IMB_imbuf.h"
52 #include "intern/openexr/openexr_multi.h"
55 #include "DNA_packedFile_types.h"
56 #include "DNA_scene_types.h"
57 #include "DNA_camera_types.h"
58 #include "DNA_sequence_types.h"
59 #include "DNA_userdef_types.h"
61 #include "BLI_blenlib.h"
62 #include "BLI_threads.h"
64 #include "BKE_bmfont.h"
65 #include "BKE_global.h"
66 #include "BKE_icons.h"
67 #include "BKE_image.h"
68 #include "BKE_library.h"
70 #include "BKE_packedFile.h"
71 #include "BKE_scene.h"
73 //XXX #include "BIF_editseq.h"
79 #include "RE_pipeline.h"
83 #include "BLO_sys_types.h" // for intptr_t support
85 /* max int, to indicate we don't store sequences in ibuf */
86 #define IMA_NO_INDEX 0x7FEFEFEF
88 /* quick lookup: supports 1 million frames, thousand passes */
89 #define IMA_MAKE_INDEX(frame, index) ((frame)<<10)+index
90 #define IMA_INDEX_FRAME(index) (index>>10)
91 #define IMA_INDEX_PASS(index) (index & ~1023)
93 /* ******** IMAGE PROCESSING ************* */
95 static void de_interlace_ng(struct ImBuf *ibuf) /* neogeo fields */
97 struct ImBuf * tbuf1, * tbuf2;
99 if (ibuf == 0) return;
100 if (ibuf->flags & IB_fields) return;
101 ibuf->flags |= IB_fields;
105 tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect, (unsigned char)0);
106 tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, (int)IB_rect, (unsigned char)0);
110 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
111 IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
114 IMB_rectcpy(ibuf, tbuf1, 0, 0, 0, 0, tbuf1->x, tbuf1->y);
115 IMB_rectcpy(ibuf, tbuf2, 0, tbuf2->y, 0, 0, tbuf2->x, tbuf2->y);
117 IMB_freeImBuf(tbuf1);
118 IMB_freeImBuf(tbuf2);
123 static void de_interlace_st(struct ImBuf *ibuf) /* standard fields */
125 struct ImBuf * tbuf1, * tbuf2;
127 if (ibuf == 0) return;
128 if (ibuf->flags & IB_fields) return;
129 ibuf->flags |= IB_fields;
133 tbuf1 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect, 0);
134 tbuf2 = IMB_allocImBuf(ibuf->x, (short)(ibuf->y >> 1), (unsigned char)32, IB_rect, 0);
138 IMB_rectcpy(tbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
139 IMB_rectcpy(tbuf2, ibuf, 0, 0, tbuf2->x, 0, ibuf->x, ibuf->y);
142 IMB_rectcpy(ibuf, tbuf2, 0, 0, 0, 0, tbuf2->x, tbuf2->y);
143 IMB_rectcpy(ibuf, tbuf1, 0, tbuf2->y, 0, 0, tbuf1->x, tbuf1->y);
145 IMB_freeImBuf(tbuf1);
146 IMB_freeImBuf(tbuf2);
151 void image_de_interlace(Image *ima, int odd)
153 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
156 de_interlace_st(ibuf);
158 de_interlace_ng(ibuf);
162 /* ***************** ALLOC & FREE, DATA MANAGING *************** */
164 static void image_free_buffers(Image *ima)
168 while((ibuf = ima->ibufs.first)) {
169 BLI_remlink(&ima->ibufs, ibuf);
171 if (ibuf->userdata) {
172 MEM_freeN(ibuf->userdata);
173 ibuf->userdata = NULL;
178 if(ima->anim) IMB_free_anim(ima->anim);
182 RE_FreeRenderResult(ima->rr);
191 /* called by library too, do not free ima itself */
192 void free_image(Image *ima)
196 image_free_buffers(ima);
197 if (ima->packedfile) {
198 freePackedFile(ima->packedfile);
199 ima->packedfile = NULL;
201 BKE_icon_delete(&ima->id);
204 BKE_previewimg_free(&ima->preview);
206 for(a=0; a<IMA_MAX_RENDER_SLOT; a++) {
207 if(ima->renders[a]) {
208 RE_FreeRenderResult(ima->renders[a]);
209 ima->renders[a]= NULL;
214 /* only image block itself */
215 static Image *image_alloc(const char *name, short source, short type)
219 ima= alloc_libblock(&G.main->image, ID_IM, name);
223 ima->xrep= ima->yrep= 1;
224 ima->aspx= ima->aspy= 1.0;
225 ima->gen_x= 1024; ima->gen_y= 1024;
226 ima->gen_type= 1; /* no defines yet? */
234 /* get the ibuf from an image cache, local use here only */
235 static ImBuf *image_get_ibuf(Image *ima, int index, int frame)
237 /* this function is intended to be thread safe. with IMA_NO_INDEX this
238 * should be OK, but when iterating over the list this is more tricky
240 if(index==IMA_NO_INDEX)
241 return ima->ibufs.first;
245 index= IMA_MAKE_INDEX(frame, index);
246 for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next)
247 if(ibuf->index==index)
254 /* no ima->ibuf anymore, but listbase */
255 static void image_remove_ibuf(Image *ima, ImBuf *ibuf)
258 BLI_remlink(&ima->ibufs, ibuf);
264 /* no ima->ibuf anymore, but listbase */
265 static void image_assign_ibuf(Image *ima, ImBuf *ibuf, int index, int frame)
270 if(index!=IMA_NO_INDEX)
271 index= IMA_MAKE_INDEX(frame, index);
273 /* insert based on index */
274 for(link= ima->ibufs.first; link; link= link->next)
275 if(link->index>=index)
280 /* this function accepts link==NULL */
281 BLI_insertlinkbefore(&ima->ibufs, link, ibuf);
283 /* now we don't want copies? */
284 if(link && ibuf->index==link->index)
285 image_remove_ibuf(ima, link);
289 /* empty image block, of similar type and filename */
290 Image *copy_image(Image *ima)
292 Image *nima= image_alloc(ima->id.name+2, ima->source, ima->type);
294 BLI_strncpy(nima->name, ima->name, sizeof(ima->name));
296 nima->flag= ima->flag;
297 nima->tpageflag= ima->tpageflag;
299 nima->gen_x= ima->gen_x;
300 nima->gen_y= ima->gen_y;
301 nima->gen_type= ima->gen_type;
303 nima->animspeed= ima->animspeed;
305 nima->aspx= ima->aspx;
306 nima->aspy= ima->aspy;
311 void BKE_image_merge(Image *dest, Image *source)
316 if(dest && source && dest!=source) {
318 while((ibuf= source->ibufs.first)) {
319 BLI_remlink(&source->ibufs, ibuf);
320 image_assign_ibuf(dest, ibuf, IMA_INDEX_PASS(ibuf->index), IMA_INDEX_FRAME(ibuf->index));
323 free_libblock(&G.main->image, source);
328 /* checks if image was already loaded, then returns same image */
329 /* otherwise creates new. */
330 /* does not load ibuf itself */
331 /* pass on optional frame for #name images */
332 Image *BKE_add_image_file(const char *name, int frame)
337 char str[FILE_MAX], strtest[FILE_MAX];
339 BLI_strncpy(str, name, sizeof(str));
340 BLI_path_abs(str, G.sce);
343 file= open(str, O_BINARY|O_RDONLY);
344 if(file== -1) return NULL;
347 /* first search an identical image */
348 for(ima= G.main->image.first; ima; ima= ima->id.next) {
349 if(ima->source!=IMA_SRC_VIEWER && ima->source!=IMA_SRC_GENERATED) {
350 BLI_strncpy(strtest, ima->name, sizeof(ima->name));
351 BLI_path_abs(strtest, G.sce);
353 if( strcmp(strtest, str)==0 ) {
354 if(ima->anim==NULL || ima->id.us==0) {
355 BLI_strncpy(ima->name, name, sizeof(ima->name)); /* for stringcode */
356 ima->id.us++; /* officially should not, it doesn't link here! */
367 /* create a short library name */
370 while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\') len--;
373 ima= image_alloc(libname, IMA_SRC_FILE, IMA_TYPE_IMAGE);
374 BLI_strncpy(ima->name, name, sizeof(ima->name));
376 /* do a wild guess! */
377 if(BLI_testextensie(name, ".avi") || BLI_testextensie(name, ".mov")
378 || BLI_testextensie(name, ".mpg") || BLI_testextensie(name, ".mp4"))
379 ima->source= IMA_SRC_MOVIE;
384 static ImBuf *add_ibuf_size(int width, int height, char *name, int depth, int floatbuf, short uvtestgrid, float color[4])
387 unsigned char *rect= NULL;
388 float *rect_float= NULL;
391 ibuf= IMB_allocImBuf(width, height, depth, IB_rectfloat, 0);
392 rect_float= (float*)ibuf->rect_float;
395 ibuf= IMB_allocImBuf(width, height, depth, IB_rect, 0);
396 rect= (unsigned char*)ibuf->rect;
399 strcpy(ibuf->name, "//Untitled");
400 ibuf->userflags |= IB_BITMAPDIRTY;
404 BKE_image_buf_fill_checker(rect, rect_float, width, height);
407 BKE_image_buf_fill_checker_color(rect, rect_float, width, height);
410 BKE_image_buf_fill_color(rect, rect_float, width, height, color);
416 /* adds new image block, creates ImBuf and initializes color */
417 Image *BKE_add_image_size(int width, int height, char *name, int depth, int floatbuf, short uvtestgrid, float color[4])
419 /* on save, type is changed to FILE in editsima.c */
420 Image *ima= image_alloc(name, IMA_SRC_GENERATED, IMA_TYPE_UV_TEST);
425 BLI_strncpy(ima->name, name, FILE_MAX);
428 ima->gen_type= uvtestgrid;
430 ibuf= add_ibuf_size(width, height, name, depth, floatbuf, uvtestgrid, color);
431 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
433 ima->ok= IMA_OK_LOADED;
439 /* creates an image image owns the imbuf passed */
440 Image *BKE_add_image_imbuf(ImBuf *ibuf)
442 /* on save, type is changed to FILE in editsima.c */
445 ima= image_alloc(BLI_path_basename(ibuf->name), IMA_SRC_FILE, IMA_TYPE_IMAGE);
448 BLI_strncpy(ima->name, ibuf->name, FILE_MAX);
449 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
450 ima->ok= IMA_OK_LOADED;
456 /* packs rect from memory as PNG */
457 void BKE_image_memorypack(Image *ima)
459 ImBuf *ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
463 if (ima->packedfile) {
464 freePackedFile(ima->packedfile);
465 ima->packedfile = NULL;
471 IMB_saveiff(ibuf, ibuf->name, IB_rect | IB_mem);
472 if(ibuf->encodedbuffer==NULL) {
473 printf("memory save for pack error\n");
476 PackedFile *pf = MEM_callocN(sizeof(*pf), "PackedFile");
478 pf->data = ibuf->encodedbuffer;
479 pf->size = ibuf->encodedsize;
481 ibuf->encodedbuffer= NULL;
482 ibuf->encodedsize= 0;
483 ibuf->userflags &= ~IB_BITMAPDIRTY;
485 if(ima->source==IMA_SRC_GENERATED) {
486 ima->source= IMA_SRC_FILE;
487 ima->type= IMA_TYPE_IMAGE;
492 void tag_image_time(Image *ima)
495 ima->lastused = (int)PIL_check_seconds_timer();
499 static void tag_all_images_time()
502 int ctime = (int)PIL_check_seconds_timer();
504 ima= G.main->image.first;
506 if(ima->bindcode || ima->repbind || ima->ibufs.first) {
507 ima->lastused = ctime;
513 void free_old_images()
516 static int lasttime = 0;
517 int ctime = (int)PIL_check_seconds_timer();
520 Run garbage collector once for every collecting period of time
521 if textimeout is 0, that's the option to NOT run the collector
523 if (U.textimeout == 0 || ctime % U.texcollectrate || ctime == lasttime)
528 ima= G.main->image.first;
530 if((ima->flag & IMA_NOCOLLECT)==0 && ctime - ima->lastused > U.textimeout) {
532 If it's in GL memory, deallocate and set time tag to current time
533 This gives textures a "second chance" to be used before dying.
535 if(ima->bindcode || ima->repbind) {
537 ima->lastused = ctime;
539 /* Otherwise, just kill the buffers */
540 else if (ima->ibufs.first) {
541 image_free_buffers(ima);
548 static uintptr_t image_mem_size(Image *ima)
556 /* viewers have memory depending on other rules, has no valid rect pointer */
557 if(ima->source==IMA_SRC_VIEWER)
560 for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next) {
561 if(ibuf->rect) size += MEM_allocN_len(ibuf->rect);
562 else if(ibuf->rect_float) size += MEM_allocN_len(ibuf->rect_float);
564 for(level=0; level<IB_MIPMAP_LEVELS; level++) {
565 ibufm= ibuf->mipmap[level];
567 if(ibufm->rect) size += MEM_allocN_len(ibufm->rect);
568 else if(ibufm->rect_float) size += MEM_allocN_len(ibufm->rect_float);
576 void BKE_image_print_memlist(void)
579 uintptr_t size, totsize= 0;
581 for(ima= G.main->image.first; ima; ima= ima->id.next)
582 totsize += image_mem_size(ima);
584 printf("\ntotal image memory len: %.3lf MB\n", (double)totsize/(double)(1024*1024));
586 for(ima= G.main->image.first; ima; ima= ima->id.next) {
587 size= image_mem_size(ima);
590 printf("%s len: %.3f MB\n", ima->id.name+2, (double)size/(double)(1024*1024));
594 void BKE_image_free_all_textures(void)
598 unsigned int totsize= 0;
600 for(ima= G.main->image.first; ima; ima= ima->id.next)
601 ima->id.flag &= ~LIB_DOIT;
603 for(tex= G.main->tex.first; tex; tex= tex->id.next)
605 tex->ima->id.flag |= LIB_DOIT;
607 for(ima= G.main->image.first; ima; ima= ima->id.next) {
608 if(ima->ibufs.first && (ima->id.flag & LIB_DOIT)) {
610 for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next) {
612 totsize+= 1.33*ibuf->x*ibuf->y*4;
614 totsize+= ibuf->x*ibuf->y*4;
616 image_free_buffers(ima);
619 /* printf("freed total %d MB\n", totsize/(1024*1024)); */
622 /* except_frame is weak, only works for seqs without offset... */
623 void BKE_image_free_anim_ibufs(Image *ima, int except_frame)
627 for(ibuf= ima->ibufs.first; ibuf; ibuf= nbuf) {
629 if(ibuf->userflags & IB_BITMAPDIRTY)
631 if(ibuf->index==IMA_NO_INDEX)
633 if(except_frame!=IMA_INDEX_FRAME(ibuf->index)) {
634 BLI_remlink(&ima->ibufs, ibuf);
636 if (ibuf->userdata) {
637 MEM_freeN(ibuf->userdata);
638 ibuf->userdata = NULL;
645 void BKE_image_all_free_anim_ibufs(int cfra)
649 for(ima= G.main->image.first; ima; ima= ima->id.next)
650 if(ELEM(ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE))
651 BKE_image_free_anim_ibufs(ima, cfra);
655 /* *********** READ AND WRITE ************** */
657 int BKE_imtype_to_ftype(int imtype)
661 else if(imtype== R_IRIS)
663 else if (imtype==R_RADHDR)
665 else if (imtype==R_PNG)
668 else if (imtype==R_DDS)
671 else if (imtype==R_BMP)
673 else if (imtype==R_TIFF)
675 else if (imtype==R_OPENEXR || imtype==R_MULTILAYER)
677 else if (imtype==R_CINEON)
679 else if (imtype==R_DPX)
681 else if (imtype==R_TARGA)
683 else if(imtype==R_RAWTGA)
686 else if(imtype==R_JP2)
693 int BKE_ftype_to_imtype(int ftype)
697 else if(ftype == IMAGIC)
699 else if (ftype & RADHDR)
701 else if (ftype & PNG)
704 else if (ftype & DDS)
707 else if (ftype & BMP)
709 else if (ftype & TIF)
711 else if (ftype & OPENEXR)
713 else if (ftype & CINEON)
715 else if (ftype & DPX)
717 else if (ftype & TGA)
719 else if(ftype & RAWTGA)
730 int BKE_imtype_is_movie(int imtype)
747 void BKE_add_image_extension(char *string, int imtype)
751 if(imtype== R_IRIS) {
752 if(!BLI_testextensie(string, ".rgb"))
755 else if(imtype==R_IRIZ) {
756 if(!BLI_testextensie(string, ".rgb"))
759 else if(imtype==R_RADHDR) {
760 if(!BLI_testextensie(string, ".hdr"))
763 else if (ELEM5(imtype, R_PNG, R_FFMPEG, R_H264, R_THEORA, R_XVID)) {
764 if(!BLI_testextensie(string, ".png"))
768 else if(imtype==R_DDS) {
769 if(!BLI_testextensie(string, ".dds"))
773 else if(imtype==R_RAWTGA) {
774 if(!BLI_testextensie(string, ".tga"))
777 else if(imtype==R_BMP) {
778 if(!BLI_testextensie(string, ".bmp"))
782 else if(imtype==R_TIFF) {
783 if(!BLI_testextensie(string, ".tif") &&
784 !BLI_testextensie(string, ".tiff")) extension= ".tif";
788 else if( ELEM(imtype, R_OPENEXR, R_MULTILAYER)) {
789 if(!BLI_testextensie(string, ".exr"))
793 else if(imtype==R_CINEON){
794 if (!BLI_testextensie(string, ".cin"))
797 else if(imtype==R_DPX){
798 if (!BLI_testextensie(string, ".dpx"))
801 else if(imtype==R_TARGA) {
802 if(!BLI_testextensie(string, ".tga"))
806 else if(imtype==R_JP2) {
807 if(!BLI_testextensie(string, ".jp2"))
811 else { // R_AVICODEC, R_AVIRAW, R_AVIJPEG, R_JPEG90, R_QUICKTIME etc
812 if(!( BLI_testextensie(string, ".jpg") || BLI_testextensie(string, ".jpeg")))
816 strcat(string, extension);
819 /* could allow access externally - 512 is for long names, 64 is for id names */
820 typedef struct StampData {
833 static void stampdata(Scene *scene, StampData *stamp_data, int do_prefix)
839 if (scene->r.stamp & R_STAMP_FILENAME) {
840 if (G.relbase_valid) {
841 if (do_prefix) sprintf(stamp_data->file, "File %s", G.sce);
842 else sprintf(stamp_data->file, "%s", G.sce);
844 if (do_prefix) strcpy(stamp_data->file, "File <untitled>");
845 else strcpy(stamp_data->file, "<untitled>");
848 stamp_data->file[0] = '\0';
851 if (scene->r.stamp & R_STAMP_NOTE) {
852 /* Never do prefix for Note */
853 sprintf(stamp_data->note, "%s", scene->r.stamp_udata);
855 stamp_data->note[0] = '\0';
858 if (scene->r.stamp & R_STAMP_DATE) {
862 sprintf (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);
864 if (do_prefix) sprintf(stamp_data->date, "Date %s", text);
865 else sprintf(stamp_data->date, "%s", text);
867 stamp_data->date[0] = '\0';
870 if (scene->r.stamp & R_STAMP_MARKER) {
871 char *name = scene_find_last_marker_name(scene, CFRA);
873 if (name) strcpy(text, name);
874 else strcpy(text, "<none>");
876 if (do_prefix) sprintf(stamp_data->marker, "Marker %s", text);
877 else sprintf(stamp_data->marker, "%s", text);
879 stamp_data->marker[0] = '\0';
882 if (scene->r.stamp & R_STAMP_TIME) {
885 f = (int)(scene->r.cfra % scene->r.frs_sec);
886 s = (int)(scene->r.cfra / scene->r.frs_sec);
898 if (scene->r.frs_sec < 100)
899 sprintf (text, "%02d:%02d:%02d.%02d", h, m, s, f);
901 sprintf (text, "%02d:%02d:%02d.%03d", h, m, s, f);
903 if (do_prefix) sprintf(stamp_data->time, "Time %s", text);
904 else sprintf(stamp_data->time, "%s", text);
906 stamp_data->time[0] = '\0';
909 if (scene->r.stamp & R_STAMP_FRAME) {
911 if (do_prefix) sprintf(format, "Frame %%0%di", 1 + (int) log10(scene->r.efra));
912 else sprintf(format, "%%0%di", 1 + (int) log10(scene->r.efra));
913 sprintf (stamp_data->frame, format, scene->r.cfra);
915 stamp_data->frame[0] = '\0';
918 if (scene->r.stamp & R_STAMP_CAMERA) {
919 if (scene->camera) strcpy(text, ((Camera *) scene->camera)->id.name+2);
920 else strcpy(text, "<none>");
922 if (do_prefix) sprintf(stamp_data->camera, "Camera %s", text);
923 else sprintf(stamp_data->camera, "%s", text);
925 stamp_data->camera[0] = '\0';
928 if (scene->r.stamp & R_STAMP_SCENE) {
929 if (do_prefix) sprintf(stamp_data->scene, "Scene %s", scene->id.name+2);
930 else sprintf(stamp_data->scene, "%s", scene->id.name+2);
932 stamp_data->scene[0] = '\0';
935 if (scene->r.stamp & R_STAMP_SEQSTRIP) {
936 Sequence *seq= NULL; //XXX = get_foreground_frame_seq(scene->r.cfra);
938 if (seq) strcpy(text, seq->name+2);
939 else strcpy(text, "<none>");
941 if (do_prefix) sprintf(stamp_data->strip, "Strip %s", text);
942 else sprintf(stamp_data->strip, "%s", text);
944 stamp_data->strip[0] = '\0';
948 Render *re= RE_GetRender(scene->id.name);
949 RenderStats *stats= re ? RE_GetStats(re):NULL;
951 if (stats && (scene->r.stamp & R_STAMP_RENDERTIME)) {
952 BLI_timestr(stats->lastframetime, text);
954 if (do_prefix) sprintf(stamp_data->rendertime, "RenderTime %s", text);
955 else sprintf(stamp_data->rendertime, "%s", text);
957 stamp_data->rendertime[0] = '\0';
962 // XXX - Bad level call.
963 extern int datatoc_bmonofont_ttf_size;
964 extern char datatoc_bmonofont_ttf[];
966 // XXX - copied from text_font_begin ! Change all the BLF_* here
969 int stamp_font_begin(int size)
972 mono= BLF_load_mem_unique("monospace", (unsigned char *)datatoc_bmonofont_ttf, datatoc_bmonofont_ttf_size);
974 BLF_aspect(mono, 1.0);
975 BLF_size(mono, size, 72);
976 return(mono); // XXX This is for image_gen.c!!
979 void BKE_stamp_buf(Scene *scene, unsigned char *rect, float *rectf, int width, int height, int channels)
981 struct StampData stamp_data;
989 stampdata(scene, &stamp_data, 1);
991 /* TODO, do_versions */
992 if(scene->r.stamp_font_id < 8)
993 scene->r.stamp_font_id= 12;
995 stamp_font_begin(scene->r.stamp_font_id);
997 BLF_buffer(mono, rectf, rect, width, height, channels);
998 BLF_buffer_col(mono, scene->r.fg_stamp[0], scene->r.fg_stamp[1], scene->r.fg_stamp[2], 1.0);
999 pad= BLF_width(mono, "--");
1001 /* use 'h_fixed' rather then 'h', aligns better */
1002 // BLF_width_and_height(mono, "^|/_AgPpJjlYy", &w, &h_fixed);
1005 BLF_boundbox(mono, "^|/_AgPpJjlYy", &box);
1006 h_fixed= box.ymax - box.ymin;
1012 if (stamp_data.file[0]) {
1013 /* Top left corner */
1014 BLF_width_and_height(mono, stamp_data.file, &w, &h); h= h_fixed;
1017 /* also a little of space to the background. */
1018 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y-3, w+3, y+h+2);
1020 /* and draw the text. */
1021 BLF_position(mono, x, y, 0.0);
1022 BLF_draw_buffer(mono, stamp_data.file);
1024 /* the extra pixel for background. */
1028 /* Top left corner, below File */
1029 if (stamp_data.note[0]) {
1030 BLF_width_and_height(mono, stamp_data.note, &w, &h); h= h_fixed;
1033 /* and space for background. */
1034 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, 0, y-3, w+3, y+h+2);
1036 BLF_position(mono, x, y+1, 0.0);
1037 BLF_draw_buffer(mono, stamp_data.note);
1039 /* the extra pixel for background. */
1043 /* Top left corner, below File (or Note) */
1044 if (stamp_data.date[0]) {
1045 BLF_width_and_height(mono, stamp_data.date, &w, &h); h= h_fixed;
1048 /* and space for background. */
1049 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, 0, y-3, w+3, y+h+2);
1051 BLF_position(mono, x, y, 0.0);
1052 BLF_draw_buffer(mono, stamp_data.date);
1054 /* the extra pixel for background. */
1058 /* Top left corner, below File, Date or Note */
1059 if (stamp_data.rendertime[0]) {
1060 BLF_width_and_height(mono, stamp_data.rendertime, &w, &h); h= h_fixed;
1063 /* and space for background. */
1064 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, 0, y-3, w+3, y+h+2);
1066 BLF_position(mono, x, y, 0.0);
1067 BLF_draw_buffer(mono, stamp_data.rendertime);
1073 /* Bottom left corner, leaving space for timing */
1074 if (stamp_data.marker[0]) {
1075 BLF_width_and_height(mono, stamp_data.marker, &w, &h); h= h_fixed;
1077 /* extra space for background. */
1078 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y, w+2, y+h+2);
1080 /* and pad the text. */
1081 BLF_position(mono, x, y+3, 0.0);
1082 BLF_draw_buffer(mono, stamp_data.marker);
1088 /* Left bottom corner */
1089 if (stamp_data.time[0]) {
1090 BLF_width_and_height(mono, stamp_data.time, &w, &h); h= h_fixed;
1092 /* extra space for background */
1093 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y, x+w+2, y+h+2);
1095 /* and pad the text. */
1096 BLF_position(mono, x, y+3, 0.0);
1097 BLF_draw_buffer(mono, stamp_data.time);
1103 if (stamp_data.frame[0]) {
1104 BLF_width_and_height(mono, stamp_data.frame, &w, &h); h= h_fixed;
1106 /* extra space for background. */
1107 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y, x+w+2, y+h+2);
1109 /* and pad the text. */
1110 BLF_position(mono, x, y+3, 0.0);
1111 BLF_draw_buffer(mono, stamp_data.frame);
1117 if (stamp_data.camera[0]) {
1118 BLF_width_and_height(mono, stamp_data.camera, &w, &h); h= h_fixed;
1120 /* extra space for background. */
1121 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y, x+w+2, y+h+2);
1122 BLF_position(mono, x, y+3, 0.0);
1123 BLF_draw_buffer(mono, stamp_data.camera);
1126 if (stamp_data.scene[0]) {
1127 BLF_width_and_height(mono, stamp_data.scene, &w, &h); h= h_fixed;
1129 /* Bottom right corner, with an extra space because blenfont is too strict! */
1132 /* extra space for background. */
1133 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y, x+w+3, y+h+2);
1135 /* and pad the text. */
1136 BLF_position(mono, x, y+3, 0.0);
1137 BLF_draw_buffer(mono, stamp_data.scene);
1140 if (stamp_data.strip[0]) {
1141 BLF_width_and_height(mono, stamp_data.scene, &w, &h); h= h_fixed;
1143 /* Top right corner, with an extra space because blenfont is too strict! */
1147 /* extra space for background. */
1148 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y-3, x+w+pad, y+h+2);
1150 BLF_position(mono, x, y, 0.0);
1151 BLF_draw_buffer(mono, stamp_data.strip);
1154 /* cleanup the buffer. */
1155 BLF_buffer(mono, NULL, NULL, 0, 0, 0);
1158 void BKE_stamp_info(Scene *scene, struct ImBuf *ibuf)
1160 struct StampData stamp_data;
1164 /* fill all the data values, no prefix */
1165 stampdata(scene, &stamp_data, 0);
1167 if (stamp_data.file[0]) IMB_metadata_change_field (ibuf, "File", stamp_data.file);
1168 if (stamp_data.note[0]) IMB_metadata_change_field (ibuf, "Note", stamp_data.note);
1169 if (stamp_data.date[0]) IMB_metadata_change_field (ibuf, "Date", stamp_data.date);
1170 if (stamp_data.marker[0]) IMB_metadata_change_field (ibuf, "Marker", stamp_data.marker);
1171 if (stamp_data.time[0]) IMB_metadata_change_field (ibuf, "Time", stamp_data.time);
1172 if (stamp_data.frame[0]) IMB_metadata_change_field (ibuf, "Frame", stamp_data.frame);
1173 if (stamp_data.camera[0]) IMB_metadata_change_field (ibuf, "Camera", stamp_data.camera);
1174 if (stamp_data.scene[0]) IMB_metadata_change_field (ibuf, "Scene", stamp_data.scene);
1175 if (stamp_data.strip[0]) IMB_metadata_change_field (ibuf, "Strip", stamp_data.strip);
1176 if (stamp_data.rendertime[0]) IMB_metadata_change_field (ibuf, "RenderTime", stamp_data.rendertime);
1179 int BKE_write_ibuf(Scene *scene, ImBuf *ibuf, char *name, int imtype, int subimtype, int quality)
1184 else if(imtype== R_IRIS)
1185 ibuf->ftype= IMAGIC;
1186 else if ((imtype==R_RADHDR)) {
1187 ibuf->ftype= RADHDR;
1189 else if (ELEM5(imtype, R_PNG, R_FFMPEG, R_H264, R_THEORA, R_XVID)) {
1193 ibuf->ftype |= quality; /* quality is actually compression 0-100 --> 0-9 */
1197 else if ((imtype==R_DDS)) {
1201 else if ((imtype==R_BMP)) {
1205 else if (imtype==R_TIFF) {
1208 if(subimtype & R_TIFF_16BIT)
1209 ibuf->ftype |= TIF_16BIT;
1213 else if (imtype==R_OPENEXR || imtype==R_MULTILAYER) {
1214 ibuf->ftype= OPENEXR;
1215 if(subimtype & R_OPENEXR_HALF)
1216 ibuf->ftype |= OPENEXR_HALF;
1217 ibuf->ftype |= (quality & OPENEXR_COMPRESS);
1219 if(!(subimtype & R_OPENEXR_ZBUF))
1220 ibuf->zbuf_float = NULL; /* signal for exr saving */
1224 else if (imtype==R_CINEON) {
1225 ibuf->ftype = CINEON;
1227 else if (imtype==R_DPX) {
1230 else if (imtype==R_TARGA) {
1233 else if(imtype==R_RAWTGA) {
1234 ibuf->ftype= RAWTGA;
1236 #ifdef WITH_OPENJPEG
1237 else if(imtype==R_JP2) {
1238 if(quality < 10) quality= 90;
1239 ibuf->ftype= JP2|quality;
1241 if (subimtype & R_JPEG2K_16BIT) {
1242 ibuf->ftype |= JP2_16BIT;
1243 } else if (subimtype & R_JPEG2K_12BIT) {
1244 ibuf->ftype |= JP2_12BIT;
1247 if (subimtype & R_JPEG2K_YCC) {
1248 ibuf->ftype |= JP2_YCC;
1251 if (subimtype & R_JPEG2K_CINE_PRESET) {
1252 ibuf->ftype |= JP2_CINE;
1253 if (subimtype & R_JPEG2K_CINE_48FPS)
1254 ibuf->ftype |= JP2_CINE_48FPS;
1259 /* R_JPEG90, etc. default we save jpegs */
1260 if(quality < 10) quality= 90;
1261 ibuf->ftype= JPG|quality;
1262 if(ibuf->depth==32) ibuf->depth= 24; /* unsupported feature only confuses other s/w */
1265 BLI_make_existing_file(name);
1267 if(scene && scene->r.stamp & R_STAMP_ALL)
1268 BKE_stamp_info(scene, ibuf);
1270 ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
1279 void BKE_makepicstring(char *string, char *base, int frame, int imtype, int use_ext)
1281 if (string==NULL) return;
1282 BLI_strncpy(string, base, FILE_MAX - 10); /* weak assumption */
1283 BLI_path_abs(string, G.sce);
1284 BLI_path_frame(string, frame, 4);
1287 BKE_add_image_extension(string, imtype);
1291 /* used by sequencer too */
1292 struct anim *openanim(char *name, int flags)
1297 anim = IMB_open_anim(name, flags);
1298 if (anim == NULL) return(0);
1300 ibuf = IMB_anim_absolute(anim, 0);
1302 if(BLI_exists(name))
1303 printf("not an anim: %s\n", name);
1305 printf("anim file doesn't exist: %s\n", name);
1306 IMB_free_anim(anim);
1309 IMB_freeImBuf(ibuf);
1314 /* ************************* New Image API *************** */
1317 /* Notes about Image storage
1319 -> written in .blend
1321 -> written in .blend
1323 -> comes from packedfile or filename
1325 -> comes from packedfile or filename
1327 -> ibufs from exrhandle
1329 -> ibufs come from movie, temporary renderresult or sequence
1331 -> comes from packedfile or filename or generated
1336 /* forces existence of 1 Image for renderout or nodes, returns Image */
1337 /* name is only for default, when making new one */
1338 Image *BKE_image_verify_viewer(int type, const char *name)
1342 for(ima=G.main->image.first; ima; ima= ima->id.next)
1343 if(ima->source==IMA_SRC_VIEWER)
1348 ima= image_alloc(name, IMA_SRC_VIEWER, type);
1350 /* happens on reload, imagewindow cannot be image user when hidden*/
1352 id_us_plus(&ima->id);
1357 void BKE_image_assign_ibuf(Image *ima, ImBuf *ibuf)
1359 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
1362 void BKE_image_signal(Image *ima, ImageUser *iuser, int signal)
1368 case IMA_SIGNAL_FREE:
1369 image_free_buffers(ima);
1373 case IMA_SIGNAL_SRC_CHANGE:
1374 if(ima->type == IMA_TYPE_UV_TEST)
1375 if(ima->source != IMA_SRC_GENERATED)
1376 ima->type= IMA_TYPE_IMAGE;
1378 if(ima->source==IMA_SRC_GENERATED) {
1379 if(ima->gen_x==0 || ima->gen_y==0) {
1380 ImBuf *ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
1382 ima->gen_x= ibuf->x;
1383 ima->gen_y= ibuf->y;
1388 image_free_buffers(ima);
1395 case IMA_SIGNAL_RELOAD:
1396 /* try to repack file */
1397 if(ima->packedfile) {
1399 pf = newPackedFile(NULL, ima->name);
1401 freePackedFile(ima->packedfile);
1402 ima->packedfile = pf;
1403 image_free_buffers(ima);
1405 printf("ERROR: Image not available. Keeping packed image\n");
1409 image_free_buffers(ima);
1415 case IMA_SIGNAL_USER_NEW_IMAGE:
1418 if(ima->source==IMA_SRC_FILE || ima->source==IMA_SRC_SEQUENCE) {
1419 if(ima->type==IMA_TYPE_MULTILAYER) {
1420 iuser->multi_index= 0;
1421 iuser->layer= iuser->pass= 0;
1429 /* if layer or pass changes, we need an index for the imbufs list */
1430 /* note it is called for rendered results, but it doesnt use the index! */
1431 /* and because rendered results use fake layer/passes, don't correct for wrong indices here */
1432 RenderPass *BKE_image_multilayer_index(RenderResult *rr, ImageUser *iuser)
1435 RenderPass *rpass= NULL;
1441 short index= 0, rl_index= 0, rp_index;
1443 for(rl= rr->layers.first; rl; rl= rl->next, rl_index++) {
1445 for(rpass= rl->passes.first; rpass; rpass= rpass->next, index++, rp_index++)
1446 if(iuser->layer==rl_index && iuser->pass==rp_index)
1453 iuser->multi_index= index;
1455 iuser->multi_index= 0;
1458 rl= rr->layers.first;
1460 rpass= rl->passes.first;
1466 RenderResult *BKE_image_acquire_renderresult(Scene *scene, Image *ima)
1471 else if(ima->type==IMA_TYPE_R_RESULT) {
1472 if(ima->render_slot == ima->last_render_slot)
1473 return RE_AcquireResultRead(RE_GetRender(scene->id.name));
1475 return ima->renders[ima->render_slot];
1481 void BKE_image_release_renderresult(Scene *scene, Image *ima)
1484 else if(ima->type==IMA_TYPE_R_RESULT) {
1485 if(ima->render_slot == ima->last_render_slot)
1486 RE_ReleaseResult(RE_GetRender(scene->id.name));
1490 void BKE_image_backup_render(Scene *scene, Image *ima)
1492 /* called right before rendering, ima->renders contains render
1493 result pointers for everything but the current render */
1494 Render *re= RE_GetRender(scene->id.name);
1495 int slot= ima->render_slot, last= ima->last_render_slot;
1498 if(ima->renders[slot]) {
1499 RE_FreeRenderResult(ima->renders[slot]);
1500 ima->renders[slot]= NULL;
1503 ima->renders[last]= NULL;
1504 RE_SwapResult(re, &ima->renders[last]);
1507 ima->last_render_slot= slot;
1510 /* after imbuf load, openexr type can return with a exrhandle open */
1511 /* in that case we have to build a render-result */
1512 static void image_create_multilayer(Image *ima, ImBuf *ibuf, int framenr)
1515 ima->rr= RE_MultilayerConvert(ibuf->userdata, ibuf->x, ibuf->y);
1518 IMB_exr_close(ibuf->userdata);
1521 ibuf->userdata= NULL;
1523 ima->rr->framenr= framenr;
1526 /* common stuff to do with images after loading */
1527 static void image_initialize_after_load(Image *ima, ImBuf *ibuf)
1531 /* preview is NULL when it has never been used as an icon before */
1532 if(G.background==0 && ima->preview==NULL)
1533 BKE_icon_changed(BKE_icon_getid(&ima->id));
1535 /* stringcodes also in ibuf, ibuf->name is used to retrieve original (buttons) */
1536 BLI_strncpy(ibuf->name, ima->name, FILE_MAX);
1539 if (ima->flag & IMA_FIELDS) {
1540 if(ima->flag & IMA_STD_FIELD) de_interlace_st(ibuf);
1541 else de_interlace_ng(ibuf);
1544 ima->lastused = clock() / CLOCKS_PER_SEC;
1546 ima->ok= IMA_OK_LOADED;
1550 static ImBuf *image_load_sequence_file(Image *ima, ImageUser *iuser, int frame)
1553 unsigned short numlen;
1554 char name[FILE_MAX], head[FILE_MAX], tail[FILE_MAX];
1557 /* XXX temp stuff? */
1558 if(ima->lastframe != frame)
1559 ima->tpageflag |= IMA_TPAGE_REFRESH;
1561 ima->lastframe= frame;
1563 BLI_stringdec(ima->name, head, tail, &numlen);
1564 BLI_stringenc(ima->name, head, tail, numlen, frame);
1565 BLI_strncpy(name, ima->name, sizeof(name));
1568 BLI_path_abs(name, ima->id.lib->filepath);
1570 BLI_path_abs(name, G.sce);
1572 flag= IB_rect|IB_multilayer;
1573 if(ima->flag & IMA_DO_PREMUL)
1577 ibuf = IMB_loadiffname(name, flag);
1578 if(G.f & G_DEBUG) printf("loaded %s\n", name);
1582 /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_get_ibuf */
1583 if (ibuf->ftype==OPENEXR && ibuf->userdata) {
1584 image_create_multilayer(ima, ibuf, frame);
1585 ima->type= IMA_TYPE_MULTILAYER;
1586 IMB_freeImBuf(ibuf);
1590 image_initialize_after_load(ima, ibuf);
1591 image_assign_ibuf(ima, ibuf, 0, frame);
1594 image_initialize_after_load(ima, ibuf);
1595 image_assign_ibuf(ima, ibuf, 0, frame);
1607 static ImBuf *image_load_sequence_multilayer(Image *ima, ImageUser *iuser, int frame)
1609 struct ImBuf *ibuf= NULL;
1611 /* either we load from RenderResult, or we have to load a new one */
1613 /* check for new RenderResult */
1614 if(ima->rr==NULL || frame!=ima->rr->framenr) {
1615 /* copy to survive not found multilayer image */
1616 RenderResult *oldrr= ima->rr;
1619 ibuf = image_load_sequence_file(ima, iuser, frame);
1621 if(ibuf) { /* actually an error */
1622 ima->type= IMA_TYPE_IMAGE;
1623 printf("error, multi is normal image\n");
1625 // printf("loaded new result %p\n", ima->rr);
1626 /* free result if new one found */
1628 // if(oldrr) printf("freed previous result %p\n", oldrr);
1629 if(oldrr) RE_FreeRenderResult(oldrr);
1637 RenderPass *rpass= BKE_image_multilayer_index(ima->rr, iuser);
1640 // printf("load from pass %s\n", rpass->name);
1641 /* since we free render results, we copy the rect */
1642 ibuf= IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0, 0);
1643 ibuf->rect_float= MEM_dupallocN(rpass->rect);
1644 ibuf->flags |= IB_rectfloat;
1645 ibuf->mall= IB_rectfloat;
1646 ibuf->channels= rpass->channels;
1648 image_initialize_after_load(ima, ibuf);
1649 image_assign_ibuf(ima, ibuf, iuser?iuser->multi_index:0, frame);
1652 // else printf("pass not found\n");
1664 static ImBuf *image_load_movie_file(Image *ima, ImageUser *iuser, int frame)
1666 struct ImBuf *ibuf= NULL;
1668 ima->lastframe= frame;
1670 if(ima->anim==NULL) {
1673 BLI_strncpy(str, ima->name, FILE_MAX);
1675 BLI_path_abs(str, ima->id.lib->filepath);
1677 BLI_path_abs(str, G.sce);
1679 ima->anim = openanim(str, IB_rect);
1681 /* let's initialize this user */
1682 if(ima->anim && iuser && iuser->frames==0)
1683 iuser->frames= IMB_anim_get_duration(ima->anim);
1687 int dur = IMB_anim_get_duration(ima->anim);
1691 if(fra>(dur-1)) fra= dur-1;
1692 ibuf = IMB_anim_absolute(ima->anim, fra);
1695 image_initialize_after_load(ima, ibuf);
1696 image_assign_ibuf(ima, ibuf, 0, frame);
1710 /* cfra used for # code, Image can only have this # for all its users
1711 * warning, 'iuser' can be NULL */
1712 static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
1716 int assign = 0, flag;
1718 /* always ensure clean ima */
1719 image_free_buffers(ima);
1721 /* is there a PackedFile with this image ? */
1722 if (ima->packedfile) {
1723 flag = IB_rect|IB_multilayer;
1724 if(ima->flag & IMA_DO_PREMUL) flag |= IB_premul;
1726 ibuf = IMB_ibImageFromMemory((unsigned char*)ima->packedfile->data, ima->packedfile->size, flag);
1729 flag= IB_rect|IB_multilayer|IB_metadata;
1730 if(ima->flag & IMA_DO_PREMUL)
1733 /* get the right string */
1734 BLI_strncpy(str, ima->name, sizeof(str));
1736 BLI_path_abs(str, ima->id.lib->filepath);
1738 BLI_path_abs(str, G.sce);
1740 BLI_path_frame(str, cfra, 0);
1743 ibuf = IMB_loadiffname(str, flag);
1747 /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_get_ibuf */
1748 if (ibuf->ftype==OPENEXR && ibuf->userdata) {
1749 image_create_multilayer(ima, ibuf, cfra);
1750 ima->type= IMA_TYPE_MULTILAYER;
1751 IMB_freeImBuf(ibuf);
1755 image_initialize_after_load(ima, ibuf);
1758 /* check if the image is a font image... */
1759 detectBitmapFont(ibuf);
1761 /* make packed file for autopack */
1762 if ((ima->packedfile == NULL) && (G.fileflags & G_AUTOPACK))
1763 ima->packedfile = newPackedFile(NULL, str);
1770 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
1778 static ImBuf *image_get_ibuf_multilayer(Image *ima, ImageUser *iuser)
1783 ibuf = image_load_image_file(ima, iuser, 0);
1784 if(ibuf) { /* actually an error */
1785 ima->type= IMA_TYPE_IMAGE;
1790 RenderPass *rpass= BKE_image_multilayer_index(ima->rr, iuser);
1793 ibuf= IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0, 0);
1795 image_initialize_after_load(ima, ibuf);
1797 ibuf->rect_float= rpass->rect;
1798 ibuf->flags |= IB_rectfloat;
1799 ibuf->channels= rpass->channels;
1800 ibuf->profile = IB_PROFILE_LINEAR_RGB;
1802 image_assign_ibuf(ima, ibuf, iuser?iuser->multi_index:IMA_NO_INDEX, 0);
1815 /* showing RGBA result itself (from compo/sequence) or
1816 like exr, using layers etc */
1817 /* always returns a single ibuf, also during render progress */
1818 static ImBuf *image_get_render_result(Image *ima, ImageUser *iuser, void **lock_r)
1822 float *rectf, *rectz;
1825 int channels, layer, pass;
1827 int from_render= (ima->render_slot == ima->last_render_slot);
1829 if(!(iuser && iuser->scene))
1832 /* if we the caller is not going to release the lock, don't give the image */
1836 re= RE_GetRender(iuser->scene->id.name);
1839 layer= (iuser)? iuser->layer: 0;
1840 pass= (iuser)? iuser->pass: 0;
1843 RE_AcquireResultImage(re, &rres);
1845 else if(ima->renders[ima->render_slot]) {
1846 rres= *(ima->renders[ima->render_slot]);
1847 rres.have_combined= rres.rectf != NULL;
1850 memset(&rres, 0, sizeof(RenderResult));
1852 if(!(rres.rectx > 0 && rres.recty > 0)) {
1854 RE_ReleaseResultImage(re);
1858 /* release is done in BKE_image_release_ibuf using lock_r */
1860 BLI_lock_thread(LOCK_VIEWER);
1864 /* this gives active layer, composite or seqence result */
1865 rect= (unsigned int *)rres.rect32;
1868 dither= iuser->scene->r.dither_intensity;
1870 /* combined layer gets added as first layer */
1871 if(rres.have_combined && layer==0);
1872 else if(rres.layers.first) {
1873 RenderLayer *rl= BLI_findlink(&rres.layers, layer-(rres.have_combined?1:0));
1877 /* there's no combined pass, is in renderlayer itself */
1882 rpass= BLI_findlink(&rl->passes, pass-1);
1884 channels= rpass->channels;
1886 dither= 0.0f; /* don't dither passes */
1890 for(rpass= rl->passes.first; rpass; rpass= rpass->next)
1891 if(rpass->passtype == SCE_PASS_Z)
1896 ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
1898 /* make ibuf if needed, and initialize it */
1900 ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, 0, 0);
1901 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
1904 ibuf->x= rres.rectx;
1905 ibuf->y= rres.recty;
1907 if(ibuf->rect_float!=rectf || rect) /* ensure correct redraw */
1908 imb_freerectImBuf(ibuf);
1914 ibuf->rect_float= rectf;
1915 ibuf->flags |= IB_rectfloat;
1916 ibuf->channels= channels;
1919 ibuf->rect_float= NULL;
1920 ibuf->flags &= ~IB_rectfloat;
1924 ibuf->zbuf_float= rectz;
1925 ibuf->flags |= IB_zbuffloat;
1928 ibuf->zbuf_float= NULL;
1929 ibuf->flags &= ~IB_zbuffloat;
1932 ibuf->dither= dither;
1934 ima->ok= IMA_OK_LOADED;
1939 static ImBuf *image_get_ibuf_threadsafe(Image *ima, ImageUser *iuser, int *frame_r, int *index_r)
1942 int frame = 0, index = 0;
1944 /* see if we already have an appropriate ibuf, with image source and type */
1945 if(ima->source==IMA_SRC_MOVIE) {
1946 frame= iuser?iuser->framenr:ima->lastframe;
1947 ibuf= image_get_ibuf(ima, 0, frame);
1948 /* XXX temp stuff? */
1949 if(ima->lastframe != frame)
1950 ima->tpageflag |= IMA_TPAGE_REFRESH;
1951 ima->lastframe = frame;
1953 else if(ima->source==IMA_SRC_SEQUENCE) {
1954 if(ima->type==IMA_TYPE_IMAGE) {
1955 frame= iuser?iuser->framenr:ima->lastframe;
1956 ibuf= image_get_ibuf(ima, 0, frame);
1958 /* XXX temp stuff? */
1959 if(ima->lastframe != frame)
1960 ima->tpageflag |= IMA_TPAGE_REFRESH;
1961 ima->lastframe = frame;
1963 else if(ima->type==IMA_TYPE_MULTILAYER) {
1964 frame= iuser?iuser->framenr:ima->lastframe;
1965 index= iuser?iuser->multi_index:IMA_NO_INDEX;
1966 ibuf= image_get_ibuf(ima, index, frame);
1969 else if(ima->source==IMA_SRC_FILE) {
1970 if(ima->type==IMA_TYPE_IMAGE)
1971 ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
1972 else if(ima->type==IMA_TYPE_MULTILAYER)
1973 ibuf= image_get_ibuf(ima, iuser?iuser->multi_index:IMA_NO_INDEX, 0);
1975 else if(ima->source == IMA_SRC_GENERATED) {
1976 ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
1978 else if(ima->source == IMA_SRC_VIEWER) {
1979 if(ima->type==IMA_TYPE_R_RESULT) {
1980 /* always verify entirely, not that this shouldn't happen
1981 * during render anyway */
1983 else if(ima->type==IMA_TYPE_COMPOSITE) {
1984 frame= iuser?iuser->framenr:0;
1985 ibuf= image_get_ibuf(ima, 0, frame);
1995 /* Checks optional ImageUser and verifies/creates ImBuf. */
1996 /* use this one if you want to get a render result in progress,
1997 * if not, use BKE_image_get_ibuf which doesn't require a release */
1998 ImBuf *BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **lock_r)
2001 float color[] = {0, 0, 0, 1};
2002 int frame= 0, index= 0;
2004 /* This function is intended to be thread-safe. It postpones the mutex lock
2005 * until it needs to load the image, if the image is already there it
2006 * should just get the pointer and return. The reason is that a lot of mutex
2007 * locks appears to be very slow on certain multicore macs, causing a render
2008 * with image textures to actually slow down as more threads are used.
2010 * Note that all the image loading functions should also make sure they do
2011 * things in a threadsafe way for image_get_ibuf_threadsafe to work correct.
2012 * That means, the last two steps must be, 1) add the ibuf to the list and
2013 * 2) set ima/iuser->ok to 0 to IMA_OK_LOADED */
2018 /* quick reject tests */
2028 /* try to get the ibuf without locking */
2029 ibuf= image_get_ibuf_threadsafe(ima, iuser, &frame, &index);
2032 /* couldn't get ibuf and image is not ok, so let's lock and try to
2034 BLI_lock_thread(LOCK_IMAGE);
2036 /* need to check ok flag and loading ibuf again, because the situation
2037 * might have changed in the meantime */
2040 BLI_unlock_thread(LOCK_IMAGE);
2044 else if(ima->ok==0) {
2045 BLI_unlock_thread(LOCK_IMAGE);
2049 ibuf= image_get_ibuf_threadsafe(ima, iuser, &frame, &index);
2052 /* we are sure we have to load the ibuf, using source and type */
2053 if(ima->source==IMA_SRC_MOVIE) {
2054 /* source is from single file, use flipbook to store ibuf */
2055 ibuf= image_load_movie_file(ima, iuser, frame);
2057 else if(ima->source==IMA_SRC_SEQUENCE) {
2058 if(ima->type==IMA_TYPE_IMAGE) {
2059 /* regular files, ibufs in flipbook, allows saving */
2060 ibuf= image_load_sequence_file(ima, iuser, frame);
2062 /* no else; on load the ima type can change */
2063 if(ima->type==IMA_TYPE_MULTILAYER) {
2064 /* only 1 layer/pass stored in imbufs, no exrhandle anim storage, no saving */
2065 ibuf= image_load_sequence_multilayer(ima, iuser, frame);
2069 BLI_strncpy(ima->name, ibuf->name, sizeof(ima->name));
2071 else if(ima->source==IMA_SRC_FILE) {
2073 if(ima->type==IMA_TYPE_IMAGE)
2074 ibuf= image_load_image_file(ima, iuser, frame); /* cfra only for '#', this global is OK */
2075 /* no else; on load the ima type can change */
2076 if(ima->type==IMA_TYPE_MULTILAYER)
2077 /* keeps render result, stores ibufs in listbase, allows saving */
2078 ibuf= image_get_ibuf_multilayer(ima, iuser);
2081 else if(ima->source == IMA_SRC_GENERATED) {
2082 /* generated is: ibuf is allocated dynamically */
2083 /* UV testgrid or black or solid etc */
2084 if(ima->gen_x==0) ima->gen_x= 1024;
2085 if(ima->gen_y==0) ima->gen_y= 1024;
2086 ibuf= add_ibuf_size(ima->gen_x, ima->gen_y, ima->name, 24, 0, ima->gen_type, color);
2087 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
2088 ima->ok= IMA_OK_LOADED;
2090 else if(ima->source == IMA_SRC_VIEWER) {
2091 if(ima->type==IMA_TYPE_R_RESULT) {
2092 /* always verify entirely, and potentially
2093 returns pointer to release later */
2094 ibuf= image_get_render_result(ima, iuser, lock_r);
2096 else if(ima->type==IMA_TYPE_COMPOSITE) {
2097 /* requires lock/unlock, otherwise don't return image */
2099 /* unlock in BKE_image_release_ibuf */
2100 BLI_lock_thread(LOCK_VIEWER);
2103 /* Composite Viewer, all handled in compositor */
2104 /* fake ibuf, will be filled in compositor */
2105 ibuf= IMB_allocImBuf(256, 256, 32, IB_rect, 0);
2106 image_assign_ibuf(ima, ibuf, 0, frame);
2112 BLI_unlock_thread(LOCK_IMAGE);
2115 /* we assuming that if it is not rendering, it's also not multithreaded
2116 * (a somewhat weak assumption) */
2118 tag_image_time(ima);
2123 void BKE_image_release_ibuf(Image *ima, void *lock)
2125 /* for getting image during threaded render / compositing, need to release */
2127 BLI_unlock_thread(LOCK_VIEWER); /* viewer image */
2130 RE_ReleaseResultImage(lock); /* render result */
2131 BLI_unlock_thread(LOCK_VIEWER); /* view image imbuf */
2135 ImBuf *BKE_image_get_ibuf(Image *ima, ImageUser *iuser)
2137 return BKE_image_acquire_ibuf(ima, iuser, NULL);
2140 void BKE_image_user_calc_frame(ImageUser *iuser, int cfra, int fieldnr)
2144 /* here (+fie_ima/2-1) makes sure that division happens correctly */
2145 len= (iuser->fie_ima*iuser->frames)/2;
2151 cfra= cfra - iuser->sfra+1;
2155 cfra= ( (cfra) % len );
2156 if(cfra < 0) cfra+= len;
2157 if(cfra==0) cfra= len;
2161 else if(cfra>len) cfra= len;
2163 /* convert current frame to current field */
2167 /* transform to images space */
2168 imanr= (cfra+iuser->fie_ima-2)/iuser->fie_ima;
2169 if(imanr>iuser->frames) imanr= iuser->frames;
2170 imanr+= iuser->offset;
2173 imanr= ( (imanr) % len );
2174 while(imanr < 0) imanr+= len;
2175 if(imanr==0) imanr= len;
2178 /* allows image users to handle redraws */
2179 if(iuser->flag & IMA_ANIM_ALWAYS)
2180 if(imanr!=iuser->framenr)
2181 iuser->flag |= IMA_ANIM_REFRESHED;
2183 iuser->framenr= imanr;
2184 if(iuser->ok==0) iuser->ok= 1;