Imlib2: Imlib2 Library Documentation (original) (raw)

imlib2.png

Version:

1.1.1

Date:

1999-2004

What is Imlib2 ?

Imlib 2 is the successor to Imlib. It is NOT a newer version - it is a completely new library. Imlib 2 can be installed alongside Imlib 1.x without any problems since they are effectively different libraries - BUT they Have very similar functionality.

Imlib 2 does the following:

If what you want isn't in the list above somewhere then likely Imlib 2 does not do it. If it does it it likely does it faster than any other library you can find (this includes gdk-pixbuf, gdkrgb, etc.) primarily because of highly optimized code and a smart subsystem that does the dirty work for you and picks up the pieces for you so you can be lazy and let all the optimizations for FOR you.

Imlib 2 can run without a display, so it can be easily used for background image processing for web sites or servers - it only requires the X libraries to be installed - that is all - it does not require an XServer to run unless you wish to display images.

The interface is simple - once you get used to it, the functions do exactly what they say they do.

A Simple Example

The best way to start is to show a simple example of an Imlib2 program. This one will load an image of any format you have a loader installed for (all loaders are dynamic code objects that Imlib2 will use and update automatically runtime - anyone is free to write a loader. All that has to be done is for the object to be dropped into the loaders directory with the others and all Imlib2 programs will automatically be able to use it - without a restart).

Now to compile this

cc imlib2_convert.c -o imlib2_convert imlib2-config --cflags imlib2-config --libs

You now have a program that if used as follows:

cc imlib2_con ./imlib2_convert image1.jpg image2.png

will convert image1.jpg into a png called image2.png. It is that simple.

How Image Loading Works

It is probably a good idea to discuss how Imlib2 actually loads an Image so the programmer knows what is going on, how to take advantage of the optimizations already there and to explain why things work as they do.

Loading using imlib_load_image();

This is likely to be by far the most common way to load an image - when you don't really care about the details of the loading process or why it failed - all you care about is if you got a valid image handle.

When you call this function Imlib2 attempts to find the file specified as the parameter. This will involve Imlib2 first checking to see if that file path already has been loaded and is in Imlib2's cache (a cache of already decoded images in memory to save having to load and decode from disk all the time). If there already is a copy in the cache (either already active or speculatively cached from a previous load & free) this copy will have its handle returned instead of Imlib2 checking on disk (in some circumstances this is not true - see later in this section to find out). This means if your program blindly loads an Image, renders it, then frees it - then soon afterwards loads the same image again, it will not be loaded from disk at all, instead it will simply be re-referenced from the cache - meaning the load will be almost instant. A great way to take full advantage of this is to set the cache to some size you are happy with for the image data being used by your application and then all rendering o an image follows the pseudo code:

set cache to some amount (e.g. 4 Mb) ... rendering loop ... load image render image free image ... continue loop

This may normally sound silly - load image, render then free - EVERY time we want to use it, BUT - it is actually the smartest way to use Imlib2 - since the caching will find the image for you in the cache - you do not need to manage your own cache, or worry about filling up memory with image data - only as much memory as you have set for the cache size will actually ever be used to store image data - if you have lots of image data to work with then increase the cache size for better performance, but this is the only thing you need to worry about. you won't have problems of accidentally forgetting to free images later since you free them immediately after use.

Now what happens if the file changes on disk while it's in cache? By default nothing. The file is ignored. This is an optimization (to avoid hitting the disk to check if the file changed for every load if it's cached). You can inform Imlib2 that you care about this by using the imlib_image_set_changes_on_disk(); call. Do this whenever you load an Image that you expect will change on disk, and the fact that it changes really matters. Remember this will marginally reduce the caching performance.

Now what actually happens when we try and load an image using a filename? First the filename is broken down into 2 parts. the filename before a colon (:) and the key after the colon. This means when we have a filename like:

the filename is:

and the key is blank. If we have:

/path/to/file.db:key.value/blah

the filename is:

and the key is:

You may ask what is this thing with keys and filenames? Well Imlib2 has loaders that are able to load data that is WITHIN a file (the loader capable of this right now is the database loader that is able to load image data stored with a key in a Berkeley-db database file). The colon is used to delimit where the filename ends and the key begins. Fro the majority of files you load you won't have to worry, but there is a limit in this case that filenames cannot contain a color character.

First Imlib2 checks to see if the file exists and that you have permission to read it. If this fails it will abort the load. Now that it has checked that this is the case it evaluates that it's list of dynamically loaded loader modules it up to date then it runs through the loader modules until one of them claims it can load this file. If this is the case that loader is now used to decode the image and return an Image handle to the calling program. If the loader is written correctly and the file format sanely supports this, the loader will NOT decode any image data at this point. It will ONLY read the header of the image to figure out its size, if it has an alpha channel, format and any other header information. The loader is remembered and it will be re-used to load the image data itself later if and ONLY if the actual image data itself is needed. This means you can scan vast directories of files figuring their format and size and other such information just by loading and freeing - and it will be fast because no image data is decoded. You can take advantage of this by loading the image and checking its size to calculate the size of an output area before you ever load the data. This means geometry calculations can be done fast ahead of time.

If you desire more detailed information about why a load failed you can use imlib_load_image_with_error_return(); and it will return a detailed error return code.

If you do not wish to have the image data loaded later using the optimized "deferred" method of loading, you can force the data to be decoded immediately with imlib_load_image_immediately();

If you wish to bypass the cache when loading images you can using imlib_load_image_without_cache(); and imlib_load_image_immediately_without_cache();.

Sometimes loading images can take a while. Often it is a good idea to provide feedback to the user whilst this is happening. This is when you set the progress function callback. Setting this to NULL will mean no progress function is called during load - this is the default. When it is set you set it to a function that will get called every so often (depending on the progress granularity) during load. Use imlib_context_set_progress_function(); and imlib_context_set_progress_granularity(); to set this up.

A more advanced Example

This is a more comprehensive example that should show off a fair number of features of imlib2. The code this was based off can be found in Imlib2's test directory. This covers a lot of the core of Imlib2's API so you should have a pretty good idea on how it works if you understand this code snippet.

#include <X11/Xlib.h>

#include <Imlib2.h>

#include <stdio.h>

Display *disp; Window win; Visual *vis; Colormap cm; int depth;

int main(int argc, char **argv) {

XEvent ev;

Imlib_Updates updates, current_update;

Imlib_Image buffer;

Imlib_Font font;

Imlib_Color_Range range;

int mouse_x = 0, mouse_y = 0;

disp = XOpenDisplay(NULL);

vis = DefaultVisual(disp, DefaultScreen(disp)); depth = DefaultDepth(disp, DefaultScreen(disp)); cm = DefaultColormap(disp, DefaultScreen(disp));

win = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 0, 0, 640, 480, 0, 0, 0);

XSelectInput(disp, win, ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask);

XMapWindow(disp, win);

imlib_set_cache_size(2048 * 1024);

imlib_set_font_cache_size(512 * 1024);

imlib_add_path_to_font_path("./ttfonts");

imlib_set_color_usage(128);

imlib_context_set_dither(1);

imlib_context_set_display(disp); imlib_context_set_visual(vis); imlib_context_set_colormap(cm); imlib_context_set_drawable(win);

for (;;) {

    Imlib_Image image;
    
    int w, h, text_w, text_h;
    
    
    updates = [imlib_updates_init](imlib2%5F8c.html#7f3407f95d0972f5b5bfc465b93714a6)();
    
    do
      {
         XNextEvent(disp, &ev);
         switch (ev.type)
           {
           case Expose:
              
              
              updates = [imlib_update_append_rect](imlib2%5F8c.html#22c2366e4eef7f602e1987891ba12667)(updates,
                                                 ev.xexpose.x, ev.xexpose.y,
                                                 ev.xexpose.width, ev.xexpose.height);
              break;
           case ButtonPress:
              
              exit(0);
              break;
           case MotionNotify:
              
              
              image = [imlib_load_image](imlib2%5F8c.html#1a059be0808b1233d72fb07a0224aeaf)("./test_images/mush.png");
              [imlib_context_set_image](imlib2%5F8c.html#e9e9156e66ed4c916ccd218b9f235379)(image);
              w = [imlib_image_get_width](imlib2%5F8c.html#fddedbe369ed8e0c18f8eb072cc1ce34)();
              h = [imlib_image_get_height](imlib2%5F8c.html#81c9808862c7beda1784f2877dc2b59a)();
              [imlib_context_set_image](imlib2%5F8c.html#e9e9156e66ed4c916ccd218b9f235379)(image);
              [imlib_free_image](imlib2%5F8c.html#92425f7d08b75644d5d32c7c1dd116c3)();
              
              updates = [imlib_update_append_rect](imlib2%5F8c.html#22c2366e4eef7f602e1987891ba12667)(updates,
                                                 mouse_x - (w / 2), mouse_y - (h / 2),
                                                 w, h);
              font = [imlib_load_font](imlib2%5F8c.html#9469e1861efe35a6e7fc0f65465987a3)("notepad/30");
              if (font)
                {
                   char text[4096];
                   
                   [imlib_context_set_font](imlib2%5F8c.html#e125d4cb2f5101e056605736c6f645af)(font);
                   sprintf(text, "Mouse is at %i, %i", mouse_x, mouse_y);
                   [imlib_get_text_size](imlib2%5F8c.html#72de880a2b9e8bb3f180de42e774f487)(text, &text_w, &text_h); 
                   [imlib_free_font](imlib2%5F8c.html#17cff450fb0bbe5535c09c5420c960ae)();
                   updates = [imlib_update_append_rect](imlib2%5F8c.html#22c2366e4eef7f602e1987891ba12667)(updates,
                                                      320 - (text_w / 2), 240 - (text_h / 2),
                                                      text_w, text_h);
                }
              
              mouse_x = ev.xmotion.x;
              mouse_y = ev.xmotion.y;
              
              updates = [imlib_update_append_rect](imlib2%5F8c.html#22c2366e4eef7f602e1987891ba12667)(updates,
                                                 mouse_x - (w / 2), mouse_y - (h / 2),
                                                 w, h);
              font = [imlib_load_font](imlib2%5F8c.html#9469e1861efe35a6e7fc0f65465987a3)("notepad/30");
              if (font)
                {
                   char text[4096];
                   
                   [imlib_context_set_font](imlib2%5F8c.html#e125d4cb2f5101e056605736c6f645af)(font);
                   sprintf(text, "Mouse is at %i, %i", mouse_x, mouse_y);
                   [imlib_get_text_size](imlib2%5F8c.html#72de880a2b9e8bb3f180de42e774f487)(text, &text_w, &text_h); 
                   [imlib_free_font](imlib2%5F8c.html#17cff450fb0bbe5535c09c5420c960ae)();
                   updates = [imlib_update_append_rect](imlib2%5F8c.html#22c2366e4eef7f602e1987891ba12667)(updates,
                                                      320 - (text_w / 2), 240 - (text_h / 2),
                                                      text_w, text_h);
                }
           default:
              
              break;
           }
      }
    while (XPending(disp));
    
    
    updates = [imlib_updates_merge_for_rendering](imlib2%5F8c.html#16a0b70fcababed3524571b63157b8e2)(updates, 640, 480);
    for (current_update = updates; 
         current_update; 
         current_update = [imlib_updates_get_next](imlib2%5F8c.html#afe4d1c32f29e6f687f74dcbe0b05c52)(current_update))
      {
         int up_x, up_y, up_w, up_h;

         
         [imlib_updates_get_coordinates](imlib2%5F8c.html#09e812bc0f082de0b1c66b1fa0bf7ae1)(current_update, 
                                       &up_x, &up_y, &up_w, &up_h);
         
         
         buffer = [imlib_create_image](imlib2%5F8c.html#a27178bd596d8dee6f239f083262dfb6)(up_w, up_h);
         
         
         [imlib_context_set_blend](imlib2%5F8c.html#8d41edc6cca884590c362997c96296a8)(1);
         
         
         image = [imlib_load_image](imlib2%5F8c.html#1a059be0808b1233d72fb07a0224aeaf)("./test_images/bg.png");
         
         [imlib_context_set_image](imlib2%5F8c.html#e9e9156e66ed4c916ccd218b9f235379)(image);
         
         w = [imlib_image_get_width](imlib2%5F8c.html#fddedbe369ed8e0c18f8eb072cc1ce34)();
         h = [imlib_image_get_height](imlib2%5F8c.html#81c9808862c7beda1784f2877dc2b59a)();
         
         [imlib_context_set_image](imlib2%5F8c.html#e9e9156e66ed4c916ccd218b9f235379)(buffer);
         
         if (image) 
           {
              
              [imlib_blend_image_onto_image](imlib2%5F8c.html#622086e746e488a8147029fa6aa2868d)(image, 0, 
                                           0, 0, w, h, 
                                           - up_x, - up_y, 640, 480);
              
              [imlib_context_set_image](imlib2%5F8c.html#e9e9156e66ed4c916ccd218b9f235379)(image);
              
              [imlib_free_image](imlib2%5F8c.html#92425f7d08b75644d5d32c7c1dd116c3)();
           }
         
         
         image = [imlib_load_image](imlib2%5F8c.html#1a059be0808b1233d72fb07a0224aeaf)("./test_images/mush.png");
         [imlib_context_set_image](imlib2%5F8c.html#e9e9156e66ed4c916ccd218b9f235379)(image);
         w = [imlib_image_get_width](imlib2%5F8c.html#fddedbe369ed8e0c18f8eb072cc1ce34)();
         h = [imlib_image_get_height](imlib2%5F8c.html#81c9808862c7beda1784f2877dc2b59a)();
         [imlib_context_set_image](imlib2%5F8c.html#e9e9156e66ed4c916ccd218b9f235379)(buffer);
         if (image) 
           {
              [imlib_blend_image_onto_image](imlib2%5F8c.html#622086e746e488a8147029fa6aa2868d)(image, 0, 
                                           0, 0, w, h, 
                                           mouse_x - (w / 2) - up_x, mouse_y - (h / 2) - up_y, w, h);
              [imlib_context_set_image](imlib2%5F8c.html#e9e9156e66ed4c916ccd218b9f235379)(image);
              [imlib_free_image](imlib2%5F8c.html#92425f7d08b75644d5d32c7c1dd116c3)();
           }
         
         
         range = [imlib_create_color_range](imlib2%5F8c.html#5d6cf6fde757e6e4083ee83a624ee94f)();
         [imlib_context_set_color_range](imlib2%5F8c.html#4f7116dfd765cb9b66439d306fd59d34)(range);
         
         [imlib_context_set_color](imlib2%5F8c.html#1f021f6885bff92d7d6116f33cc37420)(255, 255, 255, 255);
         [imlib_add_color_to_color_range](imlib2%5F8c.html#08b4857df0ff0fa53ffdcff302aeb36d)(0);
         
         [imlib_context_set_color](imlib2%5F8c.html#1f021f6885bff92d7d6116f33cc37420)(255, 200, 10, 100);
         [imlib_add_color_to_color_range](imlib2%5F8c.html#08b4857df0ff0fa53ffdcff302aeb36d)(10);
         
         [imlib_context_set_color](imlib2%5F8c.html#1f021f6885bff92d7d6116f33cc37420)(0, 0, 0, 0);
         [imlib_add_color_to_color_range](imlib2%5F8c.html#08b4857df0ff0fa53ffdcff302aeb36d)(20);
         
         [imlib_context_set_image](imlib2%5F8c.html#e9e9156e66ed4c916ccd218b9f235379)(buffer);
         [imlib_image_fill_color_range_rectangle](imlib2%5F8c.html#8121f8a9848859b7af9d03f9c55cf1d8)(- up_x, - up_y, 128, 128, -45.0);
         
         [imlib_free_color_range](imlib2%5F8c.html#efc3a0446f269bb30cae65aa1819e4fd)();
         
         
         font = [imlib_load_font](imlib2%5F8c.html#9469e1861efe35a6e7fc0f65465987a3)("notepad/30");
         if (font)
           {
              char text[4096];
              
              
              [imlib_context_set_font](imlib2%5F8c.html#e125d4cb2f5101e056605736c6f645af)(font);
              
              [imlib_context_set_image](imlib2%5F8c.html#e9e9156e66ed4c916ccd218b9f235379)(buffer);
              
              [imlib_context_set_color](imlib2%5F8c.html#1f021f6885bff92d7d6116f33cc37420)(0, 0, 0, 255);
              
              sprintf(text, "Mouse is at %i, %i", mouse_x, mouse_y);
              
              [imlib_get_text_size](imlib2%5F8c.html#72de880a2b9e8bb3f180de42e774f487)(text, &text_w, &text_h); 
              
              [imlib_text_draw](imlib2%5F8c.html#c370caef97ed56496fde307993235c92)(320 - (text_w / 2) - up_x, 240 - (text_h / 2) - up_y, text); 
              
              [imlib_free_font](imlib2%5F8c.html#17cff450fb0bbe5535c09c5420c960ae)();
           }
         
         
         [imlib_context_set_blend](imlib2%5F8c.html#8d41edc6cca884590c362997c96296a8)(0);
         
         [imlib_context_set_image](imlib2%5F8c.html#e9e9156e66ed4c916ccd218b9f235379)(buffer);
         
         imlib_render_image_on_drawable(up_x, up_y);
         
         [imlib_free_image](imlib2%5F8c.html#92425f7d08b75644d5d32c7c1dd116c3)();
      }
    
    if (updates)
       [imlib_updates_free](imlib2%5F8c.html#be954339ea1cba3ffa63e11217c2e20c)(updates);
    
 }

return 0; }

Imlib2 filters

Imlib 2 Dynamic Filters

Imlib2 has built in features allowing filters and effects to be applied at run time through a very small scripting language, this is similar to that of script-fu found in the GIMP (http://www.gimp.org). There are two parts to the system, the client library call ``imlib_apply_filter'' and the library side filters. The library side filters are synonymous with image loaders.

To run a script on an image you need to set the context image then call:

imlib_apply_filter( script_string, ... );

The script_string variable is made up of the script language, which is very simple and made up of only function calls. Functions calls look like this:

filter name( key=value [, ...] );

Where,

the program, or the result of another filter.

eg.

bump_map( map=tint(red=50,tint=200), blue=10 );

This example would bump map using a a map generated from the tint filter.

It is also possible to pass application information to the filters via the usage of the [] operator. When the script is being compiled the script engine looks on the parameters passed to it and picks up a pointer for every [] found.

eg2.

imlib_apply_filter( "tint( x=[], y=[], red=255, alpha=55 );", &myxint, &myyint );

This will cause a tint to the current image at (myxint,myyint) to be done. This is very useful for when you want the filters to dynamically change according to program variables. The system is very quick as the code is pseudo-compiled and then run. The advantage of having the scripting system allows customization of the image manipulations, this is particularly useful in applications that allow modifications to be done (eg. image viewers).

Filter Library

There are three functions that must be in every filter library

void init( struct imlib_filter_info *info ); - Called once on loading of the filter

info - a structure passed to the filter to be filled in with information about the filter info->name - Name of the filter library info->author - Name of the library author info->description - Description of the filter library info->num_filters - Number of filters the library exports info->filters - An array of ``char *'' with each filter name in it.

void deinit(); - Called when the filter is closed

void *exec( char *filter, void *im, pIFunctionParam params );

filter - The name of the filter being asked for im - The image that the filter should be applied against params - A linked list of parameters.

The best way to get all the values is such:

Declare all parameters and initialize them to there default values.

for( ptr = params; ptr != NULL; ptr = ptr->next ) { ..MACRO TO GET VALUE.. }

Current Macros are:

ASSIGN_INT( keyname, local variable ) ASSIGN_DATA8( keyname, local variable ) ASSIGN_IMAGE( keyname, local variable )

eg.

int r = 50; IFunctionParam *ptr;

for( ptr = params; ptr != NULL; ptr = ptr->next ) { ASSIGN_INT( "red", r ); }

If the value "red" is not passed to the filter then it will remain at 50, but it a value is passed, it will be assign to r.

return type - Imlib_Image, this is the result of filter.

Todo:

line code doesnt draw nice liens when clipping - fix

Todo:

filled polygons can break fill bounds on corner cases - fix

Todo:

go thru TODOs and FIXMEs