Formatting automated-tests
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor / utc-Dali-PixelBuffer.cpp
1 /*
2  * Copyright (c) 2020 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-test-suite-utils.h>
18 #include <dali/dali.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 void Mask1stQuadrant(Devel::PixelBuffer maskData)
49 {
50   int           width       = maskData.GetWidth();
51   int           height      = maskData.GetHeight();
52   Pixel::Format pixelFormat = maskData.GetPixelFormat();
53   int           bpp         = Pixel::GetBytesPerPixel(pixelFormat);
54
55   unsigned char* maskBuffer = maskData.GetBuffer();
56   memset(maskBuffer, 0, width * height * bpp);
57   int offset = 0;
58   for(int x = 0; x < width; ++x)
59   {
60     for(int y = 0; y < height; ++y)
61     {
62       if(x >= width / 2 || y >= height / 2)
63       {
64         for(int b = 0; b < bpp; ++b)
65         {
66           maskBuffer[offset + b] = 0xff;
67         }
68       }
69       offset += bpp;
70     }
71   }
72 }
73
74 void MaskCenterSquare(Devel::PixelBuffer maskData)
75 {
76   int           width       = maskData.GetWidth();
77   int           height      = maskData.GetHeight();
78   Pixel::Format pixelFormat = maskData.GetPixelFormat();
79   int           bpp         = Pixel::GetBytesPerPixel(pixelFormat);
80
81   unsigned char* maskBuffer = maskData.GetBuffer();
82   memset(maskBuffer, 0, width * height * bpp);
83   int offset = 0;
84   for(int y = 0; y < height; ++y)
85   {
86     for(int x = 0; x < width; ++x)
87     {
88       if(x >= width / 4 && x < 3 * width / 4 &&
89          y >= height / 4 && y < 3 * height / 4)
90       {
91         for(int b = 0; b < bpp; ++b)
92         {
93           maskBuffer[offset + b] = 0xff;
94         }
95       }
96       offset += bpp;
97     }
98   }
99 }
100
101 void AlternateQuadrants(Devel::PixelBuffer buffer)
102 {
103   int           width       = buffer.GetWidth();
104   int           height      = buffer.GetHeight();
105   Pixel::Format pixelFormat = buffer.GetPixelFormat();
106   int           bpp         = Pixel::GetBytesPerPixel(pixelFormat);
107   int           stride      = width * bpp;
108
109   unsigned char* pixels = buffer.GetBuffer();
110   memset(pixels, 0, width * height * bpp);
111
112   for(int x = 0; x < width; ++x)
113   {
114     for(int y = 0; y < height; ++y)
115     {
116       if((x < width / 2 && y >= height / 2) ||
117          (x >= width / 2 && y < height / 2))
118       {
119         for(int b = 0; b < bpp; ++b)
120         {
121           pixels[y * stride + x * bpp + b] = 0xff;
122         }
123       }
124     }
125   }
126 }
127
128 void FillCheckerboard(Devel::PixelBuffer imageData)
129 {
130   int           width       = imageData.GetWidth();
131   int           height      = imageData.GetHeight();
132   Pixel::Format pixelFormat = imageData.GetPixelFormat();
133   int           bpp         = Pixel::GetBytesPerPixel(pixelFormat);
134
135   unsigned char* imageBuffer = imageData.GetBuffer();
136   memset(imageBuffer, 0, width * height * bpp);
137   int offset = 0;
138   for(int x = 0; x < width; ++x)
139   {
140     for(int y = 0; y < height; ++y)
141     {
142       // on even lines, odd pixels, or on odd lines, even pixels
143       if((x % 2 && y % 2 == 0) || (x % 2 == 0 && y % 2))
144       {
145         switch(pixelFormat)
146         {
147           case Pixel::RGBA5551:
148             imageBuffer[offset]     = 0xFF;
149             imageBuffer[offset + 1] = 0xFF;
150             break;
151           case Pixel::RGBA4444:
152             imageBuffer[offset]     = 0xFF;
153             imageBuffer[offset + 1] = 0xFF;
154             break;
155           case Pixel::RGB565:
156             imageBuffer[offset]     = 0xFF;
157             imageBuffer[offset + 1] = 0xFF;
158             break;
159           case Pixel::RGB888:
160             imageBuffer[offset]     = 0xFF;
161             imageBuffer[offset + 1] = 0xFF;
162             imageBuffer[offset + 2] = 0xFF;
163             break;
164           case Pixel::RGBA8888:
165             imageBuffer[offset]     = 0xFF;
166             imageBuffer[offset + 1] = 0xFF;
167             imageBuffer[offset + 2] = 0xFF;
168             imageBuffer[offset + 3] = 0xFF;
169             break;
170           default:
171             break;
172         }
173       }
174       offset += bpp;
175     }
176   }
177 }
178
179 int GetAlphaAt(Devel::PixelBuffer buffer, int x, int y)
180 {
181   unsigned char* pixels = buffer.GetBuffer();
182   int            bpp    = Pixel::GetBytesPerPixel(buffer.GetPixelFormat());
183   int            stride = buffer.GetWidth() * bpp;
184   int            byteOffset;
185   int            bitMask;
186   GetAlphaOffsetAndMask(buffer.GetPixelFormat(), byteOffset, bitMask);
187   return int(pixels[stride * y + x * bpp + byteOffset]) & bitMask;
188 }
189
190 int UtcDaliPixelBufferNew01P(void)
191 {
192   TestApplication    application;
193   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New(10, 10, Pixel::RGBA8888);
194   DALI_TEST_CHECK(pixbuf);
195   DALI_TEST_CHECK(pixbuf.GetBuffer() != NULL);
196   END_TEST;
197 }
198
199 int UtcDaliPixelBufferNew01N(void)
200 {
201   TestApplication    application;
202   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New(0, 0, Pixel::RGBA8888);
203   DALI_TEST_CHECK(pixbuf);
204   DALI_TEST_CHECK(pixbuf.GetBuffer() == NULL);
205   END_TEST;
206 }
207
208 int UtcDaliPixelBufferConvert(void)
209 {
210   TestApplication    application;
211   TestGlAbstraction& gl           = application.GetGlAbstraction();
212   TraceCallStack&    textureTrace = gl.GetTextureTrace();
213   textureTrace.Enable(true);
214
215   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New(10, 10, Pixel::RGB565);
216   FillCheckerboard(pixbuf);
217
218   {
219     Devel::PixelBuffer pixbufPrime = pixbuf; // store a second handle to the data
220
221     Dali::PixelData pixelData = Devel::PixelBuffer::Convert(pixbuf);
222     DALI_TEST_CHECK(!pixbuf);
223
224     // check the buffer in the second handle is empty
225     DALI_TEST_CHECK(pixbufPrime.GetBuffer() == NULL);
226
227     DALI_TEST_CHECK(pixelData);
228     DALI_TEST_EQUALS(pixelData.GetWidth(), 10, TEST_LOCATION);
229     DALI_TEST_EQUALS(pixelData.GetHeight(), 10, TEST_LOCATION);
230     DALI_TEST_EQUALS(pixelData.GetPixelFormat(), Pixel::RGB565, TEST_LOCATION);
231
232     // Try drawing it
233     Texture t = Texture::New(TextureType::TEXTURE_2D, Pixel::RGB565, 10, 10);
234     t.Upload(pixelData);
235     TextureSet ts = TextureSet::New();
236     ts.SetTexture(0, t);
237     Geometry g = CreateQuadGeometry();
238     Shader   s = Shader::New("v", "f");
239     Renderer r = Renderer::New(g, s);
240     r.SetTextures(ts);
241     Actor a = Actor::New();
242     a.AddRenderer(r);
243     a.SetProperty(Actor::Property::SIZE, Vector2(10, 10));
244     a.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
245     application.GetScene().Add(a);
246
247     application.SendNotification();
248     application.Render();
249     DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
250
251     // Let secondary scope destroy pixbufPrime
252   }
253
254   END_TEST;
255 }
256
257 int UtcDaliPixelBufferGetWidth(void)
258 {
259   TestApplication    application;
260   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New(10, 10, Pixel::RGB565);
261   FillCheckerboard(pixbuf);
262
263   DALI_TEST_EQUALS(pixbuf.GetWidth(), 10, TEST_LOCATION);
264
265   END_TEST;
266 }
267
268 int UtcDaliPixelBufferGetHeight(void)
269 {
270   TestApplication    application;
271   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New(10, 10, Pixel::RGB565);
272   FillCheckerboard(pixbuf);
273
274   DALI_TEST_EQUALS(pixbuf.GetHeight(), 10, TEST_LOCATION);
275
276   END_TEST;
277 }
278
279 int UtcDaliPixelBufferGetPixelFormat(void)
280 {
281   TestApplication    application;
282   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New(10, 10, Pixel::RGB565);
283   FillCheckerboard(pixbuf);
284
285   DALI_TEST_EQUALS(pixbuf.GetPixelFormat(), Pixel::RGB565, TEST_LOCATION);
286
287   END_TEST;
288 }
289
290 int UtcDaliPixelBufferMask01(void)
291 {
292   TestApplication application;
293
294   unsigned int       width       = 10u;
295   unsigned int       height      = 10u;
296   Pixel::Format      pixelFormat = Pixel::L8;
297   Devel::PixelBuffer maskData    = Devel::PixelBuffer::New(width, height, pixelFormat);
298
299   Mask1stQuadrant(maskData);
300
301   width       = 20u;
302   height      = 20u;
303   pixelFormat = Pixel::RGBA5551;
304
305   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(width, height, pixelFormat);
306   FillCheckerboard(imageData);
307
308   imageData.ApplyMask(maskData, 1.0f, false);
309
310   // Test that the pixel format has been promoted to RGBA8888
311   DALI_TEST_EQUALS(imageData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION);
312
313   // Test that a pixel in the first quadrant has no alpha value
314   unsigned char* buffer = imageData.GetBuffer();
315   DALI_TEST_EQUALS(buffer[3], 0x00u, TEST_LOCATION);
316   DALI_TEST_EQUALS(buffer[7], 0x00u, TEST_LOCATION);
317
318   // Test that an even pixel in the second quadrant has a full alpha value
319   DALI_TEST_EQUALS(buffer[43], 0x00u, TEST_LOCATION);
320
321   // Test that an odd pixel in the second quadrant has full alpha value
322   DALI_TEST_EQUALS(buffer[47], 0xffu, TEST_LOCATION);
323
324   END_TEST;
325 }
326
327 int UtcDaliPixelBufferMask02(void)
328 {
329   TestApplication application;
330
331   unsigned int       width       = 10u;
332   unsigned int       height      = 10u;
333   Pixel::Format      pixelFormat = Pixel::L8;
334   Devel::PixelBuffer maskData    = Devel::PixelBuffer::New(width, height, pixelFormat);
335
336   Mask1stQuadrant(maskData);
337
338   width       = 20u;
339   height      = 20u;
340   pixelFormat = Pixel::RGBA4444;
341
342   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(width, height, pixelFormat);
343   FillCheckerboard(imageData);
344
345   imageData.ApplyMask(maskData, 1.0f, false);
346
347   // Test that the pixel format has been promoted to RGBA8888
348   DALI_TEST_EQUALS(imageData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION);
349
350   // Test that a pixel in the first quadrant has no alpha value
351   unsigned char* buffer = imageData.GetBuffer();
352   DALI_TEST_EQUALS(buffer[3], 0x00u, TEST_LOCATION);
353   DALI_TEST_EQUALS(buffer[7], 0x00u, TEST_LOCATION);
354
355   // Test that an even pixel in the second quadrant has no alpha value
356   DALI_TEST_EQUALS(buffer[43], 0x00u, TEST_LOCATION);
357
358   // Test that an odd pixel in the second quadrant has full alpha value
359   DALI_TEST_EQUALS(buffer[47], 0xffu, TEST_LOCATION);
360
361   END_TEST;
362 }
363
364 int UtcDaliPixelBufferMask03(void)
365 {
366   TestApplication application;
367   tet_infoline("Test application of alpha mask to smaller RGB565 image");
368
369   unsigned int       width    = 20u;
370   unsigned int       height   = 20u;
371   Devel::PixelBuffer maskData = Devel::PixelBuffer::New(width, height, Pixel::L8);
372   Mask1stQuadrant(maskData);
373
374   width                        = 10u;
375   height                       = 10u;
376   Pixel::Format      format    = Pixel::RGB565;
377   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(width, height, format);
378   FillCheckerboard(imageData);
379
380   imageData.ApplyMask(maskData, 1.0f, false);
381
382   // Test that the pixel format has been promoted to RGBA8888
383   DALI_TEST_EQUALS(imageData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION);
384
385   // Test that a pixel in the first quadrant has no alpha value
386   unsigned char* buffer = imageData.GetBuffer();
387   DALI_TEST_EQUALS(buffer[3], 0x00u, TEST_LOCATION);
388   DALI_TEST_EQUALS(buffer[7], 0x00u, TEST_LOCATION);
389
390   // Test that an odd pixel in the fourth quadrant has full alpha value
391   DALI_TEST_EQUALS(buffer[(6 * 10 + 7) * 4 + 3], 0xffu, TEST_LOCATION);
392
393   // Test that an even pixel in the fourth quadrant has full alpha value
394   DALI_TEST_EQUALS(buffer[(6 * 10 + 8) * 4 + 3], 0xffu, TEST_LOCATION);
395
396   END_TEST;
397 }
398
399 int UtcDaliPixelBufferMask04(void)
400 {
401   TestApplication application;
402   tet_infoline("Test application of alpha mask to larger RGBA8888 image");
403
404   unsigned int       width    = 10u;
405   unsigned int       height   = 10u;
406   Devel::PixelBuffer maskData = Devel::PixelBuffer::New(width, height, Pixel::L8);
407   Mask1stQuadrant(maskData);
408
409   width                        = 20u;
410   height                       = 20u;
411   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
412   FillCheckerboard(imageData);
413
414   imageData.ApplyMask(maskData, 1.0f, false);
415
416   // Test that the pixel format has been promoted to RGBA8888
417   DALI_TEST_EQUALS(imageData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION);
418
419   // Test that a pixel in the first quadrant has no alpha value
420   unsigned char* buffer = imageData.GetBuffer();
421   DALI_TEST_EQUALS(buffer[3], 0x00u, TEST_LOCATION);
422   DALI_TEST_EQUALS(buffer[7], 0x00u, TEST_LOCATION);
423
424   // Test that an even pixel in the second quadrant has no alpha value
425   DALI_TEST_EQUALS(buffer[43], 0x00u, TEST_LOCATION);
426
427   // Test that an odd pixel in the second quadrant has full alpha value
428   DALI_TEST_EQUALS(buffer[47], 0xffu, TEST_LOCATION);
429
430   END_TEST;
431 }
432
433 int UtcDaliPixelBufferMask05(void)
434 {
435   TestApplication application;
436   tet_infoline("Test application of alpha mask to smaller RGBA8888 image");
437
438   unsigned int       width    = 20u;
439   unsigned int       height   = 20u;
440   Devel::PixelBuffer maskData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
441   Mask1stQuadrant(maskData);
442
443   width                        = 10u;
444   height                       = 10u;
445   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
446   FillCheckerboard(imageData);
447
448   imageData.ApplyMask(maskData, 1.0f, false);
449
450   // Test that the pixel format has been promoted to RGBA8888
451   DALI_TEST_EQUALS(imageData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION);
452
453   // Test that a pixel in the first quadrant has no alpha value
454   unsigned char* buffer = imageData.GetBuffer();
455   DALI_TEST_EQUALS(buffer[3], 0x00u, TEST_LOCATION);
456   DALI_TEST_EQUALS(buffer[7], 0x00u, TEST_LOCATION);
457
458   // Test that an odd pixel in the second quadrant has full alpha value
459   DALI_TEST_EQUALS(buffer[39], 0xffu, TEST_LOCATION);
460
461   // Test that an even pixel in the second quadrant has no alpha value
462   DALI_TEST_EQUALS(buffer[27], 0x00u, TEST_LOCATION);
463
464   END_TEST;
465 }
466
467 int UtcDaliPixelBufferMask06(void)
468 {
469   TestApplication application;
470   tet_infoline("Test application of alpha mask to same size RGBA8888 image");
471
472   unsigned int       width    = 10u;
473   unsigned int       height   = 10u;
474   Devel::PixelBuffer maskData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
475   Mask1stQuadrant(maskData);
476
477   width                        = 10u;
478   height                       = 10u;
479   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
480   FillCheckerboard(imageData);
481
482   imageData.ApplyMask(maskData, 1.0f, false);
483
484   // Test that the pixel format has been promoted to RGBA8888
485   DALI_TEST_EQUALS(imageData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION);
486
487   // Test that a pixel in the first quadrant has no alpha value
488   unsigned char* buffer = imageData.GetBuffer();
489   DALI_TEST_EQUALS(buffer[3], 0x00u, TEST_LOCATION);
490   DALI_TEST_EQUALS(buffer[7], 0x00u, TEST_LOCATION);
491
492   // Test that an odd pixel in the second quadrant has full alpha value
493   DALI_TEST_EQUALS(buffer[39], 0xffu, TEST_LOCATION);
494
495   // Test that an even pixel in the second quadrant has no alpha value
496   DALI_TEST_EQUALS(buffer[27], 0x00u, TEST_LOCATION);
497
498   END_TEST;
499 }
500
501 int UtcDaliPixelBufferMask07(void)
502 {
503   TestApplication application;
504   tet_infoline("Test scaling of source image to match alpha mask");
505
506   unsigned int       width    = 20u;
507   unsigned int       height   = 20u;
508   Devel::PixelBuffer maskData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
509   MaskCenterSquare(maskData);
510
511   // +----------+
512   // |  XXXXXX  |
513   // |  XXXXXX  |
514   // |  XXXXXX  |
515   // |  XXXXXX  |
516   // *----------+
517
518   width                        = 10u;
519   height                       = 10u;
520   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
521   AlternateQuadrants(imageData);
522
523   // +-----XXXXX+
524   // |     XXXXX|
525   // |     XXXXX|
526   // |XXXXX     |
527   // |XXXXX     |
528   // *XXXXX-----+
529
530   imageData.ApplyMask(maskData, 2.0f, true);
531
532   // +----------+
533   // |     XXX  |
534   // |     XXX  |
535   // |  XXX     |
536   // |  XXX     |
537   // *----------+
538
539   tet_infoline("Test that the image has been scaled to match the alpha mask");
540   DALI_TEST_EQUALS(imageData.GetWidth(), 20, TEST_LOCATION);
541   DALI_TEST_EQUALS(imageData.GetHeight(), 20, TEST_LOCATION);
542
543   tet_infoline("Test that pixels in the outer eighths have no alpha\n");
544
545   DALI_TEST_EQUALS(GetAlphaAt(imageData, 0, 0), 0x00u, TEST_LOCATION);
546   DALI_TEST_EQUALS(GetAlphaAt(imageData, 9, 4), 0x00u, TEST_LOCATION);
547   DALI_TEST_EQUALS(GetAlphaAt(imageData, 15, 4), 0x00u, TEST_LOCATION);
548   DALI_TEST_EQUALS(GetAlphaAt(imageData, 19, 4), 0x00u, TEST_LOCATION);
549
550   DALI_TEST_EQUALS(GetAlphaAt(imageData, 0, 19), 0x00u, TEST_LOCATION);
551   DALI_TEST_EQUALS(GetAlphaAt(imageData, 8, 18), 0x00u, TEST_LOCATION);
552   DALI_TEST_EQUALS(GetAlphaAt(imageData, 15, 17), 0x00u, TEST_LOCATION);
553   DALI_TEST_EQUALS(GetAlphaAt(imageData, 19, 16), 0x00u, TEST_LOCATION);
554
555   DALI_TEST_EQUALS(GetAlphaAt(imageData, 0, 1), 0x00u, TEST_LOCATION);
556   DALI_TEST_EQUALS(GetAlphaAt(imageData, 1, 7), 0x00u, TEST_LOCATION);
557   DALI_TEST_EQUALS(GetAlphaAt(imageData, 2, 10), 0x00u, TEST_LOCATION);
558   DALI_TEST_EQUALS(GetAlphaAt(imageData, 3, 19), 0x00u, TEST_LOCATION);
559
560   DALI_TEST_EQUALS(GetAlphaAt(imageData, 19, 1), 0x00u, TEST_LOCATION);
561   DALI_TEST_EQUALS(GetAlphaAt(imageData, 18, 7), 0x00u, TEST_LOCATION);
562   DALI_TEST_EQUALS(GetAlphaAt(imageData, 17, 10), 0x00u, TEST_LOCATION);
563   DALI_TEST_EQUALS(GetAlphaAt(imageData, 16, 19), 0x00u, TEST_LOCATION);
564
565   tet_infoline("Test that pixels in the center have full alpha\n");
566
567   DALI_TEST_EQUALS(GetAlphaAt(imageData, 12, 8), 0xffu, TEST_LOCATION);
568   DALI_TEST_EQUALS(GetAlphaAt(imageData, 8, 12), 0xffu, TEST_LOCATION);
569
570   END_TEST;
571 }
572
573 int UtcDaliPixelBufferMask08(void)
574 {
575   TestApplication application;
576   tet_infoline("Test scaling of source image to larger than the alpha mask");
577
578   unsigned int       width    = 32u;
579   unsigned int       height   = 20u;
580   Devel::PixelBuffer maskData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
581   AlternateQuadrants(maskData);
582
583   // +-----XXXXX+
584   // |     XXXXX|
585   // |     XXXXX|
586   // |XXXXX     |
587   // |XXXXX     |
588   // *XXXXX-----+
589
590   width                        = 20u;
591   height                       = 16u;
592   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
593   MaskCenterSquare(imageData);
594
595   // +----------+
596   // |  XXXXXX  |
597   // |  XXXXXX  |
598   // |  XXXXXX  |
599   // |  XXXXXX  |
600   // *----------+
601
602   imageData.ApplyMask(maskData, 4.0f, true);
603
604   // +-----XXXXX+   quadrant
605   // |     XXXXX|    1    2
606   // |     XXXXX|
607   // |XXXXX     |    4    3
608   // |XXXXX     |
609   // *XXXXX-----+
610
611   tet_infoline("Test that the image has been scaled and cropped to match the alpha mask");
612   DALI_TEST_EQUALS(imageData.GetWidth(), 32, TEST_LOCATION);
613   DALI_TEST_EQUALS(imageData.GetHeight(), 20, TEST_LOCATION);
614
615   tet_infoline("Test that the image has been resized (the center square should now fill the image)\n");
616   tet_infoline("Test that the first quadrant has no alpha");
617   DALI_TEST_EQUALS(GetAlphaAt(imageData, 0, 0), 0x00u, TEST_LOCATION);
618   DALI_TEST_EQUALS(GetAlphaAt(imageData, 5, 4), 0x00u, TEST_LOCATION);
619   DALI_TEST_EQUALS(GetAlphaAt(imageData, 5, 8), 0x00u, TEST_LOCATION);
620   DALI_TEST_EQUALS(GetAlphaAt(imageData, 14, 8), 0x00u, TEST_LOCATION);
621
622   tet_infoline("Test that the second quadrant has alpha and data");
623   DALI_TEST_EQUALS(GetAlphaAt(imageData, 18, 0), 0xffu, TEST_LOCATION);
624   DALI_TEST_EQUALS(GetAlphaAt(imageData, 30, 1), 0xffu, TEST_LOCATION);
625   DALI_TEST_EQUALS(GetAlphaAt(imageData, 30, 8), 0xffu, TEST_LOCATION);
626   DALI_TEST_EQUALS(GetAlphaAt(imageData, 19, 8), 0xffu, TEST_LOCATION);
627
628   tet_infoline("Test that the third quadrant has no alpha");
629   DALI_TEST_EQUALS(GetAlphaAt(imageData, 18, 12), 0x00u, TEST_LOCATION);
630   DALI_TEST_EQUALS(GetAlphaAt(imageData, 31, 12), 0x00u, TEST_LOCATION);
631   DALI_TEST_EQUALS(GetAlphaAt(imageData, 31, 19), 0x00u, TEST_LOCATION);
632   DALI_TEST_EQUALS(GetAlphaAt(imageData, 18, 19), 0x00u, TEST_LOCATION);
633
634   tet_infoline("Test that the fourth quadrant has alpha and data");
635   DALI_TEST_EQUALS(GetAlphaAt(imageData, 1, 12), 0xffu, TEST_LOCATION);
636   DALI_TEST_EQUALS(GetAlphaAt(imageData, 7, 12), 0xffu, TEST_LOCATION);
637   DALI_TEST_EQUALS(GetAlphaAt(imageData, 7, 19), 0xffu, TEST_LOCATION);
638   DALI_TEST_EQUALS(GetAlphaAt(imageData, 1, 19), 0xffu, TEST_LOCATION);
639
640   END_TEST;
641 }
642
643 int UtcDaliPixelBufferMask09(void)
644 {
645   TestApplication application;
646   tet_infoline("Test scaling of large source image to larger than the alpha mask");
647
648   unsigned int       width    = 32u;
649   unsigned int       height   = 20u;
650   Devel::PixelBuffer maskData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
651   AlternateQuadrants(maskData);
652
653   // +-----XXXXX+
654   // |     XXXXX|
655   // |     XXXXX|
656   // |XXXXX     |
657   // |XXXXX     |
658   // *XXXXX-----+
659
660   width                        = 40u;
661   height                       = 50u;
662   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
663   MaskCenterSquare(imageData);
664
665   // +----------+
666   // |  XXXXXX  |
667   // |  XXXXXX  |
668   // |  XXXXXX  |
669   // |  XXXXXX  |
670   // *----------+
671
672   imageData.ApplyMask(maskData, 1.6f, true);
673
674   // +-----XXXXX+   quadrant
675   // |     XXXXX|    1    2
676   // |     XXXXX|
677   // |XXXXX     |    4    3
678   // |XXXXX     |
679   // *XXXXX-----+
680
681   tet_infoline("Test that the image has been scaled and cropped to match the alpha mask");
682   DALI_TEST_EQUALS(imageData.GetWidth(), 32, TEST_LOCATION);
683   DALI_TEST_EQUALS(imageData.GetHeight(), 20, TEST_LOCATION);
684
685   tet_infoline("Test that the image has been resized (the center square should now fill the image)\n");
686   tet_infoline("Test that the first quadrant has no alpha");
687   DALI_TEST_EQUALS(GetAlphaAt(imageData, 0, 0), 0x00u, TEST_LOCATION);
688   DALI_TEST_EQUALS(GetAlphaAt(imageData, 5, 4), 0x00u, TEST_LOCATION);
689   DALI_TEST_EQUALS(GetAlphaAt(imageData, 5, 8), 0x00u, TEST_LOCATION);
690   DALI_TEST_EQUALS(GetAlphaAt(imageData, 14, 8), 0x00u, TEST_LOCATION);
691
692   tet_infoline("Test that the second quadrant has alpha and data");
693   DALI_TEST_EQUALS(GetAlphaAt(imageData, 18, 0), 0xffu, TEST_LOCATION);
694   DALI_TEST_EQUALS(GetAlphaAt(imageData, 30, 1), 0xffu, TEST_LOCATION);
695   DALI_TEST_EQUALS(GetAlphaAt(imageData, 30, 8), 0xffu, TEST_LOCATION);
696   DALI_TEST_EQUALS(GetAlphaAt(imageData, 19, 8), 0xffu, TEST_LOCATION);
697
698   tet_infoline("Test that the third quadrant has no alpha");
699   DALI_TEST_EQUALS(GetAlphaAt(imageData, 18, 12), 0x00u, TEST_LOCATION);
700   DALI_TEST_EQUALS(GetAlphaAt(imageData, 31, 12), 0x00u, TEST_LOCATION);
701   DALI_TEST_EQUALS(GetAlphaAt(imageData, 31, 19), 0x00u, TEST_LOCATION);
702   DALI_TEST_EQUALS(GetAlphaAt(imageData, 18, 19), 0x00u, TEST_LOCATION);
703
704   tet_infoline("Test that the fourth quadrant has alpha and data");
705   DALI_TEST_EQUALS(GetAlphaAt(imageData, 1, 12), 0xffu, TEST_LOCATION);
706   DALI_TEST_EQUALS(GetAlphaAt(imageData, 7, 12), 0xffu, TEST_LOCATION);
707   DALI_TEST_EQUALS(GetAlphaAt(imageData, 7, 19), 0xffu, TEST_LOCATION);
708   DALI_TEST_EQUALS(GetAlphaAt(imageData, 1, 19), 0xffu, TEST_LOCATION);
709
710   END_TEST;
711 }
712
713 int UtcDaliPixelBufferGaussianBlur(void)
714 {
715   TestApplication application;
716
717   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(10, 10, Pixel::RGBA8888);
718   FillCheckerboard(imageData);
719
720   DALI_TEST_EQUALS(imageData.GetWidth(), 10, TEST_LOCATION);
721   DALI_TEST_EQUALS(imageData.GetHeight(), 10, TEST_LOCATION);
722
723   unsigned char* buffer = imageData.GetBuffer();
724
725   // Test that an even pixel in the odd row has full alpha value
726   DALI_TEST_EQUALS(buffer[43], 0xffu, TEST_LOCATION);
727
728   // Test that an even pixel in the even row has no alpha value
729   DALI_TEST_EQUALS(buffer[55], 0x00u, TEST_LOCATION);
730
731   imageData.ApplyGaussianBlur(0.0f);
732
733   // Test that the pixels' alpha values are not changed because there is no blur
734   DALI_TEST_EQUALS(buffer[43], 0xffu, TEST_LOCATION);
735   DALI_TEST_EQUALS(buffer[55], 0x00u, TEST_LOCATION);
736
737   imageData.ApplyGaussianBlur(1.0f);
738
739   // Test that the pixels' alpha values are changed after applying gaussian blur
740   DALI_TEST_EQUALS(buffer[43], 0x7Au, TEST_LOCATION);
741   DALI_TEST_EQUALS(buffer[55], 0x7Eu, TEST_LOCATION);
742
743   END_TEST;
744 }