mosaic.c 75.1 KB
Newer Older
Téo Mazars's avatar
Téo Mazars committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* This file is an image processing operation for GEGL
 *
 * GEGL is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * GEGL 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 1996 Spencer Kimball
 * Copyright 1997 Elliot Lee
 * Copyright 2013 Téo Mazars <teo.mazars@ensimag.fr>
 */

/*  Mosaic is a filter which transforms an image into
 *  what appears to be a mosaic, composed of small primitives,
 *  each of constant color and of an approximate size.
 */

#include "config.h"
#include <glib/gi18n-lib.h>

29 30
#ifdef GEGL_PROPERTIES

31
enum_start (gegl_mosaic_tile)
32 33 34 35
  enum_value (GEGL_MOSAIC_TILE_SQUARES,   "squares",   N_("Squares"))
  enum_value (GEGL_MOSAIC_TILE_HEXAGONS,  "hexagons",  N_("Hexagons"))
  enum_value (GEGL_MOSAIC_TILE_OCTAGONS,  "octagons",  N_("Octagons"))
  enum_value (GEGL_MOSAIC_TILE_TRIANGLES, "triangles", N_("Triangles"))
36
enum_end (GeglMosaicTile)
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
property_enum (tile_type, _("Tile geometry"),
    GeglMosaicTile, gegl_mosaic_tile, GEGL_MOSAIC_TILE_HEXAGONS)
    description (_("What shape to use for tiles"))

property_double (tile_size, _("Tile size"), 15.0)
    description (_("Average diameter of each tile (in pixels)"))
    value_range (1.0, 1000.0)
    ui_range    (5.0, 400.0)
    ui_meta     ("unit", "pixel-distance")

property_double (tile_height, _("Tile height"), 4.0)
    description (_("Apparent height of each tile (in pixels)"))
    value_range (1.0, 1000.0)
    ui_range    (1.0, 20.0)

property_double (tile_neatness, _("Tile neatness"), 0.65)
    description (_("Deviation from perfectly formed tiles"))
    value_range (0.0, 1.0)

property_double (color_variation, _("Tile color variation"), 0.2)
    description (("Magnitude of random color variations"))
    value_range (0.0, 1.0)

property_boolean (color_averaging, _("Color averaging"), TRUE)
    description (_("Tile color based on average of subsumed pixels"))

property_boolean (tile_surface, _("Rough tile surface"), FALSE)
    description (_("Surface characteristics"))

property_boolean (tile_allow_split, _("Allow splitting tiles"), TRUE)
    description (_("Allows splitting tiles at hard edges"))

property_double (tile_spacing, _("Tile spacing"), 1.0)
    description (_("Inter-tile spacing (in pixels)"))
    value_range (0.0, 1000.0)
    ui_range    (0.5, 30.0)
    ui_meta     ("unit", "pixel-distance")

property_color (joints_color, _("Joints color"), "black")

property_color (light_color, _("Light color"), "white")

property_double (light_dir, _("Light direction"), 135.0)
    description (("Direction of light-source (in degrees)"))
    value_range (0.0, 360.0)
    ui_meta     ("unit", "degree")

property_boolean (antialiasing, _("Antialiasing"), TRUE)
    description  (_("Enables smoother tile output"))

property_seed (seed, _("Random seed"), rand)
89

Téo Mazars's avatar
Téo Mazars committed
90 91
#else

92 93
#define GEGL_OP_AREA_FILTER
#define GEGL_OP_C_FILE "mosaic.c"
Téo Mazars's avatar
Téo Mazars committed
94

95
#include "gegl-op.h"
Téo Mazars's avatar
Téo Mazars committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#include <math.h>

#define SUPERSAMPLE       3
#define MAG_THRESHOLD     (7.5/255.0)
#define COUNT_THRESHOLD   0.1
#define MAX_POINTS        12
#define NB_CPN            4
#define STD_DEV           1.0

typedef enum
{
  SQUARES   = 0,
  HEXAGONS  = 1,
  OCTAGONS  = 2,
  TRIANGLES = 3
} TileType;

#define SMOOTH   FALSE
#define ROUGH    TRUE

typedef struct
{
  gdouble x, y;
} Vertex;

typedef struct
{
  guint  npts;
  Vertex pts[MAX_POINTS];
} Polygon;

typedef struct
{
  gdouble base_x, base_y;
130
  gdouble base_x2, base_y2;
Téo Mazars's avatar
Téo Mazars committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  gdouble norm_x, norm_y;
  gdouble light;
} SpecVec;

typedef struct
{
  Vertex *vert, *vert_o;
  gint    vert_rows;
  gint    vert_cols;
  gint    row_pad;
  gint    col_pad;
  gint    vert_multiple;
  gint    vert_rowstride;
} GridDescriptor;

typedef struct
{
  gdouble        light_x;
  gdouble        light_y;
  gdouble        scale;
  gfloat        *h_grad;
  gfloat        *v_grad;
  gfloat        *m_grad;
  GridDescriptor grid;
  gfloat         back[4];
  gfloat         fore[4];
  gint           width, height;
} MosaicDatas;


/* Declare local functions.
 */

static gfloat*   mosaic                (GeglOperation       *operation,
                                        GeglBuffer          *drawable,
                                        const GeglRectangle *result);

/*  gradient finding machinery  */
static void      find_gradients        (gfloat              *input_buf,
                                        gdouble              std_dev,
                                        const GeglRectangle *result,
                                        MosaicDatas         *mdatas);
static void      find_max_gradient     (gfloat              *src_rgn,
                                        gfloat              *dest_rgn,
                                        gint                 width,
                                        gint                 height);

/*  gaussian & 1st derivative  */
static void      gaussian_deriv        (gfloat              *src_rgn,
                                        gfloat              *dest_rgn,
181
                                        GeglOrientation      direction,
Téo Mazars's avatar
Téo Mazars committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
                                        gdouble              std_dev,
                                        const GeglRectangle *result);
static void      make_curve            (gfloat              *curve,
                                        gfloat              *sum,
                                        gdouble              std_dev,
                                        gint                 length);
static void      make_curve_d          (gfloat              *curve,
                                        gfloat              *sum,
                                        gdouble              std_dev,
                                        gint                 length);

/*  grid creation and localization machinery  */
static void      grid_create_squares   (const GeglRectangle *result,
                                        gdouble              tile_size,
                                        GridDescriptor      *grid);
static void      grid_create_hexagons  (const GeglRectangle *result,
                                        gdouble              tile_size,
                                        GridDescriptor      *grid);
static void      grid_create_octagons  (const GeglRectangle *result,
                                        gdouble              tile_size,
                                        GridDescriptor      *grid);
static void      grid_create_triangles (const GeglRectangle *result,
                                        gdouble              tile_size,
                                        GridDescriptor      *grid);
static void      grid_localize         (const GeglRectangle *result,
207
                                        GeglProperties          *o,
Téo Mazars's avatar
Téo Mazars committed
208 209 210
                                        MosaicDatas         *mdatas);
static gfloat*   grid_render           (gfloat              *input_buf,
                                        const GeglRectangle *result,
211
                                        GeglProperties          *o,
Téo Mazars's avatar
Téo Mazars committed
212 213 214 215 216 217 218
                                        MosaicDatas         *mdatas);
static void      split_poly            (Polygon             *poly,
                                        gfloat              *input_buf,
                                        gfloat              *output_buf,
                                        gdouble             *dir,
                                        gdouble              color_vary,
                                        const GeglRectangle *result,
219
                                        GeglProperties          *o,
Téo Mazars's avatar
Téo Mazars committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
                                        MosaicDatas         *mdatas);
static void      clip_poly             (gdouble             *vec,
                                        gdouble             *pt,
                                        Polygon             *poly,
                                        Polygon             *new_poly);
static void      clip_point            (gdouble             *dir,
                                        gdouble             *pt,
                                        gdouble              x1,
                                        gdouble              y1,
                                        gdouble              x2,
                                        gdouble              y2,
                                        Polygon             *poly);
static void      process_poly          (Polygon             *poly,
                                        gboolean             allow_split,
                                        gfloat              *input_buf,
                                        gfloat              *output_buf,
                                        const GeglRectangle *result,
237
                                        GeglProperties          *o,
Téo Mazars's avatar
Téo Mazars committed
238 239 240 241 242 243
                                        MosaicDatas         *mdatas);
static void      render_poly           (Polygon             *poly,
                                        gfloat              *input_buf,
                                        gfloat              *output_buf,
                                        gdouble              vary,
                                        const GeglRectangle *result,
244
                                        GeglProperties          *o,
Téo Mazars's avatar
Téo Mazars committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
                                        MosaicDatas         *mdatas);
static void      find_poly_dir         (Polygon             *poly,
                                        gfloat              *m_gr,
                                        gfloat              *h_gr,
                                        gfloat              *v_gr,
                                        gdouble             *dir,
                                        gdouble             *loc,
                                        const GeglRectangle *result);
static void      find_poly_color       (Polygon             *poly,
                                        gfloat              *input_buf,
                                        gfloat              *col,
                                        double               vary,
                                        const GeglRectangle *result);
static void      scale_poly            (Polygon             *poly,
                                        gdouble              cx,
                                        gdouble              cy,
                                        gdouble              scale);
static void      fill_poly_color       (Polygon             *poly,
                                        gfloat              *input_buf,
                                        gfloat              *output_buf,
                                        gfloat              *col,
                                        const GeglRectangle *result,
                                        gboolean             antialiasing,
                                        gboolean             tile_rough,
                                        gdouble              tile_height,
                                        MosaicDatas         *mdatas);
static void      fill_poly_image       (Polygon             *poly,
                                        gfloat              *input_buf,
                                        gfloat              *output_buf,
                                        gdouble             vary,
                                        const GeglRectangle *result,
                                        gboolean             antialiasing,
                                        gboolean             tile_rough,
                                        gdouble              tile_height,
                                        MosaicDatas         *mdatas);
static void      calc_spec_vec         (SpecVec             *vec,
                                        gint                 xs,
                                        gint                 ys,
                                        gint                 xe,
                                        gint                 ye,
                                        gdouble              light_x,
                                        gdouble              light_y);
static gdouble   calc_spec_contrib     (SpecVec             *vec,
                                        gint                 n,
                                        gdouble              x,
                                        gdouble              y,
                                        gboolean             tile_rough,
                                        gdouble              tile_height);
/*  Polygon machinery  */
static void      convert_segment       (gint                 x1,
                                        gint                 y1,
                                        gint                 x2,
                                        gint                 y2,
                                        gint                 offset,
                                        gint                *min,
                                        gint                *max);
static void      polygon_add_point     (Polygon             *poly,
                                        gdouble              x,
                                        gdouble              y);
static gboolean  polygon_find_center   (Polygon             *poly,
                                        gdouble             *x,
                                        gdouble             *y);
static void      polygon_translate     (Polygon             *poly,
                                        gdouble              tx,
                                        gdouble              ty);
static void      polygon_scale         (Polygon             *poly,
                                        gdouble              scale);
static gboolean  polygon_extents       (Polygon             *poly,
                                        gdouble             *min_x,
                                        gdouble             *min_y,
                                        gdouble             *max_x,
                                        gdouble             *max_y);
static void      polygon_reset         (Polygon             *poly);
318
static gfloat    rand_f                (GeglRandom          *rand,
Téo Mazars's avatar
Téo Mazars committed
319 320 321 322
                                        gfloat               pos_x,
                                        gfloat               pos_y,
                                        gfloat               min,
                                        gfloat               max);
323
static gint      rand_i                (GeglRandom          *rand,
Téo Mazars's avatar
Téo Mazars committed
324 325 326 327
                                        gfloat               pos_x,
                                        gfloat               pos_y,
                                        gint                 min,
                                        gint                 max);
328 329 330
static gfloat    distance              (SpecVec             *vec,
                                        gfloat               x,
                                        gfloat               y);
Téo Mazars's avatar
Téo Mazars committed
331 332 333 334 335 336 337 338

#define ROUND(x)   ((gint) ((((x) < 0) ? (x) - 0.5 : (x) + 0.5)))

#define CLAMP01(v) ((v) < 0.0 ? 0.0 : ((v) > 1.0 ? 1.0 : (v)))

#define SQR(v)     ((v)*(v))

static gfloat
339 340 341 342 343
rand_f (GeglRandom *rand,
        gfloat      pos_x,
        gfloat      pos_y,
        gfloat      min,
        gfloat      max)
Téo Mazars's avatar
Téo Mazars committed
344
{
345
  return gegl_random_float_range (rand, ROUND (pos_x), ROUND (pos_y),
Téo Mazars's avatar
Téo Mazars committed
346 347 348 349
                                  0, 0, min, max);
}

static gint
350 351 352 353 354
rand_i (GeglRandom *rand,
        gfloat      pos_x,
        gfloat      pos_y,
        gint        min,
        gint        max)
Téo Mazars's avatar
Téo Mazars committed
355
{
356
  return gegl_random_int_range (rand, ROUND (pos_x), ROUND (pos_y),
Téo Mazars's avatar
Téo Mazars committed
357 358 359 360 361 362 363 364
                                0, 0, min, max);
}

static gfloat*
mosaic (GeglOperation       *operation,
        GeglBuffer          *drawable,
        const GeglRectangle *result)
{
365
  GeglProperties          *o;
Téo Mazars's avatar
Téo Mazars committed
366 367 368 369 370
  MosaicDatas          mdatas;
  gfloat              *rendered;
  const GeglRectangle *whole_region;
  gfloat              *input_buf;

371
  o = GEGL_PROPERTIES (operation);
Téo Mazars's avatar
Téo Mazars committed
372 373 374 375 376 377 378 379 380

  input_buf = g_new (gfloat, NB_CPN * result->width * result->height);

  gegl_buffer_get (drawable, result,
                   1.0, babl_format ("R'G'B'A float"), input_buf,
                   GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);

  {
    gdouble r, g, b, a;
381
    gegl_color_get_rgba (o->light_color, &r, &g, &b, &a);
Téo Mazars's avatar
Téo Mazars committed
382 383 384 385 386
    mdatas.fore[0] = r;
    mdatas.fore[1] = g;
    mdatas.fore[2] = b;
    mdatas.fore[3] = a;

387
    gegl_color_get_rgba (o->joints_color, &r, &g, &b, &a);
Téo Mazars's avatar
Téo Mazars committed
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
    mdatas.back[0] = r;
    mdatas.back[1] = g;
    mdatas.back[2] = b;
    mdatas.back[3] = a;
  }

  whole_region = gegl_operation_source_get_bounding_box (operation, "input");

  mdatas.width  = whole_region->width;
  mdatas.height = whole_region->height;

  /*  Find the gradients  */
  find_gradients (input_buf, STD_DEV, result, &mdatas);

  /*  Create the tile geometry grid  */
  switch (o->tile_type)
    {
    case SQUARES:
      grid_create_squares (result, o->tile_size, &(mdatas.grid));
      break;
    case HEXAGONS:
      grid_create_hexagons (result, o->tile_size, &(mdatas.grid));
      break;
    case OCTAGONS:
      grid_create_octagons (result, o->tile_size, &(mdatas.grid));
      break;
    case TRIANGLES:
      grid_create_triangles (result, o->tile_size, &(mdatas.grid));
      break;
    default:
      break;
    }

  /*  Deform the tiles based on image content  */
  grid_localize (result, o, &mdatas);

  mdatas.light_x = -cos (o->light_dir * G_PI / 180.0);
  mdatas.light_y =  sin (o->light_dir * G_PI / 180.0);
  mdatas.scale = (o->tile_spacing > o->tile_size / 2.0) ?
    0.5 : 1.0 - o->tile_spacing / o->tile_size;

  /*  Render the tiles  */
  rendered = grid_render (input_buf, result, o, &mdatas);

  g_free (mdatas.h_grad);
  g_free (mdatas.v_grad);
  g_free (mdatas.m_grad);
  g_free (mdatas.grid.vert_o);
  g_free (input_buf);

  return rendered;
}


/*
 *  Gradient finding machinery
 */

static void
find_gradients (gfloat              *input_buf,
                gdouble              std_dev,
                const GeglRectangle *result,
                MosaicDatas         *mdatas)
{
  gfloat *dest_rgn;
  gint    i, j;
  gfloat *gr, *dh, *dv;
  gfloat  hmax, vmax;

  /*  allocate the gradient maps  */
  mdatas->h_grad = g_new (gfloat, result->width * result->height);
  mdatas->v_grad = g_new (gfloat, result->width * result->height);
  mdatas->m_grad = g_new (gfloat, result->width * result->height);

  dest_rgn = g_new (gfloat, result->width * result->height * NB_CPN);

464
  gaussian_deriv (input_buf, dest_rgn, GEGL_ORIENTATION_HORIZONTAL, std_dev, result);
Téo Mazars's avatar
Téo Mazars committed
465 466 467

  find_max_gradient (dest_rgn, mdatas->h_grad, result->width, result->height);

468
  gaussian_deriv (input_buf, dest_rgn, GEGL_ORIENTATION_VERTICAL, std_dev, result);
Téo Mazars's avatar
Téo Mazars committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533

  find_max_gradient (dest_rgn, mdatas->v_grad, result->width, result->height);

  /*  fill in the gradient map  */
  gr = mdatas->m_grad;
  dh = mdatas->h_grad;
  dv = mdatas->v_grad;

  for (i = 0; i < result->height; i++)
    {
      for (j = 0; j < result->width; j++, dh++, dv++, gr++)
        {
          /*  Find the gradient  */
          if (!j || !i || (j == result->width - 1) || (i == result->height - 1))
            {
              *gr = MAG_THRESHOLD;
            }
          else
            {
              hmax = *dh - 0.5;
              vmax = *dv - 0.5;

              *gr = (gfloat) sqrt (SQR (hmax) + SQR (vmax));
            }
        }
    }

  g_free (dest_rgn);
}

static void
find_max_gradient (gfloat *src_rgn,
                   gfloat *dest_rgn,
                   gint    width,
                   gint    height)
{
  gfloat *s, *d;
  gint    i, j, k;
  gfloat  val;
  gfloat  max;

  s = src_rgn;
  d = dest_rgn;

  for (i = 0; i < height; i++)
    {
      for (j = 0; j < width; j++)
        {
          max = 0.5;
          for (k = 0; k < NB_CPN; k++)
            {
              val = *s;
              if (fabs (val - 0.5) > fabs (max - 0.5))
                max = val;
            }

          *d++ = max;
          s += NB_CPN;
        }
    }
}

static void
gaussian_deriv (gfloat              *src_rgn,
                gfloat              *dest_rgn,
534
                GeglOrientation      direction,
Téo Mazars's avatar
Téo Mazars committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
                gdouble              std_dev,
                const GeglRectangle *result)
{
  gfloat *dp;
  gfloat *sp, *s;
  gfloat *data;
  gfloat *buf, *b;
  gint    chan;
  gint    i, row, col;
  gint    start, end;
  gfloat  curve_array [9];
  gfloat  sum_array [9];
  gfloat *curve;
  gfloat *sum;
  gfloat  val;
  gfloat  total;
  gint    length;
  gfloat  initial_p[4], initial_m[4];
  gint    width = result->width, height = result->height;

  /*  allocate buffers for rows/cols  */
  length = MAX (result->width, result->height) * NB_CPN;
  data = g_new (gfloat, length * 2);

  length = 3;

  /*  initialize  */
  curve = curve_array + length;
  sum = sum_array + length;
  buf = g_new (gfloat, MAX (width, height) * NB_CPN);

566
  if (direction == GEGL_ORIENTATION_VERTICAL)
Téo Mazars's avatar
Téo Mazars committed
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
    {
      make_curve_d (curve, sum, std_dev, length);
      total = sum[0] * -2;
    }
  else
    {
      make_curve (curve, sum, std_dev, length);
      total = sum[length] + curve[length];
    }

  for (col = 0; col < width; col++)
    {
      sp = src_rgn + col * NB_CPN;
      dp = dest_rgn + col * NB_CPN;;
      b = buf;

      for (chan = 0; chan < NB_CPN; chan++)
        {
          initial_p[chan] = sp[chan];
          initial_m[chan] = sp[width * (height - 1) * NB_CPN + chan];
        }

      for (row = 0; row < height; row++)
        {
          start = (row < length) ?  - row : -length;
          end = ((height - row - 1) < length) ? (height - row - 1) : length;

          for (chan = 0; chan < NB_CPN; chan++)
            {
              s = sp + width * (start * NB_CPN) + chan;
              val = 0;
              i = start;

              if (start != -length)
                val += initial_p[chan] * (sum[start] - sum[-length]);

              while (i <= end)
                {
                  val += *s * curve[i++];
                  s += NB_CPN * width;
                }

              if (end != length)
                val += initial_m[chan] * (sum[length] + curve[length] - sum[end+1]);

              if ( val == 0)
                *b++ = val;
              else
                *b++ = val / total;
            }

          sp += NB_CPN * width;
        }

      b = buf;
622
      if (direction == GEGL_ORIENTATION_VERTICAL)
Téo Mazars's avatar
Téo Mazars committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
        for (row = 0; row < height; row++)
          {
            for (chan = 0; chan < NB_CPN; chan++)
              {
                b[chan] += 0.5;
                dp[chan] = CLAMP01 (b[chan]);
              }
            b += NB_CPN;
            dp += NB_CPN * width;
          }
      else
        for (row = 0; row < height; row++)
          {
            for (chan = 0; chan < NB_CPN; chan++)
              {
                dp[chan] = CLAMP01 (b[chan]);
              }
            b += NB_CPN;
            dp += NB_CPN * width;
          }
    }

645
  if (direction == GEGL_ORIENTATION_HORIZONTAL)
Téo Mazars's avatar
Téo Mazars committed
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
    {
      make_curve_d (curve, sum, std_dev, length);
      total = sum[0] * -2;
    }
  else
    {
      make_curve (curve, sum, std_dev, length);
      total = sum[length] + curve[length];
    }

  for (row = 0; row < height; row++)
    {
      sp = dest_rgn + width * row * NB_CPN;
      dp = dest_rgn + width * row * NB_CPN;
      b = buf;


      for (chan = 0; chan < NB_CPN; chan++)
        {
          initial_p[chan] = sp[chan];
          initial_m[chan] = sp[(width - 1) * NB_CPN + chan];
        }

      for (col = 0; col < width; col++)
        {
          start = (col < length) ?  - col : -length;
          end = (width - col - 1 < length) ? (width - col - 1) : length;

          for (chan = 0; chan < NB_CPN; chan++)
            {
              s = sp + (start * NB_CPN) + chan;
              val = 0;
              i = start;

              if (start != -length)
                val += initial_p[chan] * (sum[start] - sum[-length]);

              while (i <= end)
                {
                  val += *s * curve[i++];
                  s += NB_CPN;
                }

              if (end != length)
                val += initial_m[chan] * (sum[length] + curve[length] - sum[end+1]);

              if ( val == 0)
                *b++ = val;
              else
                *b++ = val / total;
            }

          sp += NB_CPN;
        }

      b = buf;
702
      if (direction == GEGL_ORIENTATION_HORIZONTAL)
Téo Mazars's avatar
Téo Mazars committed
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
        for (col = 0; col < width; col++)
          {
            for (chan = 0; chan < NB_CPN; chan++)
              {
                b[chan] += 0.5;
                dp[chan] = CLAMP01 (b[chan]);
              }
            b += NB_CPN;
            dp += NB_CPN;
          }
      else
        for (col = 0; col < width; col++)
          {
            for (chan = 0; chan < NB_CPN; chan++)
              {
                dp[chan] = CLAMP01 (b[chan]);
              }
            b += NB_CPN;
            dp += NB_CPN;
          }
    }

  g_free (buf);
  g_free (data);
}

/*
 * The equations: g(r) = exp (- r^2 / (2 * sigma^2))
 *                   r = sqrt (x^2 + y ^2)
 */

static void
make_curve (gfloat  *curve,
            gfloat  *sum,
            gdouble  sigma,
            gint     length)
{
  gdouble sigma2;
  gint    i;

  sigma2 = sigma * sigma;

  curve[0] = 1.0;
  for (i = 1; i <= length; i++)
    {
      curve[i] = (gfloat) (exp (- (i * i) / (2 * sigma2)));
      curve[-i] = curve[i];
    }

  sum[-length] = 0;
  for (i = -length+1; i <= length; i++)
    sum[i] = sum[i-1] + curve[i-1];
}


/*
 * The equations: d_g(r) = -r * exp (- r^2 / (2 * sigma^2)) / sigma^2
 *                   r = sqrt (x^2 + y ^2)
 */

static void
make_curve_d (gfloat  *curve,
              gfloat  *sum,
              gdouble  sigma,
              gint     length)
{
  gdouble sigma2;
  gint    i;

  sigma2 = sigma * sigma;

  curve[0] = 0;
  for (i = 1; i <= length; i++)
    {
      curve[i] = (gfloat) ((i * exp (- (i * i) / (2 * sigma2)) / sigma2));
      curve[-i] = -curve[i];
    }

  sum[-length] = 0;
  sum[0] = 0;
  for (i = 1; i <= length; i++)
    {
      sum[-length + i] = sum[-length + i - 1] + curve[-length + i - 1];
      sum[i] = sum[i - 1] + curve[i - 1];
    }
}


/*********************************************/
/*   Functions for grid manipulation         */
/*********************************************/

static void
grid_create_squares (const GeglRectangle *result,
                     gdouble              tile_size,
                     GridDescriptor      *grid)
{
  gint    rows, cols;
  gint    width, height;
  gdouble pre_cols, pre_rows;
  gdouble offset_x, offset_y;
  gint    i, j;
  gint    size = (gint) tile_size;
  Vertex *pt;

  pre_cols = (result->x / size) * size;
  pre_rows = (result->y / size) * size;

  offset_x = pre_cols - result->x;
  offset_y = pre_rows - result->y;

  width = result->width - offset_x;
  height = result->height - offset_y;

  rows = (height + size - 1) / size;
  cols = (width + size - 1) / size;

  grid->vert_o = grid->vert = g_new (Vertex, (cols + 2) * (rows + 2));
  grid->vert += (cols + 2) + 1;

  for (i = -1; i <= rows; i++)
    for (j = -1; j <= cols; j++)
      {
        pt = grid->vert + (i * (cols + 2) + j);

        pt->x = j * size + size / 2 + offset_x;
        pt->y = i * size + size / 2 + offset_y;
      }

  grid->vert_rows = rows;
  grid->vert_cols = cols;
  grid->row_pad = 1;
  grid->col_pad = 1;
  grid->vert_multiple = 1;
  grid->vert_rowstride = cols + 2;
}

static void
grid_create_hexagons (const GeglRectangle *result,
                      gdouble              tile_size,
                      GridDescriptor      *grid)
{
  gint     rows, cols;
  gint     i, j;
  gint     width, height;
  gdouble  pre_rows, pre_cols;
  gdouble  hex_l1, hex_l2, hex_l3;
  gdouble  hex_width;
  gdouble  hex_height;
  gdouble  offset_x, offset_y;
  Vertex  *pt;
  hex_l1 = tile_size / 2.0;
  hex_l2 = hex_l1 * 2.0 / sqrt (3.0);
  hex_l3 = hex_l1 / sqrt (3.0);
  hex_width = 6 * hex_l1 / sqrt (3.0);
  hex_height = tile_size;

  pre_cols = floor (result->x / hex_width) * hex_width;
  pre_rows = floor (result->y / hex_height) * hex_height;

  offset_x = pre_cols - result->x;
  offset_y = pre_rows - result->y;

  width = result->width - offset_x;
  height = result->height - offset_y;

  rows = ((height + hex_height - 1) / hex_height);
  cols = ((width + hex_width * 2 - 1) / hex_width);

  grid->vert_o = grid->vert = g_new (Vertex, (cols + 2) * 4 * (rows + 2));
  grid->vert += (cols + 2) * 4 + 4;

  for (i = -1; i <= rows; i++)
    for (j = -1; j <= cols; j++)
      {
        pt = grid->vert + (i * (cols + 2) * 4 + j * 4);

        pt[0].x = hex_width * j + hex_l3 + offset_x;
        pt[0].y = hex_height * i + offset_y;
        pt[1].x = pt[0].x + hex_l2;
        pt[1].y = pt[0].y;
        pt[2].x = pt[1].x + hex_l3;
        pt[2].y = pt[1].y + hex_l1;
        pt[3].x = pt[0].x - hex_l3;
        pt[3].y = pt[0].y + hex_l1;
      }

  grid->vert_rows = rows;
  grid->vert_cols = cols;
  grid->row_pad = 1;
  grid->col_pad = 1;
  grid->vert_multiple = 4;
  grid->vert_rowstride = (cols + 2) * 4;
}


static void
grid_create_octagons (const GeglRectangle *result,
                      gdouble              tile_size,
                      GridDescriptor      *grid)
{
  gint     rows, cols;
  gint     width, height;
  gdouble  pre_rows, pre_cols;
  gint     i, j;
  gdouble  ts, side, leg;
  gdouble  oct_size;
  Vertex  *pt;
  gdouble  offset_x, offset_y;

  ts = tile_size;
  side = ts / (sqrt (2.0) + 1.0);
  leg = side * sqrt (2.0) * 0.5;
  oct_size = ts + side;

  pre_cols = floor (result->x / oct_size) * oct_size;
  pre_rows = floor (result->y / oct_size) * oct_size;

  offset_x = pre_cols - result->x;
  offset_y = pre_rows - result->y;

  width = result->width - offset_x;
  height = result->height - offset_y;

  rows = ((height + oct_size - 1) / oct_size);
  cols = ((width + oct_size * 2 - 1) / oct_size);

  grid->vert_o = grid->vert = g_new (Vertex, (cols + 2) * 8 * (rows + 2));
  grid->vert += (cols + 2) * 8 + 8;

  for (i = -1; i < rows + 1; i++)
    for (j = -1; j < cols + 1; j++)
      {
        pt = grid->vert + (i * (cols + 2) * 8 + j * 8);

        pt[0].x = oct_size * j + offset_x;
        pt[0].y = oct_size * i + offset_y;
        pt[1].x = pt[0].x + side;
        pt[1].y = pt[0].y;
        pt[2].x = pt[0].x + leg + side;
        pt[2].y = pt[0].y + leg;
        pt[3].x = pt[2].x;
        pt[3].y = pt[0].y + leg + side;
        pt[4].x = pt[1].x;
        pt[4].y = pt[0].y + 2 * leg + side;
        pt[5].x = pt[0].x;
        pt[5].y = pt[4].y;
        pt[6].x = pt[0].x - leg;
        pt[6].y = pt[3].y;
        pt[7].x = pt[6].x;
        pt[7].y = pt[2].y;
      }

  grid->vert_rows = rows;
  grid->vert_cols = cols;
  grid->row_pad = 1;
  grid->col_pad = 1;
  grid->vert_multiple = 8;
  grid->vert_rowstride = (cols + 2) * 8;
}


static void
grid_create_triangles (const GeglRectangle *result,
                       gdouble              tile_size,
                       GridDescriptor      *grid)
{
  gint     rows, cols;
  gdouble  pre_rows, pre_cols;
  gint     width, height;
  gint     i, j;
  gdouble  tri_mid, tri_height;
  Vertex  *pt;
  gdouble  offset_x, offset_y;

  tri_mid    = tile_size / 2.0;              /* cos 60 */
  tri_height = tile_size / 2.0 * sqrt (3.0); /* sin 60 */

  pre_cols = floor (result->x / tile_size) * tile_size;
  pre_rows = floor (result->y / (2 * tri_height)) * (2 * tri_height);

  offset_x = pre_cols - result->x;
  offset_y = pre_rows - result->y;

  width = result->width - offset_x;
  height = result->height - offset_y;

  rows = (height + 2 * tri_height - 1) / (2 * tri_height);
  cols = (width + tile_size - 1) / tile_size;

  grid->vert_o = grid->vert = g_new (Vertex, (cols + 2) * 2 * (rows + 2));
  grid->vert += (cols + 2) * 2 + 2;

  for (i = -1; i <= rows; i++)
    for (j = -1; j <= cols; j++)
      {
        pt = grid->vert + (i * (cols + 2) * 2 + j * 2);

        pt[0].x = tile_size * j + offset_x;
        pt[0].y = (tri_height * 2) * i + offset_y;
        pt[1].x = pt[0].x + tri_mid;
        pt[1].y = pt[0].y + tri_height;
      }

  grid->vert_rows = rows;
  grid->vert_cols = cols;
  grid->row_pad = 1;
  grid->col_pad = 1;
  grid->vert_multiple = 2;
  grid->vert_rowstride = (cols + 2) * 2;
}

static void
grid_localize (const GeglRectangle *result,
1017
               GeglProperties          *o,
Téo Mazars's avatar
Téo Mazars committed
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
               MosaicDatas         *mdatas)
{
  gint           i, j;
  gint           k, l;
  gint           x3, y3, x4, y4;
  gint           size;
  gint           max_x, max_y;
  gfloat         max;
  gfloat        *data;
  gdouble        rand_localize;
  Vertex        *pt;
  GridDescriptor grid;

  grid = mdatas->grid;

  size = (gint) o->tile_size;
  rand_localize = size * (1.0 - o->tile_neatness);

  for (i = -grid.row_pad; i < grid.vert_rows + grid.row_pad; i++)
    for (j = -grid.col_pad * grid.vert_multiple;
         j < (grid.vert_cols + grid.col_pad) * grid.vert_multiple;
         j++)
      {
        pt = grid.vert + (i * grid.vert_rowstride + j);

        if (result->x + pt->x < 0 || result->x + pt->x >= mdatas->width ||
            result->y + pt->y < 0 || result->y + pt->y >= mdatas->height)
          continue;

1047
        if (rand_localize > GEGL_FLOAT_EPSILON)
Téo Mazars's avatar
Téo Mazars committed
1048 1049
          {
            /* Rely on global values to make gegl's tiles seamless */
1050
            max_x = pt->x + (gint) rand_f (o->rand,
Téo Mazars's avatar
Téo Mazars committed
1051 1052
                                           result->x + pt->x, result->y + pt->y,
                                           - rand_localize / 2.0, rand_localize / 2.0);
1053
            max_y = pt->y + (gint) rand_f (o->rand,
Téo Mazars's avatar
Téo Mazars committed
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
                                           result->y + pt->y, result->x + pt->x, /*sic*/
                                           - rand_localize / 2.0, rand_localize / 2.0);
          }
        else
          {
            max_x = pt->x;
            max_y = pt->y;
          }

        x3 = pt->x - (gint) (rand_localize / 2.0);
        y3 = pt->y - (gint) (rand_localize / 2.0);
        x4 = x3 + (gint) rand_localize;
        y4 = y3 + (gint) rand_localize;

        x3 = CLAMP (x3, 0, result->width  - 1);
        y3 = CLAMP (y3, 0, result->height - 1);
        x4 = CLAMP (x4, 0, result->width  - 1);
        y4 = CLAMP (y4, 0, result->height - 1);

        max = * (mdatas->m_grad + y3 * result->width + x3);
        data = mdatas->m_grad + result->width * y3;

        for (k = y3; k <= y4; k++)
          {
            for (l = x3; l <= x4; l++)
              {
                if (data[l] > max)
                  {
                    max_y = k;
                    max_x = l;
                    max = data[l];
                  }
              }
            data += result->width;
          }

        pt->x = max_x;
        pt->y = max_y;
      }
}

static gfloat*
grid_render (gfloat              *input_buf,
             const GeglRectangle *result,
1098
             GeglProperties          *o,
Téo Mazars's avatar
Téo Mazars committed
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
             MosaicDatas         *mdatas)
{
  gint            i, j, k, index;
  Polygon         poly;
  gfloat         *output_buf, *o_buf;
  GridDescriptor  grid;

  grid = mdatas->grid;

  output_buf = g_new (gfloat, NB_CPN * (result->width) * (result->height));

  o_buf = output_buf;

  for (i = 0; i < result->height; i++)
    {
      for (j = 0; j < result->width; j++)
        {
          for (k = 0; k < NB_CPN; k++)
            o_buf[k] = mdatas->back[k];
          o_buf += NB_CPN;
        }
    }

  for (i = -grid.row_pad; i < grid.vert_rows; i++)
    for (j = -grid.col_pad; j < grid.vert_cols; j++)
      {
        index = i * grid.vert_rowstride + j * grid.vert_multiple;

        switch (o->tile_type)
          {
          case SQUARES:
            polygon_reset (&poly);
            polygon_add_point (&poly,
                               grid.vert[index].x,
                               grid.vert[index].y);
            polygon_add_point (&poly,
                               grid.vert[index + 1].x,
                               grid.vert[index + 1].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_rowstride + 1].x,
                               grid.vert[index + grid.vert_rowstride + 1].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_rowstride].x,
                               grid.vert[index + grid.vert_rowstride].y);

1144
            process_poly (&poly, o->tile_allow_split, input_buf, output_buf,
Téo Mazars's avatar
Téo Mazars committed
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
                          result, o, mdatas);
            break;

          case HEXAGONS:
            /*  The main hexagon  */
            polygon_reset (&poly);
            polygon_add_point (&poly,
                               grid.vert[index].x,
                               grid.vert[index].y);
            polygon_add_point (&poly,
                               grid.vert[index + 1].x,
                               grid.vert[index + 1].y);
            polygon_add_point (&poly,
                               grid.vert[index + 2].x,
                               grid.vert[index + 2].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_rowstride + 1].x,
                               grid.vert[index + grid.vert_rowstride + 1].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_rowstride].x,
                               grid.vert[index + grid.vert_rowstride].y);
            polygon_add_point (&poly,
                               grid.vert[index + 3].x,
                               grid.vert[index + 3].y);
1169
            process_poly (&poly, o->tile_allow_split, input_buf, output_buf,
Téo Mazars's avatar
Téo Mazars committed
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
                          result, o, mdatas);

            /*  The auxiliary hexagon  */
            polygon_reset (&poly);
            polygon_add_point (&poly,
                               grid.vert[index + 2].x,
                               grid.vert[index + 2].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_multiple * 2 - 1].x,
                               grid.vert[index + grid.vert_multiple * 2 - 1].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_rowstride + grid.vert_multiple].x,
                               grid.vert[index + grid.vert_rowstride + grid.vert_multiple].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_rowstride + grid.vert_multiple + 3].x,
                               grid.vert[index + grid.vert_rowstride + grid.vert_multiple + 3].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_rowstride + 2].x,
                               grid.vert[index + grid.vert_rowstride + 2].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_rowstride + 1].x,
                               grid.vert[index + grid.vert_rowstride + 1].y);
1192
            process_poly (&poly, o->tile_allow_split, input_buf, output_buf,
Téo Mazars's avatar
Téo Mazars committed
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
                          result, o, mdatas);
            break;

          case OCTAGONS:
            /*  The main octagon  */
            polygon_reset (&poly);
            for (k = 0; k < 8; k++)
              polygon_add_point (&poly,
                                 grid.vert[index + k].x,
                                 grid.vert[index + k].y);
1203
            process_poly (&poly, o->tile_allow_split, input_buf, output_buf,
Téo Mazars's avatar
Téo Mazars committed
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
                          result, o, mdatas);

            /*  The auxiliary octagon  */
            polygon_reset (&poly);
            polygon_add_point (&poly,
                               grid.vert[index + 3].x,
                               grid.vert[index + 3].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_multiple * 2 - 2].x,
                               grid.vert[index + grid.vert_multiple * 2 - 2].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_multiple * 2 - 3].x,
                               grid.vert[index + grid.vert_multiple * 2 - 3].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_rowstride + grid.vert_multiple].x,
                               grid.vert[index + grid.vert_rowstride + grid.vert_multiple].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_rowstride + grid.vert_multiple * 2 - 1].x,
                               grid.vert[index + grid.vert_rowstride + grid.vert_multiple * 2 - 1].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_rowstride + 2].x,
                               grid.vert[index + grid.vert_rowstride + 2].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_rowstride + 1].x,
                               grid.vert[index + grid.vert_rowstride + 1].y);
            polygon_add_point (&poly,
                               grid.vert[index + 4].x,
                               grid.vert[index + 4].y);
1232
            process_poly (&poly, o->tile_allow_split, input_buf, output_buf,
Téo Mazars's avatar
Téo Mazars committed
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
                          result, o, mdatas);

            /*  The main square  */
            polygon_reset (&poly);
            polygon_add_point (&poly,
                               grid.vert[index + 2].x,
                               grid.vert[index + 2].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_multiple * 2 - 1].x,
                               grid.vert[index + grid.vert_multiple * 2 - 1].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_multiple * 2 - 2].x,
                               grid.vert[index + grid.vert_multiple * 2 - 2].y);
            polygon_add_point (&poly,
                               grid.vert[index + 3].x,
                               grid.vert[index + 3].y);
1249
            process_poly (&poly, FALSE, input_buf, output_buf,
Téo Mazars's avatar
Téo Mazars committed
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
                          result, o, mdatas);

            /*  The auxiliary square  */
            polygon_reset (&poly);
            polygon_add_point (&poly,
                               grid.vert[index + 5].x,
                               grid.vert[index + 5].y);
            polygon_add_point (&poly,
                               grid.vert[index + 4].x,
                               grid.vert[index + 4].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_rowstride + 1].x,
                               grid.vert[index + grid.vert_rowstride + 1].y);
            polygon_add_point (&poly,
                               grid.vert[index + grid.vert_rowstride].x,
                               grid.vert[index + grid.vert_rowstride].y);
1266
            process_poly (&poly, FALSE, input_buf, output_buf,
Téo Mazars's avatar
Téo Mazars committed
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
                          result, o, mdatas);
            break;
          case TRIANGLES:
            /*  Lower left  */
            polygon_reset (&poly);
            polygon_add_point (&poly,
                                grid.vert[index].x,
                                grid.vert[index].y);
            polygon_add_point (&poly,
                                grid.vert[index + grid.vert_multiple].x,
                                grid.vert[index + grid.vert_multiple].y);
            polygon_add_point (&poly,
                                grid.vert[index + 1].x,
                                grid.vert[index + 1].y);
1281
            process_poly (&poly, o->tile_allow_split, input_buf, output_buf,
Téo Mazars's avatar
Téo Mazars committed
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
                          result, o, mdatas);

            /*  lower right  */
            polygon_reset (&poly);
            polygon_add_point (&poly,
                                grid.vert[index + 1].x,
                                grid.vert[index + 1].y);
            polygon_add_point (&poly,
                                grid.vert[index + grid.vert_multiple].x,
                                grid.vert[index + grid.vert_multiple].y);
            polygon_add_point (&poly,
                                grid.vert[index + grid.vert_multiple + 1].x,
                                grid.vert[index + grid.vert_multiple + 1].y);
1295
            process_poly (&poly, o->tile_allow_split, input_buf, output_buf,
Téo Mazars's avatar
Téo Mazars committed
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
                          result, o, mdatas);

            /*  upper left  */
            polygon_reset (&poly);
            polygon_add_point (&poly,
                                grid.vert[index + 1].x,
                                grid.vert[index + 1].y);
            polygon_add_point (&poly,
                                grid.vert[index + grid.vert_multiple + grid.vert_rowstride].x,
                                grid.vert[index + grid.vert_multiple + grid.vert_rowstride].y);
            polygon_add_point (&poly,
                                grid.vert[index + grid.vert_rowstride].x,
                                grid.vert[index + grid.vert_rowstride].y);
1309
            process_poly (&poly, o->tile_allow_split, input_buf, output_buf,
Téo Mazars's avatar
Téo Mazars committed
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
                          result, o, mdatas);

            /*  upper right  */
            polygon_reset (&poly);
            polygon_add_point (&poly,
                                grid.vert[index + 1].x,
                                grid.vert[index + 1].y);
            polygon_add_point (&poly,
                                grid.vert[index + grid.vert_multiple +1 ].x,
                                grid.vert[index + grid.vert_multiple +1 ].y);
            polygon_add_point (&poly,
                                grid.vert[index + grid.vert_multiple + grid.vert_rowstride].x,
                                grid.vert[index + grid.vert_multiple + grid.vert_rowstride].y);
1323
            process_poly (&poly, o->tile_allow_split, input_buf, output_buf,
Téo Mazars's avatar
Téo Mazars committed
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
                          result, o, mdatas);
            break;

          }
      }
  return output_buf;
}

static void
process_poly (Polygon             *poly,
              gboolean             allow_split,
              gfloat              *input_buf,
              gfloat              *output_buf,
              const GeglRectangle *result,
1338
              GeglProperties          *o,
Téo Mazars's avatar
Téo Mazars committed
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
              MosaicDatas         *mdatas)
{
  gdouble  dir[2];
  gdouble  loc[2];
  gdouble  cx = 0.0, cy = 0.0;
  gdouble  magnitude;
  gdouble  distance;
  gboolean vary;
  gint     size, frac_size;
  gdouble  color_vary = 0;

  /*  Determine direction of edges inside polygon, if any  */
  find_poly_dir (poly, mdatas->m_grad, mdatas->h_grad, mdatas->v_grad,
                 dir, loc, result);

  magnitude = sqrt (SQR (dir[0] - 0.5) + SQR (dir[1] - 0.5));

  /*  Find the center of the polygon  */
  polygon_find_center (poly, &cx, &cy);
  distance = sqrt (SQR (loc[0] - cx) + SQR (loc[1] - cy));

  /* Here we rely on global values and coordinates to get predictable results,
   * and to make gegl's tiles seamless */
  size = (mdatas->width * mdatas->height);
  frac_size = size * o->color_variation;

1365
  vary = rand_i (o->rand, result->x + cx, result->y + cy, 0, size) < frac_size;
Téo Mazars's avatar
Téo Mazars committed
1366 1367 1368 1369

  /*  determine the variation of tile color based on tile number  */
  if (vary)
    {
1370
      color_vary = rand_f (o->rand, result->x + cx, result->y + cy,
Téo Mazars's avatar
Téo Mazars committed
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
                           - o->color_variation * 0.5, o->color_variation * 0.5);
    }

  /*  If the magnitude of direction inside the polygon is greater than
   *  THRESHOLD, split the polygon into two new polygons
   */
  if (magnitude > MAG_THRESHOLD &&
      (2 * distance / o->tile_size) < 0.5 && allow_split)
    {
      split_poly (poly, input_buf, output_buf,
1381
                  dir, color_vary, result, o, mdatas);
Téo Mazars's avatar
Téo Mazars committed
1382 1383 1384 1385 1386
    }
  else
    {
      /*  Otherwise, render the original polygon  */
      render_poly (poly, input_buf, output_buf,
1387
                   color_vary, result, o, mdatas);
Téo Mazars's avatar
Téo Mazars committed
1388 1389 1390 1391 1392 1393 1394 1395 1396
    }
}

static void
render_poly (Polygon             *poly,
             gfloat              *input_buf,
             gfloat              *output_buf,
             gdouble              vary,
             const GeglRectangle *result,
1397
             GeglProperties          *o,
Téo Mazars's avatar
Téo Mazars committed
1398 1399 1400 1401
             MosaicDatas         *mdatas)
{
  gdouble cx = 0.0;
  gdouble cy = 0.0;
1402
  gfloat  col[4];
Téo Mazars's avatar
Téo Mazars committed
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425

  polygon_find_center (poly, &cx, &cy);

  if (o->color_averaging)
    find_poly_color (poly, input_buf, col, vary, result);

  scale_poly (poly, cx, cy, mdatas->scale);

  if (o->color_averaging)
    fill_poly_color (poly, input_buf, output_buf, col, result,
                     o->antialiasing, o->tile_surface, o->tile_height, mdatas);
  else
    fill_poly_image (poly, input_buf, output_buf, vary, result,
                     o->antialiasing, o->tile_surface, o->tile_height, mdatas);
}

static void
split_poly (Polygon             *poly,
            gfloat              *input_buf,
            gfloat              *output_buf,
            gdouble             *dir,
            gdouble              vary,
            const GeglRectangle *result,
1426
            GeglProperties          *o,
Téo Mazars's avatar
Téo Mazars committed
1427 1428 1429 1430 1431 1432 1433 1434 1435
            MosaicDatas         *mdatas)
{
  Polygon new_poly;
  gdouble spacing;
  gdouble cx = 0.0;
  gdouble cy = 0.0;
  gdouble magnitude;
  gdouble vec[2];
  gdouble pt[2];
1436
  gfloat  col[4];
Téo Mazars's avatar
Téo Mazars committed
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769

  spacing = o->tile_spacing / (2.0 * mdatas->scale);

  polygon_find_center (poly, &cx, &cy);
  polygon_translate (poly, -cx, -cy);

  magnitude = sqrt (SQR (dir[0] - 0.5) + SQR (dir[1] - 0.5));
  vec[0] = - (dir[1] - 0.5) / magnitude;
  vec[1] =   (dir[0] - 0.5) / magnitude;
  pt[0] = -vec[1] * spacing;
  pt[1] =  vec[0] * spacing;

  polygon_reset (&new_poly);
  clip_poly (vec, pt, poly, &new_poly);
  polygon_translate (&new_poly, cx, cy);

  if (new_poly.npts)
    {
      if (o->color_averaging)
        find_poly_color (&new_poly, input_buf, col, vary, result);

      scale_poly (&new_poly, cx, cy, mdatas->scale);

      if (o->color_averaging)
        fill_poly_color (&new_poly, input_buf, output_buf, col, result,
                         o->antialiasing, o->tile_surface, o->tile_height,
                         mdatas);
      else
        fill_poly_image (&new_poly, input_buf, output_buf, vary, result,
                         o->antialiasing, o->tile_surface, o->tile_height,
                         mdatas);
    }

  vec[0] = -vec[0];
  vec[1] = -vec[1];
  pt[0] = -pt[0];
  pt[1] = -pt[1];

  polygon_reset (&new_poly);
  clip_poly (vec, pt, poly, &new_poly);
  polygon_translate (&new_poly, cx, cy);

  if (new_poly.npts)
    {
      if (o->color_averaging)
        find_poly_color (&new_poly, input_buf, col, vary, result);

      scale_poly (&new_poly, cx, cy, mdatas->scale);

      if (o->color_averaging)
        fill_poly_color (&new_poly, input_buf, output_buf, col, result,
                         o->antialiasing, o->tile_surface, o->tile_height,
                         mdatas);
      else
        fill_poly_image (&new_poly, input_buf, output_buf, vary, result,
                         o->antialiasing, o->tile_surface, o->tile_height,
                         mdatas);
    }
}

static void
clip_poly (gdouble *dir,
           gdouble *pt,
           Polygon *poly,
           Polygon *poly_new)
{
  gint    i;
  gdouble x1, y1, x2, y2;

  for (i = 0; i < poly->npts; i++)
    {
      x1 = (i) ? poly->pts[i-1].x : poly->pts[poly->npts-1].x;
      y1 = (i) ? poly->pts[i-1].y : poly->pts[poly->npts-1].y;
      x2 = poly->pts[i].x;
      y2 = poly->pts[i].y;

      clip_point (dir, pt, x1, y1, x2, y2, poly_new);
    }
}


static void
clip_point (gdouble *dir,
            gdouble *pt,
            gdouble  x1,
            gdouble  y1,
            gdouble  x2,
            gdouble  y2,
            Polygon *poly_new)
{
  gdouble det, m11, m12;
  gdouble side1, side2;
  gdouble t;
  gdouble vec[2];

  x1 -= pt[0]; x2 -= pt[0];
  y1 -= pt[1]; y2 -= pt[1];

  side1 = x1 * -dir[1] + y1 * dir[0];
  side2 = x2 * -dir[1] + y2 * dir[0];

  /*  If both points are to be clipped, ignore  */
  if (side1 < 0.0 && side2 < 0.0)
    {
      return;
    }
  /*  If both points are non-clipped, set point  */
  else if (side1 >= 0.0 && side2 >= 0.0)
    {
      polygon_add_point (poly_new, x2 + pt[0], y2 + pt[1]);
      return;
    }
  /*  Otherwise, there is an intersection...  */
  else
    {
      vec[0] = x1 - x2;
      vec[1] = y1 - y2;
      det = dir[0] * vec[1] - dir[1] * vec[0];

      if (det == 0.0)
        {
          polygon_add_point (poly_new, x2 + pt[0], y2 + pt[1]);
          return;
        }

      m11 = vec[1] / det;
      m12 = -vec[0] / det;

      t = m11 * x1 + m12 * y1;

      /*  If the first point is clipped, set intersection and point  */
      if (side1 < 0.0 && side2 > 0.0)
        {
          polygon_add_point (poly_new, dir[0] * t + pt[0], dir[1] * t + pt[1]);
          polygon_add_point (poly_new, x2 + pt[0], y2 + pt[1]);
        }
      else
        {
          polygon_add_point (poly_new, dir[0] * t + pt[0], dir[1] * t + pt[1]);
        }
    }
}

static void
find_poly_dir (Polygon             *poly,
               gfloat              *m_gr,
               gfloat              *h_gr,
               gfloat              *v_gr,
               gdouble             *dir,
               gdouble             *loc,
               const GeglRectangle *result)
{
  gdouble dmin_x = 0.0, dmin_y = 0.0;
  gdouble dmax_x = 0.0, dmax_y = 0.0;
  gint    xs, ys;
  gint    xe, ye;
  gint    min_x, min_y;
  gint    max_x, max_y;
  gint    size_y;
  gint   *max_scanlines;
  gint   *min_scanlines;
  gfloat *dm, *dv, *dh;
  gint    count, total;
  gint    rowstride;
  gint    i, j;

  rowstride = result->width;
  count = 0;
  total = 0;
  dir[0] = 0.0;
  dir[1] = 0.0;
  loc[0] = 0.0;
  loc[1] = 0.0;

  polygon_extents (poly, &dmin_x, &dmin_y, &dmax_x, &dmax_y);

  min_x = (gint) dmin_x;
  min_y = (gint) dmin_y;
  max_x = (gint) dmax_x;
  max_y = (gint) dmax_y;

  size_y = max_y - min_y;

  min_scanlines = g_new (gint, size_y);
  max_scanlines = g_new (gint, size_y);

  for (i = 0; i < size_y; i++)
    {
      min_scanlines[i] = max_x;
      max_scanlines[i] = min_x;
    }

  for (i = 0; i < poly->npts; i++)
    {
      xs = (gint) ((i) ? poly->pts[i-1].x : poly->pts[poly->npts-1].x);
      ys = (gint) ((i) ? poly->pts[i-1].y : poly->pts[poly->npts-1].y);
      xe = (gint) poly->pts[i].x;
      ye = (gint) poly->pts[i].y;

      convert_segment (xs, ys, xe, ye, min_y,
                       min_scanlines, max_scanlines);
    }

  for (i = 0; i < size_y; i++)
    {
      if ((i + min_y) >= 0 && (i + min_y) < result->height)
        {
          dm = m_gr + (i + min_y) * rowstride;
          dh = h_gr + (i + min_y) * rowstride;
          dv = v_gr + (i + min_y) * rowstride;

          for (j = min_scanlines[i]; j < max_scanlines[i]; j++)
            {
              if (j >= 0 && j < result->width)
                {
                  if (dm[j] > MAG_THRESHOLD)
                    {
                      dir[0] += dh[j];
                      dir[1] += dv[j];
                      loc[0] += j;
                      loc[1] += i + min_y;
                      count++;
                    }
                  total++;
                }
            }
        }
    }

  if (!total)
    {
      g_free (max_scanlines);
      g_free (min_scanlines);
      return;
    }

  if ((gdouble) count / (gdouble) total > COUNT_THRESHOLD)
    {
      dir[0] /= count;
      dir[1] /= count;
      loc[0] /= count;
      loc[1] /= count;
    }
  else
    {
      dir[0] = 0.5;
      dir[1] = 0.5;
      loc[0] = 0.0;
      loc[1] = 0.0;
    }

  g_free (min_scanlines);
  g_free (max_scanlines);
}

static void
find_poly_color (Polygon             *poly,
                 gfloat              *input_buf,
                 gfloat              *col,
                 gdouble              color_var,
                 const GeglRectangle *result)
{
  gdouble       dmin_x = 0.0, dmin_y = 0.0;
  gdouble       dmax_x = 0.0, dmax_y = 0.0;
  gint          xs, ys, xe, ye;
  gint          min_x, min_y, max_x, max_y;
  gint          size_y;
  gint         *max_scanlines, *min_scanlines;
  gfloat        col_sum[4] = {0, 0, 0, 0};
  gint          b, count;
  gint          i, j, y;

  count = 0;

  polygon_extents (poly, &dmin_x, &dmin_y, &dmax_x, &dmax_y);

  min_x = (gint) dmin_x;
  min_y = (gint) dmin_y;
  max_x = (gint) dmax_x;
  max_y = (gint) dmax_y;

  size_y = max_y - min_y;

  min_scanlines = g_new (gint, size_y);
  max_scanlines = g_new (gint, size_y);

  for (i = 0; i < size_y; i++)
    {
      min_scanlines[i] = max_x;
      max_scanlines[i] = min_x;
    }

  for (i = 0; i < poly->npts; i++)
    {
      xs = (gint) ((i) ? poly->pts[i-1].x : poly->pts[poly->npts-1].x);
      ys = (gint) ((i) ? poly->pts[i-1].y : poly->pts[poly->npts-1].y);
      xe = (gint) poly->pts[i].x;
      ye = (gint) poly->pts[i].y;

      convert_segment (xs, ys, xe, ye, min_y,
                       min_scanlines, max_scanlines);
    }

  for (i = 0; i < size_y; i++)
    {
      y = i + min_y;

      if (y >= 0 && y < result->height)
        {
          for (j = min_scanlines[i]; j < max_scanlines[i]; j++)
            {
              if (j >= 0 && j < result->width)
                {
                  gint id = (j + y * result->width) * NB_CPN;

                  for (b = 0; b < NB_CPN; b++)
                    col_sum[b] += input_buf[id + b];

                  count++;
                }
            }
        }
    }

  if (count)
    {
      /* but alpha */
      for (b = 0; b < NB_CPN-1; b++)
        {
          col_sum[b] = (col_sum[b] / count + color_var);
          col[b] = CLAMP01 (col_sum[b]);
        }
      col_sum[NB_CPN -1] = (col_sum[NB_CPN - 1] / count);
1770
      col[NB_CPN -1] = CLAMP01 (col_sum[NB_CPN -1]);
Téo Mazars's avatar
Téo Mazars committed
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
    }

  g_free (min_scanlines);
  g_free (max_scanlines);
}

static void
scale_poly (Polygon *poly,
            gdouble  tx,
            gdouble  ty,
            gdouble  poly_scale)
{
  polygon_translate (poly, -tx, -ty);
  polygon_scale (poly, poly_scale);
  polygon_translate (poly, tx, ty);
}

static void
fill_poly_color (Polygon             *poly,
                 gfloat              *input_buf,
                 gfloat              *output_buf,
                 gfloat              *col,
                 const GeglRectangle *result,
                 gboolean             antialiasing,
                 gboolean             tile_rough,
                 gdouble              tile_height,
                 MosaicDatas         *mdatas)
{
  gdouble    dmin_x = 0.0, dmin_y = 0.0;
  gdouble    dmax_x = 0.0, dmax_y = 0.0;
  gint       xs, ys, xe, ye;
  gint       min_x, min_y, max_x, max_y;
  gint       size_x, size_y;
  gint      *max_scanlines, *max_scanlines_iter;
  gint      *min_scanlines, *min_scanlines_iter;
  gfloat    *vals;
  gfloat     val, pixel;
  gfloat     buf[NB_CPN];
  gint       b, i, j, k, x, y;
  gdouble    contrib;
  gdouble    xx, yy;
  gint       supersample, supersample2;
  Vertex    *pts_tmp;
  const gint poly_npts = poly->npts;
1815
  SpecVec    vecs[MAX_POINTS];
Téo Mazars's avatar
Téo Mazars committed
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835

  /*  Determine antialiasing  */
  if (antialiasing)
    {
      supersample = SUPERSAMPLE;
      supersample2 = SQR (supersample);
    }
  else
    {
      supersample = supersample2 = 1;
    }

  if (poly_npts)
    {
      pts_tmp = poly->pts;
      xs = (gint) pts_tmp[poly_npts - 1].x;
      ys = (gint) pts_tmp[poly_npts - 1].y;
      xe = (gint) pts_tmp->x;
      ye = (gint) pts_tmp->y;

1836
      calc_spec_vec (vecs, xs, ys, xe, ye,
Téo Mazars's avatar
Téo Mazars committed
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
                     mdatas->light_x, mdatas->light_y);

      for (i = 1; i < poly_npts; i++)
        {
          xs = (gint) (pts_tmp->x);
          ys = (gint) (pts_tmp->y);
          pts_tmp++;
          xe = (gint) pts_tmp->x;
          ye = (gint) pts_tmp->y;

1847
          calc_spec_vec (vecs+i, xs, ys, xe, ye,
Téo Mazars's avatar
Téo Mazars committed
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
                         mdatas->light_x, mdatas->light_y);
        }
    }

  polygon_extents (poly, &dmin_x, &dmin_y, &dmax_x, &dmax_y);

  min_x = (gint) dmin_x;
  min_y = (gint) dmin_y;
  max_x = (gint) dmax_x;
  max_y = (gint) dmax_y;

  size_y = (max_y - min_y) * supersample;
  size_x = (max_x - min_x) * supersample;

  min_scanlines = min_scanlines_iter = g_new (gint, size_y);
  max_scanlines = max_scanlines_iter = g_new (gint, size_y);

  for (i = 0; i < size_y; i++)
    {
      min_scanlines[i] = max_x * supersample;
      max_scanlines[i] = min_x * supersample;
    }

  if (poly_npts)
    {
      pts_tmp = poly->pts;
      xs = (gint) pts_tmp[poly_npts-1].x;
      ys = (gint) pts_tmp[poly_npts-1].y;
      xe = (gint) pts_tmp->x;
      ye = (gint) pts_tmp->y;

      xs *= supersample;
      ys *= supersample;
      xe *= supersample;
      ye *= supersample;

      convert_segment (xs, ys, xe, ye, min_y * supersample,
                       min_scanlines, max_scanlines);

      for (i = 1; i < poly_npts; i++)
        {
          xs = (gint) pts_tmp->x;
          ys = (gint) pts_tmp->y;
          pts_tmp++;
          xe = (gint) pts_tmp->x;
          ye = (gint) pts_tmp->y;

          xs *= supersample;
          ys *= supersample;
          xe *= supersample;
          ye *= supersample;

          convert_segment (xs, ys, xe, ye, min_y * supersample,
                           min_scanlines, max_scanlines);
        }
    }

  vals = g_new (gfloat, size_x);
  for (i = 0; i < size_y; i++, min_scanlines_iter++, max_scanlines_iter++)
    {
      if (! (i % supersample))
        memset (vals, 0, sizeof (gfloat) * size_x);

      yy = (gdouble) i / (gdouble) supersample + min_y;

      for (j = *min_scanlines_iter; j < *max_scanlines_iter; j++)
        {
          x = j - min_x * supersample;
          vals[x] += 1.0;
        }

      if (! ((i + 1) % supersample))
        {
          y = (i / supersample) + min_y;

          if (y >= 0 && y < result->height)
            {
              for (j = 0; j < size_x; j += supersample)
                {
                  x = (j / supersample) + min_x;

                  if (x >= 0 && x < result->width)
                    {
                      val = 0;

                      for (k = 0; k < supersample; k++)
                        val += vals[j + k];

                      val /= supersample2;

                      if (val > 0)
                        {
                          xx = (gdouble) j / (gdouble) supersample + min_x;

1942
                          contrib = calc_spec_contrib (vecs, poly_npts,
Téo Mazars's avatar
Téo Mazars committed
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
                                                       xx, yy, tile_rough,
                                                       tile_height);

                          for (b = 0; b < NB_CPN; b++)
                            {
                              pixel = col[b];

                              if (contrib < 0.0)
                                pixel += (col[b] - mdatas->back[b]) * contrib;
                              else
                                pixel += (mdatas->fore[b] - col[b]) * contrib;

                              buf[b] = ((pixel * val) + (mdatas->back[b] * (1.0 - val)));
                            }
                          memcpy (output_buf + (y * result->width + x) * NB_CPN,
                                  buf, NB_CPN * sizeof (gfloat));
                        }
                    }
                }
            }
        }
    }

  g_free (vals);
  g_free (min_scanlines);
  g_free (max_scanlines);
}

static void
fill_poly_image (Polygon             *poly,
                 gfloat              *input_buf,
                 gfloat              *output_buf,
                 gdouble              vary,
                 const GeglRectangle *result,
                 gboolean             antialiasing,
                 gboolean             tile_rough,
                 gdouble              tile_height,
                 MosaicDatas         *mdatas)
{
  gdouble   dmin_x = 0.0, dmin_y = 0.0;
  gdouble   dmax_x = 0.0, dmax_y = 0.0;
  gint      xs, ys, xe, ye;
  gint      min_x, min_y, max_x, max_y;
  gint      size_x, size_y;
  gint     *max_scanlines, *min_scanlines;
  gfloat   *vals;
  gfloat    val;
  gfloat    pixel;
  gfloat    buf[NB_CPN];
  gint      b, i, j, k, x, y;
  gdouble   contrib;
  gdouble   xx, yy;
  gint      supersample, supersample2;
1996
  SpecVec   vecs[MAX_POINTS];
Téo Mazars's avatar
Téo Mazars committed
1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015

  /*  Determine antialiasing  */
  if (antialiasing)
    {
      supersample = SUPERSAMPLE;
      supersample2 = SQR (supersample);
    }
  else
    {
      supersample = supersample2 = 1;
    }

  for (i = 0; i < poly->npts; i++)
    {
      xs = (gint) ((i) ? poly->pts[i-1].x : poly->pts[poly->npts-1].x);
      ys = (gint) ((i) ? poly->pts[i-1].y : poly->pts[poly->npts-1].y);
      xe = (gint) poly->pts[i].x;
      ye = (gint) poly->pts[i].y;

2016
      calc_spec_vec (vecs+i, xs, ys, xe, ye,
Téo Mazars's avatar
Téo Mazars committed
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
                     mdatas->light_x, mdatas->light_y);
    }

  polygon_extents (poly, &dmin_x, &dmin_y, &dmax_x, &dmax_y);

  min_x = (gint) dmin_x;
  min_y = (gint) dmin_y;
  max_x = (gint) dmax_x;
  max_y = (gint) dmax_y;

  size_y = (max_y - min_y) * supersample;
  size_x = (max_x - min_x) * supersample;

  min_scanlines = g_new (gint, size_y);
  max_scanlines = g_new (gint, size_y);

  for (i = 0; i < size_y; i++)
    {
      min_scanlines[i] = max_x * supersample;
      max_scanlines[i] = min_x * supersample;
    }

  for (i = 0; i < poly->npts; i++)
    {
      xs = (gint) ((i) ? poly->pts[i-1].x : poly->pts[poly->npts-1].x);
      ys = (gint) ((i) ? poly->pts[i-1].y : poly->pts[poly->npts-1].y);
      xe = (gint) poly->pts[i].x;
      ye = (gint) poly->pts[i].y;

      xs *= supersample;
      ys *= supersample;
      xe *= supersample;
      ye *= supersample;

      convert_segment (xs, ys, xe, ye, min_y * supersample,
                       min_scanlines, max_scanlines);
    }

  vals = g_new (gfloat, size_x);

  for (i = 0; i < size_y; i++)
    {
      if (! (i % supersample))
        memset (vals, 0, sizeof (gfloat) * size_x);

      yy = (gdouble) i / (gdouble) supersample + min_y;

      for (j = min_scanlines[i]; j < max_scanlines[i]; j++)
        {
          x = j - min_x * supersample;
          vals[x] += 1.0;
        }

      if (! ((i + 1) % supersample))
        {
          y = (i / supersample) + min_y;

          if (y >= 0 && y < result->height)
            {
              for (j = 0; j < size_x; j += supersample)
                {
                  x = (j / supersample) + min_x;

                  if (x >= 0 && x < result->width)
                    {
                      val = 0;
                      for (k = 0; k < supersample; k++)
                        val += vals[j + k];
                      val /= supersample2;

                      if (val > 0)
                        {
                          xx = (gdouble) j / (gdouble) supersample + min_x;

2091
                          contrib = calc_spec_contrib (vecs,
Téo Mazars's avatar
Téo Mazars committed
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101
                                                       poly->npts, xx, yy,
                                                       tile_rough, tile_height);

                          memcpy (buf,
                                  input_buf + (x + y*result->width) * NB_CPN,
                                  NB_CPN * sizeof (gfloat));

                          for (b = 0; b < NB_CPN; b++)
                            {
                              if (contrib < 0.0)
2102
                                pixel = buf[b] + (buf[b] - mdatas->back[b]) * contrib;
Téo Mazars's avatar
Téo Mazars committed
2103
                              else
2104
                                pixel = buf[b] + (mdatas->fore[b] - buf[b]) * contrib;
Téo Mazars's avatar
Téo Mazars committed
2105

2106 2107
                              /* factor in per-tile intensity variation but alpha */
                              if (b < NB_CPN - 1)
Téo Mazars's avatar
Téo Mazars committed
2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141
                                pixel += vary;

                              pixel = CLAMP (pixel, 0.0, 1.0);

                              buf[b] = (mdatas->back[b]) + (pixel - mdatas->back[b]) * val;
                            }

                          memcpy (output_buf + (y * result->width + x) * NB_CPN,
                                  buf, NB_CPN * sizeof (gfloat));
                        }
                    }
                }
            }
        }
    }

  g_free (vals);
  g_free (min_scanlines);
  g_free (max_scanlines);
}

static void
calc_spec_vec (SpecVec *vec,
               gint     x1,
               gint     y1,
               gint     x2,
               gint     y2,
               gdouble  light_x,
               gdouble  light_y)
{
  gdouble r;

  vec->base_x = x1;
  vec->base_y = y1;
2142 2143
  vec->base_x2 = x2;
  vec->base_y2 = y2;
Téo Mazars's avatar
Téo Mazars committed
2144 2145 2146

  r = sqrt (SQR (x2 - x1) + SQR (y2 - y1));

2147
  if (r > GEGL_FLOAT_EPSILON)
Téo Mazars's avatar
Téo Mazars committed
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
    {
      vec->norm_x = - (y2 - y1) / r;
      vec->norm_y =   (x2 - x1) / r;
    }
  else
    {
      vec->norm_x = 0;
      vec->norm_y = 0;
    }

  vec->light = vec->norm_x * light_x + vec->norm_y * light_y;
}

2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173
static gfloat
distance (SpecVec *vec,
          gfloat   x,
          gfloat   y)
{
  gfloat l2, t;
  gfloat pv_x, pv_y;
  gfloat pw_x, pw_y;
  gfloat wv_x, wv_y;
  gfloat proj_x, proj_y;

  l2 = SQR (vec->base_x - vec->base_x2) + SQR (vec->base_y - vec->base_y2);

2174
  if (l2 < GEGL_FLOAT_EPSILON)
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
    return sqrt (SQR (vec->base_x - x) + SQR (vec->base_y - y));

  pv_x = x - vec->base_x;
  pv_y = y - vec->base_y;

  pw_x = x - vec->base_x2;
  pw_y = y - vec->base_y2;

  wv_x = vec->base_x2 - vec->base_x;
  wv_y = vec->base_y2 - vec->base_y;

  t = (pv_x * wv_x + pv_y * wv_y) / l2;

  if (t < 0.0)
    return sqrt (SQR (pv_x) + SQR (pv_y));

  else if (t > 1.0)
    return sqrt (SQR (pw_x) + SQR (pw_y));

  proj_x = vec->base_x + t * wv_x;
  proj_y = vec->base_y + t * wv_y;

  return sqrt (SQR (x - proj_x) + SQR (y - proj_y));
}

Téo Mazars's avatar
Téo Mazars committed
2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213

static double
calc_spec_contrib (SpecVec *vecs,
                   gint     n,
                   gdouble  x,
                   gdouble  y,
                   gboolean tile_rough,
                   gdouble  tile_height)
{
  gint i;
  gdouble contrib = 0;

  for (i = 0; i < n; i++)
    {
2214
      gfloat dist;
Téo Mazars's avatar
Téo Mazars committed
2215

2216
      dist = distance (vecs+i, x, y);
Téo Mazars's avatar
Téo Mazars committed
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379

      if (tile_rough)
        {
          /*  If the surface is rough, randomly perturb the distance  */
          dist -= dist * g_random_double ();
        }

      /*  If the distance to an edge is less than the tile_spacing, there
       *  will be no highlight as the tile blends to background here
       */
      if (dist < 1.0)
        {
          contrib += vecs[i].light;
        }
      else if (dist <= tile_height)
        {
          contrib += vecs[i].light * (1.0 - (dist / tile_height));
        }
    }

  return contrib / 4.0;
}

static void
convert_segment (gint  x1,
                 gint  y1,
                 gint  x2,
                 gint  y2,
                 gint  offset,
                 gint *min,
                 gint *max)
{
  gint    ydiff, y, tmp;
  gdouble xinc, xstart;

  if (y1 > y2)
    {
      tmp = y2; y2 = y1; y1 = tmp;
      tmp = x2; x2 = x1; x1 = tmp;
    }

  ydiff = y2 - y1;

  if (ydiff)
    {
      xinc = (gdouble) (x2 - x1) / (gdouble) ydiff;
      xstart = x1 + 0.5 * xinc;

      for (y = y1; y < y2; y++)
        {
          min[y - offset] = MIN (min[y - offset], xstart);
          max[y - offset] = MAX (max[y - offset], xstart);

          xstart += xinc;
        }
    }
}

static void
polygon_add_point (Polygon *poly,
                   gdouble  x,
                   gdouble  y)
{
  if (poly->npts < 12)
    {
      poly->pts[poly->npts].x = x;
      poly->pts[poly->npts].y = y;
      poly->npts++;
    }
  else
    {
      g_warning ("can't add more points");
    }
}

static gboolean
polygon_find_center (Polygon *poly,
                     gdouble *cx,
                     gdouble *cy)
{
  guint i;

  if (!poly->npts)
    return FALSE;

  *cx = 0.0;
  *cy = 0.0;

  for (i = 0; i < poly->npts; i++)
    {
      *cx += poly->pts[i].x;
      *cy += poly->pts[i].y;
    }

  *cx /= poly->npts;
  *cy /= poly->npts;

  return TRUE;
}

static void
polygon_translate (Polygon *poly,
                   gdouble  tx,
                   gdouble  ty)
{
  guint i;

  for (i = 0; i < poly->npts; i++)
    {
      poly->pts[i].x += tx;
      poly->pts[i].y += ty;
    }
}

static void
polygon_scale (Polygon *poly,
               gdouble  poly_scale)
{
  guint i;

  for (i = 0; i < poly->npts; i++)
    {
      poly->pts[i].x *= poly_scale;
      poly->pts[i].y *= poly_scale;
    }
}

static gboolean
polygon_extents (Polygon *poly,
                 gdouble *min_x,
                 gdouble *min_y,
                 gdouble *max_x,
                 gdouble *max_y)
{
  guint i;

  if (!poly->npts)
    return FALSE;

  *min_x = *max_x = poly->pts[0].x;
  *min_y = *max_y = poly->pts[0].y;

  for (i = 1; i < poly->npts; i++)
    {
      *min_x = MIN (*min_x, poly->pts[i].x);
      *max_x = MAX (*max_x, poly->pts[i].x);
      *min_y = MIN (*min_y, poly->pts[i].y);
      *max_y = MAX (*max_y, poly->pts[i].y);
    }

  return TRUE;
}

static void
polygon_reset (Polygon *poly)
{
  poly->npts = 0;
}

static void
prepare (GeglOperation *operation)
{
  GeglOperationAreaFilter *area = GEGL_OPERATION_AREA_FILTER (operation);
2380
  GeglProperties              *o    = GEGL_PROPERTIES (operation);
Téo Mazars's avatar
Téo Mazars committed
2381 2382 2383 2384 2385 2