[dali_1.0.1] Merge branch 'tizen'
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor-internal / image-loaders.cpp
1 /*
2  * Copyright (c) 2014 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 class StubImageLoaderClient : public Dali::SlpPlatform::ResourceLoadingClient
23 {
24 public:
25   StubImageLoaderClient() {}
26   ~StubImageLoaderClient() {}
27
28   virtual void InterruptionPoint() const {}
29 };
30
31 AutoCloseFile::AutoCloseFile( FILE *fp )
32 : filePtr( fp )
33 {
34 }
35
36 AutoCloseFile::~AutoCloseFile()
37 {
38   if ( filePtr )
39   {
40     fclose( filePtr );
41   }
42 }
43
44
45 ImageDetails::ImageDetails( const char * const _name, unsigned int _width, unsigned int _height )
46 : name( _name ),
47   width( _width ),
48   height( _height ),
49   reportedWidth( _width ),
50   reportedHeight( _height ),
51   refBufferSize( _width * _height ),
52   refBuffer( new Dali::PixelBuffer[ refBufferSize ] )
53 {
54   LoadBuffer();
55 }
56
57 ImageDetails::ImageDetails( const char * const _name, unsigned int _width, unsigned int _height, unsigned int _reportedWidth, unsigned int _reportedHeight )
58 : name( _name ),
59   width( _width ),
60   height( _height ),
61   reportedWidth( _reportedWidth ),
62   reportedHeight( _reportedHeight ),
63   refBufferSize( _width * _height ),
64   refBuffer( new Dali::PixelBuffer[ refBufferSize ] )
65 {
66   LoadBuffer();
67 }
68
69 ImageDetails::~ImageDetails()
70 {
71   delete [] refBuffer;
72 }
73
74 void ImageDetails::LoadBuffer()
75 {
76   // Load the reference buffer from the buffer file
77
78   std::string refBufferFilename( name + ".buffer" );
79   FILE *fp = fopen ( refBufferFilename.c_str(), "rb" );
80   AutoCloseFile autoCloseBufferFile( fp );
81
82   if ( fp )
83   {
84     fread( refBuffer, sizeof( Dali::PixelBuffer ), refBufferSize, fp );
85   }
86 }
87
88
89 LoadFunctions::LoadFunctions( LoadBitmapHeaderFunction _header, LoadBitmapFunction _loader )
90 : header( _header ),
91   loader( _loader )
92 {
93 }
94
95 void TestImageLoading( const ImageDetails& image, const LoadFunctions& functions )
96 {
97   FILE* fp = fopen( image.name.c_str() , "rb" );
98   AutoCloseFile autoClose( fp );
99   DALI_TEST_CHECK( fp != NULL );
100
101   // Check the header file.
102
103   unsigned int width(0), height(0);
104   Dali::ImageAttributes attributes;
105   DALI_TEST_CHECK( functions.header( fp, attributes, width, height ) );
106
107   DALI_TEST_EQUALS( width,  image.reportedWidth,  TEST_LOCATION );
108   DALI_TEST_EQUALS( height, image.reportedHeight, TEST_LOCATION );
109
110   // Loading the header moves the pointer within the file so reset to start of file.
111   fseek( fp, 0, 0 );
112
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,  false  );
115   Dali::Integration::BitmapPtr bitmapPtr( bitmap );
116
117
118   // Load Bitmap and check its return values.
119   DALI_TEST_CHECK( functions.loader( fp, *bitmap, attributes, StubImageLoaderClient() ) );
120   DALI_TEST_EQUALS( image.width,  attributes.GetWidth(),  TEST_LOCATION );
121   DALI_TEST_EQUALS( image.height, attributes.GetHeight(), TEST_LOCATION );
122
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 )
127   {
128     if( *bufferPtr != *refBufferPtr )
129     {
130       tet_result( TET_FAIL );
131       tet_printf("%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);
132       break;
133     }
134   }
135 }
136
137 void DumpImageBufferToTempFile( std::string filename, std::string targetFilename, const LoadFunctions& functions )
138 {
139   FILE* fp = fopen( filename.c_str() , "rb" );
140   AutoCloseFile autoClose( fp );
141
142   Dali::Integration::Bitmap* bitmap = Dali::Integration::Bitmap::New( Dali::Integration::Bitmap::BITMAP_2D_PACKED_PIXELS,  false );
143   Dali::Integration::BitmapPtr bitmapPtr( bitmap );
144   Dali::ImageAttributes attributes;
145
146   DALI_TEST_CHECK( functions.loader( fp, *bitmap, attributes, StubImageLoaderClient() ) );
147
148   Dali::PixelBuffer* bufferPtr( bitmapPtr->GetBuffer() );
149
150   FILE* writeFp = fopen( targetFilename.c_str(), "wb" );
151   AutoCloseFile autoCloseWrite( writeFp );
152   fwrite( bufferPtr, sizeof( Dali::PixelBuffer ), attributes.GetWidth() * attributes.GetHeight(), writeFp );
153 }