2006-12-09 13:33:38 -08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
1997-11-24 14:05:25 -08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
|
*
|
2009-01-17 14:28:01 -08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
1997-11-24 14:05:25 -08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-17 14:28:01 -08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
1997-11-24 14:05:25 -08:00
|
|
|
* (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
|
2018-07-11 14:47:19 -07:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
1997-11-24 14:05:25 -08:00
|
|
|
*/
|
2000-12-16 13:37:03 -08:00
|
|
|
|
app/appenv.h New file. Includes <math.h>. Move G_PI, RINT(), ROUND() etc
1999-09-01 Tor Lillqvist <tml@iki.fi>
* app/appenv.h
* libgimp/gimpmath.h: New file. Includes <math.h>. Move G_PI,
RINT(), ROUND() etc from app/appenv.h here, so plug-ins can
use them, too. Remove some commented-out old stuff in appenv.h.
* libgimp/gimp.h: Include gimpmath.h.
* libgimp/gimp.c (gimp_main): Win32: Don't install signal
handlers, we can't do anything useful in the handler ourselves
anyway (it would be nice to print out a backtrace, but that seems
pretty hard to do, even if not impossible). Let Windows inform the
user about the crash. If the plug-in was compiled with MSVC, and
the user also has it, she is offered a chance to start the
debugger automatically anyway.
* app/*several*.c: Include gimpmath.h for G_PI etc. Don't include
<math.h>, as gimpmath.h includes it.
* plug-ins/*/*many*.c: Include config.h. Don't include <math.h>.
Remove all the duplicated definitions of G_PI and rint(). Use
RINT() instead of rint().
* app/app_procs.[ch]: app_exit() takes a gboolean.
* app/batch.c
* app/commands.c
* app/interface.c: Call app_exit() with FALSE or TRUE.
* app/main.c (on_error): Call gimp_fatal_error. (main): Don't
install any signal handler on Win32 here, either.
* app/errors.c (gimp_fatal_error, gimp_terminate): Win32: Format
the message and call MessageBox with it. g_on_error_query doesn't
do anything useful on Win32, and printf'ing a message to stdout or
stderr doesn't do anything, either, in a windowing application.
1999-09-01 13:30:56 -07:00
|
|
|
#include "config.h"
|
|
|
|
|
|
2011-04-28 06:50:39 -07:00
|
|
|
#include <cairo.h>
|
2008-10-09 13:24:04 -07:00
|
|
|
#include <gegl.h>
|
2012-05-02 18:36:22 -07:00
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
1999-09-01 18:41:18 -07:00
|
|
|
|
2018-04-25 10:58:09 -07:00
|
|
|
#include "libgimpmath/gimpmath.h"
|
|
|
|
|
|
2001-11-09 08:54:56 -08:00
|
|
|
#include "core-types.h"
|
2000-12-16 13:37:03 -08:00
|
|
|
|
2012-10-22 07:03:40 -07:00
|
|
|
#include "gegl/gimp-gegl-apply-operation.h"
|
2018-05-24 07:21:21 -07:00
|
|
|
#include "gegl/gimp-gegl-loops.h"
|
2012-03-19 12:53:28 -07:00
|
|
|
#include "gegl/gimp-gegl-utils.h"
|
|
|
|
|
|
2018-04-25 08:12:47 -07:00
|
|
|
#include "operations/layer-modes/gimp-layer-modes.h"
|
|
|
|
|
|
2001-11-09 08:54:56 -08:00
|
|
|
#include "gimp.h"
|
2003-09-03 08:13:19 -07:00
|
|
|
#include "gimpchannel.h"
|
2001-11-09 08:54:56 -08:00
|
|
|
#include "gimpcontext.h"
|
2018-04-13 14:43:27 -07:00
|
|
|
#include "gimpdrawable-gradient.h"
|
2001-11-09 08:54:56 -08:00
|
|
|
#include "gimpgradient.h"
|
|
|
|
|
#include "gimpimage.h"
|
2004-08-10 11:47:21 -07:00
|
|
|
#include "gimpprogress.h"
|
1998-07-26 14:49:42 -07:00
|
|
|
|
2003-03-25 08:38:19 -08:00
|
|
|
#include "gimp-intl.h"
|
2003-02-14 06:14:29 -08:00
|
|
|
|
1997-11-24 14:05:25 -08:00
|
|
|
|
2001-11-09 08:54:56 -08:00
|
|
|
/* public functions */
|
1999-04-08 15:25:54 -07:00
|
|
|
|
2001-03-11 12:01:14 -08:00
|
|
|
void
|
2018-04-13 16:34:50 -07:00
|
|
|
gimp_drawable_gradient (GimpDrawable *drawable,
|
|
|
|
|
GimpContext *context,
|
|
|
|
|
GimpGradient *gradient,
|
|
|
|
|
GeglDistanceMetric metric,
|
|
|
|
|
GimpLayerMode paint_mode,
|
|
|
|
|
GimpGradientType gradient_type,
|
|
|
|
|
gdouble opacity,
|
|
|
|
|
gdouble offset,
|
|
|
|
|
GimpRepeatMode repeat,
|
|
|
|
|
gboolean reverse,
|
|
|
|
|
GimpGradientBlendColorSpace blend_color_space,
|
|
|
|
|
gboolean supersample,
|
|
|
|
|
gint max_depth,
|
|
|
|
|
gdouble threshold,
|
|
|
|
|
gboolean dither,
|
|
|
|
|
gdouble startx,
|
|
|
|
|
gdouble starty,
|
|
|
|
|
gdouble endx,
|
|
|
|
|
gdouble endy,
|
|
|
|
|
GimpProgress *progress)
|
2001-11-09 08:54:56 -08:00
|
|
|
{
|
2014-04-30 00:17:42 -07:00
|
|
|
GimpImage *image;
|
|
|
|
|
GeglBuffer *buffer;
|
2016-02-03 01:44:07 -08:00
|
|
|
GeglBuffer *shapeburst = NULL;
|
|
|
|
|
GeglNode *render;
|
2014-04-30 00:17:42 -07:00
|
|
|
gint x, y, width, height;
|
1997-11-24 14:05:25 -08:00
|
|
|
|
2001-11-09 08:54:56 -08:00
|
|
|
g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
|
2004-11-11 06:05:34 -08:00
|
|
|
g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)));
|
2004-04-14 16:37:34 -07:00
|
|
|
g_return_if_fail (GIMP_IS_CONTEXT (context));
|
2014-04-30 00:17:42 -07:00
|
|
|
g_return_if_fail (GIMP_IS_GRADIENT (gradient));
|
2004-08-10 11:47:21 -07:00
|
|
|
g_return_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress));
|
2001-11-09 08:54:56 -08:00
|
|
|
|
2006-03-28 09:08:36 -08:00
|
|
|
image = gimp_item_get_image (GIMP_ITEM (drawable));
|
2001-11-09 08:54:56 -08:00
|
|
|
|
2010-09-07 12:28:00 -07:00
|
|
|
if (! gimp_item_mask_intersect (GIMP_ITEM (drawable), &x, &y, &width, &height))
|
2004-10-19 15:52:04 -07:00
|
|
|
return;
|
1999-01-10 15:36:29 -08:00
|
|
|
|
2006-03-28 09:08:36 -08:00
|
|
|
gimp_set_busy (image->gimp);
|
1997-11-24 14:05:25 -08:00
|
|
|
|
|
|
|
|
/* Always create an alpha temp buf (for generality) */
|
2015-05-11 19:51:32 -07:00
|
|
|
buffer = gegl_buffer_new (GEGL_RECTANGLE (x, y, width, height),
|
2012-05-20 13:00:27 -07:00
|
|
|
gimp_drawable_get_format_with_alpha (drawable));
|
1997-11-24 14:05:25 -08:00
|
|
|
|
2016-02-03 01:44:07 -08:00
|
|
|
if (gradient_type >= GIMP_GRADIENT_SHAPEBURST_ANGULAR &&
|
|
|
|
|
gradient_type <= GIMP_GRADIENT_SHAPEBURST_DIMPLED)
|
|
|
|
|
{
|
|
|
|
|
shapeburst =
|
2018-04-13 14:43:27 -07:00
|
|
|
gimp_drawable_gradient_shapeburst_distmap (drawable, metric,
|
|
|
|
|
GEGL_RECTANGLE (x, y, width, height),
|
|
|
|
|
progress);
|
2016-02-03 01:44:07 -08:00
|
|
|
}
|
|
|
|
|
|
2018-04-25 10:58:09 -07:00
|
|
|
gimp_drawable_gradient_adjust_coords (drawable,
|
|
|
|
|
gradient_type,
|
|
|
|
|
GEGL_RECTANGLE (x, y, width, height),
|
|
|
|
|
&startx, &starty, &endx, &endy);
|
|
|
|
|
|
2016-02-03 01:44:07 -08:00
|
|
|
render = gegl_node_new_child (NULL,
|
2018-04-13 16:34:50 -07:00
|
|
|
"operation", "gimp:gradient",
|
|
|
|
|
"context", context,
|
|
|
|
|
"gradient", gradient,
|
|
|
|
|
"start-x", startx,
|
|
|
|
|
"start-y", starty,
|
|
|
|
|
"end-x", endx,
|
|
|
|
|
"end-y", endy,
|
|
|
|
|
"gradient-type", gradient_type,
|
|
|
|
|
"gradient-repeat", repeat,
|
|
|
|
|
"offset", offset,
|
|
|
|
|
"gradient-reverse", reverse,
|
|
|
|
|
"gradient-blend-color-space", blend_color_space,
|
|
|
|
|
"supersample", supersample,
|
|
|
|
|
"supersample-depth", max_depth,
|
|
|
|
|
"supersample-threshold", threshold,
|
|
|
|
|
"dither", dither,
|
2016-02-03 01:44:07 -08:00
|
|
|
NULL);
|
|
|
|
|
|
2018-05-25 09:39:08 -07:00
|
|
|
gimp_gegl_apply_operation (shapeburst, progress, C_("undo-type", "Gradient"),
|
2016-02-03 01:44:07 -08:00
|
|
|
render,
|
2018-03-22 10:31:00 -07:00
|
|
|
buffer, GEGL_RECTANGLE (x, y, width, height),
|
|
|
|
|
FALSE);
|
2016-02-03 01:44:07 -08:00
|
|
|
|
|
|
|
|
g_object_unref (render);
|
|
|
|
|
|
|
|
|
|
if (shapeburst)
|
|
|
|
|
g_object_unref (shapeburst);
|
1997-11-24 14:05:25 -08:00
|
|
|
|
2012-03-22 12:01:10 -07:00
|
|
|
gimp_drawable_apply_buffer (drawable, buffer,
|
2015-05-11 19:51:32 -07:00
|
|
|
GEGL_RECTANGLE (x, y, width, height),
|
2018-04-13 14:43:27 -07:00
|
|
|
TRUE, C_("undo-type", "Gradient"),
|
2017-02-12 14:49:26 -08:00
|
|
|
opacity, paint_mode,
|
|
|
|
|
GIMP_LAYER_COLOR_SPACE_AUTO,
|
|
|
|
|
GIMP_LAYER_COLOR_SPACE_AUTO,
|
2018-04-25 08:12:47 -07:00
|
|
|
gimp_layer_mode_get_paint_composite_mode (paint_mode),
|
2012-03-23 16:57:11 -07:00
|
|
|
NULL, x, y);
|
1997-11-24 14:05:25 -08:00
|
|
|
|
2004-10-19 15:52:04 -07:00
|
|
|
gimp_drawable_update (drawable, x, y, width, height);
|
1997-11-24 14:05:25 -08:00
|
|
|
|
2012-03-22 12:01:10 -07:00
|
|
|
g_object_unref (buffer);
|
1999-01-10 15:36:29 -08:00
|
|
|
|
2006-03-28 09:08:36 -08:00
|
|
|
gimp_unset_busy (image->gimp);
|
1997-11-24 14:05:25 -08:00
|
|
|
}
|
|
|
|
|
|
2015-04-26 20:58:15 -07:00
|
|
|
GeglBuffer *
|
2018-04-13 14:43:27 -07:00
|
|
|
gimp_drawable_gradient_shapeburst_distmap (GimpDrawable *drawable,
|
|
|
|
|
GeglDistanceMetric metric,
|
|
|
|
|
const GeglRectangle *region,
|
|
|
|
|
GimpProgress *progress)
|
1997-11-24 14:05:25 -08:00
|
|
|
{
|
2001-01-28 18:45:02 -08:00
|
|
|
GimpChannel *mask;
|
2015-04-26 20:58:15 -07:00
|
|
|
GimpImage *image;
|
2012-03-26 17:47:06 -07:00
|
|
|
GeglBuffer *dist_buffer;
|
2012-03-19 12:53:28 -07:00
|
|
|
GeglBuffer *temp_buffer;
|
2012-03-26 17:39:15 -07:00
|
|
|
GeglNode *shapeburst;
|
1997-11-24 14:05:25 -08:00
|
|
|
|
2015-04-26 20:58:15 -07:00
|
|
|
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 (progress == NULL || GIMP_IS_PROGRESS (progress), NULL);
|
|
|
|
|
|
|
|
|
|
image = gimp_item_get_image (GIMP_ITEM (drawable));
|
|
|
|
|
|
1997-11-24 14:05:25 -08:00
|
|
|
/* allocate the distance map */
|
2015-05-11 18:41:16 -07:00
|
|
|
dist_buffer = gegl_buffer_new (region, babl_format ("Y float"));
|
1997-11-24 14:05:25 -08:00
|
|
|
|
2016-02-03 01:44:07 -08:00
|
|
|
/* allocate the selection mask copy */
|
2015-05-11 18:41:16 -07:00
|
|
|
temp_buffer = gegl_buffer_new (region, babl_format ("Y float"));
|
1997-11-24 14:05:25 -08:00
|
|
|
|
2006-03-28 09:08:36 -08:00
|
|
|
mask = gimp_image_get_mask (image);
|
2003-09-03 08:13:19 -07:00
|
|
|
|
2006-03-28 09:08:36 -08:00
|
|
|
/* If the image mask is not empty, use it as the shape burst source */
|
2003-09-03 08:13:19 -07:00
|
|
|
if (! gimp_channel_is_empty (mask))
|
1997-11-24 14:05:25 -08:00
|
|
|
{
|
2012-04-22 10:38:12 -07:00
|
|
|
gint x, y, width, height;
|
|
|
|
|
gint off_x, off_y;
|
1997-11-24 14:05:25 -08:00
|
|
|
|
2010-09-07 12:28:00 -07:00
|
|
|
gimp_item_mask_intersect (GIMP_ITEM (drawable), &x, &y, &width, &height);
|
2010-09-07 08:43:39 -07:00
|
|
|
gimp_item_get_offset (GIMP_ITEM (drawable), &off_x, &off_y);
|
1997-11-24 14:05:25 -08:00
|
|
|
|
|
|
|
|
/* copy the mask to the temp mask */
|
2018-05-24 07:21:21 -07:00
|
|
|
gimp_gegl_buffer_copy (
|
|
|
|
|
gimp_drawable_get_buffer (GIMP_DRAWABLE (mask)),
|
|
|
|
|
GEGL_RECTANGLE (x + off_x, y + off_y, width, height),
|
|
|
|
|
GEGL_ABYSS_NONE, temp_buffer, region);
|
1997-11-24 14:05:25 -08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* If the intended drawable has an alpha channel, use that */
|
2001-01-14 13:11:52 -08:00
|
|
|
if (gimp_drawable_has_alpha (drawable))
|
2006-04-12 05:49:29 -07:00
|
|
|
{
|
2012-04-22 10:38:12 -07:00
|
|
|
const Babl *component_format;
|
|
|
|
|
|
2014-05-01 11:32:26 -07:00
|
|
|
component_format = babl_format ("A float");
|
2012-04-22 10:38:12 -07:00
|
|
|
|
2012-03-19 12:53:28 -07:00
|
|
|
/* extract the aplha into the temp mask */
|
2012-04-22 10:38:12 -07:00
|
|
|
gegl_buffer_set_format (temp_buffer, component_format);
|
2018-05-24 07:21:21 -07:00
|
|
|
gimp_gegl_buffer_copy (gimp_drawable_get_buffer (drawable), region,
|
|
|
|
|
GEGL_ABYSS_NONE,
|
|
|
|
|
temp_buffer, region);
|
2012-03-26 15:48:20 -07:00
|
|
|
gegl_buffer_set_format (temp_buffer, NULL);
|
2006-04-12 05:49:29 -07:00
|
|
|
}
|
1997-11-24 14:05:25 -08:00
|
|
|
else
|
2006-04-12 05:49:29 -07:00
|
|
|
{
|
2012-03-19 12:53:28 -07:00
|
|
|
GeglColor *white = gegl_color_new ("white");
|
|
|
|
|
|
2006-04-12 05:49:29 -07:00
|
|
|
/* Otherwise, just fill the shapeburst to white */
|
2012-03-19 12:53:28 -07:00
|
|
|
gegl_buffer_set_color (temp_buffer, NULL, white);
|
|
|
|
|
g_object_unref (white);
|
2006-04-12 05:49:29 -07:00
|
|
|
}
|
1997-11-24 14:05:25 -08:00
|
|
|
}
|
|
|
|
|
|
2018-03-14 07:27:15 -07:00
|
|
|
shapeburst = gegl_node_new_child (NULL,
|
|
|
|
|
"operation", "gegl:distance-transform",
|
|
|
|
|
"normalize", TRUE,
|
2018-03-14 11:54:10 -07:00
|
|
|
"metric", metric,
|
2018-03-14 07:27:15 -07:00
|
|
|
NULL);
|
2012-03-26 15:48:20 -07:00
|
|
|
|
2015-04-26 20:58:15 -07:00
|
|
|
if (progress)
|
2016-05-11 13:50:05 -07:00
|
|
|
gimp_gegl_progress_connect (shapeburst, progress,
|
|
|
|
|
_("Calculating distance map"));
|
2012-03-27 08:07:42 -07:00
|
|
|
|
2012-10-22 07:03:40 -07:00
|
|
|
gimp_gegl_apply_operation (temp_buffer, NULL, NULL,
|
|
|
|
|
shapeburst,
|
2018-03-22 10:31:00 -07:00
|
|
|
dist_buffer, region, FALSE);
|
2012-03-26 15:48:20 -07:00
|
|
|
|
2012-03-26 17:39:15 -07:00
|
|
|
g_object_unref (shapeburst);
|
2012-03-19 12:53:28 -07:00
|
|
|
|
2012-03-26 15:48:20 -07:00
|
|
|
g_object_unref (temp_buffer);
|
1997-11-24 14:05:25 -08:00
|
|
|
|
2012-03-26 17:47:06 -07:00
|
|
|
return dist_buffer;
|
1997-11-24 14:05:25 -08:00
|
|
|
}
|
2018-04-25 10:58:09 -07:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
gimp_drawable_gradient_adjust_coords (GimpDrawable *drawable,
|
|
|
|
|
GimpGradientType gradient_type,
|
|
|
|
|
const GeglRectangle *region,
|
|
|
|
|
gdouble *startx,
|
|
|
|
|
gdouble *starty,
|
|
|
|
|
gdouble *endx,
|
|
|
|
|
gdouble *endy)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
|
|
|
|
|
g_return_if_fail (region != NULL);
|
|
|
|
|
g_return_if_fail (startx != NULL);
|
|
|
|
|
g_return_if_fail (starty != NULL);
|
|
|
|
|
g_return_if_fail (endx != NULL);
|
|
|
|
|
g_return_if_fail (endy != NULL);
|
|
|
|
|
|
|
|
|
|
/* we potentially adjust the gradient coordinates according to the gradient
|
|
|
|
|
* type, so that in cases where the gradient span is not related to the
|
|
|
|
|
* segment length, the gradient cache (in GimpOperationGradient) is big
|
|
|
|
|
* enough not to produce banding.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
switch (gradient_type)
|
|
|
|
|
{
|
|
|
|
|
/* for conical gradients, use a segment with the original origin and
|
|
|
|
|
* direction, whose length is the circumference of the largest circle
|
|
|
|
|
* centered at the origin, passing through one of the regions's vertices.
|
|
|
|
|
*/
|
|
|
|
|
case GIMP_GRADIENT_CONICAL_SYMMETRIC:
|
|
|
|
|
case GIMP_GRADIENT_CONICAL_ASYMMETRIC:
|
|
|
|
|
{
|
|
|
|
|
gdouble r = 0.0;
|
|
|
|
|
GimpVector2 v;
|
|
|
|
|
|
|
|
|
|
r = MAX (r, hypot (region->x - *startx,
|
|
|
|
|
region->y - *starty));
|
|
|
|
|
r = MAX (r, hypot (region->x + region->width - *startx,
|
|
|
|
|
region->y - *starty));
|
|
|
|
|
r = MAX (r, hypot (region->x - *startx,
|
|
|
|
|
region->y + region->height - *starty));
|
|
|
|
|
r = MAX (r, hypot (region->x + region->width - *startx,
|
|
|
|
|
region->y + region->height - *starty));
|
|
|
|
|
|
2018-04-25 14:39:10 -07:00
|
|
|
/* symmetric conical gradients only span half a revolution, and
|
|
|
|
|
* therefore require only half the cache size.
|
|
|
|
|
*/
|
|
|
|
|
if (gradient_type == GIMP_GRADIENT_CONICAL_SYMMETRIC)
|
|
|
|
|
r /= 2.0;
|
|
|
|
|
|
2018-04-25 10:58:09 -07:00
|
|
|
gimp_vector2_set (&v, *endx - *startx, *endy - *starty);
|
|
|
|
|
gimp_vector2_normalize (&v);
|
|
|
|
|
gimp_vector2_mul (&v, 2.0 * G_PI * r);
|
|
|
|
|
|
|
|
|
|
*endx = *startx + v.x;
|
|
|
|
|
*endy = *starty + v.y;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* for shaped gradients, only the segment's length matters; use the
|
|
|
|
|
* regions's diagonal, which is the largest possible distance between two
|
|
|
|
|
* points in the region.
|
|
|
|
|
*/
|
|
|
|
|
case GIMP_GRADIENT_SHAPEBURST_ANGULAR:
|
|
|
|
|
case GIMP_GRADIENT_SHAPEBURST_SPHERICAL:
|
|
|
|
|
case GIMP_GRADIENT_SHAPEBURST_DIMPLED:
|
|
|
|
|
*startx = region->x;
|
|
|
|
|
*starty = region->y;
|
|
|
|
|
*endx = region->x + region->width;
|
|
|
|
|
*endy = region->y + region->height;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|