Search code examples
imageopenclpixel

OpenCL image2d pixel edit


I'm a total newbie in OpenCL but i have a project to do in OpenCL and i'm stuck. I need to load image(bmp), throw it into GPU, change some pixels and save it into new bmp file. Everything works fine when i'm just copying an image or reversing Red/Green palette for example. But when i'm trying to change 2,4,6 .... pixel column to black i got error: CL_INVALID_KERNEL_NAME. As i said i'm a total newbie and i lack of ideas. But i'm sure that whole main code is 100% valid and the problem is with the kernel:

#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable

__kernel void image_change(__read_only image2d_t image1, __write_only image2d_t image2)
{
const sampler_t sampler=CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;

int x = get_global_id(0);
int y = get_global_id(1);
int2 pixelcoord;
float4 pixel = read_imagef(image1, sampler, pixelcoord(x,y));
float4 blackpixel = (float4)(0,0,0,0);
width = get_image_width(image1);
height = get_image_height(image1);
for (pixelcoord.x=x-width; pixelcoord.x<width; pixelcoord.y++)
{
    if(pixelcoord.x % 2 == 0)
    {
    for (pixelcoord.y=y-height; pixelcoord.y<height; pixelcoord.y++) 
        write_imagef(image2, pixelcoord, pixel);
    }
    else
    {
    for (pixelcoord.y=y-height; pixelcoord.y<height; pixelcoord.y++) 
        write_imagef(image2, pixelcoord, blackpixel);
    }
}
}

I'm sure there is something bad in code from the first "for".

  1. Why i got this error (coz kernel name is 100% ok)?
  2. How can i change pixels? (I mean i don't get how can i access to certain pixel and it's color)

I would be happy if someone could guide me at this point.


Solution

  • SOLVED! I found a post which explained me how it works. First of all i didn't initialise width and height (kid mistake). The rest went easier. Code:

    __kernel void image_change(__read_only image2d_t image1, __write_only image2d_t image2)
    {
    const sampler_t sampler=CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;
    
    
    int width = get_image_width(image1);
    int height = get_image_height(image1);
    
    int2 pixelcoord = (int2) (get_global_id(0), get_global_id(1));
    if (pixelcoord.x < width && pixelcoord.y < height)
    {   
        float4 pixel = read_imagef(image1, sampler, (int2)(pixelcoord.x, pixelcoord.y));
        float4 black = (float4)(0,0,0,0);
    
    if (pixelcoord.x % 2== 1)
    {   
        const float4 outColor = black;
        write_imagef(image2, pixelcoord, outColor);
    }
    else
        write_imagef(image2, pixelcoord, pixel);
    
    }
    }