Skia
2D Graphics Library
SkColorTable.h
Go to the documentation of this file.
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 
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.
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).
41  const uint8_t* alphaTable() const { return fTable.getAddr8(0, 0); }
42  const uint8_t* redTable() const { return fTable.getAddr8(0, 1); }
43  const uint8_t* greenTable() const { return fTable.getAddr8(0, 2); }
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 
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.
57  const SkBitmap& bitmap() const { return fTable; }
58 
59  SkBitmap fTable; // A 256x4 A8 image
60 };
61 
62 #endif // SkColorTable_DEFINED
SkBitmap describes a two-dimensional raster pixel array.
Definition: SkBitmap.h:59
SkColorTable holds the lookup tables for each channel (ARGB) used to define the filter behavior of Sk...
Definition: SkColorTable.h:25
static sk_sp< SkColorTable > Deserialize(SkReadBuffer &buffer)
void flatten(SkWriteBuffer &buffer) const
const uint8_t * blueTable() const
Definition: SkColorTable.h:44
static sk_sp< SkColorTable > Make(const uint8_t table[256])
Definition: SkColorTable.h:29
static sk_sp< SkColorTable > Make(const uint8_t tableA[256], const uint8_t tableR[256], const uint8_t tableG[256], const uint8_t tableB[256])
const uint8_t * greenTable() const
Definition: SkColorTable.h:43
const uint8_t * redTable() const
Definition: SkColorTable.h:42
const uint8_t * alphaTable() const
Definition: SkColorTable.h:41
Definition: SkRefCnt.h:119
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:220
sk_sp< SkDrawLooper > SK_API Make(SkColor4f color, SkColorSpace *cs, SkScalar sigma, SkScalar dx, SkScalar dy)