Merged changes in the trunk up to revision 54110.
[blender.git] / source / blender / render / intern / source / pipeline.c
index 981f705e07cb9e569be40842b68883f7a5183e6e..866932632c27b06152feaaa82fa1208dcb758c5d 100644 (file)
 
 #include "MEM_guardedalloc.h"
 
+#include "BLI_math.h"
+#include "BLI_rect.h"
+#include "BLI_listbase.h"
+#include "BLI_string.h"
+#include "BLI_path_util.h"
+#include "BLI_fileops.h"
+#include "BLI_threads.h"
+#include "BLI_rand.h"
+#include "BLI_callbacks.h"
+
 #include "BKE_animsys.h"  /* <------ should this be here?, needed for sequencer update */
 #include "BKE_camera.h"
 #include "BKE_global.h"
 #include "BKE_image.h"
-#include "BKE_library.h"
 #include "BKE_main.h"
 #include "BKE_node.h"
 #include "BKE_pointcache.h"
 #include "BKE_sequencer.h"
 #include "BKE_writeavi.h"  /* <------ should be replaced once with generic movie module */
 
-#include "BLI_math.h"
-#include "BLI_rect.h"
-#include "BLI_listbase.h"
-#include "BLI_string.h"
-#include "BLI_path_util.h"
-#include "BLI_fileops.h"
-#include "BLI_rand.h"
-#include "BLI_callbacks.h"
-
 #include "PIL_time.h"
 #include "IMB_colormanagement.h"
 #include "IMB_imbuf.h"
 #include "RE_engine.h"
 #include "RE_pipeline.h"
 
-#include "FRS_freestyle.h"
+#ifdef WITH_FREESTYLE
+#  include "BKE_library.h"
+#  include "FRS_freestyle.h"
+#endif
 
 /* internal */
 #include "render_result.h"
@@ -247,6 +250,7 @@ Render *RE_GetRender(const char *name)
        return re;
 }
 
+
 /* if you want to know exactly what has been done */
 RenderResult *RE_AcquireResultRead(Render *re)
 {
@@ -337,7 +341,7 @@ void RE_ResultGet32(Render *re, unsigned int *rect)
        RenderResult rres;
        
        RE_AcquireResultImage(re, &rres);
-       render_result_rect_get_pixels(&rres, &re->r, rect, re->rectx, re->recty, &re->scene->view_settings, &re->scene->display_settings);
+       render_result_rect_get_pixels(&rres, rect, re->rectx, re->recty, &re->scene->view_settings, &re->scene->display_settings);
        RE_ReleaseResultImage(re);
 }
 
@@ -390,6 +394,9 @@ void RE_InitRenderCB(Render *re)
 /* only call this while you know it will remove the link too */
 void RE_FreeRender(Render *re)
 {
+       if (re->engine)
+               RE_engine_free(re->engine);
+
        BLI_rw_mutex_end(&re->resultmutex);
        
        free_renderdata_tables(re);
@@ -424,6 +431,22 @@ void RE_FreeAllRenderResults(void)
        }
 }
 
+void RE_FreePersistentData(void)
+{
+       Render *re;
+
+       /* render engines can be kept around for quick re-render, this clears all */
+       for (re = RenderGlobal.renderlist.first; re; re = re->next) {
+               if (re->engine) {
+                       /* if engine is currently rendering, just tag it to be freed when render is finished */
+                       if (!(re->engine->flag & RE_ENGINE_RENDERING))
+                               RE_engine_free(re->engine);
+
+                       re->engine = NULL;
+               }
+       }
+}
+
 /* ********* initialize state ******** */
 
 
@@ -451,7 +474,7 @@ void RE_InitState(Render *re, Render *source, RenderData *rd, SceneRenderLayer *
                re->recty = winy;
        }
        
-       if (re->rectx < 2 || re->recty < 2 || (BKE_imtype_is_movie(rd->im_format.imtype) &&
+       if (re->rectx < 1 || re->recty < 1 || (BKE_imtype_is_movie(rd->im_format.imtype) &&
                                               (re->rectx < 16 || re->recty < 16) ))
        {
                BKE_report(re->reports, RPT_ERROR, "Image too small");
@@ -502,13 +525,15 @@ void RE_InitState(Render *re, Render *source, RenderData *rd, SceneRenderLayer *
        }
                
        /* always call, checks for gamma, gamma tables and jitter too */
-       make_sample_tables(re); 
+       make_sample_tables(re);
        
        /* if preview render, we try to keep old result */
        BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
 
        if (re->r.scemode & R_PREVIEWBUTS) {
-               if (re->result && re->result->rectx == re->rectx && re->result->recty == re->recty) ;
+               if (re->result && re->result->rectx == re->rectx && re->result->recty == re->recty) {
+                       /* pass */
+               }
                else {
                        render_result_free(re->result);
                        re->result = NULL;
@@ -562,7 +587,7 @@ void RE_SetOrtho(Render *re, rctf *viewplane, float clipsta, float clipend)
                        re->viewplane.ymin, re->viewplane.ymax, re->clipsta, re->clipend);
 }
 
-void RE_SetView(Render *re, float mat[][4])
+void RE_SetView(Render *re, float mat[4][4])
 {
        /* re->ok flag? */
        copy_m4_m4(re->viewmat, mat);
@@ -635,7 +660,9 @@ static int render_display_draw_enabled(Render *re)
 static void *do_part_thread(void *pa_v)
 {
        RenderPart *pa = pa_v;
-       
+
+       pa->status = PART_STATUS_IN_PROGRESS;
+
        /* need to return nicely all parts on esc */
        if (R.test_break(R.tbh) == 0) {
                
@@ -657,23 +684,28 @@ static void *do_part_thread(void *pa_v)
                }
                else if (render_display_draw_enabled(&R)) {
                        /* on break, don't merge in result for preview renders, looks nicer */
-                       if (R.test_break(R.tbh) && (R.r.scemode & R_PREVIEWBUTS)) ;
-                       else render_result_merge(R.result, pa->result);
+                       if (R.test_break(R.tbh) && (R.r.scemode & R_PREVIEWBUTS)) {
+                               /* pass */
+                       }
+                       else {
+                               render_result_merge(R.result, pa->result);
+                       }
                }
        }
        
-       pa->ready = 1;
+       pa->status = PART_STATUS_READY;
        
        return NULL;
 }
 
 /* calculus for how much 1 pixel rendered should rotate the 3d geometry */
 /* is not that simple, needs to be corrected for errors of larger viewplane sizes */
-/* called in initrender.c, initparts() and convertblender.c, for speedvectors */
+/* called in initrender.c, RE_parts_init() and convertblender.c, for speedvectors */
 float panorama_pixel_rot(Render *re)
 {
        float psize, phi, xfac;
        float borderfac = (float)BLI_rcti_size_x(&re->disprect) / (float)re->winx;
+       int xparts = (re->rectx + re->partx - 1) / re->partx;
        
        /* size of 1 pixel mapped to viewplane coords */
        psize = BLI_rctf_size_x(&re->viewplane) / (float)re->winx;
@@ -681,7 +713,7 @@ float panorama_pixel_rot(Render *re)
        phi = atan(psize / re->clipsta);
        
        /* correction factor for viewplane shifting, first calculate how much the viewplane angle is */
-       xfac = borderfac * BLI_rctf_size_x(&re->viewplane) / (float)re->xparts;
+       xfac = borderfac * BLI_rctf_size_x(&re->viewplane) / (float)xparts;
        xfac = atan(0.5f * xfac / re->clipsta);
        /* and how much the same viewplane angle is wrapped */
        psize = 0.5f * phi * ((float)re->partx);
@@ -702,7 +734,7 @@ static RenderPart *find_next_pano_slice(Render *re, int *minx, rctf *viewplane)
        
        /* most left part of the non-rendering parts */
        for (pa = re->parts.first; pa; pa = pa->next) {
-               if (pa->ready == 0 && pa->nr == 0) {
+               if (pa->status == PART_STATUS_NONE && pa->nr == 0) {
                        if (pa->disprect.xmin < *minx) {
                                best = pa;
                                *minx = pa->disprect.xmin;
@@ -715,7 +747,7 @@ static RenderPart *find_next_pano_slice(Render *re, int *minx, rctf *viewplane)
 
                R.panodxp = (re->winx - (best->disprect.xmin + best->disprect.xmax) ) / 2;
                R.panodxv = (BLI_rctf_size_x(viewplane) * R.panodxp) / (float)(re->winx);
-               
+
                /* shift viewplane */
                R.viewplane.xmin = viewplane->xmin + R.panodxv;
                R.viewplane.xmax = viewplane->xmax + R.panodxv;
@@ -740,7 +772,7 @@ static RenderPart *find_next_part(Render *re, int minx)
        
        /* find center of rendered parts, image center counts for 1 too */
        for (pa = re->parts.first; pa; pa = pa->next) {
-               if (pa->ready) {
+               if (pa->status == PART_STATUS_READY) {
                        centx += BLI_rcti_cent_x(&pa->disprect);
                        centy += BLI_rcti_cent_y(&pa->disprect);
                        tot++;
@@ -751,7 +783,7 @@ static RenderPart *find_next_part(Render *re, int minx)
        
        /* closest of the non-rendering parts */
        for (pa = re->parts.first; pa; pa = pa->next) {
-               if (pa->ready == 0 && pa->nr == 0) {
+               if (pa->status == PART_STATUS_NONE && pa->nr == 0) {
                        long long int distx = centx - BLI_rcti_cent_x(&pa->disprect);
                        long long int disty = centy - BLI_rcti_cent_y(&pa->disprect);
                        distx = (long long int)sqrt(distx * distx + disty * disty);
@@ -808,10 +840,10 @@ static void threaded_tile_processor(Render *re)
        
        if (re->result == NULL)
                return;
-       
+
        /* warning; no return here without closing exr file */
        
-       initparts(re, TRUE);
+       RE_parts_init(re, TRUE);
 
        if (re->result->do_exr_tile)
                render_result_exr_file_begin(re);
@@ -859,7 +891,7 @@ static void threaded_tile_processor(Render *re)
                rendering = 0;
                hasdrawn = 0;
                for (pa = re->parts.first; pa; pa = pa->next) {
-                       if (pa->ready) {
+                       if (pa->status == PART_STATUS_READY) {
                                
                                BLI_remove_thread(&threads, pa);
                                
@@ -903,7 +935,7 @@ static void threaded_tile_processor(Render *re)
        g_break = 0;
        
        BLI_end_threads(&threads);
-       freeparts(re);
+       RE_parts_free(re);
        re->viewplane = viewplane; /* restore viewplane, modified by pano render */
 }
 
@@ -915,19 +947,29 @@ void RE_TileProcessor(Render *re)
 
 /* ************  This part uses API, for rendering Blender scenes ********** */
 
+#ifdef WITH_FREESTYLE
 static void add_freestyle(Render *re);
+#endif
 
 static void do_render_3d(Render *re)
 {
+       float cfra;
+       int cfra_backup;
+
        /* try external */
        if (RE_engine_render(re, 0))
                return;
 
        /* internal */
+       RE_parts_clamp(re);
        
-//     re->cfra= cfra; /* <- unused! */
-       re->scene->r.subframe = re->mblur_offs + re->field_offs;
-       
+       /* add motion blur and fields offset to frames */
+       cfra_backup = re->scene->r.cfra;
+
+       cfra = re->scene->r.cfra + re->mblur_offs + re->field_offs;
+       re->scene->r.cfra = floorf(cfra);
+       re->scene->r.subframe = cfra - floorf(cfra);
+
        /* lock drawing in UI during data phase */
        if (re->draw_lock)
                re->draw_lock(re->dlh, 1);
@@ -949,14 +991,17 @@ static void do_render_3d(Render *re)
                if (!re->test_break(re->tbh))
                        add_halo_flare(re);
        
+#ifdef WITH_FREESTYLE
        /* Freestyle  */
        if( re->r.mode & R_EDGE_FRS)
                if(!re->test_break(re->tbh))
                        add_freestyle(re);
+#endif
                
        /* free all render verts etc */
        RE_Database_Free(re);
        
+       re->scene->r.cfra = cfra_backup;
        re->scene->r.subframe = 0.f;
 }
 
@@ -988,7 +1033,7 @@ static void addblur_rect_key(RenderResult *rr, float *rectf, float *rectf1, floa
                                        rf[1] = mfac * rf[1] + blurfac * rf1[1];
                                        rf[2] = mfac * rf[2] + blurfac * rf1[2];
                                        rf[3] = mfac * rf[3] + blurfac * rf1[3];
-                               }                               
+                               }
                        }
                }
                rectf += stride;
@@ -1065,7 +1110,7 @@ static void do_render_blur_3d(Render *re)
                
                blurfac = 1.0f / (float)(re->r.mblur_samples - blur);
                
-               merge_renderresult_blur(rres, re->result, blurfac, re->r.alphamode & R_ALPHAKEY);
+               merge_renderresult_blur(rres, re->result, blurfac, FALSE);
                if (re->test_break(re->tbh)) break;
        }
        
@@ -1080,7 +1125,7 @@ static void do_render_blur_3d(Render *re)
        
        /* weak... the display callback wants an active renderlayer pointer... */
        re->result->renlay = render_get_active_layer(re, re->result);
-       re->display_draw(re->ddh, re->result, NULL);    
+       re->display_draw(re->ddh, re->result, NULL);
 }
 
 
@@ -1210,7 +1255,7 @@ static void do_render_fields_blur_3d(Render *re)
        Object *camera = RE_GetCamera(re);
        /* also check for camera here */
        if (camera == NULL) {
-               printf("ERROR: Cannot render, no camera\n");
+               BKE_report(re->reports, RPT_ERROR, "Cannot render, no camera");
                G.is_break = TRUE;
                return;
        }
@@ -1392,6 +1437,7 @@ static void render_composit_stats(void *UNUSED(arg), char *str)
        R.i.infostr = NULL;
 }
 
+#ifdef WITH_FREESTYLE
 /* invokes Freestyle stroke rendering */
 static void add_freestyle(Render *re)
 {
@@ -1458,7 +1504,7 @@ static void free_all_freestyle_renders(Scene *scene)
                BLI_freelistN( &re1->freestyle_renders );
        }
 }
-
+#endif
 
 /* reads all buffers, calls optional composite, merges in first result->rectf */
 static void do_merge_fullsample(Render *re, bNodeTree *ntree)
@@ -1500,8 +1546,10 @@ static void do_merge_fullsample(Render *re, bNodeTree *ntree)
                                        if (sample) {
                                                BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
                                                render_result_exr_file_read(re1, sample);
+#ifdef WITH_FREESTYLE
                                                if( re1->r.mode & R_EDGE_FRS)
                                                        composite_freestyle_renders(re1, sample);
+#endif
                                                BLI_rw_mutex_unlock(&re->resultmutex);
                                        }
                                        ntreeCompositTagRender(re1->scene); /* ensure node gets exec to put buffers on stack */
@@ -1592,6 +1640,7 @@ void RE_MergeFullSample(Render *re, Main *bmain, Scene *sce, bNodeTree *ntree)
        
        re->main = bmain;
        re->scene = sce;
+       re->scene_color_manage = BKE_scene_check_color_management_enabled(sce);
        
        /* first call RE_ReadRenderResult on every renderlayer scene. this creates Render structs */
        
@@ -1639,7 +1688,7 @@ static void do_render_composite_fields_blur_3d(Render *re)
                ntreeFreeCache(ntree);
                
                do_render_fields_blur_3d(re);
-       } 
+       }
        else {
                /* ensure new result gets added, like for regular renders */
                BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
@@ -1703,7 +1752,9 @@ static void do_render_composite_fields_blur_3d(Render *re)
                        do_merge_fullsample(re, NULL);
        }
 
+#ifdef WITH_FREESTYLE
        free_all_freestyle_renders(re->scene);
+#endif
 
        /* weak... the display callback wants an active renderlayer pointer... */
        re->result->renlay = render_get_active_layer(re, re->result);
@@ -1828,6 +1879,8 @@ static void do_render_all_options(Render *re)
        /* ensure no images are in memory from previous animated sequences */
        BKE_image_all_free_anim_ibufs(re->r.cfra);
 
+       re->pool = BKE_image_pool_new();
+
        if (RE_engine_render(re, 1)) {
                /* in this case external render overrides all */
        }
@@ -1852,6 +1905,9 @@ static void do_render_all_options(Render *re)
                renderresult_stampinfo(re);
                re->display_draw(re->ddh, re->result, NULL);
        }
+
+       BKE_image_pool_free(re->pool);
+       re->pool = NULL;
 }
 
 static int check_valid_camera(Scene *scene, Object *camera_override)
@@ -1946,7 +2002,7 @@ int RE_is_rendering_allowed(Scene *scene, Object *camera_override, ReportList *r
                if (scene->r.border.xmax <= scene->r.border.xmin ||
                    scene->r.border.ymax <= scene->r.border.ymin)
                {
-                       BKE_report(reports, RPT_ERROR, "No border area selected.");
+                       BKE_report(reports, RPT_ERROR, "No border area selected");
                        return 0;
                }
        }
@@ -1957,13 +2013,13 @@ int RE_is_rendering_allowed(Scene *scene, Object *camera_override, ReportList *r
                render_result_exr_file_path(scene, "", 0, str);
                
                if (BLI_file_is_writable(str) == 0) {
-                       BKE_report(reports, RPT_ERROR, "Can not save render buffers, check the temp default path");
+                       BKE_report(reports, RPT_ERROR, "Cannot save render buffers, check the temp default path");
                        return 0;
                }
                
                /* no fullsample and edge */
                if ((scene->r.scemode & R_FULL_SAMPLE) && (scene->r.mode & R_EDGE)) {
-                       BKE_report(reports, RPT_ERROR, "Full Sample doesn't support Edge Enhance");
+                       BKE_report(reports, RPT_ERROR, "Full sample does not support edge enhance");
                        return 0;
                }
                
@@ -1974,18 +2030,18 @@ int RE_is_rendering_allowed(Scene *scene, Object *camera_override, ReportList *r
        if (scene->r.scemode & R_DOCOMP) {
                if (scene->use_nodes) {
                        if (!scene->nodetree) {
-                               BKE_report(reports, RPT_ERROR, "No Nodetree in Scene");
+                               BKE_report(reports, RPT_ERROR, "No node tree in scene");
                                return 0;
                        }
                        
                        if (!check_composite_output(scene)) {
-                               BKE_report(reports, RPT_ERROR, "No Render Output Node in Scene");
+                               BKE_report(reports, RPT_ERROR, "No render output node in scene");
                                return 0;
                        }
                        
                        if (scene->r.scemode & R_FULL_SAMPLE) {
                                if (composite_needs_render(scene, 0) == 0) {
-                                       BKE_report(reports, RPT_ERROR, "Full Sample AA not supported without 3d rendering");
+                                       BKE_report(reports, RPT_ERROR, "Full sample AA not supported without 3D rendering");
                                        return 0;
                                }
                        }
@@ -2004,7 +2060,7 @@ int RE_is_rendering_allowed(Scene *scene, Object *camera_override, ReportList *r
        /* forbidden combinations */
        if (scene->r.mode & R_PANORAMA) {
                if (scene->r.mode & R_ORTHO) {
-                       BKE_report(reports, RPT_ERROR, "No Ortho render possible for Panorama");
+                       BKE_report(reports, RPT_ERROR, "No ortho render possible for panorama");
                        return 0;
                }
        }
@@ -2020,7 +2076,7 @@ int RE_is_rendering_allowed(Scene *scene, Object *camera_override, ReportList *r
                if (!(srl->layflag & SCE_LAY_DISABLE))
                        break;
        if (srl == NULL) {
-               BKE_report(reports, RPT_ERROR, "All RenderLayers are disabled");
+               BKE_report(reports, RPT_ERROR, "All render layers are disabled");
                return 0;
        }
 
@@ -2089,6 +2145,7 @@ static int render_initialize_from_main(Render *re, Main *bmain, Scene *scene, Sc
        
        re->main = bmain;
        re->scene = scene;
+       re->scene_color_manage = BKE_scene_check_color_management_enabled(scene);
        re->camera_override = camera_override;
        re->lay = lay;
        
@@ -2151,7 +2208,7 @@ void RE_BlenderFrame(Render *re, Main *bmain, Scene *scene, SceneRenderLayer *sr
                BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_PRE);
 
                do_render_all_options(re);
-               
+
                if (write_still && !G.is_break) {
                        if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
                                /* operator checks this but in case its called from elsewhere */
@@ -2159,7 +2216,7 @@ void RE_BlenderFrame(Render *re, Main *bmain, Scene *scene, SceneRenderLayer *sr
                        }
                        else {
                                char name[FILE_MAX];
-                               BKE_makepicstring(name, scene->r.pic, bmain->name, scene->r.cfra, scene->r.im_format.imtype, scene->r.scemode & R_EXTENSION, FALSE);
+                               BKE_makepicstring(name, scene->r.pic, bmain->name, scene->r.cfra, &scene->r.im_format, scene->r.scemode & R_EXTENSION, FALSE);
 
                                /* reports only used for Movie */
                                do_write_image_or_movie(re, bmain, scene, NULL, name);
@@ -2175,6 +2232,7 @@ void RE_BlenderFrame(Render *re, Main *bmain, Scene *scene, SceneRenderLayer *sr
        G.is_rendering = FALSE;
 }
 
+#ifdef WITH_FREESTYLE
 void RE_RenderFreestyleStrokes(Render *re, Main *bmain, Scene *scene)
 {
        re->result_ok= 0;
@@ -2183,6 +2241,7 @@ void RE_RenderFreestyleStrokes(Render *re, Main *bmain, Scene *scene)
        }
        re->result_ok= 1;
 }
+#endif
 
 static int do_write_image_or_movie(Render *re, Main *bmain, Scene *scene, bMovieHandle *mh, const char *name_override)
 {
@@ -2201,6 +2260,7 @@ static int do_write_image_or_movie(Render *re, Main *bmain, Scene *scene, bMovie
                /* note; the way it gets 32 bits rects is weak... */
                if (ibuf->rect == NULL) {
                        ibuf->rect = MEM_mapallocN(sizeof(int) * rres.rectx * rres.recty, "temp 32 bits rect");
+                       ibuf->mall |= IB_rect;
                        RE_ResultGet32(re, ibuf->rect);
                        do_free = TRUE;
                }
@@ -2214,18 +2274,19 @@ static int do_write_image_or_movie(Render *re, Main *bmain, Scene *scene, bMovie
                if (do_free) {
                        MEM_freeN(ibuf->rect);
                        ibuf->rect = NULL;
+                       ibuf->mall &= ~IB_rect;
                }
 
                /* imbuf knows which rects are not part of ibuf */
                IMB_freeImBuf(ibuf);
 
                printf("Append frame %d", scene->r.cfra);
-       } 
+       }
        else {
                if (name_override)
                        BLI_strncpy(name, name_override, sizeof(name));
                else
-                       BKE_makepicstring(name, scene->r.pic, bmain->name, scene->r.cfra, scene->r.im_format.imtype, scene->r.scemode & R_EXTENSION, TRUE);
+                       BKE_makepicstring(name, scene->r.pic, bmain->name, scene->r.cfra, &scene->r.im_format, scene->r.scemode & R_EXTENSION, TRUE);
                
                if (re->r.im_format.imtype == R_IMF_IMTYPE_MULTILAYER) {
                        if (re->result) {
@@ -2253,7 +2314,7 @@ static int do_write_image_or_movie(Render *re, Main *bmain, Scene *scene, bMovie
 
                                if (BLI_testextensie(name, ".exr"))
                                        name[strlen(name) - 4] = 0;
-                               BKE_add_image_extension(name, R_IMF_IMTYPE_JPEG90);
+                               BKE_add_image_extension(name, &imf);
                                ibuf->planes = 24;
 
                                IMB_colormanagement_imbuf_for_write(ibuf, TRUE, FALSE, &scene->view_settings,
@@ -2358,7 +2419,7 @@ void RE_BlenderAnim(Render *re, Main *bmain, Scene *scene, Object *camera_overri
                        /* Touch/NoOverwrite options are only valid for image's */
                        if (BKE_imtype_is_movie(scene->r.im_format.imtype) == 0) {
                                if (scene->r.mode & (R_NO_OVERWRITE | R_TOUCH))
-                                       BKE_makepicstring(name, scene->r.pic, bmain->name, scene->r.cfra, scene->r.im_format.imtype, scene->r.scemode & R_EXTENSION, TRUE);
+                                       BKE_makepicstring(name, scene->r.pic, bmain->name, scene->r.cfra, &scene->r.im_format, scene->r.scemode & R_EXTENSION, TRUE);
 
                                if (scene->r.mode & R_NO_OVERWRITE && BLI_exists(name)) {
                                        printf("skipping existing frame \"%s\"\n", name);
@@ -2432,14 +2493,20 @@ void RE_PreviewRender(Render *re, Main *bmain, Scene *sce)
 
        RE_InitState(re, NULL, &sce->r, NULL, winx, winy, NULL);
 
+       re->pool = BKE_image_pool_new();
+
        re->main = bmain;
        re->scene = sce;
+       re->scene_color_manage = BKE_scene_check_color_management_enabled(sce);
        re->lay = sce->lay;
 
        camera = RE_GetCamera(re);
        RE_SetCamera(re, camera);
 
        do_render_3d(re);
+
+       BKE_image_pool_free(re->pool);
+       re->pool = NULL;
 }
 
 /* note; repeated win/disprect calc... solve that nicer, also in compo */
@@ -2478,6 +2545,7 @@ int RE_ReadRenderResult(Scene *scene, Scene *scenode)
                re = RE_NewRender(scene->id.name);
        RE_InitState(re, NULL, &scene->r, NULL, winx, winy, &disprect);
        re->scene = scene;
+       re->scene_color_manage = BKE_scene_check_color_management_enabled(scene);
        
        BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
        success = render_result_exr_file_read(re, 0);
@@ -2538,25 +2606,25 @@ void RE_layer_load_from_file(RenderLayer *layer, ReportList *reports, const char
                                        IMB_freeImBuf(ibuf_clip);
                                }
                                else {
-                                       BKE_reportf(reports, RPT_ERROR, "RE_result_rect_from_file: failed to allocate clip buffer '%s'\n", filename);
+                                       BKE_reportf(reports, RPT_ERROR, "RE_result_rect_from_file: failed to allocate clip buffer '%s'", filename);
                                }
                        }
                        else {
-                               BKE_reportf(reports, RPT_ERROR, "RE_result_rect_from_file: incorrect dimensions for partial copy '%s'\n", filename);
+                               BKE_reportf(reports, RPT_ERROR, "RE_result_rect_from_file: incorrect dimensions for partial copy '%s'", filename);
                        }
                }
 
                IMB_freeImBuf(ibuf);
        }
        else {
-               BKE_reportf(reports, RPT_ERROR, "RE_result_rect_from_file: failed to load '%s'\n", filename);
+               BKE_reportf(reports, RPT_ERROR, "RE_result_rect_from_file: failed to load '%s'", filename);
        }
 }
 
 void RE_result_load_from_file(RenderResult *result, ReportList *reports, const char *filename)
 {
        if (!render_result_exr_file_read_path(result, NULL, filename)) {
-               BKE_reportf(reports, RPT_ERROR, "RE_result_rect_from_file: failed to load '%s'\n", filename);
+               BKE_reportf(reports, RPT_ERROR, "RE_result_rect_from_file: failed to load '%s'", filename);
                return;
        }
 }
@@ -2584,8 +2652,8 @@ int RE_WriteEnvmapResult(struct ReportList *reports, Scene *scene, EnvMap *env,
 
        if (env->type == ENV_CUBE) {
                for (i = 0; i < 12; i += 2) {
-                       maxX = MAX2(maxX, layout[i] + 1);
-                       maxY = MAX2(maxY, layout[i + 1] + 1);
+                       maxX = max_ii(maxX, (int)layout[i] + 1);
+                       maxY = max_ii(maxY, (int)layout[i + 1] + 1);
                }
 
                ibuf = IMB_allocImBuf(maxX * dx, maxY * dx, 24, IB_rectfloat);
@@ -2617,7 +2685,7 @@ int RE_WriteEnvmapResult(struct ReportList *reports, Scene *scene, EnvMap *env,
                return TRUE;
        }
        else {
-               BKE_report(reports, RPT_ERROR, "Error writing environment map.");
+               BKE_report(reports, RPT_ERROR, "Error writing environment map");
                return FALSE;
        }
 }