summaryrefslogtreecommitdiffstats
path: root/modules/display-filter-clip-warning.c
blob: 89d290dadb0c8dff5afb6be266b5aa248c4a8fb8 (plain)
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
29
30
31
32
33
34
35
36
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
89
90
91
92
93
94
95
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
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
189
190
191
192
193
194
195
196
197
198
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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
318
319
320
321
322
323
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
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
464
465
466
467
468
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
/* GIMP - The GNU Image Manipulation Program
 *
 * Copyright (C) 2017 Ell
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>

#include <gegl.h>
#include <gtk/gtk.h>

#include "libgimpcolor/gimpcolor.h"
#include "libgimpconfig/gimpconfig.h"
#include "libgimpmath/gimpmath.h"
#include "libgimpmodule/gimpmodule.h"
#include "libgimpwidgets/gimpwidgets.h"

#include "libgimp/libgimp-intl.h"


#define DEFAULT_SHADOWS_COLOR    (&(GimpRGB) {0.25, 0.25, 1.00, 1.00})
#define DEFAULT_HIGHLIGHTS_COLOR (&(GimpRGB) {1.00, 0.25, 0.25, 1.00})
#define DEFAULT_BOGUS_COLOR      (&(GimpRGB) {1.00, 1.00, 0.25, 1.00})


#define CDISPLAY_TYPE_CLIP_WARNING            (cdisplay_clip_warning_get_type ())
#define CDISPLAY_CLIP_WARNING(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), CDISPLAY_TYPE_CLIP_WARNING, CdisplayClipWarning))
#define CDISPLAY_CLIP_WARNING_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), CDISPLAY_TYPE_CLIP_WARNING, CdisplayClipWarningClass))
#define CDISPLAY_IS_CLIP_WARNING(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CDISPLAY_TYPE_CLIP_WARNING))
#define CDISPLAY_IS_CLIP_WARNING_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CDISPLAY_TYPE_CLIP_WARNING))


typedef enum
{
  WARNING_SHADOW    = 1 << 0,
  WARNING_HIGHLIGHT = 1 << 1,
  WARNING_BOGUS     = 1 << 2
} Warning;


typedef struct _CdisplayClipWarning      CdisplayClipWarning;
typedef struct _CdisplayClipWarningClass CdisplayClipWarningClass;

struct _CdisplayClipWarning
{
  GimpColorDisplay  parent_instance;

  gboolean          show_shadows;
  GimpRGB           shadows_color;
  gboolean          show_highlights;
  GimpRGB           highlights_color;
  gboolean          show_bogus;
  GimpRGB           bogus_color;
  gboolean          include_alpha;
  gboolean          include_transparent;

  gfloat            colors[8][2][4];
};

struct _CdisplayClipWarningClass
{
  GimpColorDisplayClass  parent_instance;
};


enum
{
  PROP_0,
  PROP_SHOW_SHADOWS,
  PROP_SHADOWS_COLOR,
  PROP_SHOW_HIGHLIGHTS,
  PROP_HIGHLIGHTS_COLOR,
  PROP_SHOW_BOGUS,
  PROP_BOGUS_COLOR,
  PROP_INCLUDE_ALPHA,
  PROP_INCLUDE_TRANSPARENT
};


static GType   cdisplay_clip_warning_get_type       (void);

static void    cdisplay_clip_warning_set_property   (GObject             *object,
                                                     guint                property_id,
                                                     const GValue        *value,
                                                     GParamSpec          *pspec);
static void    cdisplay_clip_warning_get_property   (GObject             *object,
                                                     guint                property_id,
                                                     GValue              *value,
                                                     GParamSpec          *pspec);

static void    cdisplay_clip_warning_convert_buffer (GimpColorDisplay    *display,
                                                     GeglBuffer          *buffer,
                                                     GeglRectangle       *area);

static void    cdisplay_clip_warning_set_member     (CdisplayClipWarning *clip_warning,
                                                     const gchar         *property_name,
                                                     gpointer             member,
                                                     gconstpointer        value,
                                                     gsize                size);
static void    cdisplay_clip_warning_update_colors  (CdisplayClipWarning *clip_warning);


static const GimpModuleInfo cdisplay_clip_warning_info =
{
  GIMP_MODULE_ABI_VERSION,
  N_("Clip warning color display filter"),
  "Ell",
  "v1.0",
  "(c) 2017, released under the GPL",
  "2017"
};


G_DEFINE_DYNAMIC_TYPE (CdisplayClipWarning, cdisplay_clip_warning,
                       GIMP_TYPE_COLOR_DISPLAY)


G_MODULE_EXPORT const GimpModuleInfo *
gimp_module_query (GTypeModule *module)
{
  return &cdisplay_clip_warning_info;
}

G_MODULE_EXPORT gboolean
gimp_module_register (GTypeModule *module)
{
  cdisplay_clip_warning_register_type (module);

  return TRUE;
}

static void
cdisplay_clip_warning_class_init (CdisplayClipWarningClass *klass)
{
  GObjectClass          *object_class  = G_OBJECT_CLASS (klass);
  GimpColorDisplayClass *display_class = GIMP_COLOR_DISPLAY_CLASS (klass);

  object_class->get_property     = cdisplay_clip_warning_get_property;
  object_class->set_property     = cdisplay_clip_warning_set_property;

  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_SHOW_SHADOWS,
                            "show-shadows",
                            _("Show shadows"),
                            _("Show warning for pixels with a negative component"),
                            TRUE,
                            GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_RGB (object_class, PROP_SHADOWS_COLOR,
                        "shadows-color",
                        _("Shadows color"),
                        _("Shadows warning color"),
                        FALSE,
                        DEFAULT_SHADOWS_COLOR,
                        GIMP_PARAM_STATIC_STRINGS |
                        GIMP_CONFIG_PARAM_DEFAULTS);

  gegl_param_spec_set_property_key (
    g_object_class_find_property (G_OBJECT_CLASS (klass), "shadows-color"),
    "sensitive", "show-shadows");

  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_SHOW_HIGHLIGHTS,
                            "show-highlights",
                            _("Show highlights"),
                            _("Show warning for pixels with a component greater than one"),
                            TRUE,
                            GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_RGB (object_class, PROP_HIGHLIGHTS_COLOR,
                        "highlights-color",
                        _("Highlights color"),
                        _("Highlights warning color"),
                        FALSE,
                        DEFAULT_HIGHLIGHTS_COLOR,
                        GIMP_PARAM_STATIC_STRINGS |
                        GIMP_CONFIG_PARAM_DEFAULTS);

  gegl_param_spec_set_property_key (
    g_object_class_find_property (G_OBJECT_CLASS (klass), "highlights-color"),
    "sensitive", "show-highlights");

  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_SHOW_BOGUS,
                            "show-bogus",
                            _("Show bogus"),
                            _("Show warning for pixels with an infinite or NaN component"),
                            TRUE,
                            GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_RGB (object_class, PROP_BOGUS_COLOR,
                        "bogus-color",
                        _("Bogus color"),
                        _("Bogus warning color"),
                        FALSE,
                        DEFAULT_BOGUS_COLOR,
                        GIMP_PARAM_STATIC_STRINGS |
                        GIMP_CONFIG_PARAM_DEFAULTS);

  gegl_param_spec_set_property_key (
    g_object_class_find_property (G_OBJECT_CLASS (klass), "bogus-color"),
    "sensitive", "show-bogus");

  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_INCLUDE_ALPHA,
                            "include-alpha",
                            _("Include alpha component"),
                            _("Include alpha component in the warning"),
                            TRUE,
                            GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_INCLUDE_TRANSPARENT,
                            "include-transparent",
                            _("Include transparent pixels"),
                            _("Include fully transparent pixels in the warning"),
                            TRUE,
                            GIMP_PARAM_STATIC_STRINGS);

  display_class->name            = _("Clip Warning");
  display_class->help_id         = "gimp-colordisplay-clip-warning";
  display_class->icon_name       = GIMP_ICON_DISPLAY_FILTER_CLIP_WARNING;

  display_class->convert_buffer  = cdisplay_clip_warning_convert_buffer;
}

static void
cdisplay_clip_warning_class_finalize (CdisplayClipWarningClass *klass)
{
}

static void
cdisplay_clip_warning_init (CdisplayClipWarning *clip_warning)
{
}

static void
cdisplay_clip_warning_get_property (GObject    *object,
                                    guint       property_id,
                                    GValue     *value,
                                    GParamSpec *pspec)
{
  CdisplayClipWarning *clip_warning = CDISPLAY_CLIP_WARNING (object);

  switch (property_id)
    {
    case PROP_SHOW_SHADOWS:
      g_value_set_boolean (value, clip_warning->show_shadows);
      break;
    case PROP_SHADOWS_COLOR:
      g_value_set_boxed (value, &clip_warning->shadows_color);
      break;

    case PROP_SHOW_HIGHLIGHTS:
      g_value_set_boolean (value, clip_warning->show_highlights);
      break;
    case PROP_HIGHLIGHTS_COLOR:
      g_value_set_boxed (value, &clip_warning->highlights_color);
      break;

    case PROP_SHOW_BOGUS:
      g_value_set_boolean (value, clip_warning->show_bogus);
      break;
    case PROP_BOGUS_COLOR:
      g_value_set_boxed (value, &clip_warning->bogus_color);
      break;

    case PROP_INCLUDE_ALPHA:
      g_value_set_boolean (value, clip_warning->include_alpha);
      break;
    case PROP_INCLUDE_TRANSPARENT:
      g_value_set_boolean (value, clip_warning->include_transparent);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
cdisplay_clip_warning_set_property (GObject      *object,
                                    guint         property_id,
                                    const GValue *value,
                                    GParamSpec   *pspec)
{
  CdisplayClipWarning *clip_warning = CDISPLAY_CLIP_WARNING (object);

#define SET_MEMBER_PTR(member, value)                                          \
  cdisplay_clip_warning_set_member (clip_warning,                              \
                                    pspec->name,                               \
                                    &clip_warning->member,                     \
                                    (value),                                   \
                                    sizeof (clip_warning->member))
#define SET_MEMBER_VAL(member, type, value)                                    \
  SET_MEMBER_PTR (member, &(type) {value})

  switch (property_id)
    {
    case PROP_SHOW_SHADOWS:
      SET_MEMBER_VAL (show_shadows, gboolean, g_value_get_boolean (value));
      break;
    case PROP_SHADOWS_COLOR:
      SET_MEMBER_PTR (shadows_color, g_value_get_boxed (value));
      break;

    case PROP_SHOW_HIGHLIGHTS:
      SET_MEMBER_VAL (show_highlights, gboolean, g_value_get_boolean (value));
      break;
    case PROP_HIGHLIGHTS_COLOR:
      SET_MEMBER_PTR (highlights_color, g_value_get_boxed (value));
      break;

    case PROP_SHOW_BOGUS:
      SET_MEMBER_VAL (show_bogus, gboolean, g_value_get_boolean (value));
      break;
    case PROP_BOGUS_COLOR:
      SET_MEMBER_PTR (bogus_color, g_value_get_boxed (value));
      break;

    case PROP_INCLUDE_ALPHA:
      SET_MEMBER_VAL (include_alpha, gboolean, g_value_get_boolean (value));
      break;
    case PROP_INCLUDE_TRANSPARENT:
      SET_MEMBER_VAL (include_transparent, gboolean, g_value_get_boolean (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }

#undef SET_MEMBER_PTR
#undef SET_MEMBER_VAL
}

static void
cdisplay_clip_warning_convert_buffer (GimpColorDisplay *display,
                                      GeglBuffer       *buffer,
                                      GeglRectangle    *area)
{
  CdisplayClipWarning *clip_warning = CDISPLAY_CLIP_WARNING (display);
  GeglBufferIterator  *iter;

  iter = gegl_buffer_iterator_new (buffer, area, 0,
                                   babl_format ("R'G'B'A float"),
                                   GEGL_ACCESS_READWRITE, GEGL_ABYSS_NONE, 1);

  while (gegl_buffer_iterator_next (iter))
    {
      gfloat *data  = iter->items[0].data;
      gint    count = iter->length;
      gint    x     = iter->items[0].roi.x;
      gint    y     = iter->items[0].roi.y;

      while (count--)
        {
          gint warning = 0;

          if (clip_warning->include_transparent ||
              ! (data[3] <= 0.0f) /* include nan */)
            {
              if (clip_warning->show_bogus                                              &&
                  (! isfinite (data[0]) || ! isfinite (data[1]) || ! isfinite (data[2]) ||
                   (clip_warning->include_alpha && ! isfinite (data[3]))))
                {
                  /* don't combine warning color of pixels with a bogus
                   * component with other warnings
                   */
                  warning = WARNING_BOGUS;
                }
              else
                {
                  if (clip_warning->show_shadows                          &&
                      (data[0] < 0.0f || data[1] < 0.0f || data[2] < 0.0f ||
                       (clip_warning->include_alpha && data[3] < 0.0f)))
                    {
                      warning |= WARNING_SHADOW;
                    }

                  if (clip_warning->show_highlights                       &&
                      (data[0] > 1.0f || data[1] > 1.0f || data[2] > 1.0f ||
                       (clip_warning->include_alpha && data[3] > 1.0f)))
                    {
                      warning |= WARNING_HIGHLIGHT;
                    }
                }
            }

          if (warning)
            {
              gboolean alt = ((x + y) >> 3) & 1;

              memcpy (data, clip_warning->colors[warning][alt],
                      4 * sizeof (gfloat));
            }

          data += 4;

          if (++x == iter->items[0].roi.x + iter->items[0].roi.width)
            {
              x = iter->items[0].roi.x;
              y++;
            }
        }
    }
}

static void
cdisplay_clip_warning_set_member (CdisplayClipWarning *clip_warning,
                                  const gchar         *property_name,
                                  gpointer             member,
                                  gconstpointer        value,
                                  gsize                size)
{
  if (memcmp (member, value, size))
    {
      memcpy (member, value, size);

      cdisplay_clip_warning_update_colors (clip_warning);

      g_object_notify (G_OBJECT (clip_warning), property_name);
      gimp_color_display_changed (GIMP_COLOR_DISPLAY (clip_warning));
    }
}

static void
cdisplay_clip_warning_update_colors (CdisplayClipWarning *clip_warning)
{
  gint i;
  gint j;

  for (i = 0; i < 8; i++)
    {
      gfloat *color     = clip_warning->colors[i][0];
      gfloat *alt_color = clip_warning->colors[i][1];
      gfloat  alt_value;
      gint    n         = 0;

      memset (color, 0, 3 * sizeof (gfloat));

      if (i & WARNING_SHADOW)
        {
          color[0] += clip_warning->shadows_color.r;
          color[1] += clip_warning->shadows_color.g;
          color[2] += clip_warning->shadows_color.b;

          n++;
        }

      if (i & WARNING_HIGHLIGHT)
        {
          color[0] += clip_warning->highlights_color.r;
          color[1] += clip_warning->highlights_color.g;
          color[2] += clip_warning->highlights_color.b;

          n++;
        }

      if (i & WARNING_BOGUS)
        {
          color[0] += clip_warning->bogus_color.r;
          color[1] += clip_warning->bogus_color.g;
          color[2] += clip_warning->bogus_color.b;

          n++;
        }

      if (n)
        {
          for (j = 0; j < 3; j++)
            color[j] /= n;
        }
      color[3] = 1.0;

      if (MAX (color[0], MAX (color[1], color[2])) <= 0.5)
        alt_value = 1.0;
      else
        alt_value = 0.0;

      for (j = 0; j < 3; j++)
        alt_color[j] = 0.75 * color[j] + 0.25 * alt_value;
      alt_color[3] = 1.0;
    }
}