summaryrefslogtreecommitdiff
path: root/gs/src/gdevht.c
blob: 00d8a5ab47880f20e9f8d471538a830a14886ae6 (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
/* Copyright (C) 1995, 1996 Aladdin Enterprises.  All rights reserved.

   This file is part of Aladdin Ghostscript.

   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
   or distributor accepts any responsibility for the consequences of using it,
   or for whether it serves any particular purpose or works at all, unless he
   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
   License (the "License") for full details.

   Every copy of Aladdin Ghostscript must include a copy of the License,
   normally in a plain ASCII text file named PUBLIC.  The License grants you
   the right to copy, modify and redistribute Aladdin Ghostscript, but only
   under certain conditions described in the License.  Among other things, the
   License requires that the copyright notice and this notice be preserved on
   all copies.
 */

/* gdevht.c */
/* Halftoning device implementation */
#include "gx.h"
#include "gserrors.h"
#include "gxdevice.h"
#include "gdevht.h"
#include "gxdcolor.h"
#include "gxdcconv.h"
#include "gxdither.h"

/* The device procedures */
private dev_proc_open_device(ht_open);
private dev_proc_map_rgb_color(ht_map_rgb_color);
private dev_proc_map_color_rgb(ht_map_color_rgb);
private dev_proc_fill_rectangle(ht_fill_rectangle);
private dev_proc_map_cmyk_color(ht_map_cmyk_color);
private dev_proc_map_rgb_alpha_color(ht_map_rgb_alpha_color);
private const gx_device_ht far_data gs_ht_device =
{std_device_dci_body(gx_device_ht, 0, "halftoner",
		     0, 0, 1, 1,
		     1, 8, 255, 0, 0, 0),
 {ht_open,
  gx_forward_get_initial_matrix,
  gx_forward_sync_output,
  gx_forward_output_page,
  gx_default_close_device,
  ht_map_rgb_color,
  ht_map_color_rgb,
  ht_fill_rectangle,
  gx_default_tile_rectangle,
  gx_default_copy_mono,
  gx_default_copy_color,
  gx_default_draw_line,
  gx_default_get_bits,
  gx_forward_get_params,
  gx_forward_put_params,
  ht_map_cmyk_color,
  gx_forward_get_xfont_procs,
  gx_forward_get_xfont_device,
  ht_map_rgb_alpha_color,
  gx_forward_get_page_device,
  gx_forward_get_alpha_bits,
  gx_default_copy_alpha,
  gx_forward_get_band,
  gx_default_copy_rop,
  gx_default_fill_path,
  gx_default_stroke_path,
  gx_default_fill_mask,
  gx_default_fill_trapezoid,
  gx_default_fill_parallelogram,
  gx_default_fill_triangle,
  gx_default_draw_thin_line,
  gx_default_begin_image,
  gx_default_image_data,
  gx_default_end_image,
  gx_default_strip_tile_rectangle,
  gx_default_strip_copy_rop,
  gx_forward_get_clipping_box,
  gx_forward_get_hardware_params,
 }
};

/*
 * Define the packing of two target colors and a halftone level into
 * a gx_color_index.  Since C doesn't let us cast between a structure
 * and a scalar, we have to use explicit shifting and masking.
 */
#define cx_bits (sizeof(gx_color_index) * 8)
#define cx_color_mask ((1 << ht_target_max_depth) - 1)
#define cx_color0(color) ((color) >> (cx_bits - ht_target_max_depth))
#define cx_color1(color) (((color) >> (ht_level_depth)) & cx_color_mask)
#define cx_level(color) ((color) & ((1 << ht_level_depth) - 1))
#define cx_values(c0, c1, lev)\
  ( ((((c0) << ht_target_max_depth) + (c1)) << ht_level_depth) + (lev) )

/* Open the device.  Right now we just make some error checks. */
private int
ht_open(gx_device * dev)
{
    if (htdev->target == 0 ||
	htdev->target->color_info.depth > ht_target_max_depth
	)
	return_error(gs_error_rangecheck);
    htdev->phase.x = imod(-htdev->ht_phase.x, htdev->dev_ht->lcm_width);
    htdev->phase.y = imod(-htdev->ht_phase.y, htdev->dev_ht->lcm_height);
    return 0;
}

/* Map from RGB or CMYK colors to the packed representation. */
private gx_color_index
ht_finish_map_color(int code, const gx_device_color * pdevc)
{
    if (code < 0)
	return gx_no_color_index;
    if (pdevc->type == &gx_dc_pure)
	return cx_values(pdevc->colors.pure, 0, 0);
    if (pdevc->type == &gx_dc_ht_binary)
	return cx_values(pdevc->colors.binary.color[0],
			 pdevc->colors.binary.color[1],
			 pdevc->colors.binary.b_level);
    lprintf("bad type in ht color mapping!");
    return gx_no_color_index;
}
private gx_color_index
ht_map_rgb_color(gx_device * dev, gx_color_value r, gx_color_value g,
		 gx_color_value b)
{
    return ht_map_rgb_alpha_color(dev, r, g, b, gx_max_color_value);
}
gx_color_index
ht_map_cmyk_color(gx_device * dev, gx_color_value c, gx_color_value m,
		  gx_color_value y, gx_color_value k)
{
    gx_device_color devc;
    frac fc = cv2frac(k);
    frac fk = cv2frac(k);
    int code =
    (c == m & m == y ?
     gx_render_device_gray(color_cmyk_to_gray(fc, fc, fc, fk, NULL),
			   gx_max_color_value,
			   &devc, htdev->target, htdev->dev_ht,
			   &htdev->ht_phase) :
     gx_render_device_color(fc, cv2frac(m), cv2frac(y),
			    fk, false, gx_max_color_value,
			    &devc, htdev->target, htdev->dev_ht,
			    &htdev->ht_phase));

    return ht_finish_map_color(code, &devc);
}
gx_color_index
ht_map_rgb_alpha_color(gx_device * dev, gx_color_value r,
		   gx_color_value g, gx_color_value b, gx_color_value alpha)
{
    gx_device_color devc;
    int code =
    (r == g & g == b ?
     gx_render_device_gray(cv2frac(r), alpha,
			   &devc, htdev->target, htdev->dev_ht,
			   &htdev->ht_phase) :
     gx_render_device_color(cv2frac(r), cv2frac(g), cv2frac(b),
			    frac_0, false, alpha,
			    &devc, htdev->target, htdev->dev_ht,
			    &htdev->ht_phase));

    return ht_finish_map_color(code, &devc);
}

/* Map back to an RGB color. */
private int
ht_map_color_rgb(gx_device * dev, gx_color_index color,
		 gx_color_value prgb[3])
{
    gx_color_index color0 = cx_color0(color);
    uint level = cx_level(color);
    gx_device *tdev = htdev->target;

    dev_proc_map_color_rgb((*map)) = dev_proc(tdev, map_color_rgb);

    if (level == 0)
	return (*map) (tdev, color0, prgb);
    {
	gx_color_index color1 = cx_color1(color);
	gx_color_value rgb0[3], rgb1[3];
	uint num_levels = htdev->dev_ht->order.num_levels;
	int i;

	(*map) (tdev, color0, rgb0);
	(*map) (tdev, color1, rgb1);
	for (i = 0; i < 3; ++i)
	    prgb[i] = rgb0[i] +
		(rgb1[i] - rgb0[i]) * (ulong) level / num_levels;
	return 0;
    }
}

/* Fill a rectangle by tiling with a halftone. */
private int
ht_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
		  gx_color_index color)
{
    gx_color_index color0 = cx_color0(color);
    uint level = cx_level(color);
    gx_device *tdev = htdev->target;

    if (level == 0)
	return (*dev_proc(tdev, fill_rectangle))
	    (tdev, x, y, w, h, color0);
    {
	gx_color_index color1 = cx_color1(color);
	const gx_ht_order *porder = &htdev->dev_ht->order;
	gx_ht_cache *pcache = porder->cache;
	gx_ht_tile *tile;

	/* Ensure that the tile cache is current. */
	if (pcache->order.bits != porder->bits)
	    gx_ht_init_cache(pcache, porder);
	/* Ensure that the tile we want is cached. */
	tile = gx_render_ht(pcache, level);
	if (tile == 0)
	    return_error(gs_error_Fatal);
	/* Fill the rectangle with the tile. */
	return (*dev_proc(tdev, tile_rectangle))
	    (tdev, &tile->tile, x, y, w, h, color0, color1,
	     htdev->phase.x, htdev->phase.y);
    }
}