RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
rawspeed-identify.cpp
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 copyright (c) 2009--2011 johannes hanika.
4 copyright (c) 2016 Pedro CĂ´rte-Real
5
6 darktable is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 darktable is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with darktable. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#include "RawSpeed-API.h"
21#include "adt/Array1DRef.h"
22#include "adt/Array2DRef.h"
23#include <cstddef>
24#include <cstdint>
25#include <cstdio>
26#include <memory>
27#include <string>
28#include <sys/stat.h>
29#include <vector>
30
32
33namespace {
34
35std::string find_cameras_xml(const char* argv0) {
36 struct stat statbuf;
37
38#ifdef RS_CAMERAS_XML_PATH
39 if (static const char* set_camfile = RS_CAMERAS_XML_PATH;
40 stat(set_camfile, &statbuf)) {
41 fprintf(stderr, "WARNING: Couldn't find cameras.xml in '%s'\n",
42 set_camfile);
43 } else {
44 return set_camfile;
45 }
46#endif
47
48 const std::string self(argv0);
49
50 // If we haven't been provided with a valid cameras.xml path on compile try
51 // relative to argv[0]
52 const std::size_t lastslash = self.find_last_of(R"(/\)");
53 const std::string bindir(self.substr(0, lastslash));
54
55 std::string found_camfile(bindir +
56 "/../share/darktable/rawspeed/cameras.xml");
57
58 if (stat(found_camfile.c_str(), &statbuf)) {
59#ifndef __APPLE__
60 fprintf(stderr, "WARNING: Couldn't find cameras.xml in '%s'\n",
61 found_camfile.c_str());
62#else
63 fprintf(stderr, "WARNING: Couldn't find cameras.xml in '%s'\n",
64 found_camfile.c_str());
65 found_camfile =
66 bindir + "/../Resources/share/darktable/rawspeed/cameras.xml";
67 if (stat(found_camfile.c_str(), &statbuf)) {
68 fprintf(stderr, "WARNING: Couldn't find cameras.xml in '%s'\n",
69 found_camfile.c_str());
70 }
71#endif
72 }
73
74#ifdef RAWSPEED_STANDALONE_BUILD
75 // running from build dir?
76 found_camfile = std::string(RAWSPEED_SOURCE_DIR "/data/cameras.xml");
77#endif
78
79 if (stat(found_camfile.c_str(), &statbuf)) {
80#ifndef __APPLE__
81 fprintf(stderr, "ERROR: Couldn't find cameras.xml in '%s'\n",
82 found_camfile.c_str());
83 return {};
84#else
85 fprintf(stderr, "WARNING: Couldn't find cameras.xml in '%s'\n",
86 found_camfile.c_str());
87 found_camfile =
88 bindir + "/../Resources/share/darktable/rawspeed/cameras.xml";
89 if (stat(found_camfile.c_str(), &statbuf)) {
90 fprintf(stderr, "ERROR: Couldn't find cameras.xml in '%s'\n",
91 found_camfile.c_str());
92 return {};
93 }
94#endif
95 }
96
97 return found_camfile;
98}
99
100} // namespace
101
102} // namespace rawspeed::identify
103
104using rawspeed::Buffer;
112using rawspeed::identify::find_cameras_xml;
113
114// NOLINTNEXTLINE(readability-function-size)
115int main(int argc_, char* argv_[]) {
116 auto argv = rawspeed::Array1DRef(argv_, argc_);
117
118 if (argv.size() != 2) {
119 fprintf(stderr, "Usage: darktable-rs-identify <file>\n");
120 return 0;
121 }
122
123 const std::string camfile = find_cameras_xml(argv(0));
124 if (camfile.empty()) {
125 // fprintf(stderr, "ERROR: Couldn't find cameras.xml\n");
126 return 2;
127 }
128 // fprintf(stderr, "Using cameras.xml from '%s'\n", camfile.c_str());
129
130 try {
131 std::unique_ptr<const CameraMetaData> meta;
132
133#ifdef HAVE_PUGIXML
134 meta = std::make_unique<CameraMetaData>(camfile.c_str());
135#else
136 meta = std::make_unique<CameraMetaData>();
137#endif
138
139 if (!meta) {
140 fprintf(stderr, "ERROR: Couldn't get a CameraMetaData instance\n");
141 return 2;
142 }
143
144 fprintf(stderr, "Loading file: \"%s\"\n", argv(1));
145
146 FileReader f(argv(1));
147
148 auto [storage, buf] = f.readFile();
149
150 RawParser t(buf);
151
152 auto d(t.getDecoder(meta.get()));
153
154 if (!d) {
155 fprintf(stderr, "ERROR: Couldn't get a RawDecoder instance\n");
156 return 2;
157 }
158
159 d->applyCrop = false;
160 d->failOnUnknown = true;
161 RawImage r = d->mRaw;
162 const RawImage* const raw = &r;
163
164 d->decodeMetaData(meta.get());
165
166 fprintf(stdout, "make: %s\n", r->metadata.make.c_str());
167 fprintf(stdout, "model: %s\n", r->metadata.model.c_str());
168
169 fprintf(stdout, "canonical_make: %s\n", r->metadata.canonical_make.c_str());
170 fprintf(stdout, "canonical_model: %s\n",
171 r->metadata.canonical_model.c_str());
172 fprintf(stdout, "canonical_alias: %s\n",
173 r->metadata.canonical_alias.c_str());
174
175 d->checkSupport(meta.get());
176 d->decodeRaw();
177 d->decodeMetaData(meta.get());
178 r = d->mRaw;
179
180 for (const auto errors = r->getErrors(); const auto& error : errors)
181 fprintf(stderr, "WARNING: [rawspeed] %s\n", error.c_str());
182
183 fprintf(stdout, "blackLevel: %d\n", r->blackLevel);
184
185 fprintf(stdout, "whitePoint: ");
186 if (!r->whitePoint)
187 fprintf(stdout, "unknown");
188 else
189 fprintf(stdout, "%d", *r->whitePoint);
190 fprintf(stdout, "\n");
191
192 fprintf(stdout, "blackLevelSeparate: ");
193 if (!r->blackLevelSeparate) {
194 fprintf(stdout, "none");
195 } else {
196 fprintf(stdout, "(%i x %i)", r->blackLevelSeparate->width(),
197 r->blackLevelSeparate->height());
198 if (auto blackLevelSeparate1D = r->blackLevelSeparate->getAsArray1DRef();
199 blackLevelSeparate1D && blackLevelSeparate1D->size() != 0) {
200 for (auto l : *blackLevelSeparate1D)
201 fprintf(stdout, " %d", l);
202 }
203 }
204 fprintf(stdout, "\n");
205
206 fprintf(stdout, "wbCoeffs:");
207 if (!r->metadata.wbCoeffs)
208 fprintf(stdout, " (none)");
209 else {
210 for (const auto& e : *r->metadata.wbCoeffs)
211 fprintf(stdout, " %f", implicit_cast<double>(e));
212 }
213 fprintf(stdout, "\n");
214
215 fprintf(stdout, "isCFA: %d\n", r->isCFA);
216 uint32_t filters = r->cfa.getDcrawFilter();
217 fprintf(stdout, "filters: %u (0x%x)\n", filters, filters);
218 const uint32_t bpp = r->getBpp();
219 fprintf(stdout, "bpp: %u\n", bpp);
220 const uint32_t cpp = r->getCpp();
221 fprintf(stdout, "cpp: %u\n", cpp);
222 fprintf(stdout, "dataType: %u\n", static_cast<unsigned>(r->getDataType()));
223
224 // dimensions of uncropped image
225 const iPoint2D dimUncropped = r->getUncroppedDim();
226 fprintf(stdout, "dimUncropped: %dx%d\n", dimUncropped.x, dimUncropped.y);
227
228 // dimensions of cropped image
229 iPoint2D dimCropped = r->dim;
230 fprintf(stdout, "dimCropped: %dx%d\n", dimCropped.x, dimCropped.y);
231
232 // crop - Top,Left corner
233 iPoint2D cropTL = r->getCropOffset();
234 fprintf(stdout, "cropOffset: %dx%d\n", cropTL.x, cropTL.y);
235
236 fprintf(stdout, "fuji_rotation_pos: %u\n", r->metadata.fujiRotationPos);
237 fprintf(stdout, "pixel_aspect_ratio: %f\n", r->metadata.pixelAspectRatio);
238
239 double sum = 0.0;
240#ifdef HAVE_OPENMP
241#pragma omp parallel for default(none) firstprivate(dimUncropped, raw, bpp) \
242 schedule(static) reduction(+ : sum)
243#endif
244 for (int y = 0; y < dimUncropped.y; ++y) {
246 (*raw)->getByteDataAsUncroppedArray2DRef();
247 for (unsigned x = 0; x < bpp * dimUncropped.x; ++x)
248 sum += static_cast<double>(img(y, x));
249 }
250 fprintf(stdout, "Image byte sum: %lf\n", sum);
251 fprintf(stdout, "Image byte avg: %lf\n",
252 sum / static_cast<double>(dimUncropped.y * dimUncropped.x * bpp));
253
255 sum = 0.0;
256
257#ifdef HAVE_OPENMP
258#pragma omp parallel for default(none) firstprivate(dimUncropped, raw, cpp) \
259 schedule(static) reduction(+ : sum)
260#endif
261 for (int y = 0; y < dimUncropped.y; ++y) {
263 (*raw)->getF32DataAsUncroppedArray2DRef();
264 for (unsigned x = 0; x < cpp * dimUncropped.x; ++x)
265 sum += static_cast<double>(img(y, x));
266 }
267
268 fprintf(stdout, "Image float sum: %lf\n", sum);
269 fprintf(stdout, "Image float avg: %lf\n",
270 sum / static_cast<double>(dimUncropped.y * dimUncropped.x));
271 } else if (r->getDataType() == rawspeed::RawImageType::UINT16) {
272 sum = 0.0;
273
274#ifdef HAVE_OPENMP
275#pragma omp parallel for default(none) firstprivate(dimUncropped, raw, cpp) \
276 schedule(static) reduction(+ : sum)
277#endif
278 for (int y = 0; y < dimUncropped.y; ++y) {
280 (*raw)->getU16DataAsUncroppedArray2DRef();
281 for (unsigned x = 0; x < cpp * dimUncropped.x; ++x)
282 sum += static_cast<double>(img(y, x));
283 }
284
285 fprintf(stdout, "Image uint16_t sum: %lf\n", sum);
286 fprintf(stdout, "Image uint16_t avg: %lf\n",
287 sum / static_cast<double>(dimUncropped.y * dimUncropped.x));
288 }
289 } catch (const RawspeedException& e) {
290 fprintf(stderr, "ERROR: [rawspeed] %s\n", e.what());
291
292 /* if an exception is raised lets not retry or handle the
293 specific ones, consider the file as corrupted */
294 return 2;
295 }
296
297 return 0;
298}
299
300// vim: shiftwidth=2 expandtab tabstop=2 cindent
301// kate: indent-mode cstyle; replace-tabs on; tab-indents: off;
302// kate: remove-trailing-space on;
dim y
Definition Common.cpp:51
dim x
Definition Common.cpp:50
std::vector< std::string > && getErrors() REQUIRES(!mutex)
Definition ErrorLog.cpp:47
std::pair< std::unique_ptr< std::vector< uint8_t, DefaultInitAllocatorAdaptor< uint8_t, AlignedAllocator< uint8_t, 16 > > > >, Buffer > readFile() const
std::string canonical_alias
Definition RawImage.h:104
Optional< std::array< float, 4 > > wbCoeffs
Definition RawImage.h:86
std::string canonical_model
Definition RawImage.h:103
std::string canonical_make
Definition RawImage.h:102
rawspeed::RawImageType getDataType() const
Definition RawImage.h:125
uint32_t RAWSPEED_READONLY getCpp() const
Definition RawImage.h:118
Optional< Array2DRef< int > > blackLevelSeparate
Definition RawImage.h:165
iPoint2D RAWSPEED_READONLY getCropOffset() const
Definition RawImage.cpp:171
iPoint2D RAWSPEED_READONLY getUncroppedDim() const
Definition RawImage.cpp:167
Optional< int > whitePoint
Definition RawImage.h:172
uint32_t RAWSPEED_READONLY getBpp() const
Definition RawImage.h:119
ImageMetaData metadata
Definition RawImage.h:184
ColorFilterArray cfa
Definition RawImage.h:162
virtual std::unique_ptr< RawDecoder > getDecoder(const CameraMetaData *meta=nullptr)
Definition RawParser.cpp:42
value_type x
Definition Point.h:102
value_type y
Definition Point.h:103
int main()
constexpr RAWSPEED_READNONE Ttgt implicit_cast(Tsrc value)
Definition Casts.h:32
Array1DRef(T *data_, int numElts_) -> Array1DRef< T >
constexpr RAWSPEED_READNONE Ttgt implicit_cast(Tsrc value)
Definition Casts.h:32