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