Merge "Fix SVACE issue" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / loader-astc.cpp
1 /*
2  * Copyright (c) 2017 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 // CLASS HEADER
19 #include <dali/internal/imaging/common/loader-astc.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring>
23 #include <dali/public-api/common/compile-time-assert.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/public-api/images/pixel.h>
26 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
27 #include <dali/internal/imaging/common/pixel-buffer-impl.h>
28
29 namespace Dali
30 {
31 namespace TizenPlatform
32 {
33 namespace
34 {
35
36 // Max width or height of an image.
37 const unsigned MAX_TEXTURE_DIMENSION = 4096;
38 // Max bytes of image data allowed. Not a precise number, just a sanity check.
39 const unsigned MAX_IMAGE_DATA_SIZE = MAX_TEXTURE_DIMENSION * MAX_TEXTURE_DIMENSION;
40
41 // Minimum and maximum possible sizes for ASTC blocks.
42 const unsigned int MINIMUM_ASTC_BLOCK_SIZE = 4;
43 const unsigned int MAXIMUM_ASTC_BLOCK_SIZE = 12;
44
45 typedef uint8_t Byte;
46
47 // This bytes identify an ASTC native file.
48 const Byte FileIdentifier[] = {
49    0x13, 0xAB, 0xA1, 0x5C
50 };
51
52
53 /**
54  * @brief This struct defines the ASTC file header values. From ASTC specifications.
55  * Packed attribute stops the structure from being aligned to compiler defaults
56  * so we can be sure of reading the whole header from file in one call to fread().
57  * Note: members to not conform to coding standards in order to be consistent with ASTC spec.
58  */
59 struct AstcFileHeader
60 {
61   unsigned char magic[ 4 ];
62   unsigned char blockdim_x;
63   unsigned char blockdim_y;
64   unsigned char blockdim_z;
65   unsigned char xsize[ 3 ];
66   unsigned char ysize[ 3 ];
67   unsigned char zsize[ 3 ];
68 } __attribute__ ( (__packed__));
69
70 using namespace Pixel;
71
72 /**
73  * @brief This table allows fast conversion from an ASTC block size ([height][width]) to a pixel format.
74  * This could be done within a switch, but this way we have a constant time function.
75  * Note: As 4 is the minimum block size, 4 is subtracted from both the width and height to optimise size.
76  * IE. Table format is: Increasing order of block width from left-to-right:  4 -> 12
77  *                      Increasing order of block height from top-to-bottom: 4 -> 12
78  */
79 Pixel::Format AstcLinearBlockSizeToPixelFormatTable[][( MAXIMUM_ASTC_BLOCK_SIZE - MINIMUM_ASTC_BLOCK_SIZE ) + 1] = {
80     { COMPRESSED_RGBA_ASTC_4x4_KHR, COMPRESSED_RGBA_ASTC_5x4_KHR, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID },
81     { INVALID, COMPRESSED_RGBA_ASTC_5x5_KHR, COMPRESSED_RGBA_ASTC_6x5_KHR, INVALID, COMPRESSED_RGBA_ASTC_8x5_KHR, INVALID, COMPRESSED_RGBA_ASTC_10x5_KHR, INVALID, INVALID },
82     { INVALID, INVALID, COMPRESSED_RGBA_ASTC_6x6_KHR, INVALID, COMPRESSED_RGBA_ASTC_8x6_KHR, INVALID, COMPRESSED_RGBA_ASTC_10x6_KHR, INVALID, INVALID },
83     { INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID },
84     { INVALID, INVALID, INVALID, INVALID, COMPRESSED_RGBA_ASTC_8x8_KHR, INVALID, COMPRESSED_RGBA_ASTC_10x8_KHR, INVALID, INVALID },
85     { INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID },
86     { INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, COMPRESSED_RGBA_ASTC_10x10_KHR, INVALID, COMPRESSED_RGBA_ASTC_12x10_KHR },
87     { INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID },
88     { INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, INVALID, COMPRESSED_RGBA_ASTC_12x12_KHR }
89 };
90
91 /**
92  * @brief Uses header information to return the respective ASTC pixel format.
93  *
94  * @param[in] header A populated AstcFileHeader struct
95  * @return    The pixel format, or INVALID if the block size was invalid
96  */
97 Pixel::Format GetAstcPixelFormat( AstcFileHeader& header )
98 {
99   // Check the block size is valid. This will also prevent an invalid read from the conversion table.
100   if( ( header.blockdim_x < MINIMUM_ASTC_BLOCK_SIZE ) || ( header.blockdim_x > MAXIMUM_ASTC_BLOCK_SIZE ) ||
101       ( header.blockdim_y < MINIMUM_ASTC_BLOCK_SIZE ) || ( header.blockdim_y > MAXIMUM_ASTC_BLOCK_SIZE ) )
102   {
103     return Pixel::INVALID;
104   }
105
106   // Read the equivalent pixel format from the conversion table.
107   return AstcLinearBlockSizeToPixelFormatTable[ header.blockdim_y - MINIMUM_ASTC_BLOCK_SIZE ][ header.blockdim_x - MINIMUM_ASTC_BLOCK_SIZE ];
108 }
109
110 /**
111  * @brief Internal method to load ASTC header info from a file.
112  *
113  * @param[in]  filePointer The file pointer to the ASTC file to read
114  * @param[out] width       The width is output to this value
115  * @param[out] height      The height is output to this value
116  * @param[out] fileHeader  This will be populated with the header data
117  * @return                 True if the file is valid, false otherwise
118  */
119 bool LoadAstcHeader( FILE * const filePointer, unsigned int& width, unsigned int& height, AstcFileHeader& fileHeader )
120 {
121   // Pull the bytes of the file header in as a block:
122   const unsigned int readLength = sizeof( AstcFileHeader );
123   if( fread( &fileHeader, 1, readLength, filePointer ) != readLength )
124   {
125     return false;
126   }
127
128   // Check the header contains the ASTC native file identifier.
129   bool headerIsValid = memcmp( fileHeader.magic, FileIdentifier, sizeof( fileHeader.magic ) ) == 0;
130   if( !headerIsValid )
131   {
132     DALI_LOG_ERROR( "File is not a valid ASTC native file\n" );
133     // Return here as otherwise, if not a valid ASTC file, we are likely to pick up other header errors spuriously.
134     return false;
135   }
136
137   // Convert the 3-byte values for width and height to a single resultant value.
138   width = fileHeader.xsize[0] | ( fileHeader.xsize[1] << 8 ) | ( fileHeader.xsize[2] << 16 );
139   height = fileHeader.ysize[0] | ( fileHeader.ysize[1] << 8 ) | ( fileHeader.ysize[2] << 16 );
140
141   const unsigned int zDepth = static_cast<unsigned int>( fileHeader.zsize[0] )
142                               + ( static_cast<unsigned int>( fileHeader.zsize[1] ) << 8 )
143                               + ( static_cast<unsigned int>( fileHeader.zsize[2] ) << 16 );
144
145   // Check image dimensions are within limits.
146   if( ( width > MAX_TEXTURE_DIMENSION ) || ( height > MAX_TEXTURE_DIMENSION ) )
147   {
148     DALI_LOG_ERROR( "ASTC file has larger than supported dimensions: %d,%d\n", width, height );
149     headerIsValid = false;
150   }
151
152   // Confirm the ASTC block does not have any Z depth.
153   if( zDepth != 1 )
154   {
155     DALI_LOG_ERROR( "ASTC files with z size other than 1 are not supported. Z size is: %d\n", zDepth );
156     headerIsValid = false;
157   }
158
159   return headerIsValid;
160 }
161
162 } // Unnamed namespace.
163
164
165 // File loading API entry-point:
166 bool LoadAstcHeader( const Dali::ImageLoader::Input& input, unsigned int& width, unsigned int& height )
167 {
168   AstcFileHeader fileHeader;
169   return LoadAstcHeader( input.file, width, height, fileHeader );
170 }
171
172 // File loading API entry-point:
173 bool LoadBitmapFromAstc( const Dali::ImageLoader::Input& input, Dali::Devel::PixelBuffer& bitmap )
174 {
175   FILE* const filePointer = input.file;
176   if( !filePointer )
177   {
178     DALI_LOG_ERROR( "Null file handle passed to ASTC compressed bitmap file loader.\n" );
179     return false;
180   }
181
182   // Load the header info.
183   AstcFileHeader fileHeader;
184   unsigned int width, height;
185
186   if( !LoadAstcHeader( filePointer, width, height, fileHeader ) )
187   {
188     DALI_LOG_ERROR( "Could not load ASTC Header from file.\n" );
189     return false;
190   }
191
192   // Retrieve the pixel format from the ASTC block size.
193   Pixel::Format pixelFormat = GetAstcPixelFormat( fileHeader );
194   if( pixelFormat == Pixel::INVALID )
195   {
196     DALI_LOG_ERROR( "No internal pixel format supported for ASTC file pixel format.\n" );
197     return false;
198   }
199
200   // Retrieve the file size.
201   if( fseek( filePointer, 0L, SEEK_END ) )
202   {
203     DALI_LOG_ERROR( "Could not seek through file.\n" );
204     return false;
205   }
206
207   off_t fileSize = ftell( filePointer );
208   if( fileSize == -1L )
209   {
210     DALI_LOG_ERROR( "Could not determine ASTC file size.\n" );
211     return false;
212   }
213
214   if( fseek( filePointer, sizeof( AstcFileHeader ), SEEK_SET ) )
215   {
216     DALI_LOG_ERROR( "Could not seek through file.\n" );
217     return false;
218   }
219
220   // Data size is file size - header size.
221   size_t imageByteCount = fileSize - sizeof( AstcFileHeader );
222
223   // Sanity-check the image data is not too large and that it is at less than 2 bytes per texel:
224   if( ( imageByteCount > MAX_IMAGE_DATA_SIZE ) || ( imageByteCount > ( ( static_cast< size_t >( width ) * height ) << 1 ) ) )
225   {
226     DALI_LOG_ERROR( "ASTC file has too large image-data field.\n" );
227     return false;
228   }
229
230   // allocate pixel data
231   bitmap = Dali::Devel::PixelBuffer::New(width, height, pixelFormat);
232
233   // Compressed format won't allocate the buffer
234   auto pixels = bitmap.GetBuffer();
235   if( !pixels )
236   {
237     // allocate buffer manually
238     auto& impl = GetImplementation( bitmap );
239     impl.AllocateFixedSize( imageByteCount );
240     pixels = bitmap.GetBuffer();
241   }
242
243   // Load the image data.
244   const size_t bytesRead = fread( pixels, 1, imageByteCount, filePointer );
245
246   // Check the size of loaded data is what we expected.
247   if( bytesRead != imageByteCount )
248   {
249     DALI_LOG_ERROR( "Read of image pixel data failed.\n" );
250     return false;
251   }
252
253   return true;
254 }
255
256 } // namespace TizenPlatform
257
258 } // namespace Dali