#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2021 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License // If the library is compiled as a DLL, this ensures symbols are exported #define NUCLEX_PIXELS_SOURCE 1 #include "LibWebPHelpers.h" #if defined(NUCLEX_PIXELS_HAVE_LIBWEBP) #include "Nuclex/Pixels/Errors/FileFormatError.h" #include // for assert() #include // for std::min() namespace { // ------------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------------- // } // anonymous namespace namespace Nuclex { namespace Pixels { namespace Storage { namespace WebP { // ------------------------------------------------------------------------------------------- // bool Helpers::IsValidWebPHeader(const std::uint8_t *fileHeader) { // The RIFF format is a chunk-based one and theoretically, it would allow // chunks in any order. However, the WebP specification states that a VP8 // chunk (either vanilla, lossless or extended) should be the first chunk. // At least that's my interpretation... // // https://docs.fileformat.com/image/webp/ // https://developers.google.com/speed/webp/docs/riff_container // return ( (fileHeader[0] == 0x52) && // 1 RIFF header (magic number #1) (fileHeader[1] == 0x49) && // 1 (fileHeader[2] == 0x46) && // 1 (fileHeader[3] == 0x46) && // 1 ( (fileHeader[4] >= 18) || // 2 Chunk size (fileHeader[5] >= 1) || // 2 (header length at least 12 bytes) (fileHeader[6] >= 1) || // 2 (fileHeader[7] >= 1) // 2 ) && (fileHeader[8] == 0x57) && // 3 WebP marker (magic number #2) (fileHeader[9] == 0x45) && // 3 (fileHeader[10] == 0x42) && // 3 (fileHeader[11] == 0x50) && // 3 (fileHeader[12] == 0x56) && // 4 VP8 marker (magic number #3) (fileHeader[13] == 0x50) && // 4 (fileHeader[14] == 0x38) && // 4 ( (fileHeader[15] == 0x20) || // 4 VP8 marker, vanilla VP8 keyframe (fileHeader[15] == 0x4C) || // 4 VP8 marker, lossless VP8 keyframe (fileHeader[15] == 0x58) // 4 VP8 marker, extended VP8 keyframe ) ); } // ------------------------------------------------------------------------------------------- // std::string Helpers::GetErrorMessage(::VP8StatusCode statusCode) { switch(statusCode) { case VP8_STATUS_OK: { return u8"No error"; } case VP8_STATUS_OUT_OF_MEMORY: { return u8"Out of memory - image too big or appearing too big due to corrupted data"; } case VP8_STATUS_INVALID_PARAM: { return u8"Invalid parameters - invalid parameters were passed to a WebP function"; } case VP8_STATUS_BITSTREAM_ERROR: { return u8"Bitstream error - input data was corrupted"; } case VP8_STATUS_UNSUPPORTED_FEATURE: { return u8"Unsupported feature - input data likely conforms to a newer WebP version"; } case VP8_STATUS_SUSPENDED: { return u8"Suspended - processing was suspended"; } case VP8_STATUS_USER_ABORT: { return u8"User abort - processing was aborted"; } case VP8_STATUS_NOT_ENOUGH_DATA: { return u8"Not enough data - input data is truncated or corrupted"; } default: { return u8"Unknown error - the error code returned by a WebP function is undocumented"; } } } // ------------------------------------------------------------------------------------------- // }}}} // namespace Nuclex::Pixels::Storage::WebP #endif // defined(NUCLEX_PIXELS_HAVE_LIBWEBP)