Fix and option for "fields"
[colorize.git] / tools / fields.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include "../src/img.h"
6
7 /* split interlaced image into two fields */
8 static void split_image(const unsigned short *source, unsigned short *dest1,
9         unsigned short *dest2, int width, int height)
10 {
11         int y;
12
13         for (y = 0; y < (height / 2); y++) {
14                 memcpy(dest2 + width * y * 3, source + width * y * 6,
15                         width * 3 * sizeof(unsigned short));
16                 memcpy(dest1 + width * y * 3, source + width * y * 6 + width * 3,
17                         width * 3 * sizeof(unsigned short));
18         }
19 }
20
21 /* joint two fields into interlaced image */
22 static void join_image(const unsigned short *source1, unsigned short *source2,
23         unsigned short *dest, int width, int height)
24 {
25         int y;
26
27         for (y = 0; y < (height / 2); y++) {
28                 memcpy(dest + width * y * 6, source2 + width * y * 3,
29                         width * 3 * sizeof(unsigned short));
30                 memcpy(dest + width * y * 6 + width * 3, source1 + width * y * 3,
31                         width * 3 * sizeof(unsigned short));
32         }
33 }
34
35 #define MODE_FRAME_2_FIELD 1
36 #define MODE_FIELD_2_FRAME 2
37 int mode;
38
39 int main(int argc, char *argv[])
40 {
41         unsigned short *field1_img = NULL, *field2_img = NULL, *frame_img = NULL;
42         int width, height;
43         int frame, field, swap = 0;
44         int _argc = argc - 1;
45         int _arg = 1;
46
47         if (_argc > 1 && !strcmp(argv[_arg], "--depth")) {
48                 save_depth = atoi(argv[_arg+1]);
49                 _arg += 2;
50                 _argc -= 2;
51         }
52
53         if (_argc > 0 && !strcmp(argv[_arg], "--even")) {
54                 swap = 1;
55                 _arg += 1;
56                 _argc -= 1;
57         }
58
59         if (_argc != 4) {
60 usage:
61                 printf("This tool will split images to fields or join from frames.\n");
62                 printf("The odd lines (second line, fourth line, ...) is the first field.\n\n");
63                 printf("Usage: %s [--depth 8] frame2field <frame> <field> <first>\n", argv[0]);
64                 printf("       %s [--depth 8] field2frame <field> <frame> <first>\n", argv[0]);
65                 printf(" --depth <bits>   Change color depth from default %d to given bits\n", save_depth);
66                 printf(" --even           Use even lines for first field and odd for second\n");
67                 return 0;
68         }
69
70         if (!strcmp(argv[_arg], "frame2field"))
71                 mode = MODE_FRAME_2_FIELD;
72         else if (!strcmp(argv[_arg], "field2frame"))
73                 mode = MODE_FIELD_2_FRAME;
74         else
75                 goto usage;
76
77         frame = field = atoi(argv[_arg+3]);
78
79 next_frame:
80         printf("\n\nProcessing frame %d, field %d+%d\n", frame, field, field+1);
81         if (mode == MODE_FRAME_2_FIELD) {
82                 frame_img = load_img(&width, &height, argv[_arg+1], frame++);
83                 if (!frame_img)
84                         goto out;
85                 if (!(field1_img = malloc(width * height/2 * 3 * sizeof(unsigned short))))
86                         goto out;
87                 if (!(field2_img = malloc(width * height/2 * 3 * sizeof(unsigned short))))
88                         goto out;
89                 if (swap)
90                         split_image(frame_img, field2_img, field1_img, width, height);
91                 else
92                         split_image(frame_img, field1_img, field2_img, width, height);
93                 if (save_img(field1_img, width, height/2, 0, argv[_arg+2], field++))
94                         goto out;
95                 if (save_img(field2_img, width, height/2, 0, argv[_arg+2], field++))
96                         goto out;
97                 free(frame_img);
98                 free(field1_img);
99                 free(field2_img);
100                 goto next_frame;
101         }
102         if (mode == MODE_FIELD_2_FRAME) {
103                 field1_img = load_img(&width, &height, argv[_arg+1], field++);
104                 if (!field1_img)
105                         goto out;
106                 field2_img = load_img(&width, &height, argv[_arg+1], field++);
107                 if (!field2_img)
108                         goto out;
109                 if (!(frame_img = malloc(width * height*2 * 3 * sizeof(unsigned short))))
110                         goto out;
111                 if (swap)
112                         join_image(field2_img, field1_img, frame_img, width, height*2);
113                 else
114                         join_image(field1_img, field2_img, frame_img, width, height*2);
115                 if (save_img(frame_img, width, height*2, 0, argv[_arg+2], frame++))
116                         goto out;
117                 free(field1_img);
118                 free(field2_img);
119                 free(frame_img);
120                 goto next_frame;
121         }
122
123 out:
124         return 0;
125 }