Skip to content

Commit

Permalink
Fix errors resolving PNG changes (#2713) against dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Lestropie committed Jan 16, 2025
1 parent 2f62732 commit 8545aef
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
6 changes: 4 additions & 2 deletions cpp/core/file/png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,16 @@ Writer::Writer(const Header &H, const std::string &filename)
break;
case DataType::Float32:
bit_depth = 8;
multiplier = std::numeric_limits<uint8_t>::infinity();
multiplier = std::numeric_limits<uint8_t>::max();
break;
case DataType::UInt16:
case DataType::UInt32:
case DataType::UInt64:
bit_depth = 16;
break;
case DataType::Float64:
bit_depth = 16;
multiplier = std::numeric_limits<uint16_t>::infinity();
multiplier = std::numeric_limits<uint16_t>::max();
break;
}
// Detect cases where one axis has a size of 1, and hence represents the image plane
Expand Down
14 changes: 7 additions & 7 deletions cpp/core/file/png.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ class Writer {

template <typename T>
void Writer::fill(uint8_t *in_ptr, uint8_t *out_ptr, const DataType data_type, const size_t num_elements) {
auto fetch_func = __set_fetch_function<default_type>(data_type);
std::function<default_type(const void *, size_t, default_type, default_type)> fetch_func;
std::function<void(default_type, void *, size_t, default_type, default_type)> store_func;
__set_fetch_store_scale_functions<default_type>(fetch_func, store_func, data_type);
for (size_t i = 0; i != num_elements; ++i) {
Raw::store_BE<T>(std::min(default_type(std::numeric_limits<T>::max()), //
std::max(0.0, std::round(multiplier * fetch_func(in_ptr, 0)))), //
out_ptr, //
i); //
in_ptr += data_type.bytes();
out_ptr += sizeof(T);
Raw::store_BE<T>(std::min(default_type(std::numeric_limits<T>::max()), //
std::max(0.0, std::round(multiplier * fetch_func(in_ptr, i, 0.0, 1.0)))), //
out_ptr, //
i); //
}
};

Expand Down
1 change: 1 addition & 0 deletions cpp/core/image_io/png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace MR::ImageIO {
void PNG::load(const Header &header, size_t) {
DEBUG(std::string("loading PNG image") + (files.size() > 1 ? "s" : "") + " \"" + header.name() + "\"");
segsize = (header.datatype().bits() * voxel_count(header) + 7) / 8;
addresses.resize(1);
addresses[0].reset(new uint8_t[segsize]);
if (is_new) {
memset(addresses[0].get(), 0x00, segsize);
Expand Down
6 changes: 3 additions & 3 deletions testing/binaries/tests/mrconvert/format_png
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ rm -f tmp-*.png

mrconvert mrconvert/in.mif tmp-gray[].png
mrcalc mrconvert/in.mif 0 -max - | \
mrtransform - -replace identity.txt tmp-gray.mif -force
mrtransform - -replace identity.txt tmp-gray.mif -force
testing_diff_image tmp-gray[].png tmp-gray.mif

mrconvert unit_warp.mif tmp-rgb[].png -datatype uint8
mrcalc unit_warp.mif 0 -max -round - | \
mrtransform - -replace identity.txt - | \
mrconvert - -vox 1,1,1 tmp-rgb.mif -force
mrtransform - -replace identity.txt - | \
mrconvert - -vox 1,1,1 tmp-rgb.mif -force
testing_diff_image tmp-rgb[].png tmp-rgb.mif

# Now some more advanced tests:
Expand Down

0 comments on commit 8545aef

Please sign in to comment.