-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiscUtils.h
81 lines (63 loc) · 2.4 KB
/
MiscUtils.h
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
#ifndef MISCUTILS_H
#define MISCUTILS_H
#include <QColor>
// takes a slice of total size numElem in contiguous memory
// and creates a mask with an overlay, according to the given color
// destImg can be equal to baseImg
static inline void overlayRGB( const unsigned int *baseImg, const unsigned char *overlayImg, unsigned int *destImg, unsigned int numElem, const QColor &color, float alpha = 0.5 )
{
qreal sR, sG, sB;
color.getRgbF( &sR, &sG,&sB );
sR *= 255;
sG *= 255;
sB *= 255;
const qreal div255 = 1.0 / 255.0;
for (unsigned int i=0; i < numElem; i++)
{
qreal r = (baseImg[i] >> 16) & 0xFF;
qreal g = (baseImg[i] >> 8) & 0xFF;
qreal b = (baseImg[i] >> 0) & 0xFF;
qreal sc = overlayImg[i] * div255;
qreal scInv = 1.0 - sc;
g = g*(1-alpha) + (scInv*g + sc*sG)*alpha;
b = b*(1-alpha) + (scInv*b + sc*sB)*alpha;
r = r*(1-alpha) + (scInv*r + sc*sR)*alpha;
unsigned int iR = r;
unsigned int iG = g;
unsigned int iB = b;
//pixPtr[i] = (pixPtr[i] & 0xFF00FFFF) | (((unsigned int) scorePtr[i]) << 16);
destImg[i] = 0xFF000000 | (iR<<16) | (iG<<8) | iB;
}
}
static inline void overlayRGBThresholded( const unsigned int *baseImg, const unsigned char *overlayImg, unsigned int *destImg, unsigned int numElem, const QColor &color,
unsigned char minThr, unsigned char maxThr, bool hardThreshold)
{
qreal sR, sG, sB;
color.getRgbF( &sR, &sG,&sB );
sR *= 255;
sG *= 255;
sB *= 255;
const qreal div255 = 1.0 / 255.0;
for (unsigned int i=0; i < numElem; i++)
{
qreal r = (baseImg[i] >> 16) & 0xFF;
qreal g = (baseImg[i] >> 8) & 0xFF;
qreal b = (baseImg[i] >> 0) & 0xFF;
unsigned char overlayVal = overlayImg[i];
if (overlayVal < minThr || overlayVal > maxThr)
overlayVal = 0;
if (hardThreshold && (overlayVal > 0))
overlayVal = 255;
qreal sc = overlayVal * div255;
qreal scInv = 1.0 - sc;
g = scInv*g + sc*sG;
b = scInv*b + sc*sB;
r = r * scInv + sc*sR;
unsigned int iR = r;
unsigned int iG = g;
unsigned int iB = b;
//pixPtr[i] = (pixPtr[i] & 0xFF00FFFF) | (((unsigned int) scorePtr[i]) << 16);
destImg[i] = 0xFF000000 | (iR<<16) | (iG<<8) | iB;
}
}
#endif // MISCUTILS_H