home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / GSPMSRC / SRC / GXCMAP.C < prev    next >
C/C++ Source or Header  |  1993-12-16  |  16KB  |  495 lines

  1. /* Copyright (C) 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gxcmap.c */
  20. /* Color mapping and conversion for Ghostscript */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gscspace.h"
  24. #include "gxarith.h"
  25. #include "gxfrac.h"
  26. #include "gxlum.h"
  27. #include "gxcolor.h"
  28. #include "gxdevice.h"
  29. #include "gzcolor.h"
  30. #include "gzstate.h"
  31. /* Brought back from a later release.... */
  32. #define color_set_pure(pdc, color)\
  33.   ((pdc)->color1 = (pdc)->color2 = (color), (pdc)->halftone_level = 0)
  34.  
  35. /* Convert a frac to a gx_color_value. */
  36. /* This is needed because map_rgb_color still uses gx_color_value. */
  37. #define _cv_bits (sizeof(gx_color_value) * 8)
  38. #define frac2cv(fr)\
  39.   ( ((fr) << (_cv_bits - frac_bits)) +\
  40.     ((fr) >> (frac_bits * 2 - _cv_bits)) )
  41. #define cv2frac(cv) ((frac)((cv) >> (_cv_bits - frac_bits)))
  42.  
  43. /* Note: the color model conversion algorithms are taken from */
  44. /* Rogers, Procedural Elements for Computer Graphics, pp. 401-403. */
  45.  
  46. /* ------ Conversion between HSB and RGB ------ */
  47.  
  48. /* Convert RGB to HSB. */
  49. void
  50. color_rgb_to_hsb(floatp r, floatp g, floatp b, float hsb[3])
  51. {    frac red = float2frac(r), green = float2frac(g), blue = float2frac(b);
  52. #define rhue hsb[0]
  53. #define rsat hsb[1]
  54. #define rbri hsb[2]
  55.     if ( red == green && green == blue )
  56.        {    rhue = 0;    /* arbitrary */
  57.         rsat = 0;
  58.         rbri = r;    /* pick any one */
  59.        }
  60.     else
  61.        {    /* Convert rgb to hsb */
  62.         frac V, Temp;
  63.         long diff, H;
  64.         V = (red > green ? red : green);
  65.         if ( blue > V ) V = blue;
  66.         Temp = (red > green ? green : red);
  67.         if ( blue < Temp ) Temp = blue;
  68.         diff = V - Temp;
  69.         if ( V == red )
  70.             H = (green - blue) * frac_1_long / diff;
  71.         else if ( V == green )
  72.             H = (blue - red) * frac_1_long / diff + 2 * frac_1_long;
  73.         else /* V == blue */
  74.             H = (red - green) * frac_1_long / diff + 4 * frac_1_long;
  75.         if ( H < 0 ) H += 6 * frac_1_long;
  76.         rhue = H / (frac_1 * 6.0);
  77.         rsat = diff / (float)V;
  78.         rbri = frac2float(V);
  79.        }
  80. #undef rhue
  81. #undef rsat
  82. #undef rbri
  83. }
  84.  
  85. /* Convert HSB to RGB. */
  86. void
  87. color_hsb_to_rgb(floatp hue, floatp saturation, floatp brightness, float rgb[3])
  88. {    if ( saturation == 0 )
  89.        {    rgb[0] = rgb[1] = rgb[2] = brightness;
  90.        }
  91.     else
  92.        {    /* Convert hsb to rgb. */
  93.         /* We rely on the fact that the product of two */
  94.         /* fracs fits into an unsigned long. */
  95.         floatp h6 = hue * 6;
  96.         ulong V = float2frac(brightness);    /* force arithmetic to long */
  97.         frac S = float2frac(saturation);
  98.             int I = (int)h6;
  99.         ulong F = float2frac(h6 - I);        /* ditto */
  100.         /* M = V*(1-S), N = V*(1-S*F), K = V*(1-S*(1-F)) = M-N+V */
  101.         frac M = V * (frac_1_long - S) / frac_1_long;
  102.         frac N = V * (frac_1_long - S * F / frac_1_long) / frac_1_long;
  103.         frac K = M - N + V;
  104.         frac R, G, B;
  105.         switch ( I )
  106.            {
  107.         default: R = V; G = K; B = M; break;
  108.         case 1: R = N; G = V; B = M; break;
  109.         case 2: R = M; G = V; B = K; break;
  110.         case 3: R = M; G = N; B = V; break;
  111.         case 4: R = K; G = M; B = V; break;
  112.         case 5: R = V; G = M; B = N; break;
  113.            }
  114.         rgb[0] = frac2float(R);
  115.         rgb[1] = frac2float(G);
  116.         rgb[2] = frac2float(B);
  117. #ifdef DEBUG
  118. if ( debug_c('c') )
  119. {        dprintf7("[c]hsb(%g,%g,%g)->VSFI(%ld,%d,%ld,%d)->\n",
  120.              hue, saturation, brightness, V, S, F, I);
  121.         dprintf6("   RGB(%d,%d,%d)->rgb(%g,%g,%g)\n",
  122.              R, G, B, rgb[0], rgb[1], rgb[2]);
  123. }
  124. #endif
  125.        }
  126. }
  127.  
  128. /* ------ Color space conversion ------ */
  129.  
  130. /* Only 4 of the 6 conversions are implemented here; */
  131. /* the other 2 (Gray to RGB/CMYK) are trivial. */
  132. /* The CMYK to RGB algorithms specified by Adobe are, e.g., */
  133. /*    R = 1.0 - min(1.0, C + K)    */
  134. /* but we get much better results with */
  135. /*    R = (1.0 - C) * (1.0 - K)    */
  136.  
  137. /* Convert RGB to Gray. */
  138. frac
  139. color_rgb_to_gray(frac r, frac g, frac b, const gs_state *pgs)
  140. {    return (r * (unsigned long)lum_red_weight +
  141.         g * (unsigned long)lum_green_weight +
  142.         b * (unsigned long)lum_blue_weight +
  143.         (lum_all_weights / 2))
  144.         / lum_all_weights;
  145. }
  146.  
  147. /* Convert RGB to CMYK. */
  148. /* Note that this involves black generation and undercolor removal. */
  149. void
  150. color_rgb_to_cmyk(frac r, frac g, frac b, const gs_state *pgs,
  151.   frac cmyk[4])
  152. {    frac c = frac_1 - r, m = frac_1 - g, y = frac_1 - b;
  153.     frac k = (c < m ? min(c, y) : min(m, y));
  154.     /* The default UCR and BG functions are pretty arbitrary.... */
  155.     frac bg =
  156.         (pgs->black_generation == NULL ? frac_0 :
  157.          float2frac((*pgs->black_generation)(pgs, frac2float(k))));
  158.     signed_frac ucr =
  159.         (pgs->undercolor_removal == NULL ? frac_0 :
  160.          float2frac((*pgs->undercolor_removal)(pgs, frac2float(k))));
  161.     /* Adobe specifies, e.g., */
  162.     /*    C = max(0.0, min(1.0, 1 - R - UCR)) */
  163.     /* but in order to match our improved CMYK->RGB mapping, we use */
  164.     /*    C = max(0.0, min(1.0, 1 - R / (1 - UCR)) */
  165.     if ( ucr == frac_1 )
  166.         cmyk[0] = cmyk[1] = cmyk[2] = 0;
  167.     else
  168.     {    float denom = frac2float(frac_1 - ucr);    /* unscaled */
  169.         float v;
  170.         v = (float)frac_1 - r / denom;    /* unscaled */
  171.         cmyk[0] =
  172.           (is_fneg(v) ? frac_0 : v >= (float)frac_1 ? frac_1 : (frac)v);
  173.         v = (float)frac_1 - g / denom;    /* unscaled */
  174.         cmyk[1] =
  175.           (is_fneg(v) ? frac_0 : v >= (float)frac_1 ? frac_1 : (frac)v);
  176.         v = (float)frac_1 - b / denom;    /* unscaled */
  177.         cmyk[2] =
  178.           (is_fneg(v) ? frac_0 : v >= (float)frac_1 ? frac_1 : (frac)v);
  179.     }
  180.     cmyk[3] = bg;
  181.     if_debug7('c', "[c]RGB 0x%x,0x%x,0x%x -> CMYK 0x%x,0x%x,0x%x,0x%x\n",
  182.           r, g, b, cmyk[0], cmyk[1], cmyk[2], cmyk[3]);
  183. }
  184.  
  185. /* Convert CMYK to Gray. */
  186. frac
  187. color_cmyk_to_gray(frac c, frac m, frac y, frac k, const gs_state *pgs)
  188. {    frac not_gray = color_rgb_to_gray(c, m, y, pgs);
  189.     return (not_gray > frac_1 - k ?        /* gray + k > 1.0 */
  190.         frac_0 : frac_1 - (not_gray + k));
  191. }
  192.  
  193. /* Convert CMYK to RGB. */
  194. void
  195. color_cmyk_to_rgb(frac c, frac m, frac y, frac k, const gs_state *pgs,
  196.   frac rgb[3])
  197. {    switch ( k )
  198.     {
  199.     case frac_0:
  200.         rgb[0] = frac_1 - c;
  201.         rgb[1] = frac_1 - m;
  202.         rgb[2] = frac_1 - y;
  203.         break;
  204.     case frac_1:
  205.         rgb[0] = rgb[1] = rgb[2] = frac_0;
  206.         break;
  207.     default:
  208.     {    ulong not_k = frac_1 - k;
  209.         /* Compute not_k * (frac_1 - v) / frac_1 efficiently. */
  210.         ulong prod;
  211. #define deduct_black(v)\
  212.   (prod = (frac_1 - (v)) * not_k,\
  213.    (prod + (prod >> frac_bits) + 1) >> frac_bits)
  214.         rgb[0] = deduct_black(c);
  215.         rgb[1] = deduct_black(m);
  216.         rgb[2] = deduct_black(y);
  217.     }
  218.     }
  219.     if_debug7('c', "[c]CMYK 0x%x,0x%x,0x%x,0x%x -> RGB 0x%x,0x%x,0x%x\n",
  220.           c, m, y, k, rgb[0], rgb[1], rgb[2]);
  221. }
  222.  
  223. /* ------ Device color rendering ------ */
  224.  
  225. private cmap_proc_gray(cmap_gray_halftoned);
  226. private cmap_proc_gray(cmap_gray_direct);
  227. private cmap_proc_gray(cmap_gray_to_rgb);
  228. private cmap_proc_gray(cmap_gray_to_cmyk);
  229. #define cmap_rgb_halftoned cmap_rgb_direct
  230. private cmap_proc_rgb(cmap_rgb_direct);
  231. private cmap_proc_rgb(cmap_rgb_to_gray);
  232. private cmap_proc_rgb(cmap_rgb_to_cmyk);
  233. #define cmap_cmyk_halftoned cmap_cmyk_direct
  234. private cmap_proc_cmyk(cmap_cmyk_direct);
  235. private cmap_proc_cmyk(cmap_cmyk_to_gray);
  236. private cmap_proc_cmyk(cmap_cmyk_to_rgb);
  237.  
  238. private const gx_color_map_procs
  239.     cmap_gray_few =
  240.         { cmap_gray_halftoned, cmap_rgb_to_gray, cmap_cmyk_to_gray },
  241.     cmap_gray_many =
  242.         { cmap_gray_direct, cmap_rgb_to_gray, cmap_cmyk_to_gray },
  243.     cmap_rgb_few =
  244.         { cmap_gray_to_rgb, cmap_rgb_halftoned, cmap_cmyk_to_rgb },
  245.     cmap_rgb_many =
  246.         { cmap_gray_to_rgb, cmap_rgb_direct, cmap_cmyk_to_rgb },
  247.     cmap_cmyk_few =
  248.         { cmap_gray_to_cmyk, cmap_rgb_to_cmyk, cmap_cmyk_halftoned },
  249.     cmap_cmyk_many =
  250.         { cmap_gray_to_cmyk, cmap_rgb_to_cmyk, cmap_cmyk_direct };
  251.  
  252. const gx_color_map_procs *cmap_procs_default = &cmap_gray_many;
  253.  
  254. private const gx_color_map_procs _ds *cmap_few[] = {
  255.     0, &cmap_gray_few, 0, &cmap_rgb_few, &cmap_cmyk_few
  256. };
  257.  
  258. private const gx_color_map_procs _ds *cmap_many[] = {
  259.     0, &cmap_gray_many, 0, &cmap_rgb_many, &cmap_cmyk_many
  260. };
  261.  
  262. /* Set the color mapping procedures in the graphics state. */
  263. void
  264. gx_set_cmap_procs(gs_state *pgs)
  265. {    gx_device *dev = gs_currentdevice(pgs);
  266.     pgs->cmap_procs =
  267.         ((gx_device_has_color(dev) ? dev->color_info.max_rgb :
  268.           dev->color_info.max_gray) >= 31 ? cmap_many : cmap_few)
  269.          [dev->color_info.num_components];
  270. }
  271.  
  272. /* Remap the color in the graphics state. */
  273. int
  274. gx_remap_color(gs_state *pgs)
  275. {    const gs_color_space *pcs = pgs->color_space;
  276.     return (*pcs->type->remap_color)(pgs->ccolor, pcs,
  277.                 pgs->dev_color, pgs);
  278. }
  279. /* Color remappers for the standard color spaces. */
  280. #define unit_frac(v)\
  281.   (ftemp = (v),\
  282.    (is_fneg(ftemp) ? frac_0 : ftemp >= 1.0 ? frac_1 : float2frac(ftemp)))
  283. int
  284. gx_remap_DeviceGray(const gs_client_color *pc, const gs_color_space *pcs,
  285.   gx_device_color *pdc, gs_state *pgs)
  286. {    float ftemp;
  287.     (*pgs->cmap_procs->map_gray)
  288.         (unit_frac(pc->paint.values[0]),
  289.          pdc, pgs);
  290.     return 0;
  291. }
  292. int
  293. gx_remap_DeviceRGB(const gs_client_color *pc, const gs_color_space *pcs,
  294.   gx_device_color *pdc, gs_state *pgs)
  295. {    float ftemp;
  296.     (*pgs->cmap_procs->map_rgb)
  297.         (unit_frac(pc->paint.values[0]),
  298.          unit_frac(pc->paint.values[1]),
  299.          unit_frac(pc->paint.values[2]),
  300.          pdc, pgs);
  301.     return 0;
  302. }
  303. int
  304. gx_remap_DeviceCMYK(const gs_client_color *pc, const gs_color_space *pcs,
  305.   gx_device_color *pdc, gs_state *pgs)
  306. {    float ftemp;
  307.     (*pgs->cmap_procs->map_cmyk)
  308.         (unit_frac(pc->paint.values[0]),
  309.          unit_frac(pc->paint.values[1]),
  310.          unit_frac(pc->paint.values[2]),
  311.          unit_frac(pc->paint.values[3]),
  312.          pdc, pgs);
  313.     return 0;
  314. }
  315. #undef unit_frac
  316.  
  317. /* Render Gray color. */
  318.  
  319. private void
  320. cmap_gray_direct(frac gray, gx_device_color *pdc, const gs_state *pgs)
  321. {    gx_device *dev = gs_currentdevice(pgs);
  322.     frac mgray = gx_map_color_frac(pgs, gray, gray);
  323.     gx_color_value cv_gray = frac2cv(mgray);
  324.     gx_color_index color =
  325.         (*dev->procs->map_rgb_color)(dev, cv_gray, cv_gray, cv_gray);
  326.     if ( color == gx_no_color_index )
  327.     {    gx_render_gray(mgray, pdc, pgs);
  328.         return;
  329.     }
  330.     color_set_pure(pdc, color);
  331. }
  332.  
  333. private void
  334. cmap_gray_halftoned(frac gray, gx_device_color *pdc, const gs_state *pgs)
  335. {    gx_render_gray(gx_map_color_frac(pgs, gray, gray), pdc, pgs);
  336. }
  337.  
  338. private void
  339. cmap_gray_to_rgb(frac gray, gx_device_color *pdc, const gs_state *pgs)
  340. {    (*pgs->cmap_procs->map_rgb)(gray, gray, gray, pdc, pgs);
  341. }
  342.  
  343. private void
  344. cmap_gray_to_cmyk(frac gray, gx_device_color *pdc, const gs_state *pgs)
  345. {    (*pgs->cmap_procs->map_cmyk)(frac_0, frac_0, frac_0, gray, pdc, pgs);
  346. }
  347.  
  348. /* Render RGB color. */
  349.  
  350. /*
  351.  * This code should test r == g and g == b and then use the gray
  352.  * rendering procedures.  The Adobe documentation allows this:
  353.  * conversion between color spaces occurs before the transfer function
  354.  * and halftoning.  However, output from FrameMaker (mis)uses the
  355.  * transfer function to provide the equivalent of indexed color;
  356.  * it requires the color components to be passed through unchanged.
  357.  * For this reason, we have to make the check after the transfer
  358.  * function rather than before.
  359.  */
  360.  
  361. private void
  362. cmap_rgb_direct(frac r, frac g, frac b, gx_device_color *pdc,
  363.   const gs_state *pgs)
  364. {    gx_device *dev = gs_currentdevice(pgs);
  365.     frac mred = gx_map_color_frac(pgs, r, red);
  366.     frac mgreen = gx_map_color_frac(pgs, g, green);
  367.     frac mblue = gx_map_color_frac(pgs, b, blue);
  368.     /* We make a test for direct vs. halftoned, rather than */
  369.     /* duplicating most of the code of this procedure. */
  370.     if ( dev->color_info.max_rgb >= 31 )
  371.     {    gx_color_index color =
  372.             (*dev->procs->map_rgb_color)(dev,
  373.                 frac2cv(mred), frac2cv(mgreen),
  374.                 frac2cv(mblue));
  375.         if ( color != gx_no_color_index )
  376.         {    color_set_pure(pdc, color);
  377.             return;
  378.         }
  379.     }
  380.     if ( mred == mgreen && mred == mblue )    /* gray shade */
  381.         gx_render_gray(mred, pdc, pgs);
  382.     else
  383.         gx_render_rgb(mred, mgreen, mblue, pdc, pgs);
  384. }
  385.  
  386. private void
  387. cmap_rgb_to_gray(frac r, frac g, frac b, gx_device_color *pdc,
  388.   const gs_state *pgs)
  389. {    (*pgs->cmap_procs->map_gray)(color_rgb_to_gray(r, g, b, pgs), pdc, pgs);
  390. }
  391.  
  392. private void
  393. cmap_rgb_to_cmyk(frac r, frac g, frac b, gx_device_color *pdc,
  394.   const gs_state *pgs)
  395. {    frac cmyk[4];
  396.     color_rgb_to_cmyk(r, g, b, pgs, cmyk);
  397.     (*pgs->cmap_procs->map_cmyk)(cmyk[0], cmyk[1], cmyk[2], cmyk[3], pdc, pgs);
  398. }
  399.  
  400. /* Render CMYK color. */
  401.  
  402. /* See above under RGB for why we can't use a shortcut for gray. */
  403.  
  404. private void
  405. cmap_cmyk_direct(frac c, frac m, frac y, frac k, gx_device_color *pdc,
  406.   const gs_state *pgs)
  407. {    gx_device *dev = gs_currentdevice(pgs);
  408.     frac mcyan = gx_map_color_frac(pgs, c, red);
  409.     frac mmagenta = gx_map_color_frac(pgs, m, green);
  410.     frac myellow = gx_map_color_frac(pgs, y, blue);
  411.     frac mblack = gx_map_color_frac(pgs, k, gray);
  412.     /* We make a test for direct vs. halftoned, rather than */
  413.     /* duplicating most of the code of this procedure. */
  414.     if ( dev->color_info.max_rgb >= 31 )
  415.     {    gx_color_index color =
  416.             (*dev->procs->map_cmyk_color)(dev,
  417.                 frac2cv(mcyan), frac2cv(mmagenta),
  418.                 frac2cv(myellow), frac2cv(mblack));
  419.         if ( color != gx_no_color_index )
  420.         {    color_set_pure(pdc, color);
  421.             return;
  422.         }
  423.     }
  424.     /* CMYK halftones are not implemented yet. */
  425.     {    frac rgb[3];
  426.         color_cmyk_to_rgb(c, m, y, k, pgs, rgb);
  427.         cmap_rgb_halftoned(rgb[0], rgb[1], rgb[2], pdc, pgs);
  428.     }
  429. }
  430.  
  431. private void
  432. cmap_cmyk_to_gray(frac c, frac m, frac y, frac k, gx_device_color *pdc, const gs_state *pgs)
  433. {    (*pgs->cmap_procs->map_gray)(color_cmyk_to_gray(c, m, y, k, pgs), pdc, pgs);
  434. }
  435.  
  436. private void
  437. cmap_cmyk_to_rgb(frac c, frac m, frac y, frac k, gx_device_color *pdc, const gs_state *pgs)
  438. {    frac rgb[3];
  439.     color_cmyk_to_rgb(c, m, y, k, pgs, rgb);
  440.     (*pgs->cmap_procs->map_rgb)(rgb[0], rgb[1], rgb[2], pdc, pgs);
  441. }
  442.  
  443. /* ------ Transfer function mapping ------ */
  444.  
  445. /* Map a color fraction through a transfer map. */
  446. frac
  447. gx_color_frac_map(frac cv, const frac *values)
  448. {
  449. #define cp_frac_bits (frac_bits - log2_transfer_map_size)
  450.     int cmi = cv >> cp_frac_bits;
  451.     frac mv = values[cmi];
  452.     int rem, mdv;
  453.     /* Interpolate between two adjacent values if needed. */
  454.     rem = (cv & ((1 << cp_frac_bits) - 1)) - (cv >> (frac_bits - cp_frac_bits));
  455.     if ( rem == 0 ) return mv;
  456.     else if ( rem > 0 ) mdv = values[cmi + 1] - mv;
  457.     else mdv = mv - values[cmi - 1];
  458. #if arch_ints_are_short
  459.     /* Only use long multiplication if necessary. */
  460.     if ( mdv > 1 << (16 - cp_frac_bits) )
  461.         return mv + (uint)(((ulong)rem * mdv) >> cp_frac_bits);
  462. #endif
  463.     return mv + ((rem * mdv) >> cp_frac_bits);
  464. #undef cp_frac_bits
  465. }
  466.  
  467. /* ------ Default device color mapping ------ */
  468.  
  469. gx_color_index
  470. gx_default_map_rgb_color(gx_device *dev,
  471.   gx_color_value r, gx_color_value g, gx_color_value b)
  472. {    /* Map values >= 1/2 to 1, < 1/2 to 0. */
  473.     return ((r | g | b) > gx_max_color_value / 2 ?
  474.         (gx_color_index)1 : (gx_color_index)0);
  475. }
  476.  
  477. int
  478. gx_default_map_color_rgb(gx_device *dev, gx_color_index color,
  479.   gx_color_value prgb[3])
  480. {    /* Map 1 to max_value, 0 to 0. */
  481.     prgb[0] = prgb[1] = prgb[2] = -(gx_color_value)color;
  482.     return 0;
  483. }
  484.  
  485. gx_color_index
  486. gx_default_map_cmyk_color(gx_device *dev,
  487.   gx_color_value c, gx_color_value m, gx_color_value y, gx_color_value k)
  488. {    /* Convert to RGB */
  489.     frac rgb[3];
  490.     color_cmyk_to_rgb(cv2frac(c), cv2frac(m), cv2frac(y), cv2frac(k),
  491.               NULL, rgb);
  492.     return (*dev->procs->map_rgb_color)(dev,
  493.         frac2cv(rgb[0]), frac2cv(rgb[1]), frac2cv(rgb[2]));
  494. }
  495.