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/nine-patch-image-impl.h>
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/integration-api/bitmap.h>
24 #include <dali/internal/event/images/bitmap-external.h>
25 #include <dali/internal/event/common/thread-local-storage.h>
26 #include <dali/internal/event/resources/resource-client.h>
27 #include <dali/internal/update/manager/update-manager.h>
28 #include <dali/internal/event/images/image-factory.h>
29 #include <dali/integration-api/platform-abstraction.h>
30 #include <dali/integration-api/resource-types.h>
31 #include <dali/integration-api/resource-cache.h>
36 void GetRedOffsetAndMask(Dali::Pixel::Format pixelFormat, int& byteOffset, int& bitMask)
42 case Dali::Pixel::LA88:
49 case Dali::Pixel::RGB888:
50 case Dali::Pixel::RGB8888:
51 case Dali::Pixel::RGBA8888:
57 case Dali::Pixel::BGR8888:
58 case Dali::Pixel::BGRA8888:
64 case Dali::Pixel::RGB565:
70 case Dali::Pixel::BGR565:
77 case Dali::Pixel::RGBA4444:
83 case Dali::Pixel::BGRA4444:
90 case Dali::Pixel::RGBA5551:
97 case Dali::Pixel::BGRA5551:
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:
117 DALI_LOG_ERROR("Pixel formats for compressed images are not compatible with simple masking-out of per-pixel alpha.\n");
125 } // anonymous namespace
135 TypeRegistration mType( typeid( Dali::NinePatchImage ), typeid( Dali::Image ), NULL );
136 } // unnamed namespace
138 NinePatchImagePtr NinePatchImage::New( const std::string& filename, const Dali::ImageAttributes& attributes, LoadPolicy loadPol, ReleasePolicy releasePol )
140 Internal::NinePatchImagePtr internal( new NinePatchImage( filename, attributes, loadPol, releasePol ) );
141 internal->Initialize();
145 NinePatchImage::NinePatchImage( const std::string& filename, const Dali::ImageAttributes& attributes, LoadPolicy loadPol, ReleasePolicy releasePol)
146 : Image(Dali::Image::Immediate, Dali::Image::Never),
149 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
150 mResourceClient = &tls.GetResourceClient();
152 Integration::PlatformAbstraction& platformAbstraction = Internal::ThreadLocalStorage::Get().GetPlatformAbstraction();
155 platformAbstraction.GetClosestImageSize( filename, attributes, closestSize );
156 ImageAttributes loadedAttrs;
157 loadedAttrs.SetSize( closestSize );
158 mWidth = closestSize.width;
159 mHeight = closestSize.height;
160 mNaturalSizeSet = true;
162 Integration::BitmapResourceType resourceType( loadedAttrs );
164 // Note, bitmap is only destroyed when the image is destroyed.
165 Integration::ResourcePointer resource = platformAbstraction.LoadResourceSynchronously(resourceType, filename);
168 mBitmap = static_cast<Integration::Bitmap*>( resource.Get());
176 NinePatchImage* NinePatchImage::GetNinePatchImage( Image* image)
178 return dynamic_cast<NinePatchImage*>(image);
181 NinePatchImage::~NinePatchImage()
185 Vector4 NinePatchImage::GetStretchBorders()
187 if( ! mParsedBorder )
191 return mStretchBorders;
194 Rect<int> NinePatchImage::GetChildRectangle()
196 if( ! mParsedBorder )
200 return mChildRectangle;
203 Internal::BitmapImagePtr NinePatchImage::CreateCroppedBitmapImage()
205 BitmapImagePtr cropped;
209 DALI_LOG_ERROR( "NinePatchImage: Bitmap not loaded, cannot perform operation\n");
213 Pixel::Format pixelFormat = mBitmap->GetPixelFormat();
215 cropped = BitmapImage::New( mWidth-2, mHeight-2, pixelFormat, Dali::Image::Immediate, Dali::Image::Never );
217 Integration::Bitmap::PackedPixelsProfile* srcProfile = mBitmap->GetPackedPixelsProfile();
218 DALI_ASSERT_DEBUG( srcProfile && "Wrong profile for source bitmap");
222 PixelBuffer* destPixels = cropped->GetBuffer();
223 unsigned int destStride = cropped->GetBufferStride();
224 unsigned int pixelWidth = GetBytesPerPixel(pixelFormat);
226 PixelBuffer* srcPixels = mBitmap->GetBuffer();
227 unsigned int srcStride = srcProfile->GetBufferStride();
229 for( unsigned int row=1; row < mHeight-1; ++row )
231 PixelBuffer* src = srcPixels + row*srcStride + pixelWidth;
232 PixelBuffer* dest = destPixels + (row-1)*destStride;
233 memcpy(dest, src, destStride );
238 cropped->Update(area); // default area has no width or height
243 void NinePatchImage::Connect()
249 const ImageTicketPtr& t = mResourceClient->AddBitmapImage(mBitmap.Get());
251 mTicket->AddObserver(*this);
258 void NinePatchImage::Disconnect()
260 if( mConnectionCount > 0 )
267 void NinePatchImage::ParseBorders()
271 DALI_LOG_ERROR( "NinePatchImage: Bitmap not loaded, cannot perform operation\n");
275 Pixel::Format pixelFormat = mBitmap->GetPixelFormat();
277 Integration::Bitmap::PackedPixelsProfile* srcProfile = mBitmap->GetPackedPixelsProfile();
278 DALI_ASSERT_DEBUG( srcProfile && "Wrong profile for source bitmap");
282 unsigned int pixelWidth = GetBytesPerPixel(pixelFormat);
283 PixelBuffer* srcPixels = mBitmap->GetBuffer();
284 unsigned int srcStride = srcProfile->GetBufferStride();
288 Pixel::GetAlphaOffsetAndMask(pixelFormat, alphaByte, alphaBits);
291 GetRedOffsetAndMask(pixelFormat, redByte, redBits);
293 int testByte = alphaByte;
294 int testBits = alphaBits;
295 int testValue = alphaBits; // Opaque == stretch
300 testValue = 0; // Black == stretch
312 PixelBuffer* top = srcPixels + pixelWidth;
313 PixelBuffer* bottom = srcPixels + (mHeight-1)*srcStride + pixelWidth;
315 // Read the top and bottom rows:
316 // (Also read the last column to ensure end value gets set)
317 for( unsigned int col=1; col < mWidth; ++col )
319 if( (top[testByte] & testBits) == testValue )
326 else if(startX1 >= 0 && endX1 < 0)
332 if( (bottom[testByte] & testBits) == testValue )
339 else if(startX2 >= 0 && endX2 < 0)
348 // Read the left and right columns:
349 PixelBuffer* left = srcPixels + srcStride;
350 PixelBuffer* right = left + (srcStride - pixelWidth);
352 // (Also read the last row to ensure end value gets set)
353 for( unsigned int row=1; row < mHeight; ++row )
355 if((left[testByte] & testBits) == testValue)
362 else if(startY1 >= 0 && endY1 < 0)
368 if((right[testByte] & testBits) == testValue)
375 else if(startY2 >= 0 && endY2 < 0)
384 mStretchBorders.x = startX1-1;
385 mStretchBorders.y = startY1-1;
386 mStretchBorders.z = mWidth-endX1-1;
387 mStretchBorders.w = mHeight-endY1-1;
389 mChildRectangle.x = startX2-1;
390 mChildRectangle.y = startY2-1;
391 mChildRectangle.width = endX2-startX2;
392 mChildRectangle.height = endY2-startY2;
394 mParsedBorder = true;
398 } // namespace Internal