2 * Copyright (c) 2016 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, Dali::Integration::Bitmap::Profile bitmapProfile )
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( bitmapProfile, ResourcePolicy::OWNED_RETAIN );
115 Dali::Integration::BitmapPtr bitmapPtr( bitmap );
117 // Load Bitmap and check its return values.
118 DALI_TEST_CHECK( functions.loader( StubImageLoaderClient(), input, *bitmap ) );
119 DALI_TEST_EQUALS( image.width, bitmap->GetImageWidth(), TEST_LOCATION );
120 DALI_TEST_EQUALS( image.height, bitmap->GetImageHeight(), TEST_LOCATION );
122 // Compare buffer generated with reference buffer.
123 Dali::PixelBuffer* bufferPtr( bitmapPtr->GetBuffer() );
124 Dali::PixelBuffer* refBufferPtr( image.refBuffer );
125 for ( unsigned int i = 0; i < image.refBufferSize; ++i, ++bufferPtr, ++refBufferPtr )
127 if( *bufferPtr != *refBufferPtr )
129 tet_result( TET_FAIL );
130 tet_printf("%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);
136 void CompareLoadedImageData( const ImageDetails& image, const LoadFunctions& functions, const uint32_t* master )
138 FILE* filePointer = fopen( image.name.c_str() , "rb" );
139 AutoCloseFile autoClose( filePointer );
140 DALI_TEST_CHECK( filePointer != NULL );
142 // Check the header file.
143 unsigned int width = 0, height = 0;
144 const Dali::TizenPlatform::ImageLoader::Input input( filePointer );
145 DALI_TEST_CHECK( functions.header( input, width, height ) );
147 DALI_TEST_EQUALS( width, image.reportedWidth, TEST_LOCATION );
148 DALI_TEST_EQUALS( height, image.reportedHeight, TEST_LOCATION );
150 // Loading the header moves the pointer within the file so reset to start of file.
151 fseek( filePointer, 0, SEEK_SET );
153 // Create a bitmap object and store a pointer to that object so it is destroyed at the end.
154 Dali::Integration::Bitmap * bitmap = Dali::Integration::Bitmap::New( Dali::Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
155 Dali::Integration::BitmapPtr bitmapPointer( bitmap );
157 // Load Bitmap and check its return values.
158 DALI_TEST_CHECK( functions.loader( StubImageLoaderClient(), input, *bitmap ) );
159 DALI_TEST_EQUALS( image.width, bitmap->GetImageWidth(), TEST_LOCATION );
160 DALI_TEST_EQUALS( image.height, bitmap->GetImageHeight(), TEST_LOCATION );
162 // Check the bytes per pixel.
163 const Pixel::Format pixelFormat = bitmap->GetPixelFormat();
164 const unsigned int bytesPerPixel = Pixel::GetBytesPerPixel( pixelFormat );
166 // Compare buffer generated with reference buffer.
167 Dali::PixelBuffer* pBitmapData( bitmapPointer->GetBuffer() );
168 const uint32_t* pMaster( master );
170 // Loop through each pixel in the bitmap.
171 for ( unsigned int i = 0; i < image.refBufferSize; ++i, ++pMaster )
173 unsigned int color = 0;
174 // Loop through each byte per pixel, to build up a color value for that pixel.
175 for( unsigned int j = 0; j < bytesPerPixel; ++j, ++pBitmapData )
177 color = ( color << 8 ) | *pBitmapData;
180 // Check the color value is what we expect.
181 DALI_TEST_EQUALS( color, *pMaster, TEST_LOCATION );
185 void DumpImageBufferToTempFile( std::string filename, std::string targetFilename, const LoadFunctions& functions )
187 FILE* fp = fopen( filename.c_str() , "rb" );
188 AutoCloseFile autoClose( fp );
190 Dali::Integration::Bitmap* bitmap = Dali::Integration::Bitmap::New( Dali::Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
191 Dali::Integration::BitmapPtr bitmapPtr( bitmap );
192 const Dali::TizenPlatform::ImageLoader::Input input( fp );
194 DALI_TEST_CHECK( functions.loader( StubImageLoaderClient(), input, *bitmap ) );
196 Dali::PixelBuffer* bufferPtr( bitmapPtr->GetBuffer() );
198 FILE* writeFp = fopen( targetFilename.c_str(), "wb" );
199 AutoCloseFile autoCloseWrite( writeFp );
200 fwrite( bufferPtr, 1, bitmap->GetBufferSize(), writeFp );