1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may !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 <cstring>
16 #include <iomanip>
17 #include <iostream>
18 #include <vector>
19
20 #include "absl/flags/parse.h"
21 #include "absl/log/globals.h"
22 #include "absl/log/initialize.h"
23 #include "absl/status/statusor.h"
24 #include "contrib/libraw/sandboxed.h"
25 #include "contrib/libraw/utils/utils_libraw.h"
26
PrintUsage(const char * name)27 void PrintUsage(const char* name) {
28 std::cout << "Dump (small) selecton of RAW file as tab-separated text file\n"
29 << "Usage: " << name
30 << " inputfile COL ROW [CHANNEL] [width] [height]\n"
31 " COL - start column\n"
32 " ROW - start row\n"
33 " CHANNEL - raw channel to dump, default is 0 (red for rggb)\n"
34 " width - area width to dump, default is 16\n"
35 " height - area height to dump, default is 4\n";
36 }
37
SubtractBlack(uint16_t val,unsigned int bl)38 uint16_t SubtractBlack(uint16_t val, unsigned int bl) {
39 return val > bl ? val - bl : 0;
40 }
41
main(int argc,char * argv[])42 int main(int argc, char* argv[]) {
43 absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
44 absl::ParseCommandLine(argc, argv);
45 absl::InitializeLog();
46
47 if (argc < 4) {
48 PrintUsage(argv[0]);
49 return EXIT_FAILURE;
50 }
51
52 int colstart = atoi(argv[2]); // NOLINT(runtime/deprecated_fn)
53 int rowstart = atoi(argv[3]); // NOLINT(runtime/deprecated_fn)
54 int channel = 0;
55 if (argc > 4) {
56 channel = atoi(argv[4]); // NOLINT(runtime/deprecated_fn)
57 }
58 int width = 16;
59 if (argc > 5) {
60 width = atoi(argv[5]); // NOLINT(runtime/deprecated_fn)
61 }
62 int height = 4;
63 if (argc > 6) {
64 height = atoi(argv[6]); // NOLINT(runtime/deprecated_fn)
65 }
66 if (width < 1 || height < 1) {
67 PrintUsage(argv[0]);
68 return EXIT_FAILURE;
69 }
70
71 sapi::v::ConstCStr file_name(argv[1]);
72 absl::Status status;
73 LibRawSapiSandbox sandbox(file_name.GetData());
74
75 status = sandbox.Init();
76 if (!status.ok()) {
77 std::cerr << "Unable to start sandbox: " << status.message() << "\n";
78 return EXIT_FAILURE;
79 }
80
81 LibRaw lr(&sandbox, argv[1]);
82 if (!lr.CheckIsInit().ok()) {
83 std::cerr << "Unable init LibRaw: " << lr.CheckIsInit().message();
84 return EXIT_FAILURE;
85 }
86
87 status = lr.OpenFile();
88 if (!status.ok()) {
89 std::cerr << "Unable to open file " << argv[1] << ": " << status.message();
90 return EXIT_FAILURE;
91 }
92
93 if ((lr.GetColorCount() == 1 && channel > 0) || (channel > 3)) {
94 std::cerr << "Incorrect CHANNEL specified: " << channel << "\n";
95 return EXIT_FAILURE;
96 }
97
98 status = lr.Unpack();
99 if (!status.ok()) {
100 std::cerr << "Unable to unpack file " << argv[1] << status.message();
101 return EXIT_FAILURE;
102 }
103
104 status = lr.SubtractBlack();
105 if (!status.ok()) {
106 std::cerr << "Unable to subtract black level: " << status.message();
107 // ok, but different output
108 }
109
110 absl::StatusOr<std::vector<uint16_t>> rawdata = lr.RawData();
111 if (!rawdata.ok()) {
112 std::cerr << "Unable to get raw data: " << rawdata.status().message();
113 return EXIT_FAILURE;
114 }
115
116 absl::StatusOr<ushort> raw_height = lr.GetRawHeight();
117 absl::StatusOr<ushort> raw_width = lr.GetRawWidth();
118 if (!raw_height.ok() || !raw_width.ok()) {
119 std::cerr << "Unable to get raw image sizes";
120 return EXIT_FAILURE;
121 }
122
123 // header
124 std::cout << argv[1] << "\t" << colstart << "-" << rowstart << "-" << width
125 << "x" << height << "\t"
126 << "channel: " << channel << "\n";
127 std::cout << std::setw(6) << "R\\C";
128 for (int col = colstart; col < colstart + width && col < *raw_width; col++) {
129 std::cout << std::setw(6) << col;
130 }
131 std::cout << "\n";
132
133 // dump raw to output
134 for (int row = rowstart; row < rowstart + height && row < *raw_height;
135 ++row) {
136 int rcolors[48];
137 if (lr.GetColorCount() > 1) {
138 absl::StatusOr<int> color;
139 for (int c = 0; c < 48; c++) {
140 color = lr.COLOR(row, c);
141 if (color.ok()) rcolors[c] = *color;
142 }
143 } else {
144 memset(rcolors, 0, sizeof(rcolors));
145 }
146 std::cout << std::setw(6) << row;
147
148 for (int col = colstart; col < colstart + width && col < *raw_width;
149 ++col) {
150 int idx = row * lr.GetImgData().sizes.raw_pitch / 2 + col;
151
152 if (rcolors[col % 48] == channel) {
153 absl::StatusOr<unsigned int> cblack = lr.GetCBlack(channel);
154 if (!cblack.ok()) {
155 std::cerr << "Unable to get cblack for channel " << channel << ": "
156 << cblack.status().message();
157 return EXIT_FAILURE;
158 }
159 std::cout << std::setw(6) << SubtractBlack((*rawdata).at(idx), *cblack);
160 } else {
161 std::cout << " -";
162 }
163 }
164 std::cout << "\n";
165 }
166
167 return EXIT_SUCCESS;
168 }
169