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