Revert "[4.0] Fixed BMP loader."
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor-internal / image-loaders.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include "image-loaders.h"
19 #include <dali-test-suite-utils.h>
20
21
22 AutoCloseFile::AutoCloseFile( FILE *fp )
23 : filePtr( fp )
24 {
25 }
26
27 AutoCloseFile::~AutoCloseFile()
28 {
29   if ( filePtr )
30   {
31     fclose( filePtr );
32   }
33 }
34
35
36 ImageDetails::ImageDetails( const char * const _name, unsigned int _width, unsigned int _height )
37 : name( _name ),
38   width( _width ),
39   height( _height ),
40   reportedWidth( _width ),
41   reportedHeight( _height ),
42   refBufferSize( _width * _height ),
43   refBuffer( new Dali::PixelBuffer[ refBufferSize ] )
44 {
45   LoadBuffer();
46 }
47
48 ImageDetails::ImageDetails( const char * const _name, unsigned int _width, unsigned int _height, unsigned int _reportedWidth, unsigned int _reportedHeight )
49 : name( _name ),
50   width( _width ),
51   height( _height ),
52   reportedWidth( _reportedWidth ),
53   reportedHeight( _reportedHeight ),
54   refBufferSize( _width * _height ),
55   refBuffer( new Dali::PixelBuffer[ refBufferSize ] )
56 {
57   LoadBuffer();
58 }
59
60 ImageDetails::~ImageDetails()
61 {
62   delete [] refBuffer;
63 }
64
65 void ImageDetails::LoadBuffer()
66 {
67   // Load the reference buffer from the buffer file
68
69   std::string refBufferFilename( name + ".buffer" );
70   FILE *fp = fopen ( refBufferFilename.c_str(), "rb" );
71   AutoCloseFile autoCloseBufferFile( fp );
72
73   if ( fp )
74   {
75     fread( refBuffer, sizeof( Dali::PixelBuffer ), refBufferSize, fp );
76   }
77 }
78
79
80 LoadFunctions::LoadFunctions( LoadBitmapHeaderFunction _header, LoadBitmapFunction _loader )
81 : header( _header ),
82   loader( _loader )
83 {
84 }
85
86 void TestImageLoading( const ImageDetails& image, const LoadFunctions& functions, Dali::Integration::Bitmap::Profile bitmapProfile )
87 {
88   FILE* fp = fopen( image.name.c_str() , "rb" );
89   AutoCloseFile autoClose( fp );
90   DALI_TEST_CHECK( fp != NULL );
91
92   // Check the header file.
93
94   unsigned int width(0), height(0);
95   const Dali::TizenPlatform::ImageLoader::Input input( fp );
96   DALI_TEST_CHECK( functions.header( input, width, height ) );
97
98   DALI_TEST_EQUALS( width,  image.reportedWidth,  TEST_LOCATION );
99   DALI_TEST_EQUALS( height, image.reportedHeight, TEST_LOCATION );
100
101   // Loading the header moves the pointer within the file so reset to start of file.
102   fseek( fp, 0, 0 );
103
104   // Create a bitmap object and store a pointer to that object so it is destroyed at the end.
105   Dali::Integration::Bitmap * bitmap = Dali::Integration::Bitmap::New( bitmapProfile, ResourcePolicy::OWNED_RETAIN  );
106   Dali::Integration::BitmapPtr bitmapPtr( bitmap );
107
108   // Load Bitmap and check its return values.
109   DALI_TEST_CHECK( functions.loader( input, *bitmap ) );
110   DALI_TEST_EQUALS( image.width,  bitmap->GetImageWidth(),  TEST_LOCATION );
111   DALI_TEST_EQUALS( image.height, bitmap->GetImageHeight(), TEST_LOCATION );
112
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 )
117   {
118     if( *bufferPtr != *refBufferPtr )
119     {
120       tet_result( TET_FAIL );
121       tet_printf("%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);
122       break;
123     }
124   }
125 }
126
127 void CompareLoadedImageData( const ImageDetails& image, const LoadFunctions& functions, const uint32_t* master )
128 {
129   FILE* filePointer = fopen( image.name.c_str() , "rb" );
130   AutoCloseFile autoClose( filePointer );
131   DALI_TEST_CHECK( filePointer != NULL );
132
133   // Check the header file.
134   unsigned int width = 0, height = 0;
135   const Dali::TizenPlatform::ImageLoader::Input input( filePointer );
136   DALI_TEST_CHECK( functions.header( input, width, height ) );
137
138   DALI_TEST_EQUALS( width,  image.reportedWidth,  TEST_LOCATION );
139   DALI_TEST_EQUALS( height, image.reportedHeight, TEST_LOCATION );
140
141   // Loading the header moves the pointer within the file so reset to start of file.
142   fseek( filePointer, 0, SEEK_SET );
143
144   // Create a bitmap object and store a pointer to that object so it is destroyed at the end.
145   Dali::Integration::Bitmap * bitmap = Dali::Integration::Bitmap::New( Dali::Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN  );
146   Dali::Integration::BitmapPtr bitmapPointer( bitmap );
147
148   // Load Bitmap and check its return values.
149   DALI_TEST_CHECK( functions.loader( input, *bitmap ) );
150   DALI_TEST_EQUALS( image.width,  bitmap->GetImageWidth(),  TEST_LOCATION );
151   DALI_TEST_EQUALS( image.height, bitmap->GetImageHeight(), TEST_LOCATION );
152
153   // Check the bytes per pixel.
154   const Pixel::Format pixelFormat = bitmap->GetPixelFormat();
155   const unsigned int bytesPerPixel = Pixel::GetBytesPerPixel( pixelFormat );
156
157   // Compare buffer generated with reference buffer.
158   Dali::PixelBuffer* pBitmapData( bitmapPointer->GetBuffer() );
159   const uint32_t* pMaster( master );
160
161   // Loop through each pixel in the bitmap.
162   for ( unsigned int i = 0; i < image.refBufferSize; ++i, ++pMaster )
163   {
164     unsigned int color = 0;
165     // Loop through each byte per pixel, to build up a color value for that pixel.
166     for( unsigned int j = 0; j < bytesPerPixel; ++j, ++pBitmapData )
167     {
168       color = ( color << 8 ) | *pBitmapData;
169     }
170
171     // Check the color value is what we expect.
172     DALI_TEST_EQUALS( color, *pMaster, TEST_LOCATION );
173   }
174 }
175
176 void DumpImageBufferToTempFile( std::string filename, std::string targetFilename, const LoadFunctions& functions )
177 {
178   FILE* fp = fopen( filename.c_str() , "rb" );
179   AutoCloseFile autoClose( fp );
180
181   Dali::Integration::Bitmap* bitmap = Dali::Integration::Bitmap::New( Dali::Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,  ResourcePolicy::OWNED_RETAIN );
182   Dali::Integration::BitmapPtr bitmapPtr( bitmap );
183   const Dali::TizenPlatform::ImageLoader::Input input( fp );
184
185   DALI_TEST_CHECK( functions.loader( input, *bitmap ) );
186
187   Dali::PixelBuffer* bufferPtr( bitmapPtr->GetBuffer() );
188
189   FILE* writeFp = fopen( targetFilename.c_str(), "wb" );
190   AutoCloseFile autoCloseWrite( writeFp );
191   fwrite( bufferPtr, 1, bitmap->GetBufferSize(), writeFp );
192 }