VXLIB User Guide
VXLIB_convolve_cn.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  ******************************************************************************/
33 
34 /**********************************************************************************************************************/
35 /* */
36 /* INCLUDES */
37 /* */
38 /**********************************************************************************************************************/
39 
40 #include "VXLIB_convolve_priv.h"
41 #include "VXLIB_types.h"
42 
43 /**********************************************************************************************************************/
44 /* */
45 /* DEFINES */
46 /* */
47 /**********************************************************************************************************************/
48 
49 #define VXLIB_CONVOLVE_NUMERIC_MIN(x) std::numeric_limits<x>::min()
50 #define VXLIB_CONVOLVE_NUMERIC_MAX(x) std::numeric_limits<x>::max()
51 
52 /**********************************************************************************************************************/
53 /* */
54 /* VXLIB_convolve_exec_cn */
55 /* */
56 /**********************************************************************************************************************/
57 
58 // this method computes convolution of an input image with a filterHeight x filterWidth filter via natural C code
59 template <typename dTypeIn, typename dTypeFilter, typename dTypeOut>
61 VXLIB_convolve_exec_cn(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pFilter, void *restrict pOut)
62 {
63 
64  VXLIB_STATUS status = VXLIB_SUCCESS; // assign status to success by default
65 
66 #if VXLIB_DEBUGPRINT
67  printf("Enter VXLIB_convolve_exec_cn\n");
68 #endif
69 
70  // typecast handle (void) to struct pointer type associated to kernel
71  VXLIB_convolve_PrivArgs *pKerPrivArgs = (VXLIB_convolve_PrivArgs *) handle;
72 
73  // obtain image parameters and overflow policy
74  size_t width = pKerPrivArgs->width;
75  size_t height = pKerPrivArgs->height;
76  size_t strideInElements = pKerPrivArgs->strideInElements;
77  size_t strideOutElements = pKerPrivArgs->strideOutElements;
78 
79  size_t filterHeight = pKerPrivArgs->pKerInitArgs.filterHeight;
80  size_t filterWidth = pKerPrivArgs->pKerInitArgs.filterWidth;
81  size_t scale = pKerPrivArgs->pKerInitArgs.scale;
82 
83  size_t padLeft = pKerPrivArgs->pKerInitArgs.padLeft;
84  size_t padRight = pKerPrivArgs->pKerInitArgs.padRight;
85  size_t padTop = pKerPrivArgs->pKerInitArgs.padTop;
86  size_t padBottom = pKerPrivArgs->pKerInitArgs.padBottom;
87 
88  bool isNotPadded = (padLeft == 0) && (padRight == 0) && (padTop == 0) && (padBottom == 0);
89 
90  // create local pointers
91  dTypeIn *restrict pInLocal = (dTypeIn *) pIn;
92  dTypeFilter *restrict pFilterLocal = (dTypeFilter *) pFilter;
93  dTypeOut *restrict pOutLocal = (dTypeOut *) pOut;
94 
95 #if VXLIB_DEBUGPRINT
96  printf("In VXLIB_convolve_exec_cn, width: %d, height: %d\n", width, height);
97 #endif
98 
99  // non-padded natc implementation
100  if (isNotPadded) {
101 
102  int32_t outHeight = (height - filterHeight + 1);
103  int32_t outWidth = (width - filterWidth + 1);
104  int32_t i, j, m, n;
105  int32_t sum, result;
106 
107  int32_t MAX = VXLIB_CONVOLVE_NUMERIC_MAX(dTypeOut);
108  int32_t MIN = VXLIB_CONVOLVE_NUMERIC_MIN(dTypeOut);
109 
110  for (i = 0; i < outHeight; i++) {
111  for (j = 0; j < outWidth; j++) {
112 
113  sum = 0;
114 
115  for (m = 0; m < filterHeight; m++) {
116  for (n = 0; n < filterWidth; n++) {
117  sum += (pInLocal[(i + m) * strideInElements + (j + n)]) *
118  pFilterLocal[(filterHeight - 1 - m) * filterWidth + (filterWidth - 1 - n)];
119  }
120  }
121 
122  result = (int32_t)((uint64_t)sum >> (uint64_t)scale);
123 
124  // saturate the scaled result
125  result = (result > MAX) ? MAX : result;
126  result = (result < MIN) ? MIN : result;
127 
128  pOutLocal[i * strideOutElements + j] = result;
129  }
130  }
131  }
132 
133  // padded natc implementation
134  else {
135 
137  }
138 
139  return (status);
140 }
141 
142 /**********************************************************************************************************************/
143 /* */
144 /* Explicit instantiation for the different data type versions */
145 /* */
146 /**********************************************************************************************************************/
147 
149  void *restrict pIn,
150  void *restrict pFilter,
151  void *restrict pOut);
152 
154  void *restrict pIn,
155  void *restrict pFilter,
156  void *restrict pOut);
template VXLIB_STATUS VXLIB_convolve_exec_cn< VXLIB_CONVOLVE_TYPENAME_I8U_C16S_O8U >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pFilter, void *restrict pOut)
VXLIB_STATUS VXLIB_convolve_exec_cn(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pFilter, void *restrict pOut)
This function is the main execution function for the natural C implementation of the kernel....
#define VXLIB_CONVOLVE_NUMERIC_MAX(x)
template VXLIB_STATUS VXLIB_convolve_exec_cn< VXLIB_CONVOLVE_TYPENAME_I8U_C16S_O16S >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pFilter, void *restrict pOut)
#define VXLIB_CONVOLVE_NUMERIC_MIN(x)
Header file for kernel's internal use. For the kernel's interface, please see VXLIB_convolve.
void * VXLIB_kernelHandle
Handle type for VXLIB operations.
Definition: VXLIB_types.h:247
VXLIB_STATUS_NAME
The enumeration of all status codes.
Definition: VXLIB_types.h:220
@ VXLIB_SUCCESS
Definition: VXLIB_types.h:221
@ VXLIB_ERR_CONVOLVE_PADDED_NOT_IMPLEMENTED
Definition: VXLIB_types.h:238
int32_t padLeft
Padding options
int32_t filterWidth
filter columns
int32_t filterHeight
filter rows
uint32_t scale
scale factor
Structure that is reserved for internal use by the kernel.
VXLIB_convolve_InitArgs pKerInitArgs
Initargs of the kernel.
size_t width
Width of image
size_t strideInElements
Stride of input in elements.
size_t strideOutElements
Stride of output in elements.
size_t height
Height of image