1b4739f2cd7adaee75ec64b0c9f13ef6a9daba98
[colorize.git] / tools / fields.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include "../src/ppm.h"
6
7 /* split interlaced image into two fields */
8 static void split_image(const unsigned char *source, unsigned char *dest1,
9         unsigned char *dest2, int width, int height)
10 {
11         int y;
12
13         for (y = 0; y < (height / 2); y++) {
14                 memcpy(dest2 + width * y * 3,
15                         source + width * y * 6, width * 3);
16                 memcpy(dest1 + width * y * 3,
17                         source + width * y * 6 + width * 3, width * 3);
18         }
19 }
20
21 /* joint two fields into interlaced image */
22 static void join_image(const unsigned char *source1, unsigned char *source2,
23         unsigned char *dest, int width, int height)
24 {
25         int y;
26
27         for (y = 0; y < (height / 2); y++) {
28                 memcpy(dest + width * y * 6,
29                         source2 + width * y * 3, width * 3);
30                 memcpy(dest + width * y * 6 + width * 3,
31                         source1 + width * y * 3, width * 3);
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 char *field1_img = NULL, *field2_img = NULL, *frame_img = NULL;
42         int width, height;
43         int frame, field;
44
45         if (argc <= 4) {
46 usage:
47                 printf("This tool will split images to fields or join from frames.\n");
48                 printf("The odd lines (second line, fourth line, ...) is the first field.\n\n");
49                 printf("Usage: %s frame2field <field> <frame> <first>\n", argv[0]);
50                 printf("       %s field2frame <frame> <field> <first>\n", argv[0]);
51                 return 0;
52         }
53
54         if (!strcmp(argv[1], "frame2field"))
55                 mode = MODE_FRAME_2_FIELD;
56         else if (!strcmp(argv[1], "field2frame"))
57                 mode = MODE_FIELD_2_FRAME;
58         else
59                 goto usage;
60
61         frame = field = atoi(argv[4]);
62
63 next_frame:
64         printf("\n\nProcessing frame %d, field %d+%d\n", frame, field, field+1);
65         if (mode == MODE_FRAME_2_FIELD) {
66                 if (load_img(-1, &frame_img, &width, &height, argv[2], frame++))
67                         goto out;
68                 if (!(field1_img = malloc(width * height/2 * 3)))
69                         goto out;
70                 if (!(field2_img = malloc(width * height/2 * 3)))
71                         goto out;
72                 split_image(frame_img, field1_img, field2_img, width, height);
73                 if (save_img(field1_img, width, height/2, argv[3], field++))
74                         goto out;
75                 if (save_img(field2_img, width, height/2, argv[3], field++))
76                         goto out;
77                 free(frame_img);
78                 free(field1_img);
79                 free(field2_img);
80                 goto next_frame;
81         }
82         if (mode == MODE_FIELD_2_FRAME) {
83                 if (load_img(-1, &field1_img, &width, &height, argv[2], field++))
84                         goto out;
85                 if (load_img(-1, &field2_img, &width, &height, argv[2], field++))
86                         goto out;
87                 if (!(frame_img = malloc(width * height*2 * 3)))
88                         goto out;
89                 join_image(field1_img, field2_img, frame_img, width, height*2);
90                 if (save_img(frame_img, width, height*2, argv[3], frame++))
91                         goto out;
92                 free(field1_img);
93                 free(field2_img);
94                 free(frame_img);
95                 goto next_frame;
96         }
97
98 out:
99         return 0;
100 }