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