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
17/** \class SkColorTable
18
19    SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by
20    8-bit bitmaps, where the bitmap bytes are interpreted as indices into the colortable.
21*/
22class SK_API SkColorTable : public SkRefCnt {
23public:
24    SK_DECLARE_INST_COUNT(SkColorTable)
25
26    /** Makes a deep copy of colors.
27     */
28    SkColorTable(const SkColorTable& src);
29    SkColorTable(const SkPMColor colors[], int count,
30                 SkAlphaType alphaType = kPremul_SkAlphaType);
31    virtual ~SkColorTable();
32
33    SkAlphaType alphaType() const { return (SkAlphaType)fAlphaType; }
34
35    bool isOpaque() const {
36        return SkAlphaTypeIsOpaque(this->alphaType());
37    }
38
39    /** Returns the number of colors in the table.
40    */
41    int count() const { return fCount; }
42
43    /** Returns the specified color from the table. In the debug build, this asserts that
44        the index is in range (0 <= index < count).
45    */
46    SkPMColor operator[](int index) const {
47        SkASSERT(fColors != NULL && (unsigned)index < fCount);
48        return fColors[index];
49    }
50
51    /**
52     *  Return the array of colors for reading. This must be balanced by a call
53     *  to unlockColors().
54     */
55    const SkPMColor* lockColors() {
56        SkDEBUGCODE(sk_atomic_inc(&fColorLockCount);)
57        return fColors;
58    }
59
60    /**
61     *  Balancing call to lockColors().
62     */
63    void unlockColors();
64
65    /** Similar to lockColors(), lock16BitCache() returns the array of
66        RGB16 colors that mirror the 32bit colors. However, this function
67        will return null if kColorsAreOpaque_Flag is not set.
68        Also, unlike lockColors(), the returned array here cannot be modified.
69    */
70    const uint16_t* lock16BitCache();
71    /** Balancing call to lock16BitCache().
72    */
73    void unlock16BitCache() {
74        SkASSERT(f16BitCacheLockCount > 0);
75        SkDEBUGCODE(f16BitCacheLockCount -= 1);
76    }
77
78    explicit SkColorTable(SkReadBuffer&);
79    void writeToBuffer(SkWriteBuffer&) const;
80
81private:
82    SkPMColor*  fColors;
83    uint16_t*   f16BitCache;
84    uint16_t    fCount;
85    uint8_t     fAlphaType;
86    SkDEBUGCODE(int fColorLockCount;)
87    SkDEBUGCODE(int f16BitCacheLockCount;)
88
89    void inval16BitCache();
90
91    typedef SkRefCnt INHERITED;
92};
93
94#endif
95