Changed alpha mask scaling to use Lanczos.
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor / utc-Dali-PixelBuffer.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <dali/dali.h>
18 #include <dali-test-suite-utils.h>
19 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
20 #include "mesh-builder.h"
21 using namespace Dali;
22
23 void utc_dali_pixelbuffer_startup(void)
24 {
25   test_return_value = TET_UNDEF;
26 }
27
28 void utc_dali_pixelbuffer_cleanup(void)
29 {
30   test_return_value = TET_PASS;
31 }
32
33 int UtcDaliPixelBufferCreatePixelData(void)
34 {
35   TestApplication application;
36
37   unsigned int width = 20u;
38   unsigned int height = 20u;
39   Devel::PixelBuffer imageData = Devel::PixelBuffer::New( width, height, Pixel::RGB888 );
40
41   PixelData pixelData = imageData.CreatePixelData();
42
43   DALI_TEST_EQUALS( true, (bool)pixelData, TEST_LOCATION );
44
45   END_TEST;
46 }
47
48
49 void Mask1stQuadrant( Devel::PixelBuffer maskData )
50 {
51   int width = maskData.GetWidth();
52   int height = maskData.GetHeight();
53   Pixel::Format pixelFormat = maskData.GetPixelFormat();
54   int bpp = Pixel::GetBytesPerPixel(pixelFormat);
55
56   unsigned char* maskBuffer = maskData.GetBuffer();
57   memset( maskBuffer, 0, width*height*bpp );
58   int offset=0;
59   for( int x=0; x<width; ++x)
60   {
61     for( int y=0; y<height; ++y)
62     {
63       if(x>=width/2 || y>=height/2)
64       {
65         for(int b=0;b<bpp;++b)
66         {
67           maskBuffer[offset+b] = 0xff;
68         }
69       }
70       offset+=bpp;
71     }
72   }
73 }
74
75 void FillCheckerboard( Devel::PixelBuffer imageData )
76 {
77   int width = imageData.GetWidth();
78   int height = imageData.GetHeight();
79   Pixel::Format pixelFormat = imageData.GetPixelFormat();
80   int bpp = Pixel::GetBytesPerPixel(pixelFormat);
81
82   unsigned char* imageBuffer = imageData.GetBuffer();
83   memset( imageBuffer, 0, width*height*bpp );
84   int offset=0;
85   for( int x=0; x<width; ++x)
86   {
87     for( int y=0; y<height; ++y)
88     {
89       // on even lines, odd pixels, or on odd lines, even pixels
90       if( (x%2 && y%2==0) || (x%2==0 && y%2) )
91       {
92         switch(pixelFormat)
93         {
94           case Pixel::RGBA5551:
95             imageBuffer[offset] = 0xFF;
96             imageBuffer[offset+1] = 0xFF;
97             break;
98           case Pixel::RGBA4444:
99             imageBuffer[offset] = 0xFF;
100             imageBuffer[offset+1] = 0xFF;
101             break;
102           case Pixel::RGB565:
103             imageBuffer[offset] = 0xFF;
104             imageBuffer[offset+1] = 0xFF;
105             break;
106           case Pixel::RGB888:
107             imageBuffer[offset] = 0xFF;
108             imageBuffer[offset+1] = 0xFF;
109             imageBuffer[offset+2] = 0xFF;
110             break;
111           case Pixel::RGBA8888:
112             imageBuffer[offset] = 0xFF;
113             imageBuffer[offset+1] = 0xFF;
114             imageBuffer[offset+2] = 0xFF;
115             imageBuffer[offset+3] = 0xFF;
116             break;
117           default:
118             break;
119         }
120       }
121       offset+=bpp;
122     }
123   }
124 }
125
126 int UtcDaliPixelBufferNew01P(void)
127 {
128   TestApplication application;
129   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New( 10, 10, Pixel::RGBA8888 );
130   DALI_TEST_CHECK( pixbuf );
131   DALI_TEST_CHECK( pixbuf.GetBuffer() != NULL );
132   END_TEST;
133 }
134
135 int UtcDaliPixelBufferNew01N(void)
136 {
137   TestApplication application;
138   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New( 0, 0, Pixel::RGBA8888 );
139   DALI_TEST_CHECK( pixbuf );
140   DALI_TEST_CHECK( pixbuf.GetBuffer() == NULL );
141   END_TEST;
142 }
143
144 int UtcDaliPixelBufferConvert(void)
145 {
146   TestApplication application;
147   TestGlAbstraction& gl=application.GetGlAbstraction();
148   TraceCallStack& textureTrace=gl.GetTextureTrace();
149   textureTrace.Enable(true);
150
151   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New( 10, 10, Pixel::RGB565 );
152   FillCheckerboard(pixbuf);
153
154   {
155     Devel::PixelBuffer pixbufPrime = pixbuf; // store a second handle to the data
156
157     Dali::PixelData pixelData = Devel::PixelBuffer::Convert( pixbuf );
158     DALI_TEST_CHECK( !pixbuf );
159
160     // check the buffer in the second handle is empty
161     DALI_TEST_CHECK( pixbufPrime.GetBuffer() == NULL );
162
163     DALI_TEST_CHECK( pixelData );
164     DALI_TEST_EQUALS( pixelData.GetWidth(), 10, TEST_LOCATION );
165     DALI_TEST_EQUALS( pixelData.GetHeight(), 10, TEST_LOCATION );
166     DALI_TEST_EQUALS( pixelData.GetPixelFormat(), Pixel::RGB565, TEST_LOCATION );
167
168     // Try drawing it
169     Texture t = Texture::New(TextureType::TEXTURE_2D, Pixel::RGB565, 10, 10);
170     t.Upload( pixelData );
171     TextureSet ts = TextureSet::New();
172     ts.SetTexture(0, t);
173     Geometry g = CreateQuadGeometry();
174     Shader s = Shader::New("v", "f");
175     Renderer r = Renderer::New( g, s );
176     r.SetTextures(ts);
177     Actor a = Actor::New();
178     a.AddRenderer(r);
179     a.SetSize(10, 10);
180     a.SetParentOrigin(ParentOrigin::CENTER);
181     Stage::GetCurrent().Add(a);
182
183     application.SendNotification();
184     application.Render();
185     DALI_TEST_EQUALS( textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION );
186
187     // Let secondary scope destroy pixbufPrime
188   }
189
190   END_TEST;
191 }
192
193 int UtcDaliPixelBufferGetWidth(void)
194 {
195   TestApplication application;
196   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New( 10, 10, Pixel::RGB565 );
197   FillCheckerboard(pixbuf);
198
199   DALI_TEST_EQUALS( pixbuf.GetWidth(), 10, TEST_LOCATION ) ;
200
201   END_TEST;
202 }
203
204 int UtcDaliPixelBufferGetHeight(void)
205 {
206   TestApplication application;
207   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New( 10, 10, Pixel::RGB565 );
208   FillCheckerboard(pixbuf);
209
210   DALI_TEST_EQUALS( pixbuf.GetHeight(), 10, TEST_LOCATION ) ;
211
212   END_TEST;
213 }
214
215 int UtcDaliPixelBufferGetPixelFormat(void)
216 {
217   TestApplication application;
218   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New( 10, 10, Pixel::RGB565 );
219   FillCheckerboard(pixbuf);
220
221   DALI_TEST_EQUALS( pixbuf.GetPixelFormat(), Pixel::RGB565, TEST_LOCATION ) ;
222
223   END_TEST;
224 }
225
226
227
228 int UtcDaliPixelBufferMask01(void)
229 {
230   TestApplication application;
231
232   unsigned int width = 10u;
233   unsigned int height = 10u;
234   Pixel::Format pixelFormat = Pixel::L8;
235   Devel::PixelBuffer maskData = Devel::PixelBuffer::New( width, height, pixelFormat );
236
237   Mask1stQuadrant(maskData);
238
239   width = 20u;
240   height = 20u;
241   pixelFormat = Pixel::RGBA5551;
242
243   Devel::PixelBuffer imageData = Devel::PixelBuffer::New( width, height, pixelFormat );
244   FillCheckerboard(imageData);
245
246   imageData.ApplyMask( maskData );
247
248   // Test that the pixel format has been promoted to RGBA8888
249   DALI_TEST_EQUALS( imageData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION );
250
251   // Test that a pixel in the first quadrant has no alpha value
252   unsigned char* buffer = imageData.GetBuffer();
253   DALI_TEST_EQUALS( buffer[3], 0x00u, TEST_LOCATION );
254   DALI_TEST_EQUALS( buffer[7], 0x00u, TEST_LOCATION );
255
256   // Test that an even pixel in the second quadrant has a full alpha value
257   DALI_TEST_EQUALS( buffer[43], 0x00u, TEST_LOCATION );
258
259   // Test that an odd pixel in the second quadrant has full alpha value
260   DALI_TEST_EQUALS( buffer[47], 0xffu, TEST_LOCATION );
261
262   END_TEST;
263 }
264
265
266 int UtcDaliPixelBufferMask02(void)
267 {
268   TestApplication application;
269
270   unsigned int width = 10u;
271   unsigned int height = 10u;
272   Pixel::Format pixelFormat = Pixel::L8;
273   Devel::PixelBuffer maskData = Devel::PixelBuffer::New( width, height, pixelFormat );
274
275   Mask1stQuadrant(maskData);
276
277   width = 20u;
278   height = 20u;
279   pixelFormat = Pixel::RGBA4444;
280
281   Devel::PixelBuffer imageData = Devel::PixelBuffer::New( width, height, pixelFormat );
282   FillCheckerboard(imageData);
283
284   imageData.ApplyMask( maskData );
285
286   // Test that the pixel format has been promoted to RGBA8888
287   DALI_TEST_EQUALS( imageData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION );
288
289   // Test that a pixel in the first quadrant has no alpha value
290   unsigned char* buffer = imageData.GetBuffer();
291   DALI_TEST_EQUALS( buffer[3], 0x00u, TEST_LOCATION );
292   DALI_TEST_EQUALS( buffer[7], 0x00u, TEST_LOCATION );
293
294   // Test that an even pixel in the second quadrant has no alpha value
295   DALI_TEST_EQUALS( buffer[43], 0x00u, TEST_LOCATION );
296
297   // Test that an odd pixel in the second quadrant has full alpha value
298   DALI_TEST_EQUALS( buffer[47], 0xffu, TEST_LOCATION );
299
300   END_TEST;
301 }
302
303 int UtcDaliPixelBufferMask03(void)
304 {
305   TestApplication application;
306   tet_infoline("Test application of alpha mask to smaller RGB565 image");
307
308   unsigned int width = 20u;
309   unsigned int height = 20u;
310   Devel::PixelBuffer maskData = Devel::PixelBuffer::New( width, height, Pixel::L8 );
311   Mask1stQuadrant(maskData);
312
313   width = 10u;
314   height = 10u;
315   Pixel::Format format = Pixel::RGB565;
316   Devel::PixelBuffer imageData = Devel::PixelBuffer::New( width, height, format );
317   FillCheckerboard(imageData);
318
319   imageData.ApplyMask( maskData );
320
321   // Test that the pixel format has been promoted to RGBA8888
322   DALI_TEST_EQUALS( imageData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION );
323
324   // Test that a pixel in the first quadrant has no alpha value
325   unsigned char* buffer = imageData.GetBuffer();
326   DALI_TEST_EQUALS( buffer[3], 0x00u, TEST_LOCATION );
327   DALI_TEST_EQUALS( buffer[7], 0x00u, TEST_LOCATION );
328
329   // Test that an odd pixel in the fourth quadrant has full alpha value
330   DALI_TEST_EQUALS( buffer[(6*10+7)*4+3], 0xffu, TEST_LOCATION );
331
332   // Test that an even pixel in the fourth quadrant has full alpha value
333   DALI_TEST_EQUALS( buffer[(6*10+8)*4+3], 0xffu, TEST_LOCATION );
334
335   END_TEST;
336 }
337
338
339 int UtcDaliPixelBufferMask04(void)
340 {
341   TestApplication application;
342   tet_infoline("Test application of alpha mask to larger RGBA8888 image");
343
344   unsigned int width = 10u;
345   unsigned int height = 10u;
346   Devel::PixelBuffer maskData = Devel::PixelBuffer::New( width, height, Pixel::L8 );
347   Mask1stQuadrant(maskData);
348
349   width = 20u;
350   height = 20u;
351   Devel::PixelBuffer imageData = Devel::PixelBuffer::New( width, height, Pixel::RGBA8888 );
352   FillCheckerboard(imageData);
353
354   imageData.ApplyMask( maskData );
355
356   // Test that the pixel format has been promoted to RGBA8888
357   DALI_TEST_EQUALS( imageData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION );
358
359   // Test that a pixel in the first quadrant has no alpha value
360   unsigned char* buffer = imageData.GetBuffer();
361   DALI_TEST_EQUALS( buffer[3], 0x00u, TEST_LOCATION );
362   DALI_TEST_EQUALS( buffer[7], 0x00u, TEST_LOCATION );
363
364   // Test that an even pixel in the second quadrant has no alpha value
365   DALI_TEST_EQUALS( buffer[43], 0x00u, TEST_LOCATION );
366
367   // Test that an odd pixel in the second quadrant has full alpha value
368   DALI_TEST_EQUALS( buffer[47], 0xffu, TEST_LOCATION );
369
370   END_TEST;
371 }
372
373 int UtcDaliPixelBufferMask05(void)
374 {
375   TestApplication application;
376   tet_infoline("Test application of alpha mask to smaller RGBA8888 image");
377
378   unsigned int width = 20u;
379   unsigned int height = 20u;
380   Devel::PixelBuffer maskData = Devel::PixelBuffer::New( width, height, Pixel::RGBA8888 );
381   Mask1stQuadrant(maskData);
382
383   width = 10u;
384   height = 10u;
385   Devel::PixelBuffer imageData = Devel::PixelBuffer::New( width, height, Pixel::RGBA8888 );
386   FillCheckerboard(imageData);
387
388   imageData.ApplyMask( maskData );
389
390   // Test that the pixel format has been promoted to RGBA8888
391   DALI_TEST_EQUALS( imageData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION );
392
393   // Test that a pixel in the first quadrant has no alpha value
394   unsigned char* buffer = imageData.GetBuffer();
395   DALI_TEST_EQUALS( buffer[3], 0x00u, TEST_LOCATION );
396   DALI_TEST_EQUALS( buffer[7], 0x00u, TEST_LOCATION );
397
398   // Test that an odd pixel in the second quadrant has full alpha value
399   DALI_TEST_EQUALS( buffer[39], 0xffu, TEST_LOCATION );
400
401   // Test that an even pixel in the second quadrant has no alpha value
402   DALI_TEST_EQUALS( buffer[27], 0x00u, TEST_LOCATION );
403
404   END_TEST;
405 }
406
407 int UtcDaliPixelBufferMask06(void)
408 {
409   TestApplication application;
410   tet_infoline("Test application of alpha mask to same size RGBA8888 image");
411
412   unsigned int width = 10u;
413   unsigned int height = 10u;
414   Devel::PixelBuffer maskData = Devel::PixelBuffer::New( width, height, Pixel::RGBA8888 );
415   Mask1stQuadrant(maskData);
416
417   width = 10u;
418   height = 10u;
419   Devel::PixelBuffer imageData = Devel::PixelBuffer::New( width, height, Pixel::RGBA8888 );
420   FillCheckerboard(imageData);
421
422   imageData.ApplyMask( maskData );
423
424   // Test that the pixel format has been promoted to RGBA8888
425   DALI_TEST_EQUALS( imageData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION );
426
427   // Test that a pixel in the first quadrant has no alpha value
428   unsigned char* buffer = imageData.GetBuffer();
429   DALI_TEST_EQUALS( buffer[3], 0x00u, TEST_LOCATION );
430   DALI_TEST_EQUALS( buffer[7], 0x00u, TEST_LOCATION );
431
432   // Test that an odd pixel in the second quadrant has full alpha value
433   DALI_TEST_EQUALS( buffer[39], 0xffu, TEST_LOCATION );
434
435   // Test that an even pixel in the second quadrant has no alpha value
436   DALI_TEST_EQUALS( buffer[27], 0x00u, TEST_LOCATION );
437
438   END_TEST;
439 }