From 33515fbec463e3038b3d54fb74fe4b51af1791c6 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Wed, 16 May 2018 18:41:42 +0000 Subject: Do more optimizations to make FindBit() faster. Change-Id: Ibbc020393e38405f9d1cb0d483ef875777d4e721 Reviewed-on: https://pdfium-review.googlesource.com/32650 Reviewed-by: Henrique Nakashima Commit-Queue: Lei Zhang --- core/fxcodec/codec/fx_codec_fax.cpp | 42 +++++++++++-------------------------- 1 file changed, 12 insertions(+), 30 deletions(-) (limited to 'core/fxcodec/codec') diff --git a/core/fxcodec/codec/fx_codec_fax.cpp b/core/fxcodec/codec/fx_codec_fax.cpp index 09d0ebc157..4a70b5302e 100644 --- a/core/fxcodec/codec/fx_codec_fax.cpp +++ b/core/fxcodec/codec/fx_codec_fax.cpp @@ -31,19 +31,6 @@ const uint8_t OneLeadPos[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; -const uint8_t ZeroLeadPos[256] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, -}; // Limit of image dimension. Use the same limit as the JBIG2 codecs. const int kMaxImageDimension = 65535; @@ -53,20 +40,17 @@ int FindBit(const uint8_t* data_buf, int max_pos, int start_pos, bool bit) { if (start_pos >= max_pos) return max_pos; - const uint8_t* leading_pos = bit ? OneLeadPos : ZeroLeadPos; - if (start_pos % 8) { - uint8_t data = data_buf[start_pos / 8]; - if (bit) - data &= 0xff >> (start_pos % 8); - else - data |= 0xff << (8 - start_pos % 8); - - if (leading_pos[data] < 8) - return start_pos / 8 * 8 + leading_pos[data]; + const uint8_t bit_xor = bit ? 0x00 : 0xff; + int bit_offset = start_pos % 8; + if (bit_offset) { + const int byte_pos = start_pos / 8; + uint8_t data = (data_buf[byte_pos] ^ bit_xor) & (0xff >> bit_offset); + if (data) + return byte_pos * 8 + OneLeadPos[data]; start_pos += 7; } - const uint8_t skip = bit ? 0x00 : 0xff; + const int max_byte = (max_pos + 7) / 8; int byte_pos = start_pos / 8; @@ -85,15 +69,13 @@ int FindBit(const uint8_t* data_buf, int max_pos, int start_pos, bool bit) { } while (byte_pos < max_byte) { - if (data_buf[byte_pos] != skip) - break; + uint8_t data = data_buf[byte_pos] ^ bit_xor; + if (data) + return std::min(byte_pos * 8 + OneLeadPos[data], max_pos); ++byte_pos; } - if (byte_pos == max_byte) - return max_pos; - - return std::min(leading_pos[data_buf[byte_pos]] + byte_pos * 8, max_pos); + return max_pos; } void FaxG4FindB1B2(const std::vector& ref_buf, -- cgit v1.2.3