using AForge.Imaging.Filters;
using AForge.Imaging;
using System.Drawing;

namespace ZionGraphics.Filtering
{
    class Deskewing2_Grayscale : IFilter
    {
        public override Bitmap Apply(Bitmap bitmap)
        {
            if (bitmap.PixelFormat != System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
            {
                bitmap = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(bitmap);
            }
            // create instance of skew checker
            DocumentSkewChecker skewChecker = new DocumentSkewChecker();
            // get documents skew angle
            double angle = skewChecker.GetSkewAngle(bitmap);
            // create rotation filter
            RotateBilinear rotationFilter = new RotateBilinear(-angle);
            rotationFilter.FillColor = Color.White;
            // rotate image applying the filter
            bitmap = rotationFilter.Apply(bitmap);

            return bitmap;
        }
    }
}
