-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPNGRender.h
65 lines (44 loc) · 1.31 KB
/
PNGRender.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once
#include <Locator.h>
#include "mbedtls/base64.h"
#include <PNGdec.h>
#define IMAGE_BUFFER_SIZE 1024
static PNG png;
static unsigned char decodedArray[IMAGE_BUFFER_SIZE];
static size_t decodedArrayLength;
typedef struct png_position
{
uint8_t xoff, yoff;
} PNG_POSITION;
static void PNGDraw(PNGDRAW *pDraw)
{
uint16_t usPixels[64];
PNG_POSITION *pPos = (PNG_POSITION *)pDraw->pUser;
png.getLineAsRGB565(pDraw, usPixels, PNG_RGB565_LITTLE_ENDIAN, 0xffffffff);
Locator::getDisplay()->drawRGBBitmap(pPos->xoff, pPos->yoff + pDraw->y, usPixels, pDraw->iWidth, 1);
}
static bool openImage(const char *base64Image)
{
mbedtls_base64_decode(decodedArray, IMAGE_BUFFER_SIZE, &decodedArrayLength, (const unsigned char *)base64Image, strlen(base64Image));
int rc = png.openRAM(decodedArray, decodedArrayLength, PNGDraw);
return (rc == PNG_SUCCESS);
}
static void renderImage(const char *base64Image, const uint8_t x, const uint8_t y)
{
PNG_POSITION pos;
if (openImage(base64Image))
{
pos.xoff = x;
pos.yoff = y;
int rc = png.decode((void *)&pos, 0);
png.close();
}
}
static void getImageDimensions(const char *base64Image, uint8_t &width, uint8_t &height)
{
if (openImage(base64Image)) {
width = png.getWidth();
height = png.getHeight();
png.close();
}
}