gpt4 book ai didi

GPU 中的 C# 位图 GetPixel()、SetPixel()

转载 作者:太空狗 更新时间:2023-10-29 17:53:14 25 4
gpt4 key购买 nike

我正在使用 Cudafy 作为 C# 包装器我需要获取位图的颜色信息 InputBitmap0.GetPixel(x, y) 并为输出创建一个新的位图。

我需要在 GPU 中完成以下工作。

在 CPU 中

OutputBitmap.SetPixel(object_point_x, object_point_y, InputBitmap0.GetPixel(x, y));

简而言之:

如何在 GPU 中为输入位图的每个像素点获取 GetPixel(),为输出位图位图的每个像素点设置像素点。

最佳答案

OutputBitmap.SetPixel(object_point_x, object_point_y, InputBitmap0.GetPixel(x, y))

这花了一些时间,但最终,我破获了我的案子。

我们有两个 Bitmap :一个用于输出 OutputBitmap 另一个用于输入 InputBitmap0

让我们把这个任务分成几个部分:

  1. InputBitmap0.GetPixel() x ,y 坐标
  2. 然后,OutputBitmap.SetPixel() 用于不同的坐标 object_point_x, object_point_y

Cudafy 不支持BitmapColor 类型的数据。所以我将位图转换为 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 完成上述工作(制作数组),因为我们将在 GPU 中执行 SetPixel() 但我们会将数组复制回稍后位图。

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_coloroutput_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];

}

I am taking values of each R,G,B,A ,ex: input_ragba_color[(y *
stride0) + (x * 4) + 1]
which is solving 1st task (InputBitmap0.GetPixel())

dev_output_rgba_color is taking the values of input_ragba_color example:

dev_output_rgba_color[(object_point_y * OutputBitmapData_Stride) + (object_point_x * 4)] = input_ragba_color[(y * stride0) + (x * 4)]; 

which is solves our 2nd task (OutputBitmap.SetPixel())

我们现在知道 gpu 已经为我们的 OutputBitmap 填充了一个数组(dev_output_rgba_color)。

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 包含更新后的值。

关于GPU 中的 C# 位图 GetPixel()、SetPixel(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42037700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com