Added ASTC Native file format loader
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor-internal / utc-Dali-CompressedTextures.cpp
1 /*
2  * Copyright (c) 2016 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 // EXTERNAL INCLUDES
19 #include <iostream>
20 #include <stdlib.h>
21 #include <vector>
22 #include <dali-test-suite-utils.h>
23 #include "platform-abstractions/tizen/image-loaders/loader-ktx.h"
24 #include "platform-abstractions/tizen/image-loaders/loader-astc.h"
25
26 // INTERNAL INCLUDES
27 #include "image-loaders.h"
28
29 using namespace Dali;
30
31 // Pre-define loader functions for each image type being tested (as they are reused in different tests).
32 static const LoadFunctions KtxLoaders(  TizenPlatform::LoadKtxHeader,  TizenPlatform::LoadBitmapFromKtx  );
33 static const LoadFunctions AstcLoaders( TizenPlatform::LoadAstcHeader, TizenPlatform::LoadBitmapFromAstc );
34
35 /**
36  * This class encapsulates knowledge of testing compressed files.
37  * It requires a few input parameters per test to confirm if the file was read and understood.
38  * The fixture guarantees that each test performed is setup and closed individually, therefore run order does not matter.
39  */
40 class KtxTestFixture
41 {
42   public:
43
44     /**
45      * Constructor.
46      * Sets up the fixture.
47      */
48     KtxTestFixture( void )
49     {
50     }
51
52     /**
53      * Destructor.
54      */
55     ~KtxTestFixture()
56     {
57     }
58
59     /**
60      * This struct contains any per-test parameters.
61      * This should be added to if more properties of a file/format should be tested.
62      */
63     struct TestEntry
64     {
65         LoadFunctions loadFunctions;    ///< Used to parse the header of a given type of image.
66         std::string filename;           ///< Name of the compressed texture KTX file to load.
67         int expectedWidth;              ///< The width the texture should be.
68         int expectedHeight;             ///< The height the KTX texture should be.
69
70         TestEntry( const LoadFunctions& newLoadFunctions, std::string newFilename, int newExpectedWidth, int newExpectedHeight )
71         : loadFunctions( newLoadFunctions ),
72           filename( newFilename ),
73           expectedWidth( newExpectedWidth ),
74           expectedHeight( newExpectedHeight )
75         {
76         }
77     };
78
79   private:
80
81     typedef std::vector< TestEntry > TestContainer;
82
83   public:
84
85     /**
86      * Adds a test to be performed.
87      * @param[in] testEntry A TestEntry struct containing all the details to perform one test.
88      */
89     void AddTest( TestEntry testEntry )
90     {
91         mTests.push_back( testEntry );
92     }
93
94     /**
95      * Runs all tests created with "AddTest".
96      * This will create failures upon failing tests.
97      */
98     void RunTests()
99     {
100       for( TestContainer::iterator testIterator = mTests.begin(); testIterator != mTests.end(); ++testIterator )
101       {
102         const TestEntry& currentTest = *testIterator;
103
104         RunTest( currentTest );
105       }
106     }
107
108   private:
109
110     /**
111      * Sets up, Runs and Closes-down an individual test.
112      * @param[in] testEntry A TestEntry struct containing all the details to perform one test.
113      */
114     void RunTest( const TestEntry& testEntry )
115     {
116       FILE* fileDescriptor = fopen( testEntry.filename.c_str(), "rb" );
117       AutoCloseFile autoClose( fileDescriptor );
118       DALI_TEST_CHECK( fileDescriptor != NULL );
119
120       // Check the header file.
121       unsigned int width( 0 ), height( 0 );
122       const Dali::TizenPlatform::ImageLoader::Input input( fileDescriptor );
123
124       // Use the given loader to parse the image header.
125       DALI_TEST_CHECK( testEntry.loadFunctions.header( input, width, height ) );
126
127       DALI_TEST_EQUALS( width,  testEntry.expectedWidth,  TEST_LOCATION );
128       DALI_TEST_EQUALS( height, testEntry.expectedHeight, TEST_LOCATION );
129     }
130
131   private:
132
133     TestContainer mTests;         ///< Holds all tests to be run.
134
135 };
136
137 // KTX files (KTX is a wrapper, so can contain different compressed texture types):
138
139 int UtcDaliKtxLoaderETC(void)
140 {
141   KtxTestFixture fixture;
142
143   fixture.AddTest( KtxTestFixture::TestEntry( KtxLoaders, TEST_IMAGE_DIR "/fractal-compressed-ETC1_RGB8_OES-45x80.ktx", 45u, 80u ) );
144   fixture.AddTest( KtxTestFixture::TestEntry( KtxLoaders, TEST_IMAGE_DIR "/fractal-compressed-RGB8_ETC2-45x80.ktx", 45u, 80u ) );
145
146   fixture.RunTests();
147
148   END_TEST;
149 }
150
151 int UtcDaliKtxLoaderPVRTC(void)
152 {
153   KtxTestFixture fixture;
154
155   fixture.AddTest( KtxTestFixture::TestEntry( KtxLoaders, TEST_IMAGE_DIR "/fractal-compressed-RGB_PVRTC_4BPPV1_IMG-32x64.ktx", 32u, 64u ) );
156
157   fixture.RunTests();
158
159   END_TEST;
160 }
161
162 int UtcDaliKtxLoaderEAC(void)
163 {
164   KtxTestFixture fixture;
165
166   fixture.AddTest( KtxTestFixture::TestEntry( KtxLoaders, TEST_IMAGE_DIR "/fractal-compressed-R11_EAC-45x80.ktx", 45u, 80u ) );
167
168   fixture.RunTests();
169
170   END_TEST;
171 }
172
173 int UtcDaliKtxLoaderASTC(void)
174 {
175   KtxTestFixture fixture;
176
177   fixture.AddTest( KtxTestFixture::TestEntry( KtxLoaders, TEST_IMAGE_DIR "/fractal-compressed-RGBA_ASTC_4x4_KHR-32x64.ktx", 32u, 64u ) );
178   fixture.AddTest( KtxTestFixture::TestEntry( KtxLoaders, TEST_IMAGE_DIR "/fractal-compressed-SRBG8_ALPHA8_ASTC_4x4_KHR-32x64.ktx", 32u, 64u ) );
179
180   fixture.RunTests();
181
182   END_TEST;
183 }
184
185
186 // ASTC (Native) files:
187 int UtcDaliAstcLoader(void)
188 {
189   KtxTestFixture fixture;
190
191   fixture.AddTest( KtxTestFixture::TestEntry( AstcLoaders, TEST_IMAGE_DIR "/fractal-compressed-RGBA_ASTC_4x4_KHR-32x64.astc", 32u, 64u ) );
192
193   fixture.RunTests();
194
195   END_TEST;
196 }
197