Tizen 2.1 base
[platform/framework/native/content.git] / src / FCnt_ImageMetadataImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file                FCnt_ImageMetadataImpl.cpp
19  * @brief               This is the implementation file for the %_ImageMetadataImpl class.
20  *
21  * This file contains implementation of the %_ImageMetadataImpl class.
22  */
23
24 // includes
25 #include <FBaseSysLog.h>
26 #include <FMediaImage.h>
27 #include <FGrpBitmap.h>
28 #include <FCntImageMetadata.h>
29 #include <FCnt_ImageMetadataImpl.h>
30 #include <FIo_FileImpl.h>
31 #include <FMedia_ImageImpl.h>
32
33 // using namespaces
34 using namespace std;
35 using namespace Tizen::Base;
36 using namespace Tizen::Io;
37 using namespace Tizen::Graphics;
38 using namespace Tizen::Media;
39
40 namespace Tizen { namespace Content
41 {
42
43 static const int THUMBNAIL_IMAGE_WIDTH = 320;
44 static const int THUMBNAIL_IMAGE_HEIGHT = 240;
45 static const double DEFAULT_COORDINATE = -200.0;
46
47 _ImageMetadataImpl::_ImageMetadataImpl(void)
48         : Object()
49 {
50         unique_ptr<ImageMeta, ImageMetaDeleter> pNewImageMeta(new (nothrow) ImageMeta());
51         if (pNewImageMeta != null)
52         {
53                 __pImageMeta = move(pNewImageMeta);
54         }
55         else
56         {
57                 __pImageMeta = null;
58                 SysLog(NID_CNT, "The memory is insufficient.");
59         }
60 }
61
62 _ImageMetadataImpl::~_ImageMetadataImpl(void)
63 {
64
65 }
66
67 _ImageMetadataImpl*
68 _ImageMetadataImpl::GetInstance(ImageMetadata& imageMetadata)
69 {
70         return imageMetadata.__pImpl;
71 }
72
73 const _ImageMetadataImpl*
74 _ImageMetadataImpl::GetInstance(const ImageMetadata& imageMetadata)
75 {
76         return imageMetadata.__pImpl;
77 }
78
79 result
80 _ImageMetadataImpl::SetImageMetadata(const ImageMeta* pImageMeta)
81 {
82         ClearLastResult();
83
84         SysTryReturnResult(NID_CNT, pImageMeta != null, E_INVALID_ARG, "pImageMeta is null.");
85
86         __pImageMeta->width = pImageMeta->width;
87         __pImageMeta->height = pImageMeta->height;
88         __pImageMeta->orientation = pImageMeta->orientation;
89         __pImageMeta->latitude = pImageMeta->latitude;
90         __pImageMeta->longitude = pImageMeta->longitude;
91         __pImageMeta->contentPath = pImageMeta->contentPath;
92
93         if (pImageMeta->pManufacturer != null)
94         {
95                 __pImageMeta->pManufacturer = new (nothrow) String(*(pImageMeta->pManufacturer));
96                 SysTryReturnResult(NID_CNT, __pImageMeta->pManufacturer != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
97         }
98         if (pImageMeta->pModel != null)
99         {
100                 __pImageMeta->pModel = new (nothrow) String(*(pImageMeta->pModel));
101                 SysTryReturnResult(NID_CNT, __pImageMeta->pModel != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
102         }
103         if (pImageMeta->pDateTime != null)
104         {
105                 __pImageMeta->pDateTime = new (nothrow) String(*(pImageMeta->pDateTime));
106                 SysTryReturnResult(NID_CNT, __pImageMeta->pDateTime != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
107         }
108         if (pImageMeta->pSoftware != null)
109         {
110                 __pImageMeta->pSoftware = new (nothrow) String(*(pImageMeta->pSoftware));
111                 SysTryReturnResult(NID_CNT, __pImageMeta->pSoftware != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
112         }
113         if (pImageMeta->pWhiteBalance != null)
114         {
115                 __pImageMeta->pWhiteBalance = new (nothrow) String(*(pImageMeta->pWhiteBalance));
116                 SysTryReturnResult(NID_CNT, __pImageMeta->pWhiteBalance != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
117         }
118
119         return E_SUCCESS;
120 }
121
122 ImageMeta*
123 _ImageMetadataImpl::GetImageMetadata(void) const
124 {
125         return __pImageMeta.get();
126 }
127
128 _ImageMetadataImpl::_ImageMetadataImpl(const _ImageMetadataImpl& rhs)
129         : Object()
130 {
131         unique_ptr<ImageMeta, ImageMetaDeleter> pNewImageMeta(new (nothrow) ImageMeta());
132         if (pNewImageMeta != null)
133         {
134                 pNewImageMeta->width = (rhs.__pImageMeta)->width;
135                 pNewImageMeta->height = (rhs.__pImageMeta)->height;
136                 pNewImageMeta->orientation = (rhs.__pImageMeta)->orientation;
137                 pNewImageMeta->latitude = (rhs.__pImageMeta)->latitude;
138                 pNewImageMeta->longitude = (rhs.__pImageMeta)->longitude;
139                 pNewImageMeta->contentPath = (rhs.__pImageMeta)->contentPath;
140
141                 if ((rhs.__pImageMeta)->pManufacturer != null)
142                 {
143                         pNewImageMeta->pManufacturer = new (nothrow) String(*((rhs.__pImageMeta)->pManufacturer));
144                         if (pNewImageMeta->pManufacturer == null)
145                         {
146                                 SysLog(NID_CNT, "The memory is insufficient.");
147                                 return;
148                         }
149                 }
150                 if ((rhs.__pImageMeta)->pModel != null)
151                 {
152                         pNewImageMeta->pModel = new (nothrow) String(*((rhs.__pImageMeta)->pModel));
153                         if (pNewImageMeta->pModel == null)
154                         {
155                                 SysLog(NID_CNT, "The memory is insufficient.");
156                                 return;
157                         }
158                 }
159                 if ((rhs.__pImageMeta)->pDateTime != null)
160                 {
161                         pNewImageMeta->pDateTime = new (nothrow) String(*((rhs.__pImageMeta)->pDateTime));
162                         if (pNewImageMeta->pDateTime == null)
163                         {
164                                 SysLog(NID_CNT, "The memory is insufficient.");
165                                 return;
166                         }
167                 }
168                 if ((rhs.__pImageMeta)->pSoftware != null)
169                 {
170                         pNewImageMeta->pSoftware = new (nothrow) String(*((rhs.__pImageMeta)->pSoftware));
171                         if (pNewImageMeta->pSoftware == null)
172                         {
173                                 SysLog(NID_CNT, "The memory is insufficient.");
174                                 return;
175                         }
176                 }
177                 if ((rhs.__pImageMeta)->pWhiteBalance != null)
178                 {
179                         pNewImageMeta->pWhiteBalance = new (nothrow) String(*((rhs.__pImageMeta)->pWhiteBalance));
180                         if (pNewImageMeta->pWhiteBalance == null)
181                         {
182                                 SysLog(NID_CNT, "The memory is insufficient.");
183                                 return;
184                         }
185                 }
186
187                 __pImageMeta = move(pNewImageMeta);
188         }
189         else
190         {
191                 __pImageMeta = null;
192                 SysLog(NID_CNT, "The memory is insufficient.");
193         }
194 }
195
196 _ImageMetadataImpl&
197 _ImageMetadataImpl::operator =(const _ImageMetadataImpl& rhs)
198 {
199         // check for self-assignment
200         if (this == &rhs)
201         {
202                 return *this;
203         }
204
205         // __pImageMeta
206         if (__pImageMeta != null && (rhs.__pImageMeta) != null)
207         {
208                 // DeleteMetadata(__pImageMeta);
209                 // __pImageMeta = new (nothrow) ImageMeta;
210
211                 __pImageMeta.reset(new (nothrow) ImageMeta);
212         }
213         else if (__pImageMeta != null)
214         {
215                 //DeleteMetadata(__pImageMeta);
216
217                 __pImageMeta.reset(null);
218                 return *this;
219         }
220         else if ((rhs.__pImageMeta) != null)
221         {
222                 //__pImageMeta = new (nothrow) ImageMeta;
223
224                 __pImageMeta.reset(new (nothrow) ImageMeta);
225         }
226         else
227         {
228                 // __pImageMeta and rhs.__pImageMeta are null.
229                 return *this;
230         }
231
232         __pImageMeta->width = (rhs.__pImageMeta)->width;
233         __pImageMeta->height = (rhs.__pImageMeta)->height;
234         __pImageMeta->orientation = (rhs.__pImageMeta)->orientation;
235         __pImageMeta->latitude = (rhs.__pImageMeta)->latitude;
236         __pImageMeta->longitude = (rhs.__pImageMeta)->longitude;
237         __pImageMeta->contentPath = (rhs.__pImageMeta)->contentPath;
238
239         // pManufacturer
240         if (__pImageMeta->pManufacturer != null && (rhs.__pImageMeta)->pManufacturer != null)
241         {
242                 // delete previous data
243                 delete __pImageMeta->pManufacturer;
244                 __pImageMeta->pManufacturer = null;
245
246                 __pImageMeta->pManufacturer = new (nothrow) String(*((rhs.__pImageMeta)->pManufacturer));
247         }
248         else if (__pImageMeta->pManufacturer != null)
249         {
250                 delete __pImageMeta->pManufacturer;
251                 __pImageMeta->pManufacturer = null;
252         }
253         else if ((rhs.__pImageMeta)->pManufacturer != null)
254         {
255                 __pImageMeta->pManufacturer = new (nothrow) String(*((rhs.__pImageMeta)->pManufacturer));
256         }
257
258         // pModel
259         if (__pImageMeta->pModel != null && (rhs.__pImageMeta)->pModel != null)
260         {
261                 // delete previous data
262                 delete __pImageMeta->pModel;
263                 __pImageMeta->pModel = null;
264
265                 __pImageMeta->pModel = new (nothrow) String(*((rhs.__pImageMeta)->pModel));
266         }
267         else if (__pImageMeta->pModel != null)
268         {
269                 delete __pImageMeta->pModel;
270                 __pImageMeta->pModel = null;
271         }
272         else if ((rhs.__pImageMeta)->pModel != null)
273         {
274                 __pImageMeta->pModel = new (nothrow) String(*((rhs.__pImageMeta)->pModel));
275         }
276
277         // pDateTime
278         if (__pImageMeta->pDateTime != null && (rhs.__pImageMeta)->pDateTime != null)
279         {
280                 // delete previous data
281                 delete __pImageMeta->pDateTime;
282                 __pImageMeta->pDateTime = null;
283
284                 __pImageMeta->pDateTime = new (nothrow) String(*((rhs.__pImageMeta)->pDateTime));
285         }
286         else if (__pImageMeta->pDateTime != null)
287         {
288                 delete __pImageMeta->pDateTime;
289                 __pImageMeta->pDateTime = null;
290         }
291         else if ((rhs.__pImageMeta)->pDateTime != null)
292         {
293                 __pImageMeta->pDateTime = new (nothrow) String(*((rhs.__pImageMeta)->pDateTime));
294         }
295
296         // pSoftware
297         if (__pImageMeta->pSoftware != null && (rhs.__pImageMeta)->pSoftware != null)
298         {
299                 // delete previous data
300                 delete __pImageMeta->pSoftware;
301                 __pImageMeta->pSoftware = null;
302
303                 __pImageMeta->pSoftware = new (nothrow) String(*((rhs.__pImageMeta)->pSoftware));
304         }
305         else if (__pImageMeta->pSoftware != null)
306         {
307                 delete __pImageMeta->pSoftware;
308                 __pImageMeta->pSoftware = null;
309         }
310         else if ((rhs.__pImageMeta)->pSoftware != null)
311         {
312                 __pImageMeta->pSoftware = new (nothrow) String(*((rhs.__pImageMeta)->pSoftware));
313         }
314
315         // pWhiteBalance
316         if (__pImageMeta->pWhiteBalance != null && (rhs.__pImageMeta)->pWhiteBalance != null)
317         {
318                 // delete previous data
319                 delete __pImageMeta->pWhiteBalance;
320                 __pImageMeta->pWhiteBalance = null;
321
322                 __pImageMeta->pWhiteBalance = new (nothrow) String(*((rhs.__pImageMeta)->pWhiteBalance));
323         }
324         else if (__pImageMeta->pWhiteBalance != null)
325         {
326                 delete __pImageMeta->pWhiteBalance;
327                 __pImageMeta->pWhiteBalance = null;
328         }
329         else if ((rhs.__pImageMeta)->pWhiteBalance != null)
330         {
331                 __pImageMeta->pWhiteBalance = new (nothrow) String(*((rhs.__pImageMeta)->pWhiteBalance));
332         }
333
334         SysLog(NID_CNT, "_ImageMetadataImpl::operator =(const _ImageMetadataImpl& rhs) is called.");
335         return *this;
336 }
337
338 bool
339 _ImageMetadataImpl::Equals(const Tizen::Base::Object& rhs) const
340 {
341         const _ImageMetadataImpl* pRhs = dynamic_cast <const _ImageMetadataImpl*>(&rhs);
342         if (pRhs == null)
343         {
344                 return false;
345         }
346
347         if (!((this->GetCameraManufacturer()).Equals(pRhs->GetCameraManufacturer())))
348         {
349                 return false;
350         }
351         if (!((this->GetCameraModel()).Equals(pRhs->GetCameraModel())))
352         {
353                 return false;
354         }
355         if (!((this->GetDateTime()).Equals(pRhs->GetDateTime())))
356         {
357                 return false;
358         }
359         if (this->GetHeight() != pRhs->GetHeight())
360         {
361                 return false;
362         }
363         if (Double::Compare(this->GetLatitude(), pRhs->GetLatitude()) != 0)
364         {
365                 return false;
366         }
367         if (Double::Compare(this->GetLongitude(), pRhs->GetLongitude()) != 0)
368         {
369                 return false;
370         }
371         if (this->GetOrientation() != pRhs->GetOrientation())
372         {
373                 return false;
374         }
375         if (!((this->GetSoftware()).Equals(pRhs->GetSoftware())))
376         {
377                 return false;
378         }
379         if (!((this->GetWhiteBalance()).Equals(pRhs->GetWhiteBalance())))
380         {
381                 return false;
382         }
383         if (this->GetWidth() != pRhs->GetWidth())
384         {
385                 return false;
386         }
387
388         return true;
389 }
390
391 int
392 _ImageMetadataImpl::GetHashCode(void) const
393 {
394         int hash = 0;
395
396         hash += (this->GetCameraManufacturer()).GetHashCode();
397         hash += (this->GetCameraModel()).GetHashCode();
398         hash += (this->GetDateTime()).GetHashCode();
399         hash += Integer::GetHashCode(this->GetHeight());
400         hash += Double::GetHashCode(this->GetLatitude());
401         hash += Double::GetHashCode(this->GetLongitude());
402         hash += Integer::GetHashCode(this->GetOrientation());
403         hash += (this->GetSoftware()).GetHashCode();
404         hash += (this->GetWhiteBalance()).GetHashCode();
405         hash += Integer::GetHashCode(this->GetWidth());
406
407         return hash;
408 }
409
410 int
411 _ImageMetadataImpl::GetWidth(void) const
412 {
413         if (__pImageMeta == null)
414         {
415                 SysLog(NID_CNT, "__pImageMeta is null.");
416                 return 0;
417         }
418         return __pImageMeta->width;
419 }
420
421 int
422 _ImageMetadataImpl::GetHeight(void) const
423 {
424         if (__pImageMeta == null)
425         {
426                 SysLog(NID_CNT, "__pImageMeta is null.");
427                 return 0;
428         }
429         return __pImageMeta->height;
430 }
431
432 String
433 _ImageMetadataImpl::GetCameraManufacturer(void) const
434 {
435         if (__pImageMeta == null || __pImageMeta->pManufacturer == null)
436         {
437                 SysLog(NID_CNT, "__pImageMeta or __pImageMeta->pManufacturer is null.");
438                 return String();
439         }
440         return *(__pImageMeta->pManufacturer);
441 }
442
443 String
444 _ImageMetadataImpl::GetCameraModel(void) const
445 {
446         if (__pImageMeta == null || __pImageMeta->pModel == null)
447         {
448                 SysLog(NID_CNT, "__pImageMeta or __pImageMeta->pModel is null.");
449                 return String();
450         }
451         return *(__pImageMeta->pModel);
452 }
453
454 String
455 _ImageMetadataImpl::GetSoftware(void) const
456 {
457         if (__pImageMeta == null || __pImageMeta->pSoftware == null)
458         {
459                 SysLog(NID_CNT, "__pImageMeta or __pImageMeta->pSoftware is null.");
460                 return String();
461         }
462         return *(__pImageMeta->pSoftware);
463 }
464
465 String
466 _ImageMetadataImpl::GetDateTime(void) const
467 {
468         if (__pImageMeta == null || __pImageMeta->pDateTime == null)
469         {
470                 SysLog(NID_CNT, "__pImageMeta or __pImageMeta->pDateTime is null.");
471                 return String();
472         }
473         return *(__pImageMeta->pDateTime);
474 }
475
476 double
477 _ImageMetadataImpl::GetLatitude(void) const
478 {
479         if (__pImageMeta == null)
480         {
481                 SysLog(NID_CNT, "__pImageMeta is null.");
482                 return DEFAULT_COORDINATE;
483         }
484         return __pImageMeta->latitude;
485 }
486
487 double
488 _ImageMetadataImpl::GetLongitude(void) const
489 {
490         if (__pImageMeta == null)
491         {
492                 SysLog(NID_CNT, "__pImageMeta is null.");
493                 return DEFAULT_COORDINATE;
494         }
495         return __pImageMeta->longitude;
496 }
497
498 ImageOrientationType
499 _ImageMetadataImpl::GetOrientation(void) const
500 {
501         if (__pImageMeta == null)
502         {
503                 SysLog(NID_CNT, "__pImageMeta is null.");
504                 return IMAGE_ORIENTATION_TYPE_UNKNOWN;
505         }
506         return __pImageMeta->orientation;
507 }
508
509 String
510 _ImageMetadataImpl::GetWhiteBalance(void) const
511 {
512         if (__pImageMeta == null || __pImageMeta->pWhiteBalance == null)
513         {
514                 SysLog(NID_CNT, "__pImageMeta or __pImageMeta->pWhiteBalance is null.");
515                 return String();
516         }
517         return *(__pImageMeta->pWhiteBalance);
518 }
519
520 //
521 // E_SUCCESS
522 // E_DATA_NOT_FOUND
523 // E_OUT_OF_MEMORY
524 //
525 Bitmap*
526 _ImageMetadataImpl::GetThumbnailN(void) const
527 {
528         ClearLastResult();
529
530         SysTryReturn(NID_CNT, __pImageMeta != null, null, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] GetThumbnailN failed.");
531         SysTryReturn(NID_CNT, _FileImpl::IsFileExist(__pImageMeta->contentPath), null, E_DATA_NOT_FOUND,
532                                 "[E_DATA_NOT_FOUND] GetThumbnailN failed [%ls].", (__pImageMeta->contentPath).GetPointer());
533
534         Image image;
535         result r = image.Construct();
536         SysTryReturn(NID_CNT, !IsFailed(r), null, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] GetThumbnailN failed.");
537
538         Bitmap* pBitmap = image.DecodeN(__pImageMeta->contentPath, BITMAP_PIXEL_FORMAT_ARGB8888, THUMBNAIL_IMAGE_WIDTH, THUMBNAIL_IMAGE_HEIGHT);
539         if (pBitmap == null)
540         {
541                 r = GetLastResult();
542
543                 if (r == E_OUT_OF_MEMORY)
544                 {
545                         SysLogException(NID_CNT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] GetThumbnailN failed.");
546                 }
547                 else
548                 {
549                         SysLogException(NID_CNT, E_DATA_NOT_FOUND, "[E_DATA_NOT_FOUND] GetThumbnailN failed.");
550                 }
551
552                 return null;
553         }
554
555         return pBitmap;
556 }
557 }}