home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / ZIMAGE2.C < prev    next >
C/C++ Source or Header  |  1992-06-22  |  5KB  |  172 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zimage2.c */
  21. /* image operator extensions for Level 2 PostScript */
  22. #include "memory_.h"
  23. #include "ghost.h"
  24. #include "errors.h"
  25. #include "oper.h"
  26. #include "gscolor.h"
  27. #include "gscspace.h"
  28. #include "gscolor2.h"
  29. #include "gsmatrix.h"
  30. #include "dict.h"
  31. #include "dparam.h"
  32. #include "state.h"        /* for igs */
  33.  
  34. /* Imported from zpaint.c */
  35. extern int zimage_setup(P9(int width, int height, gs_matrix *pmat,
  36.   ref *sources, int bits_per_component, int num_components, int masked,
  37.   const float *decode, int npop));
  38.  
  39. /* Names of keys in image dictionaries: */
  40. static ref name_ImageType;
  41. static ref name_Width;
  42. static ref name_Height;
  43. static ref name_ImageMatrix;
  44. static ref name_MultipleDataSources;
  45. static ref name_DataSource;
  46. static ref name_BitsPerComponent;
  47. static ref name_Decode;
  48. static ref name_Interpolate;
  49.  
  50. /* Initialization */
  51. private void
  52. zimage2_init()
  53. {    static const names_def imn[] = {
  54.  
  55.     /* Create the names of the known entries in */
  56.     /* an image dictionary. */
  57.        { "ImageType", &name_ImageType },
  58.        { "Width", &name_Width },
  59.        { "Height", &name_Height },
  60.        { "ImageMatrix", &name_ImageMatrix },
  61.        { "MultipleDataSources", &name_MultipleDataSources },
  62.        { "DataSource", &name_DataSource },
  63.        { "BitsPerComponent", &name_BitsPerComponent },
  64.        { "Decode", &name_Decode },
  65.        { "Interpolate", &name_Interpolate },
  66.  
  67.     /* Mark the end of the initalized name list. */
  68.         names_def_end
  69.     };
  70.  
  71.     init_names(imn);
  72. }
  73.  
  74.  
  75. /* Define a structure for acquiring image parameters. */
  76. typedef struct image_params_s {
  77.     int Width;
  78.     int Height;
  79.     gs_matrix ImageMatrix;
  80.     int /*boolean*/ MultipleDataSources;
  81.     ref DataSource[4];
  82.     int BitsPerComponent;
  83.     int num_components;
  84.     float Decode[2*4];
  85.     int /*boolean*/ Interpolate;
  86. } image_params;
  87.  
  88. /* Common code for unpacking an image dictionary. */
  89. private int
  90. image_dict_unpack(os_ptr op, image_params *pip, int max_bits_per_component)
  91. {    int code;
  92.     int num_components;
  93.     gs_color_space cs;
  94.     ref *pds;
  95.     check_type(*op, t_dictionary);
  96.     check_dict_read(*op);
  97.     num_components = gs_currentcolorspace(igs, &cs);
  98.     if ( num_components < 1 ) return e_rangecheck;
  99.     if ( (code = dict_int_param(op, &name_ImageType, 1, 1, 1,
  100.                     &code)) < 0 ||
  101.          (code = dict_int_param(op, &name_Width, 0, 0x7fff, -1,
  102.                     &pip->Width)) < 0 ||
  103.          (code = dict_int_param(op, &name_Height, 0, 0x7fff, -1,
  104.                     &pip->Height)) < 0 ||
  105.          (code = dict_matrix_param(op, &name_ImageMatrix,
  106.                     &pip->ImageMatrix)) < 0 ||
  107.          (code = dict_bool_param(op, &name_MultipleDataSources, 0,
  108.                     &pip->MultipleDataSources)) < 0 ||
  109.          (code = dict_int_param(op, &name_BitsPerComponent, 0,
  110.                     max_bits_per_component, -1,
  111.                     &pip->BitsPerComponent)) < 0 ||
  112.          (code = dict_float_array_param(op, &name_Decode,
  113.                     num_components * 2,
  114.                     &pip->Decode[0], NULL)) < 0 ||
  115.          (code = dict_bool_param(op, &name_Interpolate, 0,
  116.                     &pip->Interpolate)) < 0
  117.        )
  118.         return code;
  119.     /* Extract and check the data sources. */
  120.     if ( (code = dict_find(op, &name_DataSource, &pds)) < 0 )
  121.         return code;
  122.     if ( pip->MultipleDataSources )
  123.     {    check_type(*pds, t_array);
  124.         if ( r_size(pds) != num_components ) return e_rangecheck;
  125.         memcpy(&pip->DataSource[0], pds->value.refs, sizeof(ref) * num_components);
  126.     }
  127.     else
  128.         pip->DataSource[0] = *pds;
  129.     pip->num_components = num_components;
  130.     /* We don't support arbitrary decoding yet: */
  131.     /* all the decoding pairs must be the same. */
  132.     {    int i;
  133.         float *dp;
  134.         for ( i = 1, dp = &pip->Decode[2]; i < num_components;
  135.               i++, dp += 2
  136.             )
  137.             if ( dp[-2] != dp[0] || dp[-1] != dp[1] )
  138.                 return e_rangecheck;
  139.     }
  140.     return 0;
  141. }
  142.  
  143. /* .dictimage */
  144. int
  145. zdictimage(register os_ptr op)
  146. {    image_params ip;
  147.     int code = image_dict_unpack(op, &ip, 12);
  148.     if ( code < 0 ) return code;
  149.     return zimage_setup(ip.Width, ip.Height, &ip.ImageMatrix,
  150.         &ip.DataSource[0], ip.BitsPerComponent,
  151.         ip.num_components, 0, &ip.Decode[0], 1);
  152. }
  153.  
  154. /* .dictimagemask */
  155. int
  156. zdictimagemask(register os_ptr op)
  157. {    image_params ip;
  158.     int code = image_dict_unpack(op, &ip, 1);
  159.     if ( code < 0 ) return code;
  160.     if ( ip.MultipleDataSources ) return e_rangecheck;
  161.     return zimage_setup(ip.Width, ip.Height, &ip.ImageMatrix,
  162.         &ip.DataSource[0], 1, 1, 1, &ip.Decode[0], 1);
  163. }
  164.  
  165. /* ------ Initialization procedure ------ */
  166.  
  167. op_def zimage2_op_defs[] = {
  168.     {"1.dictimage", zdictimage},
  169.     {"1.dictimagemask", zdictimagemask},
  170.     op_def_end(zimage2_init)
  171. };
  172.