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_foreach_entry(pContent, _ExifUtil::ForEachEntryStatic, this);
121 void _ExifUtil::ForEachEntryStatic(struct _ExifEntry *pEntry, void *pUserData)
123 SysTryReturnVoidResult(NID_MEDIA, pEntry != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] ExifEntry instance is not available.");
125 _ExifUtil* pUtil = static_cast<_ExifUtil*>(pUserData);
126 SysTryReturnVoidResult(NID_MEDIA, pUtil != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] _ExifUtil instance is not available.");
128 pUtil->ForEachEntry(pEntry);
131 void _ExifUtil::ForEachEntry(struct _ExifEntry *pEntry)
133 SysTryReturnVoidResult(NID_MEDIA, pEntry != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] ExifEntry instance is not available.");
134 SysTryReturnVoidResult(NID_MEDIA, pEntry->parent != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Entry's parent instance is not available.");
136 const ExifByteOrder o = exif_data_get_byte_order (pEntry->parent->parent);
137 ExifRational exifRational;
139 switch ((int)pEntry->tag)
141 case EXIF_TAG_ORIENTATION:
142 if (pEntry->format == EXIF_FORMAT_SHORT && pEntry->size == 2)
144 __orientation = *(short*)(pEntry->data);
145 __orientationExist = true;
149 case EXIF_TAG_GPS_LATITUDE:
150 if (pEntry->format == EXIF_FORMAT_RATIONAL)
153 exifRational = exif_get_rational (pEntry->data, o);
155 if (exifRational.denominator && exifRational.denominator != 1) {
156 __gpsLatitude = (double)exifRational.numerator / (double)exifRational.denominator;
160 __gpsLatitude = (double)exifRational.numerator;
164 for (int i = 0; i < (int)pEntry->components; i++)
166 exifRational = exif_get_rational (pEntry->data + 8 * i, o);
167 if (exifRational.denominator)
169 __gpsLatitude += ((double)exifRational.numerator / (double)exifRational.denominator) / pow((float)60.0, i);
173 __gpsLatitude += (double)exifRational.numerator / pow((float)60.0, i);
176 __gpsLatitudeExist = true;
180 case EXIF_TAG_GPS_LONGITUDE:
181 if (pEntry->format == EXIF_FORMAT_RATIONAL)
184 exifRational = exif_get_rational (pEntry->data, o);
186 if (exifRational.denominator && exifRational.denominator != 1) {
187 __gpsLatitude = (double)exifRational.numerator / (double)exifRational.denominator;
189 __gpsLatitude = (double)exifRational.numerator;
192 __gpsLongitude = 0.0;
194 for (int i = 0; i < (int)pEntry->components; i++)
196 exifRational = exif_get_rational (pEntry->data + 8 * i, o);
197 if (exifRational.denominator && exifRational.denominator != 1)
199 __gpsLongitude += ((double)exifRational.numerator / (double)exifRational.denominator) / pow((float)60.0, i);
203 __gpsLongitude += (double)exifRational.numerator / pow((float)60.0, i);
206 __gpsLongitudeExist = true;
210 case EXIF_TAG_GPS_ALTITUDE:
211 if (pEntry->format == EXIF_FORMAT_RATIONAL)
213 exifRational = exif_get_rational (pEntry->data, o);
215 if (exifRational.denominator && exifRational.denominator != 1)
217 __gpsAltitude = (double)exifRational.numerator / (double)exifRational.denominator;
221 __gpsAltitude = (double)exifRational.numerator;
223 __gpsAltitudeExist = true;
232 result _ExifUtil::GetValue(const Tizen::Base::String& key, int &value)
234 if (key.Equals(EXIF_TAG_IMAGE_ORIENTATION, true))
236 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.");
237 value = __orientation;
241 SysLogException(NID_MEDIA, E_INVALID_ARG, "[E_INVALID_ARG] Key:%ls was wrong.", key.GetPointer());
242 return E_INVALID_ARG;
248 result _ExifUtil::GetValue(const Tizen::Base::String& key, double &value)
250 if (key.Equals(EXIF_TAG_IMAGE_GPS_ALTITUDE, true))
252 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.");
253 value = __gpsAltitude;
255 else if (key.Equals(EXIF_TAG_IMAGE_GPS_LONGITUDE, true))
257 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.");
258 value = __gpsLongitude;
259 } else if (key.Equals(EXIF_TAG_IMAGE_GPS_LATITUDE, true))
261 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.");
262 value = __gpsLatitude;
265 SysLogException(NID_MEDIA, E_INVALID_ARG, "[E_INVALID_ARG] Key:%ls was wrong.", key.GetPointer());
266 return E_INVALID_ARG;