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