-
Notifications
You must be signed in to change notification settings - Fork 0
/
hslimage.cpp
85 lines (67 loc) · 2.17 KB
/
hslimage.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
#include "hslimage.h"
#include <cstring>
using namespace byteimage;
HSLImage::HSLImage() : nr(0), nc(0), hsl(nullptr) { }
HSLImage::HSLImage(int nr, int nc) : nr(nr), nc(nc) {hsl = new float [3 * nr * nc];}
HSLImage::HSLImage(const ByteImage& img) : HSLImage(img.nr, img.nc) {
const Byte *R = img.R(), *G = img.G(), *B = img.B();
for (int i = 0, j = 0; i < nr * nc; i++, j += 3)
rgb2hsl(R[i], G[i], B[i], hsl[j], hsl[j + 1], hsl[j + 2]);
}
HSLImage::HSLImage(const HSLImage& img) : nr(0), nc(0), hsl(nullptr) {*this = img;}
HSLImage::HSLImage(HSLImage&& img) : nr(0), nc(0), hsl(nullptr) {*this = img;}
HSLImage::~HSLImage() {delete [] hsl;}
HSLImage& HSLImage::operator=(const HSLImage& img) {
if (nr * nc != img.nr * img.nc) {
delete [] hsl;
hsl = new float [img.nr * img.nc];
}
nr = img.nr;
nc = img.nc;
memcpy(hsl, img.hsl, nr * nc * sizeof(float));
return *this;
}
HSLImage& HSLImage::operator=(HSLImage&& img) {
delete [] hsl;
nr = img.nr; img.nr = 0;
nc = img.nc; img.nc = 0;
hsl = img.hsl; img.hsl = nullptr;
return *this;
}
ByteImage HSLImage::toHue() {
ByteImage img(nr, nc, 3);
Byte *R = img.R(), *G = img.G(), *B = img.B();
for (int i = 0, j = 0; i < nr * nc; i++, j += 3)
hsl2rgb(hsl[j], 1.0, 0.5, R[i], G[i], B[i]);
return img;
}
ByteImage HSLImage::toSaturation() const {
ByteImage img(nr, nc);
for (int i = 0, j = 1; i < nr * nc; i++, j += 3)
img[i] = (Byte)(255.0 * hsl[j]);
return img;
}
ByteImage HSLImage::toLightness() const {
ByteImage img(nr, nc);
for (int i = 0, j = 2; i < nr * nc; i++, j += 3)
img[i] = (Byte)(255.0 * hsl[j]);
return img;
}
ByteImage HSLImage::toByteImage() const {
ByteImage img(nr, nc, 3);
Byte *R = img.R(), *G = img.G(), *B = img.B();
for (int i = 0, j = 0; i < nr * nc; i++, j += 3)
hsl2rgb(hsl[j], hsl[j + 1], hsl[j + 2], R[i], G[i], B[i]);
return img;
}
void HSLImage::panSharpen(const ByteImage& gray) {*this = panSharpened(gray);}
HSLImage HSLImage::panSharpened(const ByteImage& gray) const {
//TODO
return *this;
}
float byteimage::hueDistance(float h0, float h1) {
h1 -= h0;
if (h1 < 0.0) h1 += 360.0;
if (h1 > 180.0) h1 = 360.0 - h1;
return h1;
}