epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | #include "SkPackBits.h" |
| 8 | |
bungeman | ccb74b8 | 2016-02-23 12:55:20 -0800 | [diff] [blame] | 9 | size_t SkPackBits::ComputeMaxSize8(size_t srcSize) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | // worst case is the number of 8bit values + 1 byte per (up to) 128 entries. |
bungeman | ccb74b8 | 2016-02-23 12:55:20 -0800 | [diff] [blame] | 11 | return ((srcSize + 127) >> 7) + srcSize; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | } |
| 13 | |
jschuh | 699b852 | 2015-06-04 15:10:37 -0700 | [diff] [blame] | 14 | static uint8_t* flush_same8(uint8_t dst[], uint8_t value, size_t count) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | while (count > 0) { |
bungeman | ccb74b8 | 2016-02-23 12:55:20 -0800 | [diff] [blame] | 16 | size_t n = count > 128 ? 128 : count; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | *dst++ = (uint8_t)(n - 1); |
| 18 | *dst++ = (uint8_t)value; |
| 19 | count -= n; |
| 20 | } |
| 21 | return dst; |
| 22 | } |
| 23 | |
tomhudson@google.com | a87cd2a | 2011-06-15 16:50:27 +0000 | [diff] [blame] | 24 | static uint8_t* flush_diff8(uint8_t* SK_RESTRICT dst, |
jschuh | 699b852 | 2015-06-04 15:10:37 -0700 | [diff] [blame] | 25 | const uint8_t* SK_RESTRICT src, size_t count) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | while (count > 0) { |
bungeman | ccb74b8 | 2016-02-23 12:55:20 -0800 | [diff] [blame] | 27 | size_t n = count > 128 ? 128 : count; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | *dst++ = (uint8_t)(n + 127); |
jschuh | 699b852 | 2015-06-04 15:10:37 -0700 | [diff] [blame] | 29 | memcpy(dst, src, n); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | src += n; |
| 31 | dst += n; |
| 32 | count -= n; |
| 33 | } |
| 34 | return dst; |
| 35 | } |
| 36 | |
jschuh | 699b852 | 2015-06-04 15:10:37 -0700 | [diff] [blame] | 37 | size_t SkPackBits::Pack8(const uint8_t* SK_RESTRICT src, size_t srcSize, |
| 38 | uint8_t* SK_RESTRICT dst, size_t dstSize) { |
| 39 | if (dstSize < ComputeMaxSize8(srcSize)) { |
| 40 | return 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | |
jschuh | 699b852 | 2015-06-04 15:10:37 -0700 | [diff] [blame] | 43 | uint8_t* const origDst = dst; |
| 44 | const uint8_t* stop = src + srcSize; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | |
jschuh | 699b852 | 2015-06-04 15:10:37 -0700 | [diff] [blame] | 46 | for (intptr_t count = stop - src; count > 0; count = stop - src) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | if (1 == count) { |
| 48 | *dst++ = 0; |
| 49 | *dst++ = *src; |
jschuh | 699b852 | 2015-06-04 15:10:37 -0700 | [diff] [blame] | 50 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 52 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | unsigned value = *src; |
| 54 | const uint8_t* s = src + 1; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 55 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | if (*s == value) { // accumulate same values... |
| 57 | do { |
| 58 | s++; |
| 59 | if (s == stop) { |
| 60 | break; |
| 61 | } |
| 62 | } while (*s == value); |
commit-bot@chromium.org | 2cfa320 | 2014-04-19 22:00:40 +0000 | [diff] [blame] | 63 | dst = flush_same8(dst, value, SkToInt(s - src)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | } else { // accumulate diff values... |
| 65 | do { |
| 66 | if (++s == stop) { |
| 67 | goto FLUSH_DIFF; |
| 68 | } |
| 69 | // only stop if we hit 3 in a row, |
| 70 | // otherwise we get bigger than compuatemax |
| 71 | } while (*s != s[-1] || s[-1] != s[-2]); |
| 72 | s -= 2; // back up so we don't grab the "same" values that follow |
| 73 | FLUSH_DIFF: |
commit-bot@chromium.org | 2cfa320 | 2014-04-19 22:00:40 +0000 | [diff] [blame] | 74 | dst = flush_diff8(dst, src, SkToInt(s - src)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | } |
| 76 | src = s; |
| 77 | } |
jschuh | 699b852 | 2015-06-04 15:10:37 -0700 | [diff] [blame] | 78 | return dst - origDst; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | } |
| 80 | |
tomhudson@google.com | a87cd2a | 2011-06-15 16:50:27 +0000 | [diff] [blame] | 81 | int SkPackBits::Unpack8(const uint8_t* SK_RESTRICT src, size_t srcSize, |
jschuh | 699b852 | 2015-06-04 15:10:37 -0700 | [diff] [blame] | 82 | uint8_t* SK_RESTRICT dst, size_t dstSize) { |
| 83 | uint8_t* const origDst = dst; |
| 84 | uint8_t* const endDst = dst + dstSize; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | const uint8_t* stop = src + srcSize; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 86 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | while (src < stop) { |
| 88 | unsigned n = *src++; |
| 89 | if (n <= 127) { // repeat count (n + 1) |
| 90 | n += 1; |
jschuh | 699b852 | 2015-06-04 15:10:37 -0700 | [diff] [blame] | 91 | if (dst >(endDst - n)) { |
| 92 | return 0; |
| 93 | } |
| 94 | memset(dst, *src++, n); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | } else { // same count (n - 127) |
| 96 | n -= 127; |
jschuh | 699b852 | 2015-06-04 15:10:37 -0700 | [diff] [blame] | 97 | if (dst > (endDst - n)) { |
| 98 | return 0; |
| 99 | } |
| 100 | memcpy(dst, src, n); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | src += n; |
| 102 | } |
| 103 | dst += n; |
| 104 | } |
jschuh | 699b852 | 2015-06-04 15:10:37 -0700 | [diff] [blame] | 105 | SkASSERT(src <= stop); |
commit-bot@chromium.org | 2cfa320 | 2014-04-19 22:00:40 +0000 | [diff] [blame] | 106 | return SkToInt(dst - origDst); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | } |