1/*
2 * "$Id: color.c,v 1.11 2010/08/04 00:33:56 rlk Exp $"
3 *
4 *   Gimp-Print color module interface.
5 *
6 *   Copyright (C) 2003  Roger Leigh (rleigh@debian.org)
7 *
8 *   This program is free software; you can redistribute it and/or modify it
9 *   under the terms of the GNU General Public License as published by the Free
10 *   Software Foundation; either version 2 of the License, or (at your option)
11 *   any later version.
12 *
13 *   This program is distributed in the hope that it will be useful, but
14 *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 *   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 *   for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23/*
24 * This file must include only standard C header files.  The core code must
25 * compile on generic platforms that don't support glib, gimp, gtk, etc.
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include <gutenprint/gutenprint.h>
32#include "gutenprint-internal.h"
33#include <gutenprint/gutenprint-intl-internal.h>
34#include <string.h>
35#include <stdlib.h>
36
37
38static const char* stpi_color_namefunc(const void *item);
39static const char* stpi_color_long_namefunc(const void *item);
40
41static stp_list_t *color_list = NULL;
42
43
44static int
45stpi_init_color_list(void)
46{
47  if(color_list)
48    stp_list_destroy(color_list);
49  color_list = stp_list_create();
50  stp_list_set_namefunc(color_list, stpi_color_namefunc);
51  stp_list_set_long_namefunc(color_list, stpi_color_long_namefunc);
52  /* stp_list_set_sortfunc(color_list, stpi_color_sortfunc); */
53
54  return 0;
55}
56
57static inline void
58check_list(void)
59{
60  if (color_list == NULL)
61    {
62      stp_erprintf("No color drivers found: "
63		   "are STP_DATA_PATH and STP_MODULE_PATH correct?\n");
64      stpi_init_color_list();
65    }
66}
67
68
69
70int
71stp_color_count(void)
72{
73  if (color_list == NULL)
74    {
75      stp_erprintf("No color modules found: "
76		   "is STP_MODULE_PATH correct?\n");
77      stpi_init_color_list();
78    }
79  return stp_list_get_length(color_list);
80}
81
82#define CHECK_COLOR(c) STPI_ASSERT(c != NULL, NULL)
83
84const stp_color_t *
85stp_get_color_by_index(int idx)
86{
87  stp_list_item_t *color;
88
89  check_list();
90
91  color = stp_list_get_item_by_index(color_list, idx);
92  if (color == NULL)
93    return NULL;
94  return (const stp_color_t *) stp_list_item_get_data(color);
95}
96
97
98static const char *
99stpi_color_namefunc(const void *item)
100{
101  const stp_color_t *color = (const stp_color_t *) item;
102  CHECK_COLOR(color);
103  return color->short_name;
104}
105
106
107static const char *
108stpi_color_long_namefunc(const void *item)
109{
110  const stp_color_t *color = (const stp_color_t *) item;
111  CHECK_COLOR(color);
112  return color->long_name;
113}
114
115
116const char *
117stp_color_get_name(const stp_color_t *c)
118{
119  const stp_color_t *val = (const stp_color_t *) c;
120  CHECK_COLOR(val);
121  return val->short_name;
122}
123
124const char *
125stp_color_get_long_name(const stp_color_t *c)
126{
127  const stp_color_t *val = (const stp_color_t *) c;
128  CHECK_COLOR(val);
129  return gettext(val->long_name);
130}
131
132
133static const stp_colorfuncs_t *
134stpi_get_colorfuncs(const stp_color_t *c)
135{
136  const stp_color_t *val = (const stp_color_t *) c;
137  CHECK_COLOR(val);
138  return val->colorfuncs;
139}
140
141
142const stp_color_t *
143stp_get_color_by_name(const char *name)
144{
145  stp_list_item_t *color;
146
147  check_list();
148
149  color = stp_list_get_item_by_name(color_list, name);
150  if (!color)
151    return NULL;
152  return (const stp_color_t *) stp_list_item_get_data(color);
153}
154
155const stp_color_t *
156stp_get_color_by_colorfuncs(stp_colorfuncs_t *colorfuncs)
157{
158  stp_list_item_t *color_item;
159  stp_color_t *color;
160
161  check_list();
162
163  color_item = stp_list_get_start(color_list);
164  while (color_item)
165    {
166      color = (stp_color_t *) stp_list_item_get_data(color_item);
167      if (color->colorfuncs == colorfuncs)
168	return color;
169      color_item = stp_list_item_next(color_item);
170    }
171  return NULL;
172}
173
174
175int
176stp_color_init(stp_vars_t *v,
177	       stp_image_t *image,
178	       size_t steps)
179{
180  const stp_colorfuncs_t *colorfuncs =
181    stpi_get_colorfuncs(stp_get_color_by_name(stp_get_color_conversion(v)));
182  return colorfuncs->init(v, image, steps);
183}
184
185int
186stp_color_get_row(stp_vars_t *v,
187		  stp_image_t *image,
188		  int row,
189		  unsigned *zero_mask)
190{
191  const stp_colorfuncs_t *colorfuncs =
192    stpi_get_colorfuncs(stp_get_color_by_name(stp_get_color_conversion(v)));
193  return colorfuncs->get_row(v, image, row, zero_mask);
194}
195
196stp_parameter_list_t
197stp_color_list_parameters(const stp_vars_t *v)
198{
199  const stp_colorfuncs_t *colorfuncs =
200    stpi_get_colorfuncs(stp_get_color_by_name(stp_get_color_conversion(v)));
201  return colorfuncs->list_parameters(v);
202}
203
204void
205stp_color_describe_parameter(const stp_vars_t *v, const char *name,
206			     stp_parameter_t *description)
207{
208  const stp_colorfuncs_t *colorfuncs =
209    stpi_get_colorfuncs(stp_get_color_by_name(stp_get_color_conversion(v)));
210  colorfuncs->describe_parameter(v, name, description);
211}
212
213
214int
215stp_color_register(const stp_color_t *color)
216{
217  if (color_list == NULL)
218    {
219      stpi_init_color_list();
220      stp_deprintf(STP_DBG_COLORFUNC,
221		   "stpi_color_register(): initialising color_list...\n");
222    }
223
224  CHECK_COLOR(color);
225
226  if (color)
227    {
228      /* Add new color algorithm if it does not already exist */
229      if (stp_get_color_by_name(color->short_name) == NULL)
230	{
231	  stp_deprintf
232	    (STP_DBG_COLORFUNC,
233	     "stpi_color_register(): registered colour module \"%s\"\n",
234	     color->short_name);
235	  stp_list_item_create(color_list, NULL, color);
236	}
237    }
238
239  return 0;
240}
241
242int
243stp_color_unregister(const stp_color_t *color)
244{
245  stp_list_item_t *color_item;
246  stp_color_t *color_data;
247
248  if (color_list == NULL)
249    {
250      stpi_init_color_list();
251      stp_deprintf
252	(STP_DBG_COLORFUNC,
253	 "stpi_family_unregister(): initialising color_list...\n");
254    }
255
256  CHECK_COLOR(color);
257
258  color_item = stp_list_get_start(color_list);
259  while (color_item)
260    {
261      color_data = (stp_color_t *) stp_list_item_get_data(color_item);
262      if (strcmp(color->short_name, color_data->short_name) == 0)
263	{
264	  stp_deprintf
265	    (STP_DBG_COLORFUNC,
266	     "stpi_color_unregister(): unregistered colour module \"%s\"\n",
267	     color->short_name);
268	  stp_list_item_destroy(color_list, color_item);
269	  break;
270	}
271      color_item = stp_list_item_next(color_item);
272    }
273
274  return 0;
275}
276
277