/*******************************************************************************
* 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 image foreground/background segmentation
// using Intel(R) Integrated Primitives (Intel(R) IPP) functions:
//     ippiFGMMGetBufferSize_8u_C3R
//     ippiFGMMInit_8u_C3R
//     ippiFGMMForeground_8u_C3R
//     ippiFGMMBackground_8u_C3R


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

#define WIDTH  32      /* image width  */
#define HEIGHT 32      /* image height */
#define NUM_FRAME 100
#define MAX_N_RECT 3   /* number of rectangles */

static struct {
    int x;
    int y;
    int w;
    int h;
    int dx;
    int dy;
    Ipp8u color[3];
} rect[MAX_N_RECT];  /* Parameters of rectangle */

/* 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)
{
    int r = 0, frame = 0;
    IppStatus status = ippStsNoErr;
    Ipp8u* pSrc = NULL, *pDstF = NULL, *pDstB = NULL; /* Pointers to source/foreground/background images */
    int srcStep = 0, dstFStep = 0, dstBStep = 0;      /* Steps, in bytes, through the source/foreground/background images */
    IppiSize roiSize = { WIDTH, HEIGHT }; /* Size of source ROI in pixels */
    int specSize = 0;                     /* Size of the working buffer and spec structure */
    IppFGMMState_8u_C3R*  pState = NULL;  /* Pointer to the state */
    IppFGMModel           pModel = {0};   /* Pointer to the parameters structure */
    Ipp8u value[3] = {0,0,0};
    int maxNGauss = 5; /* Maximum number of allowed Gaussians */

    pSrc  = ippiMalloc_8u_C3(roiSize.width, roiSize.height, &srcStep);
    pDstF = ippiMalloc_8u_C1(roiSize.width, roiSize.height, &dstFStep);
    pDstB = ippiMalloc_8u_C3(roiSize.width, roiSize.height, &dstBStep);

    check_sts( status = ippiFGMMGetBufferSize_8u_C3R(roiSize, maxNGauss, &specSize) )

    pState = (IppFGMMState_8u_C3R*)ippsMalloc_8u(specSize);

    pModel.numFrames = 50;
    pModel.varInit = 15.0;
    pModel.varMin = 4.0;
    pModel.varMax = 75.0;
    pModel.varWBRatio = 4.0f*4.0f;
    pModel.bckgThr = 0.9f;
    pModel.varNGRatio = 3.0f*3.0f;
    pModel.reduction = 0.05f;
    pModel.shadowValue = 127;
    pModel.shadowFlag = 1;
    pModel.shadowRatio = 0.5;
    pModel.maxNGauss = maxNGauss;

    check_sts( status = ippiFGMMInit_8u_C3R(roiSize, maxNGauss, &pModel, pState) )

    for (r = 0; r<MAX_N_RECT; r++)
    {
        rect[r].x = 0;
        rect[r].y = 0;
        rect[r].w = 10 + r;
        rect[r].h = 5 + r;
        rect[r].dx = 1;
        rect[r].dy = 1;
        rect[r].color[0] = (Ipp8u)(10 * r);
        rect[r].color[1] = (Ipp8u)(20 * r);
        rect[r].color[2] = (Ipp8u)( 3 * r);
    }
    for (frame = 0; frame < NUM_FRAME; frame++)
    {
        Ipp8u valueRect[3] = { 0, 0, 0 };
        IppiSize roiRectSize = {0};
        /* Draw rectangle to simulate moving object */
        check_sts( status = ippiSet_8u_C3R(value, pSrc, srcStep, roiSize) ) /* zero image */

        for (r = 0; r<MAX_N_RECT; r++)
        {
            valueRect[0] = (Ipp8u)(rect[r].color[0] + frame);
            valueRect[1] = (Ipp8u)(rect[r].color[1] + frame);
            valueRect[2] = (Ipp8u)(rect[r].color[2] + frame);
            roiRectSize.width  = rect[r].w;
            roiRectSize.height = rect[r].h;
            check_sts( status = ippiSet_8u_C3R(valueRect, pSrc + srcStep*rect[r].y + 3 * rect[r].x, srcStep, roiRectSize) )
            if (status<0) break;

            if (rect[r].x + rect[r].w >= roiSize.width){
                rect[r].dx = -1;
            }
            else if (rect[r].x <= 0){
                rect[r].dx = 1;
            }
            if (rect[r].y + rect[r].h >= roiSize.height){
                rect[r].dy = -1;
            }
            else if (rect[r].y == 0){
                rect[r].dy = 1;
            }
            rect[r].x += rect[r].dx;
            rect[r].y += rect[r].dy;
        }
        check_sts( status = ippiFGMMForeground_8u_C3R(pSrc, srcStep, pDstF, dstFStep, roiSize, pState, &pModel, -1) )

        check_sts( status = ippiFGMMBackground_8u_C3R(pDstB, dstBStep, roiSize, pState) )
    }

EXIT_MAIN
    ippiFree(pSrc);
    ippiFree(pDstF);
    ippiFree(pDstB);
    printf("Exit status %d (%s)\n", (int)status, ippGetStatusString(status));
    return (int)status;
}