Skip to content

Commit

Permalink
Do not throw if DNG crop is undefined
Browse files Browse the repository at this point in the history
The file is probably usable, so fall back to default values and log error instead
  • Loading branch information
kmilos committed May 25, 2023
1 parent 834caca commit c44a6ad
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions src/librawspeed/decoders/DngDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,17 @@ RawImage DngDecoder::decodeRawInternal() {

void DngDecoder::handleMetadata(const TiffIFD* raw) {
// Crop
if (const std::optional<iRectangle2D> aa = parseACTIVEAREA(raw))
mRaw->subFrame(*aa);
if (const std::optional<iRectangle2D> aa = parseACTIVEAREA(raw)) {
try {
if (!aa->hasPositiveArea())
ThrowRDE("No positive active area");
mRaw->subFrame(*aa);
} catch (const RawDecoderException& e) {
// We push back errors from the active area parser, since the image may
// still be usable
mRaw->setError(e.what());
}
}

if (raw->hasEntry(TiffTag::DEFAULTCROPORIGIN) &&
raw->hasEntry(TiffTag::DEFAULTCROPSIZE)) {
Expand All @@ -509,13 +518,19 @@ void DngDecoder::handleMetadata(const TiffIFD* raw) {
const TiffEntry* size_entry = raw->getEntry(TiffTag::DEFAULTCROPSIZE);

const auto tl_r = origin_entry->getRationalArray(2);
std::array<unsigned, 2> tl;
std::transform(tl_r.begin(), tl_r.end(), tl.begin(),
[](const NotARational<unsigned>& r) {
if (r.den == 0 || r.num % r.den != 0)
ThrowRDE("Error decoding default crop origin");
return r.num / r.den;
});
std::array<unsigned, 2> tl = {0, 0};
try {
std::transform(tl_r.begin(), tl_r.end(), tl.begin(),
[](const NotARational<unsigned>& r) {
if (r.den == 0 || r.num % r.den != 0)
ThrowRDE("Error decoding default crop origin");
return r.num / r.den;
});
} catch (const RawDecoderException& e) {
// We push back errors from the crop parser, since the image may still
// be usable
mRaw->setError(e.what());
}

if (iPoint2D cropOrigin(tl[0], tl[1]);
cropped.isPointInsideInclusive(cropOrigin))
Expand All @@ -524,13 +539,20 @@ void DngDecoder::handleMetadata(const TiffIFD* raw) {
cropped.dim = mRaw->dim - cropped.pos;

const auto sz_r = size_entry->getRationalArray(2);
std::array<unsigned, 2> sz;
std::transform(sz_r.begin(), sz_r.end(), sz.begin(),
[](const NotARational<unsigned>& r) {
if (r.den == 0 || r.num % r.den != 0)
ThrowRDE("Error decoding default crop size");
return r.num / r.den;
});
std::array<unsigned, 2> sz = {static_cast<unsigned>(mRaw->dim.x),
static_cast<unsigned>(mRaw->dim.y)};
try {
std::transform(sz_r.begin(), sz_r.end(), sz.begin(),
[](const NotARational<unsigned>& r) {
if (r.den == 0 || r.num % r.den != 0)
ThrowRDE("Error decoding default crop size");
return r.num / r.den;
});
} catch (const RawDecoderException& e) {
// We push back errors from the crop parser, since the image may still
// be usable
mRaw->setError(e.what());
}

if (iPoint2D size(sz[0], sz[1]);
size.isThisInside(mRaw->dim) &&
Expand Down

0 comments on commit c44a6ad

Please sign in to comment.