Fixed memory leak in ImageBuffer::ResizeN()
[platform/framework/native/image.git] / src / FMedia_ImageBufferImpl.cpp
index 38f3016..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)
@@ -96,6 +95,8 @@ _ImageBufferImpl::_ImageBufferImpl(void)
        , __orientation(EXIF_ORIENTATION_TOP_LEFT)
        , __pixelFormat(MEDIA_PIXEL_FORMAT_NONE)
        , __isLocked(false)
+       , __transparent(false)
+       , __maskColor(0xf81f) // MAGENTA
 {
 }
 
@@ -143,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()));
@@ -164,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;
 }
 
@@ -197,6 +199,8 @@ _ImageBufferImpl::Construct(const Tizen::Base::ByteBuffer &srcImageBuf, const Ti
        std::unique_ptr<byte[]> pDstBuf;
        Tizen::Graphics::Rectangle transformRegion;
        int orientationInfo = 0;
+       Boolean transparency(false);
+       Integer chromaKey;
 
        r = dec.Construct(srcImageBuf);
        SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Decoder construct failed.", GetErrorMessage(r));
@@ -306,6 +310,29 @@ _ImageBufferImpl::Construct(const Tizen::Base::ByteBuffer &srcImageBuf, const Ti
                        __pBuffer.reset(dec.DecodeN(__bufSize));
                        SysTryReturn(NID_MEDIA, __pBuffer.get() != null, r, r,
                                "[%s] DecodeN returned empty buffer.", GetErrorMessage(r));
+                       r = dec.GetValue("transparency", transparency);
+                       if (r != E_SUCCESS)
+                       {
+                               __transparent = false;
+                               r = E_SUCCESS;
+                       }
+                       else
+                       {
+                               __transparent = transparency.ToBool();
+                               if (__transparent == true)
+                               {
+                                       r = dec.GetValue(L"chromaKey", chromaKey);
+                                       if (r != E_SUCCESS)
+                                       {
+                                               __transparent = false;
+                                               r = E_SUCCESS;
+                                       }
+                                       else
+                                       {
+                                               __maskColor = chromaKey.ToInt();
+                                       }
+                               }
+                       }
                }
                else if (r == E_UNSUPPORTED_OPERATION)
                {
@@ -329,6 +356,30 @@ _ImageBufferImpl::Construct(const Tizen::Base::ByteBuffer &srcImageBuf, const Ti
                        __bufSize = _ImageUtil::GetBufferSize(__pixelFormat, pDecodingRegion->width, pDecodingRegion->height);
                        SysTryReturn(NID_MEDIA, __bufSize > 0, GetLastResult(), GetLastResult(),
                        "[%s] Could not get output buffer size.", GetErrorMessage(GetLastResult()));
+
+                       r = dec.GetValue("transparency", transparency);
+                       if (r != E_SUCCESS)
+                       {
+                               __transparent = false;
+                               r = E_SUCCESS;
+                       }
+                       else
+                       {
+                               __transparent = transparency.ToBool();
+                               if (__transparent == true)
+                               {
+                                       r = dec.GetValue(L"chromaKey", chromaKey);
+                                       if (r != E_SUCCESS)
+                                       {
+                                               __transparent = false;
+                                               r = E_SUCCESS;
+                                       }
+                                       else
+                                       {
+                                               __maskColor = chromaKey.ToInt();
+                                       }
+                               }
+                       }
                }
        }
        else
@@ -342,6 +393,30 @@ _ImageBufferImpl::Construct(const Tizen::Base::ByteBuffer &srcImageBuf, const Ti
                SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] DecodeN failed.", GetErrorMessage(r));
                SysTryReturn(NID_MEDIA, __pBuffer.get() != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
                        "[E_OUT_OF_MEMORY] Propagated.");
+
+               r = dec.GetValue("transparency", transparency);
+               if (r != E_SUCCESS)
+               {
+                       __transparent = false;
+                       r = E_SUCCESS;
+               }
+               else
+               {
+                       __transparent = transparency.ToBool();
+                       if (__transparent == true)
+                       {
+                               r = dec.GetValue(L"chromaKey", chromaKey);
+                               if (r != E_SUCCESS)
+                               {
+                                       __transparent = false;
+                                       r = E_SUCCESS;
+                               }
+                               else
+                               {
+                                       __maskColor = chromaKey.ToInt();
+                               }
+                       }
+               }
        }
 
        if (autoRotate == true)
@@ -452,6 +527,8 @@ _ImageBufferImpl::Construct(const Tizen::Base::ByteBuffer &srcImageBuf, int dest
        _ImageDecoder dec;
        std::unique_ptr<byte[]> pDstBuf;
        Tizen::Graphics::Rectangle transformRegion;
+       Boolean transparency(false);
+       Integer chromaKey;
 
        r = dec.Construct(srcImageBuf);
        SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Decoder construct failed.", GetErrorMessage(r));
@@ -478,6 +555,30 @@ _ImageBufferImpl::Construct(const Tizen::Base::ByteBuffer &srcImageBuf, int dest
        SysTryReturn(NID_MEDIA, __pBuffer.get() != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
                "[E_OUT_OF_MEMORY] Propagated.");
 
+       r = dec.GetValue("transparency", transparency);
+       if (r != E_SUCCESS)
+       {
+               __transparent = false;
+               r = E_SUCCESS;
+       }
+       else
+       {
+               __transparent = transparency.ToBool();
+               if (__transparent == true)
+               {
+                       r = dec.GetValue(L"chromaKey", chromaKey);
+                       if (r != E_SUCCESS)
+                       {
+                               __transparent = false;
+                               r = E_SUCCESS;
+                       }
+                       else
+                       {
+                               __maskColor = chromaKey.ToInt();
+                       }
+               }
+       }
+
        r = dec.GetDimension(__width, __height);
        SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] GetDimension failed.",
                GetErrorMessage(r));
@@ -709,6 +810,38 @@ _ImageBufferImpl::GetBitmapN(BitmapPixelFormat pixelFormat, BufferScaling buffer
                r = pOutBitmap->Construct(*pTmpByteBuffer.get(), Dimension(__width, __height), pixelFormat, bufferScaling);
        }
 
+       if ((__transparent == true) && (pixelFormat == BITMAP_PIXEL_FORMAT_RGB565))
+       {
+               byte red = 0;
+               byte green = 0;
+               byte blue = 0;
+               Color *pMaskingColor = null;
+
+               if (__pixelFormat == MEDIA_PIXEL_FORMAT_BGRA8888)
+               {
+                       blue = (__maskColor & 0x000000ff);
+                       green = (__maskColor & 0x0000ff00) >> 8;
+                       red = (__maskColor & 0x00ff0000) >> 16;
+               }
+               else
+               {
+                       red = (__maskColor & 0xf800) >> 8;
+                       red = red | (red >> 5);
+                       green = (__maskColor & 0x07e0) >> 3;
+                       green = green | (green >> 6);
+                       blue = (__maskColor & 0x001f) << 3;
+                       blue = blue | (blue >> 5);
+               }
+
+               pMaskingColor = new Color(red, green, blue);
+               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));
+       }
+
        SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated.",
                GetErrorMessage(r));
        return pOutBitmap.release();
@@ -761,6 +894,38 @@ _ImageBufferImpl::GetBitmapN(BitmapPixelFormat pixelFormat, const FloatDimension
                pOutBitmap.reset(_BitmapImpl::GetNonScaledBitmapN(*pOutBuf.get(), convertDim, pixelFormat, destDim));
        }
 
+       if ((__transparent == true) && (pixelFormat == BITMAP_PIXEL_FORMAT_RGB565))
+       {
+               byte red = 0;
+               byte green = 0;
+               byte blue = 0;
+               Color *pMaskingColor = null;
+
+               if (__pixelFormat == MEDIA_PIXEL_FORMAT_BGRA8888)
+               {
+                       blue = (__maskColor & 0x000000ff);
+                       green = (__maskColor & 0x0000ff00) >> 8;
+                       red = (__maskColor & 0x00ff0000) >> 16;
+               }
+               else
+               {
+                       red = (__maskColor & 0xf800) >> 8;
+                       red = red | (red >> 5);
+                       green = (__maskColor & 0x07e0) >> 3;
+                       green = green | (green >> 6);
+                       blue = (__maskColor & 0x001f) << 3;
+                       blue = blue | (blue >> 5);
+               }
+
+               pMaskingColor = new Color(red, green, blue);
+               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));
+       }
+
        SysTryReturn(NID_MEDIA, pOutBitmap.get() != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
        return pOutBitmap.release();
 }
@@ -774,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.")
 
@@ -821,6 +983,7 @@ _ImageBufferImpl::CloneN() const
        byte* pData = null;
        int length = 0;
        MediaPixelFormat pixFmt = MEDIA_PIXEL_FORMAT_NONE;
+       _ImageBufferImpl* pClonedImpl = null;
 
        r = const_cast<_ImageBufferImpl *>(this)->Lock(pData, length, pixFmt);
        SysTryReturn(NID_MEDIA, r == E_SUCCESS, pImgBuffer.get(), r, "[%s] Propagated.", GetErrorMessage(r));
@@ -833,6 +996,12 @@ _ImageBufferImpl::CloneN() const
        SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
                "[%s] Failed to construct the cloned ImageBuffer.", GetErrorMessage(r));
 
+       pClonedImpl = GetInstance(*pImgBuffer);
+       SysTryReturn(NID_MEDIA, pClonedImpl != null, null, r,
+               "[%s] Failed to GetInstance.", GetErrorMessage(r));
+       pClonedImpl->__maskColor = __maskColor;
+       pClonedImpl->__transparent = __transparent;
+
        const_cast<_ImageBufferImpl *>(this)->Unlock();
 
        SetLastResult(r);
@@ -849,9 +1018,13 @@ _ImageBufferImpl::ConvertPixelFormatN(MediaPixelFormat pixelFormat) const
        int length = 0;
        int dstLength = 0;
        MediaPixelFormat pixFmt = MEDIA_PIXEL_FORMAT_NONE;
+       _ImageBufferImpl* pConvertedImpl = null;
+
+       SysTryReturn(NID_MEDIA, IsSupportedPixelFormat(pixelFormat) == true , null, E_UNSUPPORTED_FORMAT,
+               "[E_UNSUPPORTED_FORMAT] The specified pixelFormat is not supported.")
 
-       SysTryReturn(NID_MEDIA, ISSUPPORTED(pixelFormat) == true, pImgBuffer.get(), E_INVALID_ARG,
-               "[E_INVALID_ARG] Pixel format %d is not supported.", pixelFormat);
+       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)
        {
@@ -879,6 +1052,13 @@ _ImageBufferImpl::ConvertPixelFormatN(MediaPixelFormat pixelFormat) const
                r = pImgBuffer->Construct(__width, __height, pixelFormat, pDataConvert.get(), dstLength);
                SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
                        "[%s] Failed to construct the converted ImageBuffer.", GetErrorMessage(r));
+
+               pConvertedImpl = GetInstance(*pImgBuffer);
+               SysTryReturn(NID_MEDIA, pConvertedImpl != null, null, r,
+                       "[%s] Failed to GetInstance.", GetErrorMessage(r));
+               pConvertedImpl->__maskColor = __maskColor;
+               pConvertedImpl->__transparent = __transparent;
+
        }
        else
        {
@@ -904,10 +1084,14 @@ _ImageBufferImpl::ResizeN(int width, int height) const
        int length = 0;
        int dstLength = 0;
        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);
@@ -937,6 +1121,12 @@ _ImageBufferImpl::ResizeN(int width, int height) const
                r = pImgBuffer->Construct(width, height, __pixelFormat, pDataResized.get(), dstLength);
                SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
                        "[%s] Failed to construct the resized ImageBuffer.", GetErrorMessage(r));
+
+               pResizedImpl = GetInstance(*pImgBuffer);
+               SysTryReturn(NID_MEDIA, pResizedImpl != null, null, r,
+                       "[%s] Failed to GetInstance.", GetErrorMessage(r));
+               pResizedImpl->__maskColor = __maskColor;
+               pResizedImpl->__transparent = __transparent;
        }
        else
        {
@@ -962,6 +1152,7 @@ _ImageBufferImpl::FlipN(ImageFlipType flipType) const
        std::unique_ptr<byte[]> pDataFlip;
        int length = 0;
        MediaPixelFormat pixFmt = MEDIA_PIXEL_FORMAT_NONE;
+       _ImageBufferImpl* pFlippedImpl = null;
 
        SysTryReturn(NID_MEDIA, (flipType >= IMAGE_FLIP_NONE) && (flipType <= IMAGE_FLIP_VERTICAL),
                pImgBuffer.get(), E_INVALID_ARG, "[E_INVALID_ARG] Flip type is not valid: %d.", flipType);
@@ -989,6 +1180,12 @@ _ImageBufferImpl::FlipN(ImageFlipType flipType) const
                r = pImgBuffer->Construct(__width, __height, __pixelFormat, pDataFlip.get(), length);
                SysTryCatch(NID_MEDIA, r == E_SUCCESS, , r,
                        "[%s] Failed to construct the fliped ImageBuffer.", GetErrorMessage(r));
+
+               pFlippedImpl = GetInstance(*pImgBuffer);
+               SysTryReturn(NID_MEDIA, pFlippedImpl != null, null, r,
+                       "[%s] Failed to GetInstance.", GetErrorMessage(r));
+               pFlippedImpl->__maskColor = __maskColor;
+               pFlippedImpl->__transparent = __transparent;
        }
        else
        {
@@ -1017,6 +1214,7 @@ _ImageBufferImpl::RotateN(ImageRotationType rotationType) const
        int rotatedWidth= 0;
        int rotatedHeight = 0;
        MediaPixelFormat pixFmt = MEDIA_PIXEL_FORMAT_NONE;
+       _ImageBufferImpl* pRotatedImpl = null;
 
        SysTryReturn(NID_MEDIA, (rotationType >= IMAGE_ROTATION_0) && (rotationType <= IMAGE_ROTATION_270),
                pImgBuffer.get(), E_INVALID_ARG, "[E_INVALID_ARG] Rotation type is not valid: %d.", rotationType);
@@ -1045,6 +1243,12 @@ _ImageBufferImpl::RotateN(ImageRotationType rotationType) const
                r = pImgBuffer->Construct(rotatedWidth, rotatedHeight, __pixelFormat, pDataRotate.get(), length);
                SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
                        "[%s] Failed to construct the rotated ImageBuffer.", GetErrorMessage(r));
+
+               pRotatedImpl = GetInstance(*pImgBuffer);
+               SysTryReturn(NID_MEDIA, pRotatedImpl != null, null, r,
+                       "[%s] Failed to GetInstance.", GetErrorMessage(r));
+               pRotatedImpl->__maskColor = __maskColor;
+               pRotatedImpl->__transparent = __transparent;
        }
        else
        {
@@ -1072,6 +1276,7 @@ _ImageBufferImpl::CropN(int x, int y, int width, int height) const
        int length = 0;
        int dstLength = 0;
        MediaPixelFormat pixFmt = MEDIA_PIXEL_FORMAT_NONE;
+       _ImageBufferImpl *pCroppedImpl = null;
 
        SysTryReturn(NID_MEDIA, (x >= 0) && (y >= 0) && (x <= __width) && (y <= __height),
                pImgBuffer.get(), E_INVALID_ARG,
@@ -1121,6 +1326,12 @@ _ImageBufferImpl::CropN(int x, int y, int width, int height) const
                r = pImgBuffer->Construct(width, height, __pixelFormat, pDataCropped.get(), dstLength);
                SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
                        "[%s] Failed to construct the croped ImageBuffer.", GetErrorMessage(r));
+
+               pCroppedImpl = GetInstance(*pImgBuffer);
+               SysTryReturn(NID_MEDIA, pCroppedImpl != null, null, r,
+                       "[%s] Failed to GetInstance.", GetErrorMessage(r));
+               pCroppedImpl->__maskColor = __maskColor;
+               pCroppedImpl->__transparent = __transparent;
        }
        else
        {
@@ -1172,7 +1383,7 @@ _ImageBufferImpl::GetImageInfo(const ByteBuffer& srcImageBuf, ImageFormat& image
 bool
 _ImageBufferImpl::IsSupportedPixelFormat(MediaPixelFormat pixelFormat)
 {
-       for (int i = 0; i < sizeof(_IMAGE_BUFFER_PIXEL_FORMATS)/sizeof(_IMAGE_BUFFER_PIXEL_FORMATS[0]) ; i++)
+       for (unsigned int i = 0; i < sizeof(_IMAGE_BUFFER_PIXEL_FORMATS)/sizeof(_IMAGE_BUFFER_PIXEL_FORMATS[0]) ; i++)
        {
                if(pixelFormat == _IMAGE_BUFFER_PIXEL_FORMATS[i])
                {
@@ -1191,7 +1402,7 @@ _ImageBufferImpl::GetSupportedPixelFormatListN()
                "[E_OUT_OF_MEMORY] Could not allocate memory for output.");
 
        // TODO update list later for supported color formats in color converter?.
-       for (int i=0; i<sizeof(_IMAGE_BUFFER_PIXEL_FORMATS)/sizeof(_IMAGE_BUFFER_PIXEL_FORMATS[0]); i++)
+       for (unsigned int i=0; i<sizeof(_IMAGE_BUFFER_PIXEL_FORMATS)/sizeof(_IMAGE_BUFFER_PIXEL_FORMATS[0]); i++)
        {
                r = pList->Add(_IMAGE_BUFFER_PIXEL_FORMATS[i]);
                SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,