1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
/* Copyright (C) 1992, 1994 Aladdin Enterprises. All rights reserved.
This file is part of GNU Ghostscript.
GNU Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to
anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the GNU Ghostscript General Public License for full details.
*/
/* zimage2.c */
/* image operator extensions for Level 2 PostScript */
#include "math_.h"
#include "memory_.h"
#include "ghost.h"
#include "errors.h"
#include "oper.h"
#include "gscolor.h"
#include "gscspace.h"
#include "gscolor2.h"
#include "gsmatrix.h"
#include "idict.h"
#include "idparam.h"
#include "ilevel.h"
#include "igstate.h" /* for igs */
/* Imported from zpaint.c */
extern int zimage_setup(P11(int width, int height, gs_matrix *pmat,
ref *sources, int bits_per_component,
bool multi, const gs_color_space *pcs, int masked,
const float *decode, bool interpolate, int npop));
/* Define a structure for acquiring image parameters. */
typedef struct image_params_s {
int Width;
int Height;
gs_matrix ImageMatrix;
bool MultipleDataSources;
ref DataSource[4];
int BitsPerComponent;
float Decode[2*4];
const float *pDecode;
bool Interpolate;
} image_params;
/* Common code for unpacking an image dictionary. */
/* Assume *op is a dictionary. */
private int
image_dict_unpack(os_ptr op, image_params *pip, int max_bits_per_component)
{ int code;
int num_components;
int decode_size;
ref *pds;
check_dict_read(*op);
num_components = gs_currentcolorspace(igs)->type->num_components;
if ( num_components < 1 )
return_error(e_rangecheck);
if ( max_bits_per_component == 1 ) /* imagemask */
num_components = 1; /* for Decode */
if ( (code = dict_int_param(op, "ImageType", 1, 1, 1,
&code)) < 0 ||
(code = dict_int_param(op, "Width", 0, 0x7fff, -1,
&pip->Width)) < 0 ||
(code = dict_int_param(op, "Height", 0, 0x7fff, -1,
&pip->Height)) < 0 ||
(code = dict_matrix_param(op, "ImageMatrix",
&pip->ImageMatrix)) < 0 ||
(code = dict_bool_param(op, "MultipleDataSources", false,
&pip->MultipleDataSources)) < 0 ||
(code = dict_int_param(op, "BitsPerComponent", 0,
max_bits_per_component, -1,
&pip->BitsPerComponent)) < 0 ||
(code = decode_size = dict_float_array_param(op, "Decode",
num_components * 2,
&pip->Decode[0], NULL)) < 0 ||
(code = dict_bool_param(op, "Interpolate", false,
&pip->Interpolate)) < 0
)
return code;
if ( decode_size == 0 )
pip->pDecode = 0;
else if ( decode_size != num_components * 2 )
return_error(e_rangecheck);
else
pip->pDecode = &pip->Decode[0];
/* Extract and check the data sources. */
if ( (code = dict_find_string(op, "DataSource", &pds)) < 0 )
return code;
if ( pip->MultipleDataSources )
{ check_type_only(*pds, t_array);
if ( r_size(pds) != num_components )
return_error(e_rangecheck);
memcpy(&pip->DataSource[0], pds->value.refs, sizeof(ref) * num_components);
}
else
pip->DataSource[0] = *pds;
return 0;
}
/* (<width> <height> <bits/sample> <matrix> <datasrc> image -) */
/* <dict> image - */
private int
z2image(register os_ptr op)
{ if ( level2_enabled )
{ check_op(1);
if ( r_has_type(op, t_dictionary) )
{ image_params ip;
int code = image_dict_unpack(op, &ip, 12);
if ( code < 0 )
return code;
return zimage_setup(ip.Width, ip.Height,
&ip.ImageMatrix,
&ip.DataSource[0],
ip.BitsPerComponent,
ip.MultipleDataSources,
gs_currentcolorspace(igs),
0, ip.pDecode, ip.Interpolate, 1);
}
}
/* Level 1 image operator */
check_op(5);
return zimage(op);
}
/* (<width> <height> <paint_1s> <matrix> <datasrc> imagemask -) */
/* <dict> imagemask - */
private int
z2imagemask(register os_ptr op)
{ if ( level2_enabled )
{ check_op(1);
if ( r_has_type(op, t_dictionary) )
{ image_params ip;
gs_color_space cs;
int code = image_dict_unpack(op, &ip, 1);
if ( code < 0 )
return code;
if ( ip.MultipleDataSources )
return_error(e_rangecheck);
cs.type = &gs_color_space_type_DeviceGray;
return zimage_setup(ip.Width, ip.Height,
&ip.ImageMatrix,
&ip.DataSource[0], 1, false, &cs,
1, ip.pDecode, ip.Interpolate, 1);
}
}
/* Level 1 imagemask operator */
check_op(5);
return zimagemask(op);
}
/* ------ Experimental code ------ */
#include "gsiscale.h"
#include "ialloc.h"
/* <indata> <w> <h> <mat> <outdata> .scaleimage - */
/* Rescale an image represented as an array of bytes (representing values */
/* in the range [0..1]) using filtering. */
private int
zscaleimage(os_ptr op)
{ const_os_ptr rin = op - 4;
const_os_ptr rout = op;
gs_matrix mat;
int code;
gs_image_scale_state iss;
check_write_type(*rout, t_string);
if ( (code = read_matrix(op - 1, &mat)) < 0 )
return code;
if ( mat.xx < 1 || mat.xy != 0 || mat.yx != 0 || mat.yy < 1 )
return_error(e_rangecheck);
check_type(op[-2], t_integer);
check_type(op[-3], t_integer);
check_read_type(*rin, t_string);
if ( op[-3].value.intval <= 0 || op[-2].value.intval <= 0 ||
r_size(rin) != op[-3].value.intval * op[-2].value.intval
)
return_error(e_rangecheck);
iss.sizeofPixelIn = 1;
iss.maxPixelIn = 0xff;
iss.sizeofPixelOut = 1;
iss.maxPixelOut = 0xff;
iss.src_width = op[-3].value.intval;
iss.src_height = op[-2].value.intval;
iss.dst_width = (int)ceil(iss.src_width * mat.xx);
iss.dst_height = (int)ceil(iss.src_height * mat.yy);
if ( r_size(rout) != iss.dst_width * iss.dst_height )
return_error(e_rangecheck);
iss.values_per_pixel = 1;
iss.memory = imemory;
code = gs_image_scale_init(&iss, false, false);
if ( code < 0 )
return code;
code = gs_image_scale(rout->value.bytes, rin->value.bytes, &iss);
gs_image_scale_cleanup(&iss);
pop(5);
return 0;
}
/* ------ Initialization procedure ------ */
/* Note that these override the definitions in zpaint.c. */
BEGIN_OP_DEFS(zimage2_l2_op_defs) {
op_def_begin_level2(),
{"1image", z2image},
{"1imagemask", z2imagemask},
{"5.scaleimage", zscaleimage},
END_OP_DEFS(0) }
|