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 / GXDITHER.C < prev    next >
C/C++ Source or Header  |  1992-05-01  |  13KB  |  377 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. /* gxdither.c */
  21. #include "gx.h"
  22. #include "gxfixed.h"
  23. #include "gxlum.h"
  24. #include "gxmatrix.h"
  25. #include "gzstate.h"
  26. #include "gzdevice.h"
  27. #include "gzcolor.h"
  28. #include "gzht.h"
  29.  
  30. /* 
  31.  *    Improved dithering for Ghostscript.  The underlying device imaging 
  32.  *    model supports dithering between two colors to generate intermediate
  33.  *    shades.  
  34.  *    
  35.  *    The strategy is to first see if the color is either pure white or
  36.  *    pure black.  In this case the problem is trivial.
  37.  *
  38.  *    Next, if the device has high quality colors (at least 256 values
  39.  *    per axis), we ask it to map the color directly.
  40.  *
  41.  *    Next, if the device is black and white, or the color happens
  42.  *    to be achromatic, we perform simple B/W dithering.
  43.  *    
  44.  *    Otherwise, things are a bit more complicated.  If the device 
  45.  *     supports N shades of each R, G and B independently, there are a total 
  46.  *    of N*N*N colors.  These colors form a 3-D grid in a cubical color 
  47.  *    space.  The following dithering technique works by locating the 
  48.  *    color we want in this 3-D color grid and finding the eight colors 
  49.  *     that surround it.  In the case of dithering into 8 colors with 1 
  50.  *    bit for each red, green and blue, these eight colors will always 
  51.  *    be the same.
  52.  *
  53.  *    Now we consider all possible diagonal paths between the eight colors
  54.  *    and chose the path that runs closest to our desired color in 3-D
  55.  *    color space.  There are 28 such paths.  Then we find the position
  56.  *    on the path that is closest to our color.
  57.  *
  58.  *    The search is made faster by always reflecting our color into
  59.  *    the bottom octant of the cube and comparing it to 7 paths.
  60.  *    After the best path and the best position on that path are found,
  61.  *    the results are reflected back into the original color space.
  62.  *
  63.  *    NOTE: This code has been tested for B/W and Color imaging with
  64.  *    1, 2, 3 and 8 bits per component.
  65.  *
  66.  *    --- original code by Paul Haeberli @ Silicon Graphics - 1990
  67.  *    --- extensively revised by L. Peter Deutsch, Aladdin Enterprises
  68.  */
  69.  
  70. extern void gx_color_load(P2(gx_device_color *, gs_state *));
  71.  
  72. #define    WEIGHT1        (unsigned long)(100)    /* 1.0             */
  73. #define    WEIGHT2        (unsigned long)(71)    /* 1/sqrt(2.0)         */
  74. #define    WEIGHT3        (unsigned long)(62)    /* 1/sqrt(3.0)+tad     */
  75.  
  76. #define    DIAG_R        (0x1)
  77. #define    DIAG_G        (0x2)
  78. #define    DIAG_B        (0x4)
  79. #define    DIAG_RG        (0x3)
  80. #define    DIAG_GB        (0x6)
  81. #define    DIAG_BR        (0x5)
  82. #define    DIAG_RGB    (0x7)
  83.  
  84. private const unsigned short lum_w[8] = {
  85.     (0*lum_blue_weight+0*lum_green_weight+0*lum_red_weight),
  86.     (0*lum_blue_weight+0*lum_green_weight+1*lum_red_weight),
  87.     (0*lum_blue_weight+1*lum_green_weight+0*lum_red_weight),
  88.     (0*lum_blue_weight+1*lum_green_weight+1*lum_red_weight),
  89.     (1*lum_blue_weight+0*lum_green_weight+0*lum_red_weight),
  90.     (1*lum_blue_weight+0*lum_green_weight+1*lum_red_weight),
  91.     (1*lum_blue_weight+1*lum_green_weight+0*lum_red_weight),
  92.     (1*lum_blue_weight+1*lum_green_weight+1*lum_red_weight),
  93. };
  94.  
  95. /* Compute a fractional color, the correctly rounded quotient of */
  96. /* f * max_color_param / maxv. */
  97. #define _fc(f, maxv)\
  98.   (gx_color_value)(((f) * (max_color_param_long * 2) + maxv) / (maxv * 2))
  99. /* We have to split up the following because of a bug in the IBM AIX 3.2 */
  100. /* C compiler. */
  101. private const gx_color_value
  102.   q0[] = { 0 };
  103. private const gx_color_value
  104.   q1[] = { 0, 0xffff };
  105. private const gx_color_value
  106.   q2[] = { 0, _fc(1,2), 0xffff };
  107. private const gx_color_value
  108.   q3[] = { 0, _fc(1,3), _fc(2,3), 0xffff };
  109. private const gx_color_value
  110.   q4[] = { 0, _fc(1,4), _fc(2,4), _fc(3,4), 0xffff };
  111. private const gx_color_value
  112.   q5[] = { 0, _fc(1,5), _fc(2,5), _fc(3,5), _fc(4,5), 0xffff };
  113. private const gx_color_value
  114.   q6[] = { 0, _fc(1,6), _fc(2,6), _fc(3,6), _fc(4,6), _fc(5,6), 0xffff };
  115. private const gx_color_value
  116.   q7[] = { 0, _fc(1,7), _fc(2,7), _fc(3,7), _fc(4,7), _fc(5,7), _fc(6,7), 0xffff };
  117. private const gx_color_value _ds *color_quo[8] =
  118.  { q0, q1, q2, q3, q4, q5, q6, q7 };
  119. #define fractional_color(f, maxv)\
  120.   ((maxv) <= 7 ? color_quo[maxv][f] : _fc(f, maxv))
  121.  
  122. /* Note that this routine assumes that the incoming color */
  123. /* has already been mapped through the transfer functions. */
  124. void
  125. gx_color_render(gs_color *pcolor, gx_device_color *pdevc, gs_state *pgs)
  126. {    device *pdev = pgs->device;
  127.     uint max_value;
  128.     unsigned long hsize;
  129.     gx_device *dev;
  130.     gx_color_index (*map_rgb_color)(P4(gx_device *, gx_color_value,
  131.                     gx_color_value, gx_color_value));
  132.  
  133. /* Make a special check for black and white. */
  134.     if ( pcolor->is_gray )
  135.        {    if ( pcolor->luminance == 0 )
  136.            {    pdevc->color2 = pdevc->color1 = pdev->black;
  137.             pdevc->halftone_level = 0; /* pure color */
  138.             return;
  139.            }
  140.         else if ( pcolor->luminance == max_color_param )
  141.            {    pdevc->color2 = pdevc->color1 = pdev->white;
  142.             pdevc->halftone_level = 0; /* pure color */
  143.             return;
  144.            }
  145.        }
  146.  
  147. /* get a few handy values */
  148.     dev = pdev->info;
  149.     map_rgb_color = dev->procs->map_rgb_color;
  150.     hsize = (unsigned)pgs->halftone->order_size;
  151.  
  152. /* See if we should do it in black and white. */
  153.     if ( !gx_device_has_color(dev) || pcolor->is_gray )
  154.        {    color_param lum = color_luminance(pcolor);
  155.         if ( dev->color_info.max_gray >= 31 )
  156.            {    /* Give the device a chance first. */
  157.             gx_color_index cx =
  158.                 (*map_rgb_color)(dev, lum, lum, lum);
  159.             if ( cx != gx_no_color_index )
  160.                {    pdevc->color2 = pdevc->color1 = cx;
  161.                 pdevc->halftone_level = 0;
  162.                 return;
  163.                }
  164.            }
  165.         /* No luck, must dither. */
  166.         max_value = dev->color_info.dither_gray - 1;
  167.            {    unsigned long nshades = hsize * max_value + 1;
  168.             unsigned long lx =
  169.                 (nshades * lum) / (max_color_param_long+1);
  170.             color_param l = lx / hsize;
  171.             pdevc->halftone_level = lx % hsize;
  172.             lum = fractional_color(l, max_value);
  173.             pdevc->color1 = (*map_rgb_color)(dev, lum, lum, lum);
  174.             if ( pdevc->halftone_level == 0 )
  175.                {    /* Close enough to a pure color, */
  176.                 /* no dithering needed. */
  177.                 pdevc->color2 = pdevc->color1;
  178.                }
  179.             else
  180.                {    lum = fractional_color(l+1, max_value);
  181.                 pdevc->color2 =
  182.                     (*map_rgb_color)(dev, lum, lum, lum);
  183.                }
  184.             gx_color_load(pdevc, pgs);
  185.            }
  186.         return;
  187.        }
  188.  
  189. /* If we are on a high quality RGB display, try not dithering first. */
  190.     if ( dev->color_info.max_rgb >= 31 )
  191.        {    gx_color_index cx = (*map_rgb_color)(dev, pcolor->red,
  192.                              pcolor->green,
  193.                              pcolor->blue);
  194.         if ( cx != gx_no_color_index )
  195.            {    pdevc->color2 = pdevc->color1 = cx;
  196.             pdevc->halftone_level = 0;    /* pure color */
  197.             return;
  198.            }
  199.        }
  200.     max_value = dev->color_info.dither_rgb - 1;
  201.  
  202. /* must do real color dithering */
  203.    {    color_param r, g, b;
  204.     color_param rem_r = pcolor->red;
  205.     color_param rem_g = pcolor->green;
  206.     color_param rem_b = pcolor->blue;
  207.     int adjust_r, adjust_b, adjust_g;
  208.     unsigned short amax;
  209.     unsigned long dmax;
  210.     int axisc, diagc;
  211.     unsigned short lum_invert;
  212.     unsigned long dot1, dot2, dot3;
  213.     int level;
  214.  
  215.     /* Compute the quotient and remainder of each color component */
  216.     /* with the actual number of available colors.  Avoid */
  217.     /* multiplies and divides for the common values. */
  218.     /* Note that max_color_param is all 1s, so when only a short */
  219.     /* result is needed, subtracting x*max_color_param is equivalent to */
  220.     /* just adding x.  rem_{r,g,b} are short, so we can get away */
  221.     /* with short arithmetic. */
  222.     switch ( max_value )
  223.        {
  224.     case 1:            /* 8 colors */
  225.         if ( rem_r == max_color_param ) rem_r = 0, r = 1;
  226.         else r = 0;
  227.         if ( rem_g == max_color_param ) rem_g = 0, g = 1;
  228.         else g = 0;
  229.         if ( rem_b == max_color_param ) rem_b = 0, b = 1;
  230.         else b = 0;
  231.         break;
  232.         /* When max_color_param % max_value = 0, */
  233.         /* we can do some short arithmetic. */
  234.         /* Don't bother if ints are 32 bits. */
  235. #if arch_ints_are_short
  236.     case 3:            /* 64 colors */
  237.     case 15:        /* 4096 colors */
  238.        {    const color_param q = max_color_param / max_value;
  239.         r = rem_r / q;
  240.         rem_r = rem_r * max_value + r;
  241.         g = rem_g / q;
  242.         rem_g = rem_g * max_value + g;
  243.         b = rem_b / q;
  244.         rem_b = rem_b * max_value + b;
  245.        }    break;
  246. #endif
  247.     default:
  248.        {    unsigned long want_r = (ulong)max_value * rem_r;
  249.         unsigned long want_g = (ulong)max_value * rem_g;
  250.         unsigned long want_b = (ulong)max_value * rem_b;
  251.         r = want_r / max_color_param_long;
  252.         g = want_g / max_color_param_long;
  253.         b = want_b / max_color_param_long;
  254.         rem_r = (color_param)want_r + r;
  255.         rem_g = (color_param)want_g + g;
  256.         rem_b = (color_param)want_b + b;
  257.        }
  258.        }
  259.  
  260.     /* Check for no dithering required */
  261.     if ( !(rem_r | rem_g | rem_b) )
  262.        {    pdevc->color2 = pdevc->color1 =
  263.             (*map_rgb_color)(dev, fractional_color(r, max_value),
  264.                      fractional_color(g, max_value),
  265.                      fractional_color(b, max_value));
  266.         pdevc->halftone_level = 0;
  267.         return;
  268.        }
  269.  
  270.     if_debug9('c', "[c]rgb=%x,%x,%x -->\n   %x+%x,%x+%x,%x+%x -->\n",
  271.           (unsigned)pcolor->red, (unsigned)pcolor->green,
  272.           (unsigned)pcolor->blue,
  273.           (unsigned)r, (unsigned)rem_r, (unsigned)g, (unsigned)rem_g,
  274.           (unsigned)b, (unsigned)rem_b);
  275.  
  276. /* flip the remainder color into the 0, 0, 0 octant */
  277.     lum_invert = 0;
  278. #define half ((color_param)(max_color_param_long>>1))
  279.     if ( rem_r > half )
  280.         rem_r = max_color_param - rem_r,
  281.           adjust_r = -1, r++, lum_invert += lum_red_weight;
  282.     else
  283.         adjust_r = 1;
  284.     if ( rem_g > half)
  285.         rem_g = max_color_param - rem_g,
  286.           adjust_g = -1, g++, lum_invert += lum_green_weight;
  287.     else
  288.         adjust_g = 1;
  289.     if ( rem_b > half )
  290.         rem_b = max_color_param - rem_b,
  291.           adjust_b = -1, b++, lum_invert += lum_blue_weight;
  292.     else
  293.         adjust_b = 1;
  294.     pdevc->color1 = (*map_rgb_color)(dev, fractional_color(r, max_value),
  295.                      fractional_color(g, max_value),
  296.                      fractional_color(b, max_value));
  297. /* 
  298.  * Dot the color with each axis to find the best one of 7;
  299.  * find the color at the end of the axis chosen.
  300.  */
  301.     if ( rem_g > rem_r )
  302.        {    if ( rem_b > rem_g )
  303.             amax = rem_b, axisc = DIAG_B;
  304.         else
  305.             amax = rem_g, axisc = DIAG_G;
  306.         if ( rem_b > rem_r )
  307.             dmax = (unsigned long)rem_g+rem_b, diagc = DIAG_GB;
  308.         else
  309.             dmax = (unsigned long)rem_r+rem_g, diagc = DIAG_RG;
  310.        }
  311.     else
  312.        {    if ( rem_b > rem_r )
  313.             amax = rem_b, axisc = DIAG_B;
  314.         else
  315.             amax = rem_r, axisc = DIAG_R;
  316.         if ( rem_b > rem_g )
  317.             dmax = (unsigned long)rem_b+rem_r, diagc = DIAG_BR;
  318.         else
  319.             dmax = (unsigned long)rem_r+rem_g, diagc = DIAG_RG;
  320.        }
  321.  
  322.     dot1 = amax*WEIGHT1;
  323.     dot2 = dmax*WEIGHT2;
  324.     dot3 = (ulong)rem_r+rem_g+rem_b;    /* rgb axis */
  325.     if ( dot1 > dot2 )
  326.        {    if ( dot3*WEIGHT3 > dot1 )
  327.             diagc = DIAG_RGB,
  328.               level = (hsize * dot3) / (3 * max_color_param_long);
  329.         else
  330.             diagc = axisc,
  331.               level = (hsize * amax) / max_color_param_long;
  332.        }
  333.     else
  334.        {    if ( dot3*WEIGHT3 > dot2 )
  335.             diagc = DIAG_RGB,
  336.               level = (hsize * dot3) / (3 * max_color_param_long);
  337.         else
  338.             level = (hsize * dmax) / (2 * max_color_param_long);
  339.        };
  340.  
  341.     if_debug9('c', "   %x+%x,%x+%x,%x+%x; adjust=%d,%d,%d;\n",
  342.           (unsigned)r, (unsigned)rem_r, (unsigned)g, (unsigned)rem_g,
  343.           (unsigned)b, (unsigned)rem_b,
  344.           adjust_r, adjust_g, adjust_b);
  345.  
  346.     if ( (pdevc->halftone_level = level) == 0 )
  347.         pdevc->color2 = pdevc->color1;
  348.     else
  349.        {    gx_color_index color2;
  350. /* construct the second color, inverting back to original space if needed */
  351.         if (diagc & DIAG_R) r += adjust_r;
  352.         if (diagc & DIAG_G) g += adjust_g;
  353.         if (diagc & DIAG_B) b += adjust_b;
  354. /* get the second device color, sorting by luminance */
  355.         color2 = (*map_rgb_color)(dev, fractional_color(r, max_value),
  356.                       fractional_color(g, max_value),
  357.                       fractional_color(b, max_value));
  358. /****** THIS IS A BAD IDEA ******/
  359. #if 0
  360.         if ( lum_w[diagc] < lum_invert )
  361.            {    pdevc->color2 = pdevc->color1;
  362.             pdevc->color1 = color2;
  363.             pdevc->halftone_level = level = hsize - level;
  364.            }
  365.         else
  366. #endif
  367.             pdevc->color2 = color2;
  368.         gx_color_load(pdevc, pgs);
  369.        }
  370.  
  371.     if_debug5('c', "[c]diagc=%d; color1=%lx, color2=%lx, level=%d/%d\n",
  372.           diagc, (ulong)pdevc->color1, (ulong)pdevc->color2,
  373.           level, (unsigned)hsize);
  374.  
  375.    }
  376. }
  377.