-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathOpenCL Desaturate.jstalk
36 lines (26 loc) · 1.25 KB
/
OpenCL Desaturate.jstalk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var src = """
__kernel void clDesaturate(read_write image2d_t src, read_write image2d_t dst, int width, int height) {
int2 coord = (int2)(get_global_id(0), get_global_id(1));
if (coord.x >= width || coord.y >= height) {
return;
}
float4 color = read_imagef(src, CLK_FILTER_NEAREST, coord);
float lum = (0.30f * color.x + 0.59f * color.y + 0.11f * color.z);
float4 val = (float4)(lum, lum, lum, color.w);
write_imagef(dst, coord, val);
}
""";
var context = [COSOpenCLContext new];
var program = [context programWithSource:src];
[program build];
var imagePath = [COSImageTools pathOfImageNamed:"llama"];
var kernel = [program createKernel:"clDesaturate"];
var inBuffer = [COSOpenCLImageBuffer instanceWithContext:context usingImageAtPath:imagePath];
var outBuffer = [COSOpenCLImageBuffer instanceWithContext:context width:[inBuffer width] height:[inBuffer height]];
[kernel setArgument:0 buffer:inBuffer];
[kernel setArgument:1 buffer:outBuffer];
[kernel setIntArgumentAtIndex:2 value:[inBuffer width]];
[kernel setIntArgumentAtIndex:3 value:[inBuffer height]];
[kernel enqueueCallWithGlobalSizeX:[inBuffer width] Y:[inBuffer height]];
[context finish];
[COSImageTools viewImageBuffer:outBuffer inWindowNamed:"OpenCL"];