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 / GSCOLOR.C < prev    next >
C/C++ Source or Header  |  1992-07-17  |  15KB  |  478 lines

  1. /* Copyright (C) 1989, 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. /* gscolor.c */
  21. /* Color and halftone operators for Ghostscript library */
  22. #include "math_.h"
  23. #include "memory_.h"
  24. #include "gx.h"
  25. #include "gserrors.h"
  26. #include "gscspace.h"
  27. #include "gxfixed.h"            /* ditto */
  28. #include "gxmatrix.h"            /* for gzstate.h */
  29. #include "gxdevice.h"            /* for gx_color_index */
  30. #include "gzstate.h"
  31. #include "gzcolor.h"
  32. #include "gzht.h"
  33.  
  34. /* Map a (non-pattern) color space to the number of components */
  35. /* in a color drawn from that space. */
  36. const int gs_color_space_num_components[] = {
  37.     1,    /* DeviceGray */
  38.     3,    /* DeviceRGB */
  39.     4,    /* DeviceCMYK */
  40.     3,    /* CIEBasedABC */
  41.     1,    /* CIEBasedA */
  42.     1,    /* Separation */
  43.     1,    /* Indexed */
  44.     -1    /* Pattern -- needs special handling */
  45. };
  46.  
  47. /* Halftone enumeration structure */
  48. struct gs_screen_enum_s {
  49.     halftone_params ht;        /* constructed here */
  50.     gs_matrix mat;        /* for mapping device x,y to rotated cell */
  51.     int x, y;
  52.     gs_state *pgs;
  53. };
  54.  
  55. /* Exported values */
  56. const uint gs_screen_enum_sizeof = sizeof(gs_screen_enum);
  57. const uint gs_color_sizeof = sizeof(gs_color);
  58.  
  59. /* Forward declarations */
  60. private void tri_param(P4(floatp, floatp, floatp, color_param [3]));
  61. private void tri_return(P4(color_param, color_param, color_param, float [3]));
  62. private void set_phase(P1(gs_state *));
  63. private void load_transfer_map(P2(gs_state *, gx_transfer_map *));
  64. private int unshare_transfer(P1(gs_state *));
  65. /* Imported from gxcolor.c */
  66. void    gx_color_from_hsb(P4(gs_color *, color_param, color_param, color_param)),
  67.     gx_color_from_rgb(P1(gs_color *)),
  68.     gx_color_to_hsb(P2(gs_color *, color_param [3])),
  69.     gx_sort_ht_order(P2(ht_bit *, uint));
  70.  
  71. /* setgray */
  72. int
  73. gs_setgray(gs_state *pgs, floatp gray)
  74. {    if ( pgs->in_cachedevice ) return_error(gs_error_undefined);
  75.     gx_set_gray_only(pgs->color, gx_color_unit_param(gray));
  76.     return gx_remap_color(pgs);
  77. }
  78.  
  79. /* currentgray */
  80. float
  81. gs_currentgray(gs_state *pgs)
  82. {    return (float)(color_luminance(pgs->color) / max_color_param_float);
  83. }
  84.  
  85. /* sethsbcolor */
  86. int
  87. gs_sethsbcolor(gs_state *pgs, floatp h, floatp s, floatp b)
  88. {    color_param params[3];
  89.     if ( pgs->in_cachedevice ) return_error(gs_error_undefined);
  90.     tri_param(h, s, b, params);
  91.     gx_color_from_hsb(pgs->color, params[0], params[1], params[2]);
  92.     return gx_remap_color(pgs);
  93. }
  94.  
  95. /* currenthsbcolor */
  96. int
  97. gs_currenthsbcolor(gs_state *pgs, float pr3[3])
  98. {    color_param hsb[3];
  99.     gx_color_to_hsb(pgs->color, hsb);
  100.     tri_return(hsb[0], hsb[1], hsb[2], pr3);
  101.     return 0;
  102. }
  103.  
  104. /* setrgbcolor */
  105. int
  106. gs_setrgbcolor(gs_state *pgs, floatp r, floatp g, floatp b)
  107. {    if ( pgs->in_cachedevice ) return_error(gs_error_undefined);
  108.     gx_set_rgb_only(pgs->color, gx_color_unit_param(r),
  109.             gx_color_unit_param(g), gx_color_unit_param(b));
  110.     return gx_remap_color(pgs);
  111. }
  112.  
  113. /* currentrgbcolor */
  114. int
  115. gs_currentrgbcolor(gs_state *pgs, float pr3[3])
  116. {    gs_color *pcolor = pgs->color;
  117.     tri_return(pcolor->red, pcolor->green, pcolor->blue, pr3);
  118.     return 0;
  119. }
  120.  
  121. /* setcmykcolor */
  122. int
  123. gs_setcmykcolor(gs_state *pgs, floatp c, floatp m, floatp y, floatp k)
  124. {    floatp s = 1.0 - k;
  125.     int code = gs_setrgbcolor(pgs, (1.0 - c) * s, (1.0 - m) * s,
  126.                   (1.0 - y) * s);
  127.     if ( code == 0 )
  128.         pgs->color->space = (byte)gs_color_space_DeviceCMYK;
  129.     return code;
  130. }
  131.  
  132. /* currentcmykcolor */
  133. int
  134. gs_currentcmykcolor(gs_state *pgs, float pr4[4])
  135. {    gs_color *pcolor = pgs->color;
  136.     tri_return(max_color_param - pcolor->red,
  137.            max_color_param - pcolor->green,
  138.            max_color_param - pcolor->blue,
  139.            pr4);
  140.     pr4[3] = 0.0;
  141.     return 0;
  142. }
  143.  
  144. /* setscreen */
  145. int
  146. gs_setscreen(gs_state *pgs,
  147.   floatp freq, floatp angle, float (*proc)(P2(floatp, floatp)))
  148. {    gs_screen_enum senum;
  149.     gs_point pt;
  150.     int code = gs_screen_init(&senum, pgs, freq, angle);
  151.     if ( code < 0 ) return code;
  152.     while ( (code = gs_screen_currentpoint(&senum, &pt)) == 0 )
  153.         if ( (code = gs_screen_next(&senum, (*proc)(pt.x, pt.y))) < 0 )
  154.             return code;
  155.     if ( code < 0 ) return code;
  156.     pgs->ht_proc = proc;
  157.     set_phase(pgs);
  158.     return 0;
  159. }
  160.  
  161. /* currentscreen */
  162. int
  163. gs_currentscreen(gs_state *pgs,
  164.   float *pfreq, float *pangle, float (**pproc)(P2(floatp, floatp)))
  165. {    halftone_params *pht = pgs->halftone;
  166.     *pfreq = pht->frequency;
  167.     *pangle = pht->angle;
  168.     *pproc = pgs->ht_proc;
  169.     return 0;
  170. }
  171.  
  172. /* settransfer */
  173. /* Remap=0 is used by the interpreter. */
  174. int
  175. gs_settransfer_remap(gs_state *pgs, gs_transfer_proc tproc, int remap)
  176. {    int code = unshare_transfer(pgs);
  177.     gx_transfer *ptran;
  178.     if ( code < 0 ) return code;
  179.     ptran = pgs->transfer;
  180.     ptran->gray.proc = tproc;
  181.     if ( remap )
  182.       load_transfer_map(pgs, &ptran->gray);
  183.     ptran->red = ptran->gray;
  184.     ptran->green = ptran->gray;
  185.     ptran->blue = ptran->gray;
  186.     return (remap ? gx_remap_color(pgs) : 0);
  187. }
  188. int
  189. gs_settransfer(gs_state *pgs, gs_transfer_proc tproc)
  190. {    return gs_settransfer_remap(pgs, tproc, 1);
  191. }
  192.  
  193. /* currenttransfer */
  194. gs_transfer_proc
  195. gs_currenttransfer(gs_state *pgs)
  196. {    return pgs->transfer->gray.proc;
  197. }
  198.  
  199. /* setcolortransfer */
  200. /* Remap=0 is used by the interpreter. */
  201. int
  202. gs_setcolortransfer_remap(gs_state *pgs, gs_transfer_proc red_proc,
  203.   gs_transfer_proc green_proc, gs_transfer_proc blue_proc,
  204.   gs_transfer_proc gray_proc, int remap)
  205. {    int code = unshare_transfer(pgs);
  206.     gx_transfer *ptran;
  207.     if ( code < 0 ) return code;
  208.     ptran = pgs->transfer;
  209.     ptran->red.proc = red_proc;
  210.     ptran->green.proc = green_proc;
  211.     ptran->blue.proc = blue_proc;
  212.     ptran->gray.proc = gray_proc;
  213.     if ( remap )
  214.       { load_transfer_map(pgs, &ptran->red);
  215.         load_transfer_map(pgs, &ptran->green);
  216.         load_transfer_map(pgs, &ptran->blue);
  217.         load_transfer_map(pgs, &ptran->gray);
  218.         return gx_remap_color(pgs);
  219.       }
  220.     else
  221.       return 0;
  222. }
  223. int
  224. gs_setcolortransfer(gs_state *pgs, gs_transfer_proc red_proc,
  225.   gs_transfer_proc green_proc, gs_transfer_proc blue_proc,
  226.   gs_transfer_proc gray_proc)
  227. {    return gs_setcolortransfer_remap(pgs, red_proc, green_proc,
  228.                      blue_proc, gray_proc, 1);
  229. }
  230.  
  231. /* currentcolortransfer */
  232. void
  233. gs_currentcolortransfer(gs_state *pgs, gs_transfer_proc procs[4])
  234. {    gx_transfer *ptran = pgs->transfer;
  235.     procs[0] = ptran->red.proc;
  236.     procs[1] = ptran->green.proc;
  237.     procs[2] = ptran->blue.proc;
  238.     procs[3] = ptran->gray.proc;
  239. }
  240.  
  241. /* sethalftonephase */
  242. int
  243. gs_sethalftonephase(gs_state *pgs, int x, int y)
  244. {    pgs->ht_phase.x = x;
  245.     pgs->ht_phase.y = y;
  246.     set_phase(pgs);
  247.     return 0;
  248. }
  249.  
  250. /* currenthalftonephase */
  251. int
  252. gs_currenthalftonephase(gs_state *pgs, gs_int_point *pphase)
  253. {    *pphase = pgs->ht_phase;
  254.     return 0;
  255. }
  256.  
  257. /* ------ Halftone sampling ------ */
  258.  
  259. /* Set up for halftone sampling */
  260. typedef struct rat_s { int num, denom; } rat_t;
  261. private float adjust_screen_angle(P2(floatp, rat_t *));
  262. /* There may be a fmod function and/or macro defined.... */
  263. #define fmodu(a, b) ((a) - floor((a) / (b)) * (b))
  264. int
  265. gs_screen_init(gs_screen_enum *penum, gs_state *pgs,
  266.   floatp freq, floatp angle)
  267. {    int cell_width, cell_height;
  268.     int tile_width, tile_height;
  269.     int code;
  270.     ht_bit *order;
  271.     rat_t arat;
  272.     float copies;
  273.     if ( freq < 0.1 ) return_error(gs_error_rangecheck);
  274.     /* Convert the frequency to cell width and height */
  275.        {    float cell_size = 72.0 / freq;
  276.         gs_point pcwh;
  277.         gs_matrix imat;
  278.         gs_deviceinitialmatrix(gs_currentdevice(pgs), &imat);
  279.         if ( (code = gs_distance_transform(cell_size, cell_size,
  280.                            &imat, &pcwh)) < 0
  281.             ) return code;
  282.         /* It isn't clear to me whether we should round the */
  283.         /* width and height, truncate them, or do something */
  284.         /* more complicated.  All the problems arise from devices */
  285.         /* whose X and Y resolutions aren't the same: */
  286.         /* the halftone model isn't really designed for this. */
  287.         /* For the moment, truncate and hope for the best. */
  288. #define abs_round(z) (z < 0 ? -(int)(z) : (int)(z))
  289. /*#define abs_round(z) (z < 0 ? -(int)(z - 0.5) : (int)(z + 0.5))*/
  290.         cell_width = abs_round(pcwh.x);
  291.         cell_height = abs_round(pcwh.y);
  292. #undef abs_round
  293.        }
  294.     /* Force a halfway reasonable cell size. */
  295.     if ( cell_width <= 4 ) cell_width = 4;
  296.     if ( cell_height <= 4 ) cell_height = 4;
  297.     angle = adjust_screen_angle(angle, &arat);
  298.     copies = hypot((float)arat.num, (float)arat.denom);
  299.     tile_width = cell_width * copies;
  300.     tile_height = cell_height * copies;
  301.     if ( tile_width > max_ushort / tile_height )
  302.         return_error(gs_error_limitcheck);
  303.     order = (ht_bit *)gs_malloc(tile_width * tile_height, sizeof(ht_bit),
  304.                     "halftone samples");
  305.     if ( order == 0 ) return_error(gs_error_VMerror);
  306.     penum->ht.frequency = freq;
  307.     penum->ht.angle = angle;
  308.     penum->ht.order = order;
  309.     penum->ht.width = tile_width;
  310.     penum->ht.height = tile_height;
  311.     penum->ht.order_size = tile_width * tile_height;
  312.     penum->x = penum->y = 0;
  313.     penum->pgs = pgs;
  314.     /* The transformation matrix must include normalization to the */
  315.     /* interval (-1..1), and rotation by the negative of the angle. */
  316.        {    float xscale = 2.0 / cell_width;
  317.         float yscale = 2.0 / cell_height;
  318.         gs_make_rotation(-angle, &penum->mat);
  319.         penum->mat.xx *= xscale, penum->mat.xy *= xscale;
  320.         penum->mat.yx *= yscale, penum->mat.yy *= yscale;
  321.         penum->mat.tx = -1.0;
  322.         penum->mat.ty = -1.0;
  323.         if_debug8('h', "[h]Screen: %dx%d -> %dx%d [%f %f %f %f]\n",
  324.               cell_width, cell_height, tile_width, tile_height,
  325.               penum->mat.xx, penum->mat.xy,
  326.               penum->mat.yx, penum->mat.yy);
  327.        }
  328.     return 0;
  329. }
  330.  
  331. /* Adjust the angle to one with a rational tangent with */
  332. /* small numerator and denominator. */
  333. private float
  334. adjust_screen_angle(floatp fang, rat_t *prat)
  335. {    float tang = fmodu(fang, 90) * degrees_to_radians;
  336.     int quadrant = (int)fang / 90 % 4;
  337.     const rat_t *ptrat;
  338.     float best_diff, best_ang;
  339.     static const rat_t rattab[9] =
  340.        { {0,1}, {1,3}, {1,2}, {2,3}, {1,1}, {3,2}, {2,1}, {3,1}, {1,0} };
  341.     for ( ptrat = rattab, best_diff = M_PI; ptrat->denom != 0; ptrat++ )
  342.        {    float rang = atan2((double)ptrat->num, (double)ptrat->denom);
  343.         float diff = fabs(tang - rang);
  344.         if ( diff < best_diff )
  345.             best_diff = diff, best_ang = rang, *prat = *ptrat;
  346.        }
  347.     /* If we are in an odd quadrant, swap num and denom. */
  348.     if ( quadrant & 1 )
  349.        {    int temp = prat->num;
  350.         prat->num = prat->denom;
  351.         prat->denom = temp;
  352.        }
  353.     return best_ang * radians_to_degrees + quadrant * 90;
  354. }
  355.  
  356. /* Report current point for sampling */
  357. private int gx_screen_finish(P1(gs_screen_enum *));
  358. int
  359. gs_screen_currentpoint(gs_screen_enum *penum, gs_point *ppt)
  360. {    gs_point pt;
  361.     int code;
  362.     if ( penum->y >= penum->ht.height )    /* all done */
  363.         return gx_screen_finish(penum);
  364.     if ( (code = gs_point_transform(penum->x + 0.5, penum->y + 0.5, &penum->mat, &pt)) < 0 )
  365.         return code;
  366.     while ( pt.x < -1.0 ) pt.x += 2.0;
  367.     while ( pt.x >= 1.0 ) pt.x -= 2.0;
  368.     while ( pt.y < -1.0 ) pt.y += 2.0;
  369.     while ( pt.y >= 1.0 ) pt.y -= 2.0;
  370.     *ppt = pt;
  371.     return 0;
  372. }
  373.  
  374. /* Record next halftone sample */
  375. int
  376. gs_screen_next(gs_screen_enum *penum, floatp value)
  377. {    ushort sample;
  378.     if ( value < -1.0 || value > 1.0 )
  379.       return_error(gs_error_rangecheck);
  380.     /* The following statement was split into two */
  381.     /* to work around a bug in the Siemens C compiler. */
  382.     sample = (ushort)(value * (float)(int)(max_ushort >> 1));
  383.     sample += (max_ushort >> 1);    /* convert from signed to biased */
  384. #ifdef DEBUG
  385. if ( gs_debug['h'] )
  386.    {    gs_point pt;
  387.     gs_screen_currentpoint(penum, &pt);
  388.     dprintf6("[h]sample x=%d y=%d (%f,%f): %f -> %u\n",
  389.          penum->x, penum->y, pt.x, pt.y, value, sample);
  390.    }
  391. #endif
  392.     penum->ht.order[penum->y * penum->ht.width + penum->x].mask = sample;
  393.     if ( ++(penum->x) >= penum->ht.width )
  394.         penum->x = 0, ++(penum->y);
  395.     return 0;
  396. }
  397.  
  398. /* All points have been sampled. */
  399. /* Finish constructing the halftone. */
  400. private int
  401. gx_screen_finish(gs_screen_enum *penum)
  402. {    ht_bit *order = penum->ht.order;
  403.     uint size = penum->ht.order_size;
  404.     uint i;
  405.     int code;
  406.     /* Label each element with its ordinal position. */
  407.     for ( i = 0; i < size; i++ )
  408.         order[i].offset = i;
  409.     /* Sort the samples in increasing order by value. */
  410.     gx_sort_ht_order(order, size);
  411.     /* Set up the actual halftone description. */
  412.     code = gx_ht_construct_order(order, penum->ht.width, penum->ht.height);
  413.     if ( code < 0 ) return code;
  414.     *penum->pgs->halftone = penum->ht;
  415.     return 1;            /* all done */
  416. }
  417.  
  418. /* ------ Internal routines ------ */
  419.  
  420. /* Get 3 real parameters in the range [0..1], */
  421. /* and convert them to color_params. */
  422. private void
  423. tri_param(floatp p1, floatp p2, floatp p3, color_param pq3[3])
  424. {    pq3[0] = gx_color_unit_param(p1);
  425.     pq3[1] = gx_color_unit_param(p2);
  426.     pq3[2] = gx_color_unit_param(p3);
  427. }
  428.  
  429. /* Convert 3 color_params to reals */
  430. private void
  431. tri_return(color_param p1, color_param p2, color_param p3, float pr3[3])
  432. {    pr3[0] = p1 / max_color_param_float;
  433.     pr3[1] = p2 / max_color_param_float;
  434.     pr3[2] = p3 / max_color_param_float;
  435. }
  436.  
  437. /* Compute the negated halftone phase mod the tile size. */
  438. /* This is the displacement of the tile relative to the device coordinates. */
  439. private void
  440. set_phase(gs_state *pgs)
  441. {    halftone_params *pht = pgs->halftone;
  442.     if ( pht->width == 0 )
  443.         pgs->phase_mod.x = 0;
  444.     else
  445.        {    if ( (pgs->phase_mod.x = -pgs->ht_phase.x % pht->width) < 0 )
  446.             pgs->phase_mod.x += pht->width;
  447.        }
  448.     if ( pht->height == 0 )
  449.         pgs->phase_mod.y = 0;
  450.     else
  451.        {    if ( (pgs->phase_mod.y = -pgs->ht_phase.y % pht->height) < 0 )
  452.             pgs->phase_mod.y += pht->height;
  453.        }
  454. }
  455.  
  456. /* Load one cached transfer map. */
  457. private void
  458. load_transfer_map(gs_state *pgs, gx_transfer_map *pmap)
  459. {    gs_transfer_proc proc = pmap->proc;
  460.     color_param *values = pmap->values;
  461.     int i;
  462.     for ( i = 0; i < transfer_map_size; i++ )
  463.       values[i] = gx_color_unit_param((*proc)(pgs, (float)i / transfer_map_size));
  464. }
  465.  
  466. /* Ensure that the transfer map is not shared. */
  467. private int
  468. unshare_transfer(gs_state *pgs)
  469. {    if ( pgs->transfer->ref_count > 1 )
  470.        {    gx_transfer *ptran = (gx_transfer *)(*pgs->memory_procs.alloc)(1, sizeof(gx_transfer), "unshare_transfer");
  471.         if ( ptran == 0 ) return_error(gs_error_VMerror);
  472.         ptran->ref_count = 1;
  473.         pgs->transfer->ref_count--;
  474.         pgs->transfer = ptran;
  475.        }
  476.     return 0;
  477. }
  478.