/* 
// Copyright 2015 2016 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 initialization of x and y maps for undistortion
// using Intel 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 IPP function returned status different from ippStsNoErr */

/* Results of ippMalloc() are not validated because Intel(R) Integrated Performance Primitives 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;
}