
//Aufgabe3b.c 

//---------------------------------------------------------------------------
#include <stdlib.h>
#include <string.h>
#include <stdio.h>


//---------------------------------------------------------------------------
// Daten
#define K_FIELD 8
int datI[K_FIELD][K_FIELD];
int datF[K_FIELD][K_FIELD];


//---------------------------------------------------------------------------
/* Funktion
   Performs the 3x3 Laplace filter. This is a "Kernel" operation. Here the brightness values of the
   neighboring pixels is substracted from the central pixel. As a kernel it can be described as below. In
   convention these weights are to be multiplied by pixels surrounding the central pixel and this sum is
   normalized by dividing by the sum of the positive weights (in this case 8).

   Consequently, in region of the image that is uniform in brightness, the result of applying this kernel
   is to reduce the grey level to zero. When a discontinuity is present within the neighborhood in the
   form of a point, line or edge, the result of the Laplacian is a non-zero value. It may be either
   positive or negative, depending on where the central point lies with respect to the edge, etc. A grey
   value of 128 is added to the result for better visibility.
   (-1 -1 -1)
   (-1 +8 -1)
   (-1 -1 -1)
 */
void laplace_filter(void)
{
int zl,sp;
int y,x;
int sum;

    for (zl=0+1; zl<K_FIELD-1; zl++)
      {
        for (sp=0+1; sp<K_FIELD-1; sp++)
          {
            sum= 0;
            for (y=-1; y<=1; y++)
              {
                for (x=-1; x<=1; x++)
                  {
                    if (!(x==0&&y==0))
                      { sum = sum + datI[zl+y][sp+x];
                      }
                  }
              }
            datF[zl][sp] = 8*datI[zl][sp] - sum;
          }
      }
}

//---------------------------------------------------------------------------
// Funktion
void laplace_setting (int code)
{
int zl,sp;

    if (code==1)
    {
    randomize();
    for (zl=0; zl<K_FIELD; zl++)
      {
        for (sp=0; sp<K_FIELD; sp++)
          {
            datI[zl][sp] = random(10);
            datF[zl][sp] = datI[zl][sp];
          }
      }
    }

    if (code==2)
    {
    printf("1. datI : \r\n");
    for (zl=0; zl<K_FIELD; zl++)
      {
        for (sp=0; sp<K_FIELD; sp++)
          {
            printf("%3d ", datI[zl][sp]);
          }
        printf("\r\n");
      }
    printf("2. datF : \r\n");
    for (zl=0; zl<K_FIELD; zl++)
      {
        for (sp=0; sp<K_FIELD; sp++)
          {
            printf("%3d ", datF[zl][sp]);
          }
        printf("\r\n");
      }
    }

}

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
int key;

    laplace_setting(1);
    printf("Filtering\r\n");
    laplace_filter();
    laplace_setting(2);

    do { key= getc(stdin);
       } while (key==0);
return 0;
}
//---------------------------------------------------------------------------
