gimppaintcore-loops.cc 46.6 KB
Newer Older
Daniel Sabo's avatar
Daniel Sabo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 2013 Daniel Sabo
 *
 * 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 3 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
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
Daniel Sabo's avatar
Daniel Sabo committed
16
 */
17

Daniel Sabo's avatar
Daniel Sabo committed
18
#include "config.h"
19
#define GEGL_ITERATOR2_API
Daniel Sabo's avatar
Daniel Sabo committed
20
#include <gegl.h>
21
#include <gdk-pixbuf/gdk-pixbuf.h>
Daniel Sabo's avatar
Daniel Sabo committed
22

23 24 25
extern "C"
{

Daniel Sabo's avatar
Daniel Sabo committed
26 27
#include "paint-types.h"

28 29
#include "gegl/gimp-babl.h"

30 31
#include "operations/layer-modes/gimp-layer-modes.h"

32
#include "core/gimp-parallel.h"
Daniel Sabo's avatar
Daniel Sabo committed
33
#include "core/gimptempbuf.h"
34

35
#include "operations/layer-modes/gimpoperationlayermode.h"
36

Daniel Sabo's avatar
Daniel Sabo committed
37
#include "gimppaintcore-loops.h"
38

39 40
} /* extern "C" */

Daniel Sabo's avatar
Daniel Sabo committed
41

42 43 44 45
#define MIN_PARALLEL_SUB_SIZE 64
#define MIN_PARALLEL_SUB_AREA (MIN_PARALLEL_SUB_SIZE * MIN_PARALLEL_SUB_SIZE)


46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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
/* In order to avoid iterating over the same region of the same buffers
 * multiple times, when calling more than one of the paint-core loop functions
 * (hereafter referred to as "algorithms") in succession, we provide a single
 * function, gimp_paint_core_loops_process(), which can be used to perform
 * multiple algorithms in a row.  This function takes a pointer to a
 * GimpPaintCoreLoopsParams structure, providing the parameters for the
 * algorithms, and a GimpPaintCoreLoopsAlgorithm bitset, which specifies the
 * set of algorithms to run; currently, the algorithms are always run in a
 * fixed order.  For convenience, we provide public functions for the
 * individual algorithms, but they're merely wrappers around
 * gimp_paint_core_loops_process().
 *
 * We use some C++ magic to statically generate specialized versions of
 * gimp_paint_core_loops_process() for all possible combinations of algorithms,
 * and, where relevant, formats and input parameters, and to dispatch to the
 * correct version at runtime.
 *
 * To achieve this, each algorithm provides two components:
 *
 *   - The algorithm class template, which implements the algorithm, following
 *     a common interface.  See the AlgorithmBase class for a description of
 *     the interface.  Each algorithm class takes its base class as a template
 *     parameter, which allows us to construct a class hierarchy corresponding
 *     to a specific set of algorithms.  Some classes in the hierarchy are not
 *     algorithms themselves, but are rather helpers, which provide some
 *     functionality to the algorithms further down the hierarchy, such as
 *     access to specific buffers.
 *
 *   - A dispatch function, which takes the input parameters, the requested set
 *     of algorithms, the (type of) the current algorithm hierarchy, and a
 *     visitor object.  The function calls the visitor with a (potentially)
 *     modified hierarchy, depending on the input.  Ihe dispatch function for
 *     an algorithm checks if the requested set of algorithms contains a
 *     certain algorithm, adds the said algorithm to the hierarchy accordingly,
 *     and calls the visitor with the new hierarchy.  See the AlgorithmDispatch
 *     class, which provides a dispatch-function implementation which
 *     algorithms can use instead of providing their own dispatch function.
 *
 *     Helper classes in the hierarchy may also provide dispatch functions,
 *     which likewise modify the hierarchy based on the input parameters.  For
 *     example, the dispatch_paint_mask() function adds a certain PaintMask
 *     specialization to the hierarchy, depending on the format of the paint
 *     mask buffer; this can be used to specialize algorithms based on the mask
 *     format; an algorithm that depends on the paint mask may dispatch through
 *     this function, before modifying the hierarchy itself.
 *
 * The dispatch() function is used to construct an algorithm hierarchy by
 * dispatching through a list of functions.  gimp_paint_core_loops_process()
 * calls dispatch() with the full list of algorithm dispatch functions,
 * receiving in return the algorithm hierarchy matching the input.  It then
 * uses the algorithm interface to perform the actual processing.
 */


enum
Daniel Sabo's avatar
Daniel Sabo committed
101
{
102 103 104 105
  ALGORITHM_PAINT_BUF  = 1u << 31,
  ALGORITHM_PAINT_MASK = 1u << 30,
  ALGORITHM_STIPPLE    = 1u << 29
};
Daniel Sabo's avatar
Daniel Sabo committed
106

Jehan's avatar
Jehan committed
107

108 109 110 111 112
template <class T>
struct identity
{
  using type = T;
};
Daniel Sabo's avatar
Daniel Sabo committed
113 114


115 116 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 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 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
/* dispatch():
 *
 * Takes a list of dispatch function objects, and calls each of them, in order,
 * with the same 'params' and 'algorithms' parameters, passing 'algorithm' as
 * the input hierarchy to the first dispatch function, and passing the output
 * hierarchy of the previous dispatch function as the input hierarchy for the
 * next dispatch function.  Calls 'visitor' with the output hierarchy of the
 * last dispatch function.
 *
 * Each algorithm hierarchy should provide a 'filter' static data member, and
 * each dispatch function object should provide a 'mask' static data member.
 * If the bitwise-AND of the current hierarchy's 'filter' member and the
 * current dispatch function's 'mask' member is equal to 'mask', the dispatch
 * function is skipped.  This can be used to make sure that a class appears
 * only once in the hierarchy, even if its dispatch function is used multiple
 * times, or to prevent an algorithm from being dispatched, if it cannot be
 * used together with another algorithm.
 */

template <class Visitor,
          class Algorithm>
static inline void
dispatch (Visitor                         visitor,
          const GimpPaintCoreLoopsParams *params,
          GimpPaintCoreLoopsAlgorithm     algorithms,
          identity<Algorithm>             algorithm)
{
  visitor (algorithm);
}

template <class Algorithm,
          class Dispatch,
          gboolean = (Algorithm::filter & Dispatch::mask) == Dispatch::mask>
struct dispatch_impl
{
  template <class    Visitor,
            class... DispatchRest>
  static void
  apply (Visitor                         visitor,
         const GimpPaintCoreLoopsParams *params,
         GimpPaintCoreLoopsAlgorithm     algorithms,
         identity<Algorithm>             algorithm,
         Dispatch                        disp,
         DispatchRest...                 disp_rest)
  {
    disp (
      [&] (auto algorithm)
      {
        dispatch (visitor, params, algorithms, algorithm, disp_rest...);
      },
      params, algorithms, algorithm);
  }
};

template <class Algorithm,
          class Dispatch>
struct dispatch_impl<Algorithm, Dispatch, TRUE>
{
  template <class    Visitor,
            class... DispatchRest>
  static void
  apply (Visitor                         visitor,
         const GimpPaintCoreLoopsParams *params,
         GimpPaintCoreLoopsAlgorithm     algorithms,
         identity<Algorithm>             algorithm,
         Dispatch                        disp,
         DispatchRest...                 disp_rest)
  {
    dispatch (visitor, params, algorithms, algorithm, disp_rest...);
  }
};

template <class    Visitor,
          class    Algorithm,
          class    Dispatch,
          class... DispatchRest>
static inline void
dispatch (Visitor                         visitor,
          const GimpPaintCoreLoopsParams *params,
          GimpPaintCoreLoopsAlgorithm     algorithms,
          identity<Algorithm>             algorithm,
          Dispatch                        disp,
          DispatchRest...                 disp_rest)
{
  dispatch_impl<Algorithm, Dispatch>::apply (
    visitor, params, algorithms, algorithm, disp, disp_rest...);
}


/* value_to_float():
 *
 * Converts a component value to float.
 */

static inline gfloat
value_to_float (guint8 value)
{
  return value / 255.0f;
}

static inline gfloat
value_to_float (gfloat value)
{
  return value;
}

template <class T>
static inline gfloat
value_to_float (T value) = delete;


/* AlgorithmBase:
 *
 * The base class of the algorithm hierarchy.
 */

struct AlgorithmBase
{
  /* Used to filter-out dispatch functions; see the description of dispatch().
   * Algorithms that redefine 'filter' should bitwise-OR their filter with that
   * of their base class.
   */
  static constexpr guint          filter                 = 0;

  /* See CanvasBufferIterator. */
  static constexpr gint           canvas_buffer_iterator = -1;
  static constexpr GeglAccessMode canvas_buffer_access   = {};

  /* The current number of iterators used by the hierarchy.  Algorithms should
   * use the 'n_iterators' value of their base class as the base-index for
   * their iterators, and redefine 'n_iterators' by adding the number of
   * iterators they use to this value.
   */
  static constexpr gint           n_iterators            = 0;

  /* Non-static data members should be initialized in the constructor, and
   * should not be further modified.
   */
  explicit
  AlgorithmBase (const GimpPaintCoreLoopsParams *params)
  {
  }

  /* Algorithms should store their dynamic state in the 'State' member class
   * template.  This template will be instantiated with the most-derived type
   * of the hierarchy, which allows an algorithm to depend on the properties of
   * its descendants.  Algorithms that provide their own 'State' class should
   * derive it from the 'State' class of their base class, passing 'Derived' as
   * the template argument.
   *
   * Algorithms can be run in parallel on multiple threads.  In this case, each
   * thread uses its own 'State' object, while the algorithm object itself is
   * either shared, or is a copy of a shared algorithm object.  Either way, the
   * algorithm object itself is immutable, while the state object is mutable.
   */
  template <class Derived>
  struct State
  {
  };

  /* The 'init()' function is called once per state object before processing
   * starts, and should initialize the state object, and, if necessary, the
   * iterator.
   *
   * 'params' is the same parameter struct passed to the constructor.  'state'
   * is the state object.  'iter' is the iterator; each distinct state object
281 282
   * uses a distinct iterator; if the algorithm hierarchy doesn't use any
   * iterator, 'iter' may be NULL.  'roi' is the full region to be processed.
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
   * 'area' is the subregion to be processed by the current state object.
   *
   * An algorithm that overrides this function should call the 'init()'
   * function of its base class first, using the same arguments.
   */
  template <class Derived>
  void
  init (const GimpPaintCoreLoopsParams *params,
        State<Derived>                 *state,
        GeglBufferIterator             *iter,
        const GeglRectangle            *roi,
        const GeglRectangle            *area) const
  {
  }

  /* The 'init_step()' function is called once after each
   * 'gegl_buffer_iterator_next()' call, and should perform any necessary
   * initialization required before processing the current chunk.
   *
302 303
   * The parameters are the same as for 'init()', with the addition of 'rect',
   * which is the area of the current chunk.
304 305 306 307 308 309 310 311 312 313
   *
   * An algorithm that overrides this function should call the 'init_step()'
   * function of its base class first, using the same arguments.
   */
  template <class Derived>
  void
  init_step (const GimpPaintCoreLoopsParams *params,
             State<Derived>                 *state,
             GeglBufferIterator             *iter,
             const GeglRectangle            *roi,
314 315
             const GeglRectangle            *area,
             const GeglRectangle            *rect) const
316 317 318 319 320 321
  {
  }

  /* The 'process_row()' function is called for each row in the current chunk,
   * and should perform the actual processing.
   *
322 323
   * The parameters are the same as for 'init_step()', with the addition of
   * 'y', which is the current row.
324 325 326 327 328 329 330 331 332 333 334
   *
   * An algorithm that overrides this function should call the 'process_row()'
   * function of its base class first, using the same arguments.
   */
  template <class Derived>
  void
  process_row (const GimpPaintCoreLoopsParams *params,
               State<Derived>                 *state,
               GeglBufferIterator             *iter,
               const GeglRectangle            *roi,
               const GeglRectangle            *area,
335
               const GeglRectangle            *rect,
336 337 338 339 340 341 342 343 344
               gint                            y) const
  {
  }
};


/* BasicDispatch:
 *
 * A class template implementing a simple dispatch function object, which adds
345
 * an algorithm to the hierarchy unconditionally.  'AlgorithmTemplate' is the
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
 * alogithm class template (usually a helper class, rather than an actual
 * algorithm), and 'Mask' is the dispatch function mask, as described in
 * 'dispatch()'.
 */

template <template <class Base> class AlgorithmTemplate,
          guint                       Mask>
struct BasicDispatch
{
  static constexpr guint mask = Mask;

  template <class Visitor,
            class Algorithm>
  void
  operator () (Visitor                         visitor,
               const GimpPaintCoreLoopsParams *params,
               GimpPaintCoreLoopsAlgorithm     algorithms,
               identity<Algorithm>             algorithm) const
  {
    visitor (identity<AlgorithmTemplate<Algorithm>> ());
  }
};


/* AlgorithmDispatch:
 *
 * A class template implementing a dispatch function suitable for dispatching
 * algorithms.  'AlgorithmTemplate' is the algorithm class template, 'Mask' is
 * the dispatch function mask, as described in 'dispatch()', and 'Dependencies'
 * is a list of (types of) dispatch functions the algorithm depends on, usd as
 * explained below.
 *
378
 * 'AlgorithmDispatch' adds the algorithm to the hierarchy if it's included in
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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 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 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 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 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
 * the set of requested algorithms; specifically, if the bitwise-AND of the
 * requested-algorithms bitset and of 'Mask' is equal to 'Mask'.
 *
 * Before adding the algorithm to the hierarchy, the hierarchy is augmented by
 * dispatching through the list of dependencies, in order.
 */

template <template <class Base> class AlgorithmTemplate,
          guint                       Mask,
          class...                    Dependencies>
struct AlgorithmDispatch
{
  static constexpr guint mask = Mask;

  template <class Visitor,
            class Algorithm>
  void
  operator () (Visitor                         visitor,
               const GimpPaintCoreLoopsParams *params,
               GimpPaintCoreLoopsAlgorithm     algorithms,
               identity<Algorithm>             algorithm) const
  {
    if ((algorithms & mask) == mask)
      {
        dispatch (
          [&] (auto algorithm)
          {
            using NewAlgorithm = typename decltype (algorithm)::type;

            visitor (identity<AlgorithmTemplate<NewAlgorithm>> ());
          },
          params, algorithms, algorithm, Dependencies ()...);
      }
    else
      {
        visitor (algorithm);
      }
  }
};


/* PaintBuf, dispatch_paint_buf():
 *
 * An algorithm helper class, providing access to the paint buffer.  Algorithms
 * that use the paint buffer should specify 'dispatch_paint_buf()' as a
 * dependency, and access 'PaintBuf' members through their base type/subobject.
 */

template <class Base>
struct PaintBuf : Base
{
  /* Component type of the paint buffer. */
  using paint_type = gfloat;

  static constexpr guint filter = Base::filter | ALGORITHM_PAINT_BUF;

  /* Paint buffer stride, in 'paint_type' elements. */
  gint        paint_stride;
  /* Pointer to the start of the paint buffer data. */
  paint_type *paint_data;

  explicit
  PaintBuf (const GimpPaintCoreLoopsParams *params) :
    Base (params)
  {
    paint_stride = gimp_temp_buf_get_width (params->paint_buf) * 4;
    paint_data   = (paint_type *) gimp_temp_buf_get_data (params->paint_buf);
  }
};

static BasicDispatch<PaintBuf, ALGORITHM_PAINT_BUF> dispatch_paint_buf;


/* PaintMask, dispatch_paint_mask():
 *
 * An algorithm helper class, providing access to the paint mask.  Algorithms
 * that use the paint mask should specify 'dispatch_paint_mask()' as a
 * dependency, and access 'PaintMask' members through their base type/
 * subobject.
 */

template <class Base,
          class MaskType>
struct PaintMask : Base
{
  /* Component type of the paint mask. */
  using mask_type = MaskType;

  static constexpr guint filter = Base::filter | ALGORITHM_PAINT_MASK;

  /* Paint mask stride, in 'mask_type' elements. */
  gint             mask_stride;
  /* Pointer to the start of the paint mask data, taking the mask offset into
   * account.
   */
  const mask_type *mask_data;

  explicit
  PaintMask (const GimpPaintCoreLoopsParams *params) :
    Base (params)
  {
    mask_stride = gimp_temp_buf_get_width (params->paint_mask);
    mask_data   =
      (const mask_type *) gimp_temp_buf_get_data (params->paint_mask) +
      params->paint_mask_offset_y * mask_stride                       +
      params->paint_mask_offset_x;
  }
};

struct DispatchPaintMask
{
  static constexpr guint mask = ALGORITHM_PAINT_MASK;

  template <class Visitor,
            class Algorithm>
  void
  operator () (Visitor                         visitor,
               const GimpPaintCoreLoopsParams *params,
               GimpPaintCoreLoopsAlgorithm     algorithms,
               identity<Algorithm>             algorithm) const
  {
    const Babl *mask_format = gimp_temp_buf_get_format (params->paint_mask);

    if (mask_format == babl_format ("Y u8"))
      visitor (identity<PaintMask<Algorithm, guint8>> ());
    else if (mask_format == babl_format ("Y float"))
      visitor (identity<PaintMask<Algorithm, gfloat>> ());
    else
      g_warning ("Mask format not supported: %s", babl_get_name (mask_format));
  }
} static dispatch_paint_mask;


/* Stipple, dispatch_stipple():
 *
 * An algorithm helper class, providing access to the 'stipple' parameter.
 * Algorithms that use the 'stipple' parameter should specify
 * 'dispatch_stipple()' as a dependency, and access 'Stipple' members through
 * their base type/subobject.
 */

template <class    Base,
          gboolean StippleFlag>
struct Stipple : Base
{
  static constexpr guint filter = Base::filter | ALGORITHM_STIPPLE;

  /* The value of the 'stipple' parameter, usable as a constant expression. */
  static constexpr gboolean stipple = StippleFlag;

  using Base::Base;
};

struct DispatchStipple
{
  static constexpr guint mask = ALGORITHM_STIPPLE;

  template <class Visitor,
            class Algorithm>
  void
  operator () (Visitor                         visitor,
               const GimpPaintCoreLoopsParams *params,
               GimpPaintCoreLoopsAlgorithm     algorithms,
               identity<Algorithm>             algorithm) const
  {
    if (params->stipple)
      visitor (identity<Stipple<Algorithm, TRUE>> ());
    else
      visitor (identity<Stipple<Algorithm, FALSE>> ());
  }
} static dispatch_stipple;


/* CanvasBufferIterator:
 *
 * An algorithm helper class, providing iterator-access to the canvas buffer.
 * Algorithms that iterate over the canvas buffer should derive from this
 * class, and access its members through their base type/subobject.
 *
 * 'Base' is the base class to use, which should normally be the base template-
 * parameter class passed to the algorithm.  'Access' specifies the desired
 * iterator access to the canvas buffer.
 */

template <class Base,
          guint Access>
struct CanvasBufferIterator : Base
{
  /* The iterator index of the canvas buffer. */
  static constexpr gint           canvas_buffer_iterator =
    Base::canvas_buffer_iterator < 0 ? Base::n_iterators :
                                       Base::canvas_buffer_iterator;
  /* Used internally. */
  static constexpr GeglAccessMode canvas_buffer_access   =
    (GeglAccessMode) (Base::canvas_buffer_access | Access);
  /* The total number of iterators used by the hierarchy, up to, and including,
   * the current class.
   */
  static constexpr gint           n_iterators            =
    Base::canvas_buffer_iterator < 0 ? Base::n_iterators + 1:
                                       Base::n_iterators;

  using Base::Base;

  template <class Derived>
  using State = typename Base::template State<Derived>;

  template <class Derived>
  void
  init (const GimpPaintCoreLoopsParams *params,
        State<Derived>                 *state,
        GeglBufferIterator             *iter,
        const GeglRectangle            *roi,
        const GeglRectangle            *area) const
  {
    Base::init (params, state, iter, roi, area);

    if (Base::canvas_buffer_iterator < 0)
      {
        gegl_buffer_iterator_add (iter, params->canvas_buffer, area, 0,
                                  babl_format ("Y float"),
                                  Derived::canvas_buffer_access,
                                  GEGL_ABYSS_NONE);
      }
  }
};


/* CombinePaintMaskToCanvasMaskToPaintBufAlpha,
 * dispatch_combine_paint_mask_to_canvas_mask_to_paint_buf_alpha():
 *
 * An algorithm class, providing an optimized version combining both the
 * COMBINE_PAINT_MASK_TO_CANVAS_MASK and the CANVAS_BUFFER_TO_PAINT_BUF_ALPHA
 * algorithms.  Used instead of the individual implementations, when both
 * algorithms are requested.
 */

template <class Base>
struct CombinePaintMaskToCanvasMaskToPaintBufAlpha :
  CanvasBufferIterator<Base, GEGL_BUFFER_READWRITE>
{
  using base_type = CanvasBufferIterator<Base, GEGL_BUFFER_READWRITE>;
  using mask_type = typename base_type::mask_type;

  static constexpr guint filter =
    base_type::filter                                                 |
    GIMP_PAINT_CORE_LOOPS_ALGORITHM_COMBINE_PAINT_MASK_TO_CANVAS_MASK |
    GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_PAINT_BUF_ALPHA  |
    GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_PAINT_BUFFER;

  using base_type::base_type;

  template <class Derived>
  struct State : base_type::template State<Derived>
  {
    gfloat *canvas_pixel;
  };

  template <class Derived>
  void
  init_step (const GimpPaintCoreLoopsParams *params,
             State<Derived>                 *state,
             GeglBufferIterator             *iter,
             const GeglRectangle            *roi,
643 644
             const GeglRectangle            *area,
             const GeglRectangle            *rect) const
645
  {
646
    base_type::init_step (params, state, iter, roi, area, rect);
647 648

    state->canvas_pixel =
649
      (gfloat *) iter->items[base_type::canvas_buffer_iterator].data;
650 651 652 653 654 655 656 657 658
  }

  template <class Derived>
  void
  process_row (const GimpPaintCoreLoopsParams *params,
               State<Derived>                 *state,
               GeglBufferIterator             *iter,
               const GeglRectangle            *roi,
               const GeglRectangle            *area,
659
               const GeglRectangle            *rect,
660 661
               gint                            y) const
  {
662
    base_type::process_row (params, state, iter, roi, area, rect, y);
663

664 665
    gint             mask_offset  = (y       - roi->y) * this->mask_stride +
                                    (rect->x - roi->x);
666
    const mask_type *mask_pixel   = &this->mask_data[mask_offset];
667 668
    gint             paint_offset = (y       - roi->y) * this->paint_stride +
                                    (rect->x - roi->x) * 4;
669 670 671
    gfloat          *paint_pixel  = &this->paint_data[paint_offset];
    gint             x;

672
    for (x = 0; x < rect->width; x++)
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
      {
        if (base_type::stipple)
          {
            state->canvas_pixel[0] += (1.0 - state->canvas_pixel[0])  *
                                      value_to_float (*mask_pixel)    *
                                      params->paint_opacity;
          }
        else
          {
            if (params->paint_opacity > state->canvas_pixel[0])
              {
                state->canvas_pixel[0] += (params->paint_opacity - state->canvas_pixel[0]) *
                                           value_to_float (*mask_pixel)                    *
                                           params->paint_opacity;
              }
          }

        paint_pixel[3] *= state->canvas_pixel[0];

        mask_pixel          += 1;
        state->canvas_pixel += 1;
        paint_pixel         += 4;
      }
  }
};

static AlgorithmDispatch<
  CombinePaintMaskToCanvasMaskToPaintBufAlpha,
  GIMP_PAINT_CORE_LOOPS_ALGORITHM_COMBINE_PAINT_MASK_TO_CANVAS_MASK |
  GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_PAINT_BUF_ALPHA,
  decltype (dispatch_paint_buf),
  decltype (dispatch_paint_mask),
  decltype (dispatch_stipple)
>
dispatch_combine_paint_mask_to_canvas_mask_to_paint_buf_alpha;


/* CombinePaintMaskToCanvasMask, dispatch_combine_paint_mask_to_canvas_mask():
 *
 * An algorithm class, implementing the COMBINE_PAINT_MASK_TO_CANVAS_MASK
 * algorithm.
 */

template <class Base>
struct CombinePaintMaskToCanvasMask :
  CanvasBufferIterator<Base, GEGL_BUFFER_READWRITE>
{
  using base_type = CanvasBufferIterator<Base, GEGL_BUFFER_READWRITE>;
  using mask_type = typename base_type::mask_type;

  static constexpr guint filter =
    base_type::filter                                                 |
    GIMP_PAINT_CORE_LOOPS_ALGORITHM_COMBINE_PAINT_MASK_TO_CANVAS_MASK |
    GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_PAINT_BUFFER;

  using base_type::base_type;

  template <class Derived>
  struct State : base_type::template State<Derived>
  {
    gfloat *canvas_pixel;
  };

  template <class Derived>
  void
  init_step (const GimpPaintCoreLoopsParams *params,
             State<Derived>                 *state,
             GeglBufferIterator             *iter,
             const GeglRectangle            *roi,
742 743
             const GeglRectangle            *area,
             const GeglRectangle            *rect) const
744
  {
745
    base_type::init_step (params, state, iter, roi, area, rect);
746 747

    state->canvas_pixel =
748
      (gfloat *) iter->items[base_type::canvas_buffer_iterator].data;
749 750 751 752 753 754 755 756 757
  }

  template <class Derived>
  void
  process_row (const GimpPaintCoreLoopsParams *params,
               State<Derived>                 *state,
               GeglBufferIterator             *iter,
               const GeglRectangle            *roi,
               const GeglRectangle            *area,
758
               const GeglRectangle            *rect,
759 760
               gint                            y) const
  {
761
    base_type::process_row (params, state, iter, roi, area, rect, y);
762

763 764
    gint             mask_offset = (y       - roi->y) * this->mask_stride +
                                   (rect->x - roi->x);
765 766 767
    const mask_type *mask_pixel  = &this->mask_data[mask_offset];
    gint             x;

768
    for (x = 0; x < rect->width; x++)
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
      {
        if (base_type::stipple)
          {
            state->canvas_pixel[0] += (1.0 - state->canvas_pixel[0]) *
                                      value_to_float (*mask_pixel)   *
                                      params->paint_opacity;
          }
        else
          {
            if (params->paint_opacity > state->canvas_pixel[0])
              {
                state->canvas_pixel[0] += (params->paint_opacity - state->canvas_pixel[0]) *
                                          value_to_float (*mask_pixel)                     *
                                          params->paint_opacity;
              }
          }

        mask_pixel          += 1;
        state->canvas_pixel += 1;
      }
  }
};

static AlgorithmDispatch<
  CombinePaintMaskToCanvasMask,
  GIMP_PAINT_CORE_LOOPS_ALGORITHM_COMBINE_PAINT_MASK_TO_CANVAS_MASK,
  decltype (dispatch_paint_mask),
  decltype (dispatch_stipple)
>
dispatch_combine_paint_mask_to_canvas_mask;


/* CanvasBufferToPaintBufAlpha, dispatch_canvas_buffer_to_paint_buf_alpha():
 *
 * An algorithm class, implementing the CANVAS_BUFFER_TO_PAINT_BUF_ALPHA
 * algorithm.
 */

template <class Base>
struct CanvasBufferToPaintBufAlpha : CanvasBufferIterator<Base,
                                                          GEGL_BUFFER_READ>
{
  using base_type = CanvasBufferIterator<Base, GEGL_BUFFER_READ>;

  static constexpr guint filter =
    base_type::filter                                                |
    GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_PAINT_BUF_ALPHA |
    GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_PAINT_BUFFER;

  using base_type::base_type;

  template <class Derived>
  struct State : base_type::template State<Derived>
  {
    const gfloat *canvas_pixel;
  };

  template <class Derived>
  void
  init_step (const GimpPaintCoreLoopsParams *params,
             State<Derived>                 *state,
             GeglBufferIterator             *iter,
             const GeglRectangle            *roi,
832 833
             const GeglRectangle            *area,
             const GeglRectangle            *rect) const
834
  {
835
    base_type::init_step (params, state, iter, roi, area, rect);
836 837

    state->canvas_pixel =
838
      (const gfloat *) iter->items[base_type::canvas_buffer_iterator].data;
839 840 841 842 843 844 845 846 847
  }

  template <class Derived>
  void
  process_row (const GimpPaintCoreLoopsParams *params,
               State<Derived>                 *state,
               GeglBufferIterator             *iter,
               const GeglRectangle            *roi,
               const GeglRectangle            *area,
848
               const GeglRectangle            *rect,
849 850
               gint                            y) const
  {
851
    base_type::process_row (params, state, iter, roi, area, rect, y);
852 853 854

    /* Copy the canvas buffer in rect to the paint buffer's alpha channel */

855 856
    gint    paint_offset = (y       - roi->y) * this->paint_stride +
                           (rect->x - roi->x) * 4;
857 858 859
    gfloat *paint_pixel  = &this->paint_data[paint_offset];
    gint    x;

860
    for (x = 0; x < rect->width; x++)
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
      {
        paint_pixel[3] *= *state->canvas_pixel;

        state->canvas_pixel += 1;
        paint_pixel         += 4;
      }
  }
};

static AlgorithmDispatch<
  CanvasBufferToPaintBufAlpha,
  GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_PAINT_BUF_ALPHA,
  decltype (dispatch_paint_buf)
>
dispatch_canvas_buffer_to_paint_buf_alpha;


/* PaintMaskToPaintBuffer, dispatch_paint_mask_to_paint_buffer():
 *
 * An algorithm class, implementing the PAINT_MASK_TO_PAINT_BUFFER algorithm.
 */

template <class Base>
struct PaintMaskToPaintBuffer : Base
{
  using mask_type = typename Base::mask_type;

  static constexpr guint filter =
    Base::filter |
    GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_PAINT_BUFFER;

  explicit
  PaintMaskToPaintBuffer (const GimpPaintCoreLoopsParams *params) :
    Base (params)
  {
896
    /* Validate that the paint buffer is within the bounds of the paint mask */
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
    g_return_if_fail (gimp_temp_buf_get_width (params->paint_buf) <=
                      gimp_temp_buf_get_width (params->paint_mask) -
                      params->paint_mask_offset_x);
    g_return_if_fail (gimp_temp_buf_get_height (params->paint_buf) <=
                      gimp_temp_buf_get_height (params->paint_mask) -
                      params->paint_mask_offset_y);
  }

  template <class Derived>
  using State = typename Base::template State<Derived>;

  template <class Derived>
  void
  process_row (const GimpPaintCoreLoopsParams *params,
               State<Derived>                 *state,
               GeglBufferIterator             *iter,
               const GeglRectangle            *roi,
               const GeglRectangle            *area,
915
               const GeglRectangle            *rect,
916 917
               gint                            y) const
  {
918
    Base::process_row (params, state, iter, roi, area, rect, y);
919

920 921
    gint             paint_offset = (y       - roi->y) * this->paint_stride +
                                    (rect->x - roi->x) * 4;
922
    gfloat          *paint_pixel  = &this->paint_data[paint_offset];
923 924
    gint             mask_offset  = (y       - roi->y) * this->mask_stride +
                                    (rect->x - roi->x);
925 926 927
    const mask_type *mask_pixel   = &this->mask_data[mask_offset];
    gint             x;

928
    for (x = 0; x < rect->width; x++)
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
      {
        paint_pixel[3] *= value_to_float (*mask_pixel) * params->paint_opacity;

        mask_pixel  += 1;
        paint_pixel += 4;
      }
  }
};

static AlgorithmDispatch<
  PaintMaskToPaintBuffer,
  GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_PAINT_BUFFER,
  decltype (dispatch_paint_buf),
  decltype (dispatch_paint_mask)
>
dispatch_paint_mask_to_paint_buffer;


/* DoLayerBlend, dispatch_do_layer_blend():
 *
 * An algorithm class, implementing the DO_LAYER_BLEND algorithm.
 */

template <class Base>
struct DoLayerBlend : Base
{
  static constexpr guint filter =
    Base::filter |
    GIMP_PAINT_CORE_LOOPS_ALGORITHM_DO_LAYER_BLEND;

  static constexpr gint iterator_base = Base::n_iterators;
  static constexpr gint n_iterators   = Base::n_iterators + 3;

  const Babl             *iterator_format;
  GimpOperationLayerMode  layer_mode;

  explicit
  DoLayerBlend (const GimpPaintCoreLoopsParams *params) :
    Base (params)
  {
    layer_mode.layer_mode          = params->paint_mode;
    layer_mode.opacity             = params->image_opacity;
    layer_mode.function            = gimp_layer_mode_get_function (params->paint_mode);
    layer_mode.blend_function      = gimp_layer_mode_get_blend_function (params->paint_mode);
    layer_mode.blend_space         = gimp_layer_mode_get_blend_space (params->paint_mode);
    layer_mode.composite_space     = gimp_layer_mode_get_composite_space (params->paint_mode);
    layer_mode.composite_mode      = gimp_layer_mode_get_paint_composite_mode (params->paint_mode);
    layer_mode.real_composite_mode = layer_mode.composite_mode;

    iterator_format = gimp_layer_mode_get_format (params->paint_mode,
                                                  layer_mode.composite_space,
                                                  layer_mode.blend_space,
                                                  gimp_temp_buf_get_format (params->paint_buf));

    g_return_if_fail (gimp_temp_buf_get_format (params->paint_buf) == iterator_format);
  }

  template <class Derived>
  struct State : Base::template State<Derived>
  {
    GeglRectangle  process_roi;

    gfloat        *out_pixel;
    gfloat        *in_pixel;
    gfloat        *mask_pixel;
    gfloat        *paint_pixel;
  };

  template <class Derived>
  void
  init (const GimpPaintCoreLoopsParams *params,
        State<Derived>                 *state,
        GeglBufferIterator             *iter,
        const GeglRectangle            *roi,
        const GeglRectangle            *area) const
  {
    Base::init (params, state, iter, roi, area);

    GeglRectangle mask_area = *area;

    mask_area.x -= params->mask_offset_x;
    mask_area.y -= params->mask_offset_y;

    gegl_buffer_iterator_add (iter, params->dest_buffer, area, 0,
                              iterator_format,
                              GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE);

    gegl_buffer_iterator_add (iter, params->src_buffer, area, 0,
                              iterator_format,
                              GEGL_ACCESS_READ, GEGL_ABYSS_NONE);

    if (params->mask_buffer)
      {
        gegl_buffer_iterator_add (iter, params->mask_buffer, &mask_area, 0,
                                  babl_format ("Y float"),
                                  GEGL_ACCESS_READ, GEGL_ABYSS_NONE);
      }
  }

  template <class Derived>
  void
  init_step (const GimpPaintCoreLoopsParams *params,
             State<Derived>                 *state,
             GeglBufferIterator             *iter,
             const GeglRectangle            *roi,
1034 1035
             const GeglRectangle            *area,
             const GeglRectangle            *rect) const
1036
  {
1037
    Base::init_step (params, state, iter, roi, area, rect);
1038

1039 1040
    state->out_pixel  = (gfloat *) iter->items[iterator_base + 0].data;
    state->in_pixel   = (gfloat *) iter->items[iterator_base + 1].data;
1041 1042
    state->mask_pixel = NULL;

1043 1044 1045
    state->paint_pixel = this->paint_data                        +
                         (rect->y - roi->y) * this->paint_stride +
                         (rect->x - roi->x) * 4;
1046 1047

    if (params->mask_buffer)
1048
      state->mask_pixel = (gfloat *) iter->items[iterator_base + 2].data;
1049

1050 1051
    state->process_roi.x      = rect->x;
    state->process_roi.width  = rect->width;
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
    state->process_roi.height = 1;
  }

  template <class Derived>
  void
  process_row (const GimpPaintCoreLoopsParams *params,
               State<Derived>                 *state,
               GeglBufferIterator             *iter,
               const GeglRectangle            *roi,
               const GeglRectangle            *area,
1062
               const GeglRectangle            *rect,
1063 1064
               gint                            y) const
  {
1065
    Base::process_row (params, state, iter, roi, area, rect, y);
1066 1067 1068 1069 1070 1071 1072 1073

    state->process_roi.y = y;

    layer_mode.function ((GeglOperation*) &layer_mode,
                         state->in_pixel,
                         state->paint_pixel,
                         state->mask_pixel,
                         state->out_pixel,
1074
                         rect->width,
1075 1076 1077
                         &state->process_roi,
                         0);

1078 1079
    state->in_pixel     += rect->width * 4;
    state->out_pixel    += rect->width * 4;
1080
    if (params->mask_buffer)
1081
      state->mask_pixel += rect->width;
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
    state->paint_pixel  += this->paint_stride;
  }
};

static AlgorithmDispatch<
  DoLayerBlend,
  GIMP_PAINT_CORE_LOOPS_ALGORITHM_DO_LAYER_BLEND,
  decltype (dispatch_paint_buf)
>
dispatch_do_layer_blend;


/* gimp_paint_core_loops_process():
 *
 * Performs the set of algorithms requested in 'algorithms', specified as a
 * bitwise-OR of 'GimpPaintCoreLoopsAlgorithm' values, given the set of
 * parameters 'params'.
 *
 * Note that the order in which the algorithms are performed is currently
 * fixed, and follows their order of appearance in the
 * 'GimpPaintCoreLoopsAlgorithm' enum.
 */

void
gimp_paint_core_loops_process (const GimpPaintCoreLoopsParams *params,
                               GimpPaintCoreLoopsAlgorithm     algorithms)
{
  GeglRectangle roi;

  if (params->paint_buf)
Daniel Sabo's avatar
Daniel Sabo committed
1112
    {
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
      roi.x      = params->paint_buf_offset_x;
      roi.y      = params->paint_buf_offset_y;
      roi.width  = gimp_temp_buf_get_width  (params->paint_buf);
      roi.height = gimp_temp_buf_get_height (params->paint_buf);
    }
  else
    {
      roi.x      = params->paint_buf_offset_x;
      roi.y      = params->paint_buf_offset_y;
      roi.width  = gimp_temp_buf_get_width (params->paint_mask) -
                   params->paint_mask_offset_x;
      roi.height = gimp_temp_buf_get_height (params->paint_mask) -
                   params->paint_mask_offset_y;
    }

  dispatch (
    [&] (auto algorithm_type)
    {
      using Algorithm = typename decltype (algorithm_type)::type;
      using State     = typename Algorithm::template State<Algorithm>;
1133

1134
      Algorithm algorithm (params);
Daniel Sabo's avatar
Daniel Sabo committed
1135

1136 1137
      gimp_parallel_distribute_area (&roi, MIN_PARALLEL_SUB_AREA,
                                     [=] (const GeglRectangle *area)
1138
        {
1139 1140 1141 1142
          State state;
          gint  y;

          if (Algorithm::n_iterators > 0)
Daniel Sabo's avatar
Daniel Sabo committed
1143
            {
1144 1145
              GeglBufferIterator *iter;

1146
              iter = gegl_buffer_iterator_empty_new (Algorithm::n_iterators);
1147 1148

              algorithm.init (params, &state, iter, &roi, area);
Daniel Sabo's avatar
Daniel Sabo committed
1149

1150
              while (gegl_buffer_iterator_next (iter))
Daniel Sabo's avatar
Daniel Sabo committed
1151
                {
1152
                  const GeglRectangle *rect = &iter->items[0].roi;
Daniel Sabo's avatar
Daniel Sabo committed
1153

1154 1155 1156
                  algorithm.init_step (params, &state, iter, &roi, area, rect);

                  for (y = 0; y < rect->height; y++)
Daniel Sabo's avatar
Daniel Sabo committed
1157
                    {
1158
                      algorithm.process_row (params, &state,
1159 1160
                                             iter, &roi, area, rect,
                                             rect->y + y);
Daniel Sabo's avatar
Daniel Sabo committed
1161 1162 1163
                    }
                }
            }
1164
          else
Daniel Sabo's avatar
Daniel Sabo committed
1165
            {
1166 1167
              algorithm.init      (params, &state, NULL, &roi, area);
              algorithm.init_step (params, &state, NULL, &roi, area, area);
1168

1169
              for (y = 0; y < area->height; y++)
1170 1171
                {
                  algorithm.process_row (params, &state,
1172 1173
                                         NULL, &roi, area, area,
                                         area->y + y);
Daniel Sabo's avatar
Daniel Sabo committed
1174 1175
                }
            }
1176 1177 1178 1179 1180 1181 1182 1183 1184
        });
    },
    params, algorithms, identity<AlgorithmBase> (),
    dispatch_combine_paint_mask_to_canvas_mask_to_paint_buf_alpha,
    dispatch_combine_paint_mask_to_canvas_mask,
    dispatch_canvas_buffer_to_paint_buf_alpha,
    dispatch_paint_mask_to_paint_buffer,
    dispatch_do_layer_blend);
}
Daniel Sabo's avatar
Daniel Sabo committed
1185 1186


1187 1188 1189 1190 1191
/* combine_paint_mask_to_canvas_mask():
 *
 * A convenience wrapper around 'gimp_paint_core_loops_process()', performing
 * just the COMBINE_PAINT_MASK_TO_CANVAS_MASK algorithm.
 */
Daniel Sabo's avatar
Daniel Sabo committed
1192

1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
void
combine_paint_mask_to_canvas_mask (const GimpTempBuf *paint_mask,
                                   gint               mask_x_offset,
                                   gint               mask_y_offset,
                                   GeglBuffer        *canvas_buffer,
                                   gint               x_offset,
                                   gint               y_offset,
                                   gfloat             opacity,
                                   gboolean           stipple)
{
  GimpPaintCoreLoopsParams params = {};
1204

1205
  params.canvas_buffer       = canvas_buffer;
Daniel Sabo's avatar
Daniel Sabo committed
1206

1207 1208
  params.paint_buf_offset_x  = x_offset;
  params.paint_buf_offset_y  = y_offset;
Daniel Sabo's avatar
Daniel Sabo committed
1209

1210 1211 1212
  params.paint_mask          = paint_mask;
  params.paint_mask_offset_x = mask_x_offset;
  params.paint_mask_offset_y = mask_y_offset;
1213

1214
  params.stipple             = stipple;
Daniel Sabo's avatar
Daniel Sabo committed
1215

1216 1217 1218 1219 1220
  params.paint_opacity       = opacity;

  gimp_paint_core_loops_process (
    &params,
    GIMP_PAINT_CORE_LOOPS_ALGORITHM_COMBINE_PAINT_MASK_TO_CANVAS_MASK);
Daniel Sabo's avatar
Daniel Sabo committed
1221 1222
}

1223 1224 1225 1226 1227 1228 1229

/* canvas_buffer_to_paint_buf_alpha():
 *
 * A convenience wrapper around 'gimp_paint_core_loops_process()', performing
 * just the CANVAS_BUFFER_TO_PAINT_BUF_ALPHA algorithm.
 */

Daniel Sabo's avatar
Daniel Sabo committed
1230 1231 1232 1233 1234 1235
void
canvas_buffer_to_paint_buf_alpha (GimpTempBuf  *paint_buf,
                                  GeglBuffer   *canvas_buffer,
                                  gint          x_offset,
                                  gint          y_offset)
{
1236
  GimpPaintCoreLoopsParams params = {};
Daniel Sabo's avatar
Daniel Sabo committed
1237

1238
  params.canvas_buffer      = canvas_buffer;
Daniel Sabo's avatar
Daniel Sabo committed
1239

1240 1241 1242
  params.paint_buf          = paint_buf;
  params.paint_buf_offset_x = x_offset;
  params.paint_buf_offset_y = y_offset;
Daniel Sabo's avatar
Daniel Sabo committed
1243

1244 1245 1246 1247
  gimp_paint_core_loops_process (
    &params,
    GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_PAINT_BUF_ALPHA);
}
Daniel Sabo's avatar
Daniel Sabo committed
1248

1249

1250 1251 1252 1253 1254
/* paint_mask_to_paint_buffer():
 *
 * A convenience wrapper around 'gimp_paint_core_loops_process()', performing
 * just the PAINT_MASK_TO_PAINT_BUFFER algorithm.
 */
Daniel Sabo's avatar
Daniel Sabo committed
1255 1256 1257 1258 1259 1260 1261 1262

void
paint_mask_to_paint_buffer (const GimpTempBuf  *paint_mask,
                            gint                mask_x_offset,
                            gint                mask_y_offset,
                            GimpTempBuf        *paint_buf,
                            gfloat              paint_opacity)
{
1263
  GimpPaintCoreLoopsParams params = {};
Daniel Sabo's avatar
Daniel Sabo committed
1264

1265
  params.paint_buf           = paint_buf;
Daniel Sabo's avatar
Daniel Sabo committed
1266

1267 1268 1269
  params.paint_mask          = paint_mask;
  params.paint_mask_offset_x = mask_x_offset;
  params.paint_mask_offset_y = mask_y_offset;
1270

1271
  params.paint_opacity       = paint_opacity;
Daniel Sabo's avatar
Daniel Sabo committed
1272

1273 1274 1275 1276
  gimp_paint_core_loops_process (
    &params,
    GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_PAINT_BUFFER);
}
1277

Daniel Sabo's avatar
Daniel Sabo committed
1278

1279 1280 1281 1282 1283
/* do_layer_blend():
 *
 * A convenience wrapper around 'gimp_paint_core_loops_process()', performing
 * just the DO_LAYER_BLEND algorithm.
 */
Daniel Sabo's avatar
Daniel Sabo committed
1284 1285

void
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
do_layer_blend (GeglBuffer    *src_buffer,
                GeglBuffer    *dst_buffer,
                GimpTempBuf   *paint_buf,
                GeglBuffer    *mask_buffer,
                gfloat         opacity,
                gint           x_offset,
                gint           y_offset,
                gint           mask_x_offset,
                gint           mask_y_offset,
                GimpLayerMode  paint_mode)
Daniel Sabo's avatar
Daniel Sabo committed
1296
{
1297
  GimpPaintCoreLoopsParams params = {};
Daniel Sabo's avatar
Daniel Sabo committed
1298

1299 1300 1301
  params.paint_buf          = paint_buf;
  params.paint_buf_offset_x = x_offset;
  params.paint_buf_offset_y = y_offset;
Daniel Sabo's avatar
Daniel Sabo committed
1302

1303 1304
  params.src_buffer         = src_buffer;
  params.dest_buffer        = dst_buffer;
1305

1306 1307 1308
  params.mask_buffer        = mask_buffer;
  params.mask_offset_x      = mask_x_offset;
  params.mask_offset_y      = mask_y_offset;
1309

1310
  params.image_opacity      = opacity;
Daniel Sabo's avatar
Daniel Sabo committed
1311

1312
  params.paint_mode         = paint_mode;
Daniel Sabo's avatar
Daniel Sabo committed
1313

1314 1315 1316 1317
  gimp_paint_core_loops_process (
    &params,
    GIMP_PAINT_CORE_LOOPS_ALGORITHM_DO_LAYER_BLEND);
}
1318 1319


1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
/* mask_components_onto():
 *
 * Copies the contents of 'src_buffer' and 'aux_buffer' into 'dst_buffer', over
 * 'roi'.  Components set in 'mask' are copied from 'aux_buffer', while those
 * not set in 'mask' are copied from 'src_buffer'.  'linear_mode' specifies
 * whether to iterate over the buffers use a linear format.  It should match
 * the linear mode of the painted-to drawable, to avoid modifying masked-out
 * components.
 *
 * Note that we don't integrate this function into the rest of the algorithm
 * framework, since it uses a (potentially) different format when iterating
 * over the buffers than the rest of the algorithms.
 */
Daniel Sabo's avatar
Daniel Sabo committed
1333 1334

void
1335 1336 1337 1338 1339
mask_components_onto (GeglBuffer          *src_buffer,
                      GeglBuffer          *aux_buffer,
                      GeglBuffer          *dst_buffer,
                      const GeglRectangle *roi,
                      GimpComponentMask    mask,
1340 1341
                      GimpTRCType          trc,
                      const Babl          *space)
Daniel Sabo's avatar
Daniel Sabo committed
1342
{
1343
  const Babl *iterator_format;
Daniel Sabo's avatar
Daniel Sabo committed
1344

1345 1346 1347
  if (! roi)
    roi = gegl_buffer_get_extent (dst_buffer);

1348 1349 1350 1351
  iterator_format =
    gimp_babl_format (GIMP_RGB,
                      gimp_babl_precision (GIMP_COMPONENT_TYPE_FLOAT, trc),
                      TRUE, space);
Daniel Sabo's avatar
Daniel Sabo committed
1352

1353 1354 1355 1356
  gimp_parallel_distribute_area (roi, MIN_PARALLEL_SUB_AREA,
                                 [=] (const GeglRectangle *area)
    {
      GeglBufferIterator *iter;
Daniel Sabo's avatar
Daniel Sabo committed
1357

1358 1359
      iter = gegl_buffer_iterator_new (dst_buffer, area, 0,
                                       iterator_format,
1360
                                       GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE, 3);
Daniel Sabo's avatar
Daniel Sabo committed
1361

1362 1363 1364
      gegl_buffer_iterator_add (iter, src_buffer, area, 0,
                                iterator_format,
                                GEGL_ACCESS_READ, GEGL_ABYSS_NONE);
Daniel Sabo's avatar
Daniel Sabo committed
1365

1366 1367 1368
      gegl_buffer_iterator_add (iter, aux_buffer, area, 0,
                                iterator_format,
                                GEGL_ACCESS_READ, GEGL_ABYSS_NONE);
Daniel Sabo's avatar
Daniel Sabo committed
1369

1370
      while (gegl_buffer_iterator_next (iter))
Daniel Sabo's avatar
Daniel Sabo committed
1371
        {
1372 1373 1374
          gfloat *dest    = (gfloat *)iter->items[0].data;
          gfloat *src     = (gfloat *)iter->items[1].data;
          gfloat *aux     = (gfloat *)iter->items[2].data;
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
          glong   samples = iter->length;

          while (samples--)
            {
              dest[RED]   = (mask & GIMP_COMPONENT_MASK_RED)   ? aux[RED]   : src[RED];
              dest[GREEN] = (mask & GIMP_COMPONENT_MASK_GREEN) ? aux[GREEN] : src[GREEN];
              dest[BLUE]  = (mask & GIMP_COMPONENT_MASK_BLUE)  ? aux[BLUE]  : src[BLUE];
              dest[ALPHA] = (mask & GIMP_COMPONENT_MASK_ALPHA) ? aux[ALPHA] : src[ALPHA];

              src  += 4;
              aux  += 4;
              dest += 4;
            }
Daniel Sabo's avatar
Daniel Sabo committed
1388
        }
1389
    });
Daniel Sabo's avatar
Daniel Sabo committed
1390
}