/*******************************************************************************
* 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 computing the affine coefficients for the transform
// that rotates an image using Intel(R) Integrated Primitives (Intel(R) IPP) functions:
//     ippiGetRotateTransform
//     ippiWarpAffineLinearInit
//     ippiWarpGetBufferSize
//     ippiWarpAffineLinear_8u_C3R


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

#define WIDTH   64  /* source image width */
#define HEIGHT  64  /* source image height */
#define NUM_CHN  3

/* 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;
    IppiWarpSpec* pSpec = NULL; /* Pointer to the specification structure */
    IppiSize srcSize = { WIDTH, HEIGHT }, dstSize = { WIDTH, HEIGHT }; /* Size of source/destination images */
    int srcStep, dstStep; /* Steps, in bytes, through the source/destination images */
    Ipp8u* pSrc = NULL, *pDst = NULL;   /* Pointers to source/destination images */
    double angle = 30., xShift = 2., yShift = 1.;
    double coeffs[2][3] = {0};
    IppiPoint dstOffset = { 0, 0 }; /* Offset of the destination image ROI with respect to the destination image origin */
    int specSize = 0, initSize = 0, bufSize = 0; /* Work buffer size */
    Ipp8u* pBuffer = NULL;
    IppiBorderType   borderType = ippBorderConst;
    IppiWarpDirection direction = ippWarpForward; /* Transformation direction */
    Ipp64f pBorderValue[NUM_CHN] = {0};

    pSrc = ippiMalloc_8u_C3(srcSize.width, srcSize.height, &srcStep);
    pDst = ippiMalloc_8u_C3(dstSize.width, dstSize.height, &dstStep);

    check_sts( status = ippiGetRotateTransform(angle, xShift, yShift, (double(*)[3])coeffs) )

    /* Spec and init buffer sizes */
    check_sts( status = ippiWarpAffineGetSize(srcSize, dstSize, ipp8u, coeffs, ippLinear, direction, borderType, &specSize, &initSize) )

    pSpec = (IppiWarpSpec*)ippsMalloc_8u(specSize);

    /* Filter initialization */
    check_sts( status = ippiWarpAffineLinearInit(srcSize, dstSize, ipp8u, coeffs, direction, NUM_CHN, borderType, pBorderValue, 0, pSpec) )

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

    pBuffer = ippsMalloc_8u(bufSize);

    /* WarpAffine processing */
    check_sts( status = ippiWarpAffineLinear_8u_C3R(pSrc, srcStep, pDst, dstStep, dstOffset, dstSize, pSpec, pBuffer) )

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