xref: /plan9/sys/src/cmd/gs/src/zfunc0.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1997, 2000 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: zfunc0.c,v 1.6 2002/02/21 22:24:54 giles Exp $ */
18 /* PostScript language interface to FunctionType 0 (Sampled) Functions */
19 #include "memory_.h"
20 #include "ghost.h"
21 #include "oper.h"
22 #include "gsdsrc.h"
23 #include "gsfunc.h"
24 #include "gsfunc0.h"
25 #include "stream.h"		/* for files.h */
26 #include "files.h"
27 #include "ialloc.h"
28 #include "idict.h"
29 #include "idparam.h"
30 #include "ifunc.h"
31 
32 /* Check prototype */
33 build_function_proc(gs_build_function_0);
34 
35 /* Finish building a FunctionType 0 (Sampled) function. */
36 int
gs_build_function_0(i_ctx_t * i_ctx_p,const ref * op,const gs_function_params_t * mnDR,int depth,gs_function_t ** ppfn,gs_memory_t * mem)37 gs_build_function_0(i_ctx_t *i_ctx_p, const ref *op, const gs_function_params_t * mnDR,
38 		    int depth, gs_function_t ** ppfn, gs_memory_t *mem)
39 {
40     gs_function_Sd_params_t params;
41     ref *pDataSource;
42     int code;
43 
44     *(gs_function_params_t *) & params = *mnDR;
45     params.Encode = 0;
46     params.Decode = 0;
47     params.Size = 0;
48     if ((code = dict_find_string(op, "DataSource", &pDataSource)) <= 0)
49 	return (code < 0 ? code : gs_note_error(e_rangecheck));
50     switch (r_type(pDataSource)) {
51 	case t_string:
52 	    data_source_init_string2(&params.DataSource,
53 				     pDataSource->value.const_bytes,
54 				     r_size(pDataSource));
55 	    break;
56 	case t_file: {
57 	    stream *s;
58 
59 	    check_read_known_file_else(s, pDataSource, return_error,
60 				       return_error(e_invalidfileaccess));
61 	    if (!(s->modes & s_mode_seek))
62 		return_error(e_ioerror);
63 	    data_source_init_stream(&params.DataSource, s);
64 	    break;
65 	}
66 	default:
67 	    return_error(e_rangecheck);
68     }
69     if ((code = dict_int_param(op, "Order", 1, 3, 1, &params.Order)) < 0 ||
70 	(code = dict_int_param(op, "BitsPerSample", 1, 32, 0,
71 			       &params.BitsPerSample)) < 0 ||
72 	((code = fn_build_float_array(op, "Encode", false, true, &params.Encode, mem)) != 2 * params.m && (code != 0 || params.Encode != 0)) ||
73 	((code = fn_build_float_array(op, "Decode", false, true, &params.Decode, mem)) != 2 * params.n && (code != 0 || params.Decode != 0))
74 	) {
75 	goto fail;
76     } {
77 	int *ptr = (int *)
78 	    gs_alloc_byte_array(mem, params.m, sizeof(int), "Size");
79 
80 	if (ptr == 0) {
81 	    code = gs_note_error(e_VMerror);
82 	    goto fail;
83 	}
84 	params.Size = ptr;
85 	code = dict_ints_param(op, "Size", params.m, ptr);
86 	if (code != params.m)
87 	    goto fail;
88     }
89     code = gs_function_Sd_init(ppfn, &params, mem);
90     if (code >= 0)
91 	return 0;
92 fail:
93     gs_function_Sd_free_params(&params, mem);
94     return (code < 0 ? code : gs_note_error(e_rangecheck));
95 }
96