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>
24 #include <platform-abstractions/tizen/resource-loader/resource-loading-client.h>
31 // limit maximum image down load size to 50 MB
32 const size_t MAXIMUM_DOWNLOAD_IMAGE_SIZE = 50 * 1024 * 1024 ;
35 PixelData LoadImageFromFile( const std::string& url, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection )
37 Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
38 IntrusivePtr<Dali::RefObject> resource = TizenPlatform::ImageLoader::LoadResourceSynchronously( resourceType, url );
42 Integration::Bitmap* bitmap = static_cast<Integration::Bitmap*>( resource.Get() );
44 // Use bitmap->GetBufferOwnership() to transfer the buffer ownership to pixelData.
45 // The destroy of bitmap will not release the buffer, instead, the pixelData is responsible for releasing when its reference count falls to zero.
46 return Dali::PixelData::New( bitmap->GetBufferOwnership(),
47 bitmap->GetBufferSize(),
48 bitmap->GetImageWidth(),
49 bitmap->GetImageHeight(),
50 bitmap->GetPixelFormat(),
51 Dali::PixelData::FREE );
53 return Dali::PixelData();
56 ImageDimensions GetClosestImageSize( const std::string& filename,
58 FittingMode::Type fittingMode,
59 SamplingMode::Type samplingMode,
60 bool orientationCorrection )
62 return TizenPlatform::ImageLoader::GetClosestImageSize( filename, size, fittingMode, samplingMode, orientationCorrection );
66 PixelData DownloadImageSynchronously( const std::string& url, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection )
68 Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
71 Dali::Vector<uint8_t> dataBuffer;
74 succeeded = TizenPlatform::Network::DownloadRemoteFileIntoMemory( url, dataBuffer, dataSize,
75 MAXIMUM_DOWNLOAD_IMAGE_SIZE );
78 void *blobBytes = static_cast<void*>(&dataBuffer[0]);
79 size_t blobSize = dataBuffer.Size();
81 DALI_ASSERT_DEBUG( blobSize > 0U );
82 DALI_ASSERT_DEBUG( blobBytes != 0U );
84 if( blobBytes != 0 && blobSize > 0U )
86 // Open a file handle on the memory buffer:
87 Dali::Internal::Platform::FileCloser fileCloser( blobBytes, blobSize, "rb" );
88 FILE * const fp = fileCloser.GetFile();
91 Integration::BitmapPtr bitmap;
92 bool result = TizenPlatform::ImageLoader::ConvertStreamToBitmap(
96 TizenPlatform::StubbedResourceLoadingClient(),
99 if ( result && bitmap )
101 return Dali::PixelData::New( bitmap->GetBufferOwnership(),
102 bitmap->GetBufferSize(),
103 bitmap->GetImageWidth(),
104 bitmap->GetImageHeight(),
105 bitmap->GetPixelFormat(),
106 Dali::PixelData::FREE );
110 DALI_LOG_WARNING( "Unable to decode bitmap supplied as in-memory blob.\n" );
116 return Dali::PixelData();