gimp-gegl-loops.cc 36.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimp-gegl-loops.c
 * Copyright (C) 2012 Michael Natterer <mitch@gimp.org>
 *
 * 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
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 20 21 22
 */

#include "config.h"

23 24
#include <string.h>

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

30 31 32
extern "C"
{

33
#include "libgimpbase/gimpbase.h"
34
#include "libgimpcolor/gimpcolor.h"
35
#include "libgimpmath/gimpmath.h"
36 37 38

#include "gimp-gegl-types.h"

39
#include "gimp-babl.h"
40
#include "gimp-gegl-loops.h"
41
#include "gimp-gegl-loops-sse2.h"
42

Ell's avatar
Ell committed
43
#include "core/gimp-atomic.h"
44
#include "core/gimp-utils.h"
45 46
#include "core/gimpprogress.h"

47

48 49
#define PIXELS_PER_THREAD \
  (/* each thread costs as much as */ 64.0 * 64.0 /* pixels */)
50 51 52 53 54 55 56 57 58 59

#define SHIFTED_AREA(dest, src)                                                \
  const GeglRectangle dest##_area_ = {                                         \
    src##_area->x + (dest##_rect->x - src##_rect->x),                          \
    src##_area->y + (dest##_rect->y - src##_rect->y),                          \
    src##_area->width, src##_area->height                                      \
  };                                                                           \
  const GeglRectangle * const dest##_area = &dest##_area_


Ell's avatar
Ell committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
void
gimp_gegl_buffer_copy (GeglBuffer          *src_buffer,
                       const GeglRectangle *src_rect,
                       GeglAbyssPolicy      abyss_policy,
                       GeglBuffer          *dest_buffer,
                       const GeglRectangle *dest_rect)
{
  g_return_if_fail (GEGL_IS_BUFFER (src_buffer));
  g_return_if_fail (GEGL_IS_BUFFER (dest_buffer));

  if (gegl_buffer_get_format (src_buffer) ==
      gegl_buffer_get_format (dest_buffer))
    {
      gegl_buffer_copy (src_buffer, src_rect, abyss_policy,
                        dest_buffer, dest_rect);
    }
  else
    {
      if (! src_rect)
        src_rect = gegl_buffer_get_extent (src_buffer);

      if (! dest_rect)
        dest_rect = src_rect;

84 85 86
      gegl_parallel_distribute_area (
        src_rect, PIXELS_PER_THREAD,
        [=] (const GeglRectangle *src_area)
Ell's avatar
Ell committed
87 88 89 90 91 92 93 94 95
        {
          SHIFTED_AREA (dest, src);

          gegl_buffer_copy (src_buffer, src_area, abyss_policy,
                            dest_buffer, dest_area);
        });
    }
}

96 97 98 99 100
void
gimp_gegl_convolve (GeglBuffer          *src_buffer,
                    const GeglRectangle *src_rect,
                    GeglBuffer          *dest_buffer,
                    const GeglRectangle *dest_rect,
101 102
                    const gfloat        *kernel,
                    gint                 kernel_size,
103 104 105 106
                    gdouble              divisor,
                    GimpConvolutionType  mode,
                    gboolean             alpha_weighting)
{
107 108
  gfloat     *src;
  gint        src_rowstride;
109

110 111 112 113 114
  const Babl *src_format;
  const Babl *dest_format;
  gint        src_components;
  gint        dest_components;
  gfloat      offset;
115

116 117 118 119 120 121
  if (! src_rect)
    src_rect = gegl_buffer_get_extent (src_buffer);

  if (! dest_rect)
    dest_rect = gegl_buffer_get_extent (dest_buffer);

122 123 124
  src_format = gegl_buffer_get_format (src_buffer);

  if (babl_format_is_palette (src_format))
125 126
    src_format = gimp_babl_format (GIMP_RGB,
                                   GIMP_PRECISION_FLOAT_LINEAR,
127 128
                                   babl_format_has_alpha (src_format),
                                   babl_format_get_space (src_format));
129 130
  else
    src_format = gimp_babl_format (gimp_babl_format_get_base_type (src_format),
131
                                   GIMP_PRECISION_FLOAT_LINEAR,
132 133
                                   babl_format_has_alpha (src_format),
                                   babl_format_get_space (src_format));
134

135 136
  dest_format = gegl_buffer_get_format (dest_buffer);

137
  if (babl_format_is_palette (dest_format))
138 139
    dest_format = gimp_babl_format (GIMP_RGB,
                                    GIMP_PRECISION_FLOAT_LINEAR,
140 141
                                    babl_format_has_alpha (dest_format),
                                    babl_format_get_space (dest_format));
142 143
  else
    dest_format = gimp_babl_format (gimp_babl_format_get_base_type (dest_format),
144
                                    GIMP_PRECISION_FLOAT_LINEAR,
145 146
                                    babl_format_has_alpha (dest_format),
                                    babl_format_get_space (dest_format));
147

148 149
  src_components  = babl_format_get_n_components (src_format);
  dest_components = babl_format_get_n_components (dest_format);
150

151 152
  /* Get source pixel data */
  src_rowstride = src_components * src_rect->width;
153
  src = g_new (gfloat, src_rowstride * src_rect->height);
154 155
  gegl_buffer_get (src_buffer, src_rect, 1.0, src_format, src,
                   GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
156

157 158
  /*  If the mode is NEGATIVE_CONVOL, the offset should be 0.5  */
  if (mode == GIMP_NEGATIVE_CONVOL)
159
    {
160 161 162 163 164 165 166
      offset = 0.5;
      mode = GIMP_NORMAL_CONVOL;
    }
  else
    {
      offset = 0.0;
    }
167

168 169 170
  gegl_parallel_distribute_area (
    dest_rect, PIXELS_PER_THREAD,
    [=] (const GeglRectangle *dest_area)
171 172 173 174 175 176 177 178
    {
      const gint          components  = src_components;
      const gint          a_component = components - 1;
      const gint          margin      = kernel_size / 2;
      GeglBufferIterator *dest_iter;

      /* Set up dest iterator */
      dest_iter = gegl_buffer_iterator_new (dest_buffer, dest_area, 0, dest_format,
179
                                            GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE, 1);
180

181 182 183 184 185
      while (gegl_buffer_iterator_next (dest_iter))
        {
          /*  Convolve the src image using the convolution kernel, writing
           *  to dest Convolve is not tile-enabled--use accordingly
           */
186
          gfloat     *dest    = (gfloat *) dest_iter->items[0].data;
187 188 189 190
          const gint  x1      = 0;
          const gint  y1      = 0;
          const gint  x2      = src_rect->width  - 1;
          const gint  y2      = src_rect->height - 1;
191 192 193 194
          const gint  dest_x1 = dest_iter->items[0].roi.x;
          const gint  dest_y1 = dest_iter->items[0].roi.y;
          const gint  dest_x2 = dest_iter->items[0].roi.x + dest_iter->items[0].roi.width;
          const gint  dest_y2 = dest_iter->items[0].roi.y + dest_iter->items[0].roi.height;
195 196 197
          gint        x, y;

          for (y = dest_y1; y < dest_y2; y++)
198
            {
199
              gfloat *d = dest;
200

201 202 203
              if (alpha_weighting)
                {
                  for (x = dest_x1; x < dest_x2; x++)
204
                    {
205 206 207 208
                      const gfloat *m                = kernel;
                      gdouble       total[4]         = { 0.0, 0.0, 0.0, 0.0 };
                      gdouble       weighted_divisor = 0.0;
                      gint          i, j, b;
209

210 211 212
                      for (j = y - margin; j <= y + margin; j++)
                        {
                          for (i = x - margin; i <= x + margin; i++, m++)
213
                            {
214 215 216 217 218 219 220 221
                              gint          xx = CLAMP (i, x1, x2);
                              gint          yy = CLAMP (j, y1, y2);
                              const gfloat *s  = src + yy * src_rowstride + xx * components;
                              const gfloat  a  = s[a_component];

                              if (a)
                                {
                                  gdouble mult_alpha = *m * a;
222

223
                                  weighted_divisor += mult_alpha;
224

225 226
                                  for (b = 0; b < a_component; b++)
                                    total[b] += mult_alpha * s[b];
227

228 229
                                  total[a_component] += mult_alpha;
                                }
230 231 232
                            }
                        }

233 234
                      if (weighted_divisor == 0.0)
                        weighted_divisor = divisor;
235

236 237
                      for (b = 0; b < a_component; b++)
                        total[b] /= weighted_divisor;
238

239
                      total[a_component] /= divisor;
240

241 242 243
                      for (b = 0; b < components; b++)
                        {
                          total[b] += offset;
244

245 246
                          if (mode != GIMP_NORMAL_CONVOL && total[b] < 0.0)
                            total[b] = - total[b];
247

248 249
                          *d++ = CLAMP (total[b], 0.0, 1.0);
                        }
250 251
                    }
                }
252
              else
253
                {
254
                  for (x = dest_x1; x < dest_x2; x++)
255
                    {
256 257 258 259 260
                      const gfloat *m        = kernel;
                      gdouble       total[4] = { 0.0, 0.0, 0.0, 0.0 };
                      gint          i, j, b;

                      for (j = y - margin; j <= y + margin; j++)
261
                        {
262 263 264 265 266
                          for (i = x - margin; i <= x + margin; i++, m++)
                            {
                              gint          xx = CLAMP (i, x1, x2);
                              gint          yy = CLAMP (j, y1, y2);
                              const gfloat *s  = src + yy * src_rowstride + xx * components;
267

268 269 270
                              for (b = 0; b < components; b++)
                                total[b] += *m * s[b];
                            }
271 272
                        }

273 274 275
                      for (b = 0; b < components; b++)
                        {
                          total[b] = total[b] / divisor + offset;
276

277 278
                          if (mode != GIMP_NORMAL_CONVOL && total[b] < 0.0)
                            total[b] = - total[b];
279

280 281
                          *d++ = CLAMP (total[b], 0.0, 1.0);
                        }
282 283 284
                    }
                }

285
              dest += dest_iter->items[0].roi.width * dest_components;
286
            }
287
        }
288
    });
289 290

  g_free (src);
291
}
292

293 294 295 296 297 298 299 300 301 302
static inline gfloat
odd_powf (gfloat x,
          gfloat y)
{
  if (x >= 0.0f)
    return  powf ( x, y);
  else
    return -powf (-x, y);
}

303 304 305 306 307 308 309 310 311
void
gimp_gegl_dodgeburn (GeglBuffer          *src_buffer,
                     const GeglRectangle *src_rect,
                     GeglBuffer          *dest_buffer,
                     const GeglRectangle *dest_rect,
                     gdouble              exposure,
                     GimpDodgeBurnType    type,
                     GimpTransferMode     mode)
{
312
  if (type == GIMP_DODGE_BURN_TYPE_BURN)
313 314
    exposure = -exposure;

315 316 317 318 319 320
  if (! src_rect)
    src_rect = gegl_buffer_get_extent (src_buffer);

  if (! dest_rect)
    dest_rect = gegl_buffer_get_extent (dest_buffer);

321 322 323
  gegl_parallel_distribute_area (
    src_rect, PIXELS_PER_THREAD,
    [=] (const GeglRectangle *src_area)
324 325
    {
      GeglBufferIterator *iter;
326

327
      SHIFTED_AREA (dest, src);
328

329 330
      iter = gegl_buffer_iterator_new (src_buffer, src_area, 0,
                                       babl_format ("R'G'B'A float"),
331
                                       GEGL_ACCESS_READ, GEGL_ABYSS_NONE, 2);
332

333 334 335
      gegl_buffer_iterator_add (iter, dest_buffer, dest_area, 0,
                                babl_format ("R'G'B'A float"),
                                GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE);
336

337
      switch (mode)
338
        {
339
          gfloat factor;
340

341 342 343 344
        case GIMP_TRANSFER_HIGHLIGHTS:
          factor = 1.0 + exposure * (0.333333);

          while (gegl_buffer_iterator_next (iter))
345
            {
346 347
              gfloat *src   = (gfloat *) iter->items[0].data;
              gfloat *dest  = (gfloat *) iter->items[1].data;
348
              gint    count = iter->length;
349

350 351 352 353 354
              while (count--)
                {
                  *dest++ = *src++ * factor;
                  *dest++ = *src++ * factor;
                  *dest++ = *src++ * factor;
355

356 357 358 359
                  *dest++ = *src++;
                }
            }
          break;
360

361 362 363 364 365
        case GIMP_TRANSFER_MIDTONES:
          if (exposure < 0)
            factor = 1.0 - exposure * (0.333333);
          else
            factor = 1.0 / (1.0 + exposure);
366

367
          while (gegl_buffer_iterator_next (iter))
368
            {
369 370
              gfloat *src   = (gfloat *) iter->items[0].data;
              gfloat *dest  = (gfloat *) iter->items[1].data;
371
              gint    count = iter->length;
372

373 374 375 376 377
              while (count--)
                {
                  *dest++ = odd_powf (*src++, factor);
                  *dest++ = odd_powf (*src++, factor);
                  *dest++ = odd_powf (*src++, factor);
378

379 380 381 382
                  *dest++ = *src++;
                }
            }
          break;
383

384 385 386 387 388
        case GIMP_TRANSFER_SHADOWS:
          if (exposure >= 0)
            factor = 0.333333 * exposure;
          else
            factor = -0.333333 * exposure;
389

390
          while (gegl_buffer_iterator_next (iter))
391
            {
392 393
              gfloat *src   = (gfloat *) iter->items[0].data;
              gfloat *dest  = (gfloat *) iter->items[1].data;
394
              gint    count = iter->length;
395

396
              while (count--)
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
                  if (exposure >= 0)
                    {
                      gfloat s;

                      s = *src++; *dest++ = factor + s - factor * s;
                      s = *src++; *dest++ = factor + s - factor * s;
                      s = *src++; *dest++ = factor + s - factor * s;
                    }
                  else
                    {
                      gfloat s;

                      s = *src++;
                      if (s < factor)
                        *dest++ = 0;
                      else /* factor <= value <=1 */
                        *dest++ = (s - factor) / (1.0 - factor);

                      s = *src++;
                      if (s < factor)
                        *dest++ = 0;
                      else /* factor <= value <=1 */
                        *dest++ = (s - factor) / (1.0 - factor);

                      s = *src++;
                      if (s < factor)
                        *dest++ = 0;
                      else /* factor <= value <=1 */
                        *dest++ = (s - factor) / (1.0 - factor);
                    }
428

429 430 431 432
                  *dest++ = *src++;
               }
            }
          break;
433
        }
434
    });
435
}
436

437
/* helper function of gimp_gegl_smudge_with_paint_process()
438 439
   src and dest can be the same address
 */
440
static inline void
441 442 443 444 445
gimp_gegl_smudge_with_paint_blend (const gfloat *src1,
                                   gfloat        src1_rate,
                                   const gfloat *src2,
                                   gfloat        src2_rate,
                                   gfloat       *dest,
446
                                   gboolean      no_erasing_src2)
447
{
448 449 450 451
  gfloat orginal_src2_alpha;
  gfloat src1_alpha;
  gfloat src2_alpha;
  gfloat result_alpha;
452
  gint   b;
453

454 455 456 457
  orginal_src2_alpha = src2[3];
  src1_alpha         = src1_rate * src1[3];
  src2_alpha         = src2_rate * orginal_src2_alpha;
  result_alpha       = src1_alpha + src2_alpha;
458

459
  if (result_alpha == 0)
460
    {
461 462 463
      memset (dest, 0, sizeof (gfloat) * 4);
      return;
    }
464

465 466
  for (b = 0; b < 3; b++)
    dest[b] = (src1[b] * src1_alpha + src2[b] * src2_alpha) / result_alpha;
467

468
  if (no_erasing_src2)
469
    {
470 471
      result_alpha = MAX (result_alpha, orginal_src2_alpha);
    }
472

473 474
  dest[3] = result_alpha;
}
475

476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
/* helper function of gimp_gegl_smudge_with_paint() */
static void
gimp_gegl_smudge_with_paint_process (gfloat       *accum,
                                     const gfloat *canvas,
                                     gfloat       *paint,
                                     gint          count,
                                     const gfloat *brush_color,
                                     gfloat        brush_a,
                                     gboolean      no_erasing,
                                     gfloat        flow,
                                     gfloat        rate)
{
  while (count--)
    {
      /* blend accum_buffer and canvas_buffer to accum_buffer */
      gimp_gegl_smudge_with_paint_blend (accum, rate, canvas, 1 - rate,
                                         accum, no_erasing);

      /* blend accum_buffer and brush color/pixmap to paint_buffer */
      if (brush_a == 0) /* pure smudge */
496
        {
497
          memcpy (paint, accum, sizeof (gfloat) * 4);
498
        }
499 500 501
      else
        {
          const gfloat *src1 = brush_color ? brush_color : paint;
502

503 504 505
          gimp_gegl_smudge_with_paint_blend (src1, flow, accum, 1 - flow,
                                             paint, no_erasing);
        }
506

507 508 509
      accum  += 4;
      canvas += 4;
      paint  += 4;
510 511 512 513 514 515 516 517 518
    }
}

/*  smudge painting calculation. Currently only smudge tool uses this function
 *  Accum = rate*Accum + (1-rate)*Canvas
 *  if brush_color!=NULL
 *    Paint = flow*brushColor + (1-flow)*Accum
 *  else
 *    Paint = flow*Paint + (1-flow)*Accum
519 520
 */
void
521 522 523 524 525 526 527 528 529
gimp_gegl_smudge_with_paint (GeglBuffer          *accum_buffer,
                             const GeglRectangle *accum_rect,
                             GeglBuffer          *canvas_buffer,
                             const GeglRectangle *canvas_rect,
                             const GimpRGB       *brush_color,
                             GeglBuffer          *paint_buffer,
                             gboolean             no_erasing,
                             gdouble              flow,
                             gdouble              rate)
530
{
Ell's avatar
Ell committed
531 532 533 534 535 536 537
  gfloat         brush_color_float[4];
  gfloat         brush_a = flow;
  GeglAccessMode paint_buffer_access_mode = (brush_color ?
                                             GEGL_ACCESS_WRITE :
                                             GEGL_ACCESS_READWRITE);
  gboolean       sse2 = (gimp_cpu_accel_get_support () &
                         GIMP_CPU_ACCEL_X86_SSE2);
538

539 540 541 542 543 544
  if (! accum_rect)
    accum_rect = gegl_buffer_get_extent (accum_buffer);

  if (! canvas_rect)
    canvas_rect = gegl_buffer_get_extent (canvas_buffer);

545 546 547 548 549 550 551 552 553 554 555
  /* convert brush color from double to float */
  if (brush_color)
    {
      const gdouble *brush_color_ptr = &brush_color->r;
      gint           b;

      for (b = 0; b < 4; b++)
        brush_color_float[b] = brush_color_ptr[b];

      brush_a *= brush_color_ptr[3];
    }
556

557 558 559
  gegl_parallel_distribute_area (
    accum_rect, PIXELS_PER_THREAD,
    [=] (const GeglRectangle *accum_area)
560
    {
561
      GeglBufferIterator *iter;
562

563 564 565 566
      SHIFTED_AREA (canvas, accum);

      iter = gegl_buffer_iterator_new (accum_buffer, accum_area, 0,
                                       babl_format ("RGBA float"),
567
                                       GEGL_ACCESS_READWRITE, GEGL_ABYSS_NONE, 3);
568 569 570 571 572

      gegl_buffer_iterator_add (iter, canvas_buffer, canvas_area, 0,
                                babl_format ("RGBA float"),
                                GEGL_ACCESS_READ, GEGL_ABYSS_NONE);

Ell's avatar
Ell committed
573 574 575 576 577
      gegl_buffer_iterator_add (iter, paint_buffer,
                                GEGL_RECTANGLE (accum_area->x - accum_rect->x,
                                                accum_area->y - accum_rect->y,
                                                0, 0),
                                0,
578 579 580 581
                                babl_format ("RGBA float"),
                                paint_buffer_access_mode, GEGL_ABYSS_NONE);

      while (gegl_buffer_iterator_next (iter))
582
        {
583 584 585
          gfloat       *accum  = (gfloat *)       iter->items[0].data;
          const gfloat *canvas = (const gfloat *) iter->items[1].data;
          gfloat       *paint  = (gfloat *)       iter->items[2].data;
Ell's avatar
Ell committed
586
          gint          count  = iter->length;
587 588 589 590 591 592 593 594 595 596 597 598 599 600

#if COMPILE_SSE2_INTRINISICS
          if (sse2 && ((guintptr) accum                                     |
                       (guintptr) canvas                                    |
                       (guintptr) (brush_color ? brush_color_float : paint) |
                       (guintptr) paint) % 16 == 0)
            {
              gimp_gegl_smudge_with_paint_process_sse2 (accum, canvas, paint, count,
                                                        brush_color ? brush_color_float :
                                                                      NULL,
                                                        brush_a,
                                                        no_erasing, flow, rate);
            }
          else
601
#endif
602 603 604 605 606 607 608
            {
              gimp_gegl_smudge_with_paint_process (accum, canvas, paint, count,
                                                   brush_color ? brush_color_float :
                                                                 NULL,
                                                   brush_a,
                                                   no_erasing, flow, rate);
            }
609
        }
610
    });
611
}
612 613 614 615 616 617 618 619

void
gimp_gegl_apply_mask (GeglBuffer          *mask_buffer,
                      const GeglRectangle *mask_rect,
                      GeglBuffer          *dest_buffer,
                      const GeglRectangle *dest_rect,
                      gdouble              opacity)
{
620 621 622 623 624 625
  if (! mask_rect)
    mask_rect = gegl_buffer_get_extent (mask_buffer);

  if (! dest_rect)
    dest_rect = gegl_buffer_get_extent (dest_buffer);

626 627 628
  gegl_parallel_distribute_area (
    mask_rect, PIXELS_PER_THREAD,
    [=] (const GeglRectangle *mask_area)
629 630
    {
      GeglBufferIterator *iter;
631

632
      SHIFTED_AREA (dest, mask);
633

634 635
      iter = gegl_buffer_iterator_new (mask_buffer, mask_area, 0,
                                       babl_format ("Y float"),
636
                                       GEGL_ACCESS_READ, GEGL_ABYSS_NONE, 2);
637

638 639 640
      gegl_buffer_iterator_add (iter, dest_buffer, dest_area, 0,
                                babl_format ("RGBA float"),
                                GEGL_ACCESS_READWRITE, GEGL_ABYSS_NONE);
641

642
      while (gegl_buffer_iterator_next (iter))
643
        {
644 645
          const gfloat *mask  = (const gfloat *) iter->items[0].data;
          gfloat       *dest  = (gfloat *)       iter->items[1].data;
646 647 648 649 650
          gint          count = iter->length;

          while (count--)
            {
              dest[3] *= *mask * opacity;
651

652 653 654
              mask += 1;
              dest += 4;
            }
655
        }
656
    });
657
}
658

659 660 661 662 663 664 665
void
gimp_gegl_combine_mask (GeglBuffer          *mask_buffer,
                        const GeglRectangle *mask_rect,
                        GeglBuffer          *dest_buffer,
                        const GeglRectangle *dest_rect,
                        gdouble              opacity)
{
666 667 668 669 670 671
  if (! mask_rect)
    mask_rect = gegl_buffer_get_extent (mask_buffer);

  if (! dest_rect)
    dest_rect = gegl_buffer_get_extent (dest_buffer);

672 673 674
  gegl_parallel_distribute_area (
    mask_rect, PIXELS_PER_THREAD,
    [=] (const GeglRectangle *mask_area)
675 676
    {
      GeglBufferIterator *iter;
677

678
      SHIFTED_AREA (dest, mask);
679

680 681
      iter = gegl_buffer_iterator_new (mask_buffer, mask_area, 0,
                                       babl_format ("Y float"),
682
                                       GEGL_ACCESS_READ, GEGL_ABYSS_NONE, 2);
683

684 685 686
      gegl_buffer_iterator_add (iter, dest_buffer, dest_area, 0,
                                babl_format ("Y float"),
                                GEGL_ACCESS_READWRITE, GEGL_ABYSS_NONE);
687

688
      while (gegl_buffer_iterator_next (iter))
689
        {
690 691
          const gfloat *mask  = (const gfloat *) iter->items[0].data;
          gfloat       *dest  = (gfloat *)       iter->items[1].data;
692 693 694 695 696
          gint          count = iter->length;

          while (count--)
            {
              *dest *= *mask * opacity;
697

698 699 700
              mask += 1;
              dest += 1;
            }
701
        }
702
    });
703 704
}

705
void
706 707 708 709 710 711
gimp_gegl_combine_mask_weird (GeglBuffer          *mask_buffer,
                              const GeglRectangle *mask_rect,
                              GeglBuffer          *dest_buffer,
                              const GeglRectangle *dest_rect,
                              gdouble              opacity,
                              gboolean             stipple)
712
{
713 714 715 716 717 718
  if (! mask_rect)
    mask_rect = gegl_buffer_get_extent (mask_buffer);

  if (! dest_rect)
    dest_rect = gegl_buffer_get_extent (dest_buffer);

719 720 721
  gegl_parallel_distribute_area (
    mask_rect, PIXELS_PER_THREAD,
    [=] (const GeglRectangle *mask_area)
722 723
    {
      GeglBufferIterator *iter;
724

725
      SHIFTED_AREA (dest, mask);
726

727 728
      iter = gegl_buffer_iterator_new (mask_buffer, mask_area, 0,
                                       babl_format ("Y float"),
729
                                       GEGL_ACCESS_READ, GEGL_ABYSS_NONE, 2);
730

731 732 733
      gegl_buffer_iterator_add (iter, dest_buffer, dest_area, 0,
                                babl_format ("Y float"),
                                GEGL_ACCESS_READWRITE, GEGL_ABYSS_NONE);
734

735
      while (gegl_buffer_iterator_next (iter))
736
        {
737 738
          const gfloat *mask  = (const gfloat *) iter->items[0].data;
          gfloat       *dest  = (gfloat *)       iter->items[1].data;
739 740 741
          gint          count = iter->length;

          if (stipple)
742
            {
743 744 745
              while (count--)
                {
                  dest[0] += (1.0 - dest[0]) * *mask * opacity;
746

747 748 749
                  mask += 1;
                  dest += 1;
                }
750
            }
751
          else
752
            {
753 754 755 756
              while (count--)
                {
                  if (opacity > dest[0])
                    dest[0] += (opacity - dest[0]) * *mask * opacity;
757

758 759 760
                  mask += 1;
                  dest += 1;
                }
761 762
            }
        }
763
    });
764
}
765 766 767 768 769 770 771 772 773 774

void
gimp_gegl_replace (GeglBuffer          *top_buffer,
                   const GeglRectangle *top_rect,
                   GeglBuffer          *bottom_buffer,
                   const GeglRectangle *bottom_rect,
                   GeglBuffer          *mask_buffer,
                   const GeglRectangle *mask_rect,
                   GeglBuffer          *dest_buffer,
                   const GeglRectangle *dest_rect,
775
                   gdouble              opacity,
776 777
                   const gboolean      *affect)
{
778 779 780 781 782 783 784 785 786 787 788 789
  if (! top_rect)
    top_rect = gegl_buffer_get_extent (top_buffer);

  if (! bottom_rect)
    bottom_rect = gegl_buffer_get_extent (bottom_buffer);

  if (! mask_rect)
    mask_rect = gegl_buffer_get_extent (mask_buffer);

  if (! dest_rect)
    dest_rect = gegl_buffer_get_extent (dest_buffer);

790 791 792
  gegl_parallel_distribute_area (
    top_rect, PIXELS_PER_THREAD,
    [=] (const GeglRectangle *top_area)
793 794
    {
      GeglBufferIterator *iter;
795

796 797 798
      SHIFTED_AREA (bottom, top);
      SHIFTED_AREA (mask, top);
      SHIFTED_AREA (dest, top);
799

800 801
      iter = gegl_buffer_iterator_new (top_buffer, top_area, 0,
                                       babl_format ("RGBA float"),
802
                                       GEGL_ACCESS_READ, GEGL_ABYSS_NONE, 4);
803

804 805 806
      gegl_buffer_iterator_add (iter, bottom_buffer, bottom_area, 0,
                                babl_format ("RGBA float"),
                                GEGL_ACCESS_READ, GEGL_ABYSS_NONE);
807

808 809 810
      gegl_buffer_iterator_add (iter, mask_buffer, mask_area, 0,
                                babl_format ("Y float"),
                                GEGL_ACCESS_READ, GEGL_ABYSS_NONE);
811

812 813 814
      gegl_buffer_iterator_add (iter, dest_buffer, dest_area, 0,
                                babl_format ("RGBA float"),
                                GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE);
815

816
      while (gegl_buffer_iterator_next (iter))
817
        {
818 819 820 821
          const gfloat *top    = (const gfloat *) iter->items[0].data;
          const gfloat *bottom = (const gfloat *) iter->items[1].data;
          const gfloat *mask   = (const gfloat *) iter->items[2].data;
          gfloat       *dest   = (gfloat *)       iter->items[3].data;
822
          gint          count  = iter->length;
823

824
          while (count--)
825
            {
826 827 828 829 830 831 832 833 834
              gint    b;
              gdouble mask_val = *mask * opacity;

              /* calculate new alpha first. */
              gfloat   s1_a  = bottom[3];
              gfloat   s2_a  = top[3];
              gdouble  a_val = s1_a + mask_val * (s2_a - s1_a);

              if (a_val == 0.0)
835
                {
836 837 838 839 840 841 842 843
                  /* In any case, write out versions of the blending
                   * function that result when combinations of s1_a, s2_a,
                   * and mask_val --> 0 (or mask_val -->1)
                   */

                  /* 1: s1_a, s2_a, AND mask_val all approach 0+: */
                  /* 2: s1_a AND s2_a both approach 0+, regardless of mask_val: */
                  if (s1_a + s2_a == 0.0)
844
                    {
845 846 847
                      for (b = 0; b < 3; b++)
                        {
                          gfloat new_val;
848

849
                          new_val = bottom[b] + mask_val * (top[b] - bottom[b]);
850

851 852
                          dest[b] = affect[b] ? new_val : bottom[b];
                        }
853 854
                    }

855 856 857 858 859 860
                  /* 3: mask_val AND s1_a both approach 0+, regardless of s2_a  */
                  else if (s1_a + mask_val == 0.0)
                    {
                      for (b = 0; b < 3; b++)
                        dest[b] = bottom[b];
                    }
861

862 863 864 865 866 867
                  /* 4: mask_val -->1 AND s2_a -->0, regardless of s1_a */
                  else if (1.0 - mask_val + s2_a == 0.0)
                    {
                      for (b = 0; b < 3; b++)
                        dest[b] = affect[b] ? top[b] : bottom[b];
                    }
868
                }
869
              else
870
                {
871 872 873 874 875 876 877 878 879
                  gdouble a_recip = 1.0 / a_val;

                  /* possible optimization: fold a_recip into s1_a and s2_a */
                  for (b = 0; b < 3; b++)
                    {
                      gfloat new_val = a_recip * (bottom[b] * s1_a + mask_val *
                                                  (top[b] * s2_a - bottom[b] * s1_a));
                      dest[b] = affect[b] ? new_val : bottom[b];
                    }
880 881
                }

882
              dest[3] = affect[3] ? a_val : s1_a;
883

884 885 886 887 888
              top    += 4;
              bottom += 4;
              mask   += 1;
              dest   += 4;
            }
889
        }
890
    });
891
}
892

893 894 895 896 897 898 899 900
void
gimp_gegl_index_to_mask (GeglBuffer          *indexed_buffer,
                         const GeglRectangle *indexed_rect,
                         const Babl          *indexed_format,
                         GeglBuffer          *mask_buffer,
                         const GeglRectangle *mask_rect,
                         gint                 index)
{
901 902 903 904 905 906
  if (! indexed_rect)
    indexed_rect = gegl_buffer_get_extent (indexed_buffer);

  if (! mask_rect)
    mask_rect = gegl_buffer_get_extent (mask_buffer);

907 908 909
  gegl_parallel_distribute_area (
    indexed_rect, PIXELS_PER_THREAD,
    [=] (const GeglRectangle *indexed_area)
910 911
    {
      GeglBufferIterator *iter;
912

913
      SHIFTED_AREA (mask, indexed);
914

915 916
      iter = gegl_buffer_iterator_new (indexed_buffer, indexed_area, 0,
                                       indexed_format,
917
                                       GEGL_ACCESS_READ, GEGL_ABYSS_NONE, 2);
918

919 920 921
      gegl_buffer_iterator_add (iter, mask_buffer, mask_area, 0,
                                babl_format ("Y float"),
                                GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE);
922

923
      while (gegl_buffer_iterator_next (iter))
924
        {
925 926
          const guchar *indexed = (const guchar *) iter->items[0].data;
          gfloat       *mask    = (gfloat *)       iter->items[1].data;
927
          gint          count   = iter->length;
928

929 930 931 932 933 934 935 936 937 938
          while (count--)
            {
              if (*indexed == index)
                *mask = 1.0;
              else
                *mask = 0.0;

              indexed++;
              mask++;
            }
939
        }
940
    });
941 942
}

943 944 945 946 947 948 949 950
static void
gimp_gegl_convert_color_profile_progress (GimpProgress *progress,
                                          gdouble       value)
{
  if (gegl_is_main_thread ())
    gimp_progress_set_value (progress, value);
}

951 952 953 954 955 956 957 958
void
gimp_gegl_convert_color_profile (GeglBuffer               *src_buffer,
                                 const GeglRectangle      *src_rect,
                                 GimpColorProfile         *src_profile,
                                 GeglBuffer               *dest_buffer,
                                 const GeglRectangle      *dest_rect,
                                 GimpColorProfile         *dest_profile,
                                 GimpColorRenderingIntent  intent,
959 960
                                 gboolean                  bpc,
                                 GimpProgress             *progress)
961
{
962 963 964 965
  GimpColorTransform *transform;
  guint               flags = 0;
  const Babl         *src_format;
  const Babl         *dest_format;
966

967 968 969 970 971 972
  g_return_if_fail (GEGL_IS_BUFFER (src_buffer));
  g_return_if_fail (GIMP_IS_COLOR_PROFILE (src_profile));
  g_return_if_fail (GEGL_IS_BUFFER (dest_buffer));
  g_return_if_fail (GIMP_IS_COLOR_PROFILE (dest_profile));
  g_return_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress));

973 974 975 976
  src_format  = gegl_buffer_get_format (src_buffer);
  dest_format = gegl_buffer_get_format (dest_buffer);

  if (bpc)
977
    flags |= GIMP_COLOR_TRANSFORM_FLAGS_BLACK_POINT_COMPENSATION;
978

979 980
  flags |= GIMP_COLOR_TRANSFORM_FLAGS_NOOPTIMIZE;

981 982
  transform = gimp_color_transform_new (src_profile,  src_format,
                                        dest_profile, dest_format,
983 984
                                        intent,
                                        (GimpColorTransformFlags) flags);
985

986 987 988 989 990 991
  if (! src_rect)
    src_rect = gegl_buffer_get_extent (src_buffer);

  if (! dest_rect)
    dest_rect = gegl_buffer_get_extent (dest_buffer);

992 993
  if (transform)
    {
994
      if (progress)
995 996 997 998 999 1000
        {
          g_signal_connect_swapped (
            transform, "progress",
            G_CALLBACK (gimp_gegl_convert_color_profile_progress),
            progress);
        }
1001

1002 1003
      GIMP_TIMER_START ();

1004 1005 1006
      gegl_parallel_distribute_area (
        src_rect, PIXELS_PER_THREAD,
        [=] (const GeglRectangle *src_area)
1007 1008 1009 1010 1011 1012 1013
        {
          SHIFTED_AREA (dest, src);

          gimp_color_transform_process_buffer (transform,
                                               src_buffer,  src_area,
                                               dest_buffer, dest_area);
        });
1014

1015 1016
      GIMP_TIMER_END ("converting buffer");

1017
      g_object_unref (transform);
1018 1019 1020
    }
  else
    {
1021 1022
      gimp_gegl_buffer_copy (src_buffer,  src_rect, GEGL_ABYSS_NONE,
                             dest_buffer, dest_rect);
1023 1024 1025

      if (progress)
        gimp_progress_set_value (progress, 1.0);
1026 1027
    }
}
1028

Ell's avatar
Ell committed
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
void
gimp_gegl_average_color (GeglBuffer          *buffer,
                         const GeglRectangle *rect,
                         gboolean             clip_to_buffer,
                         GeglAbyssPolicy      abyss_policy,
                         const Babl          *format,
                         gpointer             color)
{
  typedef struct
  {
    gfloat color[4];
    gint   n;
  } Sum;

1043
  const Babl        *average_format;
Ell's avatar
Ell committed
1044
  GeglRectangle      roi;
1045
  GSList * volatile  sums    = NULL;
Ell's avatar
Ell committed
1046
  GSList            *list;
1047
  Sum                average = {};
Ell's avatar
Ell committed
1048 1049 1050 1051 1052
  gint               c;

  g_return_if_fail (GEGL_IS_BUFFER (buffer));
  g_return_if_fail (color != NULL);

1053 1054 1055
  average_format = babl_format_with_space ("RaGaBaA float",
                                           babl_format_get_space (format));

Ell's avatar
Ell committed
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
  if (! rect)
    rect = gegl_buffer_get_extent (buffer);

  if (! format)
    format = gegl_buffer_get_format (buffer);

  if (clip_to_buffer)
    gegl_rectangle_intersect (&roi, rect, gegl_buffer_get_extent (buffer));
  else
    roi = *rect;

1067 1068 1069
  gegl_parallel_distribute_area (
    &roi, PIXELS_PER_THREAD,
    [&] (const GeglRectangle *area)
Ell's avatar
Ell committed
1070 1071 1072 1073 1074 1075 1076
    {
      Sum                *sum;
      GeglBufferIterator *iter;
      gfloat              color[4] = {};
      gint                n        = 0;

      iter = gegl_buffer_iterator_new (buffer, area, 0, average_format,
1077
                                       GEGL_BUFFER_READ, abyss_policy, 1);
Ell's avatar
Ell committed
1078 1079 1080

      while (gegl_buffer_iterator_next (iter))
        {
1081
          const gfloat *p = (const gfloat *) iter->items[0].data;
Ell's avatar
Ell committed
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 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
          gint          i;

          for (i = 0; i < iter->length; i++)
            {
              gint c;

              for (c = 0; c < 4; c++)
                color[c] += p[c];

              p += 4;
            }

          n += iter->length;
        }

      sum = g_slice_new (Sum);

      memcpy (sum->color, color, sizeof (color));
      sum->n = n;

      gimp_atomic_slist_push_head (&sums, sum);
    });

  for (list = sums; list; list = g_slist_next (list))
    {
      Sum *sum = (Sum *) list->data;

      for (c = 0; c < 4; c++)
        average.color[c] += sum->color[c];

      average.n += sum->n;

      g_slice_free (Sum, sum);
    }

  g_slist_free (sums);

  if (average.n > 0)
    {
      for (c = 0; c < 4; c++)
        average.color[c] /= average.n;
    }

  babl_process (babl_fish (average_format, format), average.color, color, 1);
}

1128
} /* extern "C" */