using System;
using OpenCvSharp;

namespace OpenCVStudio_v0
{
    public sealed class GetRectSubPixExecutor : IImageBlockExecutor
    {
        public string BlockType => "GetRectSubPix";

        public Mat Execute(Mat input, ProjectBlock block, ProjectManager projectManager)
        {
            if (input == null)
                throw new ArgumentNullException(nameof(input));
            if (input.Empty())
            {
                throw new InvalidOperationException("GetRectSubPix: изображение пустое.");
            }
            double centerX = projectManager.GetBlockParameter(block.Id, "CenterX", input.Width / 2.0);
            double centerY = projectManager.GetBlockParameter(block.Id, "CenterY", input.Height / 2.0);
            int width = projectManager.GetBlockParameter(block.Id, "Width", input.Width);
            int height = projectManager.GetBlockParameter(block.Id, "Height", input.Height);
            Mat result = new Mat();
            Cv2.GetRectSubPix(input, new OpenCvSharp. Size(width, height), new Point2f((float)centerX, (float)centerY), result);
            return result;
        }
    }
}