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,
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.
19 * @file FMedia_ImageBufferImpl.cpp
20 * @brief This file contains the implementation of ImageBuffer's impl layer.
23 #include <unique_ptr.h>
24 #include <FBaseColArrayListT.h>
25 #include <FMediaImageBuffer.h>
26 #include <FBaseInternalTypes.h>
27 #include <FBaseSysLog.h>
28 #include <FApp_AppInfo.h>
29 #include <FMedia_MediaUtil.h>
30 #include <FMediaImageTypes.h>
31 #include "FMedia_ImageBufferImpl.h"
32 #include "FMedia_ImageUtil.h"
33 #include "FMedia_ImageUtilImpl.h"
34 #include "FMedia_ImageDecoder.h"
35 #include "FMedia_ImageEncoder.h"
36 #include "FMedia_ColorConverter.h"
37 #include "FMedia_ExifUtil.h"
38 #include "FUi_CoordinateSystemUtils.h"
39 #include "FGrp_BitmapImpl.h"
41 using namespace Tizen::Graphics;
42 using namespace Tizen::Base;
43 using namespace Tizen::Base::Collection;
44 using namespace Tizen::Io;
45 using namespace Tizen::App;
46 using namespace Tizen::Ui;
48 namespace Tizen { namespace Media
52 ImageRotationType rotateType;
53 ImageFlipType flipType;
57 static const _ImageExifInfo _IMAGE_ROTATE_FLIP_MAP[] = {
58 { IMAGE_ROTATION_0, IMAGE_FLIP_NONE, false }, /* NONE */
59 { IMAGE_ROTATION_0, IMAGE_FLIP_NONE, false }, /* TOP_LEFT */
60 { IMAGE_ROTATION_0, IMAGE_FLIP_VERTICAL, false }, /* TOP_RIGHT */
61 { IMAGE_ROTATION_180, IMAGE_FLIP_NONE, false }, /* BOTTOM_RIGHT */
62 { IMAGE_ROTATION_0, IMAGE_FLIP_HORIZONTAL, false }, /* BOTTOM_LEFT */
63 { IMAGE_ROTATION_90, IMAGE_FLIP_VERTICAL, true }, /* LEFT_TOP */
64 { IMAGE_ROTATION_90, IMAGE_FLIP_NONE, true }, /* RIGHT_TOP */
65 { IMAGE_ROTATION_90, IMAGE_FLIP_HORIZONTAL, true }, /* RIGHT_BOTTOM */
66 { IMAGE_ROTATION_270, IMAGE_FLIP_NONE, true } /* LEFT_BOTTOM */
69 static const ExifOrientation _ORIENTATION_MAP[] ={
70 EXIF_ORIENTATION_TOP_LEFT,
71 EXIF_ORIENTATION_TOP_LEFT,
72 EXIF_ORIENTATION_TOP_RIGHT,
73 EXIF_ORIENTATION_BOTTOM_RIGHT,
74 EXIF_ORIENTATION_BOTTOM_LEFT,
75 EXIF_ORIENTATION_LEFT_TOP,
76 EXIF_ORIENTATION_RIGHT_TOP,
77 EXIF_ORIENTATION_RIGHT_BOTTOM,
78 EXIF_ORIENTATION_LEFT_BOTTOM
81 static const MediaPixelFormat _IMAGE_BUFFER_PIXEL_FORMATS[] =
83 MEDIA_PIXEL_FORMAT_RGB565LE,
84 MEDIA_PIXEL_FORMAT_BGRA8888,
85 MEDIA_PIXEL_FORMAT_YUV420P,
86 MEDIA_PIXEL_FORMAT_NV12,
87 MEDIA_PIXEL_FORMAT_NV21,
88 MEDIA_PIXEL_FORMAT_GRAY
91 _ImageBufferImpl::_ImageBufferImpl(void)
95 , __orientation(EXIF_ORIENTATION_TOP_LEFT)
96 , __pixelFormat(MEDIA_PIXEL_FORMAT_NONE)
98 , __transparent(false)
99 , __maskColor(0xf81f) // MAGENTA
103 _ImageBufferImpl::~_ImageBufferImpl(void)
108 _ImageBufferImpl::Construct(int width, int height, MediaPixelFormat pixelFormat)
110 result r = E_SUCCESS;
115 __pixelFormat = pixelFormat;
117 SysTryReturn(NID_MEDIA, IsSupportedPixelFormat(pixelFormat) == true , null, E_UNSUPPORTED_FORMAT,
118 "[E_UNSUPPORTED_FORMAT] The specified pixelFormat is not supported.")
120 length = _ImageUtil::GetBufferSize(__pixelFormat, __width, __height);
121 SysTryReturn(NID_MEDIA, length > 0, E_INVALID_ARG, E_INVALID_ARG,
122 "[E_INVALID_ARG] Check inputs: (%d x %d), pixel format (%d).",
123 __width, __height, __pixelFormat);
125 __pBuffer.reset(new (std::nothrow) byte[length]);
126 SysTryReturn(NID_MEDIA, __pBuffer.get() != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
127 "[E_OUT_OF_MEMORY] Construct instance failed.");
128 memset(__pBuffer.get(), 0, length);
135 _ImageBufferImpl::Construct(int width, int height, MediaPixelFormat pixelFormat,
136 const byte* pData, int length)
138 result r = E_SUCCESS;
139 int reqBufferSize = 0;
141 SysTryReturn(NID_MEDIA, IsSupportedPixelFormat(pixelFormat) == true , E_UNSUPPORTED_FORMAT, E_UNSUPPORTED_FORMAT,
142 "[E_UNSUPPORTED_FORMAT] The specified pixelFormat is not supported.")
144 SysTryReturn(NID_MEDIA, (width > 0) && (height > 0), E_INVALID_ARG, E_INVALID_ARG,
145 "[E_INVALID_ARG] Dimensions should be greater than zero : (%d x %d).", width, height);
147 reqBufferSize = _ImageUtil::GetBufferSize(pixelFormat, width, height);
148 SysTryReturn(NID_MEDIA, reqBufferSize > 0, GetLastResult(), GetLastResult(),
149 "[%s] Could not get output buffer size.", GetErrorMessage(GetLastResult()));
153 SysTryReturn(NID_MEDIA, reqBufferSize <= length, E_INVALID_ARG, E_INVALID_ARG,
154 "[E_INVALID_ARG] Check length. Required for (%d x %d) in %d format is %d : arg = %d.",
155 width, height, pixelFormat, length);
160 __pixelFormat = pixelFormat;
162 __pBuffer.reset(new (std::nothrow) byte[reqBufferSize]);
163 SysTryReturn(NID_MEDIA, __pBuffer.get() != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
164 "[E_OUT_OF_MEMORY] Construct instance failed.");
165 memset(__pBuffer.get(), 0, reqBufferSize);
166 __bufSize = reqBufferSize;
170 memcpy(__pBuffer.get(), pData, reqBufferSize);
176 _ImageBufferImpl::Construct(const Tizen::Base::String &srcImagePath, const Rectangle *pDecodingRegion, bool autoRotate)
178 result r = E_SUCCESS;
180 std::unique_ptr<ByteBuffer> pBuf;
181 pBuf.reset(_MediaUtil::FileToBufferN(srcImagePath));
182 SysTryReturn(NID_MEDIA, pBuf.get() != null, GetLastResult(), GetLastResult(),
183 "[%s] FileToBufferN %S", GetErrorMessage(GetLastResult()), srcImagePath.GetPointer());
184 r = Construct(*pBuf.get(), pDecodingRegion, autoRotate);
185 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] ImageBuffer construct failed.", GetErrorMessage(r));
190 _ImageBufferImpl::Construct(const Tizen::Base::ByteBuffer &srcImageBuf, const Tizen::Graphics::Rectangle *pDecodingRegion, bool autoRotate)
192 result r = E_SUCCESS;
195 std::unique_ptr<byte[]> pDstBuf;
196 Tizen::Graphics::Rectangle transformRegion;
197 int orientationInfo = 0;
198 Boolean transparency(false);
201 r = dec.Construct(srcImageBuf);
202 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Decoder construct failed.", GetErrorMessage(r));
204 __pixelFormat = dec.GetPixelFormat();
206 SysTryReturn(NID_MEDIA, __pixelFormat != MEDIA_PIXEL_FORMAT_NONE, r, r,
207 "[%s] GetPixelFormat failed.", GetErrorMessage(r));
210 imgExif.Construct(srcImageBuf.GetPointer(), srcImageBuf.GetCapacity());
211 imgExif.GetValue(EXIF_TAG_IMAGE_ORIENTATION, orientationInfo);
212 // imgExif.GetValue() will return "r = E_OBJ_NOT_FOUND" if it could not be found exif infomation.
213 // However, the result should be decided by result of construct in this function.
214 SetLastResult(E_SUCCESS);
215 __orientation = _ORIENTATION_MAP[orientationInfo];
217 if (pDecodingRegion != null)
221 ImageRotationType rotateType = _IMAGE_ROTATE_FLIP_MAP[orientationInfo].rotateType;
222 ImageFlipType flipType = _IMAGE_ROTATE_FLIP_MAP[orientationInfo].flipType;
224 r = dec.GetDimension(imgWidth, imgHeight);
225 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] dec.GetDimension", GetErrorMessage(r));
227 if (rotateType == IMAGE_ROTATION_90 || rotateType == IMAGE_ROTATION_270)
229 SysTryReturn(NID_MEDIA, imgHeight >= (pDecodingRegion->x + pDecodingRegion->width)
230 && imgWidth >= (pDecodingRegion->y + pDecodingRegion->height),
231 E_INVALID_ARG, E_INVALID_ARG,
232 "[E_INVALID_ARG] imageWidth = %d, imageHeight = %d, x = %d, y = %d, width = %d, height = %d",
233 imgHeight, imgWidth, pDecodingRegion->x, pDecodingRegion->y,
234 pDecodingRegion->width, pDecodingRegion->height);
238 SysTryReturn(NID_MEDIA, imgWidth >= (pDecodingRegion->x + pDecodingRegion->width)
239 && imgHeight >= (pDecodingRegion->y + pDecodingRegion->height),
240 E_INVALID_ARG, E_INVALID_ARG,
241 "[E_INVALID_ARG] imageWidth = %d, imageHeight = %d, x = %d, y = %d, width = %d, height = %d",
242 imgWidth, imgHeight, pDecodingRegion->x, pDecodingRegion->y,
243 pDecodingRegion->width, pDecodingRegion->height);
246 if(autoRotate == true)
248 if (rotateType == IMAGE_ROTATION_90)
250 transformRegion.x = pDecodingRegion->y;
251 transformRegion.y = imgHeight-(pDecodingRegion->width+pDecodingRegion->x);
252 transformRegion.width = pDecodingRegion->height;
253 transformRegion.height = pDecodingRegion->width;
255 else if (rotateType == IMAGE_ROTATION_180)
257 transformRegion.x = imgWidth-(pDecodingRegion->width+pDecodingRegion->x);
258 transformRegion.y = imgHeight-(pDecodingRegion->height+pDecodingRegion->y);
259 transformRegion.width = pDecodingRegion->width;
260 transformRegion.height = pDecodingRegion->height;
262 else if (rotateType == IMAGE_ROTATION_270)
264 transformRegion.x = imgWidth-(pDecodingRegion->height+pDecodingRegion->y);
265 transformRegion.y = pDecodingRegion->x;
266 transformRegion.width = pDecodingRegion->height;
267 transformRegion.height = pDecodingRegion->width;
271 transformRegion.x = pDecodingRegion->x;
272 transformRegion.y = pDecodingRegion->y;
273 transformRegion.width = pDecodingRegion->width;
274 transformRegion.height = pDecodingRegion->height;
277 if (flipType == IMAGE_FLIP_VERTICAL)
279 transformRegion.x = imgWidth-(transformRegion.width+transformRegion.x);
281 else if (flipType == IMAGE_FLIP_HORIZONTAL)
283 transformRegion.y = imgHeight-(transformRegion.height+transformRegion.y);
288 transformRegion.x = pDecodingRegion->x;
289 transformRegion.y = pDecodingRegion->y;
290 transformRegion.width = pDecodingRegion->width;
291 transformRegion.height = pDecodingRegion->height;
294 r = dec.SetDecodingRegion(transformRegion.x, transformRegion.y,
295 transformRegion.width, transformRegion.height);
297 SysTryReturn(NID_MEDIA, r == E_SUCCESS || r == E_UNSUPPORTED_OPERATION, r, r,
298 "[%s] dec.SetDecodingRegion:%d %d %d %d", GetErrorMessage(r),
299 pDecodingRegion->x, pDecodingRegion->y, pDecodingRegion->width, pDecodingRegion->height);
301 __width = transformRegion.width;
302 __height = transformRegion.height;
306 __pBuffer.reset(dec.DecodeN(__bufSize));
307 SysTryReturn(NID_MEDIA, __pBuffer.get() != null, r, r,
308 "[%s] DecodeN returned empty buffer.", GetErrorMessage(r));
309 r = dec.GetValue("transparency", transparency);
312 __transparent = false;
317 __transparent = transparency.ToBool();
318 if (__transparent == true)
320 r = dec.GetValue(L"chromaKey", chromaKey);
323 __transparent = false;
328 __maskColor = chromaKey.ToInt();
333 else if (r == E_UNSUPPORTED_OPERATION)
336 std::unique_ptr<byte[]> pTmpbyte;
338 pTmpbyte.reset(dec.DecodeN(__bufSize));
340 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] DecodeN failed.", GetErrorMessage(r));
341 SysTryReturn(NID_MEDIA, pTmpbyte.get() != null, r, r,
342 "[%s] DecodeN returned empty buffer.", GetErrorMessage(r));
344 __pBuffer.reset(_ImageUtil::CropN(pTmpbyte.get(), __pixelFormat, imgWidth, imgHeight,
345 transformRegion.x, transformRegion.y, transformRegion.width, transformRegion.height));
347 SysTryReturn(NID_MEDIA, __pBuffer.get() != null, GetLastResult(), GetLastResult(),
348 "[%s] Crop:%x %d %d %d %d %d %d %d", GetErrorMessage(GetLastResult()), pTmpbyte.get(),
349 __pixelFormat, imgWidth, imgHeight, pDecodingRegion->x, pDecodingRegion->y,
350 pDecodingRegion->width, pDecodingRegion->height);
352 __bufSize = _ImageUtil::GetBufferSize(__pixelFormat, pDecodingRegion->width, pDecodingRegion->height);
353 SysTryReturn(NID_MEDIA, __bufSize > 0, GetLastResult(), GetLastResult(),
354 "[%s] Could not get output buffer size.", GetErrorMessage(GetLastResult()));
356 r = dec.GetValue("transparency", transparency);
359 __transparent = false;
364 __transparent = transparency.ToBool();
365 if (__transparent == true)
367 r = dec.GetValue(L"chromaKey", chromaKey);
370 __transparent = false;
375 __maskColor = chromaKey.ToInt();
383 r = dec.GetDimension(__width, __height);
384 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] GetDimension failed.",
387 __pBuffer.reset(dec.DecodeN(__bufSize));
389 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] DecodeN failed.", GetErrorMessage(r));
390 SysTryReturn(NID_MEDIA, __pBuffer.get() != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
391 "[E_OUT_OF_MEMORY] Propagated.");
393 r = dec.GetValue("transparency", transparency);
396 __transparent = false;
401 __transparent = transparency.ToBool();
402 if (__transparent == true)
404 r = dec.GetValue(L"chromaKey", chromaKey);
407 __transparent = false;
412 __maskColor = chromaKey.ToInt();
418 if (autoRotate == true)
423 __orientation = EXIF_ORIENTATION_TOP_LEFT;
425 if (orientationInfo == EXIF_ORIENTATION_TOP_LEFT || orientationInfo == 0)
430 if (orientationInfo > EXIF_ORIENTATION_LEFT_BOTTOM || orientationInfo < EXIF_ORIENTATION_TOP_LEFT)
435 pDstBuf.reset(new (std::nothrow) byte[__bufSize]);
436 SysTryReturn(NID_MEDIA, pDstBuf.get() != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
438 ImageRotationType rotateType = _IMAGE_ROTATE_FLIP_MAP[orientationInfo].rotateType;
439 ImageFlipType flipType = _IMAGE_ROTATE_FLIP_MAP[orientationInfo].flipType;
441 pDstBuf.reset(_ImageUtil::RotateN(__pBuffer.get(), __pixelFormat, __width, __height, rotateType, dstWidth, dstHeight));
443 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] _ImageUtil:Resize", GetErrorMessage(r));
446 __height = dstHeight;
448 __pBuffer.swap(pDstBuf);
449 if (flipType != IMAGE_FLIP_NONE)
451 pDstBuf.reset(_ImageUtil::FlipN(__pBuffer.get(), __pixelFormat, __width, __height, flipType));
453 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] _ImageUtil:Flip", GetErrorMessage(r));
455 __pBuffer.swap(pDstBuf);
462 _ImageBufferImpl::Construct(const Bitmap &srcBitmap)
465 Bitmap *pTmpBmp = null;
468 result r = E_SUCCESS;
470 pTmpBmp = (const_cast<Bitmap*>(&srcBitmap));
471 SysTryReturn(NID_MEDIA, pTmpBmp != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
472 "[E_OUT_OF_MEMORY] Construct instance failed.");
474 r = pTmpBmp->Lock(info);
475 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Bitmap.Lock failed.",
478 __width = info.width;
479 __height = info.height;
480 bpp = info.bitsPerPixel / 8;
481 length = __width * __height * bpp;
483 __pixelFormat = _ImageUtilImpl::ToMediaPixelFormat(srcBitmap.GetPixelColorFormat());
485 __pBuffer.reset(new (std::nothrow) byte[length]);
486 SysTryCatch(NID_MEDIA, __pBuffer.get() != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
487 "[E_OUT_OF_MEMORY] Construct instance failed.");
489 memcpy(__pBuffer.get(), (byte*)info.pPixels, length);
491 r = pTmpBmp->Unlock();
492 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Bitmap.Unlock failed.", GetErrorMessage(r));
505 _ImageBufferImpl::Construct(const Tizen::Base::String &srcImagePath, int destWidth, int destHeight, ImageScalingMethod scalingMethod)
507 result r = E_SUCCESS;
509 std::unique_ptr<ByteBuffer> pBuf;
510 pBuf.reset(_MediaUtil::FileToBufferN(srcImagePath));
511 SysTryReturn(NID_MEDIA, pBuf.get() != null, GetLastResult(), GetLastResult(),
512 "[%s] FileToBufferN %S", GetErrorMessage(GetLastResult()), srcImagePath.GetPointer());
513 r = Construct(*pBuf.get(), destWidth, destHeight, scalingMethod);
514 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] ImageBuffer construct failed.", GetErrorMessage(r));
519 _ImageBufferImpl::Construct(const Tizen::Base::ByteBuffer &srcImageBuf, int destWidth, int destHeight, ImageScalingMethod scalingMethod)
521 result r = E_SUCCESS;
522 int orientationInfo = 0;
524 std::unique_ptr<byte[]> pDstBuf;
525 Tizen::Graphics::Rectangle transformRegion;
526 Boolean transparency(false);
529 r = dec.Construct(srcImageBuf);
530 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Decoder construct failed.", GetErrorMessage(r));
532 __pixelFormat = dec.GetPixelFormat();
534 SysTryReturn(NID_MEDIA, __pixelFormat != MEDIA_PIXEL_FORMAT_NONE, r, r,
535 "[%s] GetPixelFormat failed.", GetErrorMessage(r));
538 imgExif.Construct(srcImageBuf.GetPointer(), srcImageBuf.GetCapacity());
539 imgExif.GetValue(EXIF_TAG_IMAGE_ORIENTATION, orientationInfo);
540 // imgExif.GetValue() will return "r = E_OBJ_NOT_FOUND" if it could not be found exif infomation.
541 // However, the result should be decided by result of construct in this function.
542 SetLastResult(E_SUCCESS);
544 __orientation = _ORIENTATION_MAP[orientationInfo];
546 dec.SetOutputDimension(destWidth, destHeight, false);
548 __pBuffer.reset(dec.DecodeN(__bufSize, scalingMethod));
550 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] DecodeN failed.", GetErrorMessage(r));
551 SysTryReturn(NID_MEDIA, __pBuffer.get() != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
552 "[E_OUT_OF_MEMORY] Propagated.");
554 r = dec.GetValue("transparency", transparency);
557 __transparent = false;
562 __transparent = transparency.ToBool();
563 if (__transparent == true)
565 r = dec.GetValue(L"chromaKey", chromaKey);
568 __transparent = false;
573 __maskColor = chromaKey.ToInt();
578 r = dec.GetDimension(__width, __height);
579 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] GetDimension failed.",
582 __bufSize = _ImageUtil::GetBufferSize(__pixelFormat, __width, __height);
583 SysTryReturn(NID_MEDIA, __bufSize > 0, GetLastResult(), GetLastResult(),
584 "[%s] Could not get output buffer size.", GetErrorMessage(GetLastResult()));
590 _ImageBufferImpl::Equals(ImageBuffer* pRhs) const
592 _ImageBufferImpl* pRhsImpl = null;
593 byte* pRawOther = null;
595 MediaPixelFormat pixFmtOther = MEDIA_PIXEL_FORMAT_NONE;
597 byte* pRawThis = null;
599 MediaPixelFormat pixFmtThis = MEDIA_PIXEL_FORMAT_NONE;
602 result r = E_SUCCESS;
610 pRhsImpl = GetInstance(*pRhs);
611 if (pRhsImpl == null)
615 else if (pRhsImpl == this)
621 // Compare dimensions.
622 if ((__height != pRhsImpl->GetHeight()) || (__width != pRhsImpl->GetWidth()))
628 r = const_cast<_ImageBufferImpl*>(pRhsImpl)->Lock(pRawOther, lengthOther, pixFmtOther);
629 SysTryReturn(NID_MEDIA, r == E_SUCCESS, false, r,
630 "[%s] Could not lock RHS ImageBuffer contents.", GetErrorMessage(r));
632 r = const_cast<_ImageBufferImpl *>(this)->Lock(pRawThis, lengthThis, pixFmtThis);
633 SysTryReturn(NID_MEDIA, r == E_SUCCESS, false, r,
634 "[%s] Could not lock this ImageBuffer contents.", GetErrorMessage(r));
636 // Check if contents are equal.
637 if ((__pixelFormat != pixFmtOther) || (__bufSize != lengthOther))
643 if (memcmp(pRawOther, pRawThis, lengthOther) == 0)
660 _ImageBufferImpl::GetHashCode() const
663 int len = __bufSize / sizeof(len);
665 for (int i = 0; i < len; ++i)
667 hashCode = (hashCode << 5) - hashCode + static_cast <int>(__pBuffer[(i * sizeof(hashCode))]);
670 hashCode = (hashCode ^ (__width * __height)) | (hashCode & __pixelFormat);
676 _ImageBufferImpl::GetHeight() const
682 _ImageBufferImpl::GetWidth() const
688 _ImageBufferImpl::GetPixelFormat() const
690 return __pixelFormat;
694 _ImageBufferImpl::GetExifOrientation(void) const
696 return __orientation;
700 _ImageBufferImpl::Lock(byte* &data, int &length, MediaPixelFormat &pixelFormat)
702 result r = E_SUCCESS;
704 SysTryReturn(NID_MEDIA, __isLocked != true, E_INVALID_STATE, E_INVALID_STATE,
705 "[E_INVALID_STATE] Instance is locked. Unlock to use.");
707 data = const_cast<byte *>(__pBuffer.get());
708 length = _ImageUtil::GetBufferSize(__pixelFormat, __width, __height);
709 pixelFormat = __pixelFormat;
717 _ImageBufferImpl::Unlock()
719 result r = E_SUCCESS;
721 SysTryReturn(NID_MEDIA, __isLocked, E_INVALID_STATE, E_INVALID_STATE,
722 "[E_INVALID_STATE] Instance is not locked.");
730 _ImageBufferImpl::EncodeToBufferN(ImageFormat destImageFormat, int quality) const
732 result r = E_SUCCESS;
734 ByteBuffer* pBuf = null;
736 SysTryReturn(NID_MEDIA, !__isLocked, null, E_INVALID_STATE,
737 "[E_INVALID_STATE] Instance is locked. Unlock to use.");
739 inLength = _ImageUtil::GetBufferSize(__pixelFormat, __width, __height);
740 pBuf = _ImageEncoder::EncodeN(destImageFormat, (const byte*)__pBuffer.get(), inLength, __width, __height,
741 __pixelFormat, quality);
742 SysTryReturn(NID_MEDIA, pBuf != null, null, GetLastResult(), "[%s] Propagated. ", GetErrorMessage(GetLastResult()));
749 _ImageBufferImpl::EncodeToFile(const String &destImagePath, ImageFormat destImageFormat,
750 bool overwrite, int quality) const
752 result r = E_SUCCESS;
753 std::unique_ptr<ByteBuffer> pBuf;
755 SysTryReturn(NID_MEDIA, !__isLocked, E_INVALID_STATE, E_INVALID_STATE,
756 "[E_INVALID_STATE] Instance is locked. Unlock to use.");
758 pBuf.reset(this->EncodeToBufferN(destImageFormat, quality));
759 SysTryReturn(NID_MEDIA, pBuf.get() != null, GetLastResult(), GetLastResult(),
760 "[%s] Propagated. ", GetErrorMessage(GetLastResult()));
762 r = _MediaUtil::BufferToFile(*pBuf.get(), destImagePath, overwrite);
763 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagated. ", GetErrorMessage(r));
769 _ImageBufferImpl::GetBitmapN(BitmapPixelFormat pixelFormat, BufferScaling bufferScaling) const
771 result r = E_SUCCESS;
772 std::unique_ptr<Bitmap> pOutBitmap;
773 std::unique_ptr<ByteBuffer> pConverted;
774 MediaPixelFormat inPixelFormat = MEDIA_PIXEL_FORMAT_NONE;
776 SysTryReturn(NID_MEDIA, !__isLocked, null, E_INVALID_STATE,
777 "[E_INVALID_STATE] Instance is locked. Unlock to use.");
779 inPixelFormat = _ImageUtilImpl::ToMediaPixelFormat(pixelFormat);
780 SysTryReturn(NID_MEDIA, inPixelFormat != MEDIA_PIXEL_FORMAT_NONE, null, E_INVALID_ARG,
781 "[E_INVALID_ARG] Pixel format is %d, invalid or not supported.", pixelFormat);
783 if (inPixelFormat != __pixelFormat)
785 // Convert raw data to the required pixel format.
786 pConverted.reset(GetByteBufferN(inPixelFormat));
788 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated.", GetErrorMessage(r));
791 pOutBitmap.reset(new (std::nothrow) Bitmap);
792 SysTryReturn(NID_MEDIA, pOutBitmap.get() != null, null, E_OUT_OF_MEMORY,
793 "[E_OUT_OF_MEMORY] Propagated.");
795 if (inPixelFormat != __pixelFormat)
797 r = pOutBitmap->Construct(*pConverted.get(), Dimension(__width, __height), pixelFormat, bufferScaling);
801 std::unique_ptr<ByteBuffer> pTmpByteBuffer (new (std::nothrow) ByteBuffer());
802 r = pTmpByteBuffer->Construct(__pBuffer.get(), 0, __bufSize, __bufSize);
803 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
804 "[%s] Construct instance failed.", GetErrorMessage(r));
806 r = pOutBitmap->Construct(*pTmpByteBuffer.get(), Dimension(__width, __height), pixelFormat, bufferScaling);
809 if ((__transparent == true) && (pixelFormat == BITMAP_PIXEL_FORMAT_RGB565))
814 Color *pMaskingColor = null;
816 if (__pixelFormat == MEDIA_PIXEL_FORMAT_BGRA8888)
818 blue = (__maskColor & 0x000000ff);
819 green = (__maskColor & 0x0000ff00) >> 8;
820 red = (__maskColor & 0x00ff0000) >> 16;
824 red = (__maskColor & 0xf800) >> 8;
825 red = red | (red >> 5);
826 green = (__maskColor & 0x07e0) >> 3;
827 green = green | (green >> 6);
828 blue = (__maskColor & 0x001f) << 3;
829 blue = blue | (blue >> 5);
832 pMaskingColor = new Color(red, green, blue);
833 SysTryReturn(NID_MEDIA, pMaskingColor != null, pOutBitmap.release(), E_SUCCESS,
834 "[%s] Could not apply masking color.", GetErrorMessage(r));
835 r = pOutBitmap->SetMaskingColor(pMaskingColor);
836 delete pMaskingColor;
837 SysTryReturn(NID_MEDIA, r == E_SUCCESS, pOutBitmap.release(), E_SUCCESS,
838 "[%s] Could not set masking color.", GetErrorMessage(r));
841 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated.",
843 return pOutBitmap.release();
847 _ImageBufferImpl::GetBitmapN(BitmapPixelFormat pixelFormat, const FloatDimension &destDim) const
849 result r = E_SUCCESS;
850 std::unique_ptr<Bitmap> pOutBitmap;
851 std::unique_ptr<ByteBuffer> pConverted;
852 MediaPixelFormat inPixelFormat = MEDIA_PIXEL_FORMAT_NONE;
853 Dimension convertDim;
855 SysTryReturn(NID_MEDIA, !__isLocked, null, E_INVALID_STATE,
856 "[E_INVALID_STATE] Instance is locked. Unlock to use.");
858 inPixelFormat = _ImageUtilImpl::ToMediaPixelFormat(pixelFormat);
859 SysTryReturn(NID_MEDIA, (inPixelFormat > MEDIA_PIXEL_FORMAT_NONE) && (inPixelFormat <= MEDIA_PIXEL_FORMAT_GRAY),
860 null, E_INVALID_ARG, "[E_INVALID_ARG] Pixel format is invalid.", inPixelFormat);
862 pOutBitmap.reset(new (std::nothrow) Bitmap);
863 SysTryReturn(NID_MEDIA, pOutBitmap.get() != null, null, E_OUT_OF_MEMORY,
864 "[E_OUT_OF_MEMORY] Propagated.");
866 convertDim.width = _CoordinateSystemUtils::ConvertToInteger(destDim.width);
867 convertDim.height = _CoordinateSystemUtils::ConvertToInteger(destDim.height);
869 std::unique_ptr<ByteBuffer> pTmpByteBuffer (new (std::nothrow) ByteBuffer());
870 r = pTmpByteBuffer->Construct(__pBuffer.get(), 0, __bufSize, __bufSize);
871 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
872 "[%s] Construct instance failed.", GetErrorMessage(r));
874 if (inPixelFormat == __pixelFormat && convertDim.width == __width && convertDim.height == __height)
876 pOutBitmap.reset(_BitmapImpl::GetNonScaledBitmapN(*pTmpByteBuffer.get(), convertDim, pixelFormat, destDim));
880 std::unique_ptr<ByteBuffer> pOutBuf;
883 r = cvt.Construct(__pixelFormat, __width, __height, inPixelFormat, convertDim.width, convertDim.height);
884 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated.", GetErrorMessage(r));
886 pOutBuf.reset(cvt.ConvertN(*pTmpByteBuffer));
888 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated.", GetErrorMessage(r));
890 pOutBitmap.reset(_BitmapImpl::GetNonScaledBitmapN(*pOutBuf.get(), convertDim, pixelFormat, destDim));
893 if ((__transparent == true) && (pixelFormat == BITMAP_PIXEL_FORMAT_RGB565))
898 Color *pMaskingColor = null;
900 if (__pixelFormat == MEDIA_PIXEL_FORMAT_BGRA8888)
902 blue = (__maskColor & 0x000000ff);
903 green = (__maskColor & 0x0000ff00) >> 8;
904 red = (__maskColor & 0x00ff0000) >> 16;
908 red = (__maskColor & 0xf800) >> 8;
909 red = red | (red >> 5);
910 green = (__maskColor & 0x07e0) >> 3;
911 green = green | (green >> 6);
912 blue = (__maskColor & 0x001f) << 3;
913 blue = blue | (blue >> 5);
916 pMaskingColor = new Color(red, green, blue);
917 SysTryReturn(NID_MEDIA, pMaskingColor != null, pOutBitmap.release(), E_SUCCESS,
918 "[%s] Could not apply masking color.", GetErrorMessage(r));
919 r = pOutBitmap->SetMaskingColor(pMaskingColor);
920 delete pMaskingColor;
921 SysTryReturn(NID_MEDIA, r == E_SUCCESS, pOutBitmap.release(), E_SUCCESS,
922 "[%s] Could not set masking color.", GetErrorMessage(r));
925 SysTryReturn(NID_MEDIA, pOutBitmap.get() != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
926 return pOutBitmap.release();
930 _ImageBufferImpl::GetByteBufferN(MediaPixelFormat inPixelFormat) const
932 result r = E_SUCCESS;
933 std::unique_ptr<ByteBuffer> pOutBuf;
935 SysTryReturn(NID_MEDIA, !__isLocked, null, E_INVALID_STATE,
936 "[E_INVALID_STATE] Instance is locked. Unlock to use.");
938 SysTryReturn(NID_MEDIA, IsSupportedPixelFormat(inPixelFormat) == true , null, E_UNSUPPORTED_FORMAT,
939 "[E_UNSUPPORTED_FORMAT] The specified pixelFormat is not supported.")
941 std::unique_ptr<ByteBuffer> pTmpByteBuffer (new (std::nothrow) ByteBuffer());
942 r = pTmpByteBuffer->Construct(__pBuffer.get(), 0, __bufSize, __bufSize);
943 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
944 "[%s] Construct instance failed.", GetErrorMessage(r));
946 if (inPixelFormat != __pixelFormat)
948 // Convert raw data to the required pixel format.
951 r = cvt.Construct(__pixelFormat, __width, __height,
952 inPixelFormat, __width, __height);
953 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated.", GetErrorMessage(r));
955 pOutBuf.reset(cvt.ConvertN(*pTmpByteBuffer));
957 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated.", GetErrorMessage(r));
961 pOutBuf.reset(new (std::nothrow) ByteBuffer);
962 SysTryReturn(NID_MEDIA, pOutBuf.get() != null, null, E_OUT_OF_MEMORY,
963 "[E_OUT_OF_MEMORY] Propagated.");
965 r = pOutBuf->Construct(*pTmpByteBuffer);
966 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
967 "[%s] Propagated.", GetErrorMessage(r));
971 return pOutBuf.release();
975 _ImageBufferImpl::CloneN() const
977 std::unique_ptr<ImageBuffer> pImgBuffer;
978 result r = E_SUCCESS;
981 MediaPixelFormat pixFmt = MEDIA_PIXEL_FORMAT_NONE;
982 _ImageBufferImpl* pClonedImpl = null;
984 r = const_cast<_ImageBufferImpl *>(this)->Lock(pData, length, pixFmt);
985 SysTryReturn(NID_MEDIA, r == E_SUCCESS, pImgBuffer.get(), r, "[%s] Propagated.", GetErrorMessage(r));
987 pImgBuffer.reset(new (std::nothrow) ImageBuffer);
988 SysTryReturn(NID_MEDIA, pImgBuffer.get() != null, null, E_OUT_OF_MEMORY,
989 "[E_OUT_OF_MEMORY] Could not create new instance.")
991 r = pImgBuffer->Construct(__width, __height, pixFmt, pData, length);
992 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
993 "[%s] Failed to construct the cloned ImageBuffer.", GetErrorMessage(r));
995 pClonedImpl = GetInstance(*pImgBuffer);
996 SysTryReturn(NID_MEDIA, pClonedImpl != null, null, r,
997 "[%s] Failed to GetInstance.", GetErrorMessage(r));
998 pClonedImpl->__maskColor = __maskColor;
999 pClonedImpl->__transparent = __transparent;
1001 const_cast<_ImageBufferImpl *>(this)->Unlock();
1004 return pImgBuffer.release();
1008 _ImageBufferImpl::ConvertPixelFormatN(MediaPixelFormat pixelFormat) const
1010 std::unique_ptr<ImageBuffer> pImgBuffer;
1011 result r = E_SUCCESS;
1013 std::unique_ptr<byte[]> pDataConvert;
1016 MediaPixelFormat pixFmt = MEDIA_PIXEL_FORMAT_NONE;
1017 _ImageBufferImpl* pConvertedImpl = null;
1019 SysTryReturn(NID_MEDIA, IsSupportedPixelFormat(pixelFormat) == true , null, E_UNSUPPORTED_FORMAT,
1020 "[E_UNSUPPORTED_FORMAT] The specified pixelFormat is not supported.")
1022 SysTryReturn(NID_MEDIA, _ImageUtil::IsValidDimension(pixelFormat, __width, __height) == true, null, E_INVALID_ARG,
1023 "[E_INVALID_ARG] Dimensions should be even for this pixel format: (%d x %d)", __width, __height);
1025 if (pixelFormat != __pixelFormat)
1027 _ColorConverter cvt;
1029 r = cvt.Construct(__pixelFormat, __width, __height,
1030 pixelFormat, __width, __height);
1031 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated.", GetErrorMessage(r));
1033 r = const_cast<_ImageBufferImpl *>(this)->Lock(pData, length, pixFmt);
1034 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
1035 "[%s] Lock failed. Propagated.", GetErrorMessage(r));
1037 pDataConvert.reset(cvt.ConvertN(pData, length, dstLength));
1038 r = GetLastResult();
1039 SysTryCatch(NID_MEDIA, (r == E_SUCCESS) && (pDataConvert.get() != null), , r,
1040 "[%s] Failed to convert data to required pixelformat.", GetErrorMessage(r));
1042 const_cast<_ImageBufferImpl *>(this)->Unlock();
1044 pImgBuffer.reset(new (std::nothrow) ImageBuffer);
1045 SysTryCatch(NID_MEDIA, pImgBuffer.get() != null, , E_OUT_OF_MEMORY,
1046 "[E_OUT_OF_MEMORY] Could not create new instance.")
1048 r = pImgBuffer->Construct(__width, __height, pixelFormat, pDataConvert.get(), dstLength);
1049 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
1050 "[%s] Failed to construct the converted ImageBuffer.", GetErrorMessage(r));
1052 pConvertedImpl = GetInstance(*pImgBuffer);
1053 SysTryReturn(NID_MEDIA, pConvertedImpl != null, null, r,
1054 "[%s] Failed to GetInstance.", GetErrorMessage(r));
1055 pConvertedImpl->__maskColor = __maskColor;
1056 pConvertedImpl->__transparent = __transparent;
1061 pImgBuffer.reset(CloneN());
1065 return pImgBuffer.release();
1068 const_cast<_ImageBufferImpl *>(this)->Unlock();
1074 _ImageBufferImpl::ResizeN(int width, int height) const
1076 std::unique_ptr<ImageBuffer> pImgBuffer;
1077 result r = E_SUCCESS;
1079 std::unique_ptr<byte[]> pDataResized;
1082 MediaPixelFormat pixFmt = MEDIA_PIXEL_FORMAT_NONE;
1083 _ImageBufferImpl* pResizedImpl = null;
1085 SysTryReturn(NID_MEDIA, (width > 0) && (height > 0), null, E_INVALID_ARG,
1086 "[E_INVALID_ARG] Dimensions should be greater than zero: (%d x %d)", width, height);
1088 SysTryReturn(NID_MEDIA, _ImageUtil::IsValidDimension(__pixelFormat, width, height) == true, null, E_INVALID_ARG,
1089 "[E_INVALID_ARG] Dimensions should be even for this pixel format: (%d x %d)", width, height);
1091 if ((width != __width) && (height != __height))
1093 dstLength = _ImageUtil::GetBufferSize(__pixelFormat, width, height);
1094 SysTryReturn(NID_MEDIA, dstLength > 0, pImgBuffer.get(), GetLastResult(),
1095 "[%s] Failed to get valid buffer length for (%d x %d), pixel format %d.",
1096 GetErrorMessage(GetLastResult()), width, height, __pixelFormat);
1098 pDataResized.reset(new (std::nothrow) byte[dstLength]);
1099 SysTryReturn(NID_MEDIA, pDataResized.get() != null, pImgBuffer.get(), E_OUT_OF_MEMORY,
1100 "[E_OUT_OF_MEMORY] Failed to allocate %d bytes.", dstLength);
1102 r = const_cast<_ImageBufferImpl *>(this)->Lock(pData, length, pixFmt);
1103 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
1104 "[%s] Lock failed. Propagated.", GetErrorMessage(r));
1106 r = _ImageUtil::ResizeBuffer(pData, __pixelFormat, __width, __height,
1107 pDataResized.get(), width, height);
1108 SysTryCatch(NID_MEDIA, (r == E_SUCCESS) && (pDataResized.get() != null), , r,
1109 "[%s] Could not resize buffer.", GetErrorMessage(r));
1111 const_cast<_ImageBufferImpl *>(this)->Unlock();
1113 pImgBuffer.reset(new (std::nothrow) ImageBuffer);
1114 SysTryCatch(NID_MEDIA, pImgBuffer.get() != null, , E_OUT_OF_MEMORY,
1115 "[E_OUT_OF_MEMORY] Could not create new instance.")
1117 r = pImgBuffer->Construct(width, height, __pixelFormat, pDataResized.release(), dstLength);
1118 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
1119 "[%s] Failed to construct the resized ImageBuffer.", GetErrorMessage(r));
1121 pResizedImpl = GetInstance(*pImgBuffer);
1122 SysTryReturn(NID_MEDIA, pResizedImpl != null, null, r,
1123 "[%s] Failed to GetInstance.", GetErrorMessage(r));
1124 pResizedImpl->__maskColor = __maskColor;
1125 pResizedImpl->__transparent = __transparent;
1129 pImgBuffer.reset(CloneN());
1133 return pImgBuffer.release();
1137 const_cast<_ImageBufferImpl *>(this)->Unlock();
1143 _ImageBufferImpl::FlipN(ImageFlipType flipType) const
1145 std::unique_ptr<ImageBuffer> pImgBuffer;
1146 result r = E_SUCCESS;
1148 std::unique_ptr<byte[]> pDataFlip;
1150 MediaPixelFormat pixFmt = MEDIA_PIXEL_FORMAT_NONE;
1151 _ImageBufferImpl* pFlippedImpl = null;
1153 SysTryReturn(NID_MEDIA, (flipType >= IMAGE_FLIP_NONE) && (flipType <= IMAGE_FLIP_VERTICAL),
1154 pImgBuffer.get(), E_INVALID_ARG, "[E_INVALID_ARG] Flip type is not valid: %d.", flipType);
1156 if (flipType != IMAGE_FLIP_NONE)
1158 r = const_cast<_ImageBufferImpl *>(this)->Lock(pData, length, pixFmt);
1159 SysTryReturn(NID_MEDIA, r == E_SUCCESS, pImgBuffer.get(), r,
1160 "[%s] Lock failed. Propagated.", GetErrorMessage(r));
1162 pDataFlip.reset(new (std::nothrow) byte[length]);
1163 SysTryCatch(NID_MEDIA, pDataFlip.get() != null, , E_OUT_OF_MEMORY,
1164 "[E_OUT_OF_MEMORY] Could not allocate %d bytes for flipped output.", length);
1166 r = _ImageUtil::FlipBuffer(pData, __pixelFormat, __width, __height, pDataFlip.get(), flipType);
1167 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
1168 "[%s] Could not flip the data.", GetErrorMessage(r));
1170 const_cast<_ImageBufferImpl *>(this)->Unlock();
1172 pImgBuffer.reset(new (std::nothrow) ImageBuffer);
1173 SysTryCatch(NID_MEDIA, pImgBuffer.get() != null, , E_OUT_OF_MEMORY,
1174 "[E_OUT_OF_MEMORY] Could not create new instance.")
1176 r = pImgBuffer->Construct(__width, __height, __pixelFormat, pDataFlip.get(), length);
1177 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
1178 "[%s] Failed to construct the fliped ImageBuffer.", GetErrorMessage(r));
1180 pFlippedImpl = GetInstance(*pImgBuffer);
1181 SysTryReturn(NID_MEDIA, pFlippedImpl != null, null, r,
1182 "[%s] Failed to GetInstance.", GetErrorMessage(r));
1183 pFlippedImpl->__maskColor = __maskColor;
1184 pFlippedImpl->__transparent = __transparent;
1188 pImgBuffer.reset(CloneN());
1192 return pImgBuffer.release();
1196 const_cast<_ImageBufferImpl *>(this)->Unlock();
1203 _ImageBufferImpl::RotateN(ImageRotationType rotationType) const
1205 std::unique_ptr<ImageBuffer> pImgBuffer;
1206 result r = E_SUCCESS;
1208 std::unique_ptr<byte[]> pDataRotate;
1210 int rotatedWidth= 0;
1211 int rotatedHeight = 0;
1212 MediaPixelFormat pixFmt = MEDIA_PIXEL_FORMAT_NONE;
1213 _ImageBufferImpl* pRotatedImpl = null;
1215 SysTryReturn(NID_MEDIA, (rotationType >= IMAGE_ROTATION_0) && (rotationType <= IMAGE_ROTATION_270),
1216 pImgBuffer.get(), E_INVALID_ARG, "[E_INVALID_ARG] Rotation type is not valid: %d.", rotationType);
1218 if (rotationType != IMAGE_ROTATION_0)
1220 r = const_cast<_ImageBufferImpl *>(this)->Lock(pData, length, pixFmt);
1221 SysTryReturn(NID_MEDIA, r == E_SUCCESS, pImgBuffer.get(), r,
1222 "[%s] Lock failed. Propagated.", GetErrorMessage(r));
1224 pDataRotate.reset(new (std::nothrow) byte[length]);
1225 SysTryCatch(NID_MEDIA, pDataRotate.get() != null, , E_OUT_OF_MEMORY,
1226 "[E_OUT_OF_MEMORY] Could not allocate %d bytes for rotated output.", length);
1228 r = _ImageUtil::RotateBuffer(pData, __pixelFormat, __width, __height, pDataRotate.get(),
1229 rotatedWidth, rotatedHeight, rotationType);
1230 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
1231 "[%s] Could not rotate the data.", GetErrorMessage(r));
1233 const_cast<_ImageBufferImpl *>(this)->Unlock();
1235 pImgBuffer.reset(new (std::nothrow) ImageBuffer);
1236 SysTryCatch(NID_MEDIA, pImgBuffer.get() != null, , E_OUT_OF_MEMORY,
1237 "[E_OUT_OF_MEMORY] Could not create new instance.")
1239 r = pImgBuffer->Construct(rotatedWidth, rotatedHeight, __pixelFormat, pDataRotate.get(), length);
1240 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
1241 "[%s] Failed to construct the rotated ImageBuffer.", GetErrorMessage(r));
1243 pRotatedImpl = GetInstance(*pImgBuffer);
1244 SysTryReturn(NID_MEDIA, pRotatedImpl != null, null, r,
1245 "[%s] Failed to GetInstance.", GetErrorMessage(r));
1246 pRotatedImpl->__maskColor = __maskColor;
1247 pRotatedImpl->__transparent = __transparent;
1251 pImgBuffer.reset(CloneN());
1255 return pImgBuffer.release();
1259 const_cast<_ImageBufferImpl *>(this)->Unlock();
1266 _ImageBufferImpl::CropN(int x, int y, int width, int height) const
1268 std::unique_ptr<ImageBuffer> pImgBuffer;
1269 result r = E_SUCCESS;
1271 std::unique_ptr<byte[]> pDataCropped;
1274 MediaPixelFormat pixFmt = MEDIA_PIXEL_FORMAT_NONE;
1275 _ImageBufferImpl *pCroppedImpl = null;
1277 SysTryReturn(NID_MEDIA, (x >= 0) && (y >= 0) && (x <= __width) && (y <= __height),
1278 pImgBuffer.get(), E_INVALID_ARG,
1279 "[E_INVALID_ARG] Origin should lie within image: (%d, %d) is outside (0, 0) - (%d, %d).",
1280 x, y, __width, __height);
1282 SysTryReturn(NID_MEDIA, (width > 0) && (height > 0),
1283 pImgBuffer.get(), E_INVALID_ARG,
1284 "[E_INVALID_ARG] Dimensions of cropped region should greater than zero: (%d, %d).",
1287 SysTryReturn(NID_MEDIA, (x + width <= __width) && (y + height <= __height),
1288 pImgBuffer.get(), E_INVALID_ARG,
1289 "[E_INVALID_ARG] End of cropped region should lie within image: (%d, %d) is outside (0, 0) - (%d, %d).",
1290 x + width, y + height, __width, __height);
1292 SysTryReturn(NID_MEDIA, _ImageUtil::IsValidDimension(__pixelFormat, width, height) == true,
1293 pImgBuffer.get(), E_INVALID_ARG,
1294 "[E_INVALID_ARG] Current pixel format does not support odd dimensions (%d, %d).",
1297 if ((x != 0) || (y != 0) || (width != __width) || (height != __height))
1299 r = const_cast<_ImageBufferImpl *>(this)->Lock(pData, length, pixFmt);
1300 SysTryReturn(NID_MEDIA, r == E_SUCCESS, pImgBuffer.get(), r,
1301 "[%s] Lock failed. Propagated.", GetErrorMessage(r));
1303 dstLength = _ImageUtil::GetBufferSize(__pixelFormat, width, height);
1304 SysTryCatch(NID_MEDIA, dstLength > 0, , GetLastResult(),
1305 "[%s] Could not get output buffer size.", GetErrorMessage(GetLastResult()));
1307 pDataCropped.reset(new (std::nothrow) byte[dstLength]);
1308 SysTryCatch(NID_MEDIA, pDataCropped.get() != null, , E_OUT_OF_MEMORY,
1309 "[E_OUT_OF_MEMORY] Could not allocate %d bytes for cropped output.", dstLength);
1311 r = _ImageUtil::CropBuffer(pData, __pixelFormat, __width, __height,
1312 pDataCropped.get(), x, y, width, height);
1313 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
1314 "[%s] Could not Crop the data.", GetErrorMessage(r));
1316 const_cast<_ImageBufferImpl *>(this)->Unlock();
1318 pImgBuffer.reset(new (std::nothrow) ImageBuffer);
1319 SysTryCatch(NID_MEDIA, pImgBuffer.get() != null, , E_OUT_OF_MEMORY,
1320 "[E_OUT_OF_MEMORY] Could not create new instance.")
1322 r = pImgBuffer->Construct(width, height, __pixelFormat, pDataCropped.get(), dstLength);
1323 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
1324 "[%s] Failed to construct the croped ImageBuffer.", GetErrorMessage(r));
1326 pCroppedImpl = GetInstance(*pImgBuffer);
1327 SysTryReturn(NID_MEDIA, pCroppedImpl != null, null, r,
1328 "[%s] Failed to GetInstance.", GetErrorMessage(r));
1329 pCroppedImpl->__maskColor = __maskColor;
1330 pCroppedImpl->__transparent = __transparent;
1334 pImgBuffer.reset(CloneN());
1338 return pImgBuffer.release();
1342 const_cast<_ImageBufferImpl *>(this)->Unlock();
1349 _ImageBufferImpl::GetImageInfo(const String& srcImagePath, ImageFormat& imageFormat,
1350 int &width, int &height)
1352 result r = E_SUCCESS;
1354 std::unique_ptr<ByteBuffer> pSrcBuf(_MediaUtil::FileToBufferN(srcImagePath, 0));
1355 SysTryReturn(NID_MEDIA, pSrcBuf.get() != null, GetLastResult(), GetLastResult(),
1356 "[%s] Propagated.", GetErrorMessage(GetLastResult()));
1358 r = GetImageInfo(*pSrcBuf.get(), imageFormat, width, height);
1364 _ImageBufferImpl::GetImageInfo(const ByteBuffer& srcImageBuf, ImageFormat& imageFormat,
1365 int &width, int &height)
1367 result r = E_SUCCESS;
1370 r = dec.Construct(srcImageBuf, MEDIA_PIXEL_FORMAT_RGB565LE);
1371 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] dec.Construct", GetErrorMessage(r));
1372 imageFormat = dec.GetImageFormat();
1373 r = dec.GetDimension(width, height);
1374 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] dec.GetDimension", GetErrorMessage(r));
1380 _ImageBufferImpl::IsSupportedPixelFormat(MediaPixelFormat pixelFormat)
1382 for (unsigned int i = 0; i < sizeof(_IMAGE_BUFFER_PIXEL_FORMATS)/sizeof(_IMAGE_BUFFER_PIXEL_FORMATS[0]) ; i++)
1384 if(pixelFormat == _IMAGE_BUFFER_PIXEL_FORMATS[i])
1392 IListT<MediaPixelFormat>*
1393 _ImageBufferImpl::GetSupportedPixelFormatListN()
1395 result r = E_SUCCESS;
1396 std::unique_ptr<ArrayListT<MediaPixelFormat> > pList(new (std::nothrow) ArrayListT<MediaPixelFormat>());
1397 SysTryReturn(NID_MEDIA, pList.get() != null, null, E_OUT_OF_MEMORY,
1398 "[E_OUT_OF_MEMORY] Could not allocate memory for output.");
1400 // TODO update list later for supported color formats in color converter?.
1401 for (unsigned int i=0; i<sizeof(_IMAGE_BUFFER_PIXEL_FORMATS)/sizeof(_IMAGE_BUFFER_PIXEL_FORMATS[0]); i++)
1403 r = pList->Add(_IMAGE_BUFFER_PIXEL_FORMATS[i]);
1404 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
1405 "[%s] Adding elements to list failed. List is not complete.", GetErrorMessage(r));
1409 return pList.release();
1413 _ImageBufferImpl::GetInstance(ImageBuffer& a)
1418 const _ImageBufferImpl*
1419 _ImageBufferImpl::GetInstance(const ImageBuffer& a)