Skip to content

Commit b9f1068

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

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <fstream>
2+
#include <pangolin/image/typed_image.h>
3+
4+
#ifdef HAVE_LIBRAW
5+
# include <libraw/libraw.h>
6+
#endif
7+
8+
namespace pangolin {
9+
10+
TypedImage LoadLibRaw(
11+
const std::string& filename
12+
) {
13+
#ifdef HAVE_LIBRAW
14+
static LibRaw RawProcessor;
15+
16+
int ret;
17+
18+
if ((ret = RawProcessor.open_file(filename.c_str())) != LIBRAW_SUCCESS)
19+
{
20+
throw std::runtime_error(libraw_strerror(ret));
21+
}
22+
23+
if ((ret = RawProcessor.unpack()) != LIBRAW_SUCCESS)
24+
{
25+
throw std::runtime_error(libraw_strerror(ret));
26+
}
27+
28+
const auto& S = RawProcessor.imgdata.sizes;
29+
TypedImage image(S.width, S.height, PixelFormatFromString("GRAY16LE"), sizeof(uint16_t) * S.raw_width);
30+
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);
31+
32+
// TODO: Support image metadata so that we can extract these fields.
33+
// RawProcessor.imgdata.other.iso_speed
34+
// RawProcessor.imgdata.other.aperture
35+
// RawProcessor.imgdata.other.focal_len
36+
// RawProcessor.imgdata.other.shutter
37+
// RawProcessor.imgdata.other.timestamp
38+
// RawProcessor.imgdata.makernotes.common.exifCameraElevationAngle
39+
// RawProcessor.imgdata.makernotes.sony.SonyDateTime
40+
41+
return image;
42+
#else
43+
PANGOLIN_UNUSED(filename);
44+
throw std::runtime_error("Rebuild Pangolin for libraw support.");
45+
#endif
46+
}
47+
48+
}

0 commit comments

Comments
 (0)