226
217
(guchar) color_tab->b + (color_tab->b_adj / color_tab->count),
229
gimp_palette_add_entry (palette, lab, &color);
220
gimp_palette_add_entry (palette, -1, lab, &color);
234
225
static GimpPalette *
235
gimp_palette_import_image_make_palette (GHashTable *h_array,
236
const gchar *palette_name,
226
gimp_palette_import_make_palette (GHashTable *table,
227
const gchar *palette_name,
239
GimpPalette *palette;
240
GSList *sorted_list = NULL;
242
g_hash_table_foreach (h_array, gimp_palette_import_create_sorted_list,
244
sorted_list = g_slist_sort (sorted_list, gimp_palette_import_sort_colors);
246
palette = GIMP_PALETTE (gimp_palette_new (palette_name, FALSE));
248
g_object_set_data (G_OBJECT (palette), "import_n_colors",
249
GINT_TO_POINTER (n_colors));
251
g_slist_foreach (sorted_list, gimp_palette_import_create_image_palette,
254
g_object_set_data (G_OBJECT (palette), "import_n_colors", NULL);
230
GimpPalette *palette = GIMP_PALETTE (gimp_palette_new (palette_name));
236
g_hash_table_foreach (table, gimp_palette_import_create_list, &list);
237
list = g_slist_sort (list, gimp_palette_import_sort_colors);
239
g_object_set_data (G_OBJECT (palette), "import-n-colors",
240
GINT_TO_POINTER (n_colors));
242
g_slist_foreach (list, gimp_palette_import_create_image_palette, palette);
244
g_object_set_data (G_OBJECT (palette), "import-n-colors", NULL);
256
246
/* Free up used memory
257
247
* Note the same structure is on both the hash list and the sorted
258
248
* list. So only delete it once.
260
g_hash_table_destroy (h_array);
262
g_slist_foreach (sorted_list, (GFunc) g_free, NULL);
264
g_slist_free (sorted_list);
250
g_hash_table_destroy (table);
252
g_slist_foreach (list, (GFunc) g_free, NULL);
270
gimp_palette_import_from_image (GimpImage *gimage,
271
const gchar *palette_name,
260
gimp_palette_import_extract (GimpImage *image,
261
GimpPickable *pickable,
264
gboolean selection_only,
278
guchar rgb[MAX_CHANNELS];
279
guchar rgb_real[MAX_CHANNELS];
280
gboolean has_alpha, indexed;
285
GimpImageType d_type;
286
GHashTable *store_array = NULL;
288
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
289
g_return_val_if_fail (palette_name != NULL, NULL);
290
g_return_val_if_fail (n_colors > 1, NULL);
291
g_return_val_if_fail (threshold > 0, NULL);
293
gimp_projection_finish_draw (gimage->projection);
294
gimp_projection_flush_now (gimage->projection);
296
/* Get the image information */
297
d_type = gimp_projection_get_image_type (gimage->projection);
298
bytes = gimp_projection_get_bytes (gimage->projection);
299
has_alpha = GIMP_IMAGE_TYPE_HAS_ALPHA (d_type);
300
indexed = GIMP_IMAGE_TYPE_IS_INDEXED (d_type);
301
width = gimage->width;
302
height = gimage->height;
304
pixel_region_init (&imagePR, gimp_projection_get_tiles (gimage->projection),
305
0, 0, width, height, FALSE);
309
/* iterate over the entire image */
310
for (pr = pixel_regions_register (1, &imagePR);
275
PixelRegion mask_region;
276
PixelRegion *maskPR = NULL;
278
GHashTable *colors = NULL;
280
tiles = gimp_pickable_get_tiles (pickable);
281
type = gimp_pickable_get_image_type (pickable);
283
pixel_region_init (®ion, tiles, x, y, width, height, FALSE);
285
if (selection_only &&
286
! gimp_channel_is_empty (gimp_image_get_mask (image)))
288
pixel_region_init (&mask_region,
289
GIMP_DRAWABLE (gimp_image_get_mask (image))->tiles,
290
x + pickable_off_x, y + pickable_off_y,
294
maskPR = &mask_region;
297
for (pr = pixel_regions_register (maskPR ? 2 : 1, ®ion, maskPR);
312
299
pr = pixel_regions_process (pr))
314
image_data = imagePR.data;
316
for (i = 0; i < imagePR.h; i++)
320
for (j = 0; j < imagePR.w; j++)
322
/* Get the rgb values for the color */
323
gimp_image_get_color (gimage, d_type, idata, rgb);
324
memcpy (rgb_real, rgb, MAX_CHANNELS); /* Structure copy */
326
rgb[0] = (rgb[0] / threshold) * threshold;
327
rgb[1] = (rgb[1] / threshold) * threshold;
328
rgb[2] = (rgb[2] / threshold) * threshold;
331
gimp_palette_import_store_colors (store_array, rgb, rgb_real,
337
image_data += imagePR.rowstride;
341
return gimp_palette_import_image_make_palette (store_array,
301
const guchar *data = region.data;
302
const guchar *mask_data = NULL;
306
mask_data = maskPR->data;
308
for (i = 0; i < region.h; i++)
310
const guchar *idata = data;
311
const guchar *mdata = mask_data;
313
for (j = 0; j < region.w; j++)
315
if (! mdata || *mdata)
317
guchar rgba[MAX_CHANNELS];
319
gimp_image_get_color (image, type, idata, rgba);
321
/* ignore completely transparent pixels */
324
guchar rgb_real[MAX_CHANNELS];
326
memcpy (rgb_real, rgba, MAX_CHANNELS);
328
rgba[0] = (rgba[0] / threshold) * threshold;
329
rgba[1] = (rgba[1] / threshold) * threshold;
330
rgba[2] = (rgba[2] / threshold) * threshold;
332
colors = gimp_palette_import_store_colors (colors,
338
idata += region.bytes;
341
mdata += maskPR->bytes;
344
data += region.rowstride;
347
mask_data += maskPR->rowstride;
355
gimp_palette_import_from_image (GimpImage *image,
356
const gchar *palette_name,
359
gboolean selection_only)
365
g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
366
g_return_val_if_fail (palette_name != NULL, NULL);
367
g_return_val_if_fail (n_colors > 1, NULL);
368
g_return_val_if_fail (threshold > 0, NULL);
370
gimp_pickable_flush (GIMP_PICKABLE (image->projection));
374
gimp_channel_bounds (gimp_image_get_mask (image),
375
&x, &y, &width, &height);
384
width = image->width;
385
height = image->height;
388
colors = gimp_palette_import_extract (image,
389
GIMP_PICKABLE (image->projection),
393
n_colors, threshold);
395
return gimp_palette_import_make_palette (colors, palette_name, n_colors);
346
399
/* create a palette from an indexed image **********************************/
349
gimp_palette_import_from_indexed_image (GimpImage *gimage,
350
const gchar *palette_name)
402
gimp_palette_import_from_indexed_image (GimpImage *image,
403
const gchar *palette_name)
352
405
GimpPalette *palette;
356
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
357
g_return_val_if_fail (gimp_image_base_type (gimage) == GIMP_INDEXED, NULL);
409
g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
410
g_return_val_if_fail (gimp_image_base_type (image) == GIMP_INDEXED, NULL);
358
411
g_return_val_if_fail (palette_name != NULL, NULL);
360
palette = GIMP_PALETTE (gimp_palette_new (palette_name, FALSE));
413
palette = GIMP_PALETTE (gimp_palette_new (palette_name));
362
for (count= 0; count < gimage->num_cols; ++count)
415
for (count = 0; count < image->num_cols; ++count)
419
g_snprintf (name, sizeof (name), _("Index %d"), count);
364
421
gimp_rgba_set_uchar (&color,
365
gimage->cmap[count * 3],
366
gimage->cmap[count * 3 + 1],
367
gimage->cmap[count * 3 + 2],
422
image->cmap[count * 3 + 0],
423
image->cmap[count * 3 + 1],
424
image->cmap[count * 3 + 2],
370
gimp_palette_add_entry (palette, NULL, &color);
427
gimp_palette_add_entry (palette, -1, name, &color);
434
/* create a palette from a drawable ****************************************/
437
gimp_palette_import_from_drawable (GimpDrawable *drawable,
438
const gchar *palette_name,
441
gboolean selection_only)
443
GHashTable *colors = NULL;
448
g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
449
g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), NULL);
450
g_return_val_if_fail (palette_name != NULL, NULL);
451
g_return_val_if_fail (n_colors > 1, NULL);
452
g_return_val_if_fail (threshold > 0, NULL);
456
if (! gimp_drawable_mask_intersect (drawable, &x, &y, &width, &height))
463
width = gimp_item_width (GIMP_ITEM (drawable));
464
height = gimp_item_height (GIMP_ITEM (drawable));
467
gimp_item_offsets (GIMP_ITEM (drawable), &off_x, &off_y);
470
gimp_palette_import_extract (gimp_item_get_image (GIMP_ITEM (drawable)),
471
GIMP_PICKABLE (drawable),
475
n_colors, threshold);
477
return gimp_palette_import_make_palette (colors, palette_name, n_colors);
377
481
/* create a palette from a file **********************************/
381
GIMP_PALETTE_FILE_FORMAT_UNKNOWN,
382
GIMP_PALETTE_FILE_FORMAT_GPL, /* GIMP palette file */
383
GIMP_PALETTE_FILE_FORMAT_PAL, /* RIFF palette file */
384
GIMP_PALETTE_FILE_FORMAT_ACT /* Photoshop binary color palette file */
385
} GimpPaletteFileFormat;
387
static GimpPaletteFileFormat
388
gimp_palette_detect_file_format (const gchar *filename)
390
GimpPaletteFileFormat format = GIMP_PALETTE_FILE_FORMAT_UNKNOWN;
393
struct stat file_stat;
395
fd = open (filename, O_RDONLY);
398
if (read (fd, header, sizeof (header)) == sizeof (header))
400
if (strncmp (header + 0, "RIFF", 4) == 0 &&
401
strncmp (header + 8, "PAL data", 8) == 0)
403
format = GIMP_PALETTE_FILE_FORMAT_PAL;
406
if (strncmp (header, "GIMP Palette", 12) == 0)
408
format = GIMP_PALETTE_FILE_FORMAT_GPL;
412
if (fstat (fd, &file_stat) >= 0)
414
if (file_stat.st_size == 768)
415
format = GIMP_PALETTE_FILE_FORMAT_ACT;
425
484
gimp_palette_import_from_file (const gchar *filename,
426
485
const gchar *palette_name,
429
GimpPalette *palette = NULL;
433
guchar color_bytes[4];
488
GList *palette_list = NULL;
435
490
g_return_val_if_fail (filename != NULL, NULL);
436
491
g_return_val_if_fail (palette_name != NULL, NULL);
437
492
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
439
fd = open (filename, O_RDONLY);
443
G_FILE_ERROR, g_file_error_from_errno (errno),
444
_("Opening '%s' failed: %s"),
445
gimp_filename_to_utf8 (filename), g_strerror (errno));
449
switch (gimp_palette_detect_file_format (filename))
494
switch (gimp_palette_load_detect_format (filename))
451
496
case GIMP_PALETTE_FILE_FORMAT_GPL:
452
palette_list = gimp_palette_load (filename, FALSE, error);
455
palette = palette_list->data;
456
g_list_free (palette_list);
497
palette_list = gimp_palette_load (filename, error);
460
500
case GIMP_PALETTE_FILE_FORMAT_ACT:
461
palette = GIMP_PALETTE (gimp_palette_new (palette_name, FALSE));
463
while (read (fd, color_bytes, 3) == 3)
465
gimp_rgba_set_uchar (&color,
470
gimp_palette_add_entry (palette, NULL, &color);
474
case GIMP_PALETTE_FILE_FORMAT_PAL:
475
palette = GIMP_PALETTE (gimp_palette_new (palette_name, FALSE));
477
lseek (fd, 28, SEEK_SET);
479
color_bytes, sizeof (color_bytes)) == sizeof (color_bytes))
481
gimp_rgba_set_uchar (&color,
486
gimp_palette_add_entry (palette, NULL, &color);
501
palette_list = gimp_palette_load_act (filename, error);
504
case GIMP_PALETTE_FILE_FORMAT_RIFF_PAL:
505
palette_list = gimp_palette_load_riff (filename, error);
508
case GIMP_PALETTE_FILE_FORMAT_PSP_PAL:
509
palette_list = gimp_palette_load_psp (filename, error);
491
513
g_set_error (error,
493
_("Unknown type of palette file:\n%s"),
514
GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
515
_("Unknown type of palette file: %s"),
494
516
gimp_filename_to_utf8 (filename));
522
GimpPalette *palette = g_object_ref (palette_list->data);
524
gimp_object_set_name (GIMP_OBJECT (palette), palette_name);
526
g_list_foreach (palette_list, (GFunc) g_object_unref, NULL);
527
g_list_free (palette_list);