convolution-matrix.c 18.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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
14
 * License along with GEGL; if not, see <https://www.gnu.org/licenses/>.
15 16 17 18 19 20 21 22
 *
 * Copyright (C) 1997 Lauri Alanko <la@iki.fi>
 * Copyright 2011 Robert Sasu (sasu.robert@gmail.com)
 */

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

23 24
#ifdef GEGL_PROPERTIES

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
property_double (a1, _("(1,1)"), 0.0)
property_double (a2, _("(1,2)"), 0.0)
property_double (a3, _("(1,3)"), 0.0)
property_double (a4, _("(1,4)"), 0.0)
property_double (a5, _("(1,5)"), 0.0)
property_double (b1, _("(2,1)"), 0.0)
property_double (b2, _("(2,2)"), 0.0)
property_double (b3, _("(2,3)"), 0.0)
property_double (b4, _("(2,4)"), 0.0)
property_double (b5, _("(2,5)"), 0.0)
property_double (c1, _("(3,1)"), 0.0)
property_double (c2, _("(3,2)"), 0.0)
property_double (c3, _("(3,3)"), 1.0)
property_double (c4, _("(3,4)"), 0.0)
property_double (c5, _("(3,5)"), 0.0)
property_double (d1, _("(4,1)"), 0.0)
property_double (d2, _("(4,2)"), 0.0)
property_double (d3, _("(4,3)"), 0.0)
property_double (d4, _("(4,4)"), 0.0)
property_double (d5, _("(4,5)"), 0.0)
property_double (e1, _("(5,1)"), 0.0)
property_double (e2, _("(5,2)"), 0.0)
property_double (e3, _("(5,3)"), 0.0)
property_double (e4, _("(5,4)"), 0.0)
property_double (e5, _("(5,5)"), 0.0)
50

51
property_double (divisor, _("Divisor"), 1.0)
52
    ui_range    (-1000.0, 1000.0)
53
    ui_meta     ("sensitive", "! normalize")
54

55
property_double (offset, _("Offset"), 0.0)
56
    value_range (-1.0, 1.0)
57
    ui_meta     ("sensitive", "! normalize")
58

59
property_boolean (red,   _("Red channel"),   TRUE)
60
property_boolean (green, _("Green channel"), TRUE)
61
property_boolean (blue,  _("Blue channel"),  TRUE)
62
property_boolean (alpha, _("Alpha channel"), TRUE)
63

64 65
property_boolean (normalize,    _("Normalize"),       TRUE)
property_boolean (alpha_weight, _("Alpha-weighting"), TRUE)
66

67
property_enum (border, _("Border"),
68 69
               GeglAbyssPolicy, gegl_abyss_policy,
               GEGL_ABYSS_CLAMP)
70 71 72

#else

73
#define GEGL_OP_AREA_FILTER
74
#define GEGL_OP_NAME     convolution_matrix
75
#define GEGL_OP_C_SOURCE convolution-matrix.c
76

77
#include "gegl-op.h"
78 79 80
#include <math.h>
#include <stdio.h>

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
#define MAX_MATRIX_SIZE 5

static gboolean
enough_with_3x3 (GeglProperties *o)
{
  if (o->a1 == 0.0 &&
      o->a2 == 0.0 &&
      o->a3 == 0.0 &&
      o->a4 == 0.0 &&
      o->a5 == 0.0 &&
      o->b1 == 0.0 &&
      o->b5 == 0.0 &&
      o->c1 == 0.0 &&
      o->c5 == 0.0 &&
      o->d1 == 0.0 &&
      o->d5 == 0.0 &&
      o->e1 == 0.0 &&
      o->e2 == 0.0 &&
      o->e3 == 0.0 &&
      o->e4 == 0.0 &&
      o->e5 == 0.0)
  {
    return TRUE;
  }
  return FALSE;
}
107 108 109 110

static void
prepare (GeglOperation *operation)
{
111 112
  const Babl *space = gegl_operation_get_source_space (operation, "input");

113 114
  GeglOperationAreaFilter *op_area = GEGL_OPERATION_AREA_FILTER (operation);

115 116 117 118 119
  GeglProperties          *o       = GEGL_PROPERTIES (operation);
  if (enough_with_3x3 (o))
    op_area->left = op_area->right = op_area->top = op_area->bottom = 1; /* 3 */
  else
    op_area->left = op_area->right = op_area->top = op_area->bottom = 2; /* 5 */
120 121

  gegl_operation_set_format (operation, "output",
122
                             babl_format_with_space ("RGBA float", space));
123 124 125
}

static void
126
make_matrix (GeglProperties  *o,
127 128
             gfloat           matrix[MAX_MATRIX_SIZE][MAX_MATRIX_SIZE],
             gint             matrix_size)
129
{
130 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
  if (matrix_size == 3)
  {
    matrix[0][0] = o->b2;
    matrix[0][1] = o->b3;
    matrix[0][2] = o->b4;

    matrix[1][0] = o->c2;
    matrix[1][1] = o->c3;
    matrix[1][2] = o->c4;

    matrix[2][0] = o->d2;
    matrix[2][1] = o->d3;
    matrix[2][2] = o->d4;
  }
  else
  {
    matrix[0][0] = o->a1;
    matrix[0][1] = o->a2;
    matrix[0][2] = o->a3;
    matrix[0][3] = o->a4;
    matrix[0][4] = o->a5;

    matrix[1][0] = o->b1;
    matrix[1][1] = o->b2;
    matrix[1][2] = o->b3;
    matrix[1][3] = o->b4;
    matrix[1][4] = o->b5;

    matrix[2][0] = o->c1;
    matrix[2][1] = o->c2;
    matrix[2][2] = o->c3;
    matrix[2][3] = o->c4;
    matrix[2][4] = o->c5;

    matrix[3][0] = o->d1;
    matrix[3][1] = o->d2;
    matrix[3][2] = o->d3;
    matrix[3][3] = o->d4;
    matrix[3][4] = o->d5;

    matrix[4][0] = o->e1;
    matrix[4][1] = o->e2;
    matrix[4][2] = o->e3;
    matrix[4][3] = o->e4;
    matrix[4][4] = o->e5;
  }
176 177
}

178
static gboolean
179 180 181 182
normalize_div_off (gfloat  matrix[MAX_MATRIX_SIZE][MAX_MATRIX_SIZE],
                   gint    matrix_size,
                   gfloat *divisor,
                   gfloat *offset)
183 184 185 186 187
{
  gint      x, y;
  gboolean  valid = FALSE;
  gfloat    sum   = 0.0;

188 189
  for (y = 0; y < matrix_size; y++)
    for (x = 0; x < matrix_size; x++)
190 191 192 193 194 195 196
      {
        sum += matrix[x][y];
        if (matrix[x][y] != 0.0)
          valid = TRUE;
      }

  if (sum > 0)
197
    {
198 199
      *offset  = 0.0;
      *divisor = sum;
200
    }
201
  else if (sum < 0)
202
    {
203 204
      *offset  = 1.0;
      *divisor = -sum;
205
    }
206
  else
207
    {
208 209
      *offset  = 0.5;
      *divisor = 1;
210
    }
211 212

  return valid;
213 214
}

215 216
static void inline
convolve_pixel_componentwise (GeglProperties       *o,
217
                gfloat               *src_buf,
218 219 220
                gfloat               *dst_buf,
                const GeglRectangle  *result,
                const GeglRectangle  *extended,
221 222 223 224
                gfloat                matrix[MAX_MATRIX_SIZE][MAX_MATRIX_SIZE],
                gint                  matrix_size,
                gint                  d_offset,
                gint                  ss_offset,
225 226
                gint                  xx,
                gint                  yy,
227
                gfloat                matrixsum,
228 229
                gfloat                inv_divisor,
                gfloat                offset)
230
{
231 232
  gint   i;
  gint   s_stride = (extended->width - matrix_size) * 4;
233
  for (i = 0; i < 4; i++)
234
    {
235 236
      gfloat sum = 0.0;
      gint s_offset = ss_offset;
237 238

      if ((i == 0 && o->red)   ||
239 240
          (i == 1 && o->green) ||
          (i == 2 && o->blue)  ||
241
          (i == 3 && o->alpha))
242
        {
243
          gint x, y;
244 245 246
          for (y = 0; y < matrix_size; y++)
          {
            for (x = 0; x < matrix_size; x++)
247
              {
248 249 250 251 252 253
                sum += matrix[x][y] * src_buf[s_offset + i];
                s_offset += 4;
              }
            s_offset += s_stride;
          }
          sum = sum * inv_divisor;
254
          sum += offset;
255 256 257 258 259 260 261 262
          dst_buf[d_offset + i] = sum;
        }
      else
        {
          dst_buf[d_offset + i] = src_buf[s_offset + i];
        }
    }
}
263

264 265 266 267 268 269 270 271 272 273 274 275 276
static void inline
convolve_pixel (GeglProperties       *o,
                gfloat               *src_buf,
                gfloat               *dst_buf,
                const GeglRectangle  *result,
                const GeglRectangle  *extended,
                gfloat                matrix[MAX_MATRIX_SIZE][MAX_MATRIX_SIZE],
                gint                  matrix_size,
                gint                  d_offset,
                gint                  ss_offset,
                gint                  xx,
                gint                  yy,
                gfloat                matrixsum,
277 278
                gfloat                inv_divisor,
                gfloat                offset)
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
{
  gint   i;
  gint   s_stride = (extended->width - matrix_size) * 4;
  for (i = 0; i < 4; i++)
    {
      gfloat sum = 0.0;
      gint s_offset = ss_offset;
      gint x, y;
      for (y = 0; y < matrix_size; y++)
      {
        for (x = 0; x < matrix_size; x++)
          {
            sum += matrix[x][y] * src_buf[s_offset + i];
            s_offset += 4;
          }
        s_offset += s_stride;
      }
      sum = sum * inv_divisor;
297
      sum += offset;
298 299 300
      dst_buf[d_offset + i] = sum;
    }
}
301

302 303 304 305 306 307 308 309 310 311 312 313 314
static void inline
convolve_pixel_alpha_weight (GeglProperties       *o,
                             gfloat               *src_buf,
                             gfloat               *dst_buf,
                             const GeglRectangle  *result,
                             const GeglRectangle  *extended,
                             gfloat                matrix[MAX_MATRIX_SIZE][MAX_MATRIX_SIZE],
                             gint                  matrix_size,
                             gint                  d_offset,
                             gint                  ss_offset,
                             gint                  xx,
                             gint                  yy,
                             gfloat                matrixsum,
315 316
                             gfloat                inv_divisor,
                             gfloat                offset)
317 318 319
{
  gint   i;
  gint   s_stride = (extended->width - matrix_size) * 4;
320

321 322 323 324 325 326 327 328 329 330 331 332 333 334
  for (i = 0; i < 3; i++)
    {
      gfloat sum      = 0.0;
      gint   s_offset = ss_offset;
      gint x, y;
      for (y = 0; y < matrix_size; y++)
      {
        for (x = 0; x < matrix_size; x++)
          {
            sum += matrix[x][y] * src_buf[s_offset + i] * src_buf[s_offset + 3];
            s_offset += 4;
          }
        s_offset += s_stride;
      }
335
      sum = sum * inv_divisor + offset;
336 337 338 339 340 341 342
      dst_buf[d_offset + i] = sum;
    }
    {
      gfloat sum      = 0.0;
      gfloat alphasum = 0.0;
      gint   s_offset = ss_offset;
      gint x, y;
343

344 345 346 347 348 349 350 351 352 353 354
      for (y = 0; y < matrix_size; y++)
      {
        for (x = 0; x < matrix_size; x++)
          {
            float val = matrix[x][y] * src_buf[s_offset + i];
            sum += val;
            alphasum += fabsf (val);
            s_offset += 4;
          }
        s_offset += s_stride;
      }
355

356 357 358
      if (alphasum > 0.0)
      {
        sum = sum * inv_divisor;
359
        sum = sum * matrixsum / alphasum + offset;
360 361
      }
      else
362
        sum = offset;
363 364 365
      dst_buf[d_offset + i] = sum;
    }
}
366

367 368 369 370 371 372 373 374 375 376 377 378 379
static void inline
convolve_pixel_alpha_weight_componentwise (GeglProperties       *o,
                             gfloat               *src_buf,
                             gfloat               *dst_buf,
                             const GeglRectangle  *result,
                             const GeglRectangle  *extended,
                             gfloat                matrix[MAX_MATRIX_SIZE][MAX_MATRIX_SIZE],
                             gint                  matrix_size,
                             gint                  d_offset,
                             gint                  ss_offset,
                             gint                  xx,
                             gint                  yy,
                             gfloat                matrixsum,
380 381
                             gfloat                inv_divisor,
                             gfloat                offset)
382 383 384 385 386 387 388 389
{
  gint   i;
  gint   s_stride = (extended->width - matrix_size) * 4;

  for (i = 0; i < 3; i++)
    {
      gfloat sum      = 0.0;
      gint   s_offset = ss_offset;
390

391
      if ((i == 0 && o->red)   ||
392 393
          (i == 1 && o->green) ||
          (i == 2 && o->blue))
394 395 396 397 398 399 400 401 402 403 404
        {
          gint x, y;
          for (y = 0; y < matrix_size; y++)
          {
            for (x = 0; x < matrix_size; x++)
              {
                sum += matrix[x][y] * src_buf[s_offset + i] * src_buf[s_offset + 3];
                s_offset += 4;
              }
            s_offset += s_stride;
          }
405
          sum = sum * inv_divisor + offset;
406 407
        }
      else
408
        {
409
          sum = src_buf[s_offset + i];
410
        }
411
      dst_buf[d_offset + i] = sum;
412
    }
413 414 415 416
    {
      gfloat sum      = 0.0;
      gfloat alphasum = 0.0;
      gint   s_offset = ss_offset;
417

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
      if (o->alpha)
        {
          gint x, y;

          for (y = 0; y < matrix_size; y++)
          {
            for (x = 0; x < matrix_size; x++)
              {
                float val = matrix[x][y] * src_buf[s_offset + i];
                sum += val;
                alphasum += fabsf (val);
                s_offset += 4;
              }
            s_offset += s_stride;
          }


          if (alphasum != 0)
          {
            sum = sum * inv_divisor;
438
            sum = sum * matrixsum / alphasum + offset;
439 440
          }
          else
441
            sum = offset;
442 443 444 445 446 447 448
        }
      else
        {
          sum = src_buf[s_offset + i];
        }
      dst_buf[d_offset + i] = sum;
    }
449 450 451 452 453 454
}

static gboolean
process (GeglOperation       *operation,
         GeglBuffer          *input,
         GeglBuffer          *output,
455 456
         const GeglRectangle *result,
         gint                 level)
457
{
458
  GeglProperties          *o       = GEGL_PROPERTIES (operation);
459
  GeglOperationAreaFilter *op_area = GEGL_OPERATION_AREA_FILTER (operation);
460
  const Babl              *format  = gegl_operation_get_format (operation, "output");
461 462 463
  GeglRectangle            rect;
  gfloat                  *src_buf;
  gfloat                  *dst_buf;
464 465
  gfloat                   matrix[MAX_MATRIX_SIZE][MAX_MATRIX_SIZE]={{0,}};
  gfloat                   matrixsum = 0.0;
466 467
  gfloat                   divisor = o->divisor;
  gfloat                   offset = o->offset;
468
  gfloat                   inv_divisor;
469
  gint                     x, y;
470
  gint                     matrix_size = MAX_MATRIX_SIZE;
471

472 473
  if (enough_with_3x3 (o))
    matrix_size = 3;
474

475
  make_matrix (o, matrix, matrix_size);
476

477
  if (o->normalize)
478 479
    normalize_div_off (matrix, matrix_size, &divisor, &offset);
  inv_divisor = 1.0 / divisor;
480

481 482 483
  for (x = 0; x < matrix_size; x++)
    for (y = 0; y < matrix_size; y++)
      matrixsum += fabsf (matrix[x][y]);
484 485 486 487 488 489

  rect.x      = result->x - op_area->left;
  rect.width  = result->width + op_area->left + op_area->right;
  rect.y      = result->y - op_area->top;
  rect.height = result->height + op_area->top + op_area->bottom;

490 491
  src_buf = g_new (gfloat, rect.width * rect.height * 4);
  dst_buf = g_new (gfloat, result->width * result->height * 4);
492

493
  gegl_buffer_get (input, &rect, 1.0, format, src_buf,
494
                   GEGL_AUTO_ROWSTRIDE, o->border);
495

496
  if (o->divisor != 0)
497
    {
498 499 500 501 502 503 504 505 506 507 508 509 510
      gint ss_offset = (result->y - matrix_size/2 - rect.y) * rect.width * 4 +
                       (result->x - matrix_size/2 - rect.x) * 4;
      int d_offset = 0;
      if (o->alpha_weight)
        {
          if (o->red == FALSE || o->green == FALSE || o->blue == FALSE || o->alpha == FALSE)
          {
            gint x, y;
            for (y = result->y; y < result->height + result->y; y++)
            {
              for (x = result->x; x < result->width + result->x; x++)
              {
                convolve_pixel_alpha_weight_componentwise (o, src_buf, dst_buf, result, &rect,
511
                                           matrix, matrix_size, d_offset, ss_offset, x, y, matrixsum, inv_divisor, offset);
512 513 514 515 516 517 518 519 520 521 522 523 524 525
                d_offset += 4;
                ss_offset += 4;
              }
              ss_offset += (rect.width - result->width) * 4;
            }
          }
          else
          {
            gint x, y;
            for (y = result->y; y < result->height + result->y; y++)
            {
              for (x = result->x; x < result->width + result->x; x++)
              {
                convolve_pixel_alpha_weight (o, src_buf, dst_buf, result, &rect,
526
                                           matrix, matrix_size, d_offset, ss_offset, x, y, matrixsum, inv_divisor, offset);
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
                d_offset += 4;
                ss_offset += 4;
              }
              ss_offset += (rect.width - result->width) * 4;
            }
          }
        }
      else
        {
          if (o->red == FALSE || o->green == FALSE || o->blue == FALSE || o->alpha == FALSE)
          {
            gint x, y;
            for (y = result->y; y < result->height + result->y; y++)
            {
              for (x = result->x; x < result->width + result->x; x++)
              {
                convolve_pixel_componentwise (o, src_buf, dst_buf, result, &rect,
544
                                matrix, matrix_size, d_offset, ss_offset, x, y, matrixsum, inv_divisor, offset);
545 546 547 548 549 550 551 552 553 554 555 556 557 558
                d_offset += 4;
                ss_offset += 4;
              }
              ss_offset += (rect.width - result->width) * 4;
            }
          }
          else
          {
            gint x, y;
            for (y = result->y; y < result->height + result->y; y++)
            {
              for (x = result->x; x < result->width + result->x; x++)
              {
                convolve_pixel (o, src_buf, dst_buf, result, &rect,
559
                                matrix, matrix_size, d_offset, ss_offset, x, y, matrixsum, inv_divisor, offset);
560 561 562 563 564 565 566 567 568
                d_offset += 4;
                ss_offset += 4;
              }
              ss_offset += (rect.width - result->width) * 4;
            }
          }
        }
        gegl_buffer_set (output, result, 0, format,
                         dst_buf, GEGL_AUTO_ROWSTRIDE);
569 570
    }
  else
571 572 573 574
    {
      gegl_buffer_set (output, &rect, 0, format,
                       src_buf, GEGL_AUTO_ROWSTRIDE);
    }
575

576 577 578 579 580 581 582 583 584
  g_free (src_buf);
  g_free (dst_buf);

  return TRUE;
}

static GeglRectangle
get_bounding_box (GeglOperation *operation)
{
585
  GeglRectangle  result = { 0, };
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
  GeglRectangle *in_rect;

  in_rect = gegl_operation_source_get_bounding_box (operation, "input");
  if (!in_rect)
    return result;

  return *in_rect;
}

static GeglRectangle
get_required_for_output (GeglOperation       *operation,
                         const gchar         *input_pad,
                         const GeglRectangle *roi)
{
  return get_bounding_box (operation);
}

603 604 605 606 607 608 609 610 611
static GeglAbyssPolicy
get_abyss_policy (GeglOperation *operation,
                  const gchar   *input_pad)
{
  GeglProperties *o = GEGL_PROPERTIES (operation);

  return o->border;
}

612
static void
613
gegl_op_class_init (GeglOpClass *klass)
614
{
615 616 617
  GeglOperationClass           *operation_class;
  GeglOperationFilterClass     *filter_class;
  GeglOperationAreaFilterClass *area_class;
618 619 620

  operation_class = GEGL_OPERATION_CLASS (klass);
  filter_class    = GEGL_OPERATION_FILTER_CLASS (klass);
621 622 623
  area_class      = GEGL_OPERATION_AREA_FILTER_CLASS (klass);

  area_class->get_abyss_policy             = get_abyss_policy;
624 625
  filter_class->process                    = process;
  operation_class->prepare                 = prepare;
626 627 628
  operation_class->get_bounding_box        = get_bounding_box;
  operation_class->get_required_for_output = get_required_for_output;

629
  gegl_operation_class_set_keys (operation_class,
630 631
    "categories",  "generic",
    "name",        "gegl:convolution-matrix",
632
    "reference-hash", "22d2d47a2da3d3e7cd402ea9fa1a3a25",
633
    "title",       _("Convolution Matrix"),
634
    "description", _("Apply a generic 5x5 convolution matrix"),
635
    NULL);
636 637 638
}

#endif