You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.7 KiB
C++

#include <string>
3 years ago
#include <cstdio>
3 years ago
#include <memory> // std::shared_ptr
3 years ago
#include <stdexcept> // std::runtime_error
3 years ago
#include <opencv2/opencv.hpp> // all opencv header
#include <libraw/libraw.h>
3 years ago
#include "hdrplus/bayer_image.h"
3 years ago
#include "hdrplus/utility.h" // box_filter_2x2
3 years ago
namespace hdrplus
{
bayer_image::bayer_image( const std::string& bayer_image_path )
{
3 years ago
libraw_processor = std::make_shared<LibRaw>();
// Open RAW image file
int return_code;
3 years ago
if ( ( return_code = libraw_processor->open_file( bayer_image_path.c_str() ) ) != LIBRAW_SUCCESS )
{
3 years ago
libraw_processor->recycle();
throw std::runtime_error("Error opening file " + bayer_image_path + libraw_strerror( return_code ));
}
// Unpack the raw image
3 years ago
if ( ( return_code = libraw_processor->unpack() ) != LIBRAW_SUCCESS )
{
throw std::runtime_error("Error unpack file " + bayer_image_path + libraw_strerror( return_code ));
}
// Get image basic info
3 years ago
width = int( libraw_processor->imgdata.rawdata.sizes.raw_width );
height = int( libraw_processor->imgdata.rawdata.sizes.raw_height );
white_level = int( libraw_processor->imgdata.rawdata.color.maximum );
3 years ago
#ifndef NDEBUG
3 years ago
printf("%s::%s read bayer image %s with width %zu height %zu\n", \
3 years ago
__FILE__, __func__, bayer_image_path.c_str(), width, height );
3 years ago
fflush( stdout );
3 years ago
#endif
// Create CV mat
// https://answers.opencv.org/question/105972/de-bayering-a-cr2-image/
3 years ago
// https://www.libraw.org/node/2141
3 years ago
raw_image = cv::Mat( width, height, CV_16U, libraw_processor->imgdata.rawdata.raw_image ).clone();
3 years ago
// 2x2 box filter
grayscale_image = box_filter_2x2<uint16_t>( raw_image );
}
}