xref: /aosp_15_r20/external/skia/include/core/SkColorTable.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2023 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkColorTable_DEFINED
9 #define SkColorTable_DEFINED
10 
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/private/base/SkAPI.h"
14 
15 #include <cstdint>
16 
17 class SkReadBuffer;
18 class SkWriteBuffer;
19 
20 /**
21  * SkColorTable holds the lookup tables for each channel (ARGB) used to define the filter behavior
22  * of `SkColorFilters::Table`, and provides a way to share the table data between client code and
23  * the returned SkColorFilter. Once created, an SkColorTable is immutable.
24 */
25 class SK_API SkColorTable : public SkRefCnt {
26 public:
27     // Creates a new SkColorTable with 'table' used for all four channels. The table is copied into
28     // the SkColorTable.
Make(const uint8_t table[256])29     static sk_sp<SkColorTable> Make(const uint8_t table[256]) {
30         return Make(table, table, table, table);
31     }
32 
33     // Creates a new SkColorTable with the per-channel lookup tables. Each non-null table is copied
34     // into the SkColorTable. Null parameters are interpreted as the identity table.
35     static sk_sp<SkColorTable> Make(const uint8_t tableA[256],
36                                     const uint8_t tableR[256],
37                                     const uint8_t tableG[256],
38                                     const uint8_t tableB[256]);
39 
40     // Per-channel constant value lookup (0-255).
alphaTable()41     const uint8_t* alphaTable() const { return fTable.getAddr8(0, 0); }
redTable()42     const uint8_t* redTable()   const { return fTable.getAddr8(0, 1); }
greenTable()43     const uint8_t* greenTable() const { return fTable.getAddr8(0, 2); }
blueTable()44     const uint8_t* blueTable()  const { return fTable.getAddr8(0, 3); }
45 
46     void flatten(SkWriteBuffer& buffer) const;
47 
48     static sk_sp<SkColorTable> Deserialize(SkReadBuffer& buffer);
49 
50 private:
51     friend class SkTableColorFilter; // for bitmap()
52 
SkColorTable(const SkBitmap & table)53     SkColorTable(const SkBitmap& table) : fTable(table) {}
54 
55     // The returned SkBitmap is immutable; attempting to modify its pixel data will trigger asserts
56     // in debug builds and cause undefined behavior in release builds.
bitmap()57     const SkBitmap& bitmap() const { return fTable; }
58 
59     SkBitmap fTable; // A 256x4 A8 image
60 };
61 
62 #endif // SkColorTable_DEFINED
63