Img2Num C++ (Internal Developer Docs) dev
API Documentation
Loading...
Searching...
No Matches
LABPixel.h
1#ifndef LABPIXEL_H
2#define LABPIXEL_H
3
4#include <cmath>
5#include <iostream>
6
7#include "internal/Pixel.h"
8
9/*
10can support signed data types
11preferrably float or double
12*/
13
14namespace ImageLib {
15template <typename NumberT>
16struct LABPixel : public Pixel<NumberT> {
17 // ----- Members -----
18 NumberT l, a, b;
19
20 constexpr LABPixel(NumberT l = 0, NumberT a = 0, NumberT b = 0) : l(l), a(a), b(b) {
21 }
22
23 // ----- Modifiers -----
24 [[nodiscard]] inline bool operator==(const LABPixel &other) const {
25 return l == other.l && a == other.a && b == other.b;
26 }
27 [[nodiscard]] inline bool operator!=(const LABPixel &other) const {
28 return !(*this == other);
29 }
30
31 // ----- Utilities -----
32 inline void setGray(NumberT new_luma) {
33 l = new_luma;
34 a = b = 0;
35 }
36
37 static inline float colorDistance(const LABPixel<NumberT> &a, const LABPixel<NumberT> &b) {
38 LABPixel<float> af{static_cast<float>(a.l), static_cast<float>(a.a),
39 static_cast<float>(a.b)};
40 LABPixel<float> bf{static_cast<float>(b.l), static_cast<float>(b.a),
41 static_cast<float>(b.b)};
42 return std::sqrt((a.l - b.l) * (a.l - b.l) + (a.a - b.a) * (a.a - b.a) +
43 (a.b - b.b) * (a.b - b.b));
44 }
45
46} __attribute__((packed));
47
48template <typename NumberT>
49std::ostream &operator<<(std::ostream &out, const ImageLib::LABPixel<NumberT> &pixel) {
50 out << "( " << pixel.l << "," << pixel.a << "," << pixel.b << " )";
51 return out;
52}
53
54} // namespace ImageLib
55
56#endif // LABPIXEL_H