VXLIB User Guide
VXLIB_box_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_box_priv.h"
41 
42 /**********************************************************************************************************************/
43 /* */
44 /* DEFINES */
45 /* */
46 /**********************************************************************************************************************/
47 
48 #define VXLIB_BOX_NUMERIC_MIN(x) std::numeric_limits<x>::min()
49 #define VXLIB_BOX_NUMERIC_MAX(x) std::numeric_limits<x>::max()
50 
51 /**********************************************************************************************************************/
52 /* */
53 /* VXLIB_box_exec_cn */
54 /* */
55 /**********************************************************************************************************************/
56 // this method computes kxk boxFilter in via natural C code
57 template <typename dTypeIn, typename dTypeOut>
58 VXLIB_STATUS VXLIB_box_exec_cn(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
59 {
60 
61  VXLIB_STATUS status = VXLIB_SUCCESS; // assign status to success by default
62 
63 #if VXLIB_DEBUGPRINT
64  printf("Enter VXLIB_box_exec_cn\n");
65 #endif
66 
67  // typecast handle (void) to struct pointer type associated to kernel
68  VXLIB_box_PrivArgs *pKerPrivArgs = (VXLIB_box_PrivArgs *) handle;
69 
70  // obtain image parameters and overflow policy
71  size_t width = pKerPrivArgs->width;
72  size_t height = pKerPrivArgs->height;
73  size_t strideInElements = pKerPrivArgs->strideInElements;
74  size_t strideOutElements = pKerPrivArgs->strideOutElements;
75 
76  size_t padLeft = pKerPrivArgs->pKerInitArgs.padLeft;
77  size_t padRight = pKerPrivArgs->pKerInitArgs.padRight;
78  size_t padTop = pKerPrivArgs->pKerInitArgs.padTop;
79  size_t padBottom = pKerPrivArgs->pKerInitArgs.padBottom;
80  bool isNotPadded = (padLeft == 0) && (padRight == 0) && (padTop == 0) && (padBottom == 0);
81 
82  size_t filterDim = pKerPrivArgs->pKerInitArgs.filterSize;
83 
84  // create local pointers
85  dTypeIn *restrict pInLocal = (dTypeIn *) pIn;
86  dTypeOut *restrict pOutLocal = (dTypeOut *) pOut;
87 
88 #if VXLIB_DEBUGPRINT
89  printf("In VXLIB_box_exec_cn, width: %d, height: %d\n", width, height);
90 #endif
91 
92  // non-padded natc implementation
93  if (isNotPadded) {
94 
95  int32_t x, y, i, j;
96  int32_t sum;
97  int32_t outWidth = (width - filterDim + 1);
98  int32_t outHeight = (height - filterDim + 1);
99  float spCoeff = 1.0 / (filterDim * filterDim);
100  int8_t Q_BIT = 15;
101  int32_t Q_FACTOR = (int32_t)(1u << (uint32_t)Q_BIT);
102  int16_t fixedCoeff = spCoeff * Q_FACTOR;
103 
104  for (y = 0; y < outHeight; y++) {
105  for (x = 0; x < outWidth; x++) {
106 
107  sum = 0;
108 
109  for (j = 0; j < filterDim; j++) {
110  for (i = 0; i < filterDim; i++) {
111 
112  sum += (pInLocal[(y + j) * strideInElements + (x + i)]) * fixedCoeff;
113  }
114  }
115 
116  pOutLocal[y * strideOutElements + x] = (dTypeOut)((uint32_t)sum >> (uint32_t)Q_BIT);
117  }
118  }
119  }
120 
121  // padded natc implementation
122  else {
123  status = VXLIB_ERR_NOT_IMPLEMENTED;
124 
125  }
126 
127  return (status);
128 }
129 
130 /**********************************************************************************************************************/
131 /* */
132 /* Explicit instantiation for the different data type versions */
133 /* */
134 /**********************************************************************************************************************/
135 
136 template VXLIB_STATUS
137 VXLIB_box_exec_cn<VXLIB_BOX_TYPENAME_I8U_O8U>(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut);
#define Q_FACTOR
template VXLIB_STATUS VXLIB_box_exec_cn< VXLIB_BOX_TYPENAME_I8U_O8U >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
VXLIB_STATUS VXLIB_box_exec_cn(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
This function is the main execution function for the natural C implementation of the kernel....
Header file for kernel's internal use. For the kernel's interface, please see VXLIB_box.
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_ERR_NOT_IMPLEMENTED
Definition: VXLIB_types.h:227
@ VXLIB_SUCCESS
Definition: VXLIB_types.h:221
int8_t filterSize
Width and height of filter
Definition: VXLIB_box.h:88
int32_t padRight
Definition: VXLIB_box.h:92
int32_t padBottom
Definition: VXLIB_box.h:94
int32_t padLeft
Padding options
Definition: VXLIB_box.h:91
Structure that is reserved for internal use by the kernel.
size_t strideOutElements
Stride of output in elements.
size_t height
Height of image
VXLIB_box_InitArgs pKerInitArgs
Initargs of the kernel.
size_t strideInElements
Stride of input in elements.
size_t width
Width of image