home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / UsingPDF / GhostScript / source / gs5.10 / zfunc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-01  |  6.6 KB  |  217 lines

  1. /* Copyright (C) 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* zfunc.c */
  20. /* Generic PostScript language interface to Functions */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "gsfunc.h"
  26. #include "gsstruct.h"
  27. #include "ialloc.h"
  28. #include "idict.h"
  29. #include "idparam.h"
  30. #include "ifunc.h"
  31. #include "store.h"
  32.  
  33. /* Define the maximum depth of nesting of subsidiary functions. */
  34. #define max_depth 3
  35.  
  36. /* Define the table of build procedures. */
  37. build_function_proc((*build_function_procs[5])) = {
  38.   build_function_undefined, build_function_undefined, build_function_undefined,
  39.   build_function_undefined, build_function_undefined
  40. };
  41.  
  42. int
  43. build_function_undefined(const_os_ptr op, const gs_function_params_t *mnDR,
  44.   int depth, gs_function_t **ppfn)
  45. {    return_error(e_rangecheck);
  46. }
  47.  
  48. /* GC descriptors */
  49. gs_private_st_ptr(st_function_ptr, gs_function_t *, "gs_function_t *",
  50.   function_ptr_enum_ptrs, function_ptr_reloc_ptrs);
  51. gs_private_st_element(st_function_ptr_element, gs_function_t *,
  52.   "gs_function_t *[]", function_ptr_element_enum_ptrs,
  53.   function_ptr_element_reloc_ptrs, st_function_ptr);
  54.  
  55. /* ------ Operators ------ */
  56.  
  57. private int zexecfunction(P1(os_ptr op));
  58.  
  59. /* <dict> .buildfunction <function_struct> */
  60. private int
  61. zbuildfunction(os_ptr op)
  62. {    gs_function_t *pfn;
  63.     ref cref;        /* closure */
  64.     int code;
  65.  
  66.     code = ialloc_ref_array(&cref, a_executable | a_execute, 2,
  67.                 ".buildfunction");
  68.     if ( code < 0 )
  69.       return code;
  70.     code = fn_build_function(op, &pfn);
  71.     if ( code < 0 ) {
  72.       ifree_ref_array(&cref, ".buildfunction");
  73.       return code;
  74.     }
  75.     make_istruct_new(cref.value.refs, a_executable | a_execute, pfn);
  76.     make_oper_new(cref.value.refs + 1, 0, zexecfunction);
  77.     ref_assign(op, &cref);
  78.     return 0;
  79. }
  80.  
  81. /* <in1> ... <function_struct> %execfunction <out1> ... */
  82. private int
  83. zexecfunction(os_ptr op)
  84. {    /*
  85.      * Since this operator's name begins with %, the name is not defined
  86.      * in systemdict.  The only place this operator can ever appear is
  87.      * in the execute-only closure created by .buildfunction.
  88.      * Therefore, in principle it is unnecessary to check the argument.
  89.      * However, we do a little checking anyway just on general
  90.      * principles.  Note that since the argument may be an instance of
  91.      * any subclass of gs_function_t, we currently have no way to check
  92.      * its type.
  93.      */
  94.     if ( !r_is_struct(op) ||
  95.          r_has_masked_attrs(op, a_executable | a_execute, a_all)
  96.        )
  97.       return_error(e_typecheck);
  98.     { gs_function_t *pfn = (gs_function_t *)op->value.pstruct;
  99.       int m = pfn->params.m, n = pfn->params.n;
  100.       int diff = n - (m + 1);
  101.  
  102.       if ( diff > 0 )
  103.         check_ostack(diff);
  104.       { float *in = (float *)ialloc_byte_array(m, sizeof(float),
  105.                            "%execfunction(in)");
  106.         float *out = (float *)ialloc_byte_array(n, sizeof(float),
  107.                             "%execfunction(out)");
  108.         int code;
  109.  
  110.         if ( in == 0 || out == 0 )
  111.           code = gs_note_error(e_VMerror);
  112.         else if ( (code = float_params(op - 1, m, in)) < 0 ||
  113.               (code = gs_function_evaluate(pfn, in, out)) < 0
  114.             )
  115.           DO_NOTHING;
  116.         else {
  117.           if ( diff > 0 )
  118.         push(diff);    /* can't fail */
  119.           else if ( diff < 0 ) {
  120.         pop(-diff);  op = osp;
  121.           }
  122.           code = make_floats(op + 1 - n, out, n);
  123.         }
  124.         ifree_object(out, "%execfunction(out)");
  125.         ifree_object(in, "%execfunction(in)");
  126.         return code;
  127.       }
  128.     }
  129. }
  130.  
  131. /* ------ Procedures ------ */
  132.  
  133. /* Build a function structure from a PostScript dictionary. */
  134. int
  135. fn_build_sub_function(const ref *op, gs_function_t **ppfn, int depth)
  136. {    int code, type;
  137.     gs_function_params_t params;
  138.  
  139.     if ( depth > max_depth )
  140.       return_error(e_limitcheck);
  141.     check_type(*op, t_dictionary);
  142.     code = dict_int_param(op, "FunctionType", 0,
  143.                   countof(build_function_procs) - 1, -1, &type);
  144.     if ( code < 0 )
  145.       return code;
  146.     /* Collect parameters common to all function types. */
  147.     params.Domain = 0;
  148.     params.Range = 0;
  149.     code = fn_build_float_array(op, "Domain", true, true, ¶ms.Domain);
  150.     if ( code < 0 )
  151.       goto fail;
  152.     params.m = code >> 1;
  153.     code = fn_build_float_array(op, "Range", false, true, ¶ms.Range);
  154.     if ( code < 0 )
  155.       goto fail;
  156.     params.n = code >> 1;
  157.     /* Finish building the function. */
  158.     /* If this fails, it will free all the parameters. */
  159.     return (*build_function_procs[type])(op, ¶ms, depth + 1, ppfn);
  160. fail:    ifree_object((void *)params.Range, "Range"); /* break const */
  161.     ifree_object((void *)params.Domain, "Domain"); /* break const */
  162.     return code;
  163. }
  164.  
  165. /* Allocate an array of function objects. */
  166. int
  167. ialloc_function_array(uint count, gs_function_t ***pFunctions)
  168. {    gs_function_t **ptr;
  169.  
  170.     if ( count == 0 )
  171.       return_error(e_rangecheck);
  172.     ptr = ialloc_struct_array(count, gs_function_t *,
  173.                   &st_function_ptr_element, "Functions");
  174.     if ( ptr == 0 )
  175.       return_error(e_VMerror);
  176.     memset(ptr, 0, sizeof(*ptr) * count);
  177.     *pFunctions = ptr;
  178.     return 0;
  179. }
  180.  
  181. /* Collect a heap-allocated array of floats. */
  182. /* If the key is missing, set *pparray = 0 and return 0; */
  183. /* otherwise set *pparray and return the number of elements. */
  184. int
  185. fn_build_float_array(const ref *op, const char _ds *kstr, bool required,
  186.   bool even, const float **pparray)
  187. {    ref *par;
  188.     int code;
  189.  
  190.     *pparray = 0;
  191.     if ( dict_find_string(op, kstr, &par) <= 0 )
  192.       return(required ? gs_note_error(e_rangecheck) : 0);
  193.     if ( !r_is_array(par) )
  194.       return_error(e_typecheck);
  195.     { uint size = r_size(par);
  196.       float *ptr = (float *)ialloc_byte_array(size, sizeof(float), kstr);
  197.  
  198.       if ( ptr == 0 )
  199.         return_error(e_VMerror);
  200.       code = dict_float_array_param(op, kstr, size, ptr, NULL);
  201.       if ( code <= 0 || (even && (code & 1) != 0) ) {
  202.         ifree_object(ptr, kstr);
  203.         return_error(code < 0 ? code : e_rangecheck);
  204.       }
  205.       *pparray = ptr;
  206.     }
  207. #undef max_values
  208.     return code;
  209. }
  210.  
  211. /* ------ Initialization procedure ------ */
  212.  
  213. BEGIN_OP_DEFS(zfunc_op_defs) {
  214.     {"1.buildfunction", zbuildfunction},
  215.     {"1%execfunction", zexecfunction},
  216. END_OP_DEFS(0) }
  217.