Skip to content

Commit 9fb2cef

Browse files
[image] Add support for libraw raw image loading. Tested with Sony A7S and Sony A7RIII
1 parent 85a98c2 commit 9fb2cef

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

cmake/FindLibraw.cmake

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# - Try to find libraw
2+
#
3+
# libraw_FOUND - system has libraw
4+
# libraw_INCLUDE_DIRS - the libraw include directories
5+
# libraw_LIBRARIES - link these to use libraw
6+
7+
FIND_PATH(
8+
libraw_INCLUDE_DIRS
9+
NAMES libraw/libraw.h
10+
PATHS
11+
${LIBRAW_DIR}
12+
${LIBRAW_DIR}/include
13+
/usr/include/
14+
/usr/local/include
15+
/opt/local/include
16+
)
17+
18+
FIND_LIBRARY(
19+
libraw_LIBRARIES
20+
NAMES raw_r
21+
PATHS
22+
${LIBRAW_DIR}
23+
${LIBRAW_DIR}/lib
24+
/usr/lib
25+
/usr/local/lib
26+
/opt/local/lib
27+
)
28+
29+
IF (libraw_INCLUDE_DIRS AND libraw_LIBRARIES)
30+
SET(libraw_FOUND TRUE)
31+
ENDIF (libraw_INCLUDE_DIRS AND libraw_LIBRARIES)
32+
33+
IF (libraw_FOUND)
34+
IF (NOT libraw_FIND_QUIETLY)
35+
MESSAGE(STATUS "Found libraw: ${libraw_LIBRARIES}")
36+
ENDIF (NOT libraw_FIND_QUIETLY)
37+
ELSE (libraw_FOUND)
38+
IF (libraw_FIND_REQUIRED)
39+
MESSAGE(FATAL_ERROR "Could not find libraw")
40+
ENDIF (libraw_FIND_REQUIRED)
41+
ENDIF (libraw_FOUND)

components/pango_image/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ if(BUILD_PANGOLIN_ZSTD)
7272
endif()
7373
endif()
7474

75+
option(BUILD_PANGOLIN_LIBRAW "Build support for raw images (libraw)" ON)
76+
if(BUILD_PANGOLIN_LIBRAW)
77+
find_package(libraw QUIET)
78+
if(libraw_FOUND)
79+
target_compile_definitions(${COMPONENT} PRIVATE HAVE_LIBRAW)
80+
target_include_directories(${COMPONENT} PRIVATE ${libraw_INCLUDE_DIR} )
81+
target_link_libraries(${COMPONENT} PRIVATE ${libraw_LIBRARIES})
82+
message(STATUS "libraw Found and Enabled")
83+
endif()
84+
endif()
85+
7586
target_sources( ${COMPONENT}
7687
PRIVATE
7788
${CMAKE_CURRENT_LIST_DIR}/src/pixel_format.cpp
@@ -87,6 +98,7 @@ PRIVATE
8798
${CMAKE_CURRENT_LIST_DIR}/src/image_io_tga.cpp
8899
${CMAKE_CURRENT_LIST_DIR}/src/image_io_bmp.cpp
89100
${CMAKE_CURRENT_LIST_DIR}/src/image_io_zstd.cpp
101+
${CMAKE_CURRENT_LIST_DIR}/src/image_io_libraw.cpp
90102
)
91103

92104
target_link_libraries(${COMPONENT} PUBLIC pango_core)

components/pango_image/src/image_io.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ void SaveLz4(const Image<unsigned char>& image, const pangolin::PixelFormat& fmt
7070
TypedImage LoadPacked12bit(std::istream& in);
7171
void SavePacked12bit(const Image<unsigned char>& image, const pangolin::PixelFormat& fmt, std::ostream& out);
7272

73+
// LibRaw raw camera files
74+
TypedImage LoadLibRaw(const std::string& filename);
75+
7376
TypedImage LoadImage(std::istream& in, ImageFileType file_type)
7477
{
7578
switch (file_type) {
@@ -114,6 +117,10 @@ TypedImage LoadImage(const std::string& filename, ImageFileType file_type)
114117
}
115118
case ImageFileTypePango:
116119
return LoadPango(filename);
120+
case ImageFileTypeArw:
121+
[[fallthrough]];
122+
case ImageFileTypeTiff:
123+
return LoadLibRaw(filename);
117124
default:
118125
throw std::runtime_error("Unsupported image file type, '" + filename + "'");
119126
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <fstream>
2+
#include <libraw/libraw.h>
3+
#include <pangolin/image/typed_image.h>
4+
5+
namespace pangolin {
6+
7+
TypedImage LoadLibRaw(
8+
const std::string& filename
9+
) {
10+
#ifdef HAVE_LIBRAW
11+
static LibRaw RawProcessor;
12+
13+
int ret;
14+
15+
if ((ret = RawProcessor.open_file(filename.c_str())) != LIBRAW_SUCCESS)
16+
{
17+
throw std::runtime_error(libraw_strerror(ret));
18+
}
19+
20+
if ((ret = RawProcessor.unpack()) != LIBRAW_SUCCESS)
21+
{
22+
throw std::runtime_error(libraw_strerror(ret));
23+
}
24+
25+
const auto& S = RawProcessor.imgdata.sizes;
26+
TypedImage image(S.width, S.height, PixelFormatFromString("GRAY16LE"), sizeof(uint16_t) * S.raw_width);
27+
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);
28+
29+
// TODO: Support image metadata so that we can extract these fields.
30+
// RawProcessor.imgdata.other.iso_speed
31+
// RawProcessor.imgdata.other.aperture
32+
// RawProcessor.imgdata.other.focal_len
33+
// RawProcessor.imgdata.other.shutter
34+
// RawProcessor.imgdata.other.timestamp
35+
// RawProcessor.imgdata.makernotes.common.exifCameraElevationAngle
36+
// RawProcessor.imgdata.makernotes.sony.SonyDateTime
37+
38+
return image;
39+
#else
40+
PANGOLIN_UNUSED(filename);
41+
throw std::runtime_error("Rebuild Pangolin for libraw support.");
42+
#endif
43+
}
44+
45+
}

0 commit comments

Comments
 (0)