gimp/app/paint/gimpheal.c

581 lines
21 KiB
C
Raw Normal View History

/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* 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 "paint-types.h"
#include "base/temp-buf.h"
#include "gegl/gimp-gegl-utils.h"
#include "core/gimpbrush.h"
#include "core/gimpdrawable.h"
#include "core/gimpdynamics.h"
#include "core/gimpdynamicsoutput.h"
#include "core/gimperror.h"
#include "core/gimpimage.h"
#include "core/gimppickable.h"
#include "gimpheal.h"
#include "gimpsourceoptions.h"
#include "gimp-intl.h"
/* NOTES
*
* The method used here is similar to the lighting invariant correctin
* method but slightly different: we do not divide the RGB components,
* but substract them I2 = I0 - I1, where I0 is the sample image to be
* corrected, I1 is the reference pattern. Then we solve DeltaI=0
* (Laplace) with I2 Dirichlet conditions at the borders of the
* mask. The solver is a unoptimized red/black checker Gauss-Siedel
* with an over-relaxation factor of 1.8. It can benefit from a
* multi-grid evaluation of an initial solution before the main
* iteration loop.
Merged the "soc-2006-healing-brush" branch. That branch is now officially 2006-09-02 Michael Natterer <mitch@gimp.org> Merged the "soc-2006-healing-brush" branch. That branch is now officially closed and all further fixes and changes have to be applied to HEAD. Did some minor adjustments, mostly small indentation and spacing fixes. Derive the tool from the newly introduced GimpBrushTool which did not exist when the branch was created. Thanks a lot to Kevin Sookocheff for this nice contribution! * app/paint/paint-enums.[ch]: new enum GimpHealAlignMode. * app/paint/Makefile.am * app/paint/makefile.msc * app/paint/gimpheal.[ch] * app/paint/gimphealoptions.[ch]: the heal core and its options. * app/paint/gimp-paint.c: register the heal core. * app/tools/Makefile.am * app/tools/makefile.msc * app/tools/gimphealtool.[ch]: the heal tool. * app/tools/gimp-tools.c: register the heal tool. * app/tools/gimppaintoptions-gui.c: show the widgets that are used by heal. * app/widgets/gimphelp-ids.h: the heal help ID. * tools/pdbgen/stddefs.pdb * tools/pdbgen/pdb/paint_tools.pdb: the heal PDB wrappers. * app/widgets/widgets-enums.h * app/widgets/gimpcursor.c * cursors/Makefile.am * cursors/makefile.msc * cursors/tool-heal.png * cursors/xbm/tool-heal.xbm * cursors/xbm/tool-heal-mask.xbm: a new cursor for the heal tool. * libgimpwidgets/gimpstock.[ch] * themes/Default/images/Makefile.am * themes/Default/images/makefile.msc * themes/Default/images/tools/stock-tool-heal-16.png * themes/Default/images/tools/stock-tool-heal-22.png: new stock icons for the heal tool. * app/pdb/internal_procs.c * app/pdb/paint_tools_cmds.c * libgimp/gimppainttools_pdb.[ch]: regenerated.
2006-09-03 02:54:35 +08:00
*
* I reduced the convergence criteria to 0.1% (0.001) as we are
* dealing here with RGB integer components, more is overkill.
*
* Jean-Yves Couleaud cjyves@free.fr
Merged the "soc-2006-healing-brush" branch. That branch is now officially 2006-09-02 Michael Natterer <mitch@gimp.org> Merged the "soc-2006-healing-brush" branch. That branch is now officially closed and all further fixes and changes have to be applied to HEAD. Did some minor adjustments, mostly small indentation and spacing fixes. Derive the tool from the newly introduced GimpBrushTool which did not exist when the branch was created. Thanks a lot to Kevin Sookocheff for this nice contribution! * app/paint/paint-enums.[ch]: new enum GimpHealAlignMode. * app/paint/Makefile.am * app/paint/makefile.msc * app/paint/gimpheal.[ch] * app/paint/gimphealoptions.[ch]: the heal core and its options. * app/paint/gimp-paint.c: register the heal core. * app/tools/Makefile.am * app/tools/makefile.msc * app/tools/gimphealtool.[ch]: the heal tool. * app/tools/gimp-tools.c: register the heal tool. * app/tools/gimppaintoptions-gui.c: show the widgets that are used by heal. * app/widgets/gimphelp-ids.h: the heal help ID. * tools/pdbgen/stddefs.pdb * tools/pdbgen/pdb/paint_tools.pdb: the heal PDB wrappers. * app/widgets/widgets-enums.h * app/widgets/gimpcursor.c * cursors/Makefile.am * cursors/makefile.msc * cursors/tool-heal.png * cursors/xbm/tool-heal.xbm * cursors/xbm/tool-heal-mask.xbm: a new cursor for the heal tool. * libgimpwidgets/gimpstock.[ch] * themes/Default/images/Makefile.am * themes/Default/images/makefile.msc * themes/Default/images/tools/stock-tool-heal-16.png * themes/Default/images/tools/stock-tool-heal-22.png: new stock icons for the heal tool. * app/pdb/internal_procs.c * app/pdb/paint_tools_cmds.c * libgimp/gimppainttools_pdb.[ch]: regenerated.
2006-09-03 02:54:35 +08:00
*/
static gboolean gimp_heal_start (GimpPaintCore *paint_core,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
const GimpCoords *coords,
GError **error);
static void gimp_heal_motion (GimpSourceCore *source_core,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
const GimpCoords *coords,
gdouble opacity,
GimpPickable *src_pickable,
GeglBuffer *src_buffer,
GeglRectangle *src_rect,
gint src_offset_x,
gint src_offset_y,
GeglBuffer *paint_buffer,
gint paint_buffer_x,
gint paint_buffer_y,
gint paint_area_offset_x,
gint paint_area_offset_y,
gint paint_area_width,
gint paint_area_height);
G_DEFINE_TYPE (GimpHeal, gimp_heal, GIMP_TYPE_SOURCE_CORE)
#define parent_class gimp_heal_parent_class
Merged the "soc-2006-healing-brush" branch. That branch is now officially 2006-09-02 Michael Natterer <mitch@gimp.org> Merged the "soc-2006-healing-brush" branch. That branch is now officially closed and all further fixes and changes have to be applied to HEAD. Did some minor adjustments, mostly small indentation and spacing fixes. Derive the tool from the newly introduced GimpBrushTool which did not exist when the branch was created. Thanks a lot to Kevin Sookocheff for this nice contribution! * app/paint/paint-enums.[ch]: new enum GimpHealAlignMode. * app/paint/Makefile.am * app/paint/makefile.msc * app/paint/gimpheal.[ch] * app/paint/gimphealoptions.[ch]: the heal core and its options. * app/paint/gimp-paint.c: register the heal core. * app/tools/Makefile.am * app/tools/makefile.msc * app/tools/gimphealtool.[ch]: the heal tool. * app/tools/gimp-tools.c: register the heal tool. * app/tools/gimppaintoptions-gui.c: show the widgets that are used by heal. * app/widgets/gimphelp-ids.h: the heal help ID. * tools/pdbgen/stddefs.pdb * tools/pdbgen/pdb/paint_tools.pdb: the heal PDB wrappers. * app/widgets/widgets-enums.h * app/widgets/gimpcursor.c * cursors/Makefile.am * cursors/makefile.msc * cursors/tool-heal.png * cursors/xbm/tool-heal.xbm * cursors/xbm/tool-heal-mask.xbm: a new cursor for the heal tool. * libgimpwidgets/gimpstock.[ch] * themes/Default/images/Makefile.am * themes/Default/images/makefile.msc * themes/Default/images/tools/stock-tool-heal-16.png * themes/Default/images/tools/stock-tool-heal-22.png: new stock icons for the heal tool. * app/pdb/internal_procs.c * app/pdb/paint_tools_cmds.c * libgimp/gimppainttools_pdb.[ch]: regenerated.
2006-09-03 02:54:35 +08:00
void
gimp_heal_register (Gimp *gimp,
GimpPaintRegisterCallback callback)
{
(* callback) (gimp,
GIMP_TYPE_HEAL,
GIMP_TYPE_SOURCE_OPTIONS,
"gimp-heal",
_("Heal"),
"gimp-tool-heal");
}
static void
gimp_heal_class_init (GimpHealClass *klass)
{
added GError** parameter to GimpPaintCore::start(). 2006-09-26 Michael Natterer <mitch@gimp.org> * app/paint/gimppaintcore.[ch]: added GError** parameter to GimpPaintCore::start(). * app/tools/gimppainttool.c (button_press): display the error in the statusbar. * app/paint/gimppaintcore-stroke.c: pass a NULL error, effectively swallowing mssages. Will fix that later. * app/paint/gimpbrushcore.c * app/paint/gimpclone.c * app/paint/gimpsourcecore.c: changed accordingly. Set the error instead of calling g_message(). * app/paint/gimpheal.c * app/paint/gimpperspectiveclone.c: implement start() and bail out early on indexed drawables instead of showing a g_message() in other functions that are called later. * app/tools/gimptool.[ch]: added GError** to GimpTool::initialize(). * app/tools/gimptool.c (gimp_tool_initialize): display the error in the statusbar. Keep the external API GError-free. * app/tools/gimprectangletool.[ch]: added GError** to gimp_rectangle_tool_initialize(). * app/tools/gimpbrightnesscontrasttool.c * app/tools/gimpcolorbalancetool.c * app/tools/gimpcolorizetool.c * app/tools/gimpcroptool.c * app/tools/gimpcurvestool.c * app/tools/gimphuesaturationtool.c * app/tools/gimpimagemaptool.c * app/tools/gimplevelstool.c * app/tools/gimpperspectiveclonetool.c * app/tools/gimpposterizetool.c * app/tools/gimpthresholdtool.c * app/tools/gimptransformtool.c: changed accordingly. Set the errors in initialize() instead of using gimp_message(). * app/tools/gimpblendtool.c: implement initialize() and bail out early on indexed images instead of showing a gimp_message() in button_press().
2006-09-27 04:55:40 +08:00
GimpPaintCoreClass *paint_core_class = GIMP_PAINT_CORE_CLASS (klass);
GimpSourceCoreClass *source_core_class = GIMP_SOURCE_CORE_CLASS (klass);
added GError** parameter to GimpPaintCore::start(). 2006-09-26 Michael Natterer <mitch@gimp.org> * app/paint/gimppaintcore.[ch]: added GError** parameter to GimpPaintCore::start(). * app/tools/gimppainttool.c (button_press): display the error in the statusbar. * app/paint/gimppaintcore-stroke.c: pass a NULL error, effectively swallowing mssages. Will fix that later. * app/paint/gimpbrushcore.c * app/paint/gimpclone.c * app/paint/gimpsourcecore.c: changed accordingly. Set the error instead of calling g_message(). * app/paint/gimpheal.c * app/paint/gimpperspectiveclone.c: implement start() and bail out early on indexed drawables instead of showing a g_message() in other functions that are called later. * app/tools/gimptool.[ch]: added GError** to GimpTool::initialize(). * app/tools/gimptool.c (gimp_tool_initialize): display the error in the statusbar. Keep the external API GError-free. * app/tools/gimprectangletool.[ch]: added GError** to gimp_rectangle_tool_initialize(). * app/tools/gimpbrightnesscontrasttool.c * app/tools/gimpcolorbalancetool.c * app/tools/gimpcolorizetool.c * app/tools/gimpcroptool.c * app/tools/gimpcurvestool.c * app/tools/gimphuesaturationtool.c * app/tools/gimpimagemaptool.c * app/tools/gimplevelstool.c * app/tools/gimpperspectiveclonetool.c * app/tools/gimpposterizetool.c * app/tools/gimpthresholdtool.c * app/tools/gimptransformtool.c: changed accordingly. Set the errors in initialize() instead of using gimp_message(). * app/tools/gimpblendtool.c: implement initialize() and bail out early on indexed images instead of showing a gimp_message() in button_press().
2006-09-27 04:55:40 +08:00
paint_core_class->start = gimp_heal_start;
source_core_class->motion = gimp_heal_motion;
}
static void
gimp_heal_init (GimpHeal *heal)
{
}
added GError** parameter to GimpPaintCore::start(). 2006-09-26 Michael Natterer <mitch@gimp.org> * app/paint/gimppaintcore.[ch]: added GError** parameter to GimpPaintCore::start(). * app/tools/gimppainttool.c (button_press): display the error in the statusbar. * app/paint/gimppaintcore-stroke.c: pass a NULL error, effectively swallowing mssages. Will fix that later. * app/paint/gimpbrushcore.c * app/paint/gimpclone.c * app/paint/gimpsourcecore.c: changed accordingly. Set the error instead of calling g_message(). * app/paint/gimpheal.c * app/paint/gimpperspectiveclone.c: implement start() and bail out early on indexed drawables instead of showing a g_message() in other functions that are called later. * app/tools/gimptool.[ch]: added GError** to GimpTool::initialize(). * app/tools/gimptool.c (gimp_tool_initialize): display the error in the statusbar. Keep the external API GError-free. * app/tools/gimprectangletool.[ch]: added GError** to gimp_rectangle_tool_initialize(). * app/tools/gimpbrightnesscontrasttool.c * app/tools/gimpcolorbalancetool.c * app/tools/gimpcolorizetool.c * app/tools/gimpcroptool.c * app/tools/gimpcurvestool.c * app/tools/gimphuesaturationtool.c * app/tools/gimpimagemaptool.c * app/tools/gimplevelstool.c * app/tools/gimpperspectiveclonetool.c * app/tools/gimpposterizetool.c * app/tools/gimpthresholdtool.c * app/tools/gimptransformtool.c: changed accordingly. Set the errors in initialize() instead of using gimp_message(). * app/tools/gimpblendtool.c: implement initialize() and bail out early on indexed images instead of showing a gimp_message() in button_press().
2006-09-27 04:55:40 +08:00
static gboolean
gimp_heal_start (GimpPaintCore *paint_core,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
const GimpCoords *coords,
added GError** parameter to GimpPaintCore::start(). 2006-09-26 Michael Natterer <mitch@gimp.org> * app/paint/gimppaintcore.[ch]: added GError** parameter to GimpPaintCore::start(). * app/tools/gimppainttool.c (button_press): display the error in the statusbar. * app/paint/gimppaintcore-stroke.c: pass a NULL error, effectively swallowing mssages. Will fix that later. * app/paint/gimpbrushcore.c * app/paint/gimpclone.c * app/paint/gimpsourcecore.c: changed accordingly. Set the error instead of calling g_message(). * app/paint/gimpheal.c * app/paint/gimpperspectiveclone.c: implement start() and bail out early on indexed drawables instead of showing a g_message() in other functions that are called later. * app/tools/gimptool.[ch]: added GError** to GimpTool::initialize(). * app/tools/gimptool.c (gimp_tool_initialize): display the error in the statusbar. Keep the external API GError-free. * app/tools/gimprectangletool.[ch]: added GError** to gimp_rectangle_tool_initialize(). * app/tools/gimpbrightnesscontrasttool.c * app/tools/gimpcolorbalancetool.c * app/tools/gimpcolorizetool.c * app/tools/gimpcroptool.c * app/tools/gimpcurvestool.c * app/tools/gimphuesaturationtool.c * app/tools/gimpimagemaptool.c * app/tools/gimplevelstool.c * app/tools/gimpperspectiveclonetool.c * app/tools/gimpposterizetool.c * app/tools/gimpthresholdtool.c * app/tools/gimptransformtool.c: changed accordingly. Set the errors in initialize() instead of using gimp_message(). * app/tools/gimpblendtool.c: implement initialize() and bail out early on indexed images instead of showing a gimp_message() in button_press().
2006-09-27 04:55:40 +08:00
GError **error)
{
GimpSourceCore *source_core = GIMP_SOURCE_CORE (paint_core);
if (! GIMP_PAINT_CORE_CLASS (parent_class)->start (paint_core, drawable,
paint_options, coords,
error))
{
return FALSE;
}
if (! source_core->set_source && gimp_drawable_is_indexed (drawable))
{
g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
_("Healing does not operate on indexed layers."));
added GError** parameter to GimpPaintCore::start(). 2006-09-26 Michael Natterer <mitch@gimp.org> * app/paint/gimppaintcore.[ch]: added GError** parameter to GimpPaintCore::start(). * app/tools/gimppainttool.c (button_press): display the error in the statusbar. * app/paint/gimppaintcore-stroke.c: pass a NULL error, effectively swallowing mssages. Will fix that later. * app/paint/gimpbrushcore.c * app/paint/gimpclone.c * app/paint/gimpsourcecore.c: changed accordingly. Set the error instead of calling g_message(). * app/paint/gimpheal.c * app/paint/gimpperspectiveclone.c: implement start() and bail out early on indexed drawables instead of showing a g_message() in other functions that are called later. * app/tools/gimptool.[ch]: added GError** to GimpTool::initialize(). * app/tools/gimptool.c (gimp_tool_initialize): display the error in the statusbar. Keep the external API GError-free. * app/tools/gimprectangletool.[ch]: added GError** to gimp_rectangle_tool_initialize(). * app/tools/gimpbrightnesscontrasttool.c * app/tools/gimpcolorbalancetool.c * app/tools/gimpcolorizetool.c * app/tools/gimpcroptool.c * app/tools/gimpcurvestool.c * app/tools/gimphuesaturationtool.c * app/tools/gimpimagemaptool.c * app/tools/gimplevelstool.c * app/tools/gimpperspectiveclonetool.c * app/tools/gimpposterizetool.c * app/tools/gimpthresholdtool.c * app/tools/gimptransformtool.c: changed accordingly. Set the errors in initialize() instead of using gimp_message(). * app/tools/gimpblendtool.c: implement initialize() and bail out early on indexed images instead of showing a gimp_message() in button_press().
2006-09-27 04:55:40 +08:00
return FALSE;
}
return TRUE;
}
/* Subtract bottom from top and store in result as a double
*/
static void
gimp_heal_sub (GeglBuffer *top_buffer,
const GeglRectangle *top_rect,
GeglBuffer *bottom_buffer,
const GeglRectangle *bottom_rect,
GeglBuffer *result_buffer,
const GeglRectangle *result_rect)
{
GeglBufferIterator *iter;
const Babl *format = gegl_buffer_get_format (top_buffer);
gint bpp = babl_format_get_bytes_per_pixel (format);
gegl_buffer_set_format (top_buffer, babl_format_n (babl_type ("u8"), bpp));
gegl_buffer_set_format (bottom_buffer, babl_format_n (babl_type ("u8"), bpp));
iter = gegl_buffer_iterator_new (top_buffer, top_rect, 0, NULL,
GEGL_BUFFER_READ, GEGL_ABYSS_NONE);
gegl_buffer_iterator_add (iter, bottom_buffer, bottom_rect, 0, NULL,
GEGL_BUFFER_READ, GEGL_ABYSS_NONE);
Merged the "soc-2006-healing-brush" branch. That branch is now officially 2006-09-02 Michael Natterer <mitch@gimp.org> Merged the "soc-2006-healing-brush" branch. That branch is now officially closed and all further fixes and changes have to be applied to HEAD. Did some minor adjustments, mostly small indentation and spacing fixes. Derive the tool from the newly introduced GimpBrushTool which did not exist when the branch was created. Thanks a lot to Kevin Sookocheff for this nice contribution! * app/paint/paint-enums.[ch]: new enum GimpHealAlignMode. * app/paint/Makefile.am * app/paint/makefile.msc * app/paint/gimpheal.[ch] * app/paint/gimphealoptions.[ch]: the heal core and its options. * app/paint/gimp-paint.c: register the heal core. * app/tools/Makefile.am * app/tools/makefile.msc * app/tools/gimphealtool.[ch]: the heal tool. * app/tools/gimp-tools.c: register the heal tool. * app/tools/gimppaintoptions-gui.c: show the widgets that are used by heal. * app/widgets/gimphelp-ids.h: the heal help ID. * tools/pdbgen/stddefs.pdb * tools/pdbgen/pdb/paint_tools.pdb: the heal PDB wrappers. * app/widgets/widgets-enums.h * app/widgets/gimpcursor.c * cursors/Makefile.am * cursors/makefile.msc * cursors/tool-heal.png * cursors/xbm/tool-heal.xbm * cursors/xbm/tool-heal-mask.xbm: a new cursor for the heal tool. * libgimpwidgets/gimpstock.[ch] * themes/Default/images/Makefile.am * themes/Default/images/makefile.msc * themes/Default/images/tools/stock-tool-heal-16.png * themes/Default/images/tools/stock-tool-heal-22.png: new stock icons for the heal tool. * app/pdb/internal_procs.c * app/pdb/paint_tools_cmds.c * libgimp/gimppainttools_pdb.[ch]: regenerated.
2006-09-03 02:54:35 +08:00
gegl_buffer_iterator_add (iter, result_buffer, result_rect, 0,
babl_format_n (babl_type ("double"), bpp),
GEGL_BUFFER_WRITE, GEGL_ABYSS_NONE);
while (gegl_buffer_iterator_next (iter))
{
guchar *t = iter->data[0];
guchar *b = iter->data[1];
gdouble *r = iter->data[2];
gint length = iter->length * bpp;
while (length--)
*r++ = (gdouble) *t++ - (gdouble) *b++;
}
gegl_buffer_set_format (top_buffer, NULL);
gegl_buffer_set_format (bottom_buffer, NULL);
}
/* Add first to second and store in result
*/
static void
gimp_heal_add (GeglBuffer *first_buffer,
const GeglRectangle *first_rect,
GeglBuffer *second_buffer,
const GeglRectangle *second_rect,
GeglBuffer *result_buffer,
const GeglRectangle *result_rect)
{
GeglBufferIterator *iter;
const Babl *format = gegl_buffer_get_format (result_buffer);
gint bpp = babl_format_get_bytes_per_pixel (format);
gegl_buffer_set_format (second_buffer, babl_format_n (babl_type ("u8"), bpp));
gegl_buffer_set_format (result_buffer, babl_format_n (babl_type ("u8"), bpp));
iter = gegl_buffer_iterator_new (first_buffer, first_rect, 0,
babl_format_n (babl_type ("double"), bpp),
GEGL_BUFFER_READ, GEGL_ABYSS_NONE);
gegl_buffer_iterator_add (iter, second_buffer, second_rect, 0, NULL,
GEGL_BUFFER_READ, GEGL_ABYSS_NONE);
gegl_buffer_iterator_add (iter, result_buffer, result_rect, 0, NULL,
GEGL_BUFFER_WRITE, GEGL_ABYSS_NONE);
while (gegl_buffer_iterator_next (iter))
{
gdouble *f = iter->data[0];
guchar *s = iter->data[1];
guchar *r = iter->data[2];
gint length = iter->length * bpp;
while (length--)
{
gdouble tmp = ROUND (*f++ + (gdouble) *s++);
*r++ = (guchar) CLAMP0255 (tmp);
}
}
gegl_buffer_set_format (second_buffer, NULL);
gegl_buffer_set_format (result_buffer, NULL);
}
/* Perform one iteration of the laplace solver for matrix. Store the
* result in solution and return the square of the cummulative error
* of the solution.
*/
static gdouble
gimp_heal_laplace_iteration (gdouble *matrix,
gint height,
gint depth,
gint width,
gdouble *solution,
guchar *mask)
{
const gint rowstride = width * depth;
gint i, j, k, off, offm, offm0, off0;
gdouble tmp, diff;
gdouble err = 0.0;
const gdouble w = 1.80 * 0.25; /* Over-relaxation = 1.8 */
/* we use a red/black checker model of the discretization grid */
/* do reds */
for (i = 0; i < height; i++)
{
off0 = i * rowstride;
offm0 = i * width;
for (j = i % 2; j < width; j += 2)
{
off = off0 + j * depth;
offm = offm0 + j;
if ((0 == mask[offm]) ||
(i == 0) || (i == (height - 1)) ||
(j == 0) || (j == (width - 1)))
{
/* do nothing at the boundary or outside mask */
for (k = 0; k < depth; k++)
solution[off + k] = matrix[off + k];
}
else
{
/* Use Gauss Siedel to get the correction factor then
* over-relax it
*/
for (k = 0; k < depth; k++)
{
tmp = solution[off + k];
solution[off + k] = (matrix[off + k] +
w *
(matrix[off - depth + k] + /* west */
matrix[off + depth + k] + /* east */
matrix[off - rowstride + k] + /* north */
matrix[off + rowstride + k] - 4.0 *
matrix[off+k])); /* south */
diff = solution[off + k] - tmp;
err += diff * diff;
}
}
}
}
/* Do blacks
*
* As we've done the reds earlier, we can use them right now to
* accelerate the convergence. So we have "solution" in the solver
* instead of "matrix" above
*/
for (i = 0; i < height; i++)
{
off0 = i * rowstride;
offm0 = i * width;
for (j = (i % 2) ? 0 : 1; j < width; j += 2)
{
off = off0 + j * depth;
offm = offm0 + j;
if ((0 == mask[offm]) ||
(i == 0) || (i == (height - 1)) ||
(j == 0) || (j == (width - 1)))
{
/* do nothing at the boundary or outside mask */
for (k = 0; k < depth; k++)
solution[off + k] = matrix[off + k];
}
else
{
/* Use Gauss Siedel to get the correction factor then
* over-relax it
*/
for (k = 0; k < depth; k++)
{
tmp = solution[off + k];
solution[off + k] = (matrix[off + k] +
w *
(solution[off - depth + k] + /* west */
solution[off + depth + k] + /* east */
solution[off - rowstride + k] + /* north */
solution[off + rowstride + k] - 4.0 *
matrix[off+k])); /* south */
diff = solution[off + k] - tmp;
err += diff*diff;
}
}
}
}
return err;
}
/* Solve the laplace equation for matrix and store the result in solution.
*/
static void
gimp_heal_laplace_loop (gdouble *matrix,
gint height,
gint depth,
gint width,
gdouble *solution,
guchar *mask)
{
#define EPSILON 0.001
Merged the "soc-2006-healing-brush" branch. That branch is now officially 2006-09-02 Michael Natterer <mitch@gimp.org> Merged the "soc-2006-healing-brush" branch. That branch is now officially closed and all further fixes and changes have to be applied to HEAD. Did some minor adjustments, mostly small indentation and spacing fixes. Derive the tool from the newly introduced GimpBrushTool which did not exist when the branch was created. Thanks a lot to Kevin Sookocheff for this nice contribution! * app/paint/paint-enums.[ch]: new enum GimpHealAlignMode. * app/paint/Makefile.am * app/paint/makefile.msc * app/paint/gimpheal.[ch] * app/paint/gimphealoptions.[ch]: the heal core and its options. * app/paint/gimp-paint.c: register the heal core. * app/tools/Makefile.am * app/tools/makefile.msc * app/tools/gimphealtool.[ch]: the heal tool. * app/tools/gimp-tools.c: register the heal tool. * app/tools/gimppaintoptions-gui.c: show the widgets that are used by heal. * app/widgets/gimphelp-ids.h: the heal help ID. * tools/pdbgen/stddefs.pdb * tools/pdbgen/pdb/paint_tools.pdb: the heal PDB wrappers. * app/widgets/widgets-enums.h * app/widgets/gimpcursor.c * cursors/Makefile.am * cursors/makefile.msc * cursors/tool-heal.png * cursors/xbm/tool-heal.xbm * cursors/xbm/tool-heal-mask.xbm: a new cursor for the heal tool. * libgimpwidgets/gimpstock.[ch] * themes/Default/images/Makefile.am * themes/Default/images/makefile.msc * themes/Default/images/tools/stock-tool-heal-16.png * themes/Default/images/tools/stock-tool-heal-22.png: new stock icons for the heal tool. * app/pdb/internal_procs.c * app/pdb/paint_tools_cmds.c * libgimp/gimppainttools_pdb.[ch]: regenerated.
2006-09-03 02:54:35 +08:00
#define MAX_ITER 500
gint i;
/* repeat until convergence or max iterations */
for (i = 0; i < MAX_ITER; i++)
Merged the "soc-2006-healing-brush" branch. That branch is now officially 2006-09-02 Michael Natterer <mitch@gimp.org> Merged the "soc-2006-healing-brush" branch. That branch is now officially closed and all further fixes and changes have to be applied to HEAD. Did some minor adjustments, mostly small indentation and spacing fixes. Derive the tool from the newly introduced GimpBrushTool which did not exist when the branch was created. Thanks a lot to Kevin Sookocheff for this nice contribution! * app/paint/paint-enums.[ch]: new enum GimpHealAlignMode. * app/paint/Makefile.am * app/paint/makefile.msc * app/paint/gimpheal.[ch] * app/paint/gimphealoptions.[ch]: the heal core and its options. * app/paint/gimp-paint.c: register the heal core. * app/tools/Makefile.am * app/tools/makefile.msc * app/tools/gimphealtool.[ch]: the heal tool. * app/tools/gimp-tools.c: register the heal tool. * app/tools/gimppaintoptions-gui.c: show the widgets that are used by heal. * app/widgets/gimphelp-ids.h: the heal help ID. * tools/pdbgen/stddefs.pdb * tools/pdbgen/pdb/paint_tools.pdb: the heal PDB wrappers. * app/widgets/widgets-enums.h * app/widgets/gimpcursor.c * cursors/Makefile.am * cursors/makefile.msc * cursors/tool-heal.png * cursors/xbm/tool-heal.xbm * cursors/xbm/tool-heal-mask.xbm: a new cursor for the heal tool. * libgimpwidgets/gimpstock.[ch] * themes/Default/images/Makefile.am * themes/Default/images/makefile.msc * themes/Default/images/tools/stock-tool-heal-16.png * themes/Default/images/tools/stock-tool-heal-22.png: new stock icons for the heal tool. * app/pdb/internal_procs.c * app/pdb/paint_tools_cmds.c * libgimp/gimppainttools_pdb.[ch]: regenerated.
2006-09-03 02:54:35 +08:00
{
gdouble sqr_err;
/* do one iteration and store the amount of error */
sqr_err = gimp_heal_laplace_iteration (matrix, height, depth, width,
solution, mask);
/* copy solution to matrix */
memcpy (matrix, solution, width * height * depth * sizeof (double));
if (sqr_err < EPSILON)
break;
}
}
/* Original Algorithm Design:
Merged the "soc-2006-healing-brush" branch. That branch is now officially 2006-09-02 Michael Natterer <mitch@gimp.org> Merged the "soc-2006-healing-brush" branch. That branch is now officially closed and all further fixes and changes have to be applied to HEAD. Did some minor adjustments, mostly small indentation and spacing fixes. Derive the tool from the newly introduced GimpBrushTool which did not exist when the branch was created. Thanks a lot to Kevin Sookocheff for this nice contribution! * app/paint/paint-enums.[ch]: new enum GimpHealAlignMode. * app/paint/Makefile.am * app/paint/makefile.msc * app/paint/gimpheal.[ch] * app/paint/gimphealoptions.[ch]: the heal core and its options. * app/paint/gimp-paint.c: register the heal core. * app/tools/Makefile.am * app/tools/makefile.msc * app/tools/gimphealtool.[ch]: the heal tool. * app/tools/gimp-tools.c: register the heal tool. * app/tools/gimppaintoptions-gui.c: show the widgets that are used by heal. * app/widgets/gimphelp-ids.h: the heal help ID. * tools/pdbgen/stddefs.pdb * tools/pdbgen/pdb/paint_tools.pdb: the heal PDB wrappers. * app/widgets/widgets-enums.h * app/widgets/gimpcursor.c * cursors/Makefile.am * cursors/makefile.msc * cursors/tool-heal.png * cursors/xbm/tool-heal.xbm * cursors/xbm/tool-heal-mask.xbm: a new cursor for the heal tool. * libgimpwidgets/gimpstock.[ch] * themes/Default/images/Makefile.am * themes/Default/images/makefile.msc * themes/Default/images/tools/stock-tool-heal-16.png * themes/Default/images/tools/stock-tool-heal-22.png: new stock icons for the heal tool. * app/pdb/internal_procs.c * app/pdb/paint_tools_cmds.c * libgimp/gimppainttools_pdb.[ch]: regenerated.
2006-09-03 02:54:35 +08:00
*
* T. Georgiev, "Photoshop Healing Brush: a Tool for Seamless Cloning
* http://www.tgeorgiev.net/Photoshop_Healing.pdf
*/
static void
gimp_heal (GeglBuffer *src_buffer,
const GeglRectangle *src_rect,
GeglBuffer *dest_buffer,
const GeglRectangle *dest_rect,
GeglBuffer *mask_buffer,
const GeglRectangle *mask_rect)
{
const Babl *src_format;
const Babl *dest_format;
gint src_bpp;
gint dest_bpp;
gint width;
gint height;
gdouble *i_1;
gdouble *i_2;
GeglBuffer *i_1_buffer;
GeglBuffer *i_2_buffer;
guchar *mask;
src_format = gegl_buffer_get_format (src_buffer);
dest_format = gegl_buffer_get_format (dest_buffer);
src_bpp = babl_format_get_bytes_per_pixel (src_format);
dest_bpp = babl_format_get_bytes_per_pixel (dest_format);
width = gegl_buffer_get_width (src_buffer);
height = gegl_buffer_get_height (src_buffer);
g_return_if_fail (src_bpp == dest_bpp);
i_1 = g_new (gdouble, width * height * src_bpp);
i_2 = g_new (gdouble, width * height * src_bpp);
i_1_buffer =
gegl_buffer_linear_new_from_data (i_1,
babl_format_n (babl_type ("double"),
src_bpp),
GEGL_RECTANGLE (0, 0, width, height),
GEGL_AUTO_ROWSTRIDE,
(GDestroyNotify) g_free, i_1);
i_2_buffer =
gegl_buffer_linear_new_from_data (i_2,
babl_format_n (babl_type ("double"),
src_bpp),
GEGL_RECTANGLE (0, 0, width, height),
GEGL_AUTO_ROWSTRIDE,
(GDestroyNotify) g_free, i_2);
/* substract pattern from image and store the result as a double in i_1 */
gimp_heal_sub (dest_buffer, dest_rect,
src_buffer, src_rect,
i_1_buffer, GEGL_RECTANGLE (0, 0, width, height));
mask = g_new (guchar, mask_rect->width * mask_rect->height);
gegl_buffer_get (mask_buffer, mask_rect, 1.0, babl_format ("Y u8"),
mask, GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
/* FIXME: is a faster implementation needed? */
gimp_heal_laplace_loop (i_1, height, src_bpp, width, i_2, mask);
g_free (mask);
Merged the "soc-2006-healing-brush" branch. That branch is now officially 2006-09-02 Michael Natterer <mitch@gimp.org> Merged the "soc-2006-healing-brush" branch. That branch is now officially closed and all further fixes and changes have to be applied to HEAD. Did some minor adjustments, mostly small indentation and spacing fixes. Derive the tool from the newly introduced GimpBrushTool which did not exist when the branch was created. Thanks a lot to Kevin Sookocheff for this nice contribution! * app/paint/paint-enums.[ch]: new enum GimpHealAlignMode. * app/paint/Makefile.am * app/paint/makefile.msc * app/paint/gimpheal.[ch] * app/paint/gimphealoptions.[ch]: the heal core and its options. * app/paint/gimp-paint.c: register the heal core. * app/tools/Makefile.am * app/tools/makefile.msc * app/tools/gimphealtool.[ch]: the heal tool. * app/tools/gimp-tools.c: register the heal tool. * app/tools/gimppaintoptions-gui.c: show the widgets that are used by heal. * app/widgets/gimphelp-ids.h: the heal help ID. * tools/pdbgen/stddefs.pdb * tools/pdbgen/pdb/paint_tools.pdb: the heal PDB wrappers. * app/widgets/widgets-enums.h * app/widgets/gimpcursor.c * cursors/Makefile.am * cursors/makefile.msc * cursors/tool-heal.png * cursors/xbm/tool-heal.xbm * cursors/xbm/tool-heal-mask.xbm: a new cursor for the heal tool. * libgimpwidgets/gimpstock.[ch] * themes/Default/images/Makefile.am * themes/Default/images/makefile.msc * themes/Default/images/tools/stock-tool-heal-16.png * themes/Default/images/tools/stock-tool-heal-22.png: new stock icons for the heal tool. * app/pdb/internal_procs.c * app/pdb/paint_tools_cmds.c * libgimp/gimppainttools_pdb.[ch]: regenerated.
2006-09-03 02:54:35 +08:00
/* add solution to original image and store in dest */
gimp_heal_add (i_2_buffer, GEGL_RECTANGLE (0, 0, width, height),
src_buffer, src_rect,
dest_buffer, dest_rect);
g_object_unref (i_1_buffer);
g_object_unref (i_2_buffer);
}
static void
gimp_heal_motion (GimpSourceCore *source_core,
GimpDrawable *drawable,
GimpPaintOptions *paint_options,
const GimpCoords *coords,
gdouble opacity,
GimpPickable *src_pickable,
GeglBuffer *src_buffer,
GeglRectangle *src_rect,
gint src_offset_x,
gint src_offset_y,
GeglBuffer *paint_buffer,
gint paint_buffer_x,
gint paint_buffer_y,
gint paint_area_offset_x,
gint paint_area_offset_y,
gint paint_area_width,
gint paint_area_height)
{
GimpPaintCore *paint_core = GIMP_PAINT_CORE (source_core);
GimpContext *context = GIMP_CONTEXT (paint_options);
GimpDynamics *dynamics = GIMP_BRUSH_CORE (paint_core)->dynamics;
GimpDynamicsOutput *hardness_output;
GimpImage *image = gimp_item_get_image (GIMP_ITEM (drawable));
GeglBuffer *src_copy;
GeglBuffer *dest_buffer;
GeglBuffer *mask_buffer;
const TempBuf *mask_buf;
gdouble fade_point;
gdouble hardness;
hardness_output = gimp_dynamics_get_output (dynamics,
GIMP_DYNAMICS_OUTPUT_HARDNESS);
2008-05-23 00:38:57 +08:00
fade_point = gimp_paint_options_get_fade (paint_options, image,
paint_core->pixel_dist);
2009-10-03 23:53:25 +08:00
hardness = gimp_dynamics_output_get_linear_value (hardness_output,
coords,
2009-12-19 23:29:42 +08:00
paint_options,
fade_point);
mask_buf = gimp_brush_core_get_brush_mask (GIMP_BRUSH_CORE (source_core),
coords,
2008-05-23 00:38:57 +08:00
GIMP_BRUSH_HARD,
hardness);
/* check that all buffers are of the same size */
if (src_rect->width != gegl_buffer_get_width (paint_buffer) ||
src_rect->height != gegl_buffer_get_height (paint_buffer))
{
/* this generally means that the source point has hit the edge
* of the layer, so it is not an error and we should not
* complain, just don't do anything
*/
return;
}
src_copy =
gegl_buffer_new (GEGL_RECTANGLE (0, 0,
src_rect->width,
src_rect->height),
gimp_drawable_get_format_with_alpha (drawable));
gegl_buffer_copy (src_buffer,
src_rect,
src_copy,
GEGL_RECTANGLE (0, 0,
src_rect->width,
src_rect->height));
dest_buffer =
gegl_buffer_new (GEGL_RECTANGLE (0, 0,
gegl_buffer_get_width (paint_buffer),
gegl_buffer_get_height (paint_buffer)),
gimp_drawable_get_format_with_alpha (drawable));
gegl_buffer_copy (gimp_drawable_get_buffer (drawable),
GEGL_RECTANGLE (paint_buffer_x, paint_buffer_y,
gegl_buffer_get_width (paint_buffer),
gegl_buffer_get_height (paint_buffer)),
dest_buffer,
GEGL_RECTANGLE (0, 0, 0, 0));
mask_buffer = gimp_temp_buf_create_buffer ((TempBuf *) mask_buf,
NULL, FALSE);
gimp_heal (src_copy,
GEGL_RECTANGLE (0, 0,
gegl_buffer_get_width (src_copy),
gegl_buffer_get_height (src_copy)),
dest_buffer,
GEGL_RECTANGLE (0, 0,
gegl_buffer_get_width (dest_buffer),
gegl_buffer_get_height (dest_buffer)),
mask_buffer,
GEGL_RECTANGLE (0, 0,
gegl_buffer_get_width (mask_buffer),
gegl_buffer_get_height (mask_buffer)));
gegl_buffer_copy (dest_buffer,
GEGL_RECTANGLE (0, 0, mask_buf->width, mask_buf->height),
paint_buffer,
GEGL_RECTANGLE (paint_area_offset_x,
paint_area_offset_y,
paint_area_width,
paint_area_height));
g_object_unref (src_copy);
g_object_unref (dest_buffer);
g_object_unref (mask_buffer);
Merged the "soc-2006-healing-brush" branch. That branch is now officially 2006-09-02 Michael Natterer <mitch@gimp.org> Merged the "soc-2006-healing-brush" branch. That branch is now officially closed and all further fixes and changes have to be applied to HEAD. Did some minor adjustments, mostly small indentation and spacing fixes. Derive the tool from the newly introduced GimpBrushTool which did not exist when the branch was created. Thanks a lot to Kevin Sookocheff for this nice contribution! * app/paint/paint-enums.[ch]: new enum GimpHealAlignMode. * app/paint/Makefile.am * app/paint/makefile.msc * app/paint/gimpheal.[ch] * app/paint/gimphealoptions.[ch]: the heal core and its options. * app/paint/gimp-paint.c: register the heal core. * app/tools/Makefile.am * app/tools/makefile.msc * app/tools/gimphealtool.[ch]: the heal tool. * app/tools/gimp-tools.c: register the heal tool. * app/tools/gimppaintoptions-gui.c: show the widgets that are used by heal. * app/widgets/gimphelp-ids.h: the heal help ID. * tools/pdbgen/stddefs.pdb * tools/pdbgen/pdb/paint_tools.pdb: the heal PDB wrappers. * app/widgets/widgets-enums.h * app/widgets/gimpcursor.c * cursors/Makefile.am * cursors/makefile.msc * cursors/tool-heal.png * cursors/xbm/tool-heal.xbm * cursors/xbm/tool-heal-mask.xbm: a new cursor for the heal tool. * libgimpwidgets/gimpstock.[ch] * themes/Default/images/Makefile.am * themes/Default/images/makefile.msc * themes/Default/images/tools/stock-tool-heal-16.png * themes/Default/images/tools/stock-tool-heal-22.png: new stock icons for the heal tool. * app/pdb/internal_procs.c * app/pdb/paint_tools_cmds.c * libgimp/gimppainttools_pdb.[ch]: regenerated.
2006-09-03 02:54:35 +08:00
/* replace the canvas with our healed data */
gimp_brush_core_replace_canvas (GIMP_BRUSH_CORE (paint_core), drawable,
coords,
MIN (opacity, GIMP_OPACITY_OPAQUE),
gimp_context_get_opacity (context),
gimp_paint_options_get_brush_mode (paint_options),
2008-05-23 00:38:57 +08:00
hardness,
GIMP_PAINT_INCREMENTAL);
}