Lua (d)bitmap: add hflip and vflip functions
[lsnes.git] / include / lua / bitmap.hpp
blob70efcd5174d7b5164c24f22a3cfbe52313aeca23
1 #ifndef _lua__bitmap__hpp__included__
2 #define _lua__bitmap__hpp__included__
4 #include <vector>
5 #include <string>
6 #include <cstdint>
7 #include "core/window.hpp"
8 #include "library/lua-base.hpp"
9 #include "library/lua-class.hpp"
10 #include "library/lua-params.hpp"
11 #include "library/framebuffer.hpp"
12 #include "library/range.hpp"
13 #include "library/threads.hpp"
14 #include "library/string.hpp"
16 struct lua_palette
18 std::vector<framebuffer::color> lcolors;
19 framebuffer::color* colors;
20 framebuffer::color* scolors;
21 lua_palette(lua::state& L);
22 size_t color_count;
23 const static size_t reserved_colors = 32;
24 static size_t overcommit() {
25 return lua::overcommit_std_align + reserved_colors * sizeof(framebuffer::color);
27 ~lua_palette();
28 threads::lock palette_mutex;
29 std::string print();
30 static int create(lua::state& L, lua::parameters& P);
31 static int load(lua::state& L, lua::parameters& P);
32 static int load_str(lua::state& L, lua::parameters& P);
33 int set(lua::state& L, lua::parameters& P);
34 int get(lua::state& L, lua::parameters& P);
35 int hash(lua::state& L, lua::parameters& P);
36 int debug(lua::state& L, lua::parameters& P);
37 int adjust_transparency(lua::state& L, lua::parameters& P);
38 void adjust_palette_size(size_t newsize);
39 void push_back(const framebuffer::color& c);
42 struct lua_bitmap
44 lua_bitmap(lua::state& L, uint32_t w, uint32_t h);
45 static size_t overcommit(uint32_t w, uint32_t h) {
46 return lua::overcommit_std_align + sizeof(uint16_t) * (size_t)w * h;
48 ~lua_bitmap();
49 size_t width;
50 size_t height;
51 uint16_t* pixels;
52 std::vector<char> save_png(const lua_palette& pal) const;
53 std::string print();
54 static int create(lua::state& L, lua::parameters& P);
55 template<bool outside, bool clip> int draw(lua::state& L, lua::parameters& P);
56 int pset(lua::state& L, lua::parameters& P);
57 int pget(lua::state& L, lua::parameters& P);
58 int size(lua::state& L, lua::parameters& P);
59 int hflip(lua::state& L, lua::parameters& P);
60 int vflip(lua::state& L, lua::parameters& P);
61 int hash(lua::state& L, lua::parameters& P);
62 template<bool scaled, bool porterduff> int blit(lua::state& L, lua::parameters& P);
63 template<bool scaled> int blit_priority(lua::state& L, lua::parameters& P);
64 int save_png(lua::state& L, lua::parameters& P);
65 int _save_png(lua::state& L, lua::parameters& P, bool is_method);
66 int sample_texture(lua::state& L, lua::parameters& P);
69 struct lua_dbitmap
71 lua_dbitmap(lua::state& L, uint32_t w, uint32_t h);
72 static size_t overcommit(uint32_t w, uint32_t h) {
73 return lua::overcommit_std_align + sizeof(framebuffer::color) * (size_t)w * h;
75 ~lua_dbitmap();
76 size_t width;
77 size_t height;
78 framebuffer::color* pixels;
79 std::vector<char> save_png() const;
80 std::string print();
81 static int create(lua::state& L, lua::parameters& P);
82 template<bool outside, bool clip> int draw(lua::state& L, lua::parameters& P);
83 int pset(lua::state& L, lua::parameters& P);
84 int pget(lua::state& L, lua::parameters& P);
85 int size(lua::state& L, lua::parameters& P);
86 int hflip(lua::state& L, lua::parameters& P);
87 int vflip(lua::state& L, lua::parameters& P);
88 int hash(lua::state& L, lua::parameters& P);
89 template<bool scaled, bool porterduff> int blit(lua::state& L, lua::parameters& P);
90 int save_png(lua::state& L, lua::parameters& P);
91 int adjust_transparency(lua::state& L, lua::parameters& P);
92 int _save_png(lua::state& L, lua::parameters& P, bool is_method);
93 int sample_texture(lua::state& L, lua::parameters& P);
96 struct lua_loaded_bitmap
98 size_t w;
99 size_t h;
100 bool d;
101 std::vector<int64_t> bitmap;
102 std::vector<int64_t> palette;
103 static struct lua_loaded_bitmap load(std::istream& stream);
104 static struct lua_loaded_bitmap load(const std::string& name);
105 template<bool png> static int load(lua::state& L, lua::parameters& P);
106 template<bool png> static int load_str(lua::state& L, lua::parameters& P);
109 template<bool T> class lua_bitmap_holder
111 public:
112 lua_bitmap_holder(lua_bitmap& _b, lua_palette& _p) : b(_b), p(_p) {};
113 size_t stride() { return b.width; }
114 void lock()
116 p.palette_mutex.lock();
117 palette = p.colors;
118 pallim = p.color_count;
120 void unlock()
122 p.palette_mutex.unlock();
124 void draw(size_t bmpidx, typename framebuffer::fb<T>::element_t& target)
126 uint16_t i = b.pixels[bmpidx];
127 if(i < pallim)
128 palette[i].apply(target);
130 private:
131 lua_bitmap& b;
132 lua_palette& p;
133 framebuffer::color* palette;
134 size_t pallim;
137 template<bool T> class lua_dbitmap_holder
139 public:
140 lua_dbitmap_holder(lua_dbitmap& _d) : d(_d) {};
141 size_t stride() { return d.width; }
142 void lock() {}
143 void unlock() {}
144 void draw(size_t bmpidx, typename framebuffer::fb<T>::element_t& target)
146 d.pixels[bmpidx].apply(target);
148 private:
149 lua_dbitmap& d;
153 template<bool T, class B> void lua_bitmap_composite(struct framebuffer::fb<T>& scr, int32_t xp,
154 int32_t yp, const range& X, const range& Y, const range& sX, const range& sY, bool outside, B bmp) throw()
156 if(!X.size() || !Y.size()) return;
157 size_t stride = bmp.stride();
158 bmp.lock();
160 for(uint32_t r = Y.low(); r != Y.high(); r++) {
161 typename framebuffer::fb<T>::element_t* rptr = scr.rowptr(yp + r);
162 size_t eptr = xp + X.low();
163 uint32_t xmin = X.low();
164 bool cut = outside && sY.in(r);
165 if(cut && sX.in(xmin)) {
166 xmin = sX.high();
167 eptr += (sX.high() - X.low());
169 for(uint32_t c = xmin; c < X.high(); c++, eptr++) {
170 if(__builtin_expect(cut && c == sX.low(), 0)) {
171 c += sX.size();
172 eptr += sX.size();
174 bmp.draw(r * stride + c, rptr[eptr]);
177 bmp.unlock();
180 #endif