gimpmatrix.c 22.2 KB
Newer Older
1
/* LIBGIMP - The GIMP Library
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimpmatrix.c
5
 * Copyright (C) 1998 Jay Cox <jaycox@gimp.org>
6
 *
7
 * This library is free software: you can redistribute it and/or
Marc Lehmann's avatar
Marc Lehmann committed
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 3 of the License, or (at your option) any later version.
11 12 13 14 15 16
 *
 * This library 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
 * Library General Public License for more details.
 *
Marc Lehmann's avatar
Marc Lehmann committed
17
 * You should have received a copy of the GNU Lesser General Public
18 19
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
20 21
 */

Sven Neumann's avatar
Sven Neumann committed
22 23
#include "config.h"

24
#include <glib-object.h>
25

26
#include "gimpmath.h"
27

28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/**
 * SECTION: gimpmatrix
 * @title: GimpMatrix
 * @short_description: Utilities to set up and manipulate 3x3
 *                     transformation matrices.
 * @see_also: #GimpVector2, #GimpVector3, #GimpVector4
 *
 * When doing image manipulation you will often need 3x3
 * transformation matrices that define translation, rotation, scaling,
 * shearing and arbitrary perspective transformations using a 3x3
 * matrix. Here you'll find a set of utility functions to set up those
 * matrices and to perform basic matrix manipulations and tests.
 *
 * Each matrix class has a 2 dimensional gdouble coeff member. The
 * element for row r and column c of the matrix is coeff[r][c].
 **/


47
#define EPSILON 1e-6
48

49

50 51 52 53 54 55 56 57 58
static GimpMatrix2 * matrix2_copy                  (const GimpMatrix2 *matrix);

/**
 * gimp_matrix2_get_type:
 *
 * Reveals the object type
 *
 * Returns: the #GType for Matrix2 objects
 *
59
 * Since: 2.4
60 61 62 63 64 65 66 67
 **/
GType
gimp_matrix2_get_type (void)
{
  static GType matrix_type = 0;

  if (!matrix_type)
    matrix_type = g_boxed_type_register_static ("GimpMatrix2",
Sven Neumann's avatar
Sven Neumann committed
68 69
                                               (GBoxedCopyFunc) matrix2_copy,
                                               (GBoxedFreeFunc) g_free);
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

  return matrix_type;
}


/*
 * GIMP_TYPE_PARAM_MATRIX2
 */

#define GIMP_PARAM_SPEC_MATRIX2(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), GIMP_TYPE_PARAM_MATRIX2, GimpParamSpecMatrix2))

static void   gimp_param_matrix2_class_init  (GParamSpecClass *class);
static void   gimp_param_matrix2_init        (GParamSpec      *pspec);
static void   gimp_param_matrix2_set_default (GParamSpec      *pspec,
                                              GValue          *value);
static gint   gimp_param_matrix2_values_cmp  (GParamSpec      *pspec,
                                              const GValue    *value1,
                                              const GValue    *value2);

typedef struct _GimpParamSpecMatrix2 GimpParamSpecMatrix2;

struct _GimpParamSpecMatrix2
{
  GParamSpecBoxed      parent_instance;

  GimpMatrix2          default_value;
};

/**
 * gimp_param_matrix2_get_type:
 *
 * Reveals the object type
 *
 * Returns: the #GType for a GimpMatrix2 object
 *
105
 * Since: 2.4
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 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 176 177 178 179 180 181 182 183 184 185 186 187 188
 **/
GType
gimp_param_matrix2_get_type (void)
{
  static GType spec_type = 0;

  if (!spec_type)
    {
      static const GTypeInfo type_info =
      {
        sizeof (GParamSpecClass),
        NULL, NULL,
        (GClassInitFunc) gimp_param_matrix2_class_init,
        NULL, NULL,
        sizeof (GimpParamSpecMatrix2),
        0,
        (GInstanceInitFunc) gimp_param_matrix2_init
      };

      spec_type = g_type_register_static (G_TYPE_PARAM_BOXED,
                                          "GimpParamMatrix2",
                                          &type_info, 0);
    }

  return spec_type;
}

static void
gimp_param_matrix2_class_init (GParamSpecClass *class)
{
  class->value_type        = GIMP_TYPE_MATRIX2;
  class->value_set_default = gimp_param_matrix2_set_default;
  class->values_cmp        = gimp_param_matrix2_values_cmp;
}

static void
gimp_param_matrix2_init (GParamSpec *pspec)
{
  GimpParamSpecMatrix2 *cspec = GIMP_PARAM_SPEC_MATRIX2 (pspec);

  gimp_matrix2_identity (&cspec->default_value);
}

static void
gimp_param_matrix2_set_default (GParamSpec *pspec,
                                GValue     *value)
{
  GimpParamSpecMatrix2 *cspec = GIMP_PARAM_SPEC_MATRIX2 (pspec);

  g_value_set_static_boxed (value, &cspec->default_value);
}

static gint
gimp_param_matrix2_values_cmp (GParamSpec   *pspec,
                               const GValue *value1,
                               const GValue *value2)
{
  GimpMatrix2 *matrix1;
  GimpMatrix2 *matrix2;
  gint         i, j;

  matrix1 = value1->data[0].v_pointer;
  matrix2 = value2->data[0].v_pointer;

  /*  try to return at least *something*, it's useless anyway...  */

  if (! matrix1)
    return matrix2 != NULL ? -1 : 0;
  else if (! matrix2)
    return matrix1 != NULL;

  for (i = 0; i < 2; i++)
    for (j = 0; j < 2; j++)
      if (matrix1->coeff[i][j] != matrix2->coeff[i][j])
        return 1;

  return 0;
}

/**
 * gimp_param_spec_matrix2:
 * @name:          Canonical name of the param
 * @nick:          Nickname of the param
189
 * @blurb:         Brief description of param.
190 191 192 193 194 195 196 197
 * @default_value: Value to use if none is assigned.
 * @flags:         a combination of #GParamFlags
 *
 * Creates a param spec to hold a #GimpMatrix2 value.
 * See g_param_spec_internal() for more information.
 *
 * Returns: a newly allocated #GParamSpec instance
 *
198
 * Since: 2.4
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
 **/
GParamSpec *
gimp_param_spec_matrix2 (const gchar       *name,
                         const gchar       *nick,
                         const gchar       *blurb,
                         const GimpMatrix2 *default_value,
                         GParamFlags        flags)
{
  GimpParamSpecMatrix2 *cspec;

  g_return_val_if_fail (default_value != NULL, NULL);

  cspec = g_param_spec_internal (GIMP_TYPE_PARAM_MATRIX2,
                                 name, nick, blurb, flags);

  cspec->default_value = *default_value;

  return G_PARAM_SPEC (cspec);
}


static GimpMatrix2 *
matrix2_copy (const GimpMatrix2 *matrix)
{
  return (GimpMatrix2 *) g_memdup (matrix, sizeof (GimpMatrix2));
}


227 228 229
/**
 * gimp_matrix2_identity:
 * @matrix: A matrix.
230
 *
231 232 233 234 235 236 237 238 239 240 241
 * Sets the matrix to the identity matrix.
 */
void
gimp_matrix2_identity (GimpMatrix2 *matrix)
{
  static const GimpMatrix2 identity = { { { 1.0, 0.0 },
                                          { 0.0, 1.0 } } };

  *matrix = identity;
}

242 243 244 245
/**
 * gimp_matrix2_mult:
 * @matrix1: The first input matrix.
 * @matrix2: The second input matrix which will be overwritten by the result.
246
 *
247 248 249
 * Multiplies two matrices and puts the result into the second one.
 */
void
250
gimp_matrix2_mult (const GimpMatrix2 *matrix1,
251
                   GimpMatrix2       *matrix2)
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
{
  GimpMatrix2  tmp;

  tmp.coeff[0][0] = (matrix1->coeff[0][0] * matrix2->coeff[0][0] +
                     matrix1->coeff[0][1] * matrix2->coeff[1][0]);
  tmp.coeff[0][1] = (matrix1->coeff[0][0] * matrix2->coeff[0][1] +
                     matrix1->coeff[0][1] * matrix2->coeff[1][1]);
  tmp.coeff[1][0] = (matrix1->coeff[1][0] * matrix2->coeff[0][0] +
                     matrix1->coeff[1][1] * matrix2->coeff[1][0]);
  tmp.coeff[1][1] = (matrix1->coeff[1][0] * matrix2->coeff[0][1] +
                     matrix1->coeff[1][1] * matrix2->coeff[1][1]);

  *matrix2 = tmp;
}

267 268 269 270 271 272 273 274 275 276

static GimpMatrix3 * matrix3_copy                  (const GimpMatrix3 *matrix);

/**
 * gimp_matrix3_get_type:
 *
 * Reveals the object type
 *
 * Returns: the #GType for Matrix3 objects
 *
277
 * Since: 2.8
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 318 319 320 321 322
 **/
GType
gimp_matrix3_get_type (void)
{
  static GType matrix_type = 0;

  if (!matrix_type)
    matrix_type = g_boxed_type_register_static ("GimpMatrix3",
                                               (GBoxedCopyFunc) matrix3_copy,
                                               (GBoxedFreeFunc) g_free);

  return matrix_type;
}


/*
 * GIMP_TYPE_PARAM_MATRIX3
 */

#define GIMP_PARAM_SPEC_MATRIX3(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), GIMP_TYPE_PARAM_MATRIX3, GimpParamSpecMatrix3))

static void   gimp_param_matrix3_class_init  (GParamSpecClass *class);
static void   gimp_param_matrix3_init        (GParamSpec      *pspec);
static void   gimp_param_matrix3_set_default (GParamSpec      *pspec,
                                              GValue          *value);
static gint   gimp_param_matrix3_values_cmp  (GParamSpec      *pspec,
                                              const GValue    *value1,
                                              const GValue    *value2);

typedef struct _GimpParamSpecMatrix3 GimpParamSpecMatrix3;

struct _GimpParamSpecMatrix3
{
  GParamSpecBoxed      parent_instance;

  GimpMatrix3          default_value;
};

/**
 * gimp_param_matrix3_get_type:
 *
 * Reveals the object type
 *
 * Returns: the #GType for a GimpMatrix3 object
 *
323
 * Since: 2.8
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
 **/
GType
gimp_param_matrix3_get_type (void)
{
  static GType spec_type = 0;

  if (!spec_type)
    {
      static const GTypeInfo type_info =
      {
        sizeof (GParamSpecClass),
        NULL, NULL,
        (GClassInitFunc) gimp_param_matrix3_class_init,
        NULL, NULL,
        sizeof (GimpParamSpecMatrix3),
        0,
        (GInstanceInitFunc) gimp_param_matrix3_init
      };

      spec_type = g_type_register_static (G_TYPE_PARAM_BOXED,
                                          "GimpParamMatrix3",
                                          &type_info, 0);
    }

  return spec_type;
}

static void
gimp_param_matrix3_class_init (GParamSpecClass *class)
{
  class->value_type        = GIMP_TYPE_MATRIX3;
  class->value_set_default = gimp_param_matrix3_set_default;
  class->values_cmp        = gimp_param_matrix3_values_cmp;
}

static void
gimp_param_matrix3_init (GParamSpec *pspec)
{
  GimpParamSpecMatrix3 *cspec = GIMP_PARAM_SPEC_MATRIX3 (pspec);

  gimp_matrix3_identity (&cspec->default_value);
}

static void
gimp_param_matrix3_set_default (GParamSpec *pspec,
                                GValue     *value)
{
  GimpParamSpecMatrix3 *cspec = GIMP_PARAM_SPEC_MATRIX3 (pspec);

  g_value_set_static_boxed (value, &cspec->default_value);
}

static gint
gimp_param_matrix3_values_cmp (GParamSpec   *pspec,
                               const GValue *value1,
                               const GValue *value2)
{
  GimpMatrix3 *matrix1;
  GimpMatrix3 *matrix2;
  gint         i, j;

  matrix1 = value1->data[0].v_pointer;
  matrix2 = value2->data[0].v_pointer;

  /*  try to return at least *something*, it's useless anyway...  */

  if (! matrix1)
    return matrix2 != NULL ? -1 : 0;
  else if (! matrix2)
    return matrix1 != NULL;

  for (i = 0; i < 3; i++)
    for (j = 0; j < 3; j++)
      if (matrix1->coeff[i][j] != matrix2->coeff[i][j])
        return 1;

  return 0;
}

/**
 * gimp_param_spec_matrix3:
 * @name:          Canonical name of the param
 * @nick:          Nickname of the param
407
 * @blurb:         Brief description of param.
408 409 410 411 412 413 414 415
 * @default_value: Value to use if none is assigned.
 * @flags:         a combination of #GParamFlags
 *
 * Creates a param spec to hold a #GimpMatrix3 value.
 * See g_param_spec_internal() for more information.
 *
 * Returns: a newly allocated #GParamSpec instance
 *
416
 * Since: 2.8
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
 **/
GParamSpec *
gimp_param_spec_matrix3 (const gchar       *name,
                         const gchar       *nick,
                         const gchar       *blurb,
                         const GimpMatrix3 *default_value,
                         GParamFlags        flags)
{
  GimpParamSpecMatrix3 *cspec;

  cspec = g_param_spec_internal (GIMP_TYPE_PARAM_MATRIX3,
                                 name, nick, blurb, flags);

  if (default_value)
    cspec->default_value = *default_value;

  return G_PARAM_SPEC (cspec);
}


static GimpMatrix3 *
matrix3_copy (const GimpMatrix3 *matrix)
{
  return (GimpMatrix3 *) g_memdup (matrix, sizeof (GimpMatrix3));
}


444 445 446
/**
 * gimp_matrix3_identity:
 * @matrix: A matrix.
447
 *
448 449 450 451 452 453 454 455 456 457 458
 * Sets the matrix to the identity matrix.
 */
void
gimp_matrix3_identity (GimpMatrix3 *matrix)
{
  static const GimpMatrix3 identity = { { { 1.0, 0.0, 0.0 },
                                          { 0.0, 1.0, 0.0 },
                                          { 0.0, 0.0, 1.0 } } };

  *matrix = identity;
}
Sven Neumann's avatar
Sven Neumann committed
459 460

/**
461
 * gimp_matrix3_transform_point:
Sven Neumann's avatar
Sven Neumann committed
462 463 464 465 466
 * @matrix: The transformation matrix.
 * @x: The source X coordinate.
 * @y: The source Y coordinate.
 * @newx: The transformed X coordinate.
 * @newy: The transformed Y coordinate.
467
 *
Sven Neumann's avatar
Sven Neumann committed
468 469
 * Transforms a point in 2D as specified by the transformation matrix.
 */
470
void
471
gimp_matrix3_transform_point (const GimpMatrix3 *matrix,
472 473 474 475
                              gdouble            x,
                              gdouble            y,
                              gdouble           *newx,
                              gdouble           *newy)
476
{
477
  gdouble  w;
Sven Neumann's avatar
Sven Neumann committed
478

479
  w = matrix->coeff[2][0] * x + matrix->coeff[2][1] * y + matrix->coeff[2][2];
480 481 482 483 484 485

  if (w == 0.0)
    w = 1.0;
  else
    w = 1.0/w;

486 487 488 489 490 491
  *newx = (matrix->coeff[0][0] * x +
           matrix->coeff[0][1] * y +
           matrix->coeff[0][2]) * w;
  *newy = (matrix->coeff[1][0] * x +
           matrix->coeff[1][1] * y +
           matrix->coeff[1][2]) * w;
492 493
}

Sven Neumann's avatar
Sven Neumann committed
494
/**
495
 * gimp_matrix3_mult:
Sven Neumann's avatar
Sven Neumann committed
496
 * @matrix1: The first input matrix.
497
 * @matrix2: The second input matrix which will be overwritten by the result.
498
 *
Sven Neumann's avatar
Sven Neumann committed
499 500
 * Multiplies two matrices and puts the result into the second one.
 */
501
void
502
gimp_matrix3_mult (const GimpMatrix3 *matrix1,
503
                   GimpMatrix3       *matrix2)
504
{
505 506 507
  gint         i, j;
  GimpMatrix3  tmp;
  gdouble      t1, t2, t3;
508 509 510

  for (i = 0; i < 3; i++)
    {
511 512 513 514
      t1 = matrix1->coeff[i][0];
      t2 = matrix1->coeff[i][1];
      t3 = matrix1->coeff[i][2];

Sven Neumann's avatar
Sven Neumann committed
515
      for (j = 0; j < 3; j++)
516 517 518 519 520
        {
          tmp.coeff[i][j]  = t1 * matrix2->coeff[0][j];
          tmp.coeff[i][j] += t2 * matrix2->coeff[1][j];
          tmp.coeff[i][j] += t3 * matrix2->coeff[2][j];
        }
521
    }
Sven Neumann's avatar
Sven Neumann committed
522

523
  *matrix2 = tmp;
524 525
}

Sven Neumann's avatar
Sven Neumann committed
526
/**
527
 * gimp_matrix3_translate:
Sven Neumann's avatar
Sven Neumann committed
528 529 530
 * @matrix: The matrix that is to be translated.
 * @x: Translation in X direction.
 * @y: Translation in Y direction.
531
 *
Sven Neumann's avatar
Sven Neumann committed
532 533
 * Translates the matrix by x and y.
 */
534
void
535
gimp_matrix3_translate (GimpMatrix3 *matrix,
536 537
                        gdouble      x,
                        gdouble      y)
538
{
Sven Neumann's avatar
Sven Neumann committed
539 540
  gdouble g, h, i;

541 542 543
  g = matrix->coeff[2][0];
  h = matrix->coeff[2][1];
  i = matrix->coeff[2][2];
Sven Neumann's avatar
Sven Neumann committed
544

545 546 547 548 549 550
  matrix->coeff[0][0] += x * g;
  matrix->coeff[0][1] += x * h;
  matrix->coeff[0][2] += x * i;
  matrix->coeff[1][0] += y * g;
  matrix->coeff[1][1] += y * h;
  matrix->coeff[1][2] += y * i;
551 552
}

Sven Neumann's avatar
Sven Neumann committed
553
/**
554
 * gimp_matrix3_scale:
Sven Neumann's avatar
Sven Neumann committed
555 556 557
 * @matrix: The matrix that is to be scaled.
 * @x: X scale factor.
 * @y: Y scale factor.
558 559
 *
 * Scales the matrix by x and y
Sven Neumann's avatar
Sven Neumann committed
560
 */
561
void
562
gimp_matrix3_scale (GimpMatrix3 *matrix,
563 564
                    gdouble      x,
                    gdouble      y)
565
{
566 567 568
  matrix->coeff[0][0] *= x;
  matrix->coeff[0][1] *= x;
  matrix->coeff[0][2] *= x;
569

570 571 572
  matrix->coeff[1][0] *= y;
  matrix->coeff[1][1] *= y;
  matrix->coeff[1][2] *= y;
573 574
}

Sven Neumann's avatar
Sven Neumann committed
575
/**
576
 * gimp_matrix3_rotate:
Sven Neumann's avatar
Sven Neumann committed
577 578
 * @matrix: The matrix that is to be rotated.
 * @theta: The angle of rotation (in radians).
579
 *
Sven Neumann's avatar
Sven Neumann committed
580 581
 * Rotates the matrix by theta degrees.
 */
582
void
583
gimp_matrix3_rotate (GimpMatrix3 *matrix,
584
                     gdouble      theta)
585
{
Sven Neumann's avatar
Sven Neumann committed
586 587
  gdouble t1, t2;
  gdouble cost, sint;
588

589 590
  cost = cos (theta);
  sint = sin (theta);
591

592 593 594 595 596 597 598 599 600 601 602 603 604 605
  t1 = matrix->coeff[0][0];
  t2 = matrix->coeff[1][0];
  matrix->coeff[0][0] = cost * t1 - sint * t2;
  matrix->coeff[1][0] = sint * t1 + cost * t2;

  t1 = matrix->coeff[0][1];
  t2 = matrix->coeff[1][1];
  matrix->coeff[0][1] = cost * t1 - sint * t2;
  matrix->coeff[1][1] = sint * t1 + cost * t2;

  t1 = matrix->coeff[0][2];
  t2 = matrix->coeff[1][2];
  matrix->coeff[0][2] = cost * t1 - sint * t2;
  matrix->coeff[1][2] = sint * t1 + cost * t2;
606 607
}

Sven Neumann's avatar
Sven Neumann committed
608
/**
609
 * gimp_matrix3_xshear:
Sven Neumann's avatar
Sven Neumann committed
610 611
 * @matrix: The matrix that is to be sheared.
 * @amount: X shear amount.
612
 *
Sven Neumann's avatar
Sven Neumann committed
613 614
 * Shears the matrix in the X direction.
 */
615
void
616
gimp_matrix3_xshear (GimpMatrix3 *matrix,
617
                     gdouble      amount)
618
{
619 620 621
  matrix->coeff[0][0] += amount * matrix->coeff[1][0];
  matrix->coeff[0][1] += amount * matrix->coeff[1][1];
  matrix->coeff[0][2] += amount * matrix->coeff[1][2];
622 623
}

Sven Neumann's avatar
Sven Neumann committed
624
/**
625
 * gimp_matrix3_yshear:
Sven Neumann's avatar
Sven Neumann committed
626 627
 * @matrix: The matrix that is to be sheared.
 * @amount: Y shear amount.
628
 *
Sven Neumann's avatar
Sven Neumann committed
629 630
 * Shears the matrix in the Y direction.
 */
631
void
632
gimp_matrix3_yshear (GimpMatrix3 *matrix,
633
                     gdouble      amount)
634
{
635 636 637
  matrix->coeff[1][0] += amount * matrix->coeff[0][0];
  matrix->coeff[1][1] += amount * matrix->coeff[0][1];
  matrix->coeff[1][2] += amount * matrix->coeff[0][2];
638 639
}

640 641 642
/**
 * gimp_matrix3_affine:
 * @matrix: The input matrix.
643 644 645 646 647 648
 * @a: the 'a' coefficient
 * @b: the 'b' coefficient
 * @c: the 'c' coefficient
 * @d: the 'd' coefficient
 * @e: the 'e' coefficient
 * @f: the 'f' coefficient
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
 *
 * Applies the affine transformation given by six values to @matrix.
 * The six values form define an affine transformation matrix as
 * illustrated below:
 *
 *  ( a c e )
 *  ( b d f )
 *  ( 0 0 1 )
 **/
void
gimp_matrix3_affine (GimpMatrix3 *matrix,
                     gdouble      a,
                     gdouble      b,
                     gdouble      c,
                     gdouble      d,
                     gdouble      e,
                     gdouble      f)
{
  GimpMatrix3 affine;

  affine.coeff[0][0] = a;
  affine.coeff[1][0] = b;
  affine.coeff[2][0] = 0.0;

  affine.coeff[0][1] = c;
  affine.coeff[1][1] = d;
  affine.coeff[2][1] = 0.0;

  affine.coeff[0][2] = e;
  affine.coeff[1][2] = f;
  affine.coeff[2][2] = 1.0;

  gimp_matrix3_mult (&affine, matrix);
}

Sven Neumann's avatar
Sven Neumann committed
684
/**
685
 * gimp_matrix3_determinant:
686 687
 * @matrix: The input matrix.
 *
Sven Neumann's avatar
Sven Neumann committed
688
 * Calculates the determinant of the given matrix.
689
 *
Sven Neumann's avatar
Sven Neumann committed
690 691 692
 * Returns: The determinant.
 */
gdouble
693
gimp_matrix3_determinant (const GimpMatrix3 *matrix)
694
{
Sven Neumann's avatar
Sven Neumann committed
695
  gdouble determinant;
696

697 698 699 700 701 702 703 704 705
  determinant  = (matrix->coeff[0][0] *
                  (matrix->coeff[1][1] * matrix->coeff[2][2] -
                   matrix->coeff[1][2] * matrix->coeff[2][1]));
  determinant -= (matrix->coeff[1][0] *
                  (matrix->coeff[0][1] * matrix->coeff[2][2] -
                   matrix->coeff[0][2] * matrix->coeff[2][1]));
  determinant += (matrix->coeff[2][0] *
                  (matrix->coeff[0][1] * matrix->coeff[1][2] -
                   matrix->coeff[0][2] * matrix->coeff[1][1]));
706 707 708 709

  return determinant;
}

Sven Neumann's avatar
Sven Neumann committed
710
/**
711
 * gimp_matrix3_invert:
Sven Neumann's avatar
Sven Neumann committed
712
 * @matrix: The matrix that is to be inverted.
713
 *
Sven Neumann's avatar
Sven Neumann committed
714 715
 * Inverts the given matrix.
 */
716
void
717
gimp_matrix3_invert (GimpMatrix3 *matrix)
718
{
719 720
  GimpMatrix3 inv;
  gdouble     det;
Sven Neumann's avatar
Sven Neumann committed
721

722
  det = gimp_matrix3_determinant (matrix);
723

724
  if (det == 0.0)
725
    return;
Sven Neumann's avatar
Sven Neumann committed
726

727
  det = 1.0 / det;
728

729 730
  inv.coeff[0][0] =   (matrix->coeff[1][1] * matrix->coeff[2][2] -
                       matrix->coeff[1][2] * matrix->coeff[2][1]) * det;
731

732 733
  inv.coeff[1][0] = - (matrix->coeff[1][0] * matrix->coeff[2][2] -
                       matrix->coeff[1][2] * matrix->coeff[2][0]) * det;
Sven Neumann's avatar
Sven Neumann committed
734

735 736
  inv.coeff[2][0] =   (matrix->coeff[1][0] * matrix->coeff[2][1] -
                       matrix->coeff[1][1] * matrix->coeff[2][0]) * det;
Sven Neumann's avatar
Sven Neumann committed
737

738 739
  inv.coeff[0][1] = - (matrix->coeff[0][1] * matrix->coeff[2][2] -
                       matrix->coeff[0][2] * matrix->coeff[2][1]) * det;
Sven Neumann's avatar
Sven Neumann committed
740

741 742
  inv.coeff[1][1] =   (matrix->coeff[0][0] * matrix->coeff[2][2] -
                       matrix->coeff[0][2] * matrix->coeff[2][0]) * det;
Sven Neumann's avatar
Sven Neumann committed
743

744 745
  inv.coeff[2][1] = - (matrix->coeff[0][0] * matrix->coeff[2][1] -
                       matrix->coeff[0][1] * matrix->coeff[2][0]) * det;
746

747 748
  inv.coeff[0][2] =   (matrix->coeff[0][1] * matrix->coeff[1][2] -
                       matrix->coeff[0][2] * matrix->coeff[1][1]) * det;
749

750 751
  inv.coeff[1][2] = - (matrix->coeff[0][0] * matrix->coeff[1][2] -
                       matrix->coeff[0][2] * matrix->coeff[1][0]) * det;
752

753 754
  inv.coeff[2][2] =   (matrix->coeff[0][0] * matrix->coeff[1][1] -
                       matrix->coeff[0][1] * matrix->coeff[1][0]) * det;
Sven Neumann's avatar
Sven Neumann committed
755

756
  *matrix = inv;
757 758 759 760 761
}


/*  functions to test for matrix properties  */

Sven Neumann's avatar
Sven Neumann committed
762
/**
Sven Neumann's avatar
Sven Neumann committed
763
 * gimp_matrix3_is_identity:
Sven Neumann's avatar
Sven Neumann committed
764
 * @matrix: The matrix that is to be tested.
765
 *
Sven Neumann's avatar
Sven Neumann committed
766
 * Checks if the given matrix is the identity matrix.
767
 *
Sven Neumann's avatar
Sven Neumann committed
768
 * Returns: %TRUE if the matrix is the identity matrix, %FALSE otherwise
Sven Neumann's avatar
Sven Neumann committed
769 770
 */
gboolean
Sven Neumann's avatar
Sven Neumann committed
771
gimp_matrix3_is_identity (const GimpMatrix3 *matrix)
772
{
Sven Neumann's avatar
Sven Neumann committed
773
  gint i, j;
774 775 776 777

  for (i = 0; i < 3; i++)
    {
      for (j = 0; j < 3; j++)
778
        {
Sven Neumann's avatar
Sven Neumann committed
779 780 781 782 783 784 785 786 787 788
          if (i == j)
            {
              if (fabs (matrix->coeff[i][j] - 1.0) > EPSILON)
                return FALSE;
            }
          else
            {
              if (fabs (matrix->coeff[i][j]) > EPSILON)
                return FALSE;
            }
789
        }
790
    }
Sven Neumann's avatar
Sven Neumann committed
791

792 793 794
  return TRUE;
}

Sven Neumann's avatar
Sven Neumann committed
795
/**
Sven Neumann's avatar
Sven Neumann committed
796
 * gimp_matrix3_is_diagonal:
Sven Neumann's avatar
Sven Neumann committed
797
 * @matrix: The matrix that is to be tested.
798
 *
Sven Neumann's avatar
Sven Neumann committed
799
 * Checks if the given matrix is diagonal.
800
 *
Sven Neumann's avatar
Sven Neumann committed
801
 * Returns: %TRUE if the matrix is diagonal, %FALSE otherwise
Sven Neumann's avatar
Sven Neumann committed
802 803
 */
gboolean
Sven Neumann's avatar
Sven Neumann committed
804
gimp_matrix3_is_diagonal (const GimpMatrix3 *matrix)
805
{
806
  gint i, j;
807 808 809 810

  for (i = 0; i < 3; i++)
    {
      for (j = 0; j < 3; j++)
811
        {
Sven Neumann's avatar
Sven Neumann committed
812 813
          if (i != j && fabs (matrix->coeff[i][j]) > EPSILON)
            return FALSE;
814
        }
815
    }
Sven Neumann's avatar
Sven Neumann committed
816

817
  return TRUE;
818
}
819

Sven Neumann's avatar
Sven Neumann committed
820 821 822 823 824 825 826 827 828
/**
 * gimp_matrix3_is_affine:
 * @matrix: The matrix that is to be tested.
 *
 * Checks if the given matrix defines an affine transformation.
 *
 * Returns: %TRUE if the matrix defines an affine transformation,
 *          %FALSE otherwise
 *
829
 * Since: 2.4
830
 */
Sven Neumann's avatar
Sven Neumann committed
831 832 833 834 835 836 837
gboolean
gimp_matrix3_is_affine (const GimpMatrix3 *matrix)
{
  return (fabs (matrix->coeff[2][0]) < EPSILON &&
          fabs (matrix->coeff[2][1]) < EPSILON &&
          fabs (matrix->coeff[2][2] - 1.0) < EPSILON);
}
Sven Neumann's avatar
Sven Neumann committed
838 839

/**
840
 * gimp_matrix3_is_simple:
Sven Neumann's avatar
Sven Neumann committed
841
 * @matrix: The matrix that is to be tested.
842
 *
Sven Neumann's avatar
Sven Neumann committed
843 844
 * Checks if we'll need to interpolate when applying this matrix as
 * a transformation.
845
 *
Sven Neumann's avatar
Sven Neumann committed
846 847
 * Returns: %TRUE if all entries of the upper left 2x2 matrix are
 *          either 0 or 1, %FALSE otherwise
Sven Neumann's avatar
Sven Neumann committed
848 849
 */
gboolean
850
gimp_matrix3_is_simple (const GimpMatrix3 *matrix)
851
{
Sven Neumann's avatar
Sven Neumann committed
852
  gdouble absm;
853
  gint    i, j;
854 855 856 857

  for (i = 0; i < 2; i++)
    {
      for (j = 0; j < 2; j++)
858 859 860 861 862
        {
          absm = fabs (matrix->coeff[i][j]);
          if (absm > EPSILON && fabs (absm - 1.0) > EPSILON)
            return FALSE;
        }
863
    }
Sven Neumann's avatar
Sven Neumann committed
864

865 866
  return TRUE;
}
867

868 869 870 871 872 873 874 875 876
/**
 * gimp_matrix4_to_deg:
 * @matrix:
 * @a:
 * @b:
 * @c:
 *
 *
 **/
877
void
878
gimp_matrix4_to_deg (const GimpMatrix4 *matrix,
879 880 881
                     gdouble           *a,
                     gdouble           *b,
                     gdouble           *c)
882
{
883 884 885
  *a = 180 * (asin (matrix->coeff[1][0]) / G_PI_2);
  *b = 180 * (asin (matrix->coeff[2][0]) / G_PI_2);
  *c = 180 * (asin (matrix->coeff[2][1]) / G_PI_2);
886
}