summaryrefslogtreecommitdiff
path: root/psi/zht.c
blob: efd5519ce45d1dfc9961d7c0cf7cd9ee0c80934f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* Copyright (C) 2001-2019 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
*/


/* Halftone definition operators */
#include "ghost.h"
#include "memory_.h"
#include "oper.h"
#include "estack.h"
#include "gsstruct.h"		/* must precede igstate.h, */
                                        /* because of #ifdef in gsht.h */
#include "ialloc.h"
#include "igstate.h"
#include "gsmatrix.h"
#include "gxdevice.h"		/* for gzht.h */
#include "gzht.h"
#include "gsstate.h"
#include "iht.h"		/* prototypes */
#include "store.h"

/* Forward references */
static int screen_sample(i_ctx_t *);
static int set_screen_continue(i_ctx_t *);
static int screen_cleanup(i_ctx_t *);

/* - .currenthalftone <dict> 0 */
/* - .currenthalftone <frequency> <angle> <proc> 1 */
/* - .currenthalftone <red_freq> ... <gray_proc> 2 */
static int
zcurrenthalftone(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    gs_halftone ht;

    gs_currenthalftone(igs, &ht);
    switch (ht.type) {
        case ht_type_screen:
            push(4);
            make_real(op - 3, ht.params.screen.frequency);
            make_real(op - 2, ht.params.screen.angle);
            op[-1] = istate->screen_procs.gray;
            make_int(op, 1);
            break;
        case ht_type_colorscreen:
            push(13);
            {
                os_ptr opc = op - 12;
                gs_screen_halftone *pht =
                    &ht.params.colorscreen.screens.colored.red;

                make_real(opc, pht->frequency);
                make_real(opc + 1, pht->angle);
                opc[2] = istate->screen_procs.red;

                opc = op - 9;
                pht = &ht.params.colorscreen.screens.colored.green;
                make_real(opc, pht->frequency);
                make_real(opc + 1, pht->angle);
                opc[2] = istate->screen_procs.green;

                opc = op - 6;
                pht = &ht.params.colorscreen.screens.colored.blue;
                make_real(opc, pht->frequency);
                make_real(opc + 1, pht->angle);
                opc[2] = istate->screen_procs.blue;

                opc = op - 3;
                pht = &ht.params.colorscreen.screens.colored.gray;
                make_real(opc, pht->frequency);
                make_real(opc + 1, pht->angle);
                opc[2] = istate->screen_procs.gray;
            }
            make_int(op, 2);
            break;
        default:		/* Screen was set by sethalftone. */
            push(2);
            op[-1] = istate->halftone;
            make_int(op, 0);
            break;
    }
    return 0;
}

/* - .currentscreenlevels <int> */
static int
zcurrentscreenlevels(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    push(1);
    make_int(op, gs_currentscreenlevels(igs));
    return 0;
}

/* The setscreen operator is complex because it has to sample */
/* each pixel in the pattern cell, calling a procedure, and then */
/* sort the result into a whitening order. */

/* Layout of stuff pushed on estack: */
/*      Control mark, */
/*      [other stuff for other screen-setting operators], */
/*      finishing procedure (or 0), */
/*      spot procedure, */
/*      enumeration structure (as bytes). */
#define snumpush 4
#define sproc esp[-1]
#define senum r_ptr(esp, gs_screen_enum)

/* Forward references */
static int setscreen_finish(i_ctx_t *);

/* <frequency> <angle> <proc> setscreen - */
static int
zsetscreen(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    gs_screen_halftone screen;
    gx_ht_order order;
    int code = zscreen_params(op, &screen);
    gs_memory_t *mem;
    int space_index = r_space_index(op);

    if (code < 0)
        return code;
    mem = (gs_memory_t *)idmemory->spaces_indexed[space_index];
    /*
     * Allocate the halftone in the same VM space as the procedure.
     * This keeps the space relationships consistent.
     */
    code = gs_screen_order_init_memory(&order, igs, &screen,
                                       gs_currentaccuratescreens(mem), mem);
    if (code < 0)
        return code;
    return zscreen_enum_init(i_ctx_p, &order, &screen, op, 3,
                             setscreen_finish, space_index);
}

/* We break out the body of this operator so it can be shared with */
/* the code for Type 1 halftones in sethalftone. */
int
zscreen_enum_init(i_ctx_t *i_ctx_p, const gx_ht_order * porder,
                  gs_screen_halftone * psp, ref * pproc, int npop,
                  int (*finish_proc)(i_ctx_t *), int space_index)
{
    gs_screen_enum *penum;
    gs_memory_t * mem = (gs_memory_t *)idmemory->spaces_indexed[space_index];
    int code;

    check_estack(snumpush + 1);
    penum = gs_screen_enum_alloc(mem, "setscreen");
    if (penum == 0)
        return_error(gs_error_VMerror);
    make_struct(esp + snumpush, space_index << r_space_shift, penum);	/* do early for screen_cleanup in case of error */
    code = gs_screen_enum_init_memory(penum, porder, igs, psp, mem);
    if (code < 0) {
        screen_cleanup(i_ctx_p);
        return code;
    }
    /* Push everything on the estack */
    make_mark_estack(esp + 1, es_other, screen_cleanup);
    esp += snumpush;
    make_op_estack(esp - 2, finish_proc);
    sproc = *pproc;
    push_op_estack(screen_sample);
    pop(npop);
    return o_push_estack;
}
/* Set up the next sample */
static int
screen_sample(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    gs_screen_enum *penum = senum;
    gs_point pt;
    int code = gs_screen_currentpoint(penum, &pt);
    ref proc;

    switch (code) {
        default:
            return code;
        case 1:
            /* All done */
            if (real_opproc(esp - 2) != 0)
                code = (*real_opproc(esp - 2)) (i_ctx_p);
            esp -= snumpush;
            screen_cleanup(i_ctx_p);
            return (code < 0 ? code : o_pop_estack);
        case 0:
            ;
    }
    push(2);
    make_real(op - 1, pt.x);
    make_real(op, pt.y);
    proc = sproc;
    push_op_estack(set_screen_continue);
    *++esp = proc;
    return o_push_estack;
}
/* Continuation procedure for processing sampled pixels. */
static int
set_screen_continue(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    double value;
    int code = real_param(op, &value);

    if (code < 0)
        return code;
    code = gs_screen_next(senum, value);
    if (code < 0)
        return code;
    pop(1);
    return screen_sample(i_ctx_p);
}
/* Finish setscreen. */
static int
setscreen_finish(i_ctx_t *i_ctx_p)
{
    gs_screen_install(senum);
    istate->screen_procs.red = sproc;
    istate->screen_procs.green = sproc;
    istate->screen_procs.blue = sproc;
    istate->screen_procs.gray = sproc;
    make_null(&istate->halftone);
    return 0;
}
/* Clean up after screen enumeration */
static int
screen_cleanup(i_ctx_t *i_ctx_p)
{
    gs_screen_enum *penum = r_ptr(esp + snumpush, gs_screen_enum);

    gs_free_object(penum->halftone.rc.memory, penum, "screen_cleanup");
    return 0;
}

/* ------ Utility procedures ------ */

/* Get parameters for a single screen. */
int
zscreen_params(os_ptr op, gs_screen_halftone * phs)
{
    double fa[2];
    int code = num_params(op - 1, 2, fa);

    if (code < 0)
        return code;
    check_proc(*op);
    phs->frequency = fa[0];
    phs->angle = fa[1];
    return 0;
}

/* ------ Initialization procedure ------ */

const op_def zht_op_defs[] =
{
    {"0.currenthalftone", zcurrenthalftone},
    {"0.currentscreenlevels", zcurrentscreenlevels},
    {"3setscreen", zsetscreen},
                /* Internal operators */
    {"0%screen_sample", screen_sample},
    {"1%set_screen_continue", set_screen_continue},
    {"0%setscreen_finish", setscreen_finish},
    op_def_end(0)
};