OpenCL 图像格式描述符

在clCreateImage()函数中,参数image_format指向cl_image_format结构的指针。cl_image_format结构定义如下:

typedef struct _cl_image_format
{
    cl_channel_order image_channel_order;
    cl_channel_type image_channel_data_type;
} cl_image_format;

image_channel_order指定了图形通道数以及通道布局,即图像中通道存储的内容布局。其有效值如下表所示。

图像通道次序

image_channel_order指定了图像通道的数据类型。下表列出了图像通道支持的数据类型。

图像通道支持的数据类型

上表中的数据类型容易理解。常量的24位RGB模型用CL_UNSIGNED_INT8数据来表示,每个通道使用8位来存储。如下代码展示了图像描述符的使用:

cl_image_format rgb_format;
rgb_format.image_channel_order = CL_RGB;
rgb_format.image_channel_data_type = CL_UNSIGNED_INT8;

OpenCL提供了如下函数来查询OpenCL实现所支持的图像格式:

cl_int clGetSupportedImageFormats( cl_context context,
                                          cl_mem_flags flags,
cl_mem_object_type image_type,
                                          cl_uint num_entries,
                                          cl_image_format *image_formats,
                                          cl_uint *num_image_formats)

1)参数context为OpenCL上下文,将在此上下文上创建图像对象。
2)参数f lag是一个位域,用来指明如何分配以及使用将要创建的图像对象。

3)参数image_type为图像类型,其值可以是:

  • CL_MEM_OBJECT_IMAGE1D;
  • CL_MEM_OBJECT_IMAGE1D_BUFFER;
  • CL_MEM_OBJECT_IMAGE2D;
  • CL_MEM_OBJECT_IMAGE3D;
  • CL_MEM_OBJECT_IMAGE1D_ARRAY;
  • CL_MEM_OBJECT_IMAGE2D_ARRAY。

4)参数num_entries为image_formats所能容纳的表项数目。
5)参数image_formats用于存储所返回的图像格式。每一项都是cl_image_format结构体。如果参数为NULL,则忽略。
6)参数num_image_formats即所返回的图像格式的实际数目。如果为NULL,则忽略。

clGetSupportedImageFormats()函数会返回一个图像格式的联合体,其中图像格式是上下文中所有设备都支持的。
如下代码展示了如何获得上下文支持的所有图像格式:

……
err = clGetSupportedImageFormats(context, CL_MEM_READ_WRITE,
                                        CL_MEM_OBJECT_IMAGE2D, 0, NULL,
                                        &ItemSize);
cl_image_format *image_formats;
image_formats = (cl_image_format *)malloc(
                        sizeof(cl_image_format) * ItemSize);
err = clGetSupportedImageFormats(context, CL_MEM_READ_WRITE,
                                        CL_MEM_OBJECT_IMAGE2D, size,
                                        image_formats, NULL);

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程