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 // LATC compressor | |
| 17 // | |
| 18 //////////////////////////////////////////////////////////////////////////////// | |
| 19 | |
| 20 // Return the squared minimum error cost of approximating 'pixel' using the | |
| 21 // provided palette. Return this in the middle 16 bits of the integer. Return | |
| 22 // the best index in the palette for this pixel in the bottom 8 bits. | |
| 23 static uint32_t compute_error(uint8_t pixel, uint8_t palette[8]) { | |
| 24 int minIndex = 0; | |
| 25 uint8_t error = SkTAbsDiff(palette[0], pixel); | |
| 26 for (int i = 1; i < 8; ++i) { | |
| 27 uint8_t diff = SkTAbsDiff(palette[i], pixel); | |
| 28 if (diff < error) { | |
| 29 minIndex = i; | |
| 30 error = diff; | |
| 31 } | |
| 32 } | |
| 33 uint16_t errSq = static_cast<uint16_t>(error) * static_cast<uint16_t>(error) ; | |
| 34 SkASSERT(minIndex >= 0 && minIndex < 8); | |
| 35 return (static_cast<uint32_t>(errSq) << 8) | static_cast<uint32_t>(minIndex) ; | |
| 36 } | |
| 37 | |
| 38 // Compress LATC block. Each 4x4 block of pixels is decompressed by LATC from tw o | |
| 39 // values LUM0 and LUM1, and an index into the generated palette. LATC construct s | |
| 40 // a palette of eight colors from LUM0 and LUM1 using the algorithm: | |
| 41 // | |
| 42 // LUM0, if lum0 > lum1 and code(x,y) == 0 | |
| 43 // LUM1, if lum0 > lum1 and code(x,y) == 1 | |
| 44 // (6*LUM0+ LUM1)/7, if lum0 > lum1 and code(x,y) == 2 | |
| 45 // (5*LUM0+2*LUM1)/7, if lum0 > lum1 and code(x,y) == 3 | |
| 46 // (4*LUM0+3*LUM1)/7, if lum0 > lum1 and code(x,y) == 4 | |
| 47 // (3*LUM0+4*LUM1)/7, if lum0 > lum1 and code(x,y) == 5 | |
| 48 // (2*LUM0+5*LUM1)/7, if lum0 > lum1 and code(x,y) == 6 | |
| 49 // ( LUM0+6*LUM1)/7, if lum0 > lum1 and code(x,y) == 7 | |
| 50 // | |
| 51 // LUM0, if lum0 <= lum1 and code(x,y) == 0 | |
| 52 // LUM1, if lum0 <= lum1 and code(x,y) == 1 | |
| 53 // (4*LUM0+ LUM1)/5, if lum0 <= lum1 and code(x,y) == 2 | |
| 54 // (3*LUM0+2*LUM1)/5, if lum0 <= lum1 and code(x,y) == 3 | |
| 55 // (2*LUM0+3*LUM1)/5, if lum0 <= lum1 and code(x,y) == 4 | |
| 56 // ( LUM0+4*LUM1)/5, if lum0 <= lum1 and code(x,y) == 5 | |
| 57 // 0, if lum0 <= lum1 and code(x,y) == 6 | |
| 58 // 255, if lum0 <= lum1 and code(x,y) == 7 | |
| 59 // | |
| 60 // We compute the LATC palette using the following simple algorithm: | |
| 61 // 1. Choose the minimum and maximum values in the block as LUM0 and LUM1 | |
| 62 // 2. Figure out which of the two possible palettes is better. | |
| 63 | |
| 64 static uint64_t compress_latc_block(uint8_t block[16]) { | |
|
robertphillips
2014/06/09 22:12:40
from which -> which ?
krajcevski
2014/06/10 14:38:09
Done.
| |
| 65 // Just do a simple min/max but choose from which of the | |
| 66 // two palettes is better | |
| 67 uint8_t maxVal = 0; | |
| 68 uint8_t minVal = 255; | |
| 69 for (int i = 0; i < 16; ++i) { | |
| 70 maxVal = SkMax32(maxVal, block[i]); | |
| 71 minVal = SkMin32(minVal, block[i]); | |
| 72 } | |
| 73 | |
| 74 // Generate palettes | |
| 75 uint8_t palettes[2][8]; | |
| 76 | |
| 77 // Straight linear ramp | |
| 78 palettes[0][0] = maxVal; | |
| 79 palettes[0][1] = minVal; | |
| 80 for (int i = 1; i < 7; ++i) { | |
| 81 palettes[0][i+1] = ((7-i)*maxVal + i*minVal) / 7; | |
| 82 } | |
| 83 | |
| 84 // Smaller linear ramp with min and max byte values at the end. | |
| 85 palettes[1][0] = minVal; | |
| 86 palettes[1][1] = maxVal; | |
| 87 for (int i = 1; i < 5; ++i) { | |
| 88 palettes[1][i+1] = ((5-i)*maxVal + i*minVal) / 5; | |
| 89 } | |
| 90 palettes[1][6] = 0; | |
| 91 palettes[1][7] = 255; | |
| 92 | |
| 93 // Figure out which of the two is better: | |
| 94 // - accumError holds the accumulated error for each pixel from | |
| 95 // the associated palette | |
| 96 // - indices holds the best indices for each palette in the | |
| 97 // bottom 48 (16*3) bits. | |
| 98 uint32_t accumError[2] = { 0, 0 }; | |
| 99 uint64_t indices[2] = { 0, 0 }; | |
| 100 for (int i = 15; i >= 0; ++i) { | |
| 101 uint16_t pixel = static_cast<uint16_t>(block[i]); | |
| 102 | |
| 103 // For each palette: | |
| 104 // 1. Retreive the result of this pixel | |
| 105 // 2. Store the error in accumError | |
| 106 // 3. Store the minimum palette index in indices. | |
| 107 for (int p = 0; p < 2; ++p) { | |
| 108 uint32_t result = compute_error(pixel, palettes[p]); | |
| 109 accumError[p] += (result >> 8); | |
| 110 indices[p] <<= 3; | |
| 111 indices[p] |= result & ~7; | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 SkASSERT(indices[0] < (1 << 48)); | |
| 116 SkASSERT(indices[1] < (1 << 48)); | |
| 117 | |
| 118 uint8_t paletteIdx = (accumError[0] > accumError[1]) ? 0 : 1; | |
| 119 | |
|
robertphillips
2014/06/09 22:12:40
// Assemble the compressed block ?
krajcevski
2014/06/10 14:38:09
Done.
| |
| 120 // Compress the chosen palette | |
| 121 uint64_t result = 0; | |
| 122 | |
| 123 // Jam the first two palette entries into the bottom 16 bits of | |
| 124 // a 64 bit integer. Based on the palette that we chose, one will | |
| 125 // be larger than the other and it will select the proper palette. | |
| 126 result |= static_cast<uint64_t>(palettes[paletteIdx][0]); | |
| 127 result |= static_cast<uint64_t>(palettes[paletteIdx][1]) << 8; | |
| 128 | |
| 129 // Jam the indices into the top 48 bits. | |
| 130 result |= indices[paletteIdx] << 16; | |
| 131 | |
| 132 // We assume everything is little endian, if it's not then make it so. | |
| 133 return SkEndian_SwapLE64(result); | |
| 134 } | |
| 135 | |
| 136 static SkData *compress_a8_to_latc(const SkBitmap &bm) { | |
| 137 // LATC compressed texels down into square 4x4 blocks | |
| 138 static const int kLATCBlockSize = 4; | |
| 139 | |
| 140 // Make sure that our data is well-formed enough to be | |
| 141 // considered for LATC compression | |
| 142 if (bm.width() == 0 || bm.height() == 0 || | |
| 143 (bm.width() % kLATCBlockSize) != 0 || | |
| 144 (bm.height() % kLATCBlockSize) != 0 || | |
| 145 (bm.colorType() != kAlpha_8_SkColorType)) { | |
| 146 return NULL; | |
| 147 } | |
| 148 | |
| 149 // The LATC format is 64 bits per 4x4 block. | |
| 150 static const int kLATCEncodedBlockSize = 8; | |
| 151 | |
| 152 int blocksX = bm.width() / kLATCBlockSize; | |
| 153 int blocksY = bm.height() / kLATCBlockSize; | |
| 154 | |
| 155 int compressedDataSize = blocksX * blocksY * kLATCEncodedBlockSize; | |
| 156 uint64_t* dst = reinterpret_cast<uint64_t*>(sk_malloc_throw(compressedDataSi ze)); | |
| 157 | |
| 158 uint8_t block[16]; | |
| 159 const uint8_t* row = reinterpret_cast<const uint8_t*>(bm.getPixels()); | |
| 160 uint64_t* encPtr = dst; | |
| 161 for (int y = 0; y < blocksY; ++y) { | |
| 162 for (int x = 0; x < blocksX; ++x) { | |
| 163 memcpy(block, row + (kLATCBlockSize * x), 4); | |
| 164 memcpy(block + 4, row + bm.rowBytes() + (kLATCBlockSize * x), 4); | |
| 165 memcpy(block + 8, row + 2*bm.rowBytes() + (kLATCBlockSize * x), 4); | |
| 166 memcpy(block + 12, row + 3*bm.rowBytes() + (kLATCBlockSize * x), 4); | |
| 167 | |
| 168 *encPtr = compress_latc_block(block); | |
| 169 ++encPtr; | |
| 170 } | |
| 171 row += kLATCBlockSize * bm.rowBytes(); | |
| 172 } | |
| 173 | |
| 174 return SkData::NewFromMalloc(dst, compressedDataSize); | |
| 175 } | |
| 176 | |
| 177 //////////////////////////////////////////////////////////////////////////////// | |
| 178 | |
| 179 namespace SkTextureCompressor { | |
| 180 | |
| 181 typedef SkData *(*CompressBitmapProc)(const SkBitmap &bitmap); | |
| 182 | |
| 183 SkData *CompressBitmapToFormat(const SkBitmap &bitmap, Format format) { | |
| 184 CompressBitmapProc kProcMap[kLastEnum_SkColorType + 1][kFormatCnt]; | |
| 185 memset(kProcMap, 0, sizeof(kProcMap)); | |
| 186 | |
| 187 // Map available bitmap configs to compression functions | |
| 188 kProcMap[SkBitmap::kA8_Config][kLATC_Format] = compress_a8_to_latc; | |
| 189 | |
| 190 CompressBitmapProc proc = kProcMap[bitmap.colorType()][format]; | |
| 191 if (NULL != proc) { | |
| 192 return proc(bitmap); | |
| 193 } | |
| 194 | |
| 195 return NULL; | |
| 196 } | |
| 197 | |
| 198 } // namespace SkTextureCompressor | |
| OLD | NEW |