LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-06-2007, 08:15 AM   #1
siepmann
LQ Newbie
 
Registered: Sep 2004
Location: Nottingham, UK
Posts: 28

Rep: Reputation: 15
Writing out an XLib window to an image?


I have very little experience in C, and none with XLib (I'm a Java user) so what I'm about to ask might not make sense, but...

I want to capture what I see in an XLib window (which I understand is a portion of the the program display) and stick it into an image file.

Is this possible?

Any help grateful received!

Many thanks,

Peter
 
Old 06-07-2007, 12:57 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
xwd does precisely what you are asking for.
--- rod.
 
Old 06-07-2007, 04:59 AM   #3
siepmann
LQ Newbie
 
Registered: Sep 2004
Location: Nottingham, UK
Posts: 28

Original Poster
Rep: Reputation: 15
Thanks very much for this. I had some trouble using xwd, though, probably because I'm trying to do the capture programmatically, not from the command line. Instead, though, I found the XGetImage function and so I've got

Code:
XImage* ximg = XGetImage(display, playground, 0, 0, 100, 100, ~0, XYPixmap);
where 'playground' is the Window I want to write out.

My next step is to try and write this out to an image file, so I tried:

Code:
XWriteBitmapFile(display, "name", (XImage)&ximg, 100, 100, -1, -1);
but this doesn't compile:

error: conversion to non-scalar type requested


Any more help grateful received.

Thanks so much!

Peter
 
Old 06-07-2007, 05:22 AM   #4
siepmann
LQ Newbie
 
Registered: Sep 2004
Location: Nottingham, UK
Posts: 28

Original Poster
Rep: Reputation: 15
Update: now discovered the XGetPixel function, so I can loop through my Ximage getting each pixel value:

Code:
int pix = XGetPixel(ximg, xp, yp);
Now I just need to know how to make an image file from an array of pixels!...
 
Old 06-07-2007, 05:32 AM   #5
siepmann
LQ Newbie
 
Registered: Sep 2004
Location: Nottingham, UK
Posts: 28

Original Poster
Rep: Reputation: 15
Problem solved. I hope this thread may be interesting to others trying to do the same thing.

Code:
printf("P3 %d %d 255\n", pwidth, pheight);

 for (xp=0; xp<pwidth; xp++) {
   for (yp=0; yp<pheight; yp++) {
     long pix = XGetPixel(ximg, xp, yp);
     printf("%ld %ld %ld\n", pix>>16, pix>>8&255, pix&255);
   }
 }
results in a nice ppm file.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
XLIB and/or KDE: Determing and Changing Window Focus GotWookiee Programming 2 06-24-2009 11:58 PM
Xlib output in root window (full screen) Narasaih Nakirikanti Programming 0 03-06-2007 05:34 AM
topmost window with xlib CarzyIvan Programming 0 03-21-2006 03:18 PM
window without a title bar using xlib CarzyIvan Programming 1 02-11-2006 04:21 PM
xlib & window titlebar?!?!?! tilo Programming 0 09-25-2004 04:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration