internal void Mutate(DnaDrawing drawing, JobInfo info) { if (info.WillMutate(info.Settings.ColorMutationRate)) { Red = info.GetRandomNumber(0, 255); drawing.SetDirty(); } if (info.WillMutate(info.Settings.ColorMutationRate)) { Green = info.GetRandomNumber(0, 255); drawing.SetDirty(); } if (info.WillMutate(info.Settings.ColorMutationRate)) { Blue = info.GetRandomNumber(0, 255); drawing.SetDirty(); } if (info.WillMutate(info.Settings.ColorMutationRate)) { Alpha = info.GetRandomNumber(info.Settings.AlphaRangeMin, info.Settings.AlphaRangeMax); drawing.SetDirty(); } }
public void Mutate(DnaDrawing drawing) { if (Tools.WillMutate(Settings.ActiveRedMutationRate)) { Red = Tools.GetRandomNumber(Settings.ActiveRedRangeMin, Settings.ActiveRedRangeMax); drawing.SetDirty(); } if (Tools.WillMutate(Settings.ActiveGreenMutationRate)) { Green = Tools.GetRandomNumber(Settings.ActiveGreenRangeMin, Settings.ActiveGreenRangeMax); drawing.SetDirty(); } if (Tools.WillMutate(Settings.ActiveBlueMutationRate)) { Blue = Tools.GetRandomNumber(Settings.ActiveBlueRangeMin, Settings.ActiveBlueRangeMax); drawing.SetDirty(); } if (Tools.WillMutate(Settings.ActiveAlphaMutationRate)) { Alpha = Tools.GetRandomNumber(Settings.ActiveAlphaRangeMin, Settings.ActiveAlphaRangeMax); drawing.SetDirty(); } }
internal void Init(DnaDrawing drawing, JobInfo info) { Points = new List <DnaPoint>(); //int count = info.GetRandomNumber(3, 3); var origin = new DnaPoint(); origin.Init(drawing, info); if (drawing.Polygons.Count < 1) { origin.X = info.SourceImage.Width / 2; origin.Y = info.SourceImage.Height / 2; } for (int i = 0; i < info.Settings.PointsPerPolygonMin; i++) { var point = new DnaPoint { X = Math.Min(Math.Max(0, origin.X + info.GetRandomNumber(-3, 3)), info.SourceImage.Width), Y = Math.Min(Math.Max(0, origin.Y + info.GetRandomNumber(-3, 3)), info.SourceImage.Height) }; Points.Add(point); } Brush = new DnaBrush(); Brush.Init(info); }
//Render a Drawing public static void Render(DnaDrawing drawing,Graphics g,int scale) { g.Clear(Tools.avgColour); foreach (DnaPolygon polygon in drawing.Polygons) Render(polygon, g, scale); }
//Render a Drawing public static void Render(DnaDrawing drawing,Graphics g,int scale) { g.Clear(Color.Black); foreach (DnaPolygon polygon in drawing.Polygons) Render(polygon, g, scale); }
/// <summary> /// Render a Drawing /// </summary> public static void Render(DnaDrawing drawing, Canvas c, int scale) { c.Children.Clear(); foreach (DnaPolygon polygon in drawing.Polygons) Render(polygon, c, scale); }
public static double GetDrawingFitness(DnaDrawing newDrawing, Color[,] sourceColors) { double error = 0; using (var b = new Bitmap(Tools.MaxWidth, Tools.MaxHeight, PixelFormat.Format24bppRgb)) using (Graphics g = Graphics.FromImage(b)) { Renderer.Render(newDrawing, g, 1); BitmapData bmd1 = b.LockBits(new Rectangle(0, 0, Tools.MaxWidth, Tools.MaxHeight), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); for (int y = 0; y < Tools.MaxHeight; y++) { for (int x = 0; x < Tools.MaxWidth; x++) { Color c1 = GetPixel(bmd1, x, y); Color c2 = sourceColors[x, y]; double pixelError = GetColorFitness(c1, c2); error += pixelError; } } b.UnlockBits(bmd1); } return error; }
internal DnaDrawing GetMutatedChild(JobInfo info) { DnaDrawing child = Clone(); child.Mutate(info); return(child); }
public void Open() { currentDrawing = Serializer.DeserializeDnaDrawing(FileUtil.GetOpenFileName(FileUtil.DnaExtension)); if (currentDrawing != null) { // find MaxWidth and MaxHeight - extent of the canvas to draw foreach (var poly in currentDrawing.Polygons) { foreach (var pnt in poly.Points) { if (pnt.X > currentDrawingBottomRight.X) currentDrawingBottomRight.X = pnt.X; if (pnt.Y > currentDrawingBottomRight.Y) currentDrawingBottomRight.Y = pnt.Y; } } bounds = new System.Drawing.Rectangle(0, 0, Convert.ToInt32(currentDrawingBottomRight.X), Convert.ToInt32(currentDrawingBottomRight.Y)); using ( var backBuffer = new System.Drawing.Bitmap( Convert.ToInt32(scale * bounds.Width), Convert.ToInt32(scale * bounds.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb)) using (Graphics backGraphics = Graphics.FromImage(backBuffer)) { backGraphics.SmoothingMode = SmoothingMode.HighQuality; Renderer.Render(currentDrawing, backGraphics, scale); // Image EvoImage.Source = loadBitmap(backBuffer); // Image WpfRenderer.Render(currentDrawing, EvoCanvas, scale); // Canvas } } }
internal void Init(DnaDrawing drawing, JobInfo info) { Points = new List<DnaPoint>(); //int count = info.GetRandomNumber(3, 3); var origin = new DnaPoint(); origin.Init(drawing, info); if (drawing.Polygons.Count < 1) { origin.X = info.SourceImage.Width / 2; origin.Y = info.SourceImage.Height / 2; } for (int i = 0; i < info.Settings.PointsPerPolygonMin; i++) { var point = new DnaPoint { X = Math.Min(Math.Max(0, origin.X + info.GetRandomNumber(-3, 3)), info.SourceImage.Width), Y = Math.Min(Math.Max(0, origin.Y + info.GetRandomNumber(-3, 3)), info.SourceImage.Height) }; Points.Add(point); } Brush = new DnaBrush(); Brush.Init(info); }
public double GetDrawingFitness(DnaDrawing newDrawing, Pixel[] sourcePixels) { double error = 0; Renderer.Render(newDrawing, _g, 1); BitmapData bd = _bmp.LockBits( new Rectangle(0, 0, Tools.MaxWidth, Tools.MaxHeight), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); unchecked { unsafe { fixed (Pixel* psourcePixels = sourcePixels) { Pixel* p1 = (Pixel*)bd.Scan0.ToPointer(); Pixel* p2 = psourcePixels; for (int i = sourcePixels.Length; i > 0; i--, p1++, p2++) { int a = p1->A - p2->A; //delete int r = p1->R - p2->R; int g = p1->G - p2->G; int b = p1->B - p2->B; error += r * r + g * g + b * b; } } } } _bmp.UnlockBits(bd); return error; }
public void Mutate(DnaDrawing drawing) { if (tool.WillMutate(Settings.ActiveRedMutationRate)) { Red = tool.GetRandomNumber(Settings.ActiveRedRangeMin, Settings.ActiveRedRangeMax); drawing.SetDirty(); } if (tool.WillMutate(Settings.ActiveGreenMutationRate)) { Green = tool.GetRandomNumber(Settings.ActiveGreenRangeMin, Settings.ActiveGreenRangeMax); drawing.SetDirty(); } if (tool.WillMutate(Settings.ActiveBlueMutationRate)) { Blue = tool.GetRandomNumber(Settings.ActiveBlueRangeMin, Settings.ActiveBlueRangeMax); drawing.SetDirty(); } if (tool.WillMutate(Settings.ActiveAlphaMutationRate)) { Alpha = tool.GetRandomNumber(Settings.ActiveAlphaRangeMin, Settings.ActiveAlphaRangeMax); drawing.SetDirty(); } }
public void Mutate(DnaDrawing drawing) { if (Tools.WillMutate(Settings.ActiveRedMutationRate)) //chance that the red will mutate { Red = Tools.GetRandomNumber(Settings.ActiveRedRangeMin, Settings.ActiveRedRangeMax); //set the red to a random number between some values drawing.SetDirty(); //make it so that we can update the drawing } if (Tools.WillMutate(Settings.ActiveGreenMutationRate)) { Green = Tools.GetRandomNumber(Settings.ActiveGreenRangeMin, Settings.ActiveGreenRangeMax); drawing.SetDirty(); } if (Tools.WillMutate(Settings.ActiveBlueMutationRate)) { Blue = Tools.GetRandomNumber(Settings.ActiveBlueRangeMin, Settings.ActiveBlueRangeMax); drawing.SetDirty(); } if (Tools.WillMutate(Settings.ActiveAlphaMutationRate)) { Alpha = Tools.GetRandomNumber(Settings.ActiveAlphaRangeMin, Settings.ActiveAlphaRangeMax); drawing.SetDirty(); } }
public void Mutate(DnaDrawing drawing, JobInfo info) { if (info.WillMutate(info.Settings.AddPointMutationRate)) { AddPoint(drawing, info); } if (info.WillMutate(info.Settings.RemovePointMutationRate)) { RemovePoint(drawing, info); } if (info.WillMutate(info.Settings.FlipSplinesMutationRate)) { FlipSplines(drawing, info); } if (info.WillMutate(info.Settings.FlipFilledMutationRate)) { FlipFilled(drawing, info); } if (info.WillMutate(info.Settings.FlipFilledMutationRate)) { Width = info.GetRandomNumber(1, 8); } Brush.Mutate(drawing, info); foreach (var point in Points) { point.Mutate(drawing, info); } //IsComplex = false;// checkComplex(); }
//Render a Drawing internal static void Render(DnaDrawing drawing, Graphics g, int scale) { for (int i = 0; i < drawing.Polygons.Count;i++ ) { DnaPolygon polygon = drawing.Polygons[i]; Render(polygon, g, scale); } }
public void Init() { Settings = new Settings(); Drawing = new DnaDrawing(); // Drawing.Init(Settings); ErrorLevel = double.MaxValue; LastSavedFitness = double.MaxValue; }
public DnaDrawing Clone() { var drawing = new DnaDrawing(); drawing.Polygons = new List<DnaPolygon>(); foreach (DnaPolygon polygon in Polygons) drawing.Polygons.Add(polygon.Clone()); return drawing; }
internal WorkerInstance(int randomSeed, JobInfo info) { this.randomSeed = randomSeed; this.info = info; info.InitRandom(randomSeed); parentDrawing = new DnaDrawing { Polygons = new List<DnaPolygon>(), }; }
private void RemovePoint(DnaDrawing drawing) { if (Points.Count > Settings.ActivePointsPerPolygonMin) { int index = tool.GetRandomNumber(0, Points.Count); Points.RemoveAt(index); drawing.SetDirty(); } }
private void RemovePoint(DnaDrawing drawing, JobInfo info) { if (Points.Count > info.Settings.PointsPerPolygonMin) { int index = info.GetRandomNumber(0, Points.Count); Points.RemoveAt(index); drawing.SetDirty(); } }
internal static long GetDrawingFitness(DnaDrawing newDrawing, SourceImage sourceImage, Bitmap bgImage, Bitmap fgImage) { long error = 0; if (bmp == null) { bmp = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb); g = Graphics.FromImage(bmp); } if (bgImage != null) g.DrawImage(bgImage,0,0); else g.Clear(Color.Black); Renderer.Render(newDrawing, g, 1); if (fgImage != null) g.DrawImage(fgImage,0,0); BitmapData bd = bmp.LockBits( new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); unchecked { unsafe { fixed (Pixel* psourcePixels = sourceImage.Pixels) { int partitionEnd = sourceImage.Height*sourceImage.Width; var p1 = (Pixel*) bd.Scan0.ToPointer(); Pixel* p2 = psourcePixels; for (int i = 0; i < partitionEnd; i++, p1++, p2++) { int R = p1->R - p2->R; int G = p1->G - p2->G; int B = p1->B - p2->B; error += R * R + G * G + B * B; } } } } bmp.UnlockBits(bd); // error += newDrawing.Polygons.Count * 3 ; return error; }
public void Mutate(DnaDrawing drawing) { if (Tools.WillMutate(Settings.ActiveAddPointMutationRate)) AddPoint(drawing); if (Tools.WillMutate(Settings.ActiveRemovePointMutationRate)) RemovePoint(drawing); Brush.Mutate(drawing); Points.ForEach(p => p.Mutate(drawing)); }
public DnaDrawing Clone() { var drawing = new DnaDrawing { Polygons = new List<DnaPolygon>(), //SourceImage = SourceImage, }; foreach (DnaPolygon polygon in Polygons) drawing.Polygons.Add(polygon.Clone()); return drawing; }
public DnaDrawing Clone() { var drawing = new DnaDrawing(); drawing.Polygons = new List <DnaPolygon>(); foreach (DnaPolygon polygon in Polygons) { drawing.Polygons.Add(polygon.Clone()); } return(drawing); }
public void Init(DnaDrawing drawing, JobInfo info) { Points = new List <DnaPoint>(); //int count = info.GetRandomNumber(3, 3); var origin = new DnaPoint(); origin.Init(drawing, info); if (drawing.Polygons.Count < 1) { origin.X = info.SourceImage.Width / 2; origin.Y = info.SourceImage.Height / 2; } for (int i = 0; i < info.Settings.PointsPerPolygonMin; i++) { var point = new DnaPoint { X = Math.Min(Math.Max(0, origin.X + info.GetRandomNumber(-3, 3)), info.SourceImage.Width), Y = Math.Min(Math.Max(0, origin.Y + info.GetRandomNumber(-3, 3)), info.SourceImage.Height) }; Points.Add(point); } bool findNew = true; if (info.Settings.MuteCurvePolygon && info.Settings.MuteLinePolygon && info.Settings.MuteCurveFillPolygon && info.Settings.MuteLineFillPolygon) { findNew = false; } Width = info.GetRandomNumber(1, 8); while (findNew) { bool splines = (info.GetRandomNumber(0, 2) == 1) ? true : false; bool filled = (info.GetRandomNumber(0, 2) == 1) ? true : false; findNew = !SetSplinesAndFilled(info.Settings, splines, filled); } Brush = new DnaBrush(); Brush.Init(info); }
public DefaultEvolutionJob(DnaDrawing drawing, JobInfo info) { this.info = info; if (drawing == null) drawing = GetNewInitializedDrawing(info); lock (drawing) { currentDrawing = drawing.Clone(); } currentErrorLevel = FitnessCalculator.GetDrawingFitness(currentDrawing, info.SourceImage); }
public DnaDrawing Clone() { var drawing = new DnaDrawing { Polygons = new List <DnaPolygon>(), //SourceImage = SourceImage, }; foreach (DnaPolygon polygon in Polygons) { drawing.Polygons.Add(polygon.Clone()); } return(drawing); }
public void Init(DnaDrawing drawing, JobInfo info) { Points = new List<DnaPoint>(); //int count = info.GetRandomNumber(3, 3); var origin = new DnaPoint(); origin.Init(drawing, info); if (drawing.Polygons.Count < 1) { origin.X = info.SourceImage.Width / 2; origin.Y = info.SourceImage.Height / 2; } for (int i = 0; i < info.Settings.PointsPerPolygonMin; i++) { var point = new DnaPoint { X = Math.Min(Math.Max(0, origin.X + info.GetRandomNumber(-3, 3)), info.SourceImage.Width), Y = Math.Min(Math.Max(0, origin.Y + info.GetRandomNumber(-3, 3)), info.SourceImage.Height) }; Points.Add(point); } bool findNew = true; if (info.Settings.MuteCurvePolygon && info.Settings.MuteLinePolygon && info.Settings.MuteCurveFillPolygon && info.Settings.MuteLineFillPolygon) findNew = false; Width = info.GetRandomNumber(1, 8); while (findNew) { bool splines = (info.GetRandomNumber(0, 2) == 1) ? true : false; bool filled = (info.GetRandomNumber(0, 2) == 1) ? true : false; findNew = !SetSplinesAndFilled(info.Settings, splines, filled); } Brush = new DnaBrush(); Brush.Init(info); }
internal void Mutate(DnaDrawing drawing, JobInfo info) { if (!info.Settings.MuteMovePointMax) { if (info.WillMutate(info.Settings.MovePointMaxMutationRate)) { X = info.GetRandomNumber(0, info.SourceImage.Width); Y = info.GetRandomNumber(0, info.SourceImage.Height); drawing.SetDirty(); } } if (!info.Settings.MuteMovePointMid) { if (info.WillMutate(info.Settings.MovePointMidMutationRate)) { X = X .Randomize(info ,- info.Settings.MovePointRangeMid, info.Settings.MovePointRangeMid) .Max(0) .Min(info.SourceImage.Width); Y = Y .Randomize(info, -info.Settings.MovePointRangeMid, info.Settings.MovePointRangeMid) .Max(0) .Min(info.SourceImage.Height); drawing.SetDirty(); } } if (!info.Settings.MuteMovePointMin) { if (info.WillMutate(info.Settings.MovePointMinMutationRate)) { X = X .Randomize(info, -info.Settings.MovePointRangeMin, info.Settings.MovePointRangeMin) .Max(0) .Min(info.SourceImage.Width); Y = Y .Randomize(info, -info.Settings.MovePointRangeMin, info.Settings.MovePointRangeMin) .Max(0) .Min(info.SourceImage.Height); drawing.SetDirty(); } } }
public DnaDrawing Clone() { lock (this) { var drawing = new DnaDrawing(seed); drawing.tool = tool; drawing.Shapes = new List <DnaShape>(); foreach (DnaShape shape in Shapes) { drawing.Shapes.Add(shape.Clone()); } return(drawing); } }
public void Mutate(DnaDrawing drawing) { if (Tools.WillMutate(Settings.ActiveAddPointMutationRate)) { AddPoint(drawing); } if (Tools.WillMutate(Settings.ActiveRemovePointMutationRate)) { RemovePoint(drawing); } Brush.Mutate(drawing); Points.ForEach(p => p.Mutate(drawing)); }
internal void Mutate(DnaDrawing drawing, JobInfo info) { if (!info.Settings.MuteMovePointMax) { if (info.WillMutate(info.Settings.MovePointMaxMutationRate)) { X = info.GetRandomNumber(0, info.SourceImage.Width); Y = info.GetRandomNumber(0, info.SourceImage.Height); drawing.SetDirty(); } } if (!info.Settings.MuteMovePointMid) { if (info.WillMutate(info.Settings.MovePointMidMutationRate)) { X = X .Randomize(info, -info.Settings.MovePointRangeMid, info.Settings.MovePointRangeMid) .Max(0) .Min(info.SourceImage.Width); Y = Y .Randomize(info, -info.Settings.MovePointRangeMid, info.Settings.MovePointRangeMid) .Max(0) .Min(info.SourceImage.Height); drawing.SetDirty(); } } if (!info.Settings.MuteMovePointMin) { if (info.WillMutate(info.Settings.MovePointMinMutationRate)) { X = X .Randomize(info, -info.Settings.MovePointRangeMin, info.Settings.MovePointRangeMin) .Max(0) .Min(info.SourceImage.Width); Y = Y .Randomize(info, -info.Settings.MovePointRangeMin, info.Settings.MovePointRangeMin) .Max(0) .Min(info.SourceImage.Height); drawing.SetDirty(); } } }
public static void SerializeBinary(DnaDrawing drawing, string fileName) { if (fileName == null) return; try { var formatter = new BinaryFormatter(); using (var writer = new FileStream(fileName, FileMode.Create)) { formatter.Serialize(writer, drawing); } } catch (Exception) { } }
internal DnaDrawing Clone() { var drawing = new DnaDrawing { Polygons = new List<DnaPolygon>(), //SourceImage = SourceImage, }; unchecked { for (int i = 0; i < Polygons.Count; i++) { DnaPolygon polygon = Polygons[i]; drawing.Polygons.Add(polygon.Clone()); } } return drawing; }
internal DnaDrawing Clone() { var drawing = new DnaDrawing { Polygons = new List <DnaPolygon>(), //SourceImage = SourceImage, }; unchecked { for (int i = 0; i < Polygons.Count; i++) { DnaPolygon polygon = Polygons[i]; drawing.Polygons.Add(polygon.Clone()); } } return(drawing); }
internal void Mutate(DnaDrawing drawing, JobInfo info) { if (info.WillMutate(info.Settings.AddPointMutationRate)) AddPoint(drawing, info); if (info.WillMutate(info.Settings.RemovePointMutationRate)) RemovePoint(drawing, info); Brush.Mutate(drawing, info); unchecked { for (int i = 0; i < Points.Count;i++) { var point = Points[i]; point.Mutate(drawing, info); } } }
public void Mutate(DnaDrawing drawing) { if (Tools.WillMutate(Settings.ActiveMovePointMaxMutationRate)) { X = Tools.GetRandomNumber(0, Tools.MaxWidth); Y = Tools.GetRandomNumber(0, Tools.MaxHeight); drawing.SetDirty(); } if (Tools.WillMutate(Settings.ActiveMovePointMidMutationRate)) { X = Math.Min( Math.Max(0, X + Tools.GetRandomNumber(-Settings.ActiveMovePointRangeMid, Settings.ActiveMovePointRangeMid)), Tools.MaxWidth); Y = Math.Min( Math.Max(0, Y + Tools.GetRandomNumber(-Settings.ActiveMovePointRangeMid, Settings.ActiveMovePointRangeMid)), Tools.MaxHeight); drawing.SetDirty(); } if (Tools.WillMutate(Settings.ActiveMovePointMinMutationRate)) { X = Math.Min( Math.Max(0, X + Tools.GetRandomNumber(-Settings.ActiveMovePointRangeMin, Settings.ActiveMovePointRangeMin)), Tools.MaxWidth); Y = Math.Min( Math.Max(0, Y + Tools.GetRandomNumber(-Settings.ActiveMovePointRangeMin, Settings.ActiveMovePointRangeMin)), Tools.MaxHeight); drawing.SetDirty(); } }
//produces a mutated/changed copy - basically, samll chance to mutate a lot, larger chance to mutate a little public void Mutate(DnaDrawing drawing) { if (Tools.WillMutate(Settings.ActiveMovePointMaxMutationRate)) //find out if it should mutate based on max rate { X = Tools.GetRandomNumber(0, Tools.MaxWidth); //set x and y to another random point Y = Tools.GetRandomNumber(0, Tools.MaxHeight); drawing.SetDirty(); //???? makes it dirty - this means that the drawing can be updates } if (Tools.WillMutate(Settings.ActiveMovePointMidMutationRate)) { X = Math.Min( Math.Max(0, X + Tools.GetRandomNumber(-Settings.ActiveMovePointRangeMid, Settings.ActiveMovePointRangeMid)), Tools.MaxWidth); Y = Math.Min( Math.Max(0, Y + Tools.GetRandomNumber(-Settings.ActiveMovePointRangeMid, Settings.ActiveMovePointRangeMid)), Tools.MaxHeight); drawing.SetDirty(); } if (Tools.WillMutate(Settings.ActiveMovePointMinMutationRate)) { X = Math.Min( Math.Max(0, X + Tools.GetRandomNumber(-Settings.ActiveMovePointRangeMin, Settings.ActiveMovePointRangeMin)), Tools.MaxWidth); Y = Math.Min( Math.Max(0, Y + Tools.GetRandomNumber(-Settings.ActiveMovePointRangeMin, Settings.ActiveMovePointRangeMin)), Tools.MaxHeight); drawing.SetDirty(); } }
public void breed(DnaDrawing other) { lock (this) { lock (other) { shuffleShapes(); other.shuffleShapes(); List <DnaShape> otherFirst = other.firstHalf; List <DnaShape> otherSnd = other.secondHalf; List <DnaShape> thisFirst = firstHalf; List <DnaShape> thisSnd = secondHalf; otherFirst.AddRange(thisSnd); thisFirst.AddRange(otherSnd); Shapes = thisFirst; other.Shapes = otherFirst; } } }
public static double GetDrawingFitness(DnaDrawing newDrawing, SourceImage sourceImage,int partitionY,int partitionHeight) { double error = 0; using (var bmp = new Bitmap(sourceImage.Width, sourceImage.Height)) using (Graphics g = Graphics.FromImage(bmp)) { g.SetClip(new Rectangle(0,partitionY,sourceImage.Width,partitionHeight)); Renderer.Render(newDrawing, g, 1); g.ResetClip(); BitmapData bd = bmp.LockBits( new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); unchecked { unsafe { fixed (Pixel* psourcePixels = sourceImage.Pixels) { int partitionOffset = partitionY * sourceImage.Width; int partitionEnd = partitionOffset + partitionHeight * sourceImage.Width; var p1 = (Pixel*)bd.Scan0.ToPointer() + partitionOffset; Pixel* p2 = psourcePixels + partitionOffset; for (int i = partitionOffset; i < partitionEnd; i++, p1++, p2++) { int R = p1->R - p2->R; int G = p1->G - p2->G; int B = p1->B - p2->B; error += R * R + G * G + B * B; } } } } bmp.UnlockBits(bd); } // error += newDrawing.Polygons.Count * 3 ; return error; }
/// <summary> /// Render a Drawing /// </summary> /// <remarks> /// Sorry about the hardcoded Width/Height and ScaleTransform... feel free to fix it :) /// </remarks> /// <param name="bounds">previously calculated extent of image, so we can draw a black background behind everything</param> public static void Render(DnaDrawing drawing, ref StringBuilder sb, int scale, System.Drawing.Rectangle bounds) { sb = new StringBuilder(); sb.Append(@"<?xml version=""1.0"" standalone=""no""?> <!DOCTYPE svg PUBLIC ""-//W3C//DTD SVG 1.1//EN"" ""http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd""> <svg width=""100%"" height=""100%"" version=""1.1"" xmlns=""http://www.w3.org/2000/svg"">"); sb.Append("\r\n"); sb.Append( String.Format(@"<polygon points=""0,0 {0},0 {1},{2} 0,{3}"" style=""fill:Black;fill-opacity:1"" />", bounds.Width, bounds.Width, bounds.Height, bounds.Height) ); // black background - excuse my lack of SVG knowledge - didn't seem to work in the <svg> tag foreach (DnaPolygon polygon in drawing.Polygons) { Render(polygon, sb, scale); } sb.Append(@"</svg>"); }
private void AddPoint(DnaDrawing drawing, JobInfo info) { if (Points.Count < info.Settings.PointsPerPolygonMax) { var newPoint = new DnaPoint(); int index = info.GetRandomNumber(1, Points.Count - 1); DnaPoint prev = Points[index - 1]; DnaPoint next = Points[index]; newPoint.X = (prev.X + next.X) / 2; newPoint.Y = (prev.Y + next.Y) / 2; Points.Insert(index, newPoint); drawing.SetDirty(); } }
/// <summary> /// Render a Drawing /// </summary> /// <remarks> /// Sorry about the hardcoded Width/Height and ScaleTransform... feel free to fix it :) /// </remarks> public static void Render(DnaDrawing drawing, ref StringBuilder sb, int scale) { sb = new StringBuilder(); sb.Append(@"<Canvas xmlns=""http://schemas.microsoft.com/client/2007"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" Width=""800"" Height=""600"" Background=""Black"" x:Name=""canvas""> <Canvas.RenderTransform> <TransformGroup> <ScaleTransform ScaleX=""2"" ScaleY=""2"" /> </TransformGroup> </Canvas.RenderTransform>"); sb.Append("\r\n"); foreach (DnaPolygon polygon in drawing.Polygons) { Render(polygon, sb, scale); } sb.Append(@"</Canvas>"); }
private void AddPoint(DnaDrawing drawing) { if (Points.Count < Settings.ActivePointsPerPolygonMax) { if (drawing.PointCount < Settings.ActivePointsMax) { var newPoint = new DnaPoint(); int index = Tools.GetRandomNumber(1, Points.Count - 1); DnaPoint prev = Points[index - 1]; DnaPoint next = Points[index]; newPoint.X = (prev.X + next.X)/2; newPoint.Y = (prev.Y + next.Y)/2; Points.Insert(index, newPoint); drawing.SetDirty(); } } }
// 2008-12-14 DanByström: method optimized for speed public static double GetDrawingFitness(DnaDrawing newDrawing, SourceImage sourceImage) { double error = 0; using (var bmp = new Bitmap(sourceImage.Width, sourceImage.Height)) using (Graphics g = Graphics.FromImage(bmp)) { Renderer.Render(newDrawing, g, 1); BitmapData bd = bmp.LockBits( new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); unchecked { unsafe { fixed (Pixel* psourcePixels = sourceImage.Pixels) { var p1 = (Pixel*) bd.Scan0.ToPointer(); Pixel* p2 = psourcePixels; for (int i = sourceImage.Pixels.Length; i > 0; i--, p1++, p2++) { int R = p1->R - p2->R; int G = p1->G - p2->G; int B = p1->B - p2->B; error += R*R + G*G + B*B; } } } } bmp.UnlockBits(bd); } // error += newDrawing.Polygons.Count * 3 ; return error; }
internal void Mutate(DnaDrawing drawing, JobInfo info) { if (info.WillMutate(info.Settings.AddPointMutationRate)) { AddPoint(drawing, info); } if (info.WillMutate(info.Settings.RemovePointMutationRate)) { RemovePoint(drawing, info); } Brush.Mutate(drawing, info); unchecked { for (int i = 0; i < Points.Count; i++) { var point = Points[i]; point.Mutate(drawing, info); } } }
private void FlipSplines(DnaDrawing drawing, JobInfo info) { SetSplinesAndFilled(info.Settings, !Splines, Filled); }
internal void Init(DnaDrawing drawing, JobInfo info) { X = info.GetRandomNumber(0, info.SourceImage.Width); Y = info.GetRandomNumber(0, info.SourceImage.Height); }