/* Initialization code to talk to the X server */ #include #include #include #include # include #define BORDER_WIDTH 2 #define MAX_COLOR 10 /* Global Variables */ Display *theDisplay; int theScreen; int theDepth; unsigned long theBlackPixel; unsigned long theWhitePixel; Colormap theColormap; unsigned long pixel; GC thDestinationGC; unsigned long theplane,width,height; /////////////////////////////////// unsigned long thePixels[10]; char *theColorNames[10]= { "Black" , "Red", "Green","Blue","Yellow","Pink","Orange","Magenta","Cyan","Violet"}; ///////////////////////////////////// Window theWindow, openWindow(); GC theGC; XImage *ximage; // Establised a connection to the X server nitX() { theDisplay = XOpenDisplay( NULL); /* Check for connection */ if( theDisplay== NULL) { fprintf(stderr, "Error :- %s \n", XDisplayName(NULL)); exit(1); } //set default setting theScreen = DefaultScreen(theDisplay); theDepth = DefaultDepth(theDisplay, theScreen ); theBlackPixel = BlackPixel(theDisplay , theScreen); theWhitePixel = WhitePixel(theDisplay , theScreen); theColormap = DefaultColormap(theDisplay,theScreen); }//End of nitX Function /* getxinfo prints information */ getXInfo() { printf("%s version %d of the X window system , X %d R %d \n ", ServerVendor(theDisplay),VendorRelease(theDisplay), ProtocolVersion(theDisplay),ProtocolRevision(theDisplay)); if(theDepth == 1) { printf("color planes depth...........%d(monochrome)\n",theDepth); } else { printf("color plane depth ............%d\n",theDepth); } printf("Display width..............%d\n",DisplayWidth(theDisplay ,theScreen)); printf("Display heightwidth..............%d\n",DisplayHeight(theDisplay ,theScreen)); }/*End Of Function GetXinfo */ //quitx function to destroy the window quitX() { XCloseDisplay(theDisplay); }//End of quit function /*window function code*/ Window openWindow( int x , int y , int width , int height, int flag, GC *theNewGC) { XSetWindowAttributes theWindowAttributes; XSizeHints theSizeHints; unsigned long theWindowMask; Window theNewWindow; XWMHints theWMHints; theWindowAttributes.border_pixel = BlackPixel(theDisplay,theScreen); theWindowAttributes.background_pixel = WhitePixel(theDisplay,theScreen); theWindowAttributes.override_redirect = True; theWindowMask = CWBackPixel | CWBorderPixel | CWOverrideRedirect; theNewWindow = XCreateWindow(theDisplay, RootWindow(theDisplay,theScreen) ,x,y,width,height,BORDER_WIDTH,theDepth,InputOutput,CopyFromParent, theWindowMask,&theWindowAttributes); theWMHints.initial_state = NormalState; XSetWMHints(theDisplay ,theNewWindow ,&theWMHints); theSizeHints.flags = PPosition |PSize; theSizeHints.x = x; theSizeHints.y = y; theSizeHints.width = width; theSizeHints.height = height; XSetNormalHints(theDisplay ,theNewWindow, &theSizeHints); if(createGC (theNewWindow ,theNewGC ) == 0) { XDestroyWindow(theDisplay,theNewWindow ); return((Window) 0); } XMapWindow(theDisplay ,theNewWindow); return(theNewWindow); }//End of Window function createGC(Window theNewWindow , GC *theNewGC) { XGCValues theGCValues; *theNewGC = XCreateGC(theDisplay, theNewWindow,(unsigned long ) 0 ,&theGCValues); if(*theNewGC == 0 ) { return(0); } else { XSetForeground(theDisplay, *theNewGC ,theBlackPixel); XSetBackground(theDisplay, *theNewGC ,theWhitePixel); return(1); } }//end of creategc initDefaultColors() { XColor theRGBColor, theHardwareColor; int theStatus; int i; if(theDepth >1) { for( i=0;i< 10;i++) { theStatus = XLookupColor(theDisplay ,theColormap , theColorNames[i],&theRGBColor,&theHardwareColor); if(theStatus != 0) { theStatus = XAllocColor(theDisplay ,theColormap , &theHardwareColor); if (theStatus != 0) { thePixels[i] = theHardwareColor.pixel; } else { thePixels[i] = theBlackPixel; } // printf ("\n the color name = %s and pixel value is %d " , theColorNames[i] , thePixels[i]); } } } else { // } } /* Sets the foreground color using name of the color */ setColorWithName(GC theGC,char theName[]) { XColor theRGBColor,theHardwareColor; int theStatus; if (theDepth>1) { theStatus = XLookupColor(theDisplay,theColormap, theName,&theRGBColor,&theHardwareColor); if(theStatus != 0) { theStatus = XAllocColor(theDisplay,theColormap, &theHardwareColor); if(theStatus != 0) { XSetForeground(theDisplay,theGC, theHardwareColor.pixel); } } } } //End of setColorWithName /* This function sets the foreground color using Pixel value of the color */ setColor(GC theGC,unsigned long pixelval) { initDefaultColors(); XSetForeground(theDisplay,theGC,pixelval); } /* This function takes name of the colorname (string) and returns the corresponding pixel value */ int getColor( char strColor[]) { // initDefaultColors(); int i; for(i=0; i< 10 ;i++) { if(strcmp(strColor , theColorNames[i]) == 0) // searches for the color in the array theColoNames[] { return (thePixels[i]);//return the pixel value } } } /* Write your function here */ int main() { /* Declare all local variable here */ /* connect to the server*/ nitX(); /* Get the information abount the Display */ getXInfo(); /* U can change the width and height of the window */ theWindow = openWindow(0,0,600,600,0, &theGC); /*Initialize default colors and get the corresponding pixel values*/ initDefaultColors(); /* set the FOREGROUND color */ /* You can use setColorWithName() or setColor() function to set the foreground color */ setColorWithName(theGC,"Red"); /* call your function here */ /* Get the specified image copied to the XYPixmap(basically used for flood fill or Boundary fill algorithms */ ximage= XGetImage(theDisplay,theWindow,0,0,300,300,AllPlanes,XYPixmap); /* call your boundaryfill or floodfill function here */ sleep(2); /* Distroy the window and quit*/ XDestroyWindow(theDisplay ,theWindow); quitX(); return(0); } //end of main // command to compile // $cc filename.c -L /usr/X11R6/lib -lX11 -lm