-
Notifications
You must be signed in to change notification settings - Fork 2
/
libpx.cpp
3201 lines (2710 loc) · 72.8 KB
/
libpx.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
#include "libpx.hpp"
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <vector>
#include <cerrno>
#include <cstring>
namespace px {
namespace {
//======================//
// Section: Scalar Math //
//======================//
/// Calculates the absolute value of a number.
///
/// @tparam T The type of the number to get the absolute value for.
///
/// @param in The value to get the absolute value of.
///
/// @return The absolute value of @p in.
template <typename T>
inline constexpr T absolute(T in) noexcept
{
return in < 0 ? -in : in;
}
/// Gets the minimum between two values.
///
/// @tparam T The type of the values being compared.
///
/// @return The minimum between @p a and @p b.
template <typename T>
inline constexpr T min(T a, T b) noexcept
{
return (a < b) ? a : b;
}
/// Gets the maximum between two values.
///
/// @tparam T The type of the values being compared.
///
/// @return The maximum between @p a and @p b.
template <typename T>
inline constexpr T max(T a, T b) noexcept
{
return (a > b) ? a : b;
}
/// Clips a scalar value to be in an inclusive interval
/// of a certain min and maximum.
template <typename T>
inline constexpr T clip(T in, T minValue = 0, T maxValue = 1) noexcept
{
return max(minValue, min(in, maxValue));
}
//======================//
// Section: Vector Math //
//======================//
/// A generic vector class. This
/// is used for spatial information
/// such as position and direction as
/// well as RGBA values.
///
/// @tparam T The type for each component.
/// This may be an integer for spatial data
/// and a floating point type for color data.
///
/// @tparam dims The number of dimensions in
/// the vector. Generally this is 2 for spatial
/// data and 4 for color data.
template <typename T, std::size_t dims>
struct Vector final
{
/// One component per dimension.
/// The first component is X and
/// the second is Y. This may also
/// be used for color values, in which
/// case the first component is red, the
/// second is green, the third is blue, and
/// the last is alpha.
T data[dims] {};
/// Accesses a component in the vector.
constexpr inline T& operator [] (std::size_t index) noexcept {
return data[index];
}
/// Accesses a component in the vector.
constexpr inline const T& operator [] (std::size_t index) const noexcept {
return data[index];
}
/// Adds two vectors.
inline constexpr Vector operator + (const Vector& other) const noexcept
{
Vector out;
for (std::size_t i = 0; i < dims; i++) {
out.data[i] = data[i] + other.data[i];
}
return out;
}
/// Subtracts two vectors.
inline constexpr Vector operator - (const Vector& other) const noexcept
{
Vector out;
for (std::size_t i = 0; i < dims; i++) {
out.data[i] = data[i] - other.data[i];
}
return out;
}
/// Adds a scalar value to the vector.
inline constexpr Vector operator + (T n) const noexcept
{
Vector out;
for (std::size_t i = 0; i < dims; i++) {
out.data[i] = data[i] + n;
}
return out;
}
/// Subtracts a scalar value from the vector.
inline constexpr Vector operator - (T n) const noexcept
{
Vector out;
for (std::size_t i = 0; i < dims; i++) {
out.data[i] = data[i] - n;
}
return out;
}
/// Divides the vector by a scalar value.
inline constexpr Vector operator / (T n) const noexcept
{
Vector out;
for (std::size_t i = 0; i < dims; i++) {
out.data[i] = data[i] / n;
}
return out;
}
/// Multiplies the vector by a scalar value.
inline constexpr Vector operator * (T n) const noexcept
{
Vector out;
for (std::size_t i = 0; i < dims; i++) {
out.data[i] = data[i] * n;
}
return out;
}
};
/// Calculates a vector with all absolute value components.
///
/// @param v The vector to get the absolute values of.
///
/// @return The resultant vector.
template <typename T, std::size_t dims>
inline constexpr Vector<T, dims> absolute(const Vector<T, dims>& v) noexcept
{
Vector<T, dims> out;
for (std::size_t i = 0; i < dims; i++) {
out.data[i] = absolute(v[i]);
}
return out;
}
/// Calculates a vector whose components
/// are the minimum values between two other vectors.
template <typename T, std::size_t dims>
inline constexpr Vector<T, dims> min(const Vector<T, dims>& a, const Vector<T, dims>& b) noexcept
{
Vector<T, dims> out;
for (std::size_t i = 0; i < dims; i++) {
out.data[i] = min(a.data[i], b.data[i]);
}
return out;
}
/// Calculates a vector whose components
/// are the maximum values between two other vectors.
template <typename T, std::size_t dims>
inline constexpr Vector<T, dims> max(const Vector<T, dims>& a, const Vector<T, dims>& b) noexcept
{
Vector<T, dims> out;
for (std::size_t i = 0; i < dims; i++) {
out.data[i] = max(a.data[i], b.data[i]);
}
return out;
}
/// A 2D vector type definition.
/// Since this is usually used for
/// spatial information, it is an
/// integer vector.
using Vec2 = Vector<int, 2>;
/// Indicates if two integer vectors are equal.
inline bool operator == (const Vec2& a, const Vec2& b) noexcept
{
return (a[0] == b[0]) && (a[1] == b[1]);
}
} // namespace
//================//
// Section: Color //
//================//
namespace {
/// A color type definition.
using RGBA = Vector<float, 4>;
/// This is the change in value of a
/// color channel that is seen from the
/// final result of an 8-bit per channel image.
/// This value is primarily used to see if
/// two colors are mostly equal.
constexpr float colorDelta() noexcept { return 1.0f / 256.0f; }
/// Indicates if two colors are almost equal.
///
/// @param a The first color operand.
/// @param b The second color operand.
/// @param bias The maximum allowable difference.
/// This defaults to (1 / 256), which is the
/// user perceivable difference.
///
/// @return True if they're almost equal, false otherwise.
constexpr bool almostEqual(const RGBA& a, const RGBA& b, float bias = colorDelta()) noexcept
{
auto diff = absolute(a - b);
return (diff[0] < bias)
&& (diff[1] < bias)
&& (diff[2] < bias)
&& (diff[3] < bias);
}
/// Clips a color to be between a minimum and maximum value.
///
/// @param in The color to clip.
/// @param minValue The minimum value to clip to.
/// @param maxValue The maximum value to clip to.
constexpr RGBA clip(const RGBA& in, float minValue = 0, float maxValue = 1) noexcept
{
return RGBA {
min(max(minValue, in[0]), maxValue),
min(max(minValue, in[1]), maxValue),
min(max(minValue, in[2]), maxValue),
min(max(minValue, in[3]), maxValue)
};
}
/// Premultiplies the alpha channel.
///
/// @param c The color to premultiply the alpha channel of.
///
/// @return The resultant color.
constexpr RGBA premultiply(const RGBA& c) noexcept
{
return RGBA { c[0] * c[3],
c[1] * c[3],
c[2] * c[3],
c[3] };
}
/// Represents a color.
struct Color final
{
/// The original color, given from an API call.
RGBA original;
/// The premultiplied color.
RGBA premultiplied;
/// Constructs a new color instance.
///
/// @param rgba The original color values.
constexpr Color(const RGBA& rgba) noexcept
: original(rgba), premultiplied(premultiply(rgba)) { }
};
inline constexpr RGBA normalBlend(const RGBA& bg, const Color& fg) noexcept
{
return fg.premultiplied + (bg * (1.0f - fg.premultiplied[3]));
}
inline constexpr RGBA subtractionBlend(const RGBA& bg, const Color& fg) noexcept
{
return clip(bg - fg.original);
}
inline constexpr RGBA blend(BlendMode mode, const RGBA& bg, const Color& fg) noexcept
{
switch (mode) {
case BlendMode::Normal:
return normalBlend(bg, fg);
case BlendMode::Subtract:
return subtractionBlend(bg, fg);
}
return bg;
}
inline constexpr RGBA blend(BlendMode mode, const float* bg, const Color& fg) noexcept
{
RGBA tmp { bg[0], bg[1], bg[2], bg[3] };
return blend(mode, tmp, fg);
}
} // namespace
//===================//
// Section: Geometry //
//===================//
namespace {
//constexpr RGBA white() noexcept { return RGBA { 1, 1, 1, 1 }; }
constexpr RGBA black() noexcept { return RGBA { 0, 0, 0, 1 }; }
constexpr RGBA transparent() noexcept { return RGBA { 0, 0, 0, 0 }; }
/// Used for visiting nodes in the scene graph.
class NodeAccessor
{
public:
/// Just a stub.
virtual ~NodeAccessor() {}
virtual void access(const Ellipse& ellipse) noexcept = 0;
virtual void access(const Fill& fill) noexcept = 0;
virtual void access(const Line& line) noexcept = 0;
virtual void access(const Quad& quad) noexcept = 0;
};
/// This is the base of any
/// class that appears in the scene graph.
struct Node
{
/// Just a stub.
virtual ~Node() {}
/// Allows a node accessor class access
/// to the derived node type.
virtual void accept(NodeAccessor& accessor) const noexcept = 0;
/// Copies the derived node.
virtual Node* copy() const = 0;
};
/// A type definition for a node smart pointer.
using NodePtr = std::unique_ptr<Node>;
/// This is the base of any class that has
/// a stroke. It contains the basic properties
/// of how the stroke should be drawn.
struct StrokeNode : public Node
{
/// The size of the squares that
/// are drawn along the stroke.
std::size_t pixelSize = 1;
/// The blend mode of the node.
BlendMode blendMode = BlendMode::Normal;
/// The color that the stroke is drawn with.
RGBA color = black();
};
/// Evaluates a number to a safe pixel size.
/// A pixel size must be greater than or equal to 1.
inline constexpr int safePixelSize(int in) noexcept
{
return (in <= 0) ? 1 : in;
}
} // namespace
struct Ellipse final : public StrokeNode
{
Vec2 center = Vec2 { 0, 0 };
Vec2 radius = Vec2 { 0, 0 };
void accept(NodeAccessor& accessor) const noexcept override
{
accessor.access(*this);
}
Node* copy() const override
{
return new Ellipse(*this);
}
};
void setBlendMode(Ellipse* ellipse, BlendMode blendMode) noexcept
{
ellipse->blendMode = blendMode;
}
void setCenter(Ellipse* ellipse, int x, int y) noexcept
{
ellipse->center = Vec2 { x, y };
}
void setRadius(Ellipse* ellipse, int x, int y) noexcept
{
ellipse->radius = Vec2 { x, y };
}
void setColor(Ellipse* ellipse, float r, float g, float b, float a) noexcept
{
ellipse->color = clip(RGBA { r, g, b, a });
}
void setPixelSize(Ellipse* ellipse, int pixelSize) noexcept
{
ellipse->pixelSize = safePixelSize(pixelSize);
}
void resizeRect(Ellipse* ellipse, int x1, int y1, int x2, int y2) noexcept
{
auto p1 = Vec2 { x1, y1 };
auto p2 = Vec2 { x2, y2 };
auto pMin = min(p1, p2);
auto pMax = max(p1, p2);
ellipse->center = (pMax + pMin) / 2;
ellipse->radius = (pMax - pMin) / 2;
}
/// Represents a flood fill operation.
struct Fill final : public Node
{
/// The blend mode of the fill operation.
BlendMode blendMode = BlendMode::Normal;
/// The color to fill the area with.
RGBA color = black();
/// The position on the image to start the fill operation at.
/// All pixels connected to this point are filled.
Vec2 origin = Vec2 { 0, 0 };
void accept(NodeAccessor& accessor) const noexcept override
{
accessor.access(*this);
}
Node* copy() const override
{
return new Fill(*this);
}
};
void setBlendMode(Fill* fill, BlendMode blendMode) noexcept
{
fill->blendMode = blendMode;
}
void setFillOrigin(Fill* fill, int x, int y) noexcept
{
fill->origin = Vec2 { x, y };
}
void setColor(Fill* fill, float r, float g, float b, float a) noexcept
{
fill->color = clip(RGBA { r, g, b, a });
}
/// Represents a series of straight line segments.
struct Line final : public StrokeNode
{
/// The points making up the line.
std::vector<Vec2> points;
void accept(NodeAccessor& accessor) const noexcept override
{
accessor.access(*this);
}
Node* copy() const override
{
return new Line(*this);
}
};
void addPoint(Line* line, int x, int y)
{
line->points.emplace_back(Vec2 { x, y });
}
void setBlendMode(Line* line, BlendMode blendMode) noexcept
{
line->blendMode = blendMode;
}
namespace {
/// Removes duplicate neighboring points from a line.
///
/// @param line The line to remove the duplicate neighboring points of.
void removeDuplicatePoints(Line* line) noexcept
{
std::size_t i = 1;
while (i < line->points.size()) {
auto a = line->points[i - 1];
auto b = line->points[i - 0];
if (a == b) {
line->points.erase(line->points.begin() + (i - 1));
} else {
i++;
}
}
}
/// Removes points that have duplicate neighboring slopes.
void removeDuplicateSlopes(Line* line) noexcept
{
std::size_t i = 1;
while (i < (line->points.size() - 1)) {
auto a = line->points[i - 1];
auto b = line->points[i - 0];
auto c = line->points[i + 1];
auto diffA = b - a;
auto diffB = c - b;
// Two vertical slopes must be handled specifically
// because they would otherwise cause a divide by zero exception.
if (!diffA[0] && !diffB[0]) {
line->points.erase(line->points.begin() + i);
continue;
}
// Same as before, we want to avoid divide by zero errors
// by checking either X-delta. Since we know they're not equal,
// if one of them is zero then the two slopes are different.
if (!diffA[0] || !diffB[0]) {
i++;
continue;
}
auto slopeA = diffA[1] / diffA[0];
auto slopeB = diffB[1] / diffB[0];
auto remA = diffA[1] % diffA[0];
auto remB = diffB[1] % diffB[0];
if ((slopeA == slopeB) && (remA == remB)) {
line->points.erase(line->points.begin() + i);
} else {
i++;
}
}
}
} // namespace
void dissolvePoints(Line* line) noexcept
{
removeDuplicatePoints(line);
removeDuplicateSlopes(line);
}
std::size_t getPointCount(const Line* line) noexcept
{
return line->points.size();
}
void getPoint(const Line* line, std::size_t index, int* point)
{
auto& p = line->points.at(index);
point[0] = p[0];
point[1] = p[1];
}
int getPointX(const Line* line, std::size_t index)
{
return line->points.at(index)[0];
}
int getPointY(const Line* line, std::size_t index)
{
return line->points.at(index)[1];
}
bool setPoint(Line* line, std::size_t index, int x, int y) noexcept
{
if (index >= line->points.size()) {
return false;
} else {
line->points[index] = Vec2 { x, y };
return true;
}
}
void setPixelSize(Line* line, int pixelSize) noexcept
{
line->pixelSize = safePixelSize(pixelSize);
}
void setColor(Line* line, float r, float g, float b, float a) noexcept
{
line->color = clip(RGBA { r, g, b, a });
}
/// Represents a quadrilateral shape.
/// A quadrilateral shape differs from
/// a rectangle in that the lines do not
/// have to be axis-aligned.
struct Quad final : public StrokeNode
{
/// The points making up the quadrilateral.
Vec2 points[4] { Vec2 { 0, 0 }, Vec2 { 1, 0 }, Vec2 { 1, 1 }, Vec2 { 0, 1 } };
void accept(NodeAccessor& accessor) const noexcept override
{
accessor.access(*this);
}
Node* copy() const override
{
return new Quad(*this);
}
};
bool setPoint(Quad* quad, std::size_t index, int x, int y) noexcept
{
if (index >= 4) {
return false;
} else {
quad->points[index] = Vec2 { x, y };
return true;
}
}
void setBlendMode(Quad* quad, BlendMode blendMode) noexcept
{
quad->blendMode = blendMode;
}
void setColor(Quad* quad, float r, float g, float b, float a) noexcept
{
quad->color = clip(RGBA { r, g, b, a });
}
void setPixelSize(Quad* quad, int pixelSize) noexcept
{
quad->pixelSize = safePixelSize(pixelSize);
}
//=================//
// Section: Layers //
//=================//
/// A layer here is what it is in most image
/// editing applications, a collection of 2D data
/// that is meant for a certain Z index and opacity,
/// to be drawn in a certain order relative to the other layers.
struct Layer final
{
/// The alpha channel value of this layer.
float opacity = 1;
/// The name given to this layer.
std::string name;
/// Whether or not the layer is visible.
bool visible = true;
/// The nodes for this layer
std::vector<NodePtr> nodes;
/// Just a stub.
Layer() {}
/// Copies a layer.
Layer(const Layer& other)
{
opacity = other.opacity;
name = other.name;
visible = other.visible;
for (const auto& otherNode : other.nodes) {
nodes.emplace_back(otherNode->copy());
}
}
/// Adds a node to the layer.
///
/// @tparam NodeType The type of the node to add.
///
/// @param node The node to add to the layer.
///
/// @return A pointer to to @p node.
template <typename NodeType>
NodeType* addNode(NodeType* node)
{
// In case an exception gets thrown.
std::unique_ptr<NodeType> nodePtr(node);
nodes.emplace_back(node);
return nodePtr.release();
}
};
/// A type definition for a layer smart pointer.
using LayerPtr = std::unique_ptr<Layer>;
const char* getLayerName(const Layer* layer) noexcept
{
return layer->name.c_str();
}
float getLayerOpacity(const Layer* layer) noexcept
{
return layer->opacity;
}
bool getLayerVisibility(const Layer* layer) noexcept
{
return layer->visible;
}
void setLayerName(Layer* layer, const char* name)
{
layer->name = name ? name : "";
}
void setLayerOpacity(Layer* layer, float opacity) noexcept
{
layer->opacity = clip(opacity);
}
void setLayerVisibility(Layer* layer, bool visibility) noexcept
{
layer->visible = visibility;
}
//================//
// Section: Image //
//================//
/// Contains basic image data.
struct Image final
{
/// The image colors, formatted
/// in the order of RGBA.
std::vector<float> colorBuffer;
/// The width of the image, in pixels.
std::size_t width = 0;
/// The height of the image, in pixels.
std::size_t height = 0;
};
Image* createImage(std::size_t width, std::size_t height)
{
auto image = std::make_unique<Image>(Image());
resizeImage(image.get(), width, height);
return image.release();
}
void closeImage(Image* image) noexcept
{
delete image;
}
const float* getColorBuffer(const Image* image) noexcept
{
return image->colorBuffer.data();
}
bool getColor(const Image* image, std::size_t x, std::size_t y, float* rgba) noexcept
{
if ((x >= image->width)
|| (y >= image->height)) {
return false;
}
const auto* src = &image->colorBuffer[((y * image->width) + x) * 4];
// TODO : Restore RGB before multiply
rgba[0] = src[0];
rgba[1] = src[1];
rgba[2] = src[2];
rgba[3] = src[3];
return true;
}
std::size_t getImageWidth(const Image* image) noexcept { return image->width; }
std::size_t getImageHeight(const Image* image) noexcept { return image->height; }
void resizeImage(Image* image, std::size_t w, std::size_t h)
{
image->colorBuffer.resize(w * h * 4);
image->width = w;
image->height = h;
}
//=====================//
// Section: Error List //
//=====================//
/// Contains information on a single error.
struct Error final
{
/// The stream used to format the error.
/// This isn't seen by the end user but it can
/// be used to make error reporting easier.
std::ostringstream stream;
/// A human-readable description of the error.
std::string description;
/// The line that the error can be found at.
std::size_t line = 0;
/// The column that the error can be found at.
std::size_t column = 0;
/// The index within the file that the error begins at.
std::size_t index = 0;
/// The number of characters that the error pertains to.
std::size_t size = 0;
};
/// Prints an error to a stream.
///
/// @param stream The stream to print to.
/// @param err The error to print.
///
/// @return A reference to @p stream.
std::ostream& operator << (std::ostream& stream, const Error& err)
{
return stream << err.line << ':' << err.column << ": " << err.description.c_str();
}
struct ErrorList final
{
/// The path to the file that was opened.
std::string filename;
/// The source code that the errors pertain to.
/// This is all the source code in the original file.
std::string source;
/// The list of errors that were found.
std::vector<Error> errors;
};
void closeErrorList(ErrorList* errList) noexcept
{
delete errList;
}
void printErrorListToStderr(const ErrorList* errList) noexcept
{
if (!errList) {
return;
}
for (std::size_t i = 0; i < errList->errors.size(); i++) {
printErrorToStderr(errList, i);
}
}
void printErrorToStderr(const ErrorList* errList, std::size_t index) noexcept
{
if (!errList) {
return;
}
if (index >= errList->errors.size()) {
return;
}
std::cerr << errList->filename << ':' << errList->errors[index] << std::endl;
}
const char* getErrorSource(const ErrorList* errList) noexcept
{
if (!errList) {
return "";
}
return errList->source.c_str();
}
std::size_t getErrorSourceSize(const ErrorList* errList) noexcept
{
if (!errList) {
return 0;
}
return errList->source.size();
}
std::size_t getErrorCount(const ErrorList* errList) noexcept
{
if (!errList) {
return 0;
}
return errList->errors.size();
}
std::size_t getErrorColumn(const ErrorList* errList, std::size_t error) noexcept
{
if (!errList) {
return 0;
}
if (error >= errList->errors.size()) {
return 0;
}
return errList->errors[error].column;
}
std::size_t getErrorLine(const ErrorList* errList, std::size_t error) noexcept
{
if (!errList) {
return 0;
}
if (error >= errList->errors.size()) {
return 0;
}
return errList->errors[error].line;
}
std::size_t getErrorPosition(const ErrorList* errList, std::size_t error) noexcept
{
if (!errList) {
return 0;
}
if (error >= errList->errors.size()) {
return 0;
}
return errList->errors[error].index;
}
std::size_t getErrorSize(const ErrorList* errList, std::size_t error) noexcept
{
if (!errList) {
return 0;
}
if (error >= errList->errors.size()) {
return 0;
}
return errList->errors[error].size;
}
const char* getErrorDescription(const ErrorList* errList, std::size_t error) noexcept
{
if (!errList) {
return "";
}
if (error >= errList->errors.size()) {
return 0;
}
return errList->errors[error].description.c_str();
}
//========================//
// Section: Serialization //
//========================//
namespace {
/// This value indicates the resolution
/// at which floating point colors are
/// converting to integer values for encoding
/// and decoding.
constexpr std::size_t colorRes() noexcept
{
return 32768;
}
/// Prints an nth dimensional vector.
///
/// @tparam T The type used in the vector components.
/// @tparam dims The number of dimensions in the vector.
///