RLE:由两个符号编码
RLE: encode by two symbols
我已经创建了RLE编码函数,它将像"A1A1B7B7B7B7"这样的序列编码为:"#A12#B74"。
void encode(const char *input_path, const char *output_path)
{ // Begin of SBDLib::SBIMask::encode
std::fstream input(input_path, std::ios_base::in | std::ios_base::binary);
std::fstream output(output_path, std::ios_base::out | std::ios_base::binary);
int size = 0; // Set size variable
input.seekg(0, std::ios::end); // Move to EOF
size = input.tellg(); // Tell position
input.seekg(0); // Move to the beginning
int i = 1; // Create encoding counter
int counter = 0; // Create color counter
int cbyte1, cbyte2; // Create current color bytes
int pbyte1 = 0x0; int pbyte2 = 0x0; // Create previous color bytes
while (((cbyte1 = input.get()) != EOF && (cbyte2 = input.get()) != EOF)
|| input.tellg() >= size)
{ // Begin of while
// If current bytes are not equal to previous bytes
// or cursor is at the end of the input file, write
// binary data to file; don't do it if previous bytes
// were not set from 0x0 to any other integer.
if (((cbyte1 != pbyte1 || cbyte2 != pbyte2)
|| (input.tellg() == size))
&& (pbyte1 != 0x0 && pbyte2 != 0x0))
{ // Begin of main if
output << SEPARATOR; // Write separator to file
output.write(reinterpret_cast<const char*>(&pbyte1), 1);
output.write(reinterpret_cast<const char*>(&pbyte2), 1);
output << std::hex << counter; // Write separator, bytes and count
counter = 1; // Reset counter
} // End of main if
else counter++; // Increment counter
pbyte1 = cbyte1; pbyte2 = cbyte2; // Set previous bytes
} // End of main while
} // End of encode
然而,函数没有我需要的那么快。这是函数的第二个版本,我已经改进了它,使它更快,但它仍然太慢了。你有什么改进的办法吗?我缺乏创意。
根据您从文件中读取的数据的大小,一次从输入文件中读取数据块而不是单个字符可能是一个好主意。这可能比为每个输入字符访问磁盘上的输入文件要快得多。
伪代码示例:
char dataArray[100];
while( !EOF )
{
input.get( &dataArray[0], 100 ); // read a block of data not a single charater
process( dataArray ); // process one line
}
相关文章:
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 两个有符号数字之间的距离
- 为什么 Clang 和 GCC 中两个无符号整数之和的结果类型不同
- C++ 分段错误:11 错误,同时编码将两个数字相加的链接列表
- 我该如何编码,使计算机知道两个名称条目和三个名称条目之间的区别
- 如何安全地比较两个无符号整数计数器?
- 如何使用位操作在单个整数中编码和解码两个数字
- 获取两个无符号整数 C++ 乘积的高 32 位的有效方法
- 如何以精度换取速度来评估C++中两个向量的点积符号?(不特定于硬件)
- 使用定点方法编码查找根的两个必要条件
- 当两个共享库定义相同的符号时,实际会发生什么?
- 与两个符号链接.一个在存档文件中定义
- SSE2 内部函数 - 找到两个无符号短向量的最大值
- 加载具有相同符号的两个共享库时是否存在符号冲突
- 如何链接(或绕过)定义相同符号的两个第三方静态库
- 将两个字符安全转换为无符号短字符
- C++模需要在两个*无*符号字节之间进行减法转换才能工作,为什么
- C++:从两个符号之间提取字符串
- 在两个单独的 cpp 文件中定义的内联函数是否可以在链接期间创建重复的符号
- RLE:由两个符号编码