2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "image-loaders.h"
19 #include <dali-test-suite-utils.h>
21 AutoCloseFile::AutoCloseFile( FILE *fp )
26 AutoCloseFile::~AutoCloseFile()
35 ImageDetails::ImageDetails( const char * const _name, unsigned int _width, unsigned int _height )
39 reportedWidth( _width ),
40 reportedHeight( _height ),
41 refBufferSize( _width * _height ),
42 refBuffer( new Dali::PixelBuffer[ refBufferSize ] )
47 ImageDetails::ImageDetails( const char * const _name, unsigned int _width, unsigned int _height, unsigned int _reportedWidth, unsigned int _reportedHeight )
51 reportedWidth( _reportedWidth ),
52 reportedHeight( _reportedHeight ),
53 refBufferSize( _width * _height ),
54 refBuffer( new Dali::PixelBuffer[ refBufferSize ] )
59 ImageDetails::~ImageDetails()
64 void ImageDetails::LoadBuffer()
66 // Load the reference buffer from the buffer file
68 std::string refBufferFilename( name + ".buffer" );
69 FILE *fp = fopen ( refBufferFilename.c_str(), "rb" );
70 AutoCloseFile autoCloseBufferFile( fp );
74 fread( refBuffer, sizeof( Dali::PixelBuffer ), refBufferSize, fp );
79 LoadFunctions::LoadFunctions( LoadBitmapHeaderFunction _header, LoadBitmapFunction _loader )
85 void TestImageLoading( const ImageDetails& image, const LoadFunctions& functions )
87 FILE* fp = fopen( image.name.c_str() , "rb" );
88 AutoCloseFile autoClose( fp );
89 DALI_TEST_CHECK( fp != NULL );
91 // Check the header file.
93 unsigned int width(0), height(0);
94 Dali::ImageAttributes attributes;
95 DALI_TEST_CHECK( functions.header( fp, attributes, width, height ) );
97 DALI_TEST_EQUALS( width, image.reportedWidth, TEST_LOCATION );
98 DALI_TEST_EQUALS( height, image.reportedHeight, TEST_LOCATION );
100 // Loading the header moves the pointer within the file so reset to start of file.
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 );
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 );
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 )
118 if( *bufferPtr != *refBufferPtr )
120 tet_result( TET_FAIL );
121 tet_printf("%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);
127 void DumpImageBufferToTempFile( std::string filename, std::string targetFilename, const LoadFunctions& functions )
129 FILE* fp = fopen( filename.c_str() , "rb" );
130 AutoCloseFile autoClose( fp );
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;
136 DALI_TEST_CHECK( functions.loader( fp, *bitmap, attributes ) );
138 Dali::PixelBuffer* bufferPtr( bitmapPtr->GetBuffer() );
140 FILE* writeFp = fopen( targetFilename.c_str(), "wb" );
141 AutoCloseFile autoCloseWrite( writeFp );
142 fwrite( bufferPtr, sizeof( Dali::PixelBuffer ), attributes.GetWidth() * attributes.GetHeight(), writeFp );