| 1 |
/* |
| 2 |
* Copyright 2023 Google LLC |
| 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 |
#include "include/core/SkColorTable.h" |
| 9 |
|
| 10 |
#include "include/core/SkImageInfo.h" |
| 11 |
#include "src/core/SkReadBuffer.h" |
| 12 |
#include "src/core/SkWriteBuffer.h" |
| 13 |
|
| 14 |
sk_sp<SkColorTable> SkColorTable::Make(const uint8_t tableA[256], |
| 15 |
const uint8_t tableR[256], |
| 16 |
const uint8_t tableG[256], |
| 17 |
const uint8_t tableB[256]) { |
| 18 |
if (!tableA && !tableR && !tableG && !tableB) { |
| 19 |
return nullptr; // The table is the identity |
| 20 |
} |
| 21 |
|
| 22 |
SkBitmap table; |
| 23 |
if (!table.tryAllocPixels(SkImageInfo::MakeA8(256, 4))) { |
| 24 |
return nullptr; |
| 25 |
} |
| 26 |
uint8_t *a = table.getAddr8(0,0), |
| 27 |
*r = table.getAddr8(0,1), |
| 28 |
*g = table.getAddr8(0,2), |
| 29 |
*b = table.getAddr8(0,3); |
| 30 |
for (int i = 0; i < 256; i++) { |
| 31 |
a[i] = tableA ? tableA[i] : i; |
| 32 |
r[i] = tableR ? tableR[i] : i; |
| 33 |
g[i] = tableG ? tableG[i] : i; |
| 34 |
b[i] = tableB ? tableB[i] : i; |
| 35 |
} |
| 36 |
table.setImmutable(); |
| 37 |
|
| 38 |
return sk_sp<SkColorTable>(new SkColorTable(table)); |
| 39 |
} |
| 40 |
|
| 41 |
void SkColorTable::flatten(SkWriteBuffer& buffer) const { |
| 42 |
buffer.writeByteArray(fTable.getAddr8(0, 0), 4 * 256); |
| 43 |
} |
| 44 |
|
| 45 |
sk_sp<SkColorTable> SkColorTable::Deserialize(SkReadBuffer& buffer) { |
| 46 |
uint8_t argb[4*256]; |
| 47 |
if (buffer.readByteArray(argb, sizeof(argb))) { |
| 48 |
return SkColorTable::Make(argb+0*256, argb+1*256, argb+2*256, argb+3*256); |
| 49 |
} |
| 50 |
return nullptr; |
| 51 |
} |