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

#include "config.h"

20 21
#include <string.h>

22
#include <cairo.h>
23
#include <gdk-pixbuf/gdk-pixbuf.h>
24
#include <gegl.h>
25

26
#include "libgimpbase/gimpbase.h"
27
#include "libgimpmath/gimpmath.h"
28

29 30
#include "core-types.h"

31
#include "gegl/gimp-gegl-apply-operation.h"
32
#include "gegl/gimp-gegl-loops.h"
33 34
#include "gegl/gimp-gegl-mask-combine.h"

35
#include "gimpchannel.h"
36
#include "gimpchannel-select.h"
37
#include "gimpchannel-combine.h"
38 39
#include "gimppickable.h"
#include "gimppickable-contiguous-region.h"
40 41
#include "gimpscanconvert.h"

42 43 44
#include "vectors/gimpstroke.h"
#include "vectors/gimpvectors.h"

45
#include "gimp-intl.h"
46

47

48 49
/*  basic selection functions  */

50
void
51 52 53 54 55 56 57 58
gimp_channel_select_rectangle (GimpChannel    *channel,
                               gint            x,
                               gint            y,
                               gint            w,
                               gint            h,
                               GimpChannelOps  op,
                               gboolean        feather,
                               gdouble         feather_radius_x,
59 60
                               gdouble         feather_radius_y,
                               gboolean        push_undo)
61
{
62
  g_return_if_fail (GIMP_IS_CHANNEL (channel));
63
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (channel)));
64

65
  if (push_undo)
66
    gimp_channel_push_undo (channel, C_("undo-type", "Rectangle Select"));
67

68
  /*  if applicable, replace the current selection  */
69
  if (op == GIMP_CHANNEL_OP_REPLACE)
70
    gimp_channel_clear (channel, NULL, FALSE);
71 72 73 74

  /*  if feathering for rect, make a new mask with the
   *  rectangle and feather that with the old mask
   */
75
  if (feather || op == GIMP_CHANNEL_OP_INTERSECT)
76
    {
77 78 79 80 81 82 83
      GimpItem   *item = GIMP_ITEM (channel);
      GeglBuffer *add_on;

      add_on = gegl_buffer_new (GEGL_RECTANGLE (0, 0,
                                                gimp_item_get_width  (item),
                                                gimp_item_get_height (item)),
                                babl_format ("Y float"));
84

85
      gimp_gegl_mask_combine_rect (add_on, GIMP_CHANNEL_OP_ADD, x, y, w, h);
86 87

      if (feather)
88
        gimp_gegl_apply_feather (add_on, NULL, NULL, add_on, NULL,
89 90
                                 feather_radius_x,
                                 feather_radius_y);
91

92
      gimp_channel_combine_buffer (channel, add_on, op, 0, 0);
93
      g_object_unref (add_on);
94 95 96
    }
  else
    {
97
      gimp_channel_combine_rect (channel, op, x, y, w, h);
98 99 100 101
    }
}

void
102 103 104 105 106 107 108 109 110
gimp_channel_select_ellipse (GimpChannel    *channel,
                             gint            x,
                             gint            y,
                             gint            w,
                             gint            h,
                             GimpChannelOps  op,
                             gboolean        antialias,
                             gboolean        feather,
                             gdouble         feather_radius_x,
111 112
                             gdouble         feather_radius_y,
                             gboolean        push_undo)
113
{
114
  g_return_if_fail (GIMP_IS_CHANNEL (channel));
115
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (channel)));
116

117
  if (push_undo)
118
    gimp_channel_push_undo (channel, C_("undo-type", "Ellipse Select"));
119

120
  /*  if applicable, replace the current selection  */
121
  if (op == GIMP_CHANNEL_OP_REPLACE)
122
    gimp_channel_clear (channel, NULL, FALSE);
123 124 125 126

  /*  if feathering for rect, make a new mask with the
   *  rectangle and feather that with the old mask
   */
127
  if (feather || op == GIMP_CHANNEL_OP_INTERSECT)
128
    {
129 130 131 132 133 134 135
      GimpItem   *item = GIMP_ITEM (channel);
      GeglBuffer *add_on;

      add_on = gegl_buffer_new (GEGL_RECTANGLE (0, 0,
                                                gimp_item_get_width  (item),
                                                gimp_item_get_height (item)),
                                babl_format ("Y float"));
136

137 138
      gimp_gegl_mask_combine_ellipse (add_on, GIMP_CHANNEL_OP_ADD,
                                      x, y, w, h, antialias);
139 140

      if (feather)
141
        gimp_gegl_apply_feather (add_on, NULL, NULL, add_on, NULL,
142 143
                                 feather_radius_x,
                                 feather_radius_y);
144

145
      gimp_channel_combine_buffer (channel, add_on, op, 0, 0);
146
      g_object_unref (add_on);
147 148 149
    }
  else
    {
150
      gimp_channel_combine_ellipse (channel, op, x, y, w, h, antialias);
151 152 153
    }
}

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
void
gimp_channel_select_round_rect (GimpChannel         *channel,
                                gint                 x,
                                gint                 y,
                                gint                 w,
                                gint                 h,
                                gdouble              corner_radius_x,
                                gdouble              corner_radius_y,
                                GimpChannelOps       op,
                                gboolean             antialias,
                                gboolean             feather,
                                gdouble              feather_radius_x,
                                gdouble              feather_radius_y,
                                gboolean             push_undo)
{
  g_return_if_fail (GIMP_IS_CHANNEL (channel));
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (channel)));

  if (push_undo)
173
    gimp_channel_push_undo (channel, C_("undo-type", "Rounded Rectangle Select"));
174 175 176 177 178 179 180 181 182 183

  /*  if applicable, replace the current selection  */
  if (op == GIMP_CHANNEL_OP_REPLACE)
    gimp_channel_clear (channel, NULL, FALSE);

  /*  if feathering for rect, make a new mask with the
   *  rectangle and feather that with the old mask
   */
  if (feather || op == GIMP_CHANNEL_OP_INTERSECT)
    {
184 185 186 187 188 189 190 191 192 193 194 195
      GimpItem   *item = GIMP_ITEM (channel);
      GeglBuffer *add_on;

      add_on = gegl_buffer_new (GEGL_RECTANGLE (0, 0,
                                                gimp_item_get_width  (item),
                                                gimp_item_get_height (item)),
                                babl_format ("Y float"));

      gimp_gegl_mask_combine_ellipse_rect (add_on, GIMP_CHANNEL_OP_ADD,
                                           x, y, w, h,
                                           corner_radius_x, corner_radius_y,
                                           antialias);
196 197

      if (feather)
198
        gimp_gegl_apply_feather (add_on, NULL, NULL, add_on, NULL,
199 200
                                 feather_radius_x,
                                 feather_radius_y);
201

202
      gimp_channel_combine_buffer (channel, add_on, op, 0, 0);
203 204 205 206 207 208 209 210 211
      g_object_unref (add_on);
    }
  else
    {
      gimp_channel_combine_ellipse_rect (channel, op, x, y, w, h,
                                         corner_radius_x, corner_radius_y,
                                         antialias);
    }
}
212 213 214

/*  select by GimpScanConvert functions  */

215
void
216 217 218 219 220 221 222 223 224
gimp_channel_select_scan_convert (GimpChannel     *channel,
                                  const gchar     *undo_desc,
                                  GimpScanConvert *scan_convert,
                                  gint             offset_x,
                                  gint             offset_y,
                                  GimpChannelOps   op,
                                  gboolean         antialias,
                                  gboolean         feather,
                                  gdouble          feather_radius_x,
225 226
                                  gdouble          feather_radius_y,
                                  gboolean         push_undo)
227
{
228 229
  GimpItem   *item;
  GeglBuffer *add_on;
230

231
  g_return_if_fail (GIMP_IS_CHANNEL (channel));
232
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (channel)));
233 234
  g_return_if_fail (undo_desc != NULL);
  g_return_if_fail (scan_convert != NULL);
235

236 237
  if (push_undo)
    gimp_channel_push_undo (channel, undo_desc);
238

239
  /*  if applicable, replace the current selection  */
240
  if (op == GIMP_CHANNEL_OP_REPLACE)
241
    gimp_channel_clear (channel, NULL, FALSE);
242

243 244
  item = GIMP_ITEM (channel);

245 246 247 248
  add_on = gegl_buffer_new (GEGL_RECTANGLE (0, 0,
                                            gimp_item_get_width  (item),
                                            gimp_item_get_height (item)),
                            babl_format ("Y float"));
249

250
  gimp_scan_convert_render (scan_convert, add_on,
251
                            offset_x, offset_y, antialias);
252

253
  if (feather)
254
    gimp_gegl_apply_feather (add_on, NULL, NULL, add_on, NULL,
255 256
                             feather_radius_x,
                             feather_radius_y);
257

258
  gimp_channel_combine_buffer (channel, add_on, op, 0, 0);
259
  g_object_unref (add_on);
260 261
}

262
void
263 264 265 266 267 268 269 270 271 272
gimp_channel_select_polygon (GimpChannel       *channel,
                             const gchar       *undo_desc,
                             gint               n_points,
                             const GimpVector2 *points,
                             GimpChannelOps     op,
                             gboolean           antialias,
                             gboolean           feather,
                             gdouble            feather_radius_x,
                             gdouble            feather_radius_y,
                             gboolean           push_undo)
273 274 275 276
{
  GimpScanConvert *scan_convert;

  g_return_if_fail (GIMP_IS_CHANNEL (channel));
277
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (channel)));
278 279 280 281 282 283 284 285
  g_return_if_fail (undo_desc != NULL);

  scan_convert = gimp_scan_convert_new ();

  gimp_scan_convert_add_polyline (scan_convert, n_points, points, TRUE);

  gimp_channel_select_scan_convert (channel, undo_desc, scan_convert, 0, 0,
                                    op, antialias, feather,
286 287
                                    feather_radius_x, feather_radius_y,
                                    push_undo);
288 289 290 291

  gimp_scan_convert_free (scan_convert);
}

292
void
293 294 295 296 297 298 299
gimp_channel_select_vectors (GimpChannel    *channel,
                             const gchar    *undo_desc,
                             GimpVectors    *vectors,
                             GimpChannelOps  op,
                             gboolean        antialias,
                             gboolean        feather,
                             gdouble         feather_radius_x,
300 301
                             gdouble         feather_radius_y,
                             gboolean        push_undo)
302
{
303
  const GimpBezierDesc *bezier;
304

305
  g_return_if_fail (GIMP_IS_CHANNEL (channel));
306
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (channel)));
307
  g_return_if_fail (undo_desc != NULL);
308 309
  g_return_if_fail (GIMP_IS_VECTORS (vectors));

310
  bezier = gimp_vectors_get_bezier (vectors);
311

312
  if (bezier && bezier->num_data > 4)
313
    {
314
      GimpScanConvert *scan_convert;
315

316 317
      scan_convert = gimp_scan_convert_new ();
      gimp_scan_convert_add_bezier (scan_convert, bezier);
318

319 320 321 322
      gimp_channel_select_scan_convert (channel, undo_desc, scan_convert, 0, 0,
                                        op, antialias, feather,
                                        feather_radius_x, feather_radius_y,
                                        push_undo);
323

324
      gimp_scan_convert_free (scan_convert);
325
    }
326 327
}

328 329 330

/*  select by GimpChannel functions  */

331
void
332 333 334 335 336 337 338 339 340
gimp_channel_select_buffer (GimpChannel    *channel,
                            const gchar    *undo_desc,
                            GeglBuffer     *add_on,
                            gint            offset_x,
                            gint            offset_y,
                            GimpChannelOps  op,
                            gboolean        feather,
                            gdouble         feather_radius_x,
                            gdouble         feather_radius_y)
341 342
{
  g_return_if_fail (GIMP_IS_CHANNEL (channel));
343
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (channel)));
344
  g_return_if_fail (undo_desc != NULL);
345
  g_return_if_fail (GEGL_IS_BUFFER (add_on));
346

347
  gimp_channel_push_undo (channel, undo_desc);
348

349
  /*  if applicable, replace the current selection  */
350
  if (op == GIMP_CHANNEL_OP_REPLACE)
351
    gimp_channel_clear (channel, NULL, FALSE);
352

353 354
  if (feather || op == GIMP_CHANNEL_OP_INTERSECT)
    {
355 356 357 358 359 360 361
      GimpItem   *item = GIMP_ITEM (channel);
      GeglBuffer *add_on2;

      add_on2 = gegl_buffer_new (GEGL_RECTANGLE (0, 0,
                                                 gimp_item_get_width  (item),
                                                 gimp_item_get_height (item)),
                                 babl_format ("Y float"));
362

363
      gimp_gegl_mask_combine_buffer (add_on2, add_on,
364 365
                                     GIMP_CHANNEL_OP_ADD,
                                     offset_x, offset_y);
366 367

      if (feather)
368
        gimp_gegl_apply_feather (add_on2, NULL, NULL, add_on2, NULL,
369 370
                                 feather_radius_x,
                                 feather_radius_y);
371

372
      gimp_channel_combine_buffer (channel, add_on2, op, 0, 0);
373
      g_object_unref (add_on2);
374 375 376
    }
  else
    {
377
      gimp_channel_combine_buffer (channel, add_on, op, offset_x, offset_y);
378
    }
379 380
}

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
void
gimp_channel_select_channel (GimpChannel    *channel,
                             const gchar    *undo_desc,
                             GimpChannel    *add_on,
                             gint            offset_x,
                             gint            offset_y,
                             GimpChannelOps  op,
                             gboolean        feather,
                             gdouble         feather_radius_x,
                             gdouble         feather_radius_y)
{
  g_return_if_fail (GIMP_IS_CHANNEL (channel));
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (channel)));
  g_return_if_fail (undo_desc != NULL);
  g_return_if_fail (GIMP_IS_CHANNEL (add_on));

  gimp_channel_select_buffer (channel, undo_desc,
                              gimp_drawable_get_buffer (GIMP_DRAWABLE (add_on)),
                              offset_x, offset_y, op,
                              feather,
                              feather_radius_x, feather_radius_y);
}

404
void
405
gimp_channel_select_alpha (GimpChannel    *channel,
406
                           GimpDrawable   *drawable,
407 408 409 410
                           GimpChannelOps  op,
                           gboolean        feather,
                           gdouble         feather_radius_x,
                           gdouble         feather_radius_y)
411
{
412 413
  GimpItem    *item;
  GimpChannel *add_on;
414
  gint         off_x, off_y;
415

416
  g_return_if_fail (GIMP_IS_CHANNEL (channel));
417
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (channel)));
418
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
419

420 421
  item = GIMP_ITEM (channel);

422
  if (gimp_drawable_has_alpha (drawable))
423 424
    {
      add_on = gimp_channel_new_from_alpha (gimp_item_get_image (item),
425
                                            drawable, NULL, NULL);
426 427 428 429 430 431 432
    }
  else
    {
      /*  no alpha is equivalent to completely opaque alpha,
       *  so simply select the whole layer's extents.  --mitch
       */
      add_on = gimp_channel_new_mask (gimp_item_get_image (item),
433 434
                                      gimp_item_get_width  (GIMP_ITEM (drawable)),
                                      gimp_item_get_height (GIMP_ITEM (drawable)));
435 436
      gimp_channel_all (add_on, FALSE);
    }
437

438
  gimp_item_get_offset (GIMP_ITEM (drawable), &off_x, &off_y);
439

440
  gimp_channel_select_channel (channel, C_("undo-type", "Alpha to Selection"), add_on,
441 442 443 444
                               off_x, off_y,
                               op, feather,
                               feather_radius_x,
                               feather_radius_y);
445
  g_object_unref (add_on);
446 447 448
}

void
449 450 451 452 453 454
gimp_channel_select_component (GimpChannel     *channel,
                               GimpChannelType  component,
                               GimpChannelOps   op,
                               gboolean         feather,
                               gdouble          feather_radius_x,
                               gdouble          feather_radius_y)
455
{
456 457
  GimpItem    *item;
  GimpChannel *add_on;
458
  const gchar *desc;
459
  gchar       *undo_desc;
460

461
  g_return_if_fail (GIMP_IS_CHANNEL (channel));
462
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (channel)));
463

464 465 466
  item = GIMP_ITEM (channel);

  add_on = gimp_channel_new_from_component (gimp_item_get_image (item),
467
                                            component, NULL, NULL);
468 469

  if (feather)
470
    gimp_channel_feather (add_on,
471 472 473 474
                          feather_radius_x,
                          feather_radius_y,
                          FALSE /* no undo */);

475 476
  gimp_enum_get_value (GIMP_TYPE_CHANNEL_TYPE, component,
                       NULL, NULL, &desc, NULL);
477

478
  undo_desc = g_strdup_printf (C_("undo-type", "%s Channel to Selection"), desc);
479

480
  gimp_channel_select_channel (channel, undo_desc, add_on,
481 482
                               0, 0, op,
                               FALSE, 0.0, 0.0);
483

484
  g_free (undo_desc);
485
  g_object_unref (add_on);
486 487
}

488
void
489 490 491 492 493
gimp_channel_select_fuzzy (GimpChannel         *channel,
                           GimpDrawable        *drawable,
                           gboolean             sample_merged,
                           gint                 x,
                           gint                 y,
494
                           gfloat               threshold,
495 496
                           gboolean             select_transparent,
                           GimpSelectCriterion  select_criterion,
497
                           gboolean             diagonal_neighbors,
498 499 500 501 502
                           GimpChannelOps       op,
                           gboolean             antialias,
                           gboolean             feather,
                           gdouble              feather_radius_x,
                           gdouble              feather_radius_y)
503
{
504 505 506 507
  GimpPickable *pickable;
  GeglBuffer   *add_on;
  gint          add_on_x = 0;
  gint          add_on_y = 0;
508 509

  g_return_if_fail (GIMP_IS_CHANNEL (channel));
510
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (channel)));
511 512
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));

513 514 515 516
  if (sample_merged)
    pickable = GIMP_PICKABLE (gimp_item_get_image (GIMP_ITEM (drawable)));
  else
    pickable = GIMP_PICKABLE (drawable);
517

518
  add_on = gimp_pickable_contiguous_region_by_seed (pickable,
519
                                                    NULL,
520 521 522 523
                                                    antialias,
                                                    threshold,
                                                    select_transparent,
                                                    select_criterion,
524
                                                    diagonal_neighbors,
525
                                                    0.92, /* TODO */
526
                                                    x, y);
527 528

  if (! sample_merged)
529
    gimp_item_get_offset (GIMP_ITEM (drawable), &add_on_x, &add_on_y);
530

531 532 533 534 535 536
  gimp_channel_select_buffer (channel, C_("undo-type", "Fuzzy Select"),
                              add_on, add_on_x, add_on_y,
                              op,
                              feather,
                              feather_radius_x,
                              feather_radius_y);
537 538 539 540
  g_object_unref (add_on);
}

void
541 542 543 544
gimp_channel_select_by_color (GimpChannel         *channel,
                              GimpDrawable        *drawable,
                              gboolean             sample_merged,
                              const GimpRGB       *color,
545
                              gfloat               threshold,
546 547 548 549 550 551 552
                              gboolean             select_transparent,
                              GimpSelectCriterion  select_criterion,
                              GimpChannelOps       op,
                              gboolean             antialias,
                              gboolean             feather,
                              gdouble              feather_radius_x,
                              gdouble              feather_radius_y)
553
{
554 555 556 557
  GimpPickable *pickable;
  GeglBuffer   *add_on;
  gint          add_on_x = 0;
  gint          add_on_y = 0;
558

559
  g_return_if_fail (GIMP_IS_CHANNEL (channel));
560
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (channel)));
561 562 563
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
  g_return_if_fail (color != NULL);

564 565 566 567 568 569 570 571 572 573 574
  if (sample_merged)
    pickable = GIMP_PICKABLE (gimp_item_get_image (GIMP_ITEM (drawable)));
  else
    pickable = GIMP_PICKABLE (drawable);

  add_on = gimp_pickable_contiguous_region_by_color (pickable,
                                                     antialias,
                                                     threshold,
                                                     select_transparent,
                                                     select_criterion,
                                                     color);
575 576

  if (! sample_merged)
577
    gimp_item_get_offset (GIMP_ITEM (drawable), &add_on_x, &add_on_y);
578

579 580 581 582 583 584
  gimp_channel_select_buffer (channel, C_("undo-type", "Select by Color"),
                              add_on, add_on_x, add_on_y,
                              op,
                              feather,
                              feather_radius_x,
                              feather_radius_y);
585
  g_object_unref (add_on);
586
}
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625

void
gimp_channel_select_by_index (GimpChannel    *channel,
                              GimpDrawable   *drawable,
                              gint            index,
                              GimpChannelOps  op,
                              gboolean        feather,
                              gdouble         feather_radius_x,
                              gdouble         feather_radius_y)
{
  GeglBuffer *add_on;
  gint        add_on_x = 0;
  gint        add_on_y = 0;

  g_return_if_fail (GIMP_IS_CHANNEL (channel));
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (channel)));
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
  g_return_if_fail (gimp_drawable_is_indexed (drawable));

  add_on = gegl_buffer_new (GEGL_RECTANGLE (0, 0,
                                            gimp_item_get_width  (GIMP_ITEM (drawable)),
                                            gimp_item_get_height (GIMP_ITEM (drawable))),
                            babl_format ("Y float"));

  gimp_gegl_index_to_mask (gimp_drawable_get_buffer (drawable), NULL,
                           gimp_drawable_get_format_without_alpha (drawable),
                           add_on, NULL,
                           index);

  gimp_item_get_offset (GIMP_ITEM (drawable), &add_on_x, &add_on_y);

  gimp_channel_select_buffer (channel, C_("undo-type", "Select by Indexed Color"),
                              add_on, add_on_x, add_on_y,
                              op,
                              feather,
                              feather_radius_x,
                              feather_radius_y);
  g_object_unref (add_on);
}