Pixel auslesen



  • Hallo Zusammen,

    für das was ich vorhabe, ist es Wichtig zu Wissen welche Farbe bestimmte Pixel des Bildschirms haben. Nun denn habe ich mich ein wenig in die xlib eingelesen. Und raus kam das:

    #include <stdio.h>
    #include <stdlib.h>
    #include <X11/X.h>
    #include <X11/Xutil.h>
    
    int main(int argc, char *argv[])
    {
    	Display *display;
    	int depth;
    	int screen;
    	int connection;
    	display = XOpenDisplay (NULL);
    	screen = DefaultScreen (display);
    	depth = DefaultDepth (display, screen);
    	connection = ConnectionNumber (display);
    	Window rootWindow;
    	rootWindow = RootWindow (display, screen); 
    	printf ("Display: \t\t%s\r\n", XDisplayName((char*)display));
    	printf ("Width: \t\t%d\r\n", DisplayWidth(display, screen));
    	printf ("Height: \t\t%d\r\n", DisplayHeight(display, screen));
    	printf ("Connection: \t%d\r\n", connection);
    	printf ("Color Depth: \t%d\r\n", depth);
    	unsigned int dispxmax, dispymax;
    	dispxmax = DisplayWidth(display, screen);
    	dispymax = DisplayHeight(display, screen);
    	XImage *myImage;
    	myImage = XGetImage(display, rootWindow,0,0,dispxmax ,dispymax,AllPlanes,XYPixmap);
    	unsigned long myPixel;
    	int anzahl;
    	for (anzahl = 0; anzahl < 5; anzahl++){
    		myPixel = XGetPixel(myImage, 0,0);
    		int rgb[3];
    		rgb[0] = (myPixel >> 16) & 0xff;
    		rgb[1] = (myPixel >>  8) & 0xff;
    		rgb[2] = (myPixel >>  0) & 0xff;
    		printf("%x",rgb[0]);
    		printf("%x",rgb[1]);
    		printf("%x\n",rgb[2]);
    	}
    	XDestroyImage(myImage);
    	XCloseDisplay (display);
    	return EXIT_SUCCESS;
    }
    

    Scheint soweit zu funktionieren.

    Nur das Problem an der Sache ist, die Laufzeit. Das ganz läuft je nah Auflösung 7-20 sek. Da sind wir Welten davon entfernt wie lange es brauchen darf.

    Was ich schon versucht habe auf einzelne Fenster die mich interessieren einzuschränken, das bringt aber auch nicht viel, da das meist Bilddschirmfüllen ist.

    Kann man auch Pixel direkt auslesen ohne XGetImage? Denn ich bräuchte ja nicht den ganzen Desktop, sondern nur einzelne Pixel.

    Hat jemand eine Idee?



  • hi,
    ich habe noch nie mit der Xlib gearbeitet - vielleicht verstehe ich es deswegen auch nicht ganz. warum fragst du fünf mal den gleichen Pixel ab?

    int anzahl;
    for (anzahl = 0; anzahl < 5; anzahl++){
        myPixel = XGetPixel(myImage, 0,0);
        int rgb[3];
        rgb[0] = (myPixel >> 16) & 0xff;
        rgb[1] = (myPixel >>  8) & 0xff;
        rgb[2] = (myPixel >>  0) & 0xff;
        printf("%x",rgb[0]);
        printf("%x",rgb[1]);
        printf("%x\n",rgb[2]);
    }
    

    Edit:

    habe jetzt mal dein Code kompiliert und genauer angeschaut. Bei mir funktioniert die Abfrage der Pixelfarbe ohne Probleme, wenn du dein XGetImage einfach auf ein kleineren Bereich festlegst.

    myImage = XGetImage(display, rootWindow, 0, 0, 1, 1, AllPlanes, XYPixmap);
    
    //int anzahl;
    //for (anzahl = 0; anzahl < 5; anzahl++){
        myPixel = XGetPixel(myImage, 0,0);
        int rgb[3];
        rgb[0] = (myPixel >> 16) & 0xff;
        rgb[1] = (myPixel >>  8) & 0xff;
        rgb[2] = (myPixel >>  0) & 0xff;
        printf("%x",rgb[0]);
        printf("%x",rgb[1]);
        printf("%x\n",rgb[2]);
    //}
    

    Ich bekomme die Farbe sofort ausgegeben. Die Bildschirminformationen habe ich auch noch auskommentiert.

    mfg blan



  • Ich brauch Urlaub.

    Zum einen Danke das Du einen Fehler gefunden hast. Das ist natürlich eine Überlegung nicht das ganze Bild abzurufen, sondern nur die benötigten Pixel. Wo bei das in der Summe. Worst Case das ganze Bild erfolglos durchsucht noch länger dauern dürfte.

    Danke, damit komm ich schon wieder ein Stück weiter.


Anmelden zum Antworten