summaryrefslogtreecommitdiff
path: root/gs/src/gxhldevc.c
blob: cfde556e9016cd496b75a39bea7781928fe1a400 (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) 2003 Artifex Software Inc, artofcode llc.  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.
  
  For more information about licensing, please refer to
  http://www.ghostscript.com/licensing/. For information on
  commercial licensing, go to http://www.artifex.com/licensing/ or
  contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  San Rafael, CA  94903, U.S.A., +1(415)492-9861.
*/
/* $Id$ */
/* High level device color save/compare procedures */

/*
 * See comments at the start of gxhldevc.h for more explanation of the
 * purpose and operation of these procedures.
 */
#include "gx.h"
#include "gzstate.h"
#include "gscspace.h"
#include "gxcspace.h"
#include "gxhldevc.h"
#include "memory_.h"
#include "gxpcolor.h"
#include "gsptype2.h"

/*
 * Initiailze a high level saved color to null
 */
void gx_hld_saved_color_init(gx_hl_saved_color * psc)
{
    gx_device_color temp_devc;

    memset(psc, 0, sizeof(*psc));	/* clear the entire structure */   
    psc->color_space_id = psc->pattern_id = gs_no_id;
    color_set_null(&temp_devc);
    temp_devc.type->save_dc(&temp_devc, &(psc->saved_dev_color));
}

/*
 * Get graphics state pointer (from imager state pointer)
 */
const gs_state * gx_hld_get_gstate_ptr(const gs_imager_state * pis)
{
    extern_st(st_gs_state);		/* only for testing */

    /* Check to verify the structure type is really st_gs_state */
    if (pis == NULL || gs_object_type(pis->memory, pis) != &st_gs_state)
	return NULL;

    return (const gs_state *) pis;
}

/*
 * Save the device color information including the color space id and
 * client color data (if available).
 *
 * More description in src/gxhldevc.h
 */
bool
gx_hld_save_color(const gs_imager_state * pis, const gx_device_color * pdevc,
		gx_hl_saved_color * psc)
{
    const gs_state * pgs = gx_hld_get_gstate_ptr(pis);
    memset(psc, 0, sizeof(*psc));	/* clear the entire structure */

    if (pdevc == NULL) {
        /* No device color given, should not happen */
        gx_hld_saved_color_init(psc);	/* revert to unknown color */
	return false;
    } else if (pgs == NULL) {
        /* No color space, simply save device color specific info */
        psc->color_space_id = psc->pattern_id = gs_no_id;
        pdevc->type->save_dc(pdevc, &(psc->saved_dev_color));
	return false;
    } else {
        /*
	 * Have color space, save id,  ccolor, & device color specific info.
	 * Also save the high level colors since two gx_color_index values
	 * may be the same but for differing high level colors (due to the
	 * usual lower resolution of the gx_color_index values.
	 */
        const gs_color_space * pcs = pgs->color_space;
        int i = gs_color_space_num_components(pcs);

        psc->color_space_id = pcs->id;
        pdevc->type->save_dc(pdevc, &(psc->saved_dev_color));
	if (pdevc->type == gx_dc_type_pattern2)
	    i = 0;
        else if (i < 0)
	    i = -i - 1; /* See gx_num_components_Pattern. */
        for (i--; i >= 0; i--)
	    psc->ccolor.paint.values[i] = pdevc->ccolor.paint.values[i];

	/* Save the pattern id - if present */
	if ((pdevc->type == gx_dc_type_pattern 
	   || pdevc->type == gx_dc_type_pattern2) && pdevc->ccolor_valid)
            psc->pattern_id = pdevc->ccolor.pattern->pattern_id;
	else
            psc->pattern_id = gs_no_id;
	return true;
    }
}

/*
 * Compare two saved colors to check if match.  Note this routine assumes
 * unused parts of the saved color have been zeroed.  See gx_hld_save_color()
 * for what is actually being compared.
 */
bool gx_hld_saved_color_equal(const gx_hl_saved_color * psc1,
			   const gx_hl_saved_color * psc2)
{
    return (memcmp(psc1, psc2, sizeof(*psc1)) == 0);
}

/*
 * Check whether two saved colors have same color space.
 */
bool gx_hld_saved_color_same_cspace(const gx_hl_saved_color * psc1,
			   const gx_hl_saved_color * psc2)
{
    if (psc1->color_space_id != psc2->color_space_id)
	return false;
    if (psc1->pattern_id != psc2->pattern_id)
	return false;
    if (psc1->ccolor_valid != psc2->ccolor_valid)
	return false;
    if (psc1->color_space_id != psc2->color_space_id)
	return false;
    return true;
}

/*
 * Check if a high level color is availavble.
 */
bool
gx_hld_is_hl_color_available(const gs_imager_state * pis,
		const gx_device_color * pdevc)
{
    const gs_state * pgs = gx_hld_get_gstate_ptr(pis);

    if (pgs != NULL && pdevc != NULL && pdevc->ccolor_valid)
	return true;
    return false;
}

/*
 * Get pointers to the current color space and client color.
 *
 * More description in src/gxhldevc.h
 */
gx_hld_get_color_space_and_ccolor_status
gx_hld_get_color_space_and_ccolor(const gs_imager_state * pis,
		const gx_device_color * pdevc, const gs_color_space ** ppcs,
		const gs_client_color ** ppcc)
{
    /* Check if the current color space was used to build the device color */
    if (gx_hld_is_hl_color_available(pis, pdevc)) {
	const gs_state * pgs = gx_hld_get_gstate_ptr(pis);
        const gs_color_space * pcs = pgs->color_space;

	*ppcs = pcs;
	*ppcc = &(pdevc->ccolor);
	if (pdevc->type == gx_dc_type_pattern 
	   || pdevc->type == &gx_dc_pure_masked
	   || pdevc->type == gx_dc_type_pattern2)
            return pattern_color_sapce;
	else {
            return non_pattern_color_space;
	}
    }
    /* No color space */
    *ppcs = NULL;
    *ppcc = NULL;
    return use_process_color;
}

/*
 * Get the number of components in the current color space.
 *
 * More description in src/gxhldevc.h
 */
int
gx_hld_get_number_color_components(const gs_imager_state * pis)
{
    const gs_state * pgs = gx_hld_get_gstate_ptr(pis);

    if (pgs != NULL) {
        const gs_color_space * pcs = pgs->color_space;
	int n = gs_color_space_num_components(pcs);

	return (n >= 0 ? n : -n - 1);
    } else
	return -1;
}

/*
 * Get the requested high level color value.
 *
 * More description in src/gxhldevc.h
 */
gx_hld_get_color_component_status
gx_hld_get_color_component(const gs_imager_state * pis,
			  const gx_device_color * pdevc,
			  int comp_num, float * output)
{
    if (pdevc != NULL && pdevc->ccolor_valid) {
	int ncomp = gx_hld_get_number_color_components(pis);

	if (ncomp < 0)
	    return invalid_color_info;
	if (comp_num < 0 || comp_num >= ncomp) 
	    return invalid_component_requested;
	*output = pdevc->ccolor.paint.values[comp_num];
        return valid_result;
    }
    return invalid_color_info;
}