- Finished/rewrote Arnar's old SGI RGB file format support, so Imager
[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_img_dim;
106
107 struct i_img_ {
108   int channels;
109   i_img_dim xsize,ysize;
110   size_t bytes;
111   unsigned int ch_mask;
112   i_img_bits_t bits;
113   i_img_type_t type;
114   int virtual; /* image might not keep any data, must use functions */
115   unsigned char *idata; /* renamed to force inspection of existing code */
116                         /* can be NULL if virtual is non-zero */
117   i_img_tags tags;
118
119   void *ext_data;
120
121   /* interface functions */
122   i_f_ppix_t i_f_ppix;
123   i_f_ppixf_t i_f_ppixf;
124   i_f_plin_t i_f_plin;
125   i_f_plinf_t i_f_plinf;
126   i_f_gpix_t i_f_gpix;
127   i_f_gpixf_t i_f_gpixf;
128   i_f_glin_t i_f_glin;
129   i_f_glinf_t i_f_glinf;
130   i_f_gsamp_t i_f_gsamp;
131   i_f_gsampf_t i_f_gsampf;
132   
133   /* only valid for type == i_palette_type */
134   i_f_gpal_t i_f_gpal;
135   i_f_ppal_t i_f_ppal;
136   i_f_addcolors_t i_f_addcolors;
137   i_f_getcolors_t i_f_getcolors;
138   i_f_colorcount_t i_f_colorcount;
139   i_f_maxcolors_t i_f_maxcolors;
140   i_f_findcolor_t i_f_findcolor;
141   i_f_setcolors_t i_f_setcolors;
142
143   i_f_destroy_t i_f_destroy;
144 };
145
146 /* ext_data for paletted images
147  */
148 typedef struct {
149   int count; /* amount of space used in palette (in entries) */
150   int alloc; /* amount of space allocated for palette (in entries) */
151   i_color *pal;
152   int last_found;
153 } i_img_pal_ext;
154
155 /* Helper datatypes
156   The types in here so far are:
157
158   doubly linked bucket list - pretty efficient
159   octtree - no idea about goodness
160   
161   needed: hashes.
162
163 */
164
165
166
167
168
169 /* bitmap mask */
170
171 struct i_bitmap {
172   int xsize,ysize;
173   char *data;
174 };
175
176 struct i_bitmap* btm_new(int xsize,int ysize);
177 void btm_destroy(struct i_bitmap *btm);
178 int btm_test(struct i_bitmap *btm,int x,int y);
179 void btm_set(struct i_bitmap *btm,int x,int y);
180
181
182
183
184
185
186
187
188 /* Stack/Linked list */
189
190 struct llink {
191   struct llink *p,*n;
192   void *data;
193   int fill;             /* Number used in this link */
194 };
195
196 struct llist {
197   struct llink *h,*t;
198   int multip;           /* # of copies in a single chain  */
199   int ssize;            /* size of each small element     */
200   int count;           /* number of elements on the list */
201 };
202
203
204 /* Links */
205
206 struct llink *llink_new( struct llink* p,int size );
207 int  llist_llink_push( struct llist *lst, struct llink *lnk, void *data );
208
209 /* Lists */
210
211 struct llist *llist_new( int multip, int ssize );
212 void llist_destroy( struct llist *l );
213 void llist_push( struct llist *l, void *data );
214 void llist_dump( struct llist *l );
215 int llist_pop( struct llist *l,void *data );
216
217
218
219
220 /* Octtree */
221
222 struct octt {
223   struct octt *t[8]; 
224   int cnt;
225 };
226
227 struct octt *octt_new(void);
228 int octt_add(struct octt *ct,unsigned char r,unsigned char g,unsigned char b);
229 void octt_dump(struct octt *ct);
230 void octt_count(struct octt *ct,int *tot,int max,int *overflow);
231 void octt_delete(struct octt *ct);
232
233 /* font bounding box results */
234 enum bounding_box_index_t {
235   BBOX_NEG_WIDTH,
236   BBOX_GLOBAL_DESCENT,
237   BBOX_POS_WIDTH,
238   BBOX_GLOBAL_ASCENT,
239   BBOX_DESCENT,
240   BBOX_ASCENT,
241   BBOX_ADVANCE_WIDTH,
242   BBOX_RIGHT_BEARING,
243   BOUNDING_BOX_COUNT
244 };
245
246 /* Generic fills */
247 struct i_fill_tag;
248
249 typedef void (*i_fill_with_color_f)
250      (struct i_fill_tag *fill, int x, int y, int width, int channels, 
251       i_color *data);
252 typedef void (*i_fill_with_fcolor_f)
253      (struct i_fill_tag *fill, int x, int y, int width, int channels,
254       i_fcolor *data);
255 typedef void (*i_fill_destroy_f)(struct i_fill_tag *fill);
256 typedef void (*i_fill_combine_f)(i_color *out, i_color *in, int channels, 
257                                  int count);
258 typedef void (*i_fill_combinef_f)(i_fcolor *out, i_fcolor *in, int channels,
259                                   int count);
260
261 /* fountain fill types */
262 typedef enum {
263   i_fst_linear,
264   i_fst_curved,
265   i_fst_sine,
266   i_fst_sphere_up,
267   i_fst_sphere_down,
268   i_fst_end
269 } i_fountain_seg_type;
270 typedef enum {
271   i_fc_direct,
272   i_fc_hue_up,
273   i_fc_hue_down,
274   i_fc_end
275 } i_fountain_color;
276 typedef struct {
277   double start, middle, end;
278   i_fcolor c[2];
279   i_fountain_seg_type type;
280   i_fountain_color color;
281 } i_fountain_seg;
282 typedef enum {
283   i_fr_none,
284   i_fr_sawtooth,
285   i_fr_triangle,
286   i_fr_saw_both,
287   i_fr_tri_both
288 } i_fountain_repeat;
289 typedef enum {
290   i_ft_linear,
291   i_ft_bilinear,
292   i_ft_radial,
293   i_ft_radial_square,
294   i_ft_revolution,
295   i_ft_conical,
296   i_ft_end
297 } i_fountain_type;
298 typedef enum {
299   i_fts_none,
300   i_fts_grid,
301   i_fts_random,
302   i_fts_circle
303 } i_ft_supersample;
304
305
306 typedef struct i_fill_tag
307 {
308   /* called for 8-bit/sample image (and maybe lower) */
309   /* this may be NULL, if so call fill_with_fcolor */
310   i_fill_with_color_f fill_with_color;
311
312   /* called for other sample sizes */
313   /* this must be non-NULL */
314   i_fill_with_fcolor_f fill_with_fcolor;
315
316   /* called if non-NULL to release any extra resources */
317   i_fill_destroy_f destroy;
318
319   /* if non-zero the caller will fill data with the original data
320      from the image */
321   i_fill_combine_f combine;
322   i_fill_combinef_f combinef;
323 } i_fill_t;
324
325 typedef enum {
326   ic_none,
327   ic_normal,
328   ic_multiply,
329   ic_dissolve,
330   ic_add,
331   ic_subtract,
332   ic_diff,
333   ic_lighten,
334   ic_darken,
335   ic_hue,
336   ic_sat,
337   ic_value,
338   ic_color
339 } i_combine_t;
340
341 /*
342    describes an axis of a MM font.
343    Modelled on FT2's FT_MM_Axis.
344    It would be nice to have a default entry too, but FT2 
345    doesn't support it.
346 */
347 typedef struct i_font_mm_axis_tag {
348   char const *name;
349   int minimum;
350   int maximum;
351 } i_font_mm_axis;
352
353 #define IM_FONT_MM_MAX_AXES 4
354
355 /* 
356    multiple master information for a font, if any 
357    modelled on FT2's FT_Multi_Master.
358 */
359 typedef struct i_font_mm_tag {
360   int num_axis;
361   int num_designs; /* provided but not necessarily useful */
362   i_font_mm_axis axis[IM_FONT_MM_MAX_AXES];
363 } i_font_mm;
364
365 #ifdef HAVE_LIBTT
366
367 struct TT_Fonthandle_;
368
369 typedef struct TT_Fonthandle_ TT_Fonthandle;
370
371 #endif
372
373 #ifdef HAVE_FT2
374
375 typedef struct FT2_Fonthandle FT2_Fonthandle;
376
377 #endif
378
379 /* transparency handling for quantized output */
380 typedef enum i_transp_tag {
381   tr_none, /* ignore any alpha channel */
382   tr_threshold, /* threshold the transparency - uses tr_threshold */
383   tr_errdiff, /* error diffusion */
384   tr_ordered /* an ordered dither */
385 } i_transp;
386
387 /* controls how we build the colour map */
388 typedef enum i_make_colors_tag {
389   mc_none, /* user supplied colour map only */
390   mc_web_map, /* Use the 216 colour web colour map */
391   mc_addi, /* Addi's algorithm */
392   mc_median_cut, /* median cut - similar to giflib, hopefully */
393   mc_mono, /* fixed mono color map */
394   mc_mask = 0xFF /* (mask for generator) */
395 } i_make_colors;
396
397 /* controls how we translate the colours */
398 typedef enum i_translate_tag {
399   pt_giflib, /* get gif lib to do it (ignores make_colours) */
400   pt_closest, /* just use the closest match within the hashbox */
401   pt_perturb, /* randomly perturb the data - uses perturb_size*/
402   pt_errdiff /* error diffusion dither - uses errdiff */
403 } i_translate;
404
405 /* Which error diffusion map to use */
406 typedef enum i_errdiff_tag {
407   ed_floyd, /* floyd-steinberg */
408   ed_jarvis, /* Jarvis, Judice and Ninke */
409   ed_stucki, /* Stucki */
410   ed_custom, /* the map found in ed_map|width|height|orig */
411   ed_mask = 0xFF, /* mask to get the map */
412   ed_bidir = 0x100 /* change direction for each row */
413 } i_errdiff;
414
415 /* which ordered dither map to use
416    currently only available for transparency
417    I don't know of a way to do ordered dither of an image against some 
418    general palette
419  */
420 typedef enum i_ord_dith_tag
421 {
422   od_random, /* sort of random */
423   od_dot8, /* large dot */
424   od_dot4,
425   od_hline,
426   od_vline,
427   od_slashline, /* / line dither */
428   od_backline, /* \ line dither */
429   od_tiny, /* small checkerbox */
430   od_custom /* custom 8x8 map */
431 } i_ord_dith;
432
433 typedef struct i_gif_pos_tag {
434   int x, y;
435 } i_gif_pos;
436
437 /* passed into i_writegif_gen() to control quantization */
438 typedef struct i_quantize_tag {
439   /* how to handle transparency */
440   i_transp transp;
441   /* the threshold at which to make pixels opaque */
442   int tr_threshold;
443   i_errdiff tr_errdiff;
444   i_ord_dith tr_orddith;
445   unsigned char tr_custom[64];
446   
447   /* how to make the colour map */
448   i_make_colors make_colors;
449
450   /* any existing colours 
451      mc_existing is an existing colour table
452      mc_count is the number of existing colours
453      mc_size is the total size of the array that mc_existing points
454      at - this must be at least 256
455   */
456   i_color *mc_colors;
457   int mc_size;
458   int mc_count;
459
460   /* how we translate the colours */
461   i_translate translate;
462
463   /* the error diffusion map to use if translate is mc_errdiff */
464   i_errdiff errdiff;
465   /* the following define the error diffusion values to use if 
466      errdiff is ed_custom.  ed_orig is the column on the top row that
467      represents the current 
468   */
469   int *ed_map;
470   int ed_width, ed_height, ed_orig;
471
472   /* the amount of perturbation to use for translate is mc_perturb */
473   int perturb;
474 } i_quantize;
475
476 typedef struct i_gif_opts {
477   /* each image has a local color map */
478   int each_palette;
479
480   /* images are interlaced */
481   int interlace;
482
483   /* time for which image is displayed 
484    (in 1/100 seconds)
485    default: 0
486   */
487   int delay_count;
488   int *delays;
489
490   /* user input flags 
491      default: 0
492    */
493   int user_input_count;
494   char *user_input_flags;
495
496   /* disposal
497      default: 0 */
498   int disposal_count;
499   char *disposal;
500
501   /* this is added to the color table when we make an image transparent */
502   i_color tran_color;
503
504   /* image positions */
505   int position_count;
506   i_gif_pos *positions;
507
508   /* Netscape loop extension - number of loops */
509   int loop_count;
510
511   /* should be eliminate unused colors? */
512   int eliminate_unused;
513 } i_gif_opts;
514
515 /* distance measures used by some filters */
516 enum {
517   i_dmeasure_euclidean = 0,
518   i_dmeasure_euclidean_squared = 1,
519   i_dmeasure_manhatten = 2,
520   i_dmeasure_limit = 2,
521 };
522
523 #include "iolayert.h"
524
525 #include "rendert.h"
526
527 #endif
528