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