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 "internal/Pixel.h"
5
6#include <cmath>
7#include <iostream>
8
9/*
10can support signed data types
11preferrably float or double
12*/
13
14namespace ImageLib {
15
16#ifdef _MSC_VER
17#pragma pack(push, 1)
18#endif
19template <typename NumberT> struct LABPixel : public Pixel<NumberT> {
20 // ----- Members -----
21 NumberT l, a, b;
22
23 constexpr LABPixel(NumberT l = 0, NumberT a = 0, NumberT b = 0)
24 : l(l)
25 , a(a)
26 , b(b) {
27 }
28
29 // ----- Modifiers -----
30 [[nodiscard]] inline bool operator==(const LABPixel& other) const {
31 return l == other.l && a == other.a && b == other.b;
32 }
33 [[nodiscard]] inline bool operator!=(const LABPixel& other) const {
34 return !(*this == other);
35 }
36
37 // ----- Utilities -----
38 inline void setGray(NumberT new_luma) {
39 l = new_luma;
40 a = b = 0;
41 }
42
43 static inline float colorDistance(const LABPixel<NumberT>& a, const LABPixel<NumberT>& b) {
45 static_cast<float>(a.l), static_cast<float>(a.a), static_cast<float>(a.b)};
47 static_cast<float>(b.l), static_cast<float>(b.a), static_cast<float>(b.b)};
48 return std::sqrt(
49 (a.l - b.l) * (a.l - b.l) + (a.a - b.a) * (a.a - b.a) + (a.b - b.b) * (a.b - b.b)
50 );
51 }
52
53}
54#ifndef _MSC_VER
55__attribute__((packed))
56#endif
57;
58#ifdef _MSC_VER
59#pragma pack(pop)
60#endif
61
62template <typename NumberT>
63std::ostream& operator<<(std::ostream& out, const ImageLib::LABPixel<NumberT>& pixel) {
64 out << "( " << pixel.l << "," << pixel.a << "," << pixel.b << " )";
65 return out;
66}
67
68} // namespace ImageLib
69
70#endif // LABPIXEL_H