/*******************************************************************************
* 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 performing bilinear warping of an image
// using Intel(R) Integrated Primitives (Intel(R) IPP) functions:
//     ippiWarpBilinearGetBufferSize
//     ippiGetBilinearTransform
//     ippiWarpBilinear_32f_C1R
//     ippiWarpBilinearBack_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 */
{
    { 1.0, 1.0 },
    { 8.0, 0.0 },
    { 6.0, 8.0 },
    { 2.0, 6.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;
    IppiSize srcSize = { WIDTH, HEIGHT }; /* Size of source ROI in pixels */
    IppiRect srcRoi = { 0, 0, WIDTH, HEIGHT };/* Region of interest in the source image */
    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 };
    IppiRect dstbRoi = { 0, 0, WIDTH, HEIGHT }; /* Region of interest in the back destination image */
    IppiRect dstRoi  = { 0, 0, WIDTH, HEIGHT }; /* Region of interest in the destination image */
    double coeffs[2][4] = {0};
    int bufSize = 0, bufSizeB = 0; /* Work buffers size */
    Ipp8u* pBuffer = NULL,*pBufferB = NULL;

    check_sts( status = ippiSet_32f_C1R(2, pDst, srcStep, srcSize) )

    check_sts( status = ippiSet_32f_C1R(3, pDstB, srcStep, srcSize) )

    /* Computes bilinear transform matrix */
    check_sts( status = ippiGetBilinearTransform(srcRoi, quad, (double(*)[4])coeffs) )

    /* Computes the size of external buffer for bilinear transform */
    check_sts( status = ippiWarpBilinearGetBufferSize(srcSize, srcRoi, dstRoi, ippWarpForward, coeffs, IPPI_INTER_NN, &bufSize) )

    /* Computes the size of external buffer for Back bilinear transform */
    check_sts( status = ippiWarpBilinearGetBufferSize(dstSize, dstRoi, dstbRoi, ippWarpBackward, coeffs, IPPI_INTER_NN, &bufSizeB) )

    pBuffer  = ippsMalloc_8u(bufSize);
    pBufferB = ippsMalloc_8u(bufSizeB);

    /* Performs bilinear warping of an image */
    check_sts( status = ippiWarpBilinear_32f_C1R(pSrc, srcSize, srcStep, srcRoi, pDst, dstStep, dstRoi, coeffs, IPPI_INTER_NN, pBuffer) )

    /* Performs an inverse bilinear warping of an image */
    check_sts( status = ippiWarpBilinearBack_32f_C1R(pDst, dstSize, dstStep, dstRoi, pDstB, dstbStep, dstbRoi, coeffs, IPPI_INTER_NN, pBuffer) )

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