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_GifDecoder.cpp
20 // @brief This file has implementatoin details of GifDecoder class .
22 #include <unique_ptr.h>
23 #include <FGrpDimension.h>
24 #include <FGrpBitmap.h>
25 #include <FBaseLong.h>
26 #include <FBaseSysLog.h>
27 #include <FMediaTypes.h>
28 #include "FMedia_MediaUtil.h"
29 #include "FMedia_GifDecoder.h"
30 #include "FMedia_GifDecoderImpl.h"
32 using namespace Tizen::Base;
33 using namespace Tizen::Graphics;
35 namespace Tizen { namespace Media
39 _GifDecoderImpl::_GifDecoderImpl()
43 _GifDecoderImpl::~_GifDecoderImpl()
48 _GifDecoderImpl::Construct(const Tizen::Base::String& filePath, MediaPixelFormat pixelFormat)
51 std::unique_ptr<ByteBuffer> pByteBuffer;
53 SysTryReturnResult(NID_MEDIA, __pGifDecoder.get() == null, E_INVALID_STATE, "Already constructed");
54 __pGifDecoder.reset(new (std::nothrow) _GifDecoder);
55 SysTryReturnResult(NID_MEDIA, __pGifDecoder.get(), E_OUT_OF_MEMORY, "could not create _GifDecoder");
57 SysTryReturnResult(NID_MEDIA, (pixelFormat == MEDIA_PIXEL_FORMAT_BGRA8888 || pixelFormat == MEDIA_PIXEL_FORMAT_RGB565LE),
58 E_INVALID_ARG, "Invalid Pixel Format");
60 pByteBuffer.reset(_MediaUtil::FileToBufferN(filePath, 0));
62 SysTryReturn(NID_MEDIA, pByteBuffer.get() != null, r, r, "FiltToeBufferN:%S", filePath.GetPointer());
63 r = __pGifDecoder->Construct(pByteBuffer->GetPointer(), pByteBuffer->GetLimit(), pixelFormat);
66 SysLogException(NID_MEDIA, r, "_GifDecoder::Construct failed");
72 _GifDecoderImpl::Construct(const Tizen::Base::ByteBuffer& srcBuf, MediaPixelFormat pixelFormat)
75 SysTryReturnResult(NID_MEDIA, __pGifDecoder.get() == null, E_INVALID_STATE, "Already constructed");
76 __pGifDecoder.reset(new (std::nothrow) _GifDecoder);
77 SysTryReturnResult(NID_MEDIA, __pGifDecoder, E_OUT_OF_MEMORY, "could not create _GifDecoder");
79 SysTryReturnResult(NID_MEDIA, (pixelFormat == MEDIA_PIXEL_FORMAT_BGRA8888 || pixelFormat == MEDIA_PIXEL_FORMAT_RGB565LE),
80 E_INVALID_ARG, "Invalid Pixel Format");
81 r = __pGifDecoder->Construct(srcBuf.GetPointer(), srcBuf.GetLimit(), pixelFormat);
84 SysLogException(NID_MEDIA, r, "_GifDecoder::Construct failed");
90 _GifDecoderImpl::GetWidth()const
94 SysTryReturnResult(NID_MEDIA, __pGifDecoder.get(), E_INVALID_STATE, "not constructed");
95 r = __pGifDecoder->GetDimension(dim.width, dim.height);
96 SysTryReturn(NID_MEDIA, r == E_SUCCESS, 0, r, "[%s] propagated", GetErrorMessage(r));
101 _GifDecoderImpl::GetHeight()const
104 result r = E_SUCCESS;
105 SysTryReturnResult(NID_MEDIA, __pGifDecoder.get(), E_INVALID_STATE, "not constructed");
106 r = __pGifDecoder->GetDimension(dim.width, dim.height);
107 SysTryReturn(NID_MEDIA, r == E_SUCCESS, 0, r, "[%s] propagated", GetErrorMessage(r));
111 Tizen::Base::ByteBuffer*
112 _GifDecoderImpl::GetNextFrameN(long& duration)
114 result r = E_SUCCESS;
115 std::unique_ptr<ByteBuffer> pBuf;
116 std::unique_ptr<byte[]> pOutBuf;
120 SysTryReturn(NID_MEDIA, __pGifDecoder.get(), null, E_INVALID_STATE, "not constructed");
121 pOutBuf.reset(__pGifDecoder->DecodeN(outLength));
122 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated.", GetErrorMessage(r));
125 SetLastResult(E_SUCCESS);
128 pBuf.reset(new (std::nothrow) ByteBuffer());
129 SysTryReturn(NID_MEDIA, pBuf.get(), null, GetLastResult(),
130 "new ByteBuffer:%s", GetErrorMessage(GetLastResult()));
131 r = pBuf.get()->Construct(outLength);
132 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
133 "[%s] buf.Construct. %d", GetErrorMessage(r), outLength);
134 r = pBuf.get()->SetArray(pOutBuf.get(), 0, outLength);
135 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ", GetErrorMessage(r));
138 r = __pGifDecoder->GetValue(L"duration", longValue);
139 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
140 "[%s] dec.GetValue(duration)", GetErrorMessage(r));
141 duration = longValue.ToLong();
142 SetLastResult(E_SUCCESS);
143 return pBuf.release();
146 Tizen::Graphics::Bitmap*
147 _GifDecoderImpl::GetNextBitmapN(long& duration, Tizen::Graphics::BufferScaling bufferScaling)
149 std::unique_ptr<Bitmap> pBitmap;
150 std::unique_ptr<ByteBuffer> pByteBuffer;
151 BitmapPixelFormat pixelFormat = BITMAP_PIXEL_FORMAT_MIN;
153 result r = E_SUCCESS;
155 SysTryReturn(NID_MEDIA, __pGifDecoder.get(), null, E_INVALID_STATE, "not constructed");
156 pByteBuffer.reset(GetNextFrameN(duration));
157 SysTryReturn(NID_MEDIA, GetLastResult() == E_SUCCESS, null, GetLastResult(), "GetNextFameN failed ");
159 if (pByteBuffer.get())
161 pBitmap.reset(new (std::nothrow) Bitmap);
162 SysTryReturn(NID_MEDIA, pBitmap.get(), pBitmap.get(), E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] could not allocate memory to bitmap");
163 __pGifDecoder->GetDimension(dim.width, dim.height);
164 pixelFormat = (__pGifDecoder->GetPixelFormat() == MEDIA_PIXEL_FORMAT_RGB565LE)
165 ? BITMAP_PIXEL_FORMAT_RGB565 : BITMAP_PIXEL_FORMAT_ARGB8888;
166 r = pBitmap->Construct(*pByteBuffer.get(), dim, pixelFormat, bufferScaling);
167 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null,r, "pBmp->Construct:%s", GetErrorMessage(r));
169 SetLastResult(E_SUCCESS);
170 return pBitmap.release();