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>
20 #include <adaptors/common/pixel-buffer-impl.h>
22 AutoCloseFile::AutoCloseFile( FILE *fp )
27 AutoCloseFile::~AutoCloseFile()
36 ImageDetails::ImageDetails( const char * const _name, unsigned int _width, unsigned int _height )
40 reportedWidth( _width ),
41 reportedHeight( _height ),
42 refBufferSize( _width * _height ),
43 refBuffer( new Dali::PixelBuffer[ refBufferSize ] )
48 ImageDetails::ImageDetails( const char * const _name, unsigned int _width, unsigned int _height, unsigned int _reportedWidth, unsigned int _reportedHeight )
52 reportedWidth( _reportedWidth ),
53 reportedHeight( _reportedHeight ),
54 refBufferSize( _width * _height ),
55 refBuffer( new Dali::PixelBuffer[ refBufferSize ] )
60 ImageDetails::~ImageDetails()
65 void ImageDetails::LoadBuffer()
67 // Load the reference buffer from the buffer file
69 std::string refBufferFilename( name + ".buffer" );
70 FILE *fp = fopen ( refBufferFilename.c_str(), "rb" );
71 AutoCloseFile autoCloseBufferFile( fp );
75 fread( refBuffer, sizeof( Dali::PixelBuffer ), refBufferSize, fp );
80 LoadFunctions::LoadFunctions( LoadBitmapHeaderFunction _header, LoadBitmapFunction _loader )
86 void TestImageLoading( const ImageDetails& image, const LoadFunctions& functions, Dali::Integration::Bitmap::Profile bitmapProfile )
88 FILE* fp = fopen( image.name.c_str() , "rb" );
89 AutoCloseFile autoClose( fp );
90 DALI_TEST_CHECK( fp != NULL );
92 // Check the header file.
94 unsigned int width(0), height(0);
95 const Dali::TizenPlatform::ImageLoader::Input input( fp );
96 DALI_TEST_CHECK( functions.header( input, width, height ) );
98 DALI_TEST_EQUALS( width, image.reportedWidth, TEST_LOCATION );
99 DALI_TEST_EQUALS( height, image.reportedHeight, TEST_LOCATION );
101 // Loading the header moves the pointer within the file so reset to start of file.
104 Dali::Devel::PixelBuffer bitmap;
106 // Load Bitmap and check its return values.
107 DALI_TEST_CHECK( functions.loader( input, bitmap ) );
108 DALI_TEST_EQUALS( image.width, bitmap.GetWidth(), TEST_LOCATION );
109 DALI_TEST_EQUALS( image.height, bitmap.GetHeight(), TEST_LOCATION );
111 // Compare buffer generated with reference buffer.
112 Dali::PixelBuffer* bufferPtr( bitmap.GetBuffer() );
113 Dali::PixelBuffer* refBufferPtr( image.refBuffer );
114 for ( unsigned int i = 0; i < image.refBufferSize; ++i, ++bufferPtr, ++refBufferPtr )
116 if( *bufferPtr != *refBufferPtr )
118 tet_result( TET_FAIL );
119 tet_printf("%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);
125 void CompareLoadedImageData( const ImageDetails& image, const LoadFunctions& functions, const uint32_t* master )
127 FILE* filePointer = fopen( image.name.c_str() , "rb" );
128 AutoCloseFile autoClose( filePointer );
129 DALI_TEST_CHECK( filePointer != NULL );
131 // Check the header file.
132 unsigned int width = 0, height = 0;
133 const Dali::TizenPlatform::ImageLoader::Input input( filePointer );
134 DALI_TEST_CHECK( functions.header( input, width, height ) );
136 DALI_TEST_EQUALS( width, image.reportedWidth, TEST_LOCATION );
137 DALI_TEST_EQUALS( height, image.reportedHeight, TEST_LOCATION );
139 // Loading the header moves the pointer within the file so reset to start of file.
140 fseek( filePointer, 0, SEEK_SET );
142 Dali::Devel::PixelBuffer bitmap;
144 // Load Bitmap and check its return values.
145 DALI_TEST_CHECK( functions.loader( input, bitmap ) );
146 DALI_TEST_EQUALS( image.width, bitmap.GetWidth(), TEST_LOCATION );
147 DALI_TEST_EQUALS( image.height, bitmap.GetHeight(), TEST_LOCATION );
149 // Check the bytes per pixel.
150 const Pixel::Format pixelFormat = bitmap.GetPixelFormat();
151 const unsigned int bytesPerPixel = Pixel::GetBytesPerPixel( pixelFormat );
153 // Compare buffer generated with reference buffer.
154 Dali::PixelBuffer* pBitmapData( bitmap.GetBuffer() );
155 const uint32_t* pMaster( master );
157 // Loop through each pixel in the bitmap.
158 for ( unsigned int i = 0; i < image.refBufferSize; ++i, ++pMaster )
160 unsigned int color = 0;
161 // Loop through each byte per pixel, to build up a color value for that pixel.
162 for( unsigned int j = 0; j < bytesPerPixel; ++j, ++pBitmapData )
164 color = ( color << 8 ) | *pBitmapData;
167 // Check the color value is what we expect.
168 DALI_TEST_EQUALS( color, *pMaster, TEST_LOCATION );
172 void DumpImageBufferToTempFile( std::string filename, std::string targetFilename, const LoadFunctions& functions )
174 FILE* fp = fopen( filename.c_str() , "rb" );
175 AutoCloseFile autoClose( fp );
177 Dali::Devel::PixelBuffer bitmap;
178 const Dali::TizenPlatform::ImageLoader::Input input( fp );
180 DALI_TEST_CHECK( functions.loader( input, bitmap ) );
182 Dali::PixelBuffer* bufferPtr( bitmap.GetBuffer() );
184 FILE* writeFp = fopen( targetFilename.c_str(), "wb" );
185 AutoCloseFile autoCloseWrite( writeFp );
186 auto& impl = GetImplementation(bitmap);
187 fwrite( bufferPtr, 1, impl.GetBufferSize(), writeFp );