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

  1. /* Copyright (C) 1992, 1993, 1994 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. /* gscie.c */
  20. /* CIE color rendering for Ghostscript */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gsstruct.h"
  24. #include "gscspace.h"
  25. #include "gsmatrix.h"            /* for gscolor2.h */
  26. #include "gscolor2.h"            /* for gs_set/currentcolorrendering */
  27. #include "gscie.h"
  28. #include "gxarith.h"
  29. #include "gzstate.h"
  30.  
  31. /* Forward references */
  32. private void near cie_mult3(P3(const gs_vector3 *, const gs_matrix3 *, gs_vector3 *));
  33. private void near cie_matrix_mult3(P3(const gs_matrix3 *, const gs_matrix3 *, gs_matrix3 *));
  34. private void near cie_invert3(P2(const gs_matrix3 *, gs_matrix3 *));
  35. private void near cie_restrict3(P3(const gs_vector3 *, const gs_range3 *, gs_vector3 *));
  36. private void near cie_lookup3(P3(const gs_vector3 *, const gx_cie_cache *, gs_vector3 *));
  37. private void near cie_matrix_init(P1(gs_matrix3 *));
  38.  
  39. #define lookup(vin, pcache, vout)\
  40.   if ( (pcache)->is_identity ) vout = (vin);\
  41.   else vout = (pcache)->values[(int)(((vin) - (pcache)->base) * (pcache)->factor)]
  42.  
  43. #define restrict(vin, range, vout)\
  44.   if ( (vin) < (range).rmin ) vout = (range).rmin;\
  45.   else if ( (vin) > (range).rmax ) vout = (range).rmax;\
  46.   else vout = (vin)
  47.  
  48. /* Allocator structure type */
  49. private_st_joint_caches();
  50.  
  51. /* ------ Default values for CIE dictionary elements ------ */
  52.  
  53. /* Default transformation procedures. */
  54.  
  55. private int
  56. a_identity(const float *in, const gs_cie_a *pcie, float *out)
  57. {    *out = *in;
  58.     return 0;
  59. }
  60. private int
  61. abc_identity(const gs_vector3 *in, const gs_cie_abc *pcie, gs_vector3 *out)
  62. {    *out = *in;
  63.     return 0;
  64. }
  65. private int
  66. common_identity(const gs_vector3 *in, const gs_cie_common *pcie, gs_vector3 *out)
  67. {    *out = *in;
  68.     return 0;
  69. }
  70. private int
  71. render_identity(const gs_vector3 *in, const gs_cie_render *pcie, gs_vector3 *out)
  72. {    *out = *in;
  73.     return 0;
  74. }
  75. private int
  76. tpqr_identity(const gs_vector3 *in, const gs_cie_wbsd *pwbsd, const gs_cie_render *pcie, gs_vector3 *out)
  77. {    *out = *in;
  78.     return 0;
  79. }
  80. private int
  81. render_table_identity(const byte *in, int m, const gs_cie_render *pcie, float *out)
  82. {    int j;
  83.     for ( j = 0; j < m; j++ ) out[j] = in[j] / 255.0;
  84.     return 0;
  85. }
  86.  
  87. /* Default vectors and matrices. */
  88.  
  89. const gs_range3 Range3_default = { {0,1}, {0,1}, {0,1} };
  90. const gs_cie_abc_proc3 DecodeABC_default = abc_identity;
  91. const gs_cie_common_proc3 DecodeLMN_default = common_identity;
  92. const gs_matrix3 Matrix3_default = { {1,0,0}, {0,1,0}, {0,0,1}, 1 };
  93. const gs_range RangeA_default = {0,1};
  94. const gs_cie_a_proc DecodeA_default = a_identity;
  95. const gs_vector3 MatrixA_default = { 1, 1, 1 };
  96. const gs_vector3 BlackPoint_default = { 0, 0, 0 };
  97. const gs_cie_render_proc3 Encode_default = render_identity;
  98. const gs_cie_transform_proc3 TransformPQR_default = tpqr_identity;
  99. const gs_cie_render_table_proc RenderTableT_default = render_table_identity;
  100.  
  101. /* setcolorrendering */
  102. int
  103. gs_setcolorrendering(gs_state *pgs, gs_cie_render *pcie)
  104. {    int code = gs_cie_render_init(pcie);
  105.     if ( code < 0 )
  106.       return code;
  107.     rc_assign(pgs->cie_render, pcie, pgs->memory,
  108.           "gs_setcolorrendering");
  109.     /* Initialize the joint caches, if needed, */
  110.     /* by re-installing the color space. */
  111.     (*pgs->color_space->type->install_cspace)(pgs->color_space, pgs);
  112.     gx_unset_dev_color(pgs);
  113.     return 0;
  114. }
  115.  
  116. /* currentcolorrendering */
  117. const gs_cie_render *
  118. gs_currentcolorrendering(const gs_state *pgs)
  119. {    return pgs->cie_render;
  120. }
  121.  
  122. /* Get the joint caches, to avoid having to import gzstate.h */
  123. gx_cie_joint_caches *
  124. gx_currentciecaches(gs_state *pgs)
  125. {    return pgs->cie_joint_caches;
  126. }
  127.  
  128. /* ------ Complete a rendering structure ------ */
  129.  
  130. int
  131. gs_cie_render_init(gs_cie_render *pcie)
  132. {    gs_matrix3 PQR_inverse;
  133.     cie_matrix_init(&pcie->MatrixLMN);
  134.     cie_matrix_init(&pcie->MatrixABC);
  135.     cie_matrix_init(&pcie->MatrixPQR);
  136.     cie_invert3(&pcie->MatrixPQR, &PQR_inverse);
  137.     cie_matrix_mult3(&PQR_inverse, &pcie->MatrixLMN, &pcie->MatrixPQR_inverse_LMN);
  138.     cie_mult3(&pcie->points.WhitePoint, &pcie->MatrixPQR, &pcie->wdpqr);
  139.     cie_mult3(&pcie->points.BlackPoint, &pcie->MatrixPQR, &pcie->bdpqr);
  140.     /****** FINISH ******/
  141.     return 0;
  142. }
  143.  
  144. /* ------ Fill in the joint cache ------ */
  145.  
  146. int
  147. gx_cie_joint_caches_init(gx_cie_joint_caches *pjc,
  148.   const gs_cie_common *pcie, const gs_cie_render *pcier)
  149. {    pjc->points_sd.ws.xyz = pcie->points.WhitePoint;
  150.     cie_mult3(&pjc->points_sd.ws.xyz, &pcier->MatrixPQR, &pjc->points_sd.ws.pqr);
  151.     pjc->points_sd.bs.xyz = pcie->points.BlackPoint;
  152.     cie_mult3(&pjc->points_sd.bs.xyz, &pcier->MatrixPQR, &pjc->points_sd.bs.pqr);
  153.     pjc->points_sd.wd.xyz = pcier->points.WhitePoint;
  154.     pjc->points_sd.wd.pqr = pcier->wdpqr;
  155.     pjc->points_sd.bd.xyz = pcier->points.BlackPoint;
  156.     pjc->points_sd.bd.pqr = pcier->bdpqr;
  157.     /****** FINISH ******/
  158.     return 0;
  159. }
  160.  
  161. /* ------ Remap (render) a CIE color (using the caches). ------ */
  162.  
  163. private int near cie_remap_finish(P4(const gs_vector3 *,
  164.   const gs_cie_common *, gx_device_color *, gs_state *));
  165.  
  166. /* Render a CIEBasedABC color. */
  167. int
  168. gx_remap_CIEBasedABC(const gs_client_color *pc, const gs_color_space *pcs,
  169.   gx_device_color *pdc, gs_state *pgs)
  170. {    const gs_cie_abc *pcie = pcs->params.abc;
  171.     gs_vector3 abc, lmn;
  172.     cie_restrict3((const gs_vector3 *)&pc->paint.values[0], &pcie->RangeABC, &abc);
  173.         /* (*pcie->DecodeABC)(&abc, pcie, &abc); */
  174.     cie_lookup3(&abc, &pcie->caches.DecodeABC[0], &abc);
  175.     cie_mult3(&abc, &pcie->MatrixABC, &lmn);
  176.     return cie_remap_finish(&lmn, &pcie->common, pdc, pgs);
  177. }
  178.  
  179. /* Render a CIEBasedA color. */
  180. int
  181. gx_remap_CIEBasedA(const gs_client_color *pc, const gs_color_space *pcs,
  182.   gx_device_color *pdc, gs_state *pgs)
  183. {    const gs_cie_a *pcie = pcs->params.a;
  184.     const gx_cie_cache *pcache = &pcie->caches.DecodeA;
  185.     float a;
  186.     gs_vector3 lmn;
  187.     restrict(pc->paint.values[0], pcie->RangeA, a);
  188.         /* (*pcie->DecodeA)(&a, pcie, &a); */
  189.     lookup(a, pcache, a);
  190.     lmn.u = a * pcie->MatrixA.u;
  191.     lmn.v = a * pcie->MatrixA.v;
  192.     lmn.w = a * pcie->MatrixA.w;
  193.     return cie_remap_finish(&lmn, &pcie->common, pdc, pgs);
  194. }
  195.  
  196. /* Common rendering code. */
  197. private int near
  198. cie_remap_finish(const gs_vector3 *plmn, const gs_cie_common *pcommon,
  199.   gx_device_color *pdc, gs_state *pgs)
  200. {    const gs_cie_render *pcie = pgs->cie_render;
  201.     const byte **table;
  202.     gs_vector3 abc, lmn, xyz, pqr;
  203.     gs_client_color cc;
  204.     gs_color_space cs;
  205.  
  206.         /* Finish decoding. */
  207.  
  208.     cie_restrict3(plmn, &pcommon->RangeLMN, &lmn);
  209.         /* (*pcommon->DecodeLMN)(&lmn, pcommon, &lmn); */
  210.     cie_lookup3(&lmn, &pcommon->caches.DecodeLMN[0], &lmn);
  211.     cie_mult3(&lmn, &pcommon->MatrixLMN, &xyz);
  212.  
  213.         /* Render. */
  214.  
  215.     if ( pcie == 0 )        /* default rendering */
  216.     {    abc = xyz;
  217.         table = 0;
  218.     }
  219.     else
  220.     {    const gx_cie_joint_caches *pjc = pgs->cie_joint_caches;
  221.         cie_mult3(&xyz, &pcie->MatrixPQR, &pqr);
  222.         cie_restrict3(&pqr, &pcie->RangePQR, &pqr);
  223.             /* (*pcie->TransformPQR)(&pqr, &pjc->points_sd, pcie, &pqr); */
  224.         cie_lookup3(&pqr, &pjc->TransformPQR[0], &pqr);
  225.         cie_mult3(&pqr, &pcie->MatrixPQR_inverse_LMN, &lmn);
  226.             /* (*pcie->EncodeLMN)(&lmn, pcie, &lmn); */
  227.         cie_lookup3(&lmn, &pcie->caches.EncodeLMN[0], &lmn);
  228.         cie_restrict3(&lmn, &pcie->RangeLMN, &lmn);
  229.         cie_mult3(&lmn, &pcie->MatrixABC, &abc);
  230.             /* (*pcie->EncodeABC)(&abc, pcie, &abc); */
  231.         cie_lookup3(&abc, &pcie->caches.EncodeABC[0], &abc);
  232.         cie_restrict3(&abc, &pcie->RangeABC, &abc);
  233.         table = pcie->RenderTable.table;
  234.     }
  235.     if ( table == 0 )
  236.     {    /* No further transformation */
  237.         cc.paint.values[0] = abc.u;
  238.         cc.paint.values[1] = abc.v;
  239.         cc.paint.values[2] = abc.w;
  240.         cs.type = &gs_color_space_type_DeviceRGB;
  241.     }
  242.     else
  243.     {    /* Use the RenderTable. */
  244.         int m = pcie->RenderTable.m;
  245. #define ri(s,n)\
  246.   (int)((abc.s - pcie->RangeABC.s.rmin) * (pcie->RenderTable.n - 1) /\
  247.     (pcie->RangeABC.s.rmax - pcie->RangeABC.s.rmin) + 0.5)
  248.         int ia = ri(u, NA);
  249.         int ib = ri(v, NB);
  250.         int ic = ri(w, NC);
  251.         const byte *prtc =
  252.           table[ia] + m * (ib * pcie->RenderTable.NC + ic);
  253.             /* (*pcie->RenderTable.T)(prtc, m, pcie, &cc.paint.values[0]); */
  254. #define shift_in(b) gx_cie_byte_to_cache_index(b)
  255. #define rtc(i) (pcie->caches.RenderTableT[i])
  256.         cc.paint.values[0] = rtc(0).values[shift_in(prtc[0])];
  257.         cc.paint.values[1] = rtc(1).values[shift_in(prtc[1])];
  258.         cc.paint.values[2] = rtc(2).values[shift_in(prtc[2])];
  259.         if ( m == 3 )
  260.         {    cs.type = &gs_color_space_type_DeviceRGB;
  261.         }
  262.         else
  263.         {    cc.paint.values[3] = rtc(3).values[shift_in(prtc[3])];
  264.             cs.type = &gs_color_space_type_DeviceCMYK;
  265.         }
  266. #undef rtc
  267. #undef shift_in
  268.     }
  269.     return (*cs.type->remap_color)(&cc, &cs, pdc, pgs);
  270. }
  271.  
  272. /* ------ Adjust reference counts for a CIE color space ------ */
  273.  
  274. int
  275. gx_adjust_cspace_CIEBasedABC(gs_color_space *pcs, gs_state *pgs, int delta)
  276. {    rc_adjust(pcs->params.abc, delta, pgs->memory,
  277.           "gx_adjust_cspace_CIEBasedABC");
  278.     return 0;
  279. }
  280.  
  281. int
  282. gx_adjust_cspace_CIEBasedA(gs_color_space *pcs, gs_state *pgs, int delta)
  283. {    rc_adjust(pcs->params.a, delta, pgs->memory,
  284.           "gx_adjust_cspace_CIEBasedA");
  285.     return 0;
  286. }
  287.  
  288. /* ------ Install a CIE color space ------ */
  289. /* These routines should load the cache, but they don't. */
  290.  
  291. int
  292. gx_install_CIEBasedABC(gs_color_space *pcs, gs_state *pgs)
  293. {    gs_cie_abc *pcie = pcs->params.abc;
  294.     cie_matrix_init(&pcie->common.MatrixLMN);
  295.     cie_matrix_init(&pcie->MatrixABC);
  296.     if ( pgs->cie_render == 0 )
  297.         return 0;
  298.     rc_unshare_struct(pgs->cie_joint_caches, gx_cie_joint_caches,
  299.               &st_joint_caches, pgs->memory,
  300.               return_error(gs_error_VMerror),
  301.               "gx_install_CIEBasedABC");
  302.     return gx_cie_joint_caches_init(pgs->cie_joint_caches,
  303.         &pcie->common, pgs->cie_render);
  304. }
  305.  
  306. int
  307. gx_install_CIEBasedA(gs_color_space *pcs, gs_state *pgs)
  308. {    gs_cie_a *pcie = pcs->params.a;
  309.     cie_matrix_init(&pcie->common.MatrixLMN);
  310.     if ( pgs->cie_render == 0 )
  311.         return 0;
  312.     rc_unshare_struct(pgs->cie_joint_caches, gx_cie_joint_caches,
  313.               &st_joint_caches, pgs->memory,
  314.               return_error(gs_error_VMerror),
  315.               "gx_install_CIEBasedA");
  316.     return gx_cie_joint_caches_init(pgs->cie_joint_caches,
  317.         &pcie->common, pgs->cie_render);
  318. }
  319.  
  320. /* GC procedures for CIE color spaces */
  321.  
  322. #define pcs ((gs_color_space *)vptr)
  323.  
  324. ENUM_PTRS_BEGIN(gx_enum_ptrs_CIEBasedABC) return 0;
  325.     ENUM_PTR(0, gs_color_space, params.abc);
  326. ENUM_PTRS_END
  327. RELOC_PTRS_BEGIN(gx_reloc_ptrs_CIEBasedABC) {
  328.     RELOC_PTR(gs_color_space, params.abc);
  329. } RELOC_PTRS_END
  330.  
  331. ENUM_PTRS_BEGIN(gx_enum_ptrs_CIEBasedA) return 0;
  332.     ENUM_PTR(0, gs_color_space, params.a);
  333. ENUM_PTRS_END
  334. RELOC_PTRS_BEGIN(gx_reloc_ptrs_CIEBasedA) {
  335.     RELOC_PTR(gs_color_space, params.a);
  336. } RELOC_PTRS_END
  337.  
  338. #undef pcs
  339.  
  340. /* ------ Utilities ------ */
  341.  
  342. #define if_debug_vector3(str, vec)\
  343.   if_debug4('c', "%s[%g %g %g]\n", str, vec->u, vec->v, vec->w)
  344. #define if_debug_matrix3(str, mat)\
  345.   if_debug10('c', "%s[%g %g %g / %g %g %g / %g %g %g]\n", str,\
  346.     mat->cu.u, mat->cu.v, mat->cu.w, mat->cv.u, mat->cv.v, mat->cv.w,\
  347.     mat->cw.u, mat->cw.v, mat->cw.w)
  348.  
  349. /* Multiply a vector by a matrix. */
  350. /* Optimizing this routine is the justification for is_identity! */
  351. private void near
  352. cie_mult3(const gs_vector3 *in, register const gs_matrix3 *mat, gs_vector3 *out)
  353. {    if_debug_vector3("[c]mult", in);
  354.     if_debug_matrix3("    *", mat);
  355.     if ( mat->is_identity )
  356.         *out = *in;
  357.     else
  358.     {    float u = in->u, v = in->v, w = in->w;
  359.         out->u = (u * mat->cu.u) + (v * mat->cv.u) + (w * mat->cw.u);
  360.         out->v = (u * mat->cu.v) + (v * mat->cv.v) + (w * mat->cw.v);
  361.         out->w = (u * mat->cu.w) + (v * mat->cv.w) + (w * mat->cw.w);
  362.     }
  363.     if_debug_vector3("    =", out);
  364. }
  365.  
  366. /* Multiply two matrices.  We assume the result is not an alias for */
  367. /* either of the operands. */
  368. private void near
  369. cie_matrix_mult3(const gs_matrix3 *ma, const gs_matrix3 *mb, gs_matrix3 *mc)
  370. {    gs_vector3 row_in, row_out;
  371.     if_debug_matrix3("[c]matrix_mult", ma);
  372.     if_debug_matrix3("             *", mb);
  373. #define mult_row(e)\
  374.   row_in.u = ma->cu.e, row_in.v = ma->cv.e, row_in.w = ma->cw.e;\
  375.   cie_mult3(&row_in, mb, &row_out);\
  376.   mc->cu.e = row_out.u, mc->cv.e = row_out.v, mc->cw.e = row_out.w
  377.     mult_row(u);
  378.     mult_row(v);
  379.     mult_row(w);
  380. #undef mult_row
  381.     cie_matrix_init(mc);
  382.     if_debug_matrix3("             =", mc);
  383. }
  384.  
  385. /* Invert a matrix. */
  386. /* The output must not be an alias for the input. */
  387. private void near
  388. cie_invert3(register const gs_matrix3 *in, register gs_matrix3 *out)
  389. {    /* This is a brute force algorithm; maybe there are better. */
  390.     /* We label the array elements */
  391.     /*   [ A B C ]   */
  392.     /*   [ D E F ]   */
  393.     /*   [ G H I ]   */
  394. #define A cu.u
  395. #define B cv.u
  396. #define C cw.u
  397. #define D cu.v
  398. #define E cv.v
  399. #define F cw.v
  400. #define G cu.w
  401. #define H cv.w
  402. #define I cw.w
  403.     double coA = in->E * in->I - in->F * in->H; 
  404.     double coB = in->F * in->G - in->D * in->I; 
  405.     double coC = in->D * in->H - in->E * in->G;
  406.     double det = in->A * coA + in->B * coB + in->C * coC;
  407.     if_debug_matrix3("[c]invert", in);
  408.     out->A = coA / det;
  409.     out->D = coB / det;
  410.     out->G = coC / det;
  411.     out->B = (in->C * in->H - in->B * in->I) / det;
  412.     out->E = (in->A * in->I - in->C * in->G) / det;
  413.     out->H = (in->B * in->G - in->A * in->H) / det;
  414.     out->C = (in->B * in->F - in->C * in->E) / det;
  415.     out->F = (in->C * in->D - in->A * in->F) / det;
  416.     out->I = (in->A * in->E - in->B * in->D) / det;
  417.     if_debug_matrix3("        =", out);
  418. #undef A
  419. #undef B
  420. #undef C
  421. #undef D
  422. #undef E
  423. #undef F
  424. #undef G
  425. #undef H
  426. #undef I
  427.     out->is_identity = in->is_identity;
  428. }
  429.  
  430. /* Force values within bounds. */
  431. private void near
  432. cie_restrict3(const gs_vector3 *in, const gs_range3 *range, gs_vector3 *out)
  433. {    float temp;
  434.     temp = in->u; restrict(temp, range->u, out->u);
  435.     temp = in->v; restrict(temp, range->v, out->v);
  436.     temp = in->w; restrict(temp, range->w, out->w);
  437. }
  438.  
  439. private void near
  440. cie_lookup3(const gs_vector3 *in, const gx_cie_cache *pc /*[3]*/, gs_vector3 *out)
  441. {    if_debug4('c', "[c]lookup 0x%lx [%g %g %g]\n", (ulong)pc,
  442.           in->u, in->v, in->w);
  443.     lookup(in->u, pc, out->u); pc++;
  444.     lookup(in->v, pc, out->v); pc++;
  445.     lookup(in->w, pc, out->w);
  446.     if_debug_vector3("        =", out);
  447. }
  448.  
  449. /* Set the is_identity flag that accelerates multiplication. */
  450. private void near
  451. cie_matrix_init(register gs_matrix3 *mat)
  452. {    mat->is_identity =
  453.         mat->cu.u == 1.0 && is_fzero2(mat->cu.v, mat->cu.w) &&
  454.         mat->cv.v == 1.0 && is_fzero2(mat->cv.u, mat->cv.w) &&
  455.         mat->cw.w == 1.0 && is_fzero2(mat->cw.u, mat->cw.v);
  456. }
  457.