Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2014 Google Inc. | |
| 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 "SkTextureCompressor.h" | |
| 9 | |
| 10 #include "SkBitmap.h" | |
| 11 #include "SkData.h" | |
| 12 #include "SkEndian.h" | |
| 13 | |
| 14 //////////////////////////////////////////////////////////////////////////////// | |
| 15 // | |
| 16 // Utility Functions | |
| 17 // | |
| 18 //////////////////////////////////////////////////////////////////////////////// | |
| 19 | |
|
robertphillips
2014/06/09 19:50:18
Maybe SkTAbsDiff in SkTypes.h ?
krajcevski
2014/06/09 20:39:34
Done.
| |
| 20 template<typename T> | |
| 21 static const T absolute_difference(const T& a, const T& b) { | |
| 22 return (a > b) ? (a - b) : (b - a); | |
| 23 } | |
| 24 | |
| 25 //////////////////////////////////////////////////////////////////////////////// | |
| 26 // | |
| 27 // LATC compressor | |
| 28 // | |
| 29 //////////////////////////////////////////////////////////////////////////////// | |
| 30 | |
|
robertphillips
2014/06/09 19:50:18
static const int kLATCPaletteSize = 8; ?
// Retur
krajcevski
2014/06/09 20:39:34
Done.
| |
| 31 static uint16_t compute_error(uint16_t pixel, uint8_t palette[8]) { | |
| 32 uint16_t error = 65535; | |
| 33 for (int i = 0; i < 8; ++i) { | |
| 34 uint16_t diff = absolute_difference(static_cast<uint16_t>(palette[i]), p ixel); | |
| 35 error = SkMin32(diff, error); | |
| 36 } | |
| 37 return error*error; | |
| 38 } | |
| 39 | |
|
robertphillips
2014/06/09 19:50:18
// Maybe an overview of the compression algorithm
krajcevski
2014/06/09 20:39:34
Done.
| |
| 40 static uint64_t compress_latc_block(uint8_t block[16]) { | |
| 41 // Just do a simple min/max but choose from which of the | |
| 42 // two palettes is better | |
| 43 uint8_t maxVal = 0; | |
| 44 uint8_t minVal = 255; | |
| 45 for (int i = 0; i < 16; ++i) { | |
| 46 maxVal = SkMax32(maxVal, block[i]); | |
| 47 minVal = SkMin32(minVal, block[i]); | |
| 48 } | |
| 49 | |
| 50 // Generate palettes | |
| 51 uint8_t palettes[2][8]; | |
| 52 | |
|
robertphillips
2014/06/09 19:50:18
// palette 0 is a straight up linear ramp between
krajcevski
2014/06/09 20:39:34
Done.
| |
| 53 palettes[0][0] = maxVal; | |
| 54 palettes[0][1] = minVal; | |
| 55 for (int i = 1; i < 7; ++i) { | |
| 56 palettes[0][i+1] = ((7-i)*maxVal + i*minVal) / 7; | |
| 57 } | |
| 58 | |
|
robertphillips
2014/06/09 19:50:18
// palette 1 is a shorter linear ramp between minV
krajcevski
2014/06/09 20:39:34
The way this algorithm is set up, probably not. In
| |
| 59 palettes[1][0] = minVal; | |
| 60 palettes[1][1] = maxVal; | |
| 61 for (int i = 1; i < 5; ++i) { | |
| 62 palettes[1][i+1] = ((5-i)*maxVal + i*minVal) / 5; | |
| 63 } | |
| 64 palettes[1][6] = 0; | |
| 65 palettes[1][7] = 255; | |
|
robertphillips
2014/06/09 19:50:17
The raster code only ever generates 16 different a
krajcevski
2014/06/09 20:39:34
Yes, probably. I think that investigating better c
| |
| 66 | |
|
robertphillips
2014/06/09 19:50:18
error0 & error1 to match palette numbering ?
krajcevski
2014/06/09 20:39:34
Done.
| |
| 67 // Figure out which of the two is better | |
| 68 uint16_t error1 = 0, error2 = 0; | |
| 69 for (int i = 0; i < 16; ++i) { | |
| 70 uint16_t pixel = static_cast<uint16_t>(block[i]); | |
| 71 error1 += compute_error(pixel, palettes[0]); | |
| 72 error2 += compute_error(pixel, palettes[1]); | |
| 73 } | |
| 74 uint8_t *palette = (error1 > error2) ? palettes[1] : palettes[0]; | |
| 75 | |
| 76 // Compress the chosen palette | |
| 77 uint64_t result = 0; | |
|
robertphillips
2014/06/09 19:50:18
Is this jamming the right values in?
krajcevski
2014/06/09 20:39:34
Yes, although I did have to double check.
| |
| 78 result |= static_cast<uint64_t>(palette[0]); | |
| 79 result |= static_cast<uint64_t>(palette[1]) << 8; | |
| 80 | |
| 81 uint64_t indices = 0; | |
| 82 for (int i = 15; i >= 0; --i) { | |
| 83 uint8_t pixel = block[i]; | |
| 84 | |
| 85 uint8_t minError = absolute_difference(pixel, palette[0]); | |
| 86 uint8_t minErrorIndex = 0; | |
|
robertphillips
2014/06/09 19:50:18
So we essentially do this loop to select the palet
krajcevski
2014/06/09 20:39:34
The palette is selected in the preceding loop. Thi
| |
| 87 for (int j = 1; j < 8; ++j) { | |
| 88 uint8_t error = absolute_difference(pixel, palette[j]); | |
| 89 if (error < minError) { | |
| 90 minError = error; | |
| 91 minErrorIndex = j; | |
| 92 } | |
| 93 } | |
| 94 SkASSERT(minErrorIndex < 8); | |
| 95 | |
| 96 indices <<= 3; | |
| 97 indices |= minErrorIndex; | |
| 98 } | |
| 99 result |= indices << 16; | |
| 100 | |
| 101 // We assume everything is little endian, if it's not then make it so. | |
| 102 return SkEndian_SwapLE64(result); | |
| 103 } | |
| 104 | |
| 105 static SkData *compress_a8_to_latc(const SkBitmap &bm) { | |
| 106 // LATC compressed texels down into square 4x4 blocks | |
| 107 static const int kLATCBlockSize = 4; | |
| 108 | |
| 109 // Make sure that our data is well-formed enough to be | |
| 110 // considered for LATC compression | |
| 111 if (bm.width() == 0 || bm.height() == 0 || | |
| 112 (bm.width() % kLATCBlockSize) != 0 || | |
| 113 (bm.height() % kLATCBlockSize) != 0 || | |
| 114 (bm.config() != SkBitmap::kA8_Config)) { | |
| 115 return NULL; | |
| 116 } | |
| 117 | |
| 118 // The LATC format is 64 bits per 4x4 block. | |
| 119 static const int kLATCEncodedBlockSize = 8; | |
| 120 | |
| 121 int blocksX = bm.width() / kLATCBlockSize; | |
| 122 int blocksY = bm.height() / kLATCBlockSize; | |
| 123 | |
| 124 int compressedDataSize = blocksX * blocksY * kLATCEncodedBlockSize; | |
| 125 uint64_t* dst = reinterpret_cast<uint64_t*>(sk_malloc_throw(compressedDataSi ze)); | |
| 126 | |
| 127 uint8_t block[16]; | |
| 128 const uint8_t* row = reinterpret_cast<const uint8_t*>(bm.getPixels()); | |
| 129 uint64_t* encPtr = dst; | |
| 130 for (int y = 0; y < blocksY; ++y) { | |
| 131 for (int x = 0; x < blocksX; ++x) { | |
| 132 memcpy(block, row + (kLATCBlockSize * x), 4); | |
| 133 memcpy(block + 4, row + bm.rowBytes() + (kLATCBlockSize * x), 4); | |
| 134 memcpy(block + 8, row + 2*bm.rowBytes() + (kLATCBlockSize * x), 4); | |
| 135 memcpy(block + 12, row + 3*bm.rowBytes() + (kLATCBlockSize * x), 4); | |
| 136 | |
| 137 *encPtr = compress_latc_block(block); | |
| 138 ++encPtr; | |
| 139 } | |
| 140 row += kLATCBlockSize * bm.rowBytes(); | |
| 141 } | |
| 142 | |
| 143 return SkData::NewFromMalloc(dst, compressedDataSize); | |
| 144 } | |
| 145 | |
| 146 //////////////////////////////////////////////////////////////////////////////// | |
| 147 | |
| 148 namespace SkTextureCompressor { | |
| 149 | |
| 150 typedef SkData *(*CompressBitmapProc)(const SkBitmap &bitmap); | |
| 151 | |
| 152 SkData *CompressBitmapToConfig(const SkBitmap &bitmap, Format config) { | |
| 153 CompressBitmapProc kProcMap[SkBitmap::kConfigCount][kFormatCnt]; | |
| 154 memset(kProcMap, 0, sizeof(kProcMap)); | |
| 155 | |
| 156 // Map available bitmap configs to compression functions | |
| 157 kProcMap[SkBitmap::kA8_Config][kLATC_Format] = compress_a8_to_latc; | |
| 158 | |
| 159 CompressBitmapProc proc = kProcMap[bitmap.config()][config]; | |
| 160 if (NULL != proc) { | |
| 161 return proc(bitmap); | |
| 162 } | |
| 163 | |
| 164 return NULL; | |
| 165 } | |
| 166 | |
| 167 } // namespace SkTextureCompressor | |
| OLD | NEW |