OutputBitmap.SetPixel(object_point_x, object_point_y, InputBitmap0.GetPixel(x, y))
这需要时间,但最后,我,破获了我的案子。
我们有两个Bitmap
:一个用于输出OutputBitmap
,另一个用于输入InputBitmap0
让我们把这个任务分成几个部分:
InputBitmap0.GetPixel()
为x
,y
协调_
- 然后,
OutputBitmap.SetPixel()
对于不同的坐标object_point_x, object_point_y
Cudafy 不支持Bitmap
或Color
键入数据。所以我将位图转换为byte
类型。
BitmapData InputBitmapData0 = InputBitmap0.LockBits(new Rectangle(0, 0, InputBitmap0.Width, InputBitmap0.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
IntPtr ptr0 = InputBitmapData0.Scan0;//pointer for color
int stride0 = InputBitmapData0.Stride;
byte[] input_ragba_color = new byte[InputBitmapData0.Stride * InputBitmap0.Height];
Marshal.Copy(ptr0, input_ragba_color, 0, bytes0);// Copy the RGB values of color value into the array.
我们已将 的内容复制InputBitmap0
到rgbValues
数组中。现在我们需要做GetPixel()
(获取R,G,B,A的值)的工作。
我们也需要完成上述工作(制作数组),OutputBitmap
因为我们将SetPixel()
在 GPU 中完成,但稍后我们会将数组复制回位图。
BitmapData OutputBitmapData = OutputBitmap.LockBits(new Rectangle(0, 0, OutputBitmap.Width, OutputBitmap.Height), ImageLockMode.WriteOnly, OutputBitmap.PixelFormat);
IntPtr ptr_output = OutputBitmapData.Scan0;
byte[] output_ragba = new byte[OutputBitmapData.Stride * OutputBitmap.Height];
它的 GPU 计算时间。让我们初始化gpu。
CudafyModule km = new CudafyTranslator.Cudafy();
GPGPU gpu = new CudafyHost.getDevice(CudafyModes.Target, CudafyModes.DeviceId);
gpu.LoadModule(km);
现在发送input_ragba_color
和output_ragba
到 gpu,因为我们可以迭代数组并进行任何计算。
byte[] dev_output_rgba_color = gpu.Allocate<byte>(output_ragba.Length);
byte[] dev_input_ragba_color = gpu.CopyToDevice(input_ragba_color);
gpu.Launch(N, 1).update_bitmap(x, y, object_point_x, object_point_y,int stride0, int OutputBitmapData.Stride,dev_input_ragba_color,dev_output_rgba_color);
现在在 GPU(内核)内部
[Cudafy]
public static void update_bitmap(GThread thread, int x,int y,int object_point_x,int object_point_y,int stride0, int OutputBitmapData_Stride,byte [] dev_input_ragba_color,byte [] dev_output_rgba_color)
{
dev_output_rgba_color[(object_point_y * OutputBitmapData_Stride) + (object_point_x * 4)] = input_ragba_color[(y * stride0) + (x * 4)];
dev_output_rgba_color[(object_point_y * OutputBitmapData_Stride) + (object_point_x * 4) + 1] = input_ragba_color[(y * stride0) + (x * 4) + 1];
dev_output_rgba_color[(object_point_y * OutputBitmapData_Stride) + (object_point_x * 4) + 2] = input_ragba_color[(y * stride0) + (x * 4) + 2];
dev_output_rgba_color[(object_point_y * OutputBitmapData_Stride) + (object_point_x * 4) + 3] = input_ragba_color[(y * stride0) + (x * 4) + 3];
}
我正在获取每个 R、G、B、A 的值,例如:input_ragba_color[(y *
stride0) + (x * 4) + 1]
正在解决第一个任务(InputBitmap0.GetPixel()
)
dev_output_rgba_color
正在采用input_ragba_color
示例的值:
dev_output_rgba_color[(object_point_y * OutputBitmapData_Stride) + (object_point_x * 4)] = input_ragba_color[(y * stride0) + (x * 4)];
这解决了我们的第二个任务 ( OutputBitmap.SetPixel()
)
我们现在知道 gpu 已经dev_output_rgba_color
为我们的OutputBitmap
.
gpu.CopyFromDevice(dev_output_rgba_color, output_ragba); //dev_output_rgba_color values will be assigned to output_ragba
gpu.FreeAll();
OutputBitmap
使用内存指针将结果复制回并从内存中解锁。
Marshal.Copy(output_ragba, 0, ptr_output, output_bytes);// Copy the RGB values of color value into the array.
OutputBitmap.UnlockBits(OutputBitmapData);
现在OutputBitmap
包含更新的值。