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

// Performs morphological reconstruction of pSrcDst under/above pSrc
//     ippiMorphReconstructGetBufferSize
//     ippiMorphReconstructDilate_8u_C1IR



#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) Integrated Primitives (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;
    IppiMorphAdvState *pState = NULL;
    IppiSize roiSize= { WIDTH, HEIGHT };            /* image size */
    IppiSize maskSize={3,3};                        /* mask size */
    Ipp8u *pSrc = NULL, *pDst = NULL, *pImg = NULL; /*  initial and working image */
    Ipp8u *pBuf = NULL, *pAdvBuf = NULL, pMask[9]={1,1,1,1,1,1,1,1,1};
    int srcStep = 0;                                /* srcStep, in bytes, through the source image */
    int size = 0, specSize = 0, bufferSize = 0;     /*  working buffers size */
    int cap = 100;                                  /* cap value */

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

    pDst = ippiMalloc_8u_C1(roiSize.width, roiSize.height, &srcStep);

    pImg = ippiMalloc_8u_C1(roiSize.width, roiSize.height, &srcStep);

    check_sts( status = ippiMorphReconstructGetBufferSize(roiSize, ipp8u, 1, &size) )

    pBuf = ippsMalloc_8u(size);

    check_sts( status = ippiMorphAdvGetSize_8u_C1R( roiSize, maskSize, &specSize, &bufferSize ) )

    pState = (IppiMorphAdvState*)ippsMalloc_8u(specSize);

    pAdvBuf = (Ipp8u*)ippsMalloc_8u(bufferSize);

    check_sts( status = ippiMorphAdvInit_8u_C1R( roiSize, pMask, maskSize, pState, pAdvBuf ) )

    check_sts( status = ippiCopy_8u_C1R(pSrc, srcStep, pDst, srcStep, roiSize) )
    check_sts( status = ippiCopy_8u_C1R(pSrc, srcStep, pImg, srcStep, roiSize) )
    /* subtract cap size */
    check_sts( status = ippiSubC_8u_C1IRSfs(cap, pDst, srcStep, roiSize, 0) )
    /* reconstruct image */
    check_sts( status = ippiMorphReconstructDilate_8u_C1IR(pSrc, srcStep, pDst, srcStep, roiSize, pBuf, (IppiNorm)ippiNormL1) )
    /* get caps */
    check_sts( status = ippiSub_8u_C1IRSfs(pDst, srcStep, pImg, srcStep, roiSize, 0) )
    /* caps to white */
    check_sts( status = ippiThreshold_GTVal_8u_C1IR(pImg, srcStep, roiSize, 0, 255) )
    /* delete noise */
    check_sts( status = ippiMorphOpenBorder_8u_C1R(pImg, srcStep, pDst, srcStep, roiSize, 
                                   ippBorderRepl, 0, pState, pAdvBuf) )
    /* revert to get markers */
    check_sts( status = ippiXorC_8u_C1IR(0xff, pDst, srcStep, roiSize) )

EXIT_MAIN
    ippsFree(pBuf);
    ippsFree(pState);
    ippsFree(pAdvBuf);
    ippiFree(pSrc);
    ippiFree(pDst);
    ippiFree(pImg);
    printf("Exit status %d (%s)\n", (int)status, ippGetStatusString(status));
    return status;
}