GimpDrawable *
gimp_drawable_transform_flip (GimpDrawable        *drawable,
                              GimpContext         *context,
                              GimpOrientationType  flip_type,
                              gdouble              axis,
                              gboolean             clip_result)
{
  GimpImage    *image;
  GeglBuffer   *orig_buffer;
  gint          orig_offset_x;
  gint          orig_offset_y;
  gboolean      new_layer;
  GimpDrawable *result = NULL;

  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), NULL);
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);

  image = gimp_item_get_image (GIMP_ITEM (drawable));

  /* Start a transform undo group */
  gimp_image_undo_group_start (image,
                               GIMP_UNDO_GROUP_TRANSFORM,
                               C_("undo-type", "Flip"));

  /* Cut/Copy from the specified drawable */
  orig_buffer = gimp_drawable_transform_cut (drawable, context,
                                             &orig_offset_x, &orig_offset_y,
                                             &new_layer);

  if (orig_buffer)
    {
      GeglBuffer *new_buffer = NULL;
      gint        new_offset_x;
      gint        new_offset_y;

      /*  always clip unfloated buffers so they keep their size  */
      if (GIMP_IS_CHANNEL (drawable) &&
          ! babl_format_has_alpha (gegl_buffer_get_format (orig_buffer)))
        clip_result = TRUE;

      /*  also transform the mask if we are transforming an entire layer  */
      if (GIMP_IS_LAYER (drawable) &&
          gimp_layer_get_mask (GIMP_LAYER (drawable)) &&
          gimp_channel_is_empty (gimp_image_get_mask (image)))
        {
          GimpLayerMask *mask = gimp_layer_get_mask (GIMP_LAYER (drawable));

          gimp_item_flip (GIMP_ITEM (mask), context,
                          flip_type,
                          axis,
                          clip_result);
        }

      /* transform the buffer */
      if (orig_buffer)
        {
          new_buffer = gimp_drawable_transform_buffer_flip (drawable, context,
                                                            orig_buffer,
                                                            orig_offset_x,
                                                            orig_offset_y,
                                                            flip_type, axis,
                                                            clip_result,
                                                            &new_offset_x,
                                                            &new_offset_y);

          /* Free the cut/copied buffer */
          g_object_unref (orig_buffer);
        }

      if (new_buffer)
        {
          result = gimp_drawable_transform_paste (drawable, new_buffer,
                                                  new_offset_x, new_offset_y,
                                                  new_layer);
          g_object_unref (new_buffer);
        }
    }

  /*  push the undo group end  */
  gimp_image_undo_group_end (image);

  return result;
}
Beispiel #2
0
static GeglBuffer *
gimp_flip_tool_transform (GimpTransformTool *tr_tool,
                          GimpItem          *active_item,
                          GeglBuffer        *orig_buffer,
                          gint               orig_offset_x,
                          gint               orig_offset_y,
                          GimpColorProfile **buffer_profile,
                          gint              *new_offset_x,
                          gint              *new_offset_y)
{
  GimpFlipTool         *flip        = GIMP_FLIP_TOOL (tr_tool);
  GimpFlipOptions      *options     = GIMP_FLIP_TOOL_GET_OPTIONS (tr_tool);
  GimpTransformOptions *tr_options  = GIMP_TRANSFORM_TOOL_GET_OPTIONS (tr_tool);
  GimpContext          *context     = GIMP_CONTEXT (options);
  GimpOrientationType   flip_type   = GIMP_ORIENTATION_UNKNOWN;
  gdouble               axis        = 0.0;
  gboolean              clip_result = FALSE;
  GeglBuffer           *ret         = NULL;

  flip_type = gimp_flip_tool_get_flip_type (flip);

  if (flip->guide)
    {
      axis = gimp_guide_get_position (flip->guide);
    }
  else
    {
      switch (flip_type)
        {
        case GIMP_ORIENTATION_HORIZONTAL:
          axis = ((gdouble) tr_tool->x1 +
                  (gdouble) (tr_tool->x2 - tr_tool->x1) / 2.0);
          break;

        case GIMP_ORIENTATION_VERTICAL:
          axis = ((gdouble) tr_tool->y1 +
                  (gdouble) (tr_tool->y2 - tr_tool->y1) / 2.0);
          break;

        default:
          break;
        }
    }

  switch (tr_options->clip)
    {
    case GIMP_TRANSFORM_RESIZE_ADJUST:
      clip_result = FALSE;
      break;

    case GIMP_TRANSFORM_RESIZE_CLIP:
      clip_result = TRUE;
      break;

    default:
      g_return_val_if_reached (NULL);
    }

  if (orig_buffer)
    {
      /*  this happens when transforming a selection cut out of a
       *  normal drawable, or the selection
       */

      /*  always clip the selection and unfloated channels
       *  so they keep their size
       */
      if (GIMP_IS_CHANNEL (active_item) &&
          ! babl_format_has_alpha (gegl_buffer_get_format (orig_buffer)))
        clip_result = TRUE;

      ret = gimp_drawable_transform_buffer_flip (GIMP_DRAWABLE (active_item),
                                                 context,
                                                 orig_buffer,
                                                 orig_offset_x,
                                                 orig_offset_y,
                                                 flip_type, axis,
                                                 clip_result,
                                                 buffer_profile,
                                                 new_offset_x,
                                                 new_offset_y);
    }
  else
    {
      /*  this happens for entire drawables, paths and layer groups  */

      /*  always clip layer masks so they keep their size
       */
      if (GIMP_IS_CHANNEL (active_item))
        clip_result = TRUE;

      if (gimp_item_get_linked (active_item))
        {
          gimp_item_linked_flip (active_item, context,
                                 flip_type, axis, clip_result);
        }
      else
        {
          gimp_item_flip (active_item, context,
                          flip_type, axis, clip_result);
        }
    }

  return ret;
}