Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/lvandeve/lodepng.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLode <lvandeve@gmail.com>2022-06-13 21:53:21 +0300
committerLode <lvandeve@gmail.com>2022-06-13 21:53:21 +0300
commit71064f28b6ac8283a3fc529aa5b67f6c027293f7 (patch)
tree01038dfc2404a4924b1ba3ae2523f83afeed5f55 /lodepng.h
parent3d9fda048393e32cc11d0c3d3caba0a85c1c2dfe (diff)
sBIT chunk support, and a few improved bounds checks
Diffstat (limited to 'lodepng.h')
-rw-r--r--lodepng.h78
1 files changed, 62 insertions, 16 deletions
diff --git a/lodepng.h b/lodepng.h
index 685e785..e83a508 100644
--- a/lodepng.h
+++ b/lodepng.h
@@ -1,5 +1,5 @@
/*
-LodePNG version 20220109
+LodePNG version 20220613
Copyright (c) 2005-2022 Lode Vandevenne
@@ -374,8 +374,10 @@ typedef struct LodePNGColorMode {
The alpha channels must be set as well, set them to 255 for opaque images.
- When decoding, by default you can ignore this palette, since LodePNG already
- fills the palette colors in the pixels of the raw RGBA output.
+ When decoding, with the default settings you can ignore this palette, since
+ LodePNG already fills the palette colors in the pixels of the raw RGBA output,
+ but when decoding to the original PNG color mode it is needed to reconstruct
+ the colors.
The palette is only supported for color type 3.
*/
@@ -465,10 +467,12 @@ typedef struct LodePNGInfo {
with values truncated to the bit depth in the unsigned integer.
For grayscale and palette PNGs, the value is stored in background_r. The values
- in background_g and background_b are then unused.
+ in background_g and background_b are then unused. The decoder will set them
+ equal to background_r, the encoder ignores them in this case.
- So when decoding, you may get these in a different color mode than the one you requested
- for the raw pixels.
+ When decoding, you may get these in a different color mode than the one you requested
+ for the raw pixels: the colortype and bitdepth defined by info_png.color, that is the
+ ones defined in the header of the PNG image, are used.
When encoding with auto_convert, you must use the color model defined in info_png.color for
these values. The encoder normally ignores info_png.color when auto_convert is on, but will
@@ -535,7 +539,7 @@ typedef struct LodePNGInfo {
unsigned phys_unit; /*may be 0 (unknown unit) or 1 (metre)*/
/*
- Color profile related chunks: gAMA, cHRM, sRGB, iCPP
+ Color profile related chunks: gAMA, cHRM, sRGB, iCPP, sBIT
LodePNG does not apply any color conversions on pixels in the encoder or decoder and does not interpret these color
profile values. It merely passes on the information. If you wish to use color profiles and convert colors, please
@@ -598,6 +602,45 @@ typedef struct LodePNGInfo {
unsigned char* iccp_profile;
unsigned iccp_profile_size; /* The size of iccp_profile in bytes */
+ /*
+ sBIT chunk: significant bits. Optional metadata, only set this if needed.
+
+ If defined, these values give the bit depth of the original data. Since PNG only stores 1, 2, 4, 8 or 16-bit
+ per channel data, the significant bits value can be used to indicate the original encoded data has another
+ sample depth, such as 10 or 12.
+
+ Encoders using this value, when storing the pixel data, should use the most significant bits
+ of the data to store the original bits, and use a good sample depth scaling method such as
+ "left bit replication" to fill in the least significant bits, rather than fill zeroes.
+
+ Decoders using this value, if able to work with data that's e.g. 10-bit or 12-bit, should right
+ shift the data to go back to the original bit depth, but decoders are also allowed to ignore
+ sbit and work e.g. with the 8-bit or 16-bit data from the PNG directly, since thanks
+ to the encoder contract, the values encoded in PNG are in valid range for the PNG bit depth.
+
+ For grayscale images, sbit_g and sbit_b are not used, and for images that don't use color
+ type RGBA or grayscale+alpha, sbit_a is not used (it's not used even for palette images with
+ translucent palette values, or images with color key). The values that are used must be
+ greater than zero and smaller than or equal to the PNG bit depth.
+
+ The color type from the header in the PNG image defines these used and unused fields: if
+ decoding with a color mode conversion, such as always decoding to RGBA, this metadata still
+ only uses the color type of the original PNG, and may e.g. lack the alpha channel info
+ if the PNG was RGB. When encoding with auto_convert (as well as without), also always the
+ color model defined in info_png.color determines this.
+
+ NOTE: enabling sbit can hurt compression, because the encoder can then not always use
+ auto_convert to choose a more optimal color mode for the data, because the PNG format has
+ strict requirements for the allowed sbit values in combination with color modes.
+ For example, setting these fields to 10-bit will force the encoder to keep using a 16-bit per channel
+ color mode, even if the pixel data would in fact fit in a more efficient 8-bit mode.
+ */
+ unsigned sbit_defined; /*is significant bits given? if not, the values below are unused*/
+ unsigned sbit_r; /*red or gray component of significant bits*/
+ unsigned sbit_g; /*green component of significant bits*/
+ unsigned sbit_b; /*blue component of significant bits*/
+ unsigned sbit_a; /*alpha component of significant bits*/
+
/* End of color profile related chunks */
@@ -770,7 +813,11 @@ typedef struct LodePNGEncoderSettings {
const unsigned char* predefined_filters;
/*force creating a PLTE chunk if colortype is 2 or 6 (= a suggested palette).
- If colortype is 3, PLTE is _always_ created.*/
+ If colortype is 3, PLTE is always created. If color type is explicitely set
+ to a grayscale type (1 or 4), this is not done and is ignored. If enabling this,
+ a palette must be present in the info_png.
+ NOTE: enabling this may worsen compression if auto_convert is used to choose
+ optimal color mode, because it cannot use grayscale color modes in this case*/
unsigned force_palette;
#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
/*add LodePNG identifier and version as a text chunk, for debugging*/
@@ -824,8 +871,8 @@ unsigned lodepng_inspect(unsigned* w, unsigned* h,
#endif /*LODEPNG_COMPILE_DECODER*/
/*
-Reads one metadata chunk (other than IHDR) of the PNG file and outputs what it
-read in the state. Returns error code on failure.
+Reads one metadata chunk (other than IHDR, which is handled by lodepng_inspect)
+of the PNG file and outputs what it read in the state. Returns error code on failure.
Use lodepng_inspect first with a new state, then e.g. lodepng_chunk_find_const
to find the desired chunk type, and if non null use lodepng_inspect_chunk (with
chunk_pointer - start_of_file as pos).
@@ -1103,7 +1150,7 @@ TODO:
[.] check compatibility with various compilers - done but needs to be redone for every newer version
[X] converting color to 16-bit per channel types
[X] support color profile chunk types (but never let them touch RGB values by default)
-[ ] support all public PNG chunk types (almost done except sBIT, sPLT and hIST)
+[ ] support all public PNG chunk types (almost done except sPLT and hIST)
[ ] make sure encoder generates no chunks with size > (2^31)-1
[ ] partial decoding (stream processing)
[X] let the "isFullyOpaque" function check color keys and transparent palettes too
@@ -1230,18 +1277,16 @@ The following features are supported by the decoder:
gAMA: RGB gamma correction
iCCP: ICC color profile
sRGB: rendering intent
+ sBIT: significant bits
1.2. features not supported
---------------------------
-The following features are _not_ supported:
+The following features are not (yet) supported:
*) some features needed to make a conformant PNG-Editor might be still missing.
*) partial loading/stream processing. All data must be available and is processed in one call.
-*) The following public chunks are not (yet) supported but treated as unknown chunks by LodePNG:
- sBIT
- hIST
- sPLT
+*) The hIST and sPLT public chunks are not (yet) supported but treated as unknown chunks
2. C and C++ version
@@ -1845,6 +1890,7 @@ symbol.
Not all changes are listed here, the commit history in github lists more:
https://github.com/lvandeve/lodepng
+*) 13 jun 2022: added support for the sBIT chunk.
*) 09 jan 2022: minor decoder speed improvements.
*) 27 jun 2021: added warnings that file reading/writing functions don't support
wide-character filenames (support for this is not planned, opening files is