~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to libgimpmath/gimpmatrix.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3
3
 *
4
4
 * gimpmatrix.c
5
 
 * Copyright (C) 1998 Jay Cox <jaycox@earthlink.net>
 
5
 * Copyright (C) 1998 Jay Cox <jaycox@gimp.org>
6
6
 *
7
7
 * This library is free software; you can redistribute it and/or
8
8
 * modify it under the terms of the GNU Lesser General Public
22
22
 
23
23
#include "config.h"
24
24
 
25
 
#include <glib.h>
 
25
#include <glib-object.h>
26
26
 
27
27
#include "gimpmath.h"
28
28
 
30
30
#define EPSILON 1e-6
31
31
 
32
32
 
 
33
static GimpMatrix2 * matrix2_copy                  (const GimpMatrix2 *matrix);
 
34
 
 
35
/**
 
36
 * gimp_matrix2_get_type:
 
37
 *
 
38
 * Reveals the object type
 
39
 *
 
40
 * Returns: the #GType for Matrix2 objects
 
41
 *
 
42
 * Since: GIMP 2.4
 
43
 **/
 
44
GType
 
45
gimp_matrix2_get_type (void)
 
46
{
 
47
  static GType matrix_type = 0;
 
48
 
 
49
  if (!matrix_type)
 
50
    matrix_type = g_boxed_type_register_static ("GimpMatrix2",
 
51
                                               (GBoxedCopyFunc) matrix2_copy,
 
52
                                               (GBoxedFreeFunc) g_free);
 
53
 
 
54
  return matrix_type;
 
55
}
 
56
 
 
57
 
 
58
/*
 
59
 * GIMP_TYPE_PARAM_MATRIX2
 
60
 */
 
61
 
 
62
#define GIMP_PARAM_SPEC_MATRIX2(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), GIMP_TYPE_PARAM_MATRIX2, GimpParamSpecMatrix2))
 
63
 
 
64
static void   gimp_param_matrix2_class_init  (GParamSpecClass *class);
 
65
static void   gimp_param_matrix2_init        (GParamSpec      *pspec);
 
66
static void   gimp_param_matrix2_set_default (GParamSpec      *pspec,
 
67
                                              GValue          *value);
 
68
static gint   gimp_param_matrix2_values_cmp  (GParamSpec      *pspec,
 
69
                                              const GValue    *value1,
 
70
                                              const GValue    *value2);
 
71
 
 
72
typedef struct _GimpParamSpecMatrix2 GimpParamSpecMatrix2;
 
73
 
 
74
struct _GimpParamSpecMatrix2
 
75
{
 
76
  GParamSpecBoxed      parent_instance;
 
77
 
 
78
  GimpMatrix2          default_value;
 
79
};
 
80
 
 
81
/**
 
82
 * gimp_param_matrix2_get_type:
 
83
 *
 
84
 * Reveals the object type
 
85
 *
 
86
 * Returns: the #GType for a GimpMatrix2 object
 
87
 *
 
88
 * Since: GIMP 2.4
 
89
 **/
 
90
GType
 
91
gimp_param_matrix2_get_type (void)
 
92
{
 
93
  static GType spec_type = 0;
 
94
 
 
95
  if (!spec_type)
 
96
    {
 
97
      static const GTypeInfo type_info =
 
98
      {
 
99
        sizeof (GParamSpecClass),
 
100
        NULL, NULL,
 
101
        (GClassInitFunc) gimp_param_matrix2_class_init,
 
102
        NULL, NULL,
 
103
        sizeof (GimpParamSpecMatrix2),
 
104
        0,
 
105
        (GInstanceInitFunc) gimp_param_matrix2_init
 
106
      };
 
107
 
 
108
      spec_type = g_type_register_static (G_TYPE_PARAM_BOXED,
 
109
                                          "GimpParamMatrix2",
 
110
                                          &type_info, 0);
 
111
    }
 
112
 
 
113
  return spec_type;
 
114
}
 
115
 
 
116
static void
 
117
gimp_param_matrix2_class_init (GParamSpecClass *class)
 
118
{
 
119
  class->value_type        = GIMP_TYPE_MATRIX2;
 
120
  class->value_set_default = gimp_param_matrix2_set_default;
 
121
  class->values_cmp        = gimp_param_matrix2_values_cmp;
 
122
}
 
123
 
 
124
static void
 
125
gimp_param_matrix2_init (GParamSpec *pspec)
 
126
{
 
127
  GimpParamSpecMatrix2 *cspec = GIMP_PARAM_SPEC_MATRIX2 (pspec);
 
128
 
 
129
  gimp_matrix2_identity (&cspec->default_value);
 
130
}
 
131
 
 
132
static void
 
133
gimp_param_matrix2_set_default (GParamSpec *pspec,
 
134
                                GValue     *value)
 
135
{
 
136
  GimpParamSpecMatrix2 *cspec = GIMP_PARAM_SPEC_MATRIX2 (pspec);
 
137
 
 
138
  g_value_set_static_boxed (value, &cspec->default_value);
 
139
}
 
140
 
 
141
static gint
 
142
gimp_param_matrix2_values_cmp (GParamSpec   *pspec,
 
143
                               const GValue *value1,
 
144
                               const GValue *value2)
 
145
{
 
146
  GimpMatrix2 *matrix1;
 
147
  GimpMatrix2 *matrix2;
 
148
  gint         i, j;
 
149
 
 
150
  matrix1 = value1->data[0].v_pointer;
 
151
  matrix2 = value2->data[0].v_pointer;
 
152
 
 
153
  /*  try to return at least *something*, it's useless anyway...  */
 
154
 
 
155
  if (! matrix1)
 
156
    return matrix2 != NULL ? -1 : 0;
 
157
  else if (! matrix2)
 
158
    return matrix1 != NULL;
 
159
 
 
160
  for (i = 0; i < 2; i++)
 
161
    for (j = 0; j < 2; j++)
 
162
      if (matrix1->coeff[i][j] != matrix2->coeff[i][j])
 
163
        return 1;
 
164
 
 
165
  return 0;
 
166
}
 
167
 
 
168
/**
 
169
 * gimp_param_spec_matrix2:
 
170
 * @name:          Canonical name of the param
 
171
 * @nick:          Nickname of the param
 
172
 * @blurb:         Brief desciption of param.
 
173
 * @default_value: Value to use if none is assigned.
 
174
 * @flags:         a combination of #GParamFlags
 
175
 *
 
176
 * Creates a param spec to hold a #GimpMatrix2 value.
 
177
 * See g_param_spec_internal() for more information.
 
178
 *
 
179
 * Returns: a newly allocated #GParamSpec instance
 
180
 *
 
181
 * Since: GIMP 2.4
 
182
 **/
 
183
GParamSpec *
 
184
gimp_param_spec_matrix2 (const gchar       *name,
 
185
                         const gchar       *nick,
 
186
                         const gchar       *blurb,
 
187
                         const GimpMatrix2 *default_value,
 
188
                         GParamFlags        flags)
 
189
{
 
190
  GimpParamSpecMatrix2 *cspec;
 
191
 
 
192
  g_return_val_if_fail (default_value != NULL, NULL);
 
193
 
 
194
  cspec = g_param_spec_internal (GIMP_TYPE_PARAM_MATRIX2,
 
195
                                 name, nick, blurb, flags);
 
196
 
 
197
  cspec->default_value = *default_value;
 
198
 
 
199
  return G_PARAM_SPEC (cspec);
 
200
}
 
201
 
 
202
 
 
203
static GimpMatrix2 *
 
204
matrix2_copy (const GimpMatrix2 *matrix)
 
205
{
 
206
  return (GimpMatrix2 *) g_memdup (matrix, sizeof (GimpMatrix2));
 
207
}
 
208
 
 
209
 
33
210
/**
34
211
 * gimp_matrix2_identity:
35
212
 * @matrix: A matrix.
266
443
  matrix->coeff[1][2] += amount * matrix->coeff[0][2];
267
444
}
268
445
 
269
 
 
270
446
/**
271
447
 * gimp_matrix3_affine:
272
448
 * @matrix: The input matrix.
311
487
  gimp_matrix3_mult (&affine, matrix);
312
488
}
313
489
 
314
 
 
315
490
/**
316
491
 * gimp_matrix3_determinant:
317
492
 * @matrix: The input matrix.
390
565
 
391
566
/*  functions to test for matrix properties  */
392
567
 
393
 
 
394
 
/**
395
 
 * gimp_matrix3_is_diagonal:
396
 
 * @matrix: The matrix that is to be tested.
397
 
 *
398
 
 * Checks if the given matrix is diagonal.
399
 
 *
400
 
 * Returns: TRUE if the matrix is diagonal.
401
 
 */
402
 
gboolean
403
 
gimp_matrix3_is_diagonal (const GimpMatrix3 *matrix)
404
 
{
405
 
  gint i, j;
406
 
 
407
 
  for (i = 0; i < 3; i++)
408
 
    {
409
 
      for (j = 0; j < 3; j++)
410
 
        {
411
 
          if (i != j && fabs (matrix->coeff[i][j]) > EPSILON)
412
 
            return FALSE;
413
 
        }
414
 
    }
415
 
 
416
 
  return TRUE;
417
 
}
418
 
 
419
568
/**
420
569
 * gimp_matrix3_is_identity:
421
570
 * @matrix: The matrix that is to be tested.
422
571
 *
423
572
 * Checks if the given matrix is the identity matrix.
424
573
 *
425
 
 * Returns: TRUE if the matrix is the identity matrix.
 
574
 * Returns: %TRUE if the matrix is the identity matrix, %FALSE otherwise
426
575
 */
427
576
gboolean
428
577
gimp_matrix3_is_identity (const GimpMatrix3 *matrix)
449
598
  return TRUE;
450
599
}
451
600
 
452
 
/*  Check if we'll need to interpolate when applying this matrix.
453
 
    This function returns TRUE if all entries of the upper left
454
 
    2x2 matrix are either 0 or 1
455
 
 */
456
 
 
 
601
/**
 
602
 * gimp_matrix3_is_diagonal:
 
603
 * @matrix: The matrix that is to be tested.
 
604
 *
 
605
 * Checks if the given matrix is diagonal.
 
606
 *
 
607
 * Returns: %TRUE if the matrix is diagonal, %FALSE otherwise
 
608
 */
 
609
gboolean
 
610
gimp_matrix3_is_diagonal (const GimpMatrix3 *matrix)
 
611
{
 
612
  gint i, j;
 
613
 
 
614
  for (i = 0; i < 3; i++)
 
615
    {
 
616
      for (j = 0; j < 3; j++)
 
617
        {
 
618
          if (i != j && fabs (matrix->coeff[i][j]) > EPSILON)
 
619
            return FALSE;
 
620
        }
 
621
    }
 
622
 
 
623
  return TRUE;
 
624
}
 
625
 
 
626
/**
 
627
 * gimp_matrix3_is_affine:
 
628
 * @matrix: The matrix that is to be tested.
 
629
 *
 
630
 * Checks if the given matrix defines an affine transformation.
 
631
 *
 
632
 * Returns: %TRUE if the matrix defines an affine transformation,
 
633
 *          %FALSE otherwise
 
634
 *
 
635
 * Since: GIMP 2.4
 
636
 */
 
637
gboolean
 
638
gimp_matrix3_is_affine (const GimpMatrix3 *matrix)
 
639
{
 
640
  return (fabs (matrix->coeff[2][0]) < EPSILON &&
 
641
          fabs (matrix->coeff[2][1]) < EPSILON &&
 
642
          fabs (matrix->coeff[2][2] - 1.0) < EPSILON);
 
643
}
457
644
 
458
645
/**
459
646
 * gimp_matrix3_is_simple:
462
649
 * Checks if we'll need to interpolate when applying this matrix as
463
650
 * a transformation.
464
651
 *
465
 
 * Returns: TRUE if all entries of the upper left 2x2 matrix are either
466
 
 * 0 or 1
 
652
 * Returns: %TRUE if all entries of the upper left 2x2 matrix are
 
653
 *          either 0 or 1, %FALSE otherwise
467
654
 */
468
655
gboolean
469
656
gimp_matrix3_is_simple (const GimpMatrix3 *matrix)