简体   繁体   中英

C Imlib blend two images with alpha

I'm trying to blend two images with alpha using Imlib library (this code is changing wallpaper). So far I got this:

#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;
}

It's changing wallpaper, but I want to blend this two images with custom alpha, I mean, something like this: How to blend two image

So, how to set image transparency for blending operation in Imlib?

Compile with: gcc main.c -lX11 -lImlib2

Ok, I got it! First I need to set alpha for image, create second imlib object and fill with rectangle in custom color with alpha. Then just copy alpha from one image to second... That's just... ehh...

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 );

Here img and img2 variables are pictures. It gives image blended onto image with given in imlib_context_set_color alpha.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM