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

  1. /* Copyright (C) 1989, 1991, 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. /* zht.c */
  20. /* Halftone definition operators */
  21. #include "ghost.h"
  22. #include "memory_.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "estack.h"
  26. #include "gsstruct.h"            /* must precede igstate.h, */
  27.                     /* because of #ifdef in gsht.h */
  28. #include "ialloc.h"
  29. #include "igstate.h"
  30. #include "gsmatrix.h"
  31. #include "gxdevice.h"            /* for gzht.h */
  32. #include "gzht.h"
  33. #include "gsstate.h"
  34. #include "store.h"
  35.  
  36. /* Forward references */
  37. private int screen_params(P2(os_ptr, gs_screen_halftone *));
  38. private int screen_sample(P1(os_ptr));
  39. private int set_screen_continue(P1(os_ptr));
  40. private int screen_cleanup(P1(os_ptr));
  41.  
  42. /* Dummy spot function */
  43. float
  44. spot_dummy(floatp x, floatp y)
  45. {    return (x + y) / 2;
  46. }
  47.  
  48. /* - .currenthalftone <dict> 0 */
  49. /* - .currenthalftone <frequency> <angle> <proc> 1 */
  50. /* - .currenthalftone <red_freq> ... <gray_proc> 2 */
  51. private int
  52. zcurrenthalftone(register os_ptr op)
  53. {    gs_halftone ht;
  54.     gs_currenthalftone(igs, &ht);
  55.     switch ( ht.type )
  56.     {
  57.     case ht_type_screen:
  58.         push(4);
  59.         make_real(op - 3, ht.params.screen.frequency);
  60.         make_real(op - 2, ht.params.screen.angle);
  61.         op[-1] = istate->screen_procs.colored.gray;
  62.         make_int(op, 1);
  63.         break;
  64.     case ht_type_colorscreen:
  65.         push(13);
  66.         {    int i;
  67.             for ( i = 0; i < 4; i++ )
  68.             {    os_ptr opc = op - 12 + i * 3;
  69.                 gs_screen_halftone *pht =
  70.                   &ht.params.colorscreen.screens.indexed[i];
  71.                 make_real(opc, pht->frequency);
  72.                 make_real(opc + 1, pht->angle);
  73.                 opc[2] = istate->screen_procs.indexed[i];
  74.             }
  75.         }
  76.         make_int(op, 2);
  77.         break;
  78.     default:        /* Screen was set by sethalftone. */
  79.         push(2);
  80.         op[-1] = istate->halftone;
  81.         make_int(op, 0);
  82.         break;
  83.     }
  84.     return 0;
  85. }
  86.  
  87. /* The setscreen operator is complex because it has to sample */
  88. /* each pixel in the pattern cell, calling a procedure, and then */
  89. /* sort the result into a whitening order. */
  90.  
  91. /* Layout of stuff pushed on estack: */
  92. /*    Control mark, */
  93. /*    [other stuff for other screen-setting operators], */
  94. /*    finishing procedure, */
  95. /*    spot procedure, */
  96. /*    enumeration structure (as bytes). */
  97. #define snumpush 4
  98. #define sproc esp[-1]
  99. #define senum r_ptr(esp, gs_screen_enum)
  100.  
  101. /* Forward references */
  102. int zsetscreen_init(P5(os_ptr, gs_screen_halftone *, ref *, int,
  103.   int (*)(P1(os_ptr))));
  104. private int setscreen_finish(P1(os_ptr));
  105.  
  106. /* <frequency> <angle> <proc> setscreen - */
  107. int
  108. zsetscreen(register os_ptr op)
  109. {    gs_screen_halftone screen;
  110.     int code = screen_params(op, &screen);
  111.     if ( code < 0 )
  112.         return code;
  113.     return zsetscreen_init(op, &screen, op, 3, setscreen_finish);
  114. }
  115. /* We break out the body of this operator so it can be shared with */
  116. /* the code for Type 1 halftones in sethalftone. */
  117. int
  118. zsetscreen_init(os_ptr op, gs_screen_halftone *psp, ref *pproc,
  119.   int npop, int (*finish_proc)(P1(os_ptr)))
  120. {    gs_screen_enum *penum;
  121.     int code;
  122.     check_estack(snumpush + 1);
  123.     penum = gs_screen_enum_alloc(imemory, "setscreen");
  124.     if ( penum == 0 )
  125.         return_error(e_VMerror);
  126.     make_istruct(esp + snumpush, 0, penum);    /* do early for screen_cleanup in case of error */
  127.     code = gs_screen_init(penum, igs, psp);
  128.     if ( code < 0 )
  129.     {    screen_cleanup(op);
  130.         return code;
  131.     }
  132.     /* Push everything on the estack */
  133.     make_mark_estack(esp + 1, es_other, screen_cleanup);
  134.     esp += snumpush;
  135.     make_op_estack(esp - 2, finish_proc);
  136.     sproc = *pproc;
  137.     push_op_estack(screen_sample);
  138.     pop(npop);
  139.     return o_push_estack;
  140. }
  141. /* Set up the next sample */
  142. private int
  143. screen_sample(register os_ptr op)
  144. {    gs_screen_enum *penum = senum;
  145.     gs_point pt;
  146.     int code = gs_screen_currentpoint(penum, &pt);
  147.     ref proc;
  148.     switch ( code )
  149.     {
  150.     default:
  151.         return code;
  152.     case 1:
  153.         /* All done */
  154.         code = (*real_opproc(esp - 2))(op);
  155.         esp -= snumpush;
  156.         screen_cleanup(op);
  157.         return (code < 0 ? code : o_pop_estack);
  158.     case 0:
  159.         ;
  160.     }
  161.     push(2);
  162.     make_real(op - 1, pt.x);
  163.     make_real(op, pt.y);
  164.     proc = sproc;
  165.     push_op_estack(set_screen_continue);
  166.     *++esp = proc;
  167.     return o_push_estack;
  168. }
  169. /* Continuation procedure for processing sampled pixels. */
  170. private int
  171. set_screen_continue(register os_ptr op)
  172. {    float value;
  173.     int code = num_params(op, 1, &value);
  174.     if ( code < 0 ) return code;
  175.     code = gs_screen_next(senum, value);
  176.     if ( code < 0 ) return code;
  177.     pop(1);  op--;
  178.     return screen_sample(op);
  179. }
  180. /* Finish setscreen. */
  181. private int
  182. setscreen_finish(os_ptr op)
  183. {    gs_screen_install(senum);
  184.     istate->screen_procs.colored.red = sproc;
  185.     istate->screen_procs.colored.green = sproc;
  186.     istate->screen_procs.colored.blue = sproc;
  187.     istate->screen_procs.colored.gray = sproc;
  188.     make_null(&istate->halftone);
  189.     return 0;
  190. }
  191. /* Clean up after screen enumeration */
  192. private int
  193. screen_cleanup(os_ptr op)
  194. {    ifree_object(esp[snumpush].value.pstruct, "screen_cleanup");
  195.     return 0;
  196. }
  197.  
  198. /* <red_freq> ... <gray_proc> setcolorscreen - */
  199. int spot_sample_finish(P1(os_ptr));        /* forward reference */
  200. private int setcolorscreen_finish(P1(os_ptr));
  201. private int setcolorscreen_cleanup(P1(os_ptr));
  202. /* The operator entry for setcolorscreen is in zcolor1.c, not here, */
  203. /* so we have to export the procedure. */
  204. int
  205. zsetcolorscreen(register os_ptr op)
  206. {    gs_colorscreen_halftone cscreen;
  207.     ref sprocs[4];
  208.     gs_halftone *pht;
  209.     gx_device_halftone *pdht;
  210.     int i;
  211.     int code = 0;
  212.     for ( i = 0; i < 4; i++ )
  213.     {    os_ptr op1 = op - 9 + i * 3;
  214.         int code = screen_params(op1, &cscreen.screens.indexed[i]);
  215.         if ( code < 0 )
  216.             return code;
  217.         cscreen.screens.indexed[i].spot_function = spot_dummy;
  218.         sprocs[i] = *op1;
  219.     }
  220.     check_estack(8);        /* for sampling screens */
  221.     pht = ialloc_struct(gs_halftone, &st_halftone, "setcolorscreen");
  222.     pdht = ialloc_struct(gx_device_halftone, &st_device_halftone,
  223.                  "setcolorscreen");
  224.     if ( pht == 0 || pdht == 0 )
  225.         code = gs_note_error(e_VMerror);
  226.     else
  227.       {    pht->type = ht_type_colorscreen;
  228.         pht->params.colorscreen = cscreen;
  229.         code = gs_sethalftone_prepare(igs, pht, pdht);
  230.       }
  231.     if ( code >= 0 )
  232.       {    /* Schedule the sampling of the screens. */
  233.         es_ptr esp0 = esp;        /* for backing out */
  234.         esp += 8;
  235.         make_mark_estack(esp - 7, es_other, setcolorscreen_cleanup);
  236.         memcpy(esp - 6, sprocs, sizeof(ref) * 4);    /* procs */
  237.         make_istruct(esp - 2, 0, pht);
  238.         make_istruct(esp - 1, 0, pdht);
  239.         make_op_estack(esp, setcolorscreen_finish);
  240.         for ( i = 0; i < 4; i++ )
  241.         {    code = zsetscreen_init(op,
  242.                 &cscreen.screens.indexed[i],
  243.                 &sprocs[i], 0, spot_sample_finish);
  244.             if ( code < 0 )
  245.             {    esp = esp0;
  246.                 break;
  247.             }
  248.             /* Save the order index in the enumeration */
  249.             /* structure, which is 1 below esp. */
  250.             /* Shuffle the indices to correspond to */
  251.             /* the component order. */
  252.             {    gs_screen_enum *penum =
  253.                   r_ptr(esp - 1, gs_screen_enum);
  254.                 penum->dev_ht = pdht;
  255.                 penum->comp_index = (i + 1) & 3;
  256.             }
  257.         }
  258.     }
  259.     if ( code < 0 )
  260.       {    ifree_object(pdht, "setcolorscreen");
  261.         ifree_object(pht, "setcolorscreen");
  262.         return code;
  263.       }
  264.     pop(12);
  265.     return o_push_estack;
  266. }
  267. /* Finish the sampling of one screen. */
  268. /* This is exported for Type 1 sethalftone. */
  269. int
  270. spot_sample_finish(os_ptr op)
  271. {    /* Copy the halftone order that was allocated by zsetscreen_init */
  272.     /* to the one allocated by gs_sethalftone_prepare, */
  273.     /* and free the former. */
  274.     /* The enumeration structure is on top of the e-stack. */
  275.     gs_screen_enum *penum = r_ptr(esp, gs_screen_enum);
  276.     gx_device_halftone *pdht = penum->dev_ht;
  277.     int ci = penum->comp_index;
  278.     gx_ht_order *sorder = (pdht->components == 0 ?
  279.                    &pdht->order :
  280.                    &pdht->components[ci].corder);
  281.     memcpy(sorder->levels, penum->order.levels,
  282.            penum->order.num_levels * sizeof(uint));
  283.     memcpy(sorder->bits, penum->order.bits,
  284.            penum->order.num_bits * sizeof(gx_ht_bit));
  285.     ifree_object(penum->order.bits, "spot_sample_finish(bits)");
  286.     ifree_object(penum->order.levels, "spot_sample_finish(levels)");
  287.     return 0;
  288. }
  289. /* Install the color screen after sampling. */
  290. private int
  291. setcolorscreen_finish(os_ptr op)
  292. {    gx_device_halftone *pdht = r_ptr(esp, gx_device_halftone);
  293.     int code;
  294.     pdht->order = pdht->components[0].corder;
  295.     code = gx_ht_install(igs, r_ptr(esp - 1, gs_halftone), pdht);
  296.     if ( code < 0 )
  297.         return code;
  298.     memcpy(istate->screen_procs.indexed, esp - 5, sizeof(ref) * 4);
  299.     make_null(&istate->halftone);
  300.     esp -= 7;
  301.     setcolorscreen_cleanup(op);
  302.     return o_pop_estack;
  303. }
  304. /* Clean up after installing the color screen. */
  305. private int
  306. setcolorscreen_cleanup(os_ptr op)
  307. {    ifree_object(esp[7].value.pstruct,
  308.              "setcolorscreen_cleanup(device halftone)");
  309.     ifree_object(esp[6].value.pstruct,
  310.              "setcolorscreen_cleanup(halftone)");
  311.     return 0;
  312. }
  313.  
  314. /* ------ Utility procedures ------ */
  315.  
  316. /* Get parameters for a single screen. */
  317. private int
  318. screen_params(os_ptr op, gs_screen_halftone *phs)
  319. {    float fa[2];
  320.     int code = num_params(op - 1, 2, fa);
  321.     if ( code < 0 )
  322.         return code;
  323.     check_proc(*op);
  324.     phs->frequency = fa[0];
  325.     phs->angle = fa[1];
  326.     return 0;
  327. }
  328.  
  329. /* ------ Initialization procedure ------ */
  330.  
  331. op_def zht_op_defs[] = {
  332.     {"0.currenthalftone", zcurrenthalftone},
  333.     {"3setscreen", zsetscreen},
  334.     /*{"<setcolorscreen", zsetcolorscreen},*/    /* in zcolor1.c */
  335.         /* Internal operators */
  336.     {"0%screen_sample", screen_sample},
  337.     {"1%set_screen_continue", set_screen_continue},
  338.     {"0%spot_sample_finish", spot_sample_finish},
  339.     {"0%setscreen_finish", setscreen_finish},
  340.     {"0%setcolorscreen_finish", setcolorscreen_finish},
  341.     op_def_end(0)
  342. };
  343.