gimpdrawablepreview.c 20.3 KB
Newer Older
1 2 3 4 5
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimpdrawablepreview.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 21 22
 */

#include "config.h"

23
#include <gegl.h>
24 25 26 27
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

28 29 30 31
#include "gimpuitypes.h"

#include "gimp.h"

32 33 34
#include "gimpdrawablepreview.h"


35 36 37 38 39 40 41 42 43
/**
 * SECTION: gimpdrawablepreview
 * @title: GimpDrawablePreview
 * @short_description: A widget providing a preview of a #GimpDrawable.
 *
 * A widget providing a preview of a #GimpDrawable.
 **/


44
#define SELECTION_BORDER  8
45

46 47 48
enum
{
  PROP_0,
49
  PROP_DRAWABLE
50 51
};

52 53
typedef struct
{
Sven Neumann's avatar
Sven Neumann committed
54 55 56
  gint      x;
  gint      y;
  gboolean  update;
57
} PreviewSettings;
58 59


60 61
struct _GimpDrawablePreviewPrivate
{
62
  GimpDrawable *drawable;
63 64 65 66
};

#define GET_PRIVATE(obj) (((GimpDrawablePreview *) (obj))->priv)

67

68
static void  gimp_drawable_preview_constructed   (GObject         *object);
69
static void  gimp_drawable_preview_dispose       (GObject         *object);
70
static void  gimp_drawable_preview_get_property  (GObject         *object,
Sven Neumann's avatar
Sven Neumann committed
71 72 73
                                                  guint            property_id,
                                                  GValue          *value,
                                                  GParamSpec      *pspec);
74
static void  gimp_drawable_preview_set_property  (GObject         *object,
Sven Neumann's avatar
Sven Neumann committed
75 76 77
                                                  guint            property_id,
                                                  const GValue    *value,
                                                  GParamSpec      *pspec);
78

79
static void  gimp_drawable_preview_style_updated (GtkWidget       *widget);
80

81 82 83 84 85 86 87 88
static void  gimp_drawable_preview_draw_original (GimpPreview     *preview);
static void  gimp_drawable_preview_draw_thumb    (GimpPreview     *preview,
                                                  GimpPreviewArea *area,
                                                  gint             width,
                                                  gint             height);
static void  gimp_drawable_preview_draw_buffer   (GimpPreview     *preview,
                                                  const guchar    *buffer,
                                                  gint             rowstride);
89

90 91
static void  gimp_drawable_preview_set_drawable  (GimpDrawablePreview *preview,
                                                  GimpDrawable        *drawable);
92

93

94 95
G_DEFINE_TYPE_WITH_PRIVATE (GimpDrawablePreview, gimp_drawable_preview,
                            GIMP_TYPE_SCROLLED_PREVIEW)
96

97
#define parent_class gimp_drawable_preview_parent_class
98

Sven Neumann's avatar
Sven Neumann committed
99 100
static gint gimp_drawable_preview_counter = 0;

101 102 103 104

static void
gimp_drawable_preview_class_init (GimpDrawablePreviewClass *klass)
{
105 106 107
  GObjectClass     *object_class  = G_OBJECT_CLASS (klass);
  GtkWidgetClass   *widget_class  = GTK_WIDGET_CLASS (klass);
  GimpPreviewClass *preview_class = GIMP_PREVIEW_CLASS (klass);
108

109 110 111 112
  object_class->constructed   = gimp_drawable_preview_constructed;
  object_class->dispose       = gimp_drawable_preview_dispose;
  object_class->get_property  = gimp_drawable_preview_get_property;
  object_class->set_property  = gimp_drawable_preview_set_property;
113

114
  widget_class->style_updated = gimp_drawable_preview_style_updated;
115

116 117 118
  preview_class->draw         = gimp_drawable_preview_draw_original;
  preview_class->draw_thumb   = gimp_drawable_preview_draw_thumb;
  preview_class->draw_buffer  = gimp_drawable_preview_draw_buffer;
119

120 121 122 123 124 125 126
  /**
   * GimpDrawablePreview:drawable-id:
   *
   * The drawable the #GimpDrawablePreview is attached to.
   *
   * Since: 2.10
   */
127 128 129 130 131 132 133
  g_object_class_install_property (object_class, PROP_DRAWABLE,
                                   g_param_spec_object ("drawable",
                                                        "Drawable",
                                                        "The drawable this preview is attached to",
                                                        GIMP_TYPE_DRAWABLE,
                                                        GIMP_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));
134

135 136
}

137 138 139
static void
gimp_drawable_preview_init (GimpDrawablePreview *preview)
{
140
  preview->priv = gimp_drawable_preview_get_instance_private (preview);
141

142
  g_object_set (gimp_preview_get_area (GIMP_PREVIEW (preview)),
143 144 145 146 147
                "check-size", gimp_check_size (),
                "check-type", gimp_check_type (),
                NULL);
}

148 149
static void
gimp_drawable_preview_constructed (GObject *object)
150
{
Sven Neumann's avatar
Sven Neumann committed
151
  gchar           *data_name;
152
  PreviewSettings  settings;
Sven Neumann's avatar
Sven Neumann committed
153

154
  G_OBJECT_CLASS (parent_class)->constructed (object);
155

Sven Neumann's avatar
Sven Neumann committed
156 157 158
  data_name = g_strdup_printf ("%s-drawable-preview-%d",
                               g_get_prgname (),
                               ++gimp_drawable_preview_counter);
159 160

  if (gimp_get_data (data_name, &settings))
Sven Neumann's avatar
Sven Neumann committed
161 162 163 164 165
    {
      gimp_preview_set_update (GIMP_PREVIEW (object), settings.update);
      gimp_scrolled_preview_set_position (GIMP_SCROLLED_PREVIEW (object),
                                          settings.x, settings.y);
    }
166

Sven Neumann's avatar
Sven Neumann committed
167 168
  g_object_set_data_full (object, "gimp-drawable-preview-data-name",
                          data_name, (GDestroyNotify) g_free);
169 170
}

171 172 173
static void
gimp_drawable_preview_dispose (GObject *object)
{
174 175
  GimpDrawablePreviewPrivate *priv = GET_PRIVATE (object);
  const gchar                *data_name;
176

177 178
  data_name = g_object_get_data (G_OBJECT (object),
                                 "gimp-drawable-preview-data-name");
179 180 181 182 183
  if (data_name)
    {
      GimpPreview     *preview = GIMP_PREVIEW (object);
      PreviewSettings  settings;

184
      gimp_preview_get_position (preview, &settings.x, &settings.y);
185 186 187 188 189
      settings.update = gimp_preview_get_update (preview);

      gimp_set_data (data_name, &settings, sizeof (PreviewSettings));
    }

190 191
  g_clear_object (&priv->drawable);

192 193 194
  G_OBJECT_CLASS (parent_class)->dispose (object);
}

195 196
static void
gimp_drawable_preview_get_property (GObject    *object,
Sven Neumann's avatar
Sven Neumann committed
197 198 199
                                    guint       property_id,
                                    GValue     *value,
                                    GParamSpec *pspec)
200 201 202 203 204
{
  GimpDrawablePreview *preview = GIMP_DRAWABLE_PREVIEW (object);

  switch (property_id)
    {
205 206 207
    case PROP_DRAWABLE:
      g_value_set_object (value,
                          gimp_drawable_preview_get_drawable (preview));
208 209 210 211 212 213 214 215 216 217
      break;

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

static void
gimp_drawable_preview_set_property (GObject      *object,
Sven Neumann's avatar
Sven Neumann committed
218 219 220
                                    guint         property_id,
                                    const GValue *value,
                                    GParamSpec   *pspec)
221
{
222
  GimpDrawablePreview *preview = GIMP_DRAWABLE_PREVIEW (object);
223 224 225

  switch (property_id)
    {
226 227 228
    case PROP_DRAWABLE:
      gimp_drawable_preview_set_drawable (preview,
                                          g_value_dup_object (value));
229 230 231 232 233 234 235 236
      break;

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

237
static void
238
gimp_drawable_preview_style_updated (GtkWidget *widget)
239 240
{
  GimpPreview *preview = GIMP_PREVIEW (widget);
241
  GtkWidget   *area    = gimp_preview_get_area (preview);
242

243
  GTK_WIDGET_CLASS (parent_class)->style_updated (widget);
244

245
  if (area)
246
    {
247 248
      gint xmin, ymin;
      gint xmax, ymax;
249 250
      gint size;

251 252
      gimp_preview_get_bounds (preview, &xmin, &ymin, &xmax, &ymax);

253 254 255
      gtk_widget_style_get (widget,
                            "size", &size,
                            NULL);
256

257 258 259
      gtk_widget_set_size_request (area,
                                   MIN (xmax - xmin, size),
                                   MIN (ymax - ymin, size));
260
    }
261 262 263
}

static void
264
gimp_drawable_preview_draw_original (GimpPreview *preview)
265
{
266
  GimpDrawablePreviewPrivate *priv = GET_PRIVATE (preview);
267 268
  guchar                     *buffer;
  gint                        width, height;
269 270 271
  gint                        xoff, yoff;
  gint                        xmin, ymin;
  gint                        xmax, ymax;
272 273
  gint                        bpp;
  GimpImageType               type;
274

275
  if (priv->drawable == NULL)
276 277
    return;

278 279 280 281 282 283
  gimp_preview_get_size (preview, &width, &height);
  gimp_preview_get_offsets (preview, &xoff, &yoff);
  gimp_preview_get_bounds (preview, &xmin, &ymin, &xmax, &ymax);

  xoff = CLAMP (xoff, 0, xmax - xmin - width);
  yoff = CLAMP (yoff, 0, ymax - ymin - height);
284

285
  gimp_preview_set_offsets (preview, xoff, yoff);
286

287
  buffer = gimp_drawable_get_sub_thumbnail_data (priv->drawable,
288 289 290
                                                 xoff + xmin,
                                                 yoff + ymin,
                                                 width, height,
291 292 293 294 295 296 297 298 299 300 301 302
                                                 &width, &height, &bpp);

  switch (bpp)
    {
    case 1: type = GIMP_GRAY_IMAGE; break;
    case 2: type = GIMP_GRAYA_IMAGE; break;
    case 3: type = GIMP_RGB_IMAGE; break;
    case 4: type = GIMP_RGBA_IMAGE; break;
    default:
      g_free (buffer);
      return;
    }
303

304
  gimp_preview_area_draw (GIMP_PREVIEW_AREA (gimp_preview_get_area (preview)),
305
                          0, 0, width, height, type, buffer, width * bpp);
306 307 308
  g_free (buffer);
}

309
static void
310 311 312 313
gimp_drawable_preview_draw_thumb (GimpPreview     *preview,
                                  GimpPreviewArea *area,
                                  gint             width,
                                  gint             height)
314
{
315
  GimpDrawablePreviewPrivate *priv = GET_PRIVATE (preview);
316

317 318
  if (priv->drawable)
    _gimp_drawable_preview_area_draw_thumb (area, priv->drawable,
319
                                            width, height);
320
}
321

322 323
void
_gimp_drawable_preview_area_draw_thumb (GimpPreviewArea *area,
324
                                        GimpDrawable    *drawable,
Sven Neumann's avatar
Sven Neumann committed
325 326
                                        gint             width,
                                        gint             height)
327 328 329 330 331 332 333 334
{
  guchar *buffer;
  gint    x1, y1, x2, y2;
  gint    bpp;
  gint    size = 100;
  gint    nav_width, nav_height;

  g_return_if_fail (GIMP_IS_PREVIEW_AREA (area));
335 336
  g_return_if_fail (gimp_item_is_valid (GIMP_ITEM (drawable)));
  g_return_if_fail (gimp_item_is_drawable (GIMP_ITEM (drawable)));
337

338
  if (_gimp_drawable_preview_get_bounds (drawable, &x1, &y1, &x2, &y2))
339 340 341 342 343 344
    {
      width  = x2 - x1;
      height = y2 - y1;
    }
  else
    {
345 346
      width  = gimp_drawable_width  (drawable);
      height = gimp_drawable_height (drawable);
347 348 349 350 351 352 353 354 355 356 357 358 359
    }

  if (width > height)
    {
      nav_width  = MIN (width, size);
      nav_height = (height * nav_width) / width;
    }
  else
    {
      nav_height = MIN (height, size);
      nav_width  = (width * nav_height) / height;
    }

360
  if (_gimp_drawable_preview_get_bounds (drawable, &x1, &y1, &x2, &y2))
361
    {
362
      buffer = gimp_drawable_get_sub_thumbnail_data (drawable,
363
                                                     x1, y1, x2 - x1, y2 - y1,
364
                                                     &nav_width, &nav_height,
Sven Neumann's avatar
Sven Neumann committed
365
                                                     &bpp);
366 367 368
    }
  else
    {
369
      buffer = gimp_drawable_get_thumbnail_data (drawable,
370
                                                 &nav_width, &nav_height,
Sven Neumann's avatar
Sven Neumann committed
371
                                                 &bpp);
372
    }
373 374 375

  if (buffer)
    {
376
      GimpImageType type;
377

378
      gtk_widget_set_size_request (GTK_WIDGET (area), nav_width, nav_height);
379 380 381 382 383
      gtk_widget_show (GTK_WIDGET (area));
      gtk_widget_realize (GTK_WIDGET (area));

      switch (bpp)
        {
384 385 386 387 388 389 390
        case 1:  type = GIMP_GRAY_IMAGE;   break;
        case 2:  type = GIMP_GRAYA_IMAGE;  break;
        case 3:  type = GIMP_RGB_IMAGE;    break;
        case 4:  type = GIMP_RGBA_IMAGE;   break;
        default:
          g_free (buffer);
          return;
391 392
        }

393
      gimp_preview_area_draw (area,
394 395
                              0, 0, nav_width, nav_height,
                              type, buffer, bpp * nav_width);
396 397 398 399
      g_free (buffer);
    }
}

400 401 402 403 404 405 406 407 408
static void
gimp_drawable_preview_draw_area (GimpDrawablePreview *preview,
                                 gint                 x,
                                 gint                 y,
                                 gint                 width,
                                 gint                 height,
                                 const guchar        *buf,
                                 gint                 rowstride)
{
409
  GimpDrawablePreviewPrivate *priv         = GET_PRIVATE (preview);
410
  GimpPreview                *gimp_preview = GIMP_PREVIEW (preview);
411
  GtkWidget                  *area         = gimp_preview_get_area (gimp_preview);
412
  GimpImage                  *image;
413 414
  gint                        xmin, ymin;
  gint                        xoff, yoff;
415

416 417 418
  gimp_preview_get_bounds (gimp_preview, &xmin, &ymin, NULL, NULL);
  gimp_preview_get_offsets (gimp_preview, &xoff, &yoff);

419
  image = gimp_item_get_image (GIMP_ITEM (priv->drawable));
420

421
  if (gimp_selection_is_empty (image))
422
    {
423 424 425
      gimp_preview_area_draw (GIMP_PREVIEW_AREA (area),
                              x - xoff - xmin,
                              y - yoff - ymin,
426 427
                              width,
                              height,
428
                              gimp_drawable_type (priv->drawable),
429 430 431 432
                              buf, rowstride);
    }
  else
    {
433 434 435 436 437
      gint offset_x, offset_y;
      gint mask_x, mask_y;
      gint mask_width, mask_height;
      gint draw_x, draw_y;
      gint draw_width, draw_height;
438

439
      gimp_drawable_offsets (priv->drawable, &offset_x, &offset_y);
440

441
      if (gimp_drawable_mask_intersect (priv->drawable,
442 443 444 445 446 447 448 449
                                        &mask_x, &mask_y,
                                        &mask_width, &mask_height) &&
          gimp_rectangle_intersect (mask_x, mask_y,
                                    mask_width, mask_height,
                                    x, y, width, height,
                                    &draw_x, &draw_y,
                                    &draw_width, &draw_height))
        {
450 451 452 453 454 455
          GimpImageType   type;
          GimpSelection  *selection;
          guchar         *src;
          guchar         *sel;
          gint            d_w, d_h, d_bpp;
          gint            s_w, s_h, s_bpp;
456 457 458 459 460 461 462

          d_w = draw_width;
          d_h = draw_height;

          s_w = draw_width;
          s_h = draw_height;

463
          selection = gimp_image_get_selection (image);
464

465
          src = gimp_drawable_get_sub_thumbnail_data (priv->drawable,
466 467 468 469 470
                                                      draw_x, draw_y,
                                                      draw_width, draw_height,
                                                      &d_w, &d_h,
                                                      &d_bpp);

471
          sel = gimp_drawable_get_sub_thumbnail_data (GIMP_DRAWABLE (selection),
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
                                                      draw_x + offset_x,
                                                      draw_y + offset_y,
                                                      draw_width, draw_height,
                                                      &s_w, &s_h,
                                                      &s_bpp);

          switch (d_bpp)
            {
            case 1:  type = GIMP_GRAY_IMAGE;   break;
            case 2:  type = GIMP_GRAYA_IMAGE;  break;
            case 3:  type = GIMP_RGB_IMAGE;    break;
            case 4:  type = GIMP_RGBA_IMAGE;   break;
            default:
              g_free (sel);
              g_free (src);
              return;
            }
489

490 491 492
          gimp_preview_area_mask (GIMP_PREVIEW_AREA (area),
                                  draw_x - xoff - xmin,
                                  draw_y - yoff - ymin,
493 494
                                  draw_width,
                                  draw_height,
495 496
                                  type,
                                  src, draw_width * d_bpp,
497
                                  (buf +
498 499
                                   (draw_x - x) * d_bpp +
                                   (draw_y - y) * d_w * d_bpp),
500
                                  rowstride,
501
                                  sel, s_w);
502 503 504 505

          g_free (sel);
          g_free (src);
        }
506 507 508
    }
}

509 510 511 512 513
static void
gimp_drawable_preview_draw_buffer (GimpPreview  *preview,
                                   const guchar *buffer,
                                   gint          rowstride)
{
514 515 516 517 518 519
  gint x, y;
  gint width, height;

  gimp_preview_get_position (preview, &x, &y);
  gimp_preview_get_size (preview, &width, &height);

520
  gimp_drawable_preview_draw_area (GIMP_DRAWABLE_PREVIEW (preview),
521 522
                                   x, y,
                                   width, height,
523 524 525
                                   buffer, rowstride);
}

526
static void
527 528
gimp_drawable_preview_set_drawable (GimpDrawablePreview *drawable_preview,
                                    GimpDrawable        *drawable)
529 530
{
  GimpPreview                *preview = GIMP_PREVIEW (drawable_preview);
531
  GimpDrawablePreviewPrivate *priv    = GET_PRIVATE (preview);
532 533
  gint                        x1, y1, x2, y2;

534
  g_return_if_fail (priv->drawable == NULL);
535

536
  priv->drawable = drawable;
537

538
  _gimp_drawable_preview_get_bounds (drawable, &x1, &y1, &x2, &y2);
539

540
  gimp_preview_set_bounds (preview, x1, y1, x2, y2);
541

542
  if (gimp_drawable_is_indexed (drawable))
543
    {
544
      GimpImage *image = gimp_item_get_image (GIMP_ITEM (drawable));
545
      GtkWidget *area  = gimp_preview_get_area (preview);
546 547
      guchar    *cmap;
      gint       num_colors;
548

549
      cmap = gimp_image_get_colormap (image, &num_colors);
550
      gimp_preview_area_set_colormap (GIMP_PREVIEW_AREA (area),
Michael Natterer's avatar
Michael Natterer committed
551
                                      cmap, num_colors);
552 553
      g_free (cmap);
    }
554
}
555

556 557 558 559

#define MAX3(a, b, c)  (MAX (MAX ((a), (b)), (c)))
#define MIN3(a, b, c)  (MIN (MIN ((a), (b)), (c)))

560
gboolean
561 562 563 564 565
_gimp_drawable_preview_get_bounds (GimpDrawable *drawable,
                                   gint         *xmin,
                                   gint         *ymin,
                                   gint         *xmax,
                                   gint         *ymax)
566 567 568 569 570 571 572 573 574
{
  gint     width;
  gint     height;
  gint     offset_x;
  gint     offset_y;
  gint     x1, y1;
  gint     x2, y2;
  gboolean retval;

575 576
  g_return_val_if_fail (gimp_item_is_valid (GIMP_ITEM (drawable)), FALSE);
  g_return_val_if_fail (gimp_item_is_drawable (GIMP_ITEM (drawable)), FALSE);
577

578 579
  width  = gimp_drawable_width (drawable);
  height = gimp_drawable_height (drawable);
580

581
  retval = gimp_drawable_mask_bounds (drawable, &x1, &y1, &x2, &y2);
582

583
  gimp_drawable_offsets (drawable, &offset_x, &offset_y);
584 585 586 587 588 589 590 591 592 593

  *xmin = MAX3 (x1 - SELECTION_BORDER, 0, - offset_x);
  *ymin = MAX3 (y1 - SELECTION_BORDER, 0, - offset_y);
  *xmax = MIN3 (x2 + SELECTION_BORDER, width,  width  + offset_x);
  *ymax = MIN3 (y2 + SELECTION_BORDER, height, height + offset_y);

  return retval;
}


594
/**
595 596
 * gimp_drawable_preview_new_from_drawable:
 * @drawable: (transfer none): a drawable
597
 *
598
 * Creates a new #GimpDrawablePreview widget for @drawable.
599 600 601 602 603 604
 *
 * Returns: A pointer to the new #GimpDrawablePreview widget.
 *
 * Since: 2.10
 **/
GtkWidget *
605
gimp_drawable_preview_new_from_drawable (GimpDrawable *drawable)
606
{
607 608
  g_return_val_if_fail (gimp_item_is_valid (GIMP_ITEM (drawable)), NULL);
  g_return_val_if_fail (gimp_item_is_drawable (GIMP_ITEM (drawable)), NULL);
609 610

  return g_object_new (GIMP_TYPE_DRAWABLE_PREVIEW,
611
                       "drawable", drawable,
612 613 614 615
                       NULL);
}

/**
616
 * gimp_drawable_preview_get_drawable:
617 618
 * @preview:   a #GimpDrawablePreview widget
 *
619 620
 * Returns: (transfer none): the drawable that has been passed to
 *          gimp_drawable_preview_new_from_drawable().
621 622 623
 *
 * Since: 2.10
 **/
624 625
GimpDrawable *
gimp_drawable_preview_get_drawable (GimpDrawablePreview *preview)
626
{
627
  g_return_val_if_fail (GIMP_IS_DRAWABLE_PREVIEW (preview), NULL);
628

629
  return GET_PRIVATE (preview)->drawable;
630
}