/*******************************************************************************
* Copyright 2015-2018 Intel Corporation.
*
* This software and the related documents are Intel copyrighted  materials,  and
* your use of  them is  governed by the  express license  under which  they were
* provided to you (License).  Unless the License provides otherwise, you may not
* use, modify, copy, publish, distribute,  disclose or transmit this software or
* the related documents without Intel's prior written permission.
*
* This software and the related documents  are provided as  is,  with no express
* or implied  warranties,  other  than those  that are  expressly stated  in the
* License.
*******************************************************************************/

// A simple example of performing full or valid 2-D convolution of two images using a general integer rectangular kernel
// implemented with Intel(R) Integrated Performance Primitives (Intel(R) IPP) functions:
//     ippiConvGetBufferSize
//     ippiConv_16s_C1R


#include <stdio.h>
#include "ipp.h"

#define WIDTH_DST  128  /* destination image width  */
#define HEIGHT_DST  64  /* destination image height */
#define WIDTH_SRC   16  /* source image width  */
#define HEIGHT_SRC  16  /* source image height */

/* Next two defines are created to simplify code reading and understanding */
#define EXIT_MAIN exitLine:                                  /* Label for Exit */
#define check_sts(st) if((st) != ippStsNoErr) goto exitLine; /* Go to Exit if Intel(R) IPP function returned status different from ippStsNoErr */

/* Results of ippMalloc() are not validated because Intel(R) IPP functions perform bad arguments check and will return an appropriate status  */

int main(void)
{
    IppStatus status = ippStsNoErr;
    Ipp16s* pSrc1 = NULL, *pSrc2 = NULL, *pDst = NULL; /* Pointers to source/destination images */
    int srcStep1 = 0, srcStep2 = 0, dstStep = 0;       /* Steps, in bytes, through the source/destination images */
    IppiSize dstSize  = { WIDTH_DST, HEIGHT_DST };     /* Size of destination ROI in pixels */
    IppiSize src1Size = { WIDTH_SRC, HEIGHT_SRC };     /* Size of destination ROI in pixels */
    IppiSize src2Size = { WIDTH_SRC - 1, HEIGHT_SRC - 1 }; /* Size of destination ROI in pixels */
    int divisor = 2; /* The integer value by which the computed result is divided */
    Ipp8u *pBuffer = NULL;  /* Pointer to the work buffer */
    int iTmpBufSize = 0;    /* Common work buffer size */
    int numChannels = 1;
    IppEnum funCfgFull = (IppEnum)(ippAlgAuto | ippiROIFull | ippiNormNone);

    pSrc2 = ippiMalloc_16s_C1(src2Size.width, src2Size.height, &srcStep2);
    pSrc1 = ippiMalloc_16s_C1(src1Size.width, src1Size.height, &srcStep1);
    pDst  = ippiMalloc_16s_C1(dstSize.width, dstSize.height, &dstStep);

    check_sts( status = ippiConvGetBufferSize(src1Size, src2Size, ipp16s, numChannels, funCfgFull, &iTmpBufSize) )

    pBuffer = ippsMalloc_8u(iTmpBufSize);

    check_sts( status = ippiConv_16s_C1R(pSrc1, srcStep1, src1Size, pSrc2, srcStep2, src2Size, pDst, dstStep, divisor, funCfgFull, pBuffer) )

EXIT_MAIN
    ippsFree(pBuffer);
    ippiFree(pSrc1);
    ippiFree(pSrc2);
    ippiFree(pDst);
    printf("Exit status %d (%s)\n", (int)status, ippGetStatusString(status));
    return (int)status;
}