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 #ifdef DALI_WEBP_AVAILABLE
23 #include <webp/decode.h>
24 #include <webp/demux.h>
26 #if WEBP_DEMUX_ABI_VERSION > 0x0101
27 #define DALI_WEBP_ENABLED 1
31 #include <dali/integration-api/debug.h>
32 #include <dali/public-api/images/pixel-data.h>
34 #include <sys/types.h>
39 #include <dali/internal/imaging/common/file-download.h>
40 #include <dali/internal/system/common/file-reader.h>
42 typedef unsigned char WebPByteType;
56 #if defined(DEBUG_ENABLED)
57 Debug::Filter *gWebPLoadingLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_GIF_LOADING" );
60 constexpr size_t MAXIMUM_DOWNLOAD_IMAGE_SIZE = 50 * 1024 * 1024;
64 struct WebPLoading::Impl
67 Impl( const std::string& url, bool isLocalResource )
71 #ifdef DALI_WEBP_ENABLED
72 if( ReadWebPInformation( isLocalResource ) )
74 WebPAnimDecoderOptions webPAnimDecoderOptions;
75 WebPAnimDecoderOptionsInit( &webPAnimDecoderOptions );
76 webPAnimDecoderOptions.color_mode = MODE_RGBA;
77 mWebPAnimDecoder = WebPAnimDecoderNew( &mWebPData, &webPAnimDecoderOptions );
78 WebPAnimDecoderGetInfo( mWebPAnimDecoder, &mWebPAnimInfo );
79 mTimeStamp.assign( mWebPAnimInfo.frame_count, 0 );
84 bool ReadWebPInformation( bool isLocalResource )
86 #ifdef DALI_WEBP_ENABLED
87 WebPDataInit( &mWebPData );
90 Internal::Platform::FileReader fileReader( mUrl );
91 FILE *fp = fileReader.GetFile();
97 if( fseek( fp, 0, SEEK_END ) <= -1 )
102 mWebPData.size = ftell( fp );
103 if( ( ! fseek( fp, 0, SEEK_SET ) ) )
105 unsigned char *WebPDataBuffer;
106 WebPDataBuffer = reinterpret_cast<WebPByteType*>( malloc(sizeof( WebPByteType ) * mWebPData.size ) );
107 mWebPData.size = fread( WebPDataBuffer, sizeof( WebPByteType ), mWebPData.size, fp );
108 mWebPData.bytes = WebPDataBuffer;
119 Dali::Vector<uint8_t> dataBuffer;
122 succeeded = TizenPlatform::Network::DownloadRemoteFileIntoMemory( mUrl, dataBuffer, dataSize, MAXIMUM_DOWNLOAD_IMAGE_SIZE );
125 size_t blobSize = dataBuffer.Size();
128 // Open a file handle on the memory buffer:
129 Dali::Internal::Platform::FileReader fileReader( dataBuffer, blobSize );
130 FILE * const fp = fileReader.GetFile();
133 if( ( ! fseek( fp, 0, SEEK_SET ) ) )
135 unsigned char *WebPDataBuffer;
136 WebPDataBuffer = reinterpret_cast<WebPByteType*>( malloc(sizeof( WebPByteType ) * blobSize ) );
137 mWebPData.size = fread( WebPDataBuffer, sizeof( WebPByteType ), mWebPData.size, fp );
138 mWebPData.bytes = WebPDataBuffer;
142 DALI_LOG_ERROR( "Error seeking within file\n" );
147 DALI_LOG_ERROR( "Error reading file\n" );
158 // Moveable but not copyable
160 Impl( const Impl& ) = delete;
161 Impl& operator=( const Impl& ) = delete;
162 Impl( Impl&& ) = default;
163 Impl& operator=( Impl&& ) = default;
167 #ifdef DALI_WEBP_ENABLED
168 if( &mWebPData != NULL )
170 free( (void*)mWebPData.bytes );
171 mWebPData.bytes = nullptr;
172 WebPDataInit( &mWebPData );
174 if( mWebPAnimDecoder )
176 WebPAnimDecoderDelete(mWebPAnimDecoder);
182 std::vector<uint32_t> mTimeStamp;
183 uint32_t mLoadingFrame;
185 #ifdef DALI_WEBP_ENABLED
187 WebPAnimDecoder* mWebPAnimDecoder;
188 WebPAnimInfo mWebPAnimInfo;
192 AnimatedImageLoadingPtr WebPLoading::New( const std::string &url, bool isLocalResource )
194 #ifndef DALI_WEBP_ENABLED
195 DALI_LOG_ERROR( "The system does not support Animated WebP format.\n" );
197 return AnimatedImageLoadingPtr( new WebPLoading( url, isLocalResource ) );
200 WebPLoading::WebPLoading( const std::string &url, bool isLocalResource )
201 : mImpl( new WebPLoading::Impl( url, isLocalResource ) )
205 WebPLoading::~WebPLoading()
210 bool WebPLoading::LoadNextNFrames( uint32_t frameStartIndex, int count, std::vector<Dali::PixelData> &pixelData )
212 #ifdef DALI_WEBP_ENABLED
213 if( frameStartIndex >= mImpl->mWebPAnimInfo.frame_count )
218 DALI_LOG_INFO( gWebPLoadingLogFilter, Debug::Concise, "LoadNextNFrames( frameStartIndex:%d, count:%d )\n", frameStartIndex, count );
220 if( mImpl->mLoadingFrame > frameStartIndex )
222 mImpl->mLoadingFrame = 0;
223 WebPAnimDecoderReset( mImpl->mWebPAnimDecoder );
226 for( ; mImpl->mLoadingFrame < frameStartIndex ; ++mImpl->mLoadingFrame )
228 uint8_t* frameBuffer;
230 WebPAnimDecoderGetNext( mImpl->mWebPAnimDecoder, &frameBuffer, ×tamp );
231 mImpl->mTimeStamp[mImpl->mLoadingFrame] = timestamp;
234 for( int i = 0; i < count; ++i )
236 const int bufferSize = mImpl->mWebPAnimInfo.canvas_width * mImpl->mWebPAnimInfo.canvas_height * sizeof( uint32_t );
237 uint8_t* frameBuffer;
239 WebPAnimDecoderGetNext( mImpl->mWebPAnimDecoder, &frameBuffer, ×tamp );
241 auto pixelBuffer = new uint8_t[ bufferSize ];
242 memcpy( pixelBuffer, frameBuffer, bufferSize );
243 mImpl->mTimeStamp[mImpl->mLoadingFrame] = timestamp;
247 pixelData.push_back( Dali::PixelData::New( pixelBuffer, bufferSize,
248 mImpl->mWebPAnimInfo.canvas_width, mImpl->mWebPAnimInfo.canvas_height,
249 Dali::Pixel::RGBA8888, Dali::PixelData::DELETE_ARRAY) );
252 mImpl->mLoadingFrame++;
253 if( mImpl->mLoadingFrame >= mImpl->mWebPAnimInfo.frame_count )
255 mImpl->mLoadingFrame = 0;
256 WebPAnimDecoderReset( mImpl->mWebPAnimDecoder );
266 ImageDimensions WebPLoading::GetImageSize() const
268 #ifdef DALI_WEBP_ENABLED
269 return ImageDimensions( mImpl->mWebPAnimInfo.canvas_width, mImpl->mWebPAnimInfo.canvas_height );
271 return ImageDimensions();
275 uint32_t WebPLoading::GetImageCount() const
277 #ifdef DALI_WEBP_ENABLED
278 return mImpl->mWebPAnimInfo.frame_count;
284 uint32_t WebPLoading::GetFrameInterval( uint32_t frameIndex ) const
286 if( frameIndex >= GetImageCount() )
294 return mImpl->mTimeStamp[frameIndex] - mImpl->mTimeStamp[frameIndex - 1];
296 return mImpl->mTimeStamp[frameIndex];
300 } // namespace Adaptor
302 } // namespace Internal