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.
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
28 Integration::Bitmap* CreateBitmap( unsigned int imageHeight, unsigned int imageWidth, unsigned int initialColor, Pixel::Format pixelFormat )
30 Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::RETAIN );
31 Integration::PixelBuffer* pixbuffer = bitmap->GetPackedPixelsProfile()->ReserveBuffer( pixelFormat, imageWidth,imageHeight,imageWidth,imageHeight );
32 unsigned int bytesPerPixel = GetBytesPerPixel( pixelFormat );
34 memset( pixbuffer, initialColor , imageHeight*imageWidth*bytesPerPixel);
39 void InitialiseRegionsToZeroAlpha( Integration::Bitmap* image, unsigned int imageWidth, unsigned int imageHeight, Pixel::Format pixelFormat )
41 PixelBuffer* pixbuffer = image->GetBuffer();
42 unsigned int bytesPerPixel = GetBytesPerPixel( pixelFormat );
44 for ( unsigned int row = 0; row < imageWidth; ++row )
46 unsigned int pixelOffset = (row*bytesPerPixel);
47 pixbuffer[pixelOffset+3] = 0x00;
48 pixelOffset += ( imageHeight-1 ) * imageWidth * bytesPerPixel;
49 pixbuffer[pixelOffset+3] = 0x00;
52 for ( unsigned int column = 0; column < imageHeight; ++column )
54 unsigned int pixelOffset = (column*imageWidth*bytesPerPixel);
55 pixbuffer[pixelOffset+3] = 0x00;
56 pixelOffset += (imageWidth-1)*bytesPerPixel;
57 pixbuffer[pixelOffset+3] = 0x00;
61 void AddStretchRegionsToImage( Integration::Bitmap* image, unsigned int imageWidth, unsigned int imageHeight, const Vector4& requiredStretchBorder , Pixel::Format pixelFormat )
63 PixelBuffer* pixbuffer = image->GetBuffer();
64 unsigned int bytesPerPixel = GetBytesPerPixel( pixelFormat );
66 for ( unsigned int column = requiredStretchBorder.x; column < imageWidth - requiredStretchBorder.z; ++column )
68 unsigned int pixelOffset = (column*bytesPerPixel);
69 pixbuffer[pixelOffset] = 0x00;
70 pixbuffer[pixelOffset+1] = 0x00;
71 pixbuffer[pixelOffset+2] = 0x00;
72 pixbuffer[pixelOffset+3] = 0xFF;
75 for ( unsigned int row = requiredStretchBorder.y; row < imageHeight - requiredStretchBorder.w; ++row )
77 unsigned int pixelOffset = (row*imageWidth*bytesPerPixel);
78 pixbuffer[pixelOffset] = 0x00;
79 pixbuffer[pixelOffset+1] = 0x00;
80 pixbuffer[pixelOffset+2] = 0x00;
81 pixbuffer[pixelOffset+3] = 0xFF;
85 void AddChildRegionsToImage( Integration::Bitmap* image, unsigned int imageWidth, unsigned int imageHeight, const Vector4& requiredChildRegion, Pixel::Format pixelFormat )
87 PixelBuffer* pixbuffer = image->GetBuffer();
88 unsigned int bytesPerPixel = GetBytesPerPixel( pixelFormat );
90 Integration::Bitmap::PackedPixelsProfile* srcProfile = image->GetPackedPixelsProfile();
91 unsigned int bufferStride = srcProfile->GetBufferStride();
93 // Add bottom child region
94 for ( unsigned int column = requiredChildRegion.x; column < imageWidth - requiredChildRegion.z; ++column )
96 unsigned int pixelOffset = (column*bytesPerPixel);
97 pixelOffset += ( imageHeight-1 ) * bufferStride;
98 pixbuffer[pixelOffset] = 0x00;
99 pixbuffer[pixelOffset+1] = 0x00;
100 pixbuffer[pixelOffset+2] = 0x00;
101 pixbuffer[pixelOffset+3] = 0xFF;
104 // Add right child region
105 for ( unsigned int row = requiredChildRegion.y; row < imageHeight - requiredChildRegion.w; ++row )
107 unsigned int pixelOffset = row*bufferStride + (imageWidth-1)*bytesPerPixel;
108 pixbuffer[pixelOffset] = 0x00;
109 pixbuffer[pixelOffset+1] = 0x00;
110 pixbuffer[pixelOffset+2] = 0x00;
111 pixbuffer[pixelOffset+3] = 0xFF;
117 int UtcDaliNinePatch01(void)
119 /* Stretch region left(3) top(2) right (3) bottom (2)
130 TestApplication application;
131 TestPlatformAbstraction& platform = application.GetPlatform();
133 tet_infoline("UtcDaliNinePatchImage - Stretch Regions");
135 const unsigned int ninePatchImageHeight = 8;
136 const unsigned int ninePatchImageWidth = 8;
137 Pixel::Format pixelFormat = Pixel::RGBA8888;
138 const Vector4 requiredStretchBorder( 3, 3, 3, 3);
140 tet_infoline("Create Bitmap");
141 platform.SetClosestImageSize(Vector2( ninePatchImageWidth, ninePatchImageHeight));
142 Integration::Bitmap* bitmap = CreateBitmap( ninePatchImageWidth, ninePatchImageHeight, 0xFF, pixelFormat );
144 tet_infoline("Clear border regions");
145 InitialiseRegionsToZeroAlpha( bitmap, ninePatchImageWidth, ninePatchImageHeight, pixelFormat );
147 tet_infoline("Add Stretch regions to Bitmap");
148 AddStretchRegionsToImage( bitmap, ninePatchImageWidth, ninePatchImageHeight, requiredStretchBorder, pixelFormat );
150 tet_infoline("Getting resource");
151 Integration::ResourcePointer resourcePtr(bitmap);
152 platform.SetResourceLoaded( 0, Dali::Integration::ResourceBitmap, resourcePtr );
154 Image image = ResourceImage::New( "blah.#.png" );
155 DALI_TEST_CHECK( image );
157 tet_infoline("Assign image to ImageActor");
158 ImageActor imageActor = ImageActor::New( image );
159 DALI_TEST_CHECK( imageActor );
160 Stage::GetCurrent().Add( imageActor );
162 tet_infoline("Downcast Image to a nine-patch image\n");
163 NinePatchImage ninePatchImage = NinePatchImage::DownCast( image );
164 DALI_TEST_CHECK( ninePatchImage );
166 if ( ninePatchImage )
168 tet_infoline("Get Stretch regions from NinePatch");
169 Vector4 stretchBorders = ninePatchImage.GetStretchBorders();
170 tet_printf("stretchBorders left(%f) right(%f) top(%f) bottom(%f)\n", stretchBorders.x, stretchBorders.z, stretchBorders.y, stretchBorders.w );
171 DALI_TEST_CHECK( stretchBorders == requiredStretchBorder );
175 tet_infoline("Image not NinePatch");
176 test_return_value = TET_FAIL;
182 int UtcDaliNinePatch02(void)
184 /* Child region x(2) y(2) width (4) height (4)
196 TestApplication application;
197 TestPlatformAbstraction& platform = application.GetPlatform();
199 tet_infoline("UtcDaliNinePatchImage - Child Regions");
201 const unsigned int ninePatchImageHeight = 8;
202 const unsigned int ninePatchImageWidth = 8;
203 Pixel::Format pixelFormat = Pixel::RGBA8888;
204 const Vector4 requiredChildRegion( 2, 2, 2, 2 );
205 const Vector4 requiredStretchBorder( 3, 3, 3, 3);
207 tet_infoline("Create Bitmap");
208 platform.SetClosestImageSize(Vector2( ninePatchImageWidth, ninePatchImageHeight));
209 Integration::Bitmap* bitmap = CreateBitmap( ninePatchImageWidth, ninePatchImageHeight, 0xFF, pixelFormat );
211 tet_infoline("Clear border regions");
212 InitialiseRegionsToZeroAlpha( bitmap, ninePatchImageWidth, ninePatchImageHeight, pixelFormat );
214 tet_infoline("Add Stretch regions to Bitmap");
215 AddStretchRegionsToImage( bitmap, ninePatchImageWidth, ninePatchImageHeight, requiredStretchBorder, pixelFormat );
217 tet_infoline("Add Child regions to Bitmap");
218 AddChildRegionsToImage( bitmap, ninePatchImageWidth, ninePatchImageHeight, requiredChildRegion, pixelFormat );
220 tet_infoline("Getting resource");
221 Integration::ResourcePointer resourcePtr(bitmap);
222 platform.SetResourceLoaded( 0, Dali::Integration::ResourceBitmap, resourcePtr );
223 Image image = ResourceImage::New( "blah.#.png" );
224 DALI_TEST_CHECK( image );
226 tet_infoline("Assign image to ImageActor");
227 ImageActor imageActor = ImageActor::New( image );
228 DALI_TEST_CHECK( imageActor );
229 Stage::GetCurrent().Add( imageActor );
231 tet_infoline("Downcast Image to a nine-patch image\n");
232 NinePatchImage ninePatchImage = NinePatchImage::DownCast( image );
233 DALI_TEST_CHECK( ninePatchImage );
235 if ( ninePatchImage )
237 tet_infoline("Get Child regions from NinePatch");
238 Rect<int> childRectangle = ninePatchImage.GetChildRectangle();
239 tet_printf("childRectange x(%d) y(%d) width(%d) height(%d)\n", childRectangle.x, childRectangle.y, childRectangle.width, childRectangle.height );
240 Rect<int> childRegion(requiredChildRegion.x, requiredChildRegion.y, ninePatchImageWidth - requiredChildRegion.x - requiredChildRegion.z, ninePatchImageHeight - requiredChildRegion.y - requiredChildRegion.w );
241 DALI_TEST_CHECK( childRegion == childRectangle );
245 tet_infoline("Image not NinePatch");
246 test_return_value = TET_FAIL;