Skip to content

Commit

Permalink
[image] Add support for libraw raw image loading. Tested with Sony A7…
Browse files Browse the repository at this point in the history
…S and Sony A7RIII
  • Loading branch information
stevenlovegrove committed Mar 19, 2022
1 parent 85a98c2 commit b9f1068
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
41 changes: 41 additions & 0 deletions cmake/FindLibraw.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# - Try to find libraw
#
# libraw_FOUND - system has libraw
# libraw_INCLUDE_DIRS - the libraw include directories
# libraw_LIBRARIES - link these to use libraw

FIND_PATH(
libraw_INCLUDE_DIRS
NAMES libraw/libraw.h
PATHS
${LIBRAW_DIR}
${LIBRAW_DIR}/include
/usr/include/
/usr/local/include
/opt/local/include
)

FIND_LIBRARY(
libraw_LIBRARIES
NAMES raw_r
PATHS
${LIBRAW_DIR}
${LIBRAW_DIR}/lib
/usr/lib
/usr/local/lib
/opt/local/lib
)

IF (libraw_INCLUDE_DIRS AND libraw_LIBRARIES)
SET(libraw_FOUND TRUE)
ENDIF (libraw_INCLUDE_DIRS AND libraw_LIBRARIES)

IF (libraw_FOUND)
IF (NOT libraw_FIND_QUIETLY)
MESSAGE(STATUS "Found libraw: ${libraw_LIBRARIES}")
ENDIF (NOT libraw_FIND_QUIETLY)
ELSE (libraw_FOUND)
IF (libraw_FIND_REQUIRED)
MESSAGE(FATAL_ERROR "Could not find libraw")
ENDIF (libraw_FIND_REQUIRED)
ENDIF (libraw_FOUND)
12 changes: 12 additions & 0 deletions components/pango_image/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ if(BUILD_PANGOLIN_ZSTD)
endif()
endif()

option(BUILD_PANGOLIN_LIBRAW "Build support for raw images (libraw)" ON)
if(BUILD_PANGOLIN_LIBRAW)
find_package(libraw QUIET)
if(libraw_FOUND)
target_compile_definitions(${COMPONENT} PRIVATE HAVE_LIBRAW)
target_include_directories(${COMPONENT} PRIVATE ${libraw_INCLUDE_DIR} )
target_link_libraries(${COMPONENT} PRIVATE ${libraw_LIBRARIES})
message(STATUS "libraw Found and Enabled")
endif()
endif()

target_sources( ${COMPONENT}
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/src/pixel_format.cpp
Expand All @@ -87,6 +98,7 @@ PRIVATE
${CMAKE_CURRENT_LIST_DIR}/src/image_io_tga.cpp
${CMAKE_CURRENT_LIST_DIR}/src/image_io_bmp.cpp
${CMAKE_CURRENT_LIST_DIR}/src/image_io_zstd.cpp
${CMAKE_CURRENT_LIST_DIR}/src/image_io_libraw.cpp
)

target_link_libraries(${COMPONENT} PUBLIC pango_core)
Expand Down
7 changes: 7 additions & 0 deletions components/pango_image/src/image_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ void SaveLz4(const Image<unsigned char>& image, const pangolin::PixelFormat& fmt
TypedImage LoadPacked12bit(std::istream& in);
void SavePacked12bit(const Image<unsigned char>& image, const pangolin::PixelFormat& fmt, std::ostream& out);

// LibRaw raw camera files
TypedImage LoadLibRaw(const std::string& filename);

TypedImage LoadImage(std::istream& in, ImageFileType file_type)
{
switch (file_type) {
Expand Down Expand Up @@ -114,6 +117,10 @@ TypedImage LoadImage(const std::string& filename, ImageFileType file_type)
}
case ImageFileTypePango:
return LoadPango(filename);
case ImageFileTypeArw:
[[fallthrough]];
case ImageFileTypeTiff:
return LoadLibRaw(filename);
default:
throw std::runtime_error("Unsupported image file type, '" + filename + "'");
}
Expand Down
48 changes: 48 additions & 0 deletions components/pango_image/src/image_io_libraw.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <fstream>
#include <pangolin/image/typed_image.h>

#ifdef HAVE_LIBRAW
# include <libraw/libraw.h>
#endif

namespace pangolin {

TypedImage LoadLibRaw(
const std::string& filename
) {
#ifdef HAVE_LIBRAW
static LibRaw RawProcessor;

int ret;

if ((ret = RawProcessor.open_file(filename.c_str())) != LIBRAW_SUCCESS)
{
throw std::runtime_error(libraw_strerror(ret));
}

if ((ret = RawProcessor.unpack()) != LIBRAW_SUCCESS)
{
throw std::runtime_error(libraw_strerror(ret));
}

const auto& S = RawProcessor.imgdata.sizes;
TypedImage image(S.width, S.height, PixelFormatFromString("GRAY16LE"), sizeof(uint16_t) * S.raw_width);
PitchedCopy((char*)image.ptr, image.pitch, (char*)RawProcessor.imgdata.rawdata.raw_image, sizeof(uint16_t) * S.raw_width, sizeof(uint16_t) * image.w, image.h);

// TODO: Support image metadata so that we can extract these fields.
// RawProcessor.imgdata.other.iso_speed
// RawProcessor.imgdata.other.aperture
// RawProcessor.imgdata.other.focal_len
// RawProcessor.imgdata.other.shutter
// RawProcessor.imgdata.other.timestamp
// RawProcessor.imgdata.makernotes.common.exifCameraElevationAngle
// RawProcessor.imgdata.makernotes.sony.SonyDateTime

return image;
#else
PANGOLIN_UNUSED(filename);
throw std::runtime_error("Rebuild Pangolin for libraw support.");
#endif
}

}

0 comments on commit b9f1068

Please sign in to comment.