/* 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 "pika.h"
#include "pikadrawable.h"
#include "pikaimage.h"
#include "pikaimage-color-profile.h"
#include "pikaimage-colormap.h"
#include "pikaimage-convert-type.h"
#include "pikaimage-undo.h"
#include "pikaimage-undo-push.h"
#include "pikaobjectqueue.h"
#include "pikaprogress.h"

#include "pika-intl.h"


gboolean
pika_image_convert_type (PikaImage          *image,
                         PikaImageBaseType   new_type,
                         PikaColorProfile   *dest_profile,
                         PikaProgress       *progress,
                         GError            **error)
{
  PikaColorProfile  *src_profile;
  PikaImageBaseType  old_type;
  const Babl        *new_layer_format;
  PikaObjectQueue   *queue;
  GList             *all_layers;
  PikaDrawable      *drawable;
  const gchar       *undo_desc = NULL;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), FALSE);
  g_return_val_if_fail (new_type != pika_image_get_base_type (image), FALSE);
  g_return_val_if_fail (new_type != PIKA_INDEXED, FALSE);
  g_return_val_if_fail (pika_babl_is_valid (new_type,
                                            pika_image_get_precision (image)),
                        FALSE);
  g_return_val_if_fail (dest_profile == NULL || PIKA_IS_COLOR_PROFILE (dest_profile),
                        FALSE);
  g_return_val_if_fail (progress == NULL || PIKA_IS_PROGRESS (progress), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  new_layer_format = pika_babl_format (new_type,
                                       pika_image_get_precision (image),
                                       TRUE,
                                       pika_image_get_layer_space (image));

  if (dest_profile &&
      ! pika_image_validate_color_profile_by_format (new_layer_format,
                                                     dest_profile,
                                                     NULL, error))
    {
      return FALSE;
    }

  switch (new_type)
    {
    case PIKA_RGB:
      undo_desc = C_("undo-type", "Convert Image to RGB");
      break;

    case PIKA_GRAY:
      undo_desc = C_("undo-type", "Convert Image to Grayscale");
      break;

    default:
      g_return_val_if_reached (FALSE);
    }

  pika_set_busy (image->pika);

  queue    = pika_object_queue_new (progress);
  progress = PIKA_PROGRESS (queue);

  all_layers = pika_image_get_layer_list (image);
  pika_object_queue_push_list (queue, all_layers);
  g_list_free (all_layers);

  g_object_freeze_notify (G_OBJECT (image));

  pika_image_undo_group_start (image, PIKA_UNDO_GROUP_IMAGE_CONVERT,
                               undo_desc);

  src_profile = pika_color_managed_get_color_profile (PIKA_COLOR_MANAGED (image));

  /*  Push the image type to the stack  */
  pika_image_undo_push_image_type (image, NULL);

  /*  Set the new base type  */
  old_type = pika_image_get_base_type (image);

  g_object_set (image, "base-type", new_type, NULL);

  /*  When converting to/from GRAY, always convert to the new type's
   *  builtin profile as a fallback, we need one for convert_type on
   *  the same image
   */
  if (old_type == PIKA_GRAY ||
      new_type == PIKA_GRAY)
    {
      if (! dest_profile)
        dest_profile = pika_image_get_builtin_color_profile (image);
    }

  while ((drawable = pika_object_queue_pop (queue)))
    {
      pika_drawable_convert_type (drawable, image,
                                  new_type,
                                  pika_drawable_get_precision (drawable),
                                  pika_drawable_has_alpha (drawable),
                                  src_profile,
                                  dest_profile,
                                  GEGL_DITHER_NONE, GEGL_DITHER_NONE,
                                  TRUE, progress);
    }

  if (old_type == PIKA_INDEXED)
    pika_image_unset_colormap (image, TRUE);

  /*  When converting to/from GRAY, set the new profile.
   */
  if (old_type == PIKA_GRAY ||
      new_type == PIKA_GRAY)
    {
      pika_image_set_color_profile (image, dest_profile, NULL);
    }

  pika_image_undo_group_end (image);

  pika_image_mode_changed (image);
  g_object_thaw_notify (G_OBJECT (image));

  g_object_unref (queue);

  pika_unset_busy (image->pika);

  return TRUE;
}
