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

  1. /* Copyright (C) 1989, 1992, 1993 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. /* zcolor.c */
  20. /* Color operators */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "estack.h"
  25. #include "ialloc.h"
  26. #include "igstate.h"
  27. #include "iutil.h"
  28. #include "store.h"
  29. #include "gxfixed.h"
  30. #include "gxmatrix.h"
  31. #include "gzstate.h"
  32. #include "gxdevice.h"
  33. #include "gxcmap.h"
  34.  
  35. /* Import the 'for' operator */
  36. extern int
  37.   zfor(P1(os_ptr));
  38.  
  39. /* The gray transfer function for the library layer. */
  40. /* This just returns what's already in the map. */
  41. private float
  42. gray_transfer(const gs_state *pgs, floatp value)
  43. {    return gx_map_color_float(pgs, value, transfer.gray);
  44. }
  45.  
  46. /* Export color remapping stuff for zcolor1.c. */
  47. /* Define the number of stack slots needed for zcolor_remap_one. */
  48. const int zcolor_remap_one_ostack = 4;
  49. const int zcolor_remap_one_estack = 3;
  50. int zcolor_remap_one(P5(const ref *, os_ptr, gx_transfer_map *, const gs_state *, int (*)(P1(os_ptr))));
  51. int
  52.   zcolor_remap_one_finish(P1(os_ptr)),
  53.   zcolor_remap_one_signed_finish(P1(os_ptr)),
  54.   zcolor_remap_color(P1(os_ptr));
  55.  
  56. /* - currentalpha <alpha> */
  57. int
  58. zcurrentalpha(register os_ptr op)
  59. {    push(1);
  60.     make_real(op, gs_currentalpha(igs));
  61.     return 0;
  62. }
  63.  
  64. /* - currentgray <gray> */
  65. int
  66. zcurrentgray(register os_ptr op)
  67. {    push(1);
  68.     make_real(op, gs_currentgray(igs));
  69.     return 0;
  70. }
  71.  
  72. /* - currenthsbcolor <hue> <saturation> <brightness> */
  73. int
  74. zcurrenthsbcolor(register os_ptr op)
  75. {    float par[3];
  76.     gs_currenthsbcolor(igs, par);
  77.     push(3);
  78.     make_reals(op - 2, par, 3);
  79.     return 0;
  80. }
  81.  
  82. /* - currentrgbcolor <red> <green> <blue> */
  83. int
  84. zcurrentrgbcolor(register os_ptr op)
  85. {    float par[3];
  86.     gs_currentrgbcolor(igs, par);
  87.     push(3);
  88.     make_reals(op - 2, par, 3);
  89.     return 0;
  90. }
  91.  
  92. /* - currenttransfer <proc> */
  93. int
  94. zcurrenttransfer(register os_ptr op)
  95. {    push(1);
  96.     *op = istate->transfer_procs.colored.gray;
  97.     return 0;
  98. }
  99.  
  100. /* - processcolors <int> - */
  101. /* Note: this is an undocumented operator that is not supported */
  102. /* in Level 2. */
  103. int
  104. zprocesscolors(register os_ptr op)
  105. {    push(1);
  106.     make_int(op, gs_currentdevice(igs)->color_info.num_components);
  107.     return 0;
  108. }
  109.  
  110. /* <alpha> setalpha - */
  111. int
  112. zsetalpha(register os_ptr op)
  113. {    float alpha;
  114.     int code;
  115.     if ( (code = real_param(op, &alpha)) < 0 ||
  116.          (code = gs_setalpha(igs, alpha)) < 0
  117.        )
  118.         return code;
  119.     pop(1);
  120.     return 0;
  121. }
  122.  
  123. /* <gray> setgray - */
  124. int
  125. zsetgray(register os_ptr op)
  126. {    float gray;
  127.     int code;
  128.     if ( (code = real_param(op, &gray)) < 0 ||
  129.          (code = gs_setgray(igs, gray)) < 0
  130.        )
  131.         return code;
  132.     make_null(&istate->colorspace.array);
  133.     pop(1);
  134.     return 0;
  135. }
  136.  
  137. /* <hue> <saturation> <brightness> sethsbcolor - */
  138. int
  139. zsethsbcolor(register os_ptr op)
  140. {    float par[3];
  141.     int code;
  142.     if (    (code = num_params(op, 3, par)) < 0 ||
  143.         (code = gs_sethsbcolor(igs, par[0], par[1], par[2])) < 0
  144.        )
  145.         return code;
  146.     make_null(&istate->colorspace.array);
  147.     pop(3);
  148.     return 0;
  149. }
  150.  
  151. /* <red> <green> <blue> setrgbcolor - */
  152. int
  153. zsetrgbcolor(register os_ptr op)
  154. {    float par[3];
  155.     int code;
  156.     if (    (code = num_params(op, 3, par)) < 0 ||
  157.         (code = gs_setrgbcolor(igs, par[0], par[1], par[2])) < 0
  158.        )
  159.         return code;
  160.     make_null(&istate->colorspace.array);
  161.     pop(3);
  162.     return 0;
  163. }
  164.  
  165. /* <proc> settransfer - */
  166. int
  167. zsettransfer(register os_ptr op)
  168. {    int code;
  169.     check_proc(*op);
  170.     check_ostack(zcolor_remap_one_ostack - 1);
  171.     check_estack(1 + zcolor_remap_one_estack);
  172.     istate->transfer_procs.colored.red =
  173.       istate->transfer_procs.colored.green =
  174.       istate->transfer_procs.colored.blue =
  175.       istate->transfer_procs.colored.gray = *op;
  176.     code = gs_settransfer_remap(igs, gray_transfer, 0);
  177.     if ( code < 0 ) return code;
  178.     push_op_estack(zcolor_remap_color);
  179.     pop(1);  op--;
  180.     return zcolor_remap_one(&istate->transfer_procs.colored.gray, op,
  181.                 igs->transfer.gray, igs,
  182.                 zcolor_remap_one_finish);
  183. }
  184.  
  185. /* ------ Internal routines ------ */
  186.  
  187. /* Prepare to remap one color component */
  188. /* (also used for black generation and undercolor removal). */
  189. /* Use the 'for' operator to gather the values. */
  190. /* The caller must have done the necessary check_ostack and check_estack. */
  191. int
  192. zcolor_remap_one(const ref *pproc, register os_ptr op, gx_transfer_map *pmap,
  193.   const gs_state *pgs, int (*finish_proc)(P1(os_ptr)))
  194. {    osp = op += 4;
  195.     make_real(op - 3, 1.0);
  196.     make_real(op - 2, -0.999999 / (transfer_map_size - 1));
  197.     make_real(op - 1, 0.0);
  198.     *op = *pproc;
  199.     ++esp;
  200.     make_struct(esp, imemory_local_attr((gs_ref_memory_t *)pgs->memory),
  201.             pmap);
  202.     push_op_estack(finish_proc);
  203.     push_op_estack(zfor);
  204.     return o_push_estack;
  205. }
  206.  
  207. /* Store the result of remapping a component. */
  208. private int
  209. zcolor_remap_one_store(os_ptr op, floatp min_value)
  210. {    int i;
  211.     gx_transfer_map *pmap = r_ptr(esp, gx_transfer_map);
  212.     for ( i = 0; i < transfer_map_size; i++ )
  213.     {    float v;
  214.         int code = real_param(ref_stack_index(&o_stack, i), &v);
  215.         if ( code < 0 ) return code;
  216.         pmap->values[i] =
  217.             (v < min_value ? float2frac(min_value) :
  218.              v >= 1.0 ? frac_1 :
  219.              float2frac(v));
  220.     }
  221.     ref_stack_pop(&o_stack, transfer_map_size);
  222.     esp--;                /* pop pointer to transfer map */
  223.     return o_pop_estack;
  224. }
  225. int
  226. zcolor_remap_one_finish(os_ptr op)
  227. {    return zcolor_remap_one_store(op, 0.0);
  228. }
  229. int
  230. zcolor_remap_one_signed_finish(os_ptr op)
  231. {    return zcolor_remap_one_store(op, -1.0);
  232. }
  233.  
  234. /* Finally, invalidate the current color. */
  235. int
  236. zcolor_remap_color(os_ptr op)
  237. {    gx_unset_dev_color(igs);
  238.     return 0;
  239. }
  240.  
  241. /* ------ Initialization procedure ------ */
  242.  
  243. op_def zcolor_op_defs[] = {
  244.     {"0currentalpha", zcurrentalpha},
  245.     {"0currentgray", zcurrentgray},
  246.     {"0currenthsbcolor", zcurrenthsbcolor},
  247.     {"0currentrgbcolor", zcurrentrgbcolor},
  248.     {"0currenttransfer", zcurrenttransfer},
  249.     {"0processcolors", zprocesscolors},
  250.     {"1setalpha", zsetalpha},
  251.     {"1setgray", zsetgray},
  252.     {"3sethsbcolor", zsethsbcolor},
  253.     {"3setrgbcolor", zsetrgbcolor},
  254.     {"1settransfer", zsettransfer},
  255.         /* Internal operators */
  256.     {"1%zcolor_remap_one_finish", zcolor_remap_one_finish},
  257.     {"1%zcolor_remap_one_signed_finish", zcolor_remap_one_signed_finish},
  258.     {"0%zcolor_remap_color", zcolor_remap_color},
  259.     op_def_end(0)
  260. };
  261.