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