2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
1999-08-13 01:53:51 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2009-02-06 05:47:57 +08:00
|
|
|
* gimpbrush-transform.c
|
2007-04-01 21:38:22 +08:00
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
1999-08-13 01:53:51 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
1999-08-13 01:53:51 +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
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
1999-08-13 01:53:51 +08:00
|
|
|
*/
|
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2017-04-07 22:14:05 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-10-15 07:58:39 +08:00
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
2012-04-04 20:06:31 +08:00
|
|
|
#include <gegl.h>
|
2000-12-29 23:22:01 +08:00
|
|
|
|
2018-04-05 04:53:00 +08:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
|
2009-02-06 05:47:57 +08:00
|
|
|
#include "libgimpmath/gimpmath.h"
|
|
|
|
|
2011-04-09 03:18:39 +08:00
|
|
|
#include "core-types.h"
|
2007-04-01 21:38:22 +08:00
|
|
|
|
2012-04-04 20:06:31 +08:00
|
|
|
#include "gegl/gimp-gegl-loops.h"
|
2007-04-01 21:38:22 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
#include "gimp-parallel.h"
|
2011-04-09 03:18:39 +08:00
|
|
|
#include "gimpbrush.h"
|
|
|
|
#include "gimpbrush-transform.h"
|
2012-04-09 06:59:20 +08:00
|
|
|
#include "gimptempbuf.h"
|
2011-04-09 03:18:39 +08:00
|
|
|
|
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
#define MIN_PARALLEL_SUB_SIZE 64
|
|
|
|
#define MIN_PARALLEL_SUB_AREA (MIN_PARALLEL_SUB_SIZE * MIN_PARALLEL_SUB_SIZE)
|
|
|
|
|
|
|
|
|
2007-04-01 21:38:22 +08:00
|
|
|
/* local function prototypes */
|
|
|
|
|
2017-04-08 16:36:47 +08:00
|
|
|
static void gimp_brush_transform_bounding_box (GimpBrush *brush,
|
|
|
|
const GimpMatrix3 *matrix,
|
|
|
|
gint *x,
|
|
|
|
gint *y,
|
|
|
|
gint *width,
|
|
|
|
gint *height);
|
|
|
|
|
|
|
|
static void gimp_brush_transform_blur (GimpTempBuf *buf,
|
|
|
|
gint r);
|
|
|
|
static gint gimp_brush_transform_blur_radius (gint height,
|
|
|
|
gint width,
|
|
|
|
gdouble hardness);
|
|
|
|
static void gimp_brush_transform_adjust_hardness_matrix (gdouble width,
|
|
|
|
gdouble height,
|
|
|
|
gdouble blur_radius,
|
|
|
|
GimpMatrix3 *matrix);
|
2011-04-09 03:18:39 +08:00
|
|
|
|
|
|
|
|
2007-04-01 21:38:22 +08:00
|
|
|
/* public functions */
|
|
|
|
|
|
|
|
void
|
2009-02-01 19:08:22 +08:00
|
|
|
gimp_brush_real_transform_size (GimpBrush *brush,
|
2009-04-26 01:53:09 +08:00
|
|
|
gdouble scale,
|
|
|
|
gdouble aspect_ratio,
|
2009-02-06 05:47:57 +08:00
|
|
|
gdouble angle,
|
2017-12-23 05:53:56 +08:00
|
|
|
gboolean reflect,
|
2009-02-01 19:08:22 +08:00
|
|
|
gint *width,
|
|
|
|
gint *height)
|
2007-04-01 21:38:22 +08:00
|
|
|
{
|
2009-02-06 05:47:57 +08:00
|
|
|
GimpMatrix3 matrix;
|
|
|
|
gint x, y;
|
|
|
|
|
2014-10-12 07:16:32 +08:00
|
|
|
gimp_brush_transform_matrix (gimp_brush_get_width (brush),
|
|
|
|
gimp_brush_get_height (brush),
|
2017-12-23 05:53:56 +08:00
|
|
|
scale, aspect_ratio, angle, reflect, &matrix);
|
2009-04-26 01:53:09 +08:00
|
|
|
|
2014-10-12 07:16:32 +08:00
|
|
|
gimp_brush_transform_bounding_box (brush, &matrix, &x, &y, width, height);
|
2007-04-01 21:38:22 +08:00
|
|
|
}
|
|
|
|
|
2009-02-28 21:06:44 +08:00
|
|
|
/*
|
|
|
|
* Transforms the brush mask with bilinear interpolation.
|
|
|
|
*
|
|
|
|
* Rather than calculating the inverse transform for each point in the
|
2009-03-17 03:59:58 +08:00
|
|
|
* transformed image, this algorithm uses the inverse transformed
|
|
|
|
* corner points of the destination image to work out the starting
|
|
|
|
* position in the source image and the U and V deltas in the source
|
|
|
|
* image space. It then uses a scan-line approach, looping through
|
2018-04-26 02:31:11 +08:00
|
|
|
* rows and columns in the transformed (destination) image while
|
2009-03-17 03:59:58 +08:00
|
|
|
* walking along the corresponding rows and columns (named U and V) in
|
|
|
|
* the source image.
|
2009-02-28 21:06:44 +08:00
|
|
|
*
|
2009-03-17 03:59:58 +08:00
|
|
|
* The horizontal in destination space (transform result) is reverse
|
|
|
|
* transformed into source image space to get U. The vertical in
|
|
|
|
* destination space (transform result) is reverse transformed into
|
|
|
|
* source image space to get V.
|
2009-02-28 21:06:44 +08:00
|
|
|
*
|
2009-03-17 03:59:58 +08:00
|
|
|
* The strength of this particular algorithm is that calculation work
|
|
|
|
* should depend more upon the final transformed brush size rather
|
|
|
|
* than the input brush size.
|
2009-02-28 21:06:44 +08:00
|
|
|
*
|
|
|
|
* There are no floating point calculations in the inner loop for speed.
|
2009-09-26 03:41:21 +08:00
|
|
|
*
|
|
|
|
* Some variables end with the suffix _i to indicate they have been
|
|
|
|
* premultiplied by int_multiple
|
2009-02-28 21:06:44 +08:00
|
|
|
*/
|
2012-04-09 02:25:49 +08:00
|
|
|
GimpTempBuf *
|
2009-02-01 19:08:22 +08:00
|
|
|
gimp_brush_real_transform_mask (GimpBrush *brush,
|
2009-04-26 01:53:09 +08:00
|
|
|
gdouble scale,
|
|
|
|
gdouble aspect_ratio,
|
2010-01-01 21:41:07 +08:00
|
|
|
gdouble angle,
|
2017-12-23 05:53:56 +08:00
|
|
|
gboolean reflect,
|
2010-01-01 21:41:07 +08:00
|
|
|
gdouble hardness)
|
2007-04-01 21:38:22 +08:00
|
|
|
{
|
2012-04-09 02:25:49 +08:00
|
|
|
GimpTempBuf *result;
|
|
|
|
GimpTempBuf *source;
|
2009-02-06 05:47:57 +08:00
|
|
|
const guchar *src;
|
|
|
|
GimpMatrix3 matrix;
|
|
|
|
gint src_width;
|
|
|
|
gint src_height;
|
2009-09-26 03:41:21 +08:00
|
|
|
gint src_width_minus_one;
|
|
|
|
gint src_height_minus_one;
|
2009-02-06 05:47:57 +08:00
|
|
|
gint dest_width;
|
|
|
|
gint dest_height;
|
2017-04-08 16:36:47 +08:00
|
|
|
gint blur_radius;
|
2009-02-06 05:47:57 +08:00
|
|
|
gint x, y;
|
2018-04-05 04:53:00 +08:00
|
|
|
gdouble b_lx, b_rx, t_lx, t_rx;
|
|
|
|
gdouble b_ly, b_ry, t_ly, t_ry;
|
2009-02-28 21:06:44 +08:00
|
|
|
gdouble src_tl_to_tr_delta_x;
|
|
|
|
gdouble src_tl_to_tr_delta_y;
|
|
|
|
gdouble src_tl_to_bl_delta_x;
|
|
|
|
gdouble src_tl_to_bl_delta_y;
|
2009-09-26 03:41:21 +08:00
|
|
|
gint src_walk_ux_i;
|
|
|
|
gint src_walk_uy_i;
|
|
|
|
gint src_walk_vx_i;
|
|
|
|
gint src_walk_vy_i;
|
|
|
|
|
2009-02-28 21:06:44 +08:00
|
|
|
/*
|
2009-03-17 03:59:58 +08:00
|
|
|
* tl, tr etc are used because it is easier to visualize top left,
|
|
|
|
* top right etc corners of the forward transformed source image
|
|
|
|
* rectangle.
|
2009-02-28 21:06:44 +08:00
|
|
|
*/
|
2009-03-03 23:00:14 +08:00
|
|
|
const gint fraction_bits = 12;
|
2009-09-26 03:41:21 +08:00
|
|
|
const gint int_multiple = pow (2, fraction_bits);
|
2009-02-28 21:06:44 +08:00
|
|
|
|
2009-03-17 03:59:58 +08:00
|
|
|
/* In inner loop's bilinear calculation, two numbers that were each
|
|
|
|
* previously multiplied by int_multiple are multiplied together.
|
2009-02-28 21:06:44 +08:00
|
|
|
* To get back the right result, the multiplication result must be
|
2009-03-17 03:59:58 +08:00
|
|
|
* divided *twice* by 2^fraction_bits, equivalent to bit shift right
|
|
|
|
* by 2 * fraction_bits
|
2009-02-28 21:06:44 +08:00
|
|
|
*/
|
|
|
|
const gint recovery_bits = 2 * fraction_bits;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* example: suppose fraction_bits = 9
|
|
|
|
* a 9-bit mask looks like this: 0001 1111 1111
|
|
|
|
* and is given by: 2^fraction_bits - 1
|
|
|
|
* demonstration:
|
|
|
|
* 2^0 = 0000 0000 0001
|
|
|
|
* 2^1 = 0000 0000 0010
|
|
|
|
* :
|
|
|
|
* 2^8 = 0001 0000 0000
|
|
|
|
* 2^9 = 0010 0000 0000
|
|
|
|
* 2^9 - 1 = 0001 1111 1111
|
|
|
|
*/
|
|
|
|
const guint fraction_bitmask = pow(2, fraction_bits) - 1 ;
|
|
|
|
|
2014-10-12 07:16:32 +08:00
|
|
|
source = gimp_brush_get_mask (brush);
|
2007-04-01 21:38:22 +08:00
|
|
|
|
2014-10-12 07:16:32 +08:00
|
|
|
src_width = gimp_brush_get_width (brush);
|
|
|
|
src_height = gimp_brush_get_height (brush);
|
2012-04-23 15:40:56 +08:00
|
|
|
|
|
|
|
gimp_brush_transform_matrix (src_width, src_height,
|
2017-12-23 05:53:56 +08:00
|
|
|
scale, aspect_ratio, angle, reflect, &matrix);
|
2007-04-01 21:38:22 +08:00
|
|
|
|
2016-11-24 04:17:41 +08:00
|
|
|
if (gimp_matrix3_is_identity (&matrix) && hardness == 1.0)
|
2012-04-09 05:56:52 +08:00
|
|
|
return gimp_temp_buf_copy (source);
|
2007-04-01 21:38:22 +08:00
|
|
|
|
2009-09-26 03:41:21 +08:00
|
|
|
src_width_minus_one = src_width - 1;
|
|
|
|
src_height_minus_one = src_height - 1;
|
2007-04-01 21:38:22 +08:00
|
|
|
|
2014-10-12 07:16:32 +08:00
|
|
|
gimp_brush_transform_bounding_box (brush, &matrix,
|
2009-02-06 05:47:57 +08:00
|
|
|
&x, &y, &dest_width, &dest_height);
|
2017-04-08 16:36:47 +08:00
|
|
|
|
|
|
|
blur_radius = 0;
|
|
|
|
|
2016-11-26 18:05:42 +08:00
|
|
|
if (hardness < 1.0)
|
|
|
|
{
|
2017-04-08 16:36:47 +08:00
|
|
|
GimpMatrix3 unrotated_matrix;
|
|
|
|
gint unrotated_x;
|
|
|
|
gint unrotated_y;
|
|
|
|
gint unrotated_dest_width;
|
|
|
|
gint unrotated_dest_height;
|
|
|
|
|
|
|
|
gimp_brush_transform_matrix (src_width, src_height,
|
2017-12-23 05:53:56 +08:00
|
|
|
scale, aspect_ratio, 0.0, FALSE,
|
|
|
|
&unrotated_matrix);
|
2017-04-08 16:36:47 +08:00
|
|
|
|
|
|
|
gimp_brush_transform_bounding_box (brush, &unrotated_matrix,
|
|
|
|
&unrotated_x, &unrotated_y,
|
|
|
|
&unrotated_dest_width,
|
|
|
|
&unrotated_dest_height);
|
|
|
|
|
|
|
|
blur_radius = gimp_brush_transform_blur_radius (unrotated_dest_width,
|
|
|
|
unrotated_dest_height,
|
|
|
|
hardness);
|
|
|
|
|
|
|
|
gimp_brush_transform_adjust_hardness_matrix (dest_width, dest_height,
|
|
|
|
blur_radius, &matrix);
|
2016-11-26 18:05:42 +08:00
|
|
|
}
|
|
|
|
|
2009-02-06 05:47:57 +08:00
|
|
|
gimp_matrix3_translate (&matrix, -x, -y);
|
|
|
|
gimp_matrix3_invert (&matrix);
|
2007-04-01 21:38:22 +08:00
|
|
|
|
2012-04-22 22:28:02 +08:00
|
|
|
result = gimp_temp_buf_new (dest_width, dest_height,
|
2014-10-12 07:16:32 +08:00
|
|
|
gimp_temp_buf_get_format (source));
|
2000-12-29 23:22:01 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
src = gimp_temp_buf_get_data (source);
|
2007-04-01 21:38:22 +08:00
|
|
|
|
2009-03-17 03:57:31 +08:00
|
|
|
/* prevent disappearance of 1x1 pixel brush at some rotations when
|
|
|
|
scaling < 1 */
|
2009-03-03 23:00:14 +08:00
|
|
|
/*
|
|
|
|
if (src_width == 1 && src_height == 1 && scale_x < 1 && scale_y < 1 )
|
|
|
|
{
|
|
|
|
*dest = src[0];
|
|
|
|
return result;
|
|
|
|
}*/
|
|
|
|
|
2018-04-05 04:53:00 +08:00
|
|
|
gimp_matrix3_transform_point (&matrix, 0, 0, &t_lx, &t_ly);
|
|
|
|
gimp_matrix3_transform_point (&matrix, dest_width, 0, &t_rx, &t_ry);
|
|
|
|
gimp_matrix3_transform_point (&matrix, 0, dest_height, &b_lx, &b_ly);
|
|
|
|
gimp_matrix3_transform_point (&matrix, dest_width, dest_height, &b_rx, &b_ry);
|
2009-02-28 21:06:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* in image space, calc U (what was horizontal originally)
|
|
|
|
* note: double precision
|
|
|
|
*/
|
2018-04-05 04:53:00 +08:00
|
|
|
src_tl_to_tr_delta_x = t_rx - t_lx;
|
|
|
|
src_tl_to_tr_delta_y = t_ry - t_ly;
|
2009-02-28 21:06:44 +08:00
|
|
|
|
|
|
|
/* in image space, calc V (what was vertical originally)
|
|
|
|
* note: double precision
|
|
|
|
*/
|
2018-04-05 04:53:00 +08:00
|
|
|
src_tl_to_bl_delta_x = b_lx - t_lx;
|
|
|
|
src_tl_to_bl_delta_y = b_ly - t_ly;
|
2009-02-28 21:06:44 +08:00
|
|
|
|
|
|
|
/* speed optimized, note conversion to int precision */
|
2011-04-09 03:18:39 +08:00
|
|
|
src_walk_ux_i = (gint) ((src_tl_to_tr_delta_x / dest_width) * int_multiple);
|
|
|
|
src_walk_uy_i = (gint) ((src_tl_to_tr_delta_y / dest_width) * int_multiple);
|
|
|
|
src_walk_vx_i = (gint) ((src_tl_to_bl_delta_x / dest_height) * int_multiple);
|
|
|
|
src_walk_vy_i = (gint) ((src_tl_to_bl_delta_y / dest_height) * int_multiple);
|
2009-02-28 21:06:44 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
gimp_parallel_distribute_area (GEGL_RECTANGLE (0, 0, dest_width, dest_height),
|
|
|
|
MIN_PARALLEL_SUB_AREA,
|
|
|
|
[=] (const GeglRectangle *area)
|
2007-04-01 21:38:22 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
guchar *dest;
|
|
|
|
gint src_space_cur_pos_x;
|
|
|
|
gint src_space_cur_pos_y;
|
|
|
|
gint src_space_cur_pos_x_i;
|
|
|
|
gint src_space_cur_pos_y_i;
|
|
|
|
gint src_space_row_start_x_i;
|
|
|
|
gint src_space_row_start_y_i;
|
|
|
|
const guchar *src_walker;
|
|
|
|
const guchar *pixel_next;
|
|
|
|
const guchar *pixel_below;
|
|
|
|
const guchar *pixel_below_next;
|
|
|
|
gint opposite_x, distance_from_true_x;
|
|
|
|
gint opposite_y, distance_from_true_y;
|
|
|
|
gint u, v;
|
|
|
|
|
|
|
|
dest = gimp_temp_buf_get_data (result) +
|
|
|
|
dest_width * area->y + area->x;
|
|
|
|
|
|
|
|
/* initialize current position in source space to the start position (tl)
|
|
|
|
* speed optimized, note conversion to int precision
|
|
|
|
*/
|
|
|
|
src_space_row_start_x_i = (gint) (t_lx * int_multiple) +
|
|
|
|
src_walk_vx_i * area->y +
|
|
|
|
src_walk_ux_i * area->x;
|
|
|
|
src_space_row_start_y_i = (gint) (t_ly * int_multiple) +
|
|
|
|
src_walk_vy_i * area->y +
|
|
|
|
src_walk_uy_i * area->x;
|
|
|
|
|
|
|
|
for (v = 0; v < area->height; v++)
|
2009-02-06 05:47:57 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
src_space_cur_pos_x_i = src_space_row_start_x_i;
|
|
|
|
src_space_cur_pos_y_i = src_space_row_start_y_i;
|
2009-02-28 21:06:44 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
for (u = 0; u < area->width; u++)
|
|
|
|
{
|
|
|
|
src_space_cur_pos_x = src_space_cur_pos_x_i >> fraction_bits;
|
|
|
|
src_space_cur_pos_y = src_space_cur_pos_y_i >> fraction_bits;
|
|
|
|
|
|
|
|
if (src_space_cur_pos_x > src_width_minus_one ||
|
|
|
|
src_space_cur_pos_x < 0 ||
|
|
|
|
src_space_cur_pos_y > src_height_minus_one ||
|
|
|
|
src_space_cur_pos_y < 0)
|
|
|
|
/* no corresponding pixel in source space */
|
2009-02-28 21:06:44 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
*dest = 0;
|
2009-02-28 21:06:44 +08:00
|
|
|
}
|
2018-04-05 04:57:35 +08:00
|
|
|
else /* reverse transformed point hits source pixel */
|
2009-02-28 21:06:44 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
src_walker = src +
|
|
|
|
src_space_cur_pos_y * src_width +
|
|
|
|
src_space_cur_pos_x;
|
|
|
|
|
|
|
|
/* bottom right corner
|
|
|
|
* no pixel below, reuse current pixel instead
|
|
|
|
* no next pixel to the right so reuse current pixel instead
|
|
|
|
*/
|
|
|
|
if (src_space_cur_pos_y == src_height_minus_one &&
|
|
|
|
src_space_cur_pos_x == src_width_minus_one )
|
|
|
|
{
|
|
|
|
pixel_next = src_walker;
|
|
|
|
pixel_below = src_walker;
|
|
|
|
pixel_below_next = src_walker;
|
|
|
|
}
|
2009-02-28 21:06:44 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
/* bottom edge pixel row, except rightmost corner
|
|
|
|
* no pixel below, reuse current pixel instead */
|
|
|
|
else if (src_space_cur_pos_y == src_height_minus_one)
|
|
|
|
{
|
|
|
|
pixel_next = src_walker + 1;
|
|
|
|
pixel_below = src_walker;
|
|
|
|
pixel_below_next = src_walker + 1;
|
|
|
|
}
|
2009-02-28 21:06:44 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
/* right edge pixel column, except bottom corner
|
|
|
|
* no next pixel to the right so reuse current pixel instead */
|
|
|
|
else if (src_space_cur_pos_x == src_width_minus_one)
|
|
|
|
{
|
|
|
|
pixel_next = src_walker;
|
|
|
|
pixel_below = src_walker + src_width;
|
|
|
|
pixel_below_next = pixel_below;
|
|
|
|
}
|
2009-02-28 21:06:44 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
/* neither on bottom edge nor on right edge */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pixel_next = src_walker + 1;
|
|
|
|
pixel_below = src_walker + src_width;
|
|
|
|
pixel_below_next = pixel_below + 1;
|
|
|
|
}
|
2007-04-01 21:38:22 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
distance_from_true_x = src_space_cur_pos_x_i & fraction_bitmask;
|
|
|
|
distance_from_true_y = src_space_cur_pos_y_i & fraction_bitmask;
|
|
|
|
opposite_x = int_multiple - distance_from_true_x;
|
|
|
|
opposite_y = int_multiple - distance_from_true_y;
|
2009-09-26 03:41:21 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
*dest = ((src_walker[0] * opposite_x +
|
|
|
|
pixel_next[0] * distance_from_true_x) * opposite_y +
|
|
|
|
(pixel_below[0] * opposite_x +
|
|
|
|
pixel_below_next[0] *distance_from_true_x) * distance_from_true_y
|
|
|
|
) >> recovery_bits;
|
|
|
|
}
|
2009-09-26 03:41:21 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
src_space_cur_pos_x_i += src_walk_ux_i;
|
|
|
|
src_space_cur_pos_y_i += src_walk_uy_i;
|
2009-03-17 03:57:31 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
dest++;
|
|
|
|
} /* end for x */
|
2009-09-26 03:41:21 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
src_space_row_start_x_i += src_walk_vx_i;
|
|
|
|
src_space_row_start_y_i += src_walk_vy_i;
|
2009-02-28 21:06:44 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
dest += dest_width - area->width;
|
|
|
|
} /* end for y */
|
|
|
|
});
|
2010-06-23 05:02:23 +08:00
|
|
|
|
2017-04-08 16:36:47 +08:00
|
|
|
gimp_brush_transform_blur (result, blur_radius);
|
2010-03-18 01:48:12 +08:00
|
|
|
|
2009-02-06 05:47:57 +08:00
|
|
|
return result;
|
2007-04-01 21:38:22 +08:00
|
|
|
}
|
|
|
|
|
2009-02-28 21:06:44 +08:00
|
|
|
/*
|
2009-03-17 03:57:31 +08:00
|
|
|
* Transforms the brush pixmap with bilinear interpolation.
|
2009-02-28 21:06:44 +08:00
|
|
|
*
|
|
|
|
* The algorithm used is exactly the same as for the brush mask
|
|
|
|
* (gimp_brush_real_transform_mask) except it accounts for 3 color channels
|
2013-06-07 05:26:16 +08:00
|
|
|
* instead of 1 grayscale channel.
|
2009-02-28 21:06:44 +08:00
|
|
|
*
|
|
|
|
* Rather than calculating the inverse transform for each point in the
|
2009-03-17 03:57:31 +08:00
|
|
|
* transformed image, this algorithm uses the inverse transformed
|
|
|
|
* corner points of the destination image to work out the starting
|
|
|
|
* position in the source image and the U and V deltas in the source
|
|
|
|
* image space. It then uses a scan-line approach, looping through
|
2018-04-26 02:31:11 +08:00
|
|
|
* rows and columns in the transformed (destination) image while
|
2009-03-17 03:57:31 +08:00
|
|
|
* walking along the corresponding rows and columns (named U and V) in
|
|
|
|
* the source image.
|
2009-02-28 21:06:44 +08:00
|
|
|
*
|
2009-03-17 03:57:31 +08:00
|
|
|
* The horizontal in destination space (transform result) is reverse
|
|
|
|
* transformed into source image space to get U. The vertical in
|
|
|
|
* destination space (transform result) is reverse transformed into
|
|
|
|
* source image space to get V.
|
2009-02-28 21:06:44 +08:00
|
|
|
*
|
2009-03-17 03:57:31 +08:00
|
|
|
* The strength of this particular algorithm is that calculation work
|
|
|
|
* should depend more upon the final transformed brush size rather
|
|
|
|
* than the input brush size.
|
2009-02-28 21:06:44 +08:00
|
|
|
*
|
|
|
|
* There are no floating point calculations in the inner loop for speed.
|
2009-09-26 03:41:21 +08:00
|
|
|
*
|
|
|
|
* Some variables end with the suffix _i to indicate they have been
|
|
|
|
* premultiplied by int_multiple
|
2009-02-28 21:06:44 +08:00
|
|
|
*/
|
2012-04-09 02:25:49 +08:00
|
|
|
GimpTempBuf *
|
2009-02-06 05:47:57 +08:00
|
|
|
gimp_brush_real_transform_pixmap (GimpBrush *brush,
|
2009-04-26 01:53:09 +08:00
|
|
|
gdouble scale,
|
|
|
|
gdouble aspect_ratio,
|
2010-01-01 21:41:07 +08:00
|
|
|
gdouble angle,
|
2017-12-23 05:53:56 +08:00
|
|
|
gboolean reflect,
|
2010-01-01 21:41:07 +08:00
|
|
|
gdouble hardness)
|
2007-03-12 17:33:57 +08:00
|
|
|
{
|
2012-04-09 02:25:49 +08:00
|
|
|
GimpTempBuf *result;
|
|
|
|
GimpTempBuf *source;
|
2009-02-06 05:47:57 +08:00
|
|
|
const guchar *src;
|
|
|
|
GimpMatrix3 matrix;
|
|
|
|
gint src_width;
|
|
|
|
gint src_height;
|
2009-09-26 03:41:21 +08:00
|
|
|
gint src_width_minus_one;
|
|
|
|
gint src_height_minus_one;
|
2009-02-06 05:47:57 +08:00
|
|
|
gint dest_width;
|
|
|
|
gint dest_height;
|
2017-04-08 16:36:47 +08:00
|
|
|
gint blur_radius;
|
2009-02-06 05:47:57 +08:00
|
|
|
gint x, y;
|
2018-04-05 04:53:00 +08:00
|
|
|
gdouble b_lx, b_rx, t_lx, t_rx;
|
|
|
|
gdouble b_ly, b_ry, t_ly, t_ry;
|
2009-02-28 21:06:44 +08:00
|
|
|
gdouble src_tl_to_tr_delta_x;
|
|
|
|
gdouble src_tl_to_tr_delta_y;
|
|
|
|
gdouble src_tl_to_bl_delta_x;
|
|
|
|
gdouble src_tl_to_bl_delta_y;
|
2009-09-26 03:41:21 +08:00
|
|
|
gint src_walk_ux_i;
|
|
|
|
gint src_walk_uy_i;
|
|
|
|
gint src_walk_vx_i;
|
|
|
|
gint src_walk_vy_i;
|
|
|
|
|
2009-02-28 21:06:44 +08:00
|
|
|
/*
|
2009-03-17 03:59:58 +08:00
|
|
|
* tl, tr etc are used because it is easier to visualize top left,
|
|
|
|
* top right etc corners of the forward transformed source image
|
|
|
|
* rectangle.
|
2009-02-28 21:06:44 +08:00
|
|
|
*/
|
2009-03-03 23:00:14 +08:00
|
|
|
const gint fraction_bits = 12;
|
2018-04-05 04:57:35 +08:00
|
|
|
const gint int_multiple = pow (2, fraction_bits);
|
2009-02-28 21:06:44 +08:00
|
|
|
|
2009-03-17 03:59:58 +08:00
|
|
|
/* In inner loop's bilinear calculation, two numbers that were each
|
|
|
|
* previously multiplied by int_multiple are multiplied together.
|
2009-02-28 21:06:44 +08:00
|
|
|
* To get back the right result, the multiplication result must be
|
2009-03-17 03:59:58 +08:00
|
|
|
* divided *twice* by 2^fraction_bits, equivalent to bit shift right
|
|
|
|
* by 2 * fraction_bits
|
2009-02-28 21:06:44 +08:00
|
|
|
*/
|
|
|
|
const gint recovery_bits = 2 * fraction_bits;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* example: suppose fraction_bits = 9
|
|
|
|
* a 9-bit mask looks like this: 0001 1111 1111
|
|
|
|
* and is given by: 2^fraction_bits - 1
|
|
|
|
* demonstration:
|
|
|
|
* 2^0 = 0000 0000 0001
|
|
|
|
* 2^1 = 0000 0000 0010
|
|
|
|
* :
|
|
|
|
* 2^8 = 0001 0000 0000
|
|
|
|
* 2^9 = 0010 0000 0000
|
|
|
|
* 2^9 - 1 = 0001 1111 1111
|
|
|
|
*/
|
2018-04-05 04:57:35 +08:00
|
|
|
const guint fraction_bitmask = pow(2, fraction_bits) - 1 ;
|
2009-02-28 21:06:44 +08:00
|
|
|
|
2014-10-12 07:16:32 +08:00
|
|
|
source = gimp_brush_get_pixmap (brush);
|
2007-03-12 17:33:57 +08:00
|
|
|
|
2014-10-12 07:16:32 +08:00
|
|
|
src_width = gimp_brush_get_width (brush);
|
|
|
|
src_height = gimp_brush_get_height (brush);
|
2012-04-23 15:40:56 +08:00
|
|
|
|
|
|
|
gimp_brush_transform_matrix (src_width, src_height,
|
2017-12-23 05:53:56 +08:00
|
|
|
scale, aspect_ratio, angle, reflect, &matrix);
|
2007-03-12 17:33:57 +08:00
|
|
|
|
2016-11-24 04:17:41 +08:00
|
|
|
if (gimp_matrix3_is_identity (&matrix) && hardness == 1.0)
|
2012-04-09 05:56:52 +08:00
|
|
|
return gimp_temp_buf_copy (source);
|
2007-03-12 17:33:57 +08:00
|
|
|
|
2009-09-26 03:41:21 +08:00
|
|
|
src_width_minus_one = src_width - 1;
|
|
|
|
src_height_minus_one = src_height - 1;
|
2007-03-12 17:33:57 +08:00
|
|
|
|
2014-10-12 07:16:32 +08:00
|
|
|
gimp_brush_transform_bounding_box (brush, &matrix,
|
2009-02-06 05:47:57 +08:00
|
|
|
&x, &y, &dest_width, &dest_height);
|
2016-11-26 18:05:42 +08:00
|
|
|
|
2017-04-08 16:36:47 +08:00
|
|
|
blur_radius = 0;
|
|
|
|
|
|
|
|
if (hardness < 1.0)
|
2016-11-26 18:05:42 +08:00
|
|
|
{
|
2017-04-08 16:36:47 +08:00
|
|
|
GimpMatrix3 unrotated_matrix;
|
|
|
|
gint unrotated_x;
|
|
|
|
gint unrotated_y;
|
|
|
|
gint unrotated_dest_width;
|
|
|
|
gint unrotated_dest_height;
|
|
|
|
|
|
|
|
gimp_brush_transform_matrix (src_width, src_height,
|
2017-12-23 05:53:56 +08:00
|
|
|
scale, aspect_ratio, 0.0, FALSE,
|
|
|
|
&unrotated_matrix);
|
2017-04-08 16:36:47 +08:00
|
|
|
|
|
|
|
gimp_brush_transform_bounding_box (brush, &unrotated_matrix,
|
|
|
|
&unrotated_x, &unrotated_y,
|
|
|
|
&unrotated_dest_width,
|
|
|
|
&unrotated_dest_height);
|
|
|
|
|
|
|
|
blur_radius = gimp_brush_transform_blur_radius (unrotated_dest_width,
|
|
|
|
unrotated_dest_height,
|
|
|
|
hardness);
|
|
|
|
|
|
|
|
gimp_brush_transform_adjust_hardness_matrix (dest_width, dest_height,
|
|
|
|
blur_radius, &matrix);
|
2016-11-26 18:05:42 +08:00
|
|
|
}
|
|
|
|
|
2009-02-06 05:47:57 +08:00
|
|
|
gimp_matrix3_translate (&matrix, -x, -y);
|
|
|
|
gimp_matrix3_invert (&matrix);
|
2007-03-14 15:29:43 +08:00
|
|
|
|
2012-04-09 05:56:52 +08:00
|
|
|
result = gimp_temp_buf_new (dest_width, dest_height,
|
2014-10-12 07:16:32 +08:00
|
|
|
gimp_temp_buf_get_format (source));
|
2007-03-12 17:33:57 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
src = gimp_temp_buf_get_data (source);
|
|
|
|
|
|
|
|
/* prevent disappearance of 1x1 pixel brush at some rotations when
|
|
|
|
scaling < 1 */
|
|
|
|
/*
|
|
|
|
if (src_width == 1 && src_height == 1 && scale_x < 1 && scale_y < 1 )
|
|
|
|
{
|
|
|
|
*dest = src[0];
|
|
|
|
return result;
|
|
|
|
}*/
|
2007-03-12 17:33:57 +08:00
|
|
|
|
2018-04-05 04:53:00 +08:00
|
|
|
gimp_matrix3_transform_point (&matrix, 0, 0, &t_lx, &t_ly);
|
|
|
|
gimp_matrix3_transform_point (&matrix, dest_width, 0, &t_rx, &t_ry);
|
|
|
|
gimp_matrix3_transform_point (&matrix, 0, dest_height, &b_lx, &b_ly);
|
|
|
|
gimp_matrix3_transform_point (&matrix, dest_width, dest_height, &b_rx, &b_ry);
|
2009-02-28 21:06:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* in image space, calc U (what was horizontal originally)
|
|
|
|
* note: double precision
|
|
|
|
*/
|
2018-04-05 04:53:00 +08:00
|
|
|
src_tl_to_tr_delta_x = t_rx - t_lx;
|
|
|
|
src_tl_to_tr_delta_y = t_ry - t_ly;
|
2009-02-28 21:06:44 +08:00
|
|
|
|
|
|
|
/* in image space, calc V (what was vertical originally)
|
|
|
|
* note: double precision
|
|
|
|
*/
|
2018-04-05 04:53:00 +08:00
|
|
|
src_tl_to_bl_delta_x = b_lx - t_lx;
|
|
|
|
src_tl_to_bl_delta_y = b_ly - t_ly;
|
2009-02-28 21:06:44 +08:00
|
|
|
|
|
|
|
/* speed optimized, note conversion to int precision */
|
2018-04-05 04:57:35 +08:00
|
|
|
src_walk_ux_i = (gint) ((src_tl_to_tr_delta_x / dest_width) * int_multiple);
|
|
|
|
src_walk_uy_i = (gint) ((src_tl_to_tr_delta_y / dest_width) * int_multiple);
|
|
|
|
src_walk_vx_i = (gint) ((src_tl_to_bl_delta_x / dest_height) * int_multiple);
|
|
|
|
src_walk_vy_i = (gint) ((src_tl_to_bl_delta_y / dest_height) * int_multiple);
|
2009-02-28 21:06:44 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
gimp_parallel_distribute_area (GEGL_RECTANGLE (0, 0, dest_width, dest_height),
|
|
|
|
MIN_PARALLEL_SUB_AREA,
|
|
|
|
[=] (const GeglRectangle *area)
|
2007-03-12 17:33:57 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
guchar *dest;
|
|
|
|
gint src_space_cur_pos_x;
|
|
|
|
gint src_space_cur_pos_y;
|
|
|
|
gint src_space_cur_pos_x_i;
|
|
|
|
gint src_space_cur_pos_y_i;
|
|
|
|
gint src_space_row_start_x_i;
|
|
|
|
gint src_space_row_start_y_i;
|
|
|
|
const guchar *src_walker;
|
|
|
|
const guchar *pixel_next;
|
|
|
|
const guchar *pixel_below;
|
|
|
|
const guchar *pixel_below_next;
|
|
|
|
gint opposite_x, distance_from_true_x;
|
|
|
|
gint opposite_y, distance_from_true_y;
|
|
|
|
gint u, v;
|
|
|
|
|
|
|
|
dest = gimp_temp_buf_get_data (result) +
|
|
|
|
3 * (dest_width * area->y + area->x);
|
|
|
|
|
|
|
|
/* initialize current position in source space to the start position (tl)
|
|
|
|
* speed optimized, note conversion to int precision
|
|
|
|
*/
|
|
|
|
src_space_row_start_x_i = (gint) (t_lx * int_multiple) +
|
|
|
|
src_walk_vx_i * area->y +
|
|
|
|
src_walk_ux_i * area->x;
|
|
|
|
src_space_row_start_y_i = (gint) (t_ly * int_multiple) +
|
|
|
|
src_walk_vy_i * area->y +
|
|
|
|
src_walk_uy_i * area->x;
|
|
|
|
|
|
|
|
for (v = 0; v < area->height; v++)
|
2007-03-12 17:33:57 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
src_space_cur_pos_x_i = src_space_row_start_x_i;
|
|
|
|
src_space_cur_pos_y_i = src_space_row_start_y_i;
|
|
|
|
|
|
|
|
for (u = 0; u < area->width; u++)
|
2009-02-28 21:06:44 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
src_space_cur_pos_x = src_space_cur_pos_x_i >> fraction_bits;
|
|
|
|
src_space_cur_pos_y = src_space_cur_pos_y_i >> fraction_bits;
|
|
|
|
|
|
|
|
if (src_space_cur_pos_x > src_width_minus_one ||
|
|
|
|
src_space_cur_pos_x < 0 ||
|
|
|
|
src_space_cur_pos_y > src_height_minus_one ||
|
|
|
|
src_space_cur_pos_y < 0)
|
|
|
|
/* no corresponding pixel in source space */
|
2009-02-28 21:06:44 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
dest[0] = 0;
|
|
|
|
dest[1] = 0;
|
|
|
|
dest[2] = 0;
|
2009-02-28 21:06:44 +08:00
|
|
|
}
|
2018-04-05 04:57:35 +08:00
|
|
|
else /* reverse transformed point hits source pixel */
|
2009-02-28 21:06:44 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
src_walker = src +
|
|
|
|
3 * (src_space_cur_pos_y * src_width +
|
|
|
|
src_space_cur_pos_x);
|
|
|
|
|
|
|
|
/* bottom right corner
|
|
|
|
* no pixel below, reuse current pixel instead
|
|
|
|
* no next pixel to the right so reuse current pixel instead
|
|
|
|
*/
|
|
|
|
if (src_space_cur_pos_y == src_height_minus_one &&
|
|
|
|
src_space_cur_pos_x == src_width_minus_one )
|
|
|
|
{
|
|
|
|
pixel_next = src_walker;
|
|
|
|
pixel_below = src_walker;
|
|
|
|
pixel_below_next = src_walker;
|
|
|
|
}
|
2009-02-28 21:06:44 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
/* bottom edge pixel row, except rightmost corner
|
|
|
|
* no pixel below, reuse current pixel instead */
|
|
|
|
else if (src_space_cur_pos_y == src_height_minus_one)
|
|
|
|
{
|
|
|
|
pixel_next = src_walker + 3;
|
|
|
|
pixel_below = src_walker;
|
|
|
|
pixel_below_next = src_walker + 3;
|
|
|
|
}
|
2009-02-28 21:06:44 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
/* right edge pixel column, except bottom corner
|
|
|
|
* no next pixel to the right so reuse current pixel instead */
|
|
|
|
else if (src_space_cur_pos_x == src_width_minus_one)
|
|
|
|
{
|
|
|
|
pixel_next = src_walker;
|
|
|
|
pixel_below = src_walker + src_width * 3;
|
|
|
|
pixel_below_next = pixel_below;
|
|
|
|
}
|
2009-02-28 21:06:44 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
/* neither on bottom edge nor on right edge */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pixel_next = src_walker + 3;
|
|
|
|
pixel_below = src_walker + src_width * 3;
|
|
|
|
pixel_below_next = pixel_below + 3;
|
|
|
|
}
|
2007-03-12 17:33:57 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
distance_from_true_x = src_space_cur_pos_x_i & fraction_bitmask;
|
|
|
|
distance_from_true_y = src_space_cur_pos_y_i & fraction_bitmask;
|
|
|
|
opposite_x = int_multiple - distance_from_true_x;
|
|
|
|
opposite_y = int_multiple - distance_from_true_y;
|
|
|
|
|
|
|
|
dest[0] = ((src_walker[0] * opposite_x +
|
|
|
|
pixel_next[0] * distance_from_true_x) * opposite_y +
|
|
|
|
(pixel_below[0] * opposite_x +
|
|
|
|
pixel_below_next[0] *distance_from_true_x) * distance_from_true_y
|
|
|
|
) >> recovery_bits;
|
|
|
|
|
|
|
|
dest[1] = ((src_walker[1] * opposite_x +
|
|
|
|
pixel_next[1] * distance_from_true_x) * opposite_y +
|
|
|
|
(pixel_below[1] * opposite_x +
|
|
|
|
pixel_below_next[1] *distance_from_true_x) * distance_from_true_y
|
|
|
|
) >> recovery_bits;
|
|
|
|
|
|
|
|
dest[2] = ((src_walker[2] * opposite_x +
|
|
|
|
pixel_next[2] * distance_from_true_x) * opposite_y +
|
|
|
|
(pixel_below[2] * opposite_x +
|
|
|
|
pixel_below_next[2] *distance_from_true_x) * distance_from_true_y
|
|
|
|
) >> recovery_bits;
|
|
|
|
}
|
2009-09-26 03:41:21 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
src_space_cur_pos_x_i += src_walk_ux_i;
|
|
|
|
src_space_cur_pos_y_i += src_walk_uy_i;
|
2009-09-26 03:41:21 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
dest += 3;
|
|
|
|
} /* end for x */
|
2009-02-28 21:06:44 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
src_space_row_start_x_i += src_walk_vx_i;
|
|
|
|
src_space_row_start_y_i += src_walk_vy_i;
|
2009-02-28 21:06:44 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
dest += 3 * (dest_width - area->width);
|
|
|
|
} /* end for y */
|
|
|
|
});
|
2007-03-12 17:33:57 +08:00
|
|
|
|
2017-04-08 16:36:47 +08:00
|
|
|
gimp_brush_transform_blur (result, blur_radius);
|
2010-01-01 21:41:07 +08:00
|
|
|
|
2009-02-06 05:47:57 +08:00
|
|
|
return result;
|
2007-03-12 17:33:57 +08:00
|
|
|
}
|
|
|
|
|
2010-03-18 01:48:12 +08:00
|
|
|
void
|
|
|
|
gimp_brush_transform_matrix (gdouble width,
|
|
|
|
gdouble height,
|
2010-05-10 05:11:07 +08:00
|
|
|
gdouble scale,
|
|
|
|
gdouble aspect_ratio,
|
2009-02-06 05:47:57 +08:00
|
|
|
gdouble angle,
|
2017-12-23 05:53:56 +08:00
|
|
|
gboolean reflect,
|
2009-02-06 05:47:57 +08:00
|
|
|
GimpMatrix3 *matrix)
|
1999-08-22 04:33:13 +08:00
|
|
|
{
|
2010-03-18 01:48:12 +08:00
|
|
|
const gdouble center_x = width / 2;
|
|
|
|
const gdouble center_y = height / 2;
|
2014-10-12 07:16:32 +08:00
|
|
|
gdouble scale_x = scale;
|
|
|
|
gdouble scale_y = scale;
|
2010-05-10 05:11:07 +08:00
|
|
|
|
2011-01-26 05:27:23 +08:00
|
|
|
if (aspect_ratio < 0.0)
|
2010-05-10 05:11:07 +08:00
|
|
|
{
|
2011-02-02 06:58:09 +08:00
|
|
|
scale_x = scale * (1.0 - (fabs (aspect_ratio) / 20.0));
|
|
|
|
scale_y = scale;
|
2010-05-10 05:11:07 +08:00
|
|
|
}
|
2011-01-26 05:27:23 +08:00
|
|
|
else if (aspect_ratio > 0.0)
|
2010-05-10 05:11:07 +08:00
|
|
|
{
|
|
|
|
scale_x = scale;
|
2011-01-26 05:27:23 +08:00
|
|
|
scale_y = scale * (1.0 - (aspect_ratio / 20.0));
|
2010-05-10 05:11:07 +08:00
|
|
|
}
|
2009-02-06 05:47:57 +08:00
|
|
|
|
|
|
|
gimp_matrix3_identity (matrix);
|
2009-02-15 06:00:48 +08:00
|
|
|
gimp_matrix3_scale (matrix, scale_x, scale_y);
|
2010-05-10 05:11:07 +08:00
|
|
|
gimp_matrix3_translate (matrix, - center_x * scale_x, - center_y * scale_y);
|
|
|
|
gimp_matrix3_rotate (matrix, -2 * G_PI * angle);
|
2017-12-23 05:53:56 +08:00
|
|
|
if (reflect)
|
|
|
|
gimp_matrix3_scale (matrix, -1.0, 1.0);
|
2010-05-10 05:11:07 +08:00
|
|
|
gimp_matrix3_translate (matrix, center_x * scale_x, center_y * scale_y);
|
2009-02-06 05:47:57 +08:00
|
|
|
}
|
2007-03-14 15:29:43 +08:00
|
|
|
|
2010-03-18 01:48:12 +08:00
|
|
|
/* private functions */
|
|
|
|
|
2009-02-06 05:47:57 +08:00
|
|
|
static void
|
2014-10-12 07:16:32 +08:00
|
|
|
gimp_brush_transform_bounding_box (GimpBrush *brush,
|
2009-02-06 05:47:57 +08:00
|
|
|
const GimpMatrix3 *matrix,
|
|
|
|
gint *x,
|
|
|
|
gint *y,
|
|
|
|
gint *width,
|
|
|
|
gint *height)
|
|
|
|
{
|
2014-10-12 07:16:32 +08:00
|
|
|
const gdouble w = gimp_brush_get_width (brush);
|
|
|
|
const gdouble h = gimp_brush_get_height (brush);
|
2009-02-06 05:47:57 +08:00
|
|
|
gdouble x1, x2, x3, x4;
|
|
|
|
gdouble y1, y2, y3, y4;
|
2010-04-08 05:29:09 +08:00
|
|
|
gdouble temp_x;
|
|
|
|
gdouble temp_y;
|
2007-03-12 17:33:57 +08:00
|
|
|
|
2009-02-06 05:47:57 +08:00
|
|
|
gimp_matrix3_transform_point (matrix, 0, 0, &x1, &y1);
|
|
|
|
gimp_matrix3_transform_point (matrix, w, 0, &x2, &y2);
|
|
|
|
gimp_matrix3_transform_point (matrix, 0, h, &x3, &y3);
|
|
|
|
gimp_matrix3_transform_point (matrix, w, h, &x4, &y4);
|
2007-03-14 15:29:43 +08:00
|
|
|
|
2010-04-08 05:29:09 +08:00
|
|
|
temp_x = MIN (MIN (x1, x2), MIN (x3, x4));
|
|
|
|
temp_y = MIN (MIN (y1, y2), MIN (y3, y4));
|
2007-03-12 17:33:57 +08:00
|
|
|
|
2010-04-08 05:29:09 +08:00
|
|
|
*width = (gint) ceil (MAX (MAX (x1, x2), MAX (x3, x4)) - temp_x);
|
|
|
|
*height = (gint) ceil (MAX (MAX (y1, y2), MAX (y3, y4)) - temp_y);
|
|
|
|
|
|
|
|
*x = floor (temp_x);
|
|
|
|
*y = floor (temp_y);
|
2009-10-11 18:43:28 +08:00
|
|
|
|
2009-10-12 19:00:16 +08:00
|
|
|
/* Transform size can not be less than 1 px */
|
2011-04-09 03:18:39 +08:00
|
|
|
*width = MAX (1, *width);
|
|
|
|
*height = MAX (1, *height);
|
1999-08-22 04:33:13 +08:00
|
|
|
}
|
2010-01-01 21:41:07 +08:00
|
|
|
|
2017-04-08 16:30:35 +08:00
|
|
|
/* Blurs the brush mask/pixmap, in place, using a convolution of the form:
|
2017-04-07 22:14:05 +08:00
|
|
|
*
|
|
|
|
* 12 11 10 9 8
|
|
|
|
* 7 6 5 4 3
|
|
|
|
* 2 1 0 1 2
|
|
|
|
* 3 4 5 6 7
|
|
|
|
* 8 9 10 11 12
|
|
|
|
*
|
|
|
|
* (i.e., an array, wrapped into a matrix, whose i-th element is
|
|
|
|
* `abs (i - a / 2)`, where `a` is the length of the array.) `r` specifies the
|
|
|
|
* convolution kernel's radius.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
gimp_brush_transform_blur (GimpTempBuf *buf,
|
|
|
|
gint r)
|
2010-01-01 21:41:07 +08:00
|
|
|
{
|
2017-04-07 22:14:05 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
gint sum;
|
|
|
|
gint weighted_sum;
|
|
|
|
gint middle_sum;
|
|
|
|
} Sums;
|
|
|
|
|
2017-04-08 16:30:35 +08:00
|
|
|
const Babl *format = gimp_temp_buf_get_format (buf);
|
|
|
|
gint components = babl_format_get_n_components (format);
|
|
|
|
gint components_r = components * r;
|
|
|
|
gint width = gimp_temp_buf_get_width (buf);
|
|
|
|
gint height = gimp_temp_buf_get_height (buf);
|
|
|
|
gint stride = components * width;
|
|
|
|
gint stride_r = stride * r;
|
|
|
|
guchar *data = gimp_temp_buf_get_data (buf);
|
|
|
|
gint rw = MIN (r, width - 1);
|
|
|
|
gint rh = MIN (r, height - 1);
|
|
|
|
gfloat n = 2 * r + 1;
|
|
|
|
gfloat n_r = n * r;
|
2017-04-08 19:37:28 +08:00
|
|
|
gfloat weight = floor (n * n / 2) * (floor (n * n / 2) + 1);
|
2017-04-08 16:30:35 +08:00
|
|
|
gfloat weight_inv = 1 / weight;
|
2017-04-07 22:14:05 +08:00
|
|
|
Sums *sums;
|
|
|
|
|
|
|
|
if (rw <= 0 || rh <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sums = g_new (Sums, width * height * components);
|
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
gimp_parallel_distribute_range (height, MIN_PARALLEL_SUB_SIZE,
|
|
|
|
[=] (gint y0, gint height)
|
2010-01-01 21:41:07 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
gint x;
|
|
|
|
gint y;
|
|
|
|
gint c;
|
|
|
|
const guchar *d;
|
|
|
|
Sums *s;
|
2017-04-08 16:30:35 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
d = data + y0 * stride;
|
|
|
|
s = sums + y0 * stride;
|
2011-04-09 03:18:39 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
for (y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
const guchar *p;
|
2010-01-01 21:41:07 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
gint sum;
|
|
|
|
gint weighted_sum;
|
|
|
|
gint leading_sum;
|
|
|
|
gint leading_weighted_sum;
|
|
|
|
} acc[components];
|
2017-04-08 16:30:35 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
memset (acc, 0, sizeof (acc));
|
|
|
|
|
|
|
|
p = d;
|
|
|
|
|
|
|
|
for (x = 0; x <= rw; x++)
|
2017-04-07 22:14:05 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
for (c = 0; c < components; c++)
|
|
|
|
{
|
|
|
|
acc[c].sum += *p;
|
|
|
|
acc[c].weighted_sum += -x * *p;
|
2017-04-08 16:30:35 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
p++;
|
|
|
|
}
|
2017-04-07 22:14:05 +08:00
|
|
|
}
|
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
for (x = 0; x < width; x++)
|
2017-04-07 22:14:05 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
for (c = 0; c < components; c++)
|
2017-04-07 22:14:05 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
if (x > 0)
|
2017-04-07 22:14:05 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
acc[c].weighted_sum += acc[c].sum;
|
|
|
|
acc[c].leading_weighted_sum += acc[c].leading_sum;
|
|
|
|
|
|
|
|
if (x < width - r)
|
|
|
|
{
|
|
|
|
acc[c].sum += d[components_r];
|
|
|
|
acc[c].weighted_sum += -r * d[components_r];
|
|
|
|
}
|
2017-04-07 22:14:05 +08:00
|
|
|
}
|
2010-01-01 21:41:07 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
acc[c].leading_sum += d[0];
|
2017-04-07 22:14:05 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
s->sum = acc[c].sum;
|
|
|
|
s->weighted_sum = acc[c].weighted_sum;
|
|
|
|
s->middle_sum = 2 * acc[c].leading_weighted_sum -
|
|
|
|
acc[c].weighted_sum;
|
2017-04-07 22:14:05 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
if (x >= r)
|
|
|
|
{
|
|
|
|
acc[c].sum -= d[-components_r];
|
|
|
|
acc[c].weighted_sum -= r * d[-components_r];
|
|
|
|
acc[c].leading_sum -= d[-components_r];
|
|
|
|
acc[c].leading_weighted_sum -= r * d[-components_r];
|
|
|
|
}
|
2017-04-07 22:14:05 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
d++;
|
|
|
|
s++;
|
|
|
|
}
|
2017-04-07 22:14:05 +08:00
|
|
|
}
|
|
|
|
}
|
2018-04-05 04:57:35 +08:00
|
|
|
});
|
2017-04-07 22:14:05 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
gimp_parallel_distribute_range (width, MIN_PARALLEL_SUB_SIZE,
|
|
|
|
[=] (gint x0, gint width)
|
2010-01-01 21:41:07 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
gint x;
|
|
|
|
gint y;
|
|
|
|
gint c;
|
|
|
|
guchar *d0;
|
|
|
|
const Sums *s0;
|
|
|
|
guchar *d;
|
|
|
|
const Sums *s;
|
2017-04-08 16:30:35 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
d0 = data + x0 * components;
|
|
|
|
s0 = sums + x0 * components;
|
2017-04-07 22:14:05 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
const Sums *p;
|
|
|
|
gfloat n_y;
|
2017-04-07 22:14:05 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
gfloat weighted_sum;
|
|
|
|
gint leading_sum;
|
|
|
|
gint trailing_sum;
|
|
|
|
} acc[components];
|
2017-04-07 22:14:05 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
memset (acc, 0, sizeof (acc));
|
2017-04-08 16:30:35 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
d = d0 + components * x;
|
|
|
|
s = s0 + components * x;
|
|
|
|
|
|
|
|
p = s + stride;
|
|
|
|
|
|
|
|
for (y = 1, n_y = n; y <= rh; y++, n_y += n)
|
2017-04-07 22:14:05 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
for (c = 0; c < components; c++)
|
|
|
|
{
|
|
|
|
acc[c].weighted_sum += n_y * p->sum - p->weighted_sum;
|
|
|
|
acc[c].trailing_sum += p->sum;
|
2017-04-08 16:30:35 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
p++;
|
|
|
|
}
|
2017-04-08 16:30:35 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
p += stride - components;
|
|
|
|
}
|
2017-04-07 22:14:05 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
for (y = 0; y < height; y++)
|
2017-04-07 22:14:05 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
for (c = 0; c < components; c++)
|
2017-04-07 22:14:05 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
if (y > 0)
|
2017-04-07 22:14:05 +08:00
|
|
|
{
|
2018-04-05 04:57:35 +08:00
|
|
|
acc[c].weighted_sum += s->weighted_sum +
|
|
|
|
n * (acc[c].leading_sum -
|
|
|
|
acc[c].trailing_sum);
|
|
|
|
acc[c].trailing_sum -= s->sum;
|
|
|
|
|
|
|
|
if (y < height - r)
|
|
|
|
{
|
|
|
|
acc[c].weighted_sum += n_r * s[stride_r].sum -
|
|
|
|
s[stride_r].weighted_sum;
|
|
|
|
acc[c].trailing_sum += s[stride_r].sum;
|
|
|
|
}
|
2017-04-07 22:14:05 +08:00
|
|
|
}
|
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
acc[c].leading_sum += s->sum;
|
2017-04-07 22:14:05 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
*d = (acc[c].weighted_sum + s->middle_sum) * weight_inv + 0.5f;
|
2017-04-07 22:14:05 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
acc[c].weighted_sum += s->weighted_sum;
|
2017-04-07 22:14:05 +08:00
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
if (y >= r)
|
|
|
|
{
|
|
|
|
acc[c].weighted_sum -= n_r * s[-stride_r].sum +
|
|
|
|
s[-stride_r].weighted_sum;
|
|
|
|
acc[c].leading_sum -= s[-stride_r].sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
d++;
|
|
|
|
s++;
|
2017-04-07 22:14:05 +08:00
|
|
|
}
|
|
|
|
|
2018-04-05 04:57:35 +08:00
|
|
|
d += stride - components;
|
|
|
|
s += stride - components;
|
2017-04-07 22:14:05 +08:00
|
|
|
}
|
|
|
|
}
|
2018-04-05 04:57:35 +08:00
|
|
|
});
|
2017-04-07 22:14:05 +08:00
|
|
|
|
|
|
|
g_free (sums);
|
2010-01-01 21:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
2017-04-08 16:36:47 +08:00
|
|
|
gimp_brush_transform_blur_radius (gint height,
|
|
|
|
gint width,
|
|
|
|
gdouble hardness)
|
2010-01-01 21:41:07 +08:00
|
|
|
{
|
2017-04-08 16:36:47 +08:00
|
|
|
return floor ((1.0 - hardness) * (sqrt (0.5) - 0.5) * MIN (width, height));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_brush_transform_adjust_hardness_matrix (gdouble width,
|
|
|
|
gdouble height,
|
|
|
|
gdouble blur_radius,
|
|
|
|
GimpMatrix3 *matrix)
|
|
|
|
{
|
|
|
|
gdouble scale;
|
|
|
|
|
|
|
|
if (blur_radius == 0.0)
|
|
|
|
return;
|
2011-04-09 03:18:39 +08:00
|
|
|
|
2017-04-08 16:36:47 +08:00
|
|
|
scale = (MIN (width, height) - 2.0 * blur_radius) / MIN (width, height);
|
2010-03-18 01:48:12 +08:00
|
|
|
|
2017-04-08 16:36:47 +08:00
|
|
|
gimp_matrix3_scale (matrix, scale, scale);
|
2017-04-08 18:47:38 +08:00
|
|
|
gimp_matrix3_translate (matrix,
|
|
|
|
(1.0 - scale) * width / 2.0,
|
|
|
|
(1.0 - scale) * height / 2.0);
|
2010-01-06 04:19:12 +08:00
|
|
|
}
|
2018-04-05 04:53:00 +08:00
|
|
|
|
|
|
|
} /* extern "C" */
|