-
Notifications
You must be signed in to change notification settings - Fork 1
/
RawImage.h
71 lines (60 loc) · 1.44 KB
/
RawImage.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
65
66
67
68
69
70
71
#pragma once
#include<stdint.h>
#include<SD/Seeed_SD.h>
/*
USAGE:
// when use 8bit color.
Raw8 * img8 = newImage<uint8_t>("path to sd card image.");
// when use 16bit color.
Raw16 * img16 = newImage<uint16_t>("path to sd card image.");
// do some drawing.
// img8->draw();
// remember release it
img8->release();
img16->release();
*/
extern TFT_eSPI tft;
template<class type>
struct RawImage{
type * ptr(){
return (type *)(this + 1);
}
type get(int16_t x, int16_t y){
return this->ptr()[y * width() + x];
}
void draw(size_t x = 0, size_t y = 0){
tft.pushImage(x, y, width(), height(), ptr());
}
void release(){
delete [] this;
}
int16_t width(){ return _width; }
int16_t height(){ return _height; }
private:
int16_t _width;
int16_t _height;
};
typedef RawImage<uint8_t> Raw8;
typedef RawImage<uint16_t> Raw16;
template<class type>
RawImage<type> * newImage(const char * path){
typedef RawImage<type> raw;
File f = SD.open(path, FILE_READ);
if (!f){
return nullptr;
}
int32_t size = f.size();
raw * mem = (raw *)new uint8_t[size];
if (mem == nullptr){
return nullptr;
}
f.read(mem, size);
f.close();
return mem;
}
template<class type>
void drawImage(const char * path, size_t x = 0, size_t y = 0){
auto img = newImage<type>(path);
img->draw(x, y);
img->release();
}