zfunc0.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright (C) 1997, 2000 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: zfunc0.c,v 1.6 2002/02/21 22:24:54 giles Exp $ */
  14. /* PostScript language interface to FunctionType 0 (Sampled) Functions */
  15. #include "memory_.h"
  16. #include "ghost.h"
  17. #include "oper.h"
  18. #include "gsdsrc.h"
  19. #include "gsfunc.h"
  20. #include "gsfunc0.h"
  21. #include "stream.h" /* for files.h */
  22. #include "files.h"
  23. #include "ialloc.h"
  24. #include "idict.h"
  25. #include "idparam.h"
  26. #include "ifunc.h"
  27. /* Check prototype */
  28. build_function_proc(gs_build_function_0);
  29. /* Finish building a FunctionType 0 (Sampled) function. */
  30. int
  31. gs_build_function_0(i_ctx_t *i_ctx_p, const ref *op, const gs_function_params_t * mnDR,
  32. int depth, gs_function_t ** ppfn, gs_memory_t *mem)
  33. {
  34. gs_function_Sd_params_t params;
  35. ref *pDataSource;
  36. int code;
  37. *(gs_function_params_t *) & params = *mnDR;
  38. params.Encode = 0;
  39. params.Decode = 0;
  40. params.Size = 0;
  41. if ((code = dict_find_string(op, "DataSource", &pDataSource)) <= 0)
  42. return (code < 0 ? code : gs_note_error(e_rangecheck));
  43. switch (r_type(pDataSource)) {
  44. case t_string:
  45. data_source_init_string2(&params.DataSource,
  46. pDataSource->value.const_bytes,
  47. r_size(pDataSource));
  48. break;
  49. case t_file: {
  50. stream *s;
  51. check_read_known_file_else(s, pDataSource, return_error,
  52. return_error(e_invalidfileaccess));
  53. if (!(s->modes & s_mode_seek))
  54. return_error(e_ioerror);
  55. data_source_init_stream(&params.DataSource, s);
  56. break;
  57. }
  58. default:
  59. return_error(e_rangecheck);
  60. }
  61. if ((code = dict_int_param(op, "Order", 1, 3, 1, &params.Order)) < 0 ||
  62. (code = dict_int_param(op, "BitsPerSample", 1, 32, 0,
  63. &params.BitsPerSample)) < 0 ||
  64. ((code = fn_build_float_array(op, "Encode", false, true, &params.Encode, mem)) != 2 * params.m && (code != 0 || params.Encode != 0)) ||
  65. ((code = fn_build_float_array(op, "Decode", false, true, &params.Decode, mem)) != 2 * params.n && (code != 0 || params.Decode != 0))
  66. ) {
  67. goto fail;
  68. } {
  69. int *ptr = (int *)
  70. gs_alloc_byte_array(mem, params.m, sizeof(int), "Size");
  71. if (ptr == 0) {
  72. code = gs_note_error(e_VMerror);
  73. goto fail;
  74. }
  75. params.Size = ptr;
  76. code = dict_ints_param(op, "Size", params.m, ptr);
  77. if (code != params.m)
  78. goto fail;
  79. }
  80. code = gs_function_Sd_init(ppfn, &params, mem);
  81. if (code >= 0)
  82. return 0;
  83. fail:
  84. gs_function_Sd_free_params(&params, mem);
  85. return (code < 0 ? code : gs_note_error(e_rangecheck));
  86. }