2 * Copyright (c) 2017 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-reader.h>
24 #include "pixel-buffer-impl.h"
32 // limit maximum image down load size to 50 MB
33 const size_t MAXIMUM_DOWNLOAD_IMAGE_SIZE = 50 * 1024 * 1024 ;
35 static unsigned int gMaxTextureSize = 4096;
39 Devel::PixelBuffer LoadImageFromFile( const std::string& url, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection )
41 Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
43 Internal::Platform::FileReader fileReader( url );
44 FILE * const fp = fileReader.GetFile();
47 Integration::BitmapPtr bitmap;
48 bool success = TizenPlatform::ImageLoader::ConvertStreamToBitmap( resourceType, url, fp, bitmap );
49 if( success && bitmap )
51 // Use bitmap->GetBufferOwnership() to transfer the buffer ownership
52 // to pixelData. The destroy of bitmap will not release the buffer,
53 // instead, the pixelBuffer is responsible for releasing when its
54 // reference count falls to zero.
55 Internal::Adaptor::PixelBufferPtr pixelBufferImpl =
56 Internal::Adaptor::PixelBuffer::New( bitmap->GetBufferOwnership(),
57 bitmap->GetBufferSize(),
58 bitmap->GetImageWidth(),
59 bitmap->GetImageHeight(),
60 bitmap->GetPixelFormat() );
62 Dali::Devel::PixelBuffer pixelBuffer( pixelBufferImpl.Get() );
66 return Dali::Devel::PixelBuffer();
69 ImageDimensions GetClosestImageSize( const std::string& filename,
71 FittingMode::Type fittingMode,
72 SamplingMode::Type samplingMode,
73 bool orientationCorrection )
75 ImageDimensions dimension = TizenPlatform::ImageLoader::GetClosestImageSize( filename, size, fittingMode, samplingMode, orientationCorrection );
77 dimension.SetWidth( std::min( dimension.GetWidth(), static_cast< uint16_t >( GetMaxTextureSize() ) ) );
78 dimension.SetHeight( std::min( dimension.GetHeight(), static_cast< uint16_t >( GetMaxTextureSize() ) ) );
84 Devel::PixelBuffer DownloadImageSynchronously( const std::string& url, ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, bool orientationCorrection )
86 Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
89 Dali::Vector<uint8_t> dataBuffer;
92 succeeded = TizenPlatform::Network::DownloadRemoteFileIntoMemory( url, dataBuffer, dataSize,
93 MAXIMUM_DOWNLOAD_IMAGE_SIZE );
96 size_t blobSize = dataBuffer.Size();
98 DALI_ASSERT_DEBUG( blobSize > 0U );
102 // Open a file handle on the memory buffer:
103 Dali::Internal::Platform::FileReader fileReader( dataBuffer, blobSize );
104 FILE * const fp = fileReader.GetFile();
107 Integration::BitmapPtr bitmap;
108 bool result = TizenPlatform::ImageLoader::ConvertStreamToBitmap(
114 if ( result && bitmap )
116 Internal::Adaptor::PixelBufferPtr pixelBufferImpl =
117 Internal::Adaptor::PixelBuffer::New( bitmap->GetBufferOwnership(),
118 bitmap->GetBufferSize(),
119 bitmap->GetImageWidth(),
120 bitmap->GetImageHeight(),
121 bitmap->GetPixelFormat() );
123 Dali::Devel::PixelBuffer pixelBuffer( pixelBufferImpl.Get() );
128 DALI_LOG_WARNING( "Unable to decode bitmap supplied as in-memory blob.\n" );
133 return Dali::Devel::PixelBuffer();
136 void SetMaxTextureSize( unsigned int size )
138 gMaxTextureSize = size;
141 unsigned int GetMaxTextureSize()
143 return gMaxTextureSize;