| 134 | } |
| 135 | |
| 136 | bool CImageLoader::LoadPng(CByteBufferReader &Reader, const char *pContextName, CImageInfo &Image, int &PngliteIncompatible) |
| 137 | { |
| 138 | CUserErrorStruct UserErrorStruct = {&Reader, pContextName, {}}; |
| 139 | |
| 140 | if(setjmp(UserErrorStruct.m_JmpBuf)) |
| 141 | { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | png_structp pPngStruct = png_create_read_struct(PNG_LIBPNG_VER_STRING, &UserErrorStruct, PngErrorCallback, PngWarningCallback); |
| 146 | if(pPngStruct == nullptr) |
| 147 | { |
| 148 | log_error("png", "libpng internal failure: png_create_read_struct failed."); |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | png_infop pPngInfo = nullptr; |
| 153 | png_bytepp pRowPointers = nullptr; |
| 154 | int Height = 0; // ensure this is not undefined for the Cleanup function |
| 155 | const auto &&Cleanup = [&]() { |
| 156 | if(pRowPointers != nullptr) |
| 157 | { |
| 158 | for(int y = 0; y < Height; ++y) |
| 159 | { |
| 160 | delete[] pRowPointers[y]; |
| 161 | } |
| 162 | } |
| 163 | delete[] pRowPointers; |
| 164 | if(pPngInfo != nullptr) |
| 165 | { |
| 166 | png_destroy_info_struct(pPngStruct, &pPngInfo); |
| 167 | } |
| 168 | png_destroy_read_struct(&pPngStruct, nullptr, nullptr); |
| 169 | }; |
| 170 | if(setjmp(UserErrorStruct.m_JmpBuf)) |
| 171 | { |
| 172 | Cleanup(); |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | pPngInfo = png_create_info_struct(pPngStruct); |
| 177 | if(pPngInfo == nullptr) |
| 178 | { |
| 179 | Cleanup(); |
| 180 | log_error("png", "libpng internal failure: png_create_info_struct failed."); |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | png_byte aSignature[8]; |
| 185 | if(!Reader.Read(aSignature, sizeof(aSignature)) || png_sig_cmp(aSignature, 0, sizeof(aSignature)) != 0) |
| 186 | { |
| 187 | Cleanup(); |
| 188 | log_error("png", "file is not a valid PNG file (signature mismatch)."); |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | png_set_read_fn(pPngStruct, (png_bytep)&Reader, PngReadDataCallback); |
| 193 | png_set_sig_bytes(pPngStruct, sizeof(aSignature)); |
no test coverage detected