2 * Copyright (c) 2016 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.
18 #include "image-loading.h"
21 #include "image-loaders/image-loader.h"
22 #include <resource-loader/network/file-download.h>
23 #include <platform-abstractions/portable/file-closer.h>
30 // limit maximum image down load size to 50 MB
31 const size_t MAXIMUM_DOWNLOAD_IMAGE_SIZE = 50 * 1024 * 1024 ;
34 PixelData LoadImageFromFile( const std::string& url, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection )
36 Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
37 IntrusivePtr<Dali::RefObject> resource = TizenPlatform::ImageLoader::LoadImageSynchronously( resourceType, url );
41 Integration::Bitmap* bitmap = static_cast<Integration::Bitmap*>( resource.Get() );
43 // Use bitmap->GetBufferOwnership() to transfer the buffer ownership to pixelData.
44 // The destroy of bitmap will not release the buffer, instead, the pixelData is responsible for releasing when its reference count falls to zero.
45 return Dali::PixelData::New( bitmap->GetBufferOwnership(),
46 bitmap->GetBufferSize(),
47 bitmap->GetImageWidth(),
48 bitmap->GetImageHeight(),
49 bitmap->GetPixelFormat(),
50 Dali::PixelData::FREE );
52 return Dali::PixelData();
55 ImageDimensions GetClosestImageSize( const std::string& filename,
57 FittingMode::Type fittingMode,
58 SamplingMode::Type samplingMode,
59 bool orientationCorrection )
61 return TizenPlatform::ImageLoader::GetClosestImageSize( filename, size, fittingMode, samplingMode, orientationCorrection );
65 PixelData DownloadImageSynchronously( const std::string& url, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection )
67 Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
70 Dali::Vector<uint8_t> dataBuffer;
73 succeeded = TizenPlatform::Network::DownloadRemoteFileIntoMemory( url, dataBuffer, dataSize,
74 MAXIMUM_DOWNLOAD_IMAGE_SIZE );
77 void *blobBytes = static_cast<void*>(&dataBuffer[0]);
78 size_t blobSize = dataBuffer.Size();
80 DALI_ASSERT_DEBUG( blobSize > 0U );
81 DALI_ASSERT_DEBUG( blobBytes != 0U );
83 if( blobBytes != 0 && blobSize > 0U )
85 // Open a file handle on the memory buffer:
86 Dali::Internal::Platform::FileCloser fileCloser( blobBytes, blobSize, "rb" );
87 FILE * const fp = fileCloser.GetFile();
90 Integration::BitmapPtr bitmap;
91 bool result = TizenPlatform::ImageLoader::ConvertStreamToBitmap(
97 if ( result && bitmap )
99 return Dali::PixelData::New( bitmap->GetBufferOwnership(),
100 bitmap->GetBufferSize(),
101 bitmap->GetImageWidth(),
102 bitmap->GetImageHeight(),
103 bitmap->GetPixelFormat(),
104 Dali::PixelData::FREE );
108 DALI_LOG_WARNING( "Unable to decode bitmap supplied as in-memory blob.\n" );
114 return Dali::PixelData();