2 * Copyright (c) 2017 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.
19 #include <dali/internal/imaging/common/loader-astc.h>
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>
31 namespace TizenPlatform
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;
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;
47 // This bytes identify an ASTC native file.
48 const Byte FileIdentifier[] = {
49 0x13, 0xAB, 0xA1, 0x5C
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.
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__));
70 using namespace Pixel;
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
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 }
92 * @brief Uses header information to return the respective ASTC pixel format.
94 * @param[in] header A populated AstcFileHeader struct
95 * @return The pixel format, or INVALID if the block size was invalid
97 Pixel::Format GetAstcPixelFormat( AstcFileHeader& header )
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 ) )
103 return Pixel::INVALID;
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 ];
111 * @brief Internal method to load ASTC header info from a file.
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
119 bool LoadAstcHeader( FILE * const filePointer, unsigned int& width, unsigned int& height, AstcFileHeader& fileHeader )
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 )
128 // Check the header contains the ASTC native file identifier.
129 bool headerIsValid = memcmp( fileHeader.magic, FileIdentifier, sizeof( fileHeader.magic ) ) == 0;
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.
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 );
141 const unsigned int zDepth = fileHeader.zsize[0] + ( fileHeader.zsize[1] << 8 ) + ( fileHeader.zsize[2] << 16 );
143 // Check image dimensions are within limits.
144 if( ( width > MAX_TEXTURE_DIMENSION ) || ( height > MAX_TEXTURE_DIMENSION ) )
146 DALI_LOG_ERROR( "ASTC file has larger than supported dimensions: %d,%d\n", width, height );
147 headerIsValid = false;
150 // Confirm the ASTC block does not have any Z depth.
153 DALI_LOG_ERROR( "ASTC files with z size other than 1 are not supported. Z size is: %d\n", zDepth );
154 headerIsValid = false;
157 return headerIsValid;
160 } // Unnamed namespace.
163 // File loading API entry-point:
164 bool LoadAstcHeader( const Dali::ImageLoader::Input& input, unsigned int& width, unsigned int& height )
166 AstcFileHeader fileHeader;
167 return LoadAstcHeader( input.file, width, height, fileHeader );
170 // File loading API entry-point:
171 bool LoadBitmapFromAstc( const Dali::ImageLoader::Input& input, Dali::Devel::PixelBuffer& bitmap )
173 FILE* const filePointer = input.file;
176 DALI_LOG_ERROR( "Null file handle passed to ASTC compressed bitmap file loader.\n" );
180 // Load the header info.
181 AstcFileHeader fileHeader;
182 unsigned int width, height;
184 if( !LoadAstcHeader( filePointer, width, height, fileHeader ) )
186 DALI_LOG_ERROR( "Could not load ASTC Header from file.\n" );
190 // Retrieve the pixel format from the ASTC block size.
191 Pixel::Format pixelFormat = GetAstcPixelFormat( fileHeader );
192 if( pixelFormat == Pixel::INVALID )
194 DALI_LOG_ERROR( "No internal pixel format supported for ASTC file pixel format.\n" );
198 // Retrieve the file size.
199 if( fseek( filePointer, 0L, SEEK_END ) )
201 DALI_LOG_ERROR( "Could not seek through file.\n" );
205 off_t fileSize = ftell( filePointer );
206 if( fileSize == -1L )
208 DALI_LOG_ERROR( "Could not determine ASTC file size.\n" );
212 if( fseek( filePointer, sizeof( AstcFileHeader ), SEEK_SET ) )
214 DALI_LOG_ERROR( "Could not seek through file.\n" );
218 // Data size is file size - header size.
219 size_t imageByteCount = fileSize - sizeof( AstcFileHeader );
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 ) ) )
224 DALI_LOG_ERROR( "ASTC file has too large image-data field.\n" );
228 // allocate pixel data
229 bitmap = Dali::Devel::PixelBuffer::New(width, height, pixelFormat);
231 // Compressed format won't allocate the buffer
232 auto pixels = bitmap.GetBuffer();
235 // allocate buffer manually
236 auto& impl = GetImplementation( bitmap );
237 impl.AllocateFixedSize( imageByteCount );
238 pixels = bitmap.GetBuffer();
241 // Load the image data.
242 const size_t bytesRead = fread( pixels, 1, imageByteCount, filePointer );
244 // Check the size of loaded data is what we expected.
245 if( bytesRead != imageByteCount )
247 DALI_LOG_ERROR( "Read of image pixel data failed.\n" );
254 } // namespace TizenPlatform