Chromium Code Reviews| Index: src/utils/SkTextureCompressor.cpp |
| diff --git a/src/utils/SkTextureCompressor.cpp b/src/utils/SkTextureCompressor.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..645de54e79ba14eb7004636c5767304100bbb28a |
| --- /dev/null |
| +++ b/src/utils/SkTextureCompressor.cpp |
| @@ -0,0 +1,189 @@ |
| +/* |
| + * Copyright 2014 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#include "SkTextureCompressor.h" |
| + |
| +#include "SkBitmap.h" |
| +#include "SkData.h" |
| +#include "SkEndian.h" |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// |
| +// LATC compressor |
| +// |
| +//////////////////////////////////////////////////////////////////////////////// |
| + |
| +// Return the squared minimum error cost of approximating 'pixel' using the |
| +// provided palette. |
| +static uint16_t compute_error(uint16_t pixel, uint8_t palette[8]) { |
| + uint16_t error = 65535; |
| + for (int i = 0; i < 8; ++i) { |
| + uint16_t diff = SkTAbsDiff(static_cast<uint16_t>(palette[i]), pixel); |
| + error = SkMin32(diff, error); |
| + } |
| + return error*error; |
| +} |
| + |
| +// Compress LATC block. Each 4x4 block of pixels is decompressed by LATC from two |
| +// values LUM0 and LUM1, and an index into the generated palette. LATC constructs |
| +// a palette of eight colors from LUM0 and LUM1 using the algorithm: |
| +// |
| +// LUM0, if lum0 > lum1 and code(x,y) == 0 |
| +// LUM1, if lum0 > lum1 and code(x,y) == 1 |
| +// (6*LUM0+ LUM1)/7, if lum0 > lum1 and code(x,y) == 2 |
| +// (5*LUM0+2*LUM1)/7, if lum0 > lum1 and code(x,y) == 3 |
| +// (4*LUM0+3*LUM1)/7, if lum0 > lum1 and code(x,y) == 4 |
| +// (3*LUM0+4*LUM1)/7, if lum0 > lum1 and code(x,y) == 5 |
| +// (2*LUM0+5*LUM1)/7, if lum0 > lum1 and code(x,y) == 6 |
| +// ( LUM0+6*LUM1)/7, if lum0 > lum1 and code(x,y) == 7 |
| +// |
| +// LUM0, if lum0 <= lum1 and code(x,y) == 0 |
| +// LUM1, if lum0 <= lum1 and code(x,y) == 1 |
| +// (4*LUM0+ LUM1)/5, if lum0 <= lum1 and code(x,y) == 2 |
| +// (3*LUM0+2*LUM1)/5, if lum0 <= lum1 and code(x,y) == 3 |
| +// (2*LUM0+3*LUM1)/5, if lum0 <= lum1 and code(x,y) == 4 |
| +// ( LUM0+4*LUM1)/5, if lum0 <= lum1 and code(x,y) == 5 |
| +// 0, if lum0 <= lum1 and code(x,y) == 6 |
| +// 255, if lum0 <= lum1 and code(x,y) == 7 |
| +// |
| +// We compute the LATC palette using the following simple algorithm: |
| +// 1. Choose the minimum and maximum values in the block as LUM0 and LUM1 |
| +// 2. Figure out which of the two possible palettes is better. |
| + |
| +static uint64_t compress_latc_block(uint8_t block[16]) { |
| + // Just do a simple min/max but choose from which of the |
| + // two palettes is better |
| + uint8_t maxVal = 0; |
| + uint8_t minVal = 255; |
| + for (int i = 0; i < 16; ++i) { |
| + maxVal = SkMax32(maxVal, block[i]); |
| + minVal = SkMin32(minVal, block[i]); |
| + } |
| + |
| + // Generate palettes |
| + uint8_t palettes[2][8]; |
| + |
| + // Straight linear ramp |
| + palettes[0][0] = maxVal; |
| + palettes[0][1] = minVal; |
| + for (int i = 1; i < 7; ++i) { |
| + palettes[0][i+1] = ((7-i)*maxVal + i*minVal) / 7; |
| + } |
| + |
| + // Smaller linear ramp with min and max byte values at the end. |
| + palettes[1][0] = minVal; |
| + palettes[1][1] = maxVal; |
| + for (int i = 1; i < 5; ++i) { |
| + palettes[1][i+1] = ((5-i)*maxVal + i*minVal) / 5; |
| + } |
| + palettes[1][6] = 0; |
| + palettes[1][7] = 255; |
| + |
| + // Figure out which of the two is better |
| + uint16_t error[2] = { 0, 0 }; |
| + for (int i = 0; i < 16; ++i) { |
| + uint16_t pixel = static_cast<uint16_t>(block[i]); |
| + error[0] += compute_error(pixel, palettes[0]); |
| + error[1] += compute_error(pixel, palettes[1]); |
| + } |
| + uint8_t *palette = (error[0] > error[1]) ? palettes[1] : palettes[0]; |
| + |
| + // Compress the chosen palette |
| + uint64_t result = 0; |
| + result |= static_cast<uint64_t>(palette[0]); |
| + result |= static_cast<uint64_t>(palette[1]) << 8; |
| + |
| + uint64_t indices = 0; |
| + for (int i = 15; i >= 0; --i) { |
| + uint8_t pixel = block[i]; |
| + |
| + // The palette is selected in the loop above. This loop |
| + // is to determine what index into the palette to store for |
| + // each pixel. |
| + uint8_t minError = SkTAbsDiff(pixel, palette[0]); |
| + uint8_t minErrorIndex = 0; |
| + for (int j = 1; j < 8; ++j) { |
| + uint8_t error = SkTAbsDiff(pixel, palette[j]); |
| + if (error < minError) { |
| + minError = error; |
| + minErrorIndex = j; |
| + } |
| + } |
| + SkASSERT(minErrorIndex < 8); |
| + |
| + indices <<= 3; |
| + indices |= minErrorIndex; |
| + } |
| + result |= indices << 16; |
| + |
| + // We assume everything is little endian, if it's not then make it so. |
| + return SkEndian_SwapLE64(result); |
| +} |
| + |
| +static SkData *compress_a8_to_latc(const SkBitmap &bm) { |
| + // LATC compressed texels down into square 4x4 blocks |
| + static const int kLATCBlockSize = 4; |
| + |
| + // Make sure that our data is well-formed enough to be |
| + // considered for LATC compression |
| + if (bm.width() == 0 || bm.height() == 0 || |
| + (bm.width() % kLATCBlockSize) != 0 || |
| + (bm.height() % kLATCBlockSize) != 0 || |
| + (bm.config() != SkBitmap::kA8_Config)) { |
|
scroggo
2014/06/09 20:54:18
Use colorType() instead of config(). We are removi
krajcevski
2014/06/09 21:06:02
Done.
|
| + return NULL; |
| + } |
| + |
| + // The LATC format is 64 bits per 4x4 block. |
| + static const int kLATCEncodedBlockSize = 8; |
| + |
| + int blocksX = bm.width() / kLATCBlockSize; |
| + int blocksY = bm.height() / kLATCBlockSize; |
| + |
| + int compressedDataSize = blocksX * blocksY * kLATCEncodedBlockSize; |
| + uint64_t* dst = reinterpret_cast<uint64_t*>(sk_malloc_throw(compressedDataSize)); |
| + |
| + uint8_t block[16]; |
| + const uint8_t* row = reinterpret_cast<const uint8_t*>(bm.getPixels()); |
| + uint64_t* encPtr = dst; |
| + for (int y = 0; y < blocksY; ++y) { |
| + for (int x = 0; x < blocksX; ++x) { |
| + memcpy(block, row + (kLATCBlockSize * x), 4); |
| + memcpy(block + 4, row + bm.rowBytes() + (kLATCBlockSize * x), 4); |
| + memcpy(block + 8, row + 2*bm.rowBytes() + (kLATCBlockSize * x), 4); |
| + memcpy(block + 12, row + 3*bm.rowBytes() + (kLATCBlockSize * x), 4); |
| + |
| + *encPtr = compress_latc_block(block); |
| + ++encPtr; |
| + } |
| + row += kLATCBlockSize * bm.rowBytes(); |
| + } |
| + |
| + return SkData::NewFromMalloc(dst, compressedDataSize); |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| + |
| +namespace SkTextureCompressor { |
| + |
| +typedef SkData *(*CompressBitmapProc)(const SkBitmap &bitmap); |
| + |
| +SkData *CompressBitmapToConfig(const SkBitmap &bitmap, Format config) { |
|
scroggo
2014/06/09 20:54:18
Shouldn't this be ToFormat, as in the header file?
krajcevski
2014/06/09 21:06:02
Yes, whoops. Done.
|
| + CompressBitmapProc kProcMap[SkBitmap::kConfigCount][kFormatCnt]; |
| + memset(kProcMap, 0, sizeof(kProcMap)); |
| + |
| + // Map available bitmap configs to compression functions |
| + kProcMap[SkBitmap::kA8_Config][kLATC_Format] = compress_a8_to_latc; |
| + |
| + CompressBitmapProc proc = kProcMap[bitmap.config()][config]; |
| + if (NULL != proc) { |
| + return proc(bitmap); |
| + } |
| + |
| + return NULL; |
| +} |
| + |
| +} // namespace SkTextureCompressor |