Made image buffer threaded processor generic function,
authorSergey Sharybin <sergey.vfx@gmail.com>
Tue, 7 Aug 2012 16:47:46 +0000 (16:47 +0000)
committerSergey Sharybin <sergey.vfx@gmail.com>
Tue, 7 Aug 2012 16:47:46 +0000 (16:47 +0000)
so color management could use the same routines.

Should be no functional changes.

source/blender/blenkernel/intern/sequencer.c
source/blender/imbuf/IMB_imbuf.h
source/blender/imbuf/intern/imageprocess.c

index 869b8a0df6a8977986996966e97f1630e442e516..02ae9566eed597c71fc96183d99d5532f1950e34 100644 (file)
@@ -1646,6 +1646,12 @@ static void color_balance_float_float(Sequence *seq, float *rect_float, int widt
        }
 }
 
+typedef struct ColorBalanceInitData {
+       Sequence *seq;
+       ImBuf *ibuf;
+       float mul;
+} ColorBalanceInitData;
+
 typedef struct ColorBalanceThread {
        Sequence *seq;
        float mul;
@@ -1656,6 +1662,28 @@ typedef struct ColorBalanceThread {
        float *rect_float;
 } ColorBalanceThread;
 
+static void color_balance_init_handle(void *handle_v, int start_line, int tot_line, void *init_data_v)
+{
+       ColorBalanceThread *handle = (ColorBalanceThread *) handle_v;
+       ColorBalanceInitData *init_data = (ColorBalanceInitData *) init_data_v;
+       ImBuf *ibuf = init_data->ibuf;
+
+       int offset = 4 * start_line * ibuf->x;
+
+       memset(handle, 0, sizeof(ColorBalanceThread));
+
+       handle->seq = init_data->seq;
+       handle->mul = init_data->mul;
+       handle->width = ibuf->x;
+       handle->height = tot_line;
+
+       if (ibuf->rect)
+               handle->rect = (unsigned char *) ibuf->rect + offset;
+
+       if (ibuf->rect_float)
+               handle->rect_float = ibuf->rect_float + offset;
+}
+
 static void *color_balance_do_thread(void *thread_data_v)
 {
        ColorBalanceThread *thread_data = (ColorBalanceThread *) thread_data_v;
@@ -1680,62 +1708,36 @@ static void *color_balance_do_thread(void *thread_data_v)
 
 static void color_balance(Sequence *seq, ImBuf *ibuf, float mul)
 {
-       int i, tot_thread = BLI_system_thread_count();
-       int start_line, tot_line;
-       ListBase threads;
-       ColorBalanceThread handles[BLENDER_MAX_THREADS];
+       if (!ibuf->rect_float && seq->flag & SEQ_MAKE_FLOAT)
+               imb_addrectfloatImBuf(ibuf);
 
-       if (!BLI_thread_is_main()) {
+       if (BLI_thread_is_main()) {
                /* color balance could have been called from prefetching job which
                 * is already multithreaded, so doing threading here makes no sense
                 */
-               tot_thread = 1;
-       }
+               ColorBalanceInitData init_data;
 
-       if (!ibuf->rect_float && seq->flag & SEQ_MAKE_FLOAT)
-               imb_addrectfloatImBuf(ibuf);
-
-       if (tot_thread > 1)
-               BLI_init_threads(&threads, color_balance_do_thread, tot_thread);
-
-       start_line = 0;
-       tot_line = ((float)(ibuf->y / tot_thread)) + 0.5f;
-
-       for (i = 0; i < tot_thread; i++) {
-               int offset;
+               init_data.seq = seq;
+               init_data.ibuf = ibuf;
+               init_data.mul = mul;
 
-               handles[i].seq = seq;
-               handles[i].mul = mul;
-               handles[i].width = ibuf->x;
+               IMB_processor_apply_threaded(ibuf->y, sizeof(ColorBalanceThread), &init_data,
+                                        color_balance_init_handle, color_balance_do_thread);
 
-               if (i < tot_thread - 1)
-                       handles[i].height = tot_line;
-               else
-                       handles[i].height = ibuf->y - start_line;
-
-               offset = 4 * start_line * ibuf->x;
-
-               if (ibuf->rect)
-                       handles[i].rect = (unsigned char *)ibuf->rect + offset;
-               else
-                       handles[i].rect = NULL;
+       }
+       else {
+               ColorBalanceThread handle;
 
-               if (ibuf->rect_float)
-                       handles[i].rect_float = ibuf->rect_float + offset;
-               else
-                       handles[i].rect_float = NULL;
+               handle.seq = seq;
+               handle.mul = mul;
+               handle.width = ibuf->x;
+               handle.height = ibuf->y;
+               handle.rect = (unsigned char *)ibuf->rect;
+               handle.rect_float = ibuf->rect_float;
 
-               if (tot_thread > 1)
-                       BLI_insert_thread(&threads, &handles[i]);
+               color_balance_do_thread(&handle);
+}      }
 
-               start_line += tot_line;
-       }
-
-       if (tot_thread > 1)
-               BLI_end_threads(&threads);
-       else
-               color_balance_do_thread(handles);
-}
 
 /*
  *  input preprocessing for SEQ_TYPE_IMAGE, SEQ_TYPE_MOVIE, SEQ_TYPE_MOVIECLIP and SEQ_TYPE_SCENE
index 1a68d3c1f09ee1183a65280e3b719160e2f1e24f..8d7c4bd55bf36c31b5ff6b1421b724ba65f5d828 100644 (file)
@@ -498,5 +498,11 @@ void imb_freemipmapImBuf(struct ImBuf *ibuf);
 short imb_addtilesImBuf(struct ImBuf *ibuf);
 void imb_freetilesImBuf(struct ImBuf *ibuf);
 
+/* threaded processors */
+void IMB_processor_apply_threaded(int buffer_lines, int handle_size, void *init_customdata,
+                                  void (init_handle) (void *handle, int start_line, int tot_line,
+                                                      void *customdata),
+                                  void *(do_thread) (void *));
+
 #endif
 
index e0483f70e728605b4b6007f78fb09d6e4ab38532..3fd06a7c34d87203414c887cf034c117600ad1f9 100644 (file)
 
 #include <stdlib.h>
 
+#include "MEM_guardedalloc.h"
+
 #include "BLI_utildefines.h"
+#include "BLI_threads.h"
+#include "BLI_listbase.h"
 
 #include "IMB_imbuf_types.h"
 #include "IMB_imbuf.h"
@@ -446,3 +450,49 @@ void neareast_interpolation(ImBuf *in, ImBuf *out, float x, float y, int xout, i
        
        neareast_interpolation_color(in, outI, outF, x, y);
 }
+
+/*********************** Threaded image processing *************************/
+
+void IMB_processor_apply_threaded(int buffer_lines, int handle_size, void *init_customdata,
+                                  void (init_handle) (void *handle, int start_line, int tot_line,
+                                                      void *customdata),
+                                  void *(do_thread) (void *))
+{
+       void *handles;
+       ListBase threads;
+
+       int i, tot_thread = BLI_system_thread_count();
+       int start_line, tot_line;
+
+       handles = MEM_callocN(handle_size * tot_thread, "processor apply threaded handles");
+
+       if (tot_thread > 1)
+               BLI_init_threads(&threads, do_thread, tot_thread);
+
+       start_line = 0;
+       tot_line = ((float)(buffer_lines / tot_thread)) + 0.5f;
+
+       for (i = 0; i < tot_thread; i++) {
+               int cur_tot_line;
+               void *handle = ((char *) handles) + handle_size * i;
+
+               if (i < tot_thread - 1)
+                       cur_tot_line = tot_line;
+               else
+                       cur_tot_line = buffer_lines - start_line;
+
+               init_handle(handle, start_line, cur_tot_line, init_customdata);
+
+               if (tot_thread > 1)
+                       BLI_insert_thread(&threads, handle);
+
+               start_line += tot_line;
+       }
+
+       if (tot_thread > 1)
+               BLI_end_threads(&threads);
+       else
+               do_thread(handles);
+
+       MEM_freeN(handles);
+}