summaryrefslogtreecommitdiff
path: root/gs/base/gdevdsha.c
blob: fda77b09fe326821c6ed5e9b4d6f719c6524c0ee (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* Copyright (C) 2001-2006 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 that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/
/* $Id$ */
/* Default shading drawing device procedures. */

#include "gx.h"
#include "gserrors.h"
#include "gxdevice.h"
#include "gxcindex.h"
#include "vdtrace.h"
#include "gxdevsop.h"

static bool 
gx_devn_diff(frac31 devn1[], frac31 devn2[], int num) 
{
    int k;
    
    for (k = 0; k < num; k++) {
        if (devn1[k] != devn2[k]) {
            return true;
        }
    }
    return false;
}

int
gx_hl_fill_linear_color_scanline(gx_device *dev, const gs_fill_attributes *fa,
        int i0, int j, int w, const frac31 *c0, const int32_t *c0f, 
        const int32_t *cg_num, int32_t cg_den)
{
    frac31 c[GX_DEVICE_COLOR_MAX_COMPONENTS];
    frac31 curr[GX_DEVICE_COLOR_MAX_COMPONENTS];
    ulong f[GX_DEVICE_COLOR_MAX_COMPONENTS];
    int i, i1 = i0 + w, bi = i0, k;
    const gx_device_color_info *cinfo = &dev->color_info;
    int n = cinfo->num_components;
    int si, ei, di, code;
    gs_fixed_rect rect;
    gx_device_color devc;

    /* Note: All the stepping math is done with frac color values */

    devc.type = gx_dc_type_devn;
    
    if (j < fixed2int(fa->clip->p.y) ||
            j > fixed2int_ceiling(fa->clip->q.y)) /* Must be compatible to the clipping logic. */
        return 0;
    for (k = 0; k < n; k++) {
        curr[k] = c[k] = c0[k];
        f[k] = c0f[k];
    }
    for (i = i0 + 1, di = 1; i < i1; i += di) {
        if (di == 1) {
            /* Advance colors by 1 pixel. */
            for (k = 0; k < n; k++) {
                if (cg_num[k]) {
                    int32_t m = f[k] + cg_num[k];

                    c[k] += m / cg_den;
                    m -= m / cg_den * cg_den;
                    if (m < 0) {
                        c[k]--;
                        m += cg_den;
                    }
                    f[k] = m;
                }
            }
        } else {
            /* Advance colors by di pixels. */
            for (k = 0; k < n; k++) {
                if (cg_num[k]) {
                    int64_t M = f[k] + (int64_t)cg_num[k] * di;
                    int32_t m;

                    c[k] += (frac31)(M / cg_den);
                    m = (int32_t)(M - M / cg_den * cg_den);
                    if (m < 0) {
                        c[k]--;
                        m += cg_den;
                    }
                    f[k] = m;
                }
            }
        }
        if (gx_devn_diff(c, curr, n)) {
            si = max(bi, fixed2int(fa->clip->p.x));	    /* Must be compatible to the clipping logic. */
            ei = min(i, fixed2int_ceiling(fa->clip->q.x));  /* Must be compatible to the clipping logic. */
            if (si < ei) {
                if (fa->swap_axes) {
                    rect.p.x = j;
                    rect.p.y = si;
                    rect.q.x = j + 1;
                    rect.q.y = ei;
                } else {
                    rect.p.x = si;
                    rect.p.y = j;
                    rect.q.x = ei;
                    rect.q.y = j + 1;
                }
                for (k = 0; k < n; k++) {
                    devc.colors.devn.values[k] = frac2cv(curr[k]);
                }
                code = dev_proc(dev, fill_rectangle_hl_color) (dev, &rect, NULL, &devc, NULL);
                if (code < 0)
                    return code;
            }
            bi = i;
            for (k = 0; k < n; k++) {
                curr[k] = c[k];
            }
            di = 1;
        } else if (i == i1) {
            i++;
            break;
        } else {
            /* Compute a color change pixel analitically. */
            di = i1 - i;
            for (k = 0; k < n; k++) {
                int32_t a;
                int64_t x;
                frac31 v = 1 << (31 - cinfo->comp_bits[k]); /* Color index precision in frac31. */
                frac31 u = c[k] & (v - 1);

                if (cg_num[k] == 0) {
                    /* No change. */
                    continue;
                } if (cg_num[k] > 0) {
                    /* Solve[(f[k] + cg_num[k]*x)/cg_den == v - u, x]  */
                    a = v - u;
                } else {
                    /* Solve[(f[k] + cg_num[k]*x)/cg_den == - u - 1, x]  */
                    a = -u - 1;
                }
                x = ((int64_t)a * cg_den - f[k]) / cg_num[k];
                if (i + x >= i1)
                    continue;
                else if (x < 0)
                    return_error(gs_error_unregistered); /* Must not happen. */
                else if (di > (int)x) {
                    di = (int)x;
                    if (di <= 1) {
                        di = 1;
                        break;
                    }
                }
            }
        }
    }
    si = max(bi, fixed2int(fa->clip->p.x));	    /* Must be compatible to the clipping logic. */
    ei = min(i, fixed2int_ceiling(fa->clip->q.x));  /* Must be compatible to the clipping logic. */
    if (si < ei) {
        if (fa->swap_axes) {
            rect.p.x = j;
            rect.p.y = si;
            rect.q.x = j + 1;
            rect.q.y = ei;
        } else {
            rect.p.x = si;
            rect.p.y = j;
            rect.q.x = ei;
            rect.q.y = j + 1;
        }
        for (k = 0; k < n; k++) {
            devc.colors.devn.values[k] = frac2cv(curr[k]);
        }
        return dev_proc(dev, fill_rectangle_hl_color) (dev, &rect, NULL, &devc, NULL);
    }
    return 0;
}

int
gx_default_fill_linear_color_scanline(gx_device *dev, const gs_fill_attributes *fa,
        int i0, int j, int w,
        const frac31 *c0, const int32_t *c0f, const int32_t *cg_num, int32_t cg_den)
{
    /* This default implementation decomposes the area into constant color rectangles.
       Devices may supply optimized implementations with
       the inversed nesting of the i,k cicles,
       i.e. with enumerating planes first, with a direct writing to the raster,
       and with a fixed bits per component.
     */
    /* First determine if we are doing high level style colors or pure colors */
    bool devn = dev_proc(dev, dev_spec_op)(dev, gxdso_supports_devn, NULL, 0);
    frac31 c[GX_DEVICE_COLOR_MAX_COMPONENTS];
    ulong f[GX_DEVICE_COLOR_MAX_COMPONENTS];
    int i, i1 = i0 + w, bi = i0, k;
    gx_color_index ci0 = 0, ci1;
    const gx_device_color_info *cinfo = &dev->color_info;
    int n = cinfo->num_components;
    int si, ei, di, code;

    /* Todo: set this up to vector earlier */
    if (devn && cinfo->polarity == GX_CINFO_POLARITY_SUBTRACTIVE)
        return gx_hl_fill_linear_color_scanline(dev, fa, i0, j, w, c0, c0f, 
                                                cg_num, cg_den);
    if (j < fixed2int(fa->clip->p.y) ||
            j > fixed2int_ceiling(fa->clip->q.y)) /* Must be compatible to the clipping logic. */
        return 0;
    for (k = 0; k < n; k++) {
        int shift = cinfo->comp_shift[k];
        int bits = cinfo->comp_bits[k];

        c[k] = c0[k];
        f[k] = c0f[k];
        ci0 |= (gx_color_index)(c[k] >> (sizeof(c[k]) * 8 - 1 - bits)) << shift;
    }
    for (i = i0 + 1, di = 1; i < i1; i += di) {
        if (di == 1) {
            /* Advance colors by 1 pixel. */
            ci1 = 0;
            for (k = 0; k < n; k++) {
                int shift = cinfo->comp_shift[k];
                int bits = cinfo->comp_bits[k];

                if (cg_num[k]) {
                    int32_t m = f[k] + cg_num[k];

                    c[k] += m / cg_den;
                    m -= m / cg_den * cg_den;
                    if (m < 0) {
                        c[k]--;
                        m += cg_den;
                    }
                    f[k] = m;
                }
                ci1 |= (gx_color_index)(c[k] >> (sizeof(c[k]) * 8 - 1 - bits)) << shift;
            }
        } else {
            /* Advance colors by di pixels. */
            ci1 = 0;
            for (k = 0; k < n; k++) {
                int shift = cinfo->comp_shift[k];
                int bits = cinfo->comp_bits[k];

                if (cg_num[k]) {
                    int64_t M = f[k] + (int64_t)cg_num[k] * di;
                    int32_t m;

                    c[k] += (frac31)(M / cg_den);
                    m = (int32_t)(M - M / cg_den * cg_den);
                    if (m < 0) {
                        c[k]--;
                        m += cg_den;
                    }
                    f[k] = m;
                }
                ci1 |= (gx_color_index)(c[k] >> (sizeof(c[k]) * 8 - 1 - bits)) << shift;
            }
        }
        if (ci1 != ci0) {
            si = max(bi, fixed2int(fa->clip->p.x));	    /* Must be compatible to the clipping logic. */
            ei = min(i, fixed2int_ceiling(fa->clip->q.x));  /* Must be compatible to the clipping logic. */
            if (si < ei) {
                if (fa->swap_axes) {
                    vd_rect(int2fixed(j), int2fixed(si), int2fixed(j + 1), int2fixed(ei), 1, (ulong)ci0);
                    code = dev_proc(dev, fill_rectangle)(dev, j, si, 1, ei - si, ci0);
                } else {
                    vd_rect(int2fixed(si), int2fixed(j), int2fixed(ei), int2fixed(j + 1), 1, (ulong)ci0);
                    code = dev_proc(dev, fill_rectangle)(dev, si, j, ei - si, 1, ci0);
                }
                if (code < 0)
                    return code;
            }
            bi = i;
            ci0 = ci1;
            di = 1;
        } else if (i == i1) {
            i++;
            break;
        } else {
            /* Compute a color change pixel analitically. */
            di = i1 - i;
            for (k = 0; k < n; k++) {
                int32_t a;
                int64_t x;
                frac31 v = 1 << (31 - cinfo->comp_bits[k]); /* Color index precision in frac31. */
                frac31 u = c[k] & (v - 1);

                if (cg_num[k] == 0) {
                    /* No change. */
                    continue;
                } if (cg_num[k] > 0) {
                    /* Solve[(f[k] + cg_num[k]*x)/cg_den == v - u, x]  */
                    a = v - u;
                } else {
                    /* Solve[(f[k] + cg_num[k]*x)/cg_den == - u - 1, x]  */
                    a = -u - 1;
                }
                x = ((int64_t)a * cg_den - f[k]) / cg_num[k];
                if (i + x >= i1)
                    continue;
                else if (x < 0)
                    return_error(gs_error_unregistered); /* Must not happen. */
                else if (di > (int)x) {
                    di = (int)x;
                    if (di <= 1) {
                        di = 1;
                        break;
                    }
                }
            }
        }
    }
    si = max(bi, fixed2int(fa->clip->p.x));	    /* Must be compatible to the clipping logic. */
    ei = min(i, fixed2int_ceiling(fa->clip->q.x));  /* Must be compatible to the clipping logic. */
    if (si < ei) {
        if (fa->swap_axes) {
            vd_rect(int2fixed(j), int2fixed(si), int2fixed(j + 1), int2fixed(ei), 1, (ulong)ci0);
            return dev_proc(dev, fill_rectangle)(dev, j, si, 1, ei - si, ci0);
        } else {
            vd_rect(int2fixed(si), int2fixed(j), int2fixed(ei), int2fixed(j + 1), 1, (ulong)ci0);
            return dev_proc(dev, fill_rectangle)(dev, si, j, ei - si, 1, ci0);
        }
    }
    return 0;
}