/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * GimpGroupLayer
 * Copyright (C) 2009  Joseph Miller <josephcmiller2@gmail.com>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>

#include <gegl.h>

#include "libgimpbase/gimpbase.h"
#include "libgimpmath/gimpmath.h"

#include "core-types.h"

#include "base/tile-manager.h"
#include "base/pixel-region.h"

#include "paint-funcs/scale-region.h"

#include "gimpgrouplayer.h"
#include "gimpgrouplayernd.h"
#include "gimpimage.h"
#include "gimpimage-undo-push.h"
#include "gimpdrawablestack.h"
#include "gimppickable.h"
#include "gimpprojectable.h"
#include "gimpprojection.h"
#include "gimpprogress.h"

#include "gimp-intl.h"

static void
gimp_group_layernd_update_scale(GimpGroupLayer *group) {
    GimpLayer     *layer;
    GimpItem      *item;
    GimpDrawable  *drawable;
    TileManager   *new_tiles;
    PixelRegion    srcPR, destPR;
    GimpProgress  *progress;
    gint           off_x, off_y;
    
    layer = GIMP_LAYER (group);
    item = GIMP_ITEM(layer);
    
    gimp_item_get_offset(item, &off_x, &off_y);
        
    // FIXME - what do I do about the GimpProgress?
    progress = gimp_progress_start(NULL, _("LayerND Scaling"), FALSE);
    //gimp_drawable_scalend(item, layer->ndscalex, layer->ndscaley, off_x, off_y, GIMP_INTERPOLATION_CUBIC, progress);
    
    drawable = GIMP_DRAWABLE (item);

    new_tiles = tile_manager_new (layer->ndscalex, layer->ndscaley, drawable->bytes);
    
    pixel_region_init (&srcPR, gimp_drawable_get_tiles (drawable),
                       0, 0,
                       gimp_item_get_width  (item),
                       gimp_item_get_height (item),
                       FALSE);
    pixel_region_init (&destPR, new_tiles,
                       0, 0,
                       layer->ndscalex, layer->ndscaley,
                       TRUE);
                       
    scale_region (&srcPR, &destPR,
                  gimp_drawable_is_indexed (drawable) ?
                  GIMP_INTERPOLATION_NONE : GIMP_INTERPOLATION_CUBIC,
                  NULL,
                  progress);
                  
    gimp_drawable_set_tiles_full (drawable, gimp_item_is_attached (item), NULL,
                                  new_tiles, gimp_drawable_type (drawable),
                                  off_x, off_y);
    tile_manager_unref (new_tiles);
    
    gimp_progress_end(progress);
}

/* public functions */

void
gimp_group_layernd_update (GimpGroupLayer *group)
{
  GimpLayer *layer;
  
  layer = GIMP_LAYER (group);
  
  if ( layer->ndmode & GIMP_GROUP_LAYERND_MODE_SCALE ) {
      gimp_group_layernd_update_scale(group);
  }
}

void
gimp_group_layernd_set_scale (GimpLayer *layer, gint scalex, gint scaley)
{
  layer->ndmode |= GIMP_GROUP_LAYERND_MODE_SCALE;
  layer->ndscalex = scalex;
  layer->ndscaley = scaley;
}
