blob: a4027cc4bcb631c66878b2c3e678910077015fe2 [file] [log] [blame]
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001/*
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
31extern "C" {
32#include <libexif/exif-data.h>
33}
34
35namespace std {
36
37template <>
38struct default_delete<ExifEntry> {
39 inline void operator()(ExifEntry* entry) const { exif_entry_unref(entry); }
40};
41
42} // namespace std
43
44
45namespace android {
46namespace camera3 {
47
48
49class ExifUtilsImpl : public ExifUtils {
50public:
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);
58
59 // set all known fields from a metadata structure
60 virtual bool setFromMetadata(const CameraMetadata& metadata,
61 const size_t imageWidth,
62 const size_t imageHeight);
63
64 // sets the len aperture.
65 // Returns false if memory allocation fails.
66 virtual bool setAperture(uint32_t numerator, uint32_t denominator);
67
68 // sets the value of brightness.
69 // Returns false if memory allocation fails.
70 virtual bool setBrightness(int32_t numerator, int32_t denominator);
71
72 // sets the color space.
73 // Returns false if memory allocation fails.
74 virtual bool setColorSpace(uint16_t color_space);
75
76 // sets the information to compressed data.
77 // Returns false if memory allocation fails.
78 virtual bool setComponentsConfiguration(const std::string& components_configuration);
79
80 // sets the compression scheme used for the image data.
81 // Returns false if memory allocation fails.
82 virtual bool setCompression(uint16_t compression);
83
84 // sets image contrast.
85 // Returns false if memory allocation fails.
86 virtual bool setContrast(uint16_t contrast);
87
88 // sets the date and time of image last modified. It takes local time. The
89 // name of the tag is DateTime in IFD0.
90 // Returns false if memory allocation fails.
91 virtual bool setDateTime(const struct tm& t);
92
93 // sets the image description.
94 // Returns false if memory allocation fails.
95 virtual bool setDescription(const std::string& description);
96
97 // sets the digital zoom ratio. If the numerator is 0, it means digital zoom
98 // was not used.
99 // Returns false if memory allocation fails.
100 virtual bool setDigitalZoomRatio(uint32_t numerator, uint32_t denominator);
101
102 // sets the exposure bias.
103 // Returns false if memory allocation fails.
104 virtual bool setExposureBias(int32_t numerator, int32_t denominator);
105
106 // sets the exposure mode set when the image was shot.
107 // Returns false if memory allocation fails.
108 virtual bool setExposureMode(uint16_t exposure_mode);
109
110 // sets the program used by the camera to set exposure when the picture is
111 // taken.
112 // Returns false if memory allocation fails.
113 virtual bool setExposureProgram(uint16_t exposure_program);
114
115 // sets the exposure time, given in seconds.
116 // Returns false if memory allocation fails.
117 virtual bool setExposureTime(uint32_t numerator, uint32_t denominator);
118
119 // sets the status of flash.
120 // Returns false if memory allocation fails.
121 virtual bool setFlash(uint16_t flash);
122
123 // sets the F number.
124 // Returns false if memory allocation fails.
125 virtual bool setFNumber(uint32_t numerator, uint32_t denominator);
126
127 // sets the focal length of lens used to take the image in millimeters.
128 // Returns false if memory allocation fails.
129 virtual bool setFocalLength(uint32_t numerator, uint32_t denominator);
130
131 // sets the degree of overall image gain adjustment.
132 // Returns false if memory allocation fails.
133 virtual bool setGainControl(uint16_t gain_control);
134
135 // sets the altitude in meters.
136 // Returns false if memory allocation fails.
137 virtual bool setGpsAltitude(double altitude);
138
139 // sets the latitude with degrees minutes seconds format.
140 // Returns false if memory allocation fails.
141 virtual bool setGpsLatitude(double latitude);
142
143 // sets the longitude with degrees minutes seconds format.
144 // Returns false if memory allocation fails.
145 virtual bool setGpsLongitude(double longitude);
146
147 // sets GPS processing method.
148 // Returns false if memory allocation fails.
149 virtual bool setGpsProcessingMethod(const std::string& method);
150
151 // sets GPS date stamp and time stamp (atomic clock). It takes UTC time.
152 // Returns false if memory allocation fails.
153 virtual bool setGpsTimestamp(const struct tm& t);
154
155 // sets the length (number of rows) of main image.
156 // Returns false if memory allocation fails.
157 virtual bool setImageHeight(uint32_t length);
158
159 // sets the width (number of columes) of main image.
160 // Returns false if memory allocation fails.
161 virtual bool setImageWidth(uint32_t width);
162
163 // sets the ISO speed.
164 // Returns false if memory allocation fails.
165 virtual bool setIsoSpeedRating(uint16_t iso_speed_ratings);
166
167 // sets the kind of light source.
168 // Returns false if memory allocation fails.
169 virtual bool setLightSource(uint16_t light_source);
170
171 // sets the smallest F number of the lens.
172 // Returns false if memory allocation fails.
173 virtual bool setMaxAperture(uint32_t numerator, uint32_t denominator);
174
175 // sets the metering mode.
176 // Returns false if memory allocation fails.
177 virtual bool setMeteringMode(uint16_t metering_mode);
178
179 // sets image orientation.
180 // Returns false if memory allocation fails.
181 virtual bool setOrientation(uint16_t orientation);
182
183 // sets the unit for measuring XResolution and YResolution.
184 // Returns false if memory allocation fails.
185 virtual bool setResolutionUnit(uint16_t resolution_unit);
186
187 // sets image saturation.
188 // Returns false if memory allocation fails.
189 virtual bool setSaturation(uint16_t saturation);
190
191 // sets the type of scene that was shot.
192 // Returns false if memory allocation fails.
193 virtual bool setSceneCaptureType(uint16_t type);
194
195 // sets image sharpness.
196 // Returns false if memory allocation fails.
197 virtual bool setSharpness(uint16_t sharpness);
198
199 // sets the shutter speed.
200 // Returns false if memory allocation fails.
201 virtual bool setShutterSpeed(int32_t numerator, int32_t denominator);
202
203 // sets the distance to the subject, given in meters.
204 // Returns false if memory allocation fails.
205 virtual bool setSubjectDistance(uint32_t numerator, uint32_t denominator);
206
207 // sets the fractions of seconds for the <DateTime> tag.
208 // Returns false if memory allocation fails.
209 virtual bool setSubsecTime(const std::string& subsec_time);
210
211 // sets the white balance mode set when the image was shot.
212 // Returns false if memory allocation fails.
213 virtual bool setWhiteBalance(uint16_t white_balance);
214
215 // sets the number of pixels per resolution unit in the image width.
216 // Returns false if memory allocation fails.
217 virtual bool setXResolution(uint32_t numerator, uint32_t denominator);
218
219 // sets the position of chrominance components in relation to the luminance
220 // component.
221 // Returns false if memory allocation fails.
222 virtual bool setYCbCrPositioning(uint16_t ycbcr_positioning);
223
224 // sets the number of pixels per resolution unit in the image length.
225 // Returns false if memory allocation fails.
226 virtual bool setYResolution(uint32_t numerator, uint32_t denominator);
227
228 // sets the manufacturer of camera.
229 // Returns false if memory allocation fails.
230 virtual bool setMake(const std::string& make);
231
232 // sets the model number of camera.
233 // Returns false if memory allocation fails.
234 virtual bool setModel(const std::string& model);
235
236 // Generates APP1 segment.
237 // Returns false if generating APP1 segment fails.
238 virtual bool generateApp1();
239
240 // Gets buffer of APP1 segment. This method must be called only after calling
241 // GenerateAPP1().
242 virtual const uint8_t* getApp1Buffer();
243
244 // Gets length of APP1 segment. This method must be called only after calling
245 // GenerateAPP1().
246 virtual unsigned int getApp1Length();
247
248 protected:
249 // sets the version of this standard supported.
250 // Returns false if memory allocation fails.
251 virtual bool setExifVersion(const std::string& exif_version);
252
253 // Resets the pointers and memories.
254 virtual void reset();
255
256 // Adds a variable length tag to |exif_data_|. It will remove the original one
257 // if the tag exists.
258 // Returns the entry of the tag. The reference count of returned ExifEntry is
259 // two.
260 virtual std::unique_ptr<ExifEntry> addVariableLengthEntry(ExifIfd ifd,
261 ExifTag tag, ExifFormat format, uint64_t components, unsigned int size);
262
263 // Adds a entry of |tag| in |exif_data_|. It won't remove the original one if
264 // the tag exists.
265 // Returns the entry of the tag. It adds one reference count to returned
266 // ExifEntry.
267 virtual std::unique_ptr<ExifEntry> addEntry(ExifIfd ifd, ExifTag tag);
268
269 // Helpe functions to add exif data with different types.
270 virtual bool setShort(ExifIfd ifd, ExifTag tag, uint16_t value, const std::string& msg);
271
272 virtual bool setLong(ExifIfd ifd, ExifTag tag, uint32_t value, const std::string& msg);
273
274 virtual bool setRational(ExifIfd ifd, ExifTag tag, uint32_t numerator,
275 uint32_t denominator, const std::string& msg);
276
277 virtual bool setSRational(ExifIfd ifd, ExifTag tag, int32_t numerator,
278 int32_t denominator, const std::string& msg);
279
280 virtual bool setString(ExifIfd ifd, ExifTag tag, ExifFormat format,
281 const std::string& buffer, const std::string& msg);
282
283 // Destroys the buffer of APP1 segment if exists.
284 virtual void destroyApp1();
285
286 // The Exif data (APP1). Owned by this class.
287 ExifData* exif_data_;
288 // The raw data of APP1 segment. It's allocated by ExifMem in |exif_data_| but
289 // owned by this class.
290 uint8_t* app1_buffer_;
291 // The length of |app1_buffer_|.
292 unsigned int app1_length_;
293
294};
295
296#define SET_SHORT(ifd, tag, value) \
297 do { \
298 if (setShort(ifd, tag, value, #tag) == false) \
299 return false; \
300 } while (0);
301
302#define SET_LONG(ifd, tag, value) \
303 do { \
304 if (setLong(ifd, tag, value, #tag) == false) \
305 return false; \
306 } while (0);
307
308#define SET_RATIONAL(ifd, tag, numerator, denominator) \
309 do { \
310 if (setRational(ifd, tag, numerator, denominator, #tag) == false) \
311 return false; \
312 } while (0);
313
314#define SET_SRATIONAL(ifd, tag, numerator, denominator) \
315 do { \
316 if (setSRational(ifd, tag, numerator, denominator, #tag) == false) \
317 return false; \
318 } while (0);
319
320#define SET_STRING(ifd, tag, format, buffer) \
321 do { \
322 if (setString(ifd, tag, format, buffer, #tag) == false) \
323 return false; \
324 } while (0);
325
326// This comes from the Exif Version 2.2 standard table 6.
327const char gExifAsciiPrefix[] = {0x41, 0x53, 0x43, 0x49, 0x49, 0x0, 0x0, 0x0};
328
329static void setLatitudeOrLongitudeData(unsigned char* data, double num) {
330 // Take the integer part of |num|.
331 ExifLong degrees = static_cast<ExifLong>(num);
332 ExifLong minutes = static_cast<ExifLong>(60 * (num - degrees));
333 ExifLong microseconds =
334 static_cast<ExifLong>(3600000000u * (num - degrees - minutes / 60.0));
335 exif_set_rational(data, EXIF_BYTE_ORDER_INTEL, {degrees, 1});
336 exif_set_rational(data + sizeof(ExifRational), EXIF_BYTE_ORDER_INTEL, {minutes, 1});
337 exif_set_rational(data + 2 * sizeof(ExifRational), EXIF_BYTE_ORDER_INTEL,
338 {microseconds, 1000000});
339}
340
341ExifUtils *ExifUtils::create() {
342 return new ExifUtilsImpl();
343}
344
345ExifUtils::~ExifUtils() {
346}
347
348ExifUtilsImpl::ExifUtilsImpl()
349 : exif_data_(nullptr), app1_buffer_(nullptr), app1_length_(0) {}
350
351ExifUtilsImpl::~ExifUtilsImpl() {
352 reset();
353}
354
355
356bool ExifUtilsImpl::initialize(const unsigned char *app1Segment, size_t app1SegmentSize) {
357 reset();
358 exif_data_ = exif_data_new_from_data(app1Segment, app1SegmentSize);
359 if (exif_data_ == nullptr) {
360 ALOGE("%s: allocate memory for exif_data_ failed", __FUNCTION__);
361 return false;
362 }
363 // set the image options.
364 exif_data_set_option(exif_data_, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
365 exif_data_set_data_type(exif_data_, EXIF_DATA_TYPE_COMPRESSED);
366 exif_data_set_byte_order(exif_data_, EXIF_BYTE_ORDER_INTEL);
367
368 // set exif version to 2.2.
369 if (!setExifVersion("0220")) {
370 return false;
371 }
372
373 return true;
374}
375
376bool ExifUtilsImpl::setAperture(uint32_t numerator, uint32_t denominator) {
377 SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_APERTURE_VALUE, numerator, denominator);
378 return true;
379}
380
381bool ExifUtilsImpl::setBrightness(int32_t numerator, int32_t denominator) {
382 SET_SRATIONAL(EXIF_IFD_EXIF, EXIF_TAG_BRIGHTNESS_VALUE, numerator, denominator);
383 return true;
384}
385
386bool ExifUtilsImpl::setColorSpace(uint16_t color_space) {
387 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_COLOR_SPACE, color_space);
388 return true;
389}
390
391bool ExifUtilsImpl::setComponentsConfiguration(
392 const std::string& components_configuration) {
393 SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_COMPONENTS_CONFIGURATION,
394 EXIF_FORMAT_UNDEFINED, components_configuration);
395 return true;
396}
397
398bool ExifUtilsImpl::setCompression(uint16_t compression) {
399 SET_SHORT(EXIF_IFD_0, EXIF_TAG_COMPRESSION, compression);
400 return true;
401}
402
403bool ExifUtilsImpl::setContrast(uint16_t contrast) {
404 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_CONTRAST, contrast);
405 return true;
406}
407
408bool ExifUtilsImpl::setDateTime(const struct tm& t) {
409 // The length is 20 bytes including NULL for termination in Exif standard.
410 char str[20];
411 int result = snprintf(str, sizeof(str), "%04i:%02i:%02i %02i:%02i:%02i",
412 t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
413 if (result != sizeof(str) - 1) {
414 ALOGW("%s: Input time is invalid", __FUNCTION__);
415 return false;
416 }
417 std::string buffer(str);
418 SET_STRING(EXIF_IFD_0, EXIF_TAG_DATE_TIME, EXIF_FORMAT_ASCII, buffer);
419 SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_DATE_TIME_ORIGINAL, EXIF_FORMAT_ASCII, buffer);
420 SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_DATE_TIME_DIGITIZED, EXIF_FORMAT_ASCII, buffer);
421 return true;
422}
423
424bool ExifUtilsImpl::setDescription(const std::string& description) {
425 SET_STRING(EXIF_IFD_0, EXIF_TAG_IMAGE_DESCRIPTION, EXIF_FORMAT_ASCII, description);
426 return true;
427}
428
429bool ExifUtilsImpl::setDigitalZoomRatio(uint32_t numerator, uint32_t denominator) {
430 SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_DIGITAL_ZOOM_RATIO, numerator, denominator);
431 return true;
432}
433
434bool ExifUtilsImpl::setExposureBias(int32_t numerator, int32_t denominator) {
435 SET_SRATIONAL(EXIF_IFD_EXIF, EXIF_TAG_EXPOSURE_BIAS_VALUE, numerator, denominator);
436 return true;
437}
438
439bool ExifUtilsImpl::setExposureMode(uint16_t exposure_mode) {
440 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_EXPOSURE_MODE, exposure_mode);
441 return true;
442}
443
444bool ExifUtilsImpl::setExposureProgram(uint16_t exposure_program) {
445 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_EXPOSURE_PROGRAM, exposure_program);
446 return true;
447}
448
449bool ExifUtilsImpl::setExposureTime(uint32_t numerator, uint32_t denominator) {
450 SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_EXPOSURE_TIME, numerator, denominator);
451 return true;
452}
453
454bool ExifUtilsImpl::setFlash(uint16_t flash) {
455 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_FLASH, flash);
456 return true;
457}
458
459bool ExifUtilsImpl::setFNumber(uint32_t numerator, uint32_t denominator) {
460 SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_FNUMBER, numerator, denominator);
461 return true;
462}
463
464bool ExifUtilsImpl::setFocalLength(uint32_t numerator, uint32_t denominator) {
465 SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_FOCAL_LENGTH, numerator, denominator);
466 return true;
467}
468
469bool ExifUtilsImpl::setGainControl(uint16_t gain_control) {
470 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_GAIN_CONTROL, gain_control);
471 return true;
472}
473
474bool ExifUtilsImpl::setGpsAltitude(double altitude) {
475 ExifTag refTag = static_cast<ExifTag>(EXIF_TAG_GPS_ALTITUDE_REF);
476 std::unique_ptr<ExifEntry> refEntry =
477 addVariableLengthEntry(EXIF_IFD_GPS, refTag, EXIF_FORMAT_BYTE, 1, 1);
478 if (!refEntry) {
479 ALOGE("%s: Adding GPSAltitudeRef exif entry failed", __FUNCTION__);
480 return false;
481 }
482 if (altitude >= 0) {
483 *refEntry->data = 0;
484 } else {
485 *refEntry->data = 1;
486 altitude *= -1;
487 }
488
489 ExifTag tag = static_cast<ExifTag>(EXIF_TAG_GPS_ALTITUDE);
490 std::unique_ptr<ExifEntry> entry = addVariableLengthEntry(
491 EXIF_IFD_GPS, tag, EXIF_FORMAT_RATIONAL, 1, sizeof(ExifRational));
492 if (!entry) {
493 exif_content_remove_entry(exif_data_->ifd[EXIF_IFD_GPS], refEntry.get());
494 ALOGE("%s: Adding GPSAltitude exif entry failed", __FUNCTION__);
495 return false;
496 }
497 exif_set_rational(entry->data, EXIF_BYTE_ORDER_INTEL,
498 {static_cast<ExifLong>(altitude * 1000), 1000});
499
500 return true;
501}
502
503bool ExifUtilsImpl::setGpsLatitude(double latitude) {
504 const ExifTag refTag = static_cast<ExifTag>(EXIF_TAG_GPS_LATITUDE_REF);
505 std::unique_ptr<ExifEntry> refEntry =
506 addVariableLengthEntry(EXIF_IFD_GPS, refTag, EXIF_FORMAT_ASCII, 2, 2);
507 if (!refEntry) {
508 ALOGE("%s: Adding GPSLatitudeRef exif entry failed", __FUNCTION__);
509 return false;
510 }
511 if (latitude >= 0) {
512 memcpy(refEntry->data, "N", sizeof("N"));
513 } else {
514 memcpy(refEntry->data, "S", sizeof("S"));
515 latitude *= -1;
516 }
517
518 const ExifTag tag = static_cast<ExifTag>(EXIF_TAG_GPS_LATITUDE);
519 std::unique_ptr<ExifEntry> entry = addVariableLengthEntry(
520 EXIF_IFD_GPS, tag, EXIF_FORMAT_RATIONAL, 3, 3 * sizeof(ExifRational));
521 if (!entry) {
522 exif_content_remove_entry(exif_data_->ifd[EXIF_IFD_GPS], refEntry.get());
523 ALOGE("%s: Adding GPSLatitude exif entry failed", __FUNCTION__);
524 return false;
525 }
526 setLatitudeOrLongitudeData(entry->data, latitude);
527
528 return true;
529}
530
531bool ExifUtilsImpl::setGpsLongitude(double longitude) {
532 ExifTag refTag = static_cast<ExifTag>(EXIF_TAG_GPS_LONGITUDE_REF);
533 std::unique_ptr<ExifEntry> refEntry =
534 addVariableLengthEntry(EXIF_IFD_GPS, refTag, EXIF_FORMAT_ASCII, 2, 2);
535 if (!refEntry) {
536 ALOGE("%s: Adding GPSLongitudeRef exif entry failed", __FUNCTION__);
537 return false;
538 }
539 if (longitude >= 0) {
540 memcpy(refEntry->data, "E", sizeof("E"));
541 } else {
542 memcpy(refEntry->data, "W", sizeof("W"));
543 longitude *= -1;
544 }
545
546 ExifTag tag = static_cast<ExifTag>(EXIF_TAG_GPS_LONGITUDE);
547 std::unique_ptr<ExifEntry> entry = addVariableLengthEntry(
548 EXIF_IFD_GPS, tag, EXIF_FORMAT_RATIONAL, 3, 3 * sizeof(ExifRational));
549 if (!entry) {
550 exif_content_remove_entry(exif_data_->ifd[EXIF_IFD_GPS], refEntry.get());
551 ALOGE("%s: Adding GPSLongitude exif entry failed", __FUNCTION__);
552 return false;
553 }
554 setLatitudeOrLongitudeData(entry->data, longitude);
555
556 return true;
557}
558
559bool ExifUtilsImpl::setGpsProcessingMethod(const std::string& method) {
560 std::string buffer =
561 std::string(gExifAsciiPrefix, sizeof(gExifAsciiPrefix)) + method;
562 SET_STRING(EXIF_IFD_GPS, static_cast<ExifTag>(EXIF_TAG_GPS_PROCESSING_METHOD),
563 EXIF_FORMAT_UNDEFINED, buffer);
564 return true;
565}
566
567bool ExifUtilsImpl::setGpsTimestamp(const struct tm& t) {
568 const ExifTag dateTag = static_cast<ExifTag>(EXIF_TAG_GPS_DATE_STAMP);
569 const size_t kGpsDateStampSize = 11;
570 std::unique_ptr<ExifEntry> entry = addVariableLengthEntry(EXIF_IFD_GPS,
571 dateTag, EXIF_FORMAT_ASCII, kGpsDateStampSize, kGpsDateStampSize);
572 if (!entry) {
573 ALOGE("%s: Adding GPSDateStamp exif entry failed", __FUNCTION__);
574 return false;
575 }
576 int result = snprintf(reinterpret_cast<char*>(entry->data), kGpsDateStampSize,
577 "%04i:%02i:%02i", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday);
578 if (result != kGpsDateStampSize - 1) {
579 ALOGW("%s: Input time is invalid", __FUNCTION__);
580 return false;
581 }
582
583 const ExifTag timeTag = static_cast<ExifTag>(EXIF_TAG_GPS_TIME_STAMP);
584 entry = addVariableLengthEntry(EXIF_IFD_GPS, timeTag, EXIF_FORMAT_RATIONAL, 3,
585 3 * sizeof(ExifRational));
586 if (!entry) {
587 ALOGE("%s: Adding GPSTimeStamp exif entry failed", __FUNCTION__);
588 return false;
589 }
590 exif_set_rational(entry->data, EXIF_BYTE_ORDER_INTEL,
591 {static_cast<ExifLong>(t.tm_hour), 1});
592 exif_set_rational(entry->data + sizeof(ExifRational), EXIF_BYTE_ORDER_INTEL,
593 {static_cast<ExifLong>(t.tm_min), 1});
594 exif_set_rational(entry->data + 2 * sizeof(ExifRational), EXIF_BYTE_ORDER_INTEL,
595 {static_cast<ExifLong>(t.tm_sec), 1});
596
597 return true;
598}
599
600bool ExifUtilsImpl::setImageHeight(uint32_t length) {
601 SET_LONG(EXIF_IFD_0, EXIF_TAG_IMAGE_LENGTH, length);
602 SET_LONG(EXIF_IFD_EXIF, EXIF_TAG_PIXEL_Y_DIMENSION, length);
603 return true;
604}
605
606bool ExifUtilsImpl::setImageWidth(uint32_t width) {
607 SET_LONG(EXIF_IFD_0, EXIF_TAG_IMAGE_WIDTH, width);
608 SET_LONG(EXIF_IFD_EXIF, EXIF_TAG_PIXEL_X_DIMENSION, width);
609 return true;
610}
611
612bool ExifUtilsImpl::setIsoSpeedRating(uint16_t iso_speed_ratings) {
613 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_ISO_SPEED_RATINGS, iso_speed_ratings);
614 return true;
615}
616
617bool ExifUtilsImpl::setLightSource(uint16_t light_source) {
618 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_LIGHT_SOURCE, light_source);
619 return true;
620}
621
622bool ExifUtilsImpl::setMaxAperture(uint32_t numerator, uint32_t denominator) {
623 SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_MAX_APERTURE_VALUE, numerator, denominator);
624 return true;
625}
626
627bool ExifUtilsImpl::setMeteringMode(uint16_t metering_mode) {
628 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_METERING_MODE, metering_mode);
629 return true;
630}
631
632bool ExifUtilsImpl::setOrientation(uint16_t orientation) {
633 /*
634 * Orientation value:
635 * 1 2 3 4 5 6 7 8
636 *
637 * 888888 888888 88 88 8888888888 88 88 8888888888
638 * 88 88 88 88 88 88 88 88 88 88 88 88
639 * 8888 8888 8888 8888 88 8888888888 8888888888 88
640 * 88 88 88 88
641 * 88 88 888888 888888
642 */
643 int value = 1;
644 switch (orientation) {
645 case 90:
646 value = 6;
647 break;
648 case 180:
649 value = 3;
650 break;
651 case 270:
652 value = 8;
653 break;
654 default:
655 break;
656 }
657 SET_SHORT(EXIF_IFD_0, EXIF_TAG_ORIENTATION, value);
658 return true;
659}
660
661bool ExifUtilsImpl::setResolutionUnit(uint16_t resolution_unit) {
662 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_RESOLUTION_UNIT, resolution_unit);
663 return true;
664}
665
666bool ExifUtilsImpl::setSaturation(uint16_t saturation) {
667 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_SATURATION, saturation);
668 return true;
669}
670
671bool ExifUtilsImpl::setSceneCaptureType(uint16_t type) {
672 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_SCENE_CAPTURE_TYPE, type);
673 return true;
674}
675
676bool ExifUtilsImpl::setSharpness(uint16_t sharpness) {
677 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_SHARPNESS, sharpness);
678 return true;
679}
680
681bool ExifUtilsImpl::setShutterSpeed(int32_t numerator, int32_t denominator) {
682 SET_SRATIONAL(EXIF_IFD_EXIF, EXIF_TAG_SHUTTER_SPEED_VALUE, numerator, denominator);
683 return true;
684}
685
686bool ExifUtilsImpl::setSubjectDistance(uint32_t numerator, uint32_t denominator) {
687 SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_SUBJECT_DISTANCE, numerator, denominator);
688 return true;
689}
690
691bool ExifUtilsImpl::setSubsecTime(const std::string& subsec_time) {
692 SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_SUB_SEC_TIME, EXIF_FORMAT_ASCII, subsec_time);
693 SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_SUB_SEC_TIME_ORIGINAL, EXIF_FORMAT_ASCII, subsec_time);
694 SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_SUB_SEC_TIME_DIGITIZED, EXIF_FORMAT_ASCII, subsec_time);
695 return true;
696}
697
698bool ExifUtilsImpl::setWhiteBalance(uint16_t white_balance) {
699 SET_SHORT(EXIF_IFD_EXIF, EXIF_TAG_WHITE_BALANCE, white_balance);
700 return true;
701}
702
703bool ExifUtilsImpl::setXResolution(uint32_t numerator, uint32_t denominator) {
704 SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_X_RESOLUTION, numerator, denominator);
705 return true;
706}
707
708bool ExifUtilsImpl::setYCbCrPositioning(uint16_t ycbcr_positioning) {
709 SET_SHORT(EXIF_IFD_0, EXIF_TAG_YCBCR_POSITIONING, ycbcr_positioning);
710 return true;
711}
712
713bool ExifUtilsImpl::setYResolution(uint32_t numerator, uint32_t denominator) {
714 SET_RATIONAL(EXIF_IFD_EXIF, EXIF_TAG_Y_RESOLUTION, numerator, denominator);
715 return true;
716}
717
718bool ExifUtilsImpl::generateApp1() {
719 destroyApp1();
720 // Save the result into |app1_buffer_|.
721 exif_data_save_data(exif_data_, &app1_buffer_, &app1_length_);
722 if (!app1_length_) {
723 ALOGE("%s: Allocate memory for app1_buffer_ failed", __FUNCTION__);
724 return false;
725 }
726 /*
727 * The JPEG segment size is 16 bits in spec. The size of APP1 segment should
728 * be smaller than 65533 because there are two bytes for segment size field.
729 */
730 if (app1_length_ > 65533) {
731 destroyApp1();
732 ALOGE("%s: The size of APP1 segment is too large", __FUNCTION__);
733 return false;
734 }
735 return true;
736}
737
738const uint8_t* ExifUtilsImpl::getApp1Buffer() {
739 return app1_buffer_;
740}
741
742unsigned int ExifUtilsImpl::getApp1Length() {
743 return app1_length_;
744}
745
746bool ExifUtilsImpl::setExifVersion(const std::string& exif_version) {
747 SET_STRING(EXIF_IFD_EXIF, EXIF_TAG_EXIF_VERSION, EXIF_FORMAT_UNDEFINED, exif_version);
748 return true;
749}
750
751bool ExifUtilsImpl::setMake(const std::string& make) {
752 SET_STRING(EXIF_IFD_0, EXIF_TAG_MAKE, EXIF_FORMAT_ASCII, make);
753 return true;
754}
755
756bool ExifUtilsImpl::setModel(const std::string& model) {
757 SET_STRING(EXIF_IFD_0, EXIF_TAG_MODEL, EXIF_FORMAT_ASCII, model);
758 return true;
759}
760
761void ExifUtilsImpl::reset() {
762 destroyApp1();
763 if (exif_data_) {
764 /*
765 * Since we decided to ignore the original APP1, we are sure that there is
766 * no thumbnail allocated by libexif. |exif_data_->data| is actually
767 * allocated by JpegCompressor. sets |exif_data_->data| to nullptr to
768 * prevent exif_data_unref() destroy it incorrectly.
769 */
770 exif_data_->data = nullptr;
771 exif_data_->size = 0;
772 exif_data_unref(exif_data_);
773 exif_data_ = nullptr;
774 }
775}
776
777std::unique_ptr<ExifEntry> ExifUtilsImpl::addVariableLengthEntry(ExifIfd ifd,
778 ExifTag tag, ExifFormat format, uint64_t components, unsigned int size) {
779 // Remove old entry if exists.
780 exif_content_remove_entry(exif_data_->ifd[ifd],
781 exif_content_get_entry(exif_data_->ifd[ifd], tag));
782 ExifMem* mem = exif_mem_new_default();
783 if (!mem) {
784 ALOGE("%s: Allocate memory for exif entry failed", __FUNCTION__);
785 return nullptr;
786 }
787 std::unique_ptr<ExifEntry> entry(exif_entry_new_mem(mem));
788 if (!entry) {
789 ALOGE("%s: Allocate memory for exif entry failed", __FUNCTION__);
790 exif_mem_unref(mem);
791 return nullptr;
792 }
793 void* tmpBuffer = exif_mem_alloc(mem, size);
794 if (!tmpBuffer) {
795 ALOGE("%s: Allocate memory for exif entry failed", __FUNCTION__);
796 exif_mem_unref(mem);
797 return nullptr;
798 }
799
800 entry->data = static_cast<unsigned char*>(tmpBuffer);
801 entry->tag = tag;
802 entry->format = format;
803 entry->components = components;
804 entry->size = size;
805
806 exif_content_add_entry(exif_data_->ifd[ifd], entry.get());
807 exif_mem_unref(mem);
808
809 return entry;
810}
811
812std::unique_ptr<ExifEntry> ExifUtilsImpl::addEntry(ExifIfd ifd, ExifTag tag) {
813 std::unique_ptr<ExifEntry> entry(exif_content_get_entry(exif_data_->ifd[ifd], tag));
814 if (entry) {
815 // exif_content_get_entry() won't ref the entry, so we ref here.
816 exif_entry_ref(entry.get());
817 return entry;
818 }
819 entry.reset(exif_entry_new());
820 if (!entry) {
821 ALOGE("%s: Allocate memory for exif entry failed", __FUNCTION__);
822 return nullptr;
823 }
824 entry->tag = tag;
825 exif_content_add_entry(exif_data_->ifd[ifd], entry.get());
826 exif_entry_initialize(entry.get(), tag);
827 return entry;
828}
829
830bool ExifUtilsImpl::setShort(ExifIfd ifd, ExifTag tag, uint16_t value, const std::string& msg) {
831 std::unique_ptr<ExifEntry> entry = addEntry(ifd, tag);
832 if (!entry) {
833 ALOGE("%s: Adding '%s' entry failed", __FUNCTION__, msg.c_str());
834 return false;
835 }
836 exif_set_short(entry->data, EXIF_BYTE_ORDER_INTEL, value);
837 return true;
838}
839
840bool ExifUtilsImpl::setLong(ExifIfd ifd, ExifTag tag, uint32_t value, 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_long(entry->data, EXIF_BYTE_ORDER_INTEL, value);
847 return true;
848}
849
850bool ExifUtilsImpl::setRational(ExifIfd ifd, ExifTag tag, uint32_t numerator,
851 uint32_t denominator, const std::string& msg) {
852 std::unique_ptr<ExifEntry> entry = addEntry(ifd, tag);
853 if (!entry) {
854 ALOGE("%s: Adding '%s' entry failed", __FUNCTION__, msg.c_str());
855 return false;
856 }
857 exif_set_rational(entry->data, EXIF_BYTE_ORDER_INTEL, {numerator, denominator});
858 return true;
859}
860
861bool ExifUtilsImpl::setSRational(ExifIfd ifd, ExifTag tag, int32_t numerator,
862 int32_t denominator, const std::string& msg) {
863 std::unique_ptr<ExifEntry> entry = addEntry(ifd, tag);
864 if (!entry) {
865 ALOGE("%s: Adding '%s' entry failed", __FUNCTION__, msg.c_str());
866 return false;
867 }
868 exif_set_srational(entry->data, EXIF_BYTE_ORDER_INTEL, {numerator, denominator});
869 return true;
870}
871
872bool ExifUtilsImpl::setString(ExifIfd ifd, ExifTag tag, ExifFormat format,
873 const std::string& buffer, const std::string& msg) {
874 size_t entry_size = buffer.length();
875 // Since the exif format is undefined, NULL termination is not necessary.
876 if (format == EXIF_FORMAT_ASCII) {
877 entry_size++;
878 }
879 std::unique_ptr<ExifEntry> entry =
880 addVariableLengthEntry(ifd, tag, format, entry_size, entry_size);
881 if (!entry) {
882 ALOGE("%s: Adding '%s' entry failed", __FUNCTION__, msg.c_str());
883 return false;
884 }
885 memcpy(entry->data, buffer.c_str(), entry_size);
886 return true;
887}
888
889void ExifUtilsImpl::destroyApp1() {
890 /*
891 * Since there is no API to access ExifMem in ExifData->priv, we use free
892 * here, which is the default free function in libexif. See
893 * exif_data_save_data() for detail.
894 */
895 free(app1_buffer_);
896 app1_buffer_ = nullptr;
897 app1_length_ = 0;
898}
899
900bool ExifUtilsImpl::setFromMetadata(const CameraMetadata& metadata,
901 const size_t imageWidth, const size_t imageHeight) {
902 // How precise the float-to-rational conversion for EXIF tags would be.
903 constexpr int kRationalPrecision = 10000;
904 if (!setImageWidth(imageWidth) ||
905 !setImageHeight(imageHeight)) {
906 ALOGE("%s: setting image resolution failed.", __FUNCTION__);
907 return false;
908 }
909
910 struct timespec tp;
911 struct tm time_info;
912 bool time_available = clock_gettime(CLOCK_REALTIME, &tp) != -1;
913 localtime_r(&tp.tv_sec, &time_info);
914 if (!setDateTime(time_info)) {
915 ALOGE("%s: setting data time failed.", __FUNCTION__);
916 return false;
917 }
918
919 float focal_length;
920 camera_metadata_ro_entry entry = metadata.find(ANDROID_LENS_FOCAL_LENGTH);
921 if (entry.count) {
922 focal_length = entry.data.f[0];
923
924 if (!setFocalLength(
925 static_cast<uint32_t>(focal_length * kRationalPrecision), kRationalPrecision)) {
926 ALOGE("%s: setting focal length failed.", __FUNCTION__);
927 return false;
928 }
929 } else {
930 ALOGV("%s: Cannot find focal length in metadata.", __FUNCTION__);
931 }
932
933 if (metadata.exists(ANDROID_JPEG_GPS_COORDINATES)) {
934 entry = metadata.find(ANDROID_JPEG_GPS_COORDINATES);
935 if (entry.count < 3) {
936 ALOGE("%s: Gps coordinates in metadata is not complete.", __FUNCTION__);
937 return false;
938 }
939 if (!setGpsLatitude(entry.data.d[0])) {
940 ALOGE("%s: setting gps latitude failed.", __FUNCTION__);
941 return false;
942 }
943 if (!setGpsLongitude(entry.data.d[1])) {
944 ALOGE("%s: setting gps longitude failed.", __FUNCTION__);
945 return false;
946 }
947 if (!setGpsAltitude(entry.data.d[2])) {
948 ALOGE("%s: setting gps altitude failed.", __FUNCTION__);
949 return false;
950 }
951 }
952
953 if (metadata.exists(ANDROID_JPEG_GPS_PROCESSING_METHOD)) {
954 entry = metadata.find(ANDROID_JPEG_GPS_PROCESSING_METHOD);
955 std::string method_str(reinterpret_cast<const char*>(entry.data.u8));
956 if (!setGpsProcessingMethod(method_str)) {
957 ALOGE("%s: setting gps processing method failed.", __FUNCTION__);
958 return false;
959 }
960 }
961
962 if (time_available && metadata.exists(ANDROID_JPEG_GPS_TIMESTAMP)) {
963 entry = metadata.find(ANDROID_JPEG_GPS_TIMESTAMP);
964 time_t timestamp = static_cast<time_t>(entry.data.i64[0]);
965 if (gmtime_r(&timestamp, &time_info)) {
966 if (!setGpsTimestamp(time_info)) {
967 ALOGE("%s: setting gps timestamp failed.", __FUNCTION__);
968 return false;
969 }
970 } else {
971 ALOGE("%s: Time tranformation failed.", __FUNCTION__);
972 return false;
973 }
974 }
975
976 if (metadata.exists(ANDROID_JPEG_ORIENTATION)) {
977 entry = metadata.find(ANDROID_JPEG_ORIENTATION);
978 if (!setOrientation(entry.data.i32[0])) {
979 ALOGE("%s: setting orientation failed.", __FUNCTION__);
980 return false;
981 }
982 }
983
984 if (metadata.exists(ANDROID_SENSOR_EXPOSURE_TIME)) {
985 entry = metadata.find(ANDROID_SENSOR_EXPOSURE_TIME);
986 // int64_t of nanoseconds
987 if (!setExposureTime(entry.data.i64[0],1000000000u)) {
988 ALOGE("%s: setting exposure time failed.", __FUNCTION__);
989 return false;
990 }
991 }
992
993 if (metadata.exists(ANDROID_LENS_APERTURE)) {
994 const int kAperturePrecision = 10000;
995 entry = metadata.find(ANDROID_LENS_APERTURE);
996 if (!setFNumber(entry.data.f[0] * kAperturePrecision, kAperturePrecision)) {
997 ALOGE("%s: setting F number failed.", __FUNCTION__);
998 return false;
999 }
1000 }
1001
1002 if (metadata.exists(ANDROID_FLASH_INFO_AVAILABLE)) {
1003 entry = metadata.find(ANDROID_FLASH_INFO_AVAILABLE);
1004 if (entry.data.u8[0] == ANDROID_FLASH_INFO_AVAILABLE_FALSE) {
1005 const uint32_t kNoFlashFunction = 0x20;
1006 if (!setFlash(kNoFlashFunction)) {
1007 ALOGE("%s: setting flash failed.", __FUNCTION__);
1008 return false;
1009 }
1010 } else {
1011 ALOGE("%s: Unsupported flash info: %d",__FUNCTION__, entry.data.u8[0]);
1012 return false;
1013 }
1014 }
1015
1016 if (metadata.exists(ANDROID_CONTROL_AWB_MODE)) {
1017 entry = metadata.find(ANDROID_CONTROL_AWB_MODE);
1018 if (entry.data.u8[0] == ANDROID_CONTROL_AWB_MODE_AUTO) {
1019 const uint16_t kAutoWhiteBalance = 0;
1020 if (!setWhiteBalance(kAutoWhiteBalance)) {
1021 ALOGE("%s: setting white balance failed.", __FUNCTION__);
1022 return false;
1023 }
1024 } else {
1025 ALOGE("%s: Unsupported awb mode: %d", __FUNCTION__, entry.data.u8[0]);
1026 return false;
1027 }
1028 }
1029
1030 if (time_available) {
1031 char str[4];
1032 if (snprintf(str, sizeof(str), "%03ld", tp.tv_nsec / 1000000) < 0) {
1033 ALOGE("%s: Subsec is invalid: %ld", __FUNCTION__, tp.tv_nsec);
1034 return false;
1035 }
1036 if (!setSubsecTime(std::string(str))) {
1037 ALOGE("%s: setting subsec time failed.", __FUNCTION__);
1038 return false;
1039 }
1040 }
1041
1042 return true;
1043}
1044
1045} // namespace camera3
1046} // namespace android