/
image_modifications.cpp
1303 lines (1072 loc) · 32.4 KB
/
image_modifications.cpp
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
/*
Copyright (C) 2009 - 2024
by Iris Morelle <shadowm2006@gmail.com>
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
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 2 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.
See the COPYING file for more details.
*/
#include "image_modifications.hpp"
#include "color.hpp"
#include "config.hpp"
#include "game_config.hpp"
#include "picture.hpp"
#include "lexical_cast.hpp"
#include "log.hpp"
#include "serialization/string_utils.hpp"
#include "team.hpp"
#include "utils/from_chars.hpp"
#include "formula/formula.hpp"
#include "formula/callable.hpp"
#define GETTEXT_DOMAIN "wesnoth-lib"
static lg::log_domain log_display("display");
#define ERR_DP LOG_STREAM(err, log_display)
namespace image {
/** Adds @a mod to the queue (unless mod is nullptr). */
void modification_queue::push(std::unique_ptr<modification> mod)
{
priorities_[mod->priority()].push_back(std::move(mod));
}
/** Removes the top element from the queue */
void modification_queue::pop()
{
map_type::iterator top_pair = priorities_.begin();
auto& top_vector = top_pair->second;
// Erase the top element.
top_vector.erase(top_vector.begin());
if(top_vector.empty()) {
// We need to keep the map clean.
priorities_.erase(top_pair);
}
}
/** Returns the number of elements in the queue. */
std::size_t modification_queue::size() const
{
std::size_t count = 0;
for(const map_type::value_type& pair : priorities_) {
count += pair.second.size();
}
return count;
}
/** Returns the top element in the queue . */
modification * modification_queue::top() const
{
return priorities_.begin()->second.front().get();
}
namespace {
/** A function used to parse modification arguments */
using mod_parser = std::function<std::unique_ptr<modification>(std::string_view)>;
/** A map of all registered mod parsers
*
* The mapping is between the modification name and the parser function pointer
* An example of an entry would be "TC" -> &parse_TC_mod
*/
std::map<std::string, mod_parser, std::less<>> mod_parsers;
/** Decodes a single modification using an appropriate mod_parser
*
* @param encoded_mod A string representing a single modification
*
* @return A pointer to the decoded modification object
* @retval nullptr if the string is invalid or a parser isn't found
*/
std::unique_ptr<modification> decode_modification(const std::string& encoded_mod)
{
const std::vector<std::string> split = utils::parenthetical_split(encoded_mod);
if(split.size() != 2) {
ERR_DP << "error parsing image modifications: " << encoded_mod;
return nullptr;
}
const std::string& mod_type = split[0];
const std::string& args = split[1];
if(const auto parser = mod_parsers.find(mod_type); parser != mod_parsers.end()) {
return std::invoke(parser->second, args);
} else {
ERR_DP << "unknown image function in path: " << mod_type;
return nullptr;
}
}
} // end anon namespace
modification::imod_exception::imod_exception(const std::stringstream& message_stream)
: message(message_stream.str())
{
this->store();
}
modification::imod_exception::imod_exception(const std::string& message)
: message(message)
{
this->store();
}
/** Decodes the modification string
*
* Important:
* It creates new objects which need to be deleted after use
*
* @param encoded_mods A string representing any number of modifications
*
* @return A modification_queue filled with decoded modification pointers
*/
modification_queue modification::decode(const std::string& encoded_mods)
{
modification_queue mods;
for(const std::string& encoded_mod : utils::parenthetical_split(encoded_mods, '~')) {
if(auto mod = decode_modification(encoded_mod)) {
mods.push(std::move(mod));
}
}
return mods;
}
surface rc_modification::operator()(const surface& src) const
{
// unchecked
return recolor_image(src, rc_map_);
}
surface fl_modification::operator()(const surface& src) const
{
surface ret = src;
if(horiz_ && vert_ ) {
// Slightly faster than doing both a flip and a flop.
ret = rotate_180_surface(ret);
} else if(horiz_) {
ret = flip_surface(ret);
} else if(vert_) {
ret = flop_surface(ret);
}
return ret;
}
surface rotate_modification::operator()(const surface& src) const
{
// Convert the number of degrees to the interval [0,360].
const int normalized = degrees_ >= 0 ?
degrees_ - 360 * (degrees_ / 360) :
degrees_ + 360 * (1 + (-degrees_) / 360); // In case compilers disagree as to what -90/360 is.
switch ( normalized )
{
case 0: return src;
case 90: return rotate_90_surface(src, true);
case 180: return rotate_180_surface(src);
case 270: return rotate_90_surface(src, false);
case 360: return src;
}
return rotate_any_surface(src, normalized, zoom_, offset_);
}
surface gs_modification::operator()(const surface& src) const
{
return greyscale_image(src);
}
surface crop_transparency_modification::operator()(const surface& src) const
{
return get_non_transparent_portion(src);
}
surface bw_modification::operator()(const surface& src) const
{
return monochrome_image(src, threshold_);
}
surface sepia_modification::operator()(const surface &src) const
{
return sepia_image(src);
}
surface negative_modification::operator()(const surface &src) const
{
return negative_image(src, red_, green_, blue_);
}
surface plot_alpha_modification::operator()(const surface& src) const
{
return alpha_to_greyscale(src);
}
surface wipe_alpha_modification::operator()(const surface& src) const
{
return wipe_alpha(src);
}
// TODO: Is this useful enough to move into formula/callable_objects?
class pixel_callable : public wfl::formula_callable
{
public:
pixel_callable(SDL_Point p, color_t clr, uint32_t w, uint32_t h)
: p(p), clr(clr), w(w), h(h)
{}
void get_inputs(wfl::formula_input_vector& inputs) const override
{
add_input(inputs, "x");
add_input(inputs, "y");
add_input(inputs, "u");
add_input(inputs, "v");
add_input(inputs, "red");
add_input(inputs, "green");
add_input(inputs, "blue");
add_input(inputs, "alpha");
add_input(inputs, "height");
add_input(inputs, "width");
}
wfl::variant get_value(const std::string& key) const override
{
using wfl::variant;
if(key == "x") {
return variant(p.x);
} else if(key == "y") {
return variant(p.y);
} else if(key == "red") {
return variant(clr.r);
} else if(key == "green") {
return variant(clr.g);
} else if(key == "blue") {
return variant(clr.b);
} else if(key == "alpha") {
return variant(clr.a);
} else if(key == "width") {
return variant(w);
} else if(key == "height") {
return variant(h);
} else if(key == "u") {
return variant(p.x / static_cast<float>(w));
} else if(key == "v") {
return variant(p.y / static_cast<float>(h));
}
return variant();
}
private:
SDL_Point p;
color_t clr;
uint32_t w, h;
};
surface adjust_alpha_modification::operator()(const surface & src) const
{
if(src == nullptr) {
return nullptr;
}
wfl::formula new_alpha(formula_);
surface nsurf = src.clone();
if(nsurf == nullptr) {
PLAIN_LOG << "could not make neutral surface...";
return nullptr;
}
{
surface_lock lock(nsurf);
uint32_t* cur = lock.pixels();
uint32_t* const end = cur + nsurf->w * src->h;
uint32_t* const beg = cur;
while(cur != end) {
color_t pixel;
pixel.a = (*cur) >> 24;
pixel.r = (*cur) >> 16;
pixel.g = (*cur) >> 8;
pixel.b = (*cur);
int i = cur - beg;
SDL_Point p;
p.y = i / nsurf->w;
p.x = i % nsurf->w;
pixel_callable px(p, pixel, nsurf->w, nsurf->h);
pixel.a = std::min<unsigned>(new_alpha.evaluate(px).as_int(), 255);
*cur = (pixel.a << 24) + (pixel.r << 16) + (pixel.g << 8) + pixel.b;
++cur;
}
}
return nsurf;
}
surface adjust_channels_modification::operator()(const surface & src) const
{
if(src == nullptr) {
return nullptr;
}
wfl::formula new_red(formulas_[0]);
wfl::formula new_green(formulas_[1]);
wfl::formula new_blue(formulas_[2]);
wfl::formula new_alpha(formulas_[3]);
surface nsurf = src.clone();
if(nsurf == nullptr) {
PLAIN_LOG << "could not make neutral surface...";
return nullptr;
}
{
surface_lock lock(nsurf);
uint32_t* cur = lock.pixels();
uint32_t* const end = cur + nsurf->w * src->h;
uint32_t* const beg = cur;
while(cur != end) {
color_t pixel;
pixel.a = (*cur) >> 24;
pixel.r = (*cur) >> 16;
pixel.g = (*cur) >> 8;
pixel.b = (*cur);
int i = cur - beg;
SDL_Point p;
p.y = i / nsurf->w;
p.x = i % nsurf->w;
pixel_callable px(p, pixel, nsurf->w, nsurf->h);
pixel.r = std::min<unsigned>(new_red.evaluate(px).as_int(), 255);
pixel.g = std::min<unsigned>(new_green.evaluate(px).as_int(), 255);
pixel.b = std::min<unsigned>(new_blue.evaluate(px).as_int(), 255);
pixel.a = std::min<unsigned>(new_alpha.evaluate(px).as_int(), 255);
*cur = (pixel.a << 24) + (pixel.r << 16) + (pixel.g << 8) + pixel.b;
++cur;
}
}
return nsurf;
}
surface crop_modification::operator()(const surface& src) const
{
SDL_Rect area = slice_;
if(area.w == 0) {
area.w = src->w;
}
if(area.h == 0) {
area.h = src->h;
}
/*
* Unlike other image functions cut_surface does not convert the input
* surface to a neutral surface, nor does it convert its return surface
* to an optimised surface.
*
* Since it seems to work for most cases, rather change this caller instead
* of the function signature. (The issue was discovered in bug #20876).
*/
return cut_surface(src, area);
}
surface blit_modification::operator()(const surface& src) const
{
if(x_ >= src->w) {
std::stringstream sstr;
sstr << "~BLIT(): x-coordinate '"
<< x_ << "' larger than destination image's width '"
<< src->w << "' no blitting performed.\n";
throw imod_exception(sstr);
}
if(y_ >= src->h) {
std::stringstream sstr;
sstr << "~BLIT(): y-coordinate '"
<< y_ << "' larger than destination image's height '"
<< src->h << "' no blitting performed.\n";
throw imod_exception(sstr);
}
if(surf_->w + x_ < 0) {
std::stringstream sstr;
sstr << "~BLIT(): offset and width '"
<< x_ + surf_->w << "' less than zero no blitting performed.\n";
throw imod_exception(sstr);
}
if(surf_->h + y_ < 0) {
std::stringstream sstr;
sstr << "~BLIT(): offset and height '"
<< y_ + surf_->h << "' less than zero no blitting performed.\n";
throw imod_exception(sstr);
}
surface nsrc = src.clone();
SDL_Rect r {x_, y_, 0, 0};
sdl_blit(surf_, nullptr, nsrc, &r);
return nsrc;
}
surface mask_modification::operator()(const surface& src) const
{
if(src->w == mask_->w && src->h == mask_->h && x_ == 0 && y_ == 0) {
return mask_surface(src, mask_);
}
SDL_Rect r {x_, y_, 0, 0};
surface new_mask(src->w, src->h);
sdl_blit(mask_, nullptr, new_mask, &r);
return mask_surface(src, new_mask);
}
surface light_modification::operator()(const surface& src) const {
if(src == nullptr) { return nullptr; }
// light_surface wants a neutral surface having same dimensions
surface nsurf;
if(surf_->w != src->w || surf_->h != src->h) {
nsurf = scale_surface(surf_, src->w, src->h);
} else {
nsurf = surf_;
}
return light_surface(src, nsurf);
}
surface scale_modification::operator()(const surface& src) const
{
point size = target_size_;
if(size.x <= 0) {
size.x = src->w;
} else if(flags_ & X_BY_FACTOR) {
size.x = src->w * (static_cast<double>(size.x) / 100);
}
if(size.y <= 0) {
size.y = src->h;
} else if(flags_ & Y_BY_FACTOR) {
size.y = src->h * (static_cast<double>(size.y) / 100);
}
if(flags_ & PRESERVE_ASPECT_RATIO) {
const auto ratio = std::min(
static_cast<long double>(size.x) / src->w,
static_cast<long double>(size.y) / src->h
);
size = {
static_cast<int>(src->w * ratio),
static_cast<int>(src->h * ratio)
};
}
if(flags_ & SCALE_SHARP) {
return scale_surface_sharp(src, size.x, size.y);
} else {
return scale_surface_legacy(src, size.x, size.y);
}
}
surface xbrz_modification::operator()(const surface& src) const
{
if(z_ == 1) {
return src;
}
return scale_surface_xbrz(src, z_);
}
/*
* The Opacity IPF doesn't seem to work with surface-wide alpha and instead needs per-pixel alpha.
* If this is needed anywhere else it can be moved back to sdl/utils.*pp.
*/
surface o_modification::operator()(const surface& src) const
{
surface nsurf = src.clone();
if(nsurf == nullptr) {
PLAIN_LOG << "could not make neutral surface...";
return nullptr;
}
uint8_t alpha_mod = float_to_color(opacity_);
{
surface_lock lock(nsurf);
uint32_t* beg = lock.pixels();
uint32_t* end = beg + nsurf->w * src->h;
while(beg != end) {
uint8_t alpha = (*beg) >> 24;
if(alpha) {
uint8_t r, g, b;
r = (*beg) >> 16;
g = (*beg) >> 8;
b = (*beg);
alpha = color_multiply(alpha, alpha_mod);
*beg = (alpha << 24) + (r << 16) + (g << 8) + b;
}
++beg;
}
}
return nsurf;
}
surface cs_modification::operator()(const surface& src) const
{
return((r_ != 0 || g_ != 0 || b_ != 0)
? adjust_surface_color(src, r_, g_, b_)
: src
);
}
surface blend_modification::operator()(const surface& src) const
{
return blend_surface(src, static_cast<double>(a_), color_t(r_, g_, b_));
}
surface bl_modification::operator()(const surface& src) const
{
return blur_alpha_surface(src, depth_);
}
surface background_modification::operator()(const surface &src) const
{
surface ret = src.clone();
SDL_FillRect(ret, nullptr, SDL_MapRGBA(ret->format, color_.r, color_.g,
color_.b, color_.a));
sdl_blit(src, nullptr, ret, nullptr);
return ret;
}
surface swap_modification::operator()(const surface &src) const
{
return swap_channels_image(src, red_, green_, blue_, alpha_);
}
namespace {
struct parse_mod_registration
{
parse_mod_registration(const char* name, mod_parser parser)
{
mod_parsers[name] = parser;
}
};
/** A macro for automatic modification parser registration
*
* It automatically registers the created parser in the mod_parsers map
* It should be used just like a function header (look at the uses below)
* It should only be used within an anonymous namespace
*
* @param type The modification type to be registered (unquoted)
* @param args_var The name for the string argument provided
*/
#define REGISTER_MOD_PARSER(type, args_var) \
static std::unique_ptr<modification> parse_##type##_mod(std::string_view); \
static parse_mod_registration parse_##type##_mod_registration_aux(#type, &parse_##type##_mod); \
static std::unique_ptr<modification> parse_##type##_mod(std::string_view args_var) \
// Color-range-based recoloring
REGISTER_MOD_PARSER(TC, args)
{
const auto params = utils::split_view(args,',');
if(params.size() < 2) {
ERR_DP << "too few arguments passed to the ~TC() function";
return nullptr;
}
const int side_n = utils::from_chars<int>(params[0]).value_or(-1);
if(side_n < 1) {
ERR_DP << "Invalid side (" << params[0] << ") passed to the ~TC() function";
return nullptr;
}
//
// Pass argseters for RC functor
//
if(!game_config::tc_info(params[1]).size()){
ERR_DP << "could not load TC info for '" << params[1] << "' palette";
ERR_DP << "bailing out from TC";
return nullptr;
}
color_range_map rc_map;
try {
const color_range& new_color = team::get_side_color_range(side_n);
const std::vector<color_t>& old_color = game_config::tc_info(params[1]);
rc_map = recolor_range(new_color,old_color);
} catch(const config::error& e) {
ERR_DP << "caught config::error while processing TC: " << e.message;
ERR_DP << "bailing out from TC";
return nullptr;
}
return std::make_unique<rc_modification>(rc_map);
}
// Team-color-based color range selection and recoloring
REGISTER_MOD_PARSER(RC, args)
{
const auto recolor_params = utils::split_view(args,'>');
if(recolor_params.size() <= 1) {
return nullptr;
}
//
// recolor source palette to color range
//
color_range_map rc_map;
try {
const color_range& new_color = game_config::color_info(recolor_params[1]);
const std::vector<color_t>& old_color = game_config::tc_info(recolor_params[0]);
rc_map = recolor_range(new_color,old_color);
} catch (const config::error& e) {
ERR_DP
<< "caught config::error while processing color-range RC: "
<< e.message;
ERR_DP << "bailing out from RC";
rc_map.clear();
}
return std::make_unique<rc_modification>(rc_map);
}
// Palette switch
REGISTER_MOD_PARSER(PAL, args)
{
const auto remap_params = utils::split_view(args,'>');
if(remap_params.size() < 2) {
ERR_DP << "not enough arguments passed to the ~PAL() function: " << args;
return nullptr;
}
try {
color_range_map rc_map;
const std::vector<color_t>& old_palette = game_config::tc_info(remap_params[0]);
const std::vector<color_t>& new_palette =game_config::tc_info(remap_params[1]);
for(std::size_t i = 0; i < old_palette.size() && i < new_palette.size(); ++i) {
rc_map[old_palette[i]] = new_palette[i];
}
return std::make_unique<rc_modification>(rc_map);
} catch(const config::error& e) {
ERR_DP
<< "caught config::error while processing PAL function: "
<< e.message;
ERR_DP
<< "bailing out from PAL";
return nullptr;
}
}
// Flip/flop
REGISTER_MOD_PARSER(FL, args)
{
bool horiz = (args.empty() || args.find("horiz") != std::string::npos);
bool vert = (args.find("vert") != std::string::npos);
return std::make_unique<fl_modification>(horiz, vert);
}
// Rotations
REGISTER_MOD_PARSER(ROTATE, args)
{
const auto slice_params = utils::split_view(args, ',', utils::STRIP_SPACES);
switch(slice_params.size()) {
case 0:
return std::make_unique<rotate_modification>();
case 1:
return std::make_unique<rotate_modification>(
utils::from_chars<int>(slice_params[0]).value_or(0));
case 2:
return std::make_unique<rotate_modification>(
utils::from_chars<int>(slice_params[0]).value_or(0),
utils::from_chars<int>(slice_params[1]).value_or(0));
case 3:
return std::make_unique<rotate_modification>(
utils::from_chars<int>(slice_params[0]).value_or(0),
utils::from_chars<int>(slice_params[1]).value_or(0),
utils::from_chars<int>(slice_params[2]).value_or(0));
}
return nullptr;
}
// Grayscale
REGISTER_MOD_PARSER(GS, )
{
return std::make_unique<gs_modification>();
}
// crop transparent padding
REGISTER_MOD_PARSER(CROP_TRANSPARENCY, )
{
return std::make_unique<crop_transparency_modification>();
}
// TODO: should this be made a more general util function?
bool in_range(int val, int min, int max)
{
return min <= val && val <= max;
}
// Black and white
REGISTER_MOD_PARSER(BW, args)
{
const auto params = utils::split_view(args, ',');
if(params.size() != 1) {
ERR_DP << "~BW() requires exactly one argument";
return nullptr;
}
// TODO: maybe get this directly as uint8_t?
const auto threshold = utils::from_chars<int>(params[0]);
if(!threshold) {
ERR_DP << "unsupported argument in ~BW() function";
return nullptr;
}
if(!in_range(*threshold, 0, 255)) {
ERR_DP << "~BW() argument out of range 0 - 255";
return nullptr;
}
return std::make_unique<bw_modification>(*threshold);
}
// Sepia
REGISTER_MOD_PARSER(SEPIA, )
{
return std::make_unique<sepia_modification>();
}
// Negative
REGISTER_MOD_PARSER(NEG, args)
{
const auto params = utils::split_view(args, ',');
switch(params.size()) {
case 0:
// apparently -1 may be a magic number but this is the threshold
// value required to fully invert a channel
return std::make_unique<negative_modification>(-1, -1, -1);
case 1: {
const auto threshold = utils::from_chars<int>(params[0]);
if(threshold && in_range(*threshold, -1, 255)) {
return std::make_unique<negative_modification>(*threshold, *threshold, *threshold);
} else {
ERR_DP << "unsupported argument value in ~NEG() function";
return nullptr;
}
}
case 3: {
const auto thR = utils::from_chars<int>(params[0]);
const auto thG = utils::from_chars<int>(params[1]);
const auto thB = utils::from_chars<int>(params[2]);
if(thR && thG && thB && in_range(*thR, -1, 255) && in_range(*thG, -1, 255) && in_range(*thB, -1, 255)) {
return std::make_unique<negative_modification>(*thR, *thG, *thB);
} else {
ERR_DP << "unsupported argument value in ~NEG() function";
return nullptr;
}
}
default:
ERR_DP << "~NEG() requires 0, 1 or 3 arguments";
return nullptr;
}
}
// Plot Alpha
REGISTER_MOD_PARSER(PLOT_ALPHA, )
{
return std::make_unique<plot_alpha_modification>();
}
// Wipe Alpha
REGISTER_MOD_PARSER(WIPE_ALPHA, )
{
return std::make_unique<wipe_alpha_modification>();
}
// Adjust Alpha
REGISTER_MOD_PARSER(ADJUST_ALPHA, args)
{
// Formulas may contain commas, so use parenthetical split to ensure that they're properly considered a single argument.
// (A comma in a formula is only valid in function parameters or list/map literals, so this should always work.)
const std::vector<std::string>& params = utils::parenthetical_split(args, ',', "([", ")]");
if(params.size() != 1) {
ERR_DP << "~ADJUST_ALPHA() requires exactly 1 arguments";
return nullptr;
}
return std::make_unique<adjust_alpha_modification>(params.at(0));
}
// Adjust Channels
REGISTER_MOD_PARSER(CHAN, args)
{
// Formulas may contain commas, so use parenthetical split to ensure that they're properly considered a single argument.
// (A comma in a formula is only valid in function parameters or list/map literals, so this should always work.)
const std::vector<std::string>& params = utils::parenthetical_split(args, ',', "([", ")]");
if(params.size() < 1 || params.size() > 4) {
ERR_DP << "~CHAN() requires 1 to 4 arguments";
return nullptr;
}
return std::make_unique<adjust_channels_modification>(params);
}
// Color-shift
REGISTER_MOD_PARSER(CS, args)
{
const auto factors = utils::split_view(args, ',');
const std::size_t s = factors.size();
if(s == 0) {
ERR_DP << "no arguments passed to the ~CS() function";
return nullptr;
}
int r = 0, g = 0, b = 0;
r = utils::from_chars<int>(factors[0]).value_or(0);
if(s > 1 ) {
g = utils::from_chars<int>(factors[0]).value_or(0);
}
if(s > 2 ) {
b = utils::from_chars<int>(factors[0]).value_or(0);
}
return std::make_unique<cs_modification>(r, g , b);
}
// Color blending
REGISTER_MOD_PARSER(BLEND, args)
{
const auto params = utils::split_view(args, ',');
if(params.size() != 4) {
ERR_DP << "~BLEND() requires exactly 4 arguments";
return nullptr;
}
float opacity = 0.0f;
const std::string_view& opacity_str = params[3];
const std::string_view::size_type p100_pos = opacity_str.find('%');
if(p100_pos == std::string::npos)
opacity = lexical_cast_default<float>(opacity_str);
else {
// make multiplier
const std::string_view parsed_field = opacity_str.substr(0, p100_pos);
opacity = lexical_cast_default<float>(parsed_field);
opacity /= 100.0f;
}
return std::make_unique<blend_modification>(
utils::from_chars<int>(params[0]).value_or(0),
utils::from_chars<int>(params[1]).value_or(0),
utils::from_chars<int>(params[2]).value_or(0),
opacity);
}
// Crop/slice
REGISTER_MOD_PARSER(CROP, args)
{
const auto slice_params = utils::split_view(args, ',', utils::STRIP_SPACES);
const std::size_t s = slice_params.size();
if(s == 0 || (s == 1 && slice_params[0].empty())) {
ERR_DP << "no arguments passed to the ~CROP() function";
return nullptr;
}
SDL_Rect slice_rect { 0, 0, 0, 0 };
slice_rect.x = utils::from_chars<int16_t>(slice_params[0]).value_or(0);
if(s > 1) {
slice_rect.y = utils::from_chars<int16_t>(slice_params[1]).value_or(0);
}
if(s > 2) {
slice_rect.w = utils::from_chars<uint16_t>(slice_params[2]).value_or(0);
}
if(s > 3) {
slice_rect.h = utils::from_chars<uint16_t>(slice_params[3]).value_or(0);
}
return std::make_unique<crop_modification>(slice_rect);
}
static bool check_image(const image::locator& img, std::stringstream & message)
{
if(img.file_exists()) return true;
message << " image not found: '" << img.get_filename() << "'\n";
ERR_DP << message.str();
return false;
}
// Blit
REGISTER_MOD_PARSER(BLIT, args)
{
std::vector<std::string> param = utils::parenthetical_split(args, ',');
const std::size_t s = param.size();
if(s == 0 || (s == 1 && param[0].empty())){
ERR_DP << "no arguments passed to the ~BLIT() function";
return nullptr;
}
if(s > 3){
ERR_DP << "too many arguments passed to the ~BLIT() function";
return nullptr;
}
int x = 0, y = 0;
if(s == 3) {
x = utils::from_chars<int>(param[1]).value_or(0);
y = utils::from_chars<int>(param[2]).value_or(0);
}
const image::locator img(param[0]);
std::stringstream message;
message << "~BLIT():";
if(!check_image(img, message))
return nullptr;
surface surf = get_surface(img);
return std::make_unique<blit_modification>(surf, x, y);
}