zfunc3.c 4.7 KB
Newer Older
1
/* Copyright (C) 2001-2019 Artifex Software, Inc.
2
   All Rights Reserved.
3

4 5 6
   This software is provided AS-IS with no warranty, either express or
   implied.

7 8 9 10 11
   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
12 13
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
14 15
*/

16

17 18 19 20 21 22 23 24 25 26 27 28 29
/* PostScript language interface to LL3 Functions */
#include "memory_.h"
#include "ghost.h"
#include "oper.h"
#include "gsfunc3.h"
#include "gsstruct.h"
#include "stream.h"		/* for files.h */
#include "files.h"
#include "ialloc.h"
#include "idict.h"
#include "idparam.h"
#include "ifunc.h"
#include "store.h"
30
#include "igstate.h"
31 32 33 34 35 36 37 38

/* Check prototypes */
build_function_proc(gs_build_function_2);
build_function_proc(gs_build_function_3);

/* Finish building a FunctionType 2 (ExponentialInterpolation) function. */
int
gs_build_function_2(i_ctx_t *i_ctx_p, const ref *op, const gs_function_params_t * mnDR,
39
                    int depth, gs_function_t ** ppfn, gs_memory_t *mem)
40 41 42 43 44 45 46 47
{
    gs_function_ElIn_params_t params;
    int code, n0, n1;

    *(gs_function_params_t *)&params = *mnDR;
    params.C0 = 0;
    params.C1 = 0;
    if ((code = dict_float_param(op, "N", 0.0, &params.N)) != 0 ||
48 49 50 51
        (code = n0 = fn_build_float_array_forced(op, "C0", false, &params.C0, mem)) < 0 ||
        (code = n1 = fn_build_float_array_forced(op, "C1", false, &params.C1, mem)) < 0
        )
        goto fail;
52
    if (params.C0 == 0)
53
        n0 = 1;			/* C0 defaulted */
54
    if (params.C1 == 0)
55
        n1 = 1;			/* C1 defaulted */
56
    if (params.Range == 0)
57
        params.n = n0;		/* either one will do */
58
    if (n0 != n1 || n0 != params.n)
59
        goto fail;
60 61
    code = gs_function_ElIn_init(ppfn, &params, mem);
    if (code >= 0)
62
        return 0;
63 64
fail:
    gs_function_ElIn_free_params(&params, mem);
65
    return (code < 0 ? code : gs_note_error(gs_error_rangecheck));
66 67 68 69 70
}

/* Finish building a FunctionType 3 (1-Input Stitching) function. */
int
gs_build_function_3(i_ctx_t *i_ctx_p, const ref *op, const gs_function_params_t * mnDR,
71
                    int depth, gs_function_t ** ppfn, gs_memory_t *mem)
72 73 74 75 76 77 78 79 80
{
    gs_function_1ItSg_params_t params;
    int code;

    *(gs_function_params_t *) & params = *mnDR;
    params.Functions = 0;
    params.Bounds = 0;
    params.Encode = 0;
    {
81 82 83
        ref *pFunctions;
        gs_function_t **ptr;
        int i;
84

85
        if ((code = dict_find_string(op, "Functions", &pFunctions)) <= 0)
86
            return (code < 0 ? code : gs_note_error(gs_error_rangecheck));
87 88 89 90 91 92 93 94
        check_array_only(*pFunctions);
        params.k = r_size(pFunctions);
        code = alloc_function_array(params.k, &ptr, mem);
        if (code < 0)
            return code;
        params.Functions = (const gs_function_t * const *)ptr;
        for (i = 0; i < params.k; ++i) {
            ref subfn;
95

96 97 98 99 100
            array_get(mem, pFunctions, (long)i, &subfn);
            code = fn_build_sub_function(i_ctx_p, &subfn, &ptr[i], depth, mem, 0, 0);
            if (code < 0)
                goto fail;
        }
101 102
    }
    if ((code = fn_build_float_array(op, "Bounds", true, false, &params.Bounds, mem)) != params.k - 1)
103
        goto fail;
104
    if (gs_currentcpsimode(imemory)) {
105 106 107 108 109 110
        /* Adobe implementation doesn't check the Encode length. */
        /* Extra elements are ignored; missing elements are filled with 0. */
        /* CET 12-14m.ps depends on this bug */
        uint sz, k2 = 2 * params.k;
        ref *encode;
        float *p = (float *)gs_alloc_byte_array(mem, k2, sizeof(float), "Encode");
111

112 113
        params.Encode = p;
        if (p == 0) {
114
            code = gs_note_error(gs_error_VMerror);
115 116 117
            goto fail;
        }
        if (dict_find_string(op, "Encode", &encode) <= 0) {
118
            code = gs_note_error(gs_error_undefined);
119 120 121
            goto fail;
        }
        if (!r_is_array(encode)) {
122
            code = gs_note_error(gs_error_typecheck);
123 124 125 126 127 128 129 130
            goto fail;
        }
        sz =  min(k2, r_size(encode));
        code = process_float_array(mem, encode, sz, p);
        if (code < 0)
            goto fail;
        while (sz < k2)
            p[sz++] = 0.0;
131
    } else if ((code = fn_build_float_array(op, "Encode", true, true, &params.Encode, mem)) != 2 * params.k)
132
        goto fail;
133
    if (params.Range == 0)
134
        params.n = params.Functions[0]->params.n;
135 136
    code = gs_function_1ItSg_init(ppfn, &params, mem);
    if (code >= 0)
137
        return 0;
138 139
fail:
    gs_function_1ItSg_free_params(&params, mem);
140
    return (code < 0 ? code : gs_note_error(gs_error_rangecheck));
141
}