/* 
// Copyright 2015 2018 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 the rotate operation of image.
//   Nearest neighbot interpolation is used
// implemented with Intel(R) Integrated Primitives (Intel(R) IPP) functions:
//     ippiGetRotateShift
//     ippiGetRotateTransform
//     ippiWarpAffineGetSize
//     ippiWarpAffineNearestInit
//     ippiWarpGetBufferSize
//     ippiWarpAffineNearest_8u_C1R


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

/* 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;

    Ipp8u src[4*4] = {4, 4, 4, 4,
                      3, 3, 3, 3,
                      2, 2, 2, 2,
                      1, 1, 1, 1};

    Ipp8u dst[6*6];

    IppiSize srcSize = { 4, 4 };
    IppiSize dstSize = { 6, 6 };

    IppiWarpSpec* pSpec = 0;
    double coeffs[2][3];
    int specSize = 0, initSize = 0, bufSize = 0;
    Ipp8u* pBuffer  = 0;
    IppiPoint dstOffset = {0, 0};
    double xShift;
    double yShift;
    double xShiftTmp;
    double yShiftTmp;
    double xCenterSrc = 2.0;
    double yCenterSrc = 2.0;
    double xCenterDst = 3.5;
    double yCenterDst = 2.5;
    double angle = 35.0;
 
    check_sts( status = ippiGetRotateShift ( xCenterSrc, yCenterSrc, angle, &xShift, &yShift ) )
    /* xShift = -0.79 , yShift = 1.51 */
 
    check_sts( status = ippiGetRotateShift ( 1.0, 1.0, angle, &xShiftTmp, &yShiftTmp) )
    xShift += xShiftTmp; yShift += yShiftTmp; 
    /* xShift = -1.18, yShift = 2.26 */
 
    xShift += xCenterDst - xCenterSrc;
    yShift += yCenterDst - yCenterSrc;
    /* xShift = 0.32, yShift = 2.76 */

    check_sts( status = ippiSet_8u_C1R ( 0, dst, 6, dstSize ) )

    check_sts( status = ippiGetRotateTransform (angle, xShift, yShift, coeffs) )

    /* Spec and init buffer sizes */
    check_sts( status = ippiWarpAffineGetSize(srcSize, dstSize, ipp8u, coeffs, ippNearest, ippWarpForward, ippBorderTransp, &specSize, &initSize) )

    /* Memory allocation */
    pSpec = (IppiWarpSpec*)ippsMalloc_8u(specSize);

    /* Filter initialization */
    check_sts( status = ippiWarpAffineNearestInit(srcSize, dstSize, ipp8u, coeffs, ippWarpForward, 1, ippBorderTransp, NULL, 0, pSpec) )

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

    pBuffer = ippsMalloc_8u(bufSize);

    /* Image rotation processing */
    check_sts( status = ippiWarpAffineNearest_8u_C1R(src, 4, dst, 6, dstOffset, dstSize, pSpec, pBuffer) )

    /* Result:
     *  0 0 0 0 0 0
     *  0 0 0 0 0 0
     *  0 0 4 3 0 0
     *  0 3 3 2 2 0    dst
     *  0 0 2 1 0 0
     *  0 0 1 0 0 0
     */

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