//using OpenCvSharp;
//using OpenCvSharp.DnnSuperres;
//using OpenCvSharp.Extensions;
using System.Drawing;
using System.Security.Cryptography;
using System.Web;


namespace ZionGraphics.Filtering
{
    class Resolution : IFilter
    {
        public override Bitmap Apply(Bitmap bitmap)
        {
            if (Intensity == 0) Intensity = 300;

            double proportion = (100.0 / bitmap.VerticalResolution) * Intensity;

            bitmap = new System.Drawing.Bitmap(bitmap,
                new System.Drawing.Size(
                    (int)(bitmap.Width * (proportion / 100)),
                    (int)(bitmap.Height * (proportion / 100))
                    ));

            bitmap.SetResolution(Intensity, Intensity);
            
            return bitmap;
        }
    }

    class SuperResolution : IFilter
    {
        /*Bitmap upscale(Bitmap original, int x, int y)
        {
            Mat img = BitmapConverter.ToMat(original);
            Rect roi;
            roi.X = x;
            roi.Y = y;
            roi.Width = 256;
            roi.Height = 256;
            img = new Mat(img, roi);

            const int scale = 2;
            DnnSuperResImpl sr = new DnnSuperResImpl();
            sr.ReadModel("LapSRN_x2.pb");
            sr.SetModel("lapsrn", scale);

            Mat outputImage = new Mat();
            Cv2.CvtColor(img, img, ColorConversionCodes.GRAY2BGR);

            Cv2.FastNlMeansDenoising(img, img, 3, 7, 21);

            sr.Upsample(img, outputImage);

            return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(outputImage);
        }*/

        public override Bitmap Apply(Bitmap bitmap)
        {
            /*Bitmap original = new Bitmap(((int)(bitmap.Width / 256) + 1) * 256, ((int)(bitmap.Height / 256) + 1) * 256);
            using (Graphics g = Graphics.FromImage(original))
            {
                g.Clear(Color.White);
                g.DrawImage(bitmap, new System.Drawing.Point(0, 0));
            }

            if (original.PixelFormat != System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
            {
                original = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(original);
            }

            Bitmap tempBitmap = new Bitmap(original.Width * 2, original.Height * 2);
            using (Graphics g = Graphics.FromImage(tempBitmap))
            {
                g.Clear(Color.White);
            }
            using (Graphics g = Graphics.FromImage(tempBitmap))
            {
                for (int i = 0; i < ((original.Width) / 256); i++)
                {
                    for (int j = 0; j < ((original.Height) / 256); j++)
                    {

                        g.DrawImage(upscale(original, i * 256, j * 256), new System.Drawing.Point(i * 256 * 2, j * 256 * 2));
                    }
                }
            }*/

            return bitmap;// tempBitmap;
        }
    }
}
