Merge "Add an API for 'create,window' event." into devel/master
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor-internal / utc-Dali-Internal-PixelBuffer.cpp
1 /*
2  * Copyright (c) 2014 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
18 #include <iostream>
19
20 #include <dali/public-api/dali-core.h>
21 #include <stdlib.h>
22
23 #include <dali-test-suite-utils.h>
24
25 // Internal headers are allowed here
26
27 #include <dali/devel-api/adaptor-framework/image-loading.h>
28 #include <dali/internal/imaging/common/pixel-manipulation.h>
29
30 using namespace Dali;
31 using namespace Dali::Internal::Adaptor;
32
33 namespace
34 {
35 // resolution: 96*96, pixel format: LA88
36 const char* TEST_IMAGE_LA88 = TEST_IMAGE_DIR "/circle1-LA88.png";
37
38 } // namespace
39
40 void utc_dali_internal_pixel_data_startup()
41 {
42   test_return_value = TET_UNDEF;
43 }
44
45 void utc_dali_internal_pixel_data_cleanup()
46 {
47   test_return_value = TET_PASS;
48 }
49
50 const char* ChannelToString(Dali::Internal::Adaptor::Channel channel)
51 {
52   switch(channel)
53   {
54     case LUMINANCE:
55       return "Luminance";
56     case RED:
57       return "Red";
58     case GREEN:
59       return "Green";
60     case BLUE:
61       return "Blue";
62     case ALPHA:
63       return "Alpha";
64     default:
65       return "Unknown";
66   }
67 }
68
69 const char* FormatToString(Dali::Pixel::Format format)
70 {
71   switch(format)
72   {
73     case Dali::Pixel::A8:
74       return "A8";
75     case Dali::Pixel::L8:
76       return "L8";
77     case Dali::Pixel::LA88:
78       return "LA88";
79     case Dali::Pixel::RGB565:
80       return "RGB565";
81     case Dali::Pixel::BGR565:
82       return "BGR565";
83     case Dali::Pixel::RGBA4444:
84       return "RGBA4444";
85     case Dali::Pixel::BGRA4444:
86       return "BGRA4444";
87     case Dali::Pixel::RGBA5551:
88       return "RGBA5551";
89     case Dali::Pixel::BGRA5551:
90       return "BGRA5551";
91
92     case Dali::Pixel::RGB888:
93       return "RGB888";
94     case Dali::Pixel::RGBA8888:
95       return "RGBA8888";
96     case Dali::Pixel::BGRA8888:
97       return "BGRA8888";
98
99     default:
100       return "Unknown";
101   }
102 }
103
104 int UtcDaliPixelManipulation01(void)
105 {
106   tet_infoline("Testing Dali::Internal::AdaptorManipulation HasChannel");
107
108   DALI_TEST_EQUALS(Dali::Internal::Adaptor::HasChannel(Dali::Pixel::A8, Dali::Internal::Adaptor::ALPHA), true, TEST_LOCATION);
109   DALI_TEST_EQUALS(Dali::Internal::Adaptor::HasChannel(Dali::Pixel::A8, Dali::Internal::Adaptor::LUMINANCE), false, TEST_LOCATION);
110
111   DALI_TEST_EQUALS(Dali::Internal::Adaptor::HasChannel(Dali::Pixel::L8, Dali::Internal::Adaptor::LUMINANCE), true, TEST_LOCATION);
112   DALI_TEST_EQUALS(Dali::Internal::Adaptor::HasChannel(Dali::Pixel::L8, Dali::Internal::Adaptor::ALPHA), false, TEST_LOCATION);
113
114   DALI_TEST_EQUALS(Dali::Internal::Adaptor::HasChannel(Dali::Pixel::LA88, Dali::Internal::Adaptor::LUMINANCE), true, TEST_LOCATION);
115   DALI_TEST_EQUALS(Dali::Internal::Adaptor::HasChannel(Dali::Pixel::LA88, Dali::Internal::Adaptor::ALPHA), true, TEST_LOCATION);
116   DALI_TEST_EQUALS(Dali::Internal::Adaptor::HasChannel(Dali::Pixel::LA88, Dali::Internal::Adaptor::RED), false, TEST_LOCATION);
117
118   DALI_TEST_EQUALS(Dali::Internal::Adaptor::HasChannel(Dali::Pixel::RGB565, Dali::Internal::Adaptor::RED), true, TEST_LOCATION);
119   DALI_TEST_EQUALS(Dali::Internal::Adaptor::HasChannel(Dali::Pixel::RGB565, Dali::Internal::Adaptor::GREEN), true, TEST_LOCATION);
120   DALI_TEST_EQUALS(Dali::Internal::Adaptor::HasChannel(Dali::Pixel::RGB565, Dali::Internal::Adaptor::BLUE), true, TEST_LOCATION);
121   DALI_TEST_EQUALS(Dali::Internal::Adaptor::HasChannel(Dali::Pixel::RGB565, Dali::Internal::Adaptor::LUMINANCE), false, TEST_LOCATION);
122
123   DALI_TEST_EQUALS(Dali::Internal::Adaptor::HasChannel(Dali::Pixel::RGBA8888, Dali::Internal::Adaptor::RED), true, TEST_LOCATION);
124   DALI_TEST_EQUALS(Dali::Internal::Adaptor::HasChannel(Dali::Pixel::RGBA8888, Dali::Internal::Adaptor::GREEN), true, TEST_LOCATION);
125   DALI_TEST_EQUALS(Dali::Internal::Adaptor::HasChannel(Dali::Pixel::RGBA8888, Dali::Internal::Adaptor::BLUE), true, TEST_LOCATION);
126   DALI_TEST_EQUALS(Dali::Internal::Adaptor::HasChannel(Dali::Pixel::RGBA8888, Dali::Internal::Adaptor::ALPHA), true, TEST_LOCATION);
127   DALI_TEST_EQUALS(Dali::Internal::Adaptor::HasChannel(Dali::Pixel::RGBA8888, Dali::Internal::Adaptor::LUMINANCE), false, TEST_LOCATION);
128
129   DALI_TEST_EQUALS(Dali::Internal::Adaptor::HasChannel(Dali::Pixel::COMPRESSED_RGBA_ASTC_10x6_KHR, Dali::Internal::Adaptor::BLUE), false, TEST_LOCATION);
130
131   END_TEST;
132 }
133
134 int UtcDaliPixelManipulation02(void)
135 {
136   tet_infoline("Testing Dali::Internal::AdaptorManipulation Read/WriteChannel");
137
138   unsigned char pixel[4] = {0, 0, 0, 0};
139
140   for(int formatIdx = 1; formatIdx < Dali::Pixel::COMPRESSED_R11_EAC; ++formatIdx)
141   {
142     pixel[0] = 0xFF;
143     pixel[1] = 0xFF;
144     pixel[2] = 0xFF;
145     pixel[3] = 0xFF;
146
147     for(int channelIdx = 0; channelIdx < Dali::Internal::Adaptor::MAX_NUMBER_OF_CHANNELS; ++channelIdx)
148     {
149       Dali::Pixel::Format              format  = static_cast<Dali::Pixel::Format>(formatIdx);
150       Dali::Internal::Adaptor::Channel channel = static_cast<Dali::Internal::Adaptor::Channel>(channelIdx);
151       if(Dali::Internal::Adaptor::HasChannel(format, channel))
152       {
153         Dali::Internal::Adaptor::WriteChannel(&pixel[0], format, channel, 0x15);
154         unsigned int value = Dali::Internal::Adaptor::ReadChannel(&pixel[0], format, channel);
155
156         tet_printf("Testing writing and reading to %s channel in %s format:\n",
157                    ChannelToString(channel),
158                    FormatToString(format));
159
160         if(channel == Dali::Internal::Adaptor::ALPHA && (format == Dali::Pixel::RGBA5551 || format == Dali::Pixel::BGRA5551))
161         {
162           DALI_TEST_EQUALS(value, 0x1, TEST_LOCATION);
163         }
164         else if(format == Dali::Pixel::RGBA4444 || format == Dali::Pixel::BGRA4444)
165         {
166           DALI_TEST_EQUALS(value, 0x05, TEST_LOCATION);
167         }
168         else
169         {
170           DALI_TEST_EQUALS(value, 0x15, TEST_LOCATION);
171         }
172       }
173     }
174   }
175
176   END_TEST;
177 }
178
179 int UtcDaliPixelManipulationLA88(void)
180 {
181   tet_infoline("Testing Dali::Internal::AdaptorManipulation Read/WriteChannel - LA88 format");
182
183   Devel::PixelBuffer pixelBuffer = Dali::LoadImageFromFile(TEST_IMAGE_LA88);
184   DALI_TEST_CHECK(pixelBuffer);
185
186   unsigned int width  = pixelBuffer.GetWidth();
187   unsigned int height = pixelBuffer.GetHeight();
188   DALI_TEST_EQUALS(width, 96u, TEST_LOCATION);
189   DALI_TEST_EQUALS(height, 96u, TEST_LOCATION);
190   DALI_TEST_EQUALS(pixelBuffer.GetPixelFormat(), Pixel::LA88, TEST_LOCATION);
191
192   unsigned char* pixel = pixelBuffer.GetBuffer();
193   unsigned int   value;
194
195   value = Dali::Internal::Adaptor::ReadChannel(&pixel[0], Dali::Pixel::LA88, Dali::Internal::Adaptor::LUMINANCE);
196   DALI_TEST_EQUALS(value, 0x0, TEST_LOCATION);
197   value = Dali::Internal::Adaptor::ReadChannel(&pixel[0], Dali::Pixel::LA88, Dali::Internal::Adaptor::ALPHA);
198   DALI_TEST_EQUALS(value, 0x0, TEST_LOCATION);
199
200   // Get center pixel
201   unsigned int stride = width * Pixel::GetBytesPerPixel(Dali::Pixel::LA88);
202   unsigned int center = height / 2 * stride + width / 2 * Pixel::GetBytesPerPixel(Dali::Pixel::LA88);
203   value               = Dali::Internal::Adaptor::ReadChannel(&pixel[center], Dali::Pixel::LA88, Dali::Internal::Adaptor::LUMINANCE);
204   DALI_TEST_EQUALS(value, 0x0, TEST_LOCATION);
205   value = Dali::Internal::Adaptor::ReadChannel(&pixel[center], Dali::Pixel::LA88, Dali::Internal::Adaptor::ALPHA);
206   DALI_TEST_EQUALS(value, 0xFF, TEST_LOCATION);
207
208   END_TEST;
209 }
210
211 int UtcDaliPixelManipulation03N(void)
212 {
213   tet_infoline("Testing Dali::Internal::AdaptorManipulation Read/WriteChannel");
214
215   unsigned char pixel[4] = {0, 0, 0, 0};
216
217   for(int formatIdx = 1; formatIdx < Dali::Pixel::COMPRESSED_R11_EAC; ++formatIdx)
218   {
219     pixel[0] = 0xFF;
220     pixel[1] = 0xFF;
221     pixel[2] = 0xFF;
222     pixel[3] = 0xFF;
223
224     for(int channelIdx = 0; channelIdx < Dali::Internal::Adaptor::MAX_NUMBER_OF_CHANNELS; ++channelIdx)
225     {
226       Dali::Pixel::Format              format  = static_cast<Dali::Pixel::Format>(formatIdx);
227       Dali::Internal::Adaptor::Channel channel = static_cast<Dali::Internal::Adaptor::Channel>(channelIdx);
228       if(!Dali::Internal::Adaptor::HasChannel(format, channel))
229       {
230         unsigned int value = Dali::Internal::Adaptor::ReadChannel(&pixel[0], format, channel);
231
232         tet_printf("Testing reading from %s channel in %s format:\n",
233                    ChannelToString(channel),
234                    FormatToString(format));
235
236         DALI_TEST_EQUALS(value, 0x00, TEST_LOCATION);
237       }
238     }
239   }
240
241   END_TEST;
242 }