[Tizen] Revert "Remove TypeRegistration from deprecated Image classes"
[platform/core/uifw/dali-core.git] / dali / internal / event / images / encoded-buffer-image-impl.cpp
1 /*
2  * Copyright (c) 2017 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/devel-api/common/ref-counted-dali-vector.h>
27 #include <dali/internal/event/common/thread-local-storage.h>
28 #include <dali/integration-api/platform-abstraction.h>
29
30 namespace Dali
31 {
32 namespace Internal
33 {
34
35 namespace
36 {
37
38 TypeRegistration mType( typeid( Dali::EncodedBufferImage ), typeid( Dali::Image ), NULL );
39
40 /** Raw bytes of a resource laid out exactly as it would be in a file, but in memory. */
41 typedef Dali::RefCountedVector<uint8_t> RequestBuffer;
42 /** Counting smart pointer for managing a buffer of raw bytes. */
43 typedef IntrusivePtr<RequestBuffer>     RequestBufferPtr;
44
45 } // unnamed namespace
46
47 EncodedBufferImagePtr EncodedBufferImage::New( const uint8_t * const encodedImage,
48                                                std::size_t encodedImageByteCount,
49                                                ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode,
50                                                bool orientationCorrection )
51 {
52   DALI_ASSERT_DEBUG( encodedImage && "Null image pointer passed-in for decoding from memory." );
53   DALI_ASSERT_DEBUG( encodedImageByteCount > 0U && "Zero size passed for image resource in memory buffer." );
54   DALI_ASSERT_ALWAYS( encodedImage && (encodedImageByteCount != 0) );
55   // SEGV early before we allocate anything if the caller passed in an invalid
56   // input buffer by reading both ends of it:
57   DALI_ASSERT_ALWAYS( static_cast<int>( encodedImage[0] + encodedImage[encodedImageByteCount-1] ) != -1 );
58
59   EncodedBufferImagePtr image( new EncodedBufferImage() );
60   image->Initialize(); // Second stage initialization
61
62   Dali::Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
63   RequestBufferPtr buffer( new RequestBuffer );
64   buffer->GetVector().Resize( encodedImageByteCount );
65   // Resize() won't throw on failure, so avoid a SEGV if the allocation failed:
66   DALI_ASSERT_ALWAYS( buffer->GetVector().Size() >= encodedImageByteCount );
67
68   memcpy( &(buffer->GetVector()[0]), encodedImage, encodedImageByteCount );
69
70   // Get image size from buffer
71   Dali::Integration::PlatformAbstraction& platformAbstraction = Internal::ThreadLocalStorage::Get().GetPlatformAbstraction();
72   const ImageDimensions expectedSize = platformAbstraction.GetClosestImageSize( buffer, size, fittingMode, samplingMode, orientationCorrection );
73   image->mWidth = static_cast<unsigned int>( expectedSize.GetWidth() );
74   image->mHeight = static_cast<unsigned int>( expectedSize.GetHeight() );
75
76   // Load the image synchronously
77   Integration::BitmapPtr bitmap = platformAbstraction.DecodeBuffer( resourceType, &(buffer->GetVector()[0]), encodedImageByteCount );
78
79   if( bitmap )
80   {
81     unsigned width  = bitmap->GetImageWidth();
82     unsigned height = bitmap->GetImageHeight();
83
84     //Create texture
85     Pixel::Format format = bitmap->GetPixelFormat();
86     image->mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, format, width, height );
87
88     //Upload data to the texture
89     uint32_t bufferSize = bitmap->GetBufferSize();
90     PixelDataPtr pixelData = PixelData::New( bitmap->GetBufferOwnership(), bufferSize, width, height, format,
91                                              static_cast< Dali::PixelData::ReleaseFunction >( bitmap->GetReleaseFunction() ) );
92     image->mTexture->Upload( pixelData );
93
94     image->mWidth = size.GetWidth();
95     if( image->mWidth == 0 )
96     {
97       image->mWidth = width;
98     }
99
100     image->mHeight = size.GetHeight();
101     if( image->mHeight == 0 )
102     {
103       image->mHeight = height;
104     }
105   }
106   else
107   {
108     image->mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, 0u, 0u );
109     image->mWidth = image->mHeight = 0u;
110   }
111
112   return image;
113 }
114
115 } // namespace Internal
116 } // namespace Dali