fix a dumb error in Imager::Probe
[imager.git] / imext.c
CommitLineData
92bda632
TC
1#include "imexttypes.h"
2#include "imager.h"
50c75381 3#include "imio.h"
92bda632 4
44d86483 5static im_context_t get_context(void);
eac41a26
TC
6static i_img *mathom_i_img_8_new(i_img_dim, i_img_dim, int);
7static i_img *mathom_i_img_16_new(i_img_dim, i_img_dim, int);
8static i_img *mathom_i_img_double_new(i_img_dim, i_img_dim, int);
9static i_img *mathom_i_img_pal_new(i_img_dim, i_img_dim, int, int);
10static void mathom_i_clear_error(void);
11static void mathom_i_push_error(int, const char *);
12static void mathom_i_push_errorvf(int, const char *, va_list);
13static int mathom_i_set_image_file_limits(i_img_dim, i_img_dim, size_t);
14static int mathom_i_get_image_file_limits(i_img_dim*, i_img_dim*, size_t*);
15static int
16mathom_i_int_check_image_file_limits(i_img_dim, i_img_dim, int, size_t);
17static i_img *mathom_i_img_alloc(void);
18static void mathom_i_img_init(i_img *);
19static i_io_glue_t *mathom_io_new_fd(int);
20static i_io_glue_t *mathom_io_new_bufchain(void);
21static i_io_glue_t *
22mathom_io_new_buffer(const char *data, size_t, i_io_closebufp_t, void *);
23static i_io_glue_t *
24mathom_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);
44d86483 26
92bda632
TC
27/*
28 DON'T ADD CASTS TO THESE
29*/
30im_ext_funcs imager_function_table =
31 {
d1f5892c
TC
32 IMAGER_API_VERSION,
33 IMAGER_API_LEVEL,
34
92bda632
TC
35 mymalloc,
36 myfree,
37 myrealloc,
38
e310e5f9
TC
39 mymalloc_file_line,
40 myfree_file_line,
41 myrealloc_file_line,
42
eac41a26
TC
43 mathom_i_img_8_new,
44 mathom_i_img_16_new,
45 mathom_i_img_double_new,
46 mathom_i_img_pal_new,
92bda632
TC
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
eac41a26
TC
83 mathom_i_clear_error,
84 mathom_i_push_error,
92bda632 85 i_push_errorf,
eac41a26 86 mathom_i_push_errorvf,
92bda632
TC
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,
2b405c9e
TC
122
123 /* IMAGER_API_LEVEL 2 functions */
eac41a26
TC
124 mathom_i_set_image_file_limits,
125 mathom_i_get_image_file_limits,
126 mathom_i_int_check_image_file_limits,
3efb0915
TC
127
128 i_flood_fill_border,
129 i_flood_cfill_border,
d5477d3d
TC
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,
bd8052a6
TC
138 i_loog,
139
140 /* IMAGER_API_LEVEL 4 functions */
eac41a26
TC
141 mathom_i_img_alloc,
142 mathom_i_img_init,
e5ee047b
TC
143
144 /* IMAGER_API_LEVEL 5 functions */
797a9f9c
TC
145 i_img_is_monochrome,
146 i_gsamp_bg,
147 i_gsampf_bg,
148 i_get_file_background,
718b8c97 149 i_get_file_backgroundf,
50c75381
TC
150 i_utf8_advance,
151 i_render_new,
152 i_render_delete,
153 i_render_color,
154 i_render_fill,
155 i_render_line,
6d5c85a2
TC
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,
eac41a26
TC
170 mathom_io_new_fd,
171 mathom_io_new_bufchain,
172 mathom_io_new_buffer,
173 mathom_io_new_cb,
6d5c85a2 174 io_slurp,
44d86483
TC
175 io_glue_destroy,
176
c9786b6c 177 /* level 8 */
eac41a26
TC
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,
c9786b6c 185 im_push_errorf,
eac41a26
TC
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,
c9786b6c
TC
196 im_lhead,
197 im_loog,
198 im_context_refinc,
8b30e240 199 im_context_refdec,
24c9233d
TC
200 im_errors,
201 i_mutex_new,
202 i_mutex_destroy,
203 i_mutex_lock,
fc02e376
TC
204 i_mutex_unlock,
205 im_context_slot_new,
206 im_context_slot_set,
0d80f37e
TC
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,
35db02fc
TC
213 i_poly_aa_cfill_m,
214
215 /* level 10 */
216 i_img_alpha_channel,
217 i_img_color_model,
218 i_img_color_channels
92bda632
TC
219 };
220
221/* in general these functions aren't called by Imager internally, but
222 only via the pointers above, since faster macros that call the
223 image vtable pointers are used.
224
225 () are used around the function names to prevent macro replacement
226 on the function names.
227*/
228
229/*
230=item i_ppix(im, x, y, color)
231
232=category Drawing
233
234Sets the pixel at (x,y) to I<color>.
235
236Returns 0 if the pixel was drawn, or -1 if not.
237
238Does no alpha blending, just copies the channels from the supplied
239color to the image.
240
241=cut
242*/
243
244int
8d14daab 245(i_ppix)(i_img *im, i_img_dim x, i_img_dim y, const i_color *val) {
92bda632
TC
246 return i_ppix(im, x, y, val);
247}
248
249/*
5715f7c3 250=item i_gpix(im, C<x>, C<y>, C<color>)
92bda632
TC
251
252=category Drawing
253
5715f7c3 254Retrieves the C<color> of the pixel (x,y).
92bda632
TC
255
256Returns 0 if the pixel was retrieved, or -1 if not.
257
258=cut
259*/
260
261int
8d14daab 262(i_gpix)(i_img *im,i_img_dim x,i_img_dim y,i_color *val) {
92bda632
TC
263 return i_gpix(im, x, y, val);
264}
265
266/*
5715f7c3 267=item i_ppixf(im, C<x>, C<y>, C<fcolor>)
92bda632
TC
268
269=category Drawing
270
5715f7c3 271Sets the pixel at (C<x>,C<y>) to the floating point color C<fcolor>.
92bda632
TC
272
273Returns 0 if the pixel was drawn, or -1 if not.
274
275Does no alpha blending, just copies the channels from the supplied
276color to the image.
277
278=cut
279*/
280int
8d14daab 281(i_ppixf)(i_img *im, i_img_dim x, i_img_dim y, const i_fcolor *val) {
92bda632
TC
282 return i_ppixf(im, x, y, val);
283}
284
285/*
5715f7c3 286=item i_gpixf(im, C<x>, C<y>, C<fcolor>)
92bda632
TC
287
288=category Drawing
289
290Retrieves the color of the pixel (x,y) as a floating point color into
5715f7c3 291C<fcolor>.
92bda632
TC
292
293Returns 0 if the pixel was retrieved, or -1 if not.
294
295=cut
296*/
297
298int
8d14daab 299(i_gpixf)(i_img *im,i_img_dim x,i_img_dim y,i_fcolor *val) {
92bda632
TC
300 return i_gpixf(im, x, y, val);
301}
302
303/*
304=item i_plin(im, l, r, y, colors)
305
306=category Drawing
307
308Sets (r-l) pixels starting from (l,y) using (r-l) values from
309I<colors>.
310
311Returns the number of pixels set.
312
313=cut
314*/
315
8d14daab
TC
316i_img_dim
317(i_plin)(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals) {
92bda632
TC
318 return i_plin(im, l, r, y, vals);
319}
320
321/*
322=item i_glin(im, l, r, y, colors)
323
324=category Drawing
325
326Retrieves (r-l) pixels starting from (l,y) into I<colors>.
327
328Returns the number of pixels retrieved.
329
330=cut
331*/
332
8d14daab
TC
333i_img_dim
334(i_glin)(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals) {
92bda632
TC
335 return i_glin(im, l, r, y, vals);
336}
337
338/*
5715f7c3 339=item i_plinf(im, C<left>, C<right>, C<fcolors>)
92bda632
TC
340
341=category Drawing
342
5715f7c3
TC
343Sets (right-left) pixels starting from (left,y) using (right-left)
344floating point colors from C<fcolors>.
92bda632
TC
345
346Returns the number of pixels set.
347
348=cut
349*/
350
8d14daab
TC
351i_img_dim
352(i_plinf)(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_fcolor *vals) {
92bda632
TC
353 return i_plinf(im, l, r, y, vals);
354}
355
356/*
357=item i_glinf(im, l, r, y, colors)
358
359=category Drawing
360
361Retrieves (r-l) pixels starting from (l,y) into I<colors> as floating
362point colors.
363
364Returns the number of pixels retrieved.
365
366=cut
367*/
368
8d14daab
TC
369i_img_dim
370(i_glinf)(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fcolor *vals) {
92bda632
TC
371 return i_glinf(im, l, r, y, vals);
372}
373
374/*
5715f7c3 375=item i_gsamp(im, left, right, y, samples, channels, channel_count)
92bda632
TC
376
377=category Drawing
378
5715f7c3
TC
379Reads sample values from C<im> for the horizontal line (left, y) to
380(right-1,y) for the channels specified by C<channels>, an array of int
381with C<channel_count> elements.
92bda632 382
5715f7c3 383If channels is NULL then the first channels_count channels are retrieved for
92bda632
TC
384each pixel.
385
5715f7c3
TC
386Returns the number of samples read (which should be (right-left) *
387channel_count)
92bda632
TC
388
389=cut
390*/
8d14daab
TC
391i_img_dim
392(i_gsamp)(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samp,
92bda632
TC
393 const int *chans, int chan_count) {
394 return i_gsamp(im, l, r, y, samp, chans, chan_count);
395}
396
397/*
5715f7c3 398=item i_gsampf(im, left, right, y, samples, channels, channel_count)
92bda632
TC
399
400=category Drawing
401
5715f7c3
TC
402Reads floating point sample values from C<im> for the horizontal line
403(left, y) to (right-1,y) for the channels specified by C<channels>, an
404array of int with channel_count elements.
92bda632 405
5715f7c3
TC
406If C<channels> is NULL then the first C<channel_count> channels are
407retrieved for each pixel.
92bda632 408
5715f7c3
TC
409Returns the number of samples read (which should be (C<right>-C<left>)
410* C<channel_count>)
92bda632
TC
411
412=cut
413*/
8d14daab
TC
414i_img_dim
415(i_gsampf)(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_fsample_t *samp,
92bda632
TC
416 const int *chans, int chan_count) {
417 return i_gsampf(im, l, r, y, samp, chans, chan_count);
418}
419
48095bd4
TC
420/*
421=item i_gsamp_bits(im, left, right, y, samples, channels, channel_count, bits)
422=category Drawing
423
424Reads integer samples scaled to C<bits> bits of precision into the
425C<unsigned int> array C<samples>.
426
427Expect this to be slow unless C<< bits == im->bits >>.
428
429Returns the number of samples copied, or -1 on error.
430
431Not all image types implement this method.
432
433Pushes errors, but does not call C<i_clear_error()>.
434
435=cut
436*/
437
438/*
439=item i_psamp_bits(im, left, right, y, samples, channels, channel_count, bits)
440=category Drawing
441
442Writes integer samples scaled to C<bits> bits of precision from the
443C<unsigned int> array C<samples>.
444
445Expect this to be slow unless C<< bits == im->bits >>.
446
447Returns the number of samples copied, or -1 on error.
448
449Not all image types implement this method.
450
451Pushes errors, but does not call C<i_clear_error()>.
452
453=cut
454*/
455
92bda632 456/*
5715f7c3 457=item i_gpal(im, left, right, y, indexes)
92bda632
TC
458
459=category Drawing
460
5715f7c3
TC
461Reads palette indexes for the horizontal line (left, y) to (right-1,
462y) into C<indexes>.
92bda632
TC
463
464Returns the number of indexes read.
465
466Always returns 0 for direct color images.
467
468=cut
469*/
8d14daab
TC
470i_img_dim
471(i_gpal)(i_img *im, i_img_dim x, i_img_dim r, i_img_dim y, i_palidx *vals) {
92bda632
TC
472 return i_gpal(im, x, r, y, vals);
473}
474
475/*
5715f7c3 476=item i_ppal(im, left, right, y, indexes)
92bda632
TC
477
478=category Drawing
479
5715f7c3
TC
480Writes palette indexes for the horizontal line (left, y) to (right-1,
481y) from C<indexes>.
92bda632
TC
482
483Returns the number of indexes written.
484
485Always returns 0 for direct color images.
486
487=cut
488*/
8d14daab
TC
489i_img_dim
490(i_ppal)(i_img *im, i_img_dim x, i_img_dim r, i_img_dim y, const i_palidx *vals) {
92bda632
TC
491 return i_ppal(im, x, r, y, vals);
492}
493
494/*
495=item i_addcolors(im, colors, count)
496
497=category Paletted images
498
499Adds colors to the image's palette.
500
501On success returns the index of the lowest color added.
502
503On failure returns -1.
504
505Always fails for direct color images.
506
507=cut
508*/
509
510int
97ac0a96 511(i_addcolors)(i_img *im, const i_color *colors, int count) {
92bda632
TC
512 return i_addcolors(im, colors, count);
513}
514
515/*
516=item i_getcolors(im, index, colors, count)
517
518=category Paletted images
519
520Retrieves I<count> colors starting from I<index> in the image's
521palette.
522
523On success stores the colors into I<colors> and returns true.
524
525On failure returns false.
526
527Always fails for direct color images.
528
529Fails if there are less than I<index>+I<count> colors in the image's
530palette.
531
532=cut
533*/
534
535int
536(i_getcolors)(i_img *im, int i, i_color *colors, int count) {
537 return i_getcolors(im, i, colors, count);
538}
539
540/*
541=item i_colorcount(im)
542
543=category Paletted images
544
545Returns the number of colors in the image's palette.
546
547Returns -1 for direct images.
548
549=cut
550*/
551
552int
553(i_colorcount)(i_img *im) {
554 return i_colorcount(im);
555}
556
557/*
558=item i_maxcolors(im)
559
560=category Paletted images
561
562Returns the maximum number of colors the palette can hold for the
563image.
564
565Returns -1 for direct color images.
566
567=cut
568*/
569
570int
571(i_maxcolors)(i_img *im) {
572 return i_maxcolors(im);
573}
574
575/*
576=item i_findcolor(im, color, &entry)
577
578=category Paletted images
579
580Searches the images palette for the given color.
581
582On success sets *I<entry> to the index of the color, and returns true.
583
584On failure returns false.
585
586Always fails on direct color images.
587
588=cut
589*/
590int
97ac0a96 591(i_findcolor)(i_img *im, const i_color *color, i_palidx *entry) {
92bda632
TC
592 return i_findcolor(im, color, entry);
593}
594
595/*
596=item i_setcolors(im, index, colors, count)
597
598=category Paletted images
599
600Sets I<count> colors starting from I<index> in the image's palette.
601
5715f7c3 602On success returns true.
92bda632
TC
603
604On failure returns false.
605
606The image must have at least I<index>+I<count> colors in it's palette
607for this to succeed.
608
609Always fails on direct color images.
610
611=cut
612*/
613int
97ac0a96 614(i_setcolors)(i_img *im, int index, const i_color *colors, int count) {
92bda632
TC
615 return i_setcolors(im, index, colors, count);
616}
617
44d86483
TC
618/*
619=item im_get_context()
620
621Retrieve the context object for the current thread.
622
623Inside Imager itself this is just a function pointer, which the
abffffed 624F<Imager.xs> BOOT handler initializes for use within perl. If you're
44d86483
TC
625taking the Imager code and embedding it elsewhere you need to
626initialize the C<im_get_context> pointer at some point.
627
628=cut
629*/
630
631static im_context_t
632get_context(void) {
633 return im_get_context();
634}
eac41a26
TC
635
636static i_img *
637mathom_i_img_8_new(i_img_dim xsize, i_img_dim ysize, int channels) {
638 return i_img_8_new(xsize, ysize, channels);
639}
640
641static i_img *
642mathom_i_img_16_new(i_img_dim xsize, i_img_dim ysize, int channels) {
643 return i_img_16_new(xsize, ysize, channels);
644}
645
646static i_img *
647mathom_i_img_double_new(i_img_dim xsize, i_img_dim ysize, int channels) {
648 return i_img_double_new(xsize, ysize, channels);
649}
650
651static i_img *
652mathom_i_img_pal_new(i_img_dim xsize, i_img_dim ysize, int channels,
653 int maxpal) {
654 return i_img_pal_new(xsize, ysize, channels, maxpal);
655}
656
657static void
658mathom_i_clear_error(void) {
659 i_clear_error();
660}
661
662static void
663mathom_i_push_error(int code, const char *msg) {
664 i_push_error(code, msg);
665}
666
667static void
668mathom_i_push_errorvf(int code, const char *fmt, va_list args) {
669 i_push_errorvf(code, fmt, args);
670}
671
672static int
673mathom_i_set_image_file_limits(i_img_dim max_width, i_img_dim max_height,
674 size_t max_bytes) {
675 return i_set_image_file_limits(max_width, max_height, max_bytes);
676}
677
678static int
679mathom_i_get_image_file_limits(i_img_dim *pmax_width, i_img_dim *pmax_height,
680 size_t *pmax_bytes) {
681 return i_get_image_file_limits(pmax_width, pmax_height, pmax_bytes);
682}
683
684static int
685mathom_i_int_check_image_file_limits(i_img_dim width, i_img_dim height,
686 int channels, size_t sample_size) {
687 return i_int_check_image_file_limits(width, height, channels, sample_size);
688}
689
690static i_img *
691mathom_i_img_alloc(void) {
692 return i_img_alloc();
693}
694
695static void
696mathom_i_img_init(i_img *im) {
697 i_img_init(im);
698}
699
700static i_io_glue_t *
701mathom_io_new_fd(int fd) {
702 return io_new_fd(fd);
703}
704static i_io_glue_t *
705mathom_io_new_bufchain(void) {
706 return io_new_bufchain();
707}
708
709static i_io_glue_t *
710mathom_io_new_buffer(const char *data, size_t size, i_io_closebufp_t closefp,
711 void *close_data) {
712 return io_new_buffer(data, size, closefp, close_data);
713}
714
715static i_io_glue_t *
716mathom_io_new_cb(void *p, i_io_readl_t readcb, i_io_writel_t writecb,
717 i_io_seekl_t seekcb, i_io_closel_t closecb,
718 i_io_destroyl_t destroycb) {
719 return io_new_cb(p, readcb, writecb, seekcb, closecb, destroycb);
720}