2013-08-02 17 views
1

我有示例代码,但它完全遗漏了我的(void *)should_be!如何在OpenCL中表示图像

我设置一个cl_image_desc,cl_image_format,缓存,产地,以及区域:

cl_image_desc desc; 
desc.image_type = CL_MEM_OBJECT_IMAGE2D; 
desc.image_width = width; 
desc.image_height = height; 
desc.image_depth = 0; 
desc.image_array_size = 0; 
desc.image_row_pitch = 0; 
desc.image_slice_pitch = 0; 
desc.num_mip_levels = 0; 
desc.num_samples = 0; 
desc.buffer = NULL; 

cl_image_format format; 
format.image_channel_order = CL_R; 
format.image_channel_data_type = CL_FLOAT; 

cl_mem bufferSourceImage = clCreateImage(context, CL_MEM_READ_ONLY, &format, &desc, NULL, NULL); 
size_t origin[3] = {0, 0, 0}; 
size_t region[3] = {width, height,1}; 

在接下来的片段sourceImage是一个空指针,以我的形象。但是我的形象是什么?对于每个像素有r,g,b,a,x和y值。

clEnqueueWriteImage(queue, bufferSourceImage, CL_TRUE, origin, region, 0, 0, sourceImage, 0, NULL, NULL); 

如何将我的图像(一堆(r,g,b,a,x,y))转换为合适的数组?

这是他们所提供的内核:

__kernel void convolution(__read_only image2d_t sourceImage, __write_only image2d_t outputImage, int rows, int cols, __constant float* filter, int filterWidth, sampler_t sampler) 
{ 
    int column = get_global_id(0); 
    int row = get_global_id(1); 
    int halfWidth = (int)(filterWidth/2); 

    float4 sum = {0.0f, 0.0f, 0.0f, 0.0f}; 

    int filterIdx = 0; 
    int2 coords; 
    for(int i = -halfWidth; i <= halfWidth; i++) 
    { 
     coords.y = row + i; 
     for(int i2 = -halfWidth; i2 <= halfWidth; i2++) 
     { 
      coords.x = column + i2; 
      float4 pixel; 
      pixel = read_imagef(sourceImage, sampler, coords); 
      sum.x += pixel.x * filter[filterIdx++]; 

     } 
    } 
    if(myRow < rows && myCol < cols) 
    { 
     coords.x = column; 
     coords.y = row; 
     write_imagef(outputImage, coords, sum); 
    } 
} 

回答

1

搭建cl_image_format,只要你喜欢,然后你只需要跟着你选择的是什么格式。目前您的通道(R,G,B,A)数据应表示为“单精度浮点值” - image_channel_data_type = CL_FLOAT,您可以只取这些通道中的一个通道并将其馈送到预期的R通道(image_channel_order = CL_R)。

内核预计浮动:

float4 pixel; 
pixel = read_imagef(sourceImage, sampler, coords); 
+0

好了,所以根据这个表格http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/read_imagef2d.html会我(将图像转换为数组的术语是什么?)将图像转换为看起来像{{r,g,b,1},{r,g,b,1}等}的数组,其中每个元素被映射到图像的行* w + col? – user1873073

+0

对此没有单一术语。我猜你读了一张图片,你的频道长8位。 CL_R表示三个颜色通道中只有一个通道。示例代码来自哪里? –

+0

'clCreateImage'为您创建了正确的维度矢量数组,您必须填写R通道。 –