c3f7abee9701472d37999dd2a7aeb2f2611cd57d
[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
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   // Create a bitmap object and store a pointer to that object so it is destroyed at the end.
111   Dali::Integration::Bitmap * bitmap = Dali::Integration::Bitmap::New( bitmapProfile, ResourcePolicy::OWNED_RETAIN  );
112   Dali::Integration::BitmapPtr bitmapPtr( bitmap );
113
114   // Load Bitmap and check its return values.
115   DALI_TEST_CHECK( functions.loader( input, *bitmap ) );
116   DALI_TEST_EQUALS( image.width,  bitmap->GetImageWidth(),  TEST_LOCATION );
117   DALI_TEST_EQUALS( image.height, bitmap->GetImageHeight(), TEST_LOCATION );
118
119   // Compare buffer generated with reference buffer.
120   Dali::PixelBuffer* bufferPtr( bitmapPtr->GetBuffer() );
121   Dali::PixelBuffer* refBufferPtr( image.refBuffer );
122   for ( unsigned int i = 0; i < image.refBufferSize; ++i, ++bufferPtr, ++refBufferPtr )
123   {
124     if( *bufferPtr != *refBufferPtr )
125     {
126       tet_result( TET_FAIL );
127       tet_printf("%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);
128       break;
129     }
130   }
131 }
132
133 void CompareLoadedImageData( const ImageDetails& image, const LoadFunctions& functions, const uint32_t* master )
134 {
135   FILE* filePointer = fopen( image.name.c_str() , "rb" );
136   AutoCloseFile autoClose( filePointer );
137   DALI_TEST_CHECK( filePointer != NULL );
138
139   // Check the header file.
140   unsigned int width = 0, height = 0;
141   const Dali::TizenPlatform::ImageLoader::Input input( filePointer );
142   DALI_TEST_CHECK( functions.header( input, width, height ) );
143
144   DALI_TEST_EQUALS( width,  image.reportedWidth,  TEST_LOCATION );
145   DALI_TEST_EQUALS( height, image.reportedHeight, TEST_LOCATION );
146
147   // Loading the header moves the pointer within the file so reset to start of file.
148   fseek( filePointer, 0, SEEK_SET );
149
150   // Create a bitmap object and store a pointer to that object so it is destroyed at the end.
151   Dali::Integration::Bitmap * bitmap = Dali::Integration::Bitmap::New( Dali::Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN  );
152   Dali::Integration::BitmapPtr bitmapPointer( bitmap );
153
154   // Load Bitmap and check its return values.
155   DALI_TEST_CHECK( functions.loader( input, *bitmap ) );
156   DALI_TEST_EQUALS( image.width,  bitmap->GetImageWidth(),  TEST_LOCATION );
157   DALI_TEST_EQUALS( image.height, bitmap->GetImageHeight(), TEST_LOCATION );
158
159   // Check the bytes per pixel.
160   const Pixel::Format pixelFormat = bitmap->GetPixelFormat();
161   const unsigned int bytesPerPixel = Pixel::GetBytesPerPixel( pixelFormat );
162
163   // Compare buffer generated with reference buffer.
164   Dali::PixelBuffer* pBitmapData( bitmapPointer->GetBuffer() );
165   const uint32_t* pMaster( master );
166
167   // Loop through each pixel in the bitmap.
168   for ( unsigned int i = 0; i < image.refBufferSize; ++i, ++pMaster )
169   {
170     unsigned int color = 0;
171     // Loop through each byte per pixel, to build up a color value for that pixel.
172     for( unsigned int j = 0; j < bytesPerPixel; ++j, ++pBitmapData )
173     {
174       color = ( color << 8 ) | *pBitmapData;
175     }
176
177     // Check the color value is what we expect.
178     DALI_TEST_EQUALS( color, *pMaster, TEST_LOCATION );
179   }
180 }
181
182 void DumpImageBufferToTempFile( std::string filename, std::string targetFilename, const LoadFunctions& functions )
183 {
184   FILE* fp = fopen( filename.c_str() , "rb" );
185   AutoCloseFile autoClose( fp );
186
187   Dali::Integration::Bitmap* bitmap = Dali::Integration::Bitmap::New( Dali::Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,  ResourcePolicy::OWNED_RETAIN );
188   Dali::Integration::BitmapPtr bitmapPtr( bitmap );
189   const Dali::TizenPlatform::ImageLoader::Input input( fp );
190
191   DALI_TEST_CHECK( functions.loader( input, *bitmap ) );
192
193   Dali::PixelBuffer* bufferPtr( bitmapPtr->GetBuffer() );
194
195   FILE* writeFp = fopen( targetFilename.c_str(), "wb" );
196   AutoCloseFile autoCloseWrite( writeFp );
197   fwrite( bufferPtr, 1, bitmap->GetBufferSize(), writeFp );
198 }