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.
184 =head1 Create an XS module using the Imager API
192 and bootstrap your XS code - see L<XSLoader> or L<DynaLoader>.
196 You'll need the following in your XS source:
202 include the Imager external API header, and the perl interface header:
209 create the variables used to hold the callback table:
211 DEFINE_IMAGER_CALLBACKS;
215 initialize the callback table in your C<BOOT> code:
218 PERL_INITIALIZE_IMAGER_CALLBACKS;
220 From Imager 0.91 you can supply your module name to improve error
224 PERL_INITIALIZE_IMAGER_CALLBACKS_NAME("My::Module");
230 In any other source files where you want to access the Imager API,
237 include the Imager external API header:
243 =head2 C<Makefile.PL>
245 If you're creating an XS module that depends on Imager's API your
246 C<Makefile.PL> will need to do the following:
252 C<use Imager::ExtUtils;>
256 include Imager's include directory in INC:
258 INC => Imager::ExtUtils->includes
262 use Imager's typemap:
264 TYPEMAPS => [ Imager::ExtUtils->typemap ]
268 include Imager 0.48 as a PREREQ_PM:
277 Since you use Imager::ExtUtils in C<Makefile.PL> (or C<Build.PL>) you
278 should include Imager in your configure_requires:
282 configure_requires => { Imager => "0.48" }
287 =head1 Context objects
289 Starting with Imager 0.93, Imager keeps some state per-thread rather
290 than storing it in global (or static) variables. The intent is to
291 improve support for multi-threaded perl programs.
293 For the typical XS or Inline::C module using Imager's API this won't
294 matter - the changes are hidden behind macros and rebuilding your
295 module should require no source code changes.
297 Some operations will be slightly slower, these include:
315 setting/getting/testing image file limits
323 You can avoid this fairly minor overhead by adding a C<#define>:
325 #define IMAGER_NO_CONTEXT
327 before including any Imager header files, but you will need to manage
328 context objects yourself.
330 Some functions and macros that are available without
331 C<IMAGER_NO_CONTEXT> are not available with it defined, these are:
337 mm_log() - to avoid using a different context object for the line
338 header and the line text you need to use im_log() instead, with a
339 context object visible in scope.
345 With C<IMAGER_NO_CONTEXT> defined, C<aIMCTX> refers to the locally
346 defined context object, either via one the of the C<dIMCTX> macros or
347 as a parameter with the C<pIMCTX> macro.
349 Without C<IMAGER_NO_CONTEXT>, C<aIMCTX> is a call to
350 C<im_get_context()> which retrieves the context object for the current
353 There is no C<aIMCTX_> macro, any Imager function that can accept a
354 context parameter always accepts it.
358 This macro declares a variable of type L</im_context_t> that's
359 accessible via the C<aIMCTX> macro. This is intended for use as a
360 parameter declaration for functions:
373 Defines a local context variable and initializes it via
374 L<im_get_context()|Imager::APIRef/im_get_context()>.
378 Defines a local context variable and initializes it from the context
379 stored in an L<image object|/i_img>, eg:
388 Defines a local context variable and initializes it from the context
389 stored in an L<I/O object|/i_io_glue_t> object.
391 void f(i_io_glue_t *io) {
398 Defines a local context variable accessible via C<aIMCTX> in terms of
399 an expression you supply:
401 void f(my_object *p) {
402 dIMCTXctx(p->context);
406 This can be used to define your own local context macro:
408 #define dIMCTXmine(mine) ((mine)->context)
410 void f(my_object *p) {
415 =head1 Mutex Functions
417 Since some libraries are not thread safe, Imager's API includes some
418 simple mutex functions.
422 i_mutex_t m = i_mutex_new();
424 To control or lock the mutex:
428 To release or unlock the mutex:
432 To free any resources used by the mutex:
436 I most cases where you'd use these functions, your code would create
437 the mutex in your BOOT section, then lock and unlock the mutex as
438 needed to control access to the library.
445 To avoid abstracting the platform TLS and thread clean up handling,
446 Imager provides simple APIs for storing per-context information.
450 im_slot_t slot = im_context_slot_new(callback)
452 where callback is a (possibly NULL) function pointer called when the
453 context object is destroyed.
455 By default, the stored value for a slot is NULL, whether for a new
456 context or for a cloned context.
460 im_context_slot_set(aIMCTX, slot, somevalue);
462 where C<somevalue> can be represented as a C<void *>.
464 To retrieve the value:
466 value = im_context_slot_get(aIMCTX, slot);
470 Tony Cook <tonyc@cpan.org>
474 Imager, Imager::ExtUtils, Imager::APIRef, Imager::Inline