Skia
2DGraphicsLibrary
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SkColorTable.h
1 
2 /*
3  * Copyright 2012 Google Inc.
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 
9 
10 #ifndef SkColorTable_DEFINED
11 #define SkColorTable_DEFINED
12 
13 #include "../private/SkOnce.h"
14 #include "SkColor.h"
15 #include "SkFlattenable.h"
16 #include "SkImageInfo.h"
17 
25 class SK_API SkColorTable : public SkRefCnt {
26 public:
27  static sk_sp<SkColorTable> Make(const SkPMColor colors[], int count);
28 
31  SkColorTable(const SkPMColor colors[], int count);
32  ~SkColorTable() override;
33 
36  int count() const { return fCount; }
37 
41  SkPMColor operator[](int index) const {
42  SkASSERT(fColors != NULL && (unsigned)index < (unsigned)fCount);
43  return fColors[index];
44  }
45 
48  const SkPMColor* readColors() const { return fColors; }
49 
52  const uint16_t* read16BitCache() const;
53 
54  void writeToBuffer(SkWriteBuffer&) const;
55 
56  // may return null
57  static sk_sp<SkColorTable> Create(SkReadBuffer&);
58 
59 private:
60  enum AllocatedWithMalloc {
61  kAllocatedWithMalloc
62  };
63  // assumes ownership of colors (assumes it was allocated w/ malloc)
64  SkColorTable(SkPMColor* colors, int count, AllocatedWithMalloc);
65 
66  SkPMColor* fColors;
67  mutable uint16_t* f16BitCache = nullptr;
68  mutable SkOnce f16BitCacheOnce;
69  int fCount;
70 
71  void init(const SkPMColor* colors, int count);
72 
73  friend class SkImageGenerator;
74  friend class SkBitmapRegionCodec;
75  // Only call if no other thread or cache has seen this table.
76  void dangerous_overwriteColors(const SkPMColor newColors[], int count) {
77  if (count < 0 || count > fCount) {
78  sk_throw();
79  }
80  // assumes that f16BitCache nas NOT been initialized yet, so we don't try to update it
81  memcpy(fColors, newColors, count * sizeof(SkPMColor));
82  fCount = count; // update fCount, in case count is smaller
83  }
84 
85  typedef SkRefCnt INHERITED;
86 };
87 
88 #endif
SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by 8-bit bitmaps...
Definition: SkColorTable.h:25
Definition: SkImageGenerator.h:27
Definition: SkRefCnt.h:125
int count() const
Returns the number of colors in the table.
Definition: SkColorTable.h:36
const SkPMColor * readColors() const
Return the array of colors for reading.
Definition: SkColorTable.h:48
uint32_t SkPMColor
32 bit ARGB color value, premultiplied.
Definition: SkColor.h:161
Definition: SkWriteBuffer.h:27
SkPMColor operator[](int index) const
Returns the specified color from the table.
Definition: SkColorTable.h:41
Types and macros for colors.