private static SourceImage GetIntensityMap(SourceImage sourceImage, int workerMin, int range) { var newSourceImage = new SourceImage(); newSourceImage.Height = sourceImage.Height; newSourceImage.Width = sourceImage.Width; newSourceImage.Pixels = new Pixel[newSourceImage.Width*newSourceImage.Height]; for (int y = 0; y < sourceImage.Height; y++) { for (int x = 0; x < sourceImage.Width; x++) { Color c = sourceImage.GetPixel(x, y); var intensity = (int) (c.GetBrightness()*255); if (intensity >= workerMin && intensity <= workerMin + range) { newSourceImage.SetPixel( x, y, c ); } else { newSourceImage.SetPixel( x, y, Color.Black ); } } } return newSourceImage; }
public LayeredWorker(SourceImage sourceImage, Settings settings) { this.settings = settings; CurrentDrawing = GetNewInitializedDrawing(settings); CurrentDrawing.SourceImage = sourceImage; CurrentErrorLevel = double.MaxValue; }
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; }
private void StartEvolution() { var sourceImage = new SourceImage { Pixels = SetupSourceColorMatrix(picPattern.Image as Bitmap), Width = picPattern.Width, Height = picPattern.Height }; var info = new JobInfo { Settings = Project.Settings, SourceImage = sourceImage, }; //IEvolutionJob job = new LayeredEvolutionJob(sourceImage, 4); //DefaultEvolutionJob job = new DefaultEvolutionJob(sourceImage, currentDrawing); //IEvolutionJob job = new DefaultEvolutionJob(info); IEvolutionJob job = new ClusteredEvolutionJob(info); while (Project.IsRunning) { double newErrorLevel = job.GetNextErrorLevel(); var defJob = job as DefaultEvolutionJob; if (defJob != null) Project.Generations += defJob.Generations; Project.Mutations++; if (newErrorLevel <= Project.ErrorLevel) { Project.Selected++; if (newErrorLevel < Project.ErrorLevel) Project.Positive++; else Project.Neutral++; DnaDrawing newDrawing = job.GetDrawing(); if (currentDrawing == null) // to make always lockable... currentDrawing = new DnaDrawing(); lock (currentDrawing) { currentDrawing = newDrawing; Project.Drawing = currentDrawing.Clone(); } Project.ErrorLevel = newErrorLevel; SaveAnimationImage(newDrawing); } } }
private static SourceImage GetSourceImage() { Bitmap bitmap = Resources.MonaLisa; var image = new SourceImage { Pixels = SetupSourceColorMatrix(bitmap), Width = bitmap.Width, Height = bitmap.Height }; return image; }
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; }
public LayeredEvolutionJob(SourceImage sourceImage, int layerCount, Settings settings) { this.sourceImage = sourceImage; LayerCount = layerCount; workers = new List<LayeredWorker>(); int range = 255/LayerCount; int workerMin = 0; for (int i = 0; i < LayerCount; i++) { SourceImage newSourceImage = GetIntensityMap(sourceImage, workerMin, range); var worker = new LayeredWorker(newSourceImage, settings) { MinIntensity = workerMin, MaxIntensity = (workerMin + range), }; workers.Add(worker); workerMin += range; } }
// 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; }