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