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 <cstring> // for memcpy
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/internal/event/common/thread-local-storage.h>
27 #include <dali/internal/event/resources/resource-client.h>
28 #include <dali/integration-api/platform-abstraction.h>
37 TypeRegistration mType( typeid( Dali::EncodedBufferImage ), typeid( Dali::Image ), NULL );
38 } // unnamed namespace
40 EncodedBufferImagePtr EncodedBufferImage::New( const uint8_t * const encodedImage,
41 std::size_t encodedImageByteCount,
42 ImageDimensions size, FittingMode::Type fittingMode, SamplingMode::Type samplingMode,
43 bool orientationCorrection,
44 ReleasePolicy releasePol )
46 DALI_ASSERT_DEBUG( encodedImage && "Null image pointer passed-in for decoding from memory." );
47 DALI_ASSERT_DEBUG( encodedImageByteCount > 0U && "Zero size passed for image resource in memory buffer." );
48 DALI_ASSERT_ALWAYS( encodedImage && (encodedImageByteCount != 0) );
49 // SEGV early before we allocate anything if the caller passed in an invalid
50 // input buffer by reading both ends of it:
51 DALI_ASSERT_ALWAYS( static_cast<int>( encodedImage[0] + encodedImage[encodedImageByteCount-1] ) != -1 );
53 EncodedBufferImagePtr image( new EncodedBufferImage( releasePol ) );
54 image->Initialize(); // Second stage initialization
56 // Replicate the functionality of ImageFactory::load() without the filesystem caching:
57 Dali::Integration::BitmapResourceType resourceType( size, fittingMode, samplingMode, orientationCorrection );
58 RequestBufferPtr buffer( new RequestBuffer );
59 buffer->GetVector().Resize( encodedImageByteCount );
60 // Resize() won't throw on failure, so avoid a SEGV if the allocation failed:
61 DALI_ASSERT_ALWAYS( buffer->GetVector().Size() >= encodedImageByteCount );
63 memcpy( &(buffer->GetVector()[0]), encodedImage, encodedImageByteCount );
65 // Get image size from buffer
66 Dali::Integration::PlatformAbstraction& platformAbstraction = Internal::ThreadLocalStorage::Get().GetPlatformAbstraction();
67 const ImageDimensions expectedSize = platformAbstraction.GetClosestImageSize( buffer, size, fittingMode, samplingMode, orientationCorrection );
68 image->mWidth = (unsigned int) expectedSize.GetWidth();
69 image->mHeight = (unsigned int) expectedSize.GetHeight();
71 // Load the image synchronously
72 Integration::BitmapPtr bitmap = platformAbstraction.DecodeBuffer( resourceType, &(buffer->GetVector()[0]), encodedImageByteCount );
76 ResourceClient &resourceClient = ThreadLocalStorage::Get().GetResourceClient();
77 image->mTicket = resourceClient.AddBitmapImage( bitmap.Get() );
78 image->mTicket->AddObserver( *image );
84 } // namespace Internal