[dali_2.3.31] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor / utc-Dali-PixelBuffer.cpp
1 /*
2  * Copyright (c) 2024 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 UtcDaliPixelBufferConstructor01P(void)
200 {
201   TestApplication    application;
202   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New(10, 10, Pixel::RGBA8888);
203
204   Devel::PixelBuffer copiedBuf = pixbuf;
205   DALI_TEST_CHECK(pixbuf);
206   DALI_TEST_CHECK(copiedBuf);
207   DALI_TEST_CHECK(pixbuf.GetBuffer() != NULL);
208   DALI_TEST_CHECK(copiedBuf.GetBuffer() != NULL);
209
210   Devel::PixelBuffer movedBuf = std::move(pixbuf);
211   DALI_TEST_CHECK(!pixbuf);
212   DALI_TEST_CHECK(movedBuf);
213   DALI_TEST_CHECK(movedBuf.GetBuffer() != NULL);
214   END_TEST;
215 }
216
217 int UtcDaliPixelBufferAssign01P(void)
218 {
219   TestApplication    application;
220   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New(10, 10, Pixel::RGBA8888);
221
222   Devel::PixelBuffer copiedBuf;
223   copiedBuf = pixbuf;
224   DALI_TEST_CHECK(pixbuf);
225   DALI_TEST_CHECK(copiedBuf);
226   DALI_TEST_CHECK(pixbuf.GetBuffer() != NULL);
227   DALI_TEST_CHECK(copiedBuf.GetBuffer() != NULL);
228
229   Devel::PixelBuffer movedBuf;
230   DALI_TEST_CHECK(!movedBuf);
231   movedBuf = std::move(pixbuf);
232   DALI_TEST_CHECK(!pixbuf);
233   DALI_TEST_CHECK(movedBuf);
234   DALI_TEST_CHECK(movedBuf.GetBuffer() != NULL);
235   END_TEST;
236 }
237
238 int UtcDaliPixelBufferNew01N(void)
239 {
240   TestApplication    application;
241   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New(0, 0, Pixel::RGBA8888);
242   DALI_TEST_CHECK(pixbuf);
243   DALI_TEST_CHECK(pixbuf.GetBuffer() == NULL);
244   END_TEST;
245 }
246
247 int UtcDaliPixelBufferConvert01(void)
248 {
249   TestApplication    application;
250   TestGlAbstraction& gl           = application.GetGlAbstraction();
251   TraceCallStack&    textureTrace = gl.GetTextureTrace();
252   textureTrace.Enable(true);
253
254   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New(10, 10, Pixel::RGB565);
255   FillCheckerboard(pixbuf);
256
257   {
258     Devel::PixelBuffer pixbufPrime = pixbuf; // store a second handle to the data
259
260     Dali::PixelData pixelData = Devel::PixelBuffer::Convert(pixbuf);
261     DALI_TEST_CHECK(!pixbuf);
262
263     // check the buffer in the second handle is empty
264     DALI_TEST_CHECK(pixbufPrime.GetBuffer() == NULL);
265
266     DALI_TEST_CHECK(pixelData);
267     DALI_TEST_EQUALS(pixelData.GetWidth(), 10, TEST_LOCATION);
268     DALI_TEST_EQUALS(pixelData.GetHeight(), 10, TEST_LOCATION);
269     DALI_TEST_EQUALS(pixelData.GetStride(), 10, TEST_LOCATION);
270     DALI_TEST_EQUALS(pixelData.GetPixelFormat(), Pixel::RGB565, TEST_LOCATION);
271
272     // Try drawing it
273     Texture t = Texture::New(TextureType::TEXTURE_2D, Pixel::RGB565, 10, 10);
274     t.Upload(pixelData);
275     TextureSet ts = TextureSet::New();
276     ts.SetTexture(0, t);
277     Geometry g = CreateQuadGeometry();
278     Shader   s = Shader::New("v", "f");
279     Renderer r = Renderer::New(g, s);
280     r.SetTextures(ts);
281     Actor a = Actor::New();
282     a.AddRenderer(r);
283     a.SetProperty(Actor::Property::SIZE, Vector2(10, 10));
284     a.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
285     application.GetScene().Add(a);
286
287     application.SendNotification();
288     application.Render();
289     DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
290
291     // Let secondary scope destroy pixbufPrime
292   }
293
294   END_TEST;
295 }
296
297 int UtcDaliPixelBufferConvert02(void)
298 {
299   TestApplication    application;
300   TestGlAbstraction& gl           = application.GetGlAbstraction();
301   TraceCallStack&    textureTrace = gl.GetTextureTrace();
302   textureTrace.Enable(true);
303
304   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New(10, 10, Pixel::RGB565);
305   FillCheckerboard(pixbuf);
306
307   {
308     Devel::PixelBuffer pixbufPrime = pixbuf; // store a second handle to the data
309
310     Dali::PixelData pixelData = Devel::PixelBuffer::Convert(pixbuf, true);
311     DALI_TEST_CHECK(!pixbuf);
312
313     // check the buffer in the second handle is empty
314     DALI_TEST_CHECK(pixbufPrime.GetBuffer() == NULL);
315
316     DALI_TEST_CHECK(pixelData);
317     DALI_TEST_EQUALS(pixelData.GetWidth(), 10, TEST_LOCATION);
318     DALI_TEST_EQUALS(pixelData.GetHeight(), 10, TEST_LOCATION);
319     DALI_TEST_EQUALS(pixelData.GetStride(), 10, TEST_LOCATION);
320     DALI_TEST_EQUALS(pixelData.GetPixelFormat(), Pixel::RGB565, TEST_LOCATION);
321
322     // Try drawing it
323     Texture t = Texture::New(TextureType::TEXTURE_2D, Pixel::RGB565, 10, 10);
324     t.Upload(pixelData);
325     TextureSet ts = TextureSet::New();
326     ts.SetTexture(0, t);
327     Geometry g = CreateQuadGeometry();
328     Shader   s = Shader::New("v", "f");
329     Renderer r = Renderer::New(g, s);
330     r.SetTextures(ts);
331     Actor a = Actor::New();
332     a.AddRenderer(r);
333     a.SetProperty(Actor::Property::SIZE, Vector2(10, 10));
334     a.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
335     application.GetScene().Add(a);
336
337     application.SendNotification();
338     application.Render();
339     DALI_TEST_EQUALS(textureTrace.FindMethod("BindTexture"), true, TEST_LOCATION);
340
341     // Let secondary scope destroy pixbufPrime
342   }
343
344   END_TEST;
345 }
346
347 int UtcDaliPixelBufferGetWidth(void)
348 {
349   TestApplication    application;
350   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New(10, 10, Pixel::RGB565);
351   FillCheckerboard(pixbuf);
352
353   DALI_TEST_EQUALS(pixbuf.GetWidth(), 10, TEST_LOCATION);
354   DALI_TEST_EQUALS(pixbuf.GetStride(), 10, TEST_LOCATION);
355
356   END_TEST;
357 }
358
359 int UtcDaliPixelBufferGetHeight(void)
360 {
361   TestApplication    application;
362   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New(10, 10, Pixel::RGB565);
363   FillCheckerboard(pixbuf);
364
365   DALI_TEST_EQUALS(pixbuf.GetHeight(), 10, TEST_LOCATION);
366
367   END_TEST;
368 }
369
370 int UtcDaliPixelBufferGetPixelFormat(void)
371 {
372   TestApplication    application;
373   Devel::PixelBuffer pixbuf = Devel::PixelBuffer::New(10, 10, Pixel::RGB565);
374   FillCheckerboard(pixbuf);
375
376   DALI_TEST_EQUALS(pixbuf.GetPixelFormat(), Pixel::RGB565, TEST_LOCATION);
377
378   END_TEST;
379 }
380
381 int UtcDaliPixelBufferMask01(void)
382 {
383   TestApplication application;
384
385   unsigned int       width       = 10u;
386   unsigned int       height      = 10u;
387   Pixel::Format      pixelFormat = Pixel::L8;
388   Devel::PixelBuffer maskData    = Devel::PixelBuffer::New(width, height, pixelFormat);
389
390   Mask1stQuadrant(maskData);
391
392   width       = 20u;
393   height      = 20u;
394   pixelFormat = Pixel::RGBA5551;
395
396   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(width, height, pixelFormat);
397   FillCheckerboard(imageData);
398
399   imageData.ApplyMask(maskData, 1.0f, false);
400
401   // Test that the pixel format has been promoted to RGBA8888
402   DALI_TEST_EQUALS(imageData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION);
403
404   // Test that a pixel in the first quadrant has no alpha value
405   unsigned char* buffer = imageData.GetBuffer();
406   DALI_TEST_EQUALS(buffer[3], 0x00u, TEST_LOCATION);
407   DALI_TEST_EQUALS(buffer[7], 0x00u, TEST_LOCATION);
408
409   // Test that an even pixel in the second quadrant has a full alpha value
410   DALI_TEST_EQUALS(buffer[43], 0x00u, TEST_LOCATION);
411
412   // Test that an odd pixel in the second quadrant has full alpha value
413   DALI_TEST_EQUALS(buffer[47], 0xffu, TEST_LOCATION);
414
415   END_TEST;
416 }
417
418 int UtcDaliPixelBufferMask02(void)
419 {
420   TestApplication application;
421
422   unsigned int       width       = 10u;
423   unsigned int       height      = 10u;
424   Pixel::Format      pixelFormat = Pixel::L8;
425   Devel::PixelBuffer maskData    = Devel::PixelBuffer::New(width, height, pixelFormat);
426
427   Mask1stQuadrant(maskData);
428
429   width       = 20u;
430   height      = 20u;
431   pixelFormat = Pixel::RGBA4444;
432
433   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(width, height, pixelFormat);
434   FillCheckerboard(imageData);
435
436   imageData.ApplyMask(maskData, 1.0f, false);
437
438   // Test that the pixel format has been promoted to RGBA8888
439   DALI_TEST_EQUALS(imageData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION);
440
441   // Test that a pixel in the first quadrant has no alpha value
442   unsigned char* buffer = imageData.GetBuffer();
443   DALI_TEST_EQUALS(buffer[3], 0x00u, TEST_LOCATION);
444   DALI_TEST_EQUALS(buffer[7], 0x00u, TEST_LOCATION);
445
446   // Test that an even pixel in the second quadrant has no alpha value
447   DALI_TEST_EQUALS(buffer[43], 0x00u, TEST_LOCATION);
448
449   // Test that an odd pixel in the second quadrant has full alpha value
450   DALI_TEST_EQUALS(buffer[47], 0xffu, TEST_LOCATION);
451
452   END_TEST;
453 }
454
455 int UtcDaliPixelBufferMask03(void)
456 {
457   TestApplication application;
458   tet_infoline("Test application of alpha mask to smaller RGB565 image");
459
460   unsigned int       width    = 20u;
461   unsigned int       height   = 20u;
462   Devel::PixelBuffer maskData = Devel::PixelBuffer::New(width, height, Pixel::L8);
463   Mask1stQuadrant(maskData);
464
465   width                        = 10u;
466   height                       = 10u;
467   Pixel::Format      format    = Pixel::RGB565;
468   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(width, height, format);
469   FillCheckerboard(imageData);
470
471   imageData.ApplyMask(maskData, 1.0f, false);
472
473   // Test that the pixel format has been promoted to RGBA8888
474   DALI_TEST_EQUALS(imageData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION);
475
476   // Test that a pixel in the first quadrant has no alpha value
477   unsigned char* buffer = imageData.GetBuffer();
478   DALI_TEST_EQUALS(buffer[3], 0x00u, TEST_LOCATION);
479   DALI_TEST_EQUALS(buffer[7], 0x00u, TEST_LOCATION);
480
481   // Test that an odd pixel in the fourth quadrant has full alpha value
482   DALI_TEST_EQUALS(buffer[(6 * 10 + 7) * 4 + 3], 0xffu, TEST_LOCATION);
483
484   // Test that an even pixel in the fourth quadrant has full alpha value
485   DALI_TEST_EQUALS(buffer[(6 * 10 + 8) * 4 + 3], 0xffu, TEST_LOCATION);
486
487   END_TEST;
488 }
489
490 int UtcDaliPixelBufferMask04(void)
491 {
492   TestApplication application;
493   tet_infoline("Test application of alpha mask to larger RGBA8888 image");
494
495   unsigned int       width    = 10u;
496   unsigned int       height   = 10u;
497   Devel::PixelBuffer maskData = Devel::PixelBuffer::New(width, height, Pixel::L8);
498   Mask1stQuadrant(maskData);
499
500   width                        = 20u;
501   height                       = 20u;
502   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
503   FillCheckerboard(imageData);
504
505   imageData.ApplyMask(maskData, 1.0f, false);
506
507   // Test that the pixel format has been promoted to RGBA8888
508   DALI_TEST_EQUALS(imageData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION);
509
510   // Test that a pixel in the first quadrant has no alpha value
511   unsigned char* buffer = imageData.GetBuffer();
512   DALI_TEST_EQUALS(buffer[3], 0x00u, TEST_LOCATION);
513   DALI_TEST_EQUALS(buffer[7], 0x00u, TEST_LOCATION);
514
515   // Test that an even pixel in the second quadrant has no alpha value
516   DALI_TEST_EQUALS(buffer[43], 0x00u, TEST_LOCATION);
517
518   // Test that an odd pixel in the second quadrant has full alpha value
519   DALI_TEST_EQUALS(buffer[47], 0xffu, TEST_LOCATION);
520
521   END_TEST;
522 }
523
524 int UtcDaliPixelBufferMask05(void)
525 {
526   TestApplication application;
527   tet_infoline("Test application of alpha mask to smaller RGBA8888 image");
528
529   unsigned int       width    = 20u;
530   unsigned int       height   = 20u;
531   Devel::PixelBuffer maskData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
532   Mask1stQuadrant(maskData);
533
534   width                        = 10u;
535   height                       = 10u;
536   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
537   FillCheckerboard(imageData);
538
539   imageData.ApplyMask(maskData, 1.0f, false);
540
541   // Test that the pixel format has been promoted to RGBA8888
542   DALI_TEST_EQUALS(imageData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION);
543
544   // Test that a pixel in the first quadrant has no alpha value
545   unsigned char* buffer = imageData.GetBuffer();
546   DALI_TEST_EQUALS(buffer[3], 0x00u, TEST_LOCATION);
547   DALI_TEST_EQUALS(buffer[7], 0x00u, TEST_LOCATION);
548
549   // Test that an odd pixel in the second quadrant has full alpha value
550   DALI_TEST_EQUALS(buffer[39], 0xffu, TEST_LOCATION);
551
552   // Test that an even pixel in the second quadrant has no alpha value
553   DALI_TEST_EQUALS(buffer[27], 0x00u, TEST_LOCATION);
554
555   END_TEST;
556 }
557
558 int UtcDaliPixelBufferMask06(void)
559 {
560   TestApplication application;
561   tet_infoline("Test application of alpha mask to same size RGBA8888 image");
562
563   unsigned int       width    = 10u;
564   unsigned int       height   = 10u;
565   Devel::PixelBuffer maskData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
566   Mask1stQuadrant(maskData);
567
568   width                        = 10u;
569   height                       = 10u;
570   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
571   FillCheckerboard(imageData);
572
573   imageData.ApplyMask(maskData, 1.0f, false);
574
575   // Test that the pixel format has been promoted to RGBA8888
576   DALI_TEST_EQUALS(imageData.GetPixelFormat(), Pixel::RGBA8888, TEST_LOCATION);
577
578   // Test that a pixel in the first quadrant has no alpha value
579   unsigned char* buffer = imageData.GetBuffer();
580   DALI_TEST_EQUALS(buffer[3], 0x00u, TEST_LOCATION);
581   DALI_TEST_EQUALS(buffer[7], 0x00u, TEST_LOCATION);
582
583   // Test that an odd pixel in the second quadrant has full alpha value
584   DALI_TEST_EQUALS(buffer[39], 0xffu, TEST_LOCATION);
585
586   // Test that an even pixel in the second quadrant has no alpha value
587   DALI_TEST_EQUALS(buffer[27], 0x00u, TEST_LOCATION);
588
589   END_TEST;
590 }
591
592 int UtcDaliPixelBufferMask07(void)
593 {
594   TestApplication application;
595   tet_infoline("Test scaling of source image to match alpha mask");
596
597   unsigned int       width    = 20u;
598   unsigned int       height   = 20u;
599   Devel::PixelBuffer maskData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
600   MaskCenterSquare(maskData);
601
602   // +----------+
603   // |  XXXXXX  |
604   // |  XXXXXX  |
605   // |  XXXXXX  |
606   // |  XXXXXX  |
607   // *----------+
608
609   width                        = 10u;
610   height                       = 10u;
611   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
612   AlternateQuadrants(imageData);
613
614   // +-----XXXXX+
615   // |     XXXXX|
616   // |     XXXXX|
617   // |XXXXX     |
618   // |XXXXX     |
619   // *XXXXX-----+
620
621   imageData.ApplyMask(maskData, 2.0f, true);
622
623   // +----------+
624   // |     XXX  |
625   // |     XXX  |
626   // |  XXX     |
627   // |  XXX     |
628   // *----------+
629
630   tet_infoline("Test that the image has been scaled to match the alpha mask");
631   DALI_TEST_EQUALS(imageData.GetWidth(), 20, TEST_LOCATION);
632   DALI_TEST_EQUALS(imageData.GetHeight(), 20, TEST_LOCATION);
633
634   tet_infoline("Test that pixels in the outer eighths have no alpha\n");
635
636   DALI_TEST_EQUALS(GetAlphaAt(imageData, 0, 0), 0x00u, TEST_LOCATION);
637   DALI_TEST_EQUALS(GetAlphaAt(imageData, 9, 4), 0x00u, TEST_LOCATION);
638   DALI_TEST_EQUALS(GetAlphaAt(imageData, 15, 4), 0x00u, TEST_LOCATION);
639   DALI_TEST_EQUALS(GetAlphaAt(imageData, 19, 4), 0x00u, TEST_LOCATION);
640
641   DALI_TEST_EQUALS(GetAlphaAt(imageData, 0, 19), 0x00u, TEST_LOCATION);
642   DALI_TEST_EQUALS(GetAlphaAt(imageData, 8, 18), 0x00u, TEST_LOCATION);
643   DALI_TEST_EQUALS(GetAlphaAt(imageData, 15, 17), 0x00u, TEST_LOCATION);
644   DALI_TEST_EQUALS(GetAlphaAt(imageData, 19, 16), 0x00u, TEST_LOCATION);
645
646   DALI_TEST_EQUALS(GetAlphaAt(imageData, 0, 1), 0x00u, TEST_LOCATION);
647   DALI_TEST_EQUALS(GetAlphaAt(imageData, 1, 7), 0x00u, TEST_LOCATION);
648   DALI_TEST_EQUALS(GetAlphaAt(imageData, 2, 10), 0x00u, TEST_LOCATION);
649   DALI_TEST_EQUALS(GetAlphaAt(imageData, 3, 19), 0x00u, TEST_LOCATION);
650
651   DALI_TEST_EQUALS(GetAlphaAt(imageData, 19, 1), 0x00u, TEST_LOCATION);
652   DALI_TEST_EQUALS(GetAlphaAt(imageData, 18, 7), 0x00u, TEST_LOCATION);
653   DALI_TEST_EQUALS(GetAlphaAt(imageData, 17, 10), 0x00u, TEST_LOCATION);
654   DALI_TEST_EQUALS(GetAlphaAt(imageData, 16, 19), 0x00u, TEST_LOCATION);
655
656   tet_infoline("Test that pixels in the center have full alpha\n");
657
658   DALI_TEST_EQUALS(GetAlphaAt(imageData, 12, 8), 0xffu, TEST_LOCATION);
659   DALI_TEST_EQUALS(GetAlphaAt(imageData, 8, 12), 0xffu, TEST_LOCATION);
660
661   END_TEST;
662 }
663
664 int UtcDaliPixelBufferMask08(void)
665 {
666   TestApplication application;
667   tet_infoline("Test scaling of source image to larger than the alpha mask");
668
669   unsigned int       width    = 32u;
670   unsigned int       height   = 20u;
671   Devel::PixelBuffer maskData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
672   AlternateQuadrants(maskData);
673
674   // +-----XXXXX+
675   // |     XXXXX|
676   // |     XXXXX|
677   // |XXXXX     |
678   // |XXXXX     |
679   // *XXXXX-----+
680
681   width                        = 20u;
682   height                       = 16u;
683   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
684   MaskCenterSquare(imageData);
685
686   // +----------+
687   // |  XXXXXX  |
688   // |  XXXXXX  |
689   // |  XXXXXX  |
690   // |  XXXXXX  |
691   // *----------+
692
693   imageData.ApplyMask(maskData, 4.0f, true);
694
695   // +-----XXXXX+   quadrant
696   // |     XXXXX|    1    2
697   // |     XXXXX|
698   // |XXXXX     |    4    3
699   // |XXXXX     |
700   // *XXXXX-----+
701
702   tet_infoline("Test that the image has been scaled and cropped to match the alpha mask");
703   DALI_TEST_EQUALS(imageData.GetWidth(), 32, TEST_LOCATION);
704   DALI_TEST_EQUALS(imageData.GetHeight(), 20, TEST_LOCATION);
705
706   tet_infoline("Test that the image has been resized (the center square should now fill the image)\n");
707   tet_infoline("Test that the first quadrant has no alpha");
708   DALI_TEST_EQUALS(GetAlphaAt(imageData, 0, 0), 0x00u, TEST_LOCATION);
709   DALI_TEST_EQUALS(GetAlphaAt(imageData, 5, 4), 0x00u, TEST_LOCATION);
710   DALI_TEST_EQUALS(GetAlphaAt(imageData, 5, 8), 0x00u, TEST_LOCATION);
711   DALI_TEST_EQUALS(GetAlphaAt(imageData, 14, 8), 0x00u, TEST_LOCATION);
712
713   tet_infoline("Test that the second quadrant has alpha and data");
714   DALI_TEST_EQUALS(GetAlphaAt(imageData, 18, 0), 0xffu, TEST_LOCATION);
715   DALI_TEST_EQUALS(GetAlphaAt(imageData, 30, 1), 0xffu, TEST_LOCATION);
716   DALI_TEST_EQUALS(GetAlphaAt(imageData, 30, 8), 0xffu, TEST_LOCATION);
717   DALI_TEST_EQUALS(GetAlphaAt(imageData, 19, 8), 0xffu, TEST_LOCATION);
718
719   tet_infoline("Test that the third quadrant has no alpha");
720   DALI_TEST_EQUALS(GetAlphaAt(imageData, 18, 12), 0x00u, TEST_LOCATION);
721   DALI_TEST_EQUALS(GetAlphaAt(imageData, 31, 12), 0x00u, TEST_LOCATION);
722   DALI_TEST_EQUALS(GetAlphaAt(imageData, 31, 19), 0x00u, TEST_LOCATION);
723   DALI_TEST_EQUALS(GetAlphaAt(imageData, 18, 19), 0x00u, TEST_LOCATION);
724
725   tet_infoline("Test that the fourth quadrant has alpha and data");
726   DALI_TEST_EQUALS(GetAlphaAt(imageData, 1, 12), 0xffu, TEST_LOCATION);
727   DALI_TEST_EQUALS(GetAlphaAt(imageData, 7, 12), 0xffu, TEST_LOCATION);
728   DALI_TEST_EQUALS(GetAlphaAt(imageData, 7, 19), 0xffu, TEST_LOCATION);
729   DALI_TEST_EQUALS(GetAlphaAt(imageData, 1, 19), 0xffu, TEST_LOCATION);
730
731   END_TEST;
732 }
733
734 int UtcDaliPixelBufferMask09(void)
735 {
736   TestApplication application;
737   tet_infoline("Test scaling of large source image to larger than the alpha mask");
738
739   unsigned int       width    = 32u;
740   unsigned int       height   = 20u;
741   Devel::PixelBuffer maskData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
742   AlternateQuadrants(maskData);
743
744   // +-----XXXXX+
745   // |     XXXXX|
746   // |     XXXXX|
747   // |XXXXX     |
748   // |XXXXX     |
749   // *XXXXX-----+
750
751   width                        = 40u;
752   height                       = 50u;
753   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(width, height, Pixel::RGBA8888);
754   MaskCenterSquare(imageData);
755
756   // +----------+
757   // |  XXXXXX  |
758   // |  XXXXXX  |
759   // |  XXXXXX  |
760   // |  XXXXXX  |
761   // *----------+
762
763   imageData.ApplyMask(maskData, 1.6f, true);
764
765   // +-----XXXXX+   quadrant
766   // |     XXXXX|    1    2
767   // |     XXXXX|
768   // |XXXXX     |    4    3
769   // |XXXXX     |
770   // *XXXXX-----+
771
772   tet_infoline("Test that the image has been scaled and cropped to match the alpha mask");
773   DALI_TEST_EQUALS(imageData.GetWidth(), 32, TEST_LOCATION);
774   DALI_TEST_EQUALS(imageData.GetHeight(), 20, TEST_LOCATION);
775
776   tet_infoline("Test that the image has been resized (the center square should now fill the image)\n");
777   tet_infoline("Test that the first quadrant has no alpha");
778   DALI_TEST_EQUALS(GetAlphaAt(imageData, 0, 0), 0x00u, TEST_LOCATION);
779   DALI_TEST_EQUALS(GetAlphaAt(imageData, 5, 4), 0x00u, TEST_LOCATION);
780   DALI_TEST_EQUALS(GetAlphaAt(imageData, 5, 8), 0x00u, TEST_LOCATION);
781   DALI_TEST_EQUALS(GetAlphaAt(imageData, 14, 8), 0x00u, TEST_LOCATION);
782
783   tet_infoline("Test that the second quadrant has alpha and data");
784   DALI_TEST_EQUALS(GetAlphaAt(imageData, 18, 0), 0xffu, TEST_LOCATION);
785   DALI_TEST_EQUALS(GetAlphaAt(imageData, 30, 1), 0xffu, TEST_LOCATION);
786   DALI_TEST_EQUALS(GetAlphaAt(imageData, 30, 8), 0xffu, TEST_LOCATION);
787   DALI_TEST_EQUALS(GetAlphaAt(imageData, 19, 8), 0xffu, TEST_LOCATION);
788
789   tet_infoline("Test that the third quadrant has no alpha");
790   DALI_TEST_EQUALS(GetAlphaAt(imageData, 18, 12), 0x00u, TEST_LOCATION);
791   DALI_TEST_EQUALS(GetAlphaAt(imageData, 31, 12), 0x00u, TEST_LOCATION);
792   DALI_TEST_EQUALS(GetAlphaAt(imageData, 31, 19), 0x00u, TEST_LOCATION);
793   DALI_TEST_EQUALS(GetAlphaAt(imageData, 18, 19), 0x00u, TEST_LOCATION);
794
795   tet_infoline("Test that the fourth quadrant has alpha and data");
796   DALI_TEST_EQUALS(GetAlphaAt(imageData, 1, 12), 0xffu, TEST_LOCATION);
797   DALI_TEST_EQUALS(GetAlphaAt(imageData, 7, 12), 0xffu, TEST_LOCATION);
798   DALI_TEST_EQUALS(GetAlphaAt(imageData, 7, 19), 0xffu, TEST_LOCATION);
799   DALI_TEST_EQUALS(GetAlphaAt(imageData, 1, 19), 0xffu, TEST_LOCATION);
800
801   END_TEST;
802 }
803
804 int UtcDaliPixelBufferGaussianBlur(void)
805 {
806   TestApplication application;
807
808   Devel::PixelBuffer imageData = Devel::PixelBuffer::New(10, 10, Pixel::RGBA8888);
809   FillCheckerboard(imageData);
810
811   DALI_TEST_EQUALS(imageData.GetWidth(), 10, TEST_LOCATION);
812   DALI_TEST_EQUALS(imageData.GetHeight(), 10, TEST_LOCATION);
813
814   unsigned char* buffer = imageData.GetBuffer();
815
816   // Test that an even pixel in the odd row has full alpha value
817   DALI_TEST_EQUALS(buffer[43], 0xffu, TEST_LOCATION);
818
819   // Test that an even pixel in the even row has no alpha value
820   DALI_TEST_EQUALS(buffer[55], 0x00u, TEST_LOCATION);
821
822   imageData.ApplyGaussianBlur(0.0f);
823
824   // Test that the pixels' alpha values are not changed because there is no blur
825   DALI_TEST_EQUALS(buffer[43], 0xffu, TEST_LOCATION);
826   DALI_TEST_EQUALS(buffer[55], 0x00u, TEST_LOCATION);
827
828   imageData.ApplyGaussianBlur(1.0f);
829
830   // Test that the pixels' alpha values are changed after applying gaussian blur
831   DALI_TEST_EQUALS(buffer[43], 0x7Au, TEST_LOCATION);
832   DALI_TEST_EQUALS(buffer[55], 0x7Eu, TEST_LOCATION);
833
834   END_TEST;
835 }