blob: 286d9d140c23635f3fcf4cbe241a93e2dc40f1a1 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.com8a1c16f2008-12-17 15:59:43 +00007#include "SkPackBits.h"
8
bungemanccb74b82016-02-23 12:55:20 -08009size_t SkPackBits::ComputeMaxSize8(size_t srcSize) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000010 // worst case is the number of 8bit values + 1 byte per (up to) 128 entries.
bungemanccb74b82016-02-23 12:55:20 -080011 return ((srcSize + 127) >> 7) + srcSize;
reed@android.com8a1c16f2008-12-17 15:59:43 +000012}
13
jschuh699b8522015-06-04 15:10:37 -070014static uint8_t* flush_same8(uint8_t dst[], uint8_t value, size_t count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000015 while (count > 0) {
bungemanccb74b82016-02-23 12:55:20 -080016 size_t n = count > 128 ? 128 : count;
reed@android.com8a1c16f2008-12-17 15:59:43 +000017 *dst++ = (uint8_t)(n - 1);
18 *dst++ = (uint8_t)value;
19 count -= n;
20 }
21 return dst;
22}
23
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000024static uint8_t* flush_diff8(uint8_t* SK_RESTRICT dst,
jschuh699b8522015-06-04 15:10:37 -070025 const uint8_t* SK_RESTRICT src, size_t count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000026 while (count > 0) {
bungemanccb74b82016-02-23 12:55:20 -080027 size_t n = count > 128 ? 128 : count;
reed@android.com8a1c16f2008-12-17 15:59:43 +000028 *dst++ = (uint8_t)(n + 127);
jschuh699b8522015-06-04 15:10:37 -070029 memcpy(dst, src, n);
reed@android.com8a1c16f2008-12-17 15:59:43 +000030 src += n;
31 dst += n;
32 count -= n;
33 }
34 return dst;
35}
36
jschuh699b8522015-06-04 15:10:37 -070037size_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.com8a1c16f2008-12-17 15:59:43 +000041 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000042
jschuh699b8522015-06-04 15:10:37 -070043 uint8_t* const origDst = dst;
44 const uint8_t* stop = src + srcSize;
reed@android.com8a1c16f2008-12-17 15:59:43 +000045
jschuh699b8522015-06-04 15:10:37 -070046 for (intptr_t count = stop - src; count > 0; count = stop - src) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 if (1 == count) {
48 *dst++ = 0;
49 *dst++ = *src;
jschuh699b8522015-06-04 15:10:37 -070050 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000052
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 unsigned value = *src;
54 const uint8_t* s = src + 1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000055
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 if (*s == value) { // accumulate same values...
57 do {
58 s++;
59 if (s == stop) {
60 break;
61 }
62 } while (*s == value);
commit-bot@chromium.org2cfa3202014-04-19 22:00:40 +000063 dst = flush_same8(dst, value, SkToInt(s - src));
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 } 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.org2cfa3202014-04-19 22:00:40 +000074 dst = flush_diff8(dst, src, SkToInt(s - src));
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 }
76 src = s;
77 }
jschuh699b8522015-06-04 15:10:37 -070078 return dst - origDst;
reed@android.com8a1c16f2008-12-17 15:59:43 +000079}
80
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000081int SkPackBits::Unpack8(const uint8_t* SK_RESTRICT src, size_t srcSize,
jschuh699b8522015-06-04 15:10:37 -070082 uint8_t* SK_RESTRICT dst, size_t dstSize) {
83 uint8_t* const origDst = dst;
84 uint8_t* const endDst = dst + dstSize;
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 const uint8_t* stop = src + srcSize;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000086
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 while (src < stop) {
88 unsigned n = *src++;
89 if (n <= 127) { // repeat count (n + 1)
90 n += 1;
jschuh699b8522015-06-04 15:10:37 -070091 if (dst >(endDst - n)) {
92 return 0;
93 }
94 memset(dst, *src++, n);
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 } else { // same count (n - 127)
96 n -= 127;
jschuh699b8522015-06-04 15:10:37 -070097 if (dst > (endDst - n)) {
98 return 0;
99 }
100 memcpy(dst, src, n);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 src += n;
102 }
103 dst += n;
104 }
jschuh699b8522015-06-04 15:10:37 -0700105 SkASSERT(src <= stop);
commit-bot@chromium.org2cfa3202014-04-19 22:00:40 +0000106 return SkToInt(dst - origDst);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107}