xref: /plan9/sys/src/cmd/gs/src/gsfunc.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1997, 2000, 2002 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gsfunc.h,v 1.13 2005/04/19 14:35:12 igor Exp $ */
18 /* Generic definitions for Functions */
19 
20 #ifndef gsfunc_INCLUDED
21 #  define gsfunc_INCLUDED
22 
23 #include "gstypes.h"		/* for gs_range_t */
24 
25 #ifndef stream_DEFINED
26 #  define stream_DEFINED
27 typedef struct stream_s stream;
28 #endif
29 
30 /* ---------------- Types and structures ---------------- */
31 
32 /*
33  * gs_function_type_t is defined as equivalent to int, rather than as an
34  * enum type, because we can't enumerate all its possible values here in the
35  * generic definitions.
36  */
37 typedef int gs_function_type_t;
38 
39 /*
40  * Define information common to all Function types.
41  * We separate the private part from the parameters so that
42  * clients can create statically initialized parameter structures.
43  */
44 #define gs_function_params_common\
45     int m;			/* # of inputs */\
46     const float *Domain;	/* 2 x m */\
47     int n;			/* # of outputs */\
48     const float *Range		/* 2 x n, optional except for type 0 */
49 
50 /* Define abstract types. */
51 #ifndef gs_data_source_DEFINED
52 #  define gs_data_source_DEFINED
53 typedef struct gs_data_source_s gs_data_source_t;
54 #endif
55 #ifndef gs_param_list_DEFINED
56 #  define gs_param_list_DEFINED
57 typedef struct gs_param_list_s gs_param_list;
58 #endif
59 
60 /* Define a generic function, for use as the target type of pointers. */
61 typedef struct gs_function_params_s {
62     gs_function_params_common;
63 } gs_function_params_t;
64 #ifndef gs_function_DEFINED
65 typedef struct gs_function_s gs_function_t;
66 #  define gs_function_DEFINED
67 #endif
68 typedef struct gs_function_info_s {
69     const gs_data_source_t *DataSource;
70     ulong data_size;
71     const gs_function_t *const *Functions;
72     int num_Functions;
73 } gs_function_info_t;
74 
75 /* Evaluate a function. */
76 #define FN_EVALUATE_PROC(proc)\
77   int proc(const gs_function_t * pfn, const float *in, float *out)
78 typedef FN_EVALUATE_PROC((*fn_evaluate_proc_t));
79 
80 /* Test whether a function is monotonic. */
81 #define FN_IS_MONOTONIC_PROC(proc)\
82   int proc(const gs_function_t * pfn, const float *lower,\
83 	   const float *upper, uint *mask)
84 typedef FN_IS_MONOTONIC_PROC((*fn_is_monotonic_proc_t));
85 
86 /* Get function information. */
87 #define FN_GET_INFO_PROC(proc)\
88   void proc(const gs_function_t *pfn, gs_function_info_t *pfi)
89 typedef FN_GET_INFO_PROC((*fn_get_info_proc_t));
90 
91 /* Put function parameters on a parameter list. */
92 #define FN_GET_PARAMS_PROC(proc)\
93   int proc(const gs_function_t *pfn, gs_param_list *plist)
94 typedef FN_GET_PARAMS_PROC((*fn_get_params_proc_t));
95 
96 /*
97  * Create a new function with scaled output.  The i'th output value is
98  * transformed linearly so that [0 .. 1] are mapped to [pranges[i].rmin ..
99  * pranges[i].rmax].  Any necessary parameters or subfunctions of the
100  * original function are copied, not shared, even if their values aren't
101  * changed, so that the new function can be freed without having to worry
102  * about freeing data that should be kept.  Note that if there is a "data
103  * source", it is shared, not copied: this should not be a problem, since
104  * gs_function_free does not free the data source.
105  */
106 #define FN_MAKE_SCALED_PROC(proc)\
107   int proc(const gs_function_t *pfn, gs_function_t **ppsfn,\
108 	   const gs_range_t *pranges, gs_memory_t *mem)
109 typedef FN_MAKE_SCALED_PROC((*fn_make_scaled_proc_t));
110 
111 /* Free function parameters. */
112 #define FN_FREE_PARAMS_PROC(proc)\
113   void proc(gs_function_params_t * params, gs_memory_t * mem)
114 typedef FN_FREE_PARAMS_PROC((*fn_free_params_proc_t));
115 
116 /* Free a function. */
117 #define FN_FREE_PROC(proc)\
118   void proc(gs_function_t * pfn, bool free_params, gs_memory_t * mem)
119 typedef FN_FREE_PROC((*fn_free_proc_t));
120 
121 /* Serialize a function. */
122 #define FN_SERIALIZE_PROC(proc)\
123   int proc(const gs_function_t * pfn, stream *s)
124 typedef FN_SERIALIZE_PROC((*fn_serialize_proc_t));
125 
126 /* Define the generic function structures. */
127 typedef struct gs_function_procs_s {
128     fn_evaluate_proc_t evaluate;
129     fn_is_monotonic_proc_t is_monotonic;
130     fn_get_info_proc_t get_info;
131     fn_get_params_proc_t get_params;
132     fn_make_scaled_proc_t make_scaled;
133     fn_free_params_proc_t free_params;
134     fn_free_proc_t free;
135     fn_serialize_proc_t serialize;
136 } gs_function_procs_t;
137 typedef struct gs_function_head_s {
138     gs_function_type_t type;
139     gs_function_procs_t procs;
140 } gs_function_head_t;
141 struct gs_function_s {
142     gs_function_head_t head;
143     gs_function_params_t params;
144 };
145 
146 #define FunctionType(pfn) ((pfn)->head.type)
147 
148 /*
149  * Each specific function type has a definition in its own header file
150  * for its parameter record.  In order to keep names from overflowing
151  * various compilers' limits, we take the name of the function type and
152  * reduce it to the first and last letter of each word, e.g., for
153  * Sampled functions, XxYy is Sd.
154 
155 typedef struct gs_function_XxYy_params_s {
156      gs_function_params_common;
157     << P additional members >>
158 } gs_function_XxYy_params_t;
159 #define private_st_function_XxYy()\
160   gs_private_st_suffix_addP(st_function_XxYy, gs_function_XxYy_t,\
161     "gs_function_XxYy_t", function_XxYy_enum_ptrs, function_XxYy_reloc_ptrs,\
162     st_function, <<params.additional_members>>)
163 
164  */
165 
166 /* ---------------- Procedures ---------------- */
167 
168 /*
169  * Each specific function type has a pair of procedures in its own
170  * header file, one to allocate and initialize an instance of that type,
171  * and one to free the parameters of that type.
172 
173 int gs_function_XxYy_init(gs_function_t **ppfn,
174 			  const gs_function_XxYy_params_t *params,
175 			  gs_memory_t *mem));
176 
177 void gs_function_XxYy_free_params(gs_function_XxYy_params_t *params,
178 				  gs_memory_t *mem);
179 
180  */
181 
182 /* Allocate an array of function pointers. */
183 int alloc_function_array(uint count, gs_function_t *** pFunctions,
184 			 gs_memory_t *mem);
185 
186 /* Evaluate a function. */
187 #define gs_function_evaluate(pfn, in, out)\
188   ((pfn)->head.procs.evaluate)(pfn, in, out)
189 
190 /*
191  * Test whether a function is monotonic on a given (closed) interval.
192  * return 1 = monotonic, 0 = not or don't know, <0 = error..
193  * Sets mask : 1 bit per dimension :
194  *    1 - non-monotonic or don't know,
195  *    0 - monotonic.
196  * If lower[i] > upper[i], the result may be not defined.
197  */
198 #define gs_function_is_monotonic(pfn, lower, upper, mask)\
199   ((pfn)->head.procs.is_monotonic)(pfn, lower, upper, mask)
200 
201 /* Get function information. */
202 #define gs_function_get_info(pfn, pfi)\
203   ((pfn)->head.procs.get_info(pfn, pfi))
204 
205 /* Write function parameters. */
206 #define gs_function_get_params(pfn, plist)\
207   ((pfn)->head.procs.get_params(pfn, plist))
208 
209 /* Create a scaled function. */
210 #define gs_function_make_scaled(pfn, ppsfn, pranges, mem)\
211   ((pfn)->head.procs.make_scaled(pfn, ppsfn, pranges, mem))
212 
213 /* Free function parameters. */
214 #define gs_function_free_params(pfn, mem)\
215   ((pfn)->head.procs.free_params(&(pfn)->params, mem))
216 
217 /* Free a function's implementation, optionally including its parameters. */
218 #define gs_function_free(pfn, free_params, mem)\
219   ((pfn)->head.procs.free(pfn, free_params, mem))
220 
221 /* Serialize a function. */
222 #define gs_function_serialize(pfn, s)\
223   ((pfn)->head.procs.serialize(pfn, s))
224 
225 #endif /* gsfunc_INCLUDED */
226