1// pngreader.h - Public Domain - see unlicense at bottom of pvpngreader.cpp
2#pragma once
3#include <stdint.h>
4
5namespace pv_png
6{
7 // PNG color types
8 enum
9 {
10 PNG_COLOR_TYPE_GREYSCALE = 0,
11 PNG_COLOR_TYPE_TRUECOLOR = 2,
12 PNG_COLOR_TYPE_PALETTIZED = 3,
13 PNG_COLOR_TYPE_GREYSCALE_ALPHA = 4,
14 PNG_COLOR_TYPE_TRUECOLOR_ALPHA = 6
15 };
16
17 // PNG file description
18 struct png_info
19 {
20 uint32_t m_width;
21 uint32_t m_height;
22
23 uint32_t m_num_chans; // The number of channels, factoring in transparency. Ranges from [1-4].
24
25 uint32_t m_bit_depth; // PNG ihdr bit depth: 1, 2, 4, 8 or 16
26 uint32_t m_color_type; // PNG ihdr color type, PNG_COLOR_TYPE_GRAYSCALE etc.
27
28 bool m_has_gamma; // true if the PNG file had a GAMA chunk
29 uint32_t m_gamma_value; // PNG GAMA chunk value, scaled by 100000
30
31 bool m_has_trns; // true if the PNG file used colorkey transparency
32 };
33
34 // Retrieved information about the PNG file.
35 // Returns false on any errors.
36 bool get_png_info(const void* pImage_buf, size_t buf_size, png_info& info);
37
38 // Input parameters:
39 // pImage_buf, buf_size - pointer to PNG image data
40 // desired_chans - desired number of output channels. 0=auto, 1=grayscale, 2=grayscale alpha, 3=24bpp RGB, 4=32bpp RGBA
41 //
42 // Output parameters:
43 // width, height - PNG image resolution
44 // num_chans - actual number of channels in PNG, from [1,4] (factoring in transparency)
45 //
46 // Returns nullptr on any errors.
47 void* load_png(const void* pImage_buf, size_t buf_size, uint32_t desired_chans, uint32_t &width, uint32_t &height, uint32_t& num_chans);
48}
49