Bitmap core patch 2 of 4 - Replace all uses of the Bitmap class with new simpler...
[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 Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/event/images/encoded-buffer-image-impl.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/internal/event/common/thread-local-storage.h>
22 #include <dali/internal/event/resources/resource-client.h>
23 #include <dali/integration-api/platform-abstraction.h>
24
25 namespace Dali
26 {
27 namespace Internal
28 {
29
30 Dali::EncodedBufferImage EncodedBufferImage::New( const uint8_t * const encodedImage,
31                    const std::size_t encodedImageByteCount,
32                    const ImageAttributes& attributes,
33                    const ReleasePolicy releasePol )
34 {
35   DALI_ASSERT_DEBUG( encodedImage && "Null image pointer passed-in for decoding from memory." );
36   DALI_ASSERT_DEBUG( encodedImageByteCount > 0U && "Zero size passed for image resource in memory buffer." );
37   DALI_ASSERT_ALWAYS( encodedImage && (encodedImageByteCount != 0) );
38   // SEGV early before we allocate anything if the caller passed in an invalid
39   // input buffer by reading both ends of it:
40   DALI_ASSERT_ALWAYS( static_cast<int>( encodedImage[0] + encodedImage[encodedImageByteCount-1] ) != -1 );
41
42   EncodedBufferImage* const image = new EncodedBufferImage( releasePol );
43   // Make sure that this image object cannot leak if we throw:
44   Dali::EncodedBufferImage publicImage(image);
45
46   // Replicate the functionality of ImageFactory::load() without the filesystem caching:
47   Dali::Integration::ImageResourceType resourceType( Dali::Integration::ResourceImageData, attributes );
48   RequestBufferPtr buffer( new RequestBuffer );
49   buffer->GetVector().Resize( encodedImageByteCount );
50   // Resize() won't throw on failure, so avoid a SEGV if the allocation failed:
51   DALI_ASSERT_ALWAYS( buffer->GetVector().Size() >= encodedImageByteCount );
52
53   memcpy( &(buffer->GetVector()[0]), encodedImage, encodedImageByteCount );
54
55   // Get image size from buffer
56   Vector2 size;
57   Internal::ThreadLocalStorage::Get().GetPlatformAbstraction().GetClosestImageSize( buffer, attributes, size );
58   image->mWidth = (unsigned int) size.width;
59   image->mHeight = (unsigned int) size.height;
60
61   ResourceClient &resourceClient = ThreadLocalStorage::Get().GetResourceClient();
62   ResourceTicketPtr ticket = resourceClient.DecodeResource( resourceType, buffer );
63   if( ticket )
64   {
65     DALI_ASSERT_DEBUG( dynamic_cast<ImageTicket*>( ticket.Get() ) && "Resource ticket returned for image resource has to be an ImageTicket subclass.\n" );
66     ImageTicket * const imageTicket = static_cast<ImageTicket*>( ticket.Get() );
67
68     image->mTicket = imageTicket;
69     imageTicket->AddObserver( *image );
70   }
71
72   return publicImage;
73 }
74
75 } // namespace Internal
76 } // namespace Dali