79 lines
1.5 KiB
C
Raw Normal View History

#include <stddef.h>
#include <stdint.h>
#include "lodepng.h"
#include "png.h"
// dest and src can be the same
2019-05-25 19:10:45 -03:00
static inline void _rgb24_to_rgb565(u16 *dest, const u8 *src, size_t dim)
2018-03-29 23:45:58 -03:00
{
2019-05-25 19:10:45 -03:00
for (size_t i = 0; i < dim; i++) {
u8 r, g, b;
r = *(src++) >> 3;
g = *(src++) >> 2;
b = *(src++) >> 3;
*(dest++) = r << 11 | g << 5 | b;
2018-03-29 23:45:58 -03:00
}
}
// dest and src CAN NOT be the same
2019-05-25 19:10:45 -03:00
static inline void _rgb565_to_rgb24(u8 *dest, const u16 *src, size_t dim)
{
for (size_t i = 0; i < dim; i++) {
u16 rgb = *(src++);
*(dest++) = (rgb >> 11) << 3;
*(dest++) = ((rgb >> 5) & 0x3F) << 2;
*(dest++) = (rgb & 0x1F) << 3;
}
}
u16 *PNG_Decompress(const u8 *png, size_t png_len, u32 *w, u32 *h)
{
2019-05-25 19:10:45 -03:00
u16 *img;
unsigned res;
size_t width, height;
2019-05-25 10:34:29 -03:00
img = NULL;
2019-05-25 19:10:45 -03:00
res = lodepng_decode24((u8**)&img, &width, &height, png, png_len);
2018-03-29 23:45:58 -03:00
if (res) {
2019-05-25 10:34:29 -03:00
free(img);
return NULL;
}
2019-05-25 19:10:45 -03:00
_rgb24_to_rgb565(img, (const u8*)img, width * height);
if (w) *w = width;
if (h) *h = height;
// the allocated buffer will be w*h*3 bytes long, but only w*h*2 bytes will be used
// however, this is not a problem and it'll all be freed with a regular free() call
return (u16*)img;
}
2018-03-29 23:45:58 -03:00
2019-05-25 19:10:45 -03:00
u8 *PNG_Compress(const u16 *fb, u32 w, u32 h, size_t *png_sz)
2018-03-29 23:45:58 -03:00
{
2019-05-25 19:10:45 -03:00
u8 *img, *buf;
unsigned res;
2018-03-29 23:45:58 -03:00
size_t png_size;
2019-05-25 10:34:29 -03:00
img = NULL;
2019-05-25 19:10:45 -03:00
buf = malloc(w * h * 3);
if (!buf) return NULL;
_rgb565_to_rgb24(buf, fb, w * h);
res = lodepng_encode24(&img, &png_size, buf, w, h);
free(buf);
2018-03-29 23:45:58 -03:00
if (res) {
2019-05-25 10:34:29 -03:00
free(img);
2018-03-29 23:45:58 -03:00
return NULL;
}
2019-05-25 10:34:29 -03:00
if (png_sz)
*png_sz = png_size;
2018-03-29 23:45:58 -03:00
return img;
}