app: factor file_gbr_brush_to_layer() out of the brush-to-image logic

of file-gbr-load, and add some layer handling magic that doesn't
change a thing for simple brushes, but is needed for loading brush
pipes.
This commit is contained in:
Michael Natterer 2019-02-19 23:13:59 +01:00
parent 7f05ec00cd
commit 3b89ae40d0
2 changed files with 99 additions and 47 deletions

View File

@ -33,6 +33,7 @@
#include "core/gimpdrawable.h" #include "core/gimpdrawable.h"
#include "core/gimpimage.h" #include "core/gimpimage.h"
#include "core/gimplayer-new.h" #include "core/gimplayer-new.h"
#include "core/gimpimage-resize.h"
#include "core/gimpparamspecs.h" #include "core/gimpparamspecs.h"
#include "core/gimptempbuf.h" #include "core/gimptempbuf.h"
@ -154,55 +155,62 @@ file_gbr_save_invoker (GimpProcedure *procedure,
return return_vals; return return_vals;
} }
GimpLayer *
/* private functions */ file_gbr_brush_to_layer (GimpImage *image,
static GimpImage *
file_gbr_brush_to_image (Gimp *gimp,
GimpBrush *brush) GimpBrush *brush)
{ {
GimpImage *image; GimpLayer *layer;
GimpLayer *layer; const Babl *format;
const Babl *format; gboolean alpha;
const gchar *name; gint width;
GimpImageBaseType base_type; gint height;
gboolean alpha; gint image_width;
gint width; gint image_height;
gint height; GimpTempBuf *mask;
GimpTempBuf *mask = gimp_brush_get_mask (brush); GimpTempBuf *pixmap;
GimpTempBuf *pixmap = gimp_brush_get_pixmap (brush); GeglBuffer *buffer;
GeglBuffer *buffer;
GimpParasite *parasite; g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
g_return_val_if_fail (GIMP_IS_BRUSH (brush), NULL);
mask = gimp_brush_get_mask (brush);
pixmap = gimp_brush_get_pixmap (brush);
if (pixmap) if (pixmap)
{ alpha = TRUE;
base_type = GIMP_RGB;
alpha = TRUE;
}
else else
{ alpha = FALSE;
base_type = GIMP_GRAY;
alpha = FALSE;
}
name = gimp_object_get_name (brush);
width = gimp_temp_buf_get_width (mask); width = gimp_temp_buf_get_width (mask);
height = gimp_temp_buf_get_height (mask); height = gimp_temp_buf_get_height (mask);
image = gimp_image_new (gimp, width, height, base_type, image_width = gimp_image_get_width (image);
GIMP_PRECISION_U8_PERCEPTUAL); image_height = gimp_image_get_height (image);
parasite = gimp_parasite_new ("gimp-brush-name", if (width > image_width || height > image_height)
GIMP_PARASITE_PERSISTENT, {
strlen (name) + 1, name); gint new_width = MAX (image_width, width);
gimp_image_parasite_attach (image, parasite); gint new_height = MAX (image_height, height);
gimp_parasite_free (parasite);
gimp_image_resize (image, gimp_get_user_context (image->gimp),
new_width, new_height,
(new_width - image_width) / 2,
(new_height - image_height) / 2,
NULL);
image_width = new_width;
image_height = new_height;
}
format = gimp_image_get_layer_format (image, alpha); format = gimp_image_get_layer_format (image, alpha);
layer = gimp_layer_new (image, width, height, format, name, layer = gimp_layer_new (image, width, height, format,
gimp_object_get_name (brush),
1.0, GIMP_LAYER_MODE_NORMAL); 1.0, GIMP_LAYER_MODE_NORMAL);
gimp_image_add_layer (image, layer, NULL, 0, FALSE);
gimp_item_set_offset (GIMP_ITEM (layer),
(image_width - width) / 2,
(image_height - height) / 2);
buffer = gimp_drawable_get_buffer (GIMP_DRAWABLE (layer)); buffer = gimp_drawable_get_buffer (GIMP_DRAWABLE (layer));
@ -243,6 +251,47 @@ file_gbr_brush_to_image (Gimp *gimp,
mask_data, GEGL_AUTO_ROWSTRIDE); mask_data, GEGL_AUTO_ROWSTRIDE);
} }
return layer;
}
/* private functions */
static GimpImage *
file_gbr_brush_to_image (Gimp *gimp,
GimpBrush *brush)
{
GimpImage *image;
GimpLayer *layer;
const gchar *name;
GimpImageBaseType base_type;
gint width;
gint height;
GimpTempBuf *mask = gimp_brush_get_mask (brush);
GimpTempBuf *pixmap = gimp_brush_get_pixmap (brush);
GimpParasite *parasite;
if (pixmap)
base_type = GIMP_RGB;
else
base_type = GIMP_GRAY;
name = gimp_object_get_name (brush);
width = gimp_temp_buf_get_width (mask);
height = gimp_temp_buf_get_height (mask);
image = gimp_image_new (gimp, width, height, base_type,
GIMP_PRECISION_U8_PERCEPTUAL);
parasite = gimp_parasite_new ("gimp-brush-name",
GIMP_PARASITE_PERSISTENT,
strlen (name) + 1, name);
gimp_image_parasite_attach (image, parasite);
gimp_parasite_free (parasite);
layer = file_gbr_brush_to_layer (image, brush);
gimp_image_add_layer (image, layer, NULL, 0, FALSE);
return image; return image;
} }

View File

@ -19,19 +19,22 @@
#define __FILE_DATA_GBR_H__ #define __FILE_DATA_GBR_H__
GimpValueArray * file_gbr_load_invoker (GimpProcedure *procedure, GimpValueArray * file_gbr_load_invoker (GimpProcedure *procedure,
Gimp *gimp, Gimp *gimp,
GimpContext *context, GimpContext *context,
GimpProgress *progress, GimpProgress *progress,
const GimpValueArray *args, const GimpValueArray *args,
GError **error); GError **error);
GimpValueArray * file_gbr_save_invoker (GimpProcedure *procedure, GimpValueArray * file_gbr_save_invoker (GimpProcedure *procedure,
Gimp *gimp, Gimp *gimp,
GimpContext *context, GimpContext *context,
GimpProgress *progress, GimpProgress *progress,
const GimpValueArray *args, const GimpValueArray *args,
GError **error); GError **error);
GimpLayer * file_gbr_brush_to_layer (GimpImage *image,
GimpBrush *brush);
#endif /* __FILE_DATA_GBR_H__ */ #endif /* __FILE_DATA_GBR_H__ */