]> git.imager.perl.org - imager.git/blob - image.h
tiff tests extracted from t10formats.t
[imager.git] / image.h
1 #ifndef _IMAGE_H_
2 #define _IMAGE_H_
3
4 #include "io.h"
5 #include "iolayer.h"
6 #include "log.h"
7 #include "stackmach.h"
8
9
10 #ifndef _MSC_VER
11 #include <unistd.h>
12 #endif
13 #include <string.h>
14 #include <stdio.h>
15 #include <math.h>
16 #include <stdlib.h>
17
18 #ifdef SUNOS
19 #include <strings.h>
20 #endif
21
22 #ifndef PI
23 #define PI 3.14159265358979323846
24 #endif
25
26 #ifndef MAXINT
27 #define MAXINT 2147483647
28 #endif
29
30 #include "datatypes.h"
31
32 undef_int i_has_format(char *frmt);
33
34 /* constructors and destructors */
35
36 i_color *ICL_new_internal(            unsigned char r,unsigned char g,unsigned char b,unsigned char a);
37 i_color *ICL_set_internal(i_color *cl,unsigned char r,unsigned char g,unsigned char b,unsigned char a);
38 void     ICL_info        (i_color *cl);
39 void     ICL_DESTROY     (i_color *cl);
40 void     ICL_add         (i_color *dst, i_color *src, int ch);
41
42 i_img *IIM_new(int x,int y,int ch);
43 void   IIM_DESTROY(i_img *im);
44 i_img *i_img_new( void );
45 i_img *i_img_empty(i_img *im,int x,int y);
46 i_img *i_img_empty_ch(i_img *im,int x,int y,int ch);
47 void   i_img_exorcise(i_img *im);
48 void   i_img_destroy(i_img *im);
49
50 void   i_img_info(i_img *im,int *info);
51
52 /* Image feature settings */
53
54 void   i_img_setmask    (i_img *im,int ch_mask);
55 int    i_img_getmask    (i_img *im);
56 int    i_img_getchannels(i_img *im);
57
58 /* Base functions */
59
60 int i_ppix(i_img *im,int x,int y,i_color *val);
61 int i_gpix(i_img *im,int x,int y,i_color *val);
62
63 int i_ppix_d(i_img *im,int x,int y,i_color *val);
64 int i_gpix_d(i_img *im,int x,int y,i_color *val);
65
66 float i_gpix_pch(i_img *im,int x,int y,int ch);
67
68 /* functions for drawing primitives */
69
70 void i_box         (i_img *im,int x1,int y1,int x2,int y2,i_color *val);
71 void i_box_filled  (i_img *im,int x1,int y1,int x2,int y2,i_color *val);
72 void i_draw        (i_img *im,int x1,int y1,int x2,int y2,i_color *val);
73 void i_line_aa     (i_img *im,int x1,int y1,int x2,int y2,i_color *val);
74 void i_arc         (i_img *im,int x,int y,float rad,float d1,float d2,i_color *val);
75 void i_copyto      (i_img *im,i_img *src,int x1,int y1,int x2,int y2,int tx,int ty);
76 void i_copyto_trans(i_img *im,i_img *src,int x1,int y1,int x2,int y2,int tx,int ty,i_color *trans);
77 void i_copy        (i_img *im,i_img *src);
78 void i_rubthru     (i_img *im,i_img *src,int tx,int ty);
79
80 void i_bezier_multi(i_img *im,int l,double *x,double *y,i_color *val);
81 void i_poly_aa     (i_img *im,int l,double *x,double *y,i_color *val);
82
83 void i_flood_fill  (i_img *im,int seedx,int seedy,i_color *dcol);
84
85 /* image processing functions */
86
87 void i_gaussian    (i_img *im,float stdev);
88 void i_conv        (i_img *im,float *coeff,int len);
89
90 float i_img_diff   (i_img *im1,i_img *im2);
91
92 /* font routines */
93
94 undef_int i_init_fonts( void );
95
96 #ifdef HAVE_LIBT1
97 #include <t1lib.h>
98
99 undef_int init_t1( void );
100 int       i_t1_new( char *pfb, char *afm );
101 int       i_t1_destroy( int font_id );
102 undef_int i_t1_cp( i_img *im, int xb, int yb, int channel, int fontnum, float points, char* str, int len, int align );
103 undef_int i_t1_text( i_img *im, int xb, int yb, i_color *cl, int fontnum, float points, char* str, int len, int align );
104 void      i_t1_bbox( int fontnum, float point, char *str, int len, int cords[6] );
105 void      i_t1_set_aa( int st );
106 void      close_t1( void );
107
108 #endif
109
110 #ifdef HAVE_LIBTT
111
112 #include <freetype.h>
113 #define TT_CHC 5
114
115 struct TT_Instancehandle_ {
116   TT_Instance instance;
117   TT_Instance_Metrics imetrics;
118   TT_Glyph_Metrics gmetrics[256];
119   TT_Glyph glyphs[256];
120   int smooth;
121   int ptsize;
122   int order;
123 };
124
125 typedef struct TT_Instancehandle_ TT_Instancehandle;
126
127 struct TT_Fonthandle_ {
128   TT_Face face;
129   TT_Face_Properties properties;
130   TT_Instancehandle instanceh[TT_CHC];
131   TT_CharMap char_map;
132 };
133
134 typedef struct TT_Fonthandle_ TT_Fonthandle;
135
136
137
138 undef_int init_tt( void );
139 TT_Fonthandle* i_tt_new(char *fontname);
140 void i_tt_destroy( TT_Fonthandle *handle );
141 undef_int i_tt_cp( TT_Fonthandle *handle,i_img *im,int xb,int yb,int channel,float points,char* txt,int len,int smooth);
142 undef_int i_tt_text( TT_Fonthandle *handle, i_img *im, int xb, int yb, i_color *cl, float points, char* txt, int len, int smooth);
143 undef_int i_tt_bbox( TT_Fonthandle *handle, float points,char *txt,int len,int cords[6]);
144
145
146 #endif  /* End of freetype headers */
147
148
149
150
151
152
153
154 /* functions for reading and writing formats */
155
156 /* general reader callback 
157  userdata - data the user passed into the reader
158  buffer - the buffer to fill with data
159  need - the amount of data needed
160  want - the amount of space we have to store data
161  fill buffer and return the number of bytes read, 0 for eof, -1 for error
162 */
163
164 typedef int (*i_read_callback_t)(char *userdata, char *buffer, int need, 
165                                  int want);
166
167 /* i_gen_reader() translates the low-level requests from whatever library
168    into buffered requests.
169    but the called function can always bypass buffering by only ever 
170    reading I<need> bytes.
171 */
172 #define CBBUFSIZ 4096
173
174 typedef struct {
175   i_read_callback_t cb;
176   char *userdata;
177   char buffer[CBBUFSIZ];
178   int length;
179   int cpos;
180 } i_gen_read_data;
181
182 extern int  i_gen_reader(i_gen_read_data *info, char *buffer, int need);
183 extern      i_gen_read_data *i_gen_read_data_new(i_read_callback_t cb, char *userdata);
184 extern void free_gen_read_data(i_gen_read_data *);
185
186 /* general writer callback
187    userdata - the data the user passed into the writer
188    data - the data to write
189    data_size - the number of bytes to write
190    write the data, return non-zero on success, zero on failure.
191 */
192 typedef int (*i_write_callback_t)(char *userdata, char const *data, int size);
193
194 typedef struct {
195   i_write_callback_t cb;
196   char *userdata;
197   char buffer[CBBUFSIZ];
198   int maxlength;
199   int filledto;
200 } i_gen_write_data;
201
202 extern int i_gen_writer(i_gen_write_data *info, char const *data, int size);
203 extern i_gen_write_data *i_gen_write_data_new(i_write_callback_t cb, char *userdata, int maxlength);
204 extern int free_gen_write_data(i_gen_write_data *, int flush);
205
206 /* transparency handling for quantized output */
207 typedef enum i_transp_tag {
208   tr_none, /* ignore any alpha channel */
209   tr_threshold, /* threshold the transparency - uses tr_threshold */
210   tr_errdiff, /* error diffusion */
211   tr_ordered /* an ordered dither */
212 } i_transp;
213
214 /* controls how we build the colour map */
215 typedef enum i_make_colors_tag {
216   mc_none, /* user supplied colour map only */
217   mc_web_map, /* Use the 216 colour web colour map */
218   mc_addi, /* Addi's algorithm */
219   mc_mask = 0xFF /* (mask for generator) */
220 } i_make_colors;
221
222 /* controls how we translate the colours */
223 typedef enum i_translate_tag {
224   pt_giflib, /* get gif lib to do it (ignores make_colours) */
225   pt_closest, /* just use the closest match within the hashbox */
226   pt_perturb, /* randomly perturb the data - uses perturb_size*/
227   pt_errdiff /* error diffusion dither - uses errdiff */
228 } i_translate;
229
230 /* Which error diffusion map to use */
231 typedef enum i_errdiff_tag {
232   ed_floyd, /* floyd-steinberg */
233   ed_jarvis, /* Jarvis, Judice and Ninke */
234   ed_stucki, /* Stucki */
235   ed_custom, /* the map found in ed_map|width|height|orig */
236   ed_mask = 0xFF, /* mask to get the map */
237   ed_bidir = 0x100 /* change direction for each row */
238 } i_errdiff;
239
240 /* which ordered dither map to use
241    currently only available for transparency
242    I don't know of a way to do ordered dither of an image against some 
243    general palette
244  */
245 typedef enum i_ord_dith_tag
246 {
247   od_random, /* sort of random */
248   od_dot8, /* large dot */
249   od_dot4,
250   od_hline,
251   od_vline,
252   od_slashline, /* / line dither */
253   od_backline, /* \ line dither */
254   od_tiny, /* small checkerbox */
255   od_custom /* custom 8x8 map */
256 } i_ord_dith;
257
258 typedef struct i_gif_pos_tag {
259   int x, y;
260 } i_gif_pos;
261
262 /* passed into i_writegif_gen() to control quantization */
263 typedef struct i_quantize_tag {
264   /* how to handle transparency */
265   i_transp transp;
266   /* the threshold at which to make pixels opaque */
267   int tr_threshold;
268   i_errdiff tr_errdiff;
269   i_ord_dith tr_orddith;
270   unsigned char tr_custom[64];
271   
272   /* how to make the colour map */
273   i_make_colors make_colors;
274
275   /* any existing colours 
276      mc_existing is an existing colour table
277      mc_count is the number of existing colours
278      mc_size is the total size of the array that mc_existing points
279      at - this must be at least 256
280   */
281   i_color *mc_colors;
282   int mc_size;
283   int mc_count;
284
285   /* how we translate the colours */
286   i_translate translate;
287
288   /* the error diffusion map to use if translate is mc_errdiff */
289   i_errdiff errdiff;
290   /* the following define the error diffusion values to use if 
291      errdiff is ed_custom.  ed_orig is the column on the top row that
292      represents the current 
293   */
294   int *ed_map;
295   int ed_width, ed_height, ed_orig;
296
297   /* the amount of perturbation to use for translate is mc_perturb */
298   int perturb;
299 } i_quantize;
300
301 typedef struct i_gif_opts {
302   /* each image has a local color map */
303   int each_palette;
304
305   /* images are interlaced */
306   int interlace;
307
308   /* time for which image is displayed 
309    (in 1/100 seconds)
310    default: 0
311   */
312   int delay_count;
313   int *delays;
314
315   /* user input flags 
316      default: 0
317    */
318   int user_input_count;
319   char *user_input_flags;
320
321   /* disposal
322      default: 0 */
323   int disposal_count;
324   char *disposal;
325
326   /* this is added to the color table when we make an image transparent */
327   i_color tran_color;
328
329   /* image positions */
330   int position_count;
331   i_gif_pos *positions;
332
333   /* Netscape loop extension - number of loops */
334   int loop_count;
335 } i_gif_opts;
336
337 extern void quant_makemap(i_quantize *quant, i_img **imgs, int count);
338 extern i_palidx *quant_translate(i_quantize *quant, i_img *img);
339 extern void quant_transparent(i_quantize *quant, i_palidx *indices, i_img *img, i_palidx trans_index);
340
341 #ifdef HAVE_LIBJPEG
342 i_img* i_readjpeg(int fd,char** iptc_itext,int *tlength);
343 i_img* i_readjpeg_scalar(char *data, int length,char** iptc_itext,int *itlength);
344 i_img* i_readjpeg_wiol(io_glue *ig, int length, char** iptc_itext, int *itlength);
345
346 i_img* i_readjpeg_extra2(int fd,char** iptc_itext);
347 undef_int i_writejpeg(i_img *im,int fd,int qfactor);
348 #endif /* HAVE_LIBJPEG */
349
350 #ifdef HAVE_LIBTIFF
351 i_img* i_readtiff_wiol(io_glue *ig, int length);
352 undef_int i_writetiff_wiol(i_img *im, io_glue *ig);
353
354 #endif /* HAVE_LIBTIFF */
355
356 #ifdef HAVE_LIBPNG
357 i_img *i_readpng(int fd);
358 i_img *i_readpng_scalar(char *data, int length);
359 undef_int i_writepng(i_img *im,int fd);
360 #endif /* HAVE_LIBPNG */
361
362 #ifdef HAVE_LIBGIF
363 i_img *i_readgif(int fd, int **colour_table, int *colours);
364 i_img *i_readgif_scalar(char *data, int length, int **colour_table, int *colours);
365 i_img *i_readgif_callback(i_read_callback_t callback, char *userdata, int **colour_table, int *colours);
366 undef_int i_writegif(i_img *im,int fd,int colors,int pixdev,int fixedlen,i_color fixed[]);
367 undef_int i_writegifmc(i_img *im,int fd,int colors);
368 undef_int i_writegifex(i_img *im,int fd);
369 undef_int i_writegif_gen(i_quantize *quant, int fd, i_img **imgs, int count, i_gif_opts *opts);
370 undef_int i_writegif_callback(i_quantize *quant, i_write_callback_t cb, char *userdata, int maxbuffer, i_img **imgs, int count, i_gif_opts *opts);
371
372 void i_qdist(i_img *im);
373
374 #endif /* HAVE_LIBGIF */
375
376 i_img *i_readraw(int fd,int x,int y,int datachannels,int storechannels,int intrl);
377 undef_int i_writeraw(i_img* im,int fd);
378
379 i_img *i_readpnm_wiol(io_glue *ig, int length);
380 undef_int i_writeppm(i_img *im,int fd);
381
382
383 i_img* i_scaleaxis(i_img *im, float Value, int Axis);
384 i_img* i_scale_nn(i_img *im, float scx, float scy);
385 i_img* i_haar(i_img *im);
386 int i_count_colors(i_img *im,int maxc);
387
388 i_img* i_transform(i_img *im, int *opx,int opxl,int *opy,int opyl,double parm[],int parmlen);
389
390 struct rm_op;
391 i_img* i_transform2(int width, int height, int channels,
392                     struct rm_op *ops, int ops_count, 
393                     double *n_regs, int n_regs_count, 
394                     i_color *c_regs, int c_regs_count, 
395                     i_img **in_imgs, int in_imgs_count);
396 /* filters */
397
398 void i_contrast(i_img *im, float intensity);
399 void i_hardinvert(i_img *im);
400 void i_noise(i_img *im, float amount, unsigned char type);
401 void i_bumpmap(i_img *im,i_img *bump,int channel,int light_x,int light_y,int strength);
402 void i_postlevels(i_img *im,int levels);
403 void i_mosaic(i_img *im,int size);
404 void i_watermark(i_img *im,i_img *wmark,int tx,int ty,int pixdiff);
405 void i_autolevels(i_img *im,float lsat,float usat,float skew);
406 void i_radnoise(i_img *im,int xo,int yo,float rscale,float ascale);
407 void i_turbnoise(i_img *im,float xo,float yo,float scale);
408 void i_gradgen(i_img *im, int num, int *xo, int *yo, i_color *ival, int dmeasure);
409 void i_nearest_color(i_img *im, int num, int *xo, int *yo, i_color *ival, int dmeasure);
410
411 /* Debug only functions */
412
413 void malloc_state( void );
414
415 /* this is sort of obsolete now */
416
417 typedef struct {
418   undef_int (*i_has_format)(char *frmt);
419   i_color*(*ICL_set)(i_color *cl,unsigned char r,unsigned char g,unsigned char b,unsigned char a);
420   void (*ICL_info)(i_color *cl);
421
422   i_img*(*i_img_new)( void );
423   i_img*(*i_img_empty)(i_img *im,int x,int y);
424   i_img*(*i_img_empty_ch)(i_img *im,int x,int y,int ch);
425   void(*i_img_exorcise)(i_img *im);
426
427   void(*i_img_info)(i_img *im,int *info);
428   
429   void(*i_img_setmask)(i_img *im,int ch_mask);
430   int (*i_img_getmask)(i_img *im);
431   
432   int (*i_ppix)(i_img *im,int x,int y,i_color *val);
433   int (*i_gpix)(i_img *im,int x,int y,i_color *val);
434
435   void(*i_box)(i_img *im,int x1,int y1,int x2,int y2,i_color *val);
436   void(*i_draw)(i_img *im,int x1,int y1,int x2,int y2,i_color *val);
437   void(*i_arc)(i_img *im,int x,int y,float rad,float d1,float d2,i_color *val);
438   void(*i_copyto)(i_img *im,i_img *src,int x1,int y1,int x2,int y2,int tx,int ty);
439   void(*i_copyto_trans)(i_img *im,i_img *src,int x1,int y1,int x2,int y2,int tx,int ty,i_color *trans);
440   void(*i_rubthru)(i_img *im,i_img *src,int tx,int ty);
441
442 } symbol_table_t;
443
444 /* error handling 
445    see error.c for documentation
446    the error information is currently global
447 */
448 typedef struct {
449   char *msg;
450   int code;
451 } i_errmsg;
452 typedef void (*i_error_cb)(int code, char const *msg);
453 typedef void (*i_failed_cb)(i_errmsg *msgs);
454 extern i_error_cb i_set_error_cb(i_error_cb);
455 extern i_failed_cb i_set_failed_cb(i_failed_cb);
456 extern void i_set_argv0(char const *);
457 extern int i_set_errors_fatal(int new_fatal);
458 extern i_errmsg *i_errors();
459
460 extern void i_push_error(int code, char const *msg);
461 extern void i_push_errorf(int code, char const *fmt, ...);
462 extern void i_push_errorvf(int code, char const *fmt, va_list);
463 extern void i_clear_error();
464 extern int i_failed(int code, char const *msg);
465
466
467 #endif