home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / ZHT2.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  9KB  |  300 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. /* zht2.c */
  20. /* Level 2 sethalftone operator */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "gsstruct.h"
  25. #include "gxdevice.h"        /* for gzht.h */
  26. #include "gzht.h"
  27. #include "estack.h"
  28. #include "ialloc.h"
  29. #include "idict.h"
  30. #include "idparam.h"
  31. #include "igstate.h"
  32. #include "store.h"
  33.  
  34. /* Imported from zht.c */
  35. extern int zsetscreen_init(P5(os_ptr, gs_screen_halftone *, ref *, int,
  36.                               int (*)(P1(os_ptr))));
  37.  
  38. /* Forward references */
  39. private int dict_spot_params(P4(const ref *, gs_spot_halftone *,
  40.   ref *, ref *));
  41. private int dict_spot_results(P2(ref *, const gs_spot_halftone *));
  42. private int dict_threshold_params(P3(const ref *,
  43.   gs_threshold_halftone *, ref *));
  44.  
  45. /* Structure types */
  46. private_st_halftone_component();
  47. gs_private_st_element(st_halftone_component_element, gs_halftone_component,
  48.   "gs_halftone_component[]", ht_comp_elt_enum_ptrs, ht_comp_elt_reloc_ptrs,
  49.   st_halftone_component);
  50.  
  51. /* GC procedures */
  52.  
  53. #define hptr ((gs_halftone_component *)vptr)
  54.  
  55. private ENUM_PTRS_BEGIN(halftone_component_enum_ptrs) return 0;
  56.     case 0:
  57.         if ( hptr->type != ht_type_threshold )
  58.           return 0;
  59.         *pep = (void *)hptr->params.threshold.thresholds; /* discard const */
  60.         break;
  61. ENUM_PTRS_END
  62.  
  63. private RELOC_PTRS_BEGIN(halftone_component_reloc_ptrs) {
  64.     if ( hptr->type == ht_type_threshold )
  65.       RELOC_PTR(gs_halftone_component, params.threshold.thresholds);
  66. } RELOC_PTRS_END
  67.  
  68. #undef hptr
  69.  
  70. /* <dict> <dict5> .sethalftone5 - */
  71. float spot_dummy(P2(floatp, floatp));        /* in zht.c */
  72. int spot_sample_finish(P1(os_ptr));        /* in zht.c */
  73. private int sethalftone_finish(P1(os_ptr));
  74. private int sethalftone_cleanup(P1(os_ptr));
  75. private int
  76. zsethalftone5(register os_ptr op)
  77. {    uint count;
  78.     gs_halftone_component *phtc;
  79.     gs_halftone_component *pc;
  80.     int code = 0;
  81.     int i;
  82.     gs_halftone *pht;
  83.     gx_device_halftone *pdht;
  84.     static const char _ds *color_names[] =
  85.      { gs_ht_separation_name_strings };
  86.     ref sprocs[countof(color_names)];
  87.     ref tprocs[countof(color_names)];
  88.     check_dict_read(*op);
  89.     count = 0;
  90.     for ( i = 0; i < countof(color_names); i++ )
  91.     {    ref *pvalue;
  92.         if ( dict_find_string(op, color_names[i], &pvalue) > 0 )
  93.             count++;
  94.         else if ( i == gs_ht_separation_Default )
  95.             return_error(e_typecheck);
  96.     }
  97.     check_estack(5);        /* for sampling Type 1 screens */
  98.     refset_null(sprocs, countof(sprocs));
  99.     refset_null(tprocs, countof(tprocs));
  100.     pht = ialloc_struct(gs_halftone, &st_halftone, ".sethalftone5");
  101.     phtc = ialloc_struct_array(count, gs_halftone_component,
  102.                    &st_halftone_component_element,
  103.                    ".sethalftone5");
  104.     pdht = ialloc_struct(gx_device_halftone, &st_device_halftone,
  105.                  ".sethalftone5");
  106.     pc = phtc;
  107.     if ( pht == 0 || phtc == 0 || pdht == 0 )
  108.       code = gs_note_error(e_VMerror);
  109.     else
  110.       for ( i = 0; i < countof(color_names); i++ )
  111.     {    int type;
  112.         ref *pvalue;
  113.         code = dict_find_string(op, color_names[i], &pvalue);
  114.         if ( code > 0 )
  115.         {    check_dict_read(*pvalue);
  116.             if ( dict_int_param(pvalue, "HalftoneType", 1, 5, 0,
  117.                         &type) < 0
  118.                )
  119.               {    code = gs_note_error(e_typecheck);
  120.                 break;
  121.               }
  122.             switch ( type )
  123.             {
  124.             default:
  125.                 code = gs_note_error(e_rangecheck);
  126.                 break;
  127.             case 1:
  128.                 code = dict_spot_params(pvalue,
  129.                     &pc->params.spot, sprocs + i,
  130.                     tprocs + i);
  131.                 pc->params.spot.screen.spot_function =
  132.                     spot_dummy;
  133.                 pc->params.spot.transfer = 0;
  134.                 pc->type = ht_type_spot;
  135.                 break;
  136.             case 3:
  137.                 code = dict_threshold_params(pvalue,
  138.                     &pc->params.threshold, tprocs + i);
  139.                 pc->params.threshold.transfer = 0;
  140.                 pc->type = ht_type_threshold;
  141.                 break;
  142.             }
  143.             if ( code < 0 )
  144.                 break;
  145.             pc->cname = i;
  146.             pc++;
  147.         }
  148.     }
  149.     if ( code >= 0 )
  150.       {    pht->type = ht_type_multiple;
  151.         pht->params.multiple.components = phtc;
  152.         pht->params.multiple.num_comp = count;
  153.         code = gs_sethalftone_prepare(igs, pht, pdht);
  154.       }
  155.     if ( code >= 0 )
  156.       for ( i = 0, pc = phtc; i < count; i++, pc++ )
  157.     {    if ( pc->type == ht_type_spot )
  158.         {    ref *pvalue;
  159.             dict_find_string(op, color_names[pc->cname], &pvalue);
  160.             code = dict_spot_results(pvalue, &pc->params.spot);
  161.             if ( code < 0 )
  162.                 break;
  163.         }
  164.     }
  165.     if ( code >= 0 )
  166.       {    /* Schedule the sampling of any Type 1 screens. */
  167.         es_ptr esp0 = esp;        /* for backing out */
  168.         esp += 5;
  169.         make_mark_estack(esp - 4, es_other, sethalftone_cleanup);
  170.         esp[-3] = op[-1];        /* halftone dict */
  171.         make_istruct(esp - 2, 0, pht);
  172.         make_istruct(esp - 1, 0, pdht);
  173.         make_op_estack(esp, sethalftone_finish);
  174.         for ( i = 0; i < count; i++ )
  175.           if ( phtc[i].type == ht_type_spot )
  176.         {    code = zsetscreen_init(op,
  177.                 &phtc[i].params.spot.screen,
  178.                 &sprocs[i], 0, spot_sample_finish);
  179.             if ( code < 0 )
  180.             {    esp = esp0;
  181.                 break;
  182.             }
  183.             /* Save the order index in the enumeration */
  184.             /* structure, which is 1 below esp. */
  185.             {    gs_screen_enum *penum =
  186.                   r_ptr(esp - 1, gs_screen_enum);
  187.                 penum->dev_ht = pdht;
  188.                 penum->comp_index = i;
  189.             }
  190.         }
  191.       }
  192.     if ( code < 0 )
  193.       {    ifree_object(pdht, ".sethalftone5");
  194.         ifree_object(phtc, ".sethalftone5");
  195.         ifree_object(pht, ".sethalftone5");
  196.         return code;
  197.       }
  198.     pop(2);
  199.     return o_push_estack;
  200. }
  201. /* Install the halftone after sampling. */
  202. private int
  203. sethalftone_finish(os_ptr op)
  204. {    gx_device_halftone *pdht = r_ptr(esp, gx_device_halftone);
  205.     int code;
  206.     if ( pdht->components )
  207.         pdht->order = pdht->components[0].corder;
  208.     code = gx_ht_install(igs, r_ptr(esp - 1, gs_halftone), pdht);
  209.     if ( code < 0 )
  210.         return code;
  211.     istate->halftone = esp[-2];
  212.     esp -= 4;
  213.     sethalftone_cleanup(op);
  214.     return o_pop_estack;
  215. }
  216. /* Clean up after installing the halftone. */
  217. private int
  218. sethalftone_cleanup(os_ptr op)
  219. {    ifree_object(esp[4].value.pstruct,
  220.              "sethalftone_cleanup(device halftone)");
  221.     ifree_object(esp[3].value.pstruct,
  222.              "sethalftone_cleanup(halftone)");
  223.     return 0;
  224. }
  225.  
  226. /* ------ Initialization procedure ------ */
  227.  
  228. op_def zht2_l2_op_defs[] = {
  229.         op_def_begin_level2(),
  230.     {"2.sethalftone5", zsethalftone5},
  231.         /* Internal operators */
  232.     {"0%sethalftone_finish", sethalftone_finish},
  233.     op_def_end(0)
  234. };
  235.  
  236. /* ------ Internal routines ------ */
  237.  
  238. /* Extract frequency, angle, and spot function from a dictionary. */
  239. private int
  240. dict_spot_params(const ref *pdict, gs_spot_halftone *psp,
  241.   ref *psproc, ref *ptproc)
  242. {    int code;
  243.     check_dict_read(*pdict);
  244.     if ( (code = dict_float_param(pdict, "Frequency", 0.0,
  245.                       &psp->screen.frequency)) < 0 ||
  246.          (code = dict_float_param(pdict, "Angle", 0.0,
  247.                       &psp->screen.angle)) < 0 ||
  248.          (code = dict_proc_param(pdict, "SpotFunction", psproc)) != 0 ||
  249.          (code = dict_proc_param(pdict, "TransferFunction", ptproc)) < 0
  250.        )
  251.         return (code < 0 ? code : e_undefined);
  252.     return 0;
  253. }
  254.  
  255. /* Set actual frequency and angle in a dictionary. */
  256. private int
  257. dict_real_result(ref *pdict, const char _ds *kstr, floatp val)
  258. {    int code = 0;
  259.     ref *discard;
  260.     if ( dict_find_string(pdict, kstr, &discard) > 0 )
  261.     {    ref rval;
  262.         check_dict_write(*pdict);
  263.         make_real(&rval, val);
  264.         code = dict_put_string(pdict, kstr, &rval);
  265.     }
  266.     return code;
  267. }
  268. private int
  269. dict_spot_results(ref *pdict, const gs_spot_halftone *psp)
  270. {    int code;
  271.     code = dict_real_result(pdict, "ActualFrequency",
  272.                 psp->screen.actual_frequency);
  273.     if ( code < 0 )
  274.         return code;
  275.     return dict_real_result(pdict, "ActualAngle",
  276.                 psp->screen.actual_angle);
  277. }
  278.  
  279. /* Extract width, height, and thresholds from a dictionary. */
  280. private int
  281. dict_threshold_params(const ref *pdict, gs_threshold_halftone *ptp,
  282.   ref *ptproc)
  283. {    int code;
  284.     ref *tstring;
  285.     check_dict_read(*pdict);
  286.     if ( (code = dict_int_param(pdict, "Width", 1, 0x7fff, -1,
  287.                     &ptp->width)) < 0 ||
  288.          (code = dict_int_param(pdict, "Height", 1, 0x7fff, -1,
  289.                     &ptp->height)) < 0 ||
  290.          (code = dict_find_string(pdict, "Thresholds", &tstring)) <= 0 ||
  291.          (code = dict_proc_param(pdict, "TransferFunction", ptproc)) < 0
  292.        )
  293.         return (code < 0 ? code : e_undefined);
  294.     check_read_type(*tstring, t_string);
  295.     if ( r_size(tstring) != (long)ptp->width * ptp->height )
  296.         return_error(e_rangecheck);
  297.     ptp->thresholds = tstring->value.const_bytes;
  298.     return 0;
  299. }
  300.