aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/core/SkColorTable.h
blob: 40919d3bace53e62fa5dbfbad10bf474b2f80d62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */


#ifndef SkColorTable_DEFINED
#define SkColorTable_DEFINED

#include "../private/SkOnce.h"
#include "SkColor.h"
#include "SkFlattenable.h"
#include "SkImageInfo.h"

/** \class SkColorTable

    SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by
    8-bit bitmaps, where the bitmap bytes are interpreted as indices into the colortable.

    SkColorTable is thread-safe.
*/
class SK_API SkColorTable : public SkRefCnt {
public:
    static sk_sp<SkColorTable> Make(const SkPMColor colors[], int count);

    /** Copy up to 256 colors into a new SkColorTable.
     */
    SkColorTable(const SkPMColor colors[], int count);
    ~SkColorTable() override;

    /** Returns the number of colors in the table.
     */
    int count() const { return fCount; }

    /** Returns the specified color from the table. In the debug build, this asserts that
     *  the index is in range (0 <= index < count).
     */
    SkPMColor operator[](int index) const {
        SkASSERT(fColors != NULL && (unsigned)index < (unsigned)fCount);
        return fColors[index];
    }

    /** Return the array of colors for reading.
     */
    const SkPMColor* readColors() const { return fColors; }

    /** read16BitCache() returns the array of RGB16 colors that mirror the 32bit colors.
     */
    const uint16_t* read16BitCache() const;

    void writeToBuffer(SkWriteBuffer&) const;

    // may return null
    static sk_sp<SkColorTable> Create(SkReadBuffer&);

private:
    enum AllocatedWithMalloc {
        kAllocatedWithMalloc
    };
    // assumes ownership of colors (assumes it was allocated w/ malloc)
    SkColorTable(SkPMColor* colors, int count, AllocatedWithMalloc);

    SkPMColor*            fColors;
    mutable uint16_t*     f16BitCache = nullptr;
    mutable SkOnce        f16BitCacheOnce;
    int                   fCount;

    void init(const SkPMColor* colors, int count);

    friend class SkImageGenerator;
    friend class SkBitmapRegionCodec;
    // Only call if no other thread or cache has seen this table.
    void dangerous_overwriteColors(const SkPMColor newColors[], int count) {
        if (count < 0 || count > fCount) {
            sk_throw();
        }
        // assumes that f16BitCache nas NOT been initialized yet, so we don't try to update it
        memcpy(fColors, newColors, count * sizeof(SkPMColor));
        fCount = count; // update fCount, in case count is smaller
    }

    typedef SkRefCnt INHERITED;
};

#endif