【发布时间】:2015-12-24 07:56:53
【问题描述】:
我正在尝试使用 Imlib 库将两个图像与 alpha 混合(此代码正在更改壁纸)。 到目前为止,我得到了这个:
#include <stdio.h>
#include <X11/Xlib.h>
#include <Imlib2.h>
int main( int argc, char **argv )
{
Display *display;
Pixmap pixmap;
Window root;
Screen *screen;
int width,
height,
w1,
w2,
h1,
h2;
Imlib_Image img,
img2;
// loading images
img = imlib_load_image( "/usr/share/wallpapers/wallpaper1.jpg" );
img2 = imlib_load_image( "/usr/share/wallpapers/wallpaper2.jpg" );
if( !img || !img2 ) {
printf( "Unable to load image.\n" );
return 1;
}
// open display, get screen and root window
display = XOpenDisplay(NULL);
screen = DefaultScreenOfDisplay( display );
root = DefaultRootWindow( display );
width = screen->width;
height = screen->height;
// create pixmap
pixmap = XCreatePixmap( display, root, width, height, DefaultDepthOfScreen(screen) );
// #S == BLENDING 1 =========================
imlib_context_set_image( img );
w1 = imlib_image_get_width();
h1 = imlib_image_get_height();
imlib_context_set_image( img2 );
w2 = imlib_image_get_width();
h2 = imlib_image_get_height();
imlib_context_set_image( img );
imlib_blend_image_onto_image( img2, 0, 0, 0, width, height, 0, 0, width, height );
//imlib_blend_image_onto_image( img2, 0, 0, 0, w2, h2, 0, 0, w1, h1 );
// #E == BLENDING 1 =========================
// setting context
imlib_context_set_image( img );
imlib_context_set_display( display );
imlib_context_set_visual( DefaultVisualOfScreen(screen) );
imlib_context_set_colormap( DefaultColormapOfScreen(screen) );
imlib_context_set_drawable( pixmap );
// render image into pixmap
imlib_render_image_on_drawable_at_size( 0, 0, width, height );
// set pixmap as background
XSetWindowBackgroundPixmap( display, root, pixmap );
// clear window
XClearWindow( display, root );
XFlush( display );
// free...
XFreePixmap( display, pixmap );
imlib_context_set_image( img );
imlib_free_image();
imlib_context_set_image( img2 );
imlib_free_image();
// close display
XCloseDisplay( display );
return 0;
}
它正在改变壁纸,但我想将这两个图像与自定义 alpha 混合,我的意思是,像这样:How to blend two image
那么,如何在 Imlib 中为混合操作设置图像透明度?
编译:gcc main.c -lX11 -lImlib2
【问题讨论】: