// EXTERNAL INCLUDES
#include <string.h>
#include <dali/public-api/signals/callback.h>
-#include <dali/public-api/images/resource-image.h>
-#include <dali/devel-api/adaptor-framework/bitmap-loader.h>
+#include <dali/devel-api/adaptor-framework/image-loading.h>
#include <dali/integration-api/debug.h>
namespace Dali
namespace Internal
{
+typedef unsigned char PixelBuffer;
+
+Texture ImageAtlas::PackToAtlas( const std::vector<PixelData>& pixelData, Dali::Vector<Vector4>& textureRects )
+{
+ // Record each block size
+ Dali::Vector<Uint16Pair> blockSizes;
+ SizeType count = pixelData.size();
+ for( SizeType index = 0; index < count; index++ )
+ {
+ blockSizes.PushBack( ImageDimensions( pixelData[index].GetWidth(), pixelData[index].GetHeight() ) );
+ }
+
+ // Ask atlasPacker for packing position of each block
+ Dali::Vector<Uint16Pair> packPositions;
+ ImageDimensions atlasSize = AtlasPacker::GroupPack( blockSizes, packPositions );
+
+ // Prepare for outout texture rect array
+ textureRects.Clear();
+ textureRects.Resize( count );
+
+ // create the texture for uploading the multiple pixel data
+ Texture atlasTexture = Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGBA8888, atlasSize.GetWidth(), atlasSize.GetHeight() );
+
+ float atlasWidth = static_cast<float>( atlasTexture.GetWidth() );
+ float atlasHeight = static_cast<float>( atlasTexture.GetHeight() );
+ int packPositionX, packPositionY;
+ // Upload the pixel data one by one to its packing position, and record the texture rects
+ for( SizeType index = 0; index < count; index++ )
+ {
+ packPositionX = packPositions[index].GetX();
+ packPositionY = packPositions[index].GetY();
+ atlasTexture.Upload( pixelData[index], 0u, 0u,
+ packPositionX, packPositionY,
+ pixelData[index].GetWidth(), pixelData[index].GetHeight() );
+
+ // Apply the half pixel correction to avoid the color bleeding between neighbour blocks
+ textureRects[index].x = ( static_cast<float>( packPositionX ) +0.5f ) / atlasWidth; // left
+ textureRects[index].y = ( static_cast<float>( packPositionY ) +0.5f ) / atlasHeight; // right
+ textureRects[index].z = ( static_cast<float>( packPositionX + pixelData[index].GetWidth() )-0.5f ) / atlasWidth; // right
+ textureRects[index].w = ( static_cast<float>( packPositionY + pixelData[index].GetHeight() )-0.5f ) / atlasHeight;// bottom
+ }
+
+ return atlasTexture;
+}
ImageAtlas::ImageAtlas( SizeType width, SizeType height, Pixel::Format pixelFormat )
: mAtlas( Texture::New( Dali::TextureType::TEXTURE_2D, pixelFormat, width, height ) ),
void ImageAtlas::SetBrokenImage( const std::string& brokenImageUrl )
{
- mBrokenImageSize = ResourceImage::GetImageSize( brokenImageUrl );
+ mBrokenImageSize = Dali::GetClosestImageSize( brokenImageUrl );
if(mBrokenImageSize.GetWidth() > 0 && mBrokenImageSize.GetHeight() > 0 ) // check the url is valid
{
mBrokenImageUrl = brokenImageUrl;
ImageDimensions zero;
if( size == zero ) // image size not provided
{
- dimensions = ResourceImage::GetImageSize( url );
+ dimensions = Dali::GetClosestImageSize( url );
if( dimensions == zero ) // Fail to read the image & broken image file exists
{
if( !mBrokenImageUrl.empty() )
void ImageAtlas::UploadBrokenImage( const Rect<unsigned int>& area )
{
- BitmapLoader loader = BitmapLoader::New(mBrokenImageUrl, ImageDimensions( area.width, area.height ) );
- loader.Load();
- SizeType loadedWidth = loader.GetPixelData().GetWidth();
- SizeType loadedHeight = loader.GetPixelData().GetHeight();
+ Devel::PixelBuffer brokenBuffer = LoadImageFromFile( mBrokenImageUrl, ImageDimensions( area.width, area.height ) );
+ SizeType loadedWidth = brokenBuffer.GetWidth();
+ SizeType loadedHeight = brokenBuffer.GetHeight();
bool needBackgroundClear = false;
SizeType packX = area.x;
if( needBackgroundClear )
{
SizeType size = area.width * area.height * Pixel::GetBytesPerPixel( mPixelFormat );
- PixelBuffer* buffer = new PixelBuffer [size];
- PixelData background = PixelData::New( buffer, size, area.width, area.height, mPixelFormat, PixelData::DELETE_ARRAY );
+ Devel::PixelBuffer background = Devel::PixelBuffer::New( area.width, area.height, mPixelFormat );
+ unsigned char* buffer = background.GetBuffer();
for( SizeType idx = 0; idx < size; idx++ )
{
buffer[idx] = 0x00;
}
- mAtlas.Upload( background, 0u, 0u, area.x, area.y, area.width, area.height );
+ PixelData pixelData = Devel::PixelBuffer::Convert( background );
+ mAtlas.Upload( pixelData, 0u, 0u, area.x, area.y, area.width, area.height );
}
- mAtlas.Upload( loader.GetPixelData(), 0u, 0u, packX, packY, loadedWidth, loadedHeight );
+ PixelData brokenPixelData = Devel::PixelBuffer::Convert( brokenBuffer );
+ mAtlas.Upload( brokenPixelData, 0u, 0u, packX, packY, loadedWidth, loadedHeight );
}
} // namespace Internal