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)
664 else if (imtype==R_RADHDR)
667 else if (imtype==R_PNG)
670 else if (imtype==R_DDS)
673 else if (imtype==R_BMP)
676 else if (imtype==R_TIFF)
679 else if (imtype==R_OPENEXR || imtype==R_MULTILAYER)
682 else if (imtype==R_CINEON)
684 else if (imtype==R_DPX)
687 else if (imtype==R_TARGA)
689 else if(imtype==R_RAWTGA)
692 else if(imtype==R_JP2)
699 int BKE_ftype_to_imtype(int ftype)
703 else if(ftype == IMAGIC)
706 else if (ftype & RADHDR)
709 else if (ftype & PNG)
712 else if (ftype & DDS)
715 else if (ftype & BMP)
718 else if (ftype & TIF)
721 else if (ftype & OPENEXR)
724 else if (ftype & CINEON)
726 else if (ftype & DPX)
729 else if (ftype & TGA)
731 else if(ftype & RAWTGA)
742 int BKE_imtype_is_movie(int imtype)
759 void BKE_add_image_extension(char *string, int imtype)
763 if(imtype== R_IRIS) {
764 if(!BLI_testextensie(string, ".rgb"))
767 else if(imtype==R_IRIZ) {
768 if(!BLI_testextensie(string, ".rgb"))
772 else if(imtype==R_RADHDR) {
773 if(!BLI_testextensie(string, ".hdr"))
777 else if (ELEM5(imtype, R_PNG, R_FFMPEG, R_H264, R_THEORA, R_XVID)) {
778 if(!BLI_testextensie(string, ".png"))
782 else if(imtype==R_DDS) {
783 if(!BLI_testextensie(string, ".dds"))
787 else if(imtype==R_RAWTGA) {
788 if(!BLI_testextensie(string, ".tga"))
791 else if(imtype==R_BMP) {
792 if(!BLI_testextensie(string, ".bmp"))
796 else if(imtype==R_TIFF) {
797 if(!BLI_testextensie(string, ".tif") &&
798 !BLI_testextensie(string, ".tiff")) extension= ".tif";
802 else if( ELEM(imtype, R_OPENEXR, R_MULTILAYER)) {
803 if(!BLI_testextensie(string, ".exr"))
808 else if(imtype==R_CINEON){
809 if (!BLI_testextensie(string, ".cin"))
812 else if(imtype==R_DPX){
813 if (!BLI_testextensie(string, ".dpx"))
817 else if(imtype==R_TARGA) {
818 if(!BLI_testextensie(string, ".tga"))
822 else if(imtype==R_JP2) {
823 if(!BLI_testextensie(string, ".jp2"))
827 else { // R_AVICODEC, R_AVIRAW, R_AVIJPEG, R_JPEG90, R_QUICKTIME etc
828 if(!( BLI_testextensie(string, ".jpg") || BLI_testextensie(string, ".jpeg")))
832 strcat(string, extension);
835 /* could allow access externally - 512 is for long names, 64 is for id names */
836 typedef struct StampData {
849 static void stampdata(Scene *scene, StampData *stamp_data, int do_prefix)
855 if (scene->r.stamp & R_STAMP_FILENAME) {
856 if (G.relbase_valid) {
857 if (do_prefix) sprintf(stamp_data->file, "File %s", G.sce);
858 else sprintf(stamp_data->file, "%s", G.sce);
860 if (do_prefix) strcpy(stamp_data->file, "File <untitled>");
861 else strcpy(stamp_data->file, "<untitled>");
864 stamp_data->file[0] = '\0';
867 if (scene->r.stamp & R_STAMP_NOTE) {
868 /* Never do prefix for Note */
869 sprintf(stamp_data->note, "%s", scene->r.stamp_udata);
871 stamp_data->note[0] = '\0';
874 if (scene->r.stamp & R_STAMP_DATE) {
878 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);
880 if (do_prefix) sprintf(stamp_data->date, "Date %s", text);
881 else sprintf(stamp_data->date, "%s", text);
883 stamp_data->date[0] = '\0';
886 if (scene->r.stamp & R_STAMP_MARKER) {
887 char *name = scene_find_last_marker_name(scene, CFRA);
889 if (name) strcpy(text, name);
890 else strcpy(text, "<none>");
892 if (do_prefix) sprintf(stamp_data->marker, "Marker %s", text);
893 else sprintf(stamp_data->marker, "%s", text);
895 stamp_data->marker[0] = '\0';
898 if (scene->r.stamp & R_STAMP_TIME) {
901 f = (int)(scene->r.cfra % scene->r.frs_sec);
902 s = (int)(scene->r.cfra / scene->r.frs_sec);
914 if (scene->r.frs_sec < 100)
915 sprintf (text, "%02d:%02d:%02d.%02d", h, m, s, f);
917 sprintf (text, "%02d:%02d:%02d.%03d", h, m, s, f);
919 if (do_prefix) sprintf(stamp_data->time, "Time %s", text);
920 else sprintf(stamp_data->time, "%s", text);
922 stamp_data->time[0] = '\0';
925 if (scene->r.stamp & R_STAMP_FRAME) {
927 if (do_prefix) sprintf(format, "Frame %%0%di", 1 + (int) log10(scene->r.efra));
928 else sprintf(format, "%%0%di", 1 + (int) log10(scene->r.efra));
929 sprintf (stamp_data->frame, format, scene->r.cfra);
931 stamp_data->frame[0] = '\0';
934 if (scene->r.stamp & R_STAMP_CAMERA) {
935 if (scene->camera) strcpy(text, ((Camera *) scene->camera)->id.name+2);
936 else strcpy(text, "<none>");
938 if (do_prefix) sprintf(stamp_data->camera, "Camera %s", text);
939 else sprintf(stamp_data->camera, "%s", text);
941 stamp_data->camera[0] = '\0';
944 if (scene->r.stamp & R_STAMP_SCENE) {
945 if (do_prefix) sprintf(stamp_data->scene, "Scene %s", scene->id.name+2);
946 else sprintf(stamp_data->scene, "%s", scene->id.name+2);
948 stamp_data->scene[0] = '\0';
951 if (scene->r.stamp & R_STAMP_SEQSTRIP) {
952 Sequence *seq= NULL; //XXX = get_foreground_frame_seq(scene->r.cfra);
954 if (seq) strcpy(text, seq->name+2);
955 else strcpy(text, "<none>");
957 if (do_prefix) sprintf(stamp_data->strip, "Strip %s", text);
958 else sprintf(stamp_data->strip, "%s", text);
960 stamp_data->strip[0] = '\0';
964 Render *re= RE_GetRender(scene->id.name);
965 RenderStats *stats= re ? RE_GetStats(re):NULL;
967 if (stats && (scene->r.stamp & R_STAMP_RENDERTIME)) {
968 BLI_timestr(stats->lastframetime, text);
970 if (do_prefix) sprintf(stamp_data->rendertime, "RenderTime %s", text);
971 else sprintf(stamp_data->rendertime, "%s", text);
973 stamp_data->rendertime[0] = '\0';
978 // XXX - Bad level call.
979 extern int datatoc_bmonofont_ttf_size;
980 extern char datatoc_bmonofont_ttf[];
982 // XXX - copied from text_font_begin ! Change all the BLF_* here
985 int stamp_font_begin(int size)
988 mono= BLF_load_mem_unique("monospace", (unsigned char *)datatoc_bmonofont_ttf, datatoc_bmonofont_ttf_size);
990 BLF_aspect(mono, 1.0);
991 BLF_size(mono, size, 72);
992 return(mono); // XXX This is for image_gen.c!!
995 void BKE_stamp_buf(Scene *scene, unsigned char *rect, float *rectf, int width, int height, int channels)
997 struct StampData stamp_data;
1002 if (!rect && !rectf)
1005 stampdata(scene, &stamp_data, 1);
1007 /* TODO, do_versions */
1008 if(scene->r.stamp_font_id < 8)
1009 scene->r.stamp_font_id= 12;
1011 stamp_font_begin(scene->r.stamp_font_id);
1013 BLF_buffer(mono, rectf, rect, width, height, channels);
1014 BLF_buffer_col(mono, scene->r.fg_stamp[0], scene->r.fg_stamp[1], scene->r.fg_stamp[2], 1.0);
1015 pad= BLF_width(mono, "--");
1017 /* use 'h_fixed' rather then 'h', aligns better */
1018 // BLF_width_and_height(mono, "^|/_AgPpJjlYy", &w, &h_fixed);
1021 BLF_boundbox(mono, "^|/_AgPpJjlYy", &box);
1022 h_fixed= box.ymax - box.ymin;
1028 if (stamp_data.file[0]) {
1029 /* Top left corner */
1030 BLF_width_and_height(mono, stamp_data.file, &w, &h); h= h_fixed;
1033 /* also a little of space to the background. */
1034 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y-3, w+3, y+h+2);
1036 /* and draw the text. */
1037 BLF_position(mono, x, y, 0.0);
1038 BLF_draw_buffer(mono, stamp_data.file);
1040 /* the extra pixel for background. */
1044 /* Top left corner, below File */
1045 if (stamp_data.note[0]) {
1046 BLF_width_and_height(mono, stamp_data.note, &w, &h); h= h_fixed;
1049 /* and space for background. */
1050 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, 0, y-3, w+3, y+h+2);
1052 BLF_position(mono, x, y+1, 0.0);
1053 BLF_draw_buffer(mono, stamp_data.note);
1055 /* the extra pixel for background. */
1059 /* Top left corner, below File (or Note) */
1060 if (stamp_data.date[0]) {
1061 BLF_width_and_height(mono, stamp_data.date, &w, &h); h= h_fixed;
1064 /* and space for background. */
1065 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, 0, y-3, w+3, y+h+2);
1067 BLF_position(mono, x, y, 0.0);
1068 BLF_draw_buffer(mono, stamp_data.date);
1070 /* the extra pixel for background. */
1074 /* Top left corner, below File, Date or Note */
1075 if (stamp_data.rendertime[0]) {
1076 BLF_width_and_height(mono, stamp_data.rendertime, &w, &h); h= h_fixed;
1079 /* and space for background. */
1080 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, 0, y-3, w+3, y+h+2);
1082 BLF_position(mono, x, y, 0.0);
1083 BLF_draw_buffer(mono, stamp_data.rendertime);
1089 /* Bottom left corner, leaving space for timing */
1090 if (stamp_data.marker[0]) {
1091 BLF_width_and_height(mono, stamp_data.marker, &w, &h); h= h_fixed;
1093 /* extra space for background. */
1094 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y, w+2, y+h+2);
1096 /* and pad the text. */
1097 BLF_position(mono, x, y+3, 0.0);
1098 BLF_draw_buffer(mono, stamp_data.marker);
1104 /* Left bottom corner */
1105 if (stamp_data.time[0]) {
1106 BLF_width_and_height(mono, stamp_data.time, &w, &h); h= h_fixed;
1108 /* extra space for background */
1109 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y, x+w+2, y+h+2);
1111 /* and pad the text. */
1112 BLF_position(mono, x, y+3, 0.0);
1113 BLF_draw_buffer(mono, stamp_data.time);
1119 if (stamp_data.frame[0]) {
1120 BLF_width_and_height(mono, stamp_data.frame, &w, &h); h= h_fixed;
1122 /* extra space for background. */
1123 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y, x+w+2, y+h+2);
1125 /* and pad the text. */
1126 BLF_position(mono, x, y+3, 0.0);
1127 BLF_draw_buffer(mono, stamp_data.frame);
1133 if (stamp_data.camera[0]) {
1134 BLF_width_and_height(mono, stamp_data.camera, &w, &h); h= h_fixed;
1136 /* extra space for background. */
1137 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y, x+w+2, y+h+2);
1138 BLF_position(mono, x, y+3, 0.0);
1139 BLF_draw_buffer(mono, stamp_data.camera);
1142 if (stamp_data.scene[0]) {
1143 BLF_width_and_height(mono, stamp_data.scene, &w, &h); h= h_fixed;
1145 /* Bottom right corner, with an extra space because blenfont is too strict! */
1148 /* extra space for background. */
1149 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y, x+w+3, y+h+2);
1151 /* and pad the text. */
1152 BLF_position(mono, x, y+3, 0.0);
1153 BLF_draw_buffer(mono, stamp_data.scene);
1156 if (stamp_data.strip[0]) {
1157 BLF_width_and_height(mono, stamp_data.scene, &w, &h); h= h_fixed;
1159 /* Top right corner, with an extra space because blenfont is too strict! */
1163 /* extra space for background. */
1164 buf_rectfill_area(rect, rectf, width, height, scene->r.bg_stamp, x, y-3, x+w+pad, y+h+2);
1166 BLF_position(mono, x, y, 0.0);
1167 BLF_draw_buffer(mono, stamp_data.strip);
1170 /* cleanup the buffer. */
1171 BLF_buffer(mono, NULL, NULL, 0, 0, 0);
1174 void BKE_stamp_info(Scene *scene, struct ImBuf *ibuf)
1176 struct StampData stamp_data;
1180 /* fill all the data values, no prefix */
1181 stampdata(scene, &stamp_data, 0);
1183 if (stamp_data.file[0]) IMB_metadata_change_field (ibuf, "File", stamp_data.file);
1184 if (stamp_data.note[0]) IMB_metadata_change_field (ibuf, "Note", stamp_data.note);
1185 if (stamp_data.date[0]) IMB_metadata_change_field (ibuf, "Date", stamp_data.date);
1186 if (stamp_data.marker[0]) IMB_metadata_change_field (ibuf, "Marker", stamp_data.marker);
1187 if (stamp_data.time[0]) IMB_metadata_change_field (ibuf, "Time", stamp_data.time);
1188 if (stamp_data.frame[0]) IMB_metadata_change_field (ibuf, "Frame", stamp_data.frame);
1189 if (stamp_data.camera[0]) IMB_metadata_change_field (ibuf, "Camera", stamp_data.camera);
1190 if (stamp_data.scene[0]) IMB_metadata_change_field (ibuf, "Scene", stamp_data.scene);
1191 if (stamp_data.strip[0]) IMB_metadata_change_field (ibuf, "Strip", stamp_data.strip);
1192 if (stamp_data.rendertime[0]) IMB_metadata_change_field (ibuf, "RenderTime", stamp_data.rendertime);
1195 int BKE_write_ibuf(Scene *scene, ImBuf *ibuf, char *name, int imtype, int subimtype, int quality)
1202 else if(imtype== R_IRIS) {
1203 ibuf->ftype= IMAGIC;
1206 else if ((imtype==R_RADHDR)) {
1207 ibuf->ftype= RADHDR;
1210 else if (ELEM5(imtype, R_PNG, R_FFMPEG, R_H264, R_THEORA, R_XVID)) {
1214 ibuf->ftype |= quality; /* quality is actually compression 0-100 --> 0-9 */
1218 else if ((imtype==R_DDS)) {
1222 else if ((imtype==R_BMP)) {
1226 else if (imtype==R_TIFF) {
1229 if(subimtype & R_TIFF_16BIT)
1230 ibuf->ftype |= TIF_16BIT;
1234 else if (imtype==R_OPENEXR || imtype==R_MULTILAYER) {
1235 ibuf->ftype= OPENEXR;
1236 if(subimtype & R_OPENEXR_HALF)
1237 ibuf->ftype |= OPENEXR_HALF;
1238 ibuf->ftype |= (quality & OPENEXR_COMPRESS);
1240 if(!(subimtype & R_OPENEXR_ZBUF))
1241 ibuf->zbuf_float = NULL; /* signal for exr saving */
1246 else if (imtype==R_CINEON) {
1247 ibuf->ftype = CINEON;
1249 else if (imtype==R_DPX) {
1253 else if (imtype==R_TARGA) {
1256 else if(imtype==R_RAWTGA) {
1257 ibuf->ftype= RAWTGA;
1259 #ifdef WITH_OPENJPEG
1260 else if(imtype==R_JP2) {
1261 if(quality < 10) quality= 90;
1262 ibuf->ftype= JP2|quality;
1264 if (subimtype & R_JPEG2K_16BIT) {
1265 ibuf->ftype |= JP2_16BIT;
1266 } else if (subimtype & R_JPEG2K_12BIT) {
1267 ibuf->ftype |= JP2_12BIT;
1270 if (subimtype & R_JPEG2K_YCC) {
1271 ibuf->ftype |= JP2_YCC;
1274 if (subimtype & R_JPEG2K_CINE_PRESET) {
1275 ibuf->ftype |= JP2_CINE;
1276 if (subimtype & R_JPEG2K_CINE_48FPS)
1277 ibuf->ftype |= JP2_CINE_48FPS;
1282 /* R_JPEG90, etc. default we save jpegs */
1283 if(quality < 10) quality= 90;
1284 ibuf->ftype= JPG|quality;
1285 if(ibuf->depth==32) ibuf->depth= 24; /* unsupported feature only confuses other s/w */
1288 BLI_make_existing_file(name);
1290 if(scene && scene->r.stamp & R_STAMP_ALL)
1291 BKE_stamp_info(scene, ibuf);
1293 ok = IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat);
1302 void BKE_makepicstring(char *string, char *base, int frame, int imtype, int use_ext)
1304 if (string==NULL) return;
1305 BLI_strncpy(string, base, FILE_MAX - 10); /* weak assumption */
1306 BLI_path_abs(string, G.sce);
1307 BLI_path_frame(string, frame, 4);
1310 BKE_add_image_extension(string, imtype);
1314 /* used by sequencer too */
1315 struct anim *openanim(char *name, int flags)
1320 anim = IMB_open_anim(name, flags);
1321 if (anim == NULL) return(0);
1323 ibuf = IMB_anim_absolute(anim, 0);
1325 if(BLI_exists(name))
1326 printf("not an anim: %s\n", name);
1328 printf("anim file doesn't exist: %s\n", name);
1329 IMB_free_anim(anim);
1332 IMB_freeImBuf(ibuf);
1337 /* ************************* New Image API *************** */
1340 /* Notes about Image storage
1342 -> written in .blend
1344 -> written in .blend
1346 -> comes from packedfile or filename
1348 -> comes from packedfile or filename
1350 -> ibufs from exrhandle
1352 -> ibufs come from movie, temporary renderresult or sequence
1354 -> comes from packedfile or filename or generated
1359 /* forces existence of 1 Image for renderout or nodes, returns Image */
1360 /* name is only for default, when making new one */
1361 Image *BKE_image_verify_viewer(int type, const char *name)
1365 for(ima=G.main->image.first; ima; ima= ima->id.next)
1366 if(ima->source==IMA_SRC_VIEWER)
1371 ima= image_alloc(name, IMA_SRC_VIEWER, type);
1373 /* happens on reload, imagewindow cannot be image user when hidden*/
1375 id_us_plus(&ima->id);
1380 void BKE_image_assign_ibuf(Image *ima, ImBuf *ibuf)
1382 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
1385 void BKE_image_signal(Image *ima, ImageUser *iuser, int signal)
1391 case IMA_SIGNAL_FREE:
1392 image_free_buffers(ima);
1396 case IMA_SIGNAL_SRC_CHANGE:
1397 if(ima->type == IMA_TYPE_UV_TEST)
1398 if(ima->source != IMA_SRC_GENERATED)
1399 ima->type= IMA_TYPE_IMAGE;
1401 if(ima->source==IMA_SRC_GENERATED) {
1402 if(ima->gen_x==0 || ima->gen_y==0) {
1403 ImBuf *ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
1405 ima->gen_x= ibuf->x;
1406 ima->gen_y= ibuf->y;
1411 image_free_buffers(ima);
1418 case IMA_SIGNAL_RELOAD:
1419 /* try to repack file */
1420 if(ima->packedfile) {
1422 pf = newPackedFile(NULL, ima->name);
1424 freePackedFile(ima->packedfile);
1425 ima->packedfile = pf;
1426 image_free_buffers(ima);
1428 printf("ERROR: Image not available. Keeping packed image\n");
1432 image_free_buffers(ima);
1438 case IMA_SIGNAL_USER_NEW_IMAGE:
1441 if(ima->source==IMA_SRC_FILE || ima->source==IMA_SRC_SEQUENCE) {
1442 if(ima->type==IMA_TYPE_MULTILAYER) {
1443 iuser->multi_index= 0;
1444 iuser->layer= iuser->pass= 0;
1452 /* if layer or pass changes, we need an index for the imbufs list */
1453 /* note it is called for rendered results, but it doesnt use the index! */
1454 /* and because rendered results use fake layer/passes, don't correct for wrong indices here */
1455 RenderPass *BKE_image_multilayer_index(RenderResult *rr, ImageUser *iuser)
1458 RenderPass *rpass= NULL;
1464 short index= 0, rl_index= 0, rp_index;
1466 for(rl= rr->layers.first; rl; rl= rl->next, rl_index++) {
1468 for(rpass= rl->passes.first; rpass; rpass= rpass->next, index++, rp_index++)
1469 if(iuser->layer==rl_index && iuser->pass==rp_index)
1476 iuser->multi_index= index;
1478 iuser->multi_index= 0;
1481 rl= rr->layers.first;
1483 rpass= rl->passes.first;
1489 RenderResult *BKE_image_acquire_renderresult(Scene *scene, Image *ima)
1494 else if(ima->type==IMA_TYPE_R_RESULT) {
1495 if(ima->render_slot == ima->last_render_slot)
1496 return RE_AcquireResultRead(RE_GetRender(scene->id.name));
1498 return ima->renders[ima->render_slot];
1504 void BKE_image_release_renderresult(Scene *scene, Image *ima)
1507 else if(ima->type==IMA_TYPE_R_RESULT) {
1508 if(ima->render_slot == ima->last_render_slot)
1509 RE_ReleaseResult(RE_GetRender(scene->id.name));
1513 void BKE_image_backup_render(Scene *scene, Image *ima)
1515 /* called right before rendering, ima->renders contains render
1516 result pointers for everything but the current render */
1517 Render *re= RE_GetRender(scene->id.name);
1518 int slot= ima->render_slot, last= ima->last_render_slot;
1521 if(ima->renders[slot]) {
1522 RE_FreeRenderResult(ima->renders[slot]);
1523 ima->renders[slot]= NULL;
1526 ima->renders[last]= NULL;
1527 RE_SwapResult(re, &ima->renders[last]);
1530 ima->last_render_slot= slot;
1533 /* after imbuf load, openexr type can return with a exrhandle open */
1534 /* in that case we have to build a render-result */
1535 static void image_create_multilayer(Image *ima, ImBuf *ibuf, int framenr)
1538 ima->rr= RE_MultilayerConvert(ibuf->userdata, ibuf->x, ibuf->y);
1541 IMB_exr_close(ibuf->userdata);
1544 ibuf->userdata= NULL;
1546 ima->rr->framenr= framenr;
1549 /* common stuff to do with images after loading */
1550 static void image_initialize_after_load(Image *ima, ImBuf *ibuf)
1554 /* preview is NULL when it has never been used as an icon before */
1555 if(G.background==0 && ima->preview==NULL)
1556 BKE_icon_changed(BKE_icon_getid(&ima->id));
1558 /* stringcodes also in ibuf, ibuf->name is used to retrieve original (buttons) */
1559 BLI_strncpy(ibuf->name, ima->name, FILE_MAX);
1562 if (ima->flag & IMA_FIELDS) {
1563 if(ima->flag & IMA_STD_FIELD) de_interlace_st(ibuf);
1564 else de_interlace_ng(ibuf);
1567 ima->lastused = clock() / CLOCKS_PER_SEC;
1569 ima->ok= IMA_OK_LOADED;
1573 static ImBuf *image_load_sequence_file(Image *ima, ImageUser *iuser, int frame)
1576 unsigned short numlen;
1577 char name[FILE_MAX], head[FILE_MAX], tail[FILE_MAX];
1580 /* XXX temp stuff? */
1581 if(ima->lastframe != frame)
1582 ima->tpageflag |= IMA_TPAGE_REFRESH;
1584 ima->lastframe= frame;
1586 BLI_stringdec(ima->name, head, tail, &numlen);
1587 BLI_stringenc(ima->name, head, tail, numlen, frame);
1588 BLI_strncpy(name, ima->name, sizeof(name));
1591 BLI_path_abs(name, ima->id.lib->filepath);
1593 BLI_path_abs(name, G.sce);
1595 flag= IB_rect|IB_multilayer;
1596 if(ima->flag & IMA_DO_PREMUL)
1600 ibuf = IMB_loadiffname(name, flag);
1601 if(G.f & G_DEBUG) printf("loaded %s\n", name);
1605 /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_get_ibuf */
1606 if (ibuf->ftype==OPENEXR && ibuf->userdata) {
1607 image_create_multilayer(ima, ibuf, frame);
1608 ima->type= IMA_TYPE_MULTILAYER;
1609 IMB_freeImBuf(ibuf);
1613 image_initialize_after_load(ima, ibuf);
1614 image_assign_ibuf(ima, ibuf, 0, frame);
1617 image_initialize_after_load(ima, ibuf);
1618 image_assign_ibuf(ima, ibuf, 0, frame);
1630 static ImBuf *image_load_sequence_multilayer(Image *ima, ImageUser *iuser, int frame)
1632 struct ImBuf *ibuf= NULL;
1634 /* either we load from RenderResult, or we have to load a new one */
1636 /* check for new RenderResult */
1637 if(ima->rr==NULL || frame!=ima->rr->framenr) {
1638 /* copy to survive not found multilayer image */
1639 RenderResult *oldrr= ima->rr;
1642 ibuf = image_load_sequence_file(ima, iuser, frame);
1644 if(ibuf) { /* actually an error */
1645 ima->type= IMA_TYPE_IMAGE;
1646 printf("error, multi is normal image\n");
1648 // printf("loaded new result %p\n", ima->rr);
1649 /* free result if new one found */
1651 // if(oldrr) printf("freed previous result %p\n", oldrr);
1652 if(oldrr) RE_FreeRenderResult(oldrr);
1660 RenderPass *rpass= BKE_image_multilayer_index(ima->rr, iuser);
1663 // printf("load from pass %s\n", rpass->name);
1664 /* since we free render results, we copy the rect */
1665 ibuf= IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0, 0);
1666 ibuf->rect_float= MEM_dupallocN(rpass->rect);
1667 ibuf->flags |= IB_rectfloat;
1668 ibuf->mall= IB_rectfloat;
1669 ibuf->channels= rpass->channels;
1671 image_initialize_after_load(ima, ibuf);
1672 image_assign_ibuf(ima, ibuf, iuser?iuser->multi_index:0, frame);
1675 // else printf("pass not found\n");
1687 static ImBuf *image_load_movie_file(Image *ima, ImageUser *iuser, int frame)
1689 struct ImBuf *ibuf= NULL;
1691 ima->lastframe= frame;
1693 if(ima->anim==NULL) {
1696 BLI_strncpy(str, ima->name, FILE_MAX);
1698 BLI_path_abs(str, ima->id.lib->filepath);
1700 BLI_path_abs(str, G.sce);
1702 ima->anim = openanim(str, IB_rect);
1704 /* let's initialize this user */
1705 if(ima->anim && iuser && iuser->frames==0)
1706 iuser->frames= IMB_anim_get_duration(ima->anim);
1710 int dur = IMB_anim_get_duration(ima->anim);
1714 if(fra>(dur-1)) fra= dur-1;
1715 ibuf = IMB_anim_absolute(ima->anim, fra);
1718 image_initialize_after_load(ima, ibuf);
1719 image_assign_ibuf(ima, ibuf, 0, frame);
1733 /* cfra used for # code, Image can only have this # for all its users
1734 * warning, 'iuser' can be NULL */
1735 static ImBuf *image_load_image_file(Image *ima, ImageUser *iuser, int cfra)
1739 int assign = 0, flag;
1741 /* always ensure clean ima */
1742 image_free_buffers(ima);
1744 /* is there a PackedFile with this image ? */
1745 if (ima->packedfile) {
1746 flag = IB_rect|IB_multilayer;
1747 if(ima->flag & IMA_DO_PREMUL) flag |= IB_premul;
1749 ibuf = IMB_ibImageFromMemory((unsigned char*)ima->packedfile->data, ima->packedfile->size, flag);
1752 flag= IB_rect|IB_multilayer|IB_metadata;
1753 if(ima->flag & IMA_DO_PREMUL)
1756 /* get the right string */
1757 BLI_strncpy(str, ima->name, sizeof(str));
1759 BLI_path_abs(str, ima->id.lib->filepath);
1761 BLI_path_abs(str, G.sce);
1763 BLI_path_frame(str, cfra, 0);
1766 ibuf = IMB_loadiffname(str, flag);
1770 /* handle multilayer case, don't assign ibuf. will be handled in BKE_image_get_ibuf */
1771 if (ibuf->ftype==OPENEXR && ibuf->userdata) {
1772 image_create_multilayer(ima, ibuf, cfra);
1773 ima->type= IMA_TYPE_MULTILAYER;
1774 IMB_freeImBuf(ibuf);
1778 image_initialize_after_load(ima, ibuf);
1781 /* check if the image is a font image... */
1782 detectBitmapFont(ibuf);
1784 /* make packed file for autopack */
1785 if ((ima->packedfile == NULL) && (G.fileflags & G_AUTOPACK))
1786 ima->packedfile = newPackedFile(NULL, str);
1793 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
1801 static ImBuf *image_get_ibuf_multilayer(Image *ima, ImageUser *iuser)
1806 ibuf = image_load_image_file(ima, iuser, 0);
1807 if(ibuf) { /* actually an error */
1808 ima->type= IMA_TYPE_IMAGE;
1813 RenderPass *rpass= BKE_image_multilayer_index(ima->rr, iuser);
1816 ibuf= IMB_allocImBuf(ima->rr->rectx, ima->rr->recty, 32, 0, 0);
1818 image_initialize_after_load(ima, ibuf);
1820 ibuf->rect_float= rpass->rect;
1821 ibuf->flags |= IB_rectfloat;
1822 ibuf->channels= rpass->channels;
1823 ibuf->profile = IB_PROFILE_LINEAR_RGB;
1825 image_assign_ibuf(ima, ibuf, iuser?iuser->multi_index:IMA_NO_INDEX, 0);
1838 /* showing RGBA result itself (from compo/sequence) or
1839 like exr, using layers etc */
1840 /* always returns a single ibuf, also during render progress */
1841 static ImBuf *image_get_render_result(Image *ima, ImageUser *iuser, void **lock_r)
1845 float *rectf, *rectz;
1848 int channels, layer, pass;
1850 int from_render= (ima->render_slot == ima->last_render_slot);
1852 if(!(iuser && iuser->scene))
1855 /* if we the caller is not going to release the lock, don't give the image */
1859 re= RE_GetRender(iuser->scene->id.name);
1862 layer= (iuser)? iuser->layer: 0;
1863 pass= (iuser)? iuser->pass: 0;
1866 RE_AcquireResultImage(re, &rres);
1868 else if(ima->renders[ima->render_slot]) {
1869 rres= *(ima->renders[ima->render_slot]);
1870 rres.have_combined= rres.rectf != NULL;
1873 memset(&rres, 0, sizeof(RenderResult));
1875 if(!(rres.rectx > 0 && rres.recty > 0)) {
1877 RE_ReleaseResultImage(re);
1881 /* release is done in BKE_image_release_ibuf using lock_r */
1883 BLI_lock_thread(LOCK_VIEWER);
1887 /* this gives active layer, composite or seqence result */
1888 rect= (unsigned int *)rres.rect32;
1891 dither= iuser->scene->r.dither_intensity;
1893 /* combined layer gets added as first layer */
1894 if(rres.have_combined && layer==0);
1895 else if(rres.layers.first) {
1896 RenderLayer *rl= BLI_findlink(&rres.layers, layer-(rres.have_combined?1:0));
1900 /* there's no combined pass, is in renderlayer itself */
1905 rpass= BLI_findlink(&rl->passes, pass-1);
1907 channels= rpass->channels;
1909 dither= 0.0f; /* don't dither passes */
1913 for(rpass= rl->passes.first; rpass; rpass= rpass->next)
1914 if(rpass->passtype == SCE_PASS_Z)
1919 ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
1921 /* make ibuf if needed, and initialize it */
1923 ibuf= IMB_allocImBuf(rres.rectx, rres.recty, 32, 0, 0);
1924 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
1927 ibuf->x= rres.rectx;
1928 ibuf->y= rres.recty;
1930 if(ibuf->rect_float!=rectf || rect) /* ensure correct redraw */
1931 imb_freerectImBuf(ibuf);
1937 ibuf->rect_float= rectf;
1938 ibuf->flags |= IB_rectfloat;
1939 ibuf->channels= channels;
1942 ibuf->rect_float= NULL;
1943 ibuf->flags &= ~IB_rectfloat;
1947 ibuf->zbuf_float= rectz;
1948 ibuf->flags |= IB_zbuffloat;
1951 ibuf->zbuf_float= NULL;
1952 ibuf->flags &= ~IB_zbuffloat;
1955 ibuf->dither= dither;
1957 ima->ok= IMA_OK_LOADED;
1962 static ImBuf *image_get_ibuf_threadsafe(Image *ima, ImageUser *iuser, int *frame_r, int *index_r)
1965 int frame = 0, index = 0;
1967 /* see if we already have an appropriate ibuf, with image source and type */
1968 if(ima->source==IMA_SRC_MOVIE) {
1969 frame= iuser?iuser->framenr:ima->lastframe;
1970 ibuf= image_get_ibuf(ima, 0, frame);
1971 /* XXX temp stuff? */
1972 if(ima->lastframe != frame)
1973 ima->tpageflag |= IMA_TPAGE_REFRESH;
1974 ima->lastframe = frame;
1976 else if(ima->source==IMA_SRC_SEQUENCE) {
1977 if(ima->type==IMA_TYPE_IMAGE) {
1978 frame= iuser?iuser->framenr:ima->lastframe;
1979 ibuf= image_get_ibuf(ima, 0, frame);
1981 /* XXX temp stuff? */
1982 if(ima->lastframe != frame)
1983 ima->tpageflag |= IMA_TPAGE_REFRESH;
1984 ima->lastframe = frame;
1986 else if(ima->type==IMA_TYPE_MULTILAYER) {
1987 frame= iuser?iuser->framenr:ima->lastframe;
1988 index= iuser?iuser->multi_index:IMA_NO_INDEX;
1989 ibuf= image_get_ibuf(ima, index, frame);
1992 else if(ima->source==IMA_SRC_FILE) {
1993 if(ima->type==IMA_TYPE_IMAGE)
1994 ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
1995 else if(ima->type==IMA_TYPE_MULTILAYER)
1996 ibuf= image_get_ibuf(ima, iuser?iuser->multi_index:IMA_NO_INDEX, 0);
1998 else if(ima->source == IMA_SRC_GENERATED) {
1999 ibuf= image_get_ibuf(ima, IMA_NO_INDEX, 0);
2001 else if(ima->source == IMA_SRC_VIEWER) {
2002 if(ima->type==IMA_TYPE_R_RESULT) {
2003 /* always verify entirely, not that this shouldn't happen
2004 * during render anyway */
2006 else if(ima->type==IMA_TYPE_COMPOSITE) {
2007 frame= iuser?iuser->framenr:0;
2008 ibuf= image_get_ibuf(ima, 0, frame);
2018 /* Checks optional ImageUser and verifies/creates ImBuf. */
2019 /* use this one if you want to get a render result in progress,
2020 * if not, use BKE_image_get_ibuf which doesn't require a release */
2021 ImBuf *BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **lock_r)
2024 float color[] = {0, 0, 0, 1};
2025 int frame= 0, index= 0;
2027 /* This function is intended to be thread-safe. It postpones the mutex lock
2028 * until it needs to load the image, if the image is already there it
2029 * should just get the pointer and return. The reason is that a lot of mutex
2030 * locks appears to be very slow on certain multicore macs, causing a render
2031 * with image textures to actually slow down as more threads are used.
2033 * Note that all the image loading functions should also make sure they do
2034 * things in a threadsafe way for image_get_ibuf_threadsafe to work correct.
2035 * That means, the last two steps must be, 1) add the ibuf to the list and
2036 * 2) set ima/iuser->ok to 0 to IMA_OK_LOADED */
2041 /* quick reject tests */
2051 /* try to get the ibuf without locking */
2052 ibuf= image_get_ibuf_threadsafe(ima, iuser, &frame, &index);
2055 /* couldn't get ibuf and image is not ok, so let's lock and try to
2057 BLI_lock_thread(LOCK_IMAGE);
2059 /* need to check ok flag and loading ibuf again, because the situation
2060 * might have changed in the meantime */
2063 BLI_unlock_thread(LOCK_IMAGE);
2067 else if(ima->ok==0) {
2068 BLI_unlock_thread(LOCK_IMAGE);
2072 ibuf= image_get_ibuf_threadsafe(ima, iuser, &frame, &index);
2075 /* we are sure we have to load the ibuf, using source and type */
2076 if(ima->source==IMA_SRC_MOVIE) {
2077 /* source is from single file, use flipbook to store ibuf */
2078 ibuf= image_load_movie_file(ima, iuser, frame);
2080 else if(ima->source==IMA_SRC_SEQUENCE) {
2081 if(ima->type==IMA_TYPE_IMAGE) {
2082 /* regular files, ibufs in flipbook, allows saving */
2083 ibuf= image_load_sequence_file(ima, iuser, frame);
2085 /* no else; on load the ima type can change */
2086 if(ima->type==IMA_TYPE_MULTILAYER) {
2087 /* only 1 layer/pass stored in imbufs, no exrhandle anim storage, no saving */
2088 ibuf= image_load_sequence_multilayer(ima, iuser, frame);
2092 BLI_strncpy(ima->name, ibuf->name, sizeof(ima->name));
2094 else if(ima->source==IMA_SRC_FILE) {
2096 if(ima->type==IMA_TYPE_IMAGE)
2097 ibuf= image_load_image_file(ima, iuser, frame); /* cfra only for '#', this global is OK */
2098 /* no else; on load the ima type can change */
2099 if(ima->type==IMA_TYPE_MULTILAYER)
2100 /* keeps render result, stores ibufs in listbase, allows saving */
2101 ibuf= image_get_ibuf_multilayer(ima, iuser);
2104 else if(ima->source == IMA_SRC_GENERATED) {
2105 /* generated is: ibuf is allocated dynamically */
2106 /* UV testgrid or black or solid etc */
2107 if(ima->gen_x==0) ima->gen_x= 1024;
2108 if(ima->gen_y==0) ima->gen_y= 1024;
2109 ibuf= add_ibuf_size(ima->gen_x, ima->gen_y, ima->name, 24, 0, ima->gen_type, color);
2110 image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
2111 ima->ok= IMA_OK_LOADED;
2113 else if(ima->source == IMA_SRC_VIEWER) {
2114 if(ima->type==IMA_TYPE_R_RESULT) {
2115 /* always verify entirely, and potentially
2116 returns pointer to release later */
2117 ibuf= image_get_render_result(ima, iuser, lock_r);
2119 else if(ima->type==IMA_TYPE_COMPOSITE) {
2120 /* requires lock/unlock, otherwise don't return image */
2122 /* unlock in BKE_image_release_ibuf */
2123 BLI_lock_thread(LOCK_VIEWER);
2126 /* Composite Viewer, all handled in compositor */
2127 /* fake ibuf, will be filled in compositor */
2128 ibuf= IMB_allocImBuf(256, 256, 32, IB_rect, 0);
2129 image_assign_ibuf(ima, ibuf, 0, frame);
2135 BLI_unlock_thread(LOCK_IMAGE);
2138 /* we assuming that if it is not rendering, it's also not multithreaded
2139 * (a somewhat weak assumption) */
2141 tag_image_time(ima);
2146 void BKE_image_release_ibuf(Image *ima, void *lock)
2148 /* for getting image during threaded render / compositing, need to release */
2150 BLI_unlock_thread(LOCK_VIEWER); /* viewer image */
2153 RE_ReleaseResultImage(lock); /* render result */
2154 BLI_unlock_thread(LOCK_VIEWER); /* view image imbuf */
2158 ImBuf *BKE_image_get_ibuf(Image *ima, ImageUser *iuser)
2160 return BKE_image_acquire_ibuf(ima, iuser, NULL);
2163 void BKE_image_user_calc_frame(ImageUser *iuser, int cfra, int fieldnr)
2167 /* here (+fie_ima/2-1) makes sure that division happens correctly */
2168 len= (iuser->fie_ima*iuser->frames)/2;
2174 cfra= cfra - iuser->sfra+1;
2178 cfra= ( (cfra) % len );
2179 if(cfra < 0) cfra+= len;
2180 if(cfra==0) cfra= len;
2184 else if(cfra>len) cfra= len;
2186 /* convert current frame to current field */
2190 /* transform to images space */
2191 imanr= (cfra+iuser->fie_ima-2)/iuser->fie_ima;
2192 if(imanr>iuser->frames) imanr= iuser->frames;
2193 imanr+= iuser->offset;
2196 imanr= ( (imanr) % len );
2197 while(imanr < 0) imanr+= len;
2198 if(imanr==0) imanr= len;
2201 /* allows image users to handle redraws */
2202 if(iuser->flag & IMA_ANIM_ALWAYS)
2203 if(imanr!=iuser->framenr)
2204 iuser->flag |= IMA_ANIM_REFRESHED;
2206 iuser->framenr= imanr;
2207 if(iuser->ok==0) iuser->ok= 1;