/* 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 "libpikacolor/pikacolor.h"

#include "core-types.h"

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

#include "pikaimage.h"
#include "pikaimage-color-profile.h"
#include "pikaimage-preview.h"
#include "pikapickable.h"
#include "pikaprojectable.h"
#include "pikaprojection.h"
#include "pikatempbuf.h"


const Babl *
pika_image_get_preview_format (PikaImage *image)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);

  switch (pika_image_get_base_type (image))
    {
    case PIKA_RGB:
    case PIKA_GRAY:
      return pika_babl_format_change_component_type (
        pika_projectable_get_format (PIKA_PROJECTABLE (image)),
        PIKA_COMPONENT_TYPE_U8);

    case PIKA_INDEXED:
      return babl_format ("R'G'B'A u8");
    }

  g_return_val_if_reached (NULL);
}

void
pika_image_get_preview_size (PikaViewable *viewable,
                             gint          size,
                             gboolean      is_popup,
                             gboolean      dot_for_dot,
                             gint         *width,
                             gint         *height)
{
  PikaImage *image = PIKA_IMAGE (viewable);
  gdouble    xres;
  gdouble    yres;

  pika_image_get_resolution (image, &xres, &yres);

  pika_viewable_calc_preview_size (pika_image_get_width  (image),
                                   pika_image_get_height (image),
                                   size,
                                   size,
                                   dot_for_dot,
                                   xres,
                                   yres,
                                   width,
                                   height,
                                   NULL);
}

gboolean
pika_image_get_popup_size (PikaViewable *viewable,
                           gint          width,
                           gint          height,
                           gboolean      dot_for_dot,
                           gint         *popup_width,
                           gint         *popup_height)
{
  PikaImage *image = PIKA_IMAGE (viewable);

  if (pika_image_get_width  (image) > width ||
      pika_image_get_height (image) > height)
    {
      gboolean scaling_up;

      pika_viewable_calc_preview_size (pika_image_get_width  (image),
                                       pika_image_get_height (image),
                                       width  * 2,
                                       height * 2,
                                       dot_for_dot, 1.0, 1.0,
                                       popup_width,
                                       popup_height,
                                       &scaling_up);

      if (scaling_up)
        {
          *popup_width  = pika_image_get_width  (image);
          *popup_height = pika_image_get_height (image);
        }

      return TRUE;
    }

  return FALSE;
}

PikaTempBuf *
pika_image_get_new_preview (PikaViewable *viewable,
                            PikaContext  *context,
                            gint          width,
                            gint          height)
{
  PikaImage   *image = PIKA_IMAGE (viewable);
  const Babl  *format;
  PikaTempBuf *buf;
  gdouble      scale_x;
  gdouble      scale_y;

  scale_x = (gdouble) width  / (gdouble) pika_image_get_width  (image);
  scale_y = (gdouble) height / (gdouble) pika_image_get_height (image);

  format = pika_image_get_preview_format (image);

  buf = pika_temp_buf_new (width, height, format);

  gegl_buffer_get (pika_pickable_get_buffer (PIKA_PICKABLE (image)),
                   GEGL_RECTANGLE (0, 0, width, height),
                   MIN (scale_x, scale_y),
                   pika_temp_buf_get_format (buf),
                   pika_temp_buf_get_data (buf),
                   GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_CLAMP);

  return buf;
}

GdkPixbuf *
pika_image_get_new_pixbuf (PikaViewable *viewable,
                           PikaContext  *context,
                           gint          width,
                           gint          height)
{
  PikaImage          *image = PIKA_IMAGE (viewable);
  GdkPixbuf          *pixbuf;
  gdouble             scale_x;
  gdouble             scale_y;
  PikaColorTransform *transform;

  scale_x = (gdouble) width  / (gdouble) pika_image_get_width  (image);
  scale_y = (gdouble) height / (gdouble) pika_image_get_height (image);

  pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
                           width, height);

  transform = pika_image_get_color_transform_to_srgb_u8 (image);

  if (transform)
    {
      PikaTempBuf *temp_buf;
      GeglBuffer  *src_buf;
      GeglBuffer  *dest_buf;

      temp_buf = pika_temp_buf_new (width, height,
                                    pika_pickable_get_format (PIKA_PICKABLE (image)));

      gegl_buffer_get (pika_pickable_get_buffer (PIKA_PICKABLE (image)),
                       GEGL_RECTANGLE (0, 0, width, height),
                       MIN (scale_x, scale_y),
                       pika_temp_buf_get_format (temp_buf),
                       pika_temp_buf_get_data (temp_buf),
                       GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_CLAMP);

      src_buf  = pika_temp_buf_create_buffer (temp_buf);
      dest_buf = pika_pixbuf_create_buffer (pixbuf);

      pika_temp_buf_unref (temp_buf);

      pika_color_transform_process_buffer (transform,
                                           src_buf,
                                           GEGL_RECTANGLE (0, 0,
                                                           width, height),
                                           dest_buf,
                                           GEGL_RECTANGLE (0, 0, 0, 0));

      g_object_unref (src_buf);
      g_object_unref (dest_buf);
    }
  else
    {
      gegl_buffer_get (pika_pickable_get_buffer (PIKA_PICKABLE (image)),
                       GEGL_RECTANGLE (0, 0, width, height),
                       MIN (scale_x, scale_y),
                       pika_pixbuf_get_format (pixbuf),
                       gdk_pixbuf_get_pixels (pixbuf),
                       gdk_pixbuf_get_rowstride (pixbuf),
                       GEGL_ABYSS_CLAMP);
    }

  return pixbuf;
}
