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.
21 #include <dali/public-api/dali-core.h>
22 #include <dali/devel-api/images/nine-patch-image.h>
23 #include <dali-test-suite-utils.h>
29 Integration::Bitmap* CreateBitmap( unsigned int imageWidth, unsigned int imageHeight, unsigned int initialColor, Pixel::Format pixelFormat )
31 Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
32 Integration::PixelBuffer* pixbuffer = bitmap->GetPackedPixelsProfile()->ReserveBuffer( pixelFormat, imageWidth, imageHeight, imageWidth, imageHeight );
33 unsigned int bytesPerPixel = GetBytesPerPixel( pixelFormat );
35 memset( pixbuffer, initialColor, imageHeight * imageWidth * bytesPerPixel );
40 void InitialiseRegionsToZeroAlpha( Integration::Bitmap* image, unsigned int imageWidth, unsigned int imageHeight, Pixel::Format pixelFormat )
42 PixelBuffer* pixbuffer = image->GetBuffer();
43 unsigned int bytesPerPixel = GetBytesPerPixel( pixelFormat );
45 for( unsigned int row = 0; row < imageWidth; ++row )
47 unsigned int pixelOffset = row * bytesPerPixel;
48 pixbuffer[ pixelOffset + 3 ] = 0x00;
49 pixelOffset += ( imageHeight - 1 ) * imageWidth * bytesPerPixel;
50 pixbuffer[ pixelOffset + 3 ] = 0x00;
53 for ( unsigned int column = 0; column < imageHeight; ++column )
55 unsigned int pixelOffset = column * imageWidth * bytesPerPixel;
56 pixbuffer[ pixelOffset + 3 ] = 0x00;
57 pixelOffset += ( imageWidth -1 ) * bytesPerPixel;
58 pixbuffer[ pixelOffset + 3 ] = 0x00;
62 void AddStretchRegionsToImage( Integration::Bitmap* image, unsigned int imageWidth, unsigned int imageHeight, const Vector4& requiredStretchBorder, Pixel::Format pixelFormat )
64 PixelBuffer* pixbuffer = image->GetBuffer();
65 unsigned int bytesPerPixel = GetBytesPerPixel( pixelFormat );
67 for( unsigned int column = requiredStretchBorder.x; column < imageWidth - requiredStretchBorder.z; ++column )
69 unsigned int pixelOffset = column * bytesPerPixel;
70 pixbuffer[ pixelOffset ] = 0x00;
71 pixbuffer[ pixelOffset + 1 ] = 0x00;
72 pixbuffer[ pixelOffset + 2 ] = 0x00;
73 pixbuffer[ pixelOffset + 3 ] = 0xFF;
76 for( unsigned int row = requiredStretchBorder.y; row < imageHeight - requiredStretchBorder.w; ++row )
78 unsigned int pixelOffset = row * imageWidth * bytesPerPixel;
79 pixbuffer[ pixelOffset ] = 0x00;
80 pixbuffer[ pixelOffset + 1 ] = 0x00;
81 pixbuffer[ pixelOffset + 2 ] = 0x00;
82 pixbuffer[ pixelOffset + 3 ] = 0xFF;
86 void AddChildRegionsToImage( Integration::Bitmap* image, unsigned int imageWidth, unsigned int imageHeight, const Vector4& requiredChildRegion, Pixel::Format pixelFormat )
88 PixelBuffer* pixbuffer = image->GetBuffer();
89 unsigned int bytesPerPixel = GetBytesPerPixel( pixelFormat );
91 Integration::Bitmap::PackedPixelsProfile* srcProfile = image->GetPackedPixelsProfile();
92 unsigned int bufferStride = srcProfile->GetBufferStride();
94 // Add bottom child region
95 for( unsigned int column = requiredChildRegion.x; column < imageWidth - requiredChildRegion.z; ++column )
97 unsigned int pixelOffset = column * bytesPerPixel;
98 pixelOffset += ( imageHeight - 1 ) * bufferStride;
99 pixbuffer[ pixelOffset ] = 0x00;
100 pixbuffer[ pixelOffset + 1 ] = 0x00;
101 pixbuffer[ pixelOffset + 2 ] = 0x00;
102 pixbuffer[ pixelOffset + 3 ] = 0xFF;
105 // Add right child region
106 for ( unsigned int row = requiredChildRegion.y; row < imageHeight - requiredChildRegion.w; ++row )
108 unsigned int pixelOffset = row * bufferStride + ( imageWidth - 1 ) * bytesPerPixel;
109 pixbuffer[ pixelOffset ] = 0x00;
110 pixbuffer[ pixelOffset + 1 ] = 0x00;
111 pixbuffer[ pixelOffset + 2 ] = 0x00;
112 pixbuffer[ pixelOffset + 3 ] = 0xFF;
116 NinePatchImage CustomizeNinePatch( TestApplication& application,
117 unsigned int ninePatchImageWidth,
118 unsigned int ninePatchImageHeight,
119 const Vector4& requiredStretchBorder,
120 bool addChildRegion = false,
121 Vector4 requiredChildRegion = Vector4::ZERO )
123 TestPlatformAbstraction& platform = application.GetPlatform();
125 Pixel::Format pixelFormat = Pixel::RGBA8888;
127 tet_infoline("Create Bitmap");
128 platform.SetClosestImageSize(Vector2( ninePatchImageWidth, ninePatchImageHeight));
129 Integration::Bitmap* bitmap = CreateBitmap( ninePatchImageWidth, ninePatchImageHeight, 0xFF, pixelFormat );
131 tet_infoline("Clear border regions");
132 InitialiseRegionsToZeroAlpha( bitmap, ninePatchImageWidth, ninePatchImageHeight, pixelFormat );
134 tet_infoline("Add Stretch regions to Bitmap");
135 AddStretchRegionsToImage( bitmap, ninePatchImageWidth, ninePatchImageHeight, requiredStretchBorder, pixelFormat );
139 tet_infoline("Add Child regions to Bitmap");
140 AddChildRegionsToImage( bitmap, ninePatchImageWidth, ninePatchImageHeight, requiredChildRegion, pixelFormat );
143 tet_infoline("Setting resource as it's loaded synchronously");
144 Integration::ResourcePointer resourcePtr(bitmap);
145 platform.SetSynchronouslyLoadedResource( resourcePtr );
147 Image image = ResourceImage::New( "blah.#.png" );
149 tet_infoline("Assign image to image rendering actor");
150 Actor actor = CreateRenderableActor( image );
151 Stage::GetCurrent().Add( actor );
153 tet_infoline("Downcast Image to a nine-patch image\n");
154 NinePatchImage ninePatchImage = NinePatchImage::DownCast( image );
156 return ninePatchImage;
162 int UtcDaliNinePatchImageNew(void)
164 TestApplication application;
165 tet_infoline("UtcDaliNinePatchImageNew - NinePatchImage::New(const std::string&)");
167 // invoke default handle constructor
168 NinePatchImage image;
170 DALI_TEST_CHECK( !image );
173 image = NinePatchImage::New( "blah.#.png" );
175 DALI_TEST_CHECK( image );
179 int UtcDaliNinePatchImageDowncast(void)
181 TestApplication application;
182 tet_infoline("UtcDaliNinePatchImageDowncast - NinePatchImage::DownCast(BaseHandle)");
184 NinePatchImage image = NinePatchImage::New( "blah.#.png" );
186 BaseHandle object( image );
188 NinePatchImage image2 = NinePatchImage::DownCast( object );
189 DALI_TEST_CHECK( image2 );
191 NinePatchImage image3 = DownCast< NinePatchImage >( object );
192 DALI_TEST_CHECK( image3 );
194 BaseHandle unInitializedObject;
195 NinePatchImage image4 = NinePatchImage::DownCast( unInitializedObject );
196 DALI_TEST_CHECK( !image4 );
198 NinePatchImage image5 = DownCast< NinePatchImage >( unInitializedObject );
199 DALI_TEST_CHECK( !image5 );
201 Image image6 = NinePatchImage::New( "blah.#.png" );
202 NinePatchImage image7 = NinePatchImage::DownCast( image6 );
203 DALI_TEST_CHECK( image7 );
207 int UtcDaliNinePatchImageCopyConstructor(void)
209 TestApplication application;
211 tet_infoline("UtcDaliNinePatchImageCopyConstructor - NinePatchImage::NinePatchImage( const NinePatchImage& )");
213 NinePatchImage image1;
214 DALI_TEST_CHECK( !image1 );
216 image1 = NinePatchImage::New( "blah.#.png" );
217 NinePatchImage image2( image1 );
219 DALI_TEST_CHECK( image2 );
220 DALI_TEST_EQUALS( image1, image2, TEST_LOCATION );
225 int UtcDaliNinePatchImageGetStrechBorders(void)
227 TestApplication application;
228 tet_infoline("UtcDaliNinePatchImageGetStrechBorders - NinePatchImage::GetStretchBorders()");
230 /* Stretch region left(2) top(2) right (2) bottom (2)
241 const unsigned int ninePatchImageHeight = 18;
242 const unsigned int ninePatchImageWidth = 28;
243 const Vector4 requiredStretchBorder( 3, 4, 5, 6 );
245 NinePatchImage ninePatchImage = CustomizeNinePatch( application, ninePatchImageWidth, ninePatchImageHeight, requiredStretchBorder );
246 DALI_TEST_CHECK( ninePatchImage );
250 tet_infoline("Get Stretch regions from NinePatch");
252 const NinePatchImage::StretchRanges& stretchPixelsX = ninePatchImage.GetStretchPixelsX();
253 const NinePatchImage::StretchRanges& stretchPixelsY = ninePatchImage.GetStretchPixelsY();
255 DALI_TEST_CHECK( stretchPixelsX.Size() == 1 );
256 DALI_TEST_CHECK( stretchPixelsY.Size() == 1 );
258 Vector4 stretchBorders;
259 //The NinePatchImage stretch pixels are in the cropped image space, inset by 1 to get it to uncropped image space
260 stretchBorders.x = stretchPixelsX[ 0 ].GetX() + 1;
261 stretchBorders.y = stretchPixelsY[ 0 ].GetX() + 1;
262 stretchBorders.z = ninePatchImageWidth - stretchPixelsX[ 0 ].GetY() - 1;
263 stretchBorders.w = ninePatchImageHeight - stretchPixelsY[ 0 ].GetY() - 1;
265 tet_printf("stretchBorders left(%f) right(%f) top(%f) bottom(%f)\n", stretchBorders.x, stretchBorders.z, stretchBorders.y, stretchBorders.w );
266 DALI_TEST_CHECK( stretchBorders == requiredStretchBorder );
268 Vector4 actualStretchBorders = ninePatchImage.GetStretchBorders();
269 DALI_TEST_EQUALS( actualStretchBorders, requiredStretchBorder, 0.001, TEST_LOCATION );
273 tet_infoline("Image not NinePatch");
274 test_return_value = TET_FAIL;
280 int UtcDaliNinePatchImageGetChildRectangle(void)
282 TestApplication application;
283 tet_infoline("UtcDaliNinePatchImageGetChildRectangle - NinePatchImage::GetChildRectangle()");
285 /* Child region x(2) y(2) width (4) height (4)
297 const unsigned int ninePatchImageHeight = 18;
298 const unsigned int ninePatchImageWidth = 28;
299 const Vector4 requiredChildRegion( 2, 2, 2, 2 );
300 const Vector4 requiredStretchBorder( 3, 4, 5, 6 );
302 NinePatchImage ninePatchImage = CustomizeNinePatch( application,ninePatchImageWidth, ninePatchImageHeight, requiredStretchBorder, true, requiredChildRegion );
303 DALI_TEST_CHECK( ninePatchImage );
305 if ( ninePatchImage )
307 tet_infoline("Get Child regions from NinePatch");
308 Rect< int > childRectangle = ninePatchImage.GetChildRectangle();
309 tet_printf("childRectange x(%d) y(%d) width(%d) height(%d)\n", childRectangle.x, childRectangle.y, childRectangle.width, childRectangle.height );
310 Rect< int > childRegion(requiredChildRegion.x, requiredChildRegion.y, ninePatchImageWidth - requiredChildRegion.x - requiredChildRegion.z, ninePatchImageHeight - requiredChildRegion.y - requiredChildRegion.w );
311 DALI_TEST_CHECK( childRegion == childRectangle );
315 tet_infoline("Image not NinePatch");
316 test_return_value = TET_FAIL;
322 int UtcDaliNinePatchImageCreateCroppedBufferImage(void)
324 TestApplication application;
325 tet_infoline("UtcDaliNinePatchImageCreateCroppedBufferImage - NinePatchImage::CreateCroppedBufferImage()");
327 const unsigned int ninePatchImageHeight = 8;
328 const unsigned int ninePatchImageWidth = 8;
329 const Vector4 requiredStretchBorder( 1, 1, 1, 1 );
331 NinePatchImage ninePatchImage = CustomizeNinePatch( application, ninePatchImageWidth, ninePatchImageHeight, requiredStretchBorder );
332 DALI_TEST_CHECK( ninePatchImage );
334 if ( ninePatchImage )
336 BufferImage newImage = ninePatchImage.CreateCroppedBufferImage();
337 DALI_TEST_CHECK( newImage );
339 DALI_TEST_EQUALS( newImage.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION );
341 DALI_TEST_EQUALS( newImage.GetBufferSize(), 144u/* 36*4 */, TEST_LOCATION );
345 tet_infoline("Image not NinePatch");
346 test_return_value = TET_FAIL;
352 int UtcDaliNinePatchImageIsNinePatchUrl(void)
354 TestApplication application;
355 tet_infoline("UtcDaliNinePatchImageIsNinePatchUrl - NinePatchImage::IsNinePatchUrl(const std::string&)");
357 DALI_TEST_CHECK( NinePatchImage::IsNinePatchUrl( "test.9.jpg" ) );
358 DALI_TEST_CHECK( NinePatchImage::IsNinePatchUrl( "test.#.jpg" ) );
359 DALI_TEST_CHECK( !NinePatchImage::IsNinePatchUrl( "test.9" ) );
360 DALI_TEST_CHECK( !NinePatchImage::IsNinePatchUrl( "test.#" ) );
361 DALI_TEST_CHECK( !NinePatchImage::IsNinePatchUrl( "test" ) );