8649a652937a47d3658c4e3772d8817fb64eafc3
[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;
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 != 4) {
54 usage:
55                 printf("This tool will split images to fields or join from frames.\n");
56                 printf("The odd lines (second line, fourth line, ...) is the first field.\n\n");
57                 printf("Usage: %s [--depth 8] frame2field <field> <frame> <first>\n", argv[0]);
58                 printf("       %s [--depth 8] field2frame <frame> <field> <first>\n", argv[0]);
59                 printf(" --depth <bits>   Change color depth from default %d to given bits\n", save_depth);
60                 return 0;
61         }
62
63         if (!strcmp(argv[_arg], "frame2field"))
64                 mode = MODE_FRAME_2_FIELD;
65         else if (!strcmp(argv[_arg], "field2frame"))
66                 mode = MODE_FIELD_2_FRAME;
67         else
68                 goto usage;
69
70         frame = field = atoi(argv[_arg+2]);
71
72 next_frame:
73         printf("\n\nProcessing frame %d, field %d+%d\n", frame, field, field+1);
74         if (mode == MODE_FRAME_2_FIELD) {
75                 frame_img = load_img(&width, &height, argv[_arg+1], frame++);
76                 if (!frame_img)
77                         goto out;
78                 if (!(field1_img = malloc(width * height/2 * 3 * sizeof(unsigned short))))
79                         goto out;
80                 if (!(field2_img = malloc(width * height/2 * 3 * sizeof(unsigned short))))
81                         goto out;
82                 split_image(frame_img, field1_img, field2_img, width, height);
83                 if (save_img(field1_img, width, height/2, 0, argv[_arg+2], field++))
84                         goto out;
85                 if (save_img(field2_img, width, height/2, 0, argv[_arg+2], field++))
86                         goto out;
87                 free(frame_img);
88                 free(field1_img);
89                 free(field2_img);
90                 goto next_frame;
91         }
92         if (mode == MODE_FIELD_2_FRAME) {
93                 field1_img = load_img(&width, &height, argv[_arg+1], field++);
94                 if (!field1_img)
95                         goto out;
96                 field2_img = load_img(&width, &height, argv[_arg+1], field++);
97                 if (!field2_img)
98                         goto out;
99                 if (!(frame_img = malloc(width * height*2 * 3 * sizeof(unsigned short))))
100                         goto out;
101                 join_image(field1_img, field2_img, frame_img, width, height*2);
102                 if (save_img(frame_img, width, height*2, 0, argv[_arg+2], frame++))
103                         goto out;
104                 free(field1_img);
105                 free(field2_img);
106                 free(frame_img);
107                 goto next_frame;
108         }
109
110 out:
111         return 0;
112 }