xref: /plan9/sys/src/cmd/gs/src/zfdctd.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1994, 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: zfdctd.c,v 1.8 2002/02/21 22:24:54 giles Exp $ */
18 /* DCTDecode filter creation */
19 #include "memory_.h"
20 #include "stdio_.h"		/* for jpeglib.h */
21 #include "jpeglib_.h"
22 #include "ghost.h"
23 #include "oper.h"
24 #include "gsmalloc.h"		/* for gs_memory_default */
25 #include "strimpl.h"
26 #include "sdct.h"
27 #include "sjpeg.h"
28 #include "ialloc.h"
29 #include "ifilter.h"
30 #include "iparam.h"
31 
32 private_st_jpeg_decompress_data();
33 
34 /* Import the parameter processing procedure from sddparam.c */
35 stream_state_proc_put_params(s_DCTD_put_params, stream_DCT_state);
36 
37 /* <source> <dict> DCTDecode/filter <file> */
38 /* <source> DCTDecode/filter <file> */
39 private int
zDCTD(i_ctx_t * i_ctx_p)40 zDCTD(i_ctx_t *i_ctx_p)
41 {
42     os_ptr op = osp;
43     gs_memory_t *mem = (gs_memory_t *)(i_ctx_p->memory.current);
44     stream_DCT_state state;
45     dict_param_list list;
46     jpeg_decompress_data *jddp;
47     int code;
48     const ref *dop;
49     uint dspace;
50 
51     /* First allocate space for IJG parameters. */
52     jddp = gs_alloc_struct_immovable(mem,jpeg_decompress_data,
53       &st_jpeg_decompress_data, "zDCTD");
54     if (jddp == 0)
55 	return_error(e_VMerror);
56     if (s_DCTD_template.set_defaults)
57 	(*s_DCTD_template.set_defaults) ((stream_state *) & state);
58     state.data.decompress = jddp;
59     jddp->memory = state.jpeg_memory = mem;	/* set now for allocation */
60     jddp->scanline_buffer = NULL;	/* set this early for safe error exit */
61     state.report_error = filter_report_error;	/* in case create fails */
62     if ((code = gs_jpeg_create_decompress(&state)) < 0)
63 	goto fail;		/* correct to do jpeg_destroy here */
64     /* Read parameters from dictionary */
65     if (r_has_type(op, t_dictionary))
66 	dop = op, dspace = r_space(op);
67     else
68 	dop = 0, dspace = 0;
69     if ((code = dict_param_list_read(&list, dop, NULL, false, iimemory)) < 0)
70 	goto fail;
71     if ((code = s_DCTD_put_params((gs_param_list *) & list, &state)) < 0)
72 	goto rel;
73     /* Create the filter. */
74     jddp->template = s_DCTD_template;
75     code = filter_read(i_ctx_p, 0, &jddp->template,
76 		       (stream_state *) & state, dspace);
77     if (code >= 0)		/* Success! */
78 	return code;
79     /*
80      * We assume that if filter_read fails, the stream has not been
81      * registered for closing, so s_DCTD_release will never be called.
82      * Therefore we free the allocated memory before failing.
83      */
84 rel:
85     iparam_list_release(&list);
86 fail:
87     gs_jpeg_destroy(&state);
88     gs_free_object(mem, jddp, "zDCTD fail");
89     return code;
90 }
91 
92 /* ------ Initialization procedure ------ */
93 
94 const op_def zfdctd_op_defs[] =
95 {
96     op_def_begin_filter(),
97     {"2DCTDecode", zDCTD},
98     op_def_end(0)
99 };
100