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:
\li Load image files from disk in one of many formats
\li Save images to disk in one of many formats
\li Render image data onto other images
\li Render images to an X-Windows drawable
\li Produce pixmaps and pixmap masks of Images
\li Apply filters to images
\li Rotate images
\li Accept RGBA Data for images
\li Scale images
\li Alpha blend Images on other images or drawables
\li Apply color correction and modification tables and factors to images
\li Render images onto images with color correction and modification tables
\li Render truetype anti-aliased text
\li Render truetype anti-aliased text at any angle
\li Render anti-aliased lines
\li Render rectangles
\li Render linear multi-colored gradients
\li Cache data intelligently for maximum performance
\li Allocate colors automatically
\li Allow full control over caching and color allocation
\li Provide highly optimized MMX assembly for core routines
\li Provide plug-in filter interface
\li Provide on-the-fly runtime plug-in image loading and saving interface
\li Fastest image compositing, rendering and manipulation library for X
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.
@section first_ex 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).
will convert image1.jpg into a png called image2.png. It is that simple.
@section loading 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.
@subsection load_func 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:
@verbatim
set cache to some amount (e.g. 4 Mb)
...
rendering loop ...
load image
render image
free image
... continue loop
@endverbatim
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:
@verbatim
/path/to/file.jpg
@endverbatim
the filename is:
@verbatim
/path/to/file.jpg
@endverbatim
and the key is blank. If we have:
@verbatim
/path/to/file.db:key.value/blah
@endverbatim
the filename is:
@verbatim
/path/to/file.db
@endverbatim
and the key is:
@verbatim
key.value/blah
@endverbatim
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.
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:
@verbatim
imlib_apply_filter( script_string, ... );
@endverbatim
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:
@verbatim
filter name( key=value [, ...] );
@endverbatim
Where,
\li <b>filter name</b> is the name of the filter you wish to apply
\li \b key is an expected value
\li \b value is a ``string'', a number, or an actual variable in
the program, or the result of another filter.
eg.
@verbatim
bump_map( map=tint(red=50,tint=200), blue=10 );
@endverbatim
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.
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).
@subsection lib_filter Filter Library
There are three functions that must be in every filter library
@code
void init( struct imlib_filter_info *info ); - Called once on loading of the filter
@endcode
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.
@code
void deinit(); - Called when the filter is closed
/* Called every time a filter the library exports is called */