RLE:由两个符号编码

时间:2011-10-17 06:58:05

标签: c++ run-length-encoding

我已经创建了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

然而,功能并不像我需要的那么快。这是函数的第二个版本,我已经对其进行了改进以使其更快,但它仍然太慢。你有什么想法如何提高?我缺乏想法。

1 个答案:

答案 0 :(得分:0)

根据您从文件中读取的数据大小,最好不要一次读取单个charcaters而是输入文件中的一大块数据。这可能比为每个输入字符访问磁盘上的输入文件快得多。

伪代码示例:

char dataArray[100];

while( !EOF )
{
  input.get( &dataArray[0], 100 ); // read a block of data not a single charater

  process( dataArray ); // process one line
}