/* PIKA - Photo and Image Kooker Application
 * a rebranding of The GNU Image Manipulation Program (created with heckimp)
 * A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
 *
 * Original copyright, applying to most contents (license remains unchanged): 
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <cairo.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "libpikabase/pikabase.h"

#include "core-types.h"

#include "gegl/pikaapplicator.h"

#include "pika.h"
#include "pikachannel.h"
#include "pikachunkiterator.h"
#include "pikadrawable-combine.h"
#include "pikaimage.h"


void
pika_drawable_real_apply_buffer (PikaDrawable           *drawable,
                                 GeglBuffer             *buffer,
                                 const GeglRectangle    *buffer_region,
                                 gboolean                push_undo,
                                 const gchar            *undo_desc,
                                 gdouble                 opacity,
                                 PikaLayerMode           mode,
                                 PikaLayerColorSpace     blend_space,
                                 PikaLayerColorSpace     composite_space,
                                 PikaLayerCompositeMode  composite_mode,
                                 GeglBuffer             *base_buffer,
                                 gint                    base_x,
                                 gint                    base_y)
{
  PikaItem          *item  = PIKA_ITEM (drawable);
  PikaImage         *image = pika_item_get_image (item);
  PikaChannel       *mask  = pika_image_get_mask (image);
  PikaApplicator    *applicator;
  PikaChunkIterator *iter;
  gint               x, y, width, height;
  gint               offset_x, offset_y;

  /*  don't apply the mask to itself and don't apply an empty mask  */
  if (PIKA_DRAWABLE (mask) == drawable || pika_channel_is_empty (mask))
    mask = NULL;

  if (! base_buffer)
    base_buffer = pika_drawable_get_buffer (drawable);

  /*  get the layer offsets  */
  pika_item_get_offset (item, &offset_x, &offset_y);

  /*  make sure the image application coordinates are within drawable bounds  */
  if (! pika_rectangle_intersect (base_x, base_y,
                                  buffer_region->width, buffer_region->height,
                                  0, 0,
                                  pika_item_get_width  (item),
                                  pika_item_get_height (item),
                                  &x, &y, &width, &height))
    {
      return;
    }

  if (mask)
    {
      PikaItem *mask_item = PIKA_ITEM (mask);

      /*  make sure coordinates are in mask bounds ...
       *  we need to add the layer offset to transform coords
       *  into the mask coordinate system
       */
      if (! pika_rectangle_intersect (x, y, width, height,
                                      -offset_x, -offset_y,
                                      pika_item_get_width  (mask_item),
                                      pika_item_get_height (mask_item),
                                      &x, &y, &width, &height))
        {
          return;
        }
    }

  if (push_undo)
    {
      pika_drawable_push_undo (drawable, undo_desc,
                               NULL, x, y, width, height);
    }

  applicator = pika_applicator_new (NULL);

  if (mask)
    {
      GeglBuffer *mask_buffer;

      mask_buffer = pika_drawable_get_buffer (PIKA_DRAWABLE (mask));

      pika_applicator_set_mask_buffer (applicator, mask_buffer);
      pika_applicator_set_mask_offset (applicator, -offset_x, -offset_y);
    }

  pika_applicator_set_src_buffer (applicator, base_buffer);
  pika_applicator_set_dest_buffer (applicator,
                                   pika_drawable_get_buffer (drawable));

  pika_applicator_set_apply_buffer (applicator, buffer);
  pika_applicator_set_apply_offset (applicator,
                                    base_x - buffer_region->x,
                                    base_y - buffer_region->y);

  pika_applicator_set_opacity (applicator, opacity);
  pika_applicator_set_mode (applicator, mode,
                            blend_space, composite_space, composite_mode);
  pika_applicator_set_affect (applicator,
                              pika_drawable_get_active_mask (drawable));

  iter = pika_chunk_iterator_new (cairo_region_create_rectangle (
    &(cairo_rectangle_int_t) {x, y, width, height}));

  while (pika_chunk_iterator_next (iter))
    {
      GeglRectangle rect;

      while (pika_chunk_iterator_get_rect (iter, &rect))
        pika_applicator_blit (applicator, &rect);
    }

  g_object_unref (applicator);
}
