zfdcte.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Copyright (C) 1994, 2000 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: zfdcte.c,v 1.5 2001/05/30 17:38:45 rayjj Exp $ */
  16. /* DCTEncode filter creation */
  17. #include "memory_.h"
  18. #include "stdio_.h" /* for jpeglib.h */
  19. #include "jpeglib_.h"
  20. #include "ghost.h"
  21. #include "oper.h"
  22. #include "gsmalloc.h" /* for gs_memory_default */
  23. #include "ialloc.h"
  24. #include "idict.h"
  25. #include "idparam.h"
  26. #include "strimpl.h"
  27. #include "sdct.h"
  28. #include "sjpeg.h"
  29. #include "ifilter.h"
  30. #include "iparam.h"
  31. /*#define TEST*/
  32. /* Import the parameter processing procedure from sdeparam.c */
  33. stream_state_proc_put_params(s_DCTE_put_params, stream_DCT_state);
  34. #ifdef TEST
  35. stream_state_proc_get_params(s_DCTE_get_params, stream_DCT_state);
  36. #endif
  37. /* <target> <dict> DCTEncode/filter <file> */
  38. private int
  39. zDCTE(i_ctx_t *i_ctx_p)
  40. {
  41. os_ptr op = osp;
  42. gs_memory_t *mem = &gs_memory_default;
  43. stream_DCT_state state;
  44. dict_param_list list;
  45. jpeg_compress_data *jcdp;
  46. int code;
  47. const ref *dop;
  48. uint dspace;
  49. /* First allocate space for IJG parameters. */
  50. jcdp = gs_alloc_struct_immovable(mem, jpeg_compress_data,
  51. &st_jpeg_compress_data, "zDCTE");
  52. if (jcdp == 0)
  53. return_error(e_VMerror);
  54. if (s_DCTE_template.set_defaults)
  55. (*s_DCTE_template.set_defaults) ((stream_state *) & state);
  56. state.data.compress = jcdp;
  57. jcdp->memory = state.jpeg_memory = mem; /* set now for allocation */
  58. state.report_error = filter_report_error; /* in case create fails */
  59. if ((code = gs_jpeg_create_compress(&state)) < 0)
  60. goto fail; /* correct to do jpeg_destroy here */
  61. /* Read parameters from dictionary */
  62. if (r_has_type(op, t_dictionary))
  63. dop = op, dspace = r_space(op);
  64. else
  65. dop = 0, dspace = 0;
  66. if ((code = dict_param_list_read(&list, dop, NULL, false, iimemory)) < 0)
  67. goto fail;
  68. if ((code = s_DCTE_put_params((gs_param_list *) & list, &state)) < 0)
  69. goto rel;
  70. /* Create the filter. */
  71. jcdp->template = s_DCTE_template;
  72. /* Make sure we get at least a full scan line of input. */
  73. state.scan_line_size = jcdp->cinfo.input_components *
  74. jcdp->cinfo.image_width;
  75. jcdp->template.min_in_size =
  76. max(s_DCTE_template.min_in_size, state.scan_line_size);
  77. /* Make sure we can write the user markers in a single go. */
  78. jcdp->template.min_out_size =
  79. max(s_DCTE_template.min_out_size, state.Markers.size);
  80. code = filter_write(i_ctx_p, 0, &jcdp->template,
  81. (stream_state *) & state, dspace);
  82. if (code >= 0) /* Success! */
  83. return code;
  84. /* We assume that if filter_write fails, the stream has not been
  85. * registered for closing, so s_DCTE_release will never be called.
  86. * Therefore we free the allocated memory before failing.
  87. */
  88. rel:
  89. iparam_list_release(&list);
  90. fail:
  91. gs_jpeg_destroy(&state);
  92. gs_free_object(mem, jcdp, "zDCTE fail");
  93. return code;
  94. }
  95. #ifdef TEST
  96. #include "stream.h"
  97. #include "files.h"
  98. /* <dict> <filter> <bool> .dcteparams <dict> */
  99. private int
  100. zdcteparams(i_ctx_t *i_ctx_p)
  101. {
  102. os_ptr op = osp;
  103. stream *s;
  104. dict_param_list list;
  105. int code;
  106. check_type(*op, t_boolean);
  107. check_write_file(s, op - 1);
  108. check_type(op[-2], t_dictionary);
  109. /* The DCT filters copy the template.... */
  110. if (s->state->template->process != s_DCTE_template.process)
  111. return_error(e_rangecheck);
  112. code = dict_param_list_write(&list, op - 2, NULL, iimemory);
  113. if (code < 0)
  114. return code;
  115. code = s_DCTE_get_params((gs_param_list *) & list,
  116. (stream_DCT_state *) s->state,
  117. op->value.boolval);
  118. iparam_list_release(&list);
  119. if (code >= 0)
  120. pop(2);
  121. return code;
  122. }
  123. #endif
  124. /* ------ Initialization procedure ------ */
  125. const op_def zfdcte_op_defs[] =
  126. {
  127. #ifdef TEST
  128. {"3.dcteparams", zdcteparams},
  129. #endif
  130. op_def_begin_filter(),
  131. {"2DCTEncode", zDCTE},
  132. op_def_end(0)
  133. };