/* --------------------------------------------------*/
/*   Filtrage Moyen				     */
/*   Fichier: LibMoyen.c			     */
/*   Auteur: Patrick Bonnin			     */
/*   Date de Creation:                               */
/*   Livree : Le TI a l'IUT                          */
/* --------------------------------------------------*/
#include "Structures.h"
/* ---- Code de l'operateur Moyen ---- */

/*

Faire dabord lissage moyen, le faire en parallèle pou les 3 composantes (on fait pas 3 images différentes)

Ensuite, adapter le Kirsch fournit (Code fourni sur Drive) pour le faire sur les 3 couleurs (en même temps, pas de séparation d'images)




*/



void RGBToYUV(IMAGE *image, IMAGE* imres, POINT *point) {
    for(POINT_Y(point) = 0; POINT_Y(point) < NLIG(image);
           POINT_Y(point)++)
    for(POINT_X(point) = 0; POINT_X(point) < NCOL(image);
            POINT_X(point)++)
    {

        if((0.3 * PIXEL_R(image, point) + 0.59 * PIXEL_G(image, point) + 0.11 * PIXEL_B(image, point)) > 255)
            PIXEL_Y(imres, point) = 255;
        else if((0.3 * PIXEL_R(image, point) + 0.59 * PIXEL_G(image, point) + 0.11 * PIXEL_B(image, point)) < 0)
            PIXEL_Y(imres, point) = 0;
        else
            PIXEL_Y(imres, point) = (0.3 * PIXEL_R(image, point) + 0.59 * PIXEL_G(image, point) + 0.11 * PIXEL_B(image, point));

        if((128.0 * (0.7 * PIXEL_R(image, point) - 0.59 * PIXEL_G(image, point) - 0.11 * PIXEL_B(image, point) )/255.0 + 128.0) > 255)
            PIXEL_U(imres, point) = 255;
        else if((128.0 * (0.7 * PIXEL_R(image, point) - 0.59 * PIXEL_G(image, point) - 0.11 * PIXEL_B(image, point) )/255.0 + 128.0) < 0)
            PIXEL_U(imres, point) = 0;
        else
            PIXEL_U(imres, point) = 128.0 * (0.7 * PIXEL_R(image, point) - 0.59 * PIXEL_G(image, point) - 0.11 * PIXEL_B(image, point) )/255.0 + 128.0;

        if((128.0 * (-0.3 * PIXEL_R(image, point) - 0.59 * PIXEL_G(image, point) + 0.89 * PIXEL_B(image, point) )/255.0 + 128.0) > 255)
            PIXEL_V(imres, point) = 255;
        else if((128.0 * (-0.3 * PIXEL_R(image, point) - 0.59 * PIXEL_G(image, point) + 0.89 * PIXEL_B(image, point) )/255.0 + 128.0) < 0)
            PIXEL_V(imres, point) = 0;
        else
            PIXEL_V(imres, point) = 128.0 * (-0.3 * PIXEL_R(image, point) - 0.59 * PIXEL_G(image, point) + 0.89 * PIXEL_B(image, point) )/255.0 + 128.0;
    }
}

void YUVToRGB(IMAGE *imres, IMAGE* imres2, POINT *point) {
    for(POINT_Y(point) = 0; POINT_Y(point) < NLIG(imres);
           POINT_Y(point)++)
    for(POINT_X(point) = 0; POINT_X(point) < NCOL(imres);
            POINT_X(point)++)
    {
        if((PIXEL_Y(imres, point) + 255.0 * PIXEL_U(imres, point) / 128.0 - 255.0) > 255)
            PIXEL_R(imres2, point) = 255;
        else if((PIXEL_Y(imres, point) + 255.0 * PIXEL_U(imres, point) /128.0 - 255.0) < 0)
            PIXEL_R(imres2, point) = 0;
        else
            PIXEL_R(imres2, point) = PIXEL_Y(imres, point) + 255.0 * PIXEL_U(imres, point) /128.0 - 255.0;

        if((PIXEL_Y(imres, point) + 255.0 * PIXEL_V(imres, point) /128.0 - 255.0) > 255)
            PIXEL_B(imres2, point) = 255;
        else if((PIXEL_Y(imres, point) + 255.0 * PIXEL_V(imres, point) /128.0 - 255.0) < 0)
            PIXEL_B(imres2, point) = 0;
        else
            PIXEL_B(imres2, point) = PIXEL_Y(imres, point) + 255.0 * PIXEL_V(imres, point) /128.0 - 255.0;

        if((PIXEL_Y(imres, point) - (0.3*255.0/(0.59*128.0)) * PIXEL_U(imres, point) -
          (0.11*255.0/(0.59*128.0)) * PIXEL_V(imres, point) + 0.41*255.0/0.59) > 255)
            PIXEL_G(imres2, point) = 255;
        else if(PIXEL_Y(imres, point) - (0.3*255.0/(0.59*128.0)) * PIXEL_U(imres, point) -
          (0.11*255.0/(0.59*128.0)) * PIXEL_V(imres, point) + 0.41*255.0/0.59 < 0)
            PIXEL_G(imres2, point) = 0;
        else
            PIXEL_G(imres2, point) = PIXEL_Y(imres, point) - (0.3*255.0/(0.59*128.0)) *
              PIXEL_U(imres, point) - (0.11*255.0/(0.59*128.0)) * PIXEL_V(imres, point) + 0.41*255.0/0.59;

    }
}

void Couleur(IMAGE *image, IMAGE *imres, IMAGE *imres2)
{
  POINT	*point = NULL, *pointv=NULL; /* point courant et point voisin */
  int	i,j; /* variables indices ligne et colonne du voisinage */
  int	moy; /* variable temporaire de type short */

  if(crea_POINT(point) == NULL) {
     fprintf(stderr,"Erreur d'Allocation Memoire du Point : Moyen \n");
     exit (0);
  }

  if(crea_POINT(pointv) == NULL) {
     fprintf(stderr,"Erreur d'Allocation Memoire du Point Voisin : Moyen \n");
     exit (0);
  }

  RGBToYUV(image, imres, point);

  YUVToRGB(imres, imres2, point);


  free((void *)pointv);/* --- liberation des zones memoire */
  free((void *)point);
} /* --- fin de l'operateur --- */
