mirror of
https://github.com/google/sandboxed-api.git
synced 2024-03-22 13:11:30 +08:00
libraw: data checking
This commit is contained in:
parent
7fc66b2026
commit
1b1c085cfe
|
@ -65,6 +65,8 @@ add_sapi_library(
|
||||||
libraw_cameraCount
|
libraw_cameraCount
|
||||||
|
|
||||||
libraw_COLOR
|
libraw_COLOR
|
||||||
|
libraw_get_raw_height
|
||||||
|
libraw_get_raw_width
|
||||||
|
|
||||||
INPUTS
|
INPUTS
|
||||||
"${CMAKE_BINARY_DIR}/raw.gen.h"
|
"${CMAKE_BINARY_DIR}/raw.gen.h"
|
||||||
|
|
|
@ -66,16 +66,18 @@ int main(int ac, char* av[]) {
|
||||||
LibRaw lr(&sandbox, av[1]);
|
LibRaw lr(&sandbox, av[1]);
|
||||||
if (not lr.CheckIsInit().ok()) {
|
if (not lr.CheckIsInit().ok()) {
|
||||||
std::cerr << "Unable init LibRaw";
|
std::cerr << "Unable init LibRaw";
|
||||||
|
std::cerr << lr.CheckIsInit().status();
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
status = lr.OpenFile();
|
status = lr.OpenFile();
|
||||||
if (not status.ok()) {
|
if (not status.ok()) {
|
||||||
std::cerr << "Unable to open file" << av[1] << "\n";
|
std::cerr << "Unable to open file" << av[1] << "\n";
|
||||||
|
std::cerr << status;
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((lr.GetImgData().idata.colors == 1 and channel > 0) or (channel > 3)) {
|
if ((lr.GetColorCount() == 1 and channel > 0) or (channel > 3)) {
|
||||||
std::cerr << "Incorrect CHANNEL specified:" << channel << "\n";
|
std::cerr << "Incorrect CHANNEL specified:" << channel << "\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
@ -83,37 +85,49 @@ int main(int ac, char* av[]) {
|
||||||
status = lr.Unpack();
|
status = lr.Unpack();
|
||||||
if (not status.ok()) {
|
if (not status.ok()) {
|
||||||
std::cerr << "Unable to unpack file" << av[1] << "\n";
|
std::cerr << "Unable to unpack file" << av[1] << "\n";
|
||||||
|
std::cerr << status;
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
status = lr.SubtractBlack();
|
status = lr.SubtractBlack();
|
||||||
if (not status.ok()) {
|
if (not status.ok()) {
|
||||||
std::cerr << "Unable to subtract black level";
|
std::cerr << "Unable to subtract black level";
|
||||||
|
std::cerr << status;
|
||||||
|
// ok, but different output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
absl::StatusOr<std::vector<uint16_t>> rawdata = lr.RawData();
|
||||||
|
if (not rawdata.ok()) {
|
||||||
|
std::cerr << "Unable to get raw data\n";
|
||||||
|
std::cerr << rawdata.status();
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
absl::StatusOr<ushort> raw_height = lr.GetRawHeight();
|
||||||
|
absl::StatusOr<ushort> raw_width = lr.GetRawWidth();
|
||||||
|
if (not raw_height.ok() or not raw_width.ok()) {
|
||||||
|
std::cerr << "Unable to get raw image sizes.";
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// header
|
||||||
std::cout << av[1] << "\t" << colstart << "-" << rowstart << "-" << width
|
std::cout << av[1] << "\t" << colstart << "-" << rowstart << "-" << width
|
||||||
<< "x" << height << "\t"
|
<< "x" << height << "\t"
|
||||||
<< "channel: " << channel << "\n";
|
<< "channel: " << channel << "\n";
|
||||||
std::cout << std::setw(6) << "R\\C";
|
std::cout << std::setw(6) << "R\\C";
|
||||||
for (int col = colstart;
|
for (int col = colstart;
|
||||||
col < colstart + width and col < lr.GetImgData().sizes.raw_width;
|
col < colstart + width and col < *raw_width;
|
||||||
col++) {
|
col++) {
|
||||||
std::cout << std::setw(6) << col;
|
std::cout << std::setw(6) << col;
|
||||||
}
|
}
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
|
|
||||||
if (lr.GetImgData().rawdata.raw_image) {
|
// dump raw to output
|
||||||
absl::StatusOr<std::vector<uint16_t>> rawdata = lr.RawData();
|
|
||||||
if (not rawdata.ok()) {
|
|
||||||
std::cerr << "Unable to get raw data\n";
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int row = rowstart;
|
for (int row = rowstart;
|
||||||
row < rowstart + height && row < lr.GetImgData().sizes.raw_height;
|
row < rowstart + height && row < *raw_height;
|
||||||
row++) {
|
row++) {
|
||||||
unsigned rcolors[48];
|
unsigned rcolors[48];
|
||||||
if (lr.GetImgData().idata.colors > 1) {
|
if (lr.GetColorCount() > 1) {
|
||||||
absl::StatusOr<int> color;
|
absl::StatusOr<int> color;
|
||||||
for (int c = 0; c < 48; c++) {
|
for (int c = 0; c < 48; c++) {
|
||||||
color = lr.COLOR(row, c);
|
color = lr.COLOR(row, c);
|
||||||
|
@ -125,25 +139,25 @@ int main(int ac, char* av[]) {
|
||||||
std::cout << std::setw(6) << row;
|
std::cout << std::setw(6) << row;
|
||||||
|
|
||||||
for (int col = colstart;
|
for (int col = colstart;
|
||||||
col < colstart + width && col < lr.GetImgData().sizes.raw_width;
|
col < colstart + width && col < *raw_width;
|
||||||
col++) {
|
col++) {
|
||||||
int idx = row * lr.GetImgData().sizes.raw_pitch / 2 + col;
|
int idx = row * lr.GetImgData().sizes.raw_pitch / 2 + col;
|
||||||
|
|
||||||
if (rcolors[col % 48] == (unsigned)channel) {
|
if (rcolors[col % 48] == (unsigned)channel) {
|
||||||
|
absl::StatusOr<int> cblack = lr.GetCBlack(channel);
|
||||||
|
if (not cblack.ok()) {
|
||||||
|
std::cerr << "Unable to get cblack for channel " << channel;
|
||||||
|
std::cerr << cblack.status();
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
std::cout << std::setw(6)
|
std::cout << std::setw(6)
|
||||||
<< subtract_bl((*rawdata)[idx],
|
<< subtract_bl((*rawdata).at(idx), *cblack);
|
||||||
lr.GetImgData().color.cblack[channel]);
|
|
||||||
} else {
|
} else {
|
||||||
std::cout << " -";
|
std::cout << " -";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
std::cout
|
|
||||||
<< "Unsupported file data (e.g. floating point format), or incorrect "
|
|
||||||
"channel specified\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
|
@ -31,21 +31,14 @@ class LibRawSapiSandbox : public LibRawSandbox {
|
||||||
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
|
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
|
||||||
sandbox2::PolicyBuilder*) override {
|
sandbox2::PolicyBuilder*) override {
|
||||||
return sandbox2::PolicyBuilder()
|
return sandbox2::PolicyBuilder()
|
||||||
.AllowStaticStartup()
|
|
||||||
.AllowDynamicStartup()
|
.AllowDynamicStartup()
|
||||||
.AllowTcMalloc()
|
|
||||||
.AllowSystemMalloc()
|
|
||||||
.AllowScudoMalloc()
|
|
||||||
.AllowOpen()
|
.AllowOpen()
|
||||||
.AllowRead()
|
.AllowRead()
|
||||||
.AllowWrite()
|
.AllowWrite()
|
||||||
.AllowSystemMalloc()
|
.AllowSystemMalloc()
|
||||||
.AllowExit()
|
.AllowExit()
|
||||||
.AllowSafeFcntl()
|
|
||||||
.AllowSyscalls({__NR_recvmsg})
|
.AllowSyscalls({__NR_recvmsg})
|
||||||
.AddFile(file_name_, /*is_ro=*/true)
|
.AddFile(file_name_, /*is_ro=*/true)
|
||||||
.AllowRestartableSequencesWithProcFiles(
|
|
||||||
sandbox2::PolicyBuilder::kAllowSlowFences) // hangs without it?
|
|
||||||
.BuildOrDie();
|
.BuildOrDie();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -97,10 +97,11 @@ TEST_P(LibRawTestFiles, TestSize) {
|
||||||
SAPI_ASSERT_OK(lr.OpenFile());
|
SAPI_ASSERT_OK(lr.OpenFile());
|
||||||
SAPI_ASSERT_OK(lr.Unpack());
|
SAPI_ASSERT_OK(lr.Unpack());
|
||||||
|
|
||||||
libraw_data_t lr_data = lr.GetImgData();
|
SAPI_ASSERT_OK_AND_ASSIGN(ushort raw_height, lr.GetRawHeight());
|
||||||
|
SAPI_ASSERT_OK_AND_ASSIGN(ushort raw_width, lr.GetRawWidth());
|
||||||
|
|
||||||
ASSERT_EQ(lr_data.sizes.raw_height, tv.raw_height);
|
ASSERT_EQ(raw_height, tv.raw_height);
|
||||||
ASSERT_EQ(lr_data.sizes.raw_width, tv.raw_width);
|
ASSERT_EQ(raw_width, tv.raw_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_P(LibRawTestFiles, TestCameraList) {
|
TEST_P(LibRawTestFiles, TestCameraList) {
|
||||||
|
|
|
@ -95,19 +95,63 @@ absl::StatusOr<int> LibRaw::COLOR(int row, int col) {
|
||||||
SAPI_RETURN_IF_ERROR(CheckIsInit());
|
SAPI_RETURN_IF_ERROR(CheckIsInit());
|
||||||
|
|
||||||
int color;
|
int color;
|
||||||
|
|
||||||
SAPI_ASSIGN_OR_RETURN(
|
SAPI_ASSIGN_OR_RETURN(
|
||||||
color, api_.libraw_COLOR(sapi_libraw_data_t_.PtrNone(), row, col));
|
color, api_.libraw_COLOR(sapi_libraw_data_t_.PtrNone(), row, col));
|
||||||
|
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
absl::StatusOr<ushort> LibRaw::GetRawHeight() {
|
||||||
|
SAPI_RETURN_IF_ERROR(CheckIsInit());
|
||||||
|
|
||||||
|
ushort height;
|
||||||
|
SAPI_ASSIGN_OR_RETURN(
|
||||||
|
height, api_.libraw_get_raw_height(sapi_libraw_data_t_.PtrNone()));
|
||||||
|
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
|
||||||
|
absl::StatusOr<ushort> LibRaw::GetRawWidth() {
|
||||||
|
SAPI_RETURN_IF_ERROR(CheckIsInit());
|
||||||
|
|
||||||
|
ushort width;
|
||||||
|
SAPI_ASSIGN_OR_RETURN(
|
||||||
|
width, api_.libraw_get_raw_width(sapi_libraw_data_t_.PtrNone()));
|
||||||
|
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
absl::StatusOr<unsigned> LibRaw::GetCBlack(int channel) {
|
||||||
|
SAPI_RETURN_IF_ERROR(CheckIsInit());
|
||||||
|
|
||||||
|
if (channel < 0 or channel >= LIBRAW_CBLACK_SIZE) {
|
||||||
|
return absl::OutOfRangeError(
|
||||||
|
absl::string_view(std::to_string(channel)
|
||||||
|
+ " is out of range for array with size "
|
||||||
|
+ std::to_string(LIBRAW_CBLACK_SIZE)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return GetImgData().color.cblack[channel];
|
||||||
|
|
||||||
|
ushort width;
|
||||||
|
SAPI_ASSIGN_OR_RETURN(
|
||||||
|
width, api_.libraw_get_raw_width(sapi_libraw_data_t_.PtrNone()));
|
||||||
|
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
int LibRaw::GetColorCount() {
|
||||||
|
return GetImgData().idata.colors;
|
||||||
|
}
|
||||||
|
|
||||||
absl::StatusOr<std::vector<uint16_t>> LibRaw::RawData() {
|
absl::StatusOr<std::vector<uint16_t>> LibRaw::RawData() {
|
||||||
SAPI_RETURN_IF_ERROR(CheckIsInit());
|
SAPI_RETURN_IF_ERROR(CheckIsInit());
|
||||||
|
|
||||||
int size = sapi_libraw_data_t_.data().sizes.raw_height *
|
ushort raw_height;
|
||||||
sapi_libraw_data_t_.data().sizes.raw_width;
|
ushort raw_width;
|
||||||
|
SAPI_ASSIGN_OR_RETURN(raw_height, GetRawHeight());
|
||||||
|
SAPI_ASSIGN_OR_RETURN(raw_width, GetRawWidth());
|
||||||
|
unsigned size = raw_height * raw_width;
|
||||||
std::vector<uint16_t> buf(size);
|
std::vector<uint16_t> buf(size);
|
||||||
sapi::v::Array<uint16_t> rawdata(buf.data(), buf.size());
|
sapi::v::Array<uint16_t> rawdata(buf.data(), buf.size());
|
||||||
|
|
||||||
|
|
|
@ -60,6 +60,10 @@ class LibRaw {
|
||||||
absl::Status SubtractBlack();
|
absl::Status SubtractBlack();
|
||||||
absl::StatusOr<std::vector<char*>> GetCameraList();
|
absl::StatusOr<std::vector<char*>> GetCameraList();
|
||||||
absl::StatusOr<int> COLOR(int row, int col);
|
absl::StatusOr<int> COLOR(int row, int col);
|
||||||
|
absl::StatusOr<ushort> GetRawHeight();
|
||||||
|
absl::StatusOr<ushort> GetRawWidth();
|
||||||
|
absl::StatusOr<unsigned> GetCBlack(int channel);
|
||||||
|
int GetColorCount();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
absl::Status InitLibRaw();
|
absl::Status InitLibRaw();
|
||||||
|
@ -70,7 +74,6 @@ class LibRaw {
|
||||||
|
|
||||||
std::string file_name_;
|
std::string file_name_;
|
||||||
|
|
||||||
public:
|
|
||||||
sapi::v::Struct<libraw_data_t> sapi_libraw_data_t_;
|
sapi::v::Struct<libraw_data_t> sapi_libraw_data_t_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user