Linear transformed texture sampling for (d)bitmap
[lsnes.git] / include / lua / bitmap.hpp
blob1c679cadb7f8bb6ad1b5d2c95f356ecfb8fa6450
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 hash(lua::state& L, lua::parameters& P);
60 template<bool scaled, bool porterduff> int blit(lua::state& L, lua::parameters& P);
61 template<bool scaled> int blit_priority(lua::state& L, lua::parameters& P);
62 int save_png(lua::state& L, lua::parameters& P);
63 int _save_png(lua::state& L, lua::parameters& P, bool is_method);
64 int sample_texture(lua::state& L, lua::parameters& P);
67 struct lua_dbitmap
69 lua_dbitmap(lua::state& L, uint32_t w, uint32_t h);
70 static size_t overcommit(uint32_t w, uint32_t h) {
71 return lua::overcommit_std_align + sizeof(framebuffer::color) * (size_t)w * h;
73 ~lua_dbitmap();
74 size_t width;
75 size_t height;
76 framebuffer::color* pixels;
77 std::vector<char> save_png() const;
78 std::string print();
79 static int create(lua::state& L, lua::parameters& P);
80 template<bool outside, bool clip> int draw(lua::state& L, lua::parameters& P);
81 int pset(lua::state& L, lua::parameters& P);
82 int pget(lua::state& L, lua::parameters& P);
83 int size(lua::state& L, lua::parameters& P);
84 int hash(lua::state& L, lua::parameters& P);
85 template<bool scaled, bool porterduff> int blit(lua::state& L, lua::parameters& P);
86 int save_png(lua::state& L, lua::parameters& P);
87 int adjust_transparency(lua::state& L, lua::parameters& P);
88 int _save_png(lua::state& L, lua::parameters& P, bool is_method);
89 int sample_texture(lua::state& L, lua::parameters& P);
92 struct lua_loaded_bitmap
94 size_t w;
95 size_t h;
96 bool d;
97 std::vector<int64_t> bitmap;
98 std::vector<int64_t> palette;
99 static struct lua_loaded_bitmap load(std::istream& stream);
100 static struct lua_loaded_bitmap load(const std::string& name);
101 template<bool png> static int load(lua::state& L, lua::parameters& P);
102 template<bool png> static int load_str(lua::state& L, lua::parameters& P);
105 template<bool T> class lua_bitmap_holder
107 public:
108 lua_bitmap_holder(lua_bitmap& _b, lua_palette& _p) : b(_b), p(_p) {};
109 size_t stride() { return b.width; }
110 void lock()
112 p.palette_mutex.lock();
113 palette = p.colors;
114 pallim = p.color_count;
116 void unlock()
118 p.palette_mutex.unlock();
120 void draw(size_t bmpidx, typename framebuffer::fb<T>::element_t& target)
122 uint16_t i = b.pixels[bmpidx];
123 if(i < pallim)
124 palette[i].apply(target);
126 private:
127 lua_bitmap& b;
128 lua_palette& p;
129 framebuffer::color* palette;
130 size_t pallim;
133 template<bool T> class lua_dbitmap_holder
135 public:
136 lua_dbitmap_holder(lua_dbitmap& _d) : d(_d) {};
137 size_t stride() { return d.width; }
138 void lock() {}
139 void unlock() {}
140 void draw(size_t bmpidx, typename framebuffer::fb<T>::element_t& target)
142 d.pixels[bmpidx].apply(target);
144 private:
145 lua_dbitmap& d;
149 template<bool T, class B> void lua_bitmap_composite(struct framebuffer::fb<T>& scr, int32_t xp,
150 int32_t yp, const range& X, const range& Y, const range& sX, const range& sY, bool outside, B bmp) throw()
152 if(!X.size() || !Y.size()) return;
153 size_t stride = bmp.stride();
154 bmp.lock();
156 for(uint32_t r = Y.low(); r != Y.high(); r++) {
157 typename framebuffer::fb<T>::element_t* rptr = scr.rowptr(yp + r);
158 size_t eptr = xp + X.low();
159 uint32_t xmin = X.low();
160 bool cut = outside && sY.in(r);
161 if(cut && sX.in(xmin)) {
162 xmin = sX.high();
163 eptr += (sX.high() - X.low());
165 for(uint32_t c = xmin; c < X.high(); c++, eptr++) {
166 if(__builtin_expect(cut && c == sX.low(), 0)) {
167 c += sX.size();
168 eptr += sX.size();
170 bmp.draw(r * stride + c, rptr[eptr]);
173 bmp.unlock();
176 #endif