/src/ghostpdl/psi/zfsample.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2022 Artifex Software, Inc. |
2 | | All Rights Reserved. |
3 | | |
4 | | This software is provided AS-IS with no warranty, either express or |
5 | | implied. |
6 | | |
7 | | This software is distributed under license and may not be copied, |
8 | | modified or distributed except as expressly authorized under the terms |
9 | | of the license contained in the file LICENSE in this distribution. |
10 | | |
11 | | Refer to licensing information at http://www.artifex.com or contact |
12 | | Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato, |
13 | | CA 94945, U.S.A., +1(415)492-9861, for further information. |
14 | | */ |
15 | | |
16 | | |
17 | | /* Sample data to create a type 0 function */ |
18 | | #include "memory_.h" |
19 | | #include "ghost.h" |
20 | | #include "oper.h" |
21 | | #include "gxcspace.h" |
22 | | #include "estack.h" |
23 | | #include "ialloc.h" |
24 | | #include "idict.h" |
25 | | #include "idparam.h" |
26 | | #include "ifunc.h" |
27 | | #include "ostack.h" |
28 | | #include "store.h" |
29 | | #include "gsfunc0.h" |
30 | | #include "gscdevn.h" |
31 | | #include "zfunc.h" |
32 | | #include "zcolor.h" |
33 | | |
34 | | /* |
35 | | * We store the data in a string. Since the max size for a string is 64k, |
36 | | * we use that as our max data size. |
37 | | */ |
38 | 69 | #define MAX_DATA_SIZE 0x10000 |
39 | | /* |
40 | | * We cannot handle more than 16 inputs. Otherwise the the data will not |
41 | | * fit within MAX_DATA_SIZE. |
42 | | */ |
43 | 0 | #define MAX_NUM_INPUTS 16 |
44 | | /* |
45 | | * This value is rather arbitrary. |
46 | | */ |
47 | 0 | #define MAX_NUM_OUTPUTS 128 |
48 | | |
49 | | /* --- Build sampled data function --- */ |
50 | | |
51 | | /* |
52 | | * This structure is used to hold data required while collecting samples |
53 | | * for a type 0 function (sampled data). |
54 | | */ |
55 | | struct gs_sampled_data_enum_s { |
56 | | int indexes[MAX_NUM_INPUTS]; |
57 | | int o_stack_depth; /* used to verify stack while sampling */ |
58 | | gs_function_t * pfn; |
59 | | }; |
60 | | |
61 | | typedef struct gs_sampled_data_enum_s gs_sampled_data_enum; |
62 | | |
63 | | gs_private_st_ptrs1(st_gs_sampled_data_enum, gs_sampled_data_enum, |
64 | | "gs_sampled_data_enum", gs_sampled_data_enum_enum_ptrs, |
65 | | gs_sampled_data_enum_reloc_ptrs, pfn); |
66 | | |
67 | | /* Forward references */ |
68 | | |
69 | | static int cube_build_func0(const ref * pdict, |
70 | | gs_function_Sd_params_t * params, gs_memory_t *mem); |
71 | | static int sampled_data_setup(i_ctx_t *i_ctx_p, gs_function_t *pfn, |
72 | | const ref * pproc, int (*finish_proc)(i_ctx_t *), |
73 | | gs_memory_t * mem); |
74 | | static int sampled_data_sample(i_ctx_t *i_ctx_p); |
75 | | static int sampled_data_continue(i_ctx_t *i_ctx_p); |
76 | | static int sampled_data_finish(i_ctx_t *i_ctx_p); |
77 | | |
78 | | static gs_sampled_data_enum * gs_sampled_data_enum_alloc |
79 | | (gs_memory_t * mem, client_name_t cname); |
80 | | |
81 | | /* |
82 | | * Collect data for a type 0 (sampled data) function |
83 | | * <dict> .buildsampledfunction <function_struct> |
84 | | * |
85 | | * The following keys are used from the dictionary: |
86 | | * Function (required) |
87 | | * Domain (required) |
88 | | * Range (required) |
89 | | * Size (optional) If Size is not specified then a default value is determined |
90 | | * based upon the number of inputs and outputs. |
91 | | * BitsPerSample (required) Only 8, 16, 24, and 32 accepted, |
92 | | * The remaining keys are ignored. |
93 | | */ |
94 | | static int |
95 | | zbuildsampledfunction(i_ctx_t *i_ctx_p) |
96 | 0 | { |
97 | 0 | os_ptr op = osp; |
98 | 0 | const ref * pdict = op; |
99 | 0 | ref * pfunc; |
100 | 0 | int code = 0; |
101 | 0 | gs_function_t *pfn; |
102 | 0 | gs_function_Sd_params_t params = {0}; |
103 | |
|
104 | 0 | check_type(*pdict, t_dictionary); |
105 | | /* |
106 | | * Check procedure to be sampled. |
107 | | */ |
108 | 0 | if (dict_find_string(pdict, "Function", &pfunc) <= 0) |
109 | 0 | return_error(gs_error_rangecheck); |
110 | 0 | check_proc(*pfunc); |
111 | | /* |
112 | | * Set up the hyper cube function data structure. |
113 | | */ |
114 | 0 | code = cube_build_func0(pdict, ¶ms, imemory); |
115 | 0 | if (code < 0) |
116 | 0 | return code; |
117 | | /* |
118 | | * This is temporary. We will call gs_function_Sd_init again after |
119 | | * we have collected the cube data. We are doing it now because we need |
120 | | * a function structure created (along with its GC enumeration stuff) |
121 | | * that we can use while collecting the cube data. We will call |
122 | | * the routine again after the cube data is collected to correctly |
123 | | * initialize the function. |
124 | | */ |
125 | 0 | code = gs_function_Sd_init(&pfn, ¶ms, imemory); |
126 | 0 | if (code < 0) |
127 | 0 | return code; |
128 | | /* |
129 | | * Now setup to collect the sample data. |
130 | | */ |
131 | 0 | return sampled_data_setup(i_ctx_p, pfn, pfunc, sampled_data_finish, imemory); |
132 | 0 | } |
133 | | |
134 | | /* ------- Internal procedures ------- */ |
135 | | |
136 | 34.9k | #define bits2bytes(x) ((x) >> 3) /* Convert bit count to byte count */ |
137 | | |
138 | | /* |
139 | | * This routine will verify that the requested data hypercube parameters will require |
140 | | * a data storage size less than or equal to the MAX_DATA_SIZE. |
141 | | */ |
142 | | static bool |
143 | | valid_cube_size(int num_inputs, int num_outputs, int sample_size, const int Size[]) |
144 | 69 | { |
145 | 69 | int i, total_size = num_outputs * sample_size; |
146 | | |
147 | 138 | for (i = 0; i < num_inputs; i++) { |
148 | 69 | if (Size[i] <= 0 || Size[i] > MAX_DATA_SIZE / total_size) |
149 | 0 | return false; |
150 | 69 | total_size *= Size[i]; |
151 | 69 | } |
152 | 69 | return true; |
153 | 69 | } |
154 | | |
155 | | /* |
156 | | * This routine is used to determine a default value for the sampled data size. |
157 | | * As a default, we will build a hyper cube with each side having the same |
158 | | * size. The space requirements for a hypercube grow exponentially with the |
159 | | * number of dimensions. Thus we must use fewer points if our functions has |
160 | | * many inputs. The values returned were chosen simply to given a reasonable |
161 | | * tradeoff between keeping storage requirements low but still having enough |
162 | | * points per side to minimize loss of information. |
163 | | * |
164 | | * We do check to see if the data will fit using our initial guess. If not |
165 | | * then we decrement the size of each edge until it fits. We will return a |
166 | | * gs_error_rangecheck error if the cube can not fit into the maximum size. |
167 | | * On exit the Size array contains the cube size (if a valid size was found). |
168 | | */ |
169 | | static int |
170 | | determine_sampled_data_size(int num_inputs, int num_outputs, |
171 | | int sample_size, int Size[]) |
172 | 69 | { |
173 | 69 | static const int size_list[] = {512, 50, 20, 10, 7, 5, 4, 3}; |
174 | 69 | int i, size; |
175 | | |
176 | | /* Start with initial guess at cube size */ |
177 | 69 | if (num_inputs > 0 && num_inputs <= 8) |
178 | 69 | size = size_list[num_inputs - 1]; |
179 | 0 | else |
180 | 0 | size = 2; |
181 | | /* |
182 | | * Verify that the cube will fit into MAX_DATA_SIZE. If not then |
183 | | * decrement the cube size until it will fit. |
184 | | */ |
185 | 69 | while (true) { |
186 | | /* Fill Size array with value. */ |
187 | 138 | for (i = 0; i < num_inputs; i++) |
188 | 69 | Size[i] = size; |
189 | | |
190 | | /* If we have reached the minimum size (2), don't bother checking if its 'valid' |
191 | | * as there is nothing we cna do now if it isn't. |
192 | | */ |
193 | 69 | if (size > 2) { |
194 | 69 | if (valid_cube_size(num_inputs, num_outputs, sample_size, Size)) |
195 | 69 | return 0; /* We have a valid size */ |
196 | 0 | size--; |
197 | 0 | } else { |
198 | 0 | return 0; |
199 | 0 | } |
200 | 69 | } |
201 | 69 | } |
202 | | |
203 | | /* |
204 | | * Allocate the enumerator used while collecting sampled data. This enumerator |
205 | | * is used to hold the various state data required while sampling. |
206 | | */ |
207 | | static gs_sampled_data_enum * |
208 | | gs_sampled_data_enum_alloc(gs_memory_t * mem, client_name_t cname) |
209 | 69 | { |
210 | 69 | return gs_alloc_struct(mem, gs_sampled_data_enum, |
211 | 69 | &st_gs_sampled_data_enum, cname); |
212 | 69 | } |
213 | | |
214 | | /* |
215 | | * This routine will determine the location of a block of data |
216 | | * in the hyper cube. Basically this does an index calculation |
217 | | * for an n dimensional cube. |
218 | | */ |
219 | | static byte * |
220 | | cube_ptr_from_index(gs_function_Sd_params_t * params, int indexes[]) |
221 | 17.4k | { |
222 | 17.4k | int i, sum = indexes[params->m - 1]; |
223 | | |
224 | 17.4k | for (i = params->m - 2; i >= 0; i--) { |
225 | 0 | sum *= params->Size[i]; |
226 | 0 | sum += indexes[i]; |
227 | 0 | } |
228 | 17.4k | return (byte *)(params->DataSource.data.str.data) + |
229 | 17.4k | sum * params->n * bits2bytes(params->BitsPerSample); |
230 | 17.4k | } |
231 | | |
232 | | /* |
233 | | * This routine will increment the index values for the hypercube. This |
234 | | * is used for collecting the data. If we have incremented the |
235 | | * last index beyond its last value then we return a true, else false; |
236 | | */ |
237 | | static bool |
238 | | increment_cube_indexes(gs_function_Sd_params_t * params, int indexes[]) |
239 | 17.4k | { |
240 | 17.4k | int i = 0; |
241 | | |
242 | 17.4k | while (true) { |
243 | | /* |
244 | | * Increment an index value for an edge and test if we have |
245 | | * gone past the final value for the edge. |
246 | | */ |
247 | 17.4k | indexes[i]++; |
248 | 17.4k | if (indexes[i] < params->Size[i]) |
249 | | /* |
250 | | * We have not reached the end of the edge. Exit but |
251 | | * indicate that we are not done with the hypercube. |
252 | | */ |
253 | 17.3k | return false; |
254 | | /* |
255 | | * We have reached the end of one edge of the hypercube and we |
256 | | * need to increment the next index. |
257 | | */ |
258 | 34 | indexes[i] = 0; |
259 | 34 | i++; |
260 | 34 | if (i == params->m) |
261 | | /* |
262 | | * We have finished the last edge of the hyper cube. |
263 | | * We are done. |
264 | | */ |
265 | 34 | return true; |
266 | 34 | } |
267 | 17.4k | } |
268 | | |
269 | | /* |
270 | | * Fill in the data for a function type 0 parameter object to be used while |
271 | | * we collect the data for the data cube. At the end of the process, we |
272 | | * will create a function type 0 object to be used to calculate values |
273 | | * as a replacement for the original function. |
274 | | */ |
275 | | static int |
276 | | cube_build_func0(const ref * pdict, gs_function_Sd_params_t * params, |
277 | | gs_memory_t *mem) |
278 | 0 | { |
279 | 0 | byte * bytes = 0; |
280 | 0 | int code, i; |
281 | 0 | int total_size; |
282 | |
|
283 | 0 | if ((code = dict_int_param(pdict, "Order", 1, 3, 1, ¶ms->Order)) < 0 || |
284 | 0 | (code = dict_int_param(pdict, "BitsPerSample", 1, 32, 0, |
285 | 0 | ¶ms->BitsPerSample)) < 0 || |
286 | 0 | ((code = params->m = |
287 | 0 | fn_build_float_array(pdict, "Domain", false, true, |
288 | 0 | ¶ms->Domain, mem)) < 0 ) || |
289 | 0 | ((code = params->n = |
290 | 0 | fn_build_float_array(pdict, "Range", false, true, |
291 | 0 | ¶ms->Range, mem)) < 0) |
292 | 0 | ) { |
293 | 0 | goto fail; |
294 | 0 | } |
295 | | /* |
296 | | * The previous logic set the size of m and n to the size of the Domain |
297 | | * and Range arrays. This is twice the actual size. Correct this and |
298 | | * check for valid values. |
299 | | */ |
300 | 0 | params->m >>= 1; |
301 | 0 | params->n >>= 1; |
302 | 0 | if (params->m == 0 || params->n == 0 || |
303 | 0 | params->m > MAX_NUM_INPUTS || params->n > MAX_NUM_OUTPUTS) { |
304 | 0 | code = gs_note_error(gs_error_rangecheck); |
305 | 0 | goto fail; |
306 | 0 | } |
307 | | /* |
308 | | * The Size array may or not be specified. If it is not specified then |
309 | | * we need to determine a set of default values for the Size array. |
310 | | */ |
311 | 0 | { |
312 | 0 | int *ptr = (int *) |
313 | 0 | gs_alloc_byte_array(mem, params->m, sizeof(int), "Size"); |
314 | |
|
315 | 0 | if (ptr == NULL) { |
316 | 0 | code = gs_note_error(gs_error_VMerror); |
317 | 0 | goto fail; |
318 | 0 | } |
319 | 0 | params->Size = ptr; |
320 | 0 | code = dict_ints_param(mem, pdict, "Size", params->m, ptr); |
321 | 0 | if (code < 0) |
322 | 0 | goto fail; |
323 | 0 | if (code == 0) { |
324 | | /* |
325 | | * The Size array has not been specified. Determine a default |
326 | | * set of values. |
327 | | */ |
328 | 0 | code = determine_sampled_data_size(params->m, params->n, |
329 | 0 | params->BitsPerSample, (int *)params->Size); |
330 | 0 | if (code < 0) |
331 | 0 | goto fail; |
332 | 0 | } |
333 | 0 | else { /* Size array specified - verify valid */ |
334 | 0 | if (code != params->m || !valid_cube_size(params->m, params->n, |
335 | 0 | params->BitsPerSample, params->Size)) { |
336 | 0 | code = gs_note_error(gs_error_rangecheck); |
337 | 0 | goto fail; |
338 | 0 | } |
339 | 0 | } |
340 | 0 | } |
341 | | /* |
342 | | * Determine space required for the sample data storage. |
343 | | */ |
344 | 0 | total_size = params->n * bits2bytes(params->BitsPerSample); |
345 | 0 | for (i = 0; i < params->m; i++) |
346 | 0 | total_size *= params->Size[i]; |
347 | | /* |
348 | | * Allocate space for the data cube itself. |
349 | | */ |
350 | 0 | bytes = gs_alloc_byte_array(mem, total_size, 1, "cube_build_func0(bytes)"); |
351 | 0 | if (!bytes) { |
352 | 0 | code = gs_note_error(gs_error_VMerror); |
353 | 0 | goto fail; |
354 | 0 | } |
355 | 0 | data_source_init_bytes(¶ms->DataSource, |
356 | 0 | (const unsigned char *)bytes, total_size); |
357 | |
|
358 | 0 | return 0; |
359 | | |
360 | 0 | fail: |
361 | 0 | gs_function_Sd_free_params(params, mem); |
362 | 0 | return (code < 0 ? code : gs_note_error(gs_error_rangecheck)); |
363 | 0 | } |
364 | | |
365 | | /* |
366 | | * Layout of stuff pushed on estack while collecting the sampled data. |
367 | | * The data is saved there since it is safe from attack by the procedure |
368 | | * being sampled and is convient. |
369 | | * |
370 | | * finishing procedure (or 0) |
371 | | * procedure being sampled |
372 | | * enumeration structure (as bytes) |
373 | | */ |
374 | 107 | #define estack_storage 3 |
375 | 68 | #define esp_finish_proc (*real_opproc(esp - 2)) |
376 | 10.3k | #define sample_proc esp[-1] |
377 | 27.6k | #define senum r_ptr(esp, gs_sampled_data_enum) |
378 | | /* |
379 | | * Sone invalid tint transform functions pop more items off of the stack |
380 | | * then they are supposed to use. This is a violation of the PLRM however |
381 | | * this is done by Adobe and we have to handle the situation. This is |
382 | | * a kludge but we set aside some unused stack space below the input |
383 | | * variables. The tint transform can trash this without causing any |
384 | | * real problems. |
385 | | */ |
386 | 50.4k | #define O_STACK_PAD 3 |
387 | | |
388 | | /* |
389 | | * Set up to collect the data for the sampled function. This is used for |
390 | | * those alternate tint transforms that cannot be converted into a |
391 | | * type 4 function. |
392 | | */ |
393 | | static int |
394 | | sampled_data_setup(i_ctx_t *i_ctx_p, gs_function_t *pfn, |
395 | | const ref * pproc, int (*finish_proc)(i_ctx_t *), gs_memory_t * mem) |
396 | 69 | { |
397 | 69 | os_ptr op = osp; |
398 | 69 | gs_sampled_data_enum *penum; |
399 | 69 | int i; |
400 | 69 | gs_function_Sd_params_t * params = (gs_function_Sd_params_t *)&pfn->params; |
401 | | |
402 | 69 | check_estack(estack_storage + 1); /* Verify space on estack */ |
403 | 69 | check_ostack(params->m + O_STACK_PAD); /* and the operand stack */ |
404 | 69 | check_ostack(params->n + O_STACK_PAD); |
405 | | |
406 | | /* |
407 | | * Allocate space for the enumerator data structure. |
408 | | */ |
409 | 69 | penum = gs_sampled_data_enum_alloc(imemory, "zbuildsampledfuntion(params)"); |
410 | 69 | if (penum == NULL) |
411 | 0 | return_error(gs_error_VMerror); |
412 | | |
413 | | /* Initialize data in the enumeration structure */ |
414 | | |
415 | 69 | penum->pfn = pfn; |
416 | 138 | for(i=0; i< params->m; i++) |
417 | 69 | penum->indexes[i] = 0; |
418 | | /* |
419 | | * Save stack depth for checking the correct number of values on stack |
420 | | * after the function, which is being sampled, is called. |
421 | | */ |
422 | 69 | penum->o_stack_depth = ref_stack_count(&o_stack); |
423 | | /* |
424 | | * Note: As previously mentioned, we are putting some spare (unused) stack |
425 | | * space under the input values in case the function unbalances the stack. |
426 | | * It is possible for the function to pop or change values on the stack |
427 | | * outside of the input values. (This has been found to happen with some |
428 | | * proc sets from Adobe.) |
429 | | */ |
430 | 69 | push(O_STACK_PAD); |
431 | 276 | for (i = 0; i < O_STACK_PAD; i++) /* Set space = null */ |
432 | 207 | make_null(op - i); |
433 | | |
434 | | /* Push everything on the estack */ |
435 | | |
436 | 69 | esp += estack_storage; |
437 | 69 | make_op_estack(esp - 2, finish_proc); /* Finish proc onto estack */ |
438 | 69 | sample_proc = *pproc; /* Save function to be sampled */ |
439 | 69 | make_istruct(esp, 0, penum); /* Color cube enumeration structure */ |
440 | 69 | push_op_estack(sampled_data_sample); /* Start sampling data */ |
441 | 69 | return o_push_estack; |
442 | 69 | } |
443 | | |
444 | | /* |
445 | | * Set up to collect the next sampled data value. |
446 | | */ |
447 | | static int |
448 | | sampled_data_sample(i_ctx_t *i_ctx_p) |
449 | 10.2k | { |
450 | 10.2k | os_ptr op = osp; |
451 | 10.2k | gs_sampled_data_enum *penum = senum; |
452 | 10.2k | ref proc; |
453 | 10.2k | gs_function_Sd_params_t * params = |
454 | 10.2k | (gs_function_Sd_params_t *)&penum->pfn->params; |
455 | 10.2k | int num_inputs = params->m; |
456 | 10.2k | int i; |
457 | | |
458 | | /* Put set of input values onto the stack. */ |
459 | 10.2k | push(num_inputs); |
460 | 20.4k | for (i = 0; i < num_inputs; i++) { |
461 | 10.2k | double dmin = params->Domain[2 * i]; |
462 | 10.2k | double dmax = params->Domain[2 * i + 1]; |
463 | | |
464 | 10.2k | make_real(op - num_inputs + i + 1, (float) ( |
465 | 10.2k | penum->indexes[i] * (dmax - dmin)/(params->Size[i] - 1) + dmin)); |
466 | 10.2k | } |
467 | | |
468 | 10.2k | proc = sample_proc; /* Get procedure from storage */ |
469 | 10.2k | push_op_estack(sampled_data_continue); /* Put 'save' routine on estack, after sample proc */ |
470 | 10.2k | *++esp = proc; /* Put procedure to be executed */ |
471 | 10.2k | return o_push_estack; |
472 | 10.2k | } |
473 | | |
474 | | /* |
475 | | * Continuation procedure for processing sampled values. |
476 | | */ |
477 | | static int |
478 | | sampled_data_continue(i_ctx_t *i_ctx_p) |
479 | 17.4k | { |
480 | 17.4k | os_ptr op = osp; |
481 | 17.4k | gs_sampled_data_enum *penum = senum; |
482 | 17.4k | gs_function_Sd_params_t * params = |
483 | 17.4k | (gs_function_Sd_params_t *)&penum->pfn->params; |
484 | 17.4k | int i, j, num_out = params->n; |
485 | 17.4k | int code = 0; |
486 | 17.4k | byte * data_ptr; |
487 | 17.4k | double sampled_data_value_max = (double)((1 << params->BitsPerSample) - 1); |
488 | 17.4k | int bps = bits2bytes(params->BitsPerSample), stack_depth_adjust = 0; |
489 | | |
490 | | /* |
491 | | * Check to make sure that the procedure produced the correct number of |
492 | | * values. If not, move the stack back to where it belongs and abort |
493 | | */ |
494 | 17.4k | if (num_out + O_STACK_PAD + penum->o_stack_depth != ref_stack_count(&o_stack)) { |
495 | 16.3k | stack_depth_adjust = ref_stack_count(&o_stack) - penum->o_stack_depth; |
496 | | |
497 | 16.3k | if (stack_depth_adjust < 0) { |
498 | | /* |
499 | | * If we get to here then there were major problems. The function |
500 | | * removed too many items off of the stack. We had placed extra |
501 | | * (unused) stack stack space to allow for this but the function |
502 | | * exceeded even that. Data on the stack may have been lost. |
503 | | * The only thing that we can do is move the stack pointer back and |
504 | | * hope. |
505 | | */ |
506 | 4 | push(-stack_depth_adjust); |
507 | 4 | return_error(gs_error_undefinedresult); |
508 | 4 | } |
509 | 16.3k | } |
510 | | |
511 | | /* Save data from the given function */ |
512 | 17.4k | data_ptr = cube_ptr_from_index(params, penum->indexes); |
513 | 87.0k | for (i=0; i < num_out; i++) { |
514 | 69.6k | ulong cv; |
515 | 69.6k | double value; |
516 | 69.6k | double rmin = params->Range[2 * i]; |
517 | 69.6k | double rmax = params->Range[2 * i + 1]; |
518 | | |
519 | 69.6k | code = real_param(op + i - num_out + 1, &value); |
520 | 69.6k | if (code < 0) { |
521 | 4 | esp -= estack_storage; |
522 | 4 | return code; |
523 | 4 | } |
524 | 69.6k | if (value < rmin) |
525 | 0 | value = rmin; |
526 | 69.6k | else if (value > rmax) |
527 | 142 | value = rmax; |
528 | 69.6k | value = (value - rmin) / (rmax - rmin); /* Convert to 0 to 1.0 */ |
529 | 69.6k | cv = (int) (value * sampled_data_value_max + 0.5); |
530 | 208k | for (j = 0; j < bps; j++) |
531 | 139k | data_ptr[bps * i + j] = (byte)(cv >> ((bps - 1 - j) * 8)); /* MSB first */ |
532 | 69.6k | } |
533 | | |
534 | 17.4k | pop(num_out); /* Move op to base of result values */ |
535 | | |
536 | | /* From here on, we have to use ref_stack_pop() rather than pop() |
537 | | so that it handles stack extension blocks properly, before calling |
538 | | sampled_data_sample() which also uses the op stack. |
539 | | */ |
540 | | /* Check if we are done collecting data. */ |
541 | 17.4k | if (increment_cube_indexes(params, penum->indexes)) { |
542 | 34 | if (stack_depth_adjust == 0) |
543 | 2 | ref_stack_pop(&o_stack, O_STACK_PAD); /* Remove spare stack space */ |
544 | 32 | else |
545 | 32 | ref_stack_pop(&o_stack, stack_depth_adjust - num_out); |
546 | | /* Execute the closing procedure, if given */ |
547 | 34 | code = 0; |
548 | 34 | if (esp_finish_proc != 0) |
549 | 34 | code = esp_finish_proc(i_ctx_p); |
550 | | |
551 | 34 | return code; |
552 | 17.3k | } else { |
553 | 17.3k | if (stack_depth_adjust) { |
554 | 16.3k | stack_depth_adjust -= num_out; |
555 | 16.3k | if ((O_STACK_PAD - stack_depth_adjust) < 0) { |
556 | 16.3k | stack_depth_adjust = -(O_STACK_PAD - stack_depth_adjust); |
557 | 16.3k | check_op(stack_depth_adjust); |
558 | 9.15k | ref_stack_pop(&o_stack, stack_depth_adjust); |
559 | 9.15k | } |
560 | 0 | else { |
561 | 0 | check_ostack(O_STACK_PAD - stack_depth_adjust); |
562 | 0 | ref_stack_push(&o_stack, O_STACK_PAD - stack_depth_adjust); |
563 | 0 | for (i=0;i<O_STACK_PAD - stack_depth_adjust;i++) |
564 | 0 | make_null(op - i); |
565 | 0 | } |
566 | 16.3k | } |
567 | 17.3k | } |
568 | | |
569 | | /* Now get the data for the next location */ |
570 | | |
571 | 10.1k | return sampled_data_sample(i_ctx_p); |
572 | 17.4k | } |
573 | | |
574 | | /* |
575 | | * We have collected all of the sample data. Create a type 0 function stucture. |
576 | | */ |
577 | | static int |
578 | | sampled_data_finish(i_ctx_t *i_ctx_p) |
579 | 34 | { |
580 | 34 | os_ptr op = osp; |
581 | 34 | gs_sampled_data_enum *penum = senum; |
582 | | /* Build a type 0 function using the given parameters */ |
583 | 34 | gs_function_Sd_params_t * params = |
584 | 34 | (gs_function_Sd_params_t *)&penum->pfn->params; |
585 | 34 | gs_function_t * pfn; |
586 | 34 | ref cref; /* closure */ |
587 | 34 | int code = gs_function_Sd_init(&pfn, params, imemory); |
588 | | |
589 | 34 | if (code < 0) { |
590 | 0 | esp -= estack_storage; |
591 | 0 | return code; |
592 | 0 | } |
593 | | |
594 | 34 | code = ialloc_ref_array(&cref, a_executable | a_execute, 2, |
595 | 34 | "sampled_data_finish(cref)"); |
596 | 34 | if (code < 0) { |
597 | 0 | esp -= estack_storage; |
598 | 0 | return code; |
599 | 0 | } |
600 | | |
601 | 34 | make_istruct_new(cref.value.refs, a_executable | a_execute, pfn); |
602 | 34 | make_oper_new(cref.value.refs + 1, 0, zexecfunction); |
603 | 34 | ref_assign(op, &cref); |
604 | | |
605 | 34 | esp -= estack_storage; |
606 | 34 | ifree_object(penum->pfn, "sampled_data_finish(pfn)"); |
607 | 34 | ifree_object(penum, "sampled_data_finish(enum)"); |
608 | 34 | return o_pop_estack; |
609 | 34 | } |
610 | | |
611 | | int make_sampled_function(i_ctx_t * i_ctx_p, ref *arr, ref *pproc, gs_function_t **func) |
612 | 69 | { |
613 | 69 | int code = 0, *ptr, i, total_size, num_components, CIESubst; |
614 | 69 | byte * bytes = 0; |
615 | 69 | float *fptr; |
616 | 69 | gs_function_t *pfn = *func; |
617 | 69 | gs_function_Sd_params_t params = {0}; |
618 | 69 | ref alternatespace, *palternatespace = &alternatespace; |
619 | 69 | PS_colour_space_t *space, *altspace; |
620 | | |
621 | 69 | code = get_space_object(i_ctx_p, arr, &space); |
622 | 69 | if (code < 0) |
623 | 0 | return code; |
624 | 69 | if (!space->alternateproc) |
625 | 0 | return gs_error_typecheck; |
626 | 69 | code = space->alternateproc(i_ctx_p, arr, &palternatespace, &CIESubst); |
627 | 69 | if (code < 0) |
628 | 0 | return code; |
629 | 69 | code = get_space_object(i_ctx_p, palternatespace, &altspace); |
630 | 69 | if (code < 0) |
631 | 0 | return code; |
632 | | /* |
633 | | * Set up the hyper cube function data structure. |
634 | | */ |
635 | 69 | params.Order = 3; |
636 | 69 | params.BitsPerSample = 16; |
637 | | |
638 | 69 | code = space->numcomponents(i_ctx_p, arr, &num_components); |
639 | 69 | if (code < 0) |
640 | 0 | return code; |
641 | 69 | fptr = (float *)gs_alloc_byte_array(imemory, num_components * 2, sizeof(float), "make_sampled_function(Domain)"); |
642 | 69 | if (!fptr) |
643 | 0 | return gs_error_VMerror; |
644 | 69 | code = space->domain(i_ctx_p, arr, fptr); |
645 | 69 | if (code < 0) { |
646 | 0 | gs_free_const_object(imemory, fptr, "make_sampled_function(Domain)"); |
647 | 0 | return code; |
648 | 0 | } |
649 | 69 | params.Domain = fptr; |
650 | 69 | params.m = num_components; |
651 | | |
652 | 69 | code = altspace->numcomponents(i_ctx_p, palternatespace, &num_components); |
653 | 69 | if (code < 0) { |
654 | 0 | gs_free_const_object(imemory, params.Domain, "make_type4_function(Domain)"); |
655 | 0 | return code; |
656 | 0 | } |
657 | 69 | fptr = (float *)gs_alloc_byte_array(imemory, num_components * 2, sizeof(float), "make_sampled_function(Range)"); |
658 | 69 | if (!fptr) { |
659 | 0 | gs_free_const_object(imemory, params.Domain, "make_sampled_function(Domain)"); |
660 | 0 | return gs_error_VMerror; |
661 | 0 | } |
662 | 69 | code = altspace->range(i_ctx_p, palternatespace, fptr); |
663 | 69 | if (code < 0) { |
664 | 0 | gs_free_const_object(imemory, params.Domain, "make_sampled_function(Domain)"); |
665 | 0 | gs_free_const_object(imemory, fptr, "make_sampled_function(Range)"); |
666 | 0 | return code; |
667 | 0 | } |
668 | 69 | params.Range = fptr; |
669 | 69 | params.n = num_components; |
670 | | |
671 | | /* |
672 | | * The Size array may or not be specified. If it is not specified then |
673 | | * we need to determine a set of default values for the Size array. |
674 | | */ |
675 | 69 | ptr = (int *)gs_alloc_byte_array(imemory, params.m, sizeof(int), "Size"); |
676 | 69 | if (ptr == NULL) { |
677 | 0 | code = gs_note_error(gs_error_VMerror); |
678 | 0 | goto fail; |
679 | 0 | } |
680 | 69 | params.Size = ptr; |
681 | | /* |
682 | | * Determine a default |
683 | | * set of values. |
684 | | */ |
685 | 69 | code = determine_sampled_data_size(params.m, params.n, |
686 | 69 | params.BitsPerSample, (int *)params.Size); |
687 | 69 | if (code < 0) |
688 | 0 | goto fail; |
689 | | /* |
690 | | * Determine space required for the sample data storage. |
691 | | */ |
692 | 69 | total_size = params.n * bits2bytes(params.BitsPerSample); |
693 | 138 | for (i = 0; i < params.m; i++) |
694 | 69 | total_size *= params.Size[i]; |
695 | | /* |
696 | | * Allocate space for the data cube itself. |
697 | | */ |
698 | 69 | bytes = gs_alloc_byte_array(imemory, total_size, 1, "cube_build_func0(bytes)"); |
699 | 69 | if (!bytes) { |
700 | 0 | code = gs_note_error(gs_error_VMerror); |
701 | 0 | goto fail; |
702 | 0 | } |
703 | 69 | data_source_init_bytes(¶ms.DataSource, |
704 | 69 | (const unsigned char *)bytes, total_size); |
705 | | |
706 | | /* |
707 | | * This is temporary. We will call gs_function_Sd_init again after |
708 | | * we have collected the cube data. We are doing it now because we need |
709 | | * a function structure created (along with its GC enumeration stuff) |
710 | | * that we can use while collecting the cube data. We will call |
711 | | * the routine again after the cube data is collected to correctly |
712 | | * initialize the function. |
713 | | */ |
714 | 69 | code = gs_function_Sd_init(&pfn, ¶ms, imemory); |
715 | 69 | if (code < 0) |
716 | 0 | return code; |
717 | | /* |
718 | | * Now setup to collect the sample data. |
719 | | */ |
720 | 69 | return sampled_data_setup(i_ctx_p, pfn, pproc, sampled_data_finish, imemory); |
721 | | |
722 | 0 | fail: |
723 | 0 | gs_function_Sd_free_params(¶ms, imemory); |
724 | 0 | return code; |
725 | 69 | } |
726 | | |
727 | | /* ------ Initialization procedure ------ */ |
728 | | |
729 | | const op_def zfsample_op_defs[] = |
730 | | { |
731 | | op_def_begin_level2(), |
732 | | {"1.buildsampledfunction", zbuildsampledfunction}, |
733 | | op_def_end(0) |
734 | | }; |