Skip to content

Commit

Permalink
Merge pull request google#148 from szabadka/master
Browse files Browse the repository at this point in the history
Reduce memory usage of butteraugli. Addresses google#11
  • Loading branch information
szabadka authored Mar 29, 2017
2 parents c0b5964 + 6639054 commit 5f42650
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 713 deletions.
57 changes: 16 additions & 41 deletions guetzli/butteraugli_comparator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,26 @@

namespace guetzli {

namespace {
using ::butteraugli::ConstRestrict;
using ::butteraugli::ImageF;
using ::butteraugli::CreatePlanes;
using ::butteraugli::PlanesFromPacked;
using ::butteraugli::PackedFromPlanes;

std::vector<ImageF> LinearRgb(const size_t xsize, const size_t ysize,
const std::vector<uint8_t>& rgb) {
const double* lut = Srgb8ToLinearTable();
std::vector<ImageF> planes = CreatePlanes<float>(xsize, ysize, 3);
for (int c = 0; c < 3; ++c) {
for (size_t y = 0; y < ysize; ++y) {
ConstRestrict<const uint8_t*> row_in = &rgb[3 * xsize * y];
ConstRestrict<float*> row_out = planes[c].Row(y);
for (size_t x = 0; x < xsize; ++x) {
row_out[x] = lut[row_in[3 * x + c]];
}
}
}
return planes;
}

} // namespace

ButteraugliComparator::ButteraugliComparator(const int width, const int height,
const std::vector<uint8_t>& rgb,
const float target_distance,
ProcessStats* stats)
: width_(width),
height_(height),
target_distance_(target_distance),
rgb_linear_pregamma_(3, std::vector<float>(width_ * height_)),
comparator_(width_, height_, kButteraugliStep),
distance_(0.0),
distmap_(width_, height_),
stats_(stats) {
rgb_linear_pregamma_ = LinearRgb(width, height, rgb);
const double* lut = Srgb8ToLinearTable();
for (int c = 0; c < 3; ++c) {
for (int y = 0, ix = 0; y < height_; ++y) {
for (int x = 0; x < width_; ++x, ++ix) {
rgb_linear_pregamma_[c][ix] = lut[rgb[3 * ix + c]];
}
}
}
const int block_w = (width_ + 7) / 8;
const int block_h = (height_ + 7) / 8;
const int nblocks = block_w * block_h;
Expand All @@ -72,36 +55,28 @@ ButteraugliComparator::ButteraugliComparator(const int width, const int height,
for (int ix = 0; ix < 8; ++ix, ++i) {
int x = std::min(8 * block_x + ix, width_ - 1);
int y = std::min(8 * block_y + iy, height_ - 1);
int px = y * width_ + x;
for (int c = 0; c < 3; ++c) {
ConstRestrict<const float*> row_linear =
rgb_linear_pregamma_[c].Row(y);
per_block_pregamma_[bx][c][i] = row_linear[x];
per_block_pregamma_[bx][c][i] = rgb_linear_pregamma_[c][px];
}
}
}
::butteraugli::OpsinDynamicsImage(8, 8, per_block_pregamma_[bx]);
}
}
std::vector<std::vector<float>> pregamma =
PackedFromPlanes(rgb_linear_pregamma_);
::butteraugli::OpsinDynamicsImage(width_, height_, pregamma);
rgb_linear_pregamma_ = PlanesFromPacked(width_, height_, pregamma);
::butteraugli::OpsinDynamicsImage(width_, height_, rgb_linear_pregamma_);
std::vector<std::vector<float> > dummy(3);
::butteraugli::Mask(pregamma, pregamma, width_, height_,
::butteraugli::Mask(rgb_linear_pregamma_, rgb_linear_pregamma_,
width_, height_,
&mask_xyz_, &dummy);
}

void ButteraugliComparator::Compare(const OutputImage& img) {
std::vector<std::vector<float> > rgb(3, std::vector<float>(width_ * height_));
img.ToLinearRGB(&rgb);
::butteraugli::OpsinDynamicsImage(width_, height_, rgb);
ImageF distmap;
const std::vector<ImageF> rgb_planes = PlanesFromPacked(width_, height_, rgb);
comparator_.DiffmapOpsinDynamicsImage(rgb_linear_pregamma_,
rgb_planes, distmap);
distmap_.resize(width_ * height_);
CopyToPacked(distmap, &distmap_);
distance_ = ::butteraugli::ButteraugliScoreFromDiffmap(distmap);
comparator_.DiffmapOpsinDynamicsImage(rgb_linear_pregamma_, rgb, distmap_);
distance_ = ::butteraugli::ButteraugliScoreFromDiffmap(distmap_);
GUETZLI_LOG(stats_, " BA[100.00%%] D[%6.4f]", distance_);
}

Expand Down
2 changes: 1 addition & 1 deletion guetzli/butteraugli_comparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ButteraugliComparator : public Comparator {
const int width_;
const int height_;
const float target_distance_;
std::vector<::butteraugli::ImageF> rgb_linear_pregamma_;
std::vector<std::vector<float>> rgb_linear_pregamma_;
std::vector<std::vector<float>> mask_xyz_;
std::vector<std::vector<std::vector<float>>> per_block_pregamma_;
::butteraugli::ButteraugliComparator comparator_;
Expand Down
Loading

0 comments on commit 5f42650

Please sign in to comment.