home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / ZCOLOR2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-31  |  9.4 KB  |  306 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. /* zcolor2.c */
  20. /* Level 2 color operators */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "gscolor.h"
  25. #include "gscspace.h"
  26. #include "gsmatrix.h"
  27. #include "gsstruct.h"
  28. #include "gxfixed.h"        /* for gxcolor2.h */
  29. #include "gxcolor2.h"
  30. #include "gxdevice.h"
  31. #include "gxdevmem.h"        /* for gxpcolor.h */
  32. #include "gxpcolor.h"
  33. #include "estack.h"
  34. #include "ialloc.h"
  35. #include "istruct.h"
  36. #include "idict.h"
  37. #include "idparam.h"
  38. #include "igstate.h"
  39. #include "store.h"
  40.  
  41. /* Forward references */
  42. private int store_color_params(P3(os_ptr, const gs_paint_color *, const gs_color_space *));
  43. private int load_color_params(P3(os_ptr, gs_paint_color *, const gs_color_space *));
  44. private int zPaintProc(P2(const gs_client_color *, gs_state *));
  45. private int pattern_paint_prepare(P1(os_ptr));
  46. private int pattern_paint_finish(P1(os_ptr));
  47.  
  48. /*
  49.  * Define the structure for remembering the pattern dictionary.
  50.  * This is the "client data" in the template.
  51.  * See zgstate.c (int_gstate) or zfont2.c (font_data) for information
  52.  * as to why we define this as a structure rather than a ref array.
  53.  */
  54. typedef struct int_pattern_s {
  55.     ref dict;
  56. } int_pattern;
  57. gs_private_st_ref_struct(st_int_pattern, int_pattern, "int_pattern");
  58.  
  59. /* Initialize the Pattern cache. */
  60. private void
  61. zcolor2_init(void)
  62. {    gstate_set_pattern_cache(igs,
  63.                  gx_pattern_alloc_cache(imemory_system,
  64.                      pattern_cache_default_max_tiles,
  65.                      pattern_cache_default_max_bits));
  66. }
  67.  
  68. /* <pattern> <matrix> .buildpattern <pattern> <instance> */
  69. int
  70. zbuildpattern(os_ptr op)
  71. {    os_ptr op1 = op - 1;
  72.     int code;
  73.     gs_matrix mat;
  74.     int PatternType;
  75.     float BBox[4];
  76.     gs_client_pattern template;
  77.     int_pattern *pdata;
  78.     gs_client_color cc_instance;
  79.     ref *pPaintProc;
  80.     check_type(*op1, t_dictionary);
  81.     check_dict_read(*op1);
  82.     if ( (code = read_matrix(op, &mat)) < 0 ||
  83.          (code = dict_uid_param(op1, &template.uid, 1, imemory)) != 1 ||
  84.          (code = dict_int_param(op1, "PatternType", 1, 1, 0, &PatternType)) < 0 ||
  85.          (code = dict_int_param(op1, "PaintType", 1, 2, 0, &template.PaintType)) < 0 ||
  86.          (code = dict_int_param(op1, "TilingType", 1, 3, 0, &template.TilingType)) < 0 ||
  87.          (code = dict_float_array_param(op1, "BBox", 4, BBox, NULL)) != 4 ||
  88.          (code = dict_float_param(op1, "XStep", 0.0, &template.XStep)) != 0 ||
  89.          (code = dict_float_param(op1, "YStep", 0.0, &template.YStep)) != 0 ||
  90.          (code = dict_find_string(op1, "PaintProc", &pPaintProc)) <= 0
  91.        )
  92.         return_error((code < 0 ? code : e_rangecheck));
  93.     check_proc(*pPaintProc);
  94.     template.BBox.p.x = BBox[0];
  95.     template.BBox.p.y = BBox[1];
  96.     template.BBox.q.x = BBox[2];
  97.     template.BBox.q.y = BBox[3];
  98.     template.PaintProc = zPaintProc;
  99.     pdata = ialloc_struct(int_pattern, &st_int_pattern, "int_pattern");
  100.     if ( pdata == 0 )
  101.         return_error(e_VMerror);
  102.     template.client_data = pdata;
  103.     pdata->dict = *op1;
  104.     code = gs_makepattern(&cc_instance, &template, &mat, igs);
  105.     if ( code < 0 )
  106.       {    ifree_object(pdata, "int_pattern");
  107.         return code;
  108.       }
  109.     make_istruct(op, a_readonly, cc_instance.pattern);
  110.     return code;
  111. }
  112.  
  113. /* - currentcolor <param1> ... <paramN> */
  114. int
  115. zcurrentcolor(register os_ptr op)
  116. {    const gs_client_color *pc = gs_currentcolor(igs);
  117.     const gs_color_space *pcs = gs_currentcolorspace(igs);
  118.     int n;
  119.     check_ostack(5);        /* Worst case: CMYK + pattern */
  120.     if ( pcs->type->index == gs_color_space_index_Pattern )
  121.     {    n = 1;
  122.         if ( pc->pattern->template.PaintType == 2 )  /* uncolored */
  123.           n += store_color_params(op, &pc->paint,
  124.             (const gs_color_space *)&pcs->params.pattern.base_space);
  125.         op[n] = istate->pattern;
  126.     }
  127.     else
  128.         n = store_color_params(op, &pc->paint, pcs);
  129.     push(n);
  130.     return 0;
  131. }
  132.  
  133. /* - currentoverprint <bool> */
  134. int
  135. zcurrentoverprint(register os_ptr op)
  136. {    push(1);
  137.     make_bool(op, gs_currentoverprint(igs));
  138.     return 0;
  139. }
  140.  
  141. /* <param1> ... <paramN> setcolor - */
  142. int
  143. zsetcolor(register os_ptr op)
  144. {    gs_client_color c;
  145.     const gs_color_space *pcs = gs_currentcolorspace(igs);
  146.     int n, code;
  147.     gs_pattern_instance *pinst = 0;
  148.     if ( pcs->type->index == gs_color_space_index_Pattern )
  149.     {    /* Make sure *op is a real Pattern. */
  150.         ref *pImpl;
  151.         check_type(*op, t_dictionary);
  152.         check_dict_read(*op);
  153.         if ( dict_find_string(op, "Implementation", &pImpl) <= 0 ||
  154.              !r_has_stype(pImpl, imemory, st_pattern_instance)
  155.            )
  156.             return_error(e_rangecheck);
  157.         pinst = r_ptr(pImpl, gs_pattern_instance);
  158.         c.pattern = pinst;
  159.         if ( pinst->template.PaintType == 2 )    /* uncolored */
  160.         {    if ( !pcs->params.pattern.has_base_space )
  161.                 return_error(e_rangecheck);
  162.             n = load_color_params(op - 1, &c.paint,
  163.                 (const gs_color_space *)&pcs->params.pattern.base_space);
  164.             if ( n < 0 ) return n;
  165.             n++;
  166.         }
  167.         else
  168.             n = 1;
  169.     }
  170.     else
  171.     {    n = load_color_params(op, &c.paint, pcs);
  172.         c.pattern = 0;            /* for GC */
  173.     }
  174.     if ( n < 0 )
  175.         return n;
  176.     code = gs_setcolor(igs, &c);
  177.     if ( code < 0 )
  178.         return code;
  179.     if ( pinst != 0 )
  180.         istate->pattern = *op;
  181.     pop(n);
  182.     return code;
  183. }
  184.  
  185. /* <bool> setoverprint - */
  186. int
  187. zsetoverprint(register os_ptr op)
  188. {    check_type(*op, t_boolean);
  189.     gs_setoverprint(igs, op->value.boolval);
  190.     pop(1);
  191.     return 0;
  192. }
  193.  
  194. /* ------ Initialization procedure ------ */
  195.  
  196. op_def zcolor2_l2_op_defs[] = {
  197.         op_def_begin_level2(),
  198.     {"2.buildpattern", zbuildpattern},
  199.     {"0currentcolor", zcurrentcolor},
  200.     {"0currentoverprint", zcurrentoverprint},
  201.     {"1setcolor", zsetcolor},
  202.     {"1setoverprint", zsetoverprint},
  203.         /* Internal operators */
  204.     {"0%pattern_paint_prepare", pattern_paint_prepare},
  205.     {"0%pattern_paint_finish", pattern_paint_finish},
  206.     op_def_end(zcolor2_init)
  207. };
  208.  
  209. /* ------ Internal procedures ------ */
  210.  
  211. /* Store non-pattern color values on the operand stack. */
  212. /* Return the number of values stored. */
  213. private int
  214. store_color_params(os_ptr op, const gs_paint_color *pc,
  215.   const gs_color_space *pcs)
  216. {    int n = pcs->type->num_components;
  217.     make_reals(op + 1, pc->values, n);
  218.     return n;
  219. }
  220.  
  221. /* Load non-pattern color values from the operand stack. */
  222. /* Return the number of values stored. */
  223. private int
  224. load_color_params(os_ptr op, gs_paint_color *pc,
  225.   const gs_color_space *pcs)
  226. {    int n = pcs->type->num_components;
  227.     int code = num_params(op, n, pc->values);
  228.     if ( code < 0 ) return code;
  229.     return n;
  230. }
  231.  
  232. /* Render the pattern by calling the PaintProc. */
  233. private int pattern_paint_cleanup(P1(os_ptr));
  234. private int
  235. zPaintProc(const gs_client_color *pcc, gs_state *pgs)
  236. {    /* Just schedule a call on the real PaintProc. */
  237.     check_estack(2);
  238.     esp++;
  239.     push_op_estack(pattern_paint_prepare);
  240.     return e_InsertProc;
  241. }
  242. /* Prepare to run the PaintProc. */
  243. private int
  244. pattern_paint_prepare(os_ptr op)
  245. {    gs_state *pgs = igs;
  246.     gs_pattern_instance *pinst = gs_currentcolor(pgs)->pattern;
  247.     ref *pdict = &((int_pattern *)pinst->template.client_data)->dict;
  248.     gx_device_pattern_accum *pdev;
  249.     int code;
  250.     ref *ppp;
  251.     check_estack(5);
  252.     pdev = gx_pattern_accum_alloc(imemory, "pattern_paint_prepare");
  253.     if ( pdev == 0 )
  254.         return_error(e_VMerror);
  255.     pdev->instance = pinst;
  256.     pdev->bitmap_memory = gstate_pattern_cache(pgs)->memory;
  257.     code = (*dev_proc(pdev, open_device))((gx_device *)pdev);
  258.     if ( code < 0 )
  259.       { ifree_object(pdev, "pattern_paint_prepare");
  260.         return code;
  261.       }
  262.     code = gs_gsave(pgs);
  263.     if ( code < 0 )
  264.         return code;
  265.     code = gs_setgstate(pgs, pinst->saved);
  266.     if ( code < 0 )
  267.     {    gs_grestore(pgs);
  268.         return code;
  269.     }
  270.     gx_set_device_only(pgs, (gx_device *)pdev);
  271.     push_mark_estack(es_other, pattern_paint_cleanup);
  272.     ++esp;
  273.     make_istruct(esp, 0, pdev);
  274.     push_op_estack(pattern_paint_finish);
  275.     dict_find_string(pdict, "PaintProc", &ppp); /* can't fail */
  276.     *++esp = *ppp;
  277.     *++esp = *pdict;    /* (push on ostack) */
  278.     return o_push_estack;
  279. }
  280. /* Save the rendered pattern. */
  281. private int
  282. pattern_paint_finish(os_ptr op)
  283. {    gx_device_pattern_accum *pdev = r_ptr(esp, gx_device_pattern_accum);
  284.     gx_color_tile *ctile;
  285.     int code = gx_pattern_cache_add_entry(igs, pdev, &ctile);
  286.     if ( code < 0 )
  287.       return code;
  288.     if ( ctile->bits.data != 0 )
  289.       pdev->bits->bitmap_memory = 0;    /* don't free the bits */
  290.     if ( ctile->mask.data != 0 )
  291.       pdev->mask->bitmap_memory = 0;    /* ditto */
  292.     esp -= 2;
  293.     pattern_paint_cleanup(op);
  294.     return o_pop_estack;
  295. }
  296. /* Clean up after rendering a pattern.  Note that iff the rendering */
  297. /* succeeded, closing the accumulator won't free the bits. */
  298. private int
  299. pattern_paint_cleanup(os_ptr op)
  300. {    gx_device_pattern_accum *pdev = r_ptr(esp + 2, gx_device_pattern_accum);
  301.     gs_grestore(igs);
  302.     (*dev_proc(pdev, close_device))((gx_device *)pdev);
  303.     ifree_object(pdev, "pattern_paint_cleanup");
  304.     return 0;
  305. }
  306.