20 #include "user/libraries/libtui/include/Png.h" 26 : m_PngPtr(0), m_InfoPtr(0), m_nWidth(0), m_nHeight(0), m_pRowPointers(0)
29 FILE *stream = fopen(filename,
"rb");
32 klog(LOG_ALERT,
"PNG file failed to open");
39 if (fread(buf, 1, 4, stream) != 4)
41 klog(LOG_ALERT,
"PNG file failed to read ident");
45 if (png_sig_cmp(reinterpret_cast<png_byte *>(buf), 0, 4) != 0)
47 klog(LOG_ALERT,
"PNG file failed IDENT check");
52 m_PngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
56 klog(LOG_ALERT,
"PNG file failed to initialise");
61 m_InfoPtr = png_create_info_struct(m_PngPtr);
64 klog(LOG_ALERT,
"PNG info failed to initialise");
69 png_init_io(m_PngPtr, stream);
71 png_set_sig_bytes(m_PngPtr, 4);
73 png_set_palette_to_rgb(m_PngPtr);
77 PNG_TRANSFORM_STRIP_16 |
78 PNG_TRANSFORM_STRIP_ALPHA |
79 PNG_TRANSFORM_PACKING,
81 reinterpret_cast<void *>(0));
83 m_pRowPointers = png_get_rows(m_PngPtr, m_InfoPtr);
86 int bit_depth, color_type, interlace_type, compression_type, filter_method;
89 m_PngPtr, m_InfoPtr, &w, &h, &bit_depth, &color_type, &interlace_type,
90 &compression_type, &filter_method);
96 klog(LOG_ALERT,
"PNG - invalid bit depth");
99 if (color_type != PNG_COLOR_TYPE_RGB)
101 klog(LOG_ALERT,
"PNG - invalid colour type: %d", color_type);
105 klog(LOG_ALERT,
"PNG loaded %ul %ul", m_nWidth, m_nHeight);
112 void Png::render(
rgb_t *pFb,
size_t x,
size_t y,
size_t width,
size_t height)
114 for (
size_t r = 0; r < m_nHeight; r++)
119 for (
size_t c = 0; c < m_nWidth; c++)
125 rgb.r = m_pRowPointers[r][c * 3 + 0];
126 rgb.g = m_pRowPointers[r][c * 3 + 1];
127 rgb.b = m_pRowPointers[r][c * 3 + 2];
130 pFb[(r + y) * width + (c + x)] = rgb;
139 uint8_t range = 1 << pf.
mRed;
142 r = (r * range) / 256;
147 g = (g * range) / 256;
149 range = 1 << pf.
mBlue;
152 b = (b * range) / 256;
155 return 0 | (
static_cast<uint32_t
>(r) << pf.
pRed) |
156 (
static_cast<uint32_t
>(g) << pf.
pGreen) |
157 (
static_cast<uint32_t
>(b) << pf.
pBlue);
Png(const char *filename)