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