/*******************************************************************************
* 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 universal image quality index for two images.
// implemented with Intel(R) Integrated Primitives (Intel(R) IPP) functions:
//     ippiImageJaehne_32f_C1R
//     ippiImageJaehne_32f_C1R
//     ippiAddRandUniform_32f_C1IR
//     ippiQualityIndexGetBufferSize
//     ippiQualityIndex_32f_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;
    /* Pointers to source images */
    Ipp32f pSrc1[256*256];
    Ipp32f pSrc2[256*256];
    /* Distance in bytes between starts of consecutive lines in the source images */
    int src1Step = 256*sizeof(Ipp32f);
    int src2Step = 256*sizeof(Ipp32f);
    Ipp32f qIndex = 0;  /* quality index value */
    IppiSize roiSize = {256,256};
    unsigned int pSeed = 3;
    int bufSize = 0;        /* work buffer size */
    Ipp8u *pBuffer = NULL;  /* pointer to the work buffer */

    /* create reference image (image1) */
    check_sts( status = ippiImageJaehne_32f_C1R( pSrc1, src1Step, roiSize ) )

    /* ... and distorted image (image2 = image1 + RandUniform) */
    check_sts( status = ippiImageJaehne_32f_C1R( pSrc2, src2Step, roiSize ) )

    check_sts( status = ippiAddRandUniform_32f_C1IR(pSrc2, src2Step, roiSize, 0, 1, &pSeed) )

    check_sts( status = ippiQualityIndexGetBufferSize( ipp32f, ippC1, roiSize, &bufSize ) )

    /* work buffer memory allocation */
    pBuffer = (Ipp8u*)ippMalloc( bufSize );

    /* computes the universal image quality index for two images pSrc1 and pSrc2 */
    check_sts( status = ippiQualityIndex_32f_C1R( pSrc1, src1Step, pSrc2, src2Step, roiSize, &qIndex, pBuffer ) )

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