/*******************************************************************************
* 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 perspective warping of an image
// using Intel(R) Integrated Primitives (Intel(R) IPP) functions:
//     ippiGetPerspectiveTransform
//     ippiWarpPerspectiveSize
//     ippiWarpPerspectiveNearestInit
//     ippiWarpGetBufferSize
//     ippiWarpPerspectiveNearest_32f_C1R


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

#define WIDTH   8  /* source image width */
#define HEIGHT  8  /* source 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 */

static Ipp32f pSrc[WIDTH*HEIGHT] =/* Pointers to source images */
{
    1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f,
    1.f, 1.f, 1.f, 5.f, 5.f, 1.f, 1.f, 1.f,
    1.f, 1.f, 5.f, 5.f, 5.f, 5.f, 1.f, 1.f,
    1.f, 5.f, 5.f, 5.f, 5.f, 5.f, 5.f, 1.f,
    1.f, 5.f, 5.f, 5.f, 5.f, 5.f, 5.f, 1.f,
    1.f, 1.f, 5.f, 5.f, 5.f, 5.f, 1.f, 1.f,
    1.f, 1.f, 1.f, 5.f, 5.f, 1.f, 1.f, 1.f,
    1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f };

static const double quad[4][2] = /* Vertex coordinates of the quadrangle */
{
    { 0.0, 0.0 },
    { 8.0, 0.0 },
    { 6.0, 8.0 },
    { 0.0, 8.0 } };

/* 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;
    IppiWarpSpec* pSpec  = NULL; /* Pointer to the specification structure for the forward transform */
    IppiWarpSpec* pSpecB = NULL; /* Pointer to the specification structure for the backward transform */
    IppiSize srcSize = { WIDTH, HEIGHT }; /* Size of source ROI in pixels */
    IppiRect srcRoi = { 0, 0, WIDTH, HEIGHT }; /* Region of interest in the source image */
    IppiRect dstRoi = { 0, 0, WIDTH, HEIGHT }; /* Region of interest in the destination image */
    IppiPoint dstOffset  = { 0, 0 }; /* Offset of the destination image ROI with respect to the destination image origin */
    IppiPoint dstbOffset = { 0, 0 }; /* Offset of the back destination image ROI with respect to the back destination image origin */
    int srcStep = WIDTH * sizeof(Ipp32f), dstStep = srcStep, dstbStep = srcStep; /* Steps, in bytes, through the source/destination/back destination images */
    Ipp32f pDst[WIDTH*HEIGHT], pDstB[WIDTH*HEIGHT]; /* Pointers to destination/back destination images */
    IppiSize dstSize  = {WIDTH, HEIGHT}; /* Size of the destination image */
    IppiSize dstbSize = {WIDTH, HEIGHT}; /* Size of the back destination image */
    double coeffs[3][3] = {0};
    int specSize = 0,  initSize = 0,  bufSize = 0;  /* Buffer sizes for the forward transform */
    int specSizeB = 0, initSizeB = 0, bufSizeB = 0; /* Buffer sizes for the backward transform */
    Ipp8u* pBuffer = NULL, *pBufferB = NULL; /* Work buffers */
    IppiBorderType   borderType  = ippBorderTransp;
    IppiWarpDirection direction  = ippWarpForward;  /* Forward transformation direction */
    IppiWarpDirection directionB = ippWarpBackward; /* Backward transformation direction */

    check_sts( status = ippiSet_32f_C1R(2, pDst, dstStep, dstSize) )

    check_sts( status = ippiSet_32f_C1R(3, pDstB, dstbStep, dstbSize) )

    /* Computes perspective transform matrix */
    check_sts( status = ippiGetPerspectiveTransform(srcRoi, quad, (double(*)[3])coeffs) )

    /* Spec and init buffer sizes for the forward transform*/
    check_sts( status = ippiWarpPerspectiveGetSize(srcSize, srcRoi, dstSize, ipp32f, coeffs, ippNearest, direction,  borderType, &specSize, &initSize) )

    /* Spec and init buffer sizes for the backward transform*/
    check_sts( status = ippiWarpPerspectiveGetSize(dstSize, dstRoi, dstbSize, ipp32f, coeffs, ippNearest, directionB, borderType, &specSizeB, &initSizeB) )

    pSpec  = (IppiWarpSpec*)ippsMalloc_8u(specSize);

    pSpecB = (IppiWarpSpec*)ippsMalloc_8u(specSizeB);

    /* Filter initialization for the forward transform */
    check_sts( status = ippiWarpPerspectiveNearestInit(srcSize, srcRoi, dstSize, ipp32f, coeffs, direction,  1, borderType, NULL, 0, pSpec) )

    /* Filter initialization for the backward transform */
    check_sts( status = ippiWarpPerspectiveNearestInit(dstSize, dstRoi, dstbSize, ipp32f, coeffs, directionB, 1, borderType, NULL, 0, pSpecB) )

    /* Get work buffer size for the forward transform */
    check_sts( status = ippiWarpGetBufferSize(pSpec, dstSize, &bufSize) )

    /* Get work buffer size for the backward transform */
    check_sts( status = ippiWarpGetBufferSize(pSpecB, dstSize, &bufSizeB) )

    pBuffer  = ippsMalloc_8u(bufSize);

    pBufferB = ippsMalloc_8u(bufSizeB);

    /* Warp perspective processing for the forward transform */
    check_sts( status = ippiWarpPerspectiveNearest_32f_C1R(pSrc, srcStep, pDst, dstStep, dstOffset, dstSize, pSpec, pBuffer) )

    /* Warp perspective processing for the backward transform */
    check_sts( status = ippiWarpPerspectiveNearest_32f_C1R(pDst, dstStep, pDstB, dstbStep, dstbOffset, dstbSize, pSpecB, pBufferB) )

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

/*
pDst
1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
1.0  1.0  1.0  5.0  5.0  1.0  1.0  1.0
1.0  1.0  1.0  5.0  5.0  1.0  1.0  1.0
1.0  1.0  5.0  5.0  5.0  5.0  1.0  1.0
1.0  5.0  5.0  5.0  5.0  5.0  5.0  1.0
1.0  5.0  5.0  5.0  5.0  5.0  5.0  2.0
1.0  1.0  5.0  5.0  5.0  5.0  1.0  2.0
1.0  1.0  1.0  5.0  5.0  1.0  1.0  2.0

pDstB
1.0  1.0  1.0  1.0  1.0  1.0  1.0  3.0
1.0  1.0  1.0  5.0  5.0  1.0  1.0  3.0
1.0  1.0  5.0  5.0  5.0  5.0  1.0  3.0
1.0  5.0  5.0  5.0  5.0  5.0  5.0  1.0
1.0  5.0  5.0  5.0  5.0  5.0  5.0  2.0
1.0  1.0  5.0  5.0  5.0  5.0  1.0  1.0
3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0
3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0
*/