flood_fill() wouldn't fill the right side of a single scan-line fill area.
[imager.git] / imdatatypes.h
CommitLineData
02d1d628
AMH
1#ifndef _DATATYPES_H_
2#define _DATATYPES_H_
3
92bda632 4#include "imconfig.h"
d5477d3d 5#include "imio.h"
02d1d628
AMH
6
7#define MAXCHANNELS 4
8
faa9b3e7
TC
9/* used for palette indices in some internal code (which might be
10 exposed at some point
11*/
12typedef unsigned char i_palidx;
13
14/* We handle 2 types of sample, this is hopefully the most common, and the
15 smaller of the ones we support */
16typedef unsigned char i_sample_t;
17
18typedef struct { i_sample_t gray_color; } gray_color;
19typedef struct { i_sample_t r,g,b; } rgb_color;
20typedef struct { i_sample_t r,g,b,a; } rgba_color;
21typedef struct { i_sample_t c,m,y,k; } cmyk_color;
02d1d628
AMH
22
23typedef int undef_int; /* special value to put in typemaps to retun undef on 0 and 1 on 1 */
24
6cfee9d1
TC
25/*
26=item i_color
27=category Data Types
28=synopsis i_color black;
29=synopsis black.rgba.r = black.rgba.g = black.rgba.b = black.rgba.a = 0;
30
31Type for 8-bit/sample color.
32
33Samples as per;
34
35 i_color c;
36
37i_color is a union of:
38
39=over
40
41=item *
42
5715f7c3 43gray - contains a single element gray_color, eg. C<c.gray.gray_color>
6cfee9d1
TC
44
45=item *
46
5715f7c3 47C<rgb> - contains three elements C<r>, C<g>, C<b>, eg. C<c.rgb.r>
6cfee9d1
TC
48
49=item *
50
5715f7c3 51C<rgba> - contains four elements C<r>, C<g>, C<b>, C<a>, eg. C<c.rgba.a>
6cfee9d1
TC
52
53=item *
54
5715f7c3
TC
55C<cmyk> - contains four elements C<c>, C<m>, C<y>, C<k>,
56eg. C<c.cmyk.y>. Note that Imager never uses CMYK colors except when
57reading/writing files.
6cfee9d1
TC
58
59=item *
60
61channels - an array of four channels, eg C<c.channels[2]>.
62
63=back
64
65=cut
66*/
67
02d1d628
AMH
68typedef union {
69 gray_color gray;
70 rgb_color rgb;
71 rgba_color rgba;
72 cmyk_color cmyk;
faa9b3e7 73 i_sample_t channel[MAXCHANNELS];
02d1d628
AMH
74 unsigned int ui;
75} i_color;
76
faa9b3e7
TC
77/* this is the larger sample type, it should be able to accurately represent
78 any sample size we use */
79typedef double i_fsample_t;
02d1d628 80
faa9b3e7
TC
81typedef struct { i_fsample_t gray_color; } i_fgray_color_t;
82typedef struct { i_fsample_t r, g, b; } i_frgb_color_t;
83typedef struct { i_fsample_t r, g, b, a; } i_frgba_color_t;
84typedef struct { i_fsample_t c, m, y, k; } i_fcmyk_color_t;
85
6cfee9d1
TC
86/*
87=item i_fcolor
88=category Data Types
89
90This is the double/sample color type.
91
92Its layout exactly corresponds to i_color.
93
94=cut
95*/
96
faa9b3e7
TC
97typedef union {
98 i_fgray_color_t gray;
99 i_frgb_color_t rgb;
100 i_frgba_color_t rgba;
101 i_fcmyk_color_t cmyk;
102 i_fsample_t channel[MAXCHANNELS];
103} i_fcolor;
104
105typedef enum {
106 i_direct_type, /* direct colour, keeps RGB values per pixel */
26fd367b 107 i_palette_type /* keeps a palette index per pixel */
faa9b3e7
TC
108} i_img_type_t;
109
110typedef enum {
111 /* bits per sample, not per pixel */
112 /* a paletted image might have one bit per sample */
113 i_8_bits = 8,
114 i_16_bits = 16,
26fd367b 115 i_double_bits = sizeof(double) * 8
faa9b3e7
TC
116} i_img_bits_t;
117
118typedef struct {
119 char *name; /* name of a given tag, might be NULL */
120 int code; /* number of a given tag, -1 if it has no meaning */
121 char *data; /* value of a given tag if it's not an int, may be NULL */
122 int size; /* size of the data */
123 int idata; /* value of a given tag if data is NULL */
124} i_img_tag;
125
126typedef struct {
127 int count; /* how many tags have been set */
128 int alloc; /* how many tags have been allocated for */
129 i_img_tag *tags;
130} i_img_tags;
131
132typedef struct i_img_ i_img;
97ac0a96
TC
133typedef int (*i_f_ppix_t)(i_img *im, int x, int y, const i_color *pix);
134typedef int (*i_f_ppixf_t)(i_img *im, int x, int y, const i_fcolor *pix);
135typedef int (*i_f_plin_t)(i_img *im, int x, int r, int y, const i_color *vals);
136typedef int (*i_f_plinf_t)(i_img *im, int x, int r, int y, const i_fcolor *vals);
faa9b3e7
TC
137typedef int (*i_f_gpix_t)(i_img *im, int x, int y, i_color *pix);
138typedef int (*i_f_gpixf_t)(i_img *im, int x, int y, i_fcolor *pix);
139typedef int (*i_f_glin_t)(i_img *im, int x, int r, int y, i_color *vals);
140typedef int (*i_f_glinf_t)(i_img *im, int x, int r, int y, i_fcolor *vals);
141
142typedef int (*i_f_gsamp_t)(i_img *im, int x, int r, int y, i_sample_t *samp,
18accb2a 143 const int *chans, int chan_count);
faa9b3e7 144typedef int (*i_f_gsampf_t)(i_img *im, int x, int r, int y, i_fsample_t *samp,
18accb2a 145 const int *chan, int chan_count);
faa9b3e7
TC
146
147typedef int (*i_f_gpal_t)(i_img *im, int x, int r, int y, i_palidx *vals);
97ac0a96
TC
148typedef int (*i_f_ppal_t)(i_img *im, int x, int r, int y, const i_palidx *vals);
149typedef int (*i_f_addcolors_t)(i_img *im, const i_color *colors, int count);
faa9b3e7
TC
150typedef int (*i_f_getcolors_t)(i_img *im, int i, i_color *, int count);
151typedef int (*i_f_colorcount_t)(i_img *im);
152typedef int (*i_f_maxcolors_t)(i_img *im);
97ac0a96
TC
153typedef int (*i_f_findcolor_t)(i_img *im, const i_color *color, i_palidx *entry);
154typedef int (*i_f_setcolors_t)(i_img *im, int index, const i_color *colors,
faa9b3e7
TC
155 int count);
156
157typedef void (*i_f_destroy_t)(i_img *im);
158
bd8052a6
TC
159typedef int (*i_f_gsamp_bits_t)(i_img *im, int x, int r, int y, unsigned *samp,
160 const int *chans, int chan_count, int bits);
161typedef int (*i_f_psamp_bits_t)(i_img *im, int x, int r, int y, unsigned const *samp,
162 const int *chans, int chan_count, int bits);
163
6cfee9d1
TC
164/*
165=item i_img_dim
166=category Data Types
167=synopsis i_img_dim x;
168=order 90
169
170A signed integer type that represents an image dimension or ordinate.
171
172May be larger than int on some platforms.
173
174=cut
175*/
d5477d3d
TC
176typedef int i_img_dim;
177
bd8052a6
TC
178/*
179=item i_img
180=category Data Types
181=synopsis i_img *img;
6cfee9d1 182=order 10
bd8052a6
TC
183
184This is Imager's image type.
185
186It contains the following members:
187
188=over
189
190=item *
191
5715f7c3 192C<channels> - the number of channels in the image
bd8052a6
TC
193
194=item *
195
5715f7c3 196C<xsize>, C<ysize> - the width and height of the image in pixels
bd8052a6
TC
197
198=item *
199
5715f7c3 200C<bytes> - the number of bytes used to store the image data. Undefined
bd8052a6
TC
201where virtual is non-zero.
202
203=item *
204
5715f7c3 205C<ch_mask> - a mask of writable channels. eg. if this is 6 then only
bd8052a6
TC
206channels 1 and 2 are writable. There may be bits set for which there
207are no channels in the image.
208
209=item *
210
5715f7c3 211C<bits> - the number of bits stored per sample. Should be one of
bd8052a6
TC
212i_8_bits, i_16_bits, i_double_bits.
213
214=item *
215
5715f7c3 216C<type> - either i_direct_type for direct color images, or i_palette_type
bd8052a6
TC
217for paletted images.
218
219=item *
220
5715f7c3
TC
221C<virtual> - if zero then this image is-self contained. If non-zero
222then this image could be an interface to some other implementation.
bd8052a6
TC
223
224=item *
225
5715f7c3 226C<idata> - the image data. This should not be directly accessed. A new
bd8052a6
TC
227image implementation can use this to store its image data.
228i_img_destroy() will myfree() this pointer if it's non-null.
229
230=item *
231
5715f7c3 232C<tags> - a structure storing the image's tags. This should only be
bd8052a6
TC
233accessed via the i_tags_*() functions.
234
235=item *
236
5715f7c3 237C<ext_data> - a pointer for use internal to an image implementation.
bd8052a6
TC
238This should be freed by the image's destroy handler.
239
240=item *
241
5715f7c3 242C<im_data> - data internal to Imager. This is initialized by
bd8052a6
TC
243i_img_init().
244
245=item *
246
247i_f_ppix, i_f_ppixf, i_f_plin, i_f_plinf, i_f_gpix, i_f_gpixf,
248i_f_glin, i_f_glinf, i_f_gsamp, i_f_gampf - implementations for each
249of the required image functions. An image implementation should
250initialize these between calling i_img_alloc() and i_img_init().
251
252=item *
253
254i_f_gpal, i_f_ppal, i_f_addcolors, i_f_getcolors, i_f_colorcount,
255i_f_maxcolors, i_f_findcolor, i_f_setcolors - implementations for each
256paletted image function.
257
258=item *
259
260i_f_destroy - custom image destruction function. This should be used
261to release memory if necessary.
262
263=item *
264
265i_f_gsamp_bits - implements i_gsamp_bits() for this image.
266
267=item *
268
269i_f_psamp_bits - implements i_psamp_bits() for this image.
270
271=back
272
273=cut
274*/
275
faa9b3e7 276struct i_img_ {
02d1d628 277 int channels;
d5477d3d
TC
278 i_img_dim xsize,ysize;
279 size_t bytes;
02d1d628 280 unsigned int ch_mask;
faa9b3e7
TC
281 i_img_bits_t bits;
282 i_img_type_t type;
283 int virtual; /* image might not keep any data, must use functions */
284 unsigned char *idata; /* renamed to force inspection of existing code */
285 /* can be NULL if virtual is non-zero */
286 i_img_tags tags;
02d1d628 287
02d1d628 288 void *ext_data;
02d1d628 289
faa9b3e7
TC
290 /* interface functions */
291 i_f_ppix_t i_f_ppix;
292 i_f_ppixf_t i_f_ppixf;
293 i_f_plin_t i_f_plin;
294 i_f_plinf_t i_f_plinf;
295 i_f_gpix_t i_f_gpix;
296 i_f_gpixf_t i_f_gpixf;
297 i_f_glin_t i_f_glin;
298 i_f_glinf_t i_f_glinf;
299 i_f_gsamp_t i_f_gsamp;
300 i_f_gsampf_t i_f_gsampf;
301
302 /* only valid for type == i_palette_type */
303 i_f_gpal_t i_f_gpal;
304 i_f_ppal_t i_f_ppal;
305 i_f_addcolors_t i_f_addcolors;
306 i_f_getcolors_t i_f_getcolors;
307 i_f_colorcount_t i_f_colorcount;
308 i_f_maxcolors_t i_f_maxcolors;
309 i_f_findcolor_t i_f_findcolor;
310 i_f_setcolors_t i_f_setcolors;
311
312 i_f_destroy_t i_f_destroy;
bd8052a6
TC
313
314 /* as of 0.61 */
315 i_f_gsamp_bits_t i_f_gsamp_bits;
316 i_f_psamp_bits_t i_f_psamp_bits;
317
318 void *im_data;
faa9b3e7 319};
02d1d628 320
faa9b3e7
TC
321/* ext_data for paletted images
322 */
323typedef struct {
324 int count; /* amount of space used in palette (in entries) */
325 int alloc; /* amount of space allocated for palette (in entries) */
326 i_color *pal;
327 int last_found;
328} i_img_pal_ext;
02d1d628 329
faa9b3e7 330/* Helper datatypes
02d1d628
AMH
331 The types in here so far are:
332
333 doubly linked bucket list - pretty efficient
334 octtree - no idea about goodness
335
336 needed: hashes.
337
338*/
339
02d1d628
AMH
340/* bitmap mask */
341
342struct i_bitmap {
343 int xsize,ysize;
344 char *data;
345};
346
347struct i_bitmap* btm_new(int xsize,int ysize);
348void btm_destroy(struct i_bitmap *btm);
349int btm_test(struct i_bitmap *btm,int x,int y);
350void btm_set(struct i_bitmap *btm,int x,int y);
351
352
02d1d628
AMH
353/* Stack/Linked list */
354
355struct llink {
356 struct llink *p,*n;
357 void *data;
358 int fill; /* Number used in this link */
359};
360
361struct llist {
362 struct llink *h,*t;
363 int multip; /* # of copies in a single chain */
364 int ssize; /* size of each small element */
365 int count; /* number of elements on the list */
366};
367
368
369/* Links */
370
371struct llink *llink_new( struct llink* p,int size );
372int llist_llink_push( struct llist *lst, struct llink *lnk, void *data );
373
374/* Lists */
375
376struct llist *llist_new( int multip, int ssize );
377void llist_destroy( struct llist *l );
378void llist_push( struct llist *l, void *data );
379void llist_dump( struct llist *l );
380int llist_pop( struct llist *l,void *data );
381
382
383
384
385/* Octtree */
386
387struct octt {
388 struct octt *t[8];
389 int cnt;
390};
391
faa9b3e7 392struct octt *octt_new(void);
02d1d628
AMH
393int octt_add(struct octt *ct,unsigned char r,unsigned char g,unsigned char b);
394void octt_dump(struct octt *ct);
395void octt_count(struct octt *ct,int *tot,int max,int *overflow);
396void octt_delete(struct octt *ct);
a60905e4 397void octt_histo(struct octt *ct, unsigned int **col_usage_it_adr);
02d1d628 398
3799c4d1
TC
399/* font bounding box results */
400enum bounding_box_index_t {
401 BBOX_NEG_WIDTH,
402 BBOX_GLOBAL_DESCENT,
403 BBOX_POS_WIDTH,
404 BBOX_GLOBAL_ASCENT,
405 BBOX_DESCENT,
406 BBOX_ASCENT,
407 BBOX_ADVANCE_WIDTH,
dc35bde9 408 BBOX_RIGHT_BEARING,
3799c4d1
TC
409 BOUNDING_BOX_COUNT
410};
411
92bda632
TC
412/* Generic fills */
413struct i_fill_tag;
414
415typedef void (*i_fill_with_color_f)
416 (struct i_fill_tag *fill, int x, int y, int width, int channels,
417 i_color *data);
418typedef void (*i_fill_with_fcolor_f)
419 (struct i_fill_tag *fill, int x, int y, int width, int channels,
420 i_fcolor *data);
421typedef void (*i_fill_destroy_f)(struct i_fill_tag *fill);
9b1ec2b8
TC
422
423/* combine functions modify their target and are permitted to modify
424 the source to prevent having to perform extra copying/memory
425 allocations, etc
426 The out array has I<channels> channels.
427
428 The in array has I<channels> channels + an alpha channel if one
429 isn't included in I<channels>.
430*/
431
92bda632
TC
432typedef void (*i_fill_combine_f)(i_color *out, i_color *in, int channels,
433 int count);
434typedef void (*i_fill_combinef_f)(i_fcolor *out, i_fcolor *in, int channels,
435 int count);
436
437/* fountain fill types */
438typedef enum {
439 i_fst_linear,
440 i_fst_curved,
441 i_fst_sine,
442 i_fst_sphere_up,
443 i_fst_sphere_down,
444 i_fst_end
445} i_fountain_seg_type;
446typedef enum {
447 i_fc_direct,
448 i_fc_hue_up,
449 i_fc_hue_down,
450 i_fc_end
451} i_fountain_color;
452typedef struct {
453 double start, middle, end;
454 i_fcolor c[2];
455 i_fountain_seg_type type;
456 i_fountain_color color;
457} i_fountain_seg;
458typedef enum {
459 i_fr_none,
460 i_fr_sawtooth,
461 i_fr_triangle,
462 i_fr_saw_both,
463 i_fr_tri_both
464} i_fountain_repeat;
465typedef enum {
466 i_ft_linear,
467 i_ft_bilinear,
468 i_ft_radial,
469 i_ft_radial_square,
470 i_ft_revolution,
471 i_ft_conical,
472 i_ft_end
473} i_fountain_type;
474typedef enum {
475 i_fts_none,
476 i_fts_grid,
477 i_fts_random,
478 i_fts_circle
479} i_ft_supersample;
480
6cfee9d1
TC
481/*
482=item i_fill_t
483=category Data Types
484=synopsis i_fill_t *fill;
485
486This is the "abstract" base type for Imager's fill types.
487
488Unless you're implementing a new fill type you'll typically treat this
489as an opaque type.
490
491=cut
492*/
92bda632
TC
493
494typedef struct i_fill_tag
495{
496 /* called for 8-bit/sample image (and maybe lower) */
497 /* this may be NULL, if so call fill_with_fcolor */
9b1ec2b8 498 i_fill_with_color_f f_fill_with_color;
92bda632
TC
499
500 /* called for other sample sizes */
501 /* this must be non-NULL */
9b1ec2b8 502 i_fill_with_fcolor_f f_fill_with_fcolor;
92bda632
TC
503
504 /* called if non-NULL to release any extra resources */
505 i_fill_destroy_f destroy;
506
507 /* if non-zero the caller will fill data with the original data
508 from the image */
509 i_fill_combine_f combine;
510 i_fill_combinef_f combinef;
511} i_fill_t;
512
513typedef enum {
514 ic_none,
515 ic_normal,
516 ic_multiply,
517 ic_dissolve,
518 ic_add,
519 ic_subtract,
520 ic_diff,
521 ic_lighten,
522 ic_darken,
523 ic_hue,
524 ic_sat,
525 ic_value,
526 ic_color
527} i_combine_t;
528
529/*
530 describes an axis of a MM font.
531 Modelled on FT2's FT_MM_Axis.
532 It would be nice to have a default entry too, but FT2
533 doesn't support it.
534*/
535typedef struct i_font_mm_axis_tag {
536 char const *name;
537 int minimum;
538 int maximum;
539} i_font_mm_axis;
540
541#define IM_FONT_MM_MAX_AXES 4
542
543/*
544 multiple master information for a font, if any
545 modelled on FT2's FT_Multi_Master.
546*/
547typedef struct i_font_mm_tag {
548 int num_axis;
549 int num_designs; /* provided but not necessarily useful */
550 i_font_mm_axis axis[IM_FONT_MM_MAX_AXES];
551} i_font_mm;
552
553#ifdef HAVE_LIBTT
554
555struct TT_Fonthandle_;
556
557typedef struct TT_Fonthandle_ TT_Fonthandle;
558
559#endif
560
561#ifdef HAVE_FT2
562
563typedef struct FT2_Fonthandle FT2_Fonthandle;
564
565#endif
566
567/* transparency handling for quantized output */
568typedef enum i_transp_tag {
569 tr_none, /* ignore any alpha channel */
570 tr_threshold, /* threshold the transparency - uses tr_threshold */
571 tr_errdiff, /* error diffusion */
572 tr_ordered /* an ordered dither */
573} i_transp;
574
575/* controls how we build the colour map */
576typedef enum i_make_colors_tag {
577 mc_none, /* user supplied colour map only */
578 mc_web_map, /* Use the 216 colour web colour map */
579 mc_addi, /* Addi's algorithm */
580 mc_median_cut, /* median cut - similar to giflib, hopefully */
9c106321 581 mc_mono, /* fixed mono color map */
92bda632
TC
582 mc_mask = 0xFF /* (mask for generator) */
583} i_make_colors;
584
585/* controls how we translate the colours */
586typedef enum i_translate_tag {
587 pt_giflib, /* get gif lib to do it (ignores make_colours) */
588 pt_closest, /* just use the closest match within the hashbox */
589 pt_perturb, /* randomly perturb the data - uses perturb_size*/
590 pt_errdiff /* error diffusion dither - uses errdiff */
591} i_translate;
592
593/* Which error diffusion map to use */
594typedef enum i_errdiff_tag {
595 ed_floyd, /* floyd-steinberg */
596 ed_jarvis, /* Jarvis, Judice and Ninke */
597 ed_stucki, /* Stucki */
598 ed_custom, /* the map found in ed_map|width|height|orig */
599 ed_mask = 0xFF, /* mask to get the map */
600 ed_bidir = 0x100 /* change direction for each row */
601} i_errdiff;
602
603/* which ordered dither map to use
604 currently only available for transparency
605 I don't know of a way to do ordered dither of an image against some
606 general palette
607 */
608typedef enum i_ord_dith_tag
609{
610 od_random, /* sort of random */
611 od_dot8, /* large dot */
612 od_dot4,
613 od_hline,
614 od_vline,
615 od_slashline, /* / line dither */
616 od_backline, /* \ line dither */
617 od_tiny, /* small checkerbox */
618 od_custom /* custom 8x8 map */
619} i_ord_dith;
620
621typedef struct i_gif_pos_tag {
622 int x, y;
623} i_gif_pos;
624
625/* passed into i_writegif_gen() to control quantization */
626typedef struct i_quantize_tag {
ec6d8908
TC
627 int version;
628
92bda632
TC
629 /* how to handle transparency */
630 i_transp transp;
631 /* the threshold at which to make pixels opaque */
632 int tr_threshold;
633 i_errdiff tr_errdiff;
634 i_ord_dith tr_orddith;
635 unsigned char tr_custom[64];
636
637 /* how to make the colour map */
638 i_make_colors make_colors;
639
640 /* any existing colours
641 mc_existing is an existing colour table
642 mc_count is the number of existing colours
643 mc_size is the total size of the array that mc_existing points
644 at - this must be at least 256
645 */
646 i_color *mc_colors;
647 int mc_size;
648 int mc_count;
649
650 /* how we translate the colours */
651 i_translate translate;
652
653 /* the error diffusion map to use if translate is mc_errdiff */
654 i_errdiff errdiff;
655 /* the following define the error diffusion values to use if
656 errdiff is ed_custom. ed_orig is the column on the top row that
657 represents the current
658 */
659 int *ed_map;
660 int ed_width, ed_height, ed_orig;
661
662 /* the amount of perturbation to use for translate is mc_perturb */
663 int perturb;
ec6d8908 664 /* version 2 members after here */
92bda632
TC
665} i_quantize;
666
667typedef struct i_gif_opts {
668 /* each image has a local color map */
669 int each_palette;
670
671 /* images are interlaced */
672 int interlace;
673
674 /* time for which image is displayed
675 (in 1/100 seconds)
676 default: 0
677 */
678 int delay_count;
679 int *delays;
680
681 /* user input flags
682 default: 0
683 */
684 int user_input_count;
685 char *user_input_flags;
686
687 /* disposal
688 default: 0 */
689 int disposal_count;
690 char *disposal;
691
692 /* this is added to the color table when we make an image transparent */
693 i_color tran_color;
694
695 /* image positions */
696 int position_count;
697 i_gif_pos *positions;
698
699 /* Netscape loop extension - number of loops */
700 int loop_count;
701
702 /* should be eliminate unused colors? */
703 int eliminate_unused;
704} i_gif_opts;
705
e310e5f9
TC
706/* distance measures used by some filters */
707enum {
708 i_dmeasure_euclidean = 0,
709 i_dmeasure_euclidean_squared = 1,
710 i_dmeasure_manhatten = 2,
711 i_dmeasure_limit = 2,
712};
92bda632 713
0778adbf 714#include "iolayert.h"
92bda632 715
9c106321
TC
716#include "rendert.h"
717
02d1d628
AMH
718#endif
719