home *** CD-ROM | disk | FTP | other *** search
/ Der Mediaplex Sampler - Die 6 von Plex / 6_v_plex.zip / 6_v_plex / DISK5 / DOS_14 / GS252DVX.ZIP / GSCIE.C < prev    next >
C/C++ Source or Header  |  1992-06-21  |  8KB  |  244 lines

  1. /* Copyright (C) 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. /* gscie.c */
  21. /* CIE color algorithms for Ghostscript */
  22. #include "std.h"
  23. #include "gscspace.h"
  24. #include "gscie.h"
  25.  
  26. /* Default values for components. */
  27. private float
  28. fp_identity(floatp value)
  29. {    return (float)value;
  30. }
  31. const gs_range3 Range3_default = { {0,1}, {0,1}, {0,1} };
  32. const gs_float_proc3 Decode3_default = { fp_identity, fp_identity, fp_identity };
  33. const gs_matrix3 Matrix3_default = { {1,0,0}, {0,1,0}, {0,0,1} };
  34. const gs_range RangeA_default = {0,1};
  35. const gs_float_proc DecodeA_default = fp_identity;
  36. const gs_vector3 MatrixA_default = { 1, 1, 1 };
  37. const gs_vector3 BlackPoint_default = { 0, 0, 0 };
  38.  
  39. /* Apply procedures to a vector. */
  40. private void
  41. cie_apply3(const gs_vector3 *in, const gs_float_proc3 *procs, gs_vector3 *out)
  42. {    out->u = (*procs->u)(in->u);
  43.     out->v = (*procs->v)(in->v);
  44.     out->w = (*procs->w)(in->w);
  45. }
  46.  
  47. /* Multiply a vector by a matrix. */
  48. private void
  49. cie_mult3(const gs_vector3 *in, register const gs_matrix3 *mat, gs_vector3 *out)
  50. {    float u = in->u, v = in->v, w = in->w;
  51.     out->u = (u * mat->cu.u) + (v * mat->cu.v) + (w * mat->cu.w);
  52.     out->v = (u * mat->cv.u) + (v * mat->cv.v) + (w * mat->cv.w);
  53.     out->w = (u * mat->cw.u) + (v * mat->cw.v) + (w * mat->cw.w);
  54. }
  55.  
  56. /* Invert a matrix. */
  57. private void
  58. cie_invert3(register const gs_matrix3 *in, register gs_matrix3 *out)
  59. {    /* This is a brute force algorithm; maybe there are better. */
  60.     /* We label the array elements */
  61.     /*   [ A B C ]   */
  62.     /*   [ D E F ]   */
  63.     /*   [ G H I ]   */
  64. #define A cu.u
  65. #define B cv.u
  66. #define C cw.u
  67. #define D cu.v
  68. #define E cv.v
  69. #define F cw.v
  70. #define G cu.w
  71. #define H cv.w
  72. #define I cw.w
  73.     double AE = in->A * in->E, AF = in->A * in->F,
  74.         AH = in->A * in->H, AI = in->A * in->I;
  75.     double BD = in->B * in->D, BF = in->B * in->F,
  76.         BG = in->B * in->G, BI = in->B * in->I;
  77.     double CD = in->C * in->D, CE = in->C * in->E,
  78.         CG = in->C * in->G, CH = in->C * in->H;
  79.     double DH = in->D * in->H, DI = in->D * in->I;
  80.     double EG = in->E * in->G, EI = in->E * in->I;
  81.     double FG = in->F * in->G, FH = in->F * in->H;
  82.     double coA = EI - FH, coB = FG - DI, coC = DH - EG;
  83.     double det = in->A * coA + in->B * coB + in->C * coC;
  84.     out->A = coA / det;
  85.     out->D = coB / det;
  86.     out->G = coC / det;
  87.     out->B = (CH - BI) / det;
  88.     out->E = (AI - CG) / det;
  89.     out->H = (BG - AH) / det;
  90.     out->C = (BF - CE) / det;
  91.     out->F = (CD - AF) / det;
  92.     out->I = (AE - BD) / det;
  93. #undef A
  94. #undef B
  95. #undef C
  96. #undef D
  97. #undef E
  98. #undef F
  99. #undef G
  100. #undef H
  101. #undef I
  102. }
  103.  
  104. /* Force values within bounds. */
  105. #define restrict(v, r)\
  106.   ((v) < (r).rmin ? (r).rmin : (v) > (r).rmax ? (r).rmax : (v))
  107. private void
  108. cie_restrict3(const gs_vector3 *in, const gs_range3 *range, gs_vector3 *out)
  109. {    float temp;
  110.     temp = in->u; out->u = restrict(temp, range->u);
  111.     temp = in->v; out->v = restrict(temp, range->v);
  112.     temp = in->w; out->w = restrict(temp, range->w);
  113. }
  114.  
  115. /* Decode ABC values to XYZ. */
  116. int
  117. gs_cie_abc_decode1(const gs_vector3 *pabc, gs_vector3 *ptabc, const gs_cie_abc *pcie)
  118. {    cie_restrict3(pabc, &pcie->RangeABC, ptabc);
  119.     return 0;
  120. }
  121. /*
  122.  * Client:
  123.     cie_apply3(ptabc, &pcie->DecodeABC, ptabc);
  124.  */
  125. int
  126. gs_cie_abc_decode2(const gs_vector3 *ptabc, gs_vector3 *ptlmn, const gs_cie_abc *pcie)
  127. {    cie_mult3(ptabc, &pcie->MatrixABC, ptlmn);
  128.     cie_restrict3(ptlmn, &pcie->common.RangeLMN, ptlmn);
  129.     return 0;
  130. }
  131. /*
  132.  * Client:
  133.     cie_apply3(ptlmn, &pcie->common.DecodeLMN, ptlmn);
  134.  */
  135. int
  136. gs_cie_abc_decode3(const gs_vector3 *ptlmn, gs_vector3 *pxyz, const gs_cie_abc *pcie)
  137. {    cie_mult3(ptlmn, &pcie->common.MatrixLMN, pxyz);
  138.     return 0;
  139. }
  140.  
  141. /* Decode an A value to XYZ. */
  142. int
  143. gs_cie_a_decode1(floatp va, float *pta, const gs_cie_a *pcie)
  144. {    *pta = restrict(va, pcie->RangeA);
  145.     return 0;
  146. }
  147. /*
  148.  * Client:
  149.     ta = (*pcie->DecodeA)(*pta);
  150.  */
  151. int
  152. gs_cie_a_decode2(floatp ta, gs_vector3 *ptlmn, const gs_cie_a *pcie)
  153. {    gs_vector3 lmn;
  154.     lmn.u = ta * pcie->MatrixA.u;
  155.     lmn.v = ta * pcie->MatrixA.v;
  156.     lmn.w = ta * pcie->MatrixA.w;
  157.     cie_restrict3(&lmn, &pcie->common.RangeLMN, ptlmn);
  158.     return 0;
  159. }
  160. /*
  161.  * Client:
  162.     cie_apply3(ptlmn, &pcie->common.DecodeLMN, ptlmn);
  163.  */
  164. /* gs_cie_a_decode3 is the same as gs_cie_abc_decode3. */
  165.  
  166. /* Initialize the computed fields of a CIE color rendering structure. */
  167. int
  168. gs_cie_render_init(gs_cie_render *pcie)
  169. {    cie_invert3(&pcie->MatrixPQR, &pcie->MatrixPQR_inverse);
  170.     cie_mult3(&pcie->points.WhitePoint, &pcie->MatrixPQR, &pcie->wdpqr);
  171.     cie_mult3(&pcie->points.BlackPoint, &pcie->MatrixPQR, &pcie->bdpqr);
  172.     return 0;
  173. }
  174.  
  175. /* Render CIE colors */
  176. int
  177. gs_cie_render_colors1(const gs_vector3 *pxyz, gs_cie_wbsd *pwbsd, gs_vector3 *ptpqr, gs_cie_wb *points, const gs_cie_render *pcie)
  178. {    cie_mult3(pxyz, &pcie->MatrixPQR, ptpqr);
  179.     pwbsd->ws.xyz = points->WhitePoint;
  180.     cie_mult3(&pwbsd->ws.xyz, &pcie->MatrixPQR, &pwbsd->ws.pqr);
  181.     pwbsd->bs.xyz = points->BlackPoint;
  182.     cie_mult3(&pwbsd->bs.xyz, &pcie->MatrixPQR, &pwbsd->bs.pqr);
  183.     pwbsd->wd.xyz = pcie->points.WhitePoint;
  184.     pwbsd->wd.pqr = pcie->wdpqr;
  185.     pwbsd->bd.xyz = pcie->points.BlackPoint;
  186.     pwbsd->bd.pqr = pcie->bdpqr;
  187.     return 0;
  188. }
  189. /*
  190.  * Client:
  191.     ptpqr->u = (*pcie->TransformPQR.u)(pwbsd, ptpqr->u);
  192.     ptpqr->v = (*pcie->TransformPQR.v)(pwbsd, ptpqr->v);
  193.     ptpqr->w = (*pcie->TransformPQR.w)(pwbsd, ptpqr->w);
  194.  */
  195. int
  196. gs_cie_render_colors2(const gs_vector3 *ptpqr, gs_vector3 *ptlmn, const gs_cie_render *pcie)
  197. {    gs_vector3 xyzd;
  198.     cie_mult3(ptpqr, &pcie->MatrixPQR_inverse, &xyzd);
  199.     cie_mult3(&xyzd, &pcie->MatrixLMN, ptlmn);
  200.     return 0;
  201. }
  202. /*
  203. /* Client:
  204.     cie_apply3(ptlmn, &pcie->EncodeLMN, ptlmn);
  205.  */
  206. int
  207. gs_cir_render_colors3(const gs_vector3 *ptlmn, gs_vector3 *ptabc, const gs_cie_render *pcie)
  208. {    gs_vector3 lmn;
  209.     cie_restrict3(ptlmn, &pcie->RangeLMN, &lmn);
  210.     cie_mult3(&lmn, &pcie->MatrixABC, ptabc);
  211.     return 0;
  212. }
  213. /*
  214.  * Client:
  215.     cie_apply3(ptabc, &pcie->EncodeABC, ptabc);
  216.  */
  217. int
  218. gs_cie_render_colors4(const gs_vector3 *ptabc, float *colors, const gs_cie_render *pcie)
  219. {    gs_vector3 abc;
  220.     cie_restrict3(ptabc, &pcie->RangeABC, &abc);
  221.     if ( pcie->RenderTable.table == 0 )
  222.        {    /* No further transformation */
  223.         colors[0] = abc.u;
  224.         colors[1] = abc.v;
  225.         colors[2] = abc.w;
  226.        }
  227.     else
  228.        {    /* Use the RenderTable. */
  229.         int m = pcie->RenderTable.m;
  230. #define ri(s,n)\
  231.   (int)((abc.s - pcie->RangeABC.s.rmin) * (pcie->RenderTable.n - 1) /\
  232.     (pcie->RangeABC.s.rmax - pcie->RangeABC.s.rmin) + 0.5)
  233.         int ia = ri(u, NA);
  234.         int ib = ri(v, NB);
  235.         int ic = ri(w, NC);
  236.         int j;
  237.         byte *pdc = pcie->RenderTable.table[ia] +
  238.             m * (ib * pcie->RenderTable.NC + ic);
  239.         for ( j = 0; j < m; j++ )
  240.             colors[j] = (*pcie->RenderTable.T[j])(pdc[j] / 255.0);
  241.        }
  242.     return 0;
  243. }
  244.