add option to store rendered image at a different prefix
[colorize.git] / src / colorize.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <getopt.h>
6 #include <math.h>
7 #include <time.h>
8 #include "../lib/darray.h"
9 #include "../lib/colorize.h"
10 #include "img.h"
11 #include "yuv.h"
12 #include "mark.h"
13 #include "process.h"
14 #include "settings.h"
15 #include "dir_seperator.h"
16 #ifdef WITH_OPENCV
17 #include "opticalflow.h"
18 #endif
19
20 static void print_help(const char *app);
21 static void print_test_help();
22
23 //#defbug SEQUENCE
24 //#define SINGLE_K_TEST 2
25
26 /*
27  * load sequence
28  */
29
30 struct sequence {
31         char filename[256];
32 };
33 static struct sequence *sequence = NULL;
34
35 /* load sequence list and return number of images in sequence */
36 static int load_sequence(int *start, int *next, char *first_filename)
37 {
38         const char *name = "sequence";
39         int count;
40         int i, j, keyframe_before_start, eof;
41         FILE *fp;
42         char buffer[256], *p;
43
44         /* free previously used sequence */
45         if (sequence)
46                 free(sequence);
47         sequence = NULL;
48
49 again:
50         /* get number of frames until next keyframe or end of squence */
51         fp = fopen(name, "r");
52         if (!fp) {
53                 printf("failed to load sequence '%s'\n", name);
54                 return -1;
55         }
56         count = 0;
57         j = 0;
58         eof = 1;
59         keyframe_before_start = 0;
60         while(fgets(buffer, sizeof(buffer), fp)) {
61                 p = strchr(buffer + 1, '\"');
62                 if (!p)
63                         break;
64                 *p = '\0';
65                 if (j == 0)
66                         strcpy(first_filename, buffer + 1);
67                 p++;
68                 if ((*start) && j == (*start) - 1) {
69                         if (strstr(p, "keyframe"))
70                                 keyframe_before_start = 1;
71                 }
72                 if (j < (*start)) {
73                         j++;
74                         continue;
75                 }
76 #ifdef DEBUG_SEQUENCE
77                 printf("counting %d\n", j);
78 #endif
79                 j++;
80                 count++;
81                 /* skip start frame, since it is always a keyframe */
82                 if (count == 1)
83                         continue;
84                 if (strstr(p, "keyframe")) {
85                         eof = 0;
86                         break;
87                 }
88         }
89         fclose(fp);
90
91         /* end of file case */
92         if (count == 0) {
93 #ifdef DEBUG_SEQUENCE
94                 puts("end of file");
95 #endif
96                 *next = -1;
97                 return 0;
98         } else
99         if (count == 1) {
100 #ifdef DEBUG_SEQUENCE
101                 puts("count=1");
102 #endif
103                 if (eof) {
104 #ifdef DEBUG_SEQUENCE
105                         puts("end of file at last frame");
106 #endif
107                         *next = -1;
108                         return 0;
109                 }
110                 /* we are done, because we already had the last sequence */
111                 if (!keyframe_before_start) {
112 #ifdef DEBUG_SEQUENCE
113                         puts("no keyframe before start");
114 #endif
115                         (*next) = -1;
116                         return 0;
117                 }
118 #ifdef DEBUG_SEQUENCE
119                 puts("keyframe before start");
120 #endif
121                 /* we just have a single last frame */
122                 count = 1;
123                 (*next) = (*start) + 1;
124         } else
125         /* if we have two keyframes side by side, we ignore it */
126         if (count == 2 && !eof) {
127 #ifdef DEBUG_SEQUENCE
128                 puts("count=2");
129 #endif
130                 /* only if the frame before start is not a keyframe
131                  * and we are not the first sequence, because that would mean
132                  * that the first frame is a single sequence */
133                 if (!keyframe_before_start && (*start) > 0) {
134 #ifdef DEBUG_SEQUENCE
135                         puts("no keyframe before start");
136 #endif
137                         (*start)++;
138                         goto again;
139                 }
140 #ifdef DEBUG_SEQUENCE
141                 puts("keyframe before start or first frame");
142 #endif
143                 /* now the start frame has one keyframe before and one after */
144                 count = 1;
145                 (*next) = (*start) + 1;
146         } else {
147                 (*next) = (*start) + count - 1;
148         }
149
150         /* alloc memory for sequence and fill with data */
151         sequence = (struct sequence *)malloc(sizeof(struct sequence) * count);
152         if (!sequence) {
153                 printf("no memory for sequence\n");
154                 return -1;
155         }
156         memset((struct sequence *)sequence, 0, sizeof(struct sequence) * count);
157         fp = fopen(name, "r");
158         if (!fp) {
159                 printf("failed to load sequence '%s'\n", name);
160                 return -1;
161         }
162         i = 0;
163         j = 0;
164         while(fgets(buffer, sizeof(buffer), fp)) {
165                 p = strchr(buffer + 1, '\"');
166                 if (!p)
167                         break;
168                 if (j < (*start)) {
169                         j++;
170                         continue;
171                 }
172                 *p = '\0';
173                 strcpy(sequence[i].filename, buffer + 1);
174                 i++;
175                 if (i == count)
176                         break;
177         }
178         fclose(fp);
179
180         return count;
181 }
182
183 /*
184  * options
185  */
186
187 static int in_itr_num = 5, out_itr_num = 1, quick = 0, optical_flow = 1, bright_contrast = 1, alpha_change = 1;
188 int scale = 1, scalexyz = 999;
189 static char output_prefix[256] = "";
190
191 static enum test test = NO_TEST;
192
193 static int parse_test(const char *arg)
194 {
195         if (!strcmp(arg, "help")) {
196                 print_test_help();
197                 exit(0);
198         }
199 #ifdef WITH_OPENCV
200         if (!strcmp(arg, "flow-next"))
201                 return FLOW_NEXT;
202         if (!strcmp(arg, "flow-prev"))
203                 return FLOW_PREV;
204 #endif
205         if (!strcmp(arg, "marked"))
206                 return MARKED;
207         if (!strcmp(arg, "mask"))
208                 return MASK;
209         if (!strcmp(arg, "mask+color"))
210                 return MASK_COLOR;
211         if (!strcmp(arg, "bc-only"))
212                 return BC_ONLY;
213         if (!strcmp(arg, "bc-image"))
214                 return BC_IMAGE;
215         if (!strcmp(arg, "alpha"))
216                 return ALPHA;
217         if (!strcmp(arg, "no-alpha"))
218                 return NO_ALPHA;
219         if (!strcmp(arg, "removal-image"))
220                 return REMOVAL_IMAGE;
221
222         return NO_TEST;
223 }
224
225 static int handle_options(int argc, char **argv)
226 {
227         int skip_args = 0;
228
229         while (1) {
230                 int option_index = 0, c;
231                 static struct option long_options[] = {
232                         {"help", 0, 0, 'h'},
233                         {"depth", 1, 0, 'd'},
234                         {"output-prefix", 1, 0, 'O'},
235                         {"in-itr-num", 1, 0, 'i'},
236                         {"out-itr-num", 1, 0, 'o'},
237                         {"quick", 0, 0, 'q'},
238                         {"zscale", 1, 0, 'z'},
239                         {"brightness-contrast", 1, 0, 'b'},
240                         {"optical-flow", 1, 0, 'f'},
241                         {"scale", 1, 0, 's'},
242                         {"test", 1, 0, 't'},
243                         {0, 0, 0, 0},
244                 };
245
246                 c = getopt_long(argc, argv, "hd:O:i:o:qz:b:f:s:t:", long_options, &option_index);
247
248                 if (c == -1)
249                         break;
250
251                 switch (c) {
252                 case 'h':
253                         print_help(argv[0]);
254                         exit(0);
255                 case 'd':
256                         save_depth = atoi(optarg);
257                         skip_args += 2;
258                         break;
259                 case 'O':
260                         strcpy(output_prefix, optarg);
261                         skip_args += 2;
262                         break;
263                 case 'i':
264                         in_itr_num = atoi(optarg);
265                         skip_args += 2;
266                         break;
267                 case 'o':
268                         out_itr_num = atoi(optarg);
269                         skip_args += 2;
270                         break;
271                 case 'q':
272                         quick = 1;
273                         skip_args += 1;
274                         break;
275                 case 'z':
276                         scalexyz = atoi(optarg);
277                         skip_args += 2;
278                         break;
279                 case 'b':
280                         bright_contrast = atoi(optarg);
281                         skip_args += 2;
282                         break;
283                 case 'a':
284                         alpha_change = atoi(optarg);
285                         skip_args += 2;
286                         break;
287                 case 'f':
288                         optical_flow = atoi(optarg);
289                         skip_args += 2;
290                         break;
291                 case 's':
292                         scale = atoi(optarg);
293                         skip_args += 2;
294                         break;
295                 case 't':
296                         test = parse_test(optarg);
297                         if (!test) {
298                                 fprintf(stderr, "Invalid test '%s', use '--test help' to get a list of tests\n", optarg);
299                                 exit(-1);
300                         }
301                         skip_args += 2;
302                         break;
303                 default:
304                         break;
305                 }
306         }
307
308         return skip_args;
309 }
310
311 /*
312  * usage
313  */
314
315 static void print_help(const char *app)
316 {
317         printf("Colorize version %s\n\n",
318 #include "../version.h"
319         );
320         printf("Usage: %s [options] <grey ppm image> <marked ppm image> <result ppm image> [<frames> <start>]\n", app);
321         printf("       Colorize grey image using marked image and save to result image.\n");
322         printf("       If frames and start frame is given, image names must include printf integer formatting (e.g. %%04d).\n");
323         printf("Usage: %s [options] <grey ppm image> marked <result ppm image>\n", app);
324         printf("       Colorize grey image using marked mask + palette and save to result image.\n");
325         printf("Usage: %s [options] sequence [list | <start with frame> [<stop with frame>]]\n", app);
326         printf("       Colorize movie sequence (generated by colorize gui) as found in the current directory.\n");
327         printf("       Use list to view sequence segments between keyframes.\n");
328         printf("\nOptions:\n");
329         printf(" -h --help                           This help\n");
330         printf(" -d --depth <bits>                   Save images with given color bit depth (default=%d)\n", save_depth);
331         printf(" -O --output-prefix <path>/<prefix>  Store result image of a sequence using this prefix\n");
332         printf(" -i --in-itr-num <num>               Alter inner iterations (weightening count) of colorization algorithm (default=%d)\n", in_itr_num);
333         printf(" -o --out-itr-num <num>              Alter outer iterations (complete turns) of colorization algorithm (default=%d)\n", out_itr_num);
334         printf(" -q --quick                          Use quick render, but sufaces may be colorized incomplete\n");
335         printf(" -z --zscale <levels>                How many grids (staring with the finest) should be scaled in z direction to generate the next coarse grid ");
336         if (scalexyz < 999)
337                 printf("(default=%d)\n", scalexyz);
338         else
339                 printf("(default=infinite)\n");
340         printf(" -b --brightness-contrast [0 | 1]    Apply brightnes and contrast, if defined in palette by GUI (default=%d)\n", bright_contrast);
341         printf(" -a --alpha-change [0 | 1]           Apply alpha channel change, if defined in palette by GUI (default=%d)\n", alpha_change);
342 #ifdef WITH_OPENCV
343         printf(" -f --optical-flow [0 | 1]           Apply optical flow, if defined by GUI (default=%d)\n", optical_flow);
344 #endif
345         printf(" -s --scale [1..n]                   Scale down by the given factor for quick and dirty previews (default=%d)\n", scale);
346         printf(" -t --test <test>                    Generate test images. Use 'help' for list of tests\n");
347 }
348
349 static void print_test_help()
350 {
351         printf(" -t --test <test>        Generate test images...\n");
352 #ifdef WITH_OPENCV
353         printf("           flow-next     Optical flow plane to next image\n");
354         printf("           flow-prev     Optical flow plane to previous image\n");
355 #endif
356         printf("           marked        Only apply marked colors to grey image\n");
357         printf("           mask          Show mask of marked areas\n");
358         printf("           mask+color    Show mask of marked areas + color\n");
359         printf("           bc-only       Only apply brightness+contrast, leave colors of grey image as is\n");
360         printf("           bc-image      Show brightness+contrast change on grey image as uv components\n");
361         printf("           alpha         Show the image with alpha channel only\n");
362         printf("           no-alpha      Show the image without alpha channel, to see pixles that are made transparent\n");
363         printf("           removal-image Show the image with \"transparency removal layer\" only, but keep u and v\n");
364 }
365
366 /*
367  * main function
368  */
369
370 int main(int argc, char *argv[])
371 {
372         darray_t *gI = NULL, *cI = NULL, *mI = NULL, *I = NULL;
373         darray_t *flow = NULL, *flow_i = NULL;
374         double *ptr_gI = NULL, *ptr_cI = NULL, *ptr_y = NULL, *ptr_u = NULL, *ptr_v = NULL, *ptr_a = NULL, *ptr_r = NULL, *ptr_b = NULL, *ptr_c = NULL, *ptr_m = NULL;
375         double diff, sum;
376         int dims[4];
377         int w = 0, h = 0, load_w, load_h, k = 1, index = 0, z;
378         unsigned short *img = NULL;
379         unsigned char *mark_buffer = NULL;
380         int rc, x, y, i, ii;
381         time_t start, end;
382         const char* filename;
383         char first_filename[256];
384         int seq_offset = 0, seq_next = 0;
385         int features, change_bc, change_alpha;
386         char *feat_names[20];
387         int skip_args;
388
389         skip_args = handle_options(argc, argv);
390         argc -= skip_args;
391         argv += skip_args;
392
393         if (argc <= 1) {
394                 print_help(argv[0]);
395                 return 0;
396         }
397
398 next_sequence:
399         if (argc > 1 && !strcmp(argv[1], "sequence")) {
400                 k = load_sequence(&seq_offset, &seq_next, first_filename);
401                 if (k == 0)
402                         return 0;
403                 printf("Got %d frames from sequence (frames %d..%d)\n", k, seq_offset, seq_offset + k - 1);
404                 if (argc > 2 && (!strcmp(argv[2], "list") || atoi(argv[2]) > seq_offset)) {
405                         seq_offset = seq_next;
406                         goto next_sequence;
407                 }
408         } else if (argc <= 3) {
409                 print_help(argv[0]);
410                 return 0;
411         } else if (argc > 5) {
412                 k = atoi(argv[4]);
413                 index = atoi(argv[5]);
414         }
415         if (k <= 0)
416                 return 0;
417
418 #ifdef SINGLE_K_TEST
419         if (k > SINGLE_K_TEST)
420                 k = SINGLE_K_TEST;
421 #endif
422
423         time(&start);
424
425         change_bc = change_alpha = 0;
426         for (z = 0; z < k; z++) {
427                 if (sequence) {
428                         filename = sequence[z].filename;
429                         /* first_filename is set by load_sequence */
430                 } else {
431                         filename = argv[1];
432                         strcpy(first_filename, argv[1]);
433                 }
434 #ifdef WITH_OPENCV
435                 // load flow settings
436                 if (sequence && optical_flow)
437                         flow_default();
438 #endif
439                 load_settings(first_filename);
440 #ifdef WITH_OPENCV
441                 // load flow settings
442                 if (sequence && optical_flow) {
443                         if (flow_enable == 0 && (test == FLOW_NEXT || test == FLOW_PREV)) {
444                                 fprintf(stderr, "Cannot test optical flow, because it is not enabled by GUI.\n");
445                                 exit (0);
446                         }
447                 }
448 #endif
449                 // load original image and convert their RGB components to double RGB array
450                 img = load_img(&load_w, &load_h, filename, index + z);
451                 if (!img) {
452                         fprintf(stderr, "Failed to load grey image '%s'\n", filename);
453                         return 0;
454                 }
455                 scale_img(img, load_w, load_h, scale);
456                 if (z == 0) {
457                         w = load_w / scale;
458                         h = load_h / scale;
459                 }
460                 if (load_w/scale != w || load_h/scale != h) {
461                         fprintf(stderr, "Error: All input images must have equal dimenstions.\n");
462                         return 0;
463                 }
464                 // now we know the dimensions, so we can create input arrays
465                 if (!gI) {
466                         dims[0] = w; dims[1] = h; dims[2] = 3; dims[3] = k;
467                         gI = darrayCreate(4, dims);
468                 }
469                 if (!gI) {
470                         printf("failed to create grey image array\n");
471                         exit (0);
472                 }
473                 if (!cI) {
474                         dims[0] = w; dims[1] = h; dims[2] = 3; dims[3] = k;
475                         cI = darrayCreate(4, dims);
476                 }
477                 if (!cI) {
478                         printf("failed to create marked image array\n");
479                         exit (0);
480                 }
481                 img2array_short(img, w, h, darrayGetPr(gI) + w*h*3*z, w, h);
482                 free(img);
483
484                 if (sequence || !strcmp(argv[2], "marked")) {
485                         char name[256];
486                         unsigned char c;
487                         // load marked mask and convert their RGB components to double YUV array
488                         memcpy(darrayGetPr(cI) + w*h*3*z, darrayGetPr(gI) + w*h*3*z, w*h*3 * sizeof(double));
489                         /* add extra memory for unscaled data to prevent buffer overflow */
490                         if (!mark_buffer)
491                                 mark_buffer = (unsigned char *)malloc(w*h*k + load_w*load_h);
492                         if (!mark_buffer) {
493                                 printf("no memory for mark buffer\n");
494                                 exit (0);
495                         }
496                         if (load_palette(first_filename)) {
497                                 printf("failed to load palette for file: '%s'\n", filename);
498                                 exit (0);
499                         }
500                         sprintf(name, filename, index + z);
501                         /* always load full unscaled image, then scale down */
502                         if (load_marked(mark_buffer + w*h*z, load_w, load_h, name) == 0) {
503                                 scale_mark(mark_buffer + w*h*z, load_w, load_h, scale);
504                                 ptr_cI = darrayGetPr(cI) + w*h*3*z;
505                                 for (y = 0; y < h; y++) {
506                                         for (x = 0; x < w; x++) {
507                                                 /* do not apply mask on index 0 */
508                                                 c = mark_buffer[y*w+x + w*h*z];
509                                                 if (c == 0)
510                                                         continue;
511                                                 /* check for any brightness/contrast change */
512                                                 if (bright_contrast && (mark_palette[c-1].bright != 0 || mark_palette[c-1].contrast != 1))
513                                                         change_bc = 1;
514                                                 /* check for any alpha change */
515                                                 if (alpha_change && mark_palette[c-1].alpha < 1)
516                                                         change_alpha = 1;
517                                                 /* do not apply white pixles, this meas: keep original color */
518                                                 if (mark_palette[c-1].r == 255 && mark_palette[c-1].g == 255 && mark_palette[c-1].b == 255)
519                                                         continue;
520                                                 ptr_cI[y*w+x] = mark_palette[c-1].r / 255.0F;
521                                                 ptr_cI[y*w+x + w*h] = mark_palette[c-1].g / 255.0F;
522                                                 ptr_cI[y*w+x + w*h*2] = mark_palette[c-1].b / 255.0F;
523                                         }
524                                 }
525                         } else
526                                 memset(mark_buffer + w*h*z, 0, w*h);
527                 } else {
528                         // load marked image and convert their RGB components to double YUV array
529                         img = load_img(&load_w, &load_h, argv[2], index + z);
530                         if (!img) {
531                                 scale_img(img, load_w, load_h, scale);
532                                 if (load_w/scale != w || load_h/scale != h) {
533                                         fprintf(stderr, "Error: All input images must have equal dimenstions.\n");
534                                         return 0;
535                                 }
536                                 img2array_short(img, w, h, darrayGetPr(cI) + w*h*3*z, w, h);
537                                 free(img);
538                         } else {
539                                 fprintf(stderr, "Failed to load marked image, omitting...\n");
540                                 memcpy(darrayGetPr(cI) + w*h*3*z, darrayGetPr(gI) + w*h*3*z, w*h*3 * sizeof(double));
541                         }
542                 }
543         }
544
545         rc = alloc_I_arrays(&I, &mI, w, h, k, &features, feat_names, change_alpha, change_bc);
546         if (rc)
547                 exit(0);
548
549         for (z = 0; z < k; z++) {
550                 set_I_ptr(I, mI, w, h, z, features, change_alpha, change_bc, &ptr_y, &ptr_u, &ptr_v, &ptr_a, &ptr_r, &ptr_b, &ptr_c, &ptr_m);
551                 ptr_gI = darrayGetPr(gI) + w*h*3*z;
552                 ptr_cI = darrayGetPr(cI) + w*h*3*z;
553
554                 // convert original image into YUV
555                 rgb2yuv(ptr_gI, ptr_gI, w, h);
556                 // convert maked image into YUV
557                 rgb2yuv(ptr_cI, ptr_cI, w, h);
558
559                 if (sequence || !strcmp(argv[2], "marked")) {
560                         unsigned char c;
561                         // use marked mask to fill markIm
562                         for (y = 0; y < h; y++) {
563                                 for (x = 0; x < w; x++) {
564                                         /* do not apply mask on index 0 */
565                                         c = mark_buffer[y*w+x + w*h*z];
566                                         if (c)
567                                                 ptr_m[y*w+x] = 1.0F;
568                                         else
569                                                 ptr_m[y*w+x] = 0.0F;
570                                 }
571                         }
572                 } else {
573                         // fill color image with marked pixles
574                         // - calculate the difference between two images (original image - color image)
575                         // - convert into absolute (positive values)
576                         // - sum all components to get grey image
577                         // - apply threshold (pixle is 1F, if the absolute difference is > 0.01F)
578                         // original code: markIm=(sum(abs(gI-cI),3)>0.01); (according to developers of the algorithm)
579                         for (i = 0, ii = w * h; i < ii; i++) {
580                                 diff = 0;
581                                 sum = ptr_gI[i] - ptr_cI[i];
582                                 if (sum < 0)
583                                         diff -= sum;
584                                 else
585                                         diff += sum;
586                                 sum = ptr_gI[i + ii] - ptr_cI[i + ii];
587                                 if (sum < 0)
588                                         diff -= sum;
589                                 else
590                                         diff += sum;
591                                 sum = ptr_gI[i + ii + ii] - ptr_cI[i + ii + ii];
592                                 if (sum < 0)
593                                         diff -= sum;
594                                 else
595                                         diff += sum;
596                                 if (diff > 0.01)
597                                         ptr_m[i] = 1.0;
598                                 else
599                                         ptr_m[i] = 0.0;
600                         }
601                 }
602
603                 prepare_arrays(w, h, change_alpha, change_bc, mark_buffer+w*h*z, ptr_gI, ptr_cI, ptr_y, ptr_u, ptr_v, ptr_a, ptr_r, ptr_b, ptr_c, ptr_m, test);
604         }
605
606 #ifdef WITH_OPENCV
607         if (k > 1 && flow_enable) {
608                 /* create flow vectors */
609                 dims[0] = w; dims[1] = h; dims[2] = k - 1; dims[3] = 2;
610                 flow = darrayCreate(4, dims);
611                 if (!flow) {
612                         printf("failed to create array\n");
613                         exit (0);
614                 }
615                 flow_i = darrayCreate(4, dims);
616                 if (!flow_i) {
617                         printf("failed to create array\n");
618                         exit (0);
619                 }
620                 printf("Calculating optical flow for %d frames: window=%d\n", k, flow_window/scale);
621         } else if (k > 1)
622                 printf("Note: Optical flow is not activated!\n");
623         for (z = 0; z < k-1; z++) {
624                 if (flow)
625                         create_flow_maps(NULL, darrayGetPr(gI) + w*h*3*(z+1), darrayGetPr(gI) + w*h*3*z, w, h, flow_window/scale, 0, NULL, NULL, darrayGetPr(flow) + w*h*z, darrayGetPr(flow) + w*h*z + w*h*(k-1), NULL);
626                 if (flow_i)
627                         create_flow_maps(darrayGetPr(gI) + w*h*3*z, NULL, darrayGetPr(gI) + w*h*3*(z+1), w, h, flow_window/scale, 0, darrayGetPr(flow_i) + w*h*z, darrayGetPr(flow_i) + w*h*z + w*h*(k-1), NULL, NULL, NULL);
628         }
629 #else
630         if (k > 1)
631                 printf("Note: Optical flow is not compiled in!\n");
632 #endif
633
634         darrayDestroy(gI);
635         gI = NULL;
636         darrayDestroy(cI);
637         cI = NULL;
638
639         if (test != FLOW_NEXT && test != FLOW_PREV && test != MARKED && test != MASK && test != MASK_COLOR && test != BC_ONLY && test != BC_IMAGE) {
640                 printf("Colorizing %d frames, please wait...\n", k);
641                 rc = colorize(I, mI, flow, flow_i, in_itr_num, out_itr_num, quick, scalexyz, feat_names, NULL);
642                 if (rc < 0) {
643                         if (k > 1)
644                                 printf("No memory! Use smaller frames or less frames between key frames or add more memory.");
645                         else
646                                 printf("No memory! Use smaller image or add more memory.");
647                         exit(-1);
648                 }
649         }
650
651         for (z = 0; z < k; z++) {
652                 set_I_ptr(I, mI, w, h, z, features, change_alpha, change_bc, &ptr_y, &ptr_u, &ptr_v, &ptr_a, &ptr_r, &ptr_b, &ptr_c, &ptr_m);
653                 postpare_arrays(w, h, change_alpha, change_bc, ptr_y, ptr_u, ptr_v, ptr_a, ptr_r, ptr_b, ptr_c, ptr_m, test);
654
655 #ifdef WITH_OPENCV
656                 if (test == FLOW_NEXT || test == FLOW_PREV) {
657                         double *ptr_f1 = NULL, *ptr_f2 = NULL;
658                         /* apply flow planes to result image as u and y vector */
659                         if (test == FLOW_NEXT) {
660                                 if (flow) {
661                                         ptr_f1 = darrayGetPr(flow) + w*h*z;
662                                         ptr_f2 = darrayGetPr(flow) + w*h*z*(k-1);
663                                 }
664                         } else {
665                                 if (flow_i) {
666                                         ptr_f1 = darrayGetPr(flow_i) + w*h*z;
667                                         ptr_f2 = darrayGetPr(flow_i) + w*h*z*(k-1);
668                                 }
669                         }
670                         if (ptr_f1 && ptr_f1) {
671                                 for (y = 0; y < h; y++) {
672                                         for (x = 0; x < w; x++) {
673                                                 ptr_y[w*y+x] = 0.5;
674                                                 if (z < k-1) {
675                                                         ptr_u[w*y+x] = ptr_f1[w * y + x] / 50;
676                                                         ptr_v[w*y+x] = ptr_f2[w * y + x] / 50;
677                                                 } else {
678                                                         ptr_u[w*y+x] = 0;
679                                                         ptr_v[w*y+x] = 0;
680                                                 }
681                                         }
682                                 }
683                         }
684                 }
685 #endif
686
687                 // save result YUV array to image with RGB components
688                 yuv2rgb(ptr_y, ptr_y, w, h);
689                 if (sequence) {
690                         static char name[256], *p, *q;
691                         p = sequence[z].filename;
692                         while((q = strchr(p, DIR_SEPERATOR)))
693                                 p = q + 1;
694                         if (output_prefix[0] == '\0') {
695                                 strcpy(name, sequence[z].filename);
696                                 name[p - sequence[z].filename] = '\0';
697                                 strcat(name, "colorized_");
698                         } else {
699                                 strcpy(name, output_prefix);
700                         }
701                         strcat(name, p);
702                         filename = name;
703                 } else
704                         filename = argv[3];
705                 /* don't save alpha on these tests */
706                 if (test == ALPHA || test == REMOVAL_IMAGE || test == NO_ALPHA)
707                         save_img_array(ptr_y, w, h, 0, filename, index + z);
708                 else
709                         save_img_array(ptr_y, w, h, change_alpha, filename, index + z);
710         }
711
712         time(&end);
713         printf("Elapsed time: %d minutes, %d seconds\n", (int)(end-start)/60, (int)(end-start)%60);
714
715         // destroy
716         darrayDestroy(I);
717         I = NULL;
718         darrayDestroy(mI);
719         mI = NULL;
720         darrayDestroy(flow);
721         flow = NULL;
722         darrayDestroy(flow_i);
723         flow_i = NULL;
724
725         free(mark_buffer);
726         mark_buffer = NULL;
727
728         darrayDone();
729
730 #ifdef SINGLE_K_TEST
731         exit(0);
732 #endif
733         if (sequence) {
734                 /* if end frame is not given or if not reached */
735                 if (argc <= 3 || atoi(argv[3]) > seq_offset + k) {
736                         seq_offset = seq_offset + k - 1;
737                         goto next_sequence;
738                 }
739         }
740
741         return 0;
742 }
743