Bitmap adaptor patch 1 of 1 - Replace all uses of the Bitmap class with new simpler...
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor-internal / image-loaders.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 #include "image-loaders.h"
18 #include <dali-test-suite-utils.h>
19
20 AutoCloseFile::AutoCloseFile( FILE *fp )
21 : filePtr( fp )
22 {
23 }
24
25 AutoCloseFile::~AutoCloseFile()
26 {
27   if ( filePtr )
28   {
29     fclose( filePtr );
30   }
31 }
32
33
34 ImageDetails::ImageDetails( const char * const _name, unsigned int _width, unsigned int _height )
35 : name( _name ),
36   width( _width ),
37   height( _height ),
38   reportedWidth( _width ),
39   reportedHeight( _height ),
40   refBufferSize( _width * _height ),
41   refBuffer( new Dali::PixelBuffer[ refBufferSize ] )
42 {
43   LoadBuffer();
44 }
45
46 ImageDetails::ImageDetails( const char * const _name, unsigned int _width, unsigned int _height, unsigned int _reportedWidth, unsigned int _reportedHeight )
47 : name( _name ),
48   width( _width ),
49   height( _height ),
50   reportedWidth( _reportedWidth ),
51   reportedHeight( _reportedHeight ),
52   refBufferSize( _width * _height ),
53   refBuffer( new Dali::PixelBuffer[ refBufferSize ] )
54 {
55   LoadBuffer();
56 }
57
58 ImageDetails::~ImageDetails()
59 {
60   delete [] refBuffer;
61 }
62
63 void ImageDetails::LoadBuffer()
64 {
65   // Load the reference buffer from the buffer file
66
67   std::string refBufferFilename( name + ".buffer" );
68   FILE *fp = fopen ( refBufferFilename.c_str(), "rb" );
69   AutoCloseFile autoCloseBufferFile( fp );
70
71   if ( fp )
72   {
73     fread( refBuffer, sizeof( Dali::PixelBuffer ), refBufferSize, fp );
74   }
75 }
76
77
78 LoadFunctions::LoadFunctions( LoadBitmapHeaderFunction _header, LoadBitmapFunction _loader )
79 : header( _header ),
80   loader( _loader )
81 {
82 }
83
84 void TestImageLoading( const ImageDetails& image, const LoadFunctions& functions )
85 {
86   FILE* fp = fopen( image.name.c_str() , "rb" );
87   AutoCloseFile autoClose( fp );
88   DALI_TEST_CHECK( fp != NULL );
89
90   // Check the header file.
91
92   unsigned int width(0), height(0);
93   Dali::ImageAttributes attributes;
94   DALI_TEST_CHECK( functions.header( fp, attributes, width, height ) );
95
96   DALI_TEST_EQUALS( width,  image.reportedWidth,  TEST_LOCATION );
97   DALI_TEST_EQUALS( height, image.reportedHeight, TEST_LOCATION );
98
99   // Loading the header moves the pointer within the file so reset to start of file.
100   fseek( fp, 0, 0 );
101
102   // Create a bitmap object and store a pointer to that object so it is destroyed at the end.
103   Dali::Integration::ImageDataPtr bitmap = Integration::NewBitmapImageData( 64, 64, Pixel::A8 );
104
105   // Load Bitmap and check its return values.
106   DALI_TEST_CHECK( functions.loader( fp, attributes, bitmap ) );
107   DALI_TEST_EQUALS( image.width,  attributes.GetWidth(),  TEST_LOCATION );
108   DALI_TEST_EQUALS( image.height, attributes.GetHeight(), TEST_LOCATION );
109
110   // Compare buffer generated with reference buffer.
111   Dali::PixelBuffer* bufferPtr( bitmap->GetBuffer() );
112   Dali::PixelBuffer* refBufferPtr( image.refBuffer );
113   for ( unsigned int i = 0; i < image.refBufferSize; ++i, ++bufferPtr, ++refBufferPtr )
114   {
115     if( *bufferPtr != *refBufferPtr )
116     {
117       tet_result( TET_FAIL );
118       tet_printf("%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);
119       break;
120     }
121   }
122 }
123
124 void DumpImageBufferToTempFile( std::string filename, std::string targetFilename, const LoadFunctions& functions )
125 {
126   FILE* fp = fopen( filename.c_str() , "rb" );
127   AutoCloseFile autoClose( fp );
128
129   Dali::Integration::ImageDataPtr bitmap = Integration::NewBitmapImageData( 64, 64, Pixel::A8 );
130   Dali::ImageAttributes attributes;
131
132   DALI_TEST_CHECK( functions.loader( fp, attributes, bitmap ) );
133
134   Dali::PixelBuffer* bufferPtr( bitmap->GetBuffer() );
135
136   FILE* writeFp = fopen( targetFilename.c_str(), "wb" );
137   AutoCloseFile autoCloseWrite( writeFp );
138   fwrite( bufferPtr, sizeof( Dali::PixelBuffer ), attributes.GetWidth() * attributes.GetHeight(), writeFp );
139 }