Warning, /include/Geant4/tools/fpng is written in an unsupported language. File is not indexed.
0001 #ifndef tools_fpng
0002 #define tools_fpng
0003
0004 // G.Barrand: pure header version of fpng found at https://github.com/richgel999/fpng
0005 // The original namespace fpng had been changed to tools::fpng to avoid
0006 // clashes with potential other usage of fpng within the same software.
0007
0008 // fpng.h - unlicense (see end of fpng.cpp)
0009
0010 #include <stdlib.h>
0011 #include <stdint.h>
0012 #include <vector>
0013
0014 namespace tools {
0015 namespace fpng
0016 {
0017 // Fast CRC-32 SSE4.1+pclmul or a scalar fallback (slice by 4)
0018 const uint32_t FPNG_CRC32_INIT = 0;
0019 uint32_t fpng_crc32(const void* pData, size_t size, uint32_t prev_crc32 = FPNG_CRC32_INIT);
0020
0021 // Fast Adler32 SSE4.1 Adler-32 with a scalar fallback.
0022 const uint32_t FPNG_ADLER32_INIT = 1;
0023 uint32_t fpng_adler32(const void* pData, size_t size, uint32_t adler = FPNG_ADLER32_INIT);
0024
0025 // ---- Compression
0026 enum
0027 {
0028 // Enables computing custom Huffman tables for each file, instead of using the custom global tables.
0029 // Results in roughly 6% smaller files on average, but compression is around 40% slower.
0030 FPNG_ENCODE_SLOWER = 1,
0031
0032 // Only use raw Deflate blocks (no compression at all). Intended for testing.
0033 FPNG_FORCE_UNCOMPRESSED = 2,
0034 };
0035
0036 // Fast PNG encoding. The resulting file can be decoded either using a standard PNG decoder or by the fpng_decode_memory() function below.
0037 // pImage: pointer to RGB or RGBA image pixels, R first in memory, B/A last.
0038 // w/h - image dimensions. Image's row pitch in bytes must is w*num_chans.
0039 // num_chans must be 3 or 4.
0040 bool fpng_encode_image_to_memory(const void* pImage, uint32_t w, uint32_t h, uint32_t num_chans, std::vector<uint8_t>& out_buf, uint32_t flags = 0);
0041
0042 // Fast PNG encoding to the specified file.
0043 bool fpng_encode_image_to_file(const char* pFilename, const void* pImage, uint32_t w, uint32_t h, uint32_t num_chans, uint32_t flags = 0);
0044
0045 // ---- Decompression
0046
0047 enum
0048 {
0049 FPNG_DECODE_SUCCESS = 0, // file is a valid PNG file and written by FPNG and the decode succeeded
0050
0051 FPNG_DECODE_NOT_FPNG, // file is a valid PNG file, but it wasn't written by FPNG so you should try decoding it with a general purpose PNG decoder
0052
0053 FPNG_DECODE_INVALID_ARG, // invalid function parameter
0054
0055 FPNG_DECODE_FAILED_NOT_PNG, // file cannot be a PNG file
0056 FPNG_DECODE_FAILED_HEADER_CRC32, // a chunk CRC32 check failed, file is likely corrupted or not PNG
0057 FPNG_DECODE_FAILED_INVALID_DIMENSIONS, // invalid image dimensions in IHDR chunk (0 or too large)
0058 FPNG_DECODE_FAILED_DIMENSIONS_TOO_LARGE, // decoding the file fully into memory would likely require too much memory (only on 32bpp builds)
0059 FPNG_DECODE_FAILED_CHUNK_PARSING, // failed while parsing the chunk headers, or file is corrupted
0060 FPNG_DECODE_FAILED_INVALID_IDAT, // IDAT data length is too small and cannot be valid, file is either corrupted or it's a bug
0061
0062 // fpng_decode_file() specific errors
0063 FPNG_DECODE_FILE_OPEN_FAILED,
0064 FPNG_DECODE_FILE_TOO_LARGE,
0065 FPNG_DECODE_FILE_READ_FAILED,
0066 FPNG_DECODE_FILE_SEEK_FAILED
0067 };
0068
0069 // Fast PNG decoding of files ONLY created by fpng_encode_image_to_memory() or fpng_encode_image_to_file().
0070 // If fpng_get_info() or fpng_decode_memory() returns FPNG_DECODE_NOT_FPNG, you should decode the PNG by falling back to a general purpose decoder.
0071 //
0072 // fpng_get_info() parses the PNG header and iterates through all chunks to determine if it's a file written by FPNG, but does not decompress the actual image data so it's relatively fast.
0073 //
0074 // pImage, image_size: Pointer to PNG image data and its size
0075 // width, height: output image's dimensions
0076 // channels_in_file: will be 3 or 4
0077 //
0078 // Returns FPNG_DECODE_SUCCESS on success, otherwise one of the failure codes above.
0079 // If FPNG_DECODE_NOT_FPNG is returned, you must decompress the file with a general purpose PNG decoder.
0080 // If another error occurs, the file is likely corrupted or invalid, but you can still try to decompress the file with another decoder (which will likely fail).
0081 int fpng_get_info(const void* pImage, uint32_t image_size, uint32_t& width, uint32_t& height, uint32_t& channels_in_file);
0082
0083 // fpng_decode_memory() decompresses 24/32bpp PNG files ONLY encoded by this module.
0084 // If the image was written by FPNG, it will decompress the image data, otherwise it will return FPNG_DECODE_NOT_FPNG in which case you should fall back to a general purpose PNG decoder (lodepng, stb_image, libpng, etc.)
0085 //
0086 // pImage, image_size: Pointer to PNG image data and its size
0087 // out: Output 24/32bpp image buffer
0088 // width, height: output image's dimensions
0089 // channels_in_file: will be 3 or 4
0090 // desired_channels: must be 3 or 4
0091 //
0092 // If the image is 24bpp and 32bpp is requested, the alpha values will be set to 0xFF.
0093 // If the image is 32bpp and 24bpp is requested, the alpha values will be discarded.
0094 //
0095 // Returns FPNG_DECODE_SUCCESS on success, otherwise one of the failure codes above.
0096 // If FPNG_DECODE_NOT_FPNG is returned, you must decompress the file with a general purpose PNG decoder.
0097 // If another error occurs, the file is likely corrupted or invalid, but you can still try to decompress the file with another decoder (which will likely fail).
0098 int fpng_decode_memory(const void* pImage, uint32_t image_size, std::vector<uint8_t>& out, uint32_t& width, uint32_t& height, uint32_t& channels_in_file, uint32_t desired_channels);
0099
0100 int fpng_decode_file(const char* pFilename, std::vector<uint8_t>& out, uint32_t& width, uint32_t& height, uint32_t& channels_in_file, uint32_t desired_channels);
0101
0102 } // namespace fpng
0103 } // namespace tools
0104
0105 //G.Barrand specific:
0106 #include "fpng.icc"
0107
0108 #include "sout"
0109
0110 #include <ostream>
0111
0112 namespace tools {
0113 namespace fpng {
0114
0115 inline bool write(std::ostream& a_out,
0116 const std::string& a_file,
0117 unsigned char* a_buffer,
0118 unsigned int a_width,
0119 unsigned int a_height,
0120 unsigned int a_bpp) {
0121 if((a_bpp!=3)&&(a_bpp!=4)) {
0122 a_out << "tools::fpng::write : bpp " << a_bpp << " not handled." << std::endl;
0123 return false;
0124 }
0125 if(!fpng_encode_image_to_file(a_file.c_str(),a_buffer, a_width, a_height, a_bpp)) {
0126 a_out << "tools::fpng::write : encode() failed for file " << sout(a_file) << "." << std::endl;
0127 return false;
0128 }
0129 return true;
0130 }
0131
0132 }}
0133
0134 #endif //tools_fpng