public Bitmap ProcessImage(Bitmap inImage) { ComputeImage2D oclInImage = null; ComputeImage2D oclOutImage = null; BitmapData inImageData = inImage.LockBits(new Rectangle(0, 0, inImage.Width, inImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); Bitmap outImage = new Bitmap(inImage.Width, inImage.Height, PixelFormat.Format32bppArgb); BitmapData outImageData = outImage.LockBits(new Rectangle(0, 0, outImage.Width, outImage.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); try { oclInImage = new ComputeImage2D(oclContext, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, new ComputeImageFormat(ComputeImageChannelOrder.Bgra, ComputeImageChannelType.UNormInt8), inImage.Width, inImage.Height, 0, inImageData.Scan0); oclOutImage = new ComputeImage2D(oclContext, ComputeMemoryFlags.WriteOnly | ComputeMemoryFlags.CopyHostPointer, new ComputeImageFormat(ComputeImageChannelOrder.Bgra, ComputeImageChannelType.UNormInt8), outImage.Width, outImage.Height, 0, outImageData.Scan0); oclKernel.SetMemoryArgument(0, oclInImage); oclKernel.SetMemoryArgument(1, oclOutImage); oclCommandQueue.Execute(oclKernel, new long[] { 0, 0 }, new long[] { oclInImage.Width, oclInImage.Height }, null, null); oclCommandQueue.Finish(); oclCommandQueue.ReadFromImage(oclOutImage, outImageData.Scan0, true, null); } catch (Exception) { throw; } finally { inImage.UnlockBits(inImageData); outImage.UnlockBits(outImageData); if (oclInImage != null) { oclInImage.Dispose(); oclInImage = null; } if (oclOutImage != null) { oclOutImage.Dispose(); oclOutImage = null; } } return(outImage); }
public static void Run(TextWriter log, ComputeContext context) { StartTest(log, "Image test"); try { log.Write("Creating command queue... "); ComputeCommandQueue commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); log.WriteLine("done."); int width = 16; int height = 16; log.Write("Creating first bitmap and drawing shapes... "); Bitmap firstBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); Graphics graphics = Graphics.FromImage(firstBitmap); graphics.FillEllipse(Brushes.Red, 0, 0, width / 2, height / 2); graphics.FillRectangle(Brushes.Green, width / 2 + 1, 0, width / 2, height / 2); graphics.FillRectangle(Brushes.Blue, width / 2 + 1, height / 2 + 1, width / 2, height / 2); log.WriteLine("done."); log.Write("Creating OpenCL image object from first bitmap... "); ComputeImage2D clImage = new ComputeImage2D(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, firstBitmap); log.WriteLine("done."); log.Write("Creating second bitmap... "); Bitmap secondBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); BitmapData bmpData = secondBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, secondBitmap.PixelFormat); log.WriteLine("done."); log.Write("Reading from OpenCL image object... "); commands.ReadFromImage(clImage, bmpData.Scan0, true, null); log.WriteLine("done."); secondBitmap.UnlockBits(bmpData); log.Write("Comparing bitmaps... "); for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) if (firstBitmap.GetPixel(i, j) != secondBitmap.GetPixel(i, j)) throw new Exception("Image data mismatch!"); log.WriteLine("passed."); } catch (Exception e) { log.WriteLine(e.ToString()); } EndTest(log, "Image test"); }
public static void CLApply2DLUT(ComputeContext context) { ComputeImageFormat format = new ComputeImageFormat(ComputeImageChannelOrder.Bgra, ComputeImageChannelType.UnsignedInt8); var startTime = LLTools.TimestampMS(); #region Visible / Temporary Source BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat); ComputeImage2D source0 = new ComputeImage2D(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, format, bmp.Width, bmp.Height, bitmapData.Stride, bitmapData.Scan0); bmp.UnlockBits(bitmapData); #endregion #region Infrared Source bitmapData = irBmp.LockBits(new Rectangle(0, 0, irBmp.Width, irBmp.Height), ImageLockMode.ReadOnly, irBmp.PixelFormat); ComputeImage2D source1 = new ComputeImage2D(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, format, irBmp.Width, irBmp.Height, bitmapData.Stride, bitmapData.Scan0); irBmp.UnlockBits(bitmapData); #endregion #region Output ComputeImage2D output = new ComputeImage2D(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.AllocateHostPointer, format, bmp.Width, bmp.Height, 0, IntPtr.Zero); #endregion #region Variable Initialization ComputeEventList eventList = new ComputeEventList(); ComputeCommandQueue commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); #region Apply Curve applyCurveKernel.SetMemoryArgument(0, source0); applyCurveKernel.SetMemoryArgument(1, output); applyCurveKernel.SetMemoryArgument(2, curveLutBuffer); #endregion #region Apply LUT 2D apply2DLUTKernel.SetMemoryArgument(0, source1); apply2DLUTKernel.SetMemoryArgument(1, output); apply2DLUTKernel.SetMemoryArgument(2, source0); apply2DLUTKernel.SetMemoryArgument(3, lut2DBuffer); #endregion #region Reprojection var latRangeBuff = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, latRange); var lonRangeBuff = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, lonRange); var coverageBuff = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, coverage); var trimBuff = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, trim); var sizeBuff = new ComputeBuffer <uint>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, size); reprojectKernel.SetMemoryArgument(0, source0); reprojectKernel.SetMemoryArgument(1, output); reprojectKernel.SetValueArgument(2, satelliteLongitude); reprojectKernel.SetValueArgument(3, coff); reprojectKernel.SetValueArgument(4, cfac); reprojectKernel.SetValueArgument(5, loff); reprojectKernel.SetValueArgument(6, lfac); reprojectKernel.SetValueArgument(7, (uint)(fixAspect ? 1 : 0)); reprojectKernel.SetValueArgument(8, aspectRatio); reprojectKernel.SetMemoryArgument(9, latRangeBuff); reprojectKernel.SetMemoryArgument(10, lonRangeBuff); reprojectKernel.SetMemoryArgument(11, coverageBuff); reprojectKernel.SetMemoryArgument(12, trimBuff); reprojectKernel.SetMemoryArgument(13, sizeBuff); #endregion #endregion #region Run Pipeline UIConsole.Log("Executing curve kernel"); commands.Execute(applyCurveKernel, null, new long[] { bmp.Width, bmp.Height }, null, eventList); UIConsole.Log("Executing LUT2D kernel"); commands.Execute(apply2DLUTKernel, null, new long[] { bmp.Width, bmp.Height }, null, eventList); UIConsole.Log("Executing kernel"); commands.Execute(reprojectKernel, null, new long[] { bmp.Width, bmp.Height }, null, eventList); #endregion #region Dump Bitmap UIConsole.Log("Dumping bitmap"); Bitmap obmp = new Bitmap(bmp.Width, bmp.Height, bmp.PixelFormat); BitmapData bmpData = obmp.LockBits(new Rectangle(0, 0, obmp.Width, obmp.Height), ImageLockMode.ReadWrite, obmp.PixelFormat); commands.ReadFromImage(output, bmpData.Scan0, true, null); obmp.UnlockBits(bmpData); var delta = LLTools.TimestampMS() - startTime; UIConsole.Log($"Took {delta} ms to Apply Curve -> Apply Lut2D (FalseColor) -> Reproject"); UIConsole.Log("Saving bitmap"); obmp.Save("teste.png"); UIConsole.Log("Done"); bmp.Save("original.png"); #endregion }