gimpimage.c 12.8 KB
Newer Older
1 2 3 4 5
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-2000 Peter Mattis and Spencer Kimball
 *
 * gimpimage.c
 *
6
 * This library is free software: you can redistribute it and/or
7 8
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
9
 * version 3 of the License, or (at your option) any later version.
10 11 12 13 14 15 16
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
18
 * <https://www.gnu.org/licenses/>.
19 20
 */

Sven Neumann's avatar
Sven Neumann committed
21 22
#include "config.h"

23
#include "gimp.h"
24

25 26
#include "libgimpbase/gimpwire.h" /* FIXME kill this include */

27
#include "gimppixbuf.h"
28 29 30
#include "gimpplugin-private.h"
#include "gimpprocedure-private.h"

31

32 33 34 35 36 37 38
enum
{
  PROP_0,
  PROP_ID,
  N_PROPS
};

39

40 41 42 43 44
struct _GimpImagePrivate
{
  gint id;
};

45

46 47 48 49 50 51 52 53
static void   gimp_image_set_property  (GObject      *object,
                                        guint         property_id,
                                        const GValue *value,
                                        GParamSpec   *pspec);
static void   gimp_image_get_property  (GObject      *object,
                                        guint         property_id,
                                        GValue       *value,
                                        GParamSpec   *pspec);
54

55

56 57 58 59 60 61
G_DEFINE_TYPE_WITH_PRIVATE (GimpImage, gimp_image, G_TYPE_OBJECT)

#define parent_class gimp_image_parent_class

static GParamSpec *props[N_PROPS] = { NULL, };

62

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
static void
gimp_image_class_init (GimpImageClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->set_property = gimp_image_set_property;
  object_class->get_property = gimp_image_get_property;

  props[PROP_ID] =
    g_param_spec_int ("id",
                      "The image id",
                      "The image id for internal use",
                      0, G_MAXINT32, 0,
                      GIMP_PARAM_READWRITE |
                      G_PARAM_CONSTRUCT_ONLY);

  g_object_class_install_properties (object_class, N_PROPS, props);
}

static void
gimp_image_init (GimpImage *image)
{
  image->priv = gimp_image_get_instance_private (image);
}

static void
gimp_image_set_property (GObject      *object,
                         guint         property_id,
                         const GValue *value,
                         GParamSpec   *pspec)
{
  GimpImage *image = GIMP_IMAGE (object);

  switch (property_id)
    {
    case PROP_ID:
      image->priv->id = g_value_get_int (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_image_get_property (GObject    *object,
                         guint       property_id,
                         GValue     *value,
                         GParamSpec *pspec)
{
  GimpImage *image = GIMP_IMAGE (object);

  switch (property_id)
    {
    case PROP_ID:
      g_value_set_int (value, image->priv->id);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}


129
/* Public API */
130 131 132 133 134 135 136 137 138 139 140 141

/**
 * gimp_image_get_id:
 * @image: The image.
 *
 * Returns: the image ID.
 *
 * Since: 3.0
 **/
gint32
gimp_image_get_id (GimpImage *image)
{
142
  return image ? image->priv->id : -1;
143
}
144

145
/**
146
 * gimp_image_get_by_id:
147 148
 * @image_id: The image id.
 *
149
 * Returns: (nullable) (transfer none): a #GimpImage for @image_id or
150
 *          %NULL if @image_id does not represent a valid image.
151 152
 *          The object belongs to libgimp and you must not modify
 *          or unref it.
153 154 155 156
 *
 * Since: 3.0
 **/
GimpImage *
157
gimp_image_get_by_id (gint32 image_id)
158
{
159
  if (image_id > 0)
160
    {
161 162
      GimpPlugIn    *plug_in   = gimp_get_plug_in ();
      GimpProcedure *procedure = _gimp_plug_in_get_procedure (plug_in);
163

164
      return _gimp_procedure_get_image (procedure, image_id);
165
    }
166

167
  return NULL;
168 169
}

170 171 172 173 174 175 176 177 178 179 180
/**
 * gimp_image_is_valid:
 * @image: The image to check.
 *
 * Returns TRUE if the image is valid.
 *
 * This procedure checks if the given image is valid and refers to
 * an existing image.
 *
 * Returns: Whether the image is valid.
 *
181
 * Since: 2.4
182 183 184 185 186 187 188
 **/
gboolean
gimp_image_is_valid (GimpImage *image)
{
  return gimp_image_id_is_valid (gimp_image_get_id (image));
}

189 190
/**
 * gimp_list_images:
191 192 193 194 195
 *
 * Returns the list of images currently open.
 *
 * This procedure returns the list of images currently open in GIMP.
 *
196
 * Returns: (element-type GimpImage) (transfer container):
197
 *          The list of images currently open.
198
 *          The returned list must be freed with g_list_free(). Image
199
 *          elements belong to libgimp and must not be freed.
200 201
 *
 * Since: 3.0
202 203
 **/
GList *
204
gimp_list_images (void)
205
{
206 207 208
  GimpImage **images;
  gint        num_images;
  GList      *list = NULL;
209 210
  gint        i;

211
  images = gimp_get_images (&num_images);
212

213 214
  for (i = 0; i < num_images; i++)
    list = g_list_prepend (list, images[i]);
215

216
  g_free (images);
217

218
  return g_list_reverse (list);
219 220 221 222
}

/**
 * gimp_image_list_layers:
223 224 225 226 227 228 229
 * @image: The image.
 *
 * Returns the list of layers contained in the specified image.
 *
 * This procedure returns the list of layers contained in the specified
 * image. The order of layers is from topmost to bottommost.
 *
230
 * Returns: (element-type GimpImage) (transfer container):
231
 *          The list of layers contained in the image.
232
 *          The returned list must be freed with g_list_free(). Layer
233
 *          elements belong to libgimp and must not be freed.
234 235 236 237
 *
 * Since: 3.0
 **/
GList *
238
gimp_image_list_layers (GimpImage *image)
239
{
240 241 242 243
  GimpLayer **layers;
  gint        num_layers;
  GList      *list = NULL;
  gint        i;
244

245
  layers = gimp_image_get_layers (image, &num_layers);
246

247
  for (i = 0; i < num_layers; i++)
248
    list = g_list_prepend (list, layers[i]);
249

250
  g_free (layers);
251

252
  return g_list_reverse (list);
253 254 255
}

/**
256
 * gimp_image_list_channels:
257 258 259 260 261 262 263
 * @image: The image.
 *
 * Returns the list of channels contained in the specified image.
 *
 * This procedure returns the list of channels contained in the
 * specified image. This does not include the selection mask, or layer
 * masks. The order is from topmost to bottommost. Note that
264
 * "channels" are custom channels and do not include the image's
265 266
 * color components.
 *
267
 * Returns: (element-type GimpChannel) (transfer container):
268
 *          The list of channels contained in the image.
269
 *          The returned list must be freed with g_list_free(). Channel
270
 *          elements belong to libgimp and must not be freed.
271 272 273 274
 *
 * Since: 3.0
 **/
GList *
275
gimp_image_list_channels (GimpImage *image)
276
{
277 278 279 280
  GimpChannel **channels;
  gint          num_channels;
  GList        *list = NULL;
  gint          i;
281

282
  channels = gimp_image_get_channels (image, &num_channels);
283

284
  for (i = 0; i < num_channels; i++)
285
    list = g_list_prepend (list, channels[i]);
286

287
  g_free (channels);
288

289
  return g_list_reverse (list);
290 291 292
}

/**
293
 * gimp_image_list_vectors:
294 295 296 297 298 299 300
 * @image: The image.
 *
 * Returns the list of vectors contained in the specified image.
 *
 * This procedure returns the list of vectors contained in the
 * specified image.
 *
301
 * Returns: (element-type GimpVectors) (transfer container):
302
 *          The list of vectors contained in the image.
303 304
 *          The returned value must be freed with g_list_free(). Vectors
 *          elements belong to libgimp and must not be freed.
305 306 307 308
 *
 * Since: 3.0
 **/
GList *
309
gimp_image_list_vectors (GimpImage *image)
310
{
311 312 313 314
  GimpVectors **vectors;
  gint          num_vectors;
  GList        *list = NULL;
  gint          i;
315

316
  vectors = gimp_image_get_vectors (image, &num_vectors);
317

318
  for (i = 0; i < num_vectors; i++)
319
    list = g_list_prepend (list, vectors[i]);
320

321
  g_free (vectors);
322

323
  return g_list_reverse (list);
324 325
}

Michael Natterer's avatar
Michael Natterer committed
326 327
/**
 * gimp_image_get_colormap:
328
 * @image:      The image.
329
 * @num_colors: Returns the number of colors in the colormap array.
330 331 332 333
 *
 * Returns the image's colormap
 *
 * This procedure returns an actual pointer to the image's colormap, as
334
 * well as the number of colors contained in the colormap. If the image
335 336 337 338
 * is not of base type INDEXED, this pointer will be NULL.
 *
 * Returns: The image's colormap.
 */
339
guchar *
340 341
gimp_image_get_colormap (GimpImage *image,
                         gint      *num_colors)
342 343 344 345
{
  gint    num_bytes;
  guchar *cmap;

346
  cmap = _gimp_image_get_colormap (image, &num_bytes);
347

348 349
  if (num_colors)
    *num_colors = num_bytes / 3;
350 351 352 353

  return cmap;
}

354
/**
Michael Natterer's avatar
Michael Natterer committed
355
 * gimp_image_set_colormap:
356
 * @image:      The image.
Michael Natterer's avatar
Michael Natterer committed
357
 * @colormap:   The new colormap values.
358 359 360 361 362
 * @num_colors: Number of colors in the colormap array.
 *
 * Sets the entries in the image's colormap.
 *
 * This procedure sets the entries in the specified image's colormap.
363
 * The number of colors is specified by the "num_colors" parameter
364
 * and corresponds to the number of INT8 triples that must be contained
365
 * in the "cmap" array.
366 367 368
 *
 * Returns: TRUE on success.
 */
369
gboolean
370
gimp_image_set_colormap (GimpImage    *image,
Michael Natterer's avatar
Michael Natterer committed
371 372
                         const guchar *colormap,
                         gint          num_colors)
373
{
374
  return _gimp_image_set_colormap (image, num_colors * 3, colormap);
375 376
}

377 378
/**
 * gimp_image_get_thumbnail_data:
379 380 381 382
 * @image:  The image.
 * @width:  (inout): The requested thumbnail width.
 * @height: (inout): The requested thumbnail height.
 * @bpp:    (out): The previews bpp.
383 384 385 386 387 388 389 390 391 392
 *
 * Get a thumbnail of an image.
 *
 * This function gets data from which a thumbnail of an image preview
 * can be created. Maximum x or y dimension is 1024 pixels. The pixels
 * are returned in RGB[A] or GRAY[A] format. The bpp return value
 * gives the number of bytes per pixel in the image.
 *
 * Returns: (transfer full): the thumbnail data.
 **/
393
guchar *
394 395 396 397
gimp_image_get_thumbnail_data (GimpImage *image,
                               gint      *width,
                               gint      *height,
                               gint      *bpp)
398 399 400 401 402 403
{
  gint    ret_width;
  gint    ret_height;
  guchar *image_data;
  gint    data_size;

404
  _gimp_image_thumbnail (image,
Sven Neumann's avatar
Sven Neumann committed
405 406 407 408 409 410 411
                         *width,
                         *height,
                         &ret_width,
                         &ret_height,
                         bpp,
                         &data_size,
                         &image_data);
412 413 414

  *width  = ret_width;
  *height = ret_height;
415 416 417

  return image_data;
}
418

419 420
/**
 * gimp_image_get_thumbnail:
421 422 423 424
 * @image:  the image ID
 * @width:  the requested thumbnail width  (<= 1024 pixels)
 * @height: the requested thumbnail height (<= 1024 pixels)
 * @alpha:  how to handle an alpha channel
425
 *
426
 * Retrieves a thumbnail pixbuf for the image identified by @image->priv->id.
427 428 429 430
 * The thumbnail will be not larger than the requested size.
 *
 * Returns: (transfer full): a new #GdkPixbuf
 *
431
 * Since: 2.2
432 433
 **/
GdkPixbuf *
434
gimp_image_get_thumbnail (GimpImage              *image,
435 436 437 438 439 440 441 442 443 444 445 446
                          gint                    width,
                          gint                    height,
                          GimpPixbufTransparency  alpha)
{
  gint    thumb_width  = width;
  gint    thumb_height = height;
  gint    thumb_bpp;
  guchar *data;

  g_return_val_if_fail (width  > 0 && width  <= 1024, NULL);
  g_return_val_if_fail (height > 0 && height <= 1024, NULL);

447
  data = gimp_image_get_thumbnail_data (image,
448 449 450 451 452 453 454 455 456 457 458
                                        &thumb_width,
                                        &thumb_height,
                                        &thumb_bpp);
  if (data)
    return _gimp_pixbuf_from_data (data,
                                   thumb_width, thumb_height, thumb_bpp,
                                   alpha);
  else
    return NULL;
}

459 460
/**
 * gimp_image_get_metadata:
461
 * @image: The image.
462 463 464 465 466
 *
 * Returns the image's metadata.
 *
 * Returns exif/iptc/xmp metadata from the image.
 *
467 468
 * Returns: (nullable) (transfer full): The exif/ptc/xmp metadata,
 *          or %NULL if there is none.
469
 *
470
 * Since: 2.10
471 472
 **/
GimpMetadata *
473
gimp_image_get_metadata (GimpImage *image)
474 475 476 477
{
  GimpMetadata *metadata = NULL;
  gchar        *metadata_string;

478
  metadata_string = _gimp_image_get_metadata (image);
479 480 481 482 483 484 485 486 487 488 489
  if (metadata_string)
    {
      metadata = gimp_metadata_deserialize (metadata_string);
      g_free (metadata_string);
    }

  return metadata;
}

/**
 * gimp_image_set_metadata:
490
 * @image:    The image.
491 492 493 494 495 496 497 498 499
 * @metadata: The exif/ptc/xmp metadata.
 *
 * Set the image's metadata.
 *
 * Sets exif/iptc/xmp metadata on the image, or deletes it if
 * @metadata is %NULL.
 *
 * Returns: TRUE on success.
 *
500
 * Since: 2.10
501 502
 **/
gboolean
503
gimp_image_set_metadata (GimpImage    *image,
504 505 506 507 508 509 510 511
                         GimpMetadata *metadata)
{
  gchar    *metadata_string = NULL;
  gboolean  success;

  if (metadata)
    metadata_string = gimp_metadata_serialize (metadata);

512
  success = _gimp_image_set_metadata (image, metadata_string);
513 514 515 516 517 518

  if (metadata_string)
    g_free (metadata_string);

  return success;
}