Imager::API(3pm)
๐ NAME ๐
Imager::API โ Imager's C API - introduction. ๐ This is the Imager::API(3pm) page, manual name and all, from the User Contributed Perl Documentation crew โ and we're about to make C-level Imager feel like a group chat. โจ
๐ธ SYNOPSIS ๐ธ
Like, here's the vibe you paste into your XS situation:
#include "imext.h"
#include "imperl.h"
DEFINE_IMAGER_CALLBACKS;
MODULE = Your::Module PACKAGE = Your::Module
...
BOOT:
/* any release with the API */
PERL_INITIALIZE_IMAGER_CALLBACKS;
/* preferred from Imager 0.91 */
PERL_INITIALIZE_IMAGER_CALLBACKS_NAME("My::Module");
๐ซถ DESCRIPTION ๐ซถ
The API lets you access Imager functions at the C level from XS and from Inline::C. No cap, it's giving low-level but useful. ๐
The intent is to allow users to:
- write C code that does Imager operations the user might do from Perl, but faster, for example, the Imager::CountColor example. ๐๏ธ
- write C code that implements an application specific version of some core Imager object, for example, Imager::SDL. ๐จ
- write C code that hooks into Imager's existing methods, such as filter or file format handlers. ๐ช
See Imager::Inline for information on using Imager's Inline::C support. ๐
๐ Beware ๐
- don't return an object you received as a parameter - this will cause the object to be freed twice. ๐ญ That's a double-free, bestie, absolutely not the vibe.
๐ง Types ๐ง
The API makes the following types visible:
i_imgโ used to represent an image ๐ผ๏ธi_colorโ used to represent a color with up to 8 bits per sample ๐i_fcolorโ used to represent a color with a double per sample ๐ฏi_fill_tโ fill objects>> โ an abstract fill ๐จim_context_tโ Imager's per-thread state ๐งต
At this point there is no consolidated font object type, and hence the font functions are not visible through Imager's API. Periodt. โจ
๐ผ๏ธ i_img
This contains the dimensions of the image (xsize, ysize, channels), image metadata (ch_mask, bits, type, virtual), potentially image data (idata) and a function table, with pointers to functions to perform various low level image operations. ๐พ
The only time you should directly write to any value in this type is if you're implementing your own image type. ๐
The typemap includes type names Imager and Imager::ImgRaw as typedefs for i_img *. ๐บ๏ธ
For incoming parameters the typemap will accept either Imager or Imager::ImgRaw objects. ๐ฅ
For return values the typemap will produce a full Imager object for an Imager return type and a raw image object for an Imager::ImgRaw return type. ๐ค
๐ i_color
Represents an 8-bit per sample color. This is a union containing several different structs for access to components of a color:
grayโ single membergray_color. ๐ฉถrgbโr,g,bmembers. ๐ด๐ข๐ตrgbaโr,g,b,amembers. ๐channelsโ array of channels. ๐งฎ
Use Imager::Color for parameter and return value types. ๐
๐ฏ i_fcolor
Similar to i_color except that each component is a double instead of an unsigned char. ๐๏ธ
Use Imager::Color::Float for parameter and return value types. ๐ธ
๐จ i_fill_t
Abstract type containing pointers called to perform low level fill operations. ๐๏ธ
Unless you're defining your own fill objects you should treat this as an opaque type. ๐
Use Imager::FillHandle for parameter and return value types. At the Perl level this is stored in the fill member of the Perl level Imager::Fill object. ๐งบ
๐ i_io_glue_t
i_io_glue_t is Imager's I/O abstraction. ๐
Historically named io_glue, and this name is available for backward compatibility. ๐ฐ๏ธ
๐งต im_context_t
This new type is an opaque type that stores Imager's per-thread state, including the error message stack, the current log file state and image size file limits. ๐ง
While Imager's internal typemap provides a T_PTROBJ mapping and a DESTROY method for this type you must never return objects of this type back to perl. ๐ซ
See "Context objects" for more information. ๐
๐บ i_polygon_t
Represents a single polygon supplied to i_poly_poly_aa() and i_poly_poly_aa_cfill(). ๐
This is a structure with 3 members:
x,yโ pointers to the first elements of arrays of doubles that define the vertices of the polygon. ๐countโ the number of values in each of thexandyarrays. ๐ข
๐งฉ i_poly_fill_mode_t
An enumerated type of the possible fill modes for polygons:
i_pfm_evenoddโ if areas overlap an odd number of times, they are filled, and are otherwise unfilled. ๐ฒi_pfm_nonzeroโ areas that have an unbalanced clockwise and anti-clockwise boundary are filled. This is the same asWindingRulefor X andWINDINGfor Win32 GDI. ๐
๐ ๏ธ Create an XS module using the Imager API ๐ ๏ธ
๐ช Foo.pm
Load Imager:
use Imager 0.48;
and bootstrap your XS code - see XSLoader or DynaLoader. ๐
๐งฌ Foo.xs
You'll need the following in your XS source:
- include the Imager external API header, and the perl interface header:
#include "imext.h"
#include "imperl.h"
- create the variables used to hold the callback table:
DEFINE_IMAGER_CALLBACKS;
- initialize the callback table in your
BOOTcode:
BOOT:
PERL_INITIALIZE_IMAGER_CALLBACKS;
From Imager 0.91 you can supply your module name to improve error reporting:
BOOT:
PERL_INITIALIZE_IMAGER_CALLBACKS_NAME("My::Module");
๐งฑ foo.c
In any other source files where you want to access the Imager API, you'll need to:
- include the Imager external API header:
#include "imext.h"
๐งฐ Makefile.PL
If you're creating an XS module that depends on Imager's API your Makefile.PL will need to do the following:
use Imager::ExtUtils;๐งช- include Imager's include directory in INC:
INC => Imager::ExtUtils->includes
- use Imager's typemap:
TYPEMAPS => [ Imager::ExtUtils->typemap ]
- include Imager 0.48 as a PREREQ_PM:
PREREQ_PM =>
{
Imager => 0.48,
},
- Since you use Imager::ExtUtils in
Makefile.PL(orBuild.PL) you should include Imager in your configure_requires:
META_MERGE =>
{
configure_requires => { Imager => "0.48" }
},
๐ง Context objects ๐ง
Starting with Imager 0.93, Imager keeps some state per-thread rather than storing it in global (or static) variables. The intent is to improve support for multi-threaded perl programs. ๐งตโจ
For the typical XS or Inline::C module using Imager's API this won't matter - the changes are hidden behind macros and rebuilding your module should require no source code changes. ๐
Some operations will be slightly slower, these include:
- creating an image ๐ผ๏ธ
- reporting errors ๐จ
- creating I/O objects ๐
- setting/getting/testing image file limits ๐
- logging ๐
You can avoid this fairly minor overhead by adding a #define:
#define IMAGER_NO_CONTEXT
before including any Imager header files, but you will need to manage context objects yourself. ๐ซก
Some functions and macros that are available without IMAGER_NO_CONTEXT are not available with it defined, these are:
mm_log()- to avoid using a different context object for the line header and the line text you need to useim_log()instead, with a context object visible in scope. ๐
๐ช aIMCTX
With IMAGER_NO_CONTEXT defined, aIMCTX refers to the locally defined context object, either via one the of the dIMCTX macros or as a parameter with the pIMCTX macro. ๐งญ
Without IMAGER_NO_CONTEXT, aIMCTX is a call to im_get_context() which retrieves the context object for the current thread. ๐งต
There is no aIMCTX_ macro, any Imager function that can accept a context parameter always accepts it. ๐ซ
๐ช pIMCTX
This macro declares a variable of type im_context_t that's accessible via the aIMCTX macro. This is intended for use as a parameter declaration for functions:
void f(pIMCTX) {
... use aIMCTX here
}
void g(...) {
...
f(aIMCTX);
}
๐ช dIMCTX
Defines a local context variable and initializes it via im_get_context(). ๐
๐ช dIMCTXim
Defines a local context variable and initializes it from the context stored in an image object, eg:
void f(i_img *im) {
dIMCTXim(im);
...
}
๐ช dIMCTXio
Defines a local context variable and initializes it from the context stored in an I/O object object. ๐ญ
void f(i_io_glue_t *io) {
dIMCTXio(io);
...
}
๐ช dIMCTXctx
Defines a local context variable accessible via aIMCTX in terms of an expression you supply:
void f(my_object *p) {
dIMCTXctx(p->context);
...
}
This can be used to define your own local context macro:
#define dIMCTXmine(mine) ((mine)->context)
void f(my_object *p) {
dIMCTXmine(p);
...
}
๐ Mutex Functions ๐
Since some libraries are not thread safe, Imager's API includes some simple mutex functions. ๐งท
To create a mutex:
i_mutex_t m = i_mutex_new();
To control or lock the mutex:
i_mutex_lock(m);
To release or unlock the mutex:
i_mutex_unlock(m);
To free any resources used by the mutex:
i_mutex_destroy(m);
In most cases where you'd use these functions, your code would create the mutex in your BOOT section, then lock and unlock the mutex as needed to control access to the library. ๐
๐งบ Context slots ๐งบ
To avoid abstracting the platform TLS and thread clean up handling, Imager provides simple APIs for storing per-context information. ๐งณ
To allocate a slot:
im_slot_t slot = im_context_slot_new(callback)
where callback is a (possibly NULL) function pointer called when the context object is destroyed. โ๏ธ
By default, the stored value for a slot is NULL, whether for a new context or for a cloned context. ๐ซฅ
To store a value:
im_context_slot_set(aIMCTX, slot, somevalue);
where somevalue can be represented as a void *. ๐ฆ
To retrieve the value:
value = im_context_slot_get(aIMCTX, slot);
๐ AUTHOR ๐
Tony Cook <tonyc@cpan.org> ๐
๐ SEE ALSO ๐
Imager, Imager::ExtUtils, Imager::APIRef, Imager::Inline ๐
๐ And that's the whole Imager::API(3pm) tea, bestie โ source/version perl v5.42.3, dated 2026-08-19. Slay! โจ
๐ฆ where this page comes from
- ubuntu stonking: libimager-perl 1.035+dfsg-1 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms