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