Krita Source Code Documentation
Loading...
Searching...
No Matches
KoIntegerMaths.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2005 Adrian Page <adrian@pagenet.plus.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#ifndef KO_INTEGER_MATHS_H
8#define KO_INTEGER_MATHS_H
9
10#include <cstdint>
11
12#ifndef UINT8_MAX
13#define UINT8_MAX 255u
14#endif
15
16#ifndef UINT8_MIN
17#define UINT8_MIN 0u
18#endif
19
20#ifndef UINT16_MAX
21#define UINT16_MAX 65535u
22#endif
23
24#ifndef UINT16_MIN
25#define UINT16_MIN 0u
26#endif
27
28#ifndef UINT32_MAX
29#define UINT32_MAX (4294967295u)
30#endif
31
32#ifndef UINT32_MIN
33#define UINT32_MIN 0u
34#endif
35
36#ifndef INT16_MAX
37#define INT16_MAX 32767
38#endif
39
40#ifndef INT16_MIN
41#define INT16_MIN -32768
42#endif
43
44typedef unsigned int uint;
45
46template<typename _T_, typename _T2_, typename _T3_>
47inline _T_ CLAMP(_T_ x, _T2_ l, _T3_ u)
48{
49 if (x < l)
50 return _T_(l);
51 else if (x > u)
52 return _T_(u);
53 return x;
54}
55
58{
59 uint c = a * b + 0x80u;
60 return (c >> 8) + c;
61}
62
67{
68 uint c = a * b + 0x80u;
69 return ((c >> 8) + c) >> 8;
70}
71
73{
74 uint c = (a * UINT8_MAX + (b / 2u)) / b;
75 return c;
76}
77
79inline uint UINT8_MULT3(uint a, uint b, uint c)
80{
81 uint t = a * b * c + 0x7F5B;
82 return ((t >> 7) + t) >> 16;
83}
84
88inline uint UINT8_BLEND(uint a, uint b, uint alpha)
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}
96
98{
99 uint c = a * b + 0x8000u;
100 return ((c >> 16) + c) >> 16;
101}
102
103inline int INT16_MULT(int a, int b)
104{
105 return (a*b) / INT16_MAX;
106}
107
109{
110 uint c = (a * UINT16_MAX + (b / 2u)) / b;
111 return c;
112}
113
114inline uint UINT16_BLEND(uint a, uint b, uint alpha)
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}
122
124{
125 return c | (c << 8);
126}
127
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}
135
136inline int INT16_BLEND(int a, int b, uint alpha)
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}
143
144#endif
145
qreal u
uint UINT8_SCALEBY(uint a, uint b)
take a and scale it up by 256*b/255
uint UINT8_MULT(uint a, uint b)
#define UINT16_MAX
int INT16_BLEND(int a, int b, uint alpha)
uint UINT8_BLEND(uint a, uint b, uint alpha)
uint UINT16_MULT(uint a, uint b)
uint UINT16_BLEND(uint a, uint b, uint alpha)
unsigned int uint
uint UINT8_TO_UINT16(uint c)
uint UINT16_DIVIDE(uint a, uint b)
int INT16_MULT(int a, int b)
#define INT16_MAX
uint UINT16_TO_UINT8(uint c)
uint UINT8_DIVIDE(uint a, uint b)
uint UINT8_MULT3(uint a, uint b, uint c)
Approximation of (a * b * c + 32512) / 65025.0.
#define UINT8_MAX
#define CLAMP(x, l, h)