From 41c56264a8cc7dc6ce3545f9f0becf3dec1831d3 Mon Sep 17 00:00:00 2001 From: Peter De Wachter Date: Tue, 3 Jul 2018 22:37:35 +0200 Subject: Revert "Do not use delta-encoding across block boundaries" This reverts commit 7ed0d6f2e8d5a8192c5621898b3ff920c95b66dc. This was intended as a possible fix for problems with the HL-L2300D (#4), but it didn't help. And it worsens compression. --- src/block.h | 21 +++++++++------------ src/job.cc | 31 ++++++++++++------------------- 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/block.h b/src/block.h index 4d4ad3b..13daea1 100644 --- a/src/block.h +++ b/src/block.h @@ -24,38 +24,35 @@ class block { public: - block(): block_size_(0) { + block(): line_bytes_(0) { lines_.reserve(max_lines_per_block_); } bool empty() const { - return block_size_ == 0; - } - - bool full() const { - return lines_.size() == max_lines_per_block_; + return line_bytes_ == 0; } void add_line(std::vector &&line) { assert(!line.empty()); assert(line_fits(line.size())); - block_size_ += line.size(); + line_bytes_ += line.size(); lines_.emplace_back(line); } - bool line_fits(unsigned line_size) const { - return !full() && block_size_ + line_size < max_block_size_; + bool line_fits(unsigned size) { + return lines_.size() != max_lines_per_block_ + && line_bytes_ + size < max_block_size_; } void flush(FILE *f) { if (!empty()) { fprintf(f, "%dw%c%c", - block_size_ + 2, 0, + line_bytes_ + 2, 0, static_cast(lines_.size())); for (auto &line : lines_) { fwrite(line.data(), 1, line.size(), f); } - block_size_ = 0; + line_bytes_ = 0; lines_.clear(); } } @@ -65,7 +62,7 @@ class block { static const unsigned max_lines_per_block_ = 128; std::vector> lines_; - int block_size_; + int line_bytes_; }; #endif // BLOCK_H diff --git a/src/job.cc b/src/job.cc index 95e23cd..440fc6d 100644 --- a/src/job.cc +++ b/src/job.cc @@ -91,31 +91,24 @@ void job::encode_page(const page_params &page_params, write_page_header(); } - fputs("\033*b1030m", out_); - std::vector line(linesize); std::vector reference(linesize); block block; - for (int i = 0; i < lines && nextline(line); ++i) { - if (block.empty()) { - // Beginning of a new block, do not apply delta-encoding. - block.add_line(encode_line(line)); - } else { - // In the middle of a block, try delta-encoding. - std::vector encoded = encode_line(line, reference); - if (block.line_fits(encoded.size())) { - // Ok, there's enough room for another line. - block.add_line(std::move(encoded)); - } else { - // Oops, the line didn't fit. Start a new block. - block.flush(out_); - block.add_line(encode_line(line)); - } - } - if (block.full()) { + if (!nextline(line)) { + return; + } + block.add_line(encode_line(line)); + std::swap(line, reference); + + fputs("\033*b1030m", out_); + + for (int i = 1; i < lines && nextline(line); ++i) { + std::vector encoded = encode_line(line, reference); + if (!block.line_fits(encoded.size())) { block.flush(out_); } + block.add_line(std::move(encoded)); std::swap(line, reference); } -- cgit v1.2.3