package com.kdanmobile.android.animationdesk.videoencode;

import android.graphics.Bitmap;
import android.graphics.Color;

import java.io.File;


public class FFMPEGEncode {
    private static native int addImage(int data[]);
    private static native void jstringTostring(String filePath);
    private static native int encodeVideos(float width, float height, int speed, int counts);
    private static native void endEncode();

    private int width;
    private int height;
    private int speed;
    private int pixels[];

    static {
        System.loadLibrary("ffmpegutils");
    }

    public void start(int w, int h, int speed, int imageCount, File file) {
        this.width = w;
        this.height = h;
        this.speed = speed;
        this.pixels = new int[width * height * 4];
        jstringTostring(file.getPath());
        encodeVideos(width, height, speed, imageCount);
    }

    public void addImage(Bitmap bitmap) {
        int count = 0;
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int color = bitmap.getPixel(j, i);
                pixels[count * 4] = Color.red(color);
                pixels[count * 4 + 1] = Color.green(color);
                pixels[count * 4 + 2] = Color.blue(color);
                pixels[count * 4 + 3] = Color.alpha(color);
                count++;
            }
        }
        addImage(pixels);
    }

    public void end() {
        endEncode();
    }
}
