-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimagehelpers.cpp
More file actions
67 lines (65 loc) · 2.61 KB
/
imagehelpers.cpp
File metadata and controls
67 lines (65 loc) · 2.61 KB
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
#include "imagehelpers.h"
#include <FL/Fl_RGB_Image.H>
#include <openjpeg-2.5/openjpeg.h>
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <vector>
Fl_Image *convert(const image_data &image) {
if (image.components == 1) {
if (image.bpp > 8) {
const unsigned int size = image.width * image.height;
const int *pdata = image.component0.data();
// TODO: do the normalization and cast to char in 1 step?
std::vector<uint16_t> intermediate(size);
std::transform(pdata, pdata + (size), intermediate.data(),
[](int value) { return static_cast<uint16_t>(value); });
histeq(intermediate);
normalizeToBitsUsed(intermediate, 8);
std::vector<uint8_t> converted(size);
std::transform(intermediate.cbegin(), intermediate.cend(),
converted.begin(), [](unsigned char value) {
return static_cast<unsigned char>(value);
});
Fl_Image *copy =
Fl_RGB_Image(converted.data(), image.width, image.height, 1, 0)
.copy();
return copy;
}
if (image.bpp == 8) {
std::vector<uint8_t> converted(image.component0.size());
const int *pdata = image.component0.data();
std::transform(pdata, pdata + image.component0.size(), converted.begin(),
[](int value) { return static_cast<uint8_t>(value); });
return Fl_RGB_Image(converted.data(), image.width, image.height, 1, 0)
.copy(image.width, image.height);
}
if (image.bpp == 1) {
const unsigned int size = image.width * image.height;
std::vector<uint8_t> converted(size);
std::transform(image.component0.cbegin(), image.component0.cend(),
converted.begin(),
[](int value) { return value != 0 ? 0x80 : 0x0; });
Fl_Image *copy =
Fl_RGB_Image(converted.data(), image.width, image.height, 1, 0)
.copy();
return copy;
}
} else if (image.components == 3) {
const int *pred = image.component0.data();
const int *pgreen = image.component1.data();
const int *pblue = image.component2.data();
const unsigned int size = image.width * image.height;
std::vector<uint8_t> converted(size * 3);
unsigned char *pdata = converted.data();
for (unsigned int i = 0; i < size; i++) {
*pdata++ = *pred++;
*pdata++ = *pgreen++;
*pdata++ = *pblue++;
}
Fl_Image *copy =
Fl_RGB_Image(converted.data(), image.width, image.height, 3).copy();
return copy;
}
return nullptr;
}