]> git.imager.perl.org - imager.git/blob - lib/Imager/API.pod
support libbase as an arrayref for fake probing
[imager.git] / lib / Imager / API.pod
1 =head1 NAME
2
3 Imager::API - Imager's C API - introduction.
4
5 =head1 SYNOPSIS
6
7   #include "imext.h"
8   #include "imperl.h"
9
10   DEFINE_IMAGER_CALLBACKS;
11
12   MODULE = Your::Module  PACKAGE = Your::Module
13
14   ...
15
16   BOOT:
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");
21   
22
23 =head1 DESCRIPTION
24
25 =for stopwords XS
26
27 The API allows you to access Imager functions at the C level from XS
28 and from C<Inline::C>.
29
30 The intent is to allow users to:
31
32 =over
33
34 =item *
35
36 write C code that does Imager operations the user might do from Perl,
37 but faster, for example, the L<Imager::CountColor> example.
38
39 =item *
40
41 write C code that implements an application specific version of some
42 core Imager object, for example, Imager::SDL.
43
44 =item *
45
46 write C code that hooks into Imager's existing methods, such as filter
47 or file format handlers.
48
49 =back
50
51 See L<Imager::Inline> for information on using Imager's Inline::C
52 support.
53
54 =head1 Beware
55
56 =over
57
58 =item *
59
60 don't return an object you received as a parameter - this will cause
61 the object to be freed twice.
62
63 =back
64
65 =head1 Types
66
67 The API makes the following types visible:
68
69 =over
70
71 =item *
72
73 L</i_img> - used to represent an image
74
75 =item *
76
77 L</i_color> - used to represent a color with up
78 to 8 bits per sample.
79
80 =item *
81
82 L</i_fcolor> - used to represent
83 a color with a double per sample.
84
85 =item *
86
87 L</i_fill_t> - fill objects>> - an abstract fill
88
89 =item *
90
91 L</im_context_t> - Imager's per-thread state.
92
93 =back
94
95 At this point there is no consolidated font object type, and hence the
96 font functions are not visible through Imager's API.
97
98 =head2 i_img
99
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
104 operations.
105
106 The only time you should directly write to any value in this type is
107 if you're implementing your own image type.
108
109 The typemap includes type names Imager and Imager::ImgRaw as typedefs
110 for C<i_img *>.
111
112 For incoming parameters the typemap will accept either Imager or
113 Imager::ImgRaw objects.
114
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
117 type.
118
119 =head2 i_color
120
121 Represents an 8-bit per sample color.  This is a union containing
122 several different structs for access to components of a color:
123
124 =over
125
126 =item *
127
128 C<gray> - single member C<gray_color>.
129
130 =item *
131
132 C<rgb> - C<r>, C<g>, C<b> members.
133
134 =item *
135
136 C<rgba> - C<r>, C<g>, C<b>, C<a> members.
137
138 =item *
139
140 C<channels> - array of channels.
141
142 =back
143
144 Use C<Imager::Color> for parameter and return value types.
145
146 =head2 i_fcolor
147
148 Similar to C<i_color> except that each component is a double instead of
149 an unsigned char.
150
151 Use Imager::Color::Float for parameter and return value types.
152
153 =head2 i_fill_t
154
155 Abstract type containing pointers called to perform low level fill
156 operations.
157
158 Unless you're defining your own fill objects you should treat this as
159 an opaque type.
160
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
163 Imager::Fill object.
164
165 =head2 i_io_glue_t
166
167 C<i_io_glue_t> is Imager's I/O abstraction.
168
169 Historically named C<io_glue>, and this name is available for backward
170 compatibility.
171
172 =head2 im_context_t
173
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.
177
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
180 type back to perl.
181
182 See L</Context objects> for more information.
183
184 =head2 i_polygon_t
185
186 Represents a single polygon supplied to i_poly_poly_aa() and
187 i_poly_poly_aa_cfill().
188
189 This is a structure with 3 members:
190
191 =over
192
193 =item *
194
195 C<x>, C<y> - pointers to the first elements of arrays of doubles that define
196 the vertices of the polygon.
197
198 =item *
199
200 C<count> - the number of values in each of the C<x> and C<y> arrays.
201
202 =back
203
204 =head2 i_poly_fill_mode_t
205
206 An enumerated type of the possible fill modes for polygons:
207
208 =over
209
210 =item *
211
212 C<i_pfm_evenodd> - if areas overlap an odd number of times, they
213 are filled, and are otherwise unfilled.
214
215 =item *
216
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.
220
221 =back
222
223 =head1 Create an XS module using the Imager API
224
225 =head2 Foo.pm
226
227 Load Imager:
228
229   use Imager 0.48;
230
231 and bootstrap your XS code - see L<XSLoader> or L<DynaLoader>.
232
233 =head2 C<Foo.xs>
234
235 You'll need the following in your XS source:
236
237 =over
238
239 =item *
240
241 include the Imager external API header, and the perl interface header:
242
243   #include "imext.h"
244   #include "imperl.h"
245
246 =item *
247
248 create the variables used to hold the callback table:
249
250   DEFINE_IMAGER_CALLBACKS;
251
252 =item *
253
254 initialize the callback table in your C<BOOT> code:
255
256   BOOT:
257     PERL_INITIALIZE_IMAGER_CALLBACKS;
258
259 From Imager 0.91 you can supply your module name to improve error
260 reporting:
261
262   BOOT:
263     PERL_INITIALIZE_IMAGER_CALLBACKS_NAME("My::Module");
264
265 =back
266
267 =head2 foo.c
268
269 In any other source files where you want to access the Imager API,
270 you'll need to:
271
272 =over
273
274 =item *
275
276 include the Imager external API header:
277
278   #include "imext.h"
279
280 =back
281
282 =head2 C<Makefile.PL>
283
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:
286
287 =over
288
289 =item *
290
291 C<use Imager::ExtUtils;>
292
293 =item *
294
295 include Imager's include directory in INC:
296
297   INC => Imager::ExtUtils->includes
298
299 =item *
300
301 use Imager's typemap:
302
303   TYPEMAPS => [ Imager::ExtUtils->typemap ]
304
305 =item *
306
307 include Imager 0.48 as a PREREQ_PM:
308
309    PREREQ_PM =>
310    {
311     Imager => 0.48,
312    },
313
314 =item *
315
316 Since you use Imager::ExtUtils in C<Makefile.PL> (or C<Build.PL>) you
317 should include Imager in your configure_requires:
318
319    META_MERGE =>
320    {
321      configure_requires => { Imager => "0.48" }
322    },
323
324 =back
325
326 =head1 Context objects
327
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.
331
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.
335
336 Some operations will be slightly slower, these include:
337
338 =over
339
340 =item *
341
342 creating an image
343
344 =item *
345
346 reporting errors
347
348 =item *
349
350 creating I/O objects
351
352 =item *
353
354 setting/getting/testing image file limits
355
356 =item *
357
358 logging
359
360 =back
361
362 You can avoid this fairly minor overhead by adding a C<#define>:
363
364   #define IMAGER_NO_CONTEXT
365
366 before including any Imager header files, but you will need to manage
367 context objects yourself.
368
369 Some functions and macros that are available without
370 C<IMAGER_NO_CONTEXT> are not available with it defined, these are:
371
372 =over
373
374 =item *
375
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.
379
380 =back
381
382 =head2 C<aIMCTX>
383
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.
387
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
390 thread.
391
392 There is no C<aIMCTX_> macro, any Imager function that can accept a
393 context parameter always accepts it.
394
395 =head2 C<pIMCTX>
396
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:
400
401   void f(pIMCTX) {
402     ... use aIMCTX here
403   }
404
405   void g(...) {
406     ...
407     f(aIMCTX);
408   }
409
410 =head2 C<dIMCTX>
411
412 Defines a local context variable and initializes it via
413 L<im_get_context()|Imager::APIRef/im_get_context()>.
414
415 =head2 C<dIMCTXim>
416
417 Defines a local context variable and initializes it from the context
418 stored in an L<image object|/i_img>, eg:
419
420   void f(i_img *im) {
421     dIMCTXim(im);
422     ...
423   }
424
425 =head2 C<dIMCTXio>
426
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.
429
430   void f(i_io_glue_t *io) {
431     dIMCTXio(io);
432     ...
433   }
434
435 =head2 C<dIMCTXctx>
436
437 Defines a local context variable accessible via C<aIMCTX> in terms of
438 an expression you supply:
439
440   void f(my_object *p) {
441     dIMCTXctx(p->context);
442     ...
443   }
444
445 This can be used to define your own local context macro:
446
447   #define dIMCTXmine(mine) ((mine)->context)
448
449   void f(my_object *p) {
450     dIMCTXmine(p);
451     ...
452   }
453
454 =head1 Mutex Functions
455
456 Since some libraries are not thread safe, Imager's API includes some
457 simple mutex functions.
458
459 To create a mutex:
460
461   i_mutex_t m = i_mutex_new();
462
463 To control or lock the mutex:
464
465   i_mutex_lock(m);
466
467 To release or unlock the mutex:
468
469   i_mutex_unlock(m);
470
471 To free any resources used by the mutex:
472
473   i_mutex_destroy(m);
474
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.
478
479 =head1 Context slots
480
481 =for stopwords
482 TLS APIs
483
484 To avoid abstracting the platform TLS and thread clean up handling,
485 Imager provides simple APIs for storing per-context information.
486
487 To allocate a slot:
488
489   im_slot_t slot = im_context_slot_new(callback)
490
491 where callback is a (possibly NULL) function pointer called when the
492 context object is destroyed.
493
494 By default, the stored value for a slot is NULL, whether for a new
495 context or for a cloned context.
496
497 To store a value:
498
499   im_context_slot_set(aIMCTX, slot, somevalue);
500
501 where C<somevalue> can be represented as a C<void *>.
502
503 To retrieve the value:
504
505   value = im_context_slot_get(aIMCTX, slot);
506
507 =head1 AUTHOR
508
509 Tony Cook <tonyc@cpan.org>
510
511 =head1 SEE ALSO
512
513 Imager, Imager::ExtUtils, Imager::APIRef, Imager::Inline
514
515 =cut