From 222963da09164b9e9ef4bd4c4b9e94cdbf565da0 Mon Sep 17 00:00:00 2001 From: Aleksandr Kulikov Date: Thu, 12 Apr 2018 19:26:46 +0300 Subject: [PATCH] comeback --- src/Get3DModel/CalculatedBlock/ChangeImage.cs | 2 +- src/Get3DModel/Data/Image.cs | 76 ++++++++----------- 2 files changed, 31 insertions(+), 47 deletions(-) diff --git a/src/Get3DModel/CalculatedBlock/ChangeImage.cs b/src/Get3DModel/CalculatedBlock/ChangeImage.cs index e200936..ab04fd4 100644 --- a/src/Get3DModel/CalculatedBlock/ChangeImage.cs +++ b/src/Get3DModel/CalculatedBlock/ChangeImage.cs @@ -15,7 +15,7 @@ public Bitmap translateToMonochrome(Bitmap image) for (int y = 0; y < image.Height; ++y) for (int x = 0; x < image.Width; ++x) { - Color c = image.GetPixel(x, y); + Color c = ((Bitmap)image.Clone()).GetPixel(x, y); byte rgb = (byte)(0.3 * c.R + 0.59 * c.G + 0.11 * c.B); res.SetPixel(x, y, Color.FromArgb(c.A, rgb, rgb, rgb)); } diff --git a/src/Get3DModel/Data/Image.cs b/src/Get3DModel/Data/Image.cs index 9c9e3a4..8cee2ea 100644 --- a/src/Get3DModel/Data/Image.cs +++ b/src/Get3DModel/Data/Image.cs @@ -23,6 +23,7 @@ public class Image : IDisposable, IEnumerable /// public Color DefaultColor { get; set; } private byte[] data;//буфер исходного изображения + private byte[] outData;//выходной буфер private int stride; private BitmapData bmpData; @@ -39,27 +40,27 @@ public Image(string pathImage) string name = infoImage.Name.Replace(".png", ""); string[] nameSplit = name.Split('_'); _tall = Convert.ToDouble(nameSplit[1]); - bmpData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); - stride = bmpData.Stride; - data = new byte[stride * image.Height]; - System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, data, 0, data.Length); - DefaultColor = Color.Silver; + } /// /// Создание обертки поверх bitmap. /// - public Image(Bitmap image) + /// Копирует исходное изображение в выходной буфер + public Image(Bitmap image, double tall = 0, bool copySourceToOutput = false) { this._image = image; + this._tall = tall; + bmpData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); stride = bmpData.Stride; + data = new byte[stride * image.Height]; System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, data, 0, data.Length); - DefaultColor = Color.Silver; + + outData = copySourceToOutput ? (byte[])data.Clone() : new byte[stride * image.Height]; } - public Image(Image image):this(image.image){} /// /// Ширина изображения в пикселях @@ -90,11 +91,11 @@ public Image(Image image):this(image.image){} var i = GetIndex(x, y); if (i >= 0) { - data[i] = value.B; - data[i + 1] = value.G; - data[i + 2] = value.R; - data[i + 3] = value.A; - } + outData[i] = value.B; + outData[i + 1] = value.G; + outData[i + 2] = value.R; + outData[i + 3] = value.A; + }; } } /// @@ -130,7 +131,7 @@ int GetIndex(int x, int y) public void Dispose() { - System.Runtime.InteropServices.Marshal.Copy(data, 0, bmpData.Scan0, data.Length); + System.Runtime.InteropServices.Marshal.Copy(outData, 0, bmpData.Scan0, outData.Length); _image.UnlockBits(bmpData); } @@ -149,13 +150,22 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() return GetEnumerator(); } + /// + /// Меняет местами входной и выходной буферы + /// + public void SwapBuffers() + { + var temp = data; + data = outData; + outData = temp; + } + + /// /// Относительная высота на котором сделано изображение /// public double tall { get { return _tall; } } - public Bitmap image { get { - return _image; - } } + public Bitmap image { get { return _image; } } public Bitmap Convolution(double[,] matrix) { var w = matrix.GetLength(0); @@ -186,7 +196,7 @@ public double ConvolutionAtPoint(int x, int y, double[,] matrix) var w = matrix.GetLength(0); var h = matrix.GetLength(1); double r = 0d; - using (var wr = new Image((Bitmap)_image.Clone())) + using (var wr = new Image((Bitmap)_image.Clone()) { DefaultColor = Color.Silver }) { for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) @@ -195,33 +205,7 @@ public double ConvolutionAtPoint(int x, int y, double[,] matrix) r += matrix[j, i] * pixel.R; } } - return Math.Abs(r); - } - public static double[,] GaussianKernel(int lenght, double weight) - { - double[,] kernel = new double[lenght, lenght]; - double kernelSum = 0; - int foff = (lenght - 1) / 2; - double distance = 0; - double constant = 1d / (2 * Math.PI * weight * weight); - for (int y = -foff; y <= foff; y++) - { - for (int x = -foff; x <= foff; x++) - { - distance = ((y * y) + (x * x)) / (2 * weight * weight); - kernel[y + foff, x + foff] = constant * Math.Exp(-distance); - kernelSum += kernel[y + foff, x + foff]; - } - } - for (int y = 0; y < lenght; y++) - { - for (int x = 0; x < lenght; x++) - { - kernel[y, x] = kernel[y, x] * 1d / kernelSum; - } - } - return kernel; + return r; } } -} - +} \ No newline at end of file