xref: /aosp_15_r20/external/sandboxed-api/contrib/libraw/utils/utils_libraw.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "contrib/libraw/utils/utils_libraw.h"
16 
17 #include "absl/strings/str_cat.h"
18 #include "contrib/libraw/sandboxed.h"
19 
InitLibRaw()20 absl::Status LibRaw::InitLibRaw() {
21   SAPI_ASSIGN_OR_RETURN(libraw_data_t * lr_data, api_.libraw_init(0));
22 
23   sapi_libraw_data_t_.SetRemote(lr_data);
24   SAPI_RETURN_IF_ERROR(sandbox_->TransferFromSandboxee(&sapi_libraw_data_t_));
25 
26   return absl::OkStatus();
27 }
28 
~LibRaw()29 LibRaw::~LibRaw() {
30   if (sapi_libraw_data_t_.GetRemote() != nullptr) {
31     api_.libraw_close(sapi_libraw_data_t_.PtrNone()).IgnoreError();
32   }
33 }
34 
CheckIsInit()35 absl::Status LibRaw::CheckIsInit() { return init_status_; }
36 
IsInit()37 bool LibRaw::IsInit() { return CheckIsInit().ok(); }
38 
GetImgData()39 libraw_data_t LibRaw::GetImgData() { return sapi_libraw_data_t_.data(); }
40 
OpenFile()41 absl::Status LibRaw::OpenFile() {
42   SAPI_RETURN_IF_ERROR(CheckIsInit());
43 
44   sapi::v::CStr file_name(file_name_.c_str());
45 
46   SAPI_ASSIGN_OR_RETURN(int error_code,
47                         api_.libraw_open_file(sapi_libraw_data_t_.PtrAfter(),
48                                               file_name.PtrBefore()));
49 
50   if (error_code != LIBRAW_SUCCESS) {
51     return absl::UnavailableError(
52         absl::string_view(std::to_string(error_code)));
53   }
54 
55   return absl::OkStatus();
56 }
57 
Unpack()58 absl::Status LibRaw::Unpack() {
59   SAPI_RETURN_IF_ERROR(CheckIsInit());
60 
61   SAPI_ASSIGN_OR_RETURN(int error_code,
62                         api_.libraw_unpack(sapi_libraw_data_t_.PtrAfter()));
63   if (error_code != LIBRAW_SUCCESS) {
64     return absl::UnavailableError(
65         absl::string_view(std::to_string(error_code)));
66   }
67 
68   return absl::OkStatus();
69 }
70 
SubtractBlack()71 absl::Status LibRaw::SubtractBlack() {
72   SAPI_RETURN_IF_ERROR(CheckIsInit());
73 
74   return api_.libraw_subtract_black(sapi_libraw_data_t_.PtrAfter());
75 }
76 
GetCameraList()77 absl::StatusOr<std::vector<char*>> LibRaw::GetCameraList() {
78   SAPI_RETURN_IF_ERROR(CheckIsInit());
79 
80   int size;
81   SAPI_ASSIGN_OR_RETURN(size, api_.libraw_cameraCount());
82 
83   std::vector<char*> buf(size);
84   sapi::v::Array<char*> camera_list(buf.data(), buf.size());
85 
86   char** sapi_camera_list;
87   SAPI_ASSIGN_OR_RETURN(sapi_camera_list, api_.libraw_cameraList());
88 
89   camera_list.SetRemote(sapi_camera_list);
90   SAPI_RETURN_IF_ERROR(sandbox_->TransferFromSandboxee(&camera_list));
91 
92   return buf;
93 }
94 
COLOR(int row,int col)95 absl::StatusOr<int> LibRaw::COLOR(int row, int col) {
96   SAPI_RETURN_IF_ERROR(CheckIsInit());
97 
98   int color;
99   SAPI_ASSIGN_OR_RETURN(
100       color, api_.libraw_COLOR(sapi_libraw_data_t_.PtrNone(), row, col));
101 
102   return color;
103 }
104 
GetRawHeight()105 absl::StatusOr<int> LibRaw::GetRawHeight() {
106   SAPI_RETURN_IF_ERROR(CheckIsInit());
107 
108   ushort height;
109   SAPI_ASSIGN_OR_RETURN(
110       height, api_.libraw_get_raw_height(sapi_libraw_data_t_.PtrNone()));
111 
112   return height;
113 }
114 
GetRawWidth()115 absl::StatusOr<int> LibRaw::GetRawWidth() {
116   SAPI_RETURN_IF_ERROR(CheckIsInit());
117 
118   ushort width;
119   SAPI_ASSIGN_OR_RETURN(
120       width, api_.libraw_get_raw_width(sapi_libraw_data_t_.PtrNone()));
121 
122   return width;
123 }
124 
GetCBlack(int channel)125 absl::StatusOr<unsigned int> LibRaw::GetCBlack(int channel) {
126   SAPI_RETURN_IF_ERROR(CheckIsInit());
127 
128   if (channel < 0 || channel >= LIBRAW_CBLACK_SIZE) {
129     return absl::OutOfRangeError(absl::StrCat(
130         channel, " is out of range for array with size ", LIBRAW_CBLACK_SIZE));
131   }
132 
133   return GetImgData().color.cblack[channel];
134 
135   ushort width;
136   SAPI_ASSIGN_OR_RETURN(
137       width, api_.libraw_get_raw_width(sapi_libraw_data_t_.PtrNone()));
138 
139   return width;
140 }
141 
GetColorCount()142 int LibRaw::GetColorCount() { return GetImgData().idata.colors; }
143 
RawData()144 absl::StatusOr<std::vector<uint16_t>> LibRaw::RawData() {
145   SAPI_RETURN_IF_ERROR(CheckIsInit());
146 
147   int raw_height;
148   int raw_width;
149   SAPI_ASSIGN_OR_RETURN(raw_height, GetRawHeight());
150   SAPI_ASSIGN_OR_RETURN(raw_width, GetRawWidth());
151   int size = raw_height * raw_width;
152   std::vector<uint16_t> buf(size);
153   sapi::v::Array<uint16_t> rawdata(buf.data(), buf.size());
154 
155   rawdata.SetRemote(sapi_libraw_data_t_.data().rawdata.raw_image);
156   SAPI_RETURN_IF_ERROR(sandbox_->TransferFromSandboxee(&rawdata));
157 
158   return buf;
159 }
160