Skip to content
Snippets Groups Projects
Commit ca214595 authored by Rene's avatar Rene
Browse files

Änderungen am code sowie das hinzufügen eines rudimentären Suchalgorythmus

parent ae628458
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -8,118 +8,293 @@ static int format = ZPixmap;
const char *program_name = "unknown_program";
Bool debug = False;
Bool colorParam = False;
Bool lookForColor = False;
Bool pickColor = False;
unsigned long searchColor;
unsigned x, y;
typedef struct
{
int x, y;
int width, height;
} rectangle;
extern void throwError (char *errorMSG);
extern void throwWarning (char *warningMSG);
#define ERROR throwError
#define WARN throwWarning
extern unsigned long extractHexFromString(char *hexString);
extern unsigned long extractIntFromString (char *intString);
extern rectangle searchForColorRect (unsigned long color, XImage *image, unsigned width, unsigned height);
extern unsigned long lookingForColorOnCoordinate (XImage *image, unsigned x, unsigned y);
extern void evaluateArguments (int argc, char **argv);
int main (int argc, char **argv)
{
evaluateArguments(argc, argv);
unsigned width, height;
int screenID;
XImage *image;
Window window;
Display *display;
Screen screen;
int screenID;
XWindowAttributes win_info;
unsigned width, height;
int main (int argc, char **argv)
/* Display */
display = XOpenDisplay(NULL); //NULL Should refer to the root Window
if (display == NULL) {
ERROR("unable to open display");
}
screenID = XDefaultScreen(display);
if (debug)
{
printf("DEBUG: screen ID: %d\n", screenID);
}
window = RootWindow(display, screenID);
XGrabPointer(display, window,False,ButtonPressMask,GrabModeAsync,GrabModeAsync,None,None,CurrentTime);
XEvent ev;
Bool picked = False;
if(pickColor) {
while (!picked) {
XNextEvent(display, &ev);
printf("Select a color!\n");
if(ev.type == ButtonPress) {
x = ev.xbutton.x;
y = ev.xbutton.y;
lookForColor = True;
picked = True;
}
}
}
//display_name; //TODO
if(!XGetWindowAttributes(display, window, &win_info))
printf("Can't get target window attributes.");
width = win_info.width;
height = win_info.height;
if(debug) {
printf("DEBUG: Colormap is %ld bit\n", win_info.colormap);
printf("DEBUG: Window is %d x %d pixel\n", width,height);
}
image = XGetImage (display, window, 0,0,width,height,AllPlanes,format);
if (!image) {
fprintf (stderr, "%s: unable to get image at %dx%d+%d+%d\n",
program_name, width, height, 0, 0);
exit (1);
}
if(lookForColor) {
searchColor = lookingForColorOnCoordinate(image, x, y);
colorParam = True;
}
if(colorParam)
searchForColorRect(searchColor, image, width,height);
image->f.destroy_image(image);
XDestroyWindow(display, window);
XCloseDisplay(display);
return 0;
}
void evaluateArguments (int argc, char **argv)
#define ARG argc
#define OPTION argv[0]
#define NEXTOPT ++argv, --argf>0
{
int argf= ARG;
#define NEXTOPT ++argv, --argf>0
//printf("%d\n", ARG);
Window dummywin; //Fragwürdig
int absx, absy; //Nicht in benutzung
GC mainGC;
while (NEXTOPT)
{
if (!strcmp(OPTION, "-debug"))
{
debug = True;
printf("Running in Debug mode!\n");
continue;
}
if (!strcmp(OPTION, "-test"))
{
NEXTOPT;
if(NEXTOPT)
printf("%s\n", OPTION);
else
ERROR("-test is missing an Argument!");
continue;
}
if (!strcmp(OPTION, "-debug"))
if (!strcmp(OPTION, "-color") || !strcmp(OPTION, "-c"))
{
debug = True;
if(NEXTOPT) {
if (pickColor) {
WARN("Pick excludes -color!");
continue;
}
if(strlen(OPTION) <= 6) { //TODO more cases
searchColor = extractHexFromString(OPTION);
colorParam = True;
}
else {
ERROR("Wrong color parameter!");
}
}
else {
ERROR("-color is missing an argument!");
}
continue;
}
printf("wrong prameters!\n");
exit(0);
if(!strcmp(OPTION, "-pick") || !strcmp(OPTION,"-p")) {
pickColor = True;
if(lookForColor) {
lookForColor = False;
WARN("Pick excludes -found!");
}
if (colorParam) {
colorParam = False;
WARN("Pick excludes -color!");
}
continue;
}
/* Debug ausgaben einschalten */
if(!strcmp(OPTION, "-found") || !strcmp(OPTION,"-f")) {
if (pickColor) {
WARN("Pick excludes -found!");
NEXTOPT;
NEXTOPT;
continue;
}
if(NEXTOPT) {
x = extractIntFromString(OPTION);
if(NEXTOPT) {
y = extractIntFromString(OPTION);
lookForColor = True;
continue;
}
else {
ERROR("-found is missing an argument!");
}
}
else {
ERROR("-found is missing an argument!");
}
}
ERROR("wrong prameters!");
}
}
/* Pixels are getting printet by ff blue ff00 green ff00000 red */
rectangle searchForColorRect (unsigned long color, XImage *image, unsigned width, unsigned height) {
rectangle rect;
//int tempX, tempY; //stores the information of the column and row curently beeing evalueted
Bool foundColor = False;
for (int pixY = 0; pixY < height; pixY++)
{
for (int pixX = 0; pixX < width; pixX++)
{
if(color == image->f.get_pixel(image , pixX, pixY)) {
if(foundColor) {
if (rect.y == pixY) {
rect.width+=1;
//tempX = pixX;
}
else if (pixX == rect.x) {
rect.height+=1;
}
}
else {
rect.x = pixX;
rect.y = pixY;
rect.width = 1;
rect.height = 1;
//tempX = pixX;
//tempY = pixY;
foundColor = True;
if (debug)
{
printf("Running in Debug mode!\n");
printf("DEBUG: color found first time at x %d y %d\n",rect.x, rect.y);
}
}
}
}
/* Display */
char *display_name = 0;
display = XOpenDisplay(display_name); //NULL Should refer to the root Window
if (display == NULL) {
fprintf (stderr, "%s: unable to open display '%s'\n",
program_name, XDisplayName (display_name));
exit(1);
}
screenID = XDefaultScreen(display);
if (debug)
{
printf("DEBUG: screen ID: %d\n", screenID);
if(foundColor) {
printf("%lx found at x %d y %d \n", color, rect.x, rect.y);
printf("with a width of %d and a height of %d\n", rect.width, rect.height);
}
}
if(foundColor) {
printf("%d %d %d %d\n", rect.x, rect.y, rect.width, rect.height);
}
return rect;
}
unsigned long lookingForColorOnCoordinate (XImage *image, unsigned x, unsigned y) {
printf("%lx found at x %d y %d \n", image->f.get_pixel(image, x,y),x,y);
return image->f.get_pixel(image, x,y);
}
window = RootWindow(display, screenID);
unsigned long extractHexFromString(char *hexString) {
unsigned long result = 0;
int j = 1;
int temp;
//display_name; //TODO
if(!XGetWindowAttributes(display, window, &win_info))
printf("Can't get target window attributes.");
for (int i = strlen(hexString)-1; i >= 0; i--)
{
if(hexString[i] - '0' > 9) {
temp = hexString[i] - 'a' + 10;
} else {
temp = hexString[i] - '0';
}
result += temp * j;
j*=16;
}
width = win_info.width;
height = win_info.height;
if (debug)
{
printf("DEBUG: extractet color %lx\n", result);
}
if(debug) {
printf("DEBUG: Colormap is %ld bit\n", win_info.colormap);
printf("DEBUG: Window is %d x %d pixel\n", width,height);
return result;
}
if (!XTranslateCoordinates (display, window, RootWindow (display, screenID), 0, 0,
&absx, &absy, &dummywin)) {
fprintf (stderr,
"%s: unable to translate window coordinates (%d,%d)\n",
program_name, absx, absy);
exit (1);
} //fragwürdig
unsigned long extractIntFromString(char *intString) {
mainGC = XCreateGC(display, window, 0, 0);
unsigned long result = 0;
int j = 1;
int temp;
image = XGetImage (display, window, 0,0,width,height,AllPlanes,format);
if (!image) {
fprintf (stderr, "%s: unable to get image at %dx%d+%d+%d\n",
program_name, width, height, 0, 0);
exit (1);
for (int i = strlen(intString)-1; i >= 0; i--)
{
temp = intString[i] - '0';
result += temp * j;
j*=10;
}
if (debug)
{
printf("DEBUG: Image data to String %s\n", image->data);
printf("DEBUG: extractet coord %ld\n", result);
}
XPutImage(display, window, mainGC, image, 0, 0, 100, 100, 100, 100);
/* Pixels are getting printet by ff blue ff00 green ff00000 red */
if(debug) {
printf("The pixel in the middle has the value: %lx\n", image->f.get_pixel(image,width/2,height/2));
scanf("click to close");
return result;
}
image->f.destroy_image(image);
XDestroyWindow(display, window);
XCloseDisplay(display);
return 0;
void throwError (char *errorMSG) {
fprintf(stderr, "ERROR: %s\n" ,errorMSG);
exit(0);
}
void throwWarning (char *warnMSG) {
fprintf(stdout, "WARNING: %s\n", warnMSG);
}
\ No newline at end of file
File deleted
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment