-
Notifications
You must be signed in to change notification settings - Fork 86
/
mypaint-brush.c
1698 lines (1471 loc) · 62.4 KB
/
mypaint-brush.c
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
/* libmypaint - The MyPaint Brush Library
* Copyright (C) 2007-2011 Martin Renold <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#if MYPAINT_CONFIG_USE_GLIB
#include <glib.h>
#include "glib/mypaint-brush.h"
#endif
#include "mypaint-brush.h"
#include "mypaint-brush-settings.h"
#include "mypaint-mapping.h"
#include "helpers.h"
#include "rng-double.h"
#include <json.h>
#ifdef _MSC_VER
#if _MSC_VER < 1700 // Visual Studio 2012 and later has isfinite and roundf
#include <float.h>
static inline int isfinite(double x) { return _finite(x); }
static inline float roundf (float x) { return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f); }
#endif
#endif
// Conversion from degrees to radians
#define RADIANS(x) ((x) * M_PI / 180.0)
// Conversion from radians to degrees
#define DEGREES(x) (((x) / (2 * M_PI)) * 360.0)
#define ACTUAL_RADIUS_MIN 0.2
#define ACTUAL_RADIUS_MAX 1000 // safety guard against radius like 1e20 and against rendering overload with unexpected brush dynamics
#define GRID_SIZE 256.0
/* Named indices for the smudge bucket arrays */
enum {
SMUDGE_R, SMUDGE_G, SMUDGE_B, SMUDGE_A,
PREV_COL_R, PREV_COL_G, PREV_COL_B, PREV_COL_A,
PREV_COL_RECENTNESS,
SMUDGE_BUCKET_SIZE
};
/* The Brush class stores two things:
b) settings: constant during a stroke (eg. size, spacing, dynamics, color selected by the user)
a) states: modified during a stroke (eg. speed, smudge colors, time/distance to next dab, position filter states)
FIXME: Actually those are two orthogonal things. Should separate them:
a) brush settings class that is saved/loaded/selected (without states)
b) brush core class to draw the dabs (using an instance of the above)
In python, there are two kinds of instances from this: a "global
brush" which does the cursor tracking, and the "brushlist" where
the states are ignored. When a brush is selected, its settings are
copied into the global one, leaving the state intact.
*/
/**
* MyPaintBrush:
*
* The MyPaint brush engine class.
*/
struct MyPaintBrush {
gboolean print_inputs; // debug menu
// for stroke splitting (undo/redo)
double stroke_total_painting_time;
double stroke_current_idling_time;
// the states (get_state, set_state, reset) that change during a stroke
float states[MYPAINT_BRUSH_STATES_COUNT];
// smudge bucket array: part of the state, but stored separately.
// Usually used for brushes with multiple offset dabs, where each
// dab is assigned its own bucket containing a smudge state.
float *smudge_buckets;
int num_buckets;
int min_bucket_used;
int max_bucket_used;
double random_input;
float skip;
float skip_last_x;
float skip_last_y;
float skipped_dtime;
RngDouble * rng;
// Those mappings describe how to calculate the current value for each setting.
// Most of settings will be constant (eg. only their base_value is used).
MyPaintMapping * settings[MYPAINT_BRUSH_SETTINGS_COUNT];
// the current value of all settings (calculated using the current state)
float settings_value[MYPAINT_BRUSH_SETTINGS_COUNT];
// see also brushsettings.py
// cached calculation results
float speed_mapping_gamma[2];
float speed_mapping_m[2];
float speed_mapping_q[2];
gboolean reset_requested;
json_object *brush_json;
int refcount;
};
/* Macros for accessing states and setting values
Although macros are never nice, simple file-local macros are warranted
here since it massively improves code readability.
The INPUT macro relies on 'inputs' being a float array of size MYPAINT_BRUSH_INPUTS_COUNT,
accessible in the scope where the macro is used.
*/
#define STATE(self, state_name) ((self)->states[MYPAINT_BRUSH_STATE_##state_name])
#define SETTING(self, setting_name) ((self)->settings_value[MYPAINT_BRUSH_SETTING_##setting_name])
#define BASEVAL(self, setting_name) (mypaint_mapping_get_base_value((self)->settings[MYPAINT_BRUSH_SETTING_##setting_name]))
#define INPUT(input_name) (inputs[MYPAINT_BRUSH_INPUT_##input_name])
void settings_base_values_have_changed (MyPaintBrush *self);
#include "glib/mypaint-brush.c"
void
brush_reset(MyPaintBrush *self)
{
self->skip = 0;
self->skip_last_x = 0;
self->skip_last_y = 0;
self->skipped_dtime = 0;
// Clear states
memset(self->states, 0, sizeof(self->states));
// Set the flip state such that it will be at "1" for the first
// dab, and then switch between -1 and 1 for the subsequent dabs.
STATE(self, FLIP) = -1;
// Clear smudge buckets
if (self->smudge_buckets) {
int min_index = self->min_bucket_used;
if (min_index != -1) {
int max_index = self->max_bucket_used;
size_t num_bytes = (max_index - min_index + 1) * sizeof(self->smudge_buckets[0]) * SMUDGE_BUCKET_SIZE;
memset(self->smudge_buckets + min_index, 0, num_bytes);
self->min_bucket_used = -1;
self->max_bucket_used = -1;
}
}
}
/**
* mypaint_brush_new:
*
* Create a new MyPaint brush engine instance.
* Initial reference count is 1. Release references using mypaint_brush_unref()
*/
MyPaintBrush *
mypaint_brush_new(void)
{
return mypaint_brush_new_with_buckets(0);
}
/**
* mypaint_brush_new_with_buckets:
*
* Create a new MyPaint brush engine instance with smudge bucket support.
* MyPaint 2.0 supports up to 255 buckets per brush.
* Initial reference count is 1. Release references using mypaint_brush_unref()
*/
MyPaintBrush *
mypaint_brush_new_with_buckets(int num_smudge_buckets)
{
MyPaintBrush *self = (MyPaintBrush *)malloc(sizeof(MyPaintBrush));
if (!self) {
return NULL;
}
if (num_smudge_buckets > 0) {
float *bucket_array = malloc(num_smudge_buckets * SMUDGE_BUCKET_SIZE * sizeof(float));
if (!bucket_array) {
free(self);
return NULL;
}
self->smudge_buckets = bucket_array;
self->num_buckets = num_smudge_buckets;
// Set up min/max to initialize (clear) the array in the call to brush_reset.
self->min_bucket_used = 0;
self->max_bucket_used = self->num_buckets - 1;
} else {
self->smudge_buckets = NULL;
self->num_buckets = 0;
}
self->refcount = 1;
for (int i = 0; i < MYPAINT_BRUSH_SETTINGS_COUNT; i++) {
self->settings[i] = mypaint_mapping_new(MYPAINT_BRUSH_INPUTS_COUNT);
}
self->rng = rng_double_new(1000);
self->random_input = 0;
self->print_inputs = FALSE;
brush_reset(self);
mypaint_brush_new_stroke(self);
settings_base_values_have_changed(self);
self->reset_requested = TRUE;
self->brush_json = json_object_new_object();
return self;
}
void
brush_free(MyPaintBrush *self)
{
for (int i = 0; i < MYPAINT_BRUSH_SETTINGS_COUNT; i++) {
mypaint_mapping_free(self->settings[i]);
}
rng_double_free (self->rng);
self->rng = NULL;
if (self->brush_json) {
json_object_put(self->brush_json);
}
free(self->smudge_buckets);
free(self);
}
/**
* mypaint_brush_unref: (skip)
*
* Decrease the reference count. Will be freed when it hits 0.
*/
void
mypaint_brush_unref(MyPaintBrush *self)
{
self->refcount--;
if (self->refcount == 0) {
brush_free(self);
}
}
/**
* mypaint_brush_ref: (skip)
*
* Increase the reference count.
*/
void
mypaint_brush_ref(MyPaintBrush *self)
{
self->refcount++;
}
/**
* mypaint_brush_get_total_stroke_painting_time:
*
* Return the total amount of painting time for the current stroke.
*/
double
mypaint_brush_get_total_stroke_painting_time(MyPaintBrush *self)
{
return self->stroke_total_painting_time;
}
/**
* mypaint_brush_set_print_inputs:
*
* Enable/Disable printing of brush engine inputs on stderr. Intended for debugging only.
*/
void
mypaint_brush_set_print_inputs(MyPaintBrush *self, gboolean enabled)
{
self->print_inputs = enabled;
}
/**
* mypaint_brush_reset:
*
* Reset the current brush engine state.
* Used when the next mypaint_brush_stroke_to() call is not related to the current state.
* Note that the reset request is queued and changes in state will only happen on next stroke_to()
*/
void
mypaint_brush_reset(MyPaintBrush *self)
{
self->reset_requested = TRUE;
}
/**
* mypaint_brush_new_stroke:
*
* Start a new stroke.
*/
void
mypaint_brush_new_stroke(MyPaintBrush *self)
{
self->stroke_current_idling_time = 0;
self->stroke_total_painting_time = 0;
}
/**
* mypaint_brush_set_base_value:
*
* Set the base value of a brush setting.
*/
void
mypaint_brush_set_base_value(MyPaintBrush *self, MyPaintBrushSetting id, float value)
{
assert (id < MYPAINT_BRUSH_SETTINGS_COUNT);
mypaint_mapping_set_base_value(self->settings[id], value);
settings_base_values_have_changed (self);
}
/**
* mypaint_brush_get_base_value:
*
* Get the base value of a brush setting.
*/
float
mypaint_brush_get_base_value(MyPaintBrush *self, MyPaintBrushSetting id)
{
assert (id < MYPAINT_BRUSH_SETTINGS_COUNT);
return mypaint_mapping_get_base_value(self->settings[id]);
}
/**
* mypaint_brush_set_mapping_n:
*
* Set the number of points used for the dynamics mapping between a #MyPaintBrushInput and #MyPaintBrushSetting.
*/
void
mypaint_brush_set_mapping_n(MyPaintBrush *self, MyPaintBrushSetting id, MyPaintBrushInput input, int n)
{
assert (id < MYPAINT_BRUSH_SETTINGS_COUNT);
mypaint_mapping_set_n(self->settings[id], input, n);
}
/**
* mypaint_brush_get_mapping_n:
*
* Get the number of points used for the dynamics mapping between a #MyPaintBrushInput and #MyPaintBrushSetting.
*/
int
mypaint_brush_get_mapping_n(MyPaintBrush *self, MyPaintBrushSetting id, MyPaintBrushInput input)
{
return mypaint_mapping_get_n(self->settings[id], input);
}
/**
* mypaint_brush_is_constant:
*
* Returns TRUE if the brush has no dynamics for the given #MyPaintBrushSetting
*/
gboolean
mypaint_brush_is_constant(MyPaintBrush *self, MyPaintBrushSetting id)
{
assert (id < MYPAINT_BRUSH_SETTINGS_COUNT);
return mypaint_mapping_is_constant(self->settings[id]);
}
/**
* mypaint_brush_get_inputs_used_n:
*
* Returns how many inputs are used for the dynamics of a #MyPaintBrushSetting
*/
int
mypaint_brush_get_inputs_used_n(MyPaintBrush *self, MyPaintBrushSetting id)
{
assert (id < MYPAINT_BRUSH_SETTINGS_COUNT);
return mypaint_mapping_get_inputs_used_n(self->settings[id]);
}
/**
* mypaint_brush_set_mapping_point:
*
* Set a X,Y point of a dynamics mapping.
* The index must be within the number of points set using mypaint_brush_set_mapping_n()
*/
void
mypaint_brush_set_mapping_point(MyPaintBrush *self, MyPaintBrushSetting id, MyPaintBrushInput input, int index, float x, float y)
{
assert (id < MYPAINT_BRUSH_SETTINGS_COUNT);
mypaint_mapping_set_point(self->settings[id], input, index, x, y);
}
/**
* mypaint_brush_get_mapping_point:
* @x: (out): Location to return the X value
* @y: (out): Location to return the Y value
*
* Get a X,Y point of a dynamics mapping.
**/
void
mypaint_brush_get_mapping_point(MyPaintBrush *self, MyPaintBrushSetting id, MyPaintBrushInput input, int index, float *x, float *y)
{
assert (id < MYPAINT_BRUSH_SETTINGS_COUNT);
mypaint_mapping_get_point(self->settings[id], input, index, x, y);
}
/**
* mypaint_brush_get_state:
*
* Get an internal brush engine state.
* Normally used for debugging, but can be used to implement record & replay functionality.
**/
float
mypaint_brush_get_state(MyPaintBrush *self, MyPaintBrushState i)
{
assert (i < MYPAINT_BRUSH_STATES_COUNT);
return self->states[i];
}
/**
* mypaint_brush_set_state:
*
* Set an internal brush engine state.
* Normally used for debugging, but can be used to implement record & replay functionality.
**/
void
mypaint_brush_set_state(MyPaintBrush *self, MyPaintBrushState i, float value)
{
assert (i < MYPAINT_BRUSH_STATES_COUNT);
self->states[i] = value;
}
/**
* mypaint_brush_set_smudge_bucket_state: (skip)
*
* Set an internal smudge bucket state for the bucket of the given index.
* Returns true if the state could be updated, false otherwise.
* Setting the state can only fail if the bucket index lies outside the
* capacity of the brush.
*
**/
gboolean
mypaint_brush_set_smudge_bucket_state(
MyPaintBrush* self, int bucket_index,
float r, float g, float b, float a,
float prev_r, float prev_g, float prev_b, float prev_a,
float prev_color_recentness)
{
if (self->num_buckets > bucket_index) {
float* bucket = &self->smudge_buckets[bucket_index];
bucket[SMUDGE_R] = r;
bucket[SMUDGE_G] = g;
bucket[SMUDGE_B] = b;
bucket[SMUDGE_A] = a;
bucket[PREV_COL_R] = prev_r;
bucket[PREV_COL_G] = prev_g;
bucket[PREV_COL_B] = prev_b;
bucket[PREV_COL_A] = prev_a;
bucket[PREV_COL_RECENTNESS] = prev_color_recentness;
return TRUE;
}
return FALSE;
}
/**
* mypaint_brush_get_smudge_bucket_state: (skip)
*
* Retrieve the internal state for the smudge bucket of the given index.
* If the state could be retrieved, populating the out variables, true is returned.
* Getting the state can only fail if the bucket index lies outside the
* capacity of the brush.
*
**/
gboolean
mypaint_brush_get_smudge_bucket_state(
const MyPaintBrush* self, int bucket_index,
float* r, float* g, float* b, float* a,
float* prev_r, float* prev_g, float* prev_b, float* prev_a,
float* prev_color_recentness)
{
if (self->num_buckets > bucket_index) {
float* bucket = &self->smudge_buckets[bucket_index];
*r = bucket[SMUDGE_R];
*g = bucket[SMUDGE_G];
*b = bucket[SMUDGE_B];
*a = bucket[SMUDGE_A];
*prev_r = bucket[PREV_COL_R];
*prev_g = bucket[PREV_COL_G];
*prev_b = bucket[PREV_COL_B];
*prev_a = bucket[PREV_COL_A];
*prev_color_recentness = bucket[PREV_COL_RECENTNESS];
return TRUE;
}
return FALSE;
}
/**
* mypaint_brush_get_min_smudge_bucket_used: (skip)
*
* Get the lowest index of the indices of buckets that have been written to.
* If no smudge bucket has been written to, -1 is returned.
*
*/
int mypaint_brush_get_min_smudge_bucket_used(const MyPaintBrush* self)
{
return self->min_bucket_used;
}
/**
* mypaint_brush_get_max_smudge_bucket_used: (skip)
*
* Get the largest index of the indices of buckets that have been written to.
* If no smudge bucket has been written to, -1 is returned.
*
*/
int mypaint_brush_get_max_smudge_bucket_used(const MyPaintBrush* self)
{
return self->max_bucket_used;
}
// returns the fraction still left after t seconds
float exp_decay (float T_const, float t)
{
// the argument might not make mathematical sense (whatever.)
if (T_const <= 0.001) {
return 0.0;
}
const float arg = -t / T_const;
return expf(arg);
}
void settings_base_values_have_changed (MyPaintBrush *self)
{
// precalculate stuff that does not change dynamically
// Precalculate how the physical speed will be mapped to the speed input value.
// The formula for this mapping is:
//
// y = log(gamma+x)*m + q;
//
// x: the physical speed (pixels per basic dab radius)
// y: the speed input that will be reported
// gamma: parameter set by the user (small means a logarithmic mapping, big linear)
// m, q: parameters to scale and translate the curve
//
// The code below calculates m and q given gamma and two hardcoded constraints.
//
for (int i = 0; i < 2; i++) {
const float gamma = expf(i == 0 ? BASEVAL(self, SPEED1_GAMMA) : BASEVAL(self, SPEED2_GAMMA));
const float fix1_x = 45.0;
const float fix1_y = 0.5;
const float fix2_x = 45.0;
const float fix2_dy = 0.015;
const float c1 = log(fix1_x + gamma);
const float m = fix2_dy * (fix2_x + gamma);
const float q = fix1_y - m * c1;
self->speed_mapping_gamma[i] = gamma;
self->speed_mapping_m[i] = m;
self->speed_mapping_q[i] = q;
}
}
typedef struct {
float x;
float y;
} Offsets;
Offsets
directional_offsets(const MyPaintBrush* const self, const float base_radius, const int brush_flip)
{
const float offset_mult = expf(SETTING(self, OFFSET_MULTIPLIER));
// Sanity check - it is easy to reach infinite multipliers w. logarithmic parameters
if (!isfinite(offset_mult)) {
Offsets offs = {0.0f, 0.0f};
return offs;
}
float dx = SETTING(self, OFFSET_X);
float dy = SETTING(self, OFFSET_Y);
//Anti_Art offsets tweaked by BrienD. Adjusted with ANGLE_ADJ and OFFSET_MULTIPLIER
const float offset_angle_adj = SETTING(self, OFFSET_ANGLE_ADJ);
const float dir_angle_dy = STATE(self, DIRECTION_ANGLE_DY);
const float dir_angle_dx = STATE(self, DIRECTION_ANGLE_DX);
const float angle_deg = fmodf(DEGREES(atan2f(dir_angle_dy, dir_angle_dx)) - 90, 360);
//offset to one side of direction
const float offset_angle = SETTING(self, OFFSET_ANGLE);
if (offset_angle) {
const float dir_angle = RADIANS(angle_deg + offset_angle_adj);
dx += cos(dir_angle) * offset_angle;
dy += sin(dir_angle) * offset_angle;
}
//offset to one side of ascension angle
const float view_rotation = STATE(self, VIEWROTATION);
const float offset_angle_asc = SETTING(self, OFFSET_ANGLE_ASC);
if (offset_angle_asc) {
const float ascension = STATE(self, ASCENSION);
const float asc_angle = RADIANS(ascension - view_rotation + offset_angle_adj);
dx += cos(asc_angle) * offset_angle_asc;
dy += sin(asc_angle) * offset_angle_asc;
}
//offset to one side of view orientation
const float view_offset = SETTING(self, OFFSET_ANGLE_VIEW);
if (view_offset) {
const float view_angle = RADIANS(view_rotation + offset_angle_adj);
dx += cos(-view_angle) * view_offset;
dy += sin(-view_angle) * view_offset;
}
//offset mirrored to sides of direction
const float offset_dir_mirror = MAX(0.0, SETTING(self, OFFSET_ANGLE_2));
if (offset_dir_mirror) {
const float dir_mirror_angle = RADIANS(angle_deg + offset_angle_adj * brush_flip);
const float offset_factor = offset_dir_mirror * brush_flip;
dx += cos(dir_mirror_angle) * offset_factor;
dy += sin(dir_mirror_angle) * offset_factor;
}
//offset mirrored to sides of ascension angle
const float offset_asc_mirror = MAX(0.0, SETTING(self, OFFSET_ANGLE_2_ASC));
if (offset_asc_mirror) {
const float ascension = STATE(self, ASCENSION);
const float asc_angle = RADIANS(ascension - view_rotation + offset_angle_adj * brush_flip);
const float offset_factor = brush_flip * offset_asc_mirror;
dx += cos(asc_angle) * offset_factor;
dy += sin(asc_angle) * offset_factor;
}
//offset mirrored to sides of view orientation
const float offset_view_mirror = MAX(0.0, SETTING(self, OFFSET_ANGLE_2_VIEW));
if (offset_view_mirror) {
const float offset_factor = brush_flip * offset_view_mirror;
const float offset_angle_rad = RADIANS(view_rotation + offset_angle_adj);
dx += cos(-offset_angle_rad) * offset_factor;
dy += sin(-offset_angle_rad) * offset_factor;
}
// Clamp the final offsets to avoid potential memory issues (extreme memory use from redraws)
// Allow offsets up to the 1080 * 3 pixels. Unlikely to hamper anyone artistically.
const float lim = 3240;
const float base_mul = base_radius * offset_mult;
Offsets offs = {CLAMP(dx * base_mul, -lim, lim), CLAMP(dy * base_mul, -lim, lim)};
return offs;
}
// Debugging: print brush inputs/states (not all of them)
void print_inputs(MyPaintBrush *self, float* inputs)
{
printf(
"press=% 4.3f, speed1=% 4.4f\tspeed2=% 4.4f",
INPUT(PRESSURE),
INPUT(SPEED1),
INPUT(SPEED2)
);
printf(
"\tstroke=% 4.3f\tcustom=% 4.3f",
INPUT(STROKE),
INPUT(CUSTOM)
);
printf(
"\tviewzoom=% 4.3f\tviewrotation=% 4.3f",
INPUT(VIEWZOOM),
STATE(self, VIEWROTATION)
);
printf(
"\tasc=% 4.3f\tdir=% 4.3f\tdec=% 4.3f\tdabang=% 4.3f",
INPUT(TILT_ASCENSION),
INPUT(DIRECTION),
INPUT(TILT_DECLINATION),
STATE(self, ACTUAL_ELLIPTICAL_DAB_ANGLE)
);
printf(
"\txtilt=% 4.3f\tytilt=% 4.3fattack=% 4.3f",
INPUT(TILT_DECLINATIONX),
INPUT(TILT_DECLINATIONY),
INPUT(ATTACK_ANGLE)
);
printf("\n");
}
// This function runs a brush "simulation" step. Usually it is
// called once or twice per dab. In theory the precision of the
// "simulation" gets better when it is called more often. In
// practice this only matters if there are some highly nonlinear
// mappings in critical places or extremely few events per second.
//
// note: parameters are is dx/ddab, ..., dtime/ddab (dab is the number, 5.0 = 5th dab)
void update_states_and_setting_values (MyPaintBrush *self, float step_ddab, float step_dx, float step_dy, float step_dpressure, float step_declination, float step_ascension, float step_dtime, float step_viewzoom, float step_viewrotation, float step_declinationx, float step_declinationy, float step_barrel_rotation)
{
if (step_dtime < 0.0) {
printf("Time is running backwards!\n");
step_dtime = 0.001;
} else if (step_dtime == 0.0) {
// FIXME: happens about every 10th start, workaround (against division by zero)
step_dtime = 0.001;
}
STATE(self, X) += step_dx;
STATE(self, Y) += step_dy;
STATE(self, PRESSURE) += step_dpressure;
STATE(self, DECLINATION) += step_declination;
STATE(self, ASCENSION) += step_ascension;
STATE(self, DECLINATIONX) += step_declinationx;
STATE(self, DECLINATIONY) += step_declinationy;
STATE(self, VIEWZOOM) = step_viewzoom;
const float viewrotation = mod_arith(DEGREES(step_viewrotation) + 180.0, 360.0) - 180.0;
STATE(self, VIEWROTATION) = viewrotation;
{ // Gridmap state update
const float x = STATE(self, ACTUAL_X);
const float y = STATE(self, ACTUAL_Y);
const float scale = expf(SETTING(self, GRIDMAP_SCALE));
const float scale_x = SETTING(self, GRIDMAP_SCALE_X);
const float scale_y = SETTING(self, GRIDMAP_SCALE_Y);
const float scaled_size = scale * GRID_SIZE;
STATE(self, GRIDMAP_X) = mod_arith(fabsf(x * scale_x), scaled_size) / scaled_size * GRID_SIZE;
STATE(self, GRIDMAP_Y) = mod_arith(fabsf(y * scale_y), scaled_size) / scaled_size * GRID_SIZE;
if (x < 0.0) {
STATE(self, GRIDMAP_X) = GRID_SIZE - STATE(self, GRIDMAP_X);
}
if (y < 0.0) {
STATE(self, GRIDMAP_Y) = GRID_SIZE - STATE(self, GRIDMAP_Y);
}
}
float base_radius = expf(BASEVAL(self, RADIUS_LOGARITHMIC));
STATE(self, BARREL_ROTATION) += step_barrel_rotation;
// FIXME: does happen (interpolation problem?)
if (STATE(self, PRESSURE) <= 0.0) STATE(self, PRESSURE) = 0.0;
const float pressure = STATE(self, PRESSURE);
{ // start / end stroke (for "stroke" input only)
const float lim = 0.0001;
const float threshold = BASEVAL(self, STROKE_THRESHOLD);
const float started = STATE(self, STROKE_STARTED);
if (!started && pressure > threshold + lim) {
// start new stroke
STATE(self, STROKE_STARTED) = 1;
STATE(self, STROKE) = 0.0;
} else if (started && pressure <= threshold * 0.9 + lim) {
// end stroke
STATE(self, STROKE_STARTED) = 0;
}
}
// now follows input handling
//adjust speed with viewzoom
const float norm_dx = step_dx / step_dtime * STATE(self, VIEWZOOM);
const float norm_dy = step_dy / step_dtime * STATE(self, VIEWZOOM);
const float norm_speed = hypotf(norm_dx, norm_dy);
//norm_dist should relate to brush size, whereas norm_speed should not
const float norm_dist = hypotf(step_dx / step_dtime / base_radius, step_dy / step_dtime / base_radius) * step_dtime;
float inputs[MYPAINT_BRUSH_INPUTS_COUNT];
INPUT(PRESSURE) = pressure * expf(BASEVAL(self, PRESSURE_GAIN_LOG));
const float m0 = self->speed_mapping_m[0];
const float q0 = self->speed_mapping_q[0];
const float m1 = self->speed_mapping_m[1];
const float q1 = self->speed_mapping_q[1];
INPUT(SPEED1) = log(self->speed_mapping_gamma[0] + STATE(self, NORM_SPEED1_SLOW)) * m0 + q0;
INPUT(SPEED2) = log(self->speed_mapping_gamma[1] + STATE(self, NORM_SPEED2_SLOW)) * m1 + q1;
INPUT(RANDOM) = self->random_input;
INPUT(STROKE) = MIN(STATE(self, STROKE), 1.0);
//correct direction for varying view rotation
const float dir_angle = atan2f(STATE(self, DIRECTION_DY), STATE(self, DIRECTION_DX));
INPUT(DIRECTION) = mod_arith(DEGREES(dir_angle) + viewrotation + 180.0, 180.0);
const float dir_angle_360 = atan2f(STATE(self, DIRECTION_ANGLE_DY), STATE(self, DIRECTION_ANGLE_DX));
INPUT(DIRECTION_ANGLE) = fmodf(DEGREES(dir_angle_360) + viewrotation + 360.0, 360.0) ;
INPUT(TILT_DECLINATION) = STATE(self, DECLINATION);
//correct ascension for varying view rotation, use custom mod
INPUT(TILT_ASCENSION) = mod_arith(STATE(self, ASCENSION) + viewrotation + 180.0, 360.0) - 180.0;
INPUT(VIEWZOOM) = BASEVAL(self, RADIUS_LOGARITHMIC) - logf(base_radius / STATE(self, VIEWZOOM));
INPUT(ATTACK_ANGLE) = smallest_angular_difference(STATE(self, ASCENSION), mod_arith(DEGREES(dir_angle_360) + 90, 360));
INPUT(BRUSH_RADIUS) = BASEVAL(self, RADIUS_LOGARITHMIC);
INPUT(GRIDMAP_X) = CLAMP(STATE(self, GRIDMAP_X), 0.0, GRID_SIZE);
INPUT(GRIDMAP_Y) = CLAMP(STATE(self, GRIDMAP_Y), 0.0, GRID_SIZE);
INPUT(TILT_DECLINATIONX) = STATE(self, DECLINATIONX);
INPUT(TILT_DECLINATIONY) = STATE(self, DECLINATIONY);
INPUT(CUSTOM) = STATE(self, CUSTOM_INPUT);
INPUT(BARREL_ROTATION) = mod_arith(STATE(self, BARREL_ROTATION), 360);
if (self->print_inputs) {
print_inputs(self, inputs);
}
for (int i = 0; i < MYPAINT_BRUSH_SETTINGS_COUNT; i++) {
self->settings_value[i] = mypaint_mapping_calculate(self->settings[i], (inputs));
}
STATE(self, DABS_PER_BASIC_RADIUS) = SETTING(self, DABS_PER_BASIC_RADIUS);
STATE(self, DABS_PER_ACTUAL_RADIUS) = SETTING(self, DABS_PER_ACTUAL_RADIUS);
STATE(self, DABS_PER_SECOND) = SETTING(self, DABS_PER_SECOND);
{
const float fac = 1.0 - exp_decay(SETTING(self, SLOW_TRACKING_PER_DAB), step_ddab);
STATE(self, ACTUAL_X) += (STATE(self, X) - STATE(self, ACTUAL_X)) * fac;
STATE(self, ACTUAL_Y) += (STATE(self, Y) - STATE(self, ACTUAL_Y)) * fac;
}
{ // slow speed
const float fac1 = 1.0 - exp_decay(SETTING(self, SPEED1_SLOWNESS), step_dtime);
STATE(self, NORM_SPEED1_SLOW) += (norm_speed - STATE(self, NORM_SPEED1_SLOW)) * fac1;
const float fac2 = 1.0 - exp_decay (SETTING(self, SPEED2_SLOWNESS), step_dtime);
STATE(self, NORM_SPEED2_SLOW) += (norm_speed - STATE(self, NORM_SPEED2_SLOW)) * fac2;
}
{ // slow speed, but as vector this time
float time_constant = expf(SETTING(self, OFFSET_BY_SPEED_SLOWNESS)*0.01)-1.0;
// Workaround for a bug that happens mainly on Windows, causing
// individual dabs to be placed far far away. Using the speed
// with zero filtering is just asking for trouble anyway.
if (time_constant < 0.002) time_constant = 0.002;
const float fac = 1.0 - exp_decay (time_constant, step_dtime);
STATE(self, NORM_DX_SLOW) += (norm_dx - STATE(self, NORM_DX_SLOW)) * fac;
STATE(self, NORM_DY_SLOW) += (norm_dy - STATE(self, NORM_DY_SLOW)) * fac;
}
{ // orientation (similar lowpass filter as above, but use dabtime instead of wallclock time)
// adjust speed with viewzoom
float dx = step_dx * STATE(self, VIEWZOOM);
float dy = step_dy * STATE(self, VIEWZOOM);
const float step_in_dabtime = hypotf(dx, dy);
const float fac = 1.0 - exp_decay(expf(SETTING(self, DIRECTION_FILTER) * 0.5) - 1.0, step_in_dabtime);
const float dx_old = STATE(self, DIRECTION_DX);
const float dy_old = STATE(self, DIRECTION_DY);
// 360 Direction
STATE(self, DIRECTION_ANGLE_DX) += (dx - STATE(self, DIRECTION_ANGLE_DX)) * fac;
STATE(self, DIRECTION_ANGLE_DY) += (dy - STATE(self, DIRECTION_ANGLE_DY)) * fac;
// use the opposite speed vector if it is closer (we don't care about 180 degree turns)
if (SQR(dx_old-dx) + SQR(dy_old-dy) > SQR(dx_old-(-dx)) + SQR(dy_old-(-dy))) {
dx = -dx;
dy = -dy;
}
STATE(self, DIRECTION_DX) += (dx - STATE(self, DIRECTION_DX)) * fac;
STATE(self, DIRECTION_DY) += (dy - STATE(self, DIRECTION_DY)) * fac;
}
{ // custom input
const float fac = 1.0 - exp_decay (SETTING(self, CUSTOM_INPUT_SLOWNESS), 0.1);
STATE(self, CUSTOM_INPUT) += (SETTING(self, CUSTOM_INPUT) - STATE(self, CUSTOM_INPUT)) * fac;
}
{ // stroke length
const float frequency = expf(-SETTING(self, STROKE_DURATION_LOGARITHMIC));
const float stroke = MAX(0, STATE(self, STROKE) + norm_dist * frequency);
const float wrap = 1.0 + MAX(0, SETTING(self, STROKE_HOLDTIME));
// If the hold time is above 9.9, it is considered infinite, and if the stroke value has reached
// that threshold it is no longer updated (until the stroke is reset, or the hold time changes).
if (stroke >= wrap && wrap > 9.9 + 1.0) {
STATE(self, STROKE) = 1.0;
} else if (stroke >= wrap) {
STATE(self, STROKE) = fmodf(stroke, wrap);
} else {
STATE(self, STROKE) = stroke;
}
}
// calculate final radius
const float radius_log = SETTING(self, RADIUS_LOGARITHMIC);
STATE(self, ACTUAL_RADIUS) = expf(radius_log);
if (STATE(self, ACTUAL_RADIUS) < ACTUAL_RADIUS_MIN) STATE(self, ACTUAL_RADIUS) = ACTUAL_RADIUS_MIN;
if (STATE(self, ACTUAL_RADIUS) > ACTUAL_RADIUS_MAX) STATE(self, ACTUAL_RADIUS) = ACTUAL_RADIUS_MAX;
// aspect ratio (needs to be calculated here because it can affect the dab spacing)
STATE(self, ACTUAL_ELLIPTICAL_DAB_RATIO) = SETTING(self, ELLIPTICAL_DAB_RATIO);
// correct dab angle for view rotation
STATE(self, ACTUAL_ELLIPTICAL_DAB_ANGLE) = mod_arith(SETTING(self, ELLIPTICAL_DAB_ANGLE) - viewrotation + 180.0, 180.0) - 180.0;
}
float *fetch_smudge_bucket(MyPaintBrush *self) {
if (!self->smudge_buckets || !self->num_buckets) {
return &STATE(self, SMUDGE_RA);
}
const int bucket_index = CLAMP(roundf(SETTING(self, SMUDGE_BUCKET)), 0, self->num_buckets - 1);
if (self->min_bucket_used == -1 || self->min_bucket_used > bucket_index) {
self->min_bucket_used = bucket_index;
}
if (self->max_bucket_used < bucket_index) {
self->max_bucket_used = bucket_index;
}
return &self->smudge_buckets[bucket_index * SMUDGE_BUCKET_SIZE];
}
gboolean
update_smudge_color(
const MyPaintBrush* self, MyPaintSurface* surface, float* const smudge_bucket, const float smudge_length, int px,
int py, const float radius, const float legacy_smudge, const float paint_factor)
{
// Value between 0.01 and 1.0 that determines how often the canvas should be resampled
float update_factor = MAX(0.01, smudge_length);
// Calling get_color() is almost as expensive as rendering a
// dab. Because of this we use the previous value if it is not
// expected to hurt quality too much. We call it at most every
// second dab.
float r, g, b, a;
const float smudge_length_log = SETTING(self, SMUDGE_LENGTH_LOG);
const float recentness = smudge_bucket[PREV_COL_RECENTNESS] * update_factor;
smudge_bucket[PREV_COL_RECENTNESS] = recentness;
const float margin = 0.0000000000000001;
if (recentness < MIN(1.0, powf(0.5 * update_factor, smudge_length_log) + margin)) {
if (recentness == 0.0) {
// first initialization of smudge color (initiate with color sampled from canvas)
update_factor = 0.0;
}
smudge_bucket[PREV_COL_RECENTNESS] = 1.0;
const float radius_log = SETTING(self, SMUDGE_RADIUS_LOG);
const float smudge_radius = CLAMP(radius * expf(radius_log), ACTUAL_RADIUS_MIN, ACTUAL_RADIUS_MAX);
// Sample colors on the canvas, using a negative value for the paint factor
// means that the old sampling method is used, instead of weighted spectral.
mypaint_surface_get_color(
surface, px, py, smudge_radius, &r, &g, &b, &a, legacy_smudge ? -1.0 : paint_factor);
// don't draw unless the picked-up alpha is above a certain level
// this is sort of like lock_alpha but for smudge
// negative values reverse this idea
const float smudge_op_lim = SETTING(self, SMUDGE_TRANSPARENCY);
if ((smudge_op_lim > 0.0 && a < smudge_op_lim) || (smudge_op_lim < 0.0 && a > -smudge_op_lim)) {
return TRUE; // signals the caller to return early
}
smudge_bucket[PREV_COL_R] = r;
smudge_bucket[PREV_COL_G] = g;
smudge_bucket[PREV_COL_B] = b;
smudge_bucket[PREV_COL_A] = a;
} else {
r = smudge_bucket[PREV_COL_R];
g = smudge_bucket[PREV_COL_G];
b = smudge_bucket[PREV_COL_B];
a = smudge_bucket[PREV_COL_A];
}
if (legacy_smudge) {
const float fac_old = update_factor;
const float fac_new = (1.0 - update_factor) * a;
smudge_bucket[SMUDGE_R] = fac_old * smudge_bucket[SMUDGE_R] + fac_new * r;
smudge_bucket[SMUDGE_G] = fac_old * smudge_bucket[SMUDGE_G] + fac_new * g;
smudge_bucket[SMUDGE_B] = fac_old * smudge_bucket[SMUDGE_B] + fac_new * b;
smudge_bucket[SMUDGE_A] = CLAMP((fac_old * smudge_bucket[SMUDGE_A] + fac_new), 0.0, 1.0);
} else if (a > WGM_EPSILON * 10) {
float prev_smudge_color[4] = {smudge_bucket[SMUDGE_R], smudge_bucket[SMUDGE_G], smudge_bucket[SMUDGE_B],
smudge_bucket[SMUDGE_A]};
float sampled_color[4] = {r, g, b, a};
float* smudge_new = mix_colors(prev_smudge_color, sampled_color, update_factor, paint_factor);
smudge_bucket[SMUDGE_R] = smudge_new[SMUDGE_R];
smudge_bucket[SMUDGE_G] = smudge_new[SMUDGE_G];
smudge_bucket[SMUDGE_B] = smudge_new[SMUDGE_B];
smudge_bucket[SMUDGE_A] = smudge_new[SMUDGE_A];
} else {
// To avoid color noise from spectral mixing with a low alpha,
// we'll just decrease the alpha of the existing smudge color.
smudge_bucket[SMUDGE_A] = (smudge_bucket[SMUDGE_A] + a) / 2;
}
return FALSE; // signals the caller to not return early (the default)
}
float
apply_smudge(