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 <spdlog/fmt/fmt.h>
8/*
9can support signed data types
10preferrably float or double
11*/
12
13namespace ImageLib {
14
15#ifdef _MSC_VER
16#pragma pack(push, 1)
17#endif
18template <typename NumberT> struct LABPixel : public Pixel<NumberT> {
19 // ----- Members -----
20 NumberT l, a, b;
21
22 constexpr LABPixel(NumberT l = 0, NumberT a = 0, NumberT b = 0)
23 : l(l)
24 , a(a)
25 , b(b) {
26 }
27
28 // ----- Modifiers -----
29 [[nodiscard]] inline bool operator==(const LABPixel& other) const {
30 return l == other.l && a == other.a && b == other.b;
31 }
32 [[nodiscard]] inline bool operator!=(const LABPixel& other) const {
33 return !(*this == other);
34 }
35
36 // ----- Utilities -----
37 inline void setGray(NumberT new_luma) {
38 l = new_luma;
39 a = b = 0;
40 }
41
42 static inline float colorDistance(const LABPixel<NumberT>& a, const LABPixel<NumberT>& b) {
44 static_cast<float>(a.l), static_cast<float>(a.a), static_cast<float>(a.b)
45 };
47 static_cast<float>(b.l), static_cast<float>(b.a), static_cast<float>(b.b)
48 };
49 return std::sqrt(
50 (a.l - b.l) * (a.l - b.l) + (a.a - b.a) * (a.a - b.a) + (a.b - b.b) * (a.b - b.b)
51 );
52 }
53
54}
55#ifndef _MSC_VER
56__attribute__((packed))
57#endif
58;
59#ifdef _MSC_VER
60#pragma pack(pop)
61#endif
62
63} // namespace ImageLib
64// Lets spdlog/fmt print LABPixel, e.g. IMG2NUM_LOG_DEBUG("{}", pixel);
65namespace fmt {
66template <typename NumberT> struct formatter<ImageLib::LABPixel<NumberT>> {
67 constexpr auto parse(format_parse_context& ctx) {
68 return ctx.begin();
69 }
70
71 template <typename FormatContext>
72 auto format(const ImageLib::LABPixel<NumberT>& pixel, FormatContext& ctx) const {
73 const NumberT l = pixel.l;
74 const NumberT a = pixel.a;
75 const NumberT b = pixel.b;
76 return format_to(ctx.out(), "( {},{},{} )", l, a, b);
77 }
78};
79} // namespace fmt
80
81#endif // LABPIXEL_H