summaryrefslogtreecommitdiff
path: root/src/main/color.c
blob: 8a964c35ba72c87c7ed8d2608bdf98f99eae3701 (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
/*
 * "$Id: color.c,v 1.10 2004/09/17 18:38:17 rleigh Exp $"
 *
 *   Gimp-Print color module interface.
 *
 *   Copyright (C) 2003  Roger Leigh (rleigh@debian.org)
 *
 *   This program is free software; you can redistribute it and/or modify it
 *   under the terms of the GNU General Public License as published by the Free
 *   Software Foundation; either version 2 of the License, or (at your option)
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful, but
 *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *   for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/*
 * This file must include only standard C header files.  The core code must
 * compile on generic platforms that don't support glib, gimp, gtk, etc.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gutenprint/gutenprint.h>
#include "gutenprint-internal.h"
#include <gutenprint/gutenprint-intl-internal.h>
#include <string.h>
#include <stdlib.h>


static const char* stpi_color_namefunc(const void *item);
static const char* stpi_color_long_namefunc(const void *item);

static stp_list_t *color_list = NULL;


static int
stpi_init_color_list(void)
{
  if(color_list)
    stp_list_destroy(color_list);
  color_list = stp_list_create();
  stp_list_set_namefunc(color_list, stpi_color_namefunc);
  stp_list_set_long_namefunc(color_list, stpi_color_long_namefunc);
  /* stp_list_set_sortfunc(color_list, stpi_color_sortfunc); */

  return 0;
}

static inline void
check_list(void)
{
  if (color_list == NULL)
    {
      stp_erprintf("No color drivers found: "
		   "are STP_DATA_PATH and STP_MODULE_PATH correct?\n");
      stpi_init_color_list();
    }
}



int
stp_color_count(void)
{
  if (color_list == NULL)
    {
      stp_erprintf("No color modules found: "
		   "is STP_MODULE_PATH correct?\n");
      stpi_init_color_list();
    }
  return stp_list_get_length(color_list);
}


static inline void
check_color(const stp_color_t *c)
{
  if (c == NULL)
    {
      stp_erprintf("Null stp_color_t! Please report this bug.\n");
      stp_abort();
    }
}


const stp_color_t *
stp_get_color_by_index(int idx)
{
  stp_list_item_t *color;

  check_list();

  color = stp_list_get_item_by_index(color_list, idx);
  if (color == NULL)
    return NULL;
  return (const stp_color_t *) stp_list_item_get_data(color);
}


static const char *
stpi_color_namefunc(const void *item)
{
  const stp_color_t *color = (const stp_color_t *) item;
  check_color(color);
  return color->short_name;
}


static const char *
stpi_color_long_namefunc(const void *item)
{
  const stp_color_t *color = (const stp_color_t *) item;
  check_color(color);
  return color->long_name;
}


const char *
stp_color_get_name(const stp_color_t *c)
{
  const stp_color_t *val = (const stp_color_t *) c;
  check_color(val);
  return val->short_name;
}

const char *
stp_color_get_long_name(const stp_color_t *c)
{
  const stp_color_t *val = (const stp_color_t *) c;
  check_color(val);
  return gettext(val->long_name);
}


static const stp_colorfuncs_t *
stpi_get_colorfuncs(const stp_color_t *c)
{
  const stp_color_t *val = (const stp_color_t *) c;
  check_color(val);
  return val->colorfuncs;
}


const stp_color_t *
stp_get_color_by_name(const char *name)
{
  stp_list_item_t *color;

  check_list();

  color = stp_list_get_item_by_name(color_list, name);
  if (!color)
    return NULL;
  return (const stp_color_t *) stp_list_item_get_data(color);
}

const stp_color_t *
stp_get_color_by_colorfuncs(stp_colorfuncs_t *colorfuncs)
{
  stp_list_item_t *color_item;
  stp_color_t *color;

  check_list();

  color_item = stp_list_get_start(color_list);
  while (color_item)
    {
      color = (stp_color_t *) stp_list_item_get_data(color_item);
      if (color->colorfuncs == colorfuncs)
	return color;
      color_item = stp_list_item_next(color_item);
    }
  return NULL;
}


int
stp_color_init(stp_vars_t *v,
	       stp_image_t *image,
	       size_t steps)
{
  const stp_colorfuncs_t *colorfuncs =
    stpi_get_colorfuncs(stp_get_color_by_name(stp_get_color_conversion(v)));
  return colorfuncs->init(v, image, steps);
}

int
stp_color_get_row(stp_vars_t *v,
		  stp_image_t *image,
		  int row,
		  unsigned *zero_mask)
{
  const stp_colorfuncs_t *colorfuncs =
    stpi_get_colorfuncs(stp_get_color_by_name(stp_get_color_conversion(v)));
  return colorfuncs->get_row(v, image, row, zero_mask);
}

stp_parameter_list_t
stp_color_list_parameters(const stp_vars_t *v)
{
  const stp_colorfuncs_t *colorfuncs =
    stpi_get_colorfuncs(stp_get_color_by_name(stp_get_color_conversion(v)));
  return colorfuncs->list_parameters(v);
}

void
stp_color_describe_parameter(const stp_vars_t *v, const char *name,
			     stp_parameter_t *description)
{
  const stp_colorfuncs_t *colorfuncs =
    stpi_get_colorfuncs(stp_get_color_by_name(stp_get_color_conversion(v)));
  colorfuncs->describe_parameter(v, name, description);
}


int
stp_color_register(const stp_color_t *color)
{
  if (color_list == NULL)
    {
      stpi_init_color_list();
      stp_deprintf(STP_DBG_COLORFUNC,
		   "stpi_color_register(): initialising color_list...\n");
    }

  check_color(color);

  if (color)
    {
      /* Add new color algorithm if it does not already exist */
      if (stp_get_color_by_name(color->short_name) == NULL)
	{
	  stp_deprintf
	    (STP_DBG_COLORFUNC,
	     "stpi_color_register(): registered colour module \"%s\"\n",
	     color->short_name);
	  stp_list_item_create(color_list, NULL, color);
	}
    }

  return 0;
}

int
stp_color_unregister(const stp_color_t *color)
{
  stp_list_item_t *color_item;
  stp_color_t *color_data;

  if (color_list == NULL)
    {
      stpi_init_color_list();
      stp_deprintf
	(STP_DBG_COLORFUNC,
	 "stpi_family_unregister(): initialising color_list...\n");
    }

  check_color(color);

  color_item = stp_list_get_start(color_list);
  while (color_item)
    {
      color_data = (stp_color_t *) stp_list_item_get_data(color_item);
      if (strcmp(color->short_name, color_data->short_name) == 0)
	{
	  stp_deprintf
	    (STP_DBG_COLORFUNC,
	     "stpi_color_unregister(): unregistered colour module \"%s\"\n",
	     color->short_name);
	  stp_list_item_destroy(color_list, color_item);
	  break;
	}
      color_item = stp_list_item_next(color_item);
    }

  return 0;
}