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