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 / ZCIE.C < prev    next >
C/C++ Source or Header  |  1992-07-28  |  8KB  |  252 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. /* zcie.c */
  21. /* CIE color operators for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "gscspace.h"
  26. #include "gscie.h"
  27. #include "dict.h"
  28. #include "dparam.h"
  29. #include "state.h"
  30.  
  31. /* There are actually only two CIE-specific operators, */
  32. /* but CIE color dictionaries are so complex that */
  33. /* we handle the CIE case of setcolorspace here as well. */
  34. /* CIE color takes up so much space that we intend to */
  35. /* make it a separate option. */
  36.  
  37. /* Elements in a CIEBasedABC color space dictionary: */
  38. static ref name_RangeABC;
  39. static ref name_DecodeABC;
  40. static ref name_MatrixABC;
  41. static ref name_RangeLMN;
  42. static ref name_DecodeLMN;
  43. static ref name_MatrixLMN;
  44. static ref name_WhitePoint;
  45. static ref name_BlackPoint;
  46. /* Additional or alternate elements in a CIEBasedA c.s. dictionary: */
  47. static ref name_RangeA;
  48. static ref name_DecodeA;
  49. static ref name_MatrixA;
  50. /* (Additional) elements in a CIE color rendering dictionary: */
  51. static ref name_ColorRenderingType;
  52. static ref name_EncodeLMN;
  53. static ref name_EncodeABC;
  54. static ref name_MatrixPQR;
  55. static ref name_RangePQR;
  56. static ref name_TransformPQR;
  57. static ref name_RenderTable;
  58.  
  59. /* Initialization */
  60. private void
  61. zcie_init()
  62. {    static const names_def cien[] = {
  63.  
  64.     /* Elements in a CIEBasedABC color space dictionary: */
  65.        { "RangeABC", &name_RangeABC },
  66.        { "DecodeABC", &name_DecodeABC },
  67.        { "MatrixABC", &name_MatrixABC },
  68.        { "RangeLMN", &name_RangeLMN },
  69.        { "DecodeLMN", &name_DecodeLMN },
  70.        { "MatrixLMN", &name_MatrixLMN },
  71.        { "WhitePoint", &name_WhitePoint },
  72.        { "BlackPoint", &name_BlackPoint },
  73.  
  74.     /* Additional or alternate elements in a CIEBasedA c.s. dictionary: */
  75.        { "RangeA", &name_RangeA },
  76.        { "DecodeA", &name_DecodeA },
  77.        { "MatrixA", &name_MatrixA },
  78.  
  79.     /* (Additional) elements in a CIE color rendering dictionary: */
  80.        { "ColorRenderingType", &name_ColorRenderingType },
  81.        { "EncodeLMN", &name_EncodeLMN },
  82.        { "EncodeABC", &name_EncodeABC },
  83.        { "MatrixPQR", &name_MatrixPQR },
  84.        { "RangePQR", &name_RangePQR },
  85.        { "TransformPQR", &name_TransformPQR },
  86.        { "RenderTable", &name_RenderTable },
  87.  
  88.     /* Mark the end of the initalized name list. */
  89.        names_def_end
  90.     };
  91.  
  92.     init_names(cien);
  93. }
  94.  
  95. /* ------ Operators ------ */
  96.  
  97. /* Get a 3-element range parameter from a dictionary. */
  98. #define dict_range3_param(op, pname, prange)\
  99.     dict_float_array_param(op, pname, 6, (float *)prange, (float *)&Range3_default)
  100.  
  101. /* Get a 3x3 matrix parameter from a dictionary. */
  102. #define dict_matrix3_param(op, pname, pmat)\
  103.     dict_float_array_param(op, pname, 9, (float *)pmat, (float *)&Matrix3_default)
  104.         
  105. /* Shared code for getting WhitePoint and BlackPoint values. */
  106. private int
  107. cie_points_param(ref *pdref, gs_cie_wb *pwb)
  108. {    int code;
  109.     if ( (code = dict_float_array_param(pdref, &name_WhitePoint, 3, (float *)&pwb->WhitePoint, NULL)) < 0 ||
  110.          (code = dict_float_array_param(pdref, &name_BlackPoint, 3, (float *)&pwb->BlackPoint, (float *)&BlackPoint_default)) < 0
  111.        )
  112.         return code;
  113.     if ( pwb->WhitePoint.u <= 0 ||
  114.          pwb->WhitePoint.v != 1 ||
  115.          pwb->WhitePoint.w <= 0 ||
  116.          pwb->BlackPoint.u < 0 ||
  117.          pwb->BlackPoint.v < 0 ||
  118.          pwb->BlackPoint.w < 0
  119.        )
  120.         return e_rangecheck;
  121.     return 0;
  122. }
  123.  
  124. /* Common code for the CIEBasedA[BC] cases of setcolorspace. */
  125. private int
  126. cie_lmnp_param(ref *pdref, gs_cie_common *pcie)
  127. {    int code;
  128.     if ( (code = dict_range3_param(pdref, &name_RangeLMN, &pcie->RangeLMN)) < 0 ||
  129.          /****** DecodeLMN ******/
  130.          (code = dict_matrix3_param(pdref, &name_MatrixLMN, &pcie->MatrixLMN)) < 0 ||
  131.          (code = cie_points_param(pdref, &pcie->points)) < 0
  132.        )
  133.         return code;
  134.     return 0;
  135. }
  136.  
  137. /* Get the parameters of a CIEBasedABC color space. */
  138. /* This doesn't actually implement setcolorspace, */
  139. /* since it has to be usable for the base color space */
  140. /* of Separation, Indexed, and Pattern spaces as well. */
  141. int
  142. zcolorspace_CIEBasedABC(ref *pdref, gs_cie_abc *pcie)
  143. {    int code;
  144.     check_type(*pdref, t_dictionary);
  145.     check_dict_read(*pdref);
  146.     if ( (code = dict_range3_param(pdref, &name_RangeABC, &pcie->RangeABC)) < 0 ||
  147.          /****** DecodeABC ******/
  148.          (code = dict_matrix3_param(pdref, &name_MatrixABC, &pcie->MatrixABC)) < 0 ||
  149.          (code = cie_lmnp_param(pdref, &pcie->common)) < 0
  150.        )
  151.         return code;
  152.     /* NOT IMPLEMENTED YET */
  153.     return e_undefined;
  154. }
  155.  
  156. /* Get the parameters of a CIEBasedA color space. */
  157. /* See above. */
  158. int
  159. zcolorspace_CIEBasedA(ref *pdref, gs_cie_a *pcie)
  160. {    int code;
  161.     check_type(*pdref, t_dictionary);
  162.     check_dict_read(*pdref);
  163.     if ( (code = dict_float_array_param(pdref, &name_RangeA, 2, (float *)&pcie->RangeA, (float *)&RangeA_default)) < 0 ||
  164.          /****** DecodeA ******/
  165.          (code = dict_float_array_param(pdref, &name_MatrixA, 3, (float *)&pcie->MatrixA, (float *)&MatrixA_default)) < 0 ||
  166.          (code = cie_lmnp_param(pdref, &pcie->common)) < 0
  167.        )
  168.         return code;
  169.     /* NOT IMPLEMENTED YET */
  170.     return e_undefined;
  171. }
  172.  
  173. /* currentcolorrendering */
  174. private int
  175. zcurrentcolorrendering(register os_ptr op)
  176. {    push(1);
  177.     *op = istate.colorrendering;
  178.     return 0;
  179. }
  180.  
  181. /* setcolorrendering */
  182. private int
  183. zsetcolorrendering(register os_ptr op)
  184. {    int code;
  185.     int discard;
  186.     gs_cie_render render;
  187.     ref *pRT;
  188.     check_read_type(*op, t_dictionary);
  189.     check_dict_read(*op);
  190.     if ( (code = dict_int_param(op, &name_ColorRenderingType, 1, 1, 0, &discard)) < 0 ||
  191.          (code = dict_matrix3_param(op, &name_MatrixLMN, &render.MatrixLMN)) < 0 ||
  192.          /****** EncodeLMN ******/
  193.          (code = dict_range3_param(op, &name_RangeLMN, &render.RangeLMN)) < 0 ||
  194.          (code = dict_matrix3_param(op, &name_MatrixABC, &render.MatrixABC)) < 0 ||
  195.          /****** EncodeABC ******/
  196.          (code = dict_range3_param(op, &name_RangeABC, &render.RangeABC)) < 0 ||
  197.          (code = cie_points_param(op, &render.points)) < 0 ||
  198.          (code = dict_matrix3_param(op, &name_MatrixPQR, &render.MatrixPQR)) < 0 ||
  199.          (code = dict_range3_param(op, &name_RangePQR, &render.RangePQR)) < 0 ||
  200.          0 /****** TransformPQR ******/
  201.        )
  202.         return code;
  203. #define rRT render.RenderTable
  204.     if ( dict_find(op, &name_RenderTable, &pRT) > 0 )
  205.     {    const ref *prte;
  206.         int i;
  207.         uint n2;
  208.         check_read_type(*pRT, t_array);
  209.         prte = pRT->value.const_refs;
  210.         check_type(prte[0], t_integer);
  211.         check_type(prte[1], t_integer);
  212.         check_type(prte[2], t_integer);
  213.         check_read_type(prte[3], t_array);
  214.         check_type(prte[4], t_integer);
  215.         if ( prte[0].value.intval < 1 ||
  216.              prte[1].value.intval < 1 ||
  217.              prte[2].value.intval < 1 ||
  218.              !(prte[4].value.intval == 3 || prte[4].value.intval == 4)
  219.            )
  220.             return e_rangecheck;
  221.         rRT.NA = prte[0].value.intval;
  222.         rRT.NB = prte[1].value.intval;
  223.         rRT.NC = prte[2].value.intval;
  224.         rRT.m = prte[4].value.intval;
  225.         n2 = rRT.NB * rRT.NC;
  226.         if ( r_size(pRT) != rRT.m + 5 || r_size(&prte[3]) != rRT.NA )
  227.             return e_rangecheck;
  228.         for ( i = 0; i < rRT.m; i++ )
  229.         {    const ref *prt2 = prte[3].value.const_refs + i;
  230.             check_read_type(*prt2, t_string);
  231.             if ( r_size(prt2) != n2 )
  232.                 return e_rangecheck;
  233.         }
  234.         /****** Make table ******/
  235.     }
  236.     else
  237.         rRT.table = 0;
  238. #undef rRT
  239.     /*istate.colorrendering = *op;*/
  240.     /*pop(1);*/
  241.     /* NOT IMPLEMENTED YET */
  242.     return e_undefined;
  243. }
  244.  
  245. /* ------ Initialization procedure ------ */
  246.  
  247. op_def zcie_op_defs[] = {
  248.     {"0currentcolorrendering", zcurrentcolorrendering},
  249.     {"1setcolorrendering", zsetcolorrendering},
  250.     op_def_end(zcie_init)
  251. };
  252.