Example #1
0
/**
 * gimp_item_scale_by_factors:
 * @item:     Item to be transformed by explicit width and height factors.
 * @w_factor: scale factor to apply to width and horizontal offset
 * @h_factor: scale factor to apply to height and vertical offset
 * @interpolation:
 * @progress:
 *
 * Scales item dimensions and offsets by uniform width and
 * height factors.
 *
 * Use gimp_item_scale_by_factors() in circumstances when the same
 * width and height scaling factors are to be uniformly applied to a
 * set of items. In this context, the item's dimensions and offsets
 * from the sides of the containing image all change by these
 * predetermined factors. By fiat, the fixed point of the transform is
 * the upper left hand corner of the image. Returns #FALSE if a
 * requested scale factor is zero or if a scaling zero's out a item
 * dimension; returns #TRUE otherwise.
 *
 * Use gimp_item_scale() in circumstances where new item width
 * and height dimensions are predetermined instead.
 *
 * Side effects: Undo set created for item. Old item imagery
 *               scaled & painted to new item tiles.
 *
 * Returns: #TRUE, if the scaled item has positive dimensions
 *          #FALSE if the scaled item has at least one zero dimension
 **/
gboolean
gimp_item_scale_by_factors (GimpItem              *item,
                            gdouble                w_factor,
                            gdouble                h_factor,
                            GimpInterpolationType  interpolation,
                            GimpProgress          *progress)
{
  gint new_width, new_height;
  gint new_offset_x, new_offset_y;

  g_return_val_if_fail (GIMP_IS_ITEM (item), FALSE);
  g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), FALSE);

  if (w_factor == 0.0 || h_factor == 0.0)
    {
      g_warning ("%s: requested width or height scale equals zero", G_STRFUNC);
      return FALSE;
    }

  new_offset_x = ROUND (w_factor * (gdouble) item->offset_x);
  new_offset_y = ROUND (h_factor * (gdouble) item->offset_y);
  new_width    = ROUND (w_factor * (gdouble) gimp_item_width  (item));
  new_height   = ROUND (h_factor * (gdouble) gimp_item_height (item));

  if (new_width != 0 && new_height != 0)
    {
      gimp_item_scale (item,
                       new_width, new_height,
                       new_offset_x, new_offset_y,
                       interpolation, progress);
      return TRUE;
    }

  return FALSE;
}
Example #2
0
/**
 * gimp_item_scale_by_origin:
 * @item:         The item to be transformed by width & height scale factors
 * @new_width:    The width that item will acquire
 * @new_height:   The height that the item will acquire
 * @interpolation:
 * @progress:
 * @local_origin: sets fixed point of the scaling transform. See below.
 *
 * Sets item dimensions to new_width and
 * new_height. Derives vertical and horizontal scaling
 * transforms from new width and height. If local_origin is
 * #TRUE, the fixed point of the scaling transform coincides
 * with the item's center point.  Otherwise, the fixed
 * point is taken to be [-item->offset_x, -item->offset_y].
 *
 * Since this function derives scale factors from new and
 * current item dimensions, these factors will vary from
 * item to item because of aliasing artifacts; factor
 * variations among items can be quite large where item
 * dimensions approach pixel dimensions. Use
 * gimp_item_scale_by_factors() where constant scales are to
 * be uniformly applied to a number of items.
 *
 * Side effects: undo set created for item.
 *               Old item imagery scaled
 *               & painted to new item tiles
 **/
void
gimp_item_scale_by_origin (GimpItem              *item,
                           gint                   new_width,
                           gint                   new_height,
                           GimpInterpolationType  interpolation,
                           GimpProgress          *progress,
                           gboolean               local_origin)
{
  gint new_offset_x, new_offset_y;

  g_return_if_fail (GIMP_IS_ITEM (item));
  g_return_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress));

  if (new_width == 0 || new_height == 0)
    {
      g_warning ("%s: requested width or height equals zero", G_STRFUNC);
      return;
    }

  if (local_origin)
    {
      new_offset_x = (item->offset_x +
                      ((gimp_item_width  (item) - new_width)  / 2.0));
      new_offset_y = (item->offset_y +
                      ((gimp_item_height (item) - new_height) / 2.0));
    }
  else
    {
      new_offset_x = (gint) (((gdouble) new_width *
                              (gdouble) item->offset_x /
                              (gdouble) gimp_item_width (item)));

      new_offset_y = (gint) (((gdouble) new_height *
                              (gdouble) item->offset_y /
                              (gdouble) gimp_item_height (item)));
    }

  gimp_item_scale (item,
                   new_width, new_height,
                   new_offset_x, new_offset_y,
                   interpolation, progress);
}
void
gimp_image_scale (GimpImage             *image,
                  gint                   new_width,
                  gint                   new_height,
                  GimpInterpolationType  interpolation_type,
                  GimpProgress          *progress)
{
    GimpProgress *sub_progress;
    GList        *all_layers;
    GList        *all_channels;
    GList        *all_vectors;
    GList        *list;
    gint          old_width;
    gint          old_height;
    gint          offset_x;
    gint          offset_y;
    gdouble       img_scale_w      = 1.0;
    gdouble       img_scale_h      = 1.0;
    gint          progress_steps;
    gint          progress_current = 0;

    g_return_if_fail (GIMP_IS_IMAGE (image));
    g_return_if_fail (new_width > 0 && new_height > 0);
    g_return_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress));

    gimp_set_busy (image->gimp);

    sub_progress = gimp_sub_progress_new (progress);

    all_layers   = gimp_image_get_layer_list (image);
    all_channels = gimp_image_get_channel_list (image);
    all_vectors  = gimp_image_get_vectors_list (image);

    progress_steps = (g_list_length (all_layers)   +
                      g_list_length (all_channels) +
                      g_list_length (all_vectors)  +
                      1 /* selection */);

    g_object_freeze_notify (G_OBJECT (image));

    gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_SCALE,
                                 C_("undo-type", "Scale Image"));

    old_width   = gimp_image_get_width  (image);
    old_height  = gimp_image_get_height (image);
    img_scale_w = (gdouble) new_width  / (gdouble) old_width;
    img_scale_h = (gdouble) new_height / (gdouble) old_height;

    offset_x = (old_width  - new_width)  / 2;
    offset_y = (old_height - new_height) / 2;

    /*  Push the image size to the stack  */
    gimp_image_undo_push_image_size (image,
                                     NULL,
                                     offset_x,
                                     offset_y,
                                     new_width,
                                     new_height);

    /*  Set the new width and height early, so below image item setters
     *  (esp. guides and sample points) don't choke about moving stuff
     *  out of the image
     */
    g_object_set (image,
                  "width",  new_width,
                  "height", new_height,
                  NULL);

    /*  Scale all channels  */
    for (list = all_channels; list; list = g_list_next (list))
    {
        GimpItem *item = list->data;

        gimp_sub_progress_set_step (GIMP_SUB_PROGRESS (sub_progress),
                                    progress_current++, progress_steps);

        gimp_item_scale (item,
                         new_width, new_height, 0, 0,
                         interpolation_type, sub_progress);
    }

    /*  Scale all vectors  */
    for (list = all_vectors; list; list = g_list_next (list))
    {
        GimpItem *item = list->data;

        gimp_sub_progress_set_step (GIMP_SUB_PROGRESS (sub_progress),
                                    progress_current++, progress_steps);

        gimp_item_scale (item,
                         new_width, new_height, 0, 0,
                         interpolation_type, sub_progress);
    }

    /*  Don't forget the selection mask!  */
    gimp_sub_progress_set_step (GIMP_SUB_PROGRESS (sub_progress),
                                progress_current++, progress_steps);

    gimp_item_scale (GIMP_ITEM (gimp_image_get_mask (image)),
                     new_width, new_height, 0, 0,
                     interpolation_type, sub_progress);

    /*  Scale all layers  */
    for (list = all_layers; list; list = g_list_next (list))
    {
        GimpItem *item = list->data;

        gimp_sub_progress_set_step (GIMP_SUB_PROGRESS (sub_progress),
                                    progress_current++, progress_steps);

        /*  group layers are updated automatically  */
        if (gimp_viewable_get_children (GIMP_VIEWABLE (item)))
        {
            gimp_group_layer_suspend_resize (GIMP_GROUP_LAYER (item), FALSE);
            continue;
        }

        if (! gimp_item_scale_by_factors (item,
                                          img_scale_w, img_scale_h,
                                          interpolation_type, sub_progress))
        {
            /* Since 0 < img_scale_w, img_scale_h, failure due to one or more
             * vanishing scaled layer dimensions. Implicit delete implemented
             * here. Upstream warning implemented in resize_check_layer_scaling(),
             * which offers the user the chance to bail out.
             */
            gimp_image_remove_layer (image, GIMP_LAYER (item), TRUE, NULL);
        }
    }

    for (list = all_layers; list; list = g_list_next (list))
        if (gimp_viewable_get_children (list->data))
            gimp_group_layer_resume_resize (list->data, FALSE);


    /*  Scale all Guides  */
    for (list = gimp_image_get_guides (image);
            list;
            list = g_list_next (list))
    {
        GimpGuide *guide    = list->data;
        gint       position = gimp_guide_get_position (guide);

        switch (gimp_guide_get_orientation (guide))
        {
        case GIMP_ORIENTATION_HORIZONTAL:
            gimp_image_move_guide (image, guide,
                                   (position * new_height) / old_height,
                                   TRUE);
            break;

        case GIMP_ORIENTATION_VERTICAL:
            gimp_image_move_guide (image, guide,
                                   (position * new_width) / old_width,
                                   TRUE);
            break;

        default:
            break;
        }
    }

    /*  Scale all sample points  */
    for (list = gimp_image_get_sample_points (image);
            list;
            list = g_list_next (list))
    {
        GimpSamplePoint *sample_point = list->data;

        gimp_image_move_sample_point (image, sample_point,
                                      sample_point->x * new_width  / old_width,
                                      sample_point->y * new_height / old_height,
                                      TRUE);
    }

    gimp_image_undo_group_end (image);

    g_list_free (all_layers);
    g_list_free (all_channels);
    g_list_free (all_vectors);

    g_object_unref (sub_progress);

    gimp_image_size_changed_detailed (image,
                                      -offset_x,
                                      -offset_y,
                                      old_width,
                                      old_height);

    g_object_thaw_notify (G_OBJECT (image));

    gimp_unset_busy (image->gimp);
}