From 619aabaf7a1bde1bfbf065a1ae823b222b71e212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gonzalo=20Garramu=C3=B1o?= Date: Wed, 27 Sep 2023 13:45:24 -0300 Subject: [PATCH] Fixes to RAW reader. --- .../cmake/Modules/BuildLibRaw.cmake | 2 +- lib/tlIO/RAWRead.cpp | 84 ++++++++++++++++--- 2 files changed, 72 insertions(+), 14 deletions(-) diff --git a/etc/SuperBuild/cmake/Modules/BuildLibRaw.cmake b/etc/SuperBuild/cmake/Modules/BuildLibRaw.cmake index 08549cb82..3ba1e8a72 100644 --- a/etc/SuperBuild/cmake/Modules/BuildLibRaw.cmake +++ b/etc/SuperBuild/cmake/Modules/BuildLibRaw.cmake @@ -26,7 +26,7 @@ set(LibRaw_ARGS -DBUILD_SHARED_LIBS=ON -DBUILD_STATIC_LIBS=OFF -DENABLE_OPENMP=ON - -DENABLE_JASPER=OFF + -DENABLE_JASPER=ON -DENABLE_LCMS=ON -DENABLE_EXAMPLES=OFF -DBUILD_TESTING=OFF diff --git a/lib/tlIO/RAWRead.cpp b/lib/tlIO/RAWRead.cpp index 38d176fd8..3599234a4 100644 --- a/lib/tlIO/RAWRead.cpp +++ b/lib/tlIO/RAWRead.cpp @@ -199,10 +199,15 @@ namespace tl LIBRAW_ERROR(unpack, ret); } -#if 0 ret = _processor->adjust_sizes_info_only(); LIBRAW_ERROR(adjust_sizes_info_only, ret); -#endif + + const math::Size2i size(sizes.iwidth, sizes.iheight); + if (size.w > info.size.w || size.h > info.size.h) + throw std::runtime_error( + "Decoded image size bigger than original size. " + " Cannot display."); + ret = _processor->raw2image_ex(/*substract_black=*/true); LIBRAW_ERROR(raw2image_ex, ret); @@ -235,23 +240,76 @@ namespace tl "Not supported color depth"); } - if (_image->colors == 3) + if (size.w < info.size.w || size.h < info.size.h) { - memcpy(out.image->getData(), _image->data, - _image->data_size); + out.image->zero(); + uint16_t* data = reinterpret_cast( + out.image->getData()); + size_t step = 3 * sizeof(uint16_t); + size_t xoffset = (info.size.w - size.w) / 2; + size_t yoffset = (info.size.h - size.h) / 2; + + if (_image->colors == 3) + { + for (size_t y = 0; y < size.h; ++y) + { + memcpy( + data + (y + yoffset) * info.size.w * 3 + + xoffset * 3, + _image->data + y * size.w * step, + size.w * step); + } + } + else if (_image->colors == 1) + { + for (size_t y = 0; y < size.h; ++y) + { + uint16_t* dst = + data + (y + yoffset) * info.size.w * 3 + + xoffset * 3; + uint16_t* src = reinterpret_cast( + _image->data) + + y * size.w; + for (size_t x = 0; x < size.w; ++x) + { + const size_t j = y * size.w * 3 + x; + dst[j] = src[j]; + dst[j+1] = src[j+1]; + dst[j+2] = src[j+2]; + } + } + } + else + { + throw std::runtime_error( + "Unsupport color depth"); + } } - else // image->colors == 1 + else { - uint16_t* data = reinterpret_cast(out.image->getData()); - for (size_t i = 0; i < _image->data_size; ++i) + if (_image->colors == 3) + { + memcpy(out.image->getData(), _image->data, + _image->data_size); + } + else if (_image->colors == 1) { - const size_t j = i * 3; - data[j] = _image->data[i]; - data[j + 1] = _image->data[i]; - data[j + 2] = _image->data[i]; + uint16_t* data = reinterpret_cast( + out.image->getData()); + for (size_t i = 0; i < _image->data_size; ++i) + { + const size_t j = i * 3; + data[j] = _image->data[i]; + data[j + 1] = _image->data[i]; + data[j + 2] = _image->data[i]; + } + } + else + { + throw std::runtime_error( + "Unsupport color depth"); } } - _processor->dcraw_clear_mem(_image); _processor->recycle();