Line data Source code
1 : /*
2 : * Copyright 2009 The Android Open Source Project
3 : *
4 : * Use of this source code is governed by a BSD-style license that can be
5 : * found in the LICENSE file.
6 : */
7 :
8 :
9 : #include "SkColorTable.h"
10 : #include "SkReadBuffer.h"
11 : #include "SkWriteBuffer.h"
12 : #include "SkStream.h"
13 : #include "SkTemplates.h"
14 :
15 0 : void SkColorTable::init(const SkPMColor colors[], int count) {
16 0 : SkASSERT((unsigned)count <= 256);
17 :
18 0 : fCount = count;
19 0 : fColors = reinterpret_cast<SkPMColor*>(sk_malloc_throw(count * sizeof(SkPMColor)));
20 :
21 0 : memcpy(fColors, colors, count * sizeof(SkPMColor));
22 0 : }
23 :
24 0 : SkColorTable::SkColorTable(const SkPMColor colors[], int count) {
25 0 : SkASSERT(0 == count || colors);
26 0 : SkASSERT(count >= 0 && count <= 256);
27 0 : this->init(colors, count);
28 0 : }
29 :
30 0 : SkColorTable::SkColorTable(SkPMColor* colors, int count, AllocatedWithMalloc)
31 : : fColors(colors)
32 0 : , fCount(count)
33 : {
34 0 : SkASSERT(count > 0 && count <= 256);
35 0 : SkASSERT(colors);
36 0 : }
37 :
38 0 : SkColorTable::~SkColorTable() {
39 0 : sk_free(fColors);
40 0 : sk_free(f16BitCache);
41 0 : }
42 :
43 : #include "SkColorPriv.h"
44 :
45 0 : const uint16_t* SkColorTable::read16BitCache() const {
46 0 : f16BitCacheOnce([this] {
47 0 : f16BitCache = (uint16_t*)sk_malloc_throw(fCount * sizeof(uint16_t));
48 0 : for (int i = 0; i < fCount; i++) {
49 0 : f16BitCache[i] = SkPixel32ToPixel16_ToU16(fColors[i]);
50 : }
51 0 : });
52 0 : return f16BitCache;
53 : }
54 :
55 0 : sk_sp<SkColorTable> SkColorTable::Make(const SkPMColor colors[], int count) {
56 0 : if (count < 0 || count > 256) {
57 0 : return nullptr;
58 : }
59 0 : if (count && !colors) {
60 0 : return nullptr;
61 : }
62 0 : return sk_make_sp<SkColorTable>(colors, count);
63 : }
64 :
65 : ///////////////////////////////////////////////////////////////////////////////
66 :
67 : #if 0
68 : SkColorTable::SkColorTable(SkReadBuffer& buffer) {
69 : if (buffer.isVersionLT(SkReadBuffer::kRemoveColorTableAlpha_Version)) {
70 : /*fAlphaType = */buffer.readUInt();
71 : }
72 :
73 : fCount = buffer.getArrayCount();
74 : size_t allocSize = fCount * sizeof(SkPMColor);
75 : SkDEBUGCODE(bool success = false;)
76 : if (buffer.validateAvailable(allocSize)) {
77 : fColors = (SkPMColor*)sk_malloc_throw(allocSize);
78 : SkDEBUGCODE(success =) buffer.readColorArray(fColors, fCount);
79 : } else {
80 : fCount = 0;
81 : fColors = nullptr;
82 : }
83 : #ifdef SK_DEBUG
84 : SkASSERT((unsigned)fCount <= 256);
85 : SkASSERT(success);
86 : #endif
87 : }
88 : #endif
89 :
90 0 : void SkColorTable::writeToBuffer(SkWriteBuffer& buffer) const {
91 0 : buffer.writeColorArray(fColors, fCount);
92 0 : }
93 :
94 0 : sk_sp<SkColorTable> SkColorTable::Create(SkReadBuffer& buffer) {
95 0 : if (buffer.isVersionLT(SkReadBuffer::kRemoveColorTableAlpha_Version)) {
96 0 : /*fAlphaType = */buffer.readUInt();
97 : }
98 :
99 0 : const int count = buffer.getArrayCount();
100 0 : if (0 == count) {
101 0 : return sk_sp<SkColorTable>(new SkColorTable(nullptr, 0));
102 : }
103 :
104 0 : if (count < 0 || count > 256) {
105 0 : buffer.validate(false);
106 0 : return nullptr;
107 : }
108 :
109 0 : const size_t allocSize = count * sizeof(SkPMColor);
110 0 : std::unique_ptr<SkPMColor> colors((SkPMColor*)sk_malloc_throw(allocSize));
111 0 : if (!buffer.readColorArray(colors.get(), count)) {
112 0 : return nullptr;
113 : }
114 :
115 0 : return sk_sp<SkColorTable>(new SkColorTable(colors.release(), count, kAllocatedWithMalloc));
116 : }
|