improved polygon support
[imager.git] / imext.c
1 #include "imexttypes.h"
2 #include "imager.h"
3 #include "imio.h"
4
5 static im_context_t get_context(void);
6 static i_img *mathom_i_img_8_new(i_img_dim, i_img_dim, int);
7 static i_img *mathom_i_img_16_new(i_img_dim, i_img_dim, int);
8 static i_img *mathom_i_img_double_new(i_img_dim, i_img_dim, int);
9 static i_img *mathom_i_img_pal_new(i_img_dim, i_img_dim, int, int);
10 static void mathom_i_clear_error(void);
11 static void mathom_i_push_error(int, const char *);
12 static void mathom_i_push_errorvf(int, const char *, va_list);
13 static int mathom_i_set_image_file_limits(i_img_dim, i_img_dim, size_t);
14 static int mathom_i_get_image_file_limits(i_img_dim*, i_img_dim*, size_t*);
15 static int
16 mathom_i_int_check_image_file_limits(i_img_dim, i_img_dim, int, size_t);
17 static i_img *mathom_i_img_alloc(void);
18 static void mathom_i_img_init(i_img *);
19 static i_io_glue_t *mathom_io_new_fd(int);
20 static i_io_glue_t *mathom_io_new_bufchain(void);
21 static i_io_glue_t *
22 mathom_io_new_buffer(const char *data, size_t, i_io_closebufp_t, void *);
23 static i_io_glue_t *
24 mathom_io_new_cb(void *, i_io_readl_t, i_io_writel_t, i_io_seekl_t,
25                  i_io_closel_t, i_io_destroyl_t);
26
27 /*
28  DON'T ADD CASTS TO THESE
29 */
30 im_ext_funcs imager_function_table =
31   {
32     IMAGER_API_VERSION,
33     IMAGER_API_LEVEL,
34
35     mymalloc,
36     myfree,
37     myrealloc,
38
39     mymalloc_file_line,
40     myfree_file_line,
41     myrealloc_file_line,
42
43     mathom_i_img_8_new,
44     mathom_i_img_16_new,
45     mathom_i_img_double_new,
46     mathom_i_img_pal_new,
47     i_img_destroy,
48     i_sametype,
49     i_sametype_chans,
50     i_img_info,
51
52     i_ppix,
53     i_gpix,
54     i_ppixf,
55     i_gpixf,
56     i_plin,
57     i_glin,
58     i_plinf,
59     i_glinf,
60     i_gsamp,
61     i_gsampf,
62     i_gpal,
63     i_ppal,
64     i_addcolors,
65     i_getcolors,
66     i_colorcount,
67     i_maxcolors,
68     i_findcolor,
69     i_setcolors,
70
71     i_new_fill_solid,
72     i_new_fill_solidf,
73     i_new_fill_hatch,
74     i_new_fill_hatchf,
75     i_new_fill_image,
76     i_new_fill_fount,
77     i_fill_destroy,
78
79     i_quant_makemap,
80     i_quant_translate,
81     i_quant_transparent,
82
83     mathom_i_clear_error,
84     mathom_i_push_error,
85     i_push_errorf,
86     mathom_i_push_errorvf,
87
88     i_tags_new,
89     i_tags_set,
90     i_tags_setn,
91     i_tags_destroy,
92     i_tags_find,
93     i_tags_findn,
94     i_tags_delete,
95     i_tags_delbyname,
96     i_tags_delbycode,
97     i_tags_get_float,
98     i_tags_set_float,
99     i_tags_set_float2,
100     i_tags_get_int,
101     i_tags_get_string,
102     i_tags_get_color,
103     i_tags_set_color,
104
105     i_box,
106     i_box_filled,
107     i_box_cfill,
108     i_line,
109     i_line_aa,
110     i_arc,
111     i_arc_aa,
112     i_arc_cfill,
113     i_arc_aa_cfill,
114     i_circle_aa,
115     i_flood_fill,
116     i_flood_cfill,
117
118     i_copyto,
119     i_copyto_trans,
120     i_copy,
121     i_rubthru,
122
123     /* IMAGER_API_LEVEL 2 functions */
124     mathom_i_set_image_file_limits,
125     mathom_i_get_image_file_limits,
126     mathom_i_int_check_image_file_limits,
127
128     i_flood_fill_border,
129     i_flood_cfill_border,
130
131     /* IMAGER_API_LEVEL 3 functions */
132     i_img_setmask,
133     i_img_getmask,
134     i_img_getchannels,
135     i_img_get_width,
136     i_img_get_height,
137     i_lhead,
138     i_loog,
139
140     /* IMAGER_API_LEVEL 4 functions */
141     mathom_i_img_alloc,
142     mathom_i_img_init,
143
144     /* IMAGER_API_LEVEL 5 functions */
145     i_img_is_monochrome,
146     i_gsamp_bg,
147     i_gsampf_bg,
148     i_get_file_background,
149     i_get_file_backgroundf,
150     i_utf8_advance,
151     i_render_new,
152     i_render_delete,
153     i_render_color,
154     i_render_fill,
155     i_render_line,
156     i_render_linef,
157
158     /* level 6 */
159     i_io_getc_imp,
160     i_io_peekc_imp,
161     i_io_peekn,
162     i_io_putc_imp,
163     i_io_read,
164     i_io_write,
165     i_io_seek,
166     i_io_flush,
167     i_io_close,
168     i_io_set_buffered,
169     i_io_gets,
170     mathom_io_new_fd,
171     mathom_io_new_bufchain,
172     mathom_io_new_buffer,
173     mathom_io_new_cb,
174     io_slurp,
175     io_glue_destroy,
176
177     /* level 8 */
178     im_img_8_new,
179     im_img_16_new,
180     im_img_double_new,
181     im_img_pal_new,
182     im_clear_error,
183     im_push_error,
184     im_push_errorvf,
185     im_push_errorf,
186     im_set_image_file_limits,
187     im_get_image_file_limits,
188     im_int_check_image_file_limits,
189     im_img_alloc,
190     im_img_init,
191     im_io_new_fd,
192     im_io_new_bufchain,
193     im_io_new_buffer,
194     im_io_new_cb,
195     get_context,
196     im_lhead,
197     im_loog,
198     im_context_refinc,
199     im_context_refdec,
200     im_errors,
201     i_mutex_new,
202     i_mutex_destroy,
203     i_mutex_lock,
204     i_mutex_unlock,
205     im_context_slot_new,
206     im_context_slot_set,
207     im_context_slot_get,
208
209     /* level 9 */
210     i_poly_poly_aa,
211     i_poly_poly_aa_cfill,
212     i_poly_aa_m,
213     i_poly_aa_cfill_m
214   };
215
216 /* in general these functions aren't called by Imager internally, but
217    only via the pointers above, since faster macros that call the
218    image vtable pointers are used.
219
220    () are used around the function names to prevent macro replacement
221    on the function names.
222 */
223
224 /*
225 =item i_ppix(im, x, y, color)
226
227 =category Drawing
228
229 Sets the pixel at (x,y) to I<color>.
230
231 Returns 0 if the pixel was drawn, or -1 if not.
232
233 Does no alpha blending, just copies the channels from the supplied
234 color to the image.
235
236 =cut
237 */
238
239 int 
240 (i_ppix)(i_img *im, i_img_dim x, i_img_dim y, const i_color *val) {
241   return i_ppix(im, x, y, val);
242 }
243
244 /*
245 =item i_gpix(im, C<x>, C<y>, C<color>)
246
247 =category Drawing
248
249 Retrieves the C<color> of the pixel (x,y).
250
251 Returns 0 if the pixel was retrieved, or -1 if not.
252
253 =cut
254 */
255
256 int
257 (i_gpix)(i_img *im,i_img_dim x,i_img_dim y,i_color *val) {
258   return i_gpix(im, x, y, val);
259 }
260
261 /*
262 =item i_ppixf(im, C<x>, C<y>, C<fcolor>)
263
264 =category Drawing
265
266 Sets the pixel at (C<x>,C<y>) to the floating point color C<fcolor>.
267
268 Returns 0 if the pixel was drawn, or -1 if not.
269
270 Does no alpha blending, just copies the channels from the supplied
271 color to the image.
272
273 =cut
274 */
275 int
276 (i_ppixf)(i_img *im, i_img_dim x, i_img_dim y, const i_fcolor *val) {
277   return i_ppixf(im, x, y, val);
278 }
279
280 /*
281 =item i_gpixf(im, C<x>, C<y>, C<fcolor>)
282
283 =category Drawing
284
285 Retrieves the color of the pixel (x,y) as a floating point color into
286 C<fcolor>.
287
288 Returns 0 if the pixel was retrieved, or -1 if not.
289
290 =cut
291 */
292
293 int
294 (i_gpixf)(i_img *im,i_img_dim x,i_img_dim y,i_fcolor *val) {
295   return i_gpixf(im, x, y, val);
296 }
297
298 /*
299 =item i_plin(im, l, r, y, colors)
300
301 =category Drawing
302
303 Sets (r-l) pixels starting from (l,y) using (r-l) values from
304 I<colors>.
305
306 Returns the number of pixels set.
307
308 =cut
309 */
310
311 i_img_dim
312 (i_plin)(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals) {
313   return i_plin(im, l, r, y, vals);
314 }
315
316 /*
317 =item i_glin(im, l, r, y, colors)
318
319 =category Drawing
320
321 Retrieves (r-l) pixels starting from (l,y) into I<colors>.
322
323 Returns the number of pixels retrieved.
324
325 =cut
326 */
327
328 i_img_dim
329 (i_glin)(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals) {
330   return i_glin(im, l, r, y, vals);
331 }
332
333 /*
334 =item i_plinf(im, C<left>, C<right>, C<fcolors>)
335
336 =category Drawing
337
338 Sets (right-left) pixels starting from (left,y) using (right-left)
339 floating point colors from C<fcolors>.
340
341 Returns the number of pixels set.
342
343 =cut
344 */
345
346 i_img_dim
347 (i_plinf)(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_fcolor *vals) {
348   return i_plinf(im, l, r, y, vals);
349 }
350
351 /*
352 =item i_glinf(im, l, r, y, colors)
353
354 =category Drawing
355
356 Retrieves (r-l) pixels starting from (l,y) into I<colors> as floating
357 point colors.
358
359 Returns the number of pixels retrieved.
360
361 =cut
362 */
363
364 i_img_dim
365 (i_glinf)(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fcolor *vals) {
366   return i_glinf(im, l, r, y, vals);
367 }
368
369 /*
370 =item i_gsamp(im, left, right, y, samples, channels, channel_count)
371
372 =category Drawing
373
374 Reads sample values from C<im> for the horizontal line (left, y) to
375 (right-1,y) for the channels specified by C<channels>, an array of int
376 with C<channel_count> elements.
377
378 If channels is NULL then the first channels_count channels are retrieved for
379 each pixel.
380
381 Returns the number of samples read (which should be (right-left) *
382 channel_count)
383
384 =cut
385 */
386 i_img_dim
387 (i_gsamp)(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samp, 
388                    const int *chans, int chan_count) {
389   return i_gsamp(im, l, r, y, samp, chans, chan_count);
390 }
391
392 /*
393 =item i_gsampf(im, left, right, y, samples, channels, channel_count)
394
395 =category Drawing
396
397 Reads floating point sample values from C<im> for the horizontal line
398 (left, y) to (right-1,y) for the channels specified by C<channels>, an
399 array of int with channel_count elements.
400
401 If C<channels> is NULL then the first C<channel_count> channels are
402 retrieved for each pixel.
403
404 Returns the number of samples read (which should be (C<right>-C<left>)
405 * C<channel_count>)
406
407 =cut
408 */
409 i_img_dim
410 (i_gsampf)(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fsample_t *samp, 
411            const int *chans, int chan_count) {
412   return i_gsampf(im, l, r, y, samp, chans, chan_count);
413 }
414
415 /*
416 =item i_gsamp_bits(im, left, right, y, samples, channels, channel_count, bits)
417 =category Drawing
418
419 Reads integer samples scaled to C<bits> bits of precision into the
420 C<unsigned int> array C<samples>.
421
422 Expect this to be slow unless C<< bits == im->bits >>.
423
424 Returns the number of samples copied, or -1 on error.
425
426 Not all image types implement this method.
427
428 Pushes errors, but does not call C<i_clear_error()>.
429
430 =cut
431 */
432
433 /*
434 =item i_psamp_bits(im, left, right, y, samples, channels, channel_count, bits)
435 =category Drawing
436
437 Writes integer samples scaled to C<bits> bits of precision from the
438 C<unsigned int> array C<samples>.
439
440 Expect this to be slow unless C<< bits == im->bits >>.
441
442 Returns the number of samples copied, or -1 on error.
443
444 Not all image types implement this method.
445
446 Pushes errors, but does not call C<i_clear_error()>.
447
448 =cut
449 */
450
451 /*
452 =item i_gpal(im, left, right, y, indexes)
453
454 =category Drawing
455
456 Reads palette indexes for the horizontal line (left, y) to (right-1,
457 y) into C<indexes>.
458
459 Returns the number of indexes read.
460
461 Always returns 0 for direct color images.
462
463 =cut
464 */
465 i_img_dim
466 (i_gpal)(i_img *im, i_img_dim x, i_img_dim r, i_img_dim y, i_palidx *vals) {
467   return i_gpal(im, x, r, y, vals);
468 }
469
470 /*
471 =item i_ppal(im, left, right, y, indexes)
472
473 =category Drawing
474
475 Writes palette indexes for the horizontal line (left, y) to (right-1,
476 y) from C<indexes>.
477
478 Returns the number of indexes written.
479
480 Always returns 0 for direct color images.
481
482 =cut
483 */
484 i_img_dim
485 (i_ppal)(i_img *im, i_img_dim x, i_img_dim r, i_img_dim y, const i_palidx *vals) {
486   return i_ppal(im, x, r, y, vals);
487 }
488
489 /*
490 =item i_addcolors(im, colors, count)
491
492 =category Paletted images
493
494 Adds colors to the image's palette.
495
496 On success returns the index of the lowest color added.
497
498 On failure returns -1.
499
500 Always fails for direct color images.
501
502 =cut
503 */
504
505 int
506 (i_addcolors)(i_img *im, const i_color *colors, int count) {
507   return i_addcolors(im, colors, count);
508 }
509
510 /*
511 =item i_getcolors(im, index, colors, count)
512
513 =category Paletted images
514
515 Retrieves I<count> colors starting from I<index> in the image's
516 palette.
517
518 On success stores the colors into I<colors> and returns true.
519
520 On failure returns false.
521
522 Always fails for direct color images.
523
524 Fails if there are less than I<index>+I<count> colors in the image's
525 palette.
526
527 =cut
528 */
529
530 int
531 (i_getcolors)(i_img *im, int i, i_color *colors, int count) {
532   return i_getcolors(im, i, colors, count);
533 }
534
535 /*
536 =item i_colorcount(im)
537
538 =category Paletted images
539
540 Returns the number of colors in the image's palette.
541
542 Returns -1 for direct images.
543
544 =cut
545 */
546
547 int
548 (i_colorcount)(i_img *im) {
549   return i_colorcount(im);
550 }
551
552 /*
553 =item i_maxcolors(im)
554
555 =category Paletted images
556
557 Returns the maximum number of colors the palette can hold for the
558 image.
559
560 Returns -1 for direct color images.
561
562 =cut
563 */
564
565 int
566 (i_maxcolors)(i_img *im) {
567   return i_maxcolors(im);
568 }
569
570 /*
571 =item i_findcolor(im, color, &entry)
572
573 =category Paletted images
574
575 Searches the images palette for the given color.
576
577 On success sets *I<entry> to the index of the color, and returns true.
578
579 On failure returns false.
580
581 Always fails on direct color images.
582
583 =cut
584 */
585 int
586 (i_findcolor)(i_img *im, const i_color *color, i_palidx *entry) {
587   return i_findcolor(im, color, entry);
588 }
589
590 /*
591 =item i_setcolors(im, index, colors, count)
592
593 =category Paletted images
594
595 Sets I<count> colors starting from I<index> in the image's palette.
596
597 On success returns true.
598
599 On failure returns false.
600
601 The image must have at least I<index>+I<count> colors in it's palette
602 for this to succeed.
603
604 Always fails on direct color images.
605
606 =cut
607 */
608 int
609 (i_setcolors)(i_img *im, int index, const i_color *colors, int count) {
610   return i_setcolors(im, index, colors, count);
611 }
612
613 /*
614 =item im_get_context()
615
616 Retrieve the context object for the current thread.
617
618 Inside Imager itself this is just a function pointer, which the
619 F<Imager.xs> BOOT handler initializes for use within perl.  If you're
620 taking the Imager code and embedding it elsewhere you need to
621 initialize the C<im_get_context> pointer at some point.
622
623 =cut
624 */
625
626 static im_context_t
627 get_context(void) {
628   return im_get_context();
629 }
630
631 static i_img *
632 mathom_i_img_8_new(i_img_dim xsize, i_img_dim ysize, int channels) {
633   return i_img_8_new(xsize, ysize, channels);
634 }
635
636 static i_img *
637 mathom_i_img_16_new(i_img_dim xsize, i_img_dim ysize, int channels) {
638   return i_img_16_new(xsize, ysize, channels);
639 }
640
641 static i_img *
642 mathom_i_img_double_new(i_img_dim xsize, i_img_dim ysize, int channels) {
643   return i_img_double_new(xsize, ysize, channels);
644 }
645
646 static i_img *
647 mathom_i_img_pal_new(i_img_dim xsize, i_img_dim ysize, int channels,
648                      int maxpal) {
649   return i_img_pal_new(xsize, ysize, channels, maxpal);
650 }
651
652 static void
653 mathom_i_clear_error(void) {
654   i_clear_error();
655 }
656
657 static void
658 mathom_i_push_error(int code, const char *msg) {
659   i_push_error(code, msg);
660 }
661
662 static void
663 mathom_i_push_errorvf(int code, const char *fmt, va_list args) {
664   i_push_errorvf(code, fmt, args);
665 }
666
667 static int
668 mathom_i_set_image_file_limits(i_img_dim max_width, i_img_dim max_height,
669                                size_t max_bytes) {
670   return i_set_image_file_limits(max_width, max_height, max_bytes);
671 }
672
673 static int
674 mathom_i_get_image_file_limits(i_img_dim *pmax_width, i_img_dim *pmax_height,
675                                size_t *pmax_bytes) {
676   return i_get_image_file_limits(pmax_width, pmax_height, pmax_bytes);
677 }
678
679 static int
680 mathom_i_int_check_image_file_limits(i_img_dim width, i_img_dim height,
681                                      int channels, size_t sample_size) {
682   return i_int_check_image_file_limits(width, height, channels, sample_size);
683 }
684
685 static i_img *
686 mathom_i_img_alloc(void) {
687   return i_img_alloc();
688 }
689
690 static void
691 mathom_i_img_init(i_img *im) {
692   i_img_init(im);
693 }
694
695 static i_io_glue_t *
696 mathom_io_new_fd(int fd) {
697   return io_new_fd(fd);
698 }
699 static i_io_glue_t *
700 mathom_io_new_bufchain(void) {
701   return io_new_bufchain();
702 }
703
704 static i_io_glue_t *
705 mathom_io_new_buffer(const char *data, size_t size, i_io_closebufp_t closefp,
706                      void *close_data) {
707   return io_new_buffer(data, size, closefp, close_data);
708 }
709
710 static i_io_glue_t *
711 mathom_io_new_cb(void *p, i_io_readl_t readcb, i_io_writel_t writecb,
712                  i_io_seekl_t seekcb, i_io_closel_t closecb,
713                  i_io_destroyl_t destroycb) {
714   return io_new_cb(p, readcb, writecb, seekcb, closecb, destroycb);
715 }