Revert "[Tizen] Add codes for Dali Windows Backend"
[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 = fileHeader.zsize[0] + ( fileHeader.zsize[1] << 8 ) + ( fileHeader.zsize[2] << 16 );
142
143   // Check image dimensions are within limits.
144   if( ( width > MAX_TEXTURE_DIMENSION ) || ( height > MAX_TEXTURE_DIMENSION ) )
145   {
146     DALI_LOG_ERROR( "ASTC file has larger than supported dimensions: %d,%d\n", width, height );
147     headerIsValid = false;
148   }
149
150   // Confirm the ASTC block does not have any Z depth.
151   if( zDepth != 1 )
152   {
153     DALI_LOG_ERROR( "ASTC files with z size other than 1 are not supported. Z size is: %d\n", zDepth );
154     headerIsValid = false;
155   }
156
157   return headerIsValid;
158 }
159
160 } // Unnamed namespace.
161
162
163 // File loading API entry-point:
164 bool LoadAstcHeader( const Dali::ImageLoader::Input& input, unsigned int& width, unsigned int& height )
165 {
166   AstcFileHeader fileHeader;
167   return LoadAstcHeader( input.file, width, height, fileHeader );
168 }
169
170 // File loading API entry-point:
171 bool LoadBitmapFromAstc( const Dali::ImageLoader::Input& input, Dali::Devel::PixelBuffer& bitmap )
172 {
173   FILE* const filePointer = input.file;
174   if( !filePointer )
175   {
176     DALI_LOG_ERROR( "Null file handle passed to ASTC compressed bitmap file loader.\n" );
177     return false;
178   }
179
180   // Load the header info.
181   AstcFileHeader fileHeader;
182   unsigned int width, height;
183
184   if( !LoadAstcHeader( filePointer, width, height, fileHeader ) )
185   {
186     DALI_LOG_ERROR( "Could not load ASTC Header from file.\n" );
187     return false;
188   }
189
190   // Retrieve the pixel format from the ASTC block size.
191   Pixel::Format pixelFormat = GetAstcPixelFormat( fileHeader );
192   if( pixelFormat == Pixel::INVALID )
193   {
194     DALI_LOG_ERROR( "No internal pixel format supported for ASTC file pixel format.\n" );
195     return false;
196   }
197
198   // Retrieve the file size.
199   if( fseek( filePointer, 0L, SEEK_END ) )
200   {
201     DALI_LOG_ERROR( "Could not seek through file.\n" );
202     return false;
203   }
204
205   off_t fileSize = ftell( filePointer );
206   if( fileSize == -1L )
207   {
208     DALI_LOG_ERROR( "Could not determine ASTC file size.\n" );
209     return false;
210   }
211
212   if( fseek( filePointer, sizeof( AstcFileHeader ), SEEK_SET ) )
213   {
214     DALI_LOG_ERROR( "Could not seek through file.\n" );
215     return false;
216   }
217
218   // Data size is file size - header size.
219   size_t imageByteCount = fileSize - sizeof( AstcFileHeader );
220
221   // Sanity-check the image data is not too large and that it is at less than 2 bytes per texel:
222   if( ( imageByteCount > MAX_IMAGE_DATA_SIZE ) || ( imageByteCount > ( ( static_cast< size_t >( width ) * height ) << 1 ) ) )
223   {
224     DALI_LOG_ERROR( "ASTC file has too large image-data field.\n" );
225     return false;
226   }
227
228   // allocate pixel data
229   bitmap = Dali::Devel::PixelBuffer::New(width, height, pixelFormat);
230
231   // Compressed format won't allocate the buffer
232   auto pixels = bitmap.GetBuffer();
233   if( !pixels )
234   {
235     // allocate buffer manually
236     auto& impl = GetImplementation( bitmap );
237     impl.AllocateFixedSize( imageByteCount );
238     pixels = bitmap.GetBuffer();
239   }
240
241   // Load the image data.
242   const size_t bytesRead = fread( pixels, 1, imageByteCount, filePointer );
243
244   // Check the size of loaded data is what we expected.
245   if( bytesRead != imageByteCount )
246   {
247     DALI_LOG_ERROR( "Read of image pixel data failed.\n" );
248     return false;
249   }
250
251   return true;
252 }
253
254 } // namespace TizenPlatform
255
256 } // namespace Dali