Fix and option for "fields"
[colorize.git] / tools / fields.c
index ca49e65..508afbc 100644 (file)
@@ -2,7 +2,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include "../src/ppm.h"
+#include "../src/img.h"
 
 /* split interlaced image into two fields */
 static void split_image(const unsigned short *source, unsigned short *dest1,
@@ -40,7 +40,7 @@ int main(int argc, char *argv[])
 {
        unsigned short *field1_img = NULL, *field2_img = NULL, *frame_img = NULL;
        int width, height;
-       int frame, field;
+       int frame, field, swap = 0;
        int _argc = argc - 1;
        int _arg = 1;
 
@@ -50,13 +50,20 @@ int main(int argc, char *argv[])
                _argc -= 2;
        }
 
+       if (_argc > 0 && !strcmp(argv[_arg], "--even")) {
+               swap = 1;
+               _arg += 1;
+               _argc -= 1;
+       }
+
        if (_argc != 4) {
 usage:
                printf("This tool will split images to fields or join from frames.\n");
                printf("The odd lines (second line, fourth line, ...) is the first field.\n\n");
-               printf("Usage: %s [--depth 8] frame2field <field> <frame> <first>\n", argv[0]);
-               printf("       %s [--depth 8] field2frame <frame> <field> <first>\n", argv[0]);
+               printf("Usage: %s [--depth 8] frame2field <frame> <field> <first>\n", argv[0]);
+               printf("       %s [--depth 8] field2frame <field> <frame> <first>\n", argv[0]);
                printf(" --depth <bits>   Change color depth from default %d to given bits\n", save_depth);
+               printf(" --even           Use even lines for first field and odd for second\n");
                return 0;
        }
 
@@ -67,21 +74,25 @@ usage:
        else
                goto usage;
 
-       frame = field = atoi(argv[_arg+2]);
+       frame = field = atoi(argv[_arg+3]);
 
 next_frame:
        printf("\n\nProcessing frame %d, field %d+%d\n", frame, field, field+1);
        if (mode == MODE_FRAME_2_FIELD) {
-               if (load_img(-1, &frame_img, &width, &height, argv[_arg+1], frame++))
+               frame_img = load_img(&width, &height, argv[_arg+1], frame++);
+               if (!frame_img)
                        goto out;
                if (!(field1_img = malloc(width * height/2 * 3 * sizeof(unsigned short))))
                        goto out;
                if (!(field2_img = malloc(width * height/2 * 3 * sizeof(unsigned short))))
                        goto out;
-               split_image(frame_img, field1_img, field2_img, width, height);
-               if (save_img(field1_img, width, height/2, argv[_arg+2], field++))
+               if (swap)
+                       split_image(frame_img, field2_img, field1_img, width, height);
+               else
+                       split_image(frame_img, field1_img, field2_img, width, height);
+               if (save_img(field1_img, width, height/2, 0, argv[_arg+2], field++))
                        goto out;
-               if (save_img(field2_img, width, height/2, argv[_arg+2], field++))
+               if (save_img(field2_img, width, height/2, 0, argv[_arg+2], field++))
                        goto out;
                free(frame_img);
                free(field1_img);
@@ -89,14 +100,19 @@ next_frame:
                goto next_frame;
        }
        if (mode == MODE_FIELD_2_FRAME) {
-               if (load_img(-1, &field1_img, &width, &height, argv[_arg+1], field++))
+               field1_img = load_img(&width, &height, argv[_arg+1], field++);
+               if (!field1_img)
                        goto out;
-               if (load_img(-1, &field2_img, &width, &height, argv[_arg+1], field++))
+               field2_img = load_img(&width, &height, argv[_arg+1], field++);
+               if (!field2_img)
                        goto out;
                if (!(frame_img = malloc(width * height*2 * 3 * sizeof(unsigned short))))
                        goto out;
-               join_image(field1_img, field2_img, frame_img, width, height*2);
-               if (save_img(frame_img, width, height*2, argv[_arg+2], frame++))
+               if (swap)
+                       join_image(field2_img, field1_img, frame_img, width, height*2);
+               else
+                       join_image(field1_img, field2_img, frame_img, width, height*2);
+               if (save_img(frame_img, width, height*2, 0, argv[_arg+2], frame++))
                        goto out;
                free(field1_img);
                free(field2_img);