5 #include "../src/ppm.h"
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)
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);
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)
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);
35 #define MODE_FRAME_2_FIELD 1
36 #define MODE_FIELD_2_FRAME 2
39 int main(int argc, char *argv[])
41 unsigned char *field1_img = NULL, *field2_img = NULL, *frame_img = NULL;
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]);
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;
61 frame = field = atoi(argv[4]);
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++))
68 if (!(field1_img = malloc(width * height/2 * 3)))
70 if (!(field2_img = malloc(width * height/2 * 3)))
72 split_image(frame_img, field1_img, field2_img, width, height);
73 if (save_img(field1_img, width, height/2, argv[3], field++))
75 if (save_img(field2_img, width, height/2, argv[3], field++))
82 if (mode == MODE_FIELD_2_FRAME) {
83 if (load_img(-1, &field1_img, &width, &height, argv[2], field++))
85 if (load_img(-1, &field2_img, &width, &height, argv[2], field++))
87 if (!(frame_img = malloc(width * height*2 * 3)))
89 join_image(field1_img, field2_img, frame_img, width, height*2);
90 if (save_img(frame_img, width, height*2, argv[3], frame++))