Mali OpenCL SDK v1.1.0
 All Classes Files Functions Variables Macros Pages
image_scaling.cl
Go to the documentation of this file.
1 /*
2  * This confidential and proprietary software may be used only as
3  * authorised by a licensing agreement from ARM Limited
4  * (C) COPYRIGHT 2013 ARM Limited
5  * ALL RIGHTS RESERVED
6  * The entire notice above must be reproduced on all authorised
7  * copies and copies may only be made to the extent permitted
8  * by a licensing agreement from ARM Limited.
9  */
10 
11 /* [Define a sampler] */
12 const sampler_t sampler = CLK_NORMALIZED_COORDS_TRUE | CLK_ADDRESS_CLAMP | CLK_FILTER_LINEAR;
13 /* [Define a sampler] */
14 
22 __kernel void image_scaling(__read_only image2d_t sourceImage,
23  __write_only image2d_t destinationImage,
24  const float widthNormalizationFactor,
25  const float heightNormalizationFactor)
26 {
27  /*
28  * It is possible to get the width and height of an image object (using get_image_width and get_image_height).
29  * You could use this to calculate the normalization factors in the kernel.
30  * In this case, because the width and height doesn't change for each kernel,
31  * it is better to pass normalization factors to the kernel as parameters.
32  * This way we do the calculations once on the host side instead of in every kernel.
33  */
34 
35  /* [Calculate the coordinates] */
36  /*
37  * There is one kernel instance per pixel in the destination image.
38  * The global id of this kernel instance is therefore a coordinate in the destination image.
39  */
40  int2 coordinate = (int2)(get_global_id(0), get_global_id(1));
41 
42  /*
43  * That coordinate is only valid for the destination image.
44  * If we normalize the coordinates to the range [0.0, 1.0] (using the height and width of the destination image),
45  * we can use them as coordinates in the sourceImage.
46  */
47  float2 normalizedCoordinate = convert_float2(coordinate) * (float2)(widthNormalizationFactor, heightNormalizationFactor);
48  /* [Calculate the coordinates] */
49 
50  /* [Read from the source image] */
51  /*
52  * Read colours from the source image.
53  * The sampler determines how the coordinates are interpreted.
54  * Because bilinear filtering is enabled, the value of colour will be the average of the 4 pixels closest to the coordinate.
55  */
56  float4 colour = read_imagef(sourceImage, sampler, normalizedCoordinate);
57  /* [Read from the source image] */
58 
59  /* [Write to the destination image] */
60  /*
61  * Write the colour to the destination image.
62  * No sampler is used here as all writes must specify an exact valid pixel coordinate.
63  */
64  write_imagef(destinationImage, coordinate, colour);
65  /* [Write to the destination image] */
66 }