Fixed memory leak in ImageBuffer::ResizeN()
[platform/framework/native/image.git] / src / FMedia_ImageBufferImpl.cpp
index 6746310..49f6f1d 100644 (file)
@@ -48,10 +48,6 @@ using namespace Tizen::Ui;
 namespace Tizen { namespace Media
 {
 
-#define ISSUPPORTED(x)                                                                                                                 \
-       ((x == MEDIA_PIXEL_FORMAT_YUV420P) || (x == MEDIA_PIXEL_FORMAT_BGRA8888) ||     \
-       (x == MEDIA_PIXEL_FORMAT_RGB565LE) || (x == MEDIA_PIXEL_FORMAT_GRAY))
-
 typedef struct {
        ImageRotationType rotateType;
        ImageFlipType flipType;
@@ -86,7 +82,10 @@ static const MediaPixelFormat _IMAGE_BUFFER_PIXEL_FORMATS[] =
 {
        MEDIA_PIXEL_FORMAT_RGB565LE,
        MEDIA_PIXEL_FORMAT_BGRA8888,
-       MEDIA_PIXEL_FORMAT_YUV420P
+       MEDIA_PIXEL_FORMAT_YUV420P,
+       MEDIA_PIXEL_FORMAT_NV12,
+       MEDIA_PIXEL_FORMAT_NV21,
+       MEDIA_PIXEL_FORMAT_GRAY
 };
 
 _ImageBufferImpl::_ImageBufferImpl(void)
@@ -145,9 +144,6 @@ _ImageBufferImpl::Construct(int width, int height, MediaPixelFormat pixelFormat,
        SysTryReturn(NID_MEDIA, (width > 0) && (height > 0), E_INVALID_ARG, E_INVALID_ARG,
                "[E_INVALID_ARG] Dimensions should be greater than zero : (%d x %d).", width, height);
 
-       SysTryReturn(NID_MEDIA, ISSUPPORTED(pixelFormat) == true, E_INVALID_ARG, E_INVALID_ARG,
-               "[E_INVALID_ARG] Pixelformat is not supported : %d.", pixelFormat);
-
        reqBufferSize = _ImageUtil::GetBufferSize(pixelFormat, width, height);
        SysTryReturn(NID_MEDIA, reqBufferSize > 0, GetLastResult(), GetLastResult(),
                        "[%s] Could not get output buffer size.", GetErrorMessage(GetLastResult()));
@@ -166,13 +162,17 @@ _ImageBufferImpl::Construct(int width, int height, MediaPixelFormat pixelFormat,
        __pBuffer.reset(new (std::nothrow) byte[reqBufferSize]);
        SysTryReturn(NID_MEDIA, __pBuffer.get() != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
                "[E_OUT_OF_MEMORY] Construct instance failed.");
-       memset(__pBuffer.get(), 0, reqBufferSize);
+
        __bufSize = reqBufferSize;
 
        if (pData != null)
        {
                memcpy(__pBuffer.get(), pData, reqBufferSize);
        }
+       else
+       {
+               memset(__pBuffer.get(), 0, reqBufferSize);
+       }
        return r;
 }
 
@@ -837,9 +837,9 @@ _ImageBufferImpl::GetBitmapN(BitmapPixelFormat pixelFormat, BufferScaling buffer
                SysTryReturn(NID_MEDIA, pMaskingColor != null, pOutBitmap.release(), E_SUCCESS,
                        "[%s] Could not apply masking color.", GetErrorMessage(r));
                r = pOutBitmap->SetMaskingColor(pMaskingColor);
+               delete pMaskingColor;
                SysTryReturn(NID_MEDIA, r == E_SUCCESS, pOutBitmap.release(), E_SUCCESS,
                        "[%s] Could not set masking color.", GetErrorMessage(r));
-               delete pMaskingColor;
        }
 
        SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated.",
@@ -921,9 +921,9 @@ _ImageBufferImpl::GetBitmapN(BitmapPixelFormat pixelFormat, const FloatDimension
                SysTryReturn(NID_MEDIA, pMaskingColor != null, pOutBitmap.release(), E_SUCCESS,
                        "[%s] Could not apply masking color.", GetErrorMessage(r));
                r = pOutBitmap->SetMaskingColor(pMaskingColor);
+               delete pMaskingColor;
                SysTryReturn(NID_MEDIA, r == E_SUCCESS, pOutBitmap.release(), E_SUCCESS,
                        "[%s] Could not set masking color.", GetErrorMessage(r));
-               delete pMaskingColor;
        }
 
        SysTryReturn(NID_MEDIA, pOutBitmap.get() != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
@@ -939,9 +939,6 @@ _ImageBufferImpl::GetByteBufferN(MediaPixelFormat inPixelFormat) const
        SysTryReturn(NID_MEDIA, !__isLocked, null, E_INVALID_STATE,
                "[E_INVALID_STATE] Instance is locked. Unlock to use.");
 
-       SysTryReturn(NID_MEDIA, (inPixelFormat > MEDIA_PIXEL_FORMAT_NONE) && (inPixelFormat <= MEDIA_PIXEL_FORMAT_YUYV422),
-               null, E_INVALID_ARG, "[E_INVALID_ARG] Pixel format is invalid.", inPixelFormat);
-
        SysTryReturn(NID_MEDIA, IsSupportedPixelFormat(inPixelFormat) == true , null, E_UNSUPPORTED_FORMAT,
                "[E_UNSUPPORTED_FORMAT] The specified pixelFormat is not supported.")
 
@@ -1023,8 +1020,11 @@ _ImageBufferImpl::ConvertPixelFormatN(MediaPixelFormat pixelFormat) const
        MediaPixelFormat pixFmt = MEDIA_PIXEL_FORMAT_NONE;
        _ImageBufferImpl* pConvertedImpl = null;
 
-       SysTryReturn(NID_MEDIA, ISSUPPORTED(pixelFormat) == true, pImgBuffer.get(), E_INVALID_ARG,
-               "[E_INVALID_ARG] Pixel format %d is not supported.", pixelFormat);
+       SysTryReturn(NID_MEDIA, IsSupportedPixelFormat(pixelFormat) == true , null, E_UNSUPPORTED_FORMAT,
+               "[E_UNSUPPORTED_FORMAT] The specified pixelFormat is not supported.")
+
+       SysTryReturn(NID_MEDIA, _ImageUtil::IsValidDimension(pixelFormat, __width, __height) == true, null, E_INVALID_ARG,
+               "[E_INVALID_ARG] Dimensions should be even for this pixel format: (%d x %d)", __width, __height);
 
        if (pixelFormat != __pixelFormat)
        {
@@ -1058,6 +1058,7 @@ _ImageBufferImpl::ConvertPixelFormatN(MediaPixelFormat pixelFormat) const
                        "[%s] Failed to GetInstance.", GetErrorMessage(r));
                pConvertedImpl->__maskColor = __maskColor;
                pConvertedImpl->__transparent = __transparent;
+
        }
        else
        {
@@ -1085,9 +1086,12 @@ _ImageBufferImpl::ResizeN(int width, int height) const
        MediaPixelFormat pixFmt = MEDIA_PIXEL_FORMAT_NONE;
        _ImageBufferImpl* pResizedImpl = null;
 
-       SysTryReturn(NID_MEDIA, (width > 0) && (height > 0), pImgBuffer.get(), E_INVALID_ARG,
+       SysTryReturn(NID_MEDIA, (width > 0) && (height > 0), null, E_INVALID_ARG,
                "[E_INVALID_ARG] Dimensions should be greater than zero: (%d x %d)", width, height);
 
+       SysTryReturn(NID_MEDIA, _ImageUtil::IsValidDimension(__pixelFormat, width, height) == true, null, E_INVALID_ARG,
+               "[E_INVALID_ARG] Dimensions should be even for this pixel format: (%d x %d)", width, height);
+
        if ((width != __width) && (height != __height))
        {
                dstLength = _ImageUtil::GetBufferSize(__pixelFormat, width, height);