/// <summary>Restores black pixels to a rendered bitmap</summary> /// <param name="bmp">Rendered bitmap to get restored</param> /// <param name="origBmp">Original bitmap</param> public Bitmap RestoreBlackPixels(Bitmap origBmp, Bitmap bmp) { CLbmpSrc.WriteBitmap(origBmp); CLimgFinal1.WriteBitmap(bmp); kernelRestoreBlackPixels.Execute(new CLCalc.Program.MemoryObject[] { CLcfgVars, CLbmpSrc, CLimgFinal1, CLimgFinal2 }, new int[] { bmp.Width, bmp.Height }); return(CLimgFinal2.ReadBitmap()); }
/// <summary>Shades a bitmap</summary> /// <param name="bmp">Bitmap to shade</param> /// <param name="bmp">Actually render?</param> /// <returns></returns> public void Shade(Bitmap bmp, PictureBox pb, bool doRender) { Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height); Graphics g = Graphics.FromImage(bmp2); g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height); //draws points onto thresholded bitmap for (int i = 0; i < Colors.Count; i++) { if (Points[i].Count > 1) { g.DrawLines(new Pen(Color.Black, 2), Points[i].ToArray()); } } #region Initializations if (CLbmpSrc == null || CLbmpSrc.Width != bmp.Width || CLbmpSrc.Height != bmp.Height) { CLbmpSrc = new CLCalc.Program.Image2D(bmp2); CLbmpThresh = new CLCalc.Program.Image2D(bmp); CLthreshInfo = new CLCalc.Program.Variable(typeof(byte), bmp.Width * bmp.Height); CLbmpStroke = new CLCalc.Program.Image2D(bmp); CLbmpDists = new CLCalc.Program.Image2D(bmp); CLimgFinal1 = new CLCalc.Program.Image2D(new Bitmap(bmp2.Width, bmp2.Height)); CLimgFinal2 = new CLCalc.Program.Image2D(new Bitmap(bmp2.Width, bmp2.Height)); CLTotalPixWeight = new CLCalc.Program.Variable(typeof(float), bmp.Width * bmp.Height); lastPixWeight = new float[bmp.Width * bmp.Height]; CLWeight = new CLCalc.Program.Variable(typeof(float), bmp.Width * bmp.Height); lastImage = new Bitmap(bmp.Width, bmp.Height); } else { CLbmpSrc.WriteBitmap(bmp2); CLimgFinal1.WriteBitmap(new Bitmap(bmp2.Width, bmp2.Height)); CLimgFinal2.WriteBitmap(new Bitmap(bmp2.Width, bmp2.Height)); kernelinitTotalWeight.Execute(new CLCalc.Program.MemoryObject[] { CLTotalPixWeight }, new int[] { CLbmpSrc.Width, CLbmpSrc.Height }); } #endregion System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); System.Diagnostics.Stopwatch swCL = new System.Diagnostics.Stopwatch(); sw.Start(); //computes threshold kernelThreshold.Execute(new CLCalc.Program.MemoryObject[] { CLcfgVars, CLbmpSrc, CLthreshInfo, CLbmpThresh }, new int[] { bmp.Width, bmp.Height }); if (!doRender) { return; } //Reuse last render? if (ReUseLastRender) { CLTotalPixWeight.WriteToDevice(lastPixWeight); CLimgFinal1.WriteBitmap(lastImage); } else { lastRenderedIdx = 0; } //generates images for points for (int i = lastRenderedIdx; i < Colors.Count; i++) { Bitmap bmpStroke = new Bitmap(bmp.Width, bmp.Height); if (Points[i].Count > 1) { if (DistanceMaps[i] == null) { Graphics g2 = Graphics.FromImage(bmpStroke); g2.DrawLines(new Pen(Colors[i], 2), Points[i].ToArray()); CLbmpStroke.WriteBitmap(bmpStroke); int[] changed = new int[] { 0 }; swCL.Start(); changed[0] = 1; kernelinitWeight.Execute(new CLCalc.Program.MemoryObject[] { CLWeight }, new int[] { CLbmpSrc.Width, CLbmpSrc.Height }); int n = 0; while (changed[0] > 0 && n < MAXPROPAGITERS) { n++; changed[0] = 0; CLchanged.WriteToDevice(changed); kernelPropagateDist.Execute(new CLCalc.Program.MemoryObject[] { CLchanged, CLbmpStroke, CLthreshInfo, CLWeight, CLbmpDists }, new int[] { CLbmpSrc.Width, CLbmpSrc.Height }); CLchanged.ReadFromDeviceTo(changed); } swCL.Stop(); DistanceMaps[i] = new float[CLWeight.OriginalVarLength]; CLWeight.ReadFromDeviceTo(DistanceMaps[i]); } else { CLWeight.WriteToDevice(DistanceMaps[i]); } //composes total weight CLcolor.WriteToDevice(new float[] { (float)Colors[i].B, (float)Colors[i].G, (float)Colors[i].R }); kernelAddToTotalWeight.Execute(new CLCalc.Program.MemoryObject[] { CLTotalPixWeight, CLWeight, CLcolor, CLimgFinal1, CLimgFinal2, CLthreshInfo }, new int[] { CLbmpSrc.Width, CLbmpSrc.Height }); //swaps final images - result is always on CLimgFinal1 CLCalc.Program.Image2D temp = CLimgFinal1; CLimgFinal1 = CLimgFinal2; CLimgFinal2 = temp; if (pb != null) { pb.Image = GetRenderedImage(); pb.Refresh(); } } bmpStroke.Dispose(); } sw.Stop(); lastRenderedIdx = Colors.Count; //saves total pixel weight and final image CLTotalPixWeight.ReadFromDeviceTo(lastPixWeight); if (lastImage != null) { lastImage.Dispose(); } lastImage = CLimgFinal1.ReadBitmap(); }