Krita Source Code Documentation
Loading...
Searching...
No Matches
KoIntegerMaths.h File Reference
#include <cstdint>

Go to the source code of this file.

Macros

#define INT16_MAX   32767
 
#define INT16_MIN   -32768
 
#define UINT16_MAX   65535u
 
#define UINT16_MIN   0u
 
#define UINT32_MAX   (4294967295u)
 
#define UINT32_MIN   0u
 
#define UINT8_MAX   255u
 
#define UINT8_MIN   0u
 

Typedefs

typedef unsigned int uint
 

Functions

template<typename _T_ , typename _T2_ , typename _T3_ >
_T_ CLAMP (_T_ x, _T2_ l, _T3_ u)
 
int INT16_BLEND (int a, int b, uint alpha)
 
int INT16_MULT (int a, int b)
 
uint UINT16_BLEND (uint a, uint b, uint alpha)
 
uint UINT16_DIVIDE (uint a, uint b)
 
uint UINT16_MULT (uint a, uint b)
 
uint UINT16_TO_UINT8 (uint c)
 
uint UINT8_BLEND (uint a, uint b, uint alpha)
 
uint UINT8_DIVIDE (uint a, uint b)
 
uint UINT8_MULT (uint a, uint b)
 
uint UINT8_MULT3 (uint a, uint b, uint c)
 Approximation of (a * b * c + 32512) / 65025.0.
 
uint UINT8_SCALEBY (uint a, uint b)
 take a and scale it up by 256*b/255
 
uint UINT8_TO_UINT16 (uint c)
 

Macro Definition Documentation

◆ INT16_MAX

#define INT16_MAX   32767

Definition at line 37 of file KoIntegerMaths.h.

◆ INT16_MIN

#define INT16_MIN   -32768

Definition at line 41 of file KoIntegerMaths.h.

◆ UINT16_MAX

#define UINT16_MAX   65535u

Definition at line 21 of file KoIntegerMaths.h.

◆ UINT16_MIN

#define UINT16_MIN   0u

Definition at line 25 of file KoIntegerMaths.h.

◆ UINT32_MAX

#define UINT32_MAX   (4294967295u)

Definition at line 29 of file KoIntegerMaths.h.

◆ UINT32_MIN

#define UINT32_MIN   0u

Definition at line 33 of file KoIntegerMaths.h.

◆ UINT8_MAX

#define UINT8_MAX   255u

Definition at line 13 of file KoIntegerMaths.h.

◆ UINT8_MIN

#define UINT8_MIN   0u

Definition at line 17 of file KoIntegerMaths.h.

Typedef Documentation

◆ uint

typedef unsigned int uint

Definition at line 44 of file KoIntegerMaths.h.

Function Documentation

◆ CLAMP()

template<typename _T_ , typename _T2_ , typename _T3_ >
_T_ CLAMP ( _T_ x,
_T2_ l,
_T3_ u )
inline

Definition at line 47 of file KoIntegerMaths.h.

48{
49 if (x < l)
50 return _T_(l);
51 else if (x > u)
52 return _T_(u);
53 return x;
54}
qreal u

References u.

◆ INT16_BLEND()

int INT16_BLEND ( int a,
int b,
uint alpha )
inline

Definition at line 136 of file KoIntegerMaths.h.

137{
138 // Basically we do a*alpha + b*(1-alpha)
139 // However refactored to (a-b)*alpha + b since that saves a multiplication
140 int c = ((int(a) - int(b)) * int(alpha)) >> 16;
141 return c + b;
142}

◆ INT16_MULT()

int INT16_MULT ( int a,
int b )
inline

Definition at line 103 of file KoIntegerMaths.h.

104{
105 return (a*b) / INT16_MAX;
106}
#define INT16_MAX

References INT16_MAX.

◆ UINT16_BLEND()

uint UINT16_BLEND ( uint a,
uint b,
uint alpha )
inline

Definition at line 114 of file KoIntegerMaths.h.

115{
116 // Basically we do a*alpha + b*(1-alpha)
117 // However refactored to (a-b)*alpha + b since that saves a multiplication
118 // Signed arithmetic is needed since a-b might be negative
119 int c = ((int(a) - int(b)) * int(alpha)) >> 16;
120 return uint(c + b);
121}
unsigned int uint

◆ UINT16_DIVIDE()

uint UINT16_DIVIDE ( uint a,
uint b )
inline

Definition at line 108 of file KoIntegerMaths.h.

109{
110 uint c = (a * UINT16_MAX + (b / 2u)) / b;
111 return c;
112}
#define UINT16_MAX

References UINT16_MAX.

◆ UINT16_MULT()

uint UINT16_MULT ( uint a,
uint b )
inline

Definition at line 97 of file KoIntegerMaths.h.

98{
99 uint c = a * b + 0x8000u;
100 return ((c >> 16) + c) >> 16;
101}

◆ UINT16_TO_UINT8()

uint UINT16_TO_UINT8 ( uint c)
inline

Definition at line 128 of file KoIntegerMaths.h.

129{
130 //return round(c / 257.0);
131 //For all UINT16 this calculation is the same and a lot faster (off by c/65656 which for every c is 0)
132 c = c - (c >> 8) + 128;
133 return c >> 8;
134}

◆ UINT8_BLEND()

uint UINT8_BLEND ( uint a,
uint b,
uint alpha )
inline

Blending of two scale values as described by the alpha scale value A scale value is interpreted as 255 equaling 1.0 (such as seen in rgb8 triplets) Basically we do: a*alpha + b*(1-alpha)

Definition at line 88 of file KoIntegerMaths.h.

89{
90 // However the formula is refactored to (a-b)*alpha + b since that saves a multiplication
91 // Signed arithmetic is needed since a-b might be negative
92 int c = (int(a) - int(b)) * alpha + 0x80u;
93 c = ((c >> 8) + c) >> 8;
94 return c + b;
95}

◆ UINT8_DIVIDE()

uint UINT8_DIVIDE ( uint a,
uint b )
inline

Definition at line 72 of file KoIntegerMaths.h.

73{
74 uint c = (a * UINT8_MAX + (b / 2u)) / b;
75 return c;
76}
#define UINT8_MAX

References UINT8_MAX.

◆ UINT8_MULT()

uint UINT8_MULT ( uint a,
uint b )
inline

multiplication of two scale values A scale value is interpreted as 255 equaling 1.0 (such as seen in rgb8 triplets) thus "255*255=255" because 1.0*1.0=1.0

Definition at line 66 of file KoIntegerMaths.h.

67{
68 uint c = a * b + 0x80u;
69 return ((c >> 8) + c) >> 8;
70}

◆ UINT8_MULT3()

uint UINT8_MULT3 ( uint a,
uint b,
uint c )
inline

Approximation of (a * b * c + 32512) / 65025.0.

Definition at line 79 of file KoIntegerMaths.h.

80{
81 uint t = a * b * c + 0x7F5B;
82 return ((t >> 7) + t) >> 16;
83}

◆ UINT8_SCALEBY()

uint UINT8_SCALEBY ( uint a,
uint b )
inline

take a and scale it up by 256*b/255

Definition at line 57 of file KoIntegerMaths.h.

58{
59 uint c = a * b + 0x80u;
60 return (c >> 8) + c;
61}

◆ UINT8_TO_UINT16()

uint UINT8_TO_UINT16 ( uint c)
inline

Definition at line 123 of file KoIntegerMaths.h.

124{
125 return c | (c << 8);
126}