2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/imaging/common/webp-loading.h>
22 #include <webp/decode.h>
23 #include <webp/demux.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/public-api/images/pixel-data.h>
27 #include <sys/types.h>
32 #include <dali/internal/imaging/common/file-download.h>
33 #include <dali/internal/system/common/file-reader.h>
35 typedef unsigned char WebPByteType;
49 #if defined(DEBUG_ENABLED)
50 Debug::Filter *gWebPLoadingLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_GIF_LOADING" );
53 constexpr size_t MAXIMUM_DOWNLOAD_IMAGE_SIZE = 50 * 1024 * 1024;
57 struct WebPLoading::Impl
60 Impl( const std::string& url, bool isLocalResource )
64 #if WEBP_DEMUX_ABI_VERSION > 0x0101
65 if( ReadWebPInformation( isLocalResource ) )
67 WebPAnimDecoderOptions webPAnimDecoderOptions;
68 WebPAnimDecoderOptionsInit( &webPAnimDecoderOptions );
69 webPAnimDecoderOptions.color_mode = MODE_RGBA;
70 mWebPAnimDecoder = WebPAnimDecoderNew( &mWebPData, &webPAnimDecoderOptions );
71 WebPAnimDecoderGetInfo( mWebPAnimDecoder, &mWebPAnimInfo );
72 mTimeStamp.assign( mWebPAnimInfo.frame_count, 0 );
77 bool ReadWebPInformation( bool isLocalResource )
79 #if WEBP_DEMUX_ABI_VERSION > 0x0101
80 WebPDataInit( &mWebPData );
83 Internal::Platform::FileReader fileReader( mUrl );
84 FILE *fp = fileReader.GetFile();
90 if( fseek( fp, 0, SEEK_END ) <= -1 )
95 mWebPData.size = ftell( fp );
96 if( ( ! fseek( fp, 0, SEEK_SET ) ) )
98 unsigned char *WebPDataBuffer;
99 WebPDataBuffer = reinterpret_cast<WebPByteType*>( malloc(sizeof( WebPByteType ) * mWebPData.size ) );
100 mWebPData.size = fread( WebPDataBuffer, sizeof( WebPByteType ), mWebPData.size, fp );
101 mWebPData.bytes = WebPDataBuffer;
112 Dali::Vector<uint8_t> dataBuffer;
115 succeeded = TizenPlatform::Network::DownloadRemoteFileIntoMemory( mUrl, dataBuffer, dataSize, MAXIMUM_DOWNLOAD_IMAGE_SIZE );
118 size_t blobSize = dataBuffer.Size();
121 // Open a file handle on the memory buffer:
122 Dali::Internal::Platform::FileReader fileReader( dataBuffer, blobSize );
123 FILE * const fp = fileReader.GetFile();
126 if( ( ! fseek( fp, 0, SEEK_SET ) ) )
128 unsigned char *WebPDataBuffer;
129 WebPDataBuffer = reinterpret_cast<WebPByteType*>( malloc(sizeof( WebPByteType ) * blobSize ) );
130 mWebPData.size = fread( WebPDataBuffer, sizeof( WebPByteType ), mWebPData.size, fp );
131 mWebPData.bytes = WebPDataBuffer;
135 DALI_LOG_ERROR( "Error seeking within file\n" );
140 DALI_LOG_ERROR( "Error reading file\n" );
151 // Moveable but not copyable
153 Impl( const Impl& ) = delete;
154 Impl& operator=( const Impl& ) = delete;
155 Impl( Impl&& ) = default;
156 Impl& operator=( Impl&& ) = default;
160 #if WEBP_DEMUX_ABI_VERSION > 0x0101
161 if( &mWebPData != NULL )
163 free( (void*)mWebPData.bytes );
164 mWebPData.bytes = nullptr;
165 WebPDataInit( &mWebPData );
167 if( mWebPAnimDecoder )
169 WebPAnimDecoderDelete(mWebPAnimDecoder);
175 std::vector<uint32_t> mTimeStamp;
176 uint32_t mLoadingFrame;
178 #if WEBP_DEMUX_ABI_VERSION > 0x0101
180 WebPAnimDecoder* mWebPAnimDecoder;
181 WebPAnimInfo mWebPAnimInfo;
185 AnimatedImageLoadingPtr WebPLoading::New( const std::string &url, bool isLocalResource )
187 #if WEBP_DEMUX_ABI_VERSION <= 0x0101
188 DALI_LOG_ERROR( "The system do not support Animated WebP format.\n" );
190 return AnimatedImageLoadingPtr( new WebPLoading( url, isLocalResource ) );
193 WebPLoading::WebPLoading( const std::string &url, bool isLocalResource )
194 : mImpl( new WebPLoading::Impl( url, isLocalResource ) )
198 WebPLoading::~WebPLoading()
203 bool WebPLoading::LoadNextNFrames( uint32_t frameStartIndex, int count, std::vector<Dali::PixelData> &pixelData )
205 #if WEBP_DEMUX_ABI_VERSION > 0x0101
206 if( frameStartIndex >= mImpl->mWebPAnimInfo.frame_count )
211 DALI_LOG_INFO( gWebPLoadingLogFilter, Debug::Concise, "LoadNextNFrames( frameStartIndex:%d, count:%d )\n", frameStartIndex, count );
213 if( mImpl->mLoadingFrame > frameStartIndex )
215 mImpl->mLoadingFrame = 0;
216 WebPAnimDecoderReset( mImpl->mWebPAnimDecoder );
219 for( ; mImpl->mLoadingFrame < frameStartIndex ; ++mImpl->mLoadingFrame )
221 uint8_t* frameBuffer;
223 WebPAnimDecoderGetNext( mImpl->mWebPAnimDecoder, &frameBuffer, ×tamp );
224 mImpl->mTimeStamp[mImpl->mLoadingFrame] = timestamp;
227 for( int i = 0; i < count; ++i )
229 const int bufferSize = mImpl->mWebPAnimInfo.canvas_width * mImpl->mWebPAnimInfo.canvas_height * sizeof( uint32_t );
230 uint8_t* frameBuffer;
232 WebPAnimDecoderGetNext( mImpl->mWebPAnimDecoder, &frameBuffer, ×tamp );
234 auto pixelBuffer = new uint8_t[ bufferSize ];
235 memcpy( pixelBuffer, frameBuffer, bufferSize );
236 mImpl->mTimeStamp[mImpl->mLoadingFrame] = timestamp;
240 pixelData.push_back( Dali::PixelData::New( pixelBuffer, bufferSize,
241 mImpl->mWebPAnimInfo.canvas_width, mImpl->mWebPAnimInfo.canvas_height,
242 Dali::Pixel::RGBA8888, Dali::PixelData::DELETE_ARRAY) );
245 mImpl->mLoadingFrame++;
246 if( mImpl->mLoadingFrame >= mImpl->mWebPAnimInfo.frame_count )
248 mImpl->mLoadingFrame = 0;
249 WebPAnimDecoderReset( mImpl->mWebPAnimDecoder );
259 ImageDimensions WebPLoading::GetImageSize() const
261 #if WEBP_DEMUX_ABI_VERSION > 0x0101
262 return ImageDimensions( mImpl->mWebPAnimInfo.canvas_width, mImpl->mWebPAnimInfo.canvas_height );
264 return ImageDimensions();
268 uint32_t WebPLoading::GetImageCount() const
270 #if WEBP_DEMUX_ABI_VERSION > 0x0101
271 return mImpl->mWebPAnimInfo.frame_count;
277 uint32_t WebPLoading::GetFrameInterval( uint32_t frameIndex ) const
279 if( frameIndex >= GetImageCount() )
287 return mImpl->mTimeStamp[frameIndex] - mImpl->mTimeStamp[frameIndex - 1];
289 return mImpl->mTimeStamp[frameIndex];
293 } // namespace Adaptor
295 } // namespace Internal