gimptemplate.c 18.5 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3 4 5 6
 * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
 *
 * gimptemplate.c
 * Copyright (C) 2003 Michael Natterer <mitch@gimp.org>
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10 11 12 13 14 15 16 17
 * (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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 20
 */

21 22 23
/* This file contains the definition of the image template objects.
 */

24 25
#include "config.h"

26
#include <cairo.h>
27
#include <gdk-pixbuf/gdk-pixbuf.h>
28
#include <gegl.h>
29 30

#include "libgimpbase/gimpbase.h"
31
#include "libgimpcolor/gimpcolor.h"
32
#include "libgimpconfig/gimpconfig.h"
33 34 35

#include "core-types.h"

36 37
#include "gegl/gimp-babl.h"

38
#include "gimpimage.h"
39
#include "gimpprojection.h"
40 41 42 43
#include "gimptemplate.h"

#include "gimp-intl.h"

44

45
#define DEFAULT_RESOLUTION 300.0
46 47 48 49 50 51 52 53 54 55

enum
{
  PROP_0,
  PROP_WIDTH,
  PROP_HEIGHT,
  PROP_UNIT,
  PROP_XRESOLUTION,
  PROP_YRESOLUTION,
  PROP_RESOLUTION_UNIT,
56
  PROP_BASE_TYPE,
57
  PROP_PRECISION,
58 59
  PROP_COMPONENT_TYPE,
  PROP_LINEAR,
60 61
  PROP_COLOR_MANAGED,
  PROP_COLOR_PROFILE,
62
  PROP_FILL_TYPE,
Sven Neumann's avatar
Sven Neumann committed
63
  PROP_COMMENT,
64
  PROP_FILENAME
65 66 67
};


68 69 70 71 72 73 74 75 76 77 78 79
typedef struct _GimpTemplatePrivate GimpTemplatePrivate;

struct _GimpTemplatePrivate
{
  gint               width;
  gint               height;
  GimpUnit           unit;

  gdouble            xresolution;
  gdouble            yresolution;
  GimpUnit           resolution_unit;

80
  GimpImageBaseType  base_type;
81
  GimpPrecision      precision;
82

83 84 85
  gboolean           color_managed;
  GFile             *color_profile;

86 87 88 89 90 91 92 93 94 95 96 97 98
  GimpFillType       fill_type;

  gchar             *comment;
  gchar             *filename;

  guint64            initial_size;
};

#define GET_PRIVATE(template) G_TYPE_INSTANCE_GET_PRIVATE (template, \
                                                           GIMP_TYPE_TEMPLATE, \
                                                           GimpTemplatePrivate)


99 100 101 102 103 104 105 106 107 108 109
static void      gimp_template_finalize     (GObject      *object);
static void      gimp_template_set_property (GObject      *object,
                                             guint         property_id,
                                             const GValue *value,
                                             GParamSpec   *pspec);
static void      gimp_template_get_property (GObject      *object,
                                             guint         property_id,
                                             GValue       *value,
                                             GParamSpec   *pspec);
static void      gimp_template_notify       (GObject      *object,
                                             GParamSpec   *pspec);
110 111


112
G_DEFINE_TYPE_WITH_CODE (GimpTemplate, gimp_template, GIMP_TYPE_VIEWABLE,
113
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_CONFIG, NULL))
114

115
#define parent_class gimp_template_parent_class
116 117 118 119 120


static void
gimp_template_class_init (GimpTemplateClass *klass)
{
121 122
  GObjectClass      *object_class   = G_OBJECT_CLASS (klass);
  GimpViewableClass *viewable_class = GIMP_VIEWABLE_CLASS (klass);
123

124
  object_class->finalize     = gimp_template_finalize;
125 126 127

  object_class->set_property = gimp_template_set_property;
  object_class->get_property = gimp_template_get_property;
128
  object_class->notify       = gimp_template_notify;
129

130
  viewable_class->default_icon_name = "gimp-template";
131
  viewable_class->name_editable     = TRUE;
132

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
  GIMP_CONFIG_PROP_INT (object_class, PROP_WIDTH,
                        "width",
                        _("Width"),
                        NULL,
                        GIMP_MIN_IMAGE_SIZE, GIMP_MAX_IMAGE_SIZE,
                        GIMP_DEFAULT_IMAGE_WIDTH,
                        GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_INT (object_class, PROP_HEIGHT,
                        "height",
                        _("Height"),
                        NULL,
                        GIMP_MIN_IMAGE_SIZE, GIMP_MAX_IMAGE_SIZE,
                        GIMP_DEFAULT_IMAGE_HEIGHT,
                        GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_UNIT (object_class, PROP_UNIT,
                         "unit",
                         _("Unit"),
                         _("The unit used for coordinate display "
                           "when not in dot-for-dot mode."),
                         TRUE, FALSE, GIMP_UNIT_PIXEL,
                         GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_RESOLUTION (object_class, PROP_XRESOLUTION,
                               "xresolution",
                               _("Resolution X"),
                               _("The horizontal image resolution."),
                               DEFAULT_RESOLUTION,
                               GIMP_PARAM_STATIC_STRINGS |
                               GIMP_TEMPLATE_PARAM_COPY_FIRST);

  GIMP_CONFIG_PROP_RESOLUTION (object_class, PROP_YRESOLUTION,
                               "yresolution",
                               _("Resolution X"),
                               _("The vertical image resolution."),
                               DEFAULT_RESOLUTION,
                               GIMP_PARAM_STATIC_STRINGS |
                               GIMP_TEMPLATE_PARAM_COPY_FIRST);

  GIMP_CONFIG_PROP_UNIT (object_class, PROP_RESOLUTION_UNIT,
                         "resolution-unit",
                         _("Resolution unit"),
                         NULL,
                         FALSE, FALSE, GIMP_UNIT_INCH,
                         GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_ENUM (object_class, PROP_BASE_TYPE,
                         "image-type", /* serialized name */
                         _("Image type"),
                         NULL,
                         GIMP_TYPE_IMAGE_BASE_TYPE, GIMP_RGB,
                         GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_ENUM (object_class, PROP_PRECISION,
                         "precision",
                         _("Precision"),
                         NULL,
                         GIMP_TYPE_PRECISION, GIMP_PRECISION_U8_GAMMA,
                         GIMP_PARAM_STATIC_STRINGS);

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  g_object_class_install_property (object_class, PROP_COMPONENT_TYPE,
                                   g_param_spec_enum ("component-type",
                                                      _("Precision"),
                                                      NULL,
                                                      GIMP_TYPE_COMPONENT_TYPE,
                                                      GIMP_COMPONENT_TYPE_U8,
                                                      G_PARAM_READWRITE |
                                                      GIMP_PARAM_STATIC_STRINGS));

  g_object_class_install_property (object_class, PROP_LINEAR,
                                   g_param_spec_boolean ("linear",
                                                         _("Gamma"),
                                                         NULL,
                                                         FALSE,
                                                         G_PARAM_READWRITE |
                                                         GIMP_PARAM_STATIC_STRINGS));

211 212 213
  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_COLOR_MANAGED,
                            "color-managed",
                            _("Color managed"),
214 215 216 217
                            _("Whether the image is color managed. "
                              "Disabling color management is equivalent to "
                              "choosing a built-in sRGB profile. Better "
                              "leave color management enabled."),
218 219 220 221 222 223 224 225 226 227
                            TRUE,
                            GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_OBJECT (object_class, PROP_COLOR_PROFILE,
                           "color-profile",
                           _("Color profile"),
                           NULL,
                           G_TYPE_FILE,
                           GIMP_PARAM_STATIC_STRINGS);

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  GIMP_CONFIG_PROP_ENUM (object_class, PROP_FILL_TYPE,
                         "fill-type",
                         _("Fill type"),
                         NULL,
                         GIMP_TYPE_FILL_TYPE, GIMP_FILL_BACKGROUND,
                         GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_STRING (object_class, PROP_COMMENT,
                           "comment",
                           _("Comment"),
                           NULL,
                           NULL,
                           GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_STRING (object_class, PROP_FILENAME,
                           "filename",
                           _("Filename"),
                           NULL,
                           NULL,
                           GIMP_PARAM_STATIC_STRINGS);
248 249

  g_type_class_add_private (klass, sizeof (GimpTemplatePrivate));
250 251
}

252 253 254 255 256
static void
gimp_template_init (GimpTemplate *template)
{
}

257 258 259
static void
gimp_template_finalize (GObject *object)
{
260
  GimpTemplatePrivate *private = GET_PRIVATE (object);
261

262 263 264
  g_clear_object (&private->color_profile);
  g_clear_pointer (&private->comment,  g_free);
  g_clear_pointer (&private->filename, g_free);
265 266 267 268 269 270 271 272 273 274

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gimp_template_set_property (GObject      *object,
                            guint         property_id,
                            const GValue *value,
                            GParamSpec   *pspec)
{
275
  GimpTemplatePrivate *private = GET_PRIVATE (object);
276 277 278 279

  switch (property_id)
    {
    case PROP_WIDTH:
280
      private->width = g_value_get_int (value);
281 282
      break;
    case PROP_HEIGHT:
283
      private->height = g_value_get_int (value);
284 285
      break;
    case PROP_UNIT:
286
      private->unit = g_value_get_int (value);
287 288
      break;
    case PROP_XRESOLUTION:
289
      private->xresolution = g_value_get_double (value);
290 291
      break;
    case PROP_YRESOLUTION:
292
      private->yresolution = g_value_get_double (value);
293 294
      break;
    case PROP_RESOLUTION_UNIT:
295
      private->resolution_unit = g_value_get_int (value);
296
      break;
297 298
    case PROP_BASE_TYPE:
      private->base_type = g_value_get_enum (value);
299
      break;
300 301
    case PROP_PRECISION:
      private->precision = g_value_get_enum (value);
302 303 304 305 306 307 308 309 310 311 312 313 314 315
      g_object_notify (object, "component-type");
      g_object_notify (object, "linear");
      break;
    case PROP_COMPONENT_TYPE:
      private->precision =
        gimp_babl_precision (g_value_get_enum (value),
                             gimp_babl_linear (private->precision));
      g_object_notify (object, "precision");
      break;
    case PROP_LINEAR:
      private->precision =
        gimp_babl_precision (gimp_babl_component_type (private->precision),
                             g_value_get_boolean (value));
      g_object_notify (object, "precision");
316
      break;
317 318 319 320 321 322 323 324
    case PROP_COLOR_MANAGED:
      private->color_managed = g_value_get_boolean (value);
      break;
    case PROP_COLOR_PROFILE:
      if (private->color_profile)
        g_object_unref (private->color_profile);
      private->color_profile = g_value_dup_object (value);
      break;
325
    case PROP_FILL_TYPE:
326
      private->fill_type = g_value_get_enum (value);
327
      break;
Sven Neumann's avatar
Sven Neumann committed
328
    case PROP_COMMENT:
329 330 331
      if (private->comment)
        g_free (private->comment);
      private->comment = g_value_dup_string (value);
Sven Neumann's avatar
Sven Neumann committed
332
      break;
333
    case PROP_FILENAME:
334 335 336
      if (private->filename)
        g_free (private->filename);
      private->filename = g_value_dup_string (value);
337
      break;
338 339 340 341 342 343 344 345 346 347 348 349
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_template_get_property (GObject    *object,
                            guint       property_id,
                            GValue     *value,
                            GParamSpec *pspec)
{
350
  GimpTemplatePrivate *private = GET_PRIVATE (object);
351 352 353 354

  switch (property_id)
    {
    case PROP_WIDTH:
355
      g_value_set_int (value, private->width);
356 357
      break;
    case PROP_HEIGHT:
358
      g_value_set_int (value, private->height);
359 360
      break;
    case PROP_UNIT:
361
      g_value_set_int (value, private->unit);
362 363
      break;
    case PROP_XRESOLUTION:
364
      g_value_set_double (value, private->xresolution);
365 366
      break;
    case PROP_YRESOLUTION:
367
      g_value_set_double (value, private->yresolution);
368 369
      break;
    case PROP_RESOLUTION_UNIT:
370
      g_value_set_int (value, private->resolution_unit);
371
      break;
372 373
    case PROP_BASE_TYPE:
      g_value_set_enum (value, private->base_type);
374
      break;
375 376 377
    case PROP_PRECISION:
      g_value_set_enum (value, private->precision);
      break;
378 379 380 381 382 383
    case PROP_COMPONENT_TYPE:
      g_value_set_enum (value, gimp_babl_component_type (private->precision));
      break;
    case PROP_LINEAR:
      g_value_set_boolean (value, gimp_babl_linear (private->precision));
      break;
384 385 386 387 388 389
    case PROP_COLOR_MANAGED:
      g_value_set_boolean (value, private->color_managed);
      break;
    case PROP_COLOR_PROFILE:
      g_value_set_object (value, private->color_profile);
      break;
390
    case PROP_FILL_TYPE:
391
      g_value_set_enum (value, private->fill_type);
392
      break;
Sven Neumann's avatar
Sven Neumann committed
393
    case PROP_COMMENT:
394
      g_value_set_string (value, private->comment);
Sven Neumann's avatar
Sven Neumann committed
395
      break;
396
    case PROP_FILENAME:
397
      g_value_set_string (value, private->filename);
398
      break;
399 400 401 402 403 404
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

405 406 407 408
static void
gimp_template_notify (GObject    *object,
                      GParamSpec *pspec)
{
409
  GimpTemplatePrivate *private = GET_PRIVATE (object);
410 411
  const Babl          *format;
  gint                 bytes;
412 413 414 415

  if (G_OBJECT_CLASS (parent_class)->notify)
    G_OBJECT_CLASS (parent_class)->notify (object, pspec);

416 417 418
  /* the initial layer */
  format = gimp_babl_format (private->base_type,
                             private->precision,
419
                             private->fill_type == GIMP_FILL_TRANSPARENT);
420
  bytes = babl_format_get_bytes_per_pixel (format);
421

422
  /* the selection */
423
  format = gimp_babl_mask_format (private->precision);
424
  bytes += babl_format_get_bytes_per_pixel (format);
425

426
  private->initial_size = ((guint64) bytes          *
427 428
                           (guint64) private->width *
                           (guint64) private->height);
429

430
  private->initial_size +=
431
    gimp_projection_estimate_memsize (private->base_type,
432
                                      gimp_babl_component_type (private->precision),
433
                                      private->width, private->height);
434 435
}

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

/*  public functions  */

GimpTemplate *
gimp_template_new (const gchar *name)
{
  g_return_val_if_fail (name != NULL, NULL);

  return g_object_new (GIMP_TYPE_TEMPLATE,
                       "name", name,
                       NULL);
}

void
gimp_template_set_from_image (GimpTemplate *template,
451
                              GimpImage    *image)
452
{
453 454
  gdouble             xresolution;
  gdouble             yresolution;
455
  GimpImageBaseType   base_type;
456 457
  const GimpParasite *parasite;
  gchar              *comment = NULL;
458 459

  g_return_if_fail (GIMP_IS_TEMPLATE (template));
460
  g_return_if_fail (GIMP_IS_IMAGE (image));
461

462
  gimp_image_get_resolution (image, &xresolution, &yresolution);
463

464
  base_type = gimp_image_get_base_type (image);
465

466 467
  if (base_type == GIMP_INDEXED)
    base_type = GIMP_RGB;
468

469
  parasite =  gimp_image_parasite_find (image, "gimp-comment");
Sven Neumann's avatar
Sven Neumann committed
470 471 472 473
  if (parasite)
    comment = g_strndup (gimp_parasite_data (parasite),
                         gimp_parasite_data_size (parasite));

474
  g_object_set (template,
475 476
                "width",           gimp_image_get_width (image),
                "height",          gimp_image_get_height (image),
477 478
                "xresolution",     xresolution,
                "yresolution",     yresolution,
479
                "resolution-unit", gimp_image_get_unit (image),
480
                "image-type",      base_type,
481
                "precision",       gimp_image_get_precision (image),
Sven Neumann's avatar
Sven Neumann committed
482
                "comment",         comment,
483
                NULL);
Sven Neumann's avatar
Sven Neumann committed
484 485 486

  if (comment)
    g_free (comment);
487
}
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 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

gint
gimp_template_get_width (GimpTemplate *template)
{
  g_return_val_if_fail (GIMP_IS_TEMPLATE (template), 0);

  return GET_PRIVATE (template)->width;
}

gint
gimp_template_get_height (GimpTemplate *template)
{
  g_return_val_if_fail (GIMP_IS_TEMPLATE (template), 0);

  return GET_PRIVATE (template)->height;
}

GimpUnit
gimp_template_get_unit (GimpTemplate *template)
{
  g_return_val_if_fail (GIMP_IS_TEMPLATE (template), GIMP_UNIT_INCH);

  return GET_PRIVATE (template)->unit;
}

gdouble
gimp_template_get_resolution_x (GimpTemplate *template)
{
  g_return_val_if_fail (GIMP_IS_TEMPLATE (template), 1.0);

  return GET_PRIVATE (template)->xresolution;
}

gdouble
gimp_template_get_resolution_y (GimpTemplate *template)
{
  g_return_val_if_fail (GIMP_IS_TEMPLATE (template), 1.0);

  return GET_PRIVATE (template)->yresolution;
}

GimpUnit
gimp_template_get_resolution_unit (GimpTemplate *template)
{
  g_return_val_if_fail (GIMP_IS_TEMPLATE (template), GIMP_UNIT_INCH);

  return GET_PRIVATE (template)->resolution_unit;
}

GimpImageBaseType
538
gimp_template_get_base_type (GimpTemplate *template)
539
{
540
  g_return_val_if_fail (GIMP_IS_TEMPLATE (template), GIMP_RGB);
541

542
  return GET_PRIVATE (template)->base_type;
543 544
}

545 546 547
GimpPrecision
gimp_template_get_precision (GimpTemplate *template)
{
548
  g_return_val_if_fail (GIMP_IS_TEMPLATE (template), GIMP_PRECISION_U8_GAMMA);
549 550 551 552

  return GET_PRIVATE (template)->precision;
}

553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
gboolean
gimp_template_get_color_managed (GimpTemplate *template)
{
  g_return_val_if_fail (GIMP_IS_TEMPLATE (template), FALSE);

  return GET_PRIVATE (template)->color_managed;
}

GimpColorProfile *
gimp_template_get_color_profile (GimpTemplate *template)
{
  GimpTemplatePrivate *private;

  g_return_val_if_fail (GIMP_IS_TEMPLATE (template), FALSE);

  private = GET_PRIVATE (template);

  if (private->color_profile)
    return gimp_color_profile_new_from_file (private->color_profile, NULL);

  return NULL;
}

576 577 578
GimpFillType
gimp_template_get_fill_type (GimpTemplate *template)
{
579
  g_return_val_if_fail (GIMP_IS_TEMPLATE (template), GIMP_FILL_BACKGROUND);
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

  return GET_PRIVATE (template)->fill_type;
}

const gchar *
gimp_template_get_comment (GimpTemplate *template)
{
  g_return_val_if_fail (GIMP_IS_TEMPLATE (template), NULL);

  return GET_PRIVATE (template)->comment;
}

guint64
gimp_template_get_initial_size (GimpTemplate *template)
{
  g_return_val_if_fail (GIMP_IS_TEMPLATE (template), 0);

  return GET_PRIVATE (template)->initial_size;
}