Add option to keep I and mI arrays and "neighbor pixle weights" structure
authorAndreas Eversberg <jolly@eversberg.eu>
Mon, 18 May 2015 16:29:10 +0000 (18:29 +0200)
committerAndreas Eversberg <jolly@eversberg.eu>
Mon, 18 May 2015 16:29:10 +0000 (18:29 +0200)
This prepares real time colorization, by reusing existing structure.

Also an option for quick and dirty colorization is added.

gui/colorize.c
lib/colorize.c
lib/colorize.h
src/colorize.c
src/process.c

index de33c8d..bf3bf59 100644 (file)
@@ -207,7 +207,7 @@ void colorize_image(void)
        cI = NULL;
 
        /* render u and v change */
-       rc = colorize(I, mI, NULL, NULL, 5, 1, 0, feat_names);
+       rc = colorize(I, mI, NULL, NULL, 5, 1, 0, 0, feat_names, NULL);
        if (rc < 0) {
                printerror("No memory! Use smaller image or add more memory.");
                goto error;
index 88ab413..96bd385 100644 (file)
@@ -19,7 +19,7 @@
  *     second dimension = height
  *     fourth dimension = time
  */
-int colorize_prepare(struct colorize *col)
+int colorize_prepare(struct colorize *col, int alloc)
 {
        int dimensions[4];
        int ww, hh, kk, last_kk, d, temp, z;
@@ -46,34 +46,40 @@ int colorize_prepare(struct colorize *col)
                temp=(temp+1)>>1;
        ASSERT(col->max_depth >= 2, "image too small");
 
-       col->nb_list = calloc(col->max_depth, sizeof(struct neighbors *));
+       if (alloc)
+               col->nb_list = calloc(col->max_depth, sizeof(struct neighbors *));
        if (!col->nb_list) {
                rc = -1;
                goto error;
        }
-       col->marks = calloc(col->max_depth, sizeof(darray_t *));
+       if (alloc)
+               col->marks = calloc(col->max_depth, sizeof(darray_t *));
        if (!col->marks) {
                rc = -1;
                goto error;
        }
-       col->values = calloc(col->max_depth, sizeof(darray_t *));
+       if (alloc)
+               col->values = calloc(col->max_depth, sizeof(darray_t *));
        if (!col->values) {
                rc = -1;
                goto error;
        }
-       col->flows = calloc(col->max_depth, sizeof(darray_t *));
+       if (alloc)
+               col->flows = calloc(col->max_depth, sizeof(darray_t *));
        if (!col->flows) {
                rc = -1;
                goto error;
        }
-       col->flows_i = calloc(col->max_depth, sizeof(darray_t *));
+       if (alloc)
+               col->flows_i = calloc(col->max_depth, sizeof(darray_t *));
        if (!col->flows_i) {
                rc = -1;
                goto error;
        }
        if (col->scalexyz) {
                /* the flows_xy are scaled in xy in the current level */
-               col->flows_xy = calloc(col->max_depth, sizeof(darray_t *));
+               if (alloc)
+                       col->flows_xy = calloc(col->max_depth, sizeof(darray_t *));
                if (!col->flows_xy) {
                        rc = -1;
                        goto error;
@@ -98,7 +104,8 @@ int colorize_prepare(struct colorize *col)
                dimensions[0] = ww;
                dimensions[1] = hh;
                dimensions[2] = last_kk;
-               col->marks[d] = darrayCreate(3, dimensions);
+               if (alloc)
+                       col->marks[d] = darrayCreate(3, dimensions);
                if (!col->marks[d]) {
                        rc = -1;
                        goto error;
@@ -107,12 +114,14 @@ int colorize_prepare(struct colorize *col)
                dimensions[1] = hh;
                dimensions[2] = last_kk-1;
                dimensions[3] = 2;
-               col->flows[d] = darrayCreate(4, dimensions);
+               if (alloc)
+                       col->flows[d] = darrayCreate(4, dimensions);
                if (!col->flows[d]) {
                        rc = -1;
                        goto error;
                }
-               col->flows_i[d] = darrayCreate(4, dimensions);
+               if (alloc)
+                       col->flows_i[d] = darrayCreate(4, dimensions);
                if (!col->flows_i[d]) {
                        rc = -1;
                        goto error;
@@ -122,7 +131,8 @@ int colorize_prepare(struct colorize *col)
                        dimensions[1] = (hh+1)>>1;
                        dimensions[2] = kk-1;
                        dimensions[3] = 2;
-                       col->flows_xy[d] = darrayCreate(4, dimensions);
+                       if (alloc)
+                               col->flows_xy[d] = darrayCreate(4, dimensions);
                        if (!col->flows_xy[d]) {
                                rc = -1;
                                goto error;
@@ -198,7 +208,8 @@ int colorize_prepare(struct colorize *col)
                dimensions[0] = ww;
                dimensions[1] = hh;
                dimensions[2] = kk;
-               col->values[d] = darrayCreate(3, dimensions);
+               if (alloc)
+                       col->values[d] = darrayCreate(3, dimensions);
                if (!col->values[d]) {
                        rc = -1;
                        goto error;
@@ -206,13 +217,15 @@ int colorize_prepare(struct colorize *col)
 
                /* generate array of neighbors */
 #ifndef TEST_NO_RENDER
-               col->nb_list[d] = gen_neighbor(ww, hh, kk, col->flows[d], col->flows_i[d]);
+               if (alloc)
+                       col->nb_list[d] = gen_neighbor(ww, hh, kk, col->flows[d], col->flows_i[d]);
                if (!col->nb_list[d]) {
                        rc = -1;
                        goto error;
                }
                /* weighten */
-               weighten(luminance, col->nb_list[d]);
+               if (alloc)
+                       weighten(luminance, col->nb_list[d]);
 #endif
 
                last_kk = kk;
@@ -226,7 +239,8 @@ int colorize_prepare(struct colorize *col)
        dimensions[0] = col->w;
        dimensions[1] = col->h;
        dimensions[2] = col->k;
-       col->init = darrayCreate(3, dimensions);
+       if (alloc)
+               col->init = darrayCreate(3, dimensions);
        if (!col->init) {
                rc = -1;
                goto error;
@@ -481,32 +495,45 @@ void colorize_free(struct colorize *col)
        darrayDestroy(col->init);
 }
 
-int colorize(const darray_t *image, const darray_t *image_mark, const darray_t *flow, const darray_t *flow_i, int inner_iter, int outer_iter, int scalexyz, char **feat_names)
+int colorize(const darray_t *image, const darray_t *image_mark, const darray_t *flow, const darray_t *flow_i, int inner_iter, int outer_iter, int quick, int scalexyz, char **feat_names, struct colorize **_col)
 {
-       struct colorize col;
+       struct colorize *col;
        int rc = -1;
+       int alloc = 0;
+
+       if (!_col || !(*_col)) {
+               col = calloc(sizeof(*col), 1);
+               memset(col, 0, sizeof(*col));
+               col->image = image;
+               col->image_mark = image_mark;
+               col->flow = flow;
+               col->flow_i = flow_i;
+               col->inner_iter = inner_iter;
+               col->outer_iter = outer_iter;
+               col->scalexyz = scalexyz;
+               alloc = 1;
+       }
+       /* use or assign *_col */
+       if (_col) {
+               if (!(*_col))
+                       *_col = col;
+               else
+                       col = *_col;
+       }
 
-       memset(&col, 0, sizeof(col));
-       col.image = image;
-       col.image_mark = image_mark;
-       col.flow = flow;
-       col.flow_i = flow_i;
-       col.inner_iter = inner_iter;
-       col.outer_iter = outer_iter;
-       col.scalexyz = scalexyz;
-
-       rc = colorize_prepare(&col);
+       rc = colorize_prepare(col, alloc);
        if (rc < 0)
                goto error;
 
-       rc = colorize_solve(&col, 0, feat_names);
+       rc = colorize_solve(col, quick, feat_names);
        if (rc < 0)
                goto error;
 
        rc = 0;
 
 error:
-       colorize_free(&col);
+       if (!_col)
+               colorize_free(col);
 
        return rc;
 }
index f37e30b..5f77d8b 100644 (file)
@@ -22,9 +22,9 @@ struct colorize {
        darray_t **flows_xy;
 };
 
-int colorize_prepare(struct colorize *col);
+int colorize_prepare(struct colorize *col, int alloc);
 int colorize_solve(struct colorize *col, int quick, char **feat_names);
 void colorize_free(struct colorize *col);
 
-int colorize(const darray_t *image, const darray_t *image_mark, const darray_t *flow, const darray_t *flow_i, int inner_iter, int outer_iter, int scalexyz, char **feat_names);
+int colorize(const darray_t *image, const darray_t *image_mark, const darray_t *flow, const darray_t *flow_i, int inner_iter, int outer_iter, int quick, int scalexyz, char **feat_names, struct colorize **_col);
 #endif
index dd3df42..6198e1d 100644 (file)
@@ -183,7 +183,7 @@ again:
  * options
  */
 
-static int in_itr_num = 5, out_itr_num = 1, optical_flow = 1, bright_contrast = 1, alpha_change = 1;
+static int in_itr_num = 5, out_itr_num = 1, quick = 0, optical_flow = 1, bright_contrast = 1, alpha_change = 1;
 int scale = 1, scalexyz = 999;
 
 static enum test test = NO_TEST;
@@ -231,6 +231,7 @@ static int handle_options(int argc, char **argv)
                        {"depth", 1, 0, 'd'},
                        {"in-itr-num", 1, 0, 'i'},
                        {"out-itr-num", 1, 0, 'o'},
+                       {"quick", 0, 0, 'q'},
                        {"zscale", 1, 0, 'z'},
                        {"brightness-contrast", 1, 0, 'b'},
                        {"optical-flow", 1, 0, 'f'},
@@ -239,7 +240,7 @@ static int handle_options(int argc, char **argv)
                        {0, 0, 0, 0},
                };
 
-               c = getopt_long(argc, argv, "hd:i:o:z:b:f:s:t:", long_options, &option_index);
+               c = getopt_long(argc, argv, "hd:i:o:qz:b:f:s:t:", long_options, &option_index);
 
                if (c == -1)
                        break;
@@ -260,6 +261,10 @@ static int handle_options(int argc, char **argv)
                        out_itr_num = atoi(optarg);
                        skip_args += 2;
                        break;
+               case 'q':
+                       quick = 1;
+                       skip_args += 1;
+                       break;
                case 'z':
                        scalexyz = atoi(optarg);
                        skip_args += 2;
@@ -318,6 +323,7 @@ static void print_help(const char *app)
        printf(" -d --depth <bits>                   Save images with given color bit depth (default=%d)\n", save_depth);
        printf(" -i --in-itr-num <num>               Alter inner iterations (weightening count) of colorization algorithm (default=%d)\n", in_itr_num);
        printf(" -o --out-itr-num <num>              Alter outer iterations (complete turns) of colorization algorithm (default=%d)\n", out_itr_num);
+       printf(" -q --quick                          Use quick render, but sufaces may be colorized incomplete\n");
        printf(" -z --zscale <levels>                How many grids (staring with the finest) should be scaled in z direction to generate the next coarse grid ");
        if (scalexyz < 999)
                printf("(default=%d)\n", scalexyz);
@@ -620,7 +626,7 @@ next_sequence:
 
        if (test != FLOW_NEXT && test != FLOW_PREV && test != MARKED && test != MASK && test != MASK_COLOR && test != BC_ONLY && test != BC_IMAGE) {
                printf("Colorizing %d frames, please wait...\n", k);
-               rc = colorize(I, mI, flow, flow_i, in_itr_num, out_itr_num, scalexyz, feat_names);
+               rc = colorize(I, mI, flow, flow_i, in_itr_num, out_itr_num, quick, scalexyz, feat_names, NULL);
                if (rc < 0) {
                        if (k > 1)
                                printf("No memory! Use smaller frames or less frames between key frames or add more memory.");
index a412dc2..1812b97 100644 (file)
 #include "process.h"
 
 /*
- * create image array and marked array
+ * create image array and marked array (if not already)
  */
 int alloc_I_arrays(darray_t **I, darray_t **mI, int w, int h, int k, int *features, char **feat_names, int change_alpha, int change_bc)
 {
        int dims[4];
 
-       dims[0] = w; dims[1] = h; dims[2] = k;
-       *mI = darrayCreate(3, dims);
-       if (!*mI) {
-               printf("%s:failed to create mark array\n", __func__);
-               goto exit;
+       if (!(*mI)) {
+               dims[0] = w; dims[1] = h; dims[2] = k;
+               *mI = darrayCreate(3, dims);
+               if (!*mI) {
+                       printf("%s:failed to create mark array\n", __func__);
+                       goto exit;
+               }
        }
        *features = 2; /* u and v */
        feat_names[0] = "u";
@@ -42,11 +44,13 @@ int alloc_I_arrays(darray_t **I, darray_t **mI, int w, int h, int k, int *featur
                        feat_names[3] = "contrast";
                }
        }
-       dims[0] = w; dims[1] = h; dims[2] = *features + 1; dims[3] = k;
-       *I = darrayCreate(4, dims);
-       if (!*I) {
-               printf("%s:failed to create image array\n", __func__);
-               goto exit;
+       if (!(*I)) {
+               dims[0] = w; dims[1] = h; dims[2] = *features + 1; dims[3] = k;
+               *I = darrayCreate(4, dims);
+               if (!*I) {
+                       printf("%s:failed to create image array\n", __func__);
+                       goto exit;
+               }
        }
 
        return 0;