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

  1. /* Copyright (C) 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. /* gxdcconv.c */
  20. /* Conversion between device color spaces for Ghostscript */
  21. #include "gx.h"
  22. #include "gxdcconv.h"            /* interface */
  23. #include "gxdcolor.h"            /* for gxcmap.h */
  24. #include "gxdevice.h"            /* ditto */
  25. #include "gxcmap.h"
  26. #include "gxfarith.h"
  27. #include "gxlum.h"
  28. #include "gzstate.h"
  29.  
  30. /* Note: the color model conversion algorithms are taken from */
  31. /* Rogers, Procedural Elements for Computer Graphics, pp. 401-403. */
  32.  
  33. /* ------ Conversion between HSB and RGB ------ */
  34.  
  35. /* Convert RGB to HSB. */
  36. void
  37. color_rgb_to_hsb(floatp r, floatp g, floatp b, float hsb[3])
  38. {    frac red = float2frac(r), green = float2frac(g), blue = float2frac(b);
  39. #define rhue hsb[0]
  40. #define rsat hsb[1]
  41. #define rbri hsb[2]
  42.     if ( red == green && green == blue )
  43.        {    rhue = 0;    /* arbitrary */
  44.         rsat = 0;
  45.         rbri = r;    /* pick any one */
  46.        }
  47.     else
  48.        {    /* Convert rgb to hsb */
  49.         frac V, Temp;
  50.         long diff, H;
  51.         V = (red > green ? red : green);
  52.         if ( blue > V ) V = blue;
  53.         Temp = (red > green ? green : red);
  54.         if ( blue < Temp ) Temp = blue;
  55.         diff = V - Temp;
  56.         if ( V == red )
  57.             H = (green - blue) * frac_1_long / diff;
  58.         else if ( V == green )
  59.             H = (blue - red) * frac_1_long / diff + 2 * frac_1_long;
  60.         else /* V == blue */
  61.             H = (red - green) * frac_1_long / diff + 4 * frac_1_long;
  62.         if ( H < 0 ) H += 6 * frac_1_long;
  63.         rhue = H / (frac_1 * 6.0);
  64.         rsat = diff / (float)V;
  65.         rbri = frac2float(V);
  66.        }
  67. #undef rhue
  68. #undef rsat
  69. #undef rbri
  70. }
  71.  
  72. /* Convert HSB to RGB. */
  73. void
  74. color_hsb_to_rgb(floatp hue, floatp saturation, floatp brightness, float rgb[3])
  75. {    if ( saturation == 0 )
  76.        {    rgb[0] = rgb[1] = rgb[2] = brightness;
  77.        }
  78.     else
  79.        {    /* Convert hsb to rgb. */
  80.         /* We rely on the fact that the product of two */
  81.         /* fracs fits into an unsigned long. */
  82.         floatp h6 = hue * 6;
  83.         ulong V = float2frac(brightness);    /* force arithmetic to long */
  84.         frac S = float2frac(saturation);
  85.         int I = (int)h6;
  86.         ulong F = float2frac(h6 - I);        /* ditto */
  87.         /* M = V*(1-S), N = V*(1-S*F), K = V*(1-S*(1-F)) = M-N+V */
  88.         frac M = V * (frac_1_long - S) / frac_1_long;
  89.         frac N = V * (frac_1_long - S * F / frac_1_long) / frac_1_long;
  90.         frac K = M - N + V;
  91.         frac R, G, B;
  92.         switch ( I )
  93.            {
  94.         default: R = V; G = K; B = M; break;
  95.         case 1: R = N; G = V; B = M; break;
  96.         case 2: R = M; G = V; B = K; break;
  97.         case 3: R = M; G = N; B = V; break;
  98.         case 4: R = K; G = M; B = V; break;
  99.         case 5: R = V; G = M; B = N; break;
  100.            }
  101.         rgb[0] = frac2float(R);
  102.         rgb[1] = frac2float(G);
  103.         rgb[2] = frac2float(B);
  104. #ifdef DEBUG
  105. if ( gs_debug_c('c') )
  106. {        dprintf7("[c]hsb(%g,%g,%g)->VSFI(%ld,%d,%ld,%d)->\n",
  107.              hue, saturation, brightness, V, S, F, I);
  108.         dprintf6("   RGB(%d,%d,%d)->rgb(%g,%g,%g)\n",
  109.              R, G, B, rgb[0], rgb[1], rgb[2]);
  110. }
  111. #endif
  112.        }
  113. }
  114.  
  115. /* ------ Color space conversion ------ */
  116.  
  117. /* Only 4 of the 6 conversions are implemented here; */
  118. /* the other 2 (Gray to RGB/CMYK) are trivial. */
  119. /* The CMYK to RGB algorithms specified by Adobe are, e.g., */
  120. /*    R = 1.0 - min(1.0, C + K)    */
  121. /* but we get much better results with */
  122. /*    R = (1.0 - C) * (1.0 - K)    */
  123.  
  124. /* Convert RGB to Gray. */
  125. frac
  126. color_rgb_to_gray(frac r, frac g, frac b, const gs_state *pgs)
  127. {    return (r * (unsigned long)lum_red_weight +
  128.         g * (unsigned long)lum_green_weight +
  129.         b * (unsigned long)lum_blue_weight +
  130.         (lum_all_weights / 2))
  131.         / lum_all_weights;
  132. }
  133.  
  134. /* Convert RGB to CMYK. */
  135. /* Note that this involves black generation and undercolor removal. */
  136. void
  137. color_rgb_to_cmyk(frac r, frac g, frac b, const gs_state *pgs,
  138.   frac cmyk[4])
  139. {    frac c = frac_1 - r, m = frac_1 - g, y = frac_1 - b;
  140.     frac k = (c < m ? min(c, y) : min(m, y));
  141.     /* The default UCR and BG functions are pretty arbitrary.... */
  142.     frac bg =
  143.         (pgs->black_generation == NULL ? frac_0 :
  144.          gx_map_color_frac(pgs, k, black_generation));
  145.     signed_frac ucr =
  146.         (pgs->undercolor_removal == NULL ? frac_0 :
  147.          gx_map_color_frac(pgs, k, undercolor_removal));
  148.     /* Adobe specifies, e.g., */
  149.     /*    C = max(0.0, min(1.0, 1 - R - UCR)) */
  150.     /* but in order to match our improved CMYK->RGB mapping, we use */
  151.     /*    C = max(0.0, min(1.0, 1 - R / (1 - UCR)) */
  152.     if ( ucr == frac_1 )
  153.         cmyk[0] = cmyk[1] = cmyk[2] = 0;
  154.     else
  155.     {    float denom = frac2float(frac_1 - ucr);    /* unscaled */
  156.         float v;
  157.         v = (float)frac_1 - r / denom;    /* unscaled */
  158.         cmyk[0] =
  159.           (is_fneg(v) ? frac_0 : v >= (float)frac_1 ? frac_1 : (frac)v);
  160.         v = (float)frac_1 - g / denom;    /* unscaled */
  161.         cmyk[1] =
  162.           (is_fneg(v) ? frac_0 : v >= (float)frac_1 ? frac_1 : (frac)v);
  163.         v = (float)frac_1 - b / denom;    /* unscaled */
  164.         cmyk[2] =
  165.           (is_fneg(v) ? frac_0 : v >= (float)frac_1 ? frac_1 : (frac)v);
  166.     }
  167.     cmyk[3] = bg;
  168.     if_debug7('c', "[c]RGB 0x%x,0x%x,0x%x -> CMYK 0x%x,0x%x,0x%x,0x%x\n",
  169.           r, g, b, cmyk[0], cmyk[1], cmyk[2], cmyk[3]);
  170. }
  171.  
  172. /* Convert CMYK to Gray. */
  173. frac
  174. color_cmyk_to_gray(frac c, frac m, frac y, frac k, const gs_state *pgs)
  175. {    frac not_gray = color_rgb_to_gray(c, m, y, pgs);
  176.     return (not_gray > frac_1 - k ?        /* gray + k > 1.0 */
  177.         frac_0 : frac_1 - (not_gray + k));
  178. }
  179.  
  180. /* Convert CMYK to RGB. */
  181. void
  182. color_cmyk_to_rgb(frac c, frac m, frac y, frac k, const gs_state *pgs,
  183.   frac rgb[3])
  184. {    switch ( k )
  185.     {
  186.     case frac_0:
  187.         rgb[0] = frac_1 - c;
  188.         rgb[1] = frac_1 - m;
  189.         rgb[2] = frac_1 - y;
  190.         break;
  191.     case frac_1:
  192.         rgb[0] = rgb[1] = rgb[2] = frac_0;
  193.         break;
  194.     default:
  195.     {    ulong not_k = frac_1 - k;
  196.         /* Compute not_k * (frac_1 - v) / frac_1 efficiently. */
  197.         ulong prod;
  198. #define deduct_black(v)\
  199.   (prod = (frac_1 - (v)) * not_k, frac_1_quo(prod))
  200.         rgb[0] = deduct_black(c);
  201.         rgb[1] = deduct_black(m);
  202.         rgb[2] = deduct_black(y);
  203.     }
  204.     }
  205.     if_debug7('c', "[c]CMYK 0x%x,0x%x,0x%x,0x%x -> RGB 0x%x,0x%x,0x%x\n",
  206.           c, m, y, k, rgb[0], rgb[1], rgb[2]);
  207. }
  208.