Pogosim
Loading...
Searching...
No Matches
fpng.h
Go to the documentation of this file.
1// fpng.h - unlicense (see end of fpng.cpp)
2#pragma once
3
4#include <stdlib.h>
5#include <stdint.h>
6#include <vector>
7
8#ifndef FPNG_TRAIN_HUFFMAN_TABLES
9 // Set to 1 when using the -t (training) option in fpng_test to generate new opaque/alpha Huffman tables for the single pass encoder.
10 #define FPNG_TRAIN_HUFFMAN_TABLES (0)
11#endif
12
13namespace fpng
14{
15 // ---- Library initialization - call once to identify if the processor supports SSE.
16 // Otherwise you'll only get scalar fallbacks.
17 void fpng_init();
18
19 // ---- Useful Utilities
20
21 // Returns true if the CPU supports SSE 4.1, and SSE support wasn't disabled by setting FPNG_NO_SSE=1.
22 // fpng_init() must have been called first, or it'll assert and return false.
24
25 // Fast CRC-32 SSE4.1+pclmul or a scalar fallback (slice by 4)
26 const uint32_t FPNG_CRC32_INIT = 0;
27 uint32_t fpng_crc32(const void* pData, size_t size, uint32_t prev_crc32 = FPNG_CRC32_INIT);
28
29 // Fast Adler32 SSE4.1 Adler-32 with a scalar fallback.
30 const uint32_t FPNG_ADLER32_INIT = 1;
31 uint32_t fpng_adler32(const void* pData, size_t size, uint32_t adler = FPNG_ADLER32_INIT);
32
33 // ---- Compression
34 enum
35 {
36 // Enables computing custom Huffman tables for each file, instead of using the custom global tables.
37 // Results in roughly 6% smaller files on average, but compression is around 40% slower.
39
40 // Only use raw Deflate blocks (no compression at all). Intended for testing.
42 };
43
44 // Fast PNG encoding. The resulting file can be decoded either using a standard PNG decoder or by the fpng_decode_memory() function below.
45 // pImage: pointer to RGB or RGBA image pixels, R first in memory, B/A last.
46 // w/h - image dimensions. Image's row pitch in bytes must is w*num_chans.
47 // num_chans must be 3 or 4.
48 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);
49
50#ifndef FPNG_NO_STDIO
51 // Fast PNG encoding to the specified file.
52 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);
53#endif
54
55 // ---- Decompression
56
57 enum
58 {
59 FPNG_DECODE_SUCCESS = 0, // file is a valid PNG file and written by FPNG and the decode succeeded
60
61 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
62
63 FPNG_DECODE_INVALID_ARG, // invalid function parameter
64
65 FPNG_DECODE_FAILED_NOT_PNG, // file cannot be a PNG file
66 FPNG_DECODE_FAILED_HEADER_CRC32, // a chunk CRC32 check failed, file is likely corrupted or not PNG
67 FPNG_DECODE_FAILED_INVALID_DIMENSIONS, // invalid image dimensions in IHDR chunk (0 or too large)
68 FPNG_DECODE_FAILED_DIMENSIONS_TOO_LARGE, // decoding the file fully into memory would likely require too much memory (only on 32bpp builds)
69 FPNG_DECODE_FAILED_CHUNK_PARSING, // failed while parsing the chunk headers, or file is corrupted
70 FPNG_DECODE_FAILED_INVALID_IDAT, // IDAT data length is too small and cannot be valid, file is either corrupted or it's a bug
71
72 // fpng_decode_file() specific errors
77 };
78
79 // Fast PNG decoding of files ONLY created by fpng_encode_image_to_memory() or fpng_encode_image_to_file().
80 // 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.
81 //
82 // 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.
83 //
84 // pImage, image_size: Pointer to PNG image data and its size
85 // width, height: output image's dimensions
86 // channels_in_file: will be 3 or 4
87 //
88 // Returns FPNG_DECODE_SUCCESS on success, otherwise one of the failure codes above.
89 // If FPNG_DECODE_NOT_FPNG is returned, you must decompress the file with a general purpose PNG decoder.
90 // 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).
91 int fpng_get_info(const void* pImage, uint32_t image_size, uint32_t& width, uint32_t& height, uint32_t& channels_in_file);
92
93 // fpng_decode_memory() decompresses 24/32bpp PNG files ONLY encoded by this module.
94 // 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.)
95 //
96 // pImage, image_size: Pointer to PNG image data and its size
97 // out: Output 24/32bpp image buffer
98 // width, height: output image's dimensions
99 // channels_in_file: will be 3 or 4
100 // desired_channels: must be 3 or 4
101 //
102 // If the image is 24bpp and 32bpp is requested, the alpha values will be set to 0xFF.
103 // If the image is 32bpp and 24bpp is requested, the alpha values will be discarded.
104 //
105 // Returns FPNG_DECODE_SUCCESS on success, otherwise one of the failure codes above.
106 // If FPNG_DECODE_NOT_FPNG is returned, you must decompress the file with a general purpose PNG decoder.
107 // 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).
108 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);
109
110#ifndef FPNG_NO_STDIO
111 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);
112#endif
113
114 // ---- Internal API used for Huffman table training purposes
115
116#if FPNG_TRAIN_HUFFMAN_TABLES
117 const uint32_t HUFF_COUNTS_SIZE = 288;
118 extern uint64_t g_huff_counts[HUFF_COUNTS_SIZE];
119 bool create_dynamic_block_prefix(uint64_t* pFreq, uint32_t num_chans, std::vector<uint8_t>& prefix, uint64_t& bit_buf, int& bit_buf_size, uint32_t *pCodes, uint8_t *pCodesizes);
120#endif
121
122} // namespace fpng
Definition fpng.h:14
void fpng_init()
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)
uint32_t fpng_crc32(const void *pData, size_t size, uint32_t prev_crc32=FPNG_CRC32_INIT)
const uint32_t FPNG_ADLER32_INIT
Definition fpng.h:30
const uint32_t FPNG_CRC32_INIT
Definition fpng.h:26
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)
@ FPNG_FORCE_UNCOMPRESSED
Definition fpng.h:41
@ FPNG_ENCODE_SLOWER
Definition fpng.h:38
@ FPNG_DECODE_FILE_SEEK_FAILED
Definition fpng.h:76
@ FPNG_DECODE_INVALID_ARG
Definition fpng.h:63
@ FPNG_DECODE_FILE_READ_FAILED
Definition fpng.h:75
@ FPNG_DECODE_FAILED_DIMENSIONS_TOO_LARGE
Definition fpng.h:68
@ FPNG_DECODE_FILE_TOO_LARGE
Definition fpng.h:74
@ FPNG_DECODE_SUCCESS
Definition fpng.h:59
@ FPNG_DECODE_FAILED_CHUNK_PARSING
Definition fpng.h:69
@ FPNG_DECODE_NOT_FPNG
Definition fpng.h:61
@ FPNG_DECODE_FAILED_NOT_PNG
Definition fpng.h:65
@ FPNG_DECODE_FAILED_INVALID_DIMENSIONS
Definition fpng.h:67
@ FPNG_DECODE_FAILED_INVALID_IDAT
Definition fpng.h:70
@ FPNG_DECODE_FAILED_HEADER_CRC32
Definition fpng.h:66
@ FPNG_DECODE_FILE_OPEN_FAILED
Definition fpng.h:73
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)
uint32_t fpng_adler32(const void *pData, size_t size, uint32_t adler=FPNG_ADLER32_INIT)
bool fpng_cpu_supports_sse41()
int fpng_get_info(const void *pImage, uint32_t image_size, uint32_t &width, uint32_t &height, uint32_t &channels_in_file)
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)