xref: /plan9/sys/src/cmd/gs/src/zimage2.c (revision ff8c3af2f44d95267f67219afa20ba82ff6cf7e4)
1 /* Copyright (C) 1992, 2000 Aladdin Enterprises.  All rights reserved.
2 
3   This file is part of AFPL Ghostscript.
4 
5   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
6   distributor accepts any responsibility for the consequences of using it, or
7   for whether it serves any particular purpose or works at all, unless he or
8   she says so in writing.  Refer to the Aladdin Free Public License (the
9   "License") for full details.
10 
11   Every copy of AFPL Ghostscript must include a copy of the License, normally
12   in a plain ASCII text file named PUBLIC.  The License grants you the right
13   to copy, modify and redistribute AFPL Ghostscript, but only under certain
14   conditions described in the License.  Among other things, the License
15   requires that the copyright notice and this notice be preserved on all
16   copies.
17 */
18 
19 /*$Id: zimage2.c,v 1.4 2001/03/12 22:53:22 alexcher Exp $ */
20 /* image operator extensions for Level 2 PostScript */
21 #include "math_.h"
22 #include "memory_.h"
23 #include "ghost.h"
24 #include "oper.h"
25 #include "gscolor.h"
26 #include "gscspace.h"
27 #include "gscolor2.h"
28 #include "gsmatrix.h"
29 #include "gsimage.h"
30 #include "gxfixed.h"
31 #include "idict.h"
32 #include "idparam.h"
33 #include "iimage.h"
34 #include "iimage2.h"
35 #include "ilevel.h"
36 #include "igstate.h"		/* for igs */
37 
38 /* Extract and check the parameters for a gs_data_image_t. */
39 int
40 data_image_params(const ref *op, gs_data_image_t *pim,
41 		  image_params *pip, bool require_DataSource,
42 		  int num_components, int max_bits_per_component)
43 {
44     int code;
45     int decode_size;
46     ref *pds;
47 
48     check_type(*op, t_dictionary);
49     check_dict_read(*op);
50     if ((code = dict_int_param(op, "Width", 0, max_int_in_fixed / 2,
51 			       -1, &pim->Width)) < 0 ||
52 	(code = dict_int_param(op, "Height", 0, max_int_in_fixed / 2,
53 			       -1, &pim->Height)) < 0 ||
54 	(code = dict_matrix_param(op, "ImageMatrix",
55 				  &pim->ImageMatrix)) < 0 ||
56 	(code = dict_bool_param(op, "MultipleDataSources", false,
57 				&pip->MultipleDataSources)) < 0 ||
58 	(code = dict_int_param(op, "BitsPerComponent", 1,
59 			       max_bits_per_component, -1,
60 			       &pim->BitsPerComponent)) < 0 ||
61 	(code = decode_size = dict_floats_param(op, "Decode",
62 						num_components * 2,
63 						&pim->Decode[0], NULL)) < 0 ||
64 	(code = dict_bool_param(op, "Interpolate", false,
65 				&pim->Interpolate)) < 0
66 	)
67 	return code;
68     pip->pDecode = &pim->Decode[0];
69     /* Extract and check the data sources. */
70     if ((code = dict_find_string(op, "DataSource", &pds)) <= 0) {
71 	if (require_DataSource)
72 	    return (code < 0 ? code : gs_note_error(e_rangecheck));
73 	return 1;		/* no data source */
74     }
75     if (pip->MultipleDataSources) {
76 	long i;
77         if (!r_is_array(pds))
78             return_error(e_typecheck);
79 	if (r_size(pds) != num_components)
80 	    return_error(e_rangecheck);
81 	for (i = 0; i < num_components; ++i)
82             array_get(pds, i, &pip->DataSource[i]);
83     } else
84 	pip->DataSource[0] = *pds;
85     return 0;
86 }
87 
88 /* Extract and check the parameters for a gs_pixel_image_t. */
89 int
90 pixel_image_params(i_ctx_t *i_ctx_p, const ref *op, gs_pixel_image_t *pim,
91 		   image_params *pip, int max_bits_per_component)
92 {
93     int num_components =
94 	gs_color_space_num_components(gs_currentcolorspace(igs));
95     int code;
96 
97     if (num_components < 1)
98 	return_error(e_rangecheck);	/* Pattern space not allowed */
99     pim->ColorSpace = gs_currentcolorspace(igs);
100     code = data_image_params(op, (gs_data_image_t *) pim, pip, true,
101 			     num_components, max_bits_per_component);
102     if (code < 0)
103 	return code;
104     pim->format =
105 	(pip->MultipleDataSources ? gs_image_format_component_planar :
106 	 gs_image_format_chunky);
107     return dict_bool_param(op, "CombineWithColor", false,
108 			   &pim->CombineWithColor);
109 }
110 
111 /* <dict> .image1 - */
112 private int
113 zimage1(i_ctx_t *i_ctx_p)
114 {
115     os_ptr op = osp;
116     gs_image_t image;
117     image_params ip;
118     int code;
119 
120     gs_image_t_init(&image, gs_currentcolorspace(igs));
121     code = pixel_image_params(i_ctx_p, op, (gs_pixel_image_t *)&image, &ip,
122 			      12);
123     if (code < 0)
124 	return code;
125     return zimage_setup(i_ctx_p, (gs_pixel_image_t *)&image, &ip.DataSource[0],
126 			image.CombineWithColor, 1);
127 }
128 
129 /* <dict> .imagemask1 - */
130 private int
131 zimagemask1(i_ctx_t *i_ctx_p)
132 {
133     os_ptr op = osp;
134     gs_image_t image;
135     image_params ip;
136     int code;
137 
138     gs_image_t_init_mask_adjust(&image, false,
139 				gs_incachedevice(igs) != CACHE_DEVICE_NONE);
140     code = data_image_params(op, (gs_data_image_t *) & image, &ip, true, 1, 1);
141     if (code < 0)
142 	return code;
143     if (ip.MultipleDataSources)
144 	return_error(e_rangecheck);
145     return zimage_setup(i_ctx_p, (gs_pixel_image_t *)&image, &ip.DataSource[0],
146 			true, 1);
147 }
148 
149 /*
150  * Process an image that has no explicit source data.  This isn't used by
151  * standard Level 2, but it's a very small procedure and is needed by
152  * both zdps.c and zdpnext.c.
153  */
154 int
155 process_non_source_image(i_ctx_t *i_ctx_p, const gs_image_common_t * pic,
156 			 client_name_t cname)
157 {
158     gx_image_enum_common_t *pie;
159     int code = gs_image_begin_typed(pic, igs, false /****** WRONG ******/ ,
160 				    &pie);
161 
162     /* We didn't pass any data, so there's nothing to clean up. */
163     return code;
164 }
165 
166 /* ------ Initialization procedure ------ */
167 
168 const op_def zimage2_l2_op_defs[] =
169 {
170     op_def_begin_level2(),
171     {"1.image1", zimage1},
172     {"1.imagemask1", zimagemask1},
173     op_def_end(0)
174 };
175