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