2 * Copyright (c) 2017 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/nine-patch-image-impl.h>
22 #include <cstring> // for memcmp
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/integration-api/bitmap.h>
27 #include <dali/internal/event/common/thread-local-storage.h>
28 #include <dali/internal/update/manager/update-manager.h>
29 #include <dali/integration-api/platform-abstraction.h>
30 #include <dali/integration-api/resource-types.h>
35 void GetRedOffsetAndMask(Dali::Pixel::Format pixelFormat, int& byteOffset, int& bitMask)
41 case Dali::Pixel::LA88:
48 case Dali::Pixel::RGB888:
49 case Dali::Pixel::RGB8888:
50 case Dali::Pixel::RGBA8888:
56 case Dali::Pixel::BGR8888:
57 case Dali::Pixel::BGRA8888:
63 case Dali::Pixel::RGB565:
69 case Dali::Pixel::BGR565:
76 case Dali::Pixel::RGBA4444:
82 case Dali::Pixel::BGRA4444:
89 case Dali::Pixel::RGBA5551:
96 case Dali::Pixel::BGRA5551:
103 case Dali::Pixel::INVALID:
104 case Dali::Pixel::COMPRESSED_R11_EAC:
105 case Dali::Pixel::COMPRESSED_SIGNED_R11_EAC:
106 case Dali::Pixel::COMPRESSED_RG11_EAC:
107 case Dali::Pixel::COMPRESSED_SIGNED_RG11_EAC:
108 case Dali::Pixel::COMPRESSED_RGB8_ETC2:
109 case Dali::Pixel::COMPRESSED_SRGB8_ETC2:
110 case Dali::Pixel::COMPRESSED_RGB8_ETC1:
111 case Dali::Pixel::COMPRESSED_RGB_PVRTC_4BPPV1:
112 case Dali::Pixel::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
113 case Dali::Pixel::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
114 case Dali::Pixel::COMPRESSED_RGBA8_ETC2_EAC:
115 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
116 case Dali::Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR:
117 case Dali::Pixel::COMPRESSED_RGBA_ASTC_5x4_KHR:
118 case Dali::Pixel::COMPRESSED_RGBA_ASTC_5x5_KHR:
119 case Dali::Pixel::COMPRESSED_RGBA_ASTC_6x5_KHR:
120 case Dali::Pixel::COMPRESSED_RGBA_ASTC_6x6_KHR:
121 case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x5_KHR:
122 case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x6_KHR:
123 case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x8_KHR:
124 case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x5_KHR:
125 case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x6_KHR:
126 case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x8_KHR:
127 case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x10_KHR:
128 case Dali::Pixel::COMPRESSED_RGBA_ASTC_12x10_KHR:
129 case Dali::Pixel::COMPRESSED_RGBA_ASTC_12x12_KHR:
130 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
131 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
132 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
133 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
134 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
135 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
136 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
137 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
138 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
139 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
140 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
141 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
142 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
143 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
145 DALI_LOG_ERROR("Pixel formats for compressed images are not compatible with simple masking-out of per-pixel alpha.\n");
151 case Dali::Pixel::RGB16F:
152 case Dali::Pixel::RGB32F:
154 DALI_LOG_ERROR("Pixel format not compatible.\n");
162 } // anonymous namespace
172 TypeRegistration mType( typeid( Dali::NinePatchImage ), typeid( Dali::Image ), NULL );
173 } // unnamed namespace
175 NinePatchImagePtr NinePatchImage::New( const std::string& filename )
177 Internal::NinePatchImagePtr internal( new NinePatchImage( filename ) );
178 internal->Initialize();
182 NinePatchImage::NinePatchImage( const std::string& filename )
187 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
189 Integration::PlatformAbstraction& platformAbstraction = tls.GetPlatformAbstraction();
190 Integration::BitmapResourceType resourceType;
192 // Note, bitmap is only destroyed when the image is destroyed.
193 Integration::ResourcePointer resource = platformAbstraction.LoadImageSynchronously( resourceType, filename );
196 mBitmap = static_cast<Integration::Bitmap*>( resource.Get());
198 mWidth = mBitmap->GetImageWidth();
199 mHeight = mBitmap->GetImageHeight();
200 mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, mBitmap->GetPixelFormat(), mWidth, mHeight );
202 size_t bufferSize = mBitmap->GetBufferSize();
203 unsigned char* buffer = new unsigned char[bufferSize];
204 memcpy( buffer, mBitmap->GetBuffer(), bufferSize );
205 PixelDataPtr pixelData = PixelData::New( buffer, bufferSize, mWidth, mHeight, mBitmap->GetPixelFormat(), Dali::PixelData::DELETE_ARRAY );
206 mTexture->Upload( pixelData );
216 NinePatchImage* NinePatchImage::DownCast( Image* image)
218 return dynamic_cast<NinePatchImage*>(image);
221 NinePatchImage::~NinePatchImage()
225 const NinePatchImage::StretchRanges& NinePatchImage::GetStretchPixelsX()
227 if( ! mParsedBorder )
231 return mStretchPixelsX;
234 const NinePatchImage::StretchRanges& NinePatchImage::GetStretchPixelsY()
236 if( ! mParsedBorder )
240 return mStretchPixelsY;
243 Rect<int> NinePatchImage::GetChildRectangle()
245 if( ! mParsedBorder )
249 return mChildRectangle;
252 Internal::BufferImagePtr NinePatchImage::CreateCroppedBufferImage()
254 BufferImagePtr cropped;
258 DALI_LOG_ERROR( "NinePatchImage: Bitmap not loaded, cannot perform operation\n");
262 Pixel::Format pixelFormat = mBitmap->GetPixelFormat();
264 cropped = BufferImage::New( mWidth-2, mHeight-2, pixelFormat );
266 Integration::Bitmap::PackedPixelsProfile* srcProfile = mBitmap->GetPackedPixelsProfile();
267 DALI_ASSERT_DEBUG( srcProfile && "Wrong profile for source bitmap");
271 PixelBuffer* destPixels = cropped->GetBuffer();
272 unsigned int destStride = cropped->GetBufferStride();
273 unsigned int pixelWidth = GetBytesPerPixel(pixelFormat);
275 PixelBuffer* srcPixels = mBitmap->GetBuffer();
276 unsigned int srcStride = srcProfile->GetBufferStride();
278 for( unsigned int row=1; row < mHeight-1; ++row )
280 PixelBuffer* src = srcPixels + row*srcStride + pixelWidth;
281 PixelBuffer* dest = destPixels + (row-1)*destStride;
282 memcpy(dest, src, destStride );
287 cropped->Update(area); // default area has no width or height
292 const std::string& NinePatchImage::GetUrl() const
297 void NinePatchImage::ParseBorders()
301 DALI_LOG_ERROR( "NinePatchImage: Bitmap not loaded, cannot perform operation\n");
305 mStretchPixelsX.Clear();
306 mStretchPixelsY.Clear();
308 Pixel::Format pixelFormat = mBitmap->GetPixelFormat();
310 const Integration::Bitmap::PackedPixelsProfile* srcProfile = mBitmap->GetPackedPixelsProfile();
311 DALI_ASSERT_DEBUG( srcProfile && "Wrong profile for source bitmap" );
317 Pixel::GetAlphaOffsetAndMask( pixelFormat, alphaByte, alphaBits );
319 int testByte = alphaByte;
320 int testBits = alphaBits;
321 int testValue = alphaBits; // Opaque == stretch
324 GetRedOffsetAndMask( pixelFormat, testByte, testBits );
325 testValue = 0; // Black == stretch
328 unsigned int pixelWidth = GetBytesPerPixel( pixelFormat );
329 const PixelBuffer* srcPixels = mBitmap->GetBuffer();
330 unsigned int srcStride = srcProfile->GetBufferStride();
333 const PixelBuffer* top = srcPixels + pixelWidth;
334 unsigned int index = 0;
335 unsigned int width = mBitmap->GetImageWidth();
336 unsigned int height = mBitmap->GetImageHeight();
338 for(; index < width - 2; )
340 Uint16Pair range = ParseRange( index, width - 2, top, pixelWidth, testByte, testBits, testValue );
341 if( range.GetX() != 0xFFFF )
343 mStretchPixelsX.PushBack( range );
348 const PixelBuffer* left = srcPixels + srcStride;
350 for(; index < height - 2; )
352 Uint16Pair range = ParseRange( index, height - 2, left, srcStride, testByte, testBits, testValue );
353 if( range.GetX() != 0xFFFF )
355 mStretchPixelsY.PushBack( range );
359 //If there are no stretch pixels then make the entire image stretchable
360 if( mStretchPixelsX.Size() == 0 )
362 mStretchPixelsX.PushBack( Uint16Pair( 0, width - 2 ) );
364 if( mStretchPixelsY.Size() == 0 )
366 mStretchPixelsY.PushBack( Uint16Pair( 0, height - 2 ) );
371 const PixelBuffer* bottom = srcPixels + ( height - 1 ) * srcStride + pixelWidth;
373 Uint16Pair contentRangeX = ParseRange( index, width - 2, bottom, pixelWidth, testByte, testBits, testValue );
374 if( contentRangeX.GetX() == 0xFFFF )
376 contentRangeX = Uint16Pair();
380 const PixelBuffer* right = srcPixels + srcStride + ( width - 1 ) * pixelWidth;
382 Uint16Pair contentRangeY = ParseRange( index, height - 2, right, srcStride, testByte, testBits, testValue );
383 if( contentRangeY.GetX() == 0xFFFF )
385 contentRangeY = Uint16Pair();
388 mChildRectangle.x = contentRangeX.GetX() + 1;
389 mChildRectangle.y = contentRangeY.GetX() + 1;
390 mChildRectangle.width = contentRangeX.GetY() - contentRangeX.GetX();
391 mChildRectangle.height = contentRangeY.GetY() - contentRangeY.GetX();
393 mParsedBorder = true;
397 Uint16Pair NinePatchImage::ParseRange( unsigned int& index, unsigned int width, const PixelBuffer* & pixel, unsigned int pixelStride, int testByte, int testBits, int testValue )
399 unsigned int start = 0xFFFF;
400 for( ; index < width; ++index, pixel += pixelStride )
402 if( ( pixel[ testByte ] & testBits ) == testValue )
406 pixel += pixelStride;
411 unsigned int end = width;
412 for( ; index < width; ++index, pixel += pixelStride )
414 if( ( pixel[ testByte ] & testBits ) != testValue )
418 pixel += pixelStride;
423 return Uint16Pair( start, end );
426 bool NinePatchImage::IsNinePatchUrl( const std::string& url )
430 std::string::const_reverse_iterator iter = url.rbegin();
431 enum { SUFFIX, HASH, HASH_DOT, DONE } state = SUFFIX;
432 while(iter < url.rend())
442 else if(!isalnum(*iter))
450 if( *iter == '#' || *iter == '9' )
466 state = DONE; // Stop testing characters
486 } // namespace Internal