/*
 *  makemovie     read image names from stdin and create a movie
 *  Copyright (c) 2009 Sam Hocevar <sam@hocevar.net>
 *                All Rights Reserved
 *
 *  $Id$
 *
 *  This program is free software. It comes without any warranty, to
 *  the extent permitted by applicable law. You can redistribute it
 *  and/or modify it under the terms of the Do What The Fuck You Want
 *  To Public License, Version 2, as published by Sam Hocevar. See
 *  http://sam.zoy.org/wtfpl/COPYING for more details.
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <pipi.h>

#define WIDTH 1280
#define HEIGHT 720

int main(int argc, char *argv[])
{
    char file[1024];
    pipi_image_t *image;
    pipi_sequence_t *seq;
    pipi_pixels_t *p;
    int width, height, fps, par_num, par_den, bitrate;
    int f;

    width = 1280;
    height = 720;
    fps = 30;
    par_num = 1;
    par_den = 1;
    bitrate = 16 * 1024 * 1024;

    if(argc < 2)
    {
        fprintf(stderr, "usage: makemovie FILE [width [height [fps [par_num [par_den [bitrate]]]]]]>\n");
        return EXIT_FAILURE;
    }

    if (argc > 2)
        width = atoi(argv[2]);
    if (argc > 3)
        height = atoi(argv[3]);
    if (argc > 4)
        fps = atoi(argv[4]);
    if (argc > 5)
        par_num = atoi(argv[5]);
    if (argc > 6)
        par_den = atoi(argv[6]);
    if (argc > 7)
        bitrate = atoi(argv[7]);

    seq = pipi_open_sequence(argv[1], width, height, fps,
                             par_num, par_den, bitrate);
    if(!seq)
        return EXIT_FAILURE;

    for(f = 0; ; f++)
    {
        uint8_t *start;
        int w = 0, h = 0;

        if(!fgets(file, sizeof(file), stdin))
            break;
        file[strlen(file) - 1] = '\0';

        image = pipi_load(file);
        if(!image)
            return EXIT_FAILURE;
        p = pipi_get_pixels(image, PIPI_PIXELS_RGBA_U8);
        start = (uint8_t *)p->pixels;
        pipi_feed_sequence(seq, start, p->w, p->h);
        pipi_free(image);

        fprintf(stderr, "frame %d\r", f);
    }

    fprintf(stderr, "\n");

    pipi_close_sequence(seq);

    return EXIT_SUCCESS;
}