package com.lemenzo.gallery.editor;

import android.graphics.Bitmap;
import android.graphics.Matrix;

/** Stateless bitmap rotation/flip helpers used by the local editor. */
public final class RotateController {
    private RotateController() {}

    public static Bitmap rotate(Bitmap source, float degrees) {
        if (source == null) throw new IllegalArgumentException("source == null");
        Matrix matrix = new Matrix();
        matrix.postRotate(degrees);
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
    }

    public static Bitmap flipHorizontal(Bitmap source) {
        return flip(source, true, false);
    }

    public static Bitmap flipVertical(Bitmap source) {
        return flip(source, false, true);
    }

    private static Bitmap flip(Bitmap source, boolean horizontal, boolean vertical) {
        if (source == null) throw new IllegalArgumentException("source == null");
        Matrix matrix = new Matrix();
        matrix.setScale(horizontal ? -1f : 1f, vertical ? -1f : 1f,
                source.getWidth() / 2f, source.getHeight() / 2f);
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
    }
}
