2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,// 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.
17 #include <libexif/exif-byte-order.h>
18 #include <libexif/exif-data-type.h>
19 #include <libexif/exif-ifd.h>
20 #include <libexif/exif-log.h>
21 #include <libexif/exif-tag.h>
22 #include <libexif/exif-content.h>
23 #include <libexif/exif-mnote-data.h>
24 #include <libexif/exif-mem.h>
25 #include <FBaseSysLog.h>
26 #include "FMedia_ExifUtil.h"
28 namespace Tizen { namespace Media
31 const wchar_t* const EXIF_TAG_IMAGE_ORIENTATION = L"Exif.Orientation";
32 const wchar_t* const EXIF_TAG_IMAGE_GPS_ALTITUDE = L"Exif.Gps.Altitude";
33 const wchar_t* const EXIF_TAG_IMAGE_GPS_LONGITUDE = L"Exif.Gps.Longitude";
34 const wchar_t* const EXIF_TAG_IMAGE_GPS_LATITUDE = L"Exif.Gps.Latitude";
36 _ExifUtil::_ExifUtil()
43 , __gpsLongitudeExist(false)
44 , __gpsAltitudeExist(false)
45 , __gpsLatitudeExist(false)
46 , __orientationExist(false)
50 _ExifUtil::~_ExifUtil()
54 exif_data_free(__pExifData);
58 exif_log_free(__pExifLog);
62 result _ExifUtil::Construct(const byte* pBuf, int length)
66 SysTryCatch(NID_MEDIA, pBuf != null, r = E_INVALID_DATA, E_INVALID_DATA, "[E_INVALID_DATA] Buffer is null. length: %d", length);
68 __pExifData = exif_data_new_from_data(pBuf, length);
69 SysTryCatch(NID_MEDIA, __pExifData != null, r = E_INVALID_DATA, E_INVALID_DATA, "[E_INVALID_DATA] exi data is null. length: %d", length);
71 __pExifLog = exif_log_new();
72 SysTryCatch(NID_MEDIA, __pExifLog != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] exif_log_new fail");
74 #if defined _EXIF_UTIL_LOG_
75 //exif_log_set_func(__pExifLog, _ExifUtil::ExifLogFuncStatic, this);
76 //exif_data_log(__pExifData, __pExifLog);
78 exif_data_foreach_content(__pExifData, _ExifUtil::ForEachContentStatic, this);
84 exif_log_free(__pExifLog);
89 #if defined _EXIF_UTIL_LOG_
90 //void _ExifUtil::ExifLogFuncStatic(ExifLog *log, ExifLogCode code, const char *domain, const char *format, va_list args, void *data)
92 // ((_ExifUtil*)data)->ExifLogFunc(log, code, domain, format, args);
95 //void _ExifUtil::ExifLogFunc(ExifLog *log, ExifLogCode code, const char *domain, const char *format, va_list args)
98 // vsprintf(msg, format, args);
99 // TLog("ExifLog : %s", msg);
103 void _ExifUtil::ForEachContentStatic(struct _ExifContent *pContent, void *pUserData)
105 SysTryReturnVoidResult(NID_MEDIA, pContent != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] ExifContent instance is not available.");
107 _ExifUtil* pUtil = static_cast<_ExifUtil*>(pUserData);
108 SysTryReturnVoidResult(NID_MEDIA, pUtil != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] _ExifUtil instance is not available.");
110 pUtil->ForEachContent(pContent);
113 void _ExifUtil::ForEachContent(struct _ExifContent *pContent)
115 SysTryReturnVoidResult(NID_MEDIA, pContent != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] ExifContent instance is not available.");
117 exif_content_log(pContent, __pExifLog);
118 exif_content_dump(pContent, 0);
120 exif_content_foreach_entry(pContent, _ExifUtil::ForEachEntryStatic, this);
123 void _ExifUtil::ForEachEntryStatic(struct _ExifEntry *pEntry, void *pUserData)
125 SysTryReturnVoidResult(NID_MEDIA, pEntry != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] ExifEntry instance is not available.");
127 _ExifUtil* pUtil = static_cast<_ExifUtil*>(pUserData);
128 SysTryReturnVoidResult(NID_MEDIA, pUtil != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] _ExifUtil instance is not available.");
130 pUtil->ForEachEntry(pEntry);
133 void _ExifUtil::ForEachEntry(struct _ExifEntry *pEntry)
135 SysTryReturnVoidResult(NID_MEDIA, pEntry != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] ExifEntry instance is not available.");
136 SysTryReturnVoidResult(NID_MEDIA, pEntry->parent != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Entry's parent instance is not available.");
138 const ExifByteOrder o = exif_data_get_byte_order (pEntry->parent->parent);
139 ExifRational exifRational;
141 #if defined (_EXIF_UTIL_LOG_)
145 switch ((int)pEntry->tag)
147 case EXIF_TAG_ORIENTATION:
148 if (pEntry->format == EXIF_FORMAT_SHORT && pEntry->size == 2)
150 __orientation = *(short*)(pEntry->data);
151 __orientationExist = true;
155 case EXIF_TAG_GPS_LATITUDE:
156 if (pEntry->format == EXIF_FORMAT_RATIONAL)
159 exifRational = exif_get_rational (pEntry->data, o);
161 if (exifRational.denominator && exifRational.denominator != 1) {
162 __gpsLatitude = (double)exifRational.numerator / (double)exifRational.denominator;
166 __gpsLatitude = (double)exifRational.numerator;
170 for (int i = 0; i < (int)pEntry->components; i++)
172 exifRational = exif_get_rational (pEntry->data + 8 * i, o);
173 if (exifRational.denominator)
175 __gpsLatitude += ((double)exifRational.numerator / (double)exifRational.denominator) / pow((float)60.0, i);
179 __gpsLatitude += (double)exifRational.numerator / pow((float)60.0, i);
182 __gpsLatitudeExist = true;
186 case EXIF_TAG_GPS_LONGITUDE:
187 if (pEntry->format == EXIF_FORMAT_RATIONAL)
190 exifRational = exif_get_rational (pEntry->data, o);
192 if (exifRational.denominator && exifRational.denominator != 1) {
193 __gpsLatitude = (double)exifRational.numerator / (double)exifRational.denominator;
195 __gpsLatitude = (double)exifRational.numerator;
198 __gpsLongitude = 0.0;
200 for (int i = 0; i < (int)pEntry->components; i++)
202 exifRational = exif_get_rational (pEntry->data + 8 * i, o);
203 if (exifRational.denominator && exifRational.denominator != 1)
205 __gpsLongitude += ((double)exifRational.numerator / (double)exifRational.denominator) / pow((float)60.0, i);
209 __gpsLongitude += (double)exifRational.numerator / pow((float)60.0, i);
212 __gpsLongitudeExist = true;
216 case EXIF_TAG_GPS_ALTITUDE:
217 if (pEntry->format == EXIF_FORMAT_RATIONAL)
219 exifRational = exif_get_rational (pEntry->data, o);
221 if (exifRational.denominator && exifRational.denominator != 1)
223 __gpsAltitude = (double)exifRational.numerator / (double)exifRational.denominator;
227 __gpsAltitude = (double)exifRational.numerator;
229 __gpsAltitudeExist = true;
238 result _ExifUtil::GetValue(const Tizen::Base::String& key, int &value)
240 if (key.Equals(EXIF_TAG_IMAGE_ORIENTATION, true))
242 SysTryReturn(NID_MEDIA, __orientationExist, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] bool instance is not available. Orientation value was not found.");
243 value = __orientation;
247 SysLogException(NID_MEDIA, E_INVALID_ARG, "[E_INVALID_ARG] Key:%S was wrong.", key.GetPointer());
248 return E_INVALID_ARG;
254 result _ExifUtil::GetValue(const Tizen::Base::String& key, double &value)
256 if (key.Equals(EXIF_TAG_IMAGE_GPS_ALTITUDE, true))
258 SysTryReturn(NID_MEDIA, __gpsAltitudeExist, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] bool instance is not available. Altitude value was not found.");
259 value = __gpsAltitude;
261 else if (key.Equals(EXIF_TAG_IMAGE_GPS_LONGITUDE, true))
263 SysTryReturn(NID_MEDIA, __gpsLongitude, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] bool instance is not available. Longitude value was not found.");
264 value = __gpsLongitude;
265 } else if (key.Equals(EXIF_TAG_IMAGE_GPS_LATITUDE, true))
267 SysTryReturn(NID_MEDIA, __gpsLatitude, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] bool instance is not available. Latitude value was not found.");
268 value = __gpsLatitude;
271 SysLogException(NID_MEDIA, E_INVALID_ARG, "[E_INVALID_ARG] Key:%S was wrong.", key.GetPointer());
272 return E_INVALID_ARG;