C Imlib 将两个图像与 alpha 混合

C Imlib blend two images with alpha

我正在尝试使用 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

好的,我知道了! 首先,我需要为图像设置 alpha,创建第二个 imlib 对象并用带有 alpha 的自定义颜色填充矩形。然后将 alpha 从一个图像复制到第二个图像...那只是...呃...

imlib_context_set_image( img );
w1 = imlib_image_get_width();
h1 = imlib_image_get_height();

img3 = imlib_create_image( w1, h1 );

imlib_context_set_image( img3 );
imlib_image_set_has_alpha( 1 );
imlib_context_set_color( 0, 0, 0, 150 );
imlib_image_fill_rectangle( 0, 0, w1, h1 );

imlib_context_set_image( img );
imlib_image_set_has_alpha( 1 );
imlib_image_copy_alpha_to_image( img3, 0, 0 );

imlib_context_set_image( img2 );
imlib_context_set_operation( IMLIB_OP_COPY );
imlib_blend_image_onto_image( img, 1, 0, 0, w1, h1, 0, 0, w1, h1 );

这里imgimg2变量是图片。它以 imlib_context_set_color alpha.

中给出的图像混合到图像上