| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include <config.h> |
| 23 |
#include "file-tools-enum-types.h" |
| 24 |
#include "gth-curve-preset.h" |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
enum { |
| 29 |
CHANGED, |
| 30 |
PRESET_CHANGED, |
| 31 |
LAST_SIGNAL |
| 32 |
}; |
| 33 |
|
| 34 |
|
| 35 |
typedef struct { |
| 36 |
GthPoints points[GTH_HISTOGRAM_N_CHANNELS]; |
| 37 |
int id; |
| 38 |
char *name; |
| 39 |
} Preset; |
| 40 |
|
| 41 |
|
| 42 |
struct _GthCurvePresetPrivate { |
| 43 |
GFile *file; |
| 44 |
GList *set; |
| 45 |
int next_id; |
| 46 |
}; |
| 47 |
|
| 48 |
|
| 49 |
static guint gth_curve_preset_signals[LAST_SIGNAL] = { 0 }; |
| 50 |
|
| 51 |
|
| 52 |
G_DEFINE_TYPE_WITH_CODE (GthCurvePreset, |
| 53 |
gth_curve_preset, |
| 54 |
G_TYPE_OBJECT, |
| 55 |
G_ADD_PRIVATE (GthCurvePreset)) |
| 56 |
|
| 57 |
|
| 58 |
static char * Channel_Names[GTH_HISTOGRAM_N_CHANNELS] = { "value", "red", "green", "blue", "alpha" }; |
| 59 |
|
| 60 |
|
| 61 |
static int |
| 62 |
get_channel_number (const char *name) |
| 63 |
{ |
| 64 |
int i; |
| 65 |
|
| 66 |
for (i = 0; i < GTH_HISTOGRAM_N_CHANNELS; i++) |
| 67 |
if (g_strcmp0 (Channel_Names[i], name) == 0) |
| 68 |
return i; |
| 69 |
|
| 70 |
return -1; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
static Preset * |
| 75 |
preset_new (int id) |
| 76 |
{ |
| 77 |
Preset *preset; |
| 78 |
int c; |
| 79 |
|
| 80 |
preset = g_new (Preset, 1); |
| 81 |
for (c = 0; c < GTH_HISTOGRAM_N_CHANNELS; c++) |
| 82 |
gth_points_init (&preset->points[c], 0); |
| 83 |
preset->id = id; |
| 84 |
preset->name = NULL; |
| 85 |
|
| 86 |
return preset; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
static DomElement * |
| 91 |
preset_create_element (Preset *preset, |
| 92 |
DomDocument *doc) |
| 93 |
{ |
| 94 |
DomElement *element; |
| 95 |
int c; |
| 96 |
|
| 97 |
element = dom_document_create_element (doc, "preset", "name", preset->name, NULL); |
| 98 |
|
| 99 |
for (c = 0; c < GTH_HISTOGRAM_N_CHANNELS; c++) { |
| 100 |
DomElement *channel; |
| 101 |
int i; |
| 102 |
|
| 103 |
channel = dom_document_create_element (doc, "channel", "type", Channel_Names[c], NULL); |
| 104 |
for (i = 0; i < preset->points[c].n; i++) { |
| 105 |
GthPoint *p = preset->points[c].p + i; |
| 106 |
char *x; |
| 107 |
char *y; |
| 108 |
|
| 109 |
x = g_strdup_printf ("%d", (int) p->x); |
| 110 |
y = g_strdup_printf ("%d", (int) p->y); |
| 111 |
dom_element_append_child (channel, dom_document_create_element (doc, "point", "x", x, "y", y, NULL)); |
| 112 |
|
| 113 |
g_free (x); |
| 114 |
g_free (y); |
| 115 |
} |
| 116 |
dom_element_append_child (element, channel); |
| 117 |
} |
| 118 |
|
| 119 |
return element; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
static void |
| 124 |
preset_load_from_element (Preset *preset, |
| 125 |
DomElement *element) |
| 126 |
{ |
| 127 |
int c; |
| 128 |
DomElement *node; |
| 129 |
|
| 130 |
g_return_if_fail (element != NULL); |
| 131 |
g_return_if_fail (g_strcmp0 (element->tag_name, "preset") == 0); |
| 132 |
|
| 133 |
g_free (preset->name); |
| 134 |
preset->name = g_strdup (dom_element_get_attribute (element, "name")); |
| 135 |
|
| 136 |
for (c = 0; c < GTH_HISTOGRAM_N_CHANNELS; c++) |
| 137 |
gth_points_dispose (&preset->points[c]); |
| 138 |
|
| 139 |
for (node = element->first_child; node; node = node->next_sibling) { |
| 140 |
if (g_strcmp0 (node->tag_name, "channel") == 0) { |
| 141 |
int n_channel; |
| 142 |
|
| 143 |
n_channel = get_channel_number (dom_element_get_attribute (node, "type")); |
| 144 |
if (n_channel > -1) { |
| 145 |
DomElement *point; |
| 146 |
|
| 147 |
for (point = node->first_child; point; point = point->next_sibling) { |
| 148 |
if (g_strcmp0 (point->tag_name, "point") == 0) { |
| 149 |
const char *sx, *sy; |
| 150 |
int x, y; |
| 151 |
|
| 152 |
sx = dom_element_get_attribute (point, "x"); |
| 153 |
sy = dom_element_get_attribute (point, "y"); |
| 154 |
|
| 155 |
if (sscanf (sx, "%d", &x) != 1) |
| 156 |
continue; |
| 157 |
if (sscanf (sy, "%d", &y) != 1) |
| 158 |
continue; |
| 159 |
|
| 160 |
gth_points_add_point (&preset->points[n_channel], x, y); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
static void |
| 170 |
preset_free (Preset *preset) |
| 171 |
{ |
| 172 |
int c; |
| 173 |
|
| 174 |
g_return_if_fail (preset != NULL); |
| 175 |
for (c = 0; c < GTH_HISTOGRAM_N_CHANNELS; c++) |
| 176 |
gth_points_dispose (&preset->points[c]); |
| 177 |
g_free (preset->name); |
| 178 |
g_free (preset); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
static void |
| 183 |
gth_curve_preset_finalize (GObject *obj) |
| 184 |
{ |
| 185 |
GthCurvePreset *self; |
| 186 |
|
| 187 |
self = GTH_CURVE_PRESET (obj); |
| 188 |
g_list_free_full (self->priv->set, (GDestroyNotify) preset_free); |
| 189 |
_g_object_unref (self->priv->file); |
| 190 |
|
| 191 |
G_OBJECT_CLASS (gth_curve_preset_parent_class)->finalize (obj); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
static void |
| 196 |
gth_curve_preset_class_init (GthCurvePresetClass *klass) |
| 197 |
{ |
| 198 |
GObjectClass *object_class; |
| 199 |
|
| 200 |
object_class = (GObjectClass*) klass; |
| 201 |
object_class->finalize = gth_curve_preset_finalize; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
gth_curve_preset_signals[CHANGED] = |
| 206 |
g_signal_new ("changed", |
| 207 |
G_TYPE_FROM_CLASS (klass), |
| 208 |
G_SIGNAL_RUN_LAST, |
| 209 |
G_STRUCT_OFFSET (GthCurvePresetClass, changed), |
| 210 |
NULL, NULL, |
| 211 |
g_cclosure_marshal_VOID__VOID, |
| 212 |
G_TYPE_NONE, |
| 213 |
0); |
| 214 |
gth_curve_preset_signals[PRESET_CHANGED] = |
| 215 |
g_signal_new ("preset-changed", |
| 216 |
G_TYPE_FROM_CLASS (klass), |
| 217 |
G_SIGNAL_RUN_LAST, |
| 218 |
G_STRUCT_OFFSET (GthCurvePresetClass, preset_changed), |
| 219 |
NULL, NULL, |
| 220 |
gth_marshal_VOID__ENUM_INT, |
| 221 |
G_TYPE_NONE, |
| 222 |
2, |
| 223 |
GTH_TYPE_PRESET_ACTION, |
| 224 |
G_TYPE_INT); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
static void |
| 229 |
gth_curve_preset_changed (GthCurvePreset *self) |
| 230 |
{ |
| 231 |
g_signal_emit (self, gth_curve_preset_signals[CHANGED], 0); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
static void |
| 236 |
gth_curve_preset_init (GthCurvePreset *self) |
| 237 |
{ |
| 238 |
self->priv = gth_curve_preset_get_instance_private (self); |
| 239 |
self->priv->set = NULL; |
| 240 |
self->priv->file = NULL; |
| 241 |
self->priv->next_id = 0; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
GthCurvePreset * |
| 246 |
gth_curve_preset_new_from_file (GFile *file) |
| 247 |
{ |
| 248 |
GthCurvePreset *self; |
| 249 |
DomDocument *doc; |
| 250 |
void *buffer; |
| 251 |
gsize size; |
| 252 |
|
| 253 |
self = (GthCurvePreset *) g_object_new (GTH_TYPE_CURVE_PRESET, NULL); |
| 254 |
self->priv->file = g_file_dup (file); |
| 255 |
|
| 256 |
doc = dom_document_new (); |
| 257 |
if (_g_file_load_in_buffer (self->priv->file, &buffer, &size, NULL, NULL)) { |
| 258 |
if (dom_document_load (doc, buffer, size, NULL)) { |
| 259 |
DomElement *presets = DOM_ELEMENT (doc)->first_child; |
| 260 |
|
| 261 |
if ((presets != NULL) && (g_strcmp0 (presets->tag_name, "presets") == 0)) { |
| 262 |
DomElement *node; |
| 263 |
|
| 264 |
for (node = presets->first_child; node; node = node->next_sibling) { |
| 265 |
if (g_strcmp0 (node->tag_name, "preset") == 0) { |
| 266 |
Preset *preset; |
| 267 |
|
| 268 |
preset = preset_new (self->priv->next_id++); |
| 269 |
preset_load_from_element (preset, node); |
| 270 |
self->priv->set = g_list_append (self->priv->set, preset); |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
g_free (buffer); |
| 276 |
} |
| 277 |
|
| 278 |
g_object_unref (doc); |
| 279 |
|
| 280 |
return self; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
int |
| 285 |
gth_curve_preset_get_size (GthCurvePreset *self) |
| 286 |
{ |
| 287 |
return g_list_length (self->priv->set); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
gboolean |
| 292 |
gth_curve_preset_get_nth (GthCurvePreset *self, |
| 293 |
int n, |
| 294 |
int *id, |
| 295 |
const char **name, |
| 296 |
GthPoints **points) |
| 297 |
{ |
| 298 |
Preset *preset; |
| 299 |
|
| 300 |
preset = g_list_nth_data (self->priv->set, n); |
| 301 |
if (preset == NULL) |
| 302 |
return FALSE; |
| 303 |
|
| 304 |
if (id) *id = preset->id; |
| 305 |
if (name) *name = preset->name; |
| 306 |
if (points) *points = preset->points; |
| 307 |
|
| 308 |
return TRUE; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
gboolean |
| 313 |
gth_curve_preset_get_by_id (GthCurvePreset *self, |
| 314 |
int id, |
| 315 |
const char **name, |
| 316 |
GthPoints **points) |
| 317 |
{ |
| 318 |
GList *scan; |
| 319 |
|
| 320 |
for (scan = self->priv->set; scan; scan = scan->next) { |
| 321 |
Preset *preset = scan->data; |
| 322 |
|
| 323 |
if (preset->id == id) { |
| 324 |
if (name) *name = preset->name; |
| 325 |
if (points) *points = preset->points; |
| 326 |
return TRUE; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
return FALSE; |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
int |
| 335 |
gth_curve_preset_get_pos (GthCurvePreset *self, |
| 336 |
int id) |
| 337 |
{ |
| 338 |
GList *scan; |
| 339 |
int pos; |
| 340 |
|
| 341 |
for (pos = 0, scan = self->priv->set; scan; scan = scan->next, pos++) { |
| 342 |
Preset *preset = scan->data; |
| 343 |
|
| 344 |
if (preset->id == id) |
| 345 |
return pos; |
| 346 |
} |
| 347 |
|
| 348 |
return -1; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
int |
| 353 |
gth_curve_preset_add (GthCurvePreset *self, |
| 354 |
const char *name, |
| 355 |
GthPoints *points) |
| 356 |
{ |
| 357 |
Preset *preset; |
| 358 |
int c; |
| 359 |
|
| 360 |
preset = preset_new (self->priv->next_id++); |
| 361 |
preset->name = g_strdup (name); |
| 362 |
for (c = 0; c < GTH_HISTOGRAM_N_CHANNELS; c++) |
| 363 |
gth_points_copy (points + c, &preset->points[c]); |
| 364 |
self->priv->set = g_list_append (self->priv->set, preset); |
| 365 |
gth_curve_preset_changed (self); |
| 366 |
g_signal_emit (self, gth_curve_preset_signals[PRESET_CHANGED], 0, GTH_PRESET_ACTION_ADDED, preset->id); |
| 367 |
|
| 368 |
return preset->id; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
static int |
| 373 |
compare_preset_by_id (Preset *a, |
| 374 |
gpointer id_p) |
| 375 |
{ |
| 376 |
int id = GPOINTER_TO_INT (id_p); |
| 377 |
return (a->id == id) ? 0 : (a->id > id) ? 1 : -1; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
void |
| 382 |
gth_curve_preset_remove (GthCurvePreset *self, |
| 383 |
int id) |
| 384 |
{ |
| 385 |
GList *link; |
| 386 |
|
| 387 |
link = g_list_find_custom (self->priv->set, GINT_TO_POINTER (id), (GCompareFunc) compare_preset_by_id); |
| 388 |
if (link == NULL) |
| 389 |
return; |
| 390 |
|
| 391 |
self->priv->set = g_list_remove_link (self->priv->set, link); |
| 392 |
gth_curve_preset_changed (self); |
| 393 |
g_signal_emit (self, gth_curve_preset_signals[PRESET_CHANGED], 0, GTH_PRESET_ACTION_REMOVED, id); |
| 394 |
|
| 395 |
g_list_free_full (link, (GDestroyNotify) preset_free); |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
void |
| 400 |
gth_curve_preset_rename (GthCurvePreset *self, |
| 401 |
int id, |
| 402 |
const char *new_name) |
| 403 |
{ |
| 404 |
GList *link; |
| 405 |
Preset *preset; |
| 406 |
|
| 407 |
link = g_list_find_custom (self->priv->set, GINT_TO_POINTER (id), (GCompareFunc) compare_preset_by_id); |
| 408 |
g_return_if_fail (link != NULL); |
| 409 |
|
| 410 |
preset = link->data; |
| 411 |
g_free (preset->name); |
| 412 |
preset->name = g_strdup (new_name); |
| 413 |
|
| 414 |
gth_curve_preset_changed (self); |
| 415 |
g_signal_emit (self, gth_curve_preset_signals[PRESET_CHANGED], 0, GTH_PRESET_ACTION_RENAMED, id); |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
void |
| 420 |
gth_curve_preset_change_order (GthCurvePreset *self, |
| 421 |
GList *id_list) |
| 422 |
{ |
| 423 |
GList *set, *scan; |
| 424 |
|
| 425 |
set = NULL; |
| 426 |
for (scan = id_list; scan; scan = scan->next) { |
| 427 |
int id = GPOINTER_TO_INT (scan->data); |
| 428 |
GList *link; |
| 429 |
|
| 430 |
link = g_list_find_custom (self->priv->set, GINT_TO_POINTER (id), (GCompareFunc) compare_preset_by_id); |
| 431 |
g_return_if_fail (link != NULL); |
| 432 |
|
| 433 |
set = g_list_prepend (set, link->data); |
| 434 |
} |
| 435 |
set = g_list_reverse (set); |
| 436 |
|
| 437 |
g_list_free (self->priv->set); |
| 438 |
self->priv->set = set; |
| 439 |
|
| 440 |
gth_curve_preset_changed (self); |
| 441 |
g_signal_emit (self, gth_curve_preset_signals[PRESET_CHANGED], 0, GTH_PRESET_ACTION_CHANGED_ORDER, -1); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
GList * |
| 446 |
gth_curve_preset_get_order (GthCurvePreset *self) |
| 447 |
{ |
| 448 |
GList *id_list, *scan; |
| 449 |
|
| 450 |
id_list = NULL; |
| 451 |
for (scan = self->priv->set; scan; scan = scan->next) { |
| 452 |
Preset *preset = scan->data; |
| 453 |
id_list = g_list_prepend (id_list, GINT_TO_POINTER (preset->id)); |
| 454 |
} |
| 455 |
|
| 456 |
return g_list_reverse (id_list); |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
gboolean |
| 461 |
gth_curve_preset_save (GthCurvePreset *self, |
| 462 |
GError **error) |
| 463 |
{ |
| 464 |
DomDocument *doc; |
| 465 |
DomElement *curves; |
| 466 |
GList *scan; |
| 467 |
char *buffer; |
| 468 |
gsize size; |
| 469 |
gboolean result; |
| 470 |
|
| 471 |
g_return_val_if_fail (self->priv->file != NULL, FALSE); |
| 472 |
|
| 473 |
doc = dom_document_new (); |
| 474 |
curves = dom_document_create_element (doc, "presets", NULL); |
| 475 |
for (scan = self->priv->set; scan; scan = scan->next) { |
| 476 |
Preset *preset = scan->data; |
| 477 |
dom_element_append_child (curves, preset_create_element (preset, doc)); |
| 478 |
} |
| 479 |
dom_element_append_child (DOM_ELEMENT (doc), curves); |
| 480 |
|
| 481 |
buffer = dom_document_dump (doc, &size); |
| 482 |
result = _g_file_write (self->priv->file, FALSE, G_FILE_CREATE_NONE, buffer, size, NULL, error); |
| 483 |
|
| 484 |
g_free (buffer); |
| 485 |
g_object_unref (doc); |
| 486 |
|
| 487 |
return result; |
| 488 |
} |