[libcamera-devel] [PATCH v2 4/9] android: jpeg: exif: Add functions for setting various values
Laurent Pinchart
laurent.pinchart at ideasonboard.com
Thu Jan 21 21:09:45 CET 2021
Hi Paul,
Thank you for the patch.
On Thu, Jan 21, 2021 at 07:15:44PM +0900, Paul Elder wrote:
> Add functions for setting the following EXIF fields:
>
> - GPSDatestamp
> - GPSTimestamp
> - GPSLocation
> - GPSLatitudeRef
> - GPSLatitude
> - GPSLongitudeRef
> - GPSLongitude
> - GPSAltitudeRef
> - GPSAltitude
> - GPSProcessingMethod
> - FocalLength
> - ExposureTime
> - FNumber
> - ISO
> - Flash
> - WhiteBalance
> - SubsecTime
> - SubsecTimeOriginal
> - SubsecTimeDigitized
>
> These are in preparation for fixing the following CTS tests:
>
> - android.hardware.camera2.cts.StillCaptureTest#testFocalLengths
> - android.hardware.camera2.cts.StillCaptureTest#testJpegExif
>
> Signed-off-by: Paul Elder <paul.elder at ideasonboard.com>
>
> ---
> Changes in v2:
> - some cosmetic and precision changes
> - use the new setString
> ---
> src/android/jpeg/exif.cpp | 159 ++++++++++++++++++++++++++++++++++++++
> src/android/jpeg/exif.h | 40 ++++++++++
> 2 files changed, 199 insertions(+)
>
> diff --git a/src/android/jpeg/exif.cpp b/src/android/jpeg/exif.cpp
> index 8a185130..d3d0026a 100644
> --- a/src/android/jpeg/exif.cpp
> +++ b/src/android/jpeg/exif.cpp
> @@ -7,8 +7,10 @@
>
> #include "exif.h"
>
> +#include <cmath>
> #include <map>
> #include <uchar.h>
> +#include <tuple>
Last time I checked T came before U :-) Didn't checkstyle.pl warn you ?
>
> #include "libcamera/internal/log.h"
> #include "libcamera/internal/utils.h"
> @@ -151,6 +153,16 @@ ExifEntry *Exif::createEntry(ExifIfd ifd, ExifTag tag, ExifFormat format,
> return entry;
> }
>
> +void Exif::setByte(ExifIfd ifd, ExifTag tag, uint8_t item)
> +{
> + ExifEntry *entry = createEntry(ifd, tag, EXIF_FORMAT_BYTE, 1, 1);
> + if (!entry)
> + return;
> +
> + entry->data[0] = item;
> + exif_entry_unref(entry);
> +}
> +
> void Exif::setShort(ExifIfd ifd, ExifTag tag, uint16_t item)
> {
> ExifEntry *entry = createEntry(ifd, tag);
> @@ -295,6 +307,102 @@ void Exif::setTimestamp(time_t timestamp)
> }
> }
>
> +void Exif::setGPSDateTimestamp(time_t timestamp)
> +{
> + struct tm tm;
> + gmtime_r(×tamp, &tm);
> +
> + char str[11];
> + strftime(str, sizeof(str), "%Y:%m:%d", &tm);
> + std::string tsStr(str);
> +
> + setString(EXIF_IFD_GPS, static_cast<ExifTag>(EXIF_TAG_GPS_DATE_STAMP),
> + EXIF_FORMAT_ASCII, NoEncoding, tsStr);
> +
> + /* Set GPS_TIME_STAMP */
> + ExifEntry *entry =
> + createEntry(EXIF_IFD_GPS,
> + static_cast<ExifTag>(EXIF_TAG_GPS_TIME_STAMP),
> + EXIF_FORMAT_RATIONAL, 3, 3 * sizeof(ExifRational));
> + if (!entry)
> + return;
> +
> + ExifRational ts[] = {
> + { static_cast<ExifLong>(tm.tm_hour), 1 },
> + { static_cast<ExifLong>(tm.tm_min), 1 },
> + { static_cast<ExifLong>(tm.tm_sec), 1 },
> + };
> +
> + for (int i = 0; i < 3; i++)
> + exif_set_rational(entry->data + i * sizeof(ExifRational),
> + order_, ts[i]);
> +
> + exif_entry_unref(entry);
> +}
> +
> +std::tuple<int, int, int> Exif::degreesToDMS(double decimalDegrees)
> +{
> + int degrees = std::trunc(decimalDegrees);
> + double minutes = std::abs((decimalDegrees - degrees) * 60);
> + double seconds = (minutes - std::trunc(minutes)) * 60;
> +
> + return { degrees, std::trunc(minutes), std::round(seconds) };
> +}
> +
> +void Exif::setGPSDMS(ExifIfd ifd, ExifTag tag, int deg, int min, int sec)
GPSDMS may be a bit cryptic, maybe setGPSCoordinate as it's setting a
single coordinate ? Up to you.
> +{
> + ExifEntry *entry = createEntry(ifd, tag, EXIF_FORMAT_RATIONAL, 3,
> + 3 * sizeof(ExifRational));
> + if (!entry)
> + return;
> +
> + ExifRational coords[] = {
> + { static_cast<ExifLong>(deg), 1 },
> + { static_cast<ExifLong>(min), 1 },
> + { static_cast<ExifLong>(sec), 1 },
> + };
> +
> + for (int i = 0; i < 3; i++)
> + exif_set_rational(entry->data + i * sizeof(ExifRational),
> + order_, coords[i]);
> + exif_entry_unref(entry);
> +}
> +
> +/*
> + * \brief Set GPS location (lat, long, alt)
> + * \param[in] coords Pointer to coordinates latitude, longitude, and altitude,
> + * first two in degrees, the third in meters
> + *
> + * \todo use std::array
std::array isn't great, sorry for proposing it in the first place. It
would convey the fact that we expect three values, but casting to an
array would be a bit dirty. We could use Span instead but that won't
provide much, so I think you can just drop the todo.
> + */
> +void Exif::setGPSLocation(const double *coords)
> +{
> + int deg, min, sec;
> +
> + std::tie<int, int, int>(deg, min, sec) = degreesToDMS(coords[0]);
> + setString(EXIF_IFD_GPS, static_cast<ExifTag>(EXIF_TAG_GPS_LATITUDE_REF),
> + EXIF_FORMAT_ASCII, NoEncoding, deg >= 0 ? "N" : "S");
> + setGPSDMS(EXIF_IFD_GPS, static_cast<ExifTag>(EXIF_TAG_GPS_LATITUDE),
> + std::abs(deg), min, sec);
> +
> + std::tie<int, int, int>(deg, min, sec) = degreesToDMS(coords[1]);
> + setString(EXIF_IFD_GPS, static_cast<ExifTag>(EXIF_TAG_GPS_LONGITUDE_REF),
> + EXIF_FORMAT_ASCII, NoEncoding, deg >= 0 ? "E" : "W");
> + setGPSDMS(EXIF_IFD_GPS, static_cast<ExifTag>(EXIF_TAG_GPS_LATITUDE),
> + std::abs(deg), min, sec);
> +
> + setByte(EXIF_IFD_GPS, static_cast<ExifTag>(EXIF_TAG_GPS_ALTITUDE_REF),
> + coords[2] >= 0 ? 0 : 1);
> + setRational(EXIF_IFD_GPS, static_cast<ExifTag>(EXIF_TAG_GPS_ALTITUDE),
> + ExifRational{ static_cast<ExifLong>(std::abs(coords[2])), 1 });
> +}
> +
> +void Exif::setGPSMethod(const std::string &method)
> +{
> + setString(EXIF_IFD_GPS, static_cast<ExifTag>(EXIF_TAG_GPS_PROCESSING_METHOD),
> + EXIF_FORMAT_UNDEFINED, Unicode, method);
> +}
> +
> void Exif::setOrientation(int orientation)
> {
> int value;
> @@ -331,6 +439,45 @@ void Exif::setThumbnail(Span<const unsigned char> thumbnail,
> setShort(EXIF_IFD_0, EXIF_TAG_COMPRESSION, compression);
> }
>
> +void Exif::setFocalLength(float length)
> +{
> + ExifRational rational = { static_cast<ExifLong>(length * 1000), 1000 };
> + setRational(EXIF_IFD_EXIF, EXIF_TAG_FOCAL_LENGTH, rational);
> +}
> +
> +void Exif::setExposureTime(uint64_t nsec)
> +{
> + ExifRational rational = { static_cast<ExifLong>(nsec), 1000000000 };
> + setRational(EXIF_IFD_EXIF, EXIF_TAG_EXPOSURE_TIME, rational);
> +}
> +
> +void Exif::setAperture(float size)
> +{
> + ExifRational rational = { static_cast<ExifLong>(size * 10000), 10000 };
> + setRational(EXIF_IFD_EXIF, EXIF_TAG_FNUMBER, rational);
> +}
> +
> +void Exif::setISO(uint16_t iso)
> +{
> + setShort(EXIF_IFD_EXIF, EXIF_TAG_ISO_SPEED_RATINGS, iso);
> +}
> +
> +void Exif::setFlash(Flash flash)
> +{
> + setShort(EXIF_IFD_EXIF, EXIF_TAG_FLASH, static_cast<ExifShort>(flash));
> +}
> +
> +void Exif::setWhiteBalance(WhiteBalance wb)
> +{
> + setShort(EXIF_IFD_EXIF, EXIF_TAG_WHITE_BALANCE, static_cast<ExifShort>(wb));
> +}
> +
> +void Exif::setSubsecTime(uint64_t subsec)
> +{
> + setString(EXIF_IFD_EXIF, EXIF_TAG_SUB_SEC_TIME,
> + EXIF_FORMAT_ASCII, NoEncoding, std::to_string(subsec));
> +}
> +
> /**
> * \brief Convert UTF-8 string to UTF-16 string
> * \param[in] str String to convert
> @@ -359,6 +506,18 @@ std::u16string Exif::utf8ToUtf16(const std::string &str)
> return ret;
> }
>
> +void Exif::setSubsecTimeOriginal(uint64_t subsec)
> +{
> + setString(EXIF_IFD_EXIF, EXIF_TAG_SUB_SEC_TIME_ORIGINAL,
> + EXIF_FORMAT_ASCII, NoEncoding, std::to_string(subsec));
> +}
> +
> +void Exif::setSubsecTimeDigitized(uint64_t subsec)
> +{
> + setString(EXIF_IFD_EXIF, EXIF_TAG_SUB_SEC_TIME_DIGITIZED,
> + EXIF_FORMAT_ASCII, NoEncoding, std::to_string(subsec));
> +}
Wouldn't this be better handled by adding a subsec parameter to
setTimestamp() ? setTimestamp() sets the three timestamps to the same
value, so the subsec API should do the same.
Reviewed-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
> +
> [[nodiscard]] int Exif::generate()
> {
> if (exifData_) {
> diff --git a/src/android/jpeg/exif.h b/src/android/jpeg/exif.h
> index db98ba63..baab96ed 100644
> --- a/src/android/jpeg/exif.h
> +++ b/src/android/jpeg/exif.h
> @@ -26,6 +26,27 @@ public:
> JPEG = 6,
> };
>
> + enum Flash {
> + /* bit 0 */
> + Fired = 0x01,
> + /* bits 1 and 2 */
> + StrobeDetected = 0x04,
> + StrobeNotDetected = 0x06,
> + /* bits 3 and 4 */
> + ModeCompulsoryFiring = 0x08,
> + ModeCompulsorySuppression = 0x10,
> + ModeAuto = 0x18,
> + /* bit 5 */
> + FlashNotPresent = 0x20,
> + /* bit 6 */
> + RedEye = 0x40,
> + };
> +
> + enum WhiteBalance {
> + Auto = 0,
> + Manual = 1,
> + };
> +
> enum StringEncoding {
> NoEncoding = 0,
> ASCII = 1,
> @@ -43,6 +64,21 @@ public:
> Compression compression);
> void setTimestamp(time_t timestamp);
>
> + void setGPSDateTimestamp(time_t timestamp);
> + void setGPSLocation(const double *coords);
> + void setGPSMethod(const std::string &method);
> +
> + void setFocalLength(float length);
> + void setExposureTime(uint64_t nsec);
> + void setAperture(float size);
> + void setISO(uint16_t iso);
> + void setFlash(Flash flash);
> + void setWhiteBalance(WhiteBalance wb);
> +
> + void setSubsecTime(uint64_t subsec);
> + void setSubsecTimeOriginal(uint64_t subsec);
> + void setSubsecTimeDigitized(uint64_t subsec);
> +
> libcamera::Span<const uint8_t> data() const { return { exifData_, size_ }; }
> [[nodiscard]] int generate();
>
> @@ -51,12 +87,16 @@ private:
> ExifEntry *createEntry(ExifIfd ifd, ExifTag tag, ExifFormat format,
> unsigned long components, unsigned int size);
>
> + void setByte(ExifIfd ifd, ExifTag tag, uint8_t item);
> void setShort(ExifIfd ifd, ExifTag tag, uint16_t item);
> void setLong(ExifIfd ifd, ExifTag tag, uint32_t item);
> void setString(ExifIfd ifd, ExifTag tag, ExifFormat format,
> StringEncoding enc, const std::string &item);
> void setRational(ExifIfd ifd, ExifTag tag, ExifRational item);
>
> + std::tuple<int, int, int> degreesToDMS(double decimalDegrees);
> + void setGPSDMS(ExifIfd ifd, ExifTag tag, int deg, int min, int sec);
> +
> std::u16string utf8ToUtf16(const std::string &str);
>
> bool valid_;
--
Regards,
Laurent Pinchart
More information about the libcamera-devel
mailing list