2 * Copyright (c) 2016 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/event/resources/resource-client.h>
29 #include <dali/internal/update/manager/update-manager.h>
30 #include <dali/internal/event/images/image-factory.h>
31 #include <dali/integration-api/platform-abstraction.h>
32 #include <dali/integration-api/resource-types.h>
33 #include <dali/integration-api/resource-cache.h>
38 void GetRedOffsetAndMask(Dali::Pixel::Format pixelFormat, int& byteOffset, int& bitMask)
44 case Dali::Pixel::LA88:
51 case Dali::Pixel::RGB888:
52 case Dali::Pixel::RGB8888:
53 case Dali::Pixel::RGBA8888:
59 case Dali::Pixel::BGR8888:
60 case Dali::Pixel::BGRA8888:
66 case Dali::Pixel::RGB565:
72 case Dali::Pixel::BGR565:
79 case Dali::Pixel::RGBA4444:
85 case Dali::Pixel::BGRA4444:
92 case Dali::Pixel::RGBA5551:
99 case Dali::Pixel::BGRA5551:
106 case Dali::Pixel::INVALID:
107 case Dali::Pixel::COMPRESSED_R11_EAC:
108 case Dali::Pixel::COMPRESSED_SIGNED_R11_EAC:
109 case Dali::Pixel::COMPRESSED_RG11_EAC:
110 case Dali::Pixel::COMPRESSED_SIGNED_RG11_EAC:
111 case Dali::Pixel::COMPRESSED_RGB8_ETC2:
112 case Dali::Pixel::COMPRESSED_SRGB8_ETC2:
113 case Dali::Pixel::COMPRESSED_RGB8_ETC1:
114 case Dali::Pixel::COMPRESSED_RGB_PVRTC_4BPPV1:
115 case Dali::Pixel::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
116 case Dali::Pixel::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
117 case Dali::Pixel::COMPRESSED_RGBA8_ETC2_EAC:
118 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
119 case Dali::Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR:
120 case Dali::Pixel::COMPRESSED_RGBA_ASTC_5x4_KHR:
121 case Dali::Pixel::COMPRESSED_RGBA_ASTC_5x5_KHR:
122 case Dali::Pixel::COMPRESSED_RGBA_ASTC_6x5_KHR:
123 case Dali::Pixel::COMPRESSED_RGBA_ASTC_6x6_KHR:
124 case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x5_KHR:
125 case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x6_KHR:
126 case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x8_KHR:
127 case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x5_KHR:
128 case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x6_KHR:
129 case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x8_KHR:
130 case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x10_KHR:
131 case Dali::Pixel::COMPRESSED_RGBA_ASTC_12x10_KHR:
132 case Dali::Pixel::COMPRESSED_RGBA_ASTC_12x12_KHR:
133 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
134 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
135 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
136 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
137 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
138 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
139 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
140 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
141 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
142 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
143 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
144 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
145 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
146 case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
148 DALI_LOG_ERROR("Pixel formats for compressed images are not compatible with simple masking-out of per-pixel alpha.\n");
156 } // anonymous namespace
166 TypeRegistration mType( typeid( Dali::NinePatchImage ), typeid( Dali::Image ), NULL );
167 } // unnamed namespace
169 NinePatchImagePtr NinePatchImage::New( const std::string& filename )
171 Internal::NinePatchImagePtr internal( new NinePatchImage( filename ) );
172 internal->Initialize();
176 NinePatchImage::NinePatchImage( const std::string& filename )
180 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
181 mResourceClient = &tls.GetResourceClient();
183 Integration::PlatformAbstraction& platformAbstraction = tls.GetPlatformAbstraction();
184 Integration::BitmapResourceType resourceType;
186 // Note, bitmap is only destroyed when the image is destroyed.
187 Integration::ResourcePointer resource = platformAbstraction.LoadResourceSynchronously( resourceType, filename );
190 mBitmap = static_cast<Integration::Bitmap*>( resource.Get());
191 mWidth = mBitmap->GetImageWidth();
192 mHeight = mBitmap->GetImageHeight();
202 NinePatchImage* NinePatchImage::DownCast( Image* image)
204 return dynamic_cast<NinePatchImage*>(image);
207 NinePatchImage::~NinePatchImage()
211 const NinePatchImage::StretchRanges& NinePatchImage::GetStretchPixelsX()
213 if( ! mParsedBorder )
217 return mStretchPixelsX;
220 const NinePatchImage::StretchRanges& NinePatchImage::GetStretchPixelsY()
222 if( ! mParsedBorder )
226 return mStretchPixelsY;
229 Rect<int> NinePatchImage::GetChildRectangle()
231 if( ! mParsedBorder )
235 return mChildRectangle;
238 Internal::BufferImagePtr NinePatchImage::CreateCroppedBufferImage()
240 BufferImagePtr cropped;
244 DALI_LOG_ERROR( "NinePatchImage: Bitmap not loaded, cannot perform operation\n");
248 Pixel::Format pixelFormat = mBitmap->GetPixelFormat();
250 cropped = BufferImage::New( mWidth-2, mHeight-2, pixelFormat );
252 Integration::Bitmap::PackedPixelsProfile* srcProfile = mBitmap->GetPackedPixelsProfile();
253 DALI_ASSERT_DEBUG( srcProfile && "Wrong profile for source bitmap");
257 PixelBuffer* destPixels = cropped->GetBuffer();
258 unsigned int destStride = cropped->GetBufferStride();
259 unsigned int pixelWidth = GetBytesPerPixel(pixelFormat);
261 PixelBuffer* srcPixels = mBitmap->GetBuffer();
262 unsigned int srcStride = srcProfile->GetBufferStride();
264 for( unsigned int row=1; row < mHeight-1; ++row )
266 PixelBuffer* src = srcPixels + row*srcStride + pixelWidth;
267 PixelBuffer* dest = destPixels + (row-1)*destStride;
268 memcpy(dest, src, destStride );
273 cropped->Update(area); // default area has no width or height
278 void NinePatchImage::Connect()
284 const ImageTicketPtr& t = mResourceClient->AddBitmapImage(mBitmap.Get());
286 mTicket->AddObserver(*this);
293 void NinePatchImage::Disconnect()
295 if( mConnectionCount > 0 )
302 void NinePatchImage::ParseBorders()
306 DALI_LOG_ERROR( "NinePatchImage: Bitmap not loaded, cannot perform operation\n");
310 mStretchPixelsX.Clear();
311 mStretchPixelsY.Clear();
313 Pixel::Format pixelFormat = mBitmap->GetPixelFormat();
315 const Integration::Bitmap::PackedPixelsProfile* srcProfile = mBitmap->GetPackedPixelsProfile();
316 DALI_ASSERT_DEBUG( srcProfile && "Wrong profile for source bitmap" );
322 Pixel::GetAlphaOffsetAndMask( pixelFormat, alphaByte, alphaBits );
324 int testByte = alphaByte;
325 int testBits = alphaBits;
326 int testValue = alphaBits; // Opaque == stretch
329 GetRedOffsetAndMask( pixelFormat, testByte, testBits );
330 testValue = 0; // Black == stretch
333 unsigned int pixelWidth = GetBytesPerPixel( pixelFormat );
334 const PixelBuffer* srcPixels = mBitmap->GetBuffer();
335 unsigned int srcStride = srcProfile->GetBufferStride();
338 const PixelBuffer* top = srcPixels + pixelWidth;
339 unsigned int index = 0;
340 unsigned int width = mBitmap->GetImageWidth();
341 unsigned int height = mBitmap->GetImageHeight();
343 for(; index < width - 2; )
345 Uint16Pair range = ParseRange( index, width - 2, top, pixelWidth, testByte, testBits, testValue );
346 if( range.GetX() != 0xFFFF )
348 mStretchPixelsX.PushBack( range );
353 const PixelBuffer* left = srcPixels + srcStride;
355 for(; index < height - 2; )
357 Uint16Pair range = ParseRange( index, height - 2, left, srcStride, testByte, testBits, testValue );
358 if( range.GetX() != 0xFFFF )
360 mStretchPixelsY.PushBack( range );
364 //If there are no stretch pixels then make the entire image stretchable
365 if( mStretchPixelsX.Size() == 0 )
367 mStretchPixelsX.PushBack( Uint16Pair( 0, width - 2 ) );
369 if( mStretchPixelsY.Size() == 0 )
371 mStretchPixelsY.PushBack( Uint16Pair( 0, height - 2 ) );
376 const PixelBuffer* bottom = srcPixels + ( height - 1 ) * srcStride + pixelWidth;
378 Uint16Pair contentRangeX = ParseRange( index, width - 2, bottom, pixelWidth, testByte, testBits, testValue );
379 if( contentRangeX.GetX() == 0xFFFF )
381 contentRangeX = Uint16Pair();
385 const PixelBuffer* right = srcPixels + srcStride + ( width - 1 ) * pixelWidth;
387 Uint16Pair contentRangeY = ParseRange( index, height - 2, right, srcStride, testByte, testBits, testValue );
388 if( contentRangeY.GetX() == 0xFFFF )
390 contentRangeY = Uint16Pair();
393 mChildRectangle.x = contentRangeX.GetX() + 1;
394 mChildRectangle.y = contentRangeY.GetX() + 1;
395 mChildRectangle.width = contentRangeX.GetY() - contentRangeX.GetX();
396 mChildRectangle.height = contentRangeY.GetY() - contentRangeY.GetX();
398 mParsedBorder = true;
402 Uint16Pair NinePatchImage::ParseRange( unsigned int& index, unsigned int width, const PixelBuffer* & pixel, unsigned int pixelStride, int testByte, int testBits, int testValue )
404 unsigned int start = 0xFFFF;
405 for( ; index < width; ++index, pixel += pixelStride )
407 if( ( pixel[ testByte ] & testBits ) == testValue )
411 pixel += pixelStride;
416 unsigned int end = width;
417 for( ; index < width; ++index, pixel += pixelStride )
419 if( ( pixel[ testByte ] & testBits ) != testValue )
423 pixel += pixelStride;
428 return Uint16Pair( start, end );
431 bool NinePatchImage::IsNinePatchUrl( const std::string& url )
435 std::string::const_reverse_iterator iter = url.rbegin();
436 enum { SUFFIX, HASH, HASH_DOT, DONE } state = SUFFIX;
437 while(iter < url.rend())
447 else if(!isalnum(*iter))
455 if( *iter == '#' || *iter == '9' )
471 state = DONE; // Stop testing characters
491 } // namespace Internal