image-compare.c 6.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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 2010 Øyvind Kolås <pippin@gimp.org>
 *           2012 Ville Sokk <ville.sokk@gmail.com>
18
 *           2013 Téo Mazars <teo.mazars@ensimag.fr>
19 20 21 22 23 24
 */

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

25
#ifdef GEGL_PROPERTIES
26

27 28
property_int (wrong_pixels, _("Wrong pixels"), 0)
    description(_("Number of differing pixels."))
29

30 31
property_double (max_diff, _("Maximum difference"), 0.0)
    description(_("Maximum difference between two pixels."))
32

33 34
property_double (avg_diff_wrong, _("Average difference (wrong)"), 0.0)
    description(_("Average difference between wrong pixels."))
35

36 37
property_double (avg_diff_total, _("Average difference (total)"), 0.0)
    description(_("Average difference between all pixels."))
38 39 40

#else

41 42
#define GEGL_OP_COMPOSER
#define GEGL_OP_C_FILE       "image-compare.c"
43

44
#include "gegl-op.h"
45

46 47
#define ERROR_TOLERANCE 0.01
#define SQR(x)          ((x) * (x))
48 49 50 51

static void
prepare (GeglOperation *self)
{
52 53
  gegl_operation_set_format (self, "input",  babl_format ("CIE Lab alpha float"));
  gegl_operation_set_format (self, "aux",    babl_format ("CIE Lab alpha float"));
54 55 56 57 58 59 60 61
  gegl_operation_set_format (self, "output", babl_format ("R'G'B' u8"));
}

static GeglRectangle
get_required_for_output (GeglOperation       *operation,
                         const gchar         *input_pad,
                         const GeglRectangle *region)
{
62
  return *gegl_operation_source_get_bounding_box (operation, "input");
63 64
}

65 66 67 68 69 70
static GeglRectangle
get_cached_region (GeglOperation       *operation,
                   const GeglRectangle *roi)
{
  return *gegl_operation_source_get_bounding_box (operation, "input");
}
71 72 73 74 75 76 77 78 79

static gboolean
process (GeglOperation       *operation,
         GeglBuffer          *input,
         GeglBuffer          *aux,
         GeglBuffer          *output,
         const GeglRectangle *result,
         gint                 level)
{
80
  GeglProperties         *props        = GEGL_PROPERTIES (operation);
81 82 83 84 85
  gdouble             max_diff     = 0.0;
  gdouble             diffsum      = 0.0;
  gint                wrong_pixels = 0;
  const Babl         *cielab       = babl_format ("CIE Lab alpha float");
  const Babl         *srgb         = babl_format ("R'G'B' u8");
86 87 88
  const Babl         *yadbl        = babl_format ("YA double");
  GeglBuffer         *diff_buffer;
  GeglBufferIterator *iter;
89

90 91 92
  if (aux == NULL)
    return TRUE;

93
  diff_buffer = gegl_buffer_new (result, yadbl);
94

95
  iter = gegl_buffer_iterator_new (diff_buffer, result, 0, yadbl,
96
                                   GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE);
97

98
  gegl_buffer_iterator_add (iter, input, result, 0, cielab,
99
                            GEGL_ACCESS_READ, GEGL_ABYSS_NONE);
100

101
  gegl_buffer_iterator_add (iter, aux, result, 0, cielab,
102
                            GEGL_ACCESS_READ, GEGL_ABYSS_NONE);
103

104 105 106 107 108 109
  while (gegl_buffer_iterator_next (iter))
    {
      gint    i;
      gdouble *data_out = iter->data[0];
      gfloat  *data_in1 = iter->data[1];
      gfloat  *data_in2 = iter->data[2];
110

111
      for (i = 0; i < iter->length; i++)
112
        {
113 114 115 116
          gdouble diff = sqrt (SQR (data_in1[0] - data_in2[0]) +
                               SQR (data_in1[1] - data_in2[1]) +
                               SQR (data_in1[2] - data_in2[2]));

117
          gdouble alpha_diff = fabs (data_in1[3] - data_in2[3]) * 100.0;
118 119 120 121 122 123 124 125 126

          diff = MAX (diff, alpha_diff);

          if (diff >= ERROR_TOLERANCE)
            {
              wrong_pixels++;
              diffsum += diff;
              if (diff > max_diff)
                max_diff = diff;
127 128
              data_out[0] = diff;
              data_out[1] = data_in1[0];
129 130 131
            }
          else
            {
132 133
              data_out[0] = 0.0;
              data_out[1] = data_in1[0];
134 135
            }

136
          data_out += 2;
137 138
          data_in1 += 4;
          data_in2 += 4;
139 140 141
        }
    }

142
  iter  = gegl_buffer_iterator_new (output, result, 0, srgb,
143
                                    GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE);
144 145

  gegl_buffer_iterator_add (iter, diff_buffer, result, 0, yadbl,
146
                            GEGL_ACCESS_READ, GEGL_ABYSS_NONE);
147

148 149 150 151 152
  while (gegl_buffer_iterator_next (iter))
    {
      gint     i;
      guchar  *out  = iter->data[0];
      gdouble *data = iter->data[1];
153

154
      for (i = 0; i < iter->length; i++)
155
        {
156 157
          gdouble diff = data[0];
          gdouble a = data[1];
158

159
          if (diff >= 0.01)
160
            {
161 162 163
              out[0] = CLAMP ((100 - a) / 100.0 * 64 + 32, 0, 255);
              out[1] = CLAMP (diff / max_diff * 255, 0, 255);
              out[2] = 0;
164
            }
165 166 167 168 169 170 171 172 173
          else
            {
              out[0] = CLAMP (a / 100.0 * 255, 0, 255);
              out[1] = CLAMP (a / 100.0 * 255, 0, 255);
              out[2] = CLAMP (a / 100.0 * 255, 0, 255);
            }

          out  += 3;
          data += 2;
174 175 176 177 178
        }
    }

  g_object_unref (diff_buffer);

179 180 181
  props->wrong_pixels   = wrong_pixels;
  props->max_diff       = max_diff;
  props->avg_diff_wrong = diffsum / wrong_pixels;
182
  props->avg_diff_total = diffsum / (result->width * result->height);
183 184 185 186 187

  return TRUE;
}

static void
188
gegl_op_class_init (GeglOpClass *klass)
189 190 191 192 193 194 195 196 197
{
  GeglOperationClass         *operation_class;
  GeglOperationComposerClass *composer_class;

  operation_class = GEGL_OPERATION_CLASS (klass);
  composer_class  = GEGL_OPERATION_COMPOSER_CLASS (klass);

  operation_class->prepare                 = prepare;
  operation_class->get_required_for_output = get_required_for_output;
198
  operation_class->get_cached_region       = get_cached_region;
199
  composer_class->process                  = process;
200
  operation_class->threaded                = FALSE;
201 202

  gegl_operation_class_set_keys (operation_class,
203 204 205 206 207 208
    "name"       , "gegl:image-compare",
    "categories" , "programming",
    "description", _("Compares if input and aux buffers are "
                     "different. Results are saved in the "
                     "properties."),
    NULL);
209 210 211
}

#endif