home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GXCMAP.C < prev    next >
C/C++ Source or Header  |  1994-07-30  |  15KB  |  454 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. /* gxcmap.c */
  20. /* Color mapping for Ghostscript */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gscspace.h"
  24. #include "gsccolor.h"
  25. #include "gxfarith.h"
  26. #include "gxfrac.h"
  27. #include "gxdcconv.h"
  28. #include "gxdevice.h"
  29. #include "gxdither.h"
  30. #include "gxcmap.h"
  31. #include "gxlum.h"
  32. #include "gzstate.h"
  33.  
  34. /* Convert a frac to a gx_color_value. */
  35. /* This is needed because map_rgb_color still uses gx_color_value. */
  36. #if arch_sizeof_gx_color_value == arch_sizeof_short    /* better be true! */
  37. #  define frac2cv(fr) frac2ushort(fr)
  38. #  define cv2frac(cv) ushort2frac(cv)
  39. #endif
  40.  
  41. /* Structure descriptor */
  42. public_st_device_color();
  43. /****** FOLLOWING ARE WRONG ******/
  44. private ENUM_PTRS_BEGIN(device_color_enum_ptrs) return 0;
  45. ENUM_PTRS_END
  46. private RELOC_PTRS_BEGIN(device_color_reloc_ptrs) {
  47. } RELOC_PTRS_END
  48.  
  49. /* ------ Trace device mapping procedures ------ */
  50.  
  51. /* If DEBUG is defined, these procedures substitute for direct calls */
  52. /* on the device map_{rgb,cmyk}_color procedures. */
  53.  
  54. gx_color_index
  55. gx_proc_map_rgb_color(gx_device *dev,
  56.   gx_color_value vr, gx_color_value vg, gx_color_value vb)
  57. {    gx_color_index cindex =
  58.       (*dev_proc(dev, map_rgb_color))(dev, vr, vg, vb);
  59.     if_debug4('C', "[C]RGB %u,%u,%u -> color 0x%lx\n",
  60.           (uint)vr, (uint)vg, (uint)vb, (ulong)cindex);
  61.     return cindex;
  62. }
  63.  
  64. gx_color_index
  65. gx_proc_map_rgb_alpha_color(gx_device *dev,
  66.   gx_color_value vr, gx_color_value vg, gx_color_value vb, gx_color_value va)
  67. {    gx_color_index cindex =
  68.       (*dev_proc(dev, map_rgb_alpha_color))(dev, vr, vg, vb, va);
  69.     if_debug5('C', "[C]RGBA %u,%u,%u,%u -> color 0x%lx\n",
  70.           (uint)vr, (uint)vg, (uint)vb, (uint)va, (ulong)cindex);
  71.     return cindex;
  72. }
  73.  
  74. gx_color_index
  75. gx_proc_map_cmyk_color(gx_device *dev,
  76.   gx_color_value vc, gx_color_value vm, gx_color_value vy, gx_color_value vk)
  77. {    gx_color_index cindex =
  78.       (*dev_proc(dev, map_cmyk_color))(dev, vc, vm, vy, vk);
  79.     if_debug5('C', "[C]CMYK %u,%u,%u,%u -> color 0x%lx\n",
  80.           (uint)vc, (uint)vm, (uint)vy, (uint)vk, (ulong)cindex);
  81.     return cindex;
  82.  
  83. }
  84.  
  85. /* ------ Device color rendering ------ */
  86.  
  87. private cmap_proc_gray(cmap_gray_halftoned);
  88. private cmap_proc_gray(cmap_gray_direct);
  89. private cmap_proc_gray(cmap_gray_to_rgb);
  90. private cmap_proc_gray(cmap_gray_to_cmyk);
  91. #define cmap_rgb_halftoned cmap_rgb_direct
  92. private cmap_proc_rgb(cmap_rgb_direct);
  93. private cmap_proc_rgb(cmap_rgb_to_gray);
  94. private cmap_proc_rgb(cmap_rgb_to_cmyk);
  95. #define cmap_cmyk_halftoned cmap_cmyk_direct
  96. private cmap_proc_cmyk(cmap_cmyk_direct);
  97. private cmap_proc_cmyk(cmap_cmyk_to_gray);
  98. private cmap_proc_cmyk(cmap_cmyk_to_rgb);
  99.  
  100. private const gx_color_map_procs
  101.     cmap_gray_few =
  102.         { cmap_gray_halftoned, cmap_rgb_to_gray, cmap_cmyk_to_gray },
  103.     cmap_gray_many =
  104.         { cmap_gray_direct, cmap_rgb_to_gray, cmap_cmyk_to_gray },
  105.     cmap_rgb_few =
  106.         { cmap_gray_to_rgb, cmap_rgb_halftoned, cmap_cmyk_to_rgb },
  107.     cmap_rgb_many =
  108.         { cmap_gray_to_rgb, cmap_rgb_direct, cmap_cmyk_to_rgb },
  109.     cmap_cmyk_few =
  110.         { cmap_gray_to_cmyk, cmap_rgb_to_cmyk, cmap_cmyk_halftoned },
  111.     cmap_cmyk_many =
  112.         { cmap_gray_to_cmyk, cmap_rgb_to_cmyk, cmap_cmyk_direct };
  113.  
  114. const gx_color_map_procs *cmap_procs_default = &cmap_gray_many;
  115.  
  116. private const gx_color_map_procs _ds *cmap_few[] = {
  117.     0, &cmap_gray_few, 0, &cmap_rgb_few, &cmap_cmyk_few
  118. };
  119.  
  120. private const gx_color_map_procs _ds *cmap_many[] = {
  121.     0, &cmap_gray_many, 0, &cmap_rgb_many, &cmap_cmyk_many
  122. };
  123.  
  124. /* Set the color mapping procedures in the graphics state. */
  125. void
  126. gx_set_cmap_procs(gs_state *pgs)
  127. {    gx_device *dev = gs_currentdevice_inline(pgs);
  128.     pgs->cmap_procs =
  129.         ((gx_device_has_color(dev) ? dev->color_info.max_rgb :
  130.           dev->color_info.max_gray) >= 31 ? cmap_many : cmap_few)
  131.          [dev->color_info.num_components];
  132. }
  133.  
  134. /* Remap the color in the graphics state. */
  135. int
  136. gx_remap_color(gs_state *pgs)
  137. {    const gs_color_space *pcs = pgs->color_space;
  138.     return (*pcs->type->remap_color)(pgs->ccolor, pcs, pgs->dev_color, pgs);
  139. }
  140. /* Color remappers for the standard color spaces. */
  141. #define unit_frac(v)\
  142.   (ftemp = (v),\
  143.    (is_fneg(ftemp) ? frac_0 : is_fge1(ftemp) ? frac_1 : float2frac(ftemp)))
  144. int
  145. gx_remap_DeviceGray(const gs_client_color *pc, const gs_color_space *pcs,
  146.   gx_device_color *pdc, gs_state *pgs)
  147. {    float ftemp;
  148.     (*pgs->cmap_procs->map_gray)
  149.         (unit_frac(pc->paint.values[0]),
  150.          pdc, pgs);
  151.     return 0;
  152. }
  153. int
  154. gx_remap_DeviceRGB(const gs_client_color *pc, const gs_color_space *pcs,
  155.   gx_device_color *pdc, gs_state *pgs)
  156. {    float ftemp;
  157.     (*pgs->cmap_procs->map_rgb)
  158.         (unit_frac(pc->paint.values[0]),
  159.          unit_frac(pc->paint.values[1]),
  160.          unit_frac(pc->paint.values[2]),
  161.          pdc, pgs);
  162.     return 0;
  163. }
  164. int
  165. gx_remap_DeviceCMYK(const gs_client_color *pc, const gs_color_space *pcs,
  166.   gx_device_color *pdc, gs_state *pgs)
  167. {    float ftemp;
  168.     (*pgs->cmap_procs->map_cmyk)
  169.         (unit_frac(pc->paint.values[0]),
  170.          unit_frac(pc->paint.values[1]),
  171.          unit_frac(pc->paint.values[2]),
  172.          unit_frac(pc->paint.values[3]),
  173.          pdc, pgs);
  174.     return 0;
  175. }
  176. #undef unit_frac
  177.  
  178. /* Render Gray color. */
  179.  
  180. private void
  181. cmap_gray_direct(frac gray, gx_device_color *pdc, const gs_state *pgs)
  182. {    gx_device *dev = gs_currentdevice_inline(pgs);
  183.     frac mgray = gx_map_color_frac(pgs, gray, transfer.gray);
  184.     gx_color_value cv_gray = frac2cv(mgray);
  185.     gx_color_index color =
  186.       (pgs->alpha == gx_max_color_value ?
  187.        gx_map_rgb_color(dev, cv_gray, cv_gray, cv_gray) :
  188.        gx_map_rgb_alpha_color(dev, cv_gray, cv_gray, cv_gray, pgs->alpha));
  189.     if ( color == gx_no_color_index )
  190.     {    gx_render_gray(mgray, pdc, pgs);
  191.         return;
  192.     }
  193.     color_set_pure(pdc, color);
  194. }
  195.  
  196. private void
  197. cmap_gray_halftoned(frac gray, gx_device_color *pdc, const gs_state *pgs)
  198. {    gx_render_gray(gx_map_color_frac(pgs, gray, transfer.gray), pdc, pgs);
  199. }
  200.  
  201. private void
  202. cmap_gray_to_rgb(frac gray, gx_device_color *pdc, const gs_state *pgs)
  203. {    (*pgs->cmap_procs->map_rgb)(gray, gray, gray, pdc, pgs);
  204. }
  205.  
  206. private void
  207. cmap_gray_to_cmyk(frac gray, gx_device_color *pdc, const gs_state *pgs)
  208. {    (*pgs->cmap_procs->map_cmyk)(frac_0, frac_0, frac_0, frac_1 - gray, pdc, pgs);
  209. }
  210.  
  211. /* Render RGB color. */
  212.  
  213. /*
  214.  * This code should test r == g and g == b and then use the gray
  215.  * rendering procedures.  The Adobe documentation allows this:
  216.  * conversion between color spaces occurs before the transfer function
  217.  * and halftoning.  However, output from FrameMaker (mis)uses the
  218.  * transfer function to provide the equivalent of indexed color;
  219.  * it requires the color components to be passed through unchanged.
  220.  * For this reason, we have to make the check after the transfer
  221.  * function rather than before.
  222.  */
  223.  
  224. private void
  225. cmap_rgb_direct(frac r, frac g, frac b, gx_device_color *pdc,
  226.   const gs_state *pgs)
  227. {    gx_device *dev = gs_currentdevice_inline(pgs);
  228.     frac mred = gx_map_color_frac(pgs, r, transfer.red);
  229.     frac mgreen = gx_map_color_frac(pgs, g, transfer.green);
  230.     frac mblue = gx_map_color_frac(pgs, b, transfer.blue);
  231.     /* We make a test for direct vs. halftoned, rather than */
  232.     /* duplicating most of the code of this procedure. */
  233.     if ( dev->color_info.max_rgb >= 31 )
  234.     {    gx_color_index color =
  235.           (pgs->alpha == gx_max_color_value ?
  236.            gx_map_rgb_color(dev,
  237.                     frac2cv(mred), frac2cv(mgreen),
  238.                     frac2cv(mblue)) :
  239.            gx_map_rgb_alpha_color(dev,
  240.                       frac2cv(mred), frac2cv(mgreen),
  241.                       frac2cv(mblue), pgs->alpha));
  242.         if ( color != gx_no_color_index )
  243.         {    color_set_pure(pdc, color);
  244.             return;
  245.         }
  246.     }
  247.     if ( mred == mgreen && mred == mblue )    /* gray shade */
  248.         gx_render_gray(mred, pdc, pgs);
  249.     else
  250.         gx_render_rgb(mred, mgreen, mblue, pdc, pgs);
  251. }
  252.  
  253. private void
  254. cmap_rgb_to_gray(frac r, frac g, frac b, gx_device_color *pdc,
  255.   const gs_state *pgs)
  256. {    (*pgs->cmap_procs->map_gray)(color_rgb_to_gray(r, g, b, pgs), pdc, pgs);
  257. }
  258.  
  259. private void
  260. cmap_rgb_to_cmyk(frac r, frac g, frac b, gx_device_color *pdc,
  261.   const gs_state *pgs)
  262. {    frac cmyk[4];
  263.     color_rgb_to_cmyk(r, g, b, pgs, cmyk);
  264.     (*pgs->cmap_procs->map_cmyk)(cmyk[0], cmyk[1], cmyk[2], cmyk[3], pdc, pgs);
  265. }
  266.  
  267. /* Render CMYK color. */
  268.  
  269. /* See above under RGB for why we can't use a shortcut for gray. */
  270.  
  271. private void
  272. cmap_cmyk_direct(frac c, frac m, frac y, frac k, gx_device_color *pdc,
  273.   const gs_state *pgs)
  274. {    gx_device *dev = gs_currentdevice_inline(pgs);
  275.     frac mcyan = gx_map_color_frac(pgs, c, transfer.red);
  276.     frac mmagenta = gx_map_color_frac(pgs, m, transfer.green);
  277.     frac myellow = gx_map_color_frac(pgs, y, transfer.blue);
  278.     frac mblack = gx_map_color_frac(pgs, k, transfer.gray);
  279.     /* We make a test for direct vs. halftoned, rather than */
  280.     /* duplicating most of the code of this procedure. */
  281.     if ( dev->color_info.max_rgb >= 31 )
  282.     {    gx_color_index color =
  283.             gx_map_cmyk_color(dev,
  284.                       frac2cv(mcyan), frac2cv(mmagenta),
  285.                       frac2cv(myellow), frac2cv(mblack));
  286.         if ( color != gx_no_color_index )
  287.         {    color_set_pure(pdc, color);
  288.             return;
  289.         }
  290.     }
  291.     if ( mcyan == mmagenta && mmagenta == myellow )
  292.         gx_render_gray(color_cmyk_to_gray(mcyan, mmagenta, myellow,
  293.                           mblack, pgs), pdc, pgs);
  294.     else
  295.         gx_render_cmyk(mcyan, mmagenta, myellow, mblack, pdc, pgs);
  296. }
  297.  
  298. private void
  299. cmap_cmyk_to_gray(frac c, frac m, frac y, frac k, gx_device_color *pdc, const gs_state *pgs)
  300. {    (*pgs->cmap_procs->map_gray)(color_cmyk_to_gray(c, m, y, k, pgs), pdc, pgs);
  301. }
  302.  
  303. private void
  304. cmap_cmyk_to_rgb(frac c, frac m, frac y, frac k, gx_device_color *pdc, const gs_state *pgs)
  305. {    frac rgb[3];
  306.     color_cmyk_to_rgb(c, m, y, k, pgs, rgb);
  307.     (*pgs->cmap_procs->map_rgb)(rgb[0], rgb[1], rgb[2], pdc, pgs);
  308. }
  309.  
  310. /* ------ Transfer function mapping ------ */
  311.  
  312. /* Map a color fraction through a transfer map. */
  313. frac
  314. gx_color_frac_map(frac cv, const frac *values)
  315. {
  316. #define cp_frac_bits (frac_bits - log2_transfer_map_size)
  317.     int cmi = frac2bits_floor(cv, log2_transfer_map_size);
  318.     frac mv = values[cmi];
  319.     int rem, mdv;
  320.     /* Interpolate between two adjacent values if needed. */
  321.     rem = cv - bits2frac(cmi, log2_transfer_map_size);
  322.     if ( rem == 0 ) return mv;
  323.     mdv = values[cmi + 1] - mv;
  324. #if arch_ints_are_short
  325.     /* Only use long multiplication if necessary. */
  326.     if ( mdv < -1 << (16 - cp_frac_bits) ||
  327.          mdv > 1 << (16 - cp_frac_bits)
  328.        )
  329.         return mv + (uint)(((ulong)rem * mdv) >> cp_frac_bits);
  330. #endif
  331.     return mv + ((rem * mdv) >> cp_frac_bits);
  332. #undef cp_frac_bits
  333. }
  334.  
  335. /* ------ Default device color mapping ------ */
  336.  
  337. /* RGB mapping for black-and-white devices */
  338.  
  339. gx_color_index
  340. gx_default_map_rgb_color(gx_device *dev,
  341.   gx_color_value r, gx_color_value g, gx_color_value b)
  342. {    /* Map values >= 1/2 to 1, < 1/2 to 0. */
  343.     return ((r | g | b) > gx_max_color_value / 2 ?
  344.         (gx_color_index)1 : (gx_color_index)0);
  345. }
  346.  
  347. int
  348. gx_default_map_color_rgb(gx_device *dev, gx_color_index color,
  349.   gx_color_value prgb[3])
  350. {    /* Map 1 to max_value, 0 to 0. */
  351.     prgb[0] = prgb[1] = prgb[2] = -(gx_color_value)color;
  352.     return 0;
  353. }
  354.  
  355. /* RGB mapping for gray-scale devices */
  356.  
  357. gx_color_index
  358. gx_default_gray_map_rgb_color(gx_device *dev,
  359.   gx_color_value r, gx_color_value g, gx_color_value b)
  360. {    /* We round the value rather than truncating it. */
  361.     gx_color_value gray =
  362.         ((r * (ulong)lum_red_weight) +
  363.          (g * (ulong)lum_green_weight) +
  364.          (b * (ulong)lum_blue_weight) +
  365.          (lum_all_weights / 2)) / lum_all_weights
  366.         * dev->color_info.max_gray / gx_max_color_value;
  367.     return gray;
  368. }
  369.  
  370. int
  371. gx_default_gray_map_color_rgb(gx_device *dev, gx_color_index color,
  372.   gx_color_value prgb[3])
  373. {    gx_color_value gray =
  374.         color * gx_max_color_value / dev->color_info.max_gray;
  375.     prgb[0] = gray;
  376.     prgb[1] = gray;
  377.     prgb[2] = gray;
  378.     return 0;
  379. }
  380.  
  381. /* RGB mapping for 24-bit true (RGB) color devices */
  382.  
  383. gx_color_index
  384. gx_default_rgb_map_rgb_color(gx_device *dev,
  385.   gx_color_value r, gx_color_value g, gx_color_value b)
  386. {    if ( dev->color_info.depth == 24 )
  387.         return gx_color_value_to_byte(b) +
  388.             ((uint)gx_color_value_to_byte(g) << 8) +
  389.             ((ulong)gx_color_value_to_byte(r) << 16);
  390.     else
  391.     {    uint bits_per_color = dev->color_info.depth / 3;
  392.         ulong max_value = (1 << bits_per_color) - 1;
  393.  
  394.         return ((r * max_value / gx_max_color_value) << (bits_per_color * 2)) +
  395.             ((g * max_value / gx_max_color_value) << (bits_per_color)) +
  396.             (b * max_value / gx_max_color_value);
  397.     }
  398. }
  399.  
  400. /* Map a color index to a r-g-b color. */
  401. int
  402. gx_default_rgb_map_color_rgb(gx_device *dev, gx_color_index color,
  403.   gx_color_value prgb[3])
  404. {    if ( dev->color_info.depth == 24 )
  405.     {    prgb[0] = gx_color_value_from_byte(color >> 16);
  406.         prgb[1] = gx_color_value_from_byte((color >> 8) & 0xff);
  407.         prgb[2]    = gx_color_value_from_byte(color & 0xff);
  408.     }
  409.     else
  410.     {    uint bits_per_color = dev->color_info.depth / 3;
  411.         uint color_mask = (1 << bits_per_color) - 1;
  412.  
  413.         prgb[0] = ((color >> (bits_per_color * 2)) & color_mask) *
  414.             (ulong)gx_max_color_value / color_mask;
  415.         prgb[1] = ((color >> (bits_per_color)) & color_mask) *
  416.             (ulong)gx_max_color_value / color_mask;
  417.         prgb[2] = (color & color_mask) *
  418.             (ulong)gx_max_color_value / color_mask;
  419.     }
  420.     return 0;
  421. }
  422.  
  423. /* CMYK mapping for RGB devices (should never be called!) */
  424.  
  425. gx_color_index
  426. gx_default_map_cmyk_color(gx_device *dev,
  427.   gx_color_value c, gx_color_value m, gx_color_value y, gx_color_value k)
  428. {    /* Convert to RGB */
  429.     frac rgb[3];
  430.     color_cmyk_to_rgb(cv2frac(c), cv2frac(m), cv2frac(y), cv2frac(k),
  431.               NULL, rgb);
  432.     return gx_map_rgb_color(dev, frac2cv(rgb[0]),
  433.                 frac2cv(rgb[1]), frac2cv(rgb[2]));
  434. }
  435.  
  436. /* CMYK mapping for CMYK devices */
  437.  
  438. gx_color_index
  439. gx_default_cmyk_map_cmyk_color(gx_device *dev,
  440.   gx_color_value c, gx_color_value m, gx_color_value y, gx_color_value k)
  441. {    return (gx_color_value_to_byte(k) +
  442.         ((uint)gx_color_value_to_byte(y) << 8)) +
  443.         ((ulong)(gx_color_value_to_byte(m) +
  444.             ((uint)gx_color_value_to_byte(c) << 8)) << 16);
  445. }
  446.  
  447. /* Default mapping from RGB-alpha to RGB. */
  448.  
  449. gx_color_index
  450. gx_default_map_rgb_alpha_color(gx_device *dev,
  451.   gx_color_value r, gx_color_value g, gx_color_value b, gx_color_value alpha)
  452. {    return gx_map_rgb_color(dev, r, g, b);
  453. }
  454.