From 5df5c1c13ca93875be9e5092309693cd029fa7c7 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Fri, 13 Oct 2017 13:52:31 +0200 Subject: [PATCH] Correct rendering of cat images. --- src/PNGDumper.cpp | 15 ++++++++------- src/utils.hpp | 8 ++++---- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/PNGDumper.cpp b/src/PNGDumper.cpp index 92fb330..144df8e 100644 --- a/src/PNGDumper.cpp +++ b/src/PNGDumper.cpp @@ -47,17 +47,18 @@ void PNGDumper::dump(const LayerData &layerData) void PNGDumper::dumpImageSeries(const LayerData &layer) { const auto& shape = layer.shape(); - const auto imagePixels = shape[2] * shape[3]; + const auto images = shape[0], channels = shape[1], height = shape[2], width = shape[3]; + const auto imagePixels = width * height; // Buffer for storing the current image data. vector buffer(imagePixels); auto data = layer.data(); - png::image image(shape[2], shape[3]); + png::image image(width, height); - for (int i = 0; i < shape[0]; ++i) { - for (int j = 0; j < shape[1]; ++j) { + for (int i = 0; i < images; ++i) { + for (int j = 0; j < channels; ++j) { memcpy(buffer.data(), data, imagePixels * sizeof(DType)); // advance the buffer; @@ -65,9 +66,9 @@ void PNGDumper::dumpImageSeries(const LayerData &layer) clamp(buffer.begin(), buffer.end(), 0.0, 255.0); - for (int y = 0; y < shape[2]; ++y) { - for (int x = 0; x < shape[3]; ++x) { - image[x][y] = png::gray_pixel((int) buffer[x + y * shape[3]]); + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x) { + image[y][x] = png::gray_pixel((int) buffer[x + y * width]); } } diff --git a/src/utils.hpp b/src/utils.hpp index 97549c5..5ae628b 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -91,11 +91,11 @@ namespace fmri typename std::iterator_traits::value_type minimum, typename std::iterator_traits::value_type maximum) { - const auto extremes = std::minmax(begin, end); - const auto diff = *extremes.second - *extremes.first; + const auto[minElem, maxElem] = std::minmax(begin, end); + const auto diff = *maxElem - *minElem; - const auto offset = minimum - *extremes.first; - const auto scaling = diff / (maximum - minimum); + const auto offset = minimum - *minElem; + const auto scaling = (maximum - minimum) / diff; std::for_each(begin, end, [offset, scaling] (typename std::iterator_traits::reference v) { v = (v + offset) * scaling;}); }