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