/*******************************************************************************
* 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 initialization of x and y maps for undistortion
// using Intel(R) Integrated Performance Primitives (Intel(R) IPP) functions:
//     ippiUndistortGetSize
//     ippiCreateMapCameraUndistort_32f_C1R
//     ippiRemap_32f_C1R

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

#define WIDTH  640 /* Source image width */
#define HEIGHT 480 /* Destination image width */

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

int main(void)
{
    IppStatus status = ippStsNoErr;
    Ipp32f* pSrc = NULL, *pDst = NULL;    /* Pointers to source/destination images */
    int srcStep = 0, dstStep = 0;         /* Steps, in bytes, through the source/destination images */
    IppiSize roiSize = { WIDTH, HEIGHT }; /* Size of source/destination ROI in pixels */
    Ipp32f* pxMap = NULL, *pyMap = NULL;  /* Pointers to x and y maps */
    Ipp8u*  pBuffer = NULL;               /* Pointer to work buffer */
    int buflen = 0;
    int xStep = 0, yStep = 0;                   /* Steps, in bytes, through the x and y maps */
    IppiRect rect = { 0, 0, WIDTH, HEIGHT };    /* Region of interest in the source image */
    Ipp32f fx = WIDTH / 3.f, fy = HEIGHT / 2.f; /* Focal lengths */
    Ipp32f cx = WIDTH / 2.f, cy = HEIGHT / 2.f; /* Coordinates of principal point */
    Ipp32f k1 = 0.04f, k2 = 0.004f;             /* Coeffs of radial distortion */
    Ipp32f p1 = 0.02f, p2 = 0.02f;              /* Coeffs of tangential distortion */

    pSrc  = ippiMalloc_32f_C1(roiSize.width, roiSize.height, &srcStep);
    pDst  = ippiMalloc_32f_C1(roiSize.width, roiSize.height, &dstStep);
    pxMap = ippiMalloc_32f_C1(roiSize.width, roiSize.height, &xStep);
    pyMap = ippiMalloc_32f_C1(roiSize.width, roiSize.height, &yStep);

    check_sts( status = ippiSet_32f_C1R(0.1234567f, pSrc, srcStep, roiSize) )

    check_sts( status = ippiUndistortGetSize(roiSize, &buflen) )

    pBuffer = ippsMalloc_8u(buflen);

    /* Initialize x and y maps for undistortion by the ippiRemap function */
    check_sts( status = ippiCreateMapCameraUndistort_32f_C1R(pxMap, xStep, pyMap, yStep, roiSize,
        fx, fy, cx, cy, k1, k2, p1, p2, pBuffer) )

    /* Transform the source image by remapping its pixels */
    check_sts( status = ippiRemap_32f_C1R(pSrc, roiSize, srcStep, rect, pxMap, xStep, pyMap, yStep,
        pDst, dstStep, roiSize, IPPI_INTER_LINEAR) )

EXIT_MAIN
    ippiFree(pyMap);
    ippiFree(pxMap);
    ippsFree(pBuffer);
    ippiFree(pSrc);
    ippiFree(pDst);

    printf("Exit status %d (%s)\n", (int)status, ippGetStatusString(status));
    return (int)status;
}