MrBelarus_Image-Processing/ImageProcessing/Code/Core/Processers/ConvertToBinaryS9.cs

namespace ImageProcessing.Core.Processers {
    public class ConvertToBinaryS9 : ImageProcesser {
        public override ImageData Process(ImageData image) {
            int imgClrDepth = image.ColorDepth;
            if (imgClrDepth == 32) {
                //null alpha
                for (int y = 0; y < image.Height; y++) {
                    for (int x = 0; x < image.Width; x++) {
                        int pixel = image.GetPixel(x, y);
                        image.SetPixel(x, y, pixel & 0x00ffffff);
                    }
                }
            }
            image.ApplyChanges();
            int[] pxls = image.GetPixels();
            int[] a8_matrix = ImageMatrixCalculator.GetA8(image);
            int imgWidth = image.Width;

            for (int y = 0; y < image.Height; y++) {
                for (int x = 0; x < imgWidth; x++) {
                    int i = y * imgWidth + x;
                    int pixel = pxls[i];
                    image.SetPixel(x, y,
                        ((a8_matrix[i] + pixel) / 9) < pixel ? 0 : 0x00ffffff);
                }
            }
            image.ApplyChanges();
            image = Utils.ImageUtility.ConvertToBinary(image, true, 128);

            return image;
        }
    }
}
