/*******************************************************************************
* 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 computing HOG descriptor using Intel(R) Integrated Primitives (Intel(R) IPP) functions:
//     ippiHOGGetSize
//     ippiHOGInit
//     ippiHOGGetBufferSize
//     ippiHOGGetDescriptorSize
//     ippiHOG_8u32f_C1R


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

#define WIND_WIDTH  64  /* detection window width  */
#define WIND_HEIGHT 128 /* detection window image height */

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

static IppiHOGConfig config = { /* HOG configure structure */
    1,         /* Use OpenCV compatible output format */
    8,         /* Cell size (pixels) */
    16,        /* Block size (pixels) */
    8,         /* Block stride size (pixels) */
    9,         /* Number of bins (orientations) in the histogram */
    1.0f,       /* Sigma value (in the applied Gaussian) */
    0.2f,        /* Threshold value (applied for normalization) */
    {WIND_WIDTH, WIND_HEIGHT}, /* Size (width and height in pixels) of detection window */
};

int main(void)
{
    IppStatus status = ippStsNoErr;
    Ipp8u* pSrc = NULL;       /* Pointers to source/destination/feature images */
    int srcStep = 0;          /* Steps, in bytes, through the source/destination/feature images */
    IppiSize roiSize = { WIND_WIDTH, WIND_HEIGHT };    /* Size of source ROI in pixels */
    const Ipp8u borderValue = 0;
    IppiHOGSpec* pHOGctx = NULL;    /* Pointer to the HOG context  */
    int hogCtxSize = 0, hogBuffSize = 0, winBuffSize = 0; /* Sizes of buffers */
    Ipp8u* pBuffer = NULL;          /* Pointer to the work buffer */
    IppiPoint loc[] = { { 0, 0 }, { 2, 1 }, { 4, 2 }, { 6, 3 } }; /* Locations of detection window */
    int numLocs = sizeof(loc) / sizeof(IppiPoint);
    Ipp32f* pDescriptor = NULL; /* HOG descriptor */

    check_sts( status = ippiHOGGetSize(&config, &hogCtxSize) )  /* Get size of HOG context */

    pHOGctx = (IppiHOGSpec*)ippsMalloc_8u(hogCtxSize);
    check_sts( status = ippiHOGInit(&config, pHOGctx) ) /* Initialize HOG context */

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

    /* Compute the temporary work buffer size */
    check_sts( status = ippiHOGGetBufferSize(pHOGctx, roiSize, &hogBuffSize) )

    pBuffer = ippsMalloc_8u(hogBuffSize);

    /* Compute size of HOG descriptor */
    check_sts( status = ippiHOGGetDescriptorSize(pHOGctx, &winBuffSize) )

    pDescriptor = (Ipp32f*)ippsMalloc_8u(numLocs*winBuffSize);

    check_sts( status = ippiHOG_8u32f_C1R(pSrc, srcStep, roiSize, /* Compute HOG descriptor */
        loc, numLocs, pDescriptor, pHOGctx, ippBorderConst, borderValue, pBuffer) )

EXIT_MAIN
    ippiFree(pSrc);
    ippsFree(pHOGctx);
    ippsFree(pBuffer);
    ippsFree(pDescriptor);
    printf("Exit status %d (%s)\n", (int)status, ippGetStatusString(status));
    return (int)status;
}