This source file includes following definitions.
- FlipIfNeeded
- encodePixelFromRGBA
- encodePixelFromBGRA
- EncodeDocumentHeader
- EncodePageHeader
- EncodeRowFrom32Bit
- GetRow
- EncodePage
#include "chrome/utility/cloud_print/pwg_encoder.h"
#include <algorithm>
#include "base/big_endian.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/utility/cloud_print/bitmap_image.h"
namespace cloud_print {
namespace {
const uint32 kBitsPerColor = 8;
const uint32 kColorSpace = 19;
const uint32 kColorOrder = 0;
const uint32 kNumColors = 3;
const uint32 kBitsPerPixel = kNumColors * kBitsPerColor;
const char kPwgKeyword[] = "RaS2";
const uint32 kHeaderSize = 1796;
const uint32 kHeaderHwResolutionHorizontal = 276;
const uint32 kHeaderHwResolutionVertical = 280;
const uint32 kHeaderCupsWidth = 372;
const uint32 kHeaderCupsHeight = 376;
const uint32 kHeaderCupsBitsPerColor = 384;
const uint32 kHeaderCupsBitsPerPixel = 388;
const uint32 kHeaderCupsBytesPerLine = 392;
const uint32 kHeaderCupsColorOrder = 396;
const uint32 kHeaderCupsColorSpace = 400;
const uint32 kHeaderCupsNumColors = 420;
const uint32 kHeaderPwgTotalPageCount = 452;
const int kPwgMaxPackedRows = 256;
const int kPwgMaxPackedPixels = 128;
inline int FlipIfNeeded(bool flip, int current, int total) {
return flip ? total - current : current;
}
}
PwgEncoder::PwgEncoder() {}
inline void encodePixelFromRGBA(const uint8* pixel, std::string* output) {
output->push_back(static_cast<char>(pixel[0]));
output->push_back(static_cast<char>(pixel[1]));
output->push_back(static_cast<char>(pixel[2]));
}
inline void encodePixelFromBGRA(const uint8* pixel, std::string* output) {
output->push_back(static_cast<char>(pixel[2]));
output->push_back(static_cast<char>(pixel[1]));
output->push_back(static_cast<char>(pixel[0]));
}
void PwgEncoder::EncodeDocumentHeader(std::string* output) const {
output->clear();
output->append(kPwgKeyword, 4);
}
void PwgEncoder::EncodePageHeader(const BitmapImage& image, const uint32 dpi,
const uint32 total_pages,
std::string* output) const {
char header[kHeaderSize];
memset(header, 0, kHeaderSize);
base::WriteBigEndian<uint32>(header + kHeaderHwResolutionHorizontal, dpi);
base::WriteBigEndian<uint32>(header + kHeaderHwResolutionVertical, dpi);
base::WriteBigEndian<uint32>(header + kHeaderCupsWidth, image.size().width());
base::WriteBigEndian<uint32>(header + kHeaderCupsHeight,
image.size().height());
base::WriteBigEndian<uint32>(header + kHeaderCupsBitsPerColor, kBitsPerColor);
base::WriteBigEndian<uint32>(header + kHeaderCupsBitsPerPixel, kBitsPerPixel);
base::WriteBigEndian<uint32>(header + kHeaderCupsBytesPerLine,
(kBitsPerPixel * image.size().width() + 7) / 8);
base::WriteBigEndian<uint32>(header + kHeaderCupsColorOrder, kColorOrder);
base::WriteBigEndian<uint32>(header + kHeaderCupsColorSpace, kColorSpace);
base::WriteBigEndian<uint32>(header + kHeaderCupsNumColors, kNumColors);
base::WriteBigEndian<uint32>(header + kHeaderPwgTotalPageCount, total_pages);
output->append(header, kHeaderSize);
}
bool PwgEncoder::EncodeRowFrom32Bit(const uint8* row, const int width,
const int color_space,
std::string* output) const {
void (*pixel_encoder)(const uint8*, std::string*);
switch (color_space) {
case BitmapImage::RGBA:
pixel_encoder = &encodePixelFromRGBA;
break;
case BitmapImage::BGRA:
pixel_encoder = &encodePixelFromBGRA;
break;
default:
LOG(ERROR) << "Unsupported colorspace.";
return false;
}
const uint32* pos = reinterpret_cast<const uint32*>(row);
const uint32* row_end = pos + width;
while (pos != row_end) {
const uint32* it = pos + 1;
const uint32* end = std::min(pos + kPwgMaxPackedPixels, row_end);
while (it != end && *pos == *it) {
it++;
}
if (it != pos + 1) {
output->push_back(static_cast<char>((it - pos) - 1));
pixel_encoder(reinterpret_cast<const uint8*>(pos), output);
pos = it;
} else {
while (it != end && *it != *(it - 1)) {
it++;
}
if (it != row_end && *it == *(it - 1)) {
it--;
}
output->push_back(static_cast<char>(1 - (it - pos)));
while (pos != it) {
pixel_encoder(reinterpret_cast<const uint8*>(pos++), output);
}
}
}
return true;
}
inline const uint8* PwgEncoder::GetRow(const BitmapImage& image,
int row) const {
return image.pixel_data() + row * image.size().width() * image.channels();
}
bool PwgEncoder::EncodePage(const BitmapImage& image,
const uint32 dpi,
const uint32 total_pages,
std::string* output,
bool rotate) const {
if (image.channels() != 4) {
LOG(ERROR) << "Unsupported colorspace.";
return false;
}
EncodePageHeader(image, dpi, total_pages, output);
int row_size = image.size().width() * image.channels();
scoped_ptr<uint8[]> current_row_cpy(new uint8[row_size]);
int row_number = 0;
int total_rows = image.size().height();
while (row_number < total_rows) {
const uint8* current_row =
GetRow(image, FlipIfNeeded(rotate, row_number++, total_rows));
int num_identical_rows = 1;
while (num_identical_rows < kPwgMaxPackedRows &&
row_number < image.size().height() &&
!memcmp(current_row,
GetRow(image, FlipIfNeeded(rotate, row_number, total_rows)),
row_size)) {
num_identical_rows++;
row_number++;
}
output->push_back(static_cast<char>(num_identical_rows - 1));
if (rotate) {
memcpy(current_row_cpy.get(), current_row, row_size);
std::reverse(reinterpret_cast<uint32*>(current_row_cpy.get()),
reinterpret_cast<uint32*>(current_row_cpy.get() + row_size));
current_row = current_row_cpy.get();
}
if (!EncodeRowFrom32Bit(
current_row, image.size().width(), image.colorspace(), output)) {
return false;
}
}
return true;
}
}