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 "SkColor.h"
14 #include "SkFlattenable.h"
15 #include "SkImageInfo.h"
16 #include "SkLazyPtr.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     SK_DECLARE_INST_COUNT(SkColorTable)
28 
29     /** Copy up to 256 colors into a new SkColorTable.
30      */
31     SkColorTable(const SkPMColor colors[], int count);
32     virtual ~SkColorTable();
33 
34     /** Returns the number of colors in the table.
35      */
count()36     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     SkPMColor operator[](int index) const {
42         SkASSERT(fColors != NULL && (unsigned)index < (unsigned)fCount);
43         return fColors[index];
44     }
45 
46     /** Return the array of colors for reading.
47      */
readColors()48     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     explicit SkColorTable(SkReadBuffer&);
55     void writeToBuffer(SkWriteBuffer&) const;
56 
57 private:
58     static void Free16BitCache(uint16_t*);
59 
60     SkPMColor*                          fColors;
61     SkLazyPtr<uint16_t, Free16BitCache> f16BitCache;
62     int                                 fCount;
63 
64     void init(const SkPMColor* colors, int count);
65 
66     typedef SkRefCnt INHERITED;
67 };
68 
69 #endif
70