OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 #include "SkPackBits.h" | 7 #include "SkPackBits.h" |
8 | 8 |
9 size_t SkPackBits::ComputeMaxSize8(int count) { | 9 size_t SkPackBits::ComputeMaxSize8(size_t srcSize) { |
10 // worst case is the number of 8bit values + 1 byte per (up to) 128 entries. | 10 // worst case is the number of 8bit values + 1 byte per (up to) 128 entries. |
11 return ((count + 127) >> 7) + count; | 11 return ((srcSize + 127) >> 7) + srcSize; |
12 } | 12 } |
13 | 13 |
14 static uint8_t* flush_same8(uint8_t dst[], uint8_t value, size_t count) { | 14 static uint8_t* flush_same8(uint8_t dst[], uint8_t value, size_t count) { |
15 while (count > 0) { | 15 while (count > 0) { |
16 int n = count > 128 ? 128 : count; | 16 size_t n = count > 128 ? 128 : count; |
17 *dst++ = (uint8_t)(n - 1); | 17 *dst++ = (uint8_t)(n - 1); |
18 *dst++ = (uint8_t)value; | 18 *dst++ = (uint8_t)value; |
19 count -= n; | 19 count -= n; |
20 } | 20 } |
21 return dst; | 21 return dst; |
22 } | 22 } |
23 | 23 |
24 static uint8_t* flush_diff8(uint8_t* SK_RESTRICT dst, | 24 static uint8_t* flush_diff8(uint8_t* SK_RESTRICT dst, |
25 const uint8_t* SK_RESTRICT src, size_t count) { | 25 const uint8_t* SK_RESTRICT src, size_t count) { |
26 while (count > 0) { | 26 while (count > 0) { |
27 int n = count > 128 ? 128 : count; | 27 size_t n = count > 128 ? 128 : count; |
28 *dst++ = (uint8_t)(n + 127); | 28 *dst++ = (uint8_t)(n + 127); |
29 memcpy(dst, src, n); | 29 memcpy(dst, src, n); |
30 src += n; | 30 src += n; |
31 dst += n; | 31 dst += n; |
32 count -= n; | 32 count -= n; |
33 } | 33 } |
34 return dst; | 34 return dst; |
35 } | 35 } |
36 | 36 |
37 size_t SkPackBits::Pack8(const uint8_t* SK_RESTRICT src, size_t srcSize, | 37 size_t SkPackBits::Pack8(const uint8_t* SK_RESTRICT src, size_t srcSize, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 } while (*s != s[-1] || s[-1] != s[-2]); | 71 } while (*s != s[-1] || s[-1] != s[-2]); |
72 s -= 2; // back up so we don't grab the "same" values that follow | 72 s -= 2; // back up so we don't grab the "same" values that follow |
73 FLUSH_DIFF: | 73 FLUSH_DIFF: |
74 dst = flush_diff8(dst, src, SkToInt(s - src)); | 74 dst = flush_diff8(dst, src, SkToInt(s - src)); |
75 } | 75 } |
76 src = s; | 76 src = s; |
77 } | 77 } |
78 return dst - origDst; | 78 return dst - origDst; |
79 } | 79 } |
80 | 80 |
81 #include "SkUtils.h" | |
82 | |
83 int SkPackBits::Unpack8(const uint8_t* SK_RESTRICT src, size_t srcSize, | 81 int SkPackBits::Unpack8(const uint8_t* SK_RESTRICT src, size_t srcSize, |
84 uint8_t* SK_RESTRICT dst, size_t dstSize) { | 82 uint8_t* SK_RESTRICT dst, size_t dstSize) { |
85 uint8_t* const origDst = dst; | 83 uint8_t* const origDst = dst; |
86 uint8_t* const endDst = dst + dstSize; | 84 uint8_t* const endDst = dst + dstSize; |
87 const uint8_t* stop = src + srcSize; | 85 const uint8_t* stop = src + srcSize; |
88 | 86 |
89 while (src < stop) { | 87 while (src < stop) { |
90 unsigned n = *src++; | 88 unsigned n = *src++; |
91 if (n <= 127) { // repeat count (n + 1) | 89 if (n <= 127) { // repeat count (n + 1) |
92 n += 1; | 90 n += 1; |
93 if (dst >(endDst - n)) { | 91 if (dst >(endDst - n)) { |
94 return 0; | 92 return 0; |
95 } | 93 } |
96 memset(dst, *src++, n); | 94 memset(dst, *src++, n); |
97 } else { // same count (n - 127) | 95 } else { // same count (n - 127) |
98 n -= 127; | 96 n -= 127; |
99 if (dst > (endDst - n)) { | 97 if (dst > (endDst - n)) { |
100 return 0; | 98 return 0; |
101 } | 99 } |
102 memcpy(dst, src, n); | 100 memcpy(dst, src, n); |
103 src += n; | 101 src += n; |
104 } | 102 } |
105 dst += n; | 103 dst += n; |
106 } | 104 } |
107 SkASSERT(src <= stop); | 105 SkASSERT(src <= stop); |
108 return SkToInt(dst - origDst); | 106 return SkToInt(dst - origDst); |
109 } | 107 } |
OLD | NEW |