xref: /aosp_15_r20/external/skia/include/core/SkUnPreMultiply.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 
2 /*
3  * Copyright 2008 The Android Open Source Project
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 #ifndef SkUnPreMultiply_DEFINED
9 #define SkUnPreMultiply_DEFINED
10 
11 #include "include/core/SkColor.h"
12 #include "include/core/SkTypes.h"
13 #include "include/private/base/SkCPUTypes.h"
14 
15 #include <cstdint>
16 
17 class SK_API SkUnPreMultiply {
18 public:
19     typedef uint32_t Scale;
20 
21     // index this table with alpha [0..255]
GetScaleTable()22     static const Scale* GetScaleTable() {
23         return gTable;
24     }
25 
GetScale(U8CPU alpha)26     static Scale GetScale(U8CPU alpha) {
27         SkASSERT(alpha <= 255);
28         return gTable[alpha];
29     }
30 
31     /** Usage:
32 
33         const Scale* table = SkUnPreMultiply::GetScaleTable();
34 
35         for (...) {
36             unsigned a = ...
37             SkUnPreMultiply::Scale scale = table[a];
38 
39             red = SkUnPreMultiply::ApplyScale(scale, red);
40             ...
41             // now red is unpremultiplied
42         }
43     */
ApplyScale(Scale scale,U8CPU component)44     static U8CPU ApplyScale(Scale scale, U8CPU component) {
45         SkASSERT(component <= 255);
46         return (scale * component + (1 << 23)) >> 24;
47     }
48 
49     static SkColor PMColorToColor(SkPMColor c);
50 
51 private:
52     static const uint32_t gTable[256];
53 };
54 
55 #endif
56