Skip to content

Commit

Permalink
Remove redundant abstract class in IO. Refactor CMakeLists.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rasim committed Feb 8, 2020
1 parent 4cad926 commit ff27028
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 80 deletions.
16 changes: 11 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
cmake_minimum_required(VERSION 3.10)
project(HDR_PLUS)

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

include("macro.cmake")
set(CMAKE_CXX_STANDARD 11)

# Define dependencies
link_halide()
link_libtiff()

find_package(PNG REQUIRED)
find_library(LIBRAW_LIBRARY
NAMES raw raw_r
)

set(src_files
src/HDRPlus.cpp
src/align.cpp
src/finish.cpp
src/merge.cpp
src/util.cpp
src/InputSource.cpp
src/Burst.cpp)

set(header_files
src/InputSource.h
src/Burst.cpp
src/Burst.h)
src/Burst.h
src/dngwriter.h)

include_directories(/usr/local/lib /usr/local/include ${HALIDE_DISTRIB_DIR}/include ${HALIDE_DISTRIB_DIR}/tools)
link_directories(/usr/local/lib /usr/local/include)

add_executable(stack_frames src/stack_frames.cpp ${src_files})
target_link_libraries(stack_frames Halide png ${LIBRAW_LIBRARY} ${TIFF_LIBRARIES})

add_executable(hdrplus ${src_files})

add_executable(hdrplus src/HDRPlus.cpp ${src_files})
target_link_libraries(hdrplus Halide png ${LIBRAW_LIBRARY})
9 changes: 9 additions & 0 deletions macro.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@ macro(link_halide)
include_directories(${HALIDE_DISTRIB_DIR}/include ${HALIDE_DISTRIB_DIR}/tools)
link_directories(${HALIDE_DISTRIB_DIR}/lib ${HALIDE_DISTRIB_DIR}/bin)
endmacro()

macro(link_libtiff)
# Link as follows:
# target_link_libraries(TARGET ${TIFF_LIBRARIES})
find_package(TIFF REQUIRED)
if (TIFF_FOUND)
include_directories(${TIFF_INCLUDE_DIRS})
endif()
endmacro()
8 changes: 4 additions & 4 deletions src/Burst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Halide::Runtime::Buffer<uint16_t> Burst::ToBuffer() const {
Halide::Runtime::Buffer<uint16_t> result(GetWidth(), GetHeight(), Raws.size());
for (int i = 0; i < Raws.size(); ++i) {
auto resultSlice = result.sliced(2, i);
Raws[i]->CopyToBuffer(resultSlice);
Raws[i].CopyToBuffer(resultSlice);
}
return result;
}
Expand All @@ -17,11 +17,11 @@ void Burst::CopyToBuffer(Halide::Runtime::Buffer<uint16_t> &buffer) const {
buffer.copy_from(ToBuffer());
}

std::vector<AbstractInputSource::SPtr> Burst::LoadRaws(const std::string &dirPath, std::vector<std::string> &inputs) {
std::vector<AbstractInputSource::SPtr> result;
std::vector<RawImage> Burst::LoadRaws(const std::string &dirPath, std::vector<std::string> &inputs) {
std::vector<RawImage> result;
for (const auto& input : inputs) {
const std::string img_path = dirPath + "/" + input;
result.emplace_back(std::make_shared<RawSource>(img_path));
result.emplace_back(img_path);
}
return result;
}
20 changes: 10 additions & 10 deletions src/Burst.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@
#include <vector>
#include <Halide.h>

class Burst : public AbstractInputSource {
class Burst {
public:
Burst(std::string dir_path, std::vector<std::string> inputs)
: Dir(std::move(dir_path))
, Inputs(std::move(inputs))
, Raws(LoadRaws(Dir, Inputs))
{}

~Burst() override = default;
~Burst() = default;

int GetWidth() const override { return Raws.empty() ? -1 : Raws[0]->GetWidth(); }
int GetWidth() const { return Raws.empty() ? -1 : Raws[0].GetWidth(); }

int GetHeight() const override { return Raws.empty() ? -1 : Raws[0]->GetHeight(); }
int GetHeight() const { return Raws.empty() ? -1 : Raws[0].GetHeight(); }

int GetBlackLevel() const override { return Raws.empty() ? -1 : Raws[0]->GetBlackLevel(); }
int GetBlackLevel() const { return Raws.empty() ? -1 : Raws[0].GetBlackLevel(); }

int GetWhiteLevel() const override { return Raws.empty() ? -1 : Raws[0]->GetWhiteLevel(); }
int GetWhiteLevel() const { return Raws.empty() ? -1 : Raws[0].GetWhiteLevel(); }

WhiteBalance GetWhiteBalance() const override { return Raws.empty() ? WhiteBalance{-1, -1, -1, -1} : Raws[0]->GetWhiteBalance(); }
WhiteBalance GetWhiteBalance() const { return Raws.empty() ? WhiteBalance{-1, -1, -1, -1} : Raws[0].GetWhiteBalance(); }

Halide::Runtime::Buffer<uint16_t> ToBuffer() const;

void CopyToBuffer(Halide::Runtime::Buffer<uint16_t>& buffer) const override;
void CopyToBuffer(Halide::Runtime::Buffer<uint16_t>& buffer) const;

private:
std::string Dir;
std::vector<std::string> Inputs;
std::vector<AbstractInputSource::SPtr> Raws;
std::vector<RawImage> Raws;

private:
static std::vector<AbstractInputSource::SPtr> LoadRaws(const std::string& dirPath, std::vector<std::string>& inputs);
static std::vector<RawImage> LoadRaws(const std::string& dirPath, std::vector<std::string>& inputs);
};
47 changes: 23 additions & 24 deletions src/HDRPlus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ using namespace std;
* processes main stages of the pipeline.
*/
class HDRPlus {

private:

const Halide::Runtime::Buffer<uint16_t> imgs;

public:


const int width;
const int height;

const BlackPoint bp;
const WhitePoint wp;
const WhiteBalance wb;
Expand All @@ -38,7 +38,7 @@ class HDRPlus {
char *readfile;
std::string readfilestring;
void imagesize();

HDRPlus(Halide::Runtime::Buffer<uint16_t> imgs, BlackPoint bp, WhitePoint wp, WhiteBalance wb, Compression c, Gain g)
: imgs(imgs)
, width(imgs.width())
Expand All @@ -52,7 +52,7 @@ class HDRPlus {
assert(imgs.dimensions() == 3); // width * height * img_idx
assert(imgs.extent(2) >= 2); // must have at least one alternate image
}

/*
* process -- Calls all of the main stages (align, merge, finish) of the pipeline.
*/
Expand All @@ -62,47 +62,47 @@ class HDRPlus {
Func alignment = align(imgsBuffer);
Func merged = merge(imgsBuffer, alignment);
Func finished = finish(merged, width, height, bp, wp, wb, c, g);

///////////////////////////////////////////////////////////////////////////
// realize image
///////////////////////////////////////////////////////////////////////////

Buffer<uint8_t> output_img(3, width, height);

finished.compile_jit();
finished.realize(output_img);

// transpose to account for interleaved layout

output_img.transpose(0, 1);
output_img.transpose(1, 2);

return output_img;
}

/*
* save_png -- Writes an interleaved Halide image to an output file.
*/
static bool save_png(std::string dir_path, std::string img_name, Buffer<uint8_t> &img) {

std::string img_path = dir_path + "/" + img_name;

std::remove(img_path.c_str());

int stride_in_bytes = img.width() * img.channels();

if(!stbi_write_png(img_path.c_str(), img.width(), img.height(), img.channels(), img.data(), stride_in_bytes)) {

std::cerr << "Unable to write output image '" << img_name << "'" << std::endl;
return false;
}

return true;
}
};


int main(int argc, char* argv[]) {

if (argc < 5) {
std::cerr << "Usage: " << argv[0] << " [-c comp -g gain (optional)] dir_path out_img raw_img1 raw_img2 [...]" << std::endl;
return 1;
Expand Down Expand Up @@ -145,13 +145,12 @@ int main(int argc, char* argv[]) {
while (i < argc) in_names.push_back(argv[i++]);

Burst burst(dir_path, in_names);

Halide::Runtime::Buffer<uint16_t> imgs = burst.ToBuffer();
if (imgs.channels() < 2) {
return EXIT_FAILURE;
}

HDRPlus hdr_plus = HDRPlus(
HDRPlus hdr_plus(
imgs,
burst.GetBlackLevel(),
burst.GetWhiteLevel(),
Expand All @@ -160,7 +159,7 @@ int main(int argc, char* argv[]) {
g);

Buffer<uint8_t> output = hdr_plus.process();

if(!HDRPlus::save_png(dir_path, out_name, output)) return -1;

return 0;
Expand Down
32 changes: 19 additions & 13 deletions src/InputSource.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
#include "InputSource.h"

RawSource::RawSource(const std::string &path)
: RawProcessor()
RawImage::RawImage(const std::string &path)
: RawProcessor(std::make_shared<LibRaw>())
{
if (int err = RawProcessor.open_file(path.c_str())) {
std::cerr << "Opening " << path << std::endl;
if (int err = RawProcessor->open_file(path.c_str())) {
std::cerr << "Cannot open file " << path << " error: " << libraw_strerror(err) << std::endl;
throw std::runtime_error("Error opening " + path);
}
if (int err = RawProcessor.unpack())
if (int err = RawProcessor->unpack())
{
std::cerr << "Cannot unpack file " << path << " error: " << libraw_strerror(err) << std::endl;
throw std::runtime_error("Error opening " + path);
}
if (int ret = RawProcessor.raw2image()) {
if (int ret = RawProcessor->raw2image()) {
std::cerr << "Cannot do raw2image on " << path << " error: " << libraw_strerror(ret) << std::endl;
throw std::runtime_error("Error opening " + path);
}
}

WhiteBalance RawSource::GetWhiteBalance() const {
const auto coeffs = RawProcessor.imgdata.color.cam_mul;
WhiteBalance RawImage::GetWhiteBalance() const {
const auto coeffs = RawProcessor->imgdata.color.cam_mul;
// Scale multipliers to green channel
const float r = coeffs[0] / coeffs[1];
const float g0 = 1.f; // same as coeffs[1] / coeffs[1];
Expand All @@ -28,13 +29,18 @@ WhiteBalance RawSource::GetWhiteBalance() const {
return WhiteBalance{r, g0, g1, b};
}

void RawSource::CopyToBuffer(Halide::Runtime::Buffer<uint16_t> &buffer) const {
const auto image_data = (uint16_t*)RawProcessor.imgdata.rawdata.raw_image;
const auto raw_width = RawProcessor.imgdata.rawdata.sizes.raw_width;
const auto raw_height = RawProcessor.imgdata.rawdata.sizes.raw_height;
const auto top = RawProcessor.imgdata.rawdata.sizes.top_margin;
const auto left = RawProcessor.imgdata.rawdata.sizes.left_margin;
void RawImage::CopyToBuffer(Halide::Runtime::Buffer<uint16_t> &buffer) const {
const auto image_data = (uint16_t*)RawProcessor->imgdata.rawdata.raw_image;
const auto raw_width = RawProcessor->imgdata.rawdata.sizes.raw_width;
const auto raw_height = RawProcessor->imgdata.rawdata.sizes.raw_height;
const auto top = RawProcessor->imgdata.rawdata.sizes.top_margin;
const auto left = RawProcessor->imgdata.rawdata.sizes.left_margin;
Halide::Runtime::Buffer<uint16_t> raw_buffer(image_data, raw_width, raw_height);
buffer.copy_from(raw_buffer.translated({-left, -top}));
}

void RawImage::WriteDng(const std::string &path, const Halide::Runtime::Buffer<uint16_t> &buffer) const
{

}

36 changes: 12 additions & 24 deletions src/InputSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,27 @@
#include <Halide.h>
#include "finish.h"

class AbstractInputSource {
class RawImage {
public:
virtual ~AbstractInputSource() = default;
virtual int GetWidth() const = 0;
virtual int GetHeight() const = 0;
virtual int GetBlackLevel() const = 0;
virtual int GetWhiteLevel() const = 0;
virtual WhiteBalance GetWhiteBalance() const = 0;
virtual void CopyToBuffer(Halide::Runtime::Buffer<uint16_t>& buffer) const = 0;

using SPtr = std::shared_ptr<AbstractInputSource>;
};
explicit RawImage(const std::string& path);

class RawSource : public AbstractInputSource {
public:
explicit RawSource(const std::string& path);
~RawImage() = default;

~RawSource() override {
RawProcessor.free_image();
}
int GetWidth() const { return RawProcessor->imgdata.rawdata.sizes.width; }

int GetWidth() const override { return RawProcessor.imgdata.rawdata.sizes.width; }
int GetHeight() const { return RawProcessor->imgdata.rawdata.sizes.height; }

int GetHeight() const override { return RawProcessor.imgdata.rawdata.sizes.height; }
int GetBlackLevel() const { return RawProcessor->imgdata.color.black; }

int GetBlackLevel() const override { return RawProcessor.imgdata.color.black; }
int GetWhiteLevel() const { return RawProcessor->imgdata.color.maximum; }

int GetWhiteLevel() const override { return RawProcessor.imgdata.color.maximum; }
WhiteBalance GetWhiteBalance() const;

WhiteBalance GetWhiteBalance() const override;
void CopyToBuffer(Halide::Runtime::Buffer<uint16_t>& buffer) const;

void CopyToBuffer(Halide::Runtime::Buffer<uint16_t>& buffer) const override;
// Writes current RawImage as DNG. If buffer was provided, then use it instead of internal buffer.
void WriteDng(const std::string& path, const Halide::Runtime::Buffer<uint16_t>& buffer = {}) const;

private:
LibRaw RawProcessor;
std::shared_ptr<LibRaw> RawProcessor;
};

0 comments on commit ff27028

Please sign in to comment.