64
59
gint *popup_height);
65
60
static TempBuf * gimp_palette_get_new_preview (GimpViewable *viewable,
68
64
static gchar * gimp_palette_get_description (GimpViewable *viewable,
70
static gboolean gimp_palette_save (GimpData *data,
72
66
static gchar * gimp_palette_get_extension (GimpData *data);
73
static GimpData * gimp_palette_duplicate (GimpData *data,
74
gboolean stingy_memory_use);
67
static GimpData * gimp_palette_duplicate (GimpData *data);
76
69
static void gimp_palette_entry_free (GimpPaletteEntry *entry);
79
/* private variables */
81
static GimpDataClass *parent_class = NULL;
85
gimp_palette_get_type (void)
87
static GType palette_type = 0;
91
static const GTypeInfo palette_info =
93
sizeof (GimpPaletteClass),
95
(GBaseFinalizeFunc) NULL,
96
(GClassInitFunc) gimp_palette_class_init,
97
NULL, /* class_finalize */
98
NULL, /* class_data */
101
(GInstanceInitFunc) gimp_palette_init,
104
palette_type = g_type_register_static (GIMP_TYPE_DATA,
70
static gint64 gimp_palette_entry_get_memsize(GimpPaletteEntry *entry,
74
G_DEFINE_TYPE (GimpPalette, gimp_palette, GIMP_TYPE_DATA)
76
#define parent_class gimp_palette_parent_class
113
80
gimp_palette_class_init (GimpPaletteClass *klass)
333
289
return standard_palette;
337
gimp_palette_load (const gchar *filename,
338
gboolean stingy_memory_use,
341
GimpPalette *palette;
342
GimpPaletteEntry *entry;
349
g_return_val_if_fail (filename != NULL, NULL);
350
g_return_val_if_fail (g_path_is_absolute (filename), NULL);
351
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
355
file = fopen (filename, "r");
359
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
360
_("Could not open '%s' for reading: %s"),
361
gimp_filename_to_utf8 (filename), g_strerror (errno));
367
fread (str, 13, 1, file);
370
if (strcmp (str, "GIMP Palette\n"))
372
/* bad magic, but maybe it has \r\n at the end of lines? */
373
if (!strcmp (str, "GIMP Palette\r"))
374
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
375
_("Fatal parse error in palette file '%s': "
376
"Missing magic header.\n"
377
"Does this file need converting from DOS?"),
378
gimp_filename_to_utf8 (filename));
380
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
381
_("Fatal parse error in palette file '%s': "
382
"Missing magic header."),
383
gimp_filename_to_utf8 (filename));
390
palette = g_object_new (GIMP_TYPE_PALETTE, NULL);
392
if (! fgets (str, sizeof (str), file))
394
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
395
_("Fatal parse error in palette file '%s': "
396
"Read error in line %d."),
397
gimp_filename_to_utf8 (filename), linenum);
399
g_object_unref (palette);
405
if (! strncmp (str, "Name: ", strlen ("Name: ")))
409
utf8 = gimp_any_to_utf8 (&str[strlen ("Name: ")], -1,
410
_("Invalid UTF-8 string in palette file '%s'"),
411
gimp_filename_to_utf8 (filename));
414
gimp_object_set_name (GIMP_OBJECT (palette), utf8);
417
if (! fgets (str, sizeof (str), file))
419
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
420
_("Fatal parse error in palette file '%s': "
421
"Read error in line %d."),
422
gimp_filename_to_utf8 (filename), linenum);
424
g_object_unref (palette);
430
if (! strncmp (str, "Columns: ", strlen ("Columns: ")))
434
columns = atoi (g_strstrip (&str[strlen ("Columns: ")]));
436
if (columns < 0 || columns > 256)
438
g_message (_("Reading palette file '%s': "
439
"Invalid number of columns in line %d. "
440
"Using default value."),
441
gimp_filename_to_utf8 (filename), linenum);
445
palette->n_columns = columns;
447
if (! fgets (str, sizeof (str), file))
449
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
450
_("Fatal parse error in palette file '%s': "
451
"Read error in line %d."),
452
gimp_filename_to_utf8 (filename), linenum);
454
g_object_unref (palette);
461
else /* old palette format */
466
basename = g_path_get_basename (filename);
468
utf8 = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
471
gimp_object_set_name (GIMP_OBJECT (palette), utf8);
475
while (! feof (file))
479
tok = strtok (str, " \t");
483
/* maybe we should just abort? */
484
g_message (_("Reading palette file '%s': "
485
"Missing RED component in line %d."),
486
gimp_filename_to_utf8 (filename), linenum);
488
tok = strtok (NULL, " \t");
492
g_message (_("Reading palette '%s': "
493
"Missing GREEN component in line %d."),
494
gimp_filename_to_utf8 (filename), linenum);
496
tok = strtok (NULL, " \t");
500
g_message (_("Reading palette file '%s': "
501
"Missing BLUE component in line %d."),
502
gimp_filename_to_utf8 (filename), linenum);
505
tok = strtok (NULL, "\n");
507
if (r < 0 || r > 255 ||
510
g_message (_("Reading palette file '%s': "
511
"RGB value out of range in line %d."),
512
gimp_filename_to_utf8 (filename), linenum);
514
/* don't call gimp_palette_add_entry here, it's rather inefficient */
515
entry = g_new0 (GimpPaletteEntry, 1);
517
gimp_rgba_set_uchar (&entry->color,
523
entry->name = g_strdup (tok ? tok : _("Untitled"));
524
entry->position = palette->n_colors;
526
palette->colors = g_list_prepend (palette->colors, entry);
530
if (! fgets (str, sizeof (str), file))
535
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
536
_("Fatal parse error in palette file '%s': "
537
"Read error in line %d."),
538
gimp_filename_to_utf8 (filename), linenum);
540
g_object_unref (palette);
549
palette->colors = g_list_reverse (palette->colors);
551
return g_list_prepend (NULL, palette);
555
gimp_palette_save (GimpData *data,
558
GimpPalette *palette = GIMP_PALETTE (data);
562
file = fopen (data->filename, "w");
566
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
567
_("Could not open '%s' for writing: %s"),
568
gimp_filename_to_utf8 (data->filename),
573
fprintf (file, "GIMP Palette\n");
574
fprintf (file, "Name: %s\n", GIMP_OBJECT (palette)->name);
575
fprintf (file, "Columns: %d\n#\n", CLAMP (palette->n_columns, 0, 256));
577
for (list = palette->colors; list; list = g_list_next (list))
579
GimpPaletteEntry *entry = list->data;
582
gimp_rgb_get_uchar (&entry->color, &r, &g, &b);
584
fprintf (file, "%3d %3d %3d\t%s\n", r, g, b, entry->name);
593
293
gimp_palette_get_extension (GimpData *data)
671
390
entry->position = pos++;
674
/* will make the palette dirty too */
675
gimp_object_name_changed (GIMP_OBJECT (palette));
393
gimp_data_dirty (GIMP_DATA (palette));
680
gimp_palette_set_n_columns (GimpPalette *palette,
398
gimp_palette_set_columns (GimpPalette *palette,
683
401
g_return_if_fail (GIMP_IS_PALETTE (palette));
685
n_columns = CLAMP (n_columns, 0, 64);
403
columns = CLAMP (columns, 0, 64);
687
if (palette->n_columns != n_columns)
405
if (palette->n_columns != columns)
689
palette->n_columns = n_columns;
407
palette->n_columns = columns;
691
409
gimp_data_dirty (GIMP_DATA (palette));
696
gimp_palette_get_n_columns (GimpPalette *palette)
414
gimp_palette_get_columns (GimpPalette *palette)
698
416
g_return_val_if_fail (GIMP_IS_PALETTE (palette), 0);
700
418
return palette->n_columns;
422
gimp_palette_find_entry (GimpPalette *palette,
423
const GimpRGB *color,
424
GimpPaletteEntry *start_from)
426
GimpPaletteEntry *entry;
428
g_return_val_if_fail (GIMP_IS_PALETTE (palette), NULL);
429
g_return_val_if_fail (color != NULL, NULL);
430
g_return_val_if_fail (palette->n_colors > 0, NULL);
436
/* search from the start */
438
for (list = palette->colors; list; list = g_list_next (list))
440
entry = (GimpPaletteEntry *) list->data;
441
if (gimp_rgb_distance (&entry->color, color) < EPSILON)
445
else if (gimp_rgb_distance (&start_from->color, color) < EPSILON)
451
GList *old = g_list_find (palette->colors, start_from);
455
g_return_val_if_fail (old != NULL, NULL);
460
/* proximity-based search */
466
entry = (GimpPaletteEntry *) next->data;
467
if (gimp_rgb_distance (&entry->color, color) < EPSILON)
475
entry = (GimpPaletteEntry *) prev->data;
476
if (gimp_rgb_distance (&entry->color, color) < EPSILON)
704
488
/* private functions */