ok here are the files

globals.h

#ifndef _globals 

#define _globals 



#define DATA( L ) ( ( L ) -> datapointer ) 

#define NEXT( L ) ( ( L ) -> next ) 


typedef enum { OK, ERROR } status ; 
typedef enum { FALSE = 0 , TRUE=1 } bool ;  
typedef void *generic_ptr ; 

#endif

stack_interface.h

#ifndef _stackinterface_h
#define _stackinterface_h

#include "stack.h"

typedef struct { int x;  int y ; } point ;

extern status push_xy( stack *p_S, int x, int y )  ;

extern status pop_xy( stack *p_S, int *x, int *y ) ;

#endif

stack.h

#include "globals.h"

#ifndef _stack
#define _stack

#define STACKINCREMENT 100

#define current_stacksize(p_S) ((p_S)->top - (p_S)->base) 

typedef struct{ generic_ptr *base; generic_ptr *top; int stacksize ; } stack;

extern status init_stack( stack *p_S ) ;

extern bool empty_stack( stack *p_S ); 

extern status push( stack *p_S , generic_ptr data ) ;

extern status pop( stack *p_S , generic_ptr *p_data) ; 

extern status top( stack *p_S , generic_ptr *p_data ) ;

#endif

stack.c

#include <stdlib.h> 
#include "globals.h"
#include "stack.h"



status init_stack(stack *p_S){

  p_S->base = (generic_ptr *)malloc(STACKINCREMENT*sizeof(generic_ptr)) ; 
  if(p_S->base == NULL) 
    return ERROR ; 

  p_S->top = p_S->base ;
  p_S->stacksize = STACKINCREMENT ;
  return OK ; 
}


bool empty_stack(stack *p_S){ 

  return (p_S->top == p_S->base) ? TRUE : FALSE ; 

}



status push(stack *p_S , generic_ptr data){ 

  if(current_stacksize(p_S) == p_S->stacksize){
    generic_ptr *newstack = (generic_ptr *) realloc(p_S->base,(p_S->stacksize+STACKINCREMENT)*sizeof(generic_ptr *)) ; 
    if(newstack == NULL) 
      return ERROR ;  
    p_S->base  = newstack ; 
    p_S->top = p_S->base + p_S->stacksize ;  
    p_S->stacksize += STACKINCREMENT ; 
  }

  *p_S->top = data ; 
  p_S->top++ ; 
  return OK ; 
}


status pop(stack *p_S , generic_ptr *p_data){ 

  if(empty_stack(p_S) == TRUE) 
    return ERROR ; 

  p_S->top-- ; 
  *p_data = *p_S->top ; 
  return OK ;  
} 


status top(stack *p_S,generic_ptr *p_data){

  if(pop(p_S,p_data) == ERROR ) 

    return ERROR ;  

  return push(p_S,*p_data) ; 

}

stack_interface.c

#include "globals.h"
#include "stack.h"
#include <stdlib.h>

typedef struct { int x , y ; } point ; 

status push_xy(stack *p_S , int x,int  y ){ 

  point *pt = (point *) malloc(sizeof(point)) ;  

  if (pt == NULL) 
    return ERROR ; 

  pt->x = x ; 
  pt->y = y ; 
  if(push(p_S,( generic_ptr ) pt ) == ERROR) { 
    free(pt) ; 
    return ERROR ; 

  }
  return OK ; 
} 

status pop_xy(stack *p_S,int *p_x , int *p_y ){ 

  point *pt ; 

  if(pop(p_S, (generic_ptr *) &pt) == ERROR ) 
    return ERROR ;  

  *p_x = pt->x ; 
  *p_y = pt->y ; 
  free(pt) ; 
  return OK ; 

}

Main.c

#define SCREENMAX 100
static int screen[SCREENMAX][SCREENMAX] ; 
static int xmax , ymax ;

#define PUSH_XY(p_S , x , y) \
		if (push_xy(p_S , x , y ) == ERROR){ \
			printf(PUSH_ERR) ; \
			continue ; \
		}


#define INI_ERR "Error initializing the stack. Region not filled.\n"
#define PUSH_ERR "Error adding to stack . Region may not be totally filled.\n"
#define SCREENMAX 100 
#include<stdlib.h>
#include <stdio.h>
#include "stack.h"
#include "globals.h"
#include "stack_interface.h"


void fill(int x , int y , int old_color , int new_color ) ; 
int init_image(char *filename) ; 
int write_pixel(int x , int y , int color ) ; 
int read_pixel(int x , int y ) ; 
void display_image() ; 
status push_xy(stack *p_S , int x , int y ) ; 
status pop_xy(stack *p_S , int *p_x , int *p_y) ; 

int main( int argc, char *argv[] ) { 

  char filename[BUFSIZ] ; 
  int x , y , old_color , new_color ; 

  printf("Enter image file name:  ") ; 
  scanf("%s", filename ) ; 
  if(init_image(filename) == -1 ) { 

    printf("Error initializing the image.\n") ; 
    exit(1) ; 
  }

  while( TRUE ) { 

    display_image() ; 
    printf("Enter the point at which the fill should start (x, y): " ) ; 
    scanf("%d, %d" , &x, &y) ; 

    if( x == -1 && y == -1 )  break ;
 
    old_color = read_pixel(x, y) ; 

    do{ 

      printf("Pixel color is %d. Enter the new color : " , old_color)  ; 
      scanf("%d" , &new_color) ; 

    }while(old_color== new_color || new_color < 0 || new_color > 9 ) ; 

    fill(x,y,old_color , new_color ) ; 
  } 

  printf("All done.\n") ; 

  return 0 ;

}



 
int init_image(char *filename){ 
      
	
	FILE *fd = fopen(filename , "r" ) ;

	char linebuffer[BUFSIZ] ;

	int row , col ; 

	if(fd == NULL)

	return -1 ;

fgets(linebuffer, BUFSIZ, fd) ;
sscanf(linebuffer , "%d %d" , &xmax , &ymax) ;

if(xmax < 0 || ymax < 0 ) {


fprintf(stderr , "Invalid image dimensions (%d,%d).\n" , xmax , ymax ) ; 
fclose(fd);



return -1 ; 

}

for( row = 0 ; row < xmax ; row++) {  
if(fgets(linebuffer , BUFSIZ , fd ) == NULL){

fprintf(stderr , " Missing %d rows in data file\n" , xmax - row ) ; 

break ; 
} 

 col = 0 ;

while(col < ymax) { 

if(linebuffer[col] == '\n' ) {

	fprintf(stderr , "Missing %d columns on line %d.\n",ymax - col,row) ;  


	for(;col < ymax ; screen[row][col++] = 0 ) ; 
	} else { 

	screen[row][col] = linebuffer[col] - '0' ; 

	col++ ; 
	}
   } 
} 

 xmax = row ; 
fclose(fd) ; 
return 0 ; 

}


int read_pixel(int x ,int y){ 

  if(x < 0 || x >= xmax || y < 0 || y >= ymax ) 
	return -1 ; 

	return screen[x][y] ; 
} 

int write_pixel(int x ,int y ,int color ){ 

	if(x < 0 || x >= xmax || y < 0 || y >= ymax ) 
	return -1 ; 

	return screen[x][y] = color ; 

} 

void display_image(){ 

int row , col ; 

printf("Dimensions : %d x %d\n" , xmax,ymax ) ; 

for( row = 0 ; row < xmax ; row++ ){ 
	printf("%3d" , row ) ; 
for(col = 0 ; col < ymax ; col++ ) 
	printf("%1d" , screen[row][col]) ; 
putchar('\n') ; 
} 
putchar('\n') ; 

}


#define INI_ERR "Error initializing the stack. Region not filled.\n"


			


 void fill(int x , int y , int old_color , int new_color ){

   stack S ; 

   if(init_stack(&S) == ERROR ){ 
     printf(INI_ERR) ; 
     return ; 

   } 

   push_xy(&S, x , y ) ; 
   while(! empty_stack(&S)) { 

     pop_xy(&S , &x , &y ) ; 
     if(read_pixel(x,y) == old_color ) { 

       write_pixel(x , y , new_color) ; 
       PUSH_XY(&S , x , y - 1 ); 
	 PUSH_XY(&S , x , y + 1 ) ; 
	 PUSH_XY(&S , x - 1 , y ) ;
	 PUSH_XY(&S , x+ 1 , y ) ; 
     } 
   }
 }

compiled stack_interface.c main.c and stack.c using the -Wall -pedantic -ansi -c options to get the object files

then compiled the program using = gcc -o graf –Wall –pedantic –ansi main.o stack_interface.o stack.o

it compiles and that is all well and good but when i run the program and put in the image file name it doesnt seem to be reading it correctly the image file I used is 5 by 5 and it says the dimensions are 0 x 0 which is obviously wrong and then when i try to give a coordinate say 2,2 it says the pixel in that area is -1


help!!

no replies? plz help me

This is THE most complicated, and most verbose flood fill function, (because that's all it should be - one function), I've ever seen.

You've made a serious error in how you program:

1) You've written a lot of code, but obviously didn't do any testing along the way.

2) So the bugs are likely to be anywhere.

That's a very poor methodology. What you should have done is test each function as you went along, that it was accurate and kept the flow of the program, going as is needed.

The first thing I'd do if it were me, would be to goto Wikipedia and read up on "Flood Fill" algorithms. Then I'd choose one of those that I liked, and code it up. Check your compiler, also. It may have an extension expressly for this purpose. Note that flood fill may mean filling in just one color, or one area, and not the entire picture.

I don't want to sound negative, because the work that went into this is substantial. I don't understand WHY someone would do it this way.

Beyond accuracy and run time efficiency, one of the major goals of your programs is clarity. There WILL come a time that you'll want to adapt part of this code, for use in another program, or to extend what this program already does. See what I mean? Even if you get it to work fine, it's real value is greatly diminished.

If you can't get user or file input to work, (as you state), this is CLEARLY not code you should be trying to work with.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.