2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
17 #include "image-loaders.h"
18 #include <dali-test-suite-utils.h>
20 AutoCloseFile::AutoCloseFile( FILE *fp )
25 AutoCloseFile::~AutoCloseFile()
34 ImageDetails::ImageDetails( const char * const _name, unsigned int _width, unsigned int _height )
38 reportedWidth( _width ),
39 reportedHeight( _height ),
40 refBufferSize( _width * _height ),
41 refBuffer( new Dali::PixelBuffer[ refBufferSize ] )
46 ImageDetails::ImageDetails( const char * const _name, unsigned int _width, unsigned int _height, unsigned int _reportedWidth, unsigned int _reportedHeight )
50 reportedWidth( _reportedWidth ),
51 reportedHeight( _reportedHeight ),
52 refBufferSize( _width * _height ),
53 refBuffer( new Dali::PixelBuffer[ refBufferSize ] )
58 ImageDetails::~ImageDetails()
63 void ImageDetails::LoadBuffer()
65 // Load the reference buffer from the buffer file
67 std::string refBufferFilename( name + ".buffer" );
68 FILE *fp = fopen ( refBufferFilename.c_str(), "rb" );
69 AutoCloseFile autoCloseBufferFile( fp );
73 fread( refBuffer, sizeof( Dali::PixelBuffer ), refBufferSize, fp );
78 LoadFunctions::LoadFunctions( LoadBitmapHeaderFunction _header, LoadBitmapFunction _loader )
84 void TestImageLoading( const ImageDetails& image, const LoadFunctions& functions )
86 FILE* fp = fopen( image.name.c_str() , "rb" );
87 AutoCloseFile autoClose( fp );
88 DALI_TEST_CHECK( fp != NULL );
90 // Check the header file.
92 unsigned int width(0), height(0);
93 Dali::ImageAttributes attributes;
94 DALI_TEST_CHECK( functions.header( fp, attributes, width, height ) );
96 DALI_TEST_EQUALS( width, image.reportedWidth, TEST_LOCATION );
97 DALI_TEST_EQUALS( height, image.reportedHeight, TEST_LOCATION );
99 // Loading the header moves the pointer within the file so reset to start of file.
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 );
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 );
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 )
115 if( *bufferPtr != *refBufferPtr )
117 tet_result( TET_FAIL );
118 tet_printf("%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);
124 void DumpImageBufferToTempFile( std::string filename, std::string targetFilename, const LoadFunctions& functions )
126 FILE* fp = fopen( filename.c_str() , "rb" );
127 AutoCloseFile autoClose( fp );
129 Dali::Integration::ImageDataPtr bitmap = Integration::NewBitmapImageData( 64, 64, Pixel::A8 );
130 Dali::ImageAttributes attributes;
132 DALI_TEST_CHECK( functions.loader( fp, attributes, bitmap ) );
134 Dali::PixelBuffer* bufferPtr( bitmap->GetBuffer() );
136 FILE* writeFp = fopen( targetFilename.c_str(), "wb" );
137 AutoCloseFile autoCloseWrite( writeFp );
138 fwrite( bufferPtr, sizeof( Dali::PixelBuffer ), attributes.GetWidth() * attributes.GetHeight(), writeFp );