-//
-// Open Service Platform
-// Copyright (c) 2012 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Apache License, Version 2.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-#include <math.h>
-#include <libexif/exif-byte-order.h>
-#include <libexif/exif-data-type.h>
-#include <libexif/exif-ifd.h>
-#include <libexif/exif-log.h>
-#include <libexif/exif-tag.h>
-#include <libexif/exif-content.h>
-#include <libexif/exif-mnote-data.h>
-#include <libexif/exif-mem.h>
-#include <FBaseSysLog.h>
-#include "FMedia_ExifUtil.h"
-
-namespace Tizen { namespace Media
-{
-
-const wchar_t* const EXIF_TAG_IMAGE_ORIENTATION = L"Exif.Orientation";
-const wchar_t* const EXIF_TAG_IMAGE_GPS_ALTITUDE = L"Exif.Gps.Altitude";
-const wchar_t* const EXIF_TAG_IMAGE_GPS_LONGITUDE = L"Exif.Gps.Longitude";
-const wchar_t* const EXIF_TAG_IMAGE_GPS_LATITUDE = L"Exif.Gps.Latitude";
-
-_ExifUtil::_ExifUtil()
-: __pExifData(null)
-, __pExifLog(null)
-, __orientation(-1)
-, __gpsLongitude(0.0)
-, __gpsLatitude(0.0)
-, __gpsAltitude(0.0)
-, __gpsLongitudeExist(false)
-, __gpsAltitudeExist(false)
-, __gpsLatitudeExist(false)
-, __orientationExist(false)
-{
-}
-
-_ExifUtil::~_ExifUtil()
-{
- if (__pExifData)
- {
- exif_data_free(__pExifData);
- }
- if (__pExifLog)
- {
- exif_log_free(__pExifLog);
- }
-}
-
-result _ExifUtil::Construct(const byte* pBuf, int length)
-{
- result r = E_SUCCESS;
-
- SysTryCatch(NID_MEDIA, pBuf != null, r = E_INVALID_DATA, E_INVALID_DATA, "[E_INVALID_DATA] Buffer is null. length: %d", length);
-
- __pExifData = exif_data_new_from_data(pBuf, length);
- SysTryCatch(NID_MEDIA, __pExifData != null, r = E_INVALID_DATA, E_INVALID_DATA, "[E_INVALID_DATA] exi data is null. length: %d", length);
-
- __pExifLog = exif_log_new();
- SysTryCatch(NID_MEDIA, __pExifLog != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] exif_log_new fail");
-
-#if defined _EXIF_UTIL_LOG_
- //exif_log_set_func(__pExifLog, _ExifUtil::ExifLogFuncStatic, this);
- //exif_data_log(__pExifData, __pExifLog);
-#endif
- exif_data_foreach_content(__pExifData, _ExifUtil::ForEachContentStatic, this);
- return r;
-
-CATCH:
- if (__pExifLog)
- {
- exif_log_free(__pExifLog);
- }
- return r;
-}
-
-#if defined _EXIF_UTIL_LOG_
-//void _ExifUtil::ExifLogFuncStatic(ExifLog *log, ExifLogCode code, const char *domain, const char *format, va_list args, void *data)
-//{
-// ((_ExifUtil*)data)->ExifLogFunc(log, code, domain, format, args);
-//}
-
-//void _ExifUtil::ExifLogFunc(ExifLog *log, ExifLogCode code, const char *domain, const char *format, va_list args)
-//{
-// char msg[1024];
-// vsprintf(msg, format, args);
-// TLog("ExifLog : %s", msg);
-//}
-#endif
-
-void _ExifUtil::ForEachContentStatic(struct _ExifContent *pContent, void *pUserData)
-{
- SysTryReturnVoidResult(NID_MEDIA, pContent != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] ExifContent instance is not available.");
-
- _ExifUtil* pUtil = static_cast<_ExifUtil*>(pUserData);
- SysTryReturnVoidResult(NID_MEDIA, pUtil != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] _ExifUtil instance is not available.");
-
- pUtil->ForEachContent(pContent);
-}
-
-void _ExifUtil::ForEachContent(struct _ExifContent *pContent)
-{
- SysTryReturnVoidResult(NID_MEDIA, pContent != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] ExifContent instance is not available.");
-
- exif_content_log(pContent, __pExifLog);
- exif_content_dump(pContent, 0);
-
- exif_content_foreach_entry(pContent, _ExifUtil::ForEachEntryStatic, this);
-}
-
-void _ExifUtil::ForEachEntryStatic(struct _ExifEntry *pEntry, void *pUserData)
-{
- SysTryReturnVoidResult(NID_MEDIA, pEntry != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] ExifEntry instance is not available.");
-
- _ExifUtil* pUtil = static_cast<_ExifUtil*>(pUserData);
- SysTryReturnVoidResult(NID_MEDIA, pUtil != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] _ExifUtil instance is not available.");
-
- pUtil->ForEachEntry(pEntry);
-}
-
-void _ExifUtil::ForEachEntry(struct _ExifEntry *pEntry)
-{
- SysTryReturnVoidResult(NID_MEDIA, pEntry != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] ExifEntry instance is not available.");
- SysTryReturnVoidResult(NID_MEDIA, pEntry->parent != null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Entry's parent instance is not available.");
-
- const ExifByteOrder o = exif_data_get_byte_order (pEntry->parent->parent);
- ExifRational exifRational;
-
-#if defined (_EXIF_UTIL_LOG_)
- char value[1024];
-#endif
-
- switch ((int)pEntry->tag)
- {
- case EXIF_TAG_ORIENTATION:
- if (pEntry->format == EXIF_FORMAT_SHORT && pEntry->size == 2)
- {
- __orientation = *(short*)(pEntry->data);
- __orientationExist = true;
- }
- break;
-
- case EXIF_TAG_GPS_LATITUDE:
- if (pEntry->format == EXIF_FORMAT_RATIONAL)
- {
- /*
- exifRational = exif_get_rational (pEntry->data, o);
-
- if (exifRational.denominator && exifRational.denominator != 1) {
- __gpsLatitude = (double)exifRational.numerator / (double)exifRational.denominator;
- }
- else
- {
- __gpsLatitude = (double)exifRational.numerator;
- }
- */
- __gpsLatitude = 0.0;
- for (int i = 0; i < (int)pEntry->components; i++)
- {
- exifRational = exif_get_rational (pEntry->data + 8 * i, o);
- if (exifRational.denominator)
- {
- __gpsLatitude += ((double)exifRational.numerator / (double)exifRational.denominator) / pow((float)60.0, i);
- }
- else
- {
- __gpsLatitude += (double)exifRational.numerator / pow((float)60.0, i);
- }
- }
- __gpsLatitudeExist = true;
- }
- break;
-
- case EXIF_TAG_GPS_LONGITUDE:
- if (pEntry->format == EXIF_FORMAT_RATIONAL)
- {
- /*
- exifRational = exif_get_rational (pEntry->data, o);
-
- if (exifRational.denominator && exifRational.denominator != 1) {
- __gpsLatitude = (double)exifRational.numerator / (double)exifRational.denominator;
- } else {
- __gpsLatitude = (double)exifRational.numerator;
- }
- */
- __gpsLongitude = 0.0;
-
- for (int i = 0; i < (int)pEntry->components; i++)
- {
- exifRational = exif_get_rational (pEntry->data + 8 * i, o);
- if (exifRational.denominator && exifRational.denominator != 1)
- {
- __gpsLongitude += ((double)exifRational.numerator / (double)exifRational.denominator) / pow((float)60.0, i);
- }
- else
- {
- __gpsLongitude += (double)exifRational.numerator / pow((float)60.0, i);
- }
- }
- __gpsLongitudeExist = true;
- }
- break;
-
- case EXIF_TAG_GPS_ALTITUDE:
- if (pEntry->format == EXIF_FORMAT_RATIONAL)
- {
- exifRational = exif_get_rational (pEntry->data, o);
-
- if (exifRational.denominator && exifRational.denominator != 1)
- {
- __gpsAltitude = (double)exifRational.numerator / (double)exifRational.denominator;
- }
- else
- {
- __gpsAltitude = (double)exifRational.numerator;
- }
- __gpsAltitudeExist = true;
- }
- break;
-
- default:
- break;
- }
-}
-
-result _ExifUtil::GetValue(const Tizen::Base::String& key, int &value)
-{
- if (key.Equals(EXIF_TAG_IMAGE_ORIENTATION, true))
- {
- 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.");
- value = __orientation;
- }
- else
- {
- SysLogException(NID_MEDIA, E_INVALID_ARG, "[E_INVALID_ARG] Key:%S was wrong.", key.GetPointer());
- return E_INVALID_ARG;
- }
-
- return E_SUCCESS;
-}
-
-result _ExifUtil::GetValue(const Tizen::Base::String& key, double &value)
-{
- if (key.Equals(EXIF_TAG_IMAGE_GPS_ALTITUDE, true))
- {
- 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.");
- value = __gpsAltitude;
- }
- else if (key.Equals(EXIF_TAG_IMAGE_GPS_LONGITUDE, true))
- {
- 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.");
- value = __gpsLongitude;
- } else if (key.Equals(EXIF_TAG_IMAGE_GPS_LATITUDE, true))
- {
- 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.");
- value = __gpsLatitude;
- } else
- {
- SysLogException(NID_MEDIA, E_INVALID_ARG, "[E_INVALID_ARG] Key:%S was wrong.", key.GetPointer());
- return E_INVALID_ARG;
- }
-
- return E_SUCCESS;
-}
-
-}}
-