1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/* Copyright (C) 2001-2006 artofcode LLC.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied, modified
or distributed except as expressly authorized under the terms of that
license. Refer to licensing information at http://www.artifex.com/
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
*/
/* $Id$ */
/* Generic Function support */
#include "memory_.h"
#include "gx.h"
#include "gserrors.h"
#include "gsparam.h"
#include "gxfunc.h"
#include "stream.h"
/* GC descriptors */
public_st_function();
gs_private_st_ptr(st_function_ptr, gs_function_t *, "gs_function_t *",
function_ptr_enum_ptrs, function_ptr_reloc_ptrs);
gs_private_st_element(st_function_ptr_element, gs_function_t *,
"gs_function_t *[]", function_ptr_element_enum_ptrs,
function_ptr_element_reloc_ptrs, st_function_ptr);
/* Allocate an array of function pointers. */
int
alloc_function_array(uint count, gs_function_t *** pFunctions,
gs_memory_t *mem)
{
gs_function_t **ptr;
if (count == 0)
return_error(gs_error_rangecheck);
ptr = gs_alloc_struct_array(mem, count, gs_function_t *,
&st_function_ptr_element, "Functions");
if (ptr == 0)
return_error(gs_error_VMerror);
memset(ptr, 0, sizeof(*ptr) * count);
*pFunctions = ptr;
return 0;
}
/* Generic free_params implementation. */
void
fn_common_free_params(gs_function_params_t * params, gs_memory_t * mem)
{
gs_free_const_object(mem, params->Range, "Range");
gs_free_const_object(mem, params->Domain, "Domain");
}
/* Generic free implementation. */
void
fn_common_free(gs_function_t * pfn, bool free_params, gs_memory_t * mem)
{
if (free_params)
gs_function_free_params(pfn, mem);
gs_free_object(mem, pfn, "fn_common_free");
}
/* Check the values of m, n, Domain, and (if supplied) Range. */
int
fn_check_mnDR(const gs_function_params_t * params, int m, int n)
{
int i;
if (m <= 0 || n <= 0)
return_error(gs_error_rangecheck);
for (i = 0; i < m; ++i)
if (params->Domain[2 * i] > params->Domain[2 * i + 1])
return_error(gs_error_rangecheck);
if (params->Range != 0)
for (i = 0; i < n; ++i)
if (params->Range[2 * i] > params->Range[2 * i + 1])
return_error(gs_error_rangecheck);
return 0;
}
/* Return default function information. */
void
gs_function_get_info_default(const gs_function_t *pfn, gs_function_info_t *pfi)
{
pfi->DataSource = 0;
pfi->Functions = 0;
}
/*
* Write generic parameters (FunctionType, Domain, Range) on a parameter list.
*/
int
fn_common_get_params(const gs_function_t *pfn, gs_param_list *plist)
{
int ecode = param_write_int(plist, "FunctionType", &FunctionType(pfn));
int code;
if (pfn->params.Domain) {
code = param_write_float_values(plist, "Domain", pfn->params.Domain,
2 * pfn->params.m, false);
if (code < 0)
ecode = code;
}
if (pfn->params.Range) {
code = param_write_float_values(plist, "Range", pfn->params.Range,
2 * pfn->params.n, false);
if (code < 0)
ecode = code;
}
return ecode;
}
/*
* Copy an array of numeric values when scaling a function.
*/
void *
fn_copy_values(const void *pvalues, int count, int size, gs_memory_t *mem)
{
if (pvalues) {
void *values = gs_alloc_byte_array(mem, count, size, "fn_copy_values");
if (values)
memcpy(values, pvalues, count * size);
return values;
} else
return 0; /* caller must check */
}
/*
* If necessary, scale the Range or Decode array for fn_make_scaled.
* Note that we must always allocate a new array.
*/
int
fn_scale_pairs(const float **ppvalues, const float *pvalues, int npairs,
const gs_range_t *pranges, gs_memory_t *mem)
{
if (pvalues == 0)
*ppvalues = 0;
else {
float *out = (float *)
gs_alloc_byte_array(mem, 2 * npairs, sizeof(*pvalues),
"fn_scale_pairs");
*ppvalues = out;
if (out == 0)
return_error(gs_error_VMerror);
if (pranges) {
/* Allocate and compute scaled ranges. */
int i;
for (i = 0; i < npairs; ++i) {
double base = pranges[i].rmin, factor = pranges[i].rmax - base;
out[2 * i] = pvalues[2 * i] * factor + base;
out[2 * i + 1] = pvalues[2 * i + 1] * factor + base;
}
} else
memcpy(out, pvalues, 2 * sizeof(*pvalues) * npairs);
}
return 0;
}
/*
* Scale the generic part of a function (Domain and Range).
* The client must have copied the parameters already.
*/
int
fn_common_scale(gs_function_t *psfn, const gs_function_t *pfn,
const gs_range_t *pranges, gs_memory_t *mem)
{
int code;
psfn->head = pfn->head;
psfn->params.Domain = 0; /* in case of failure */
psfn->params.Range = 0;
if ((code = fn_scale_pairs(&psfn->params.Domain, pfn->params.Domain,
pfn->params.m, NULL, mem)) < 0 ||
(code = fn_scale_pairs(&psfn->params.Range, pfn->params.Range,
pfn->params.n, pranges, mem)) < 0)
return code;
return 0;
}
/* Serialize. */
int
fn_common_serialize(const gs_function_t * pfn, stream *s)
{
uint n;
const gs_function_params_t * p = &pfn->params;
int code = sputs(s, (const byte *)&pfn->head.type, sizeof(pfn->head.type), &n);
const float dummy[8] = {0, 0, 0, 0, 0, 0, 0, 0};
if (code < 0)
return code;
code = sputs(s, (const byte *)&p->m, sizeof(p->m), &n);
if (code < 0)
return code;
code = sputs(s, (const byte *)&p->Domain[0], sizeof(p->Domain[0]) * p->m * 2, &n);
if (code < 0)
return code;
code = sputs(s, (const byte *)&p->n, sizeof(p->n), &n);
if (code < 0)
return code;
if (p->Range == NULL && p->n * 2 > count_of(dummy))
return_error(gs_error_unregistered); /* Unimplemented. */
return sputs(s, (const byte *)(p->Range != NULL ? &p->Range[0] : dummy),
sizeof(p->Range[0]) * p->n * 2, &n);
}
|