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