/*******************************************************************************
* 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 the corner finding FastN algorithm using Intel(R) Integrated Primitives (Intel(R) IPP) functions:
//     ippiFastNGetSize
//     ippiFastNInit
//     ippiFastNGetBufferSize
//     ippiFastN_8u_C1R
//     ippiFastN2DToVec_8u


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

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

Ipp8u pSrc[16 * 16] = {/* Pointer to source images */
    0,     0,   0,   0,   0,   0,   0,   0,   0,   0,  70,  70,  70,  70,  70,  70,
    0,     0,   0,   0,   0,   0,   0,   0,   0,   0,  70,  70,  70,  70,  70,  70,
    0,     0,   0,   0,   0,   0,   0,   0,   0,   0,  70,  70,  70,  70,  70,  70,
    0,     0,   0,   0,   0,   0,   0,   0,   0,   0,  70,  70,  70,  70,  70,  70,
    0,     0,   0,   0,   0,   0,   0,   0,   0,   0,  70,  70,  70,  70,  70,  70,
    0,     0,   0,   0,   0,   0,   0,   0,   0,   0,  70,  70,  70,  70,  70,  70,
    0,     0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
     10, 10,   10,  10,  10,  10, 255, 255, 255, 255, 255, 100, 100, 100, 100, 100,
     10, 10,   10,  10,  10,  10, 255, 255, 255, 255, 100, 100, 100, 100, 100, 100,
    10,  10, 10, 10, 10, 10, 255, 255, 255, 255, 100, 100, 100, 100, 100, 100,
    10,  10, 10, 10, 10, 10, 255, 255, 255, 255, 100, 100, 100, 100, 100, 100,
    10,  10, 10, 10, 10, 10, 255, 255, 255, 255, 100, 100, 100, 100, 100, 100,
    10,  10, 10, 10, 10, 10, 255, 255, 255, 255, 100, 100, 100, 100, 100, 100
};
IppiSize    srcSize = { 16 , 16 };   /* Size of source/destination ROI in pixels */
IppiSize    dstSize = { 16, 16 };
IppStatus   status = ippStsNoErr;
Ipp8u       pDstCorner[16 * 16];            /* Pointer to destination images */
IppiCornerFastN *pDstStruct = NULL;
int         srcStep = 16, dstCornerStep = 16; /* Steps, in bytes, through the source/destination images */
int         circleRadius = 3;     /* Radius for corner finding */
int         N = 9;                /*  Critical number of pixels that have different value */
int         orientationBins = 64; /* Number bins for defining direction */
int         option = IPP_FASTN_CIRCLE; /* Defines the mode of processing */
const IppDataType dataType = ipp8u;
int         nChannels = 1;
int         specSize = 0;       /* Size, in bytes, of the spec */
IppiFastNSpec *pSpec = NULL;    /* Pointer to Spec */
Ipp32f      threshold = 50.0f;  /* Level for definition of critical pixels */
int         bufSize = 0;        /* Work buffer size */
Ipp8u       *pBuffer = NULL;    /* Pointer to the work buffer */
int         maxLen = 0;         /* Length of array of structures */
IppiPoint   srcRoiOffset = { 0, 0 };   /* Offset in source image */
int         numRealCorners;   /* Number of corners */

int main(void)
{
    /* Initialization of Spec for FastN */
    check_sts( status = ippiFastNGetSize(srcSize, circleRadius, N, orientationBins, option, dataType, nChannels, &specSize) )

    pSpec = (IppiFastNSpec *)ippsMalloc_8u(specSize);

    /* Define spec size for FastN */
    check_sts( status = ippiFastNInit(srcSize, circleRadius, N, orientationBins, option, threshold, dataType, nChannels, pSpec) )

    check_sts( status = ippiFastNGetBufferSize(pSpec, dstSize, &bufSize) )

    pBuffer = ippsMalloc_8u(bufSize);

    /* Define size of working buffer for FastN */
    check_sts( status = ippiFastN_8u_C1R(pSrc, srcStep, pDstCorner, dstCornerStep, NULL, 0, &maxLen, srcRoiOffset, dstSize, pSpec, pBuffer) )

    pDstStruct = (IppiCornerFastN *)ippsMalloc_8u(sizeof(IppiCornerFastN)*maxLen);

    /* Corner finding with FastN */
    check_sts( status = ippiFastN2DToVec_8u(pDstCorner, dstCornerStep, NULL, 0, pDstStruct, dstSize, maxLen, &numRealCorners, pSpec) )

EXIT_MAIN
    ippsFree(pDstStruct);
    ippsFree(pBuffer);
    ippsFree(pSpec);
    printf("Exit status %d (%s)\n", (int)status, ippGetStatusString(status));
    return (int)status;
}