2013-12-08 44 views
1

我在OpenCL的總新手,但我有一個項目中的OpenCL做,我被卡住。 我需要加載圖像(bmp),將其投入GPU,更改一些像素並將其保存到新的bmp文件中。一切正常,當我只是複製一個圖像或反轉紅/綠色調色板例如。但是當我試圖改變2,4,6 ....像素列黑色我得到錯誤: CL_INVALID_KERNEL_NAME。 正如我所說我是一個總新手,我缺乏想法。但我敢肯定,整個主代碼是100%有效的,問題是與內核:的OpenCL image2d像素編輯

#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); 
    } 
} 
} 

我敢肯定,有什麼不愉快的從最初的「爲」代碼。

  1. 爲什麼我得到這個錯誤(因爲內核名稱是100%好)?
  2. 如何更改像素? (我的意思是我沒有得到如何訪問某個像素,它的顏色)

我會很高興,如果有人能指導我在這一點上。

回答

1

已解決!我找到了一篇文章,解釋了它是如何工作的。首先我沒有初始化寬度和高度(孩子錯誤)。其餘的更容易。 代碼:

__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); 

} 
} 
+0

非常好!我看到你也發現你不需要內核中的兩個嵌套循環,因爲運行時會爲你處理。 – Dithermaster