/* 
// 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.
*/

// A simple example of downsampling of the image with
// 3x3 Gaussian kernel using Intel(R) Integrated Primitives (Intel(R) IPP) functions:
//     ippiPyramidGetSize
//     ippiPyramidInit
//     ippiPyramidLayerDownGetSize_32f_C1R
//     ippiPyramidLayerDownInit_32f_C1R
//     ippiPyramidLayerDown_32f_C1R
//     ippiPyramidLayerUpGetSize_32f_C1R
//     ippiPyramidLayerUpInit_32f_C1R
//     ippiPyramidLayerUp_32f_C1R

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

#define WIDTH  128  /* Image width  */
#define HEIGHT 128  /* Image height */
#define KERSIZE 3   /* Kernel size  */

/* 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)
{
    int i = 0;
    IppStatus   status = ippStsNoErr;
    Ipp32f*     pSrc   = NULL;                  /* Pointer to source image*/
    int         srcStep = 0;                    /* Step, in bytes, through the source image*/
    IppiSize    roiSize = { WIDTH, HEIGHT };    /* Size of source/destination ROI in pixels*/
    Ipp32f      rate    = 2.f;                  /* Neighbor levels ratio*/
    Ipp32f kernel[KERSIZE] = { 1.f, 1.f, 1.f }; /* Separable symmetric kernel of odd length*/

    IppiPyramid *dwnPyr = NULL;                 /* pointer to Gaussian pyramid structure */
    IppiPyramid *upPyr  = NULL;                 /* pointer to Laplacian pyramid structure */
    int pyrDwnBufferSize = 0, pyrDwnStructSize = 0;
    Ipp8u* pPyrDwnBuffer = NULL, *pPyrDwnStrBuffer = NULL;
    int pyrUpBufferSize = 0, pyrUpStructSize = 0;
    Ipp8u* pPyrUpBuffer = NULL, *pPyrUpStrBuffer = NULL;

    int      level = 0;
    Ipp32f  *ptr = NULL;
    int      step = 0;
    int      pyrLdwnStateSize  = 0;
    int      pyrLdwnBufferSize = 0;
    Ipp8u   *pPyrLdwnStateBuf  = NULL;
    Ipp8u   *pPyrLdwnBuffer    = NULL;
    int      pyrLupStateSize   = 0;
    Ipp8u   *pPyrLupStateBuf   = NULL;

    IppiPyramidDownState_32f_C1R **dwnState = NULL;
    IppiPyramidUpState_32f_C1R   **upState  = NULL;

    Ipp32f **dwnImage = NULL;
    Ipp32f **upImage  = NULL;

    pSrc = ippiMalloc_32f_C1(roiSize.width, roiSize.height, &srcStep);

    /*  Gaussian pyramid building */

    /* Computes the work buffers size */
    check_sts( status = ippiPyramidGetSize( &pyrDwnStructSize, &pyrDwnBufferSize, 100000, roiSize, rate) )

    pPyrDwnBuffer    = ippsMalloc_8u(pyrDwnBufferSize);
    pPyrDwnStrBuffer = ippsMalloc_8u(pyrDwnStructSize);

    /* Initializes Gaussian structure for pyramids */
    check_sts( status = ippiPyramidInit ((IppiPyramid**)&dwnPyr, 100000, roiSize, rate, pPyrDwnStrBuffer, pPyrDwnBuffer) )
    ippsFree(pPyrDwnBuffer); pPyrDwnBuffer = NULL;

    /* Computes the work buffers size */
    check_sts( status = ippiPyramidGetSize( &pyrUpStructSize, &pyrUpBufferSize, 100000, roiSize, rate) )
    pPyrUpBuffer    = ippsMalloc_8u(pyrUpBufferSize);
    pPyrUpStrBuffer = ippsMalloc_8u(pyrUpStructSize);

    /* Initializes Gaussian structure for pyramids */
    check_sts( status = ippiPyramidInit ((IppiPyramid**)&upPyr, 100000, roiSize, rate, pPyrUpStrBuffer, pPyrUpBuffer) )
    ippsFree(pPyrUpBuffer); pPyrUpBuffer = NULL;

    {
       /*  Gaussian Pyramid started */
       IppiSize *pRoi = dwnPyr->pRoi;
       int *dwnStep = dwnPyr->pStep;
       int *upStep = upPyr->pStep;
       level = dwnPyr->level;

       dwnImage = (Ipp32f**)(dwnPyr->pImage);
       upImage = (Ipp32f**)(upPyr->pImage);

       dwnState = (IppiPyramidDownState_32f_C1R**)&(dwnPyr->pState);
       upState = (IppiPyramidUpState_32f_C1R**) &(upPyr->pState);

        /* Computes the work buffers size */
       check_sts( status = ippiPyramidLayerDownGetSize_32f_C1R(roiSize, rate, KERSIZE, &pyrLdwnStateSize, &pyrLdwnBufferSize) )

       pPyrLdwnStateBuf = ippsMalloc_8u(pyrLdwnStateSize);
       pPyrLdwnBuffer   = ippsMalloc_8u(pyrLdwnBufferSize);

        /* Allocate structures to calculate pyramid layers */
       check_sts( status = ippiPyramidLayerDownInit_32f_C1R((IppiPyramidDownState_32f_C1R**)dwnState, roiSize, rate, kernel, KERSIZE, IPPI_INTER_LINEAR, pPyrLdwnStateBuf, pPyrLdwnBuffer) )
       ippsFree(pPyrLdwnBuffer);

        /* Computes the work buffers size */
       check_sts( status = ippiPyramidLayerUpGetSize_32f_C1R(roiSize, rate, KERSIZE, &pyrLupStateSize) )

       pPyrLupStateBuf = ippsMalloc_8u(pyrLdwnStateSize);

       /* Allocate structures to calculate pyramid layers */
       check_sts( status = ippiPyramidLayerUpInit_32f_C1R((IppiPyramidUpState_32f_C1R**)upState, roiSize, rate, kernel, KERSIZE, IPPI_INTER_LINEAR, pPyrLupStateBuf) )

        /* build Gaussian pyramid with level+1 layers */
       dwnImage[0] = pSrc;
       dwnStep[0] = srcStep;
       for (i=1; i<=level;i++)
       {
           dwnImage[i] = ippiMalloc_32f_C1(pRoi[i].width,pRoi[i].height,dwnStep+i);
           check_sts( status = ippiPyramidLayerDown_32f_C1R(dwnImage[i-1], dwnStep[i-1], pRoi[i-1], dwnImage[i], dwnStep[i], pRoi[i], *dwnState) )
       }

       ptr = ippiMalloc_32f_C1(roiSize.width,roiSize.height,&step);
       for (i=level-1; i>=0; i--)
       {
           upImage[i] = ippiMalloc_32f_C1(pRoi[i].width,pRoi[i].height,upStep+i);
           check_sts( status = ippiPyramidLayerUp_32f_C1R(dwnImage[i+1], dwnStep[i+1], pRoi[i+1], ptr, step, pRoi[i], *upState) )
           check_sts( status = ippiSub_32f_C1R(ptr, step, dwnImage[i], dwnStep[i], upImage[i], upStep[i], pRoi[i]) )
       }
       ippiFree(ptr); ptr = NULL;

    }

EXIT_MAIN
    /* free allocated images */
    if (dwnImage)
    {
        for (i=1; i<=level; i++)
        {
            ippiFree(dwnImage[i]);
        }
    }
    if (upImage)
    {
        for (i=1; i<=level; i++)
        {
            ippiFree(upImage[i-1]);
        }
    }

    ippiFree(ptr);

    ippsFree(pPyrDwnBuffer);
    ippsFree(pPyrUpBuffer);

    ippiFree(pPyrDwnStrBuffer);
    ippiFree(pPyrUpStrBuffer);
    ippiFree(pPyrLdwnStateBuf);
    ippiFree(pPyrLupStateBuf);

    ippiFree(pSrc);

    printf("Exit status %d (%s)\n", (int)status, ippGetStatusString(status));
    return status;
}