2 * Copyright (c) 2018 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 ),
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 ),
60 ImageDetails::~ImageDetails()
68 void ImageDetails::LoadBuffer()
70 // Load the reference buffer from the buffer file
71 std::string refBufferFilename( name + ".buffer" );
72 FILE *fp = fopen ( refBufferFilename.c_str(), "rb" );
73 AutoCloseFile autoCloseBufferFile( fp );
77 fseek( fp, 0, SEEK_END );
78 refBufferSize = ftell( fp );
79 fseek( fp, 0, SEEK_SET );
80 refBuffer = reinterpret_cast<Dali::PixelBuffer*>( malloc( refBufferSize ) );
81 fread( refBuffer, sizeof( Dali::PixelBuffer ), refBufferSize, fp );
86 LoadFunctions::LoadFunctions( LoadBitmapHeaderFunction _header, LoadBitmapFunction _loader )
92 void TestImageLoading( const ImageDetails& image, const LoadFunctions& functions, Dali::Integration::Bitmap::Profile bitmapProfile )
94 FILE* fp = fopen( image.name.c_str() , "rb" );
95 AutoCloseFile autoClose( fp );
96 DALI_TEST_CHECK( fp != NULL );
98 // Check the header file.
100 unsigned int width(0), height(0);
101 const Dali::TizenPlatform::ImageLoader::Input input( fp );
102 DALI_TEST_CHECK( functions.header( input, width, height ) );
104 DALI_TEST_EQUALS( width, image.reportedWidth, TEST_LOCATION );
105 DALI_TEST_EQUALS( height, image.reportedHeight, TEST_LOCATION );
107 // Loading the header moves the pointer within the file so reset to start of file.
110 Dali::Devel::PixelBuffer bitmap;
112 // Load Bitmap and check its return values.
113 DALI_TEST_CHECK( functions.loader( input, bitmap ) );
114 DALI_TEST_EQUALS( image.width, bitmap.GetWidth(), TEST_LOCATION );
115 DALI_TEST_EQUALS( image.height, bitmap.GetHeight(), TEST_LOCATION );
117 // Compare buffer generated with reference buffer.
118 Dali::PixelBuffer* bufferPtr( bitmap.GetBuffer() );
119 Dali::PixelBuffer* refBufferPtr( image.refBuffer );
120 for ( unsigned int i = 0; i < image.refBufferSize; ++i, ++bufferPtr, ++refBufferPtr )
122 if( *bufferPtr != *refBufferPtr )
124 tet_result( TET_FAIL );
125 tet_printf("%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);
131 void CompareLoadedImageData( const ImageDetails& image, const LoadFunctions& functions, const uint32_t* master )
133 FILE* filePointer = fopen( image.name.c_str() , "rb" );
134 AutoCloseFile autoClose( filePointer );
135 DALI_TEST_CHECK( filePointer != NULL );
137 // Check the header file.
138 unsigned int width = 0, height = 0;
139 const Dali::TizenPlatform::ImageLoader::Input input( filePointer );
140 DALI_TEST_CHECK( functions.header( input, width, height ) );
142 DALI_TEST_EQUALS( width, image.reportedWidth, TEST_LOCATION );
143 DALI_TEST_EQUALS( height, image.reportedHeight, TEST_LOCATION );
145 // Loading the header moves the pointer within the file so reset to start of file.
146 fseek( filePointer, 0, SEEK_SET );
148 Dali::Devel::PixelBuffer bitmap;
150 // Load Bitmap and check its return values.
151 DALI_TEST_CHECK( functions.loader( input, bitmap ) );
152 DALI_TEST_EQUALS( image.width, bitmap.GetWidth(), TEST_LOCATION );
153 DALI_TEST_EQUALS( image.height, bitmap.GetHeight(), TEST_LOCATION );
155 // Check the bytes per pixel.
156 const Pixel::Format pixelFormat = bitmap.GetPixelFormat();
157 const unsigned int bytesPerPixel = Pixel::GetBytesPerPixel( pixelFormat );
159 // Compare buffer generated with reference buffer.
160 Dali::PixelBuffer* pBitmapData( bitmap.GetBuffer() );
161 const uint32_t* pMaster( master );
163 // Loop through each pixel in the bitmap.
164 for ( unsigned int i = 0; i < image.refBufferSize; ++i, ++pMaster )
166 unsigned int color = 0;
167 // Loop through each byte per pixel, to build up a color value for that pixel.
168 for( unsigned int j = 0; j < bytesPerPixel; ++j, ++pBitmapData )
170 color = ( color << 8 ) | *pBitmapData;
173 // Check the color value is what we expect.
174 DALI_TEST_EQUALS( color, *pMaster, TEST_LOCATION );
178 void DumpImageBufferToTempFile( std::string filename, std::string targetFilename, const LoadFunctions& functions )
180 FILE* fp = fopen( filename.c_str() , "rb" );
181 AutoCloseFile autoClose( fp );
183 Dali::Devel::PixelBuffer bitmap;
184 const Dali::TizenPlatform::ImageLoader::Input input( fp );
186 DALI_TEST_CHECK( functions.loader( input, bitmap ) );
188 Dali::PixelBuffer* bufferPtr( bitmap.GetBuffer() );
190 FILE* writeFp = fopen( targetFilename.c_str(), "wb" );
191 AutoCloseFile autoCloseWrite( writeFp );
192 auto& impl = GetImplementation(bitmap);
193 fwrite( bufferPtr, 1, impl.GetBufferSize(), writeFp );