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