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