843cb92f8b91dd3d91ee0064135e0f181768d2ad
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor-internal / image-loaders.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
18 #include "image-loaders.h"
19 #include <dali-test-suite-utils.h>
20 #include <dali/internal/imaging/common/pixel-buffer-impl.h>
21
22 AutoCloseFile::AutoCloseFile(FILE* fp)
23 : filePtr(fp)
24 {
25 }
26
27 AutoCloseFile::~AutoCloseFile()
28 {
29   if(filePtr)
30   {
31     fclose(filePtr);
32   }
33 }
34
35 ImageDetails::ImageDetails(const char* const _name, unsigned int _width, unsigned int _height)
36 : name(_name),
37   width(_width),
38   height(_height),
39   reportedWidth(_width),
40   reportedHeight(_height),
41   refBufferSize(0u),
42   refBuffer(nullptr)
43 {
44   LoadBuffer();
45 }
46
47 ImageDetails::ImageDetails(const char* const _name, unsigned int _width, unsigned int _height, unsigned int _reportedWidth, unsigned int _reportedHeight)
48 : name(_name),
49   width(_width),
50   height(_height),
51   reportedWidth(_reportedWidth),
52   reportedHeight(_reportedHeight),
53   refBufferSize(0u),
54   refBuffer(nullptr)
55 {
56   LoadBuffer();
57 }
58
59 ImageDetails::~ImageDetails()
60 {
61   if(refBuffer)
62   {
63     delete[] refBuffer;
64   }
65 }
66
67 void ImageDetails::LoadBuffer()
68 {
69   // Load the reference buffer from the buffer file
70   std::string   refBufferFilename(name + ".buffer");
71   FILE*         fp = fopen(refBufferFilename.c_str(), "rb");
72   AutoCloseFile autoCloseBufferFile(fp);
73
74   if(fp)
75   {
76     fseek(fp, 0, SEEK_END);
77     refBufferSize = ftell(fp);
78     fseek(fp, 0, SEEK_SET);
79     refBuffer = reinterpret_cast<Dali::Integration::PixelBuffer*>(malloc(refBufferSize));
80     fread(refBuffer, sizeof(Dali::Integration::PixelBuffer), refBufferSize, fp);
81   }
82 }
83
84 LoadFunctions::LoadFunctions(LoadBitmapHeaderFunction _header, LoadBitmapFunction _loader)
85 : header(_header),
86   loader(_loader)
87 {
88 }
89
90 void TestImageLoading(const ImageDetails& image, const LoadFunctions& functions, Dali::Integration::Bitmap::Profile bitmapProfile)
91 {
92   FILE*         fp = fopen(image.name.c_str(), "rb");
93   AutoCloseFile autoClose(fp);
94   DALI_TEST_CHECK(fp != NULL);
95
96   // Check the header file.
97
98   unsigned int                   width(0), height(0);
99   const Dali::ImageLoader::Input input(fp);
100   DALI_TEST_CHECK(functions.header(input, width, height));
101
102   DALI_TEST_EQUALS(width, image.reportedWidth, TEST_LOCATION);
103   DALI_TEST_EQUALS(height, image.reportedHeight, TEST_LOCATION);
104
105   // Loading the header moves the pointer within the file so reset to start of file.
106   fseek(fp, 0, 0);
107
108   Dali::Devel::PixelBuffer bitmap;
109
110   // Load Bitmap and check its return values.
111   DALI_TEST_CHECK(functions.loader(input, bitmap));
112   DALI_TEST_EQUALS(image.width, bitmap.GetWidth(), TEST_LOCATION);
113   DALI_TEST_EQUALS(image.height, bitmap.GetHeight(), TEST_LOCATION);
114
115   // Compare buffer generated with reference buffer.
116   Dali::Integration::PixelBuffer* bufferPtr(bitmap.GetBuffer());
117   Dali::Integration::PixelBuffer* refBufferPtr(image.refBuffer);
118   for(unsigned int i = 0; i < image.refBufferSize; ++i, ++bufferPtr, ++refBufferPtr)
119   {
120     if(*bufferPtr != *refBufferPtr)
121     {
122       tet_result(TET_FAIL);
123       tet_printf("%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);
124       break;
125     }
126   }
127 }
128
129 void CompareLoadedImageData(const ImageDetails& image, const LoadFunctions& functions, const uint32_t* master)
130 {
131   FILE*         filePointer = fopen(image.name.c_str(), "rb");
132   AutoCloseFile autoClose(filePointer);
133   DALI_TEST_CHECK(filePointer != NULL);
134
135   // Check the header file.
136   unsigned int                   width = 0, height = 0;
137   const Dali::ImageLoader::Input input(filePointer);
138   DALI_TEST_CHECK(functions.header(input, width, height));
139
140   DALI_TEST_EQUALS(width, image.reportedWidth, TEST_LOCATION);
141   DALI_TEST_EQUALS(height, image.reportedHeight, TEST_LOCATION);
142
143   // Loading the header moves the pointer within the file so reset to start of file.
144   fseek(filePointer, 0, SEEK_SET);
145
146   Dali::Devel::PixelBuffer bitmap;
147
148   // Load Bitmap and check its return values.
149   DALI_TEST_CHECK(functions.loader(input, bitmap));
150   DALI_TEST_EQUALS(image.width, bitmap.GetWidth(), TEST_LOCATION);
151   DALI_TEST_EQUALS(image.height, bitmap.GetHeight(), TEST_LOCATION);
152
153   // Check the bytes per pixel.
154   const Pixel::Format pixelFormat   = bitmap.GetPixelFormat();
155   const unsigned int  bytesPerPixel = Pixel::GetBytesPerPixel(pixelFormat);
156
157   // Compare buffer generated with reference buffer.
158   Dali::Integration::PixelBuffer* pBitmapData(bitmap.GetBuffer());
159   const uint32_t*                 pMaster(master);
160
161   // Loop through each pixel in the bitmap.
162   for(unsigned int i = 0; i < image.refBufferSize; ++i, ++pMaster)
163   {
164     unsigned int color = 0;
165     // Loop through each byte per pixel, to build up a color value for that pixel.
166     for(unsigned int j = 0; j < bytesPerPixel; ++j, ++pBitmapData)
167     {
168       color = (color << 8) | *pBitmapData;
169     }
170
171     // Check the color value is what we expect.
172     DALI_TEST_EQUALS(color, *pMaster, TEST_LOCATION);
173   }
174 }
175
176 void DumpImageBufferToTempFile(std::string filename, std::string targetFilename, const LoadFunctions& functions)
177 {
178   FILE*         fp = fopen(filename.c_str(), "rb");
179   AutoCloseFile autoClose(fp);
180
181   Dali::Devel::PixelBuffer       bitmap;
182   const Dali::ImageLoader::Input input(fp);
183
184   DALI_TEST_CHECK(functions.loader(input, bitmap));
185
186   Dali::Integration::PixelBuffer* bufferPtr(bitmap.GetBuffer());
187
188   FILE*         writeFp = fopen(targetFilename.c_str(), "wb");
189   AutoCloseFile autoCloseWrite(writeFp);
190   auto&         impl = GetImplementation(bitmap);
191   fwrite(bufferPtr, 1, impl.GetBufferSize(), writeFp);
192 }