1
0
mirror of https://github.com/pdewacht/brlaser synced 2024-12-27 07:48:21 +01:00

Split out block.h

This commit is contained in:
Peter De Wachter 2014-02-04 21:50:11 +01:00
parent cd0e4102b4
commit bb1ba17b4f
3 changed files with 68 additions and 45 deletions

View File

@ -17,6 +17,7 @@ rastertobrlaser_SOURCES = \
src/debug.cc \
src/job.h \
src/job.cc \
src/block.h \
src/line.h \
src/line.cc
rastertobrlaser_CPPFLAGS = $(CUPS_CFLAGS)

63
src/block.h Normal file
View File

@ -0,0 +1,63 @@
// This file is part of the brlaser printer driver.
//
// Copyright 2013 Peter De Wachter
//
// brlaser is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// brlaser is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with brlaser. If not, see <http://www.gnu.org/licenses/>.
#ifndef BLOCK_H
#define BLOCK_H
#include <assert.h>
#include <stdio.h>
#include <vector>
class block {
public:
block(): line_bytes_(0) {
}
void add_line(std::vector<uint8_t> &&line) {
assert(!line.empty());
assert(line_fits(line.size()));
line_bytes_ += line.size();
lines_.push_back(line);
}
bool line_fits(unsigned size) {
return lines_.size() != max_lines_per_block_
&& line_bytes_ + size < max_block_size_;
}
void flush(FILE *f) {
if (line_bytes_) {
fprintf(f, "%dw%c%c",
line_bytes_ + 2, 0,
static_cast<int>(lines_.size()));
for (auto &line : lines_) {
fwrite(line.data(), 1, line.size(), f);
}
line_bytes_ = 0;
lines_.clear();
}
}
private:
static const unsigned max_block_size_ = 16350;
static const unsigned max_lines_per_block_ = 128;
std::vector<std::vector<uint8_t>> lines_;
int line_bytes_;
};
#endif // BLOCK_H

View File

@ -20,49 +20,8 @@
#include <algorithm>
#include <vector>
#include "line.h"
#include "block.h"
using std::vector;
namespace {
class block {
public:
block(): line_bytes_(0) {
}
void add_line(vector<uint8_t> &&line) {
assert(!line.empty());
line_bytes_ += line.size();
lines_.push_back(line);
}
bool line_fits(unsigned size) {
return lines_.size() != max_lines_per_block_
&& line_bytes_ + size < max_block_size_;
}
void flush(FILE *f) {
if (line_bytes_) {
fprintf(f, "%dw%c%c",
line_bytes_ + 2, 0,
static_cast<int>(lines_.size()));
for (auto &line : lines_) {
fwrite(line.data(), 1, line.size(), f);
}
line_bytes_ = 0;
lines_.clear();
}
}
private:
static const unsigned max_block_size_ = 16350;
static const unsigned max_lines_per_block_ = 128;
vector<vector<uint8_t>> lines_;
int line_bytes_;
};
} // namespace
job::job(FILE *out, const std::string &job_name)
: out_(out),
@ -126,8 +85,8 @@ void job::encode_page(const page_params &page_params,
write_page_header();
}
vector<uint8_t> line(linesize);
vector<uint8_t> reference(linesize);
std::vector<uint8_t> line(linesize);
std::vector<uint8_t> reference(linesize);
block block;
if (!nextline(line.data())) {
@ -141,7 +100,7 @@ void job::encode_page(const page_params &page_params,
fputs("\033*b1030m", out_);
for (int i = 1; i < lines && nextline(line.data()); ++i) {
vector<uint8_t> encoded = encode_line(line, reference);
std::vector<uint8_t> encoded = encode_line(line, reference);
if (!block.line_fits(encoded.size())) {
block.flush(out_);
}