Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "CameraServerExifUtils" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | #include <cutils/log.h> |
| 22 | |
| 23 | #include <inttypes.h> |
| 24 | #include <math.h> |
| 25 | #include <stdint.h> |
| 26 | #include <string> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include "ExifUtils.h" |
| 30 | |
| 31 | extern "C" { |
| 32 | #include <libexif/exif-data.h> |
| 33 | } |
| 34 | |
| 35 | namespace std { |
| 36 | |
| 37 | template <> |
| 38 | struct default_delete<ExifEntry> { |
| 39 | inline void operator()(ExifEntry* entry) const { exif_entry_unref(entry); } |
| 40 | }; |
| 41 | |
| 42 | } // namespace std |
| 43 | |
| 44 | |
| 45 | namespace android { |
| 46 | namespace camera3 { |
| 47 | |
| 48 | |
| 49 | class ExifUtilsImpl : public ExifUtils { |
| 50 | public: |
| 51 | ExifUtilsImpl(); |
| 52 | |
| 53 | virtual ~ExifUtilsImpl(); |
| 54 | |
| 55 | // Initialize() can be called multiple times. The setting of Exif tags will be |
| 56 | // cleared. |
| 57 | virtual bool initialize(const unsigned char *app1Segment, size_t app1SegmentSize); |
Emilian Peev | 4b08d5d | 2019-02-01 09:53:14 -0800 | [diff] [blame^] | 58 | virtual bool initializeEmpty(); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 59 | |
| 60 | // set all known fields from a metadata structure |
| 61 | virtual bool setFromMetadata(const CameraMetadata& metadata, |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 62 | const CameraMetadata& staticInfo, |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 63 | const size_t imageWidth, |
| 64 | const size_t imageHeight); |
| 65 | |
| 66 | // sets the len aperture. |
| 67 | // Returns false if memory allocation fails. |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 68 | virtual bool setAperture(float aperture); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 69 | |
| 70 | // sets the color space. |
| 71 | // Returns false if memory allocation fails. |
| 72 | virtual bool setColorSpace(uint16_t color_space); |
| 73 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 74 | // sets the date and time of image last modified. It takes local time. The |
| 75 | // name of the tag is DateTime in IFD0. |
| 76 | // Returns false if memory allocation fails. |
| 77 | virtual bool setDateTime(const struct tm& t); |
| 78 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 79 | // sets the digital zoom ratio. If the numerator is 0, it means digital zoom |
| 80 | // was not used. |
| 81 | // Returns false if memory allocation fails. |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 82 | virtual bool setDigitalZoomRatio( |
| 83 | uint32_t crop_width, uint32_t crop_height, |
| 84 | uint32_t sensor_width, uint32_t sensor_height); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 85 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 86 | // Sets the exposure bias. |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 87 | // Returns false if memory allocation fails. |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 88 | virtual bool setExposureBias(int32_t ev, |
| 89 | uint32_t ev_step_numerator, uint32_t ev_step_denominator); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 90 | |
| 91 | // sets the exposure mode set when the image was shot. |
| 92 | // Returns false if memory allocation fails. |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 93 | virtual bool setExposureMode(uint8_t exposure_mode); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 94 | |
| 95 | // sets the exposure time, given in seconds. |
| 96 | // Returns false if memory allocation fails. |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 97 | virtual bool setExposureTime(float exposure_time); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 98 | |
| 99 | // sets the status of flash. |
| 100 | // Returns false if memory allocation fails. |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 101 | virtual bool setFlash(uint8_t flash_available, uint8_t flash_state, uint8_t ae_mode); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 102 | |
| 103 | // sets the F number. |
| 104 | // Returns false if memory allocation fails. |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 105 | virtual bool setFNumber(float f_number); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 106 | |
| 107 | // sets the focal length of lens used to take the image in millimeters. |
| 108 | // Returns false if memory allocation fails. |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 109 | virtual bool setFocalLength(float focal_length); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 110 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 111 | // sets the focal length of lens for 35mm film used to take the image in millimeters. |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 112 | // Returns false if memory allocation fails. |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 113 | virtual bool setFocalLengthIn35mmFilm(float focal_length, |
| 114 | float sensor_size_x, float sensor_size_y); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 115 | |
| 116 | // sets the altitude in meters. |
| 117 | // Returns false if memory allocation fails. |
| 118 | virtual bool setGpsAltitude(double altitude); |
| 119 | |
| 120 | // sets the latitude with degrees minutes seconds format. |
| 121 | // Returns false if memory allocation fails. |
| 122 | virtual bool setGpsLatitude(double latitude); |
| 123 | |
| 124 | // sets the longitude with degrees minutes seconds format. |
| 125 | // Returns false if memory allocation fails. |
| 126 | virtual bool setGpsLongitude(double longitude); |
| 127 | |
| 128 | // sets GPS processing method. |
| 129 | // Returns false if memory allocation fails. |
| 130 | virtual bool setGpsProcessingMethod(const std::string& method); |
| 131 | |
| 132 | // sets GPS date stamp and time stamp (atomic clock). It takes UTC time. |
| 133 | // Returns false if memory allocation fails. |
| 134 | virtual bool setGpsTimestamp(const struct tm& t); |
| 135 | |
| 136 | // sets the length (number of rows) of main image. |
| 137 | // Returns false if memory allocation fails. |
| 138 | virtual bool setImageHeight(uint32_t length); |
| 139 | |
| 140 | // sets the width (number of columes) of main image. |
| 141 | // Returns false if memory allocation fails. |
| 142 | virtual bool setImageWidth(uint32_t width); |
| 143 | |
| 144 | // sets the ISO speed. |
| 145 | // Returns false if memory allocation fails. |
| 146 | virtual bool setIsoSpeedRating(uint16_t iso_speed_ratings); |
| 147 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 148 | // sets the smallest F number of the lens. |
| 149 | // Returns false if memory allocation fails. |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 150 | virtual bool setMaxAperture(float aperture); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 151 | |
| 152 | // sets image orientation. |
| 153 | // Returns false if memory allocation fails. |
Emilian Peev | 4b08d5d | 2019-02-01 09:53:14 -0800 | [diff] [blame^] | 154 | virtual bool setOrientation(uint16_t degrees); |
| 155 | |
| 156 | // sets image orientation. |
| 157 | // Returns false if memory allocation fails. |
| 158 | virtual bool setOrientationValue(ExifOrientation orientationValue); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 159 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 160 | // sets the shutter speed. |
| 161 | // Returns false if memory allocation fails. |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 162 | virtual bool setShutterSpeed(float exposure_time); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 163 | |
| 164 | // sets the distance to the subject, given in meters. |
| 165 | // Returns false if memory allocation fails. |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 166 | virtual bool setSubjectDistance(float diopters); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 167 | |
| 168 | // sets the fractions of seconds for the <DateTime> tag. |
| 169 | // Returns false if memory allocation fails. |
| 170 | virtual bool setSubsecTime(const std::string& subsec_time); |
| 171 | |
| 172 | // sets the white balance mode set when the image was shot. |
| 173 | // Returns false if memory allocation fails. |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 174 | virtual bool setWhiteBalance(uint8_t white_balance); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 175 | |
| 176 | // Generates APP1 segment. |
| 177 | // Returns false if generating APP1 segment fails. |
| 178 | virtual bool generateApp1(); |
| 179 | |
| 180 | // Gets buffer of APP1 segment. This method must be called only after calling |
| 181 | // GenerateAPP1(). |
| 182 | virtual const uint8_t* getApp1Buffer(); |
| 183 | |
| 184 | // Gets length of APP1 segment. This method must be called only after calling |
| 185 | // GenerateAPP1(). |
| 186 | virtual unsigned int getApp1Length(); |
| 187 | |
| 188 | protected: |
| 189 | // sets the version of this standard supported. |
| 190 | // Returns false if memory allocation fails. |
| 191 | virtual bool setExifVersion(const std::string& exif_version); |
| 192 | |
| 193 | // Resets the pointers and memories. |
| 194 | virtual void reset(); |
| 195 | |
| 196 | // Adds a variable length tag to |exif_data_|. It will remove the original one |
| 197 | // if the tag exists. |
| 198 | // Returns the entry of the tag. The reference count of returned ExifEntry is |
| 199 | // two. |
| 200 | virtual std::unique_ptr<ExifEntry> addVariableLengthEntry(ExifIfd ifd, |
| 201 | ExifTag tag, ExifFormat format, uint64_t components, unsigned int size); |
| 202 | |
| 203 | // Adds a entry of |tag| in |exif_data_|. It won't remove the original one if |
| 204 | // the tag exists. |
| 205 | // Returns the entry of the tag. It adds one reference count to returned |
| 206 | // ExifEntry. |
| 207 | virtual std::unique_ptr<ExifEntry> addEntry(ExifIfd ifd, ExifTag tag); |
| 208 | |
| 209 | // Helpe functions to add exif data with different types. |
| 210 | virtual bool setShort(ExifIfd ifd, ExifTag tag, uint16_t value, const std::string& msg); |
| 211 | |
| 212 | virtual bool setLong(ExifIfd ifd, ExifTag tag, uint32_t value, const std::string& msg); |
| 213 | |
| 214 | virtual bool setRational(ExifIfd ifd, ExifTag tag, uint32_t numerator, |
| 215 | uint32_t denominator, const std::string& msg); |
| 216 | |
| 217 | virtual bool setSRational(ExifIfd ifd, ExifTag tag, int32_t numerator, |
| 218 | int32_t denominator, const std::string& msg); |
| 219 | |
| 220 | virtual bool setString(ExifIfd ifd, ExifTag tag, ExifFormat format, |
| 221 | const std::string& buffer, const std::string& msg); |
| 222 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 223 | float convertToApex(float val) { |
| 224 | return 2.0f * log2f(val); |
| 225 | } |
| 226 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 227 | // Destroys the buffer of APP1 segment if exists. |
| 228 | virtual void destroyApp1(); |
| 229 | |
| 230 | // The Exif data (APP1). Owned by this class. |
| 231 | ExifData* exif_data_; |
| 232 | // The raw data of APP1 segment. It's allocated by ExifMem in |exif_data_| but |
| 233 | // owned by this class. |
| 234 | uint8_t* app1_buffer_; |
| 235 | // The length of |app1_buffer_|. |
| 236 | unsigned int app1_length_; |
| 237 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 238 | // How precise the float-to-rational conversion for EXIF tags would be. |
| 239 | const static int kRationalPrecision = 10000; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | #define SET_SHORT(ifd, tag, value) \ |
| 243 | do { \ |
| 244 | if (setShort(ifd, tag, value, #tag) == false) \ |
| 245 | return false; \ |
| 246 | } while (0); |
| 247 | |
| 248 | #define SET_LONG(ifd, tag, value) \ |
| 249 | do { \ |
| 250 | if (setLong(ifd, tag, value, #tag) == false) \ |
| 251 | return false; \ |
| 252 | } while (0); |
| 253 | |
| 254 | #define SET_RATIONAL(ifd, tag, numerator, denominator) \ |
| 255 | do { \ |
| 256 | if (setRational(ifd, tag, numerator, denominator, #tag) == false) \ |
| 257 | return false; \ |
| 258 | } while (0); |
| 259 | |
| 260 | #define SET_SRATIONAL(ifd, tag, numerator, denominator) \ |
| 261 | do { \ |
| 262 | if (setSRational(ifd, tag, numerator, denominator, #tag) == false) \ |
| 263 | return false; \ |
| 264 | } while (0); |
| 265 | |
| 266 | #define SET_STRING(ifd, tag, format, buffer) \ |
| 267 | do { \ |
| 268 | if (setString(ifd, tag, format, buffer, #tag) == false) \ |
| 269 | return false; \ |
| 270 | } while (0); |
| 271 | |
| 272 | // This comes from the Exif Version 2.2 standard table 6. |
| 273 | const char gExifAsciiPrefix[] = {0x41, 0x53, 0x43, 0x49, 0x49, 0x0, 0x0, 0x0}; |
| 274 | |
| 275 | static void setLatitudeOrLongitudeData(unsigned char* data, double num) { |
| 276 | // Take the integer part of |num|. |
| 277 | ExifLong degrees = static_cast<ExifLong>(num); |
| 278 | ExifLong minutes = static_cast<ExifLong>(60 * (num - degrees)); |
| 279 | ExifLong microseconds = |
| 280 | static_cast<ExifLong>(3600000000u * (num - degrees - minutes / 60.0)); |
| 281 | exif_set_rational(data, EXIF_BYTE_ORDER_INTEL, {degrees, 1}); |
| 282 | exif_set_rational(data + sizeof(ExifRational), EXIF_BYTE_ORDER_INTEL, {minutes, 1}); |
| 283 | exif_set_rational(data + 2 * sizeof(ExifRational), EXIF_BYTE_ORDER_INTEL, |
| 284 | {microseconds, 1000000}); |
| 285 | } |
| 286 | |
| 287 | ExifUtils *ExifUtils::create() { |
| 288 | return new ExifUtilsImpl(); |
| 289 | } |
| 290 | |
| 291 | ExifUtils::~ExifUtils() { |
| 292 | } |
| 293 | |
| 294 | ExifUtilsImpl::ExifUtilsImpl() |
| 295 | : exif_data_(nullptr), app1_buffer_(nullptr), app1_length_(0) {} |
| 296 | |
| 297 | ExifUtilsImpl::~ExifUtilsImpl() { |
| 298 | reset(); |
| 299 | } |
| 300 | |
| 301 | |
| 302 | bool ExifUtilsImpl::initialize(const unsigned char *app1Segment, size_t app1SegmentSize) { |
| 303 | reset(); |
| 304 | exif_data_ = exif_data_new_from_data(app1Segment, app1SegmentSize); |
| 305 | if (exif_data_ == nullptr) { |
| 306 | ALOGE("%s: allocate memory for exif_data_ failed", __FUNCTION__); |
| 307 | return false; |
| 308 | } |
| 309 | // set the image options. |
| 310 | exif_data_set_option(exif_data_, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION); |
| 311 | exif_data_set_data_type(exif_data_, EXIF_DATA_TYPE_COMPRESSED); |
| 312 | exif_data_set_byte_order(exif_data_, EXIF_BYTE_ORDER_INTEL); |
| 313 | |
| 314 | // set exif version to 2.2. |
| 315 | if (!setExifVersion("0220")) { |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | return true; |
| 320 | } |
| 321 | |
Emilian Peev | 4b08d5d | 2019-02-01 09:53:14 -0800 | [diff] [blame^] | 322 | bool ExifUtilsImpl::initializeEmpty() { |
| 323 | reset(); |
| 324 | exif_data_ = exif_data_new(); |
| 325 | if (exif_data_ == nullptr) { |
| 326 | ALOGE("%s: allocate memory for exif_data_ failed", __FUNCTION__); |
| 327 | return false; |
| 328 | } |
| 329 | // set the image options. |
| 330 | exif_data_set_option(exif_data_, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION); |
| 331 | exif_data_set_data_type(exif_data_, EXIF_DATA_TYPE_COMPRESSED); |
| 332 | exif_data_set_byte_order(exif_data_, EXIF_BYTE_ORDER_INTEL); |
| 333 | |
| 334 | // set exif version to 2.2. |
| 335 | if (!setExifVersion("0220")) { |
| 336 | return false; |
| 337 | } |
| 338 | |
| 339 | return true; |
| 340 | } |
| 341 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 342 | bool ExifUtilsImpl::setAperture(float aperture) { |
| 343 | float apexValue = convertToApex(aperture); |
| 344 | SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_APERTURE_VALUE, |
| 345 | static_cast<uint32_t>(std::round(apexValue * kRationalPrecision)), |
| 346 | kRationalPrecision); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 347 | return true; |
| 348 | } |
| 349 | |
| 350 | bool ExifUtilsImpl::setColorSpace(uint16_t color_space) { |
| 351 | SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_COLOR_SPACE, color_space); |
| 352 | return true; |
| 353 | } |
| 354 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 355 | bool ExifUtilsImpl::setDateTime(const struct tm& t) { |
| 356 | // The length is 20 bytes including NULL for termination in Exif standard. |
| 357 | char str[20]; |
| 358 | int result = snprintf(str, sizeof(str), "%04i:%02i:%02i %02i:%02i:%02i", |
| 359 | t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec); |
| 360 | if (result != sizeof(str) - 1) { |
| 361 | ALOGW("%s: Input time is invalid", __FUNCTION__); |
| 362 | return false; |
| 363 | } |
| 364 | std::string buffer(str); |
| 365 | SET_STRING(EXIF_IFD_0, EXIF_TAG_DATE_TIME, EXIF_FORMAT_ASCII, buffer); |
| 366 | SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_DATE_TIME_ORIGINAL, EXIF_FORMAT_ASCII, buffer); |
| 367 | SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_DATE_TIME_DIGITIZED, EXIF_FORMAT_ASCII, buffer); |
| 368 | return true; |
| 369 | } |
| 370 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 371 | bool ExifUtilsImpl::setDigitalZoomRatio( |
| 372 | uint32_t crop_width, uint32_t crop_height, |
| 373 | uint32_t sensor_width, uint32_t sensor_height) { |
| 374 | float zoomRatioX = (crop_width == 0) ? 1.0 : 1.0 * sensor_width / crop_width; |
| 375 | float zoomRatioY = (crop_height == 0) ? 1.0 : 1.0 * sensor_height / crop_height; |
| 376 | float zoomRatio = std::max(zoomRatioX, zoomRatioY); |
| 377 | const static float noZoomThreshold = 1.02f; |
| 378 | |
| 379 | if (zoomRatio <= noZoomThreshold) { |
| 380 | SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_DIGITAL_ZOOM_RATIO, 0, 1); |
| 381 | } else { |
| 382 | SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_DIGITAL_ZOOM_RATIO, |
| 383 | static_cast<uint32_t>(std::round(zoomRatio * kRationalPrecision)), |
| 384 | kRationalPrecision); |
| 385 | } |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 386 | return true; |
| 387 | } |
| 388 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 389 | bool ExifUtilsImpl::setExposureMode(uint8_t exposure_mode) { |
| 390 | uint16_t exposureMode = (exposure_mode == ANDROID_CONTROL_AE_MODE_OFF) ? 1 : 0; |
| 391 | SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_EXPOSURE_MODE, exposureMode); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 392 | return true; |
| 393 | } |
| 394 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 395 | bool ExifUtilsImpl::setExposureTime(float exposure_time) { |
| 396 | SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_EXPOSURE_TIME, |
| 397 | static_cast<uint32_t>(std::round(exposure_time * kRationalPrecision)), |
| 398 | kRationalPrecision); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 399 | return true; |
| 400 | } |
| 401 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 402 | bool ExifUtilsImpl::setFlash(uint8_t flash_available, uint8_t flash_state, uint8_t ae_mode) { |
| 403 | // EXIF_TAG_FLASH bits layout per EXIF standard: |
| 404 | // Bit 0: 0 - did not fire |
| 405 | // 1 - fired |
| 406 | // Bit 1-2: status of return light |
| 407 | // Bit 3-4: 0 - unknown |
| 408 | // 1 - compulsory flash firing |
| 409 | // 2 - compulsory flash suppression |
| 410 | // 3 - auto mode |
| 411 | // Bit 5: 0 - flash function present |
| 412 | // 1 - no flash function |
| 413 | // Bit 6: 0 - no red-eye reduction mode or unknown |
| 414 | // 1 - red-eye reduction supported |
| 415 | uint16_t flash = 0x20; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 416 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 417 | if (flash_available == ANDROID_FLASH_INFO_AVAILABLE_TRUE) { |
| 418 | flash = 0x00; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 419 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 420 | if (flash_state == ANDROID_FLASH_STATE_FIRED) { |
| 421 | flash |= 0x1; |
| 422 | } |
| 423 | if (ae_mode == ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE) { |
| 424 | flash |= 0x40; |
| 425 | } |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 426 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 427 | uint16_t flashMode = 0; |
| 428 | switch (ae_mode) { |
| 429 | case ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH: |
| 430 | case ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE: |
| 431 | flashMode = 3; // AUTO |
| 432 | break; |
| 433 | case ANDROID_CONTROL_AE_MODE_ON_ALWAYS_FLASH: |
| 434 | case ANDROID_CONTROL_AE_MODE_ON_EXTERNAL_FLASH: |
| 435 | flashMode = 1; // ON |
| 436 | break; |
| 437 | case ANDROID_CONTROL_AE_MODE_OFF: |
| 438 | case ANDROID_CONTROL_AE_MODE_ON: |
| 439 | flashMode = 2; // OFF |
| 440 | break; |
| 441 | default: |
| 442 | flashMode = 0; // UNKNOWN |
| 443 | break; |
| 444 | } |
| 445 | flash |= (flashMode << 3); |
| 446 | } |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 447 | SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_FLASH, flash); |
| 448 | return true; |
| 449 | } |
| 450 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 451 | bool ExifUtilsImpl::setFNumber(float f_number) { |
| 452 | SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_FNUMBER, |
| 453 | static_cast<uint32_t>(std::round(f_number * kRationalPrecision)), |
| 454 | kRationalPrecision); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 455 | return true; |
| 456 | } |
| 457 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 458 | bool ExifUtilsImpl::setFocalLength(float focal_length) { |
| 459 | uint32_t numerator = static_cast<uint32_t>(std::round(focal_length * kRationalPrecision)); |
| 460 | SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_FOCAL_LENGTH, numerator, kRationalPrecision); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 461 | return true; |
| 462 | } |
| 463 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 464 | bool ExifUtilsImpl::setFocalLengthIn35mmFilm( |
| 465 | float focal_length, float sensor_size_x, float sensor_size_y) { |
| 466 | static const float filmDiagonal = 43.27; // diagonal of 35mm film |
| 467 | static const float minSensorDiagonal = 0.01; |
| 468 | float sensorDiagonal = std::sqrt( |
| 469 | sensor_size_x * sensor_size_x + sensor_size_y * sensor_size_y); |
| 470 | sensorDiagonal = std::max(sensorDiagonal, minSensorDiagonal); |
| 471 | float focalLength35mmFilm = std::round(focal_length * filmDiagonal / sensorDiagonal); |
| 472 | focalLength35mmFilm = std::min(1.0f * 65535, focalLength35mmFilm); |
| 473 | |
| 474 | SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_FOCAL_LENGTH_IN_35MM_FILM, |
| 475 | static_cast<uint16_t>(focalLength35mmFilm)); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 476 | return true; |
| 477 | } |
| 478 | |
| 479 | bool ExifUtilsImpl::setGpsAltitude(double altitude) { |
| 480 | ExifTag refTag = static_cast<ExifTag>(EXIF_TAG_GPS_ALTITUDE_REF); |
| 481 | std::unique_ptr<ExifEntry> refEntry = |
| 482 | addVariableLengthEntry(EXIF_IFD_GPS, refTag, EXIF_FORMAT_BYTE, 1, 1); |
| 483 | if (!refEntry) { |
| 484 | ALOGE("%s: Adding GPSAltitudeRef exif entry failed", __FUNCTION__); |
| 485 | return false; |
| 486 | } |
| 487 | if (altitude >= 0) { |
| 488 | *refEntry->data = 0; |
| 489 | } else { |
| 490 | *refEntry->data = 1; |
| 491 | altitude *= -1; |
| 492 | } |
| 493 | |
| 494 | ExifTag tag = static_cast<ExifTag>(EXIF_TAG_GPS_ALTITUDE); |
| 495 | std::unique_ptr<ExifEntry> entry = addVariableLengthEntry( |
| 496 | EXIF_IFD_GPS, tag, EXIF_FORMAT_RATIONAL, 1, sizeof(ExifRational)); |
| 497 | if (!entry) { |
| 498 | exif_content_remove_entry(exif_data_->ifd[EXIF_IFD_GPS], refEntry.get()); |
| 499 | ALOGE("%s: Adding GPSAltitude exif entry failed", __FUNCTION__); |
| 500 | return false; |
| 501 | } |
| 502 | exif_set_rational(entry->data, EXIF_BYTE_ORDER_INTEL, |
| 503 | {static_cast<ExifLong>(altitude * 1000), 1000}); |
| 504 | |
| 505 | return true; |
| 506 | } |
| 507 | |
| 508 | bool ExifUtilsImpl::setGpsLatitude(double latitude) { |
| 509 | const ExifTag refTag = static_cast<ExifTag>(EXIF_TAG_GPS_LATITUDE_REF); |
| 510 | std::unique_ptr<ExifEntry> refEntry = |
| 511 | addVariableLengthEntry(EXIF_IFD_GPS, refTag, EXIF_FORMAT_ASCII, 2, 2); |
| 512 | if (!refEntry) { |
| 513 | ALOGE("%s: Adding GPSLatitudeRef exif entry failed", __FUNCTION__); |
| 514 | return false; |
| 515 | } |
| 516 | if (latitude >= 0) { |
| 517 | memcpy(refEntry->data, "N", sizeof("N")); |
| 518 | } else { |
| 519 | memcpy(refEntry->data, "S", sizeof("S")); |
| 520 | latitude *= -1; |
| 521 | } |
| 522 | |
| 523 | const ExifTag tag = static_cast<ExifTag>(EXIF_TAG_GPS_LATITUDE); |
| 524 | std::unique_ptr<ExifEntry> entry = addVariableLengthEntry( |
| 525 | EXIF_IFD_GPS, tag, EXIF_FORMAT_RATIONAL, 3, 3 * sizeof(ExifRational)); |
| 526 | if (!entry) { |
| 527 | exif_content_remove_entry(exif_data_->ifd[EXIF_IFD_GPS], refEntry.get()); |
| 528 | ALOGE("%s: Adding GPSLatitude exif entry failed", __FUNCTION__); |
| 529 | return false; |
| 530 | } |
| 531 | setLatitudeOrLongitudeData(entry->data, latitude); |
| 532 | |
| 533 | return true; |
| 534 | } |
| 535 | |
| 536 | bool ExifUtilsImpl::setGpsLongitude(double longitude) { |
| 537 | ExifTag refTag = static_cast<ExifTag>(EXIF_TAG_GPS_LONGITUDE_REF); |
| 538 | std::unique_ptr<ExifEntry> refEntry = |
| 539 | addVariableLengthEntry(EXIF_IFD_GPS, refTag, EXIF_FORMAT_ASCII, 2, 2); |
| 540 | if (!refEntry) { |
| 541 | ALOGE("%s: Adding GPSLongitudeRef exif entry failed", __FUNCTION__); |
| 542 | return false; |
| 543 | } |
| 544 | if (longitude >= 0) { |
| 545 | memcpy(refEntry->data, "E", sizeof("E")); |
| 546 | } else { |
| 547 | memcpy(refEntry->data, "W", sizeof("W")); |
| 548 | longitude *= -1; |
| 549 | } |
| 550 | |
| 551 | ExifTag tag = static_cast<ExifTag>(EXIF_TAG_GPS_LONGITUDE); |
| 552 | std::unique_ptr<ExifEntry> entry = addVariableLengthEntry( |
| 553 | EXIF_IFD_GPS, tag, EXIF_FORMAT_RATIONAL, 3, 3 * sizeof(ExifRational)); |
| 554 | if (!entry) { |
| 555 | exif_content_remove_entry(exif_data_->ifd[EXIF_IFD_GPS], refEntry.get()); |
| 556 | ALOGE("%s: Adding GPSLongitude exif entry failed", __FUNCTION__); |
| 557 | return false; |
| 558 | } |
| 559 | setLatitudeOrLongitudeData(entry->data, longitude); |
| 560 | |
| 561 | return true; |
| 562 | } |
| 563 | |
| 564 | bool ExifUtilsImpl::setGpsProcessingMethod(const std::string& method) { |
| 565 | std::string buffer = |
| 566 | std::string(gExifAsciiPrefix, sizeof(gExifAsciiPrefix)) + method; |
| 567 | SET_STRING(EXIF_IFD_GPS, static_cast<ExifTag>(EXIF_TAG_GPS_PROCESSING_METHOD), |
| 568 | EXIF_FORMAT_UNDEFINED, buffer); |
| 569 | return true; |
| 570 | } |
| 571 | |
| 572 | bool ExifUtilsImpl::setGpsTimestamp(const struct tm& t) { |
| 573 | const ExifTag dateTag = static_cast<ExifTag>(EXIF_TAG_GPS_DATE_STAMP); |
| 574 | const size_t kGpsDateStampSize = 11; |
| 575 | std::unique_ptr<ExifEntry> entry = addVariableLengthEntry(EXIF_IFD_GPS, |
| 576 | dateTag, EXIF_FORMAT_ASCII, kGpsDateStampSize, kGpsDateStampSize); |
| 577 | if (!entry) { |
| 578 | ALOGE("%s: Adding GPSDateStamp exif entry failed", __FUNCTION__); |
| 579 | return false; |
| 580 | } |
| 581 | int result = snprintf(reinterpret_cast<char*>(entry->data), kGpsDateStampSize, |
| 582 | "%04i:%02i:%02i", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday); |
| 583 | if (result != kGpsDateStampSize - 1) { |
| 584 | ALOGW("%s: Input time is invalid", __FUNCTION__); |
| 585 | return false; |
| 586 | } |
| 587 | |
| 588 | const ExifTag timeTag = static_cast<ExifTag>(EXIF_TAG_GPS_TIME_STAMP); |
| 589 | entry = addVariableLengthEntry(EXIF_IFD_GPS, timeTag, EXIF_FORMAT_RATIONAL, 3, |
| 590 | 3 * sizeof(ExifRational)); |
| 591 | if (!entry) { |
| 592 | ALOGE("%s: Adding GPSTimeStamp exif entry failed", __FUNCTION__); |
| 593 | return false; |
| 594 | } |
| 595 | exif_set_rational(entry->data, EXIF_BYTE_ORDER_INTEL, |
| 596 | {static_cast<ExifLong>(t.tm_hour), 1}); |
| 597 | exif_set_rational(entry->data + sizeof(ExifRational), EXIF_BYTE_ORDER_INTEL, |
| 598 | {static_cast<ExifLong>(t.tm_min), 1}); |
| 599 | exif_set_rational(entry->data + 2 * sizeof(ExifRational), EXIF_BYTE_ORDER_INTEL, |
| 600 | {static_cast<ExifLong>(t.tm_sec), 1}); |
| 601 | |
| 602 | return true; |
| 603 | } |
| 604 | |
| 605 | bool ExifUtilsImpl::setImageHeight(uint32_t length) { |
| 606 | SET_LONG(EXIF_IFD_0, EXIF_TAG_IMAGE_LENGTH, length); |
| 607 | SET_LONG(EXIF_IFD_EXIF, EXIF_TAG_PIXEL_Y_DIMENSION, length); |
| 608 | return true; |
| 609 | } |
| 610 | |
| 611 | bool ExifUtilsImpl::setImageWidth(uint32_t width) { |
| 612 | SET_LONG(EXIF_IFD_0, EXIF_TAG_IMAGE_WIDTH, width); |
| 613 | SET_LONG(EXIF_IFD_EXIF, EXIF_TAG_PIXEL_X_DIMENSION, width); |
| 614 | return true; |
| 615 | } |
| 616 | |
| 617 | bool ExifUtilsImpl::setIsoSpeedRating(uint16_t iso_speed_ratings) { |
| 618 | SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_ISO_SPEED_RATINGS, iso_speed_ratings); |
| 619 | return true; |
| 620 | } |
| 621 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 622 | bool ExifUtilsImpl::setMaxAperture(float aperture) { |
| 623 | float maxAperture = convertToApex(aperture); |
| 624 | SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_MAX_APERTURE_VALUE, |
| 625 | static_cast<uint32_t>(std::round(maxAperture * kRationalPrecision)), |
| 626 | kRationalPrecision); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 627 | return true; |
| 628 | } |
| 629 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 630 | bool ExifUtilsImpl::setExposureBias(int32_t ev, |
| 631 | uint32_t ev_step_numerator, uint32_t ev_step_denominator) { |
| 632 | SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_EXPOSURE_BIAS_VALUE, |
| 633 | ev * ev_step_numerator, ev_step_denominator); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 634 | return true; |
| 635 | } |
| 636 | |
Emilian Peev | 4b08d5d | 2019-02-01 09:53:14 -0800 | [diff] [blame^] | 637 | bool ExifUtilsImpl::setOrientation(uint16_t degrees) { |
| 638 | ExifOrientation value = ExifOrientation::ORIENTATION_0_DEGREES; |
| 639 | switch (degrees) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 640 | case 90: |
Emilian Peev | 4b08d5d | 2019-02-01 09:53:14 -0800 | [diff] [blame^] | 641 | value = ExifOrientation::ORIENTATION_90_DEGREES; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 642 | break; |
| 643 | case 180: |
Emilian Peev | 4b08d5d | 2019-02-01 09:53:14 -0800 | [diff] [blame^] | 644 | value = ExifOrientation::ORIENTATION_180_DEGREES; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 645 | break; |
| 646 | case 270: |
Emilian Peev | 4b08d5d | 2019-02-01 09:53:14 -0800 | [diff] [blame^] | 647 | value = ExifOrientation::ORIENTATION_270_DEGREES; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 648 | break; |
| 649 | default: |
| 650 | break; |
| 651 | } |
Emilian Peev | 4b08d5d | 2019-02-01 09:53:14 -0800 | [diff] [blame^] | 652 | return setOrientationValue(value); |
| 653 | } |
| 654 | |
| 655 | bool ExifUtilsImpl::setOrientationValue(ExifOrientation orientationValue) { |
| 656 | SET_SHORT(EXIF_IFD_0, EXIF_TAG_ORIENTATION, orientationValue); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 657 | return true; |
| 658 | } |
| 659 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 660 | bool ExifUtilsImpl::setShutterSpeed(float exposure_time) { |
| 661 | float shutterSpeed = -log2f(exposure_time); |
| 662 | SET_SRATIONAL(EXIF_IFD_EXIF, EXIF_TAG_SHUTTER_SPEED_VALUE, |
| 663 | static_cast<uint32_t>(shutterSpeed * kRationalPrecision), kRationalPrecision); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 664 | return true; |
| 665 | } |
| 666 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 667 | bool ExifUtilsImpl::setSubjectDistance(float diopters) { |
| 668 | const static float kInfinityDiopters = 1.0e-6; |
| 669 | uint32_t numerator, denominator; |
| 670 | uint16_t distanceRange; |
| 671 | if (diopters > kInfinityDiopters) { |
| 672 | float focusDistance = 1.0f / diopters; |
| 673 | numerator = static_cast<uint32_t>(std::round(focusDistance * kRationalPrecision)); |
| 674 | denominator = kRationalPrecision; |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 675 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 676 | if (focusDistance < 1.0f) { |
| 677 | distanceRange = 1; // Macro |
| 678 | } else if (focusDistance < 3.0f) { |
| 679 | distanceRange = 2; // Close |
| 680 | } else { |
| 681 | distanceRange = 3; // Distant |
| 682 | } |
| 683 | } else { |
| 684 | numerator = 0xFFFFFFFF; |
| 685 | denominator = 1; |
| 686 | distanceRange = 3; // Distant |
| 687 | } |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 688 | SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_SUBJECT_DISTANCE, numerator, denominator); |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 689 | SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_SUBJECT_DISTANCE_RANGE, distanceRange); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 690 | return true; |
| 691 | } |
| 692 | |
| 693 | bool ExifUtilsImpl::setSubsecTime(const std::string& subsec_time) { |
| 694 | SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_SUB_SEC_TIME, EXIF_FORMAT_ASCII, subsec_time); |
| 695 | SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_SUB_SEC_TIME_ORIGINAL, EXIF_FORMAT_ASCII, subsec_time); |
| 696 | SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_SUB_SEC_TIME_DIGITIZED, EXIF_FORMAT_ASCII, subsec_time); |
| 697 | return true; |
| 698 | } |
| 699 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 700 | bool ExifUtilsImpl::setWhiteBalance(uint8_t white_balance) { |
| 701 | uint16_t whiteBalance = (white_balance == ANDROID_CONTROL_AWB_MODE_AUTO) ? 0 : 1; |
| 702 | SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_WHITE_BALANCE, whiteBalance); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 703 | return true; |
| 704 | } |
| 705 | |
| 706 | bool ExifUtilsImpl::generateApp1() { |
| 707 | destroyApp1(); |
| 708 | // Save the result into |app1_buffer_|. |
| 709 | exif_data_save_data(exif_data_, &app1_buffer_, &app1_length_); |
| 710 | if (!app1_length_) { |
| 711 | ALOGE("%s: Allocate memory for app1_buffer_ failed", __FUNCTION__); |
| 712 | return false; |
| 713 | } |
| 714 | /* |
| 715 | * The JPEG segment size is 16 bits in spec. The size of APP1 segment should |
| 716 | * be smaller than 65533 because there are two bytes for segment size field. |
| 717 | */ |
| 718 | if (app1_length_ > 65533) { |
| 719 | destroyApp1(); |
| 720 | ALOGE("%s: The size of APP1 segment is too large", __FUNCTION__); |
| 721 | return false; |
| 722 | } |
| 723 | return true; |
| 724 | } |
| 725 | |
| 726 | const uint8_t* ExifUtilsImpl::getApp1Buffer() { |
| 727 | return app1_buffer_; |
| 728 | } |
| 729 | |
| 730 | unsigned int ExifUtilsImpl::getApp1Length() { |
| 731 | return app1_length_; |
| 732 | } |
| 733 | |
| 734 | bool ExifUtilsImpl::setExifVersion(const std::string& exif_version) { |
| 735 | SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_EXIF_VERSION, EXIF_FORMAT_UNDEFINED, exif_version); |
| 736 | return true; |
| 737 | } |
| 738 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 739 | void ExifUtilsImpl::reset() { |
| 740 | destroyApp1(); |
| 741 | if (exif_data_) { |
| 742 | /* |
| 743 | * Since we decided to ignore the original APP1, we are sure that there is |
| 744 | * no thumbnail allocated by libexif. |exif_data_->data| is actually |
| 745 | * allocated by JpegCompressor. sets |exif_data_->data| to nullptr to |
| 746 | * prevent exif_data_unref() destroy it incorrectly. |
| 747 | */ |
| 748 | exif_data_->data = nullptr; |
| 749 | exif_data_->size = 0; |
| 750 | exif_data_unref(exif_data_); |
| 751 | exif_data_ = nullptr; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | std::unique_ptr<ExifEntry> ExifUtilsImpl::addVariableLengthEntry(ExifIfd ifd, |
| 756 | ExifTag tag, ExifFormat format, uint64_t components, unsigned int size) { |
| 757 | // Remove old entry if exists. |
| 758 | exif_content_remove_entry(exif_data_->ifd[ifd], |
| 759 | exif_content_get_entry(exif_data_->ifd[ifd], tag)); |
| 760 | ExifMem* mem = exif_mem_new_default(); |
| 761 | if (!mem) { |
| 762 | ALOGE("%s: Allocate memory for exif entry failed", __FUNCTION__); |
| 763 | return nullptr; |
| 764 | } |
| 765 | std::unique_ptr<ExifEntry> entry(exif_entry_new_mem(mem)); |
| 766 | if (!entry) { |
| 767 | ALOGE("%s: Allocate memory for exif entry failed", __FUNCTION__); |
| 768 | exif_mem_unref(mem); |
| 769 | return nullptr; |
| 770 | } |
| 771 | void* tmpBuffer = exif_mem_alloc(mem, size); |
| 772 | if (!tmpBuffer) { |
| 773 | ALOGE("%s: Allocate memory for exif entry failed", __FUNCTION__); |
| 774 | exif_mem_unref(mem); |
| 775 | return nullptr; |
| 776 | } |
| 777 | |
| 778 | entry->data = static_cast<unsigned char*>(tmpBuffer); |
| 779 | entry->tag = tag; |
| 780 | entry->format = format; |
| 781 | entry->components = components; |
| 782 | entry->size = size; |
| 783 | |
| 784 | exif_content_add_entry(exif_data_->ifd[ifd], entry.get()); |
| 785 | exif_mem_unref(mem); |
| 786 | |
| 787 | return entry; |
| 788 | } |
| 789 | |
| 790 | std::unique_ptr<ExifEntry> ExifUtilsImpl::addEntry(ExifIfd ifd, ExifTag tag) { |
| 791 | std::unique_ptr<ExifEntry> entry(exif_content_get_entry(exif_data_->ifd[ifd], tag)); |
| 792 | if (entry) { |
| 793 | // exif_content_get_entry() won't ref the entry, so we ref here. |
| 794 | exif_entry_ref(entry.get()); |
| 795 | return entry; |
| 796 | } |
| 797 | entry.reset(exif_entry_new()); |
| 798 | if (!entry) { |
| 799 | ALOGE("%s: Allocate memory for exif entry failed", __FUNCTION__); |
| 800 | return nullptr; |
| 801 | } |
| 802 | entry->tag = tag; |
| 803 | exif_content_add_entry(exif_data_->ifd[ifd], entry.get()); |
| 804 | exif_entry_initialize(entry.get(), tag); |
| 805 | return entry; |
| 806 | } |
| 807 | |
| 808 | bool ExifUtilsImpl::setShort(ExifIfd ifd, ExifTag tag, uint16_t value, const std::string& msg) { |
| 809 | std::unique_ptr<ExifEntry> entry = addEntry(ifd, tag); |
| 810 | if (!entry) { |
| 811 | ALOGE("%s: Adding '%s' entry failed", __FUNCTION__, msg.c_str()); |
| 812 | return false; |
| 813 | } |
| 814 | exif_set_short(entry->data, EXIF_BYTE_ORDER_INTEL, value); |
| 815 | return true; |
| 816 | } |
| 817 | |
| 818 | bool ExifUtilsImpl::setLong(ExifIfd ifd, ExifTag tag, uint32_t value, const std::string& msg) { |
| 819 | std::unique_ptr<ExifEntry> entry = addEntry(ifd, tag); |
| 820 | if (!entry) { |
| 821 | ALOGE("%s: Adding '%s' entry failed", __FUNCTION__, msg.c_str()); |
| 822 | return false; |
| 823 | } |
| 824 | exif_set_long(entry->data, EXIF_BYTE_ORDER_INTEL, value); |
| 825 | return true; |
| 826 | } |
| 827 | |
| 828 | bool ExifUtilsImpl::setRational(ExifIfd ifd, ExifTag tag, uint32_t numerator, |
| 829 | uint32_t denominator, const std::string& msg) { |
| 830 | std::unique_ptr<ExifEntry> entry = addEntry(ifd, tag); |
| 831 | if (!entry) { |
| 832 | ALOGE("%s: Adding '%s' entry failed", __FUNCTION__, msg.c_str()); |
| 833 | return false; |
| 834 | } |
| 835 | exif_set_rational(entry->data, EXIF_BYTE_ORDER_INTEL, {numerator, denominator}); |
| 836 | return true; |
| 837 | } |
| 838 | |
| 839 | bool ExifUtilsImpl::setSRational(ExifIfd ifd, ExifTag tag, int32_t numerator, |
| 840 | int32_t denominator, const std::string& msg) { |
| 841 | std::unique_ptr<ExifEntry> entry = addEntry(ifd, tag); |
| 842 | if (!entry) { |
| 843 | ALOGE("%s: Adding '%s' entry failed", __FUNCTION__, msg.c_str()); |
| 844 | return false; |
| 845 | } |
| 846 | exif_set_srational(entry->data, EXIF_BYTE_ORDER_INTEL, {numerator, denominator}); |
| 847 | return true; |
| 848 | } |
| 849 | |
| 850 | bool ExifUtilsImpl::setString(ExifIfd ifd, ExifTag tag, ExifFormat format, |
| 851 | const std::string& buffer, const std::string& msg) { |
| 852 | size_t entry_size = buffer.length(); |
| 853 | // Since the exif format is undefined, NULL termination is not necessary. |
| 854 | if (format == EXIF_FORMAT_ASCII) { |
| 855 | entry_size++; |
| 856 | } |
| 857 | std::unique_ptr<ExifEntry> entry = |
| 858 | addVariableLengthEntry(ifd, tag, format, entry_size, entry_size); |
| 859 | if (!entry) { |
| 860 | ALOGE("%s: Adding '%s' entry failed", __FUNCTION__, msg.c_str()); |
| 861 | return false; |
| 862 | } |
| 863 | memcpy(entry->data, buffer.c_str(), entry_size); |
| 864 | return true; |
| 865 | } |
| 866 | |
| 867 | void ExifUtilsImpl::destroyApp1() { |
| 868 | /* |
| 869 | * Since there is no API to access ExifMem in ExifData->priv, we use free |
| 870 | * here, which is the default free function in libexif. See |
| 871 | * exif_data_save_data() for detail. |
| 872 | */ |
| 873 | free(app1_buffer_); |
| 874 | app1_buffer_ = nullptr; |
| 875 | app1_length_ = 0; |
| 876 | } |
| 877 | |
| 878 | bool ExifUtilsImpl::setFromMetadata(const CameraMetadata& metadata, |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 879 | const CameraMetadata& staticInfo, |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 880 | const size_t imageWidth, const size_t imageHeight) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 881 | if (!setImageWidth(imageWidth) || |
| 882 | !setImageHeight(imageHeight)) { |
| 883 | ALOGE("%s: setting image resolution failed.", __FUNCTION__); |
| 884 | return false; |
| 885 | } |
| 886 | |
| 887 | struct timespec tp; |
| 888 | struct tm time_info; |
| 889 | bool time_available = clock_gettime(CLOCK_REALTIME, &tp) != -1; |
| 890 | localtime_r(&tp.tv_sec, &time_info); |
| 891 | if (!setDateTime(time_info)) { |
| 892 | ALOGE("%s: setting data time failed.", __FUNCTION__); |
| 893 | return false; |
| 894 | } |
| 895 | |
| 896 | float focal_length; |
| 897 | camera_metadata_ro_entry entry = metadata.find(ANDROID_LENS_FOCAL_LENGTH); |
| 898 | if (entry.count) { |
| 899 | focal_length = entry.data.f[0]; |
| 900 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 901 | if (!setFocalLength(focal_length)) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 902 | ALOGE("%s: setting focal length failed.", __FUNCTION__); |
| 903 | return false; |
| 904 | } |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 905 | |
| 906 | camera_metadata_ro_entry sensorSizeEntry = |
| 907 | staticInfo.find(ANDROID_SENSOR_INFO_PHYSICAL_SIZE); |
| 908 | if (sensorSizeEntry.count == 2) { |
| 909 | if (!setFocalLengthIn35mmFilm( |
| 910 | focal_length, sensorSizeEntry.data.f[0], sensorSizeEntry.data.f[1])) { |
| 911 | ALOGE("%s: setting focal length in 35mm failed.", __FUNCTION__); |
| 912 | return false; |
| 913 | } |
| 914 | } |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 915 | } else { |
| 916 | ALOGV("%s: Cannot find focal length in metadata.", __FUNCTION__); |
| 917 | } |
| 918 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 919 | if (metadata.exists(ANDROID_SCALER_CROP_REGION) && |
| 920 | staticInfo.exists(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE)) { |
| 921 | entry = metadata.find(ANDROID_SCALER_CROP_REGION); |
| 922 | camera_metadata_ro_entry activeArrayEntry = |
| 923 | staticInfo.find(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE); |
| 924 | |
| 925 | if (!setDigitalZoomRatio(entry.data.i32[2], entry.data.i32[3], |
| 926 | activeArrayEntry.data.i32[2], activeArrayEntry.data.i32[3])) { |
| 927 | ALOGE("%s: setting digital zoom ratio failed.", __FUNCTION__); |
| 928 | return false; |
| 929 | } |
| 930 | } |
| 931 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 932 | if (metadata.exists(ANDROID_JPEG_GPS_COORDINATES)) { |
| 933 | entry = metadata.find(ANDROID_JPEG_GPS_COORDINATES); |
| 934 | if (entry.count < 3) { |
| 935 | ALOGE("%s: Gps coordinates in metadata is not complete.", __FUNCTION__); |
| 936 | return false; |
| 937 | } |
| 938 | if (!setGpsLatitude(entry.data.d[0])) { |
| 939 | ALOGE("%s: setting gps latitude failed.", __FUNCTION__); |
| 940 | return false; |
| 941 | } |
| 942 | if (!setGpsLongitude(entry.data.d[1])) { |
| 943 | ALOGE("%s: setting gps longitude failed.", __FUNCTION__); |
| 944 | return false; |
| 945 | } |
| 946 | if (!setGpsAltitude(entry.data.d[2])) { |
| 947 | ALOGE("%s: setting gps altitude failed.", __FUNCTION__); |
| 948 | return false; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | if (metadata.exists(ANDROID_JPEG_GPS_PROCESSING_METHOD)) { |
| 953 | entry = metadata.find(ANDROID_JPEG_GPS_PROCESSING_METHOD); |
| 954 | std::string method_str(reinterpret_cast<const char*>(entry.data.u8)); |
| 955 | if (!setGpsProcessingMethod(method_str)) { |
| 956 | ALOGE("%s: setting gps processing method failed.", __FUNCTION__); |
| 957 | return false; |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | if (time_available && metadata.exists(ANDROID_JPEG_GPS_TIMESTAMP)) { |
| 962 | entry = metadata.find(ANDROID_JPEG_GPS_TIMESTAMP); |
| 963 | time_t timestamp = static_cast<time_t>(entry.data.i64[0]); |
| 964 | if (gmtime_r(×tamp, &time_info)) { |
| 965 | if (!setGpsTimestamp(time_info)) { |
| 966 | ALOGE("%s: setting gps timestamp failed.", __FUNCTION__); |
| 967 | return false; |
| 968 | } |
| 969 | } else { |
| 970 | ALOGE("%s: Time tranformation failed.", __FUNCTION__); |
| 971 | return false; |
| 972 | } |
| 973 | } |
| 974 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 975 | if (staticInfo.exists(ANDROID_CONTROL_AE_COMPENSATION_STEP) && |
| 976 | metadata.exists(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION)) { |
| 977 | entry = metadata.find(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION); |
| 978 | camera_metadata_ro_entry stepEntry = |
| 979 | staticInfo.find(ANDROID_CONTROL_AE_COMPENSATION_STEP); |
| 980 | if (!setExposureBias(entry.data.i32[0], stepEntry.data.r[0].numerator, |
| 981 | stepEntry.data.r[0].denominator)) { |
| 982 | ALOGE("%s: setting exposure bias failed.", __FUNCTION__); |
| 983 | return false; |
| 984 | } |
| 985 | } |
| 986 | |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 987 | if (metadata.exists(ANDROID_JPEG_ORIENTATION)) { |
| 988 | entry = metadata.find(ANDROID_JPEG_ORIENTATION); |
| 989 | if (!setOrientation(entry.data.i32[0])) { |
| 990 | ALOGE("%s: setting orientation failed.", __FUNCTION__); |
| 991 | return false; |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | if (metadata.exists(ANDROID_SENSOR_EXPOSURE_TIME)) { |
| 996 | entry = metadata.find(ANDROID_SENSOR_EXPOSURE_TIME); |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 997 | float exposure_time = 1.0f * entry.data.i64[0] / 1e9; |
| 998 | if (!setExposureTime(exposure_time)) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 999 | ALOGE("%s: setting exposure time failed.", __FUNCTION__); |
| 1000 | return false; |
| 1001 | } |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 1002 | |
| 1003 | if (!setShutterSpeed(exposure_time)) { |
| 1004 | ALOGE("%s: setting shutter speed failed.", __FUNCTION__); |
| 1005 | return false; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | if (metadata.exists(ANDROID_LENS_FOCUS_DISTANCE)) { |
| 1010 | entry = metadata.find(ANDROID_LENS_FOCUS_DISTANCE); |
| 1011 | if (!setSubjectDistance(entry.data.f[0])) { |
| 1012 | ALOGE("%s: setting subject distance failed.", __FUNCTION__); |
| 1013 | return false; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | if (metadata.exists(ANDROID_SENSOR_SENSITIVITY)) { |
| 1018 | entry = metadata.find(ANDROID_SENSOR_SENSITIVITY); |
| 1019 | int32_t iso = entry.data.i32[0]; |
| 1020 | camera_metadata_ro_entry postRawSensEntry = |
| 1021 | metadata.find(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST); |
| 1022 | if (postRawSensEntry.count > 0) { |
| 1023 | iso = iso * postRawSensEntry.data.i32[0] / 100; |
| 1024 | } |
| 1025 | |
| 1026 | if (!setIsoSpeedRating(static_cast<uint16_t>(iso))) { |
| 1027 | ALOGE("%s: setting iso rating failed.", __FUNCTION__); |
| 1028 | return false; |
| 1029 | } |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | if (metadata.exists(ANDROID_LENS_APERTURE)) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1033 | entry = metadata.find(ANDROID_LENS_APERTURE); |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 1034 | if (!setFNumber(entry.data.f[0])) { |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1035 | ALOGE("%s: setting F number failed.", __FUNCTION__); |
| 1036 | return false; |
| 1037 | } |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 1038 | if (!setAperture(entry.data.f[0])) { |
| 1039 | ALOGE("%s: setting aperture failed.", __FUNCTION__); |
| 1040 | return false; |
| 1041 | } |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 1044 | static const uint16_t kSRGBColorSpace = 1; |
| 1045 | if (!setColorSpace(kSRGBColorSpace)) { |
| 1046 | ALOGE("%s: setting color space failed.", __FUNCTION__); |
| 1047 | return false; |
| 1048 | } |
| 1049 | |
| 1050 | if (staticInfo.exists(ANDROID_LENS_INFO_AVAILABLE_APERTURES)) { |
| 1051 | entry = staticInfo.find(ANDROID_LENS_INFO_AVAILABLE_APERTURES); |
| 1052 | if (!setMaxAperture(entry.data.f[0])) { |
| 1053 | ALOGE("%s: setting max aperture failed.", __FUNCTION__); |
| 1054 | return false; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | if (staticInfo.exists(ANDROID_FLASH_INFO_AVAILABLE)) { |
| 1059 | entry = staticInfo.find(ANDROID_FLASH_INFO_AVAILABLE); |
| 1060 | camera_metadata_ro_entry flashStateEntry = metadata.find(ANDROID_FLASH_STATE); |
| 1061 | camera_metadata_ro_entry aeModeEntry = metadata.find(ANDROID_CONTROL_AE_MODE); |
| 1062 | uint8_t flashState = flashStateEntry.count > 0 ? |
| 1063 | flashStateEntry.data.u8[0] : ANDROID_FLASH_STATE_UNAVAILABLE; |
| 1064 | uint8_t aeMode = aeModeEntry.count > 0 ? |
| 1065 | aeModeEntry.data.u8[0] : ANDROID_CONTROL_AE_MODE_OFF; |
| 1066 | |
| 1067 | if (!setFlash(entry.data.u8[0], flashState, aeMode)) { |
| 1068 | ALOGE("%s: setting flash failed.", __FUNCTION__); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1069 | return false; |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | if (metadata.exists(ANDROID_CONTROL_AWB_MODE)) { |
| 1074 | entry = metadata.find(ANDROID_CONTROL_AWB_MODE); |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 1075 | if (!setWhiteBalance(entry.data.u8[0])) { |
| 1076 | ALOGE("%s: setting white balance failed.", __FUNCTION__); |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1077 | return false; |
| 1078 | } |
| 1079 | } |
| 1080 | |
Shuzhen Wang | e7f4b46 | 2019-02-12 08:43:07 -0800 | [diff] [blame] | 1081 | if (metadata.exists(ANDROID_CONTROL_AE_MODE)) { |
| 1082 | entry = metadata.find(ANDROID_CONTROL_AE_MODE); |
| 1083 | if (!setExposureMode(entry.data.u8[0])) { |
| 1084 | ALOGE("%s: setting exposure mode failed.", __FUNCTION__); |
| 1085 | return false; |
| 1086 | } |
| 1087 | } |
Shuzhen Wang | 68ac7ad | 2019-01-30 14:03:28 -0800 | [diff] [blame] | 1088 | if (time_available) { |
| 1089 | char str[4]; |
| 1090 | if (snprintf(str, sizeof(str), "%03ld", tp.tv_nsec / 1000000) < 0) { |
| 1091 | ALOGE("%s: Subsec is invalid: %ld", __FUNCTION__, tp.tv_nsec); |
| 1092 | return false; |
| 1093 | } |
| 1094 | if (!setSubsecTime(std::string(str))) { |
| 1095 | ALOGE("%s: setting subsec time failed.", __FUNCTION__); |
| 1096 | return false; |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | return true; |
| 1101 | } |
| 1102 | |
| 1103 | } // namespace camera3 |
| 1104 | } // namespace android |