display-filter-clip-warning.c 16.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24 25
 */

#include "config.h"

#include <string.h>

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

26
#include "libgimpbase/gimpbase.h"
27 28 29 30 31 32 33 34 35 36 37
#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})
38
#define DEFAULT_BOGUS_COLOR      (&(GimpRGB) {1.00, 1.00, 0.25, 1.00})
39

40

41 42 43 44 45 46 47 48 49 50 51
#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,
52
  WARNING_BOGUS     = 1 << 2
53 54 55 56 57 58 59 60 61 62 63 64 65 66
} 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;
67 68
  gboolean          show_bogus;
  GimpRGB           bogus_color;
69
  gboolean          include_alpha;
70
  gboolean          include_transparent;
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

  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,
88 89
  PROP_SHOW_BOGUS,
  PROP_BOGUS_COLOR,
90
  PROP_INCLUDE_ALPHA,
91
  PROP_INCLUDE_TRANSPARENT
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
};


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);

153 154
  object_class->get_property     = cdisplay_clip_warning_get_property;
  object_class->set_property     = cdisplay_clip_warning_set_property;
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

  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");

196 197 198 199
  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_SHOW_BOGUS,
                            "show-bogus",
                            _("Show bogus"),
                            _("Show warning for pixels with an infinite or NaN component"),
200 201 202
                            TRUE,
                            GIMP_PARAM_STATIC_STRINGS);

203 204 205 206
  GIMP_CONFIG_PROP_RGB (object_class, PROP_BOGUS_COLOR,
                        "bogus-color",
                        _("Bogus color"),
                        _("Bogus warning color"),
207
                        FALSE,
208
                        DEFAULT_BOGUS_COLOR,
209 210 211 212
                        GIMP_PARAM_STATIC_STRINGS |
                        GIMP_CONFIG_PARAM_DEFAULTS);

  gegl_param_spec_set_property_key (
213 214
    g_object_class_find_property (G_OBJECT_CLASS (klass), "bogus-color"),
    "sensitive", "show-bogus");
215 216 217

  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_INCLUDE_ALPHA,
                            "include-alpha",
218
                            _("Include alpha component"),
219
                            _("Include alpha component in the warning"),
220 221 222
                            TRUE,
                            GIMP_PARAM_STATIC_STRINGS);

223 224 225 226
  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_INCLUDE_TRANSPARENT,
                            "include-transparent",
                            _("Include transparent pixels"),
                            _("Include fully transparent pixels in the warning"),
227 228 229
                            TRUE,
                            GIMP_PARAM_STATIC_STRINGS);

230 231
  display_class->name            = _("Clip Warning");
  display_class->help_id         = "gimp-colordisplay-clip-warning";
232
  display_class->icon_name       = GIMP_ICON_DISPLAY_FILTER_CLIP_WARNING;
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
  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;

271 272
    case PROP_SHOW_BOGUS:
      g_value_set_boolean (value, clip_warning->show_bogus);
273
      break;
274 275
    case PROP_BOGUS_COLOR:
      g_value_set_boxed (value, &clip_warning->bogus_color);
276 277 278 279 280
      break;

    case PROP_INCLUDE_ALPHA:
      g_value_set_boolean (value, clip_warning->include_alpha);
      break;
281 282
    case PROP_INCLUDE_TRANSPARENT:
      g_value_set_boolean (value, clip_warning->include_transparent);
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
      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);

299
#define SET_MEMBER_PTR(member, value)                                          \
300 301 302 303 304
  cdisplay_clip_warning_set_member (clip_warning,                              \
                                    pspec->name,                               \
                                    &clip_warning->member,                     \
                                    (value),                                   \
                                    sizeof (clip_warning->member))
305 306
#define SET_MEMBER_VAL(member, type, value)                                    \
  SET_MEMBER_PTR (member, &(type) {value})
307 308 309 310

  switch (property_id)
    {
    case PROP_SHOW_SHADOWS:
311
      SET_MEMBER_VAL (show_shadows, gboolean, g_value_get_boolean (value));
312 313
      break;
    case PROP_SHADOWS_COLOR:
314
      SET_MEMBER_PTR (shadows_color, g_value_get_boxed (value));
315 316 317
      break;

    case PROP_SHOW_HIGHLIGHTS:
318
      SET_MEMBER_VAL (show_highlights, gboolean, g_value_get_boolean (value));
319 320
      break;
    case PROP_HIGHLIGHTS_COLOR:
321
      SET_MEMBER_PTR (highlights_color, g_value_get_boxed (value));
322 323
      break;

324 325
    case PROP_SHOW_BOGUS:
      SET_MEMBER_VAL (show_bogus, gboolean, g_value_get_boolean (value));
326
      break;
327 328
    case PROP_BOGUS_COLOR:
      SET_MEMBER_PTR (bogus_color, g_value_get_boxed (value));
329 330 331
      break;

    case PROP_INCLUDE_ALPHA:
332
      SET_MEMBER_VAL (include_alpha, gboolean, g_value_get_boolean (value));
333
      break;
334 335
    case PROP_INCLUDE_TRANSPARENT:
      SET_MEMBER_VAL (include_transparent, gboolean, g_value_get_boolean (value));
336 337 338 339 340 341 342
      break;

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

343 344
#undef SET_MEMBER_PTR
#undef SET_MEMBER_VAL
345 346 347 348 349 350 351 352 353 354 355 356
}

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"),
357
                                   GEGL_ACCESS_READWRITE, GEGL_ABYSS_NONE, 1);
358 359 360

  while (gegl_buffer_iterator_next (iter))
    {
361
      gfloat *data  = iter->items[0].data;
362
      gint    count = iter->length;
363 364
      gint    x     = iter->items[0].roi.x;
      gint    y     = iter->items[0].roi.y;
365 366 367

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

370 371
          if (clip_warning->include_transparent ||
              ! (data[3] <= 0.0f) /* include nan */)
372
            {
373 374 375
              if (clip_warning->show_bogus                                              &&
                  (! isfinite (data[0]) || ! isfinite (data[1]) || ! isfinite (data[2]) ||
                   (clip_warning->include_alpha && ! isfinite (data[3]))))
376
                {
377 378 379 380
                  /* don't combine warning color of pixels with a bogus
                   * component with other warnings
                   */
                  warning = WARNING_BOGUS;
381
                }
382
              else
383
                {
384 385 386 387 388 389 390 391 392 393 394 395 396
                  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;
                    }
397 398 399 400 401 402 403 404
                }
            }

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

              memcpy (data, clip_warning->colors[warning][alt],
405
                      4 * sizeof (gfloat));
406 407 408 409
            }

          data += 4;

410
          if (++x == iter->items[0].roi.x + iter->items[0].roi.width)
411
            {
412
              x = iter->items[0].roi.x;
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
              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++;
        }

470
      if (i & WARNING_BOGUS)
471
        {
472 473 474
          color[0] += clip_warning->bogus_color.r;
          color[1] += clip_warning->bogus_color.g;
          color[2] += clip_warning->bogus_color.b;
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495

          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;
    }
}