using Hawkeye.VisionBuilder.Workflow.Datatypes;
using Hawkeye.VisionBuilder.Workflow.Operations.Attributes;
using OpenCvSharp;

namespace Hawkeye.VisionBuilder.Workflow.Operations.Transformations;

[Category("Transformations")]
public class DistanceTransformOperation:BaseOperation
{
    protected override void InterpretInternal(Context context)
    {
        if (!CheckImageExists(context)) return;
        if(!CheckGrayscale(context))return;
        
        Mat res=new Mat(context.ActiveImage.ImageData.Size(),MatType.CV_8UC1);
        var distance = context.ActiveImage.ImageData.DistanceTransform(DistanceTypes.C, DistanceTransformMasks.Mask3);
        
        Cv2.MinMaxLoc(distance, out double minVal, out var maxVal);
        // clamp the distance transform to 0-255
        Cv2.Normalize(distance, res, 0, maxVal, NormTypes.MinMax, (int)MatType.CV_8UC1);



        context.ActiveImage = new HawkeyeImage()
        {
            ImageData = res
        };

        Status = $"Distance transform";
        Result = true;
    }
}