gimppalette-save.c 12.6 KB
Newer Older
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
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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.
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <gtk/gtk.h>

30 31
#include "libgimpcolor/gimpcolor.h"

Michael Natterer's avatar
Michael Natterer committed
32
#include "core-types.h"
33

Michael Natterer's avatar
Michael Natterer committed
34 35
#include "base/temp-buf.h"

36
#include "gimppalette.h"
Michael Natterer's avatar
Michael Natterer committed
37

38 39 40 41 42 43 44
#include "gimprc.h"

#include "libgimp/gimpintl.h"


/*  local function prototypes  */

45 46 47 48 49 50 51 52
static void       gimp_palette_class_init       (GimpPaletteClass  *klass);
static void       gimp_palette_init             (GimpPalette       *palette);
static void       gimp_palette_destroy          (GtkObject         *object);
static TempBuf  * gimp_palette_get_new_preview  (GimpViewable      *viewable,
                                                 gint               width,
                                                 gint               height);
static void       gimp_palette_dirty            (GimpData          *data);
static gboolean   gimp_palette_save             (GimpData          *data);
53
static gchar    * gimp_palette_get_extension    (GimpData          *data);
54

55
static void       gimp_palette_entry_free       (GimpPaletteEntry  *entry);
56 57 58 59


/*  private variables  */

60
static GimpDataClass *parent_class = NULL;
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81


GtkType
gimp_palette_get_type (void)
{
  static GtkType palette_type = 0;

  if (! palette_type)
    {
      GtkTypeInfo palette_info =
      {
        "GimpPalette",
        sizeof (GimpPalette),
        sizeof (GimpPaletteClass),
        (GtkClassInitFunc) gimp_palette_class_init,
        (GtkObjectInitFunc) gimp_palette_init,
        /* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
        (GtkClassInitFunc) NULL,
      };

82
      palette_type = gtk_type_unique (GIMP_TYPE_DATA, &palette_info);
83 84 85 86 87 88 89 90
    }

  return palette_type;
}

static void
gimp_palette_class_init (GimpPaletteClass *klass)
{
91 92 93
  GtkObjectClass    *object_class;
  GimpViewableClass *viewable_class;
  GimpDataClass     *data_class;
94

95 96 97
  object_class   = (GtkObjectClass *) klass;
  viewable_class = (GimpViewableClass *) klass;
  data_class     = (GimpDataClass *) klass;
98

99
  parent_class = gtk_type_class (GIMP_TYPE_DATA);
100 101 102

  object_class->destroy = gimp_palette_destroy;

103 104
  viewable_class->get_new_preview = gimp_palette_get_new_preview;

105 106 107
  data_class->dirty         = gimp_palette_dirty;
  data_class->save          = gimp_palette_save;
  data_class->get_extension = gimp_palette_get_extension;
108 109 110 111 112 113 114 115
}

static void
gimp_palette_init (GimpPalette *palette)
{
  palette->colors    = NULL;
  palette->n_colors  = 0;

116
  palette->n_columns = 0;
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
}

static void
gimp_palette_destroy (GtkObject *object)
{
  GimpPalette      *palette;
  GimpPaletteEntry *entry;
  GList            *list;

  g_return_if_fail (GIMP_IS_PALETTE (object));

  palette = GIMP_PALETTE (object);

  for (list = palette->colors; list; list = g_list_next (list))
    {
      entry = (GimpPaletteEntry *) list->data;

      gimp_palette_entry_free (entry);
    }

  g_list_free (palette->colors);

  if (GTK_OBJECT_CLASS (parent_class)->destroy)
    GTK_OBJECT_CLASS (parent_class)->destroy (object);
}

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
static TempBuf *
gimp_palette_get_new_preview (GimpViewable *viewable,
			      gint          width,
			      gint          height)
{
  GimpPalette      *palette;
  GimpPaletteEntry *entry;
  TempBuf          *temp_buf;
  guchar           *buf;
  guchar           *b;
  GList            *list;
  guchar            white[3] = { 255, 255, 255 };
  gint              columns;
  gint              rows;
  gint              x, y, i;

  palette = GIMP_PALETTE (viewable);

  temp_buf = temp_buf_new (width, height,
                           3,
                           0, 0,
                           white);

166 167 168 169
#define CELL_SIZE 4

  columns = width  / CELL_SIZE;
  rows    = height / CELL_SIZE;
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

  buf = temp_buf_data (temp_buf);
  b   = g_new (guchar, width * 3);

  memset (b, 255, width * 3);

  list = palette->colors;

  for (y = 0; y < rows && list; y++)
    {
      for (x = 0; x < columns && list; x++)
	{
	  entry = (GimpPaletteEntry *) list->data;

	  list = g_list_next (list);

	  gimp_rgb_get_uchar (&entry->color,
187 188 189
			      &b[x * CELL_SIZE * 3 + 0],
			      &b[x * CELL_SIZE * 3 + 1],
			      &b[x * CELL_SIZE * 3 + 2]);
190

191
	  for (i = 1; i < CELL_SIZE; i++)
192
	    {
193 194 195
	      b[(x * CELL_SIZE + i) * 3 + 0] = b[(x * CELL_SIZE) * 3 + 0];
	      b[(x * CELL_SIZE + i) * 3 + 1] = b[(x * CELL_SIZE) * 3 + 1];
	      b[(x * CELL_SIZE + i) * 3 + 2] = b[(x * CELL_SIZE) * 3 + 2];
196 197 198
	    }
	}

199
      for (i = 0; i < CELL_SIZE; i++)
200
	{
201
	  memcpy (buf + ((y * CELL_SIZE + i) * width) * 3, b, width * 3);
202 203 204
	}
    }

205 206
#undef CELL_SIZE

207 208 209 210 211
  g_free (b);

  return temp_buf;
}

212
GimpData *
213 214 215 216 217 218 219 220 221 222 223
gimp_palette_new (const gchar *name)
{
  GimpPalette *palette = NULL;

  g_return_val_if_fail (name != NULL, NULL);
  g_return_val_if_fail (*name != '\0', NULL);

  palette = GIMP_PALETTE (gtk_object_new (GIMP_TYPE_PALETTE,
					  "name", name,
					  NULL));

224
  gimp_data_dirty (GIMP_DATA (palette));
225

226
  return GIMP_DATA (palette);
227 228
}

229
GimpData *
230 231 232 233 234 235 236 237 238 239 240
gimp_palette_get_standard (void)
{
  static GimpPalette *standard_palette = NULL;

  if (! standard_palette)
    {
      standard_palette = GIMP_PALETTE (gtk_type_new (GIMP_TYPE_PALETTE));

      gimp_object_set_name (GIMP_OBJECT (standard_palette), "Standard");
    }

241
  return GIMP_DATA (standard_palette);
242 243
}

244
GimpData *
245
gimp_palette_load (const gchar *filename)
246 247
{
  GimpPalette *palette;
248
  gchar        str[1024];
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 287 288
  gchar       *tok;
  FILE        *fp;
  gint         r, g, b;
  GimpRGB      color;
  gint         linenum;

  g_return_val_if_fail (filename != NULL, NULL);
  g_return_val_if_fail (*filename != '\0', NULL);

  r = g = b = 0;

  /*  Open the requested file  */
  if (! (fp = fopen (filename, "r")))
    {
      g_warning ("Failed to open palette file %s", filename);
      return NULL;
    }

  linenum = 0;

  fread (str, 13, 1, fp);
  str[13] = '\0';
  linenum++;
  if (strcmp (str, "GIMP Palette\n"))
    {
      /* bad magic, but maybe it has \r\n at the end of lines? */
      if (!strcmp (str, "GIMP Palette\r"))
	g_message (_("Loading palette %s:\n"
		     "Corrupt palette:\n"
		     "missing magic header\n"
		     "Does this file need converting from DOS?"), filename);
      else
	g_message (_("Loading palette %s:\n"
		     "Corrupt palette: missing magic header"), filename);

      fclose (fp);

      return NULL;
    }

289 290 291
  palette = gtk_type_new (GIMP_TYPE_PALETTE);

  gimp_data_set_filename (GIMP_DATA (palette), filename);
292

293
  if (! fgets (str, 1024, fp))
294
    {
295 296
      g_message (_("Loading palette %s (line %d):\nRead error"),
		 filename, linenum);
297

298 299 300 301 302 303 304 305 306 307 308 309 310 311
      fclose (fp);
      gtk_object_sink (GTK_OBJECT (palette));
      return NULL;
    }

  linenum++;

  if (! strncmp (str, "Name: ", strlen ("Name: ")))
    {
      gimp_object_set_name (GIMP_OBJECT (palette),
			    g_strstrip (&str[strlen ("Name: ")]));

      if (! fgets (str, 1024, fp))
	{
312 313 314 315 316 317 318 319 320
	  g_message (_("Loading palette %s (line %d):\nRead error"),
		     filename, linenum);

	  fclose (fp);
	  gtk_object_sink (GTK_OBJECT (palette));
	  return NULL;
	}

      linenum++;
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350

      if (! strncmp (str, "Columns: ", strlen ("Columns: ")))
	{
	  gint columns;

	  columns = atoi (g_strstrip (&str[strlen ("Columns: ")]));

	  if (columns < 0 || columns > 256)
	    {
	      g_message (_("Loading palette %s (line %d):\n"
			   "Invalid number or columns"),
			 filename, linenum);

	      columns = 0;
	    }

	  palette->n_columns = columns;

	  if (! fgets (str, 1024, fp))
	    {
	      g_message (_("Loading palette %s (line %d):\nRead error"),
			 filename, linenum);

	      fclose (fp);
	      gtk_object_sink (GTK_OBJECT (palette));
	      return NULL;
	    }

	  linenum++;
	}
351 352 353 354
    }
  else /* old palette format */
    {
      g_warning ("old palette format %s", filename);
355

356 357 358 359 360
      gimp_object_set_name (GIMP_OBJECT (palette), g_basename (filename));
    }

  while (! feof (fp))
    {
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
      if (str[0] != '#')
	{
	  tok = strtok (str, " \t");
	  if (tok)
	    r = atoi (tok);
	  else
	    /* maybe we should just abort? */
	    g_message (_("Loading palette %s (line %d):\n"
			 "Missing RED component"), filename, linenum);

	  tok = strtok (NULL, " \t");
	  if (tok)
	    g = atoi (tok);
	  else
	    g_message (_("Loading palette %s (line %d):\n"
			 "Missing GREEN component"), filename, linenum);

	  tok = strtok (NULL, " \t");
	  if (tok)
	    b = atoi (tok);
	  else
	    g_message (_("Loading palette %s (line %d):\n"
			 "Missing BLUE component"), filename, linenum);

	  /* optional name */
	  tok = strtok (NULL, "\n");

	  if (r < 0 || r > 255 ||
	      g < 0 || g > 255 ||
	      b < 0 || b > 255)
	    g_message (_("Loading palette %s (line %d):\n"
			 "RGB value out of range"), filename, linenum);

	  gimp_rgba_set_uchar (&color,
			       (guchar) r,
			       (guchar) g,
			       (guchar) b,
			       255);

	  gimp_palette_add_entry (palette, tok, &color);
	}
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

      if (! fgets (str, 1024, fp))
	{
	  if (feof (fp))
	    break;

	  g_message (_("Loading palette %s (line %d):\nRead error"),
		     filename, linenum);

	  fclose (fp);
	  gtk_object_sink (GTK_OBJECT (palette));
	  return NULL;
	}

      linenum++;
417 418 419 420
    }

  fclose (fp);

421
  GIMP_DATA (palette)->dirty = FALSE;
422

423
  return GIMP_DATA (palette);
424 425
}

426 427
static void
gimp_palette_dirty (GimpData *data)
428
{
429 430 431 432 433 434 435 436
  if (GIMP_DATA_CLASS (parent_class)->dirty)
    GIMP_DATA_CLASS (parent_class)->dirty (data);
}

static gboolean
gimp_palette_save (GimpData *data)
{
  GimpPalette      *palette;
437 438 439 440 441
  GimpPaletteEntry *entry;
  GList            *list;
  FILE             *fp;
  guchar            r, g, b;

442
  palette = GIMP_PALETTE (data);
443

444
  if (! (fp = fopen (GIMP_DATA (palette)->filename, "w")))
445
    {
446 447
      g_message (_("Can't save palette \"%s\"\n"),
		 GIMP_DATA (palette)->filename);
448 449 450 451
      return FALSE;
    }

  fprintf (fp, "GIMP Palette\n");
452 453
  fprintf (fp, "Name: %s\n", GIMP_OBJECT (palette)->name);
  fprintf (fp, "Columns: %d\n#\n", CLAMP (palette->n_columns, 0, 256));
454 455 456 457 458 459 460

  for (list = palette->colors; list; list = g_list_next (list))
    {
      entry = (GimpPaletteEntry *) list->data;

      gimp_rgb_get_uchar (&entry->color, &r, &g, &b);

461
      fprintf (fp, "%3d %3d %3d\t%s\n",
462 463 464 465 466 467
	       r, g, b,
	       entry->name);
    }

  fclose (fp);

468 469 470
  if (GIMP_DATA_CLASS (parent_class)->save)
    return GIMP_DATA_CLASS (parent_class)->save (data);

471 472 473
  return TRUE;
}

474 475 476 477 478 479
static gchar *
gimp_palette_get_extension (GimpData *data)
{
  return GIMP_PALETTE_FILE_EXTENSION;
}

480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
GimpPaletteEntry *
gimp_palette_add_entry (GimpPalette *palette,
			const gchar *name,
			GimpRGB     *color)
{
  GimpPaletteEntry *entry;

  g_return_val_if_fail (palette != NULL, NULL);
  g_return_val_if_fail (GIMP_IS_PALETTE (palette), NULL);

  g_return_val_if_fail (color != NULL, NULL);

  entry = g_new0 (GimpPaletteEntry, 1);

  entry->color    = *color;

  entry->name     = g_strdup (name ? name : _("Untitled"));
  entry->position = palette->n_colors;

  palette->colors    = g_list_append (palette->colors, entry);
  palette->n_colors += 1;

502
  gimp_data_dirty (GIMP_DATA (palette));
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547

  return entry;
}

void
gimp_palette_delete_entry (GimpPalette      *palette,
			   GimpPaletteEntry *entry)
{
  GList *list;
  gint   pos = 0;

  g_return_if_fail (palette != NULL);
  g_return_if_fail (GIMP_IS_PALETTE (palette));

  g_return_if_fail (entry != NULL);

  if (g_list_find (palette->colors, entry))
    {
      pos = entry->position;
      gimp_palette_entry_free (entry);

      palette->colors = g_list_remove (palette->colors, entry);

      palette->n_colors--;

      for (list = g_list_nth (palette->colors, pos);
	   list;
	   list = g_list_next (list))
	{
	  entry = (GimpPaletteEntry *) list->data;

	  entry->position = pos++;
	}

      if (palette->n_colors == 0)
	{
	  GimpRGB color;

	  gimp_rgba_set (&color, 0.0, 0.0, 0.0, 1.0);

	  gimp_palette_add_entry (palette,
				  _("Black"),
				  &color);
	}

548
      gimp_data_dirty (GIMP_DATA (palette));
549 550 551 552 553 554 555 556 557 558 559
    }
}

static void
gimp_palette_entry_free (GimpPaletteEntry *entry)
{
  g_return_if_fail (entry != NULL);

  g_free (entry->name);
  g_free (entry);
}