/* Copyright (C) 1997, 1998 Aladdin Enterprises.  All rights reserved.

   This file is part of Aladdin Ghostscript.

   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
   or distributor accepts any responsibility for the consequences of using it,
   or for whether it serves any particular purpose or works at all, unless he
   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
   License (the "License") for full details.

   Every copy of Aladdin Ghostscript must include a copy of the License,
   normally in a plain ASCII text file named PUBLIC.  The License grants you
   the right to copy, modify and redistribute Aladdin Ghostscript, but only
   under certain conditions described in the License.  Among other things, the
   License requires that the copyright notice and this notice be preserved on
   all copies.
 */

/*$Id: zdps.c $ */
/* Display PostScript extensions */
#include "ghost.h"
#include "oper.h"
#include "gsstate.h"
#include "gsdps.h"
#include "gsimage.h"
#include "gsiparm2.h"
#include "gxfixed.h"		/* for gxpath.h */
#include "gxpath.h"
#include "btoken.h"		/* for user_names_p */
#include "idict.h"
#include "idparam.h"
#include "igstate.h"
#include "iname.h"
#include "store.h"

/* Import the procedure for constructing user paths. */
extern int make_upath(P4(ref *, const gs_state *, gx_path *, bool));

/* ------ Graphics state ------ */

/* <screen_index> <x> <y> .setscreenphase - */
private int
zsetscreenphase(register os_ptr op)
{
    int code;
    long x, y;

    check_type(op[-2], t_integer);
    check_type(op[-1], t_integer);
    check_type(*op, t_integer);
    x = op[-1].value.intval;
    y = op->value.intval;
    if (x != (int)x || y != (int)y ||
	op[-2].value.intval < -1 ||
	op[-2].value.intval >= gs_color_select_count
	)
	return_error(e_rangecheck);
    code = gs_setscreenphase(igs, (int)x, (int)y,
			     (gs_color_select_t) op[-2].value.intval);
    if (code >= 0)
	pop(3);
    return code;
}

/* <screen_index> .currentscreenphase <x> <y> */
private int
zcurrentscreenphase(register os_ptr op)
{
    gs_int_point phase;
    int code;

    check_type(*op, t_integer);
    if (op->value.intval < -1 ||
	op->value.intval >= gs_color_select_count
	)
	return_error(e_rangecheck);
    code = gs_currentscreenphase(igs, &phase,
				 (gs_color_select_t)op->value.intval);
    if (code < 0)
	return code;
    push(1);
    make_int(op - 1, phase.x);
    make_int(op, phase.y);
    return 0;
}

/* ------ Device-source images ------ */

/* Process an image that has no explicit source data. */
/* We export this for composite images. */
int
process_non_source_image(const gs_image_common_t * pic, client_name_t cname)
{
    gx_image_enum_common_t *pie;
    int code = gs_image_begin_typed(pic, igs, false /****** WRONG ******/ ,
				    &pie);

    /* We didn't pass any data, so there's nothing to clean up. */
    return code;
}

/* <dict> .image2 - */
private int
zimage2(register os_ptr op)
{
    int code;

    check_type(*op, t_dictionary);
    check_dict_read(*op);
    {
	gs_image2_t image;
	ref *pDataSource;

	gs_image2_t_init(&image);
	if ((code = dict_matrix_param(op, "ImageMatrix",
				      &image.ImageMatrix)) < 0 ||
	    (code = dict_find_string(op, "DataSource", &pDataSource)) < 0 ||
	    (code = dict_float_param(op, "XOrigin", 0.0,
				     &image.XOrigin)) != 0 ||
	    (code = dict_float_param(op, "YOrigin", 0.0,
				     &image.YOrigin)) != 0 ||
	    (code = dict_float_param(op, "Width", 0.0,
				     &image.Width)) != 0 ||
	    image.Width <= 0 ||
	    (code = dict_float_param(op, "Height", 0.0,
				     &image.Height)) != 0 ||
	    image.Height <= 0 ||
	    (code = dict_bool_param(op, "PixelCopy", false,
				    &image.PixelCopy)) < 0
	    )
	    return (code < 0 ? code : gs_note_error(e_rangecheck));
	check_stype(*pDataSource, st_igstate_obj);
	image.DataSource = igstate_ptr(pDataSource);
	{
	    ref *ignoref;

	    if (dict_find_string(op, "UnpaintedPath", &ignoref) > 0) {
		check_dict_write(*op);
		image.UnpaintedPath = gx_path_alloc(imemory,
						    ".image2 UnpaintedPath");
		if (image.UnpaintedPath == 0)
		    return_error(e_VMerror);
	    } else
		image.UnpaintedPath = 0;
	}
	code = process_non_source_image((const gs_image_common_t *)&image,
					".image2");
	if (image.UnpaintedPath) {
	    ref rupath;

	    if (code < 0)
		return code;
	    if (gx_path_is_null(image.UnpaintedPath))
		make_null(&rupath);
	    else
		code = make_upath(&rupath, igs, image.UnpaintedPath, false);
	    gx_path_free(image.UnpaintedPath, ".image2 UnpaintedPath");
	    if (code < 0)
		return code;
	    code = dict_put_string(op, "UnpaintedPath", &rupath);
	}
    }
    if (code >= 0)
	pop(1);
    return code;
}

/* ------ View clipping ------ */

/* - viewclip - */
private int
zviewclip(register os_ptr op)
{
    return gs_viewclip(igs);
}

/* - eoviewclip - */
private int
zeoviewclip(register os_ptr op)
{
    return gs_eoviewclip(igs);
}

/* - initviewclip - */
private int
zinitviewclip(register os_ptr op)
{
    return gs_initviewclip(igs);
}

/* - viewclippath - */
private int
zviewclippath(register os_ptr op)
{
    return gs_viewclippath(igs);
}

/* ------ User names ------ */

/* <index> <name> defineusername - */
private int
zdefineusername(register os_ptr op)
{
    ref uname;

    check_int_ltu(op[-1], max_array_size);
    check_type(*op, t_name);
    if (array_get(user_names_p, op[-1].value.intval, &uname) >= 0) {
	switch (r_type(&uname)) {
	    case t_null:
		break;
	    case t_name:
		if (name_eq(&uname, op))
		    goto ret;
		/* falls through */
	    default:
		return_error(e_invalidaccess);
	}
    } else {			/* Expand the array. */
	ref new_array;
	uint old_size = r_size(user_names_p);
	uint new_size = (uint) op[-1].value.intval + 1;

	if (new_size < 100)
	    new_size = 100;
	else if (new_size > max_array_size / 2)
	    new_size = max_array_size;
	else if (new_size >> 1 < old_size)
	    new_size = (old_size > max_array_size / 2 ? max_array_size :
			old_size << 1);
	else
	    new_size <<= 1;
	/* The user name array is always allocated in system VM, */
	/* because it must be immune to save/restore. */
	{
	    uint save_space = icurrent_space;
	    int code;

	    ialloc_set_space(idmemory, avm_system);
	    code = ialloc_ref_array(&new_array, a_all, new_size,
				    "defineusername(new)");
	    if (code < 0) {
		ialloc_set_space(idmemory, save_space);
		return code;
	    }
	    refcpy_to_new(new_array.value.refs, user_names_p->value.refs,
			  old_size);
	    refset_null(new_array.value.refs + old_size,
			new_size - old_size);
	    ifree_ref_array(user_names_p, "defineusername(old)");
	    ialloc_set_space(idmemory, save_space);
	}
	ref_assign(user_names_p, &new_array);
    }
    ref_assign(user_names_p->value.refs + op[-1].value.intval, op);
  ret:pop(2);
    return 0;
}

/* ------ Initialization procedure ------ */

const op_def zdps_op_defs[] =
{
		/* Graphics state */
    {"1.currentscreenphase", zcurrentscreenphase},
    {"3.setscreenphase", zsetscreenphase},
		/* Device-source images */
    {"1.image2", zimage2},
		/* View clipping */
    {"0eoviewclip", zeoviewclip},
    {"0initviewclip", zinitviewclip},
    {"0viewclip", zviewclip},
    {"0viewclippath", zviewclippath},
		/* User names */
    {"2defineusername", zdefineusername},
    op_def_end(0)
};
