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
51 #define ISSUPPORTED(x) \
52 ((x == MEDIA_PIXEL_FORMAT_YUV420P) || (x == MEDIA_PIXEL_FORMAT_BGRA8888) || \
53 (x == MEDIA_PIXEL_FORMAT_RGB565LE) || (x == MEDIA_PIXEL_FORMAT_GRAY))
56 ImageRotationType rotateType;
57 ImageFlipType flipType;
61 static const _ImageExifInfo _IMAGE_ROTATE_FLIP_MAP[] = {
62 { IMAGE_ROTATION_0, IMAGE_FLIP_NONE, false }, /* NONE */
63 { IMAGE_ROTATION_0, IMAGE_FLIP_NONE, false }, /* TOP_LEFT */
64 { IMAGE_ROTATION_0, IMAGE_FLIP_VERTICAL, false }, /* TOP_RIGHT */
65 { IMAGE_ROTATION_180, IMAGE_FLIP_NONE, false }, /* BOTTOM_RIGHT */
66 { IMAGE_ROTATION_0, IMAGE_FLIP_HORIZONTAL, false }, /* BOTTOM_LEFT */
67 { IMAGE_ROTATION_90, IMAGE_FLIP_VERTICAL, true }, /* LEFT_TOP */
68 { IMAGE_ROTATION_90, IMAGE_FLIP_NONE, true }, /* RIGHT_TOP */
69 { IMAGE_ROTATION_90, IMAGE_FLIP_HORIZONTAL, true }, /* RIGHT_BOTTOM */
70 { IMAGE_ROTATION_270, IMAGE_FLIP_NONE, true } /* LEFT_BOTTOM */
73 static const ExifOrientation _ORIENTATION_MAP[] ={
74 EXIF_ORIENTATION_TOP_LEFT,
75 EXIF_ORIENTATION_TOP_LEFT,
76 EXIF_ORIENTATION_TOP_RIGHT,
77 EXIF_ORIENTATION_BOTTOM_RIGHT,
78 EXIF_ORIENTATION_BOTTOM_LEFT,
79 EXIF_ORIENTATION_LEFT_TOP,
80 EXIF_ORIENTATION_RIGHT_TOP,
81 EXIF_ORIENTATION_RIGHT_BOTTOM,
82 EXIF_ORIENTATION_LEFT_BOTTOM
85 static const MediaPixelFormat _IMAGE_BUFFER_PIXEL_FORMATS[] =
87 MEDIA_PIXEL_FORMAT_RGB565LE,
88 MEDIA_PIXEL_FORMAT_BGRA8888,
89 MEDIA_PIXEL_FORMAT_YUV420P
92 _ImageBufferImpl::_ImageBufferImpl(void)
96 , __orientation(EXIF_ORIENTATION_TOP_LEFT)
97 , __pixelFormat(MEDIA_PIXEL_FORMAT_NONE)
99 , __transparent(false)
100 , __maskColor(0xf81f) // MAGENTA
104 _ImageBufferImpl::~_ImageBufferImpl(void)
109 _ImageBufferImpl::Construct(int width, int height, MediaPixelFormat pixelFormat)
111 result r = E_SUCCESS;
116 __pixelFormat = pixelFormat;
118 SysTryReturn(NID_MEDIA, IsSupportedPixelFormat(pixelFormat) == true , null, E_UNSUPPORTED_FORMAT,
119 "[E_UNSUPPORTED_FORMAT] The specified pixelFormat is not supported.")
121 length = _ImageUtil::GetBufferSize(__pixelFormat, __width, __height);
122 SysTryReturn(NID_MEDIA, length > 0, E_INVALID_ARG, E_INVALID_ARG,
123 "[E_INVALID_ARG] Check inputs: (%d x %d), pixel format (%d).",
124 __width, __height, __pixelFormat);
126 __pBuffer.reset(new (std::nothrow) byte[length]);
127 SysTryReturn(NID_MEDIA, __pBuffer.get() != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
128 "[E_OUT_OF_MEMORY] Construct instance failed.");
129 memset(__pBuffer.get(), 0, length);
136 _ImageBufferImpl::Construct(int width, int height, MediaPixelFormat pixelFormat,
137 const byte* pData, int length)
139 result r = E_SUCCESS;
140 int reqBufferSize = 0;
142 SysTryReturn(NID_MEDIA, IsSupportedPixelFormat(pixelFormat) == true , E_UNSUPPORTED_FORMAT, E_UNSUPPORTED_FORMAT,
143 "[E_UNSUPPORTED_FORMAT] The specified pixelFormat is not supported.")
145 SysTryReturn(NID_MEDIA, (width > 0) && (height > 0), E_INVALID_ARG, E_INVALID_ARG,
146 "[E_INVALID_ARG] Dimensions should be greater than zero : (%d x %d).", width, height);
148 SysTryReturn(NID_MEDIA, ISSUPPORTED(pixelFormat) == true, E_INVALID_ARG, E_INVALID_ARG,
149 "[E_INVALID_ARG] Pixelformat is not supported : %d.", pixelFormat);
151 reqBufferSize = _ImageUtil::GetBufferSize(pixelFormat, width, height);
152 SysTryReturn(NID_MEDIA, reqBufferSize > 0, GetLastResult(), GetLastResult(),
153 "[%s] Could not get output buffer size.", GetErrorMessage(GetLastResult()));
157 SysTryReturn(NID_MEDIA, reqBufferSize <= length, E_INVALID_ARG, E_INVALID_ARG,
158 "[E_INVALID_ARG] Check length. Required for (%d x %d) in %d format is %d : arg = %d.",
159 width, height, pixelFormat, length);
164 __pixelFormat = pixelFormat;
166 __pBuffer.reset(new (std::nothrow) byte[reqBufferSize]);
167 SysTryReturn(NID_MEDIA, __pBuffer.get() != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
168 "[E_OUT_OF_MEMORY] Construct instance failed.");
169 memset(__pBuffer.get(), 0, reqBufferSize);
170 __bufSize = reqBufferSize;
174 memcpy(__pBuffer.get(), pData, reqBufferSize);
180 _ImageBufferImpl::Construct(const Tizen::Base::String &srcImagePath, const Rectangle *pDecodingRegion, bool autoRotate)
182 result r = E_SUCCESS;
184 std::unique_ptr<ByteBuffer> pBuf;
185 pBuf.reset(_MediaUtil::FileToBufferN(srcImagePath));
186 SysTryReturn(NID_MEDIA, pBuf.get() != null, GetLastResult(), GetLastResult(),
187 "[%s] FileToBufferN %S", GetErrorMessage(GetLastResult()), srcImagePath.GetPointer());
188 r = Construct(*pBuf.get(), pDecodingRegion, autoRotate);
189 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] ImageBuffer construct failed.", GetErrorMessage(r));
194 _ImageBufferImpl::Construct(const Tizen::Base::ByteBuffer &srcImageBuf, const Tizen::Graphics::Rectangle *pDecodingRegion, bool autoRotate)
196 result r = E_SUCCESS;
199 std::unique_ptr<byte[]> pDstBuf;
200 Tizen::Graphics::Rectangle transformRegion;
201 int orientationInfo = 0;
202 Boolean transparency(false);
205 r = dec.Construct(srcImageBuf);
206 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Decoder construct failed.", GetErrorMessage(r));
208 __pixelFormat = dec.GetPixelFormat();
210 SysTryReturn(NID_MEDIA, __pixelFormat != MEDIA_PIXEL_FORMAT_NONE, r, r,
211 "[%s] GetPixelFormat failed.", GetErrorMessage(r));
214 imgExif.Construct(srcImageBuf.GetPointer(), srcImageBuf.GetCapacity());
215 imgExif.GetValue(EXIF_TAG_IMAGE_ORIENTATION, orientationInfo);
216 // imgExif.GetValue() will return "r = E_OBJ_NOT_FOUND" if it could not be found exif infomation.
217 // However, the result should be decided by result of construct in this function.
218 SetLastResult(E_SUCCESS);
219 __orientation = _ORIENTATION_MAP[orientationInfo];
221 if (pDecodingRegion != null)
225 ImageRotationType rotateType = _IMAGE_ROTATE_FLIP_MAP[orientationInfo].rotateType;
226 ImageFlipType flipType = _IMAGE_ROTATE_FLIP_MAP[orientationInfo].flipType;
228 r = dec.GetDimension(imgWidth, imgHeight);
229 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] dec.GetDimension", GetErrorMessage(r));
231 if (rotateType == IMAGE_ROTATION_90 || rotateType == IMAGE_ROTATION_270)
233 SysTryReturn(NID_MEDIA, imgHeight >= (pDecodingRegion->x + pDecodingRegion->width)
234 && imgWidth >= (pDecodingRegion->y + pDecodingRegion->height),
235 E_INVALID_ARG, E_INVALID_ARG,
236 "[E_INVALID_ARG] imageWidth = %d, imageHeight = %d, x = %d, y = %d, width = %d, height = %d",
237 imgHeight, imgWidth, pDecodingRegion->x, pDecodingRegion->y,
238 pDecodingRegion->width, pDecodingRegion->height);
242 SysTryReturn(NID_MEDIA, imgWidth >= (pDecodingRegion->x + pDecodingRegion->width)
243 && imgHeight >= (pDecodingRegion->y + pDecodingRegion->height),
244 E_INVALID_ARG, E_INVALID_ARG,
245 "[E_INVALID_ARG] imageWidth = %d, imageHeight = %d, x = %d, y = %d, width = %d, height = %d",
246 imgWidth, imgHeight, pDecodingRegion->x, pDecodingRegion->y,
247 pDecodingRegion->width, pDecodingRegion->height);
250 if(autoRotate == true)
252 if (rotateType == IMAGE_ROTATION_90)
254 transformRegion.x = pDecodingRegion->y;
255 transformRegion.y = imgHeight-(pDecodingRegion->width+pDecodingRegion->x);
256 transformRegion.width = pDecodingRegion->height;
257 transformRegion.height = pDecodingRegion->width;
259 else if (rotateType == IMAGE_ROTATION_180)
261 transformRegion.x = imgWidth-(pDecodingRegion->width+pDecodingRegion->x);
262 transformRegion.y = imgHeight-(pDecodingRegion->height+pDecodingRegion->y);
263 transformRegion.width = pDecodingRegion->width;
264 transformRegion.height = pDecodingRegion->height;
266 else if (rotateType == IMAGE_ROTATION_270)
268 transformRegion.x = imgWidth-(pDecodingRegion->height+pDecodingRegion->y);
269 transformRegion.y = pDecodingRegion->x;
270 transformRegion.width = pDecodingRegion->height;
271 transformRegion.height = pDecodingRegion->width;
275 transformRegion.x = pDecodingRegion->x;
276 transformRegion.y = pDecodingRegion->y;
277 transformRegion.width = pDecodingRegion->width;
278 transformRegion.height = pDecodingRegion->height;
281 if (flipType == IMAGE_FLIP_VERTICAL)
283 transformRegion.x = imgWidth-(transformRegion.width+transformRegion.x);
285 else if (flipType == IMAGE_FLIP_HORIZONTAL)
287 transformRegion.y = imgHeight-(transformRegion.height+transformRegion.y);
292 transformRegion.x = pDecodingRegion->x;
293 transformRegion.y = pDecodingRegion->y;
294 transformRegion.width = pDecodingRegion->width;
295 transformRegion.height = pDecodingRegion->height;
298 r = dec.SetDecodingRegion(transformRegion.x, transformRegion.y,
299 transformRegion.width, transformRegion.height);
301 SysTryReturn(NID_MEDIA, r == E_SUCCESS || r == E_UNSUPPORTED_OPERATION, r, r,
302 "[%s] dec.SetDecodingRegion:%d %d %d %d", GetErrorMessage(r),
303 pDecodingRegion->x, pDecodingRegion->y, pDecodingRegion->width, pDecodingRegion->height);
305 __width = transformRegion.width;
306 __height = transformRegion.height;
310 __pBuffer.reset(dec.DecodeN(__bufSize));
311 SysTryReturn(NID_MEDIA, __pBuffer.get() != null, r, r,
312 "[%s] DecodeN returned empty buffer.", GetErrorMessage(r));
313 r = dec.GetValue("transparency", transparency);
316 __transparent = false;
321 __transparent = transparency.ToBool();
322 if (__transparent == true)
324 r = dec.GetValue(L"chromaKey", chromaKey);
327 __transparent = false;
332 __maskColor = chromaKey.ToInt();
337 else if (r == E_UNSUPPORTED_OPERATION)
340 std::unique_ptr<byte[]> pTmpbyte;
342 pTmpbyte.reset(dec.DecodeN(__bufSize));
344 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] DecodeN failed.", GetErrorMessage(r));
345 SysTryReturn(NID_MEDIA, pTmpbyte.get() != null, r, r,
346 "[%s] DecodeN returned empty buffer.", GetErrorMessage(r));
348 __pBuffer.reset(_ImageUtil::CropN(pTmpbyte.get(), __pixelFormat, imgWidth, imgHeight,
349 transformRegion.x, transformRegion.y, transformRegion.width, transformRegion.height));
351 SysTryReturn(NID_MEDIA, __pBuffer.get() != null, GetLastResult(), GetLastResult(),
352 "[%s] Crop:%x %d %d %d %d %d %d %d", GetErrorMessage(GetLastResult()), pTmpbyte.get(),
353 __pixelFormat, imgWidth, imgHeight, pDecodingRegion->x, pDecodingRegion->y,
354 pDecodingRegion->width, pDecodingRegion->height);
356 __bufSize = _ImageUtil::GetBufferSize(__pixelFormat, pDecodingRegion->width, pDecodingRegion->height);
357 SysTryReturn(NID_MEDIA, __bufSize > 0, GetLastResult(), GetLastResult(),
358 "[%s] Could not get output buffer size.", GetErrorMessage(GetLastResult()));
360 r = dec.GetValue("transparency", transparency);
363 __transparent = false;
368 __transparent = transparency.ToBool();
369 if (__transparent == true)
371 r = dec.GetValue(L"chromaKey", chromaKey);
374 __transparent = false;
379 __maskColor = chromaKey.ToInt();
387 r = dec.GetDimension(__width, __height);
388 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] GetDimension failed.",
391 __pBuffer.reset(dec.DecodeN(__bufSize));
393 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] DecodeN failed.", GetErrorMessage(r));
394 SysTryReturn(NID_MEDIA, __pBuffer.get() != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
395 "[E_OUT_OF_MEMORY] Propagated.");
397 r = dec.GetValue("transparency", transparency);
400 __transparent = false;
405 __transparent = transparency.ToBool();
406 if (__transparent == true)
408 r = dec.GetValue(L"chromaKey", chromaKey);
411 __transparent = false;
416 __maskColor = chromaKey.ToInt();
422 if (autoRotate == true)
427 __orientation = EXIF_ORIENTATION_TOP_LEFT;
429 if (orientationInfo == EXIF_ORIENTATION_TOP_LEFT || orientationInfo == 0)
434 if (orientationInfo > EXIF_ORIENTATION_LEFT_BOTTOM || orientationInfo < EXIF_ORIENTATION_TOP_LEFT)
439 pDstBuf.reset(new (std::nothrow) byte[__bufSize]);
440 SysTryReturn(NID_MEDIA, pDstBuf.get() != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
442 ImageRotationType rotateType = _IMAGE_ROTATE_FLIP_MAP[orientationInfo].rotateType;
443 ImageFlipType flipType = _IMAGE_ROTATE_FLIP_MAP[orientationInfo].flipType;
445 pDstBuf.reset(_ImageUtil::RotateN(__pBuffer.get(), __pixelFormat, __width, __height, rotateType, dstWidth, dstHeight));
447 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] _ImageUtil:Resize", GetErrorMessage(r));
450 __height = dstHeight;
452 __pBuffer.swap(pDstBuf);
453 if (flipType != IMAGE_FLIP_NONE)
455 pDstBuf.reset(_ImageUtil::FlipN(__pBuffer.get(), __pixelFormat, __width, __height, flipType));
457 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] _ImageUtil:Flip", GetErrorMessage(r));
459 __pBuffer.swap(pDstBuf);
466 _ImageBufferImpl::Construct(const Bitmap &srcBitmap)
469 Bitmap *pTmpBmp = null;
472 result r = E_SUCCESS;
474 pTmpBmp = (const_cast<Bitmap*>(&srcBitmap));
475 SysTryReturn(NID_MEDIA, pTmpBmp != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
476 "[E_OUT_OF_MEMORY] Construct instance failed.");
478 r = pTmpBmp->Lock(info);
479 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Bitmap.Lock failed.",
482 __width = info.width;
483 __height = info.height;
484 bpp = info.bitsPerPixel / 8;
485 length = __width * __height * bpp;
487 __pixelFormat = _ImageUtilImpl::ToMediaPixelFormat(srcBitmap.GetPixelColorFormat());
489 __pBuffer.reset(new (std::nothrow) byte[length]);
490 SysTryCatch(NID_MEDIA, __pBuffer.get() != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
491 "[E_OUT_OF_MEMORY] Construct instance failed.");
493 memcpy(__pBuffer.get(), (byte*)info.pPixels, length);
495 r = pTmpBmp->Unlock();
496 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Bitmap.Unlock failed.", GetErrorMessage(r));
509 _ImageBufferImpl::Construct(const Tizen::Base::String &srcImagePath, int destWidth, int destHeight, ImageScalingMethod scalingMethod)
511 result r = E_SUCCESS;
513 std::unique_ptr<ByteBuffer> pBuf;
514 pBuf.reset(_MediaUtil::FileToBufferN(srcImagePath));
515 SysTryReturn(NID_MEDIA, pBuf.get() != null, GetLastResult(), GetLastResult(),
516 "[%s] FileToBufferN %S", GetErrorMessage(GetLastResult()), srcImagePath.GetPointer());
517 r = Construct(*pBuf.get(), destWidth, destHeight, scalingMethod);
518 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] ImageBuffer construct failed.", GetErrorMessage(r));
523 _ImageBufferImpl::Construct(const Tizen::Base::ByteBuffer &srcImageBuf, int destWidth, int destHeight, ImageScalingMethod scalingMethod)
525 result r = E_SUCCESS;
526 int orientationInfo = 0;
528 std::unique_ptr<byte[]> pDstBuf;
529 Tizen::Graphics::Rectangle transformRegion;
530 Boolean transparency(false);
533 r = dec.Construct(srcImageBuf);
534 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Decoder construct failed.", GetErrorMessage(r));
536 __pixelFormat = dec.GetPixelFormat();
538 SysTryReturn(NID_MEDIA, __pixelFormat != MEDIA_PIXEL_FORMAT_NONE, r, r,
539 "[%s] GetPixelFormat failed.", GetErrorMessage(r));
542 imgExif.Construct(srcImageBuf.GetPointer(), srcImageBuf.GetCapacity());
543 imgExif.GetValue(EXIF_TAG_IMAGE_ORIENTATION, orientationInfo);
544 // imgExif.GetValue() will return "r = E_OBJ_NOT_FOUND" if it could not be found exif infomation.
545 // However, the result should be decided by result of construct in this function.
546 SetLastResult(E_SUCCESS);
548 __orientation = _ORIENTATION_MAP[orientationInfo];
550 dec.SetOutputDimension(destWidth, destHeight, false);
552 __pBuffer.reset(dec.DecodeN(__bufSize, scalingMethod));
554 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] DecodeN failed.", GetErrorMessage(r));
555 SysTryReturn(NID_MEDIA, __pBuffer.get() != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
556 "[E_OUT_OF_MEMORY] Propagated.");
558 r = dec.GetValue("transparency", transparency);
561 __transparent = false;
566 __transparent = transparency.ToBool();
567 if (__transparent == true)
569 r = dec.GetValue(L"chromaKey", chromaKey);
572 __transparent = false;
577 __maskColor = chromaKey.ToInt();
582 r = dec.GetDimension(__width, __height);
583 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] GetDimension failed.",
586 __bufSize = _ImageUtil::GetBufferSize(__pixelFormat, __width, __height);
587 SysTryReturn(NID_MEDIA, __bufSize > 0, GetLastResult(), GetLastResult(),
588 "[%s] Could not get output buffer size.", GetErrorMessage(GetLastResult()));
594 _ImageBufferImpl::Equals(ImageBuffer* pRhs) const
596 _ImageBufferImpl* pRhsImpl = null;
597 byte* pRawOther = null;
599 MediaPixelFormat pixFmtOther = MEDIA_PIXEL_FORMAT_NONE;
601 byte* pRawThis = null;
603 MediaPixelFormat pixFmtThis = MEDIA_PIXEL_FORMAT_NONE;
606 result r = E_SUCCESS;
614 pRhsImpl = GetInstance(*pRhs);
615 if (pRhsImpl == null)
619 else if (pRhsImpl == this)
625 // Compare dimensions.
626 if ((__height != pRhsImpl->GetHeight()) || (__width != pRhsImpl->GetWidth()))
632 r = const_cast<_ImageBufferImpl*>(pRhsImpl)->Lock(pRawOther, lengthOther, pixFmtOther);
633 SysTryReturn(NID_MEDIA, r == E_SUCCESS, false, r,
634 "[%s] Could not lock RHS ImageBuffer contents.", GetErrorMessage(r));
636 r = const_cast<_ImageBufferImpl *>(this)->Lock(pRawThis, lengthThis, pixFmtThis);
637 SysTryReturn(NID_MEDIA, r == E_SUCCESS, false, r,
638 "[%s] Could not lock this ImageBuffer contents.", GetErrorMessage(r));
640 // Check if contents are equal.
641 if ((__pixelFormat != pixFmtOther) || (__bufSize != lengthOther))
647 if (memcmp(pRawOther, pRawThis, lengthOther) == 0)
664 _ImageBufferImpl::GetHashCode() const
667 int len = __bufSize / sizeof(len);
669 for (int i = 0; i < len; ++i)
671 hashCode = (hashCode << 5) - hashCode + static_cast <int>(__pBuffer[(i * sizeof(hashCode))]);
674 hashCode = (hashCode ^ (__width * __height)) | (hashCode & __pixelFormat);
680 _ImageBufferImpl::GetHeight() const
686 _ImageBufferImpl::GetWidth() const
692 _ImageBufferImpl::GetPixelFormat() const
694 return __pixelFormat;
698 _ImageBufferImpl::GetExifOrientation(void) const
700 return __orientation;
704 _ImageBufferImpl::Lock(byte* &data, int &length, MediaPixelFormat &pixelFormat)
706 result r = E_SUCCESS;
708 SysTryReturn(NID_MEDIA, __isLocked != true, E_INVALID_STATE, E_INVALID_STATE,
709 "[E_INVALID_STATE] Instance is locked. Unlock to use.");
711 data = const_cast<byte *>(__pBuffer.get());
712 length = _ImageUtil::GetBufferSize(__pixelFormat, __width, __height);
713 pixelFormat = __pixelFormat;
721 _ImageBufferImpl::Unlock()
723 result r = E_SUCCESS;
725 SysTryReturn(NID_MEDIA, __isLocked, E_INVALID_STATE, E_INVALID_STATE,
726 "[E_INVALID_STATE] Instance is not locked.");
734 _ImageBufferImpl::EncodeToBufferN(ImageFormat destImageFormat, int quality) const
736 result r = E_SUCCESS;
738 ByteBuffer* pBuf = null;
740 SysTryReturn(NID_MEDIA, !__isLocked, null, E_INVALID_STATE,
741 "[E_INVALID_STATE] Instance is locked. Unlock to use.");
743 inLength = _ImageUtil::GetBufferSize(__pixelFormat, __width, __height);
744 pBuf = _ImageEncoder::EncodeN(destImageFormat, (const byte*)__pBuffer.get(), inLength, __width, __height,
745 __pixelFormat, quality);
746 SysTryReturn(NID_MEDIA, pBuf != null, null, GetLastResult(), "[%s] Propagated. ", GetErrorMessage(GetLastResult()));
753 _ImageBufferImpl::EncodeToFile(const String &destImagePath, ImageFormat destImageFormat,
754 bool overwrite, int quality) const
756 result r = E_SUCCESS;
757 std::unique_ptr<ByteBuffer> pBuf;
759 SysTryReturn(NID_MEDIA, !__isLocked, E_INVALID_STATE, E_INVALID_STATE,
760 "[E_INVALID_STATE] Instance is locked. Unlock to use.");
762 pBuf.reset(this->EncodeToBufferN(destImageFormat, quality));
763 SysTryReturn(NID_MEDIA, pBuf.get() != null, GetLastResult(), GetLastResult(),
764 "[%s] Propagated. ", GetErrorMessage(GetLastResult()));
766 r = _MediaUtil::BufferToFile(*pBuf.get(), destImagePath, overwrite);
767 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagated. ", GetErrorMessage(r));
773 _ImageBufferImpl::GetBitmapN(BitmapPixelFormat pixelFormat, BufferScaling bufferScaling) const
775 result r = E_SUCCESS;
776 std::unique_ptr<Bitmap> pOutBitmap;
777 std::unique_ptr<ByteBuffer> pConverted;
778 MediaPixelFormat inPixelFormat = MEDIA_PIXEL_FORMAT_NONE;
780 SysTryReturn(NID_MEDIA, !__isLocked, null, E_INVALID_STATE,
781 "[E_INVALID_STATE] Instance is locked. Unlock to use.");
783 inPixelFormat = _ImageUtilImpl::ToMediaPixelFormat(pixelFormat);
784 SysTryReturn(NID_MEDIA, inPixelFormat != MEDIA_PIXEL_FORMAT_NONE, null, E_INVALID_ARG,
785 "[E_INVALID_ARG] Pixel format is %d, invalid or not supported.", pixelFormat);
787 if (inPixelFormat != __pixelFormat)
789 // Convert raw data to the required pixel format.
790 pConverted.reset(GetByteBufferN(inPixelFormat));
792 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated.", GetErrorMessage(r));
795 pOutBitmap.reset(new (std::nothrow) Bitmap);
796 SysTryReturn(NID_MEDIA, pOutBitmap.get() != null, null, E_OUT_OF_MEMORY,
797 "[E_OUT_OF_MEMORY] Propagated.");
799 if (inPixelFormat != __pixelFormat)
801 r = pOutBitmap->Construct(*pConverted.get(), Dimension(__width, __height), pixelFormat, bufferScaling);
805 std::unique_ptr<ByteBuffer> pTmpByteBuffer (new (std::nothrow) ByteBuffer());
806 r = pTmpByteBuffer->Construct(__pBuffer.get(), 0, __bufSize, __bufSize);
807 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
808 "[%s] Construct instance failed.", GetErrorMessage(r));
810 r = pOutBitmap->Construct(*pTmpByteBuffer.get(), Dimension(__width, __height), pixelFormat, bufferScaling);
813 if ((__transparent == true) && (pixelFormat == BITMAP_PIXEL_FORMAT_RGB565))
818 Color *pMaskingColor = null;
820 if (__pixelFormat == MEDIA_PIXEL_FORMAT_BGRA8888)
822 blue = (__maskColor & 0x000000ff);
823 green = (__maskColor & 0x0000ff00) >> 8;
824 red = (__maskColor & 0x00ff0000) >> 16;
828 red = (__maskColor & 0xf800) >> 8;
829 red = red | (red >> 5);
830 green = (__maskColor & 0x07e0) >> 3;
831 green = green | (green >> 6);
832 blue = (__maskColor & 0x001f) << 3;
833 blue = blue | (blue >> 5);
836 pMaskingColor = new Color(red, green, blue);
837 SysTryReturn(NID_MEDIA, pMaskingColor != null, pOutBitmap.release(), E_SUCCESS,
838 "[%s] Could not apply masking color.", GetErrorMessage(r));
839 r = pOutBitmap->SetMaskingColor(pMaskingColor);
840 SysTryReturn(NID_MEDIA, r == E_SUCCESS, pOutBitmap.release(), E_SUCCESS,
841 "[%s] Could not set masking color.", GetErrorMessage(r));
842 delete pMaskingColor;
845 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated.",
847 return pOutBitmap.release();
851 _ImageBufferImpl::GetBitmapN(BitmapPixelFormat pixelFormat, const FloatDimension &destDim) const
853 result r = E_SUCCESS;
854 std::unique_ptr<Bitmap> pOutBitmap;
855 std::unique_ptr<ByteBuffer> pConverted;
856 MediaPixelFormat inPixelFormat = MEDIA_PIXEL_FORMAT_NONE;
857 Dimension convertDim;
859 SysTryReturn(NID_MEDIA, !__isLocked, null, E_INVALID_STATE,
860 "[E_INVALID_STATE] Instance is locked. Unlock to use.");
862 inPixelFormat = _ImageUtilImpl::ToMediaPixelFormat(pixelFormat);
863 SysTryReturn(NID_MEDIA, (inPixelFormat > MEDIA_PIXEL_FORMAT_NONE) && (inPixelFormat <= MEDIA_PIXEL_FORMAT_GRAY),
864 null, E_INVALID_ARG, "[E_INVALID_ARG] Pixel format is invalid.", inPixelFormat);
866 pOutBitmap.reset(new (std::nothrow) Bitmap);
867 SysTryReturn(NID_MEDIA, pOutBitmap.get() != null, null, E_OUT_OF_MEMORY,
868 "[E_OUT_OF_MEMORY] Propagated.");
870 convertDim.width = _CoordinateSystemUtils::ConvertToInteger(destDim.width);
871 convertDim.height = _CoordinateSystemUtils::ConvertToInteger(destDim.height);
873 std::unique_ptr<ByteBuffer> pTmpByteBuffer (new (std::nothrow) ByteBuffer());
874 r = pTmpByteBuffer->Construct(__pBuffer.get(), 0, __bufSize, __bufSize);
875 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
876 "[%s] Construct instance failed.", GetErrorMessage(r));
878 if (inPixelFormat == __pixelFormat && convertDim.width == __width && convertDim.height == __height)
880 pOutBitmap.reset(_BitmapImpl::GetNonScaledBitmapN(*pTmpByteBuffer.get(), convertDim, pixelFormat, destDim));
884 std::unique_ptr<ByteBuffer> pOutBuf;
887 r = cvt.Construct(__pixelFormat, __width, __height, inPixelFormat, convertDim.width, convertDim.height);
888 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated.", GetErrorMessage(r));
890 pOutBuf.reset(cvt.ConvertN(*pTmpByteBuffer));
892 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated.", GetErrorMessage(r));
894 pOutBitmap.reset(_BitmapImpl::GetNonScaledBitmapN(*pOutBuf.get(), convertDim, pixelFormat, destDim));
897 if ((__transparent == true) && (pixelFormat == BITMAP_PIXEL_FORMAT_RGB565))
902 Color *pMaskingColor = null;
904 if (__pixelFormat == MEDIA_PIXEL_FORMAT_BGRA8888)
906 blue = (__maskColor & 0x000000ff);
907 green = (__maskColor & 0x0000ff00) >> 8;
908 red = (__maskColor & 0x00ff0000) >> 16;
912 red = (__maskColor & 0xf800) >> 8;
913 red = red | (red >> 5);
914 green = (__maskColor & 0x07e0) >> 3;
915 green = green | (green >> 6);
916 blue = (__maskColor & 0x001f) << 3;
917 blue = blue | (blue >> 5);
920 pMaskingColor = new Color(red, green, blue);
921 SysTryReturn(NID_MEDIA, pMaskingColor != null, pOutBitmap.release(), E_SUCCESS,
922 "[%s] Could not apply masking color.", GetErrorMessage(r));
923 r = pOutBitmap->SetMaskingColor(pMaskingColor);
924 SysTryReturn(NID_MEDIA, r == E_SUCCESS, pOutBitmap.release(), E_SUCCESS,
925 "[%s] Could not set masking color.", GetErrorMessage(r));
926 delete pMaskingColor;
929 SysTryReturn(NID_MEDIA, pOutBitmap.get() != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
930 return pOutBitmap.release();
934 _ImageBufferImpl::GetByteBufferN(MediaPixelFormat inPixelFormat) const
936 result r = E_SUCCESS;
937 std::unique_ptr<ByteBuffer> pOutBuf;
939 SysTryReturn(NID_MEDIA, !__isLocked, null, E_INVALID_STATE,
940 "[E_INVALID_STATE] Instance is locked. Unlock to use.");
942 SysTryReturn(NID_MEDIA, (inPixelFormat > MEDIA_PIXEL_FORMAT_NONE) && (inPixelFormat <= MEDIA_PIXEL_FORMAT_YUYV422),
943 null, E_INVALID_ARG, "[E_INVALID_ARG] Pixel format is invalid.", inPixelFormat);
945 SysTryReturn(NID_MEDIA, IsSupportedPixelFormat(inPixelFormat) == true , null, E_UNSUPPORTED_FORMAT,
946 "[E_UNSUPPORTED_FORMAT] The specified pixelFormat is not supported.")
948 std::unique_ptr<ByteBuffer> pTmpByteBuffer (new (std::nothrow) ByteBuffer());
949 r = pTmpByteBuffer->Construct(__pBuffer.get(), 0, __bufSize, __bufSize);
950 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
951 "[%s] Construct instance failed.", GetErrorMessage(r));
953 if (inPixelFormat != __pixelFormat)
955 // Convert raw data to the required pixel format.
958 r = cvt.Construct(__pixelFormat, __width, __height,
959 inPixelFormat, __width, __height);
960 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated.", GetErrorMessage(r));
962 pOutBuf.reset(cvt.ConvertN(*pTmpByteBuffer));
964 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated.", GetErrorMessage(r));
968 pOutBuf.reset(new (std::nothrow) ByteBuffer);
969 SysTryReturn(NID_MEDIA, pOutBuf.get() != null, null, E_OUT_OF_MEMORY,
970 "[E_OUT_OF_MEMORY] Propagated.");
972 r = pOutBuf->Construct(*pTmpByteBuffer);
973 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
974 "[%s] Propagated.", GetErrorMessage(r));
978 return pOutBuf.release();
982 _ImageBufferImpl::CloneN() const
984 std::unique_ptr<ImageBuffer> pImgBuffer;
985 result r = E_SUCCESS;
988 MediaPixelFormat pixFmt = MEDIA_PIXEL_FORMAT_NONE;
989 _ImageBufferImpl* pClonedImpl = null;
991 r = const_cast<_ImageBufferImpl *>(this)->Lock(pData, length, pixFmt);
992 SysTryReturn(NID_MEDIA, r == E_SUCCESS, pImgBuffer.get(), r, "[%s] Propagated.", GetErrorMessage(r));
994 pImgBuffer.reset(new (std::nothrow) ImageBuffer);
995 SysTryReturn(NID_MEDIA, pImgBuffer.get() != null, null, E_OUT_OF_MEMORY,
996 "[E_OUT_OF_MEMORY] Could not create new instance.")
998 r = pImgBuffer->Construct(__width, __height, pixFmt, pData, length);
999 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
1000 "[%s] Failed to construct the cloned ImageBuffer.", GetErrorMessage(r));
1002 pClonedImpl = GetInstance(*pImgBuffer);
1003 SysTryReturn(NID_MEDIA, pClonedImpl != null, null, r,
1004 "[%s] Failed to GetInstance.", GetErrorMessage(r));
1005 pClonedImpl->__maskColor = __maskColor;
1006 pClonedImpl->__transparent = __transparent;
1008 const_cast<_ImageBufferImpl *>(this)->Unlock();
1011 return pImgBuffer.release();
1015 _ImageBufferImpl::ConvertPixelFormatN(MediaPixelFormat pixelFormat) const
1017 std::unique_ptr<ImageBuffer> pImgBuffer;
1018 result r = E_SUCCESS;
1020 std::unique_ptr<byte[]> pDataConvert;
1023 MediaPixelFormat pixFmt = MEDIA_PIXEL_FORMAT_NONE;
1024 _ImageBufferImpl* pConvertedImpl = null;
1026 SysTryReturn(NID_MEDIA, ISSUPPORTED(pixelFormat) == true, pImgBuffer.get(), E_INVALID_ARG,
1027 "[E_INVALID_ARG] Pixel format %d is not supported.", pixelFormat);
1029 if (pixelFormat != __pixelFormat)
1031 _ColorConverter cvt;
1033 r = cvt.Construct(__pixelFormat, __width, __height,
1034 pixelFormat, __width, __height);
1035 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated.", GetErrorMessage(r));
1037 r = const_cast<_ImageBufferImpl *>(this)->Lock(pData, length, pixFmt);
1038 SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
1039 "[%s] Lock failed. Propagated.", GetErrorMessage(r));
1041 pDataConvert.reset(cvt.ConvertN(pData, length, dstLength));
1042 r = GetLastResult();
1043 SysTryCatch(NID_MEDIA, (r == E_SUCCESS) && (pDataConvert.get() != null), , r,
1044 "[%s] Failed to convert data to required pixelformat.", GetErrorMessage(r));
1046 const_cast<_ImageBufferImpl *>(this)->Unlock();
1048 pImgBuffer.reset(new (std::nothrow) ImageBuffer);
1049 SysTryCatch(NID_MEDIA, pImgBuffer.get() != null, , E_OUT_OF_MEMORY,
1050 "[E_OUT_OF_MEMORY] Could not create new instance.")
1052 r = pImgBuffer->Construct(__width, __height, pixelFormat, pDataConvert.get(), dstLength);
1053 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
1054 "[%s] Failed to construct the converted ImageBuffer.", GetErrorMessage(r));
1056 pConvertedImpl = GetInstance(*pImgBuffer);
1057 SysTryReturn(NID_MEDIA, pConvertedImpl != null, null, r,
1058 "[%s] Failed to GetInstance.", GetErrorMessage(r));
1059 pConvertedImpl->__maskColor = __maskColor;
1060 pConvertedImpl->__transparent = __transparent;
1064 pImgBuffer.reset(CloneN());
1068 return pImgBuffer.release();
1071 const_cast<_ImageBufferImpl *>(this)->Unlock();
1077 _ImageBufferImpl::ResizeN(int width, int height) const
1079 std::unique_ptr<ImageBuffer> pImgBuffer;
1080 result r = E_SUCCESS;
1082 std::unique_ptr<byte[]> pDataResized;
1085 MediaPixelFormat pixFmt = MEDIA_PIXEL_FORMAT_NONE;
1086 _ImageBufferImpl* pResizedImpl = null;
1088 SysTryReturn(NID_MEDIA, (width > 0) && (height > 0), pImgBuffer.get(), E_INVALID_ARG,
1089 "[E_INVALID_ARG] Dimensions should be greater than zero: (%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.get(), 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)