/* 
// 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 gradient vectors computation over an image using Sobel
// using Intel(R) Integrated Primitives (Intel(R) IPP) functions:
//     ippiGradientVectorGetBufferSize
//     ippiGradientVectorSobel_8u16s_C3C1R


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

#define WIDTH   5     /* source image width */
#define HEIGHT  9     /* source image height */
#define ROI_WIDTH  3  /* source image ROI width */
#define ROI_HEIGHT 7  /* source image ROI height */
#define NUMCHN  3     /* number of channels */

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

static Ipp8u pSrc[(NUMCHN * WIDTH + 1)* HEIGHT] = /* Pointer to source image */
{
    0x62, 0xd6, 0x4b, 0x23, 0xc4, 0x66, 0x20, 0x1a, 0x15, 0x0e, 0xf1, 0xd5, 0x42, 0x4e, 0x5a, 0x00,
    0x86, 0x1e, 0xb5, 0x2d, 0x16, 0xfd, 0x30, 0xc7, 0x5e, 0x04, 0x1d, 0x36, 0x82, 0x9c, 0xb6, 0x00,
    0x97, 0xd6, 0x16, 0x57, 0x3a, 0x1e, 0x0a, 0x55, 0xa0, 0xf3, 0xc5, 0x97, 0x68, 0xe6, 0x64, 0x00,
    0xde, 0x7d, 0x1c, 0x43, 0x4d, 0x57, 0xf8, 0xec, 0xe0, 0x9c, 0x30, 0xc3, 0xb9, 0xa2, 0x8c, 0x00,
    0x58, 0xb3, 0x10, 0x27, 0xac, 0x32, 0x59, 0x82, 0xaa, 0xe5, 0x24, 0x62, 0xd2, 0x26, 0x78, 0x00,
    0x2f, 0x05, 0xd9, 0x90, 0x01, 0x70, 0xd9, 0x5a, 0xdb, 0x36, 0x80, 0xca, 0xf7, 0x2f, 0x66, 0x00,
    0xb6, 0x13, 0x6e, 0x5f, 0x72, 0x85, 0xc8, 0x97, 0x66, 0x8b, 0x93, 0x9c, 0x74, 0x4b, 0x23, 0x00,
    0x5d, 0xd3, 0x4a, 0x18, 0x43, 0x6e, 0xe9, 0x4b, 0xac, 0xdf, 0xc3, 0xa7, 0xef, 0x69, 0xe1, 0x00,
    0xdc, 0xfe, 0x20, 0x30, 0x55, 0x7a, 0x48, 0x90, 0xd7, 0x5e, 0xfd, 0x9d, 0xc5, 0x6d, 0x15, 0x00,
};

/* 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;
    int srcStep = WIDTH*NUMCHN;                   /* Steps, in bytes, through the source images */
    Ipp8u* psrc = pSrc + 1*srcStep + 1 * NUMCHN;  /* Pointer to source ROI image */
    IppiSize roiSize = { ROI_WIDTH, ROI_HEIGHT }; /* Size of source ROI in pixels */
    Ipp16s pGx[4 * 7], pGy[4 * 7]; /* Define x and y gradient components */
    int gxStep = 4 * sizeof(Ipp16s), gyStep = 4 * sizeof(Ipp16s);/* Steps through the X and Y component image */
    Ipp8u* pBuffer = NULL;                   /* Pointer to the work buffer */
    Ipp8u borderValue[3] = { 0, 0, 0 };
    int bufferSize = 0;
    Ipp16s pMag[ROI_WIDTH*ROI_HEIGHT];       /* Pointer to the magnitude of computed gradient */
    int magStep = ROI_WIDTH*sizeof(Ipp16s);  /* Step through the magnitude image */
    Ipp32f pAngle[ROI_WIDTH*ROI_HEIGHT];     /* Pointer to the angle of computed gradient */
    int angleStep = ROI_WIDTH*sizeof(Ipp32f);/* Step through the magnitude image */
    IppiMaskSize maskSize = ippMskSize3x3;
    IppiBorderType borderType = ippBorderInMem;

    check_sts( status = ippiGradientVectorGetBufferSize(roiSize, maskSize, ipp8u, NUMCHN, &bufferSize) )

    pBuffer = ippsMalloc_8u(bufferSize);

    /* Compute gradient using 3x3 Sobel operator (source ROI at point x=1, y=1) */
    check_sts( status = ippiGradientVectorSobel_8u16s_C3C1R(psrc, srcStep, pGx, gxStep, pGy, gyStep,
        pMag, magStep, pAngle, angleStep, roiSize, maskSize, ippNormL1, borderType, borderValue, pBuffer) )

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