Fix and option for "fields"
[colorize.git] / tools / fields.c
index 1b4739f..508afbc 100644 (file)
@@ -2,33 +2,33 @@
 #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 char *source, unsigned char *dest1,
-       unsigned char *dest2, int width, int height)
+static void split_image(const unsigned short *source, unsigned short *dest1,
+       unsigned short *dest2, int width, int height)
 {
        int y;
 
        for (y = 0; y < (height / 2); y++) {
-               memcpy(dest2 + width * y * 3,
-                       source + width * y * 6, width * 3);
-               memcpy(dest1 + width * y * 3,
-                       source + width * y * 6 + width * 3, width * 3);
+               memcpy(dest2 + width * y * 3, source + width * y * 6,
+                       width * 3 * sizeof(unsigned short));
+               memcpy(dest1 + width * y * 3, source + width * y * 6 + width * 3,
+                       width * 3 * sizeof(unsigned short));
        }
 }
 
 /* joint two fields into interlaced image */
-static void join_image(const unsigned char *source1, unsigned char *source2,
-       unsigned char *dest, int width, int height)
+static void join_image(const unsigned short *source1, unsigned short *source2,
+       unsigned short *dest, int width, int height)
 {
        int y;
 
        for (y = 0; y < (height / 2); y++) {
-               memcpy(dest + width * y * 6,
-                       source2 + width * y * 3, width * 3);
-               memcpy(dest + width * y * 6 + width * 3,
-                       source1 + width * y * 3, width * 3);
+               memcpy(dest + width * y * 6, source2 + width * y * 3,
+                       width * 3 * sizeof(unsigned short));
+               memcpy(dest + width * y * 6 + width * 3, source1 + width * y * 3,
+                       width * 3 * sizeof(unsigned short));
        }
 }
 
@@ -38,41 +38,61 @@ int mode;
 
 int main(int argc, char *argv[])
 {
-       unsigned char *field1_img = NULL, *field2_img = NULL, *frame_img = NULL;
+       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;
 
-       if (argc <= 4) {
+       if (_argc > 1 && !strcmp(argv[_arg], "--depth")) {
+               save_depth = atoi(argv[_arg+1]);
+               _arg += 2;
+               _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 frame2field <field> <frame> <first>\n", argv[0]);
-               printf("       %s 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;
        }
 
-       if (!strcmp(argv[1], "frame2field"))
+       if (!strcmp(argv[_arg], "frame2field"))
                mode = MODE_FRAME_2_FIELD;
-       else if (!strcmp(argv[1], "field2frame"))
+       else if (!strcmp(argv[_arg], "field2frame"))
                mode = MODE_FIELD_2_FRAME;
        else
                goto usage;
 
-       frame = field = atoi(argv[4]);
+       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[2], frame++))
+               frame_img = load_img(&width, &height, argv[_arg+1], frame++);
+               if (!frame_img)
                        goto out;
-               if (!(field1_img = malloc(width * height/2 * 3)))
+               if (!(field1_img = malloc(width * height/2 * 3 * sizeof(unsigned short))))
                        goto out;
-               if (!(field2_img = malloc(width * height/2 * 3)))
+               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[3], 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[3], field++))
+               if (save_img(field2_img, width, height/2, 0, argv[_arg+2], field++))
                        goto out;
                free(frame_img);
                free(field1_img);
@@ -80,14 +100,19 @@ next_frame:
                goto next_frame;
        }
        if (mode == MODE_FIELD_2_FRAME) {
-               if (load_img(-1, &field1_img, &width, &height, argv[2], 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[2], field++))
+               field2_img = load_img(&width, &height, argv[_arg+1], field++);
+               if (!field2_img)
                        goto out;
-               if (!(frame_img = malloc(width * height*2 * 3)))
+               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[3], 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);