Skip to content

Instantly share code, notes, and snippets.

@Ming-Tang
Last active August 29, 2015 14:04
Show Gist options
  • Save Ming-Tang/c0d270494d80f1a531c7 to your computer and use it in GitHub Desktop.
Save Ming-Tang/c0d270494d80f1a531c7 to your computer and use it in GitHub Desktop.
OpenCL image distortion
/**
QueryFormat example : query image format dynamically inside the kernel
Written by Olivier Chafik, no right reserved :-) */
// See http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/sampler_t.html
const sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_FILTER_NEAREST | CLK_ADDRESS_CLAMP_TO_EDGE;
int2 ripple(float x, float y, float a, float w) {
float m = sqrt(x*x + y*y);
float s = sin(w*m);
float x1 = a*y*m*s;
float y1 = -a*x*m*s;
return (int2)(x1, y1);
}
int2 displacement(int xi, int yi, int width, int height) {
float aspect = ((float) height) / ((float) width);
float x = ((float) xi) / ((float) width), y = ((float) yi) / ((float) height);
return ripple(x - 0.5, aspect*(y - 0.5 + 0.35), 50, 800)
- ripple(x - 0.5, aspect*(y - 0.5 - 0.35), 50, 800)
+ ripple(x - 0.5, aspect*(y), 2, 5000);
}
__kernel void test(
read_only image2d_t inputImage,
write_only image2d_t outputImage)
{
// See http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/get_image_dim.html
int2 dimensions = get_image_dim(inputImage);
int width = dimensions.x, height = dimensions.y;
// See http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/get_image_channel_data_type.html
int channelDataType = get_image_channel_data_type(inputImage);
// See http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/get_image_channel_order.html
int channelOrder = get_image_channel_order(inputImage);
int x = get_global_id(0), y = get_global_id(1);
int2 coordinates = (int2)(x, y);
int2 disp = displacement(x, y, width, height);
int2 fromCoordinates = coordinates + disp;
// See http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/read_imagef2d.html
//float4 pixel = read_imagef(inputImage, sampler, coordinates);
float4 pixel = read_imagef(inputImage, sampler, fromCoordinates);
float4 transformedPixel = pixel;
//float4 transformedPixel = (float4)(0);//pixel;
// See http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/write_image.html
write_imagef(outputImage, coordinates, transformedPixel);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment