aboutsummaryrefslogtreecommitdiff
path: root/src/core/CL/cl_kernels/sobel_filter.cl
blob: fc2b0eed92beca268d89f5f5387705bbfd064bbe (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
 * Copyright (c) 2016, 2017 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include "helpers.h"

/***********************************************/
/*   Begin implementation of Sobel3x3 filter   */
/***********************************************/

/** This OpenCL kernel that computes a Sobel3x3 filter.
 *
 * @attention To enable computation of the X gradient -DGRAD_X must be passed at compile time, while computation of the Y gradient
 * is performed when -DGRAD_Y is used. You can use both when computation of both gradients is required.
 *
 * @param[in]  src_ptr                              Pointer to the source image. Supported data types: U8
 * @param[in]  src_stride_x                         Stride of the source image in X dimension (in bytes)
 * @param[in]  src_step_x                           src_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  src_stride_y                         Stride of the source image in Y dimension (in bytes)
 * @param[in]  src_step_y                           src_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  src_offset_first_element_in_bytes    The offset of the first element in the source image
 * @param[out] dst_gx_ptr                           Pointer to the destination image. Supported data types: S16
 * @param[in]  dst_gx_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]  dst_gx_step_x                        dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  dst_gx_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]  dst_gx_step_y                        dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  dst_gx_offset_first_element_in_bytes The offset of the first element in the destination image
 * @param[out] dst_gy_ptr                           Pointer to the destination image. Supported data types: S16
 * @param[in]  dst_gy_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]  dst_gy_step_x                        dst_gy_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  dst_gy_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]  dst_gy_step_y                        dst_gy_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  dst_gy_offset_first_element_in_bytes The offset of the first element in the destination image
 */
__kernel void sobel3x3(
    IMAGE_DECLARATION(src)
#ifdef GRAD_X
    ,
    IMAGE_DECLARATION(dst_gx)
#endif /* GRAD_X */
#ifdef GRAD_Y
    ,
    IMAGE_DECLARATION(dst_gy)
#endif /* GRAD_Y */
)
{
    Image src = CONVERT_TO_IMAGE_STRUCT(src);
#ifdef GRAD_X
    Image dst_gx = CONVERT_TO_IMAGE_STRUCT(dst_gx);
#endif /* GRAD_X */
#ifdef GRAD_Y
    Image dst_gy = CONVERT_TO_IMAGE_STRUCT(dst_gy);
#endif /* GRAD_Y */

    // Output pixels
#ifdef GRAD_X
    short8 gx = (short8)0;
#endif /* GRAD_X */
#ifdef GRAD_Y
    short8 gy = (short8)0;
#endif /* GRAD_Y */

    // Row0
    uchar16 temp   = vload16(0, offset(&src, -1, -1));
    short8  left   = convert_short8(temp.s01234567);
    short8  middle = convert_short8(temp.s12345678);
    short8  right  = convert_short8(temp.s23456789);
#ifdef GRAD_X
    gx += left * (short8)(-1);
    gx += right * (short8)(+1);
#endif /* GRAD_X */
#ifdef GRAD_Y
    gy += left * (short8)(-1);
    gy += middle * (short8)(-2);
    gy += right * (short8)(-1);
#endif /* GRAD_Y */

    // Row1
    temp  = vload16(0, offset(&src, -1, 0));
    left  = convert_short8(temp.s01234567);
    right = convert_short8(temp.s23456789);
#ifdef GRAD_X
    gx += left * (short8)(-2);
    gx += right * (short8)(+2);
#endif /* GRAD_X */

    // Row2
    temp   = vload16(0, offset(&src, -1, 1));
    left   = convert_short8(temp.s01234567);
    middle = convert_short8(temp.s12345678);
    right  = convert_short8(temp.s23456789);
#ifdef GRAD_X
    gx += left * (short8)(-1);
    gx += right * (short8)(+1);
#endif /* GRAD_X */
#ifdef GRAD_Y
    gy += left * (short8)(+1);
    gy += middle * (short8)(+2);
    gy += right * (short8)(+1);
#endif /* GRAD_Y */

    // Store results
#ifdef GRAD_X
    vstore8(gx, 0, ((__global short *)dst_gx.ptr));
#endif /* GRAD_X */
#ifdef GRAD_Y
    vstore8(gy, 0, ((__global short *)dst_gy.ptr));
#endif /* GRAD_Y */
}

/**********************************************/
/*    End implementation of Sobel3x3 filter   */
/**********************************************/

/***********************************************/
/*   Begin implementation of Sobel5x5 filter   */
/***********************************************/

/** Compute a 1D horizontal sobel filter 1x5 for 8 bytes assuming the input is made of 1 channel of 1 byte (i.e 8 pixels).
 *
 * @param[in] src             Pointer to source image.
 * @param[in] left1_coeff_gx  Weight of the most left pixel for gx
 * @param[in] left2_coeff_gx  Weight of the left pixel for gx
 * @param[in] middle_coeff_gx Weight of the middle pixel for gx
 * @param[in] right1_coeff_gx Weight of the right pixel for gx
 * @param[in] right2_coeff_gx Weight of the most right pixel for gx
 * @param[in] left1_coeff_gy  Weight of the most left pixel for gy
 * @param[in] left2_coeff_gy  Weight of the left pixel for gy
 * @param[in] middle_coeff_gy Weight of the middle pixel for gy
 * @param[in] right1_coeff_gy Weight of the right pixel for gy
 * @param[in] right2_coeff_gy Weight of the most right pixel for gy
 *
 * @return a short16 containing short8 gx and short8 gy values.
 */
short16 sobel1x5(
    Image      *src,
    const short left1_coeff_gx,
    const short left2_coeff_gx,
    const short middle_coeff_gx,
    const short right1_coeff_gx,
    const short right2_coeff_gx,
    const short left1_coeff_gy,
    const short left2_coeff_gy,
    const short middle_coeff_gy,
    const short right1_coeff_gy,
    const short right2_coeff_gy)
{
    uchar16 temp = vload16(0, offset(src, -2, 0));
    short8  gx   = 0;
    short8  gy   = 0;
    short8  val;

    val = convert_short8(temp.s01234567);
    gx += val * (short8)left1_coeff_gx;
    gy += val * (short8)left1_coeff_gy;

    val = convert_short8(temp.s12345678);
    gx += val * (short8)left2_coeff_gx;
    gy += val * (short8)left2_coeff_gy;

    val = convert_short8(temp.s23456789);
    gx += val * (short8)middle_coeff_gx;
    gy += val * (short8)middle_coeff_gy;

    val = convert_short8(temp.s3456789a);
    gx += val * (short8)right1_coeff_gx;
    gy += val * (short8)right1_coeff_gy;

    val = convert_short8(temp.s456789ab);
    gx += val * (short8)right2_coeff_gx;
    gy += val * (short8)right2_coeff_gy;

    return (short16)(gx, gy);
}

/** Compute a 1D vertical sobel filter 5x1 for 8 bytes assuming the input is made of 1 channel of 1 byte (i.e 8 pixels).
 *
 * @param[in] src          Pointer to source image.
 * @param[in] up1_coeff    Weight of the most up pixel
 * @param[in] up2_coeff    Weight of the up pixel
 * @param[in] middle_coeff Weight of the middle pixel
 * @param[in] down1_coeff  Weight of the down pixel
 * @param[in] down2_coeff  Weight of the most down pixel
 *
 * @return a short8 containing 8 convoluted values.
 */
short8 sobel5x1(
    Image      *src,
    const short up1_coeff,
    const short up2_coeff,
    const short middle_coeff,
    const short down1_coeff,
    const short down2_coeff)
{
    short8 val;
    short8 out = (short8)0;

    val = vload8(0, (__global short *)offset(src, 0, -2));
    out += val * (short8)up1_coeff;

    val = vload8(0, (__global short *)offset(src, 0, -1));
    out += val * (short8)up2_coeff;

    val = vload8(0, (__global short *)offset(src, 0, 0));
    out += val * (short8)middle_coeff;

    val = vload8(0, (__global short *)offset(src, 0, 1));
    out += val * (short8)down1_coeff;

    val = vload8(0, (__global short *)offset(src, 0, 2));
    out += val * (short8)down2_coeff;

    return (short8)(out);
}

/** Apply a 1x5 sobel matrix to a single channel U8 input image and output two temporary channel S16 images.
 *
 * @attention To enable computation of the X gradient -DGRAD_X must be passed at compile time, while computation of the Y gradient
 * is performed when -DGRAD_Y is used. You can use both when computation of both gradients is required.
 *
 * @param[in]  src_ptr                              Pointer to the source image.. Supported data types: U8
 * @param[in]  src_stride_x                         Stride of the source image in X dimension (in bytes)
 * @param[in]  src_step_x                           src_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  src_stride_y                         Stride of the source image in Y dimension (in bytes)
 * @param[in]  src_step_y                           src_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  src_offset_first_element_in_bytes    The offset of the first element in the source image
 * @param[out] dst_gx_ptr                           Pointer to the destination image.. Supported data types: S16
 * @param[in]  dst_gx_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]  dst_gx_step_x                        dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  dst_gx_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]  dst_gx_step_y                        dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  dst_gx_offset_first_element_in_bytes The offset of the first element in the destination image
 * @param[out] dst_gy_ptr                           Pointer to the destination image. Supported data types: S16
 * @param[in]  dst_gy_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]  dst_gy_step_x                        dst_gy_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  dst_gy_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]  dst_gy_step_y                        dst_gy_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  dst_gy_offset_first_element_in_bytes The offset of the first element in the destination image
 */
__kernel void sobel_separable1x5(
    IMAGE_DECLARATION(src)
#ifdef GRAD_X
    ,
    IMAGE_DECLARATION(dst_gx)
#endif /* GRAD_X */
#ifdef GRAD_Y
    ,
    IMAGE_DECLARATION(dst_gy)
#endif /* GRAD_Y */
)
{
    Image src = CONVERT_TO_IMAGE_STRUCT(src);
#ifdef GRAD_X
    Image dst_gx = CONVERT_TO_IMAGE_STRUCT(dst_gx);
#endif /* GRAD_X */
#ifdef GRAD_Y
    Image dst_gy = CONVERT_TO_IMAGE_STRUCT(dst_gy);
#endif /* GRAD_Y */

    // Output pixels
    short16 gx_gy = sobel1x5(&src,
                             -1, -2, 0, 2, 1,
                             1, 4, 6, 4, 1);

    // Store result in dst
#ifdef GRAD_X
    vstore8(gx_gy.s01234567, 0, ((__global short *)dst_gx.ptr));
#endif /* GRAD_X */
#ifdef GRAD_Y
    vstore8(gx_gy.s89ABCDEF, 0, ((__global short *)dst_gy.ptr));
#endif /* GRAD_Y */
}

/** Apply a 5x1 convolution matrix to two single channel S16 input temporary images
 *  and output two single channel S16 images.
 *
 * @attention To enable computation of the X gradient -DGRAD_X must be passed at compile time, while computation of the Y gradient
 * is performed when -DGRAD_Y is used. You can use both when computation of both gradients is required.
 *
 * @param[in]  src_x_ptr                            Pointer to the source image.. Supported data types: S16
 * @param[in]  src_x_stride_x                       Stride of the source image in X dimension (in bytes)
 * @param[in]  src_x_step_x                         src_x_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  src_x_stride_y                       Stride of the source image in Y dimension (in bytes)
 * @param[in]  src_x_step_y                         src_x_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  src_x_offset_first_element_in_bytes  The offset of the first element in the source image
 * @param[out] dst_gx_ptr                           Pointer to the destination image. Supported data types: S16
 * @param[in]  dst_gx_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]  dst_gx_step_x                        dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  dst_gx_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]  dst_gx_step_y                        dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  dst_gx_offset_first_element_in_bytes The offset of the first element in the destination image
 * @param[in]  src_y_ptr                            Pointer to the source image. Supported data types: S16
 * @param[in]  src_y_stride_x                       Stride of the source image in X dimension (in bytes)
 * @param[in]  src_y_step_x                         src_y_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  src_y_stride_y                       Stride of the source image in Y dimension (in bytes)
 * @param[in]  src_y_step_y                         src_y_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  src_y_offset_first_element_in_bytes  The offset of the first element in the source image
 * @param[out] dst_gy_ptr                           Pointer to the destination image. Supported data types: S16
 * @param[in]  dst_gy_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]  dst_gy_step_x                        dst_gy_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  dst_gy_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]  dst_gy_step_y                        dst_gy_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  dst_gy_offset_first_element_in_bytes The offset of the first element in the destination image
 * @param[in]  dummy                                Dummy parameter to easy conditional inclusion
 */
__kernel void sobel_separable5x1(
#ifdef GRAD_X
    IMAGE_DECLARATION(src_x),
    IMAGE_DECLARATION(dst_gx),
#endif /* GRAD_X */
#ifdef GRAD_Y
    IMAGE_DECLARATION(src_y),
    IMAGE_DECLARATION(dst_gy),
#endif /* GRAD_Y */
    int dummy)
{
#ifdef GRAD_X
    Image src_x  = CONVERT_TO_IMAGE_STRUCT(src_x);
    Image dst_gx = CONVERT_TO_IMAGE_STRUCT(dst_gx);
#endif /* GRAD_X */
#ifdef GRAD_Y
    Image src_y  = CONVERT_TO_IMAGE_STRUCT(src_y);
    Image dst_gy = CONVERT_TO_IMAGE_STRUCT(dst_gy);
#endif /* GRAD_Y */

#ifdef GRAD_X
    short8 gx = sobel5x1(&src_x,
                         1, 4, 6, 4, 1);
    vstore8(gx, 0, ((__global short *)dst_gx.ptr));
#endif /* GRAD_X */
#ifdef GRAD_Y
    short8 gy = sobel5x1(&src_y,
                         -1, -2, 0, 2, 1);
    vstore8(gy, 0, ((__global short *)dst_gy.ptr));
#endif /* GRAD_Y */
}

/**********************************************/
/*    End implementation of Sobel5x5 filter   */
/**********************************************/

/***********************************************/
/*   Begin implementation of Sobel7x7 filter   */
/***********************************************/

/* Sobel 1x7 horizontal X / 7x1 vertical Y coefficients */
#define X0 -1
#define X1 -4
#define X2 -5
#define X3 0
#define X4 5
#define X5 4
#define X6 1

/* Sobel 1x7 vertical X / 7x1 horizontal Y coefficients */
#define Y0 1
#define Y1 6
#define Y2 15
#define Y3 20
#define Y4 15
#define Y5 6
#define Y6 1

/* Calculates single horizontal iteration. */
#define SOBEL1x1_HOR(src, gx, gy, idx)                               \
    {                                                                \
        int8 val = convert_int8(vload8(0, offset(src, idx - 3, 0))); \
        gx += val * X##idx;                                          \
        gy += val * Y##idx;                                          \
    }

/* Calculates single vertical iteration. */
#define SOBEL1x1_VERT(src, g, direction, idx)                          \
    {                                                                  \
        int8 val = vload8(0, (__global int *)offset(src, 0, idx - 3)); \
        g += val * (int8)direction##idx;                               \
    }

/* Calculates a 1x7 horizontal iteration. */
#define SOBEL1x7(ptr, gx, gy)                        \
    SOBEL1x1_HOR(ptr, gx, gy, 0)                     \
    SOBEL1x1_HOR(ptr, gx, gy, 1)                 \
    SOBEL1x1_HOR(ptr, gx, gy, 2)             \
    SOBEL1x1_HOR(ptr, gx, gy, 3)         \
    SOBEL1x1_HOR(ptr, gx, gy, 4)     \
    SOBEL1x1_HOR(ptr, gx, gy, 5) \
    SOBEL1x1_HOR(ptr, gx, gy, 6)

/* Calculates a 7x1 vertical iteration. */
#define SOBEL7x1(ptr, g, direction)                         \
    SOBEL1x1_VERT(ptr, g, direction, 0)                     \
    SOBEL1x1_VERT(ptr, g, direction, 1)                 \
    SOBEL1x1_VERT(ptr, g, direction, 2)             \
    SOBEL1x1_VERT(ptr, g, direction, 3)         \
    SOBEL1x1_VERT(ptr, g, direction, 4)     \
    SOBEL1x1_VERT(ptr, g, direction, 5) \
    SOBEL1x1_VERT(ptr, g, direction, 6)

/** Apply a 1x7 sobel matrix to a single channel U8 input image and output two temporary channel S16 images and leave the borders undefined.
 *
 * @attention To enable computation of the X gradient -DGRAD_X must be passed at compile time, while computation of the Y gradient
 * is performed when -DGRAD_Y is used. You can use both when computation of both gradients is required.
 *
 * @param[in]  src_ptr                              Pointer to the source image. Supported data types: U8
 * @param[in]  src_stride_x                         Stride of the source image in X dimension (in bytes)
 * @param[in]  src_step_x                           src_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  src_stride_y                         Stride of the source image in Y dimension (in bytes)
 * @param[in]  src_step_y                           src_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  src_offset_first_element_in_bytes    The offset of the first element in the source image
 * @param[out] dst_gx_ptr                           Pointer to the destination image. Supported data types: S32
 * @param[in]  dst_gx_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]  dst_gx_step_x                        dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  dst_gx_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]  dst_gx_step_y                        dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  dst_gx_offset_first_element_in_bytes The offset of the first element in the destination image
 * @param[out] dst_gy_ptr                           Pointer to the destination image. Supported data types: S32
 * @param[in]  dst_gy_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]  dst_gy_step_x                        dst_gy_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  dst_gy_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]  dst_gy_step_y                        dst_gy_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  dst_gy_offset_first_element_in_bytes The offset of the first element in the destination image
 */
__kernel void sobel_separable1x7(
    IMAGE_DECLARATION(src)
#ifdef GRAD_X
    ,
    IMAGE_DECLARATION(dst_gx)
#endif /* GRAD_X */
#ifdef GRAD_Y
    ,
    IMAGE_DECLARATION(dst_gy)
#endif /* GRAD_Y */
)
{
    Image src = CONVERT_TO_IMAGE_STRUCT(src);
#ifdef GRAD_X
    Image dst_gx = CONVERT_TO_IMAGE_STRUCT(dst_gx);
#endif /* GRAD_X */
#ifdef GRAD_Y
    Image dst_gy = CONVERT_TO_IMAGE_STRUCT(dst_gy);
#endif /* GRAD_Y */
    int8 gx = (int8)0;
    int8 gy = (int8)0;

    SOBEL1x7(&src, gx, gy);

    // Store result in dst
#ifdef GRAD_X
    vstore8(gx, 0, ((__global int *)dst_gx.ptr));
#endif /* GRAD_X */
#ifdef GRAD_Y
    vstore8(gy, 0, ((__global int *)dst_gy.ptr));
#endif /* GRAD_Y */
}

/** Apply a 7x1 convolution matrix to two single channel S16 input temporary images and output two single channel S16 images and leave the borders undefined.
 *
 * @attention To enable computation of the X gradient -DGRAD_X must be passed at compile time, while computation of the Y gradient
 * is performed when -DGRAD_Y is used. You can use both when computation of both gradients is required.
 *
 * @param[in]  src_x_ptr                            Pointer to the source image. Supported data types: S32
 * @param[in]  src_x_stride_x                       Stride of the source image in X dimension (in bytes)
 * @param[in]  src_x_step_x                         src_x_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  src_x_stride_y                       Stride of the source image in Y dimension (in bytes)
 * @param[in]  src_x_step_y                         src_x_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  src_x_offset_first_element_in_bytes  The offset of the first element in the source image
 * @param[out] dst_gx_ptr                           Pointer to the destination image. Supported data types: S16
 * @param[in]  dst_gx_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]  dst_gx_step_x                        dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  dst_gx_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]  dst_gx_step_y                        dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  dst_gx_offset_first_element_in_bytes The offset of the first element in the destination image
 * @param[in]  src_y_ptr                            Pointer to the source image. Supported data types: S32
 * @param[in]  src_y_stride_x                       Stride of the source image in X dimension (in bytes)
 * @param[in]  src_y_step_x                         src_y_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  src_y_stride_y                       Stride of the source image in Y dimension (in bytes)
 * @param[in]  src_y_step_y                         src_y_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  src_y_offset_first_element_in_bytes  The offset of the first element in the source image
 * @param[out] dst_gy_ptr                           Pointer to the destination image. Supported data types: S16
 * @param[in]  dst_gy_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]  dst_gy_step_x                        dst_gy_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  dst_gy_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]  dst_gy_step_y                        dst_gy_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  dst_gy_offset_first_element_in_bytes The offset of the first element in the destination image
 * @param[in]  dummy                                Dummy parameter to easy conditional inclusion
 */
__kernel void sobel_separable7x1(
#ifdef GRAD_X
    IMAGE_DECLARATION(src_x),
    IMAGE_DECLARATION(dst_gx),
#endif /* GRAD_X */
#ifdef GRAD_Y
    IMAGE_DECLARATION(src_y),
    IMAGE_DECLARATION(dst_gy),
#endif /* GRAD_Y */
    int dummy)
{
#ifdef GRAD_X
    Image src_x  = CONVERT_TO_IMAGE_STRUCT(src_x);
    Image dst_gx = CONVERT_TO_IMAGE_STRUCT(dst_gx);
#endif /* GRAD_X */
#ifdef GRAD_Y
    Image src_y  = CONVERT_TO_IMAGE_STRUCT(src_y);
    Image dst_gy = CONVERT_TO_IMAGE_STRUCT(dst_gy);
#endif /* GRAD_Y */

    // Output pixels
#ifdef GRAD_X
    int8 gx = 0;
    SOBEL7x1(&src_x, gx, Y);
    vstore8(gx, 0, (__global int *)dst_gx.ptr);
#endif /* GRAD_X */
#ifdef GRAD_Y
    int8 gy = 0;
    SOBEL7x1(&src_y, gy, X);
    vstore8(gy, 0, (__global int *)dst_gy.ptr);
#endif /* GRAD_Y */
}

/**********************************************/
/*    End implementation of Sobel7x7 filter   */
/**********************************************/