/* Flood-Fill 8 Algorithm, Chapter 3  */
/* Program by                         */
/* Student   Mr.Jakapun  chotmanotham */
/* ID.number 39-2075-011-8            */
/* Class.    TCT-3TA                  */

/* Declare Header File ,Help Function */
#include "graphics.h"
#include "stdio.h"
#include "stdlib.h"
#include "bios.h"

/*  Initializes the graphics system */
void init_graphics(void)
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   /* initialize graphics mode */
   initgraph(&gdriver, &gmode, "..\\bgi");
   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk) { /* an error occurred */
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);             /* return with error code */
   }
}
/* Initializes Table Palette Color */
void palette(void) {
    int color, x, y;
    line(58,  49, 550, 49);     /*  Create BOX    | Line Top    */
    line(58,  73, 550, 73);     /*  around Table  | Line Bottom */
    line(58,  49,  58, 73);     /*  Palette Color | Line Left   */
    line(550, 49, 550, 73);     /*                | Line Right  */
    randomize();                /* initializes random number Color */
    y = 51;
       for(x=59; x<=515; x+=35) {   /* Count X Scale */
	  color= random (16);       /* Random value of Color 0-15 */
	     setcolor(color);       /* Create Box Color | Define color     */
	     setfillstyle(1,color); /* in Table Palette | Define Style Bar */
	     bar(x+1,y,x+34,y+20);    /* Color            | Build Box Color  */
       }
}
/* Algorithm Flood-Fill 8 */
/* Function Flood-Fill 8  */
/* Input Point X,Y, User defined color, Old Color at Point X,Y */
void floodFill8 (int x, int y, int fillColor, int oldColor)
{
 if (getpixel(x,y) == oldColor) { /* Check Color at Point X,Y == Old Color */
    setcolor (fillColor);         /* Set Color User defined color */
    putpixel(x,y,fillColor);      /* Plot pixel at first point    */
    /* Function recersive */
    floodFill8 (x+1, y, fillColor, oldColor);   /* Fill Point Left         */
    floodFill8 (x-1, y, fillColor, oldColor);   /* Fill Point Right        */
    floodFill8 (x, y+1, fillColor, oldColor);   /* Fill Point Bottom       */
    floodFill8 (x, y-1, fillColor, oldColor);   /* Fill Point Top          */
    floodFill8 (x+1, y-1, fillColor, oldColor); /* Fill Point Left Top     */
    floodFill8 (x-1, y-1, fillColor, oldColor); /* Fill Point Right Top    */
    floodFill8 (x+1, y+1, fillColor, oldColor); /* Fill Point Left Bottom  */
    floodFill8 (x-1, y+1, fillColor, oldColor); /* Fill Point Right Bottom */
  }
}
/* Main Program */             /* x,y = Point X,Y to fill color       */
void main(void) {              /* num_color = Value color of fill     */
   int x,y,num_color,color1;   /* color1 = Old color at x,y translate */
   char ch; 		       /*          to Function Flood-Fill     */
   init_graphics();            /* Call Initializes the graphics system */
   palette();                  /* Call Initializes Table Palette Color */
   /* Input Value of variable x,y,num_color and set value of color1 */
   setcolor(15);
   outtextxy(192,355,"Input Point X (60-540) = "); /* Show Input point x */
   outtextxy(192,372,"Input Point Y (50-70)  = "); /* Show Input point y */
   outtextxy(192,388,"Fill Color (0-15)      = "); /* Show Input value color */
   setcolor(LIGHTCYAN);   /* Show value of color */
   color1 = 0;
   do{
   setcolor(BLACK);
   setfillstyle(1,BLACK);
   bar(180,338,600,350);
   setcolor(LIGHTRED);               /* Tell Press value variable and next */
   outtextxy(180,338,"Press [value] and Press [ENTER]"); /* Press key ENTER */
   gotoxy(50,23);  printf("    ");
   gotoxy(50,24);  printf("    ");
   gotoxy(50,25);  printf("    ");
   do {                   /* Loop check key and input value of variable */
   color1++;
   if (color1 == 2) { printf("\a\a"); /* If wrong of range to "BEEP,BEEP" */
   gotoxy(50,23);  printf("    "); }
   color1 = 1;
   gotoxy(50,23); scanf("%d",&x);    /* Input value of x */
   } while((x<60) || (x>540));      /* range x = 60 between 540 */
   color1 = 0;
   do {
   color1++;
   if (color1 == 2) { printf("\a\a"); /* If wrong of range to "BEEP,BEEP" */
   gotoxy(50,24);  printf("    "); }f
   color1 = 1;
   gotoxy(50,24); scanf("%d",&y);    /* Input value of y */
   } while((y<50) || (y>70));	     /* range y = 50 between 370 */
   color1 = 0;
   do {
   color1++;
   if (color1 == 2) { printf("\a\a"); /* If wrong of range to "BEEP,BEEP" */
   gotoxy(50,25);  printf("    "); }
   color1 = 1;
   gotoxy(50,25); scanf("%d",&num_color);    /* Input value of num_color */
   } while((num_color<0) || (num_color>15)); /* range num_color = 0 between 15 */
   color1 = 0;
   color1=getpixel(x,y);             /* Old color at x,y send to color1*/
   floodFill8(x,y,num_color,color1); /* Call Function Flood-Fill 8 */
   setcolor(BLACK);
   setfillstyle(1,BLACK);
   bar(180,338,600,350);
   setcolor(YELLOW);                 /* Tell Press key [ESC] */
   outtextxy(180,338,"  Press [ESC] to EXIT Program  "); /* to exit program */
   ch = bioskey(0);           /* Input value of keyboard */
   } while (ch != 27);        /* if equal key ESC or 0x11b to exit program */
   closegraph();              /* Shuts down the graphics system */
}