/* 
// 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 performing bilinear warping of an image
// using Intel 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 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) Integrated Performance Primitives 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;
}