summaryrefslogtreecommitdiffstats
path: root/app/operations/gimpoperationgradient.c
blob: 2dbb74666da7dd1d69e3c2d71de563fb41142f81 (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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * Largely based on gimpdrawable-gradient.c
 *
 * gimpoperationgradient.c
 * Copyright (C) 2014 Michael Henning <drawoc@darkrefraction.com>
 *
 * 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 <cairo.h>
#include <gegl.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#include "libgimpcolor/gimpcolor.h"
#include "libgimpmath/gimpmath.h"

#include "operations-types.h"

#include "core/gimpgradient.h"

#include "gimpoperationgradient.h"


#define GRADIENT_CACHE_N_SUPERSAMPLES 4
#define GRADIENT_CACHE_MAX_SIZE       ((1 << 20) / sizeof (GimpRGB))


enum
{
  PROP_0,
  PROP_CONTEXT,
  PROP_GRADIENT,
  PROP_START_X,
  PROP_START_Y,
  PROP_END_X,
  PROP_END_Y,
  PROP_GRADIENT_TYPE,
  PROP_GRADIENT_REPEAT,
  PROP_OFFSET,
  PROP_GRADIENT_REVERSE,
  PROP_GRADIENT_BLEND_COLOR_SPACE,
  PROP_SUPERSAMPLE,
  PROP_SUPERSAMPLE_DEPTH,
  PROP_SUPERSAMPLE_THRESHOLD,
  PROP_DITHER
};

typedef struct
{
  GimpGradient                *gradient;
  gboolean                     reverse;
  GimpGradientBlendColorSpace  blend_color_space;
  GimpRGB                     *gradient_cache;
  gint                         gradient_cache_size;
  GimpGradientSegment         *last_seg;
  gdouble                      offset;
  gdouble                      sx, sy;
  GimpGradientType             gradient_type;
  gdouble                      dist;
  gdouble                      vec[2];
  GimpRepeatMode               repeat;
  GeglSampler                 *dist_sampler;
} RenderBlendData;


typedef struct
{
  gfloat        *data;
  GeglRectangle  roi;
  GRand         *dither_rand;
} PutPixelData;


/*  local function prototypes  */

static void            gimp_operation_gradient_dispose           (GObject               *gobject);
static void            gimp_operation_gradient_finalize          (GObject               *gobject);
static void            gimp_operation_gradient_get_property      (GObject               *object,
                                                                  guint                  property_id,
                                                                  GValue                *value,
                                                                  GParamSpec            *pspec);
static void            gimp_operation_gradient_set_property      (GObject               *object,
                                                                  guint                  property_id,
                                                                  const GValue          *value,
                                                                  GParamSpec            *pspec);

static void            gimp_operation_gradient_prepare           (GeglOperation         *operation);

static GeglRectangle   gimp_operation_gradient_get_bounding_box  (GeglOperation         *operation);

static gdouble         gradient_calc_conical_sym_factor          (gdouble                dist,
                                                                  gdouble               *axis,
                                                                  gdouble                offset,
                                                                  gdouble                x,
                                                                  gdouble                y);
static gdouble         gradient_calc_conical_asym_factor         (gdouble                dist,
                                                                  gdouble               *axis,
                                                                  gdouble                offset,
                                                                  gdouble                x,
                                                                  gdouble                y);
static gdouble         gradient_calc_square_factor               (gdouble                dist,
                                                                  gdouble                offset,
                                                                  gdouble                x,
                                                                  gdouble                y);
static gdouble         gradient_calc_radial_factor               (gdouble                dist,
                                                                  gdouble                offset,
                                                                  gdouble                x,
                                                                  gdouble                y);
static gdouble         gradient_calc_linear_factor               (gdouble                dist,
                                                                  gdouble               *vec,
                                                                  gdouble                offset,
                                                                  gdouble                x,
                                                                  gdouble                y);
static gdouble         gradient_calc_bilinear_factor             (gdouble                dist,
                                                                  gdouble               *vec,
                                                                  gdouble                offset,
                                                                  gdouble                x,
                                                                  gdouble                y);
static gdouble         gradient_calc_spiral_factor               (gdouble                dist,
                                                                  gdouble               *axis,
                                                                  gdouble                offset,
                                                                  gdouble                x,
                                                                  gdouble                y,
                                                                  gboolean               clockwise);

static gdouble         gradient_calc_shapeburst_angular_factor   (GeglSampler           *dist_sampler,
                                                                  gdouble                offset,
                                                                  gdouble                x,
                                                                  gdouble                y);
static gdouble         gradient_calc_shapeburst_spherical_factor (GeglSampler           *dist_sampler,
                                                                  gdouble                offset,
                                                                  gdouble                x,
                                                                  gdouble                y);
static gdouble         gradient_calc_shapeburst_dimpled_factor   (GeglSampler           *dist_sampler,
                                                                  gdouble                offset,
                                                                  gdouble                x,
                                                                  gdouble                y);

static void            gradient_render_pixel                     (gdouble                x,
                                                                  gdouble                y,
                                                                  GimpRGB               *color,
                                                                  gpointer               render_data);

static void            gradient_put_pixel                        (gint                   x,
                                                                  gint                   y,
                                                                  GimpRGB               *color,
                                                                  gpointer               put_pixel_data);

static void            gradient_dither_pixel                     (GimpRGB               *color,
                                                                  GRand                 *dither_rand,
                                                                  gfloat                *dest);

static gboolean        gimp_operation_gradient_process           (GeglOperation         *operation,
                                                                  GeglBuffer            *input,
                                                                  GeglBuffer            *output,
                                                                  const GeglRectangle   *result,
                                                                  gint                   level);

static void            gimp_operation_gradient_invalidate_cache  (GimpOperationGradient *self);
static void            gimp_operation_gradient_validate_cache    (GimpOperationGradient *self);


G_DEFINE_TYPE (GimpOperationGradient, gimp_operation_gradient,
               GEGL_TYPE_OPERATION_FILTER)

#define parent_class gimp_operation_gradient_parent_class


static void
gimp_operation_gradient_class_init (GimpOperationGradientClass *klass)
{
  GObjectClass             *object_class    = G_OBJECT_CLASS (klass);
  GeglOperationClass       *operation_class = GEGL_OPERATION_CLASS (klass);
  GeglOperationFilterClass *filter_class    = GEGL_OPERATION_FILTER_CLASS (klass);

  object_class->dispose             = gimp_operation_gradient_dispose;
  object_class->finalize            = gimp_operation_gradient_finalize;
  object_class->set_property        = gimp_operation_gradient_set_property;
  object_class->get_property        = gimp_operation_gradient_get_property;

  operation_class->prepare          = gimp_operation_gradient_prepare;
  operation_class->get_bounding_box = gimp_operation_gradient_get_bounding_box;

  filter_class->process             = gimp_operation_gradient_process;

  gegl_operation_class_set_keys (operation_class,
                                 "name",        "gimp:gradient",
                                 "categories",  "gimp",
                                 "description", "GIMP Gradient operation",
                                 NULL);

  g_object_class_install_property (object_class, PROP_CONTEXT,
                                   g_param_spec_object ("context",
                                                        "Context",
                                                        "A GimpContext",
                                                        GIMP_TYPE_OBJECT,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_GRADIENT,
                                   g_param_spec_object ("gradient",
                                                        "Gradient",
                                                        "A GimpGradient to render",
                                                        GIMP_TYPE_OBJECT,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_START_X,
                                   g_param_spec_double ("start-x",
                                                        "Start X",
                                                        "X coordinate of the first point",
                                                        -G_MAXDOUBLE, G_MAXDOUBLE, 0,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_START_Y,
                                   g_param_spec_double ("start-y",
                                                        "Start Y",
                                                        "Y coordinate of the first point",
                                                        -G_MAXDOUBLE, G_MAXDOUBLE, 0,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_END_X,
                                   g_param_spec_double ("end-x",
                                                        "End X",
                                                        "X coordinate of the second point",
                                                        -G_MAXDOUBLE, G_MAXDOUBLE, 200,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_END_Y,
                                   g_param_spec_double ("end-y",
                                                        "End Y",
                                                        "Y coordinate of the second point",
                                                        -G_MAXDOUBLE, G_MAXDOUBLE, 200,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_GRADIENT_TYPE,
                                   g_param_spec_enum ("gradient-type",
                                                      "Gradient Type",
                                                      "The type of gradient to render",
                                                      GIMP_TYPE_GRADIENT_TYPE,
                                                      GIMP_GRADIENT_LINEAR,
                                                      G_PARAM_READWRITE |
                                                      G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_GRADIENT_REPEAT,
                                   g_param_spec_enum ("gradient-repeat",
                                                      "Repeat mode",
                                                      "Repeat mode",
                                                      GIMP_TYPE_REPEAT_MODE,
                                                      GIMP_REPEAT_NONE,
                                                      G_PARAM_READWRITE |
                                                      G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_OFFSET,
                                   g_param_spec_double ("offset",
                                                        "Offset",
                                                        "Offset relates to the starting and ending coordinates "
                                                        "specified for the blend. This parameter is mode dependent.",
                                                        0, G_MAXDOUBLE, 0,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_GRADIENT_REVERSE,
                                   g_param_spec_boolean ("gradient-reverse",
                                                         "Reverse",
                                                         "Reverse the gradient",
                                                         FALSE,
                                                         G_PARAM_READWRITE |
                                                         G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_GRADIENT_BLEND_COLOR_SPACE,
                                   g_param_spec_enum ("gradient-blend-color-space",
                                                      "Blend Color Space",
                                                      "Which color space to use when blending RGB gradient segments",
                                                      GIMP_TYPE_GRADIENT_BLEND_COLOR_SPACE,
                                                      GIMP_GRADIENT_BLEND_RGB_PERCEPTUAL,
                                                      G_PARAM_READWRITE |
                                                      G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_SUPERSAMPLE,
                                   g_param_spec_boolean ("supersample",
                                                         "Supersample",
                                                         "Do adaptive supersampling",
                                                         FALSE,
                                                         G_PARAM_READWRITE |
                                                         G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_SUPERSAMPLE_DEPTH,
                                   g_param_spec_int ("supersample-depth",
                                                     "Max depth",
                                                     "Maximum recursion levels for supersampling",
                                                     1, 9, 3,
                                                     G_PARAM_READWRITE |
                                                     G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_SUPERSAMPLE_THRESHOLD,
                                   g_param_spec_double ("supersample-threshold",
                                                        "Threshold",
                                                        "Supersampling threshold",
                                                        0, 4, 0.20,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_DITHER,
                                   g_param_spec_boolean ("dither",
                                                         "Dither",
                                                         "Use dithering to reduce banding",
                                                         FALSE,
                                                         G_PARAM_READWRITE |
                                                         G_PARAM_CONSTRUCT));
}

static void
gimp_operation_gradient_init (GimpOperationGradient *self)
{
  g_mutex_init (&self->gradient_cache_mutex);
}

static void
gimp_operation_gradient_dispose (GObject *object)
{
  GimpOperationGradient *self = GIMP_OPERATION_GRADIENT (object);

  gimp_operation_gradient_invalidate_cache (self);

  g_clear_object (&self->gradient);
  g_clear_object (&self->context);

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
gimp_operation_gradient_finalize (GObject *object)
{
  GimpOperationGradient *self = GIMP_OPERATION_GRADIENT (object);

  g_mutex_clear (&self->gradient_cache_mutex);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gimp_operation_gradient_get_property (GObject    *object,
                                      guint       property_id,
                                      GValue     *value,
                                      GParamSpec *pspec)
{
 GimpOperationGradient *self = GIMP_OPERATION_GRADIENT (object);

  switch (property_id)
    {
    case PROP_CONTEXT:
      g_value_set_object (value, self->context);
      break;

    case PROP_GRADIENT:
      g_value_set_object (value, self->gradient);
      break;

    case PROP_START_X:
      g_value_set_double (value, self->start_x);
      break;

    case PROP_START_Y:
      g_value_set_double (value, self->start_y);
      break;

    case PROP_END_X:
      g_value_set_double (value, self->end_x);
      break;

    case PROP_END_Y:
      g_value_set_double (value, self->end_y);
      break;

    case PROP_GRADIENT_TYPE:
      g_value_set_enum (value, self->gradient_type);
      break;

    case PROP_GRADIENT_REPEAT:
      g_value_set_enum (value, self->gradient_repeat);
      break;

    case PROP_OFFSET:
      g_value_set_double (value, self->offset);
      break;

    case PROP_GRADIENT_REVERSE:
      g_value_set_boolean (value, self->gradient_reverse);
      break;

    case PROP_GRADIENT_BLEND_COLOR_SPACE:
      g_value_set_enum (value, self->gradient_blend_color_space);
      break;

    case PROP_SUPERSAMPLE:
      g_value_set_boolean (value, self->supersample);
      break;

    case PROP_SUPERSAMPLE_DEPTH:
      g_value_set_int (value, self->supersample_depth);
      break;

    case PROP_SUPERSAMPLE_THRESHOLD:
      g_value_set_double (value, self->supersample_threshold);
      break;

    case PROP_DITHER:
      g_value_set_boolean (value, self->dither);
      break;

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

static void
gimp_operation_gradient_set_property (GObject      *object,
                                      guint         property_id,
                                      const GValue *value,
                                      GParamSpec   *pspec)
{
  GimpOperationGradient *self = GIMP_OPERATION_GRADIENT (object);

  switch (property_id)
    {
    case PROP_CONTEXT:
      if (self->context)
        g_object_unref (self->context);

      self->context = g_value_dup_object (value);
      break;

    case PROP_GRADIENT:
      {
        GimpGradient *gradient = g_value_get_object (value);

        g_clear_object (&self->gradient);

        if (gradient)
          {
            if (gimp_gradient_has_fg_bg_segments (gradient))
              self->gradient = gimp_gradient_flatten (gradient, self->context);
            else
              self->gradient = g_object_ref (gradient);
          }

        gimp_operation_gradient_invalidate_cache (self);
      }
      break;

    case PROP_START_X:
      self->start_x = g_value_get_double (value);

      gimp_operation_gradient_invalidate_cache (self);
      break;

    case PROP_START_Y:
      self->start_y = g_value_get_double (value);

      gimp_operation_gradient_invalidate_cache (self);
      break;

    case PROP_END_X:
      self->end_x = g_value_get_double (value);

      gimp_operation_gradient_invalidate_cache (self);
      break;

    case PROP_END_Y:
      self->end_y = g_value_get_double (value);

      gimp_operation_gradient_invalidate_cache (self);
      break;

    case PROP_GRADIENT_TYPE:
      self->gradient_type = g_value_get_enum (value);
      break;

    case PROP_GRADIENT_REPEAT:
      self->gradient_repeat = g_value_get_enum (value);
      break;

    case PROP_OFFSET:
      self->offset = g_value_get_double (value);
      break;

    case PROP_GRADIENT_REVERSE:
      self->gradient_reverse = g_value_get_boolean (value);

      gimp_operation_gradient_invalidate_cache (self);
      break;

    case PROP_GRADIENT_BLEND_COLOR_SPACE:
      self->gradient_blend_color_space = g_value_get_enum (value);

      gimp_operation_gradient_invalidate_cache (self);
      break;

    case PROP_SUPERSAMPLE:
      self->supersample = g_value_get_boolean (value);
      break;

    case PROP_SUPERSAMPLE_DEPTH:
      self->supersample_depth = g_value_get_int (value);
      break;

    case PROP_SUPERSAMPLE_THRESHOLD:
      self->supersample_threshold = g_value_get_double (value);
      break;

    case PROP_DITHER:
      self->dither = g_value_get_boolean (value);
      break;

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

static void
gimp_operation_gradient_prepare (GeglOperation *operation)
{
  gegl_operation_set_format (operation, "output", babl_format ("R'G'B'A float"));
}

static GeglRectangle
gimp_operation_gradient_get_bounding_box (GeglOperation *operation)
{
  return gegl_rectangle_infinite_plane ();
}

static gdouble
gradient_calc_conical_sym_factor (gdouble  dist,
                                  gdouble *axis,
                                  gdouble  offset,
                                  gdouble  x,
                                  gdouble  y)
{
  if (dist == 0.0)
    {
      return 0.0;
    }
  else if ((x != 0) || (y != 0))
    {
      gdouble vec[2];
      gdouble r;
      gdouble rat;

      /* Calculate offset from the start in pixels */

      r = sqrt (SQR (x) + SQR (y));

      vec[0] = x / r;
      vec[1] = y / r;

      rat = axis[0] * vec[0] + axis[1] * vec[1]; /* Dot product */

      if (rat > 1.0)
        rat = 1.0;
      else if (rat < -1.0)
        rat = -1.0;

      /* This cool idea is courtesy Josh MacDonald,
       * Ali Rahimi --- two more XCF losers.  */

      rat = acos (rat) / G_PI;
      rat = pow (rat, (offset / 10.0) + 1.0);

      return CLAMP (rat, 0.0, 1.0);
    }
  else
    {
      return 0.5;
    }
}

static gdouble
gradient_calc_conical_asym_factor (gdouble  dist,
                                   gdouble *axis,
                                   gdouble  offset,
                                   gdouble  x,
                                   gdouble  y)
{
  if (dist == 0.0)
    {
      return 0.0;
    }
  else if (x != 0 || y != 0)
    {
      gdouble ang0, ang1;
      gdouble ang;
      gdouble rat;

      ang0 = atan2 (axis[0], axis[1]) + G_PI;

      ang1 = atan2 (x, y) + G_PI;

      ang = ang1 - ang0;

      if (ang < 0.0)
        ang += (2.0 * G_PI);

      rat = ang / (2.0 * G_PI);
      rat = pow (rat, (offset / 10.0) + 1.0);

      return CLAMP (rat, 0.0, 1.0);
    }
  else
    {
      return 0.5; /* We are on middle point */
    }
}

static gdouble
gradient_calc_square_factor (gdouble dist,
                             gdouble offset,
                             gdouble x,
                             gdouble y)
{
  if (dist == 0.0)
    {
      return 0.0;
    }
  else
    {
      gdouble r;
      gdouble rat;

      /* Calculate offset from start as a value in [0, 1] */

      offset = offset / 100.0;

      r   = MAX (fabs (x), fabs (y));
      rat = r / dist;

      if (rat < offset)
        return 0.0;
      else if (offset == 1.0)
        return (rat >= 1.0) ? 1.0 : 0.0;
      else
        return (rat - offset) / (1.0 - offset);
    }
}

static gdouble
gradient_calc_radial_factor (gdouble dist,
                             gdouble offset,
                             gdouble x,
                             gdouble y)
{
  if (dist == 0.0)
    {
      return 0.0;
    }
  else
    {
      gdouble r;
      gdouble rat;

      /* Calculate radial offset from start as a value in [0, 1] */

      offset = offset / 100.0;

      r   = sqrt (SQR (x) + SQR (y));
      rat = r / dist;

      if (rat < offset)
        return 0.0;
      else if (offset == 1.0)
        return (rat >= 1.0) ? 1.0 : 0.0;
      else
        return (rat - offset) / (1.0 - offset);
    }
}

static gdouble
gradient_calc_linear_factor (gdouble  dist,
                             gdouble *vec,
                             gdouble  offset,
                             gdouble  x,
                             gdouble  y)
{
  if (dist == 0.0)
    {
      return 0.0;
    }
  else
    {
      gdouble r;
      gdouble rat;

      offset = offset / 100.0;

      r   = vec[0] * x + vec[1] * y;
      rat = r / dist;

      if (rat >= 0.0 && rat < offset)
        return 0.0;
      else if (offset == 1.0)
        return (rat >= 1.0) ? 1.0 : 0.0;
      else if (rat < 0.0)
        return rat / (1.0 - offset);
      else
        return (rat - offset) / (1.0 - offset);
    }
}

static gdouble
gradient_calc_bilinear_factor (gdouble  dist,
                               gdouble *vec,
                               gdouble  offset,
                               gdouble  x,
                               gdouble  y)
{
  if (dist == 0.0)
    {
      return 0.0;
    }
  else
    {
      gdouble r;
      gdouble rat;

      /* Calculate linear offset from the start line outward */

      offset = offset / 100.0;

      r   = vec[0] * x + vec[1] * y;
      rat = r / dist;

      if (fabs (rat) < offset)
        return 0.0;
      else if (offset == 1.0)
        return (rat == 1.0) ? 1.0 : 0.0;
      else
        return (fabs (rat) - offset) / (1.0 - offset);
    }
}

static gdouble
gradient_calc_spiral_factor (gdouble   dist,
                             gdouble  *axis,
                             gdouble   offset,
                             gdouble   x,
                             gdouble   y,
                             gboolean  clockwise)
{
  if (dist == 0.0)
    {
      return 0.0;
    }
  else if (x != 0.0 || y != 0.0)
    {
      gdouble ang0, ang1;
      gdouble ang;
      double  r;

      offset = offset / 100.0;

      ang0 = atan2 (axis[0], axis[1]) + G_PI;
      ang1 = atan2 (x, y) + G_PI;

      if (clockwise)
        ang = ang1 - ang0;
      else
        ang = ang0 - ang1;

      if (ang < 0.0)
        ang += (2.0 * G_PI);

      r = sqrt (SQR (x) + SQR (y)) / dist;

      return fmod (ang / (2.0 * G_PI) + r + offset, 1.0);
    }
  else
    {
      return 0.5 ; /* We are on the middle point */
    }
}

static gdouble
gradient_calc_shapeburst_angular_factor (GeglSampler *dist_sampler,
                                         gdouble      offset,
                                         gdouble      x,
                                         gdouble      y)
{
  gfloat value;

  offset = offset / 100.0;

  gegl_sampler_get (dist_sampler, x, y, NULL, &value, GEGL_ABYSS_NONE);

  value = 1.0 - value;

  if (value < offset)
    value = 0.0;
  else if (offset == 1.0)
    value = (value >= 1.0) ? 1.0 : 0.0;
  else
    value = (value - offset) / (1.0 - offset);

  return value;
}


static gdouble
gradient_calc_shapeburst_spherical_factor (GeglSampler *dist_sampler,
                                           gdouble      offset,
                                           gdouble      x,
                                           gdouble      y)
{
  gfloat value;

  offset = 1.0 - offset / 100.0;

  gegl_sampler_get (dist_sampler, x, y, NULL, &value, GEGL_ABYSS_NONE);

  if (value > offset)
    value = 1.0;
  else if (offset == 0.0)
    value = (value <= 0.0) ? 0.0 : 1.0;
  else
    value = value / offset;

  value = 1.0 - sin (0.5 * G_PI * value);

  return value;
}


static gdouble
gradient_calc_shapeburst_dimpled_factor (GeglSampler *dist_sampler,
                                         gdouble      offset,
                                         gdouble      x,
                                         gdouble      y)
{
  gfloat value;

  offset = 1.0 - offset / 100.0;

  gegl_sampler_get (dist_sampler, x, y, NULL, &value, GEGL_ABYSS_NONE);

  if (value > offset)
    value = 1.0;
  else if (offset == 0.0)
    value = (value <= 0.0) ? 0.0 : 1.0;
  else
    value = value / offset;

  value = cos (0.5 * G_PI * value);

  return value;
}

static void
gradient_render_pixel (gdouble   x,
                       gdouble   y,
                       GimpRGB  *color,
                       gpointer  render_data)
{
  RenderBlendData *rbd = render_data;
  gdouble          factor;

  /*  we want to calculate the color at the pixel's center  */
  x += 0.5;
  y += 0.5;

  /* Calculate blending factor */

  switch (rbd->gradient_type)
    {
    case GIMP_GRADIENT_LINEAR:
      factor = gradient_calc_linear_factor (rbd->dist,
                                            rbd->vec, rbd->offset,
                                            x - rbd->sx, y - rbd->sy);
      break;

    case GIMP_GRADIENT_BILINEAR:
      factor = gradient_calc_bilinear_factor (rbd->dist,
                                              rbd->vec, rbd->offset,
                                              x - rbd->sx, y - rbd->sy);
      break;

    case GIMP_GRADIENT_RADIAL:
      factor = gradient_calc_radial_factor (rbd->dist,
                                            rbd->offset,
                                            x - rbd->sx, y - rbd->sy);
      break;

    case GIMP_GRADIENT_SQUARE:
      factor = gradient_calc_square_factor (rbd->dist, rbd->offset,
                                            x - rbd->sx, y - rbd->sy);
      break;

    case GIMP_GRADIENT_CONICAL_SYMMETRIC:
      factor = gradient_calc_conical_sym_factor (rbd->dist,
                                                 rbd->vec, rbd->offset,
                                                 x - rbd->sx, y - rbd->sy);
      break;

    case GIMP_GRADIENT_CONICAL_ASYMMETRIC:
      factor = gradient_calc_conical_asym_factor (rbd->dist,
                                                  rbd->vec, rbd->offset,
                                                  x - rbd->sx, y - rbd->sy);
      break;

    case GIMP_GRADIENT_SHAPEBURST_ANGULAR:
      factor = gradient_calc_shapeburst_angular_factor (rbd->dist_sampler,
                                                        rbd->offset,
                                                        x, y);
      break;

    case GIMP_GRADIENT_SHAPEBURST_SPHERICAL:
      factor = gradient_calc_shapeburst_spherical_factor (rbd->dist_sampler,
                                                          rbd->offset,
                                                          x, y);
      break;

    case GIMP_GRADIENT_SHAPEBURST_DIMPLED:
      factor = gradient_calc_shapeburst_dimpled_factor (rbd->dist_sampler,
                                                        rbd->offset,
                                                        x, y);
      break;

    case GIMP_GRADIENT_SPIRAL_CLOCKWISE:
      factor = gradient_calc_spiral_factor (rbd->dist,
                                            rbd->vec, rbd->offset,
                                            x - rbd->sx, y - rbd->sy, TRUE);
      break;

    case GIMP_GRADIENT_SPIRAL_ANTICLOCKWISE:
      factor = gradient_calc_spiral_factor (rbd->dist,
                                            rbd->vec, rbd->offset,
                                            x - rbd->sx, y - rbd->sy, FALSE);
      break;

    default:
      g_return_if_reached ();
      break;
    }

  /* Adjust for repeat */

  switch (rbd->repeat)
    {
    case GIMP_REPEAT_NONE:
      break;

    case GIMP_REPEAT_SAWTOOTH:
      factor = factor - floor (factor);
      break;

    case GIMP_REPEAT_TRIANGULAR:
      {
        guint ifactor;

        if (factor < 0.0)
          factor = -factor;

        ifactor = (guint) factor;
        factor = factor - floor (factor);

        if (ifactor & 1)
          factor = 1.0 - factor;
      }
      break;

    case GIMP_REPEAT_TRUNCATE:
      if (factor < 0.0 || factor > 1.0)
        {
          gimp_rgba_set (color, 0.0, 0.0, 0.0, 0.0);
          return;
        }
      break;
    }

  /* Blend the colors */

  if (rbd->gradient_cache)
    {
      factor = CLAMP (factor, 0.0, 1.0);

      *color =
        rbd->gradient_cache[ROUND (factor * (rbd->gradient_cache_size - 1))];
    }
  else
    {
      rbd->last_seg = gimp_gradient_get_color_at (rbd->gradient, NULL,
                                                  rbd->last_seg, factor,
                                                  rbd->reverse,
                                                  rbd->blend_color_space,
                                                  color);
    }
}

static void
gradient_put_pixel (gint      x,
                    gint      y,
                    GimpRGB  *color,
                    gpointer  put_pixel_data)
{
  PutPixelData *ppd   = put_pixel_data;
  const gint    index = (y - ppd->roi.y) * ppd->roi.width + (x - ppd->roi.x);
  gfloat       *dest  = ppd->data + 4 * index;

  if (ppd->dither_rand)
    {
      gradient_dither_pixel (color, ppd->dither_rand, dest);

      dest += 4;
    }
  else
    {
      *dest++ = color->r;
      *dest++ = color->g;
      *dest++ = color->b;
      *dest++ = color->a;
    }
}

static void
gradient_dither_pixel (GimpRGB *color,
                       GRand   *dither_rand,
                       gfloat  *dest)
{
  gfloat r, g, b, a;
  guint  i;

  i = g_rand_int (dither_rand);

  r = color->r + (gdouble) (i & 0xff) / 256.0 / 256.0 - 0.5 / 256.0; i >>= 8;
  g = color->g + (gdouble) (i & 0xff) / 256.0 / 256.0 - 0.5 / 256.0; i >>= 8;
  b = color->b + (gdouble) (i & 0xff) / 256.0 / 256.0 - 0.5 / 256.0; i >>= 8;

  if (color->a > 0.0 && color->a < 1.0)
    a = color->a + (gdouble) (i & 0xff) / 256.0 / 256.0 - 0.5 / 256.0;
  else
    a = color->a;

  *dest++ = CLAMP (r, 0.0, 1.0);
  *dest++ = CLAMP (g, 0.0, 1.0);
  *dest++ = CLAMP (b, 0.0, 1.0);
  *dest++ = CLAMP (a, 0.0, 1.0);
}

static gboolean
gimp_operation_gradient_process (GeglOperation       *operation,
                                 GeglBuffer          *input,
                                 GeglBuffer          *output,
                                 const GeglRectangle *result,
                                 gint                 level)
{
  GimpOperationGradient *self = GIMP_OPERATION_GRADIENT (operation);

  const gdouble sx = self->start_x;
  const gdouble sy = self->start_y;
  const gdouble ex = self->end_x;
  const gdouble ey = self->end_y;

  RenderBlendData rbd = { 0, };

  GeglBufferIterator *iter;
  GeglRectangle      *roi;
  GRand              *dither_rand = NULL;

  if (! self->gradient)
    return TRUE;

  gimp_operation_gradient_validate_cache (self);

  rbd.gradient            = self->gradient;
  rbd.reverse             = self->gradient_reverse;
  rbd.blend_color_space   = self->gradient_blend_color_space;
  rbd.gradient_cache      = self->gradient_cache;
  rbd.gradient_cache_size = self->gradient_cache_size;

  /* Calculate type-specific parameters */

  switch (self->gradient_type)
    {
    case GIMP_GRADIENT_RADIAL:
      rbd.dist = sqrt (SQR (ex - sx) + SQR (ey - sy));
      break;

    case GIMP_GRADIENT_SQUARE:
      rbd.dist = MAX (fabs (ex - sx), fabs (ey - sy));
      break;

    case GIMP_GRADIENT_CONICAL_SYMMETRIC:
    case GIMP_GRADIENT_CONICAL_ASYMMETRIC:
    case GIMP_GRADIENT_SPIRAL_CLOCKWISE:
    case GIMP_GRADIENT_SPIRAL_ANTICLOCKWISE:
    case GIMP_GRADIENT_LINEAR:
    case GIMP_GRADIENT_BILINEAR:
      rbd.dist = sqrt (SQR (ex - sx) + SQR (ey - sy));

      if (rbd.dist > 0.0)
        {
          rbd.vec[0] = (ex - sx) / rbd.dist;
          rbd.vec[1] = (ey - sy) / rbd.dist;
        }

      break;

    case GIMP_GRADIENT_SHAPEBURST_ANGULAR:
    case GIMP_GRADIENT_SHAPEBURST_SPHERICAL:
    case GIMP_GRADIENT_SHAPEBURST_DIMPLED:
      rbd.dist = sqrt (SQR (ex - sx) + SQR (ey - sy));
      rbd.dist_sampler = gegl_buffer_sampler_new_at_level (
        input, babl_format ("Y float"), GEGL_SAMPLER_NEAREST, level);
      break;

    default:
      g_return_val_if_reached (FALSE);
      break;
    }

  /* Initialize render data */

  rbd.offset        = self->offset;
  rbd.sx            = self->start_x;
  rbd.sy            = self->start_y;
  rbd.gradient_type = self->gradient_type;
  rbd.repeat        = self->gradient_repeat;

  /* Render the gradient! */

  iter = gegl_buffer_iterator_new (output, result, 0,
                                   babl_format ("R'G'B'A float"),
                                   GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE, 1);
  roi = &iter->items[0].roi;

  if (self->dither)
    dither_rand = g_rand_new ();

  if (self->supersample)
    {
      PutPixelData ppd;

      ppd.dither_rand = dither_rand;

      while (gegl_buffer_iterator_next (iter))
        {
          ppd.data = iter->items[0].data;
          ppd.roi  = *roi;

          gimp_adaptive_supersample_area (roi->x, roi->y,
                                          roi->x + roi->width  - 1,
                                          roi->y + roi->height - 1,
                                          self->supersample_depth,
                                          self->supersample_threshold,
                                          gradient_render_pixel, &rbd,
                                          gradient_put_pixel, &ppd,
                                          NULL,
                                          NULL);
        }
    }
  else
    {
      while (gegl_buffer_iterator_next (iter))
        {
          gfloat *dest = iter->items[0].data;
          gint    endx = roi->x + roi->width;
          gint    endy = roi->y + roi->height;
          gint    x, y;

          if (dither_rand)
            {
              for (y = roi->y; y < endy; y++)
                for (x = roi->x; x < endx; x++)
                  {
                    GimpRGB  color = { 0.0, 0.0, 0.0, 1.0 };

                    gradient_render_pixel (x, y, &color, &rbd);
                    gradient_dither_pixel (&color, dither_rand, dest);

                    dest += 4;
                  }
            }
          else
            {
              for (y = roi->y; y < endy; y++)
                for (x = roi->x; x < endx; x++)
                  {
                    GimpRGB  color = { 0.0, 0.0, 0.0, 1.0 };

                    gradient_render_pixel (x, y, &color, &rbd);

                    *dest++ = color.r;
                    *dest++ = color.g;
                    *dest++ = color.b;
                    *dest++ = color.a;
                  }
            }
        }
    }

  if (self->dither)
    g_rand_free (dither_rand);

  g_clear_object (&rbd.dist_sampler);

  return TRUE;
}

static void
gimp_operation_gradient_invalidate_cache (GimpOperationGradient *self)
{
  g_clear_pointer (&self->gradient_cache, g_free);
}

static void
gimp_operation_gradient_validate_cache (GimpOperationGradient *self)
{
  GimpGradientSegment *last_seg = NULL;
  gint                 cache_size;
  gint                 i;

  if (! self->gradient)
    return;

  g_mutex_lock (&self->gradient_cache_mutex);

  if (self->gradient_cache)
    {
      g_mutex_unlock (&self->gradient_cache_mutex);

      return;
    }

  cache_size = ceil (hypot (self->start_x - self->end_x,
                            self->start_y - self->end_y)) *
               GRADIENT_CACHE_N_SUPERSAMPLES;

  /*  have at least two values in the cache  */
  cache_size = MAX (cache_size, 2);

  /*  don't use a cache if its necessary size is too big  */
  if (cache_size > GRADIENT_CACHE_MAX_SIZE)
    {
      g_mutex_unlock (&self->gradient_cache_mutex);

      return;
    }

  self->gradient_cache      = g_new0 (GimpRGB, cache_size);
  self->gradient_cache_size = cache_size;

  for (i = 0; i < self->gradient_cache_size; i++)
    {
      gdouble factor = (gdouble) i / (gdouble) (self->gradient_cache_size - 1);

      last_seg = gimp_gradient_get_color_at (self->gradient, NULL, last_seg,
                                             factor,
                                             self->gradient_reverse,
                                             self->gradient_blend_color_space,
                                             self->gradient_cache + i);
    }

  g_mutex_unlock (&self->gradient_cache_mutex);
}