/* 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
 *
 * file-import.c
 *
 * 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 <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "core/core-types.h"

#include "config/pikacoreconfig.h"

#include "core/pika.h"
#include "core/pikacontext.h"
#include "core/pikaimage.h"
#include "core/pikaimage-color-profile.h"
#include "core/pikaimage-convert-precision.h"
#include "core/pikaimage-rotate.h"
#include "core/pikalayer.h"
#include "core/pikaprogress.h"

#include "text/pikatextlayer.h"

#include "file-import.h"


/*  public functions  */

void
file_import_image (PikaImage    *image,
                   PikaContext  *context,
                   GFile        *file,
                   gboolean      interactive,
                   PikaProgress *progress)
{
  PikaCoreConfig *config;

  g_return_if_fail (PIKA_IS_IMAGE (image));
  g_return_if_fail (PIKA_IS_CONTEXT (context));
  g_return_if_fail (G_IS_FILE (file));
  g_return_if_fail (progress == NULL || PIKA_IS_PROGRESS (progress));

  config = image->pika->config;

  if (interactive && pika_image_get_base_type (image) != PIKA_INDEXED)
    {
      if (config->import_promote_float)
        {
          PikaPrecision old_precision = pika_image_get_precision (image);

          if (old_precision != PIKA_PRECISION_FLOAT_LINEAR)
            {
              pika_image_convert_precision (image,
                                            PIKA_PRECISION_FLOAT_LINEAR,
                                            GEGL_DITHER_NONE,
                                            GEGL_DITHER_NONE,
                                            GEGL_DITHER_NONE,
                                            progress);

              if (config->import_promote_dither &&
                  old_precision == PIKA_PRECISION_U8_NON_LINEAR)
                {
                  pika_image_convert_dither_u8 (image, progress);
                }
            }
        }

      if (config->import_add_alpha)
        {
          GList *layers = pika_image_get_layer_list (image);
          GList *list;

          for (list = layers; list; list = g_list_next (list))
            {
              if (! pika_viewable_get_children (list->data) &&
                  ! pika_item_is_text_layer (list->data)    &&
                  ! pika_drawable_has_alpha (list->data))
                {
                  pika_layer_add_alpha (list->data);
                }
            }

          g_list_free (layers);
        }
    }

  pika_image_import_color_profile (image, context, progress, interactive);
  pika_image_import_rotation_metadata (image, context, progress, interactive);

  /* Remember the import source */
  pika_image_set_imported_file (image, file);

  /* We shall treat this file as an Untitled file */
  pika_image_set_file (image, NULL);
}
