/* 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
 *
 * pikaviewrenderer.c
 * Copyright (C) 2003 Michael Natterer <mitch@gimp.org>
 * Copyright (C) 2007 Sven Neumann <sven@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
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>

#include <gegl.h>
#include <gtk/gtk.h>

#include "libpikacolor/pikacolor.h"
#include "libpikaconfig/pikaconfig.h"
#include "libpikamath/pikamath.h"
#include "libpikabase/pikabase.h"
#include "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "config/pikacoreconfig.h"

#include "gegl/pika-gegl-loops.h"

#include "core/pika.h"
#include "core/pikacontext.h"
#include "core/pikaimage.h"
#include "core/pikatempbuf.h"
#include "core/pikaviewable.h"

#include "pikarender.h"
#include "pikaviewrenderer.h"
#include "pikaviewrenderer-utils.h"
#include "pikawidgets-utils.h"

#include "pika-priorities.h"


#define RGB_EPSILON 1e-6

enum
{
  UPDATE,
  LAST_SIGNAL
};


struct _PikaViewRendererPrivate
{
  cairo_pattern_t    *pattern;
  cairo_surface_t    *icon_surface;
  gchar              *bg_icon_name;

  PikaColorConfig    *color_config;
  PikaColorTransform *profile_transform;

  gboolean            needs_render;
  guint               idle_id;
};


static void      pika_view_renderer_dispose           (GObject            *object);
static void      pika_view_renderer_finalize          (GObject            *object);

static gboolean  pika_view_renderer_idle_update       (PikaViewRenderer   *renderer);
static void      pika_view_renderer_real_set_context  (PikaViewRenderer   *renderer,
                                                       PikaContext        *context);
static void      pika_view_renderer_real_invalidate   (PikaViewRenderer   *renderer);
static void      pika_view_renderer_real_draw         (PikaViewRenderer   *renderer,
                                                       GtkWidget          *widget,
                                                       cairo_t            *cr,
                                                       gint                available_width,
                                                       gint                available_height);
static void      pika_view_renderer_real_render       (PikaViewRenderer   *renderer,
                                                       GtkWidget          *widget);

static void      pika_view_renderer_size_changed      (PikaViewRenderer   *renderer,
                                                       PikaViewable       *viewable);
static void      pika_view_renderer_profile_changed   (PikaViewRenderer   *renderer,
                                                       PikaViewable       *viewable);
static void      pika_view_renderer_config_notify     (GObject            *config,
                                                       const GParamSpec   *pspec,
                                                       PikaViewRenderer   *renderer);

static void      pika_view_render_temp_buf_to_surface (PikaViewRenderer   *renderer,
                                                       GtkWidget          *widget,
                                                       PikaTempBuf        *temp_buf,
                                                       gint                temp_buf_x,
                                                       gint                temp_buf_y,
                                                       gint                channel,
                                                       PikaViewBG          inside_bg,
                                                       PikaViewBG          outside_bg,
                                                       cairo_surface_t    *surface,
                                                       gint                dest_width,
                                                       gint                dest_height);

static cairo_pattern_t *
                 pika_view_renderer_create_background (PikaViewRenderer   *renderer,
                                                       GtkWidget          *widget);


G_DEFINE_TYPE_WITH_PRIVATE (PikaViewRenderer, pika_view_renderer, G_TYPE_OBJECT)

#define parent_class pika_view_renderer_parent_class

static guint renderer_signals[LAST_SIGNAL] = { 0 };

static PikaRGB  black_color;
static PikaRGB  white_color;
static PikaRGB  green_color;
static PikaRGB  red_color;


static void
pika_view_renderer_class_init (PikaViewRendererClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  renderer_signals[UPDATE] =
    g_signal_new ("update",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (PikaViewRendererClass, update),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 0);

  object_class->dispose  = pika_view_renderer_dispose;
  object_class->finalize = pika_view_renderer_finalize;

  klass->update          = NULL;
  klass->set_context     = pika_view_renderer_real_set_context;
  klass->invalidate      = pika_view_renderer_real_invalidate;
  klass->draw            = pika_view_renderer_real_draw;
  klass->render          = pika_view_renderer_real_render;

  klass->frame           = NULL;
  klass->frame_left      = 0;
  klass->frame_right     = 0;
  klass->frame_top       = 0;
  klass->frame_bottom    = 0;

  pika_rgba_set (&black_color, 0.0, 0.0, 0.0, PIKA_OPACITY_OPAQUE);
  pika_rgba_set (&white_color, 1.0, 1.0, 1.0, PIKA_OPACITY_OPAQUE);
  pika_rgba_set (&green_color, 0.0, 0.94, 0.0, PIKA_OPACITY_OPAQUE);
  pika_rgba_set (&red_color,   1.0, 0.0, 0.0, PIKA_OPACITY_OPAQUE);
}

static void
pika_view_renderer_init (PikaViewRenderer *renderer)
{
  renderer->priv = pika_view_renderer_get_instance_private (renderer);

  renderer->viewable = NULL;

  renderer->dot_for_dot  = TRUE;

  renderer->border_type  = PIKA_VIEW_BORDER_BLACK;
  renderer->border_color = black_color;

  renderer->size         = -1;

  renderer->priv->needs_render = TRUE;
}

static void
pika_view_renderer_dispose (GObject *object)
{
  PikaViewRenderer *renderer = PIKA_VIEW_RENDERER (object);

  if (renderer->viewable)
    pika_view_renderer_set_viewable (renderer, NULL);

  if (renderer->context)
    pika_view_renderer_set_context (renderer, NULL);

  if (renderer->priv->color_config)
    pika_view_renderer_set_color_config (renderer, NULL);

  pika_view_renderer_remove_idle (renderer);

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
pika_view_renderer_finalize (GObject *object)
{
  PikaViewRenderer *renderer = PIKA_VIEW_RENDERER (object);

  g_clear_pointer (&renderer->priv->pattern, cairo_pattern_destroy);
  g_clear_pointer (&renderer->surface, cairo_surface_destroy);
  g_clear_pointer (&renderer->priv->icon_surface, cairo_surface_destroy);
  g_clear_pointer (&renderer->priv->bg_icon_name, g_free);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static PikaViewRenderer *
pika_view_renderer_new_internal (PikaContext *context,
                                 GType        viewable_type,
                                 gboolean     is_popup)
{
  PikaViewRenderer *renderer;

  renderer = g_object_new (pika_view_renderer_type_from_viewable_type (viewable_type),
                           NULL);

  renderer->viewable_type = viewable_type;
  renderer->is_popup      = is_popup ? TRUE : FALSE;

  if (context)
    pika_view_renderer_set_context (renderer, context);

  return renderer;
}


/*  public functions  */

PikaViewRenderer *
pika_view_renderer_new (PikaContext *context,
                        GType        viewable_type,
                        gint         size,
                        gint         border_width,
                        gboolean     is_popup)
{
  PikaViewRenderer *renderer;

  g_return_val_if_fail (context == NULL || PIKA_IS_CONTEXT (context), NULL);
  g_return_val_if_fail (g_type_is_a (viewable_type, PIKA_TYPE_VIEWABLE), NULL);
  g_return_val_if_fail (size >  0 &&
                        size <= PIKA_VIEWABLE_MAX_PREVIEW_SIZE, NULL);
  g_return_val_if_fail (border_width >= 0 &&
                        border_width <= PIKA_VIEW_MAX_BORDER_WIDTH, NULL);

  renderer = pika_view_renderer_new_internal (context, viewable_type,
                                              is_popup);

  pika_view_renderer_set_size (renderer, size, border_width);
  pika_view_renderer_remove_idle (renderer);

  return renderer;
}

PikaViewRenderer *
pika_view_renderer_new_full (PikaContext *context,
                             GType        viewable_type,
                             gint         width,
                             gint         height,
                             gint         border_width,
                             gboolean     is_popup)
{
  PikaViewRenderer *renderer;

  g_return_val_if_fail (context == NULL || PIKA_IS_CONTEXT (context), NULL);
  g_return_val_if_fail (g_type_is_a (viewable_type, PIKA_TYPE_VIEWABLE), NULL);
  g_return_val_if_fail (width >  0 &&
                        width <= PIKA_VIEWABLE_MAX_PREVIEW_SIZE, NULL);
  g_return_val_if_fail (height > 0 &&
                        height <= PIKA_VIEWABLE_MAX_PREVIEW_SIZE, NULL);
  g_return_val_if_fail (border_width >= 0 &&
                        border_width <= PIKA_VIEW_MAX_BORDER_WIDTH, NULL);

  renderer = pika_view_renderer_new_internal (context, viewable_type,
                                              is_popup);

  pika_view_renderer_set_size_full (renderer, width, height, border_width);
  pika_view_renderer_remove_idle (renderer);

  return renderer;
}

void
pika_view_renderer_set_context (PikaViewRenderer *renderer,
                                PikaContext      *context)
{
  g_return_if_fail (PIKA_IS_VIEW_RENDERER (renderer));
  g_return_if_fail (context == NULL || PIKA_IS_CONTEXT (context));

  if (context != renderer->context)
    {
      PIKA_VIEW_RENDERER_GET_CLASS (renderer)->set_context (renderer,
                                                            context);

      if (renderer->viewable)
        pika_view_renderer_invalidate (renderer);
    }
}

static void
pika_view_renderer_weak_notify (PikaViewRenderer *renderer,
                                PikaViewable     *viewable)
{
  renderer->viewable = NULL;

  pika_view_renderer_update_idle (renderer);
}

void
pika_view_renderer_set_viewable (PikaViewRenderer *renderer,
                                 PikaViewable     *viewable)
{
  g_return_if_fail (PIKA_IS_VIEW_RENDERER (renderer));
  g_return_if_fail (viewable == NULL || PIKA_IS_VIEWABLE (viewable));

  if (viewable)
    g_return_if_fail (g_type_is_a (G_TYPE_FROM_INSTANCE (viewable),
                                   renderer->viewable_type));

  if (viewable == renderer->viewable)
    return;

  g_clear_pointer (&renderer->surface, cairo_surface_destroy);
  g_clear_pointer (&renderer->priv->icon_surface, cairo_surface_destroy);

  pika_view_renderer_free_color_transform (renderer);

  if (renderer->viewable)
    {
      g_object_weak_unref (G_OBJECT (renderer->viewable),
                           (GWeakNotify) pika_view_renderer_weak_notify,
                           renderer);

      g_signal_handlers_disconnect_by_func (renderer->viewable,
                                            G_CALLBACK (pika_view_renderer_invalidate),
                                            renderer);

      g_signal_handlers_disconnect_by_func (renderer->viewable,
                                            G_CALLBACK (pika_view_renderer_size_changed),
                                            renderer);

      if (PIKA_IS_COLOR_MANAGED (renderer->viewable))
        g_signal_handlers_disconnect_by_func (renderer->viewable,
                                              G_CALLBACK (pika_view_renderer_profile_changed),
                                              renderer);
    }

  renderer->viewable = viewable;

  if (renderer->viewable)
    {
      g_object_weak_ref (G_OBJECT (renderer->viewable),
                         (GWeakNotify) pika_view_renderer_weak_notify,
                         renderer);

      g_signal_connect_swapped (renderer->viewable,
                                "invalidate-preview",
                                G_CALLBACK (pika_view_renderer_invalidate),
                                renderer);

      g_signal_connect_swapped (renderer->viewable,
                                "size-changed",
                                G_CALLBACK (pika_view_renderer_size_changed),
                                renderer);

      if (PIKA_IS_COLOR_MANAGED (renderer->viewable))
        g_signal_connect_swapped (renderer->viewable,
                                  "profile-changed",
                                  G_CALLBACK (pika_view_renderer_profile_changed),
                                  renderer);

      if (renderer->size != -1)
        pika_view_renderer_set_size (renderer, renderer->size,
                                     renderer->border_width);

      pika_view_renderer_invalidate (renderer);
    }
  else
    {
      pika_view_renderer_update_idle (renderer);
    }
}

void
pika_view_renderer_set_size (PikaViewRenderer *renderer,
                             gint              view_size,
                             gint              border_width)
{
  gint width;
  gint height;

  g_return_if_fail (PIKA_IS_VIEW_RENDERER (renderer));
  g_return_if_fail (view_size >  0 &&
                    view_size <= PIKA_VIEWABLE_MAX_PREVIEW_SIZE);
  g_return_if_fail (border_width >= 0 &&
                    border_width <= PIKA_VIEW_MAX_BORDER_WIDTH);

  renderer->size = view_size;

  if (renderer->viewable)
    {
      pika_viewable_get_preview_size (renderer->viewable,
                                      view_size,
                                      renderer->is_popup,
                                      renderer->dot_for_dot,
                                      &width, &height);
    }
  else
    {
      width  = view_size;
      height = view_size;
    }

  pika_view_renderer_set_size_full (renderer, width, height, border_width);
}

void
pika_view_renderer_set_size_full (PikaViewRenderer *renderer,
                                  gint              width,
                                  gint              height,
                                  gint              border_width)
{
  g_return_if_fail (PIKA_IS_VIEW_RENDERER (renderer));
  g_return_if_fail (width >  0 &&
                    width <= PIKA_VIEWABLE_MAX_PREVIEW_SIZE);
  g_return_if_fail (height > 0 &&
                    height <= PIKA_VIEWABLE_MAX_PREVIEW_SIZE);
  g_return_if_fail (border_width >= 0 &&
                    border_width <= PIKA_VIEW_MAX_BORDER_WIDTH);

  if (width        != renderer->width  ||
      height       != renderer->height ||
      border_width != renderer->border_width)
    {
      renderer->width        = width;
      renderer->height       = height;
      renderer->border_width = border_width;

      g_clear_pointer (&renderer->surface, cairo_surface_destroy);

      if (renderer->viewable)
        pika_view_renderer_invalidate (renderer);
    }
}

void
pika_view_renderer_set_dot_for_dot (PikaViewRenderer *renderer,
                                    gboolean             dot_for_dot)
{
  g_return_if_fail (PIKA_IS_VIEW_RENDERER (renderer));

  if (dot_for_dot != renderer->dot_for_dot)
    {
      renderer->dot_for_dot = dot_for_dot ? TRUE: FALSE;

      if (renderer->size != -1)
        pika_view_renderer_set_size (renderer, renderer->size,
                                     renderer->border_width);

      pika_view_renderer_invalidate (renderer);
    }
}

void
pika_view_renderer_set_border_type (PikaViewRenderer   *renderer,
                                    PikaViewBorderType  border_type)
{
  PikaRGB *border_color = &black_color;

  g_return_if_fail (PIKA_IS_VIEW_RENDERER (renderer));

  renderer->border_type = border_type;

  switch (border_type)
    {
    case PIKA_VIEW_BORDER_BLACK:
      border_color = &black_color;
      break;
    case PIKA_VIEW_BORDER_WHITE:
      border_color = &white_color;
      break;
    case PIKA_VIEW_BORDER_GREEN:
      border_color = &green_color;
      break;
    case PIKA_VIEW_BORDER_RED:
      border_color = &red_color;
      break;
    }

  pika_view_renderer_set_border_color (renderer, border_color);
}

void
pika_view_renderer_set_border_color (PikaViewRenderer *renderer,
                                     const PikaRGB    *color)
{
  g_return_if_fail (PIKA_IS_VIEW_RENDERER (renderer));
  g_return_if_fail (color != NULL);

  if (pika_rgb_distance (&renderer->border_color, color) > RGB_EPSILON)
    {
      renderer->border_color = *color;

      pika_view_renderer_update_idle (renderer);
    }
}

void
pika_view_renderer_set_background (PikaViewRenderer *renderer,
                                   const gchar      *icon_name)
{
  g_return_if_fail (PIKA_IS_VIEW_RENDERER (renderer));

  if (renderer->priv->bg_icon_name)
    g_free (renderer->priv->bg_icon_name);

  renderer->priv->bg_icon_name = g_strdup (icon_name);

  g_clear_object (&renderer->priv->pattern);
}

void
pika_view_renderer_set_color_config (PikaViewRenderer *renderer,
                                     PikaColorConfig  *color_config)
{
  g_return_if_fail (PIKA_IS_VIEW_RENDERER (renderer));
  g_return_if_fail (color_config == NULL || PIKA_IS_COLOR_CONFIG (color_config));

  if (color_config != renderer->priv->color_config)
    {
      if (renderer->priv->color_config)
        g_signal_handlers_disconnect_by_func (renderer->priv->color_config,
                                              pika_view_renderer_config_notify,
                                              renderer);

      g_set_object (&renderer->priv->color_config, color_config);

      if (renderer->priv->color_config)
        g_signal_connect (renderer->priv->color_config, "notify",
                          G_CALLBACK (pika_view_renderer_config_notify),
                          renderer);

      pika_view_renderer_config_notify (G_OBJECT (renderer->priv->color_config),
                                        NULL, renderer);
    }
}

void
pika_view_renderer_invalidate (PikaViewRenderer *renderer)
{
  g_return_if_fail (PIKA_IS_VIEW_RENDERER (renderer));

  if (renderer->priv->idle_id)
    {
      g_source_remove (renderer->priv->idle_id);
      renderer->priv->idle_id = 0;
    }

  PIKA_VIEW_RENDERER_GET_CLASS (renderer)->invalidate (renderer);

  renderer->priv->idle_id =
    g_idle_add_full (PIKA_PRIORITY_VIEWABLE_IDLE,
                     (GSourceFunc) pika_view_renderer_idle_update,
                     renderer, NULL);
}

void
pika_view_renderer_update (PikaViewRenderer *renderer)
{
  g_return_if_fail (PIKA_IS_VIEW_RENDERER (renderer));

  if (renderer->priv->idle_id)
    {
      g_source_remove (renderer->priv->idle_id);
      renderer->priv->idle_id = 0;
    }

  g_signal_emit (renderer, renderer_signals[UPDATE], 0);
}

void
pika_view_renderer_update_idle (PikaViewRenderer *renderer)
{
  g_return_if_fail (PIKA_IS_VIEW_RENDERER (renderer));

  if (renderer->priv->idle_id)
    g_source_remove (renderer->priv->idle_id);

  renderer->priv->idle_id =
    g_idle_add_full (PIKA_PRIORITY_VIEWABLE_IDLE,
                     (GSourceFunc) pika_view_renderer_idle_update,
                     renderer, NULL);
}

void
pika_view_renderer_remove_idle (PikaViewRenderer *renderer)
{
  g_return_if_fail (PIKA_IS_VIEW_RENDERER (renderer));

  if (renderer->priv->idle_id)
    {
      g_source_remove (renderer->priv->idle_id);
      renderer->priv->idle_id = 0;
    }
}

void
pika_view_renderer_draw (PikaViewRenderer *renderer,
                         GtkWidget        *widget,
                         cairo_t          *cr,
                         gint              available_width,
                         gint              available_height)
{
  g_return_if_fail (PIKA_IS_VIEW_RENDERER (renderer));
  g_return_if_fail (GTK_IS_WIDGET (widget));
  g_return_if_fail (cr != NULL);

  if (G_UNLIKELY (renderer->context == NULL))
    g_warning ("%s: renderer->context is NULL", G_STRFUNC);

  if (! gtk_widget_is_drawable (widget))
    return;

  if (renderer->viewable)
    {
      cairo_save (cr);

      PIKA_VIEW_RENDERER_GET_CLASS (renderer)->draw (renderer, widget, cr,
                                                     available_width,
                                                     available_height);

      cairo_restore (cr);
    }
  else
    {
      PikaViewableClass *viewable_class;

      viewable_class = g_type_class_ref (renderer->viewable_type);

      pika_view_renderer_render_icon (renderer,
                                      widget,
                                      viewable_class->default_icon_name);
      renderer->priv->needs_render = FALSE;

      g_type_class_unref (viewable_class);

      pika_view_renderer_real_draw (renderer, widget, cr,
                                    available_width,
                                    available_height);
    }

  if (renderer->border_width > 0)
    {
      gint    width  = renderer->width  + renderer->border_width;
      gint    height = renderer->height + renderer->border_width;
      gdouble x, y;

      cairo_set_line_width (cr, renderer->border_width);
      cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
      pika_cairo_set_source_rgb (cr, &renderer->border_color);

      x = (available_width  - width)  / 2.0;
      y = (available_height - height) / 2.0;

      cairo_rectangle (cr, x, y, width, height);
      cairo_stroke (cr);
    }
}


/*  private functions  */

static gboolean
pika_view_renderer_idle_update (PikaViewRenderer *renderer)
{
  renderer->priv->idle_id = 0;

  pika_view_renderer_update (renderer);

  return FALSE;
}

static void
pika_view_renderer_real_set_context (PikaViewRenderer *renderer,
                                     PikaContext      *context)
{
  if (renderer->context &&
      renderer->priv->color_config ==
      renderer->context->pika->config->color_management)
    {
      pika_view_renderer_set_color_config (renderer, NULL);
    }

  g_set_object (&renderer->context, context);

  if (renderer->context &&
      renderer->priv->color_config == NULL)
    {
      pika_view_renderer_set_color_config (renderer,
                                           renderer->context->pika->config->color_management);
    }
}

static void
pika_view_renderer_real_invalidate (PikaViewRenderer *renderer)
{
  renderer->priv->needs_render = TRUE;
}

static void
pika_view_renderer_real_draw (PikaViewRenderer *renderer,
                              GtkWidget        *widget,
                              cairo_t          *cr,
                              gint              available_width,
                              gint              available_height)
{
  if (renderer->priv->needs_render)
    {
      PIKA_VIEW_RENDERER_GET_CLASS (renderer)->render (renderer, widget);

      renderer->priv->needs_render = FALSE;
    }

  if (renderer->priv->icon_surface)
    {
      gint  scale_factor = gtk_widget_get_scale_factor (widget);
      gint  width;
      gint  height;
      gint  x, y;

      width  = cairo_image_surface_get_width  (renderer->priv->icon_surface);
      height = cairo_image_surface_get_height (renderer->priv->icon_surface);

      width  /= scale_factor;
      height /= scale_factor;

      if (renderer->priv->bg_icon_name)
        {
          if (! renderer->priv->pattern)
            {
              renderer->priv->pattern =
                pika_view_renderer_create_background (renderer, widget);
            }

          cairo_set_source (cr, renderer->priv->pattern);
          cairo_paint (cr);
        }

      x = (available_width  - width)  / 2;
      y = (available_height - height) / 2;

      cairo_set_source_surface (cr, renderer->priv->icon_surface, x, y);
      cairo_rectangle (cr, x, y, width, height);
      cairo_fill (cr);
    }
  else if (renderer->surface)
    {
      cairo_content_t content  = cairo_surface_get_content (renderer->surface);
      gint            width    = renderer->width;
      gint            height   = renderer->height;
      gint            offset_x = (available_width  - width)  / 2;
      gint            offset_y = (available_height - height) / 2;

      cairo_translate (cr, offset_x, offset_y);

      cairo_rectangle (cr, 0, 0, width, height);

      if (content == CAIRO_CONTENT_COLOR_ALPHA)
        {
          if (! renderer->priv->pattern)
            renderer->priv->pattern =
              pika_cairo_checkerboard_create (cr, PIKA_CHECK_SIZE_SM,
                                              pika_render_check_color1 (),
                                              pika_render_check_color2 ());

          cairo_set_source (cr, renderer->priv->pattern);
          cairo_fill_preserve (cr);
        }

      cairo_set_source_surface (cr, renderer->surface, 0, 0);
      cairo_fill (cr);

      cairo_translate (cr, - offset_x, - offset_y);
    }
}

static void
pika_view_renderer_real_render (PikaViewRenderer *renderer,
                                GtkWidget        *widget)
{
  GdkPixbuf   *pixbuf;
  PikaTempBuf *temp_buf;
  const gchar *icon_name;
  gint         scale_factor = gtk_widget_get_scale_factor (widget);

  pixbuf = pika_viewable_get_pixbuf (renderer->viewable,
                                     renderer->context,
                                     renderer->width  * scale_factor,
                                     renderer->height * scale_factor);
  if (pixbuf)
    {
      pika_view_renderer_render_pixbuf (renderer, widget, pixbuf);
      return;
    }

  temp_buf = pika_viewable_get_preview (renderer->viewable,
                                        renderer->context,
                                        renderer->width,
                                        renderer->height);
  if (temp_buf)
    {
      pika_view_renderer_render_temp_buf_simple (renderer, widget, temp_buf);
      return;
    }

  icon_name = pika_viewable_get_icon_name (renderer->viewable);
  pika_view_renderer_render_icon (renderer, widget, icon_name);
}

static void
pika_view_renderer_size_changed (PikaViewRenderer *renderer,
                                 PikaViewable     *viewable)
{
  if (renderer->size != -1)
    pika_view_renderer_set_size (renderer, renderer->size,
                                 renderer->border_width);

  pika_view_renderer_invalidate (renderer);
}

static void
pika_view_renderer_profile_changed (PikaViewRenderer *renderer,
                                    PikaViewable     *viewable)
{
  pika_view_renderer_free_color_transform (renderer);
}

static void
pika_view_renderer_config_notify (GObject          *config,
                                  const GParamSpec *pspec,
                                  PikaViewRenderer *renderer)
{
  pika_view_renderer_free_color_transform (renderer);
}


/*  protected functions  */

void
pika_view_renderer_render_temp_buf_simple (PikaViewRenderer *renderer,
                                           GtkWidget        *widget,
                                           PikaTempBuf      *temp_buf)
{
  gint temp_buf_x = 0;
  gint temp_buf_y = 0;
  gint temp_buf_width;
  gint temp_buf_height;

  g_return_if_fail (PIKA_IS_VIEW_RENDERER (renderer));
  g_return_if_fail (temp_buf != NULL);

  temp_buf_width  = pika_temp_buf_get_width  (temp_buf);
  temp_buf_height = pika_temp_buf_get_height (temp_buf);

  if (temp_buf_width < renderer->width)
    temp_buf_x = (renderer->width - temp_buf_width)  / 2;

  if (temp_buf_height < renderer->height)
    temp_buf_y = (renderer->height - temp_buf_height) / 2;

  pika_view_renderer_render_temp_buf (renderer, widget, temp_buf,
                                      temp_buf_x, temp_buf_y,
                                      -1,
                                      PIKA_VIEW_BG_CHECKS,
                                      PIKA_VIEW_BG_WHITE);
}

void
pika_view_renderer_render_temp_buf (PikaViewRenderer *renderer,
                                    GtkWidget        *widget,
                                    PikaTempBuf      *temp_buf,
                                    gint              temp_buf_x,
                                    gint              temp_buf_y,
                                    gint              channel,
                                    PikaViewBG        inside_bg,
                                    PikaViewBG        outside_bg)
{
  g_clear_pointer (&renderer->priv->icon_surface, cairo_surface_destroy);

  if (! renderer->surface)
    renderer->surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
                                                    renderer->width,
                                                    renderer->height);

  pika_view_render_temp_buf_to_surface (renderer,
                                        widget,
                                        temp_buf,
                                        temp_buf_x,
                                        temp_buf_y,
                                        channel,
                                        inside_bg,
                                        outside_bg,
                                        renderer->surface,
                                        renderer->width,
                                        renderer->height);
}


void
pika_view_renderer_render_pixbuf (PikaViewRenderer *renderer,
                                  GtkWidget        *widget,
                                  GdkPixbuf        *pixbuf)
{
  PikaColorTransform *transform;
  const Babl         *format;
  gint                scale_factor;

  g_clear_pointer (&renderer->surface, cairo_surface_destroy);

  format = pika_pixbuf_get_format (pixbuf);

  transform = pika_view_renderer_get_color_transform (renderer, widget,
                                                      format, format);

  if (transform)
    {
      GdkPixbuf *new;
      gint       width       = gdk_pixbuf_get_width  (pixbuf);
      gint       height      = gdk_pixbuf_get_height (pixbuf);
      gsize      src_stride  = gdk_pixbuf_get_rowstride (pixbuf);
      guchar    *src         = gdk_pixbuf_get_pixels (pixbuf);
      gsize      dest_stride;
      guchar    *dest;
      gint       i;

      new = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
                            gdk_pixbuf_get_has_alpha (pixbuf),
                            8, width, height);

      dest_stride = gdk_pixbuf_get_rowstride (new);
      dest        = gdk_pixbuf_get_pixels (new);

      for (i = 0; i < height; i++)
        {
          pika_color_transform_process_pixels (transform,
                                               format, src,
                                               format, dest,
                                               width);

          src  += src_stride;
          dest += dest_stride;
        }

      pixbuf = new;
    }
  else
    {
      g_object_ref (pixbuf);
    }

  scale_factor = gtk_widget_get_scale_factor (widget);

  g_clear_pointer (&renderer->priv->icon_surface, cairo_surface_destroy);
  renderer->priv->icon_surface =
    gdk_cairo_surface_create_from_pixbuf (pixbuf, scale_factor, NULL);
  g_object_unref (pixbuf);
}

void
pika_view_renderer_render_icon (PikaViewRenderer *renderer,
                                GtkWidget        *widget,
                                const gchar      *icon_name)
{
  GdkPixbuf *pixbuf;
  gint       scale_factor;
  gint       width;
  gint       height;

  g_return_if_fail (PIKA_IS_VIEW_RENDERER (renderer));
  g_return_if_fail (GTK_IS_WIDGET (widget));
  g_return_if_fail (icon_name != NULL);

  g_clear_pointer (&renderer->priv->icon_surface, cairo_surface_destroy);
  g_clear_pointer (&renderer->surface, cairo_surface_destroy);

  scale_factor = gtk_widget_get_scale_factor (widget);

  pixbuf = pika_widget_load_icon (widget, icon_name,
                                  MIN (renderer->width, renderer->height));
  width  = gdk_pixbuf_get_width (pixbuf);
  height = gdk_pixbuf_get_height (pixbuf);

  if (width  > renderer->width  * scale_factor ||
      height > renderer->height * scale_factor)
    {
      GdkPixbuf *scaled_pixbuf;

      pika_viewable_calc_preview_size (width, height,
                                       renderer->width  * scale_factor,
                                       renderer->height * scale_factor,
                                       TRUE, 1.0, 1.0,
                                       &width, &height,
                                       NULL);

      scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
                                               width  * scale_factor,
                                               height * scale_factor,
                                               GDK_INTERP_BILINEAR);

      g_object_unref (pixbuf);
      pixbuf = scaled_pixbuf;
    }

  g_clear_pointer (&renderer->priv->icon_surface, cairo_surface_destroy);
  renderer->priv->icon_surface =
    gdk_cairo_surface_create_from_pixbuf (pixbuf, scale_factor, NULL);
  g_object_unref (pixbuf);
}

PikaColorTransform *
pika_view_renderer_get_color_transform (PikaViewRenderer *renderer,
                                        GtkWidget        *widget,
                                        const Babl       *src_format,
                                        const Babl       *dest_format)
{
  PikaColorProfile         *profile;
  PikaColorProfile         *proof_profile     = NULL;
  PikaColorRenderingIntent  simulation_intent =
    PIKA_COLOR_RENDERING_INTENT_RELATIVE_COLORIMETRIC;
  gboolean                  simulation_bpc    = FALSE;
  PikaImage                *image;

  g_return_val_if_fail (PIKA_IS_VIEW_RENDERER (renderer), NULL);
  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
  g_return_val_if_fail (src_format != NULL, NULL);
  g_return_val_if_fail (dest_format != NULL, NULL);

  if (renderer->priv->profile_transform)
    return renderer->priv->profile_transform;

  if (! renderer->priv->color_config)
    {
      g_printerr ("EEK\n");
      return NULL;
    }

  if (PIKA_IS_COLOR_MANAGED (renderer->viewable))
    {
      PikaColorManaged *managed = PIKA_COLOR_MANAGED (renderer->viewable);

      profile = pika_color_managed_get_color_profile (managed);
    }
  else
    {
      static PikaColorProfile *srgb_profile = NULL;

      if (G_UNLIKELY (! srgb_profile))
        srgb_profile = pika_color_profile_new_rgb_srgb ();

      profile = srgb_profile;
    }

  if (renderer->context)
    {
      image = pika_context_get_image (PIKA_CONTEXT (renderer->context));
      if (image)
        {
          proof_profile =
            pika_color_managed_get_simulation_profile (PIKA_COLOR_MANAGED (image));
          simulation_intent =
            pika_color_managed_get_simulation_intent (PIKA_COLOR_MANAGED (image));
          simulation_bpc =
            pika_color_managed_get_simulation_bpc (PIKA_COLOR_MANAGED (image));
        }
    }

  renderer->priv->profile_transform =
    pika_widget_get_color_transform (widget,
                                     renderer->priv->color_config,
                                     profile,
                                     src_format,
                                     dest_format,
                                     proof_profile,
                                     simulation_intent,
                                     simulation_bpc);

  return renderer->priv->profile_transform;
}

void
pika_view_renderer_free_color_transform (PikaViewRenderer *renderer)
{
  g_return_if_fail (PIKA_IS_VIEW_RENDERER (renderer));

  g_clear_object (&renderer->priv->profile_transform);

  pika_view_renderer_invalidate (renderer);
}

/*  private functions  */

static void
pika_view_render_temp_buf_to_surface (PikaViewRenderer *renderer,
                                      GtkWidget        *widget,
                                      PikaTempBuf      *temp_buf,
                                      gint              temp_buf_x,
                                      gint              temp_buf_y,
                                      gint              channel,
                                      PikaViewBG        inside_bg,
                                      PikaViewBG        outside_bg,
                                      cairo_surface_t  *surface,
                                      gint              surface_width,
                                      gint              surface_height)
{
  cairo_t    *cr;
  gint        x, y;
  gint        width, height;
  const Babl *temp_buf_format;
  gint        temp_buf_width;
  gint        temp_buf_height;

  g_return_if_fail (temp_buf != NULL);
  g_return_if_fail (surface != NULL);

  temp_buf_format = pika_temp_buf_get_format (temp_buf);
  temp_buf_width  = pika_temp_buf_get_width  (temp_buf);
  temp_buf_height = pika_temp_buf_get_height (temp_buf);

  /*  Here are the different cases this functions handles correctly:
   *  1)  Offset temp_buf which does not necessarily cover full image area
   *  2)  Color conversion of temp_buf if it is gray and image is color
   *  3)  Background check buffer for transparent temp_bufs
   *  4)  Using the optional "channel" argument, one channel can be extracted
   *      from a multi-channel temp_buf and composited as a grayscale
   *  Prereqs:
   *  1)  Grayscale temp_bufs have bytes == {1, 2}
   *  2)  Color temp_bufs have bytes == {3, 4}
   *  3)  If image is gray, then temp_buf should have bytes == {1, 2}
   */

  cr = cairo_create (surface);

  if (outside_bg == PIKA_VIEW_BG_CHECKS ||
      inside_bg  == PIKA_VIEW_BG_CHECKS)
    {
      if (! renderer->priv->pattern)
        renderer->priv->pattern =
          pika_cairo_checkerboard_create (cr, PIKA_CHECK_SIZE_SM,
                                          pika_render_check_color1 (),
                                          pika_render_check_color2 ());
    }

  switch (outside_bg)
    {
    case PIKA_VIEW_BG_CHECKS:
      cairo_set_source (cr, renderer->priv->pattern);
      break;

    case PIKA_VIEW_BG_WHITE:
      cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
      break;
    }

  cairo_paint (cr);

  if (! pika_rectangle_intersect (0, 0,
                                  surface_width, surface_height,
                                  temp_buf_x, temp_buf_y,
                                  temp_buf_width, temp_buf_height,
                                  &x, &y,
                                  &width, &height))
    {
      cairo_destroy (cr);
      return;
    }

  if (inside_bg != outside_bg &&
      babl_format_has_alpha (temp_buf_format) && channel == -1)
    {
      cairo_rectangle (cr, x, y, width, height);

      switch (inside_bg)
        {
        case PIKA_VIEW_BG_CHECKS:
          cairo_set_source (cr, renderer->priv->pattern);
          break;

        case PIKA_VIEW_BG_WHITE:
          cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
          break;
        }

      cairo_fill (cr);
    }

  if (babl_format_has_alpha (temp_buf_format) && channel == -1)
    {
      PikaColorTransform *transform;
      GeglBuffer         *src_buffer;
      GeglBuffer         *dest_buffer;
      cairo_surface_t    *alpha_surface;

      alpha_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
                                                  width, height);

      src_buffer  = pika_temp_buf_create_buffer (temp_buf);
      dest_buffer = pika_cairo_surface_create_buffer (alpha_surface);

      transform =
        pika_view_renderer_get_color_transform (renderer, widget,
                                                gegl_buffer_get_format (src_buffer),
                                                gegl_buffer_get_format (dest_buffer));

      if (transform)
        {
          pika_color_transform_process_buffer (transform,
                                               src_buffer,
                                               GEGL_RECTANGLE (x - temp_buf_x,
                                                               y - temp_buf_y,
                                                               width, height),
                                               dest_buffer,
                                               GEGL_RECTANGLE (0, 0, 0, 0));
        }
      else
        {
          pika_gegl_buffer_copy (src_buffer,
                                 GEGL_RECTANGLE (x - temp_buf_x,
                                                 y - temp_buf_y,
                                                 width, height),
                                 GEGL_ABYSS_NONE,
                                 dest_buffer,
                                 GEGL_RECTANGLE (0, 0, 0, 0));
        }

      g_object_unref (src_buffer);
      g_object_unref (dest_buffer);

      cairo_surface_mark_dirty (alpha_surface);

      cairo_translate (cr, x, y);
      cairo_rectangle (cr, 0, 0, width, height);
      cairo_set_source_surface (cr, alpha_surface, 0, 0);
      cairo_fill (cr);

      cairo_surface_destroy (alpha_surface);
    }
  else if (channel == -1)
    {
      PikaColorTransform *transform;
      GeglBuffer         *src_buffer;
      GeglBuffer         *dest_buffer;

      cairo_surface_flush (surface);

      src_buffer  = pika_temp_buf_create_buffer (temp_buf);
      dest_buffer = pika_cairo_surface_create_buffer (surface);

      transform =
        pika_view_renderer_get_color_transform (renderer, widget,
                                                gegl_buffer_get_format (src_buffer),
                                                gegl_buffer_get_format (dest_buffer));

      if (transform)
        {
          pika_color_transform_process_buffer (transform,
                                               src_buffer,
                                               GEGL_RECTANGLE (x - temp_buf_x,
                                                               y - temp_buf_y,
                                                               width, height),
                                               dest_buffer,
                                               GEGL_RECTANGLE (x, y, 0, 0));
        }
      else
        {
          pika_gegl_buffer_copy (src_buffer,
                                 GEGL_RECTANGLE (x - temp_buf_x,
                                                 y - temp_buf_y,
                                                 width, height),
                                 GEGL_ABYSS_NONE,
                                 dest_buffer,
                                 GEGL_RECTANGLE (x, y, 0, 0));
        }

      g_object_unref (src_buffer);
      g_object_unref (dest_buffer);

      cairo_surface_mark_dirty (surface);
    }
  else
    {
      const Babl   *fish;
      const guchar *src;
      guchar       *dest;
      gint          dest_stride;
      gint          bytes;
      gint          rowstride;
      gint          i;

      cairo_surface_flush (surface);

      bytes     = babl_format_get_bytes_per_pixel (temp_buf_format);
      rowstride = temp_buf_width * bytes;

      src = pika_temp_buf_get_data (temp_buf) + ((y - temp_buf_y) * rowstride +
                                                 (x - temp_buf_x) * bytes);

      dest        = cairo_image_surface_get_data (surface);
      dest_stride = cairo_image_surface_get_stride (surface);

      dest += y * dest_stride + x * 4;

      fish = babl_fish (temp_buf_format,
                        babl_format ("cairo-RGB24"));

      for (i = y; i < (y + height); i++)
        {
          const guchar *s = src;
          guchar       *d = dest;
          gint          j;

          for (j = x; j < (x + width); j++, d += 4, s += bytes)
            {
              if (bytes > 2)
                {
                  guchar pixel[4] = { s[channel], s[channel], s[channel], 255 };

                  babl_process (fish, pixel, d, 1);
                }
              else
                {
                  guchar pixel[2] = { s[channel], 255 };

                  babl_process (fish, pixel, d, 1);
                }
            }

          src += rowstride;
          dest += dest_stride;
        }

      cairo_surface_mark_dirty (surface);
    }

  cairo_destroy (cr);
}

/* This function creates a background pattern from a named icon
 * if renderer->priv->bg_icon_name is set.
 */
static cairo_pattern_t *
pika_view_renderer_create_background (PikaViewRenderer *renderer,
                                      GtkWidget        *widget)
{
  cairo_pattern_t *pattern = NULL;

  if (renderer->priv->bg_icon_name)
    {
      cairo_surface_t *surface;
      GdkPixbuf       *pixbuf;

      pixbuf = pika_widget_load_icon (widget,
                                      renderer->priv->bg_icon_name,
                                      64);
      surface = pika_cairo_surface_create_from_pixbuf (pixbuf);
      g_object_unref (pixbuf);

      pattern = cairo_pattern_create_for_surface (surface);
      cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);

      cairo_surface_destroy (surface);
    }

  return pattern;
}
