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>
22 class StubImageLoaderClient : public Dali::TizenPlatform::ResourceLoadingClient
25 StubImageLoaderClient() {}
26 ~StubImageLoaderClient() {}
28 virtual void InterruptionPoint() const {}
31 AutoCloseFile::AutoCloseFile( FILE *fp )
36 AutoCloseFile::~AutoCloseFile()
45 ImageDetails::ImageDetails( const char * const _name, unsigned int _width, unsigned int _height )
49 reportedWidth( _width ),
50 reportedHeight( _height ),
51 refBufferSize( _width * _height ),
52 refBuffer( new Dali::PixelBuffer[ refBufferSize ] )
57 ImageDetails::ImageDetails( const char * const _name, unsigned int _width, unsigned int _height, unsigned int _reportedWidth, unsigned int _reportedHeight )
61 reportedWidth( _reportedWidth ),
62 reportedHeight( _reportedHeight ),
63 refBufferSize( _width * _height ),
64 refBuffer( new Dali::PixelBuffer[ refBufferSize ] )
69 ImageDetails::~ImageDetails()
74 void ImageDetails::LoadBuffer()
76 // Load the reference buffer from the buffer file
78 std::string refBufferFilename( name + ".buffer" );
79 FILE *fp = fopen ( refBufferFilename.c_str(), "rb" );
80 AutoCloseFile autoCloseBufferFile( fp );
84 fread( refBuffer, sizeof( Dali::PixelBuffer ), refBufferSize, fp );
89 LoadFunctions::LoadFunctions( LoadBitmapHeaderFunction _header, LoadBitmapFunction _loader )
95 void TestImageLoading( const ImageDetails& image, const LoadFunctions& functions )
97 FILE* fp = fopen( image.name.c_str() , "rb" );
98 AutoCloseFile autoClose( fp );
99 DALI_TEST_CHECK( fp != NULL );
101 // Check the header file.
103 unsigned int width(0), height(0);
104 const Dali::TizenPlatform::ImageLoader::Input input( fp );
105 DALI_TEST_CHECK( functions.header( input, width, height ) );
107 DALI_TEST_EQUALS( width, image.reportedWidth, TEST_LOCATION );
108 DALI_TEST_EQUALS( height, image.reportedHeight, TEST_LOCATION );
110 // Loading the header moves the pointer within the file so reset to start of file.
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( Dali::Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::RETAIN );
115 Dali::Integration::BitmapPtr bitmapPtr( bitmap );
118 // Load Bitmap and check its return values.
119 DALI_TEST_CHECK( functions.loader( StubImageLoaderClient(), input, *bitmap ) );
120 DALI_TEST_EQUALS( image.width, bitmap->GetImageWidth(), TEST_LOCATION );
121 DALI_TEST_EQUALS( image.height, bitmap->GetImageHeight(), TEST_LOCATION );
123 // Compare buffer generated with reference buffer.
124 Dali::PixelBuffer* bufferPtr( bitmapPtr->GetBuffer() );
125 Dali::PixelBuffer* refBufferPtr( image.refBuffer );
126 for ( unsigned int i = 0; i < image.refBufferSize; ++i, ++bufferPtr, ++refBufferPtr )
128 if( *bufferPtr != *refBufferPtr )
130 tet_result( TET_FAIL );
131 tet_printf("%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);
137 void DumpImageBufferToTempFile( std::string filename, std::string targetFilename, const LoadFunctions& functions )
139 FILE* fp = fopen( filename.c_str() , "rb" );
140 AutoCloseFile autoClose( fp );
142 Dali::Integration::Bitmap* bitmap = Dali::Integration::Bitmap::New( Dali::Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::RETAIN );
143 Dali::Integration::BitmapPtr bitmapPtr( bitmap );
144 const Dali::TizenPlatform::ImageLoader::Input input( fp );
146 DALI_TEST_CHECK( functions.loader( StubImageLoaderClient(), input, *bitmap ) );
148 Dali::PixelBuffer* bufferPtr( bitmapPtr->GetBuffer() );
150 FILE* writeFp = fopen( targetFilename.c_str(), "wb" );
151 AutoCloseFile autoCloseWrite( writeFp );
152 fwrite( bufferPtr, 1, bitmap->GetBufferSize(), writeFp );