8 #include "../lib/darray.h"
9 #include "../lib/colorize.h"
15 #include "dir_seperator.h"
17 #include "opticalflow.h"
20 static void print_help(const char *app);
21 static void print_test_help();
24 //#define SINGLE_K_TEST 2
33 static struct sequence *sequence = NULL;
35 /* load sequence list and return number of images in sequence */
36 static int load_sequence(int *start, int *next, char *first_filename)
38 const char *name = "sequence";
40 int i, j, keyframe_before_start, eof;
44 /* free previously used sequence */
50 /* get number of frames until next keyframe or end of squence */
51 fp = fopen(name, "r");
53 printf("failed to load sequence '%s'\n", name);
59 keyframe_before_start = 0;
60 while(fgets(buffer, sizeof(buffer), fp)) {
61 p = strchr(buffer + 1, '\"');
66 strcpy(first_filename, buffer + 1);
68 if ((*start) && j == (*start) - 1) {
69 if (strstr(p, "keyframe"))
70 keyframe_before_start = 1;
77 printf("counting %d\n", j);
81 /* skip start frame, since it is always a keyframe */
84 if (strstr(p, "keyframe")) {
91 /* end of file case */
100 #ifdef DEBUG_SEQUENCE
104 #ifdef DEBUG_SEQUENCE
105 puts("end of file at last frame");
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");
118 #ifdef DEBUG_SEQUENCE
119 puts("keyframe before start");
121 /* we just have a single last frame */
123 (*next) = (*start) + 1;
125 /* if we have two keyframes side by side, we ignore it */
126 if (count == 2 && !eof) {
127 #ifdef DEBUG_SEQUENCE
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");
140 #ifdef DEBUG_SEQUENCE
141 puts("keyframe before start or first frame");
143 /* now the start frame has one keyframe before and one after */
145 (*next) = (*start) + 1;
147 (*next) = (*start) + count - 1;
150 /* alloc memory for sequence and fill with data */
151 sequence = (struct sequence *)malloc(sizeof(struct sequence) * count);
153 printf("no memory for sequence\n");
156 memset((struct sequence *)sequence, 0, sizeof(struct sequence) * count);
157 fp = fopen(name, "r");
159 printf("failed to load sequence '%s'\n", name);
164 while(fgets(buffer, sizeof(buffer), fp)) {
165 p = strchr(buffer + 1, '\"');
173 strcpy(sequence[i].filename, buffer + 1);
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;
190 static enum test test = NO_TEST;
192 static int parse_test(const char *arg)
194 if (!strcmp(arg, "help")) {
199 if (!strcmp(arg, "flow-next"))
201 if (!strcmp(arg, "flow-prev"))
204 if (!strcmp(arg, "marked"))
206 if (!strcmp(arg, "mask"))
208 if (!strcmp(arg, "mask+color"))
210 if (!strcmp(arg, "bc-only"))
212 if (!strcmp(arg, "bc-image"))
214 if (!strcmp(arg, "alpha"))
216 if (!strcmp(arg, "no-alpha"))
218 if (!strcmp(arg, "removal-image"))
219 return REMOVAL_IMAGE;
224 static int handle_options(int argc, char **argv)
229 int option_index = 0, c;
230 static struct option long_options[] = {
232 {"depth", 1, 0, 'd'},
233 {"in-itr-num", 1, 0, 'i'},
234 {"out-itr-num", 1, 0, 'o'},
235 {"quick", 0, 0, 'q'},
236 {"zscale", 1, 0, 'z'},
237 {"brightness-contrast", 1, 0, 'b'},
238 {"optical-flow", 1, 0, 'f'},
239 {"scale", 1, 0, 's'},
244 c = getopt_long(argc, argv, "hd:i:o:qz:b:f:s:t:", long_options, &option_index);
254 save_depth = atoi(optarg);
258 in_itr_num = atoi(optarg);
262 out_itr_num = atoi(optarg);
270 scalexyz = atoi(optarg);
274 bright_contrast = atoi(optarg);
278 alpha_change = atoi(optarg);
282 optical_flow = atoi(optarg);
286 scale = atoi(optarg);
290 test = parse_test(optarg);
292 fprintf(stderr, "Invalid test '%s', use '--test help' to get a list of tests\n", optarg);
309 static void print_help(const char *app)
311 printf("Colorize version %s\n\n",
312 #include "../version.h"
314 printf("Usage: %s [options] <grey ppm image> <marked ppm image> <result ppm image> [<frames> <start>]\n", app);
315 printf(" Colorize grey image using marked image and save to result image.\n");
316 printf(" If frames and start frame is given, image names must include printf integer formatting (e.g. %%04d).\n");
317 printf("Usage: %s [options] <grey ppm image> marked <result ppm image>\n", app);
318 printf(" Colorize grey image using marked mask + palette and save to result image.\n");
319 printf("Usage: %s [options] sequence [list | <start with frame> [<stop with frame>]]\n", app);
320 printf(" Colorize movie sequence (generated by colorize gui) as found in the current directory.\n");
321 printf(" Use list to view sequence segments between keyframes.\n");
322 printf("\nOptions:\n");
323 printf(" -h --help This help\n");
324 printf(" -d --depth <bits> Save images with given color bit depth (default=%d)\n", save_depth);
325 printf(" -i --in-itr-num <num> Alter inner iterations (weightening count) of colorization algorithm (default=%d)\n", in_itr_num);
326 printf(" -o --out-itr-num <num> Alter outer iterations (complete turns) of colorization algorithm (default=%d)\n", out_itr_num);
327 printf(" -q --quick Use quick render, but sufaces may be colorized incomplete\n");
328 printf(" -z --zscale <levels> How many grids (staring with the finest) should be scaled in z direction to generate the next coarse grid ");
330 printf("(default=%d)\n", scalexyz);
332 printf("(default=infinite)\n");
333 printf(" -b --brightness-contrast [0 | 1] Apply brightnes and contrast, if defined in palette by GUI (default=%d)\n", bright_contrast);
334 printf(" -a --alpha-change [0 | 1] Apply alpha channel change, if defined in palette by GUI (default=%d)\n", alpha_change);
336 printf(" -f --optical-flow [0 | 1] Apply optical flow, if defined by GUI (default=%d)\n", optical_flow);
338 printf(" -s --scale [1..n] Scale down by the given factor for quick and dirty previews (default=%d)\n", scale);
339 printf(" -t --test <test> Generate test images. Use 'help' for list of tests\n");
342 static void print_test_help()
344 printf(" -t --test <test> Generate test images...\n");
346 printf(" flow-next Optical flow plane to next image\n");
347 printf(" flow-prev Optical flow plane to previous image\n");
349 printf(" marked Only apply marked colors to grey image\n");
350 printf(" mask Show mask of marked areas\n");
351 printf(" mask+color Show mask of marked areas + color\n");
352 printf(" bc-only Only apply brightness+contrast, leave colors of grey image as is\n");
353 printf(" bc-image Show brightness+contrast change on grey image as uv components\n");
354 printf(" alpha Show the image with alpha channel only\n");
355 printf(" no-alpha Show the image without alpha channel, to see pixles that are made transparent\n");
356 printf(" removal-image Show the image with \"transparency removal layer\" only, but keep u and v\n");
363 int main(int argc, char *argv[])
365 darray_t *gI = NULL, *cI = NULL, *mI = NULL, *I = NULL;
366 darray_t *flow = NULL, *flow_i = NULL;
367 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;
370 int w = 0, h = 0, load_w, load_h, k = 1, index = 0, z;
371 unsigned short *img = NULL;
372 unsigned char *mark_buffer = NULL;
375 const char* filename;
376 char first_filename[256];
377 int seq_offset = 0, seq_next = 0;
378 int features, change_bc, change_alpha;
379 char *feat_names[20];
382 skip_args = handle_options(argc, argv);
392 if (argc > 1 && !strcmp(argv[1], "sequence")) {
393 k = load_sequence(&seq_offset, &seq_next, first_filename);
396 printf("Got %d frames from sequence (frames %d..%d)\n", k, seq_offset, seq_offset + k - 1);
397 if (argc > 2 && (!strcmp(argv[2], "list") || atoi(argv[2]) > seq_offset)) {
398 seq_offset = seq_next;
401 } else if (argc <= 3) {
404 } else if (argc > 5) {
406 index = atoi(argv[5]);
412 if (k > SINGLE_K_TEST)
418 change_bc = change_alpha = 0;
419 for (z = 0; z < k; z++) {
421 filename = sequence[z].filename;
422 /* first_filename is set by load_sequence */
425 strcpy(first_filename, argv[1]);
428 // load flow settings
429 if (sequence && optical_flow)
432 load_settings(first_filename);
434 // load flow settings
435 if (sequence && optical_flow) {
436 if (flow_enable == 0 && (test == FLOW_NEXT || test == FLOW_PREV)) {
437 fprintf(stderr, "Cannot test optical flow, because it is not enabled by GUI.\n");
442 // load original image and convert their RGB components to double RGB array
443 img = load_img(&load_w, &load_h, filename, index + z);
445 fprintf(stderr, "Failed to load grey image '%s'\n", filename);
448 scale_img(img, load_w, load_h, scale);
453 if (load_w/scale != w || load_h/scale != h) {
454 fprintf(stderr, "Error: All input images must have equal dimenstions.\n");
457 // now we know the dimensions, so we can create input arrays
459 dims[0] = w; dims[1] = h; dims[2] = 3; dims[3] = k;
460 gI = darrayCreate(4, dims);
463 printf("failed to create grey image array\n");
467 dims[0] = w; dims[1] = h; dims[2] = 3; dims[3] = k;
468 cI = darrayCreate(4, dims);
471 printf("failed to create marked image array\n");
474 img2array_short(img, w, h, darrayGetPr(gI) + w*h*3*z, w, h);
477 if (sequence || !strcmp(argv[2], "marked")) {
480 // load marked mask and convert their RGB components to double YUV array
481 memcpy(darrayGetPr(cI) + w*h*3*z, darrayGetPr(gI) + w*h*3*z, w*h*3 * sizeof(double));
482 /* add extra memory for unscaled data to prevent buffer overflow */
484 mark_buffer = (unsigned char *)malloc(w*h*k + load_w*load_h);
486 printf("no memory for mark buffer\n");
489 if (load_palette(first_filename)) {
490 printf("failed to load palette for file: '%s'\n", filename);
493 sprintf(name, filename, index + z);
494 /* always load full unscaled image, then scale down */
495 if (load_marked(mark_buffer + w*h*z, load_w, load_h, name) == 0) {
496 scale_mark(mark_buffer + w*h*z, load_w, load_h, scale);
497 ptr_cI = darrayGetPr(cI) + w*h*3*z;
498 for (y = 0; y < h; y++) {
499 for (x = 0; x < w; x++) {
500 /* do not apply mask on index 0 */
501 c = mark_buffer[y*w+x + w*h*z];
504 /* check for any brightness/contrast change */
505 if (bright_contrast && (mark_palette[c-1].bright != 0 || mark_palette[c-1].contrast != 1))
507 /* check for any alpha change */
508 if (alpha_change && mark_palette[c-1].alpha < 1)
510 /* do not apply white pixles, this meas: keep original color */
511 if (mark_palette[c-1].r == 255 && mark_palette[c-1].g == 255 && mark_palette[c-1].b == 255)
513 ptr_cI[y*w+x] = mark_palette[c-1].r / 255.0F;
514 ptr_cI[y*w+x + w*h] = mark_palette[c-1].g / 255.0F;
515 ptr_cI[y*w+x + w*h*2] = mark_palette[c-1].b / 255.0F;
519 memset(mark_buffer + w*h*z, 0, w*h);
521 // load marked image and convert their RGB components to double YUV array
522 img = load_img(&load_w, &load_h, argv[2], index + z);
524 scale_img(img, load_w, load_h, scale);
525 if (load_w/scale != w || load_h/scale != h) {
526 fprintf(stderr, "Error: All input images must have equal dimenstions.\n");
529 img2array_short(img, w, h, darrayGetPr(cI) + w*h*3*z, w, h);
532 fprintf(stderr, "Failed to load marked image, omitting...\n");
533 memcpy(darrayGetPr(cI) + w*h*3*z, darrayGetPr(gI) + w*h*3*z, w*h*3 * sizeof(double));
538 rc = alloc_I_arrays(&I, &mI, w, h, k, &features, feat_names, change_alpha, change_bc);
542 for (z = 0; z < k; z++) {
543 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);
544 ptr_gI = darrayGetPr(gI) + w*h*3*z;
545 ptr_cI = darrayGetPr(cI) + w*h*3*z;
547 // convert original image into YUV
548 rgb2yuv(ptr_gI, ptr_gI, w, h);
549 // convert maked image into YUV
550 rgb2yuv(ptr_cI, ptr_cI, w, h);
552 if (sequence || !strcmp(argv[2], "marked")) {
554 // use marked mask to fill markIm
555 for (y = 0; y < h; y++) {
556 for (x = 0; x < w; x++) {
557 /* do not apply mask on index 0 */
558 c = mark_buffer[y*w+x + w*h*z];
566 // fill color image with marked pixles
567 // - calculate the difference between two images (original image - color image)
568 // - convert into absolute (positive values)
569 // - sum all components to get grey image
570 // - apply threshold (pixle is 1F, if the absolute difference is > 0.01F)
571 // original code: markIm=(sum(abs(gI-cI),3)>0.01); (according to developers of the algorithm)
572 for (i = 0, ii = w * h; i < ii; i++) {
574 sum = ptr_gI[i] - ptr_cI[i];
579 sum = ptr_gI[i + ii] - ptr_cI[i + ii];
584 sum = ptr_gI[i + ii + ii] - ptr_cI[i + ii + ii];
596 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);
600 if (k > 1 && flow_enable) {
601 /* create flow vectors */
602 dims[0] = w; dims[1] = h; dims[2] = k - 1; dims[3] = 2;
603 flow = darrayCreate(4, dims);
605 printf("failed to create array\n");
608 flow_i = darrayCreate(4, dims);
610 printf("failed to create array\n");
613 printf("Calculating optical flow for %d frames: window=%d\n", k, flow_window/scale);
615 printf("Note: Optical flow is not activated!\n");
616 for (z = 0; z < k-1; z++) {
618 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);
620 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);
624 printf("Note: Optical flow is not compiled in!\n");
632 if (test != FLOW_NEXT && test != FLOW_PREV && test != MARKED && test != MASK && test != MASK_COLOR && test != BC_ONLY && test != BC_IMAGE) {
633 printf("Colorizing %d frames, please wait...\n", k);
634 rc = colorize(I, mI, flow, flow_i, in_itr_num, out_itr_num, quick, scalexyz, feat_names, NULL);
637 printf("No memory! Use smaller frames or less frames between key frames or add more memory.");
639 printf("No memory! Use smaller image or add more memory.");
644 for (z = 0; z < k; z++) {
645 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);
646 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);
649 if (test == FLOW_NEXT || test == FLOW_PREV) {
650 double *ptr_f1 = NULL, *ptr_f2 = NULL;
651 /* apply flow planes to result image as u and y vector */
652 if (test == FLOW_NEXT) {
654 ptr_f1 = darrayGetPr(flow) + w*h*z;
655 ptr_f2 = darrayGetPr(flow) + w*h*z*(k-1);
659 ptr_f1 = darrayGetPr(flow_i) + w*h*z;
660 ptr_f2 = darrayGetPr(flow_i) + w*h*z*(k-1);
663 if (ptr_f1 && ptr_f1) {
664 for (y = 0; y < h; y++) {
665 for (x = 0; x < w; x++) {
668 ptr_u[w*y+x] = ptr_f1[w * y + x] / 50;
669 ptr_v[w*y+x] = ptr_f2[w * y + x] / 50;
680 // save result YUV array to image with RGB components
681 yuv2rgb(ptr_y, ptr_y, w, h);
683 static char name[256], *p, *q;
684 p = sequence[z].filename;
685 while((q = strchr(p, DIR_SEPERATOR)))
687 strcpy(name, sequence[z].filename);
688 name[p - sequence[z].filename] = '\0';
689 strcat(name, "colorized_");
694 /* don't save alpha on these tests */
695 if (test == ALPHA || test == REMOVAL_IMAGE || test == NO_ALPHA)
696 save_img_array(ptr_y, w, h, 0, filename, index + z);
698 save_img_array(ptr_y, w, h, change_alpha, filename, index + z);
702 printf("Elapsed time: %d minutes, %d seconds\n", (int)(end-start)/60, (int)(end-start)%60);
711 darrayDestroy(flow_i);
723 /* if end frame is not given or if not reached */
724 if (argc <= 3 || atoi(argv[3]) > seq_offset + k) {
725 seq_offset = seq_offset + k - 1;