Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect Exiv2::Value type (Exiv2::unsignedRational) in readRational of QgsExifTools (fixes #57942) #57943

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/core/raster/qgsexiftools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,21 @@
#include <QFileInfo>
#include <QTime>

double readRationale( const Exiv2::Value &value, long n = 0 )
double readRational( const Exiv2::Value &value, long n = 0 )
{
const Exiv2::Rational rational = value.toRational( n );
return static_cast< double >( rational.first ) / rational.second;
const auto numerator = rational.first;
const auto denominator = rational.second;
double res = 0;
if ( value.typeId() == Exiv2::unsignedRational )
{
res = static_cast< double >( static_cast<uint32_t>( numerator ) ) / static_cast<uint32_t>( denominator );
}
else
{
res = static_cast< double >( numerator ) / denominator;
}
return res;
};

double readCoordinate( const Exiv2::Value &value )
Expand All @@ -35,7 +46,7 @@ double readCoordinate( const Exiv2::Value &value )
double div = 1;
for ( int i = 0; i < 3; i++ )
{
res += readRationale( value, i ) / div;
res += readRational( value, i ) / div;
div *= 60;
}
return res;
Expand Down Expand Up @@ -114,7 +125,7 @@ QVariant decodeXmpData( const QString &key, Exiv2::XmpData::const_iterator &it )
{
if ( it->count() == 1 )
{
val = QVariant::fromValue( readRationale( it->value() ) );
val = QVariant::fromValue( readRational( it->value() ) );
}
else
{
Expand Down Expand Up @@ -154,9 +165,10 @@ QVariant decodeExifData( const QString &key, Exiv2::ExifData::const_iterator &it
const QStringList parts = QString::fromStdString( it->toString() ).split( QRegularExpression( QStringLiteral( "\\s+" ) ) );
if ( parts.size() == 3 )
{
const int hour = readRationale( it->value(), 0 );
const int minute = readRationale( it->value(), 1 );
const int second = readRationale( it->value(), 2 );
const int hour = std::max( 0, std::min( 23, static_cast< int >( readRational( it->value(), 0 ) ) ) );
const int minute = std::max( 0, std::min( 59, static_cast< int >( readRational( it->value(), 1 ) ) ) );
const int second = std::max( 0, std::min( 59, static_cast< int >( readRational( it->value(), 2 ) ) ) );

val = QVariant::fromValue( QTime::fromString( QStringLiteral( "%1:%2:%3" )
.arg( QString::number( hour ).rightJustified( 2, '0' ) )
.arg( QString::number( minute ).rightJustified( 2, '0' ) )
Expand Down Expand Up @@ -238,7 +250,7 @@ QVariant decodeExifData( const QString &key, Exiv2::ExifData::const_iterator &it
{
if ( it->count() == 1 )
{
val = QVariant::fromValue( readRationale( it->value() ) );
val = QVariant::fromValue( readRational( it->value() ) );
}
else
{
Expand Down Expand Up @@ -408,7 +420,7 @@ QgsPoint QgsExifTools::getGeoTag( const QString &imagePath, bool &ok )
const Exiv2::ExifData::iterator itElevRefVal = exifData.findKey( Exiv2::ExifKey( "Exif.GPSInfo.GPSAltitudeRef" ) );
if ( itElevVal != exifData.end() )
{
double elev = readRationale( itElevVal->value() );
double elev = readRational( itElevVal->value() );
if ( itElevRefVal != exifData.end() )
{
const QString elevRef = QString::fromStdString( itElevRefVal->value().toString() );
Expand Down
Loading