Read / Write Image

Could anyone point me to a client / kernel example of the following? :

  1. Write to an image_2d in a kernel
  2. Read from the image buffer and write, in the client (c/c++), to an image format such as bmp or jpg.

I have written a kernel to do this but I can’t seem to read from the buffer in the client.

To read data from an OpenCL image you can use this function: clEnqueueReadImage() - see documentation for more details
You can then store the data into a bmp/jpg/whatever image but that has nothing to do with OpenCL … to do that , you can use some image libraries like OpenIL or others

Thanks for the reply.

My question is specifically directed at what to do once you get the buffer back. For example i have written to a 1024x1024 image in my kernel and read it into an array of bytes in my c++ code.

Is this the correct way of doing it? If I have a 32 bit unsigned int rgba pixel formatted image at 1024x1024 do I just read the image into a buffer that is 4 x 1024 x 1024 and then use that as data to pack into my bitmap?

To write into an image in a kernel you have to use the function write_imageui.

Here is an example on how you can copy an image2d into another one using a kernel. (Note that that for this purpose it is better to use the clEnqueueCopyImage() into the “client” API.

__kernel void CopyImage(__read_only image2d_t imageIn,__write_only image2d_t imageOut)

{

  const sampler_t sampler=CLK_NORMALIZED_COORDS_FALSE|CLK_ADDRESS_CLAMP|CLK_FI

LTER_NEAREST;

  int gid0 = get_global_id(0);

  int gid1 = get_global_id(1);

  uint4 pixel;

  pixel=read_imageui(imageIn,sampler,(int2)(gid0,gid1));

  write_imageui (imageOut,(int2)(gid0,gid1),pixel);

}