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