Line data Source code
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 :
18 : /** \class SkColorTable
19 :
20 : SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by
21 : 8-bit bitmaps, where the bitmap bytes are interpreted as indices into the colortable.
22 :
23 : SkColorTable is thread-safe.
24 : */
25 : class SK_API SkColorTable : public SkRefCnt {
26 : public:
27 : static sk_sp<SkColorTable> Make(const SkPMColor colors[], int count);
28 :
29 : /** Copy up to 256 colors into a new SkColorTable.
30 : */
31 : SkColorTable(const SkPMColor colors[], int count);
32 : ~SkColorTable() override;
33 :
34 : /** Returns the number of colors in the table.
35 : */
36 0 : int count() const { return fCount; }
37 :
38 : /** Returns the specified color from the table. In the debug build, this asserts that
39 : * the index is in range (0 <= index < count).
40 : */
41 0 : SkPMColor operator[](int index) const {
42 0 : SkASSERT(fColors != NULL && (unsigned)index < (unsigned)fCount);
43 0 : return fColors[index];
44 : }
45 :
46 : /** Return the array of colors for reading.
47 : */
48 0 : const SkPMColor* readColors() const { return fColors; }
49 :
50 : /** read16BitCache() returns the array of RGB16 colors that mirror the 32bit colors.
51 : */
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
|