| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkTextureCompressor.h" | 8 #include "SkTextureCompressor.h" |
| 9 | 9 |
| 10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 palettes[1][6] = 0; | 102 palettes[1][6] = 0; |
| 103 palettes[1][7] = 255; | 103 palettes[1][7] = 255; |
| 104 | 104 |
| 105 // Figure out which of the two is better: | 105 // Figure out which of the two is better: |
| 106 // - accumError holds the accumulated error for each pixel from | 106 // - accumError holds the accumulated error for each pixel from |
| 107 // the associated palette | 107 // the associated palette |
| 108 // - indices holds the best indices for each palette in the | 108 // - indices holds the best indices for each palette in the |
| 109 // bottom 48 (16*3) bits. | 109 // bottom 48 (16*3) bits. |
| 110 uint32_t accumError[2] = { 0, 0 }; | 110 uint32_t accumError[2] = { 0, 0 }; |
| 111 uint64_t indices[2] = { 0, 0 }; | 111 uint64_t indices[2] = { 0, 0 }; |
| 112 for (int i = 15; i >= 0; ++i) { | 112 for (int i = 15; i >= 0; --i) { |
| 113 // For each palette: | 113 // For each palette: |
| 114 // 1. Retreive the result of this pixel | 114 // 1. Retreive the result of this pixel |
| 115 // 2. Store the error in accumError | 115 // 2. Store the error in accumError |
| 116 // 3. Store the minimum palette index in indices. | 116 // 3. Store the minimum palette index in indices. |
| 117 for (int p = 0; p < 2; ++p) { | 117 for (int p = 0; p < 2; ++p) { |
| 118 uint32_t result = compute_error(block[i], palettes[p]); | 118 uint32_t result = compute_error(block[i], palettes[p]); |
| 119 accumError[p] += (result >> 8); | 119 accumError[p] += (result >> 8); |
| 120 indices[p] <<= 3; | 120 indices[p] <<= 3; |
| 121 indices[p] |= result & ~7; | 121 indices[p] |= result & 7; |
| 122 } | 122 } |
| 123 } | 123 } |
| 124 | 124 |
| 125 SkASSERT(indices[0] < (static_cast<uint64_t>(1) << 48)); | 125 SkASSERT(indices[0] < (static_cast<uint64_t>(1) << 48)); |
| 126 SkASSERT(indices[1] < (static_cast<uint64_t>(1) << 48)); | 126 SkASSERT(indices[1] < (static_cast<uint64_t>(1) << 48)); |
| 127 | 127 |
| 128 uint8_t paletteIdx = (accumError[0] > accumError[1]) ? 0 : 1; | 128 uint8_t paletteIdx = (accumError[0] > accumError[1]) ? 0 : 1; |
| 129 | 129 |
| 130 // Assemble the compressed block. | 130 // Assemble the compressed block. |
| 131 uint64_t result = 0; | 131 uint64_t result = 0; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 return SkData::NewFromMalloc(dst, compressedDataSize); | 184 return SkData::NewFromMalloc(dst, compressedDataSize); |
| 185 } | 185 } |
| 186 | 186 |
| 187 //////////////////////////////////////////////////////////////////////////////// | 187 //////////////////////////////////////////////////////////////////////////////// |
| 188 | 188 |
| 189 namespace SkTextureCompressor { | 189 namespace SkTextureCompressor { |
| 190 | 190 |
| 191 typedef SkData *(*CompressBitmapProc)(const SkBitmap &bitmap); | 191 typedef SkData *(*CompressBitmapProc)(const SkBitmap &bitmap); |
| 192 | 192 |
| 193 SkData *CompressBitmapToFormat(const SkBitmap &bitmap, Format format) { | 193 SkData *CompressBitmapToFormat(const SkBitmap &bitmap, Format format) { |
| 194 SkAutoLockPixels alp(bitmap); |
| 195 |
| 194 CompressBitmapProc kProcMap[kLastEnum_SkColorType + 1][kFormatCnt]; | 196 CompressBitmapProc kProcMap[kLastEnum_SkColorType + 1][kFormatCnt]; |
| 195 memset(kProcMap, 0, sizeof(kProcMap)); | 197 memset(kProcMap, 0, sizeof(kProcMap)); |
| 196 | 198 |
| 197 // Map available bitmap configs to compression functions | 199 // Map available bitmap configs to compression functions |
| 198 kProcMap[SkBitmap::kA8_Config][kLATC_Format] = compress_a8_to_latc; | 200 kProcMap[SkBitmap::kA8_Config][kLATC_Format] = compress_a8_to_latc; |
| 199 | 201 |
| 200 CompressBitmapProc proc = kProcMap[bitmap.colorType()][format]; | 202 CompressBitmapProc proc = kProcMap[bitmap.colorType()][format]; |
| 201 if (NULL != proc) { | 203 if (NULL != proc) { |
| 202 return proc(bitmap); | 204 return proc(bitmap); |
| 203 } | 205 } |
| 204 | 206 |
| 205 return NULL; | 207 return NULL; |
| 206 } | 208 } |
| 207 | 209 |
| 208 } // namespace SkTextureCompressor | 210 } // namespace SkTextureCompressor |
| OLD | NEW |