home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / ZIMAGE2.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  5KB  |  158 lines

  1. /* Copyright (C) 1992, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* zimage2.c */
  20. /* image operator extensions for Level 2 PostScript */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "gscolor.h"
  26. #include "gscspace.h"
  27. #include "gscolor2.h"
  28. #include "gsmatrix.h"
  29. #include "idict.h"
  30. #include "idparam.h"
  31. #include "ilevel.h"
  32. #include "igstate.h"        /* for igs */
  33.  
  34. /* Imported from zpaint.c */
  35. extern int zimage_setup(P10(int width, int height, gs_matrix *pmat,
  36.   ref *sources, int bits_per_component,
  37.   bool multi, const gs_color_space *pcs, int masked,
  38.   const float *decode, int npop));
  39. extern int zimage(P1(os_ptr));
  40. extern int zimagemask(P1(os_ptr));
  41.  
  42. /* Define a structure for acquiring image parameters. */
  43. typedef struct image_params_s {
  44.     int Width;
  45.     int Height;
  46.     gs_matrix ImageMatrix;
  47.     bool MultipleDataSources;
  48.     ref DataSource[4];
  49.     int BitsPerComponent;
  50.     float Decode[2*4];
  51.     const float *pDecode;
  52.     bool Interpolate;
  53. } image_params;
  54.  
  55. /* Common code for unpacking an image dictionary. */
  56. /* Assume *op is a dictionary. */
  57. private int
  58. image_dict_unpack(os_ptr op, image_params *pip, int max_bits_per_component)
  59. {    int code;
  60.     int num_components;
  61.     int decode_size;
  62.     ref *pds;
  63.     check_dict_read(*op);
  64.     num_components = gs_currentcolorspace(igs)->type->num_components;
  65.     if ( num_components < 1 )
  66.         return_error(e_rangecheck);
  67.     if ( max_bits_per_component == 1 )    /* imagemask */
  68.         num_components = 1;        /* for Decode */
  69.     if ( (code = dict_int_param(op, "ImageType", 1, 1, 1,
  70.                     &code)) < 0 ||
  71.          (code = dict_int_param(op, "Width", 0, 0x7fff, -1,
  72.                     &pip->Width)) < 0 ||
  73.          (code = dict_int_param(op, "Height", 0, 0x7fff, -1,
  74.                     &pip->Height)) < 0 ||
  75.          (code = dict_matrix_param(op, "ImageMatrix",
  76.                     &pip->ImageMatrix)) < 0 ||
  77.          (code = dict_bool_param(op, "MultipleDataSources", false,
  78.                     &pip->MultipleDataSources)) < 0 ||
  79.          (code = dict_int_param(op, "BitsPerComponent", 0,
  80.                     max_bits_per_component, -1,
  81.                     &pip->BitsPerComponent)) < 0 ||
  82.          (code = decode_size = dict_float_array_param(op, "Decode",
  83.                     num_components * 2,
  84.                     &pip->Decode[0], NULL)) < 0 ||
  85.          (code = dict_bool_param(op, "Interpolate", false,
  86.                     &pip->Interpolate)) < 0
  87.        )
  88.         return code;
  89.     if ( decode_size == 0 )
  90.         pip->pDecode = 0;
  91.     else if ( decode_size != num_components * 2 )
  92.         return_error(e_rangecheck);
  93.     else
  94.         pip->pDecode = &pip->Decode[0];
  95.     /* Extract and check the data sources. */
  96.     if ( (code = dict_find_string(op, "DataSource", &pds)) < 0 )
  97.         return code;
  98.     if ( pip->MultipleDataSources )
  99.     {    check_type(*pds, t_array);
  100.         if ( r_size(pds) != num_components )
  101.             return_error(e_rangecheck);
  102.         memcpy(&pip->DataSource[0], pds->value.refs, sizeof(ref) * num_components);
  103.     }
  104.     else
  105.         pip->DataSource[0] = *pds;
  106.     return 0;
  107. }
  108.  
  109. /* (<width> <height> <bits/sample> <matrix> <datasrc> image -) */
  110. /* <dict> image - */
  111. int
  112. z2image(register os_ptr op)
  113. {    if ( level2_enabled && r_has_type(op, t_dictionary) )
  114.     {    image_params ip;
  115.         int code = image_dict_unpack(op, &ip, 12);
  116.         if ( code < 0 )
  117.             return code;
  118.         return zimage_setup(ip.Width, ip.Height, &ip.ImageMatrix,
  119.             &ip.DataSource[0], ip.BitsPerComponent,
  120.             ip.MultipleDataSources, gs_currentcolorspace(igs),
  121.             0, ip.pDecode, 1);
  122.     }
  123.     /* Level 1 image operator */
  124.     check_op(5);
  125.     return zimage(op);
  126. }
  127.  
  128. /* (<width> <height> <paint_1s> <matrix> <datasrc> imagemask -) */
  129. /* <dict> imagemask - */
  130. int
  131. z2imagemask(register os_ptr op)
  132. {    if ( level2_enabled && r_has_type(op, t_dictionary) )
  133.     {    image_params ip;
  134.         gs_color_space cs;
  135.         int code = image_dict_unpack(op, &ip, 1);
  136.         if ( code < 0 )
  137.             return code;
  138.         if ( ip.MultipleDataSources )
  139.             return_error(e_rangecheck);
  140.         cs.type = &gs_color_space_type_DeviceGray;
  141.         return zimage_setup(ip.Width, ip.Height, &ip.ImageMatrix,
  142.             &ip.DataSource[0], 1, false, &cs, 1, ip.pDecode, 1);
  143.     }
  144.     /* Level 1 imagemask operator */
  145.     check_op(5);
  146.     return zimagemask(op);
  147. }
  148.  
  149. /* ------ Initialization procedure ------ */
  150.  
  151. /* Note that these override the definitions in zpaint.c. */
  152. op_def zimage2_l2_op_defs[] = {
  153.         op_def_begin_level2(),
  154.     {"1image", z2image},
  155.     {"1imagemask", z2imagemask},
  156.     op_def_end(0)
  157. };
  158.