3 Imager::API - Imager's C API - introduction.
10 DEFINE_IMAGER_CALLBACKS;
12 MODULE = Your::Module PACKAGE = Your::Module
17 /* any release with the API */
18 PERL_INITIALIZE_IMAGER_CALLBACKS;
19 /* preferred from Imager 0.91 */
20 PERL_INITIALIZE_IMAGER_CALLBACKS_NAME("My::Module");
27 The API allows you to access Imager functions at the C level from XS
28 and from C<Inline::C>.
30 The intent is to allow users to:
36 write C code that does Imager operations the user might do from Perl,
37 but faster, for example, the L<Imager::CountColor> example.
41 write C code that implements an application specific version of some
42 core Imager object, for example, Imager::SDL.
46 write C code that hooks into Imager's existing methods, such as filter
47 or file format handlers.
51 See L<Imager::Inline> for information on using Imager's Inline::C
60 don't return an object you received as a parameter - this will cause
61 the object to be freed twice.
67 The API makes the following types visible:
73 L</i_img> - used to represent an image
77 L</i_color> - used to represent a color with up
82 L</i_fcolor> - used to represent
83 a color with a double per sample.
87 L</i_fill_t> - fill objects>> - an abstract fill
91 L</im_context_t> - Imager's per-thread state.
95 At this point there is no consolidated font object type, and hence the
96 font functions are not visible through Imager's API.
100 This contains the dimensions of the image (C<xsize>, C<ysize>,
101 C<channels>), image metadata (C<ch_mask>, C<bits>, C<type>,
102 C<virtual>), potentially image data (C<idata>) and a function table,
103 with pointers to functions to perform various low level image
106 The only time you should directly write to any value in this type is
107 if you're implementing your own image type.
109 The typemap includes type names Imager and Imager::ImgRaw as typedefs
112 For incoming parameters the typemap will accept either Imager or
113 Imager::ImgRaw objects.
115 For return values the typemap will produce a full Imager object for an
116 Imager return type and a raw image object for an Imager::ImgRaw return
121 Represents an 8-bit per sample color. This is a union containing
122 several different structs for access to components of a color:
128 C<gray> - single member C<gray_color>.
132 C<rgb> - C<r>, C<g>, C<b> members.
136 C<rgba> - C<r>, C<g>, C<b>, C<a> members.
140 C<channels> - array of channels.
144 Use C<Imager::Color> for parameter and return value types.
148 Similar to C<i_color> except that each component is a double instead of
151 Use Imager::Color::Float for parameter and return value types.
155 Abstract type containing pointers called to perform low level fill
158 Unless you're defining your own fill objects you should treat this as
161 Use Imager::FillHandle for parameter and return value types. At the
162 Perl level this is stored in the C<fill> member of the Perl level
167 C<i_io_glue_t> is Imager's I/O abstraction.
169 Historically named C<io_glue>, and this name is available for backward
174 This new type is an opaque type that stores Imager's per-thread state,
175 including the error message stack, the current log file state and
176 image size file limits.
178 While Imager's internal typemap provides a C<T_PTROBJ> mapping and a
179 DESTROY method for this type you B<must> never return objects of this
182 See L</Context objects> for more information.
186 Represents a single polygon supplied to i_poly_poly_aa() and
187 i_poly_poly_aa_cfill().
189 This is a structure with 3 members:
195 C<x>, C<y> - pointers to the first elements of arrays of doubles that define
196 the vertices of the polygon.
200 C<count> - the number of values in each of the C<x> and C<y> arrays.
204 =head2 i_poly_fill_mode_t
206 An enumerated type of the possible fill modes for polygons:
212 C<i_pfm_evenodd> - if areas overlap an odd number of times, they
213 are filled, and are otherwise unfilled.
217 C<i_pfm_nonzero> - areas that have an unbalanced clockwise and
218 anti-clockwise boundary are filled. This is the same as
219 C<WindingRule> for X and C<WINDING> for Win32 GDI.
223 =head1 Create an XS module using the Imager API
231 and bootstrap your XS code - see L<XSLoader> or L<DynaLoader>.
235 You'll need the following in your XS source:
241 include the Imager external API header, and the perl interface header:
248 create the variables used to hold the callback table:
250 DEFINE_IMAGER_CALLBACKS;
254 initialize the callback table in your C<BOOT> code:
257 PERL_INITIALIZE_IMAGER_CALLBACKS;
259 From Imager 0.91 you can supply your module name to improve error
263 PERL_INITIALIZE_IMAGER_CALLBACKS_NAME("My::Module");
269 In any other source files where you want to access the Imager API,
276 include the Imager external API header:
282 =head2 C<Makefile.PL>
284 If you're creating an XS module that depends on Imager's API your
285 C<Makefile.PL> will need to do the following:
291 C<use Imager::ExtUtils;>
295 include Imager's include directory in INC:
297 INC => Imager::ExtUtils->includes
301 use Imager's typemap:
303 TYPEMAPS => [ Imager::ExtUtils->typemap ]
307 include Imager 0.48 as a PREREQ_PM:
316 Since you use Imager::ExtUtils in C<Makefile.PL> (or C<Build.PL>) you
317 should include Imager in your configure_requires:
321 configure_requires => { Imager => "0.48" }
326 =head1 Context objects
328 Starting with Imager 0.93, Imager keeps some state per-thread rather
329 than storing it in global (or static) variables. The intent is to
330 improve support for multi-threaded perl programs.
332 For the typical XS or Inline::C module using Imager's API this won't
333 matter - the changes are hidden behind macros and rebuilding your
334 module should require no source code changes.
336 Some operations will be slightly slower, these include:
354 setting/getting/testing image file limits
362 You can avoid this fairly minor overhead by adding a C<#define>:
364 #define IMAGER_NO_CONTEXT
366 before including any Imager header files, but you will need to manage
367 context objects yourself.
369 Some functions and macros that are available without
370 C<IMAGER_NO_CONTEXT> are not available with it defined, these are:
376 mm_log() - to avoid using a different context object for the line
377 header and the line text you need to use im_log() instead, with a
378 context object visible in scope.
384 With C<IMAGER_NO_CONTEXT> defined, C<aIMCTX> refers to the locally
385 defined context object, either via one the of the C<dIMCTX> macros or
386 as a parameter with the C<pIMCTX> macro.
388 Without C<IMAGER_NO_CONTEXT>, C<aIMCTX> is a call to
389 C<im_get_context()> which retrieves the context object for the current
392 There is no C<aIMCTX_> macro, any Imager function that can accept a
393 context parameter always accepts it.
397 This macro declares a variable of type L</im_context_t> that's
398 accessible via the C<aIMCTX> macro. This is intended for use as a
399 parameter declaration for functions:
412 Defines a local context variable and initializes it via
413 L<im_get_context()|Imager::APIRef/im_get_context()>.
417 Defines a local context variable and initializes it from the context
418 stored in an L<image object|/i_img>, eg:
427 Defines a local context variable and initializes it from the context
428 stored in an L<< IE<47>O object|/i_io_glue_t >> object.
430 void f(i_io_glue_t *io) {
437 Defines a local context variable accessible via C<aIMCTX> in terms of
438 an expression you supply:
440 void f(my_object *p) {
441 dIMCTXctx(p->context);
445 This can be used to define your own local context macro:
447 #define dIMCTXmine(mine) ((mine)->context)
449 void f(my_object *p) {
454 =head1 Mutex Functions
456 Since some libraries are not thread safe, Imager's API includes some
457 simple mutex functions.
461 i_mutex_t m = i_mutex_new();
463 To control or lock the mutex:
467 To release or unlock the mutex:
471 To free any resources used by the mutex:
475 I most cases where you'd use these functions, your code would create
476 the mutex in your BOOT section, then lock and unlock the mutex as
477 needed to control access to the library.
484 To avoid abstracting the platform TLS and thread clean up handling,
485 Imager provides simple APIs for storing per-context information.
489 im_slot_t slot = im_context_slot_new(callback)
491 where callback is a (possibly NULL) function pointer called when the
492 context object is destroyed.
494 By default, the stored value for a slot is NULL, whether for a new
495 context or for a cloned context.
499 im_context_slot_set(aIMCTX, slot, somevalue);
501 where C<somevalue> can be represented as a C<void *>.
503 To retrieve the value:
505 value = im_context_slot_get(aIMCTX, slot);
509 Tony Cook <tonyc@cpan.org>
513 Imager, Imager::ExtUtils, Imager::APIRef, Imager::Inline