2 * Copyright (c) 2014 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.
19 #include <dali/internal/event/images/encoded-buffer-image-impl.h>
22 #include <dali/internal/event/common/thread-local-storage.h>
23 #include <dali/internal/event/resources/resource-client.h>
24 #include <dali/integration-api/platform-abstraction.h>
31 Dali::EncodedBufferImage EncodedBufferImage::New( const uint8_t * const encodedImage,
32 const std::size_t encodedImageByteCount,
33 const ImageAttributes& attributes,
34 const ReleasePolicy releasePol )
36 DALI_ASSERT_DEBUG( encodedImage && "Null image pointer passed-in for decoding from memory." );
37 DALI_ASSERT_DEBUG( encodedImageByteCount > 0U && "Zero size passed for image resource in memory buffer." );
38 DALI_ASSERT_ALWAYS( encodedImage && (encodedImageByteCount != 0) );
39 // SEGV early before we allocate anything if the caller passed in an invalid
40 // input buffer by reading both ends of it:
41 DALI_ASSERT_ALWAYS( static_cast<int>( encodedImage[0] + encodedImage[encodedImageByteCount-1] ) != -1 );
43 EncodedBufferImage* const image = new EncodedBufferImage( releasePol );
44 // Make sure that this image object cannot leak if we throw:
45 Dali::EncodedBufferImage publicImage(image);
47 // Replicate the functionality of ImageFactory::load() without the filesystem caching:
48 Dali::Integration::BitmapResourceType resourceType( attributes );
49 RequestBufferPtr buffer( new RequestBuffer );
50 buffer->GetVector().Resize( encodedImageByteCount );
51 // Resize() won't throw on failure, so avoid a SEGV if the allocation failed:
52 DALI_ASSERT_ALWAYS( buffer->GetVector().Size() >= encodedImageByteCount );
54 memcpy( &(buffer->GetVector()[0]), encodedImage, encodedImageByteCount );
56 // Get image size from buffer
58 Internal::ThreadLocalStorage::Get().GetPlatformAbstraction().GetClosestImageSize( buffer, attributes, size );
59 image->mWidth = (unsigned int) size.width;
60 image->mHeight = (unsigned int) size.height;
62 ResourceClient &resourceClient = ThreadLocalStorage::Get().GetResourceClient();
63 ResourceTicketPtr ticket = resourceClient.DecodeResource( resourceType, buffer );
66 DALI_ASSERT_DEBUG( dynamic_cast<ImageTicket*>( ticket.Get() ) && "Resource ticket returned for image resource has to be an ImageTicket subclass.\n" );
67 ImageTicket * const imageTicket = static_cast<ImageTicket*>( ticket.Get() );
69 image->mTicket = imageTicket;
70 imageTicket->AddObserver( *image );
76 } // namespace Internal