From c666c7e2b40492cd1190a8416d5284a59ac5ef09 Mon Sep 17 00:00:00 2001 From: Alexander Brock Date: Thu, 20 Feb 2020 23:23:19 +0100 Subject: [PATCH 1/3] Start developing tiff2dng converter --- CMakeLists.txt | 38 ++++++++++++++++++++++++++++++++++- src/CFAPattern.hpp | 7 +++++++ tiff2dng.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tiff2dng.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9330805..2b14767 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,15 @@ set(CMAKE_AUTORCC ON) find_package(Qt5 5.6 REQUIRED Core Widgets) +message(STATUS "QT_DEFINITIONS: ${QT_DEFINITIONS}") + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC ") + +include_directories(/usr/include/x86_64-linux-gnu/qt5/QtCore) +include_directories(/usr/include/x86_64-linux-gnu/qt5/QtGui) +include_directories(/usr/include/x86_64-linux-gnu/qt5) + + #include(${QT_USE_FILE}) add_definitions(${QT_DEFINITIONS}) @@ -274,7 +283,7 @@ else() ) endif() -target_link_libraries(hdrmerge ${STRIP} ${hdrmerge_libs} Qt5::Widgets) + if(WIN32) # Compile a target without GUI, for the .com executable @@ -354,3 +363,30 @@ if(Boost_FOUND) add_subdirectory(test) endif() +add_executable(tiff2dng tiff2dng.cpp) + +find_package(OpenCV REQUIRED) + +include_directories(${OpenCV_INCLUDE_DIRS}) + +find_package(ZLIB REQUIRED) + +include_directories(${ZLIB_INCLUDE_DIRS}) + +add_library(hdrmerge_lib + ${hdrmerge_sources} + ) + +target_link_libraries(hdrmerge_lib + ${hdrmerge_libs} Qt5::Widgets Qt5::Core + ) + +target_link_libraries(tiff2dng + Qt5::Widgets + ${hdrmerge_libs} + ${OpenCV_LIBS} + hdrmerge_lib + ${ZLIB_LIBRARIES} + ) + +target_link_libraries(hdrmerge ${STRIP} ${hdrmerge_libs} Qt5::Widgets) diff --git a/src/CFAPattern.hpp b/src/CFAPattern.hpp index ab36510..7d8c970 100644 --- a/src/CFAPattern.hpp +++ b/src/CFAPattern.hpp @@ -41,6 +41,13 @@ class CFAPattern { } } + void setPattern(uint32_t f) { + filters = f; + if (filters == 9) { + throw std::runtime_error("need fcol function for Fujifilm X-Trans sensor"); + } + } + bool operator==(const CFAPattern & r) const { return filters == r.filters; } diff --git a/tiff2dng.cpp b/tiff2dng.cpp new file mode 100644 index 0000000..37f4d95 --- /dev/null +++ b/tiff2dng.cpp @@ -0,0 +1,50 @@ +#include + +#include "src/DngFloatWriter.hpp" +#include "src/ImageIO.hpp" +#include +#include + + +void convert(std::string filename) { + std::cout << "reading input file " << filename << std::endl; + cv::Mat_ im = cv::imread(filename, cv::IMREAD_UNCHANGED); + int const width = im.cols; + int const height = im.rows; + std::cout << "size: " << im.size() << std::endl; + hdrmerge::Array2D arr(width, height); + std::cout << "hdrmerge::Array2D size: " << arr.size() << std::endl; + for (int yy = 0; yy < height; ++yy) { + for (int xx = 0; xx < width; ++xx) { + arr(xx, yy) = double(im(yy, xx)); + } + } + cv::Mat preview; + cv::cvtColor(im, preview, cv::COLOR_BayerBG2BGR); + cv::imwrite(filename + "-preview.tif", preview); + hdrmerge::DngFloatWriter writer; + writer.setPreviewWidth(100); // This is important, otherwise + hdrmerge::RawParameters params; + params.height = params.rawHeight = height; + params.width = params.rawWidth = width; + params.fileName = filename.c_str(); + params.isoSpeed = 100; + params.shutter = 100; + params.aperture = 2.8; + //params.FC.setPattern(2); + + std::cout << "Writing result" << std::endl; + writer.write(std::move(arr), params, (filename + ".dng").c_str()); + std::cout << "done." << std::endl; +} + +int main(int argc, char ** argv) { + if (argc < 2) { + std::cout << "Usage: " << argv[0] << " input files..." << std::endl; + } + + + for (int ii = 1; ii < argc; ++ii) { + convert(argv[ii]); + } +} From 83d210db885af313f1dbd91f7880fa8554081bdf Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 21 Feb 2020 00:49:25 +0100 Subject: [PATCH 2/3] Fix constants --- tiff2dng.cpp | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/tiff2dng.cpp b/tiff2dng.cpp index 37f4d95..71b0c4d 100644 --- a/tiff2dng.cpp +++ b/tiff2dng.cpp @@ -9,6 +9,10 @@ void convert(std::string filename) { std::cout << "reading input file " << filename << std::endl; cv::Mat_ im = cv::imread(filename, cv::IMREAD_UNCHANGED); + double minval = 0; + double maxval = 0; + cv::minMaxIdx(im, &minval, &maxval); + im *= 65535.0/maxval; int const width = im.cols; int const height = im.rows; std::cout << "size: " << im.size() << std::endl; @@ -21,17 +25,31 @@ void convert(std::string filename) { } cv::Mat preview; cv::cvtColor(im, preview, cv::COLOR_BayerBG2BGR); - cv::imwrite(filename + "-preview.tif", preview); - hdrmerge::DngFloatWriter writer; - writer.setPreviewWidth(100); // This is important, otherwise - hdrmerge::RawParameters params; + preview.convertTo(preview, CV_8UC3, 1.0/256); + cv::resize(preview, preview, cv::Size(), .5, .5); + //cv::imwrite(filename + "-preview.tif", preview); + cv::cvtColor(preview, preview, cv::COLOR_BGR2RGB); + hdrmerge::RawParameters params((filename + ".dng").c_str()); params.height = params.rawHeight = height; params.width = params.rawWidth = width; params.fileName = filename.c_str(); params.isoSpeed = 100; - params.shutter = 100; + params.shutter = .1; params.aperture = 2.8; - //params.FC.setPattern(2); + params.colors = 3; + params.black = 0; + + + //QImage q_preview = hdrmerge::ImageIO::renderPreview(arr, params, 1.0); + QImage q_preview = QImage(preview.data, preview.size().width, preview.size().height, QImage::Format_RGB888); + std::cout << "q_preview width / height: " << q_preview.width() << " / " << q_preview.height() << std::endl; + hdrmerge::DngFloatWriter writer; + writer.setBitsPerSample(16); + writer.setPreviewWidth(q_preview.width()); + writer.setPreview(q_preview); + + + params.FC.setPattern(3031741620); std::cout << "Writing result" << std::endl; writer.write(std::move(arr), params, (filename + ".dng").c_str()); @@ -43,7 +61,6 @@ int main(int argc, char ** argv) { std::cout << "Usage: " << argv[0] << " input files..." << std::endl; } - for (int ii = 1; ii < argc; ++ii) { convert(argv[ii]); } From 5033a632ca105f810e3f33e9fcd7ad60c887c4ef Mon Sep 17 00:00:00 2001 From: Alexander Brock Date: Tue, 8 Aug 2023 13:26:36 +0200 Subject: [PATCH 3/3] Fix orientation bug --- tiff2dng.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tiff2dng.cpp b/tiff2dng.cpp index 71b0c4d..75f8291 100644 --- a/tiff2dng.cpp +++ b/tiff2dng.cpp @@ -38,6 +38,14 @@ void convert(std::string filename) { params.aperture = 2.8; params.colors = 3; params.black = 0; + params.flip = 0; + switch (params.flip) { + case 0: params.tiffOrientation = 1; break; // 0° + case 3: params.tiffOrientation = 3; break; // 180° + case 5: params.tiffOrientation = 8; break; // + case 6: params.tiffOrientation = 6; break; // 270° + default: params.tiffOrientation = 9; break; // 90° + } //QImage q_preview = hdrmerge::ImageIO::renderPreview(arr, params, 1.0);