private void GrayscaleRadioButton_Click(object sender, RoutedEventArgs e) { if (GrayscaleButtonClicked == false) { Paint.SetGrayscale(SourceImageColorArray); if (TryGenerate() == true) { GrayscaleButtonClicked = true; Paint.CopyToWriteableBitmap(SourceImageWB, SourceImageColorArray); } else { SourceImageColorArray = (SimpleColor[, ])OriginalSourceImageColorArray.Clone(); } } else { SourceImageColorArray = (SimpleColor[, ])OriginalSourceImageColorArray.Clone(); if (TryGenerate() == true) { GrayscaleButtonClicked = false; Paint.CopyToWriteableBitmap(SourceImageWB, SourceImageColorArray); } else { Paint.SetGrayscale(SourceImageColorArray); } } }
private void CreateImageRadioButton_Click(object sender, RoutedEventArgs e) { if (TryGenerate(false) == false) { return; } var bitmap = new WriteableBitmap(Globals.ImageWidth, Globals.ImageHeight, 96, 96, PixelFormats.Bgr24, null); SourceImageWB = new WriteableBitmap(bitmap); DestImageWB = new WriteableBitmap(bitmap); int width = SourceImageWB.PixelWidth; int height = SourceImageWB.PixelHeight; SourceImageControl.Source = SourceImageWB; SourceImageControl.Width = width; SourceImageControl.Height = height; DestImageControl.Source = DestImageWB; DestImageControl.Width = width; DestImageControl.Height = height; SourceImageColorArray = new SimpleColor[height, width]; DestImageColorArray = new SimpleColor[height, width]; Paint.CreateHSVBitmap(SourceImageColorArray); Paint.CopyToWriteableBitmap(SourceImageWB, SourceImageColorArray); OriginalSourceImageColorArray = (SimpleColor[, ])SourceImageColorArray.Clone(); TryGenerate(); }
private void SetTextureBitmap(WriteableBitmap textureBitmap = null) { if (timer.IsEnabled == true) { IsDefaultVectorL = true; } if (IsTextureFill == false) { //AnotherTextureFillRadioButtonClick = false; widthBitmap = Globals.DefaultBitmapWidth; heightBitmap = Globals.DefaultBitmapHeight; PictureWriteableBitmap = new WriteableBitmap(widthBitmap, heightBitmap, 96, 96, PixelFormats.Bgra32, null); var colInfo = FillColorComboBox.SelectedItem as ColorInfo; Paint.FillBitmap(PictureWriteableBitmap, colInfo.color); } else { //AnotherTextureFillRadioButtonClick = true; PictureWriteableBitmap = textureBitmap; widthBitmap = PictureWriteableBitmap.PixelWidth; heightBitmap = PictureWriteableBitmap.PixelHeight; } RemoveGrid(MainCanvas); writeableBitmap = new WriteableBitmap(PictureWriteableBitmap); writeableBitmapColor = new SimpleColor[heightBitmap, widthBitmap]; PictureBitmapColor = new SimpleColor[heightBitmap, widthBitmap]; Paint.ReadColorsFromBitmap(PictureWriteableBitmap, PictureBitmapColor); Paint.ReadColorsFromBitmap(writeableBitmap, writeableBitmapColor); triangleFillingMode.writeableBitmap = writeableBitmap; triangleFillingMode.PictureBitmapColor = PictureBitmapColor; triangleFillingMode.writeableBitmapColor = writeableBitmapColor; MainCanvas.Width = widthBitmap; MainCanvas.Height = heightBitmap; this.MaxHeight = heightBitmap + 4 * Globals.GridMargin; image = new Image { Source = writeableBitmap, IsHitTestVisible = false, Width = widthBitmap, Height = heightBitmap }; Panel.SetZIndex(image, Globals.BitmapZIndex); MainCanvas.Children.Add(image); MainCanvas.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); MainCanvas.Arrange(new Rect(0, 0, MainCanvas.DesiredSize.Width, MainCanvas.DesiredSize.Height)); SetNormalVectorBitmap(NormalWriteableBitmap); GenerateGrid(MainCanvas); GenerateTimerPathList(); }
private void LoadWritableBitamp(BitmapSource bitmap, bool IsWindowInit) { SourceImageWB = new WriteableBitmap(bitmap); DestImageWB = new WriteableBitmap(bitmap); int width = SourceImageWB.PixelWidth; int height = SourceImageWB.PixelHeight; SourceImageControl.Source = SourceImageWB; SourceImageControl.Width = width; SourceImageControl.Height = height; DestImageControl.Source = DestImageWB; DestImageControl.Width = width; DestImageControl.Height = height; SourceImageColorArray = new SimpleColor[height, width]; DestImageColorArray = new SimpleColor[height, width]; Paint.ReadColorsFromBitmap(SourceImageWB, SourceImageColorArray); OriginalSourceImageColorArray = (SimpleColor[, ])SourceImageColorArray.Clone(); if (IsWindowInit == false) { ColorProfileConverter.ConvertImage(SourceImageColorArray, DestImageColorArray, (ColorProfileEnum)SourceColorSpaceComboBox.SelectedIndex, (ColorProfileEnum)DestColorSpaceComboBox.SelectedIndex); Paint.CopyToWriteableBitmap(DestImageWB, DestImageColorArray); } else { TryGenerate(); } }
public static void ConvertImageToDoubleColor(SimpleColor[,] imageSource, DoubleColor[,] imageDest) { Parallel.For(0, imageSource.GetLength(0), i => { for (int j = 0; j < imageSource.GetLength(1); j++) { imageDest[i, j] = new DoubleColor(imageSource[i, j]); } }); }
public static void ConvertImageFromDoubleColor(DoubleColor[,] imageSource, SimpleColor[,] imageDest) { Parallel.For(0, imageSource.GetLength(0), i => { for (int j = 0; j < imageSource.GetLength(1); j++) { imageDest[i, j] = imageSource[i, j].ToSimpleColor(); } }); }
public static void SetGrayscale(SimpleColor[,] pixels) { Parallel.For(0, pixels.GetLength(0), y => { for (int j = 0; j < pixels.GetLength(1); j++) { pixels[y, j].ToGrayscale(); } }); }
public static void ConvertImageToLAB(SimpleColor[,] imageSource, DoubleColor[,] imageDest, ColorProfileEnum from) { Parallel.For(0, imageSource.GetLength(0), i => { for (int j = 0; j < imageSource.GetLength(1); j++) { DoubleColor color = new DoubleColor(imageSource[i, j]); var c1 = ConvertColorToXYZ(color, from); var c2 = ConvertColorFromXYZToLAB(c1, from); imageDest[i, j] = c2; } }); }
public static void ConvertImageFromLAB(DoubleColor[,] imageSource, SimpleColor[,] imageDest, ColorProfile from) { Parallel.For(0, imageSource.GetLength(0), i => { for (int j = 0; j < imageSource.GetLength(1); j++) { //DoubleColor color = new DoubleColor(imageSource[i, j]); var c1 = ConvertColorFromLABToXYZ(imageSource[i, j], from); var c2 = ConvertColorFromXYZ(c1, from); imageDest[i, j] = c2.ToSimpleColor(); } }); }
public FastImage(int w, int h) { pixels = new SimpleColor[w, h]; width = w; height = h; Parallel.For(0, w, x => { Parallel.For(0, h, y => { pixels[x, y] = new SimpleColor(0, 0, 0, 0); }); }); }
public static void ConvertImage(SimpleColor[,] imageSource, SimpleColor[,] imageDest, ColorProfile from, ColorProfile to) { var bradfordMatrix = MyMatrix.GenerateBradfordMatrix(from, to); Parallel.For(0, imageSource.GetLength(0), i => { for (int j = 0; j < imageSource.GetLength(1); j++) { DoubleColor color = new DoubleColor(imageSource[i, j]); var c1 = ConvertColorToXYZ(color, from); var c2 = ConvertColorFromXYZ(c1, to, bradfordMatrix); imageDest[i, j] = c2.ToSimpleColor(); } }); }
// -------------------------------------------------------------------------- // Section 14.2.5 Parallelizing the application // Listing 14.17 Applying color filter in parallel public static SimpleColor[,] RunFilterParallel (this SimpleColor[,] arr, Func <SimpleColor, SimpleColor> f) { int hgt = arr.GetLength(0), wid = arr.GetLength(1); var res = new SimpleColor[hgt, wid]; // Parallelize the outer loop Parallel.For(0, hgt, y => { // Leave inner loop sequential for (int x = 0; x < wid; x++) { res[y, x] = f(arr[y, x]); } }); return(res); }
public FastImage(Image a) { pixels = new SimpleColor[a.Width, a.Height]; Bitmap b = (Bitmap)a; width = b.Width; height = b.Height; for (int x = 0; x < b.Width; x++) { for (int y = 0; y < b.Height; y++) { Color c = b.GetPixel(x, y); pixels[x, y] = new SimpleColor(c.R, c.G, c.B, ImageGenerationAlgo.Utils.Map(c.A, 0, 255, 0, 1)); } } }
// Listing 14.11 Sequential method for applying filters (C#) public static SimpleColor[,] RunFilter (this SimpleColor[,] arr, Func <SimpleColor, SimpleColor> f) { int hgt = arr.GetLength(0), wid = arr.GetLength(1); // Create a new array as the result var res = new SimpleColor[hgt, wid]; // Calculate new color for every pixel for (int y = 0; y < hgt; y++) { for (int x = 0; x < wid; x++) { res[y, x] = f(arr[y, x]); } } return(res); }
public FastImage(FastImage c) { pixels = new SimpleColor[c.width, c.height]; width = c.width; height = c.height; Parallel.For(0, width, x => { Parallel.For(0, height, y => { SimpleColor color = c.GetPixel(x, y); pixels[x, y] = new SimpleColor(color.GetR(), color.GetG(), color.GetB(), color.GetA()); }); }); }
public static void CopyToWriteableBitmap(WriteableBitmap writeableBitmap, SimpleColor[,] pixels) { writeableBitmap.Lock(); unsafe { Int64 writeablebpp = writeableBitmap.Format.BitsPerPixel / 8; Int64 writeableBuffer = (Int64)writeableBitmap.BackBuffer; Int64 bufferstride = writeableBitmap.BackBufferStride; Parallel.For(0, pixels.GetLength(0), y => { Int64 place = writeableBuffer + y * bufferstride; for (int x = 0; x < pixels.GetLength(1); x++) { *((int *)place) = pixels[y, x].ToInt(); place += writeablebpp; } }); } writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight)); writeableBitmap.Unlock(); }
public static void ReadColorsFromBitmap(WriteableBitmap writeableBitmap, SimpleColor[,] pixels) { writeableBitmap.Lock(); unsafe { Int64 writeablebpp = writeableBitmap.Format.BitsPerPixel / 8; Int64 writeableBuffer = (Int64)writeableBitmap.BackBuffer; Int64 bufferstride = writeableBitmap.BackBufferStride; for (Int64 y = 0; y < pixels.GetLength(0); y++) { for (Int64 x = 0; x < pixels.GetLength(1); x++) { int col = *((int *)writeableBuffer); //SimpleColor sc = new SimpleColor(col); pixels[y, x].ConvertFromInt(col); writeableBuffer += writeablebpp; } } } writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight)); writeableBitmap.Unlock(); }
// Convert array to .NET Bitmap type public static Bitmap ToBitmap(this SimpleColor[,] arr) { var bmp = new Bitmap(arr.GetLength(0), arr.GetLength(1)); var rect = new Rectangle(0, 0, bmp.Width, bmp.Height); var bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); var ptr0 = bmpData.Scan0; var stride = bmpData.Stride; int width = bmp.Width; int height = bmp.Height; for (int y = 0; y < width; y++) { for (int x = 0; x < height; x++) { var offset = y * 4 + stride * x; var clr = arr[y, x]; var clr2 = Color.FromArgb(clr.R, clr.G, clr.B).ToArgb(); System.Runtime.InteropServices.Marshal.WriteInt32(ptr0, offset, clr2); } } bmp.UnlockBits(bmpData); return(bmp); }
// Get pixel color after applying effect to the specified input public static SimpleColor[,] Blur(SimpleColor[,] arr, bool parallel) { int hgt = arr.GetLength(0), wid = arr.GetLength(1); var res = new SimpleColor[hgt, wid]; // The outer for loop can be parallelized, depending on the argument RunForLoop(parallel, 0, hgt, y => { // Leave inner loop sequential for (int x = 0; x < wid; x++) { // Sum colors close to the specified location var sum = SimpleColor.Zero; for (int dy = -2; dy <= 2; dy++) { for (int dx = -2; dx <= 2; dx++) { sum += CheckedRead(arr, y + dy, x + dx, hgt, wid); } } res[y, x] = sum / 25; } }); return(res); }
// Parallelized version of the blur effect public static SimpleColor[,] ParallelBlur(SimpleColor[,] arr) { return(Blur(arr, true)); }
// Sequential version of the blur effect public static SimpleColor[,] SequentialBlur(SimpleColor[,] arr) { return(Blur(arr, false)); }
// -------------------------------------------------------------------------- // BONUS: Sample graphical effect // Implements 'Blur' effect, which cannot be created using color filters // Read color at the specified location, but check bounds of the bitmap private static SimpleColor CheckedRead(SimpleColor[,] arr, int y, int x, int hgt, int wid) { return(arr[Math.Max(0, Math.Min(hgt - 1, y)), Math.Max(0, Math.Min(wid - 1, x))]); }
public static void CreateHSVBitmap(SimpleColor[,] pixels) { int middle_x = pixels.GetLength(1) / 2; int middle_y = pixels.GetLength(0) / 2; for (int y = 0; y < pixels.GetLength(0); y++) { for (int x = 0; x < pixels.GetLength(1); x++) { if (y < Globals.BorderWidth || y > pixels.GetLength(0) - Globals.BorderWidth || x < Globals.BorderWidth || x > pixels.GetLength(1) - Globals.BorderWidth) { pixels[y, x] = new SimpleColor(0, 0, 0); } else { pixels[y, x] = new SimpleColor(255, 255, 255); double dist = Distance(middle_x, middle_y, x, y); if (dist <= Globals.Radius) { //calculate HSV //double V = 1; double C = dist / (double)Globals.Radius; double m = 1 - C; double cos = Cosinus(middle_x, middle_y, x, y, dist); if (y <= middle_y)// angle between 0 and pi { double H = Math.Acos(cos); double angleH = H * 180d / Math.PI; double int_H = (angleH / 60d); //C to dist double X = C * (1 - Math.Abs((int_H % 2) - 1)); if (int_H <= 1) { var color = new DoubleColor(C + m, X + m, m); var simpleColor = color.ToSimpleColor(); //double R = 0.5 + C * Math.Cos(angleH) / Math.Cos(60 - H); //double B = 0.5 - C; //double G = 1.5 - B - R; //color = new DoubleColor(R, G, B); //simpleColor = color.ToSimpleColor(); pixels[y, x] = simpleColor; } else if (int_H <= 2) { var color = new DoubleColor(X + m, C + m, m); pixels[y, x] = color.ToSimpleColor(); } else { var color = new DoubleColor(m, C + m, X + m); pixels[y, x] = color.ToSimpleColor(); } } else // angle between pi and 2 pi { double H = Math.Acos(cos); double angleH = 360d - H * 180 / Math.PI; double int_H = (angleH / 60); //C to dist double X = C * (1 - Math.Abs((int_H % 2) - 1)); if (int_H <= 4) { var color = new DoubleColor(m, X + m, C + m); pixels[y, x] = color.ToSimpleColor(); } else if (int_H <= 5) { var color = new DoubleColor(X + m, m, C + m); pixels[y, x] = color.ToSimpleColor(); } else { var color = new DoubleColor(C + m, m, X + m); pixels[y, x] = color.ToSimpleColor(); } } } } } } }