我试图用XGetImage捕获图像。一切都很好,但我需要将数据发送到一个需要一组RGB四边形的模块。为图像中的每个像素调用XGetPixel非常慢(对于1440x900分辨率,在i5上为0.5秒)。我在xlib中查找了XGetPixel源代码,原因很明显,每个像素都进行了大量的计算。 是否有任何有效(或可能完全不同)的方式?
答案 0 :(得分:1)
使用MIT共享内存扩展,您可以将图像存储在共享内存区域中。这样,您可以避免在处理图像时通过Xlib IPC通道。 您可以分别使用XShmGetImage和XShmPutImage获取或设置drawable的内容。 你不能调整共享内存区域的大小,你必须销毁它并创建一个新区域。
下一个实用程序获取屏幕截图,将所有像素的Alpha通道设置为0xFF并将其保存到指定文件。 它假设像素是32位BGRA,你应该处理所有打包的像素格式。
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <stdbool.h>
#include <png.h>
#include <sys/shm.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/XShm.h>
#define NAME "screenshot"
#define BPP 4
struct shmimage
{
XShmSegmentInfo shminfo ;
XImage * ximage ;
unsigned int * data ; // will point to the image's BGRA packed pixels
} ;
void initimage( struct shmimage * image )
{
image->ximage = NULL ;
image->shminfo.shmaddr = (char *) -1 ;
}
void destroyimage( Display * dsp, struct shmimage * image )
{
if( image->ximage )
{
XShmDetach( dsp, &image->shminfo ) ;
XDestroyImage( image->ximage ) ;
image->ximage = NULL ;
}
if( image->shminfo.shmaddr != ( char * ) -1 )
{
shmdt( image->shminfo.shmaddr ) ;
image->shminfo.shmaddr = ( char * ) -1 ;
}
}
int createimage( Display * dsp, struct shmimage * image, int width, int height )
{
// Create a shared memory area
image->shminfo.shmid = shmget( IPC_PRIVATE, width * height * BPP, IPC_CREAT | 0600 ) ;
if( image->shminfo.shmid == -1 )
{
perror( NAME ) ;
return false ;
}
// Map the shared memory segment into the address space of this process
image->shminfo.shmaddr = (char *) shmat( image->shminfo.shmid, 0, 0 ) ;
if( image->shminfo.shmaddr == (char *) -1 )
{
perror( NAME ) ;
return false ;
}
image->data = (unsigned int*) image->shminfo.shmaddr ;
image->shminfo.readOnly = false ;
// Mark the shared memory segment for removal
// It will be removed even if this program crashes
shmctl( image->shminfo.shmid, IPC_RMID, 0 ) ;
// Allocate the memory needed for the XImage structure
image->ximage = XShmCreateImage( dsp, XDefaultVisual( dsp, XDefaultScreen( dsp ) ),
DefaultDepth( dsp, XDefaultScreen( dsp ) ), ZPixmap, 0,
&image->shminfo, 0, 0 ) ;
if( !image->ximage )
{
destroyimage( dsp, image ) ;
printf( NAME ": could not allocate the XImage structure\n" ) ;
return false ;
}
image->ximage->data = (char *)image->data ;
image->ximage->width = width ;
image->ximage->height = height ;
// Ask the X server to attach the shared memory segment and sync
XShmAttach( dsp, &image->shminfo ) ;
XSync( dsp, false ) ;
return true ;
}
void getrootwindow( Display * dsp, struct shmimage * image )
{
XShmGetImage( dsp, XDefaultRootWindow( dsp ), image->ximage, 0, 0, AllPlanes ) ;
// This is how you access the image's BGRA packed pixels
// Lets set the alpha channel of each pixel to 0xff
int x, y ;
unsigned int * p = image->data ;
for( y = 0 ; y < image->ximage->height; ++y )
{
for( x = 0 ; x < image->ximage->width; ++x )
{
*p++ |= 0xff000000 ;
}
}
}
void initpngimage( png_image * pi, struct shmimage * image )
{
bzero( pi, sizeof( png_image ) ) ;
pi->version = PNG_IMAGE_VERSION ;
pi->width = image->ximage->width ;
pi->height = image->ximage->height ;
pi->format = PNG_FORMAT_BGRA ;
}
int savepng( struct shmimage * image, char * path )
{
FILE * f = fopen( path, "w" ) ;
if( !f )
{
perror( NAME ) ;
return false ;
}
png_image pi ;
initpngimage( &pi, image ) ;
unsigned int scanline = pi.width * BPP ;
if( !png_image_write_to_stdio( &pi, f, 0, image->data, scanline, NULL) )
{
fclose( f ) ;
printf( NAME ": could not save the png image\n" ) ;
return false ;
}
fclose( f ) ;
return true ;
}
int main( int argc, char * argv[] )
{
if( argc != 2 )
{
printf( "Usage:\n" ) ;
printf( " " NAME " file\n\n" ) ;
return 0 ;
}
Display * dsp = XOpenDisplay( NULL ) ;
if( !dsp )
{
printf( NAME ": could not open a connection to the X server\n" ) ;
return 1 ;
}
if( !XShmQueryExtension( dsp ) )
{
XCloseDisplay( dsp ) ;
printf( NAME ": the X server does not support the XSHM extension\n" ) ;
return 1 ;
}
int screen = XDefaultScreen( dsp ) ;
struct shmimage image ;
initimage( &image ) ;
if( !createimage( dsp, &image, XDisplayWidth( dsp, screen ), XDisplayHeight( dsp, screen ) ) )
{
XCloseDisplay( dsp ) ;
return 1 ;
}
getrootwindow( dsp, &image ) ;
if( !savepng( &image, argv[1] ) )
{
destroyimage( dsp, &image ) ;
XCloseDisplay( dsp ) ;
return 1 ;
}
destroyimage( dsp, &image ) ;
XCloseDisplay( dsp ) ;
return 0 ;
}
您可以这样编译:
gcc screenshot.c -o screenshot -std=c99 -I/usr/X11R6/include -I/usr/local/include -L/usr/X11R6/lib -L/usr/local/lib -lX11 -lXext -lpng
您可以捕获并保存这样的屏幕截图:
screenshot screenshot.png
如果您不想处理png库并且您对截屏不感兴趣,请删除initpngimage和savepng函数,以及以下几行:
#include <strings.h>
#include <png.h>
if( savepng( &image, argv[1] ) == FALSE )
{
destroyimage( dsp, &image ) ;
XCloseDisplay( dsp ) ;
return FALSE ;
}
然后像这样编译:
gcc screenshot.c -o screenshot -std=c99 -I/usr/X11R6/include -L/usr/X11R6/lib -lX11 -lXext