/* 
// Copyright 2015 2018 Intel Corporation All Rights Reserved.
// 
// The source code, information and material ("Material") contained herein is
// owned by Intel Corporation or its suppliers or licensors, and title
// to such Material remains with Intel Corporation or its suppliers or
// licensors. The Material contains proprietary information of Intel
// or its suppliers and licensors. The Material is protected by worldwide
// copyright laws and treaty provisions. No part of the Material may be used,
// copied, reproduced, modified, published, uploaded, posted, transmitted,
// distributed or disclosed in any way without Intel's prior express written
// permission. No license under any patent, copyright or other intellectual
// property rights in the Material is granted to or conferred upon you,
// either expressly, by implication, inducement, estoppel or otherwise.
// Any license under such intellectual property rights must be express and
// approved by Intel in writing.
// 
// Unless otherwise agreed by Intel in writing,
// you may not remove or alter this notice or any other notice embedded in
// Materials by Intel or Intel's suppliers or licensors in any way.
*/

//   An example of performing The code example shows how these functions can be used to organize the separable convolution as a step of
// image processing pipeline.implemented with Intel(R) Integrated Primitives (Intel(R) IPP) functions :
//     ippiFilterRowBorderPipelineGetBufferSize_Low_16s_C1R
//     ippiFilterColumnPipeline_Low_16s_C1R
//     ippiFilterColumnPipelineGetBufferSize_Low_16s_C1R
//     ippiFilterRowBorderPipeline_Low_16s_C1R


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

#define WIDTH  128  /* image width  */
#define HEIGHT  64  /* 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;
    const Ipp16s **pGet = NULL;
    Ipp16s* src = NULL, *dst = NULL;
    int kernelSize = 3;
    int xAnchor = kernelSize >> 1; /* The anchor value, (0 <= xAnchor < kernelSize) */
    Ipp16s pKerX[3] = { 1, 2, 1 }, pKerY[3] = { 1, 0, 1 }; /* The pointer to the kernels */
    Ipp16s* pSrc = NULL, *pDst = NULL; /* Pointers to source/destination images */
    int srcStep = 0, dstStep = 0;      /* Steps, in bytes, through the source/destination images */
    IppiSize roiSize = { WIDTH, HEIGHT }; /* Size of source/destination ROI in pixels */
    IppiSize roi = { WIDTH, 1 }; /* Size of destination ROI in pixels */
    int divisor = 1; /* The value to divide output pixels by */
    Ipp8u borderValue = 0;
    Ipp8u *pBufRow = NULL, *pBufCol = NULL;/* Pointer to the work buffer */
    int sizeRow = 0, sizeCol = 0;    /* Common work buffer size */
    int todo = roiSize.height, bufLen;
    int mStep = (roiSize.width + 7)&(~7);
    int sStep = 0, dStep = 0;

    bufLen = mStep * 3 + 4;
    pGet = (const Ipp16s**)ippsMalloc_16s(bufLen);
    pSrc = ippiMalloc_16s_C1(roiSize.width, roiSize.height, &srcStep);
    pDst = ippiMalloc_16s_C1(roiSize.width, roiSize.height, &dstStep);

    dst = pDst; src = pSrc;
    sStep = srcStep >> 1, dStep = dstStep >> 1;
    pGet[0] = pGet[1] = (Ipp16s*)(pGet + 4);
    pGet[2] = pGet[1] + mStep;
    pGet[3] = pGet[2] + mStep;

    check_sts( status = ippiFilterRowBorderPipelineGetBufferSize_Low_16s_C1R(roiSize, 3, &sizeRow) )

    check_sts( status = ippiFilterColumnPipelineGetBufferSize_Low_16s_C1R(roiSize, 3, &sizeCol) )

    pBufRow = ippsMalloc_8u(sizeRow);
    pBufCol = ippsMalloc_8u(sizeCol);

    check_sts( status = ippiFilterRowBorderPipeline_Low_16s_C1R(pSrc, srcStep, ( Ipp16s**)pGet, roi, pKerX, kernelSize, xAnchor, ippBorderRepl, borderValue, divisor, pBufRow) )

    todo--;
    if (todo==0)
    {
        pGet[2] = pGet[0];
    }
    else {
        pGet[2] = pGet[0] + mStep; pGet[3] = pGet[2] + mStep;
        for (; todo>0;src += sStep, dst += dStep, todo--)
        {
            check_sts( status = ippiFilterRowBorderPipeline_Low_16s_C1R(src, srcStep, (Ipp16s**)(pGet + 2), roi, pKerX, kernelSize, xAnchor, ippBorderRepl, borderValue, divisor, pBufRow) )

            check_sts( status = ippiFilterColumnPipeline_Low_16s_C1R(pGet, dst, dstStep, roi, pKerY, kernelSize, divisor, pBufCol) )

            pGet[0] = pGet[1]; pGet[1] = pGet[2]; pGet[2] = pGet[3]; pGet[3] = pGet[0];
        }
    }
    check_sts( status = ippiFilterColumnPipeline_Low_16s_C1R(pGet, dst, dstStep, roi, pKerY, kernelSize, divisor, pBufCol) )

EXIT_MAIN
    ippiFree(pSrc);
    ippsFree(pBufRow);
    ippsFree(pBufCol);
    ippiFree(pDst);
    printf("Exit status %d (%s)\n", (int)status, ippGetStatusString(status));
    return (int)status;
}