Merging r50049 through r50076 from trunk into soc-2011-tomato
[blender.git] / source / blender / render / intern / source / pipeline.c
index 01b1f00ff9808710c37a3d8b2e0824884fcc1c13..17a7bed4498127b36d858521beceb4e4e3e871d9 100644 (file)
@@ -68,6 +68,7 @@
 #include "BLI_callbacks.h"
 
 #include "PIL_time.h"
+#include "IMB_colormanagement.h"
 #include "IMB_imbuf.h"
 #include "IMB_imbuf_types.h"
 
@@ -641,7 +642,7 @@ static void *do_part_thread(void *pa_v)
                if (!R.sss_points && (R.r.scemode & R_FULL_SAMPLE))
                        pa->result = render_result_new_full_sample(&R, &pa->fullresult, &pa->disprect, pa->crop, RR_USE_MEM);
                else
-                       pa->result = render_result_new(&R, &pa->disprect, pa->crop, RR_USE_MEM);
+                       pa->result = render_result_new(&R, &pa->disprect, pa->crop, RR_USE_MEM, RR_ALL_LAYERS);
 
                if (R.sss_points)
                        zbufshade_sss_tile(pa);
@@ -651,7 +652,7 @@ static void *do_part_thread(void *pa_v)
                        zbufshade_tile(pa);
                
                /* merge too on break! */
-               if (R.result->exrhandle) {
+               if (R.result->do_exr_tile) {
                        render_result_exr_file_merge(R.result, pa->result);
                }
                else if (render_display_draw_enabled(&R)) {
@@ -795,12 +796,12 @@ static void threaded_tile_processor(Render *re)
                render_result_free(re->result);
        
                if (re->sss_points && render_display_draw_enabled(re))
-                       re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM);
+                       re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS);
                else if (re->r.scemode & R_FULL_SAMPLE)
                        re->result = render_result_new_full_sample(re, &re->fullresult, &re->disprect, 0, RR_USE_EXR);
                else
                        re->result = render_result_new(re, &re->disprect, 0,
-                                                      (re->r.scemode & R_EXR_TILE_FILE) ? RR_USE_EXR : RR_USE_MEM);
+                                                      (re->r.scemode & R_EXR_TILE_FILE) ? RR_USE_EXR : RR_USE_MEM, RR_ALL_LAYERS);
        }
 
        BLI_rw_mutex_unlock(&re->resultmutex);
@@ -810,9 +811,9 @@ static void threaded_tile_processor(Render *re)
        
        /* warning; no return here without closing exr file */
        
-       initparts(re);
+       initparts(re, TRUE);
 
-       if (re->result->exrhandle)
+       if (re->result->do_exr_tile)
                render_result_exr_file_begin(re);
        
        BLI_init_threads(&threads, do_part_thread, re->r.threads);
@@ -892,7 +893,7 @@ static void threaded_tile_processor(Render *re)
                
        }
        
-       if (re->result->exrhandle) {
+       if (re->result->do_exr_tile) {
                BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
                render_result_exr_file_end(re);
                BLI_rw_mutex_unlock(&re->resultmutex);
@@ -1045,7 +1046,7 @@ static void do_render_blur_3d(Render *re)
        int blur = re->r.mblur_samples;
        
        /* create accumulation render result */
-       rres = render_result_new(re, &re->disprect, 0, RR_USE_MEM);
+       rres = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS);
        
        /* do the blur steps */
        while (blur--) {
@@ -1170,7 +1171,7 @@ static void do_render_fields_3d(Render *re)
        re->disprect.ymax *= 2;
 
        BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
-       re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM);
+       re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS);
 
        if (rr2) {
                if (re->r.mode & R_ODDFIELD)
@@ -1233,7 +1234,7 @@ static void do_render_fields_blur_3d(Render *re)
                                re->rectx = re->winx;
                                re->recty = re->winy;
                                
-                               rres = render_result_new(re, &re->disprect, 0, RR_USE_MEM);
+                               rres = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS);
                                
                                render_result_merge(rres, re->result);
                                render_result_free(re->result);
@@ -1553,7 +1554,7 @@ static void do_render_composite_fields_blur_3d(Render *re)
                BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
                
                render_result_free(re->result);
-               re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM);
+               re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS);
 
                BLI_rw_mutex_unlock(&re->resultmutex);
                
@@ -1689,7 +1690,7 @@ static void do_render_seq(Render *re)
                if (recurs_depth == 0) { /* with nested scenes, only free on toplevel... */
                        Editing *ed = re->scene->ed;
                        if (ed)
-                               BKE_sequencer_free_imbuf(re->scene, &ed->seqbase, TRUE, TRUE);
+                               BKE_sequencer_free_imbuf(re->scene, &ed->seqbase, TRUE);
                }
                IMB_freeImBuf(ibuf);
        }
@@ -1848,7 +1849,7 @@ int RE_is_rendering_allowed(Scene *scene, Object *camera_override, ReportList *r
        if (scene->r.scemode & (R_EXR_TILE_FILE | R_FULL_SAMPLE)) {
                char str[FILE_MAX];
                
-               render_result_exr_file_path(scene, 0, str);
+               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");
@@ -1932,7 +1933,7 @@ static void validate_render_settings(Render *re)
 
        if (RE_engine_is_external(re)) {
                /* not supported yet */
-               re->r.scemode &= ~(R_EXR_TILE_FILE | R_FULL_SAMPLE);
+               re->r.scemode &= ~(R_FULL_SAMPLE);
                re->r.mode &= ~(R_FIELDS | R_MBLUR);
        }
 }
@@ -2069,6 +2070,15 @@ void RE_BlenderFrame(Render *re, Main *bmain, Scene *scene, SceneRenderLayer *sr
        G.is_rendering = FALSE;
 }
 
+static void colormanage_image_for_write(Scene *scene, ImBuf *ibuf)
+{
+       IMB_display_buffer_to_imbuf_rect(ibuf, &scene->r.im_format.view_settings,
+                                        &scene->r.im_format.display_settings);
+
+       if (ibuf)
+               imb_freerectfloatImBuf(ibuf);
+}
+
 static int do_write_image_or_movie(Render *re, Main *bmain, Scene *scene, bMovieHandle *mh, const char *name_override)
 {
        char name[FILE_MAX];
@@ -2081,19 +2091,27 @@ static int do_write_image_or_movie(Render *re, Main *bmain, Scene *scene, bMovie
        /* write movie or image */
        if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
                int do_free = FALSE;
-               unsigned int *rect32 = (unsigned int *)rres.rect32;
+               ImBuf *ibuf = render_result_rect_to_ibuf(&rres, &scene->r);
+
                /* note; the way it gets 32 bits rects is weak... */
-               if (rres.rect32 == NULL) {
-                       rect32 = MEM_mapallocN(sizeof(int) * rres.rectx * rres.recty, "temp 32 bits rect");
-                       RE_ResultGet32(re, rect32);
+               if (ibuf->rect == NULL) {
+                       ibuf->rect = MEM_mapallocN(sizeof(int) * rres.rectx * rres.recty, "temp 32 bits rect");
+                       RE_ResultGet32(re, ibuf->rect);
                        do_free = TRUE;
                }
 
-               ok = mh->append_movie(&re->r, scene->r.sfra, scene->r.cfra, (int *)rect32,
-                                     rres.rectx, rres.recty, re->reports);
+               colormanage_image_for_write(scene, ibuf);
+
+               ok = mh->append_movie(&re->r, scene->r.sfra, scene->r.cfra, (int *) ibuf->rect,
+                                     ibuf->x, ibuf->y, re->reports);
                if (do_free) {
-                       MEM_freeN(rect32);
+                       MEM_freeN(ibuf->rect);
+                       ibuf->rect = NULL;
                }
+
+               /* imbuf knows which rects are not part of ibuf */
+               IMB_freeImBuf(ibuf);
+
                printf("Append frame %d", scene->r.cfra);
        } 
        else {
@@ -2110,6 +2128,12 @@ static int do_write_image_or_movie(Render *re, Main *bmain, Scene *scene, bMovie
                }
                else {
                        ImBuf *ibuf = render_result_rect_to_ibuf(&rres, &scene->r);
+                       int do_colormanagement;
+
+                       do_colormanagement = !BKE_imtype_supports_float(scene->r.im_format.imtype);
+
+                       if (do_colormanagement)
+                               colormanage_image_for_write(scene, ibuf);
 
                        ok = BKE_imbuf_write_stamp(scene, camera, ibuf, name, &scene->r.im_format);
                        
@@ -2127,6 +2151,9 @@ static int do_write_image_or_movie(Render *re, Main *bmain, Scene *scene, bMovie
                                        name[strlen(name) - 4] = 0;
                                BKE_add_image_extension(name, R_IMF_IMTYPE_JPEG90);
                                ibuf->planes = 24;
+
+                               colormanage_image_for_write(scene, ibuf);
+
                                BKE_imbuf_write_stamp(scene, camera, ibuf, name, &imf);
                                printf("\nSaved: %s", name);
                        }
@@ -2422,7 +2449,7 @@ void RE_layer_load_from_file(RenderLayer *layer, ReportList *reports, const char
 
 void RE_result_load_from_file(RenderResult *result, ReportList *reports, const char *filename)
 {
-       if (!render_result_exr_file_read_path(result, 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);
                return;
        }