zfdcte.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Copyright (C) 1994, 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: zfdcte.c,v 1.8 2002/09/26 18:45:02 dan Exp $ */
  14. /* DCTEncode filter creation */
  15. #include "memory_.h"
  16. #include "stdio_.h" /* for jpeglib.h */
  17. #include "jpeglib_.h"
  18. #include "ghost.h"
  19. #include "oper.h"
  20. #include "gsmalloc.h" /* for gs_memory_default */
  21. #include "ialloc.h"
  22. #include "idict.h"
  23. #include "idparam.h"
  24. #include "strimpl.h"
  25. #include "sdct.h"
  26. #include "sjpeg.h"
  27. #include "ifilter.h"
  28. #include "iparam.h"
  29. /*#define TEST*/
  30. /* Import the parameter processing procedure from sdeparam.c */
  31. stream_state_proc_put_params(s_DCTE_put_params, stream_DCT_state);
  32. #ifdef TEST
  33. stream_state_proc_get_params(s_DCTE_get_params, stream_DCT_state);
  34. #endif
  35. /* <target> <dict> DCTEncode/filter <file> */
  36. private int
  37. zDCTE(i_ctx_t *i_ctx_p)
  38. {
  39. os_ptr op = osp;
  40. gs_memory_t *mem = gs_memory_stable(imemory);
  41. stream_DCT_state state;
  42. dict_param_list list;
  43. jpeg_compress_data *jcdp;
  44. int code;
  45. const ref *dop;
  46. uint dspace;
  47. /* First allocate space for IJG parameters. */
  48. jcdp = gs_alloc_struct_immovable(mem, jpeg_compress_data,
  49. &st_jpeg_compress_data, "zDCTE");
  50. if (jcdp == 0)
  51. return_error(e_VMerror);
  52. if (s_DCTE_template.set_defaults)
  53. (*s_DCTE_template.set_defaults) ((stream_state *) & state);
  54. state.data.compress = jcdp;
  55. jcdp->memory = state.jpeg_memory = mem; /* set now for allocation */
  56. state.report_error = filter_report_error; /* in case create fails */
  57. if ((code = gs_jpeg_create_compress(&state)) < 0)
  58. goto fail; /* correct to do jpeg_destroy here */
  59. /* Read parameters from dictionary */
  60. if (r_has_type(op, t_dictionary))
  61. dop = op, dspace = r_space(op);
  62. else
  63. dop = 0, dspace = 0;
  64. if ((code = dict_param_list_read(&list, dop, NULL, false, iimemory)) < 0)
  65. goto fail;
  66. if ((code = s_DCTE_put_params((gs_param_list *) & list, &state)) < 0)
  67. goto rel;
  68. /* Create the filter. */
  69. jcdp->template = s_DCTE_template;
  70. /* Make sure we get at least a full scan line of input. */
  71. state.scan_line_size = jcdp->cinfo.input_components *
  72. jcdp->cinfo.image_width;
  73. jcdp->template.min_in_size =
  74. max(s_DCTE_template.min_in_size, state.scan_line_size);
  75. /* Make sure we can write the user markers in a single go. */
  76. jcdp->template.min_out_size =
  77. max(s_DCTE_template.min_out_size, state.Markers.size);
  78. code = filter_write(i_ctx_p, 0, &jcdp->template,
  79. (stream_state *) & state, dspace);
  80. if (code >= 0) /* Success! */
  81. return code;
  82. /* We assume that if filter_write fails, the stream has not been
  83. * registered for closing, so s_DCTE_release will never be called.
  84. * Therefore we free the allocated memory before failing.
  85. */
  86. rel:
  87. iparam_list_release(&list);
  88. fail:
  89. gs_jpeg_destroy(&state);
  90. gs_free_object(mem, jcdp, "zDCTE fail");
  91. return code;
  92. }
  93. #ifdef TEST
  94. #include "stream.h"
  95. #include "files.h"
  96. /* <dict> <filter> <bool> .dcteparams <dict> */
  97. private int
  98. zdcteparams(i_ctx_t *i_ctx_p)
  99. {
  100. os_ptr op = osp;
  101. stream *s;
  102. dict_param_list list;
  103. int code;
  104. check_type(*op, t_boolean);
  105. check_write_file(s, op - 1);
  106. check_type(op[-2], t_dictionary);
  107. /* The DCT filters copy the template.... */
  108. if (s->state->template->process != s_DCTE_template.process)
  109. return_error(e_rangecheck);
  110. code = dict_param_list_write(&list, op - 2, NULL, iimemory);
  111. if (code < 0)
  112. return code;
  113. code = s_DCTE_get_params((gs_param_list *) & list,
  114. (stream_DCT_state *) s->state,
  115. op->value.boolval);
  116. iparam_list_release(&list);
  117. if (code >= 0)
  118. pop(2);
  119. return code;
  120. }
  121. #endif
  122. /* ------ Initialization procedure ------ */
  123. const op_def zfdcte_op_defs[] =
  124. {
  125. #ifdef TEST
  126. {"3.dcteparams", zdcteparams},
  127. #endif
  128. op_def_begin_filter(),
  129. {"2DCTEncode", zDCTE},
  130. op_def_end(0)
  131. };