RLE:用两个符号进行编码

发布于 2024-12-10 07:08:17 字数 1894 浏览 2 评论 0原文

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

然而,功能并没有我需要的那么快。这是函数的第二个版本,我已经改进了它以使其更快,但它仍然太慢。您有什么改进的想法吗?我缺乏想法。

I've created RLE encoding function, which encodes sequences like "A1A1B7B7B7B7" to such strings: "#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

However, function is not as fast as I need. This is the second version of function, I've already improved it to make it faster, but it is still too slow. Do you have any ideas how to improve? I'm lack of ideas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空‖城人不在 2024-12-17 07:08:17

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

伪代码示例:

char dataArray[100];

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

  process( dataArray ); // process one line
}

Depending on the size of data you are reading from files it might be a good idea not to read single charcaters but a chunk of data from your input file at once. This might be a lot faster than accessing the input file on the disk for each input character.

Pseudo code example:

char dataArray[100];

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

  process( dataArray ); // process one line
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文