Thanks 71M/Shazz p_texture is either an existing texture (in case it must be of the correct dimensions and format) else a new texture is created and returned.
| 158 | // correct dimensions and format) else a new texture is created and returned. |
| 159 | //***************************************************************************** |
| 160 | CRefPtr<CNativeTexture> LoadPng( const char * p_filename, ETextureFormat texture_format ) |
| 161 | { |
| 162 | const size_t SIGNATURE_SIZE = 8; |
| 163 | u8 signature[ SIGNATURE_SIZE ]; |
| 164 | |
| 165 | FILE * fh = fopen( p_filename,"rb" ); |
| 166 | if (fh == NULL) |
| 167 | { |
| 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | if (fread( signature, sizeof(u8), SIGNATURE_SIZE, fh ) != SIGNATURE_SIZE) |
| 172 | { |
| 173 | fclose(fh); |
| 174 | return NULL; |
| 175 | } |
| 176 | |
| 177 | if (!png_check_sig( signature, SIGNATURE_SIZE )) |
| 178 | { |
| 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | png_struct * p_png_struct = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL ); |
| 183 | if (p_png_struct == NULL) |
| 184 | { |
| 185 | return NULL; |
| 186 | } |
| 187 | |
| 188 | png_info * p_png_info = png_create_info_struct( p_png_struct ); |
| 189 | if (p_png_info == NULL) |
| 190 | { |
| 191 | png_destroy_read_struct( &p_png_struct, NULL, NULL ); |
| 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | if (setjmp( png_jmpbuf(p_png_struct) ) != 0) |
| 196 | { |
| 197 | png_destroy_read_struct( &p_png_struct, NULL, NULL ); |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | png_init_io( p_png_struct, fh ); |
| 202 | png_set_sig_bytes( p_png_struct, SIGNATURE_SIZE ); |
| 203 | png_read_png( p_png_struct, p_png_info, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_BGR, NULL ); |
| 204 | |
| 205 | png_uint_32 width = png_get_image_width( p_png_struct, p_png_info ); |
| 206 | png_uint_32 height = png_get_image_height( p_png_struct, p_png_info ); |
| 207 | |
| 208 | CRefPtr<CNativeTexture> texture = CNativeTexture::Create( width, height, texture_format ); |
| 209 | |
| 210 | DAEDALUS_ASSERT( texture->GetWidth() >= width, "Width is unexpectedly small" ); |
| 211 | DAEDALUS_ASSERT( texture->GetHeight() >= height, "Height is unexpectedly small" ); |
| 212 | DAEDALUS_ASSERT( texture_format == texture->GetFormat(), "Texture format doesn't match" ); |
| 213 | |
| 214 | u8 * buffer = new u8[ texture->GetBytesRequired() ]; |
| 215 | if( !buffer ) |
| 216 | { |
| 217 | texture = NULL; |
no test coverage detected