8 #include "../lib/darray.h"
9 #include "../lib/colorize.h"
14 #include "dir_seperator.h"
16 #include "opticalflow.h"
19 static void print_help(const char *app);
20 static void print_test_help();
23 //#define SINGLE_K_TEST 2
32 static struct sequence *sequence = NULL;
34 /* load sequence list and return number of images in sequence */
35 static int load_sequence(int *start, int *next, char *first_filename)
37 const char *name = "sequence";
39 int i, j, keyframe_before_start, eof;
43 /* free previously used sequence */
49 /* get number of frames until next keyframe or end of squence */
50 fp = fopen(name, "r");
52 printf("failed to load sequence '%s'\n", name);
58 keyframe_before_start = 0;
59 while(fgets(buffer, sizeof(buffer), fp)) {
60 p = strchr(buffer + 1, '\"');
65 strcpy(first_filename, buffer + 1);
67 if ((*start) && j == (*start) - 1) {
68 if (strstr(p, "keyframe"))
69 keyframe_before_start = 1;
76 printf("counting %d\n", j);
80 /* skip start frame, since it is always a keyframe */
83 if (strstr(p, "keyframe")) {
90 /* end of file case */
103 #ifdef DEBUG_SEQUENCE
104 puts("end of file at last frame");
109 /* we are done, because we already had the last sequence */
110 if (!keyframe_before_start) {
111 #ifdef DEBUG_SEQUENCE
112 puts("no keyframe before start");
117 #ifdef DEBUG_SEQUENCE
118 puts("keyframe before start");
120 /* we just have a single last frame */
122 (*next) = (*start) + 1;
124 /* if we have two keyframes side by side, we ignore it */
125 if (count == 2 && !eof) {
126 #ifdef DEBUG_SEQUENCE
129 /* only if the frame before start is not a keyframe
130 * and we are not the first sequence, because that would mean
131 * that the first frame is a single sequence */
132 if (!keyframe_before_start && (*start) > 0) {
133 #ifdef DEBUG_SEQUENCE
134 puts("no keyframe before start");
139 #ifdef DEBUG_SEQUENCE
140 puts("keyframe before start or first frame");
142 /* now the start frame has one keyframe before and one after */
144 (*next) = (*start) + 1;
146 (*next) = (*start) + count - 1;
149 /* alloc memory for sequence and fill with data */
150 sequence = (struct sequence *)malloc(sizeof(struct sequence) * count);
152 printf("no memory for sequence\n");
155 memset((struct sequence *)sequence, 0, sizeof(struct sequence) * count);
156 fp = fopen(name, "r");
158 printf("failed to load sequence '%s'\n", name);
163 while(fgets(buffer, sizeof(buffer), fp)) {
164 p = strchr(buffer + 1, '\"');
172 strcpy(sequence[i].filename, buffer + 1);
186 static int in_itr_num = 5, out_itr_num = 1, optical_flow = 1, bright_contrast = 1, alpha_change = 1;
187 int scale = 1, scalexyz = 999;
189 static enum test test = NO_TEST;
191 static int parse_test(const char *arg)
193 if (!strcmp(arg, "help")) {
198 if (!strcmp(arg, "flow-next"))
200 if (!strcmp(arg, "flow-prev"))
203 if (!strcmp(arg, "marked"))
205 if (!strcmp(arg, "mask"))
207 if (!strcmp(arg, "mask+color"))
209 if (!strcmp(arg, "bc-only"))
211 if (!strcmp(arg, "bc-image"))
213 if (!strcmp(arg, "alpha"))
215 if (!strcmp(arg, "no-alpha"))
217 if (!strcmp(arg, "removal-image"))
218 return REMOVAL_IMAGE;
223 static int handle_options(int argc, char **argv)
228 int option_index = 0, c;
229 static struct option long_options[] = {
231 {"depth", 1, 0, 'd'},
232 {"in-itr-num", 1, 0, 'i'},
233 {"out-itr-num", 1, 0, 'o'},
234 {"zscale", 1, 0, 'z'},
235 {"brightness-contrast", 1, 0, 'b'},
236 {"optical-flow", 1, 0, 'f'},
237 {"scale", 1, 0, 's'},
242 c = getopt_long(argc, argv, "hd:i:o:z:b:f:s:t:", long_options, &option_index);
252 save_depth = atoi(optarg);
256 in_itr_num = atoi(optarg);
260 out_itr_num = atoi(optarg);
264 scalexyz = atoi(optarg);
268 bright_contrast = atoi(optarg);
272 alpha_change = atoi(optarg);
276 optical_flow = atoi(optarg);
280 scale = atoi(optarg);
284 test = parse_test(optarg);
286 fprintf(stderr, "Invalid test '%s', use '--test help' to get a list of tests\n", optarg);
303 static void print_help(const char *app)
305 printf("Colorize version %s\n\n",
306 #include "../version.h"
308 printf("Usage: %s [options] <grey ppm image> <marked ppm image> <result ppm image> [<frames> <start>]\n", app);
309 printf(" Colorize grey image using marked image and save to result image.\n");
310 printf(" If frames and start frame is given, image names must include printf integer formatting (e.g. %%04d).\n");
311 printf("Usage: %s [options] <grey ppm image> marked <result ppm image>\n", app);
312 printf(" Colorize grey image using marked mask + palette and save to result image.\n");
313 printf("Usage: %s [options] sequence [list | <start with frame> [<stop with frame>]]\n", app);
314 printf(" Colorize movie sequence (generated by colorize gui) as found in the current directory.\n");
315 printf(" Use list to view sequence segments between keyframes.\n");
316 printf("\nOptions:\n");
317 printf(" -h --help This help\n");
318 printf(" -d --depth <bits> Save images with given color bit depth (default=%d)\n", save_depth);
319 printf(" -i --in-itr-num <num> Alter inner iterations (weightening count) of colorization algorithm (default=%d)\n", in_itr_num);
320 printf(" -o --out-itr-num <num> Alter outer iterations (complete turns) of colorization algorithm (default=%d)\n", out_itr_num);
321 printf(" -z --zscale <levels> How many grids (staring with the finest) should be scaled in z direction to generate the next coarse grid ");
323 printf("(default=%d)\n", scalexyz);
325 printf("(default=infinite)\n");
326 printf(" -b --brightness-contrast [0 | 1] Apply brightnes and contrast, if defined in palette by GUI (default=%d)\n", bright_contrast);
327 printf(" -a --alpha-change [0 | 1] Apply alpha channel change, if defined in palette by GUI (default=%d)\n", alpha_change);
329 printf(" -f --optical-flow [0 | 1] Apply optical flow, if defined by GUI (default=%d)\n", optical_flow);
331 printf(" -s --scale [1..n] Scale down by the given factor for quick and dirty previews (default=%d)\n", scale);
332 printf(" -t --test <test> Generate test images. Use 'help' for list of tests\n");
335 static void print_test_help()
337 printf(" -t --test <test> Generate test images...\n");
339 printf(" flow-next Optical flow plane to next image\n");
340 printf(" flow-prev Optical flow plane to previous image\n");
342 printf(" marked Only apply marked colors to grey image\n");
343 printf(" mask Show mask of marked areas\n");
344 printf(" mask+color Show mask of marked areas + color\n");
345 printf(" bc-only Only apply brightness+contrast, leave colors of grey image as is\n");
346 printf(" bc-image Show brightness+contrast change on grey image as uv components\n");
347 printf(" alpha Show the image with alpha channel only\n");
348 printf(" no-alpha Show the image without alpha channel, to see pixles that are made transparent\n");
349 printf(" removal-image Show the image with \"transparency removal layer\" only, but keep u and v\n");
356 int main(int argc, char *argv[])
358 darray_t *gI = NULL, *cI = NULL, *mI = NULL, *I = NULL;
359 darray_t *flow = NULL, *flow_i = NULL;
360 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;
363 int w = 0, h = 0, load_w, load_h, k = 1, index = 0, z;
364 unsigned short *img = NULL;
365 unsigned char *mark_buffer = NULL;
368 const char* filename;
369 char first_filename[256];
370 int seq_offset = 0, seq_next = 0;
371 int features, change_bc, change_alpha;
372 char *feat_names[20];
375 skip_args = handle_options(argc, argv);
385 if (argc > 1 && !strcmp(argv[1], "sequence")) {
386 k = load_sequence(&seq_offset, &seq_next, first_filename);
389 printf("Got %d frames from sequence (frames %d..%d)\n", k, seq_offset, seq_offset + k - 1);
390 if (argc > 2 && (!strcmp(argv[2], "list") || atoi(argv[2]) > seq_offset)) {
391 seq_offset = seq_next;
394 } else if (argc <= 3) {
397 } else if (argc > 5) {
399 index = atoi(argv[5]);
405 if (k > SINGLE_K_TEST)
411 change_bc = change_alpha = 0;
412 for (z = 0; z < k; z++) {
414 filename = sequence[z].filename;
415 /* first_filename is set by load_sequence */
418 strcpy(first_filename, argv[1]);
421 // load flow settings
422 if (sequence && optical_flow) {
424 load_flow(first_filename);
425 if (flow_enable == 0 && (test == FLOW_NEXT || test == FLOW_PREV)) {
426 fprintf(stderr, "Cannot test optical flow, because it is not enabled by GUI.\n");
431 // load original image and convert their RGB components to double RGB array
432 img = load_img(&load_w, &load_h, filename, index + z);
434 fprintf(stderr, "Failed to load grey image '%s'\n", filename);
437 scale_img(img, load_w, load_h, scale);
442 if (load_w/scale != w || load_h/scale != h) {
443 fprintf(stderr, "Error: All input images must have equal dimenstions.\n");
446 // now we know the dimensions, so we can create input arrays
448 dims[0] = w; dims[1] = h; dims[2] = 3; dims[3] = k;
449 gI = darrayCreate(4, dims);
452 printf("failed to create grey image array\n");
456 dims[0] = w; dims[1] = h; dims[2] = 3; dims[3] = k;
457 cI = darrayCreate(4, dims);
460 printf("failed to create marked image array\n");
463 img2array_short(img, w, h, darrayGetPr(gI) + w*h*3*z, w, h);
466 if (sequence || !strcmp(argv[2], "marked")) {
469 // load marked mask and convert their RGB components to double YUV array
470 memcpy(darrayGetPr(cI) + w*h*3*z, darrayGetPr(gI) + w*h*3*z, w*h*3 * sizeof(double));
471 /* add extra memory for unscaled data to prevent buffer overflow */
473 mark_buffer = (unsigned char *)malloc(w*h*k + load_w*load_h);
475 printf("no memory for mark buffer\n");
478 if (load_palette(first_filename)) {
479 printf("failed to load palette for file: '%s'\n", filename);
482 sprintf(name, filename, index + z);
483 /* always load full unscaled image, then scale down */
484 if (load_marked(mark_buffer + w*h*z, load_w, load_h, name) == 0) {
485 scale_mark(mark_buffer + w*h*z, load_w, load_h, scale);
486 ptr_cI = darrayGetPr(cI) + w*h*3*z;
487 for (y = 0; y < h; y++) {
488 for (x = 0; x < w; x++) {
489 /* do not apply mask on index 0 */
490 c = mark_buffer[y*w+x + w*h*z];
493 /* check for any brightness/contrast change */
494 if (bright_contrast && (mark_palette[c-1].bright != 0 || mark_palette[c-1].contrast != 1))
496 /* check for any alpha change */
497 if (alpha_change && mark_palette[c-1].alpha < 1)
499 /* do not apply white pixles, this meas: keep original color */
500 if (mark_palette[c-1].r == 255 && mark_palette[c-1].g == 255 && mark_palette[c-1].b == 255)
502 ptr_cI[y*w+x] = mark_palette[c-1].r / 255.0F;
503 ptr_cI[y*w+x + w*h] = mark_palette[c-1].g / 255.0F;
504 ptr_cI[y*w+x + w*h*2] = mark_palette[c-1].b / 255.0F;
508 memset(mark_buffer + w*h*z, 0, w*h);
510 // load marked image and convert their RGB components to double YUV array
511 img = load_img(&load_w, &load_h, argv[2], index + z);
513 scale_img(img, load_w, load_h, scale);
514 if (load_w/scale != w || load_h/scale != h) {
515 fprintf(stderr, "Error: All input images must have equal dimenstions.\n");
518 img2array_short(img, w, h, darrayGetPr(cI) + w*h*3*z, w, h);
521 fprintf(stderr, "Failed to load marked image, omitting...\n");
522 memcpy(darrayGetPr(cI) + w*h*3*z, darrayGetPr(gI) + w*h*3*z, w*h*3 * sizeof(double));
527 rc = alloc_I_arrays(&I, &mI, w, h, k, &features, feat_names, change_alpha, change_bc);
531 for (z = 0; z < k; z++) {
532 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);
533 ptr_gI = darrayGetPr(gI) + w*h*3*z;
534 ptr_cI = darrayGetPr(cI) + w*h*3*z;
536 // convert original image into YUV
537 rgb2yuv(ptr_gI, ptr_gI, w, h);
538 // convert maked image into YUV
539 rgb2yuv(ptr_cI, ptr_cI, w, h);
541 if (sequence || !strcmp(argv[2], "marked")) {
543 // use marked mask to fill markIm
544 for (y = 0; y < h; y++) {
545 for (x = 0; x < w; x++) {
546 /* do not apply mask on index 0 */
547 c = mark_buffer[y*w+x + w*h*z];
555 // fill color image with marked pixles
556 // - calculate the difference between two images (original image - color image)
557 // - convert into absolute (positive values)
558 // - sum all components to get grey image
559 // - apply threshold (pixle is 1F, if the absolute difference is > 0.01F)
560 // original code: markIm=(sum(abs(gI-cI),3)>0.01); (according to developers of the algorithm)
561 for (i = 0, ii = w * h; i < ii; i++) {
563 sum = ptr_gI[i] - ptr_cI[i];
568 sum = ptr_gI[i + ii] - ptr_cI[i + ii];
573 sum = ptr_gI[i + ii + ii] - ptr_cI[i + ii + ii];
585 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);
589 if (k > 1 && flow_enable) {
590 /* create flow vectors */
591 dims[0] = w; dims[1] = h; dims[2] = k - 1; dims[3] = 2;
592 flow = darrayCreate(4, dims);
594 printf("failed to create array\n");
597 flow_i = darrayCreate(4, dims);
599 printf("failed to create array\n");
602 printf("Calculating optical flow for %d frames: window=%d\n", k, flow_window/scale);
604 printf("Note: Optical flow is not activated!\n");
605 for (z = 0; z < k-1; z++) {
607 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);
609 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);
613 printf("Note: Optical flow is not compiled in!\n");
621 if (test != FLOW_NEXT && test != FLOW_PREV && test != MARKED && test != MASK && test != MASK_COLOR && test != BC_ONLY && test != BC_IMAGE) {
622 printf("Colorizing %d frames, please wait...\n", k);
623 rc = colorize(I, mI, flow, flow_i, in_itr_num, out_itr_num, scalexyz, feat_names);
626 printf("No memory! Use smaller frames or less frames between key frames or add more memory.");
628 printf("No memory! Use smaller image or add more memory.");
633 for (z = 0; z < k; z++) {
634 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);
635 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);
638 if (test == FLOW_NEXT || test == FLOW_PREV) {
639 double *ptr_f1 = NULL, *ptr_f2 = NULL;
640 /* apply flow planes to result image as u and y vector */
641 if (test == FLOW_NEXT) {
643 ptr_f1 = darrayGetPr(flow) + w*h*z;
644 ptr_f2 = darrayGetPr(flow) + w*h*z*(k-1);
648 ptr_f1 = darrayGetPr(flow_i) + w*h*z;
649 ptr_f2 = darrayGetPr(flow_i) + w*h*z*(k-1);
652 if (ptr_f1 && ptr_f1) {
653 for (y = 0; y < h; y++) {
654 for (x = 0; x < w; x++) {
657 ptr_u[w*y+x] = ptr_f1[w * y + x] / 50;
658 ptr_v[w*y+x] = ptr_f2[w * y + x] / 50;
669 // save result YUV array to image with RGB components
670 yuv2rgb(ptr_y, ptr_y, w, h);
672 static char name[256], *p, *q;
673 p = sequence[z].filename;
674 while((q = strchr(p, DIR_SEPERATOR)))
676 strcpy(name, sequence[z].filename);
677 name[p - sequence[z].filename] = '\0';
678 strcat(name, "colorized_");
683 /* don't save alpha on these tests */
684 if (test == ALPHA || test == REMOVAL_IMAGE || test == NO_ALPHA)
685 save_img_array(ptr_y, w, h, 0, filename, index + z);
687 save_img_array(ptr_y, w, h, change_alpha, filename, index + z);
691 printf("Elapsed time: %d minutes, %d seconds\n", (int)(end-start)/60, (int)(end-start)%60);
700 darrayDestroy(flow_i);
712 /* if end frame is not given or if not reached */
713 if (argc <= 3 || atoi(argv[3]) > seq_offset + k) {
714 seq_offset = seq_offset + k - 1;