Merge "Added travis.yml for auto-building patches" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / event / images / encoded-buffer-image-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/images/encoded-buffer-image-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring> // for memcpy
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/internal/event/common/thread-local-storage.h>
27 #include <dali/integration-api/platform-abstraction.h>
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33
34 namespace
35 {
36 TypeRegistration mType( typeid( Dali::EncodedBufferImage ), typeid( Dali::Image ), NULL );
37 } // unnamed namespace
38
39 EncodedBufferImagePtr EncodedBufferImage::New( const uint8_t * const encodedImage,
40                                                std::size_t encodedImageByteCount,
41                                                ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode,
42                                                bool orientationCorrection )
43 {
44   DALI_ASSERT_DEBUG( encodedImage && "Null image pointer passed-in for decoding from memory." );
45   DALI_ASSERT_DEBUG( encodedImageByteCount > 0U && "Zero size passed for image resource in memory buffer." );
46   DALI_ASSERT_ALWAYS( encodedImage && (encodedImageByteCount != 0) );
47   // SEGV early before we allocate anything if the caller passed in an invalid
48   // input buffer by reading both ends of it:
49   DALI_ASSERT_ALWAYS( static_cast<int>( encodedImage[0] + encodedImage[encodedImageByteCount-1] ) != -1 );
50
51   EncodedBufferImagePtr image( new EncodedBufferImage() );
52   image->Initialize(); // Second stage initialization
53
54   Dali::Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
55   RequestBufferPtr buffer( new RequestBuffer );
56   buffer->GetVector().Resize( encodedImageByteCount );
57   // Resize() won't throw on failure, so avoid a SEGV if the allocation failed:
58   DALI_ASSERT_ALWAYS( buffer->GetVector().Size() >= encodedImageByteCount );
59
60   memcpy( &(buffer->GetVector()[0]), encodedImage, encodedImageByteCount );
61
62   // Get image size from buffer
63   Dali::Integration::PlatformAbstraction& platformAbstraction = Internal::ThreadLocalStorage::Get().GetPlatformAbstraction();
64   const ImageDimensions expectedSize = platformAbstraction.GetClosestImageSize( buffer, size, fittingMode, samplingMode, orientationCorrection );
65   image->mWidth = (unsigned int) expectedSize.GetWidth();
66   image->mHeight = (unsigned int) expectedSize.GetHeight();
67
68   // Load the image synchronously
69   Integration::BitmapPtr bitmap = platformAbstraction.DecodeBuffer( resourceType, &(buffer->GetVector()[0]), encodedImageByteCount );
70
71   if( bitmap )
72   {
73     unsigned width  = bitmap->GetImageWidth();
74     unsigned height = bitmap->GetImageHeight();
75
76     //Create texture
77     Pixel::Format format = bitmap->GetPixelFormat();
78     image->mTexture = NewTexture::New( Dali::TextureType::TEXTURE_2D, format, width, height );
79
80     //Upload data to the texture
81     size_t bufferSize = bitmap->GetBufferSize();
82     PixelDataPtr pixelData = PixelData::New( bitmap->GetBufferOwnership(), bufferSize, width, height, format,
83                                              static_cast< Dali::PixelData::ReleaseFunction >( bitmap->GetReleaseFunction() ) );
84     image->mTexture->Upload( pixelData );
85
86     image->mWidth = size.GetWidth();
87     if( image->mWidth == 0 )
88     {
89       image->mWidth = width;
90     }
91
92     image->mHeight = size.GetHeight();
93     if( image->mHeight == 0 )
94     {
95       image->mHeight = height;
96     }
97   }
98   else
99   {
100     image->mTexture = NewTexture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, 0u, 0u );
101     image->mWidth = image->mHeight = 0u;
102   }
103
104   return image;
105 }
106
107 } // namespace Internal
108 } // namespace Dali