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

  1. /* Copyright (C) 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. /* zcsindex.c */
  20. /* Level 2 Indexed and Separation color space support */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "gsstruct.h"
  25. #include "gscolor.h"
  26. #include "gscspace.h"
  27. #include "gxfixed.h"        /* for gxcolor2.h */
  28. #include "gxcolor2.h"
  29. #include "estack.h"
  30. #include "ialloc.h"
  31. #include "igstate.h"
  32. #include "store.h"
  33.  
  34. /* Imported from gscolor2.c */
  35. extern const gs_color_space_type
  36.     gs_color_space_type_Indexed,
  37.     gs_color_space_type_Separation;
  38.  
  39. /* Forward references */
  40. private int begin_map(P5(gs_indexed_map **, const ref *, int,
  41.   const gs_base_color_space *, int (*)(P1(os_ptr))));
  42. private int indexed_map1(P1(os_ptr));
  43. private int separation_map1(P1(os_ptr));
  44.  
  45. /* Allocator type for indexed map */
  46. private_st_indexed_map();
  47.  
  48. /* Free the map when freeing the gs_indexed_map structure. */
  49. private void
  50. rc_free_indexed_map(gs_memory_t *mem, char *data, client_name_t cname)
  51. {    gs_free_object(mem, ((gs_indexed_map *)data)->values, cname);
  52.     gs_free_object(mem, data, cname);
  53. }
  54.  
  55. /* ------ Indexed color space ------ */
  56.  
  57. /* Indexed lookup procedure that just consults the cache. */
  58. private int
  59. lookup_indexed(const gs_indexed_params *params, int index, float *values)
  60. {    int m = params->base_space.type->num_components;
  61.     const float *pv = ¶ms->lookup.map->values[index * m];
  62.     switch ( m )
  63.     {
  64.     default: return_error(e_rangecheck);
  65.     case 4: values[3] = pv[3];
  66.     case 3: values[2] = pv[2];
  67.         values[1] = pv[1];
  68.     case 1: values[0] = pv[0];
  69.     }
  70.     return 0;
  71. }
  72.  
  73. /* Get the parameters for an Indexed color space. */
  74. /* base_space has been set; hival has been checked, but not set. */
  75. int
  76. zcolorspace_Indexed(const ref *pcsa, gs_color_space *pcs, ref *pproc)
  77. {    int num_entries =
  78.       (pcs->params.indexed.hival = pcsa[1].value.intval) + 1;
  79.     pcs->type = &gs_color_space_type_Indexed;
  80.     if ( r_has_type(&pcsa[2], t_string) )
  81.     {    int num_values = num_entries *
  82.           pcs->params.indexed.base_space.type->num_components;
  83.         check_read(pcsa[2]);
  84.         if ( r_size(&pcsa[2]) != num_values )
  85.             return_error(e_rangecheck);
  86.         pcs->params.indexed.lookup.table.data =
  87.             pcsa[2].value.const_bytes;
  88.         pcs->params.indexed.use_proc = 0;
  89.         make_null(pproc);
  90.         return 0;
  91.     }
  92.     else
  93.     {    gs_indexed_map *map;
  94.         int code;
  95.         check_proc(pcsa[2]);
  96.         code = begin_map(&map, &pcsa[2], num_entries,
  97.                  &pcs->params.indexed.base_space,
  98.                  indexed_map1);
  99.         if ( code < 0 )
  100.           return code;
  101.         pcs->params.indexed.use_proc = 1;
  102.         *pproc = pcsa[2];
  103.         map->proc.lookup_index = lookup_indexed;
  104.         pcs->params.indexed.lookup.map = map;
  105.         return code;
  106.     }
  107. }
  108.  
  109. /* Continuation procedure for saving mapped Indexed color values. */
  110. private int
  111. indexed_map1(os_ptr op)
  112. {        /* estack: 0 = proc, -1 = index, -2 = pcs. */
  113.     es_ptr ep = esp;
  114.     int i = (int)esp[-1].value.intval;
  115.     const gs_color_space *pcs = r_ptr(esp - 2, const gs_color_space);
  116.     if ( i < 0 )        /* first time */
  117.     {    pcs = gs_currentcolorspace(igs);
  118.         make_struct(ep - 2, a_foreign, pcs);
  119.     }
  120.     else
  121.     {    int m = pcs->params.indexed.base_space.type->num_components;
  122.         int code = num_params(op, m, &pcs->params.indexed.lookup.map->values[i * m]);
  123.         if ( code < 0 )
  124.             return code;
  125.         pop(m);  op -= m;
  126.         if ( i == pcs->params.indexed.hival )
  127.         {    /* All done. */
  128.             esp -= 3;
  129.             gs_setcolorspace(igs, (gs_color_space *)pcs);
  130.             return o_pop_estack;
  131.         }
  132.     }
  133.     push(1);
  134.     ep[-1].value.intval = ++i;
  135.     make_int(op, i);
  136.     make_op_estack(ep + 1, indexed_map1);
  137.     ep[2] = *ep;        /* lookup proc */
  138.     esp = ep + 2;
  139.     return o_push_estack;
  140. }
  141.  
  142. /* ------ Separation color space ------ */
  143.  
  144. /* Define the separation cache size.  This makes many useful tint values */
  145. /* map to integer cache indices. */
  146. #define separation_cache_size 360
  147.  
  148. /* Tint transform procedure that just consults the cache. */
  149. private int
  150. lookup_tint(const gs_separation_params *params, floatp tint, float *values)
  151. {    int m = params->alt_space.type->num_components;
  152.     const gs_indexed_map *map = params->map;
  153.     int value_index =
  154.       (tint < 0 ? 0 : tint > 1 ? map->num_values - m :
  155.        (int)(tint * separation_cache_size + 0.5) * m);
  156.     const float *pv = &map->values[value_index];
  157.     switch ( m )
  158.     {
  159.     default: return_error(e_rangecheck);
  160.     case 4: values[3] = pv[3];
  161.     case 3: values[2] = pv[2];
  162.         values[1] = pv[1];
  163.     case 1: values[0] = pv[0];
  164.     }
  165.     return 0;
  166. }
  167.  
  168. /* Get the parameters for a Separation color space. */
  169. /* alt_space has been set; name and tint_transform have been checked, */
  170. /* but not set. */
  171. int
  172. zcolorspace_Separation(const ref *pcsa, gs_color_space *pcs,
  173.   ref_separation_params *params)
  174. {    gs_indexed_map *map;
  175.     int code;
  176.     code = begin_map(&map, &pcsa[2], separation_cache_size,
  177.              &pcs->params.separation.alt_space,
  178.              separation_map1);
  179.     if ( code < 0 )
  180.       return code;
  181.     map->proc.tint_transform = lookup_tint;
  182.     pcs->params.separation.map = map;
  183.     params->layer_name = pcsa[0];
  184.     params->tint_transform = pcsa[2];
  185.     pcs->type = &gs_color_space_type_Separation;
  186.     return code;
  187. }
  188.  
  189. /* Continuation procedure for saving transformed tint values. */
  190. private int
  191. separation_map1(os_ptr op)
  192. {        /* estack: 0 = proc, -1 = index, -2 = pcs. */
  193.     es_ptr ep = esp;
  194.     int i = (int)esp[-1].value.intval;
  195.     const gs_color_space *pcs = r_ptr(esp - 2, const gs_color_space);
  196.     if ( i < 0 )        /* first time */
  197.     {    pcs = gs_currentcolorspace(igs);
  198.         make_struct(ep - 2, a_foreign, pcs);
  199.     }
  200.     else
  201.     {    int m = pcs->params.separation.alt_space.type->num_components;
  202.         int code = num_params(op, m, &pcs->params.separation.map->values[i * m]);
  203.         if ( code < 0 )
  204.             return code;
  205.         pop(m);  op -= m;
  206.         if ( i == separation_cache_size )
  207.         {    /* All done. */
  208.             esp -= 3;
  209.             gs_setcolorspace(igs, (gs_color_space *)pcs);
  210.             return o_pop_estack;
  211.         }
  212.     }
  213.     push(1);
  214.     ep[-1].value.intval = ++i;
  215.     make_real(op, i / (float)separation_cache_size);
  216.     make_op_estack(ep + 1, separation_map1);
  217.     ep[2] = *ep;        /* tint_transform */
  218.     esp = ep + 2;
  219.     return o_push_estack;
  220. }
  221.  
  222. /* ------ Initialization procedure ------ */
  223.  
  224. op_def zcsindex_l2_op_defs[] = {
  225.         op_def_begin_level2(),
  226.         /* Internal operators */
  227.     {"1%indexed_map1", indexed_map1},
  228.     {"1%separation_map1", separation_map1},
  229.     op_def_end(0)
  230. };
  231.  
  232. /* ------ Internal routines ------ */
  233.  
  234. /* Allocate, and prepare to load, the index or tint map. */
  235. private int
  236. begin_map(gs_indexed_map **pmap, const ref *pproc, int num_entries,
  237.   const gs_base_color_space *base_space, int (*map1)(P1(os_ptr)))
  238. {    gs_memory_t *mem = gs_state_memory(igs);
  239.     int num_values = num_entries * base_space->type->num_components;
  240.     gs_indexed_map *map;
  241.     es_ptr ep;
  242.     float *values;
  243.     rc_alloc_struct_0(map, gs_indexed_map, &st_indexed_map,
  244.               mem, return_error(e_VMerror),
  245.               "setcolorspace(mapped)");
  246.     values =
  247.       (float *)gs_alloc_byte_array(mem, num_values, sizeof(float),
  248.                        "setcolorspace(mapped)");
  249.     if ( values == 0 )
  250.     {    gs_free_object(mem, map, "setcolorspace(mapped)");
  251.         return_error(e_VMerror);
  252.     }
  253.     map->rc.free = rc_free_indexed_map;
  254.     map->num_values = num_values;
  255.     map->values = values;
  256.     *pmap = map;
  257.     /* Map the entire set of color indices.  Since the */
  258.     /* o-stack may not be able to hold 4*4096 values, we have */
  259.     /* to load them into the cache as they are generated. */
  260.     check_estack(5);    /* 1 extra for proc */
  261.     ep = esp;
  262.     make_string(ep + 1, 0, 0, NULL);    /* pcs later */
  263.     make_int(ep + 2, -1);
  264.     ep[3] = *pproc;
  265.     make_op_estack(ep + 4, map1);
  266.     esp += 4;
  267.     return o_push_estack;
  268. }
  269.