2012-03-03 11:27:26 +02:00
|
|
|
#ifndef _lua__bitmap__hpp__included__
|
|
|
|
#define _lua__bitmap__hpp__included__
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <cstdint>
|
2012-03-07 14:44:24 +02:00
|
|
|
#include "core/window.hpp"
|
2013-12-20 02:02:12 +02:00
|
|
|
#include "library/lua-base.hpp"
|
2014-01-26 17:42:22 +02:00
|
|
|
#include "library/lua-class.hpp"
|
|
|
|
#include "library/lua-params.hpp"
|
2012-06-20 17:40:27 +03:00
|
|
|
#include "library/framebuffer.hpp"
|
2015-06-23 14:46:14 +03:00
|
|
|
#include "library/range.hpp"
|
2014-03-23 09:45:42 +02:00
|
|
|
#include "library/threads.hpp"
|
2013-09-27 10:36:19 +03:00
|
|
|
#include "library/string.hpp"
|
2012-03-03 11:27:26 +02:00
|
|
|
|
2013-10-28 21:15:08 +02:00
|
|
|
struct lua_palette
|
|
|
|
{
|
2014-03-31 12:42:33 +03:00
|
|
|
std::vector<framebuffer::color> lcolors;
|
|
|
|
framebuffer::color* colors;
|
|
|
|
framebuffer::color* scolors;
|
2013-12-20 02:02:12 +02:00
|
|
|
lua_palette(lua::state& L);
|
2014-03-31 12:42:33 +03:00
|
|
|
size_t color_count;
|
|
|
|
const static size_t reserved_colors = 32;
|
|
|
|
static size_t overcommit() {
|
|
|
|
return lua::overcommit_std_align + reserved_colors * sizeof(framebuffer::color);
|
|
|
|
}
|
2013-10-28 21:15:08 +02:00
|
|
|
~lua_palette();
|
2014-03-23 09:45:42 +02:00
|
|
|
threads::lock palette_mutex;
|
2013-10-28 21:15:08 +02:00
|
|
|
std::string print();
|
2014-01-26 17:42:22 +02:00
|
|
|
static int create(lua::state& L, lua::parameters& P);
|
|
|
|
static int load(lua::state& L, lua::parameters& P);
|
|
|
|
static int load_str(lua::state& L, lua::parameters& P);
|
2014-01-28 14:38:25 +02:00
|
|
|
int set(lua::state& L, lua::parameters& P);
|
2014-12-20 06:38:47 +02:00
|
|
|
int get(lua::state& L, lua::parameters& P);
|
2014-01-28 14:38:25 +02:00
|
|
|
int hash(lua::state& L, lua::parameters& P);
|
|
|
|
int debug(lua::state& L, lua::parameters& P);
|
|
|
|
int adjust_transparency(lua::state& L, lua::parameters& P);
|
2014-03-31 12:42:33 +03:00
|
|
|
void adjust_palette_size(size_t newsize);
|
|
|
|
void push_back(const framebuffer::color& c);
|
2013-10-28 21:15:08 +02:00
|
|
|
};
|
|
|
|
|
2012-03-03 11:27:26 +02:00
|
|
|
struct lua_bitmap
|
|
|
|
{
|
2013-12-20 02:02:12 +02:00
|
|
|
lua_bitmap(lua::state& L, uint32_t w, uint32_t h);
|
2014-03-31 12:42:33 +03:00
|
|
|
static size_t overcommit(uint32_t w, uint32_t h) {
|
2014-03-31 14:15:24 +03:00
|
|
|
return lua::overcommit_std_align + sizeof(uint16_t) * (size_t)w * h;
|
2014-03-31 12:42:33 +03:00
|
|
|
}
|
2013-02-28 04:03:01 +02:00
|
|
|
~lua_bitmap();
|
2012-03-03 11:27:26 +02:00
|
|
|
size_t width;
|
|
|
|
size_t height;
|
2014-03-31 12:42:33 +03:00
|
|
|
uint16_t* pixels;
|
2013-10-28 21:15:08 +02:00
|
|
|
std::vector<char> save_png(const lua_palette& pal) const;
|
2013-09-27 10:36:19 +03:00
|
|
|
std::string print();
|
2014-01-26 17:42:22 +02:00
|
|
|
static int create(lua::state& L, lua::parameters& P);
|
2014-04-03 00:59:05 +03:00
|
|
|
template<bool outside, bool clip> int draw(lua::state& L, lua::parameters& P);
|
2014-01-28 14:38:25 +02:00
|
|
|
int pset(lua::state& L, lua::parameters& P);
|
|
|
|
int pget(lua::state& L, lua::parameters& P);
|
|
|
|
int size(lua::state& L, lua::parameters& P);
|
|
|
|
int hash(lua::state& L, lua::parameters& P);
|
|
|
|
template<bool scaled, bool porterduff> int blit(lua::state& L, lua::parameters& P);
|
|
|
|
template<bool scaled> int blit_priority(lua::state& L, lua::parameters& P);
|
|
|
|
int save_png(lua::state& L, lua::parameters& P);
|
|
|
|
int _save_png(lua::state& L, lua::parameters& P, bool is_method);
|
2012-03-03 11:27:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct lua_dbitmap
|
|
|
|
{
|
2013-12-20 02:02:12 +02:00
|
|
|
lua_dbitmap(lua::state& L, uint32_t w, uint32_t h);
|
2014-03-31 12:42:33 +03:00
|
|
|
static size_t overcommit(uint32_t w, uint32_t h) {
|
2014-03-31 14:15:24 +03:00
|
|
|
return lua::overcommit_std_align + sizeof(framebuffer::color) * (size_t)w * h;
|
2014-03-31 12:42:33 +03:00
|
|
|
}
|
2013-02-28 04:03:01 +02:00
|
|
|
~lua_dbitmap();
|
2012-03-03 11:27:26 +02:00
|
|
|
size_t width;
|
|
|
|
size_t height;
|
2014-03-31 12:42:33 +03:00
|
|
|
framebuffer::color* pixels;
|
2013-10-28 21:15:08 +02:00
|
|
|
std::vector<char> save_png() const;
|
2013-09-27 10:36:19 +03:00
|
|
|
std::string print();
|
2014-01-26 17:42:22 +02:00
|
|
|
static int create(lua::state& L, lua::parameters& P);
|
2014-04-03 00:59:05 +03:00
|
|
|
template<bool outside, bool clip> int draw(lua::state& L, lua::parameters& P);
|
2014-01-28 14:38:25 +02:00
|
|
|
int pset(lua::state& L, lua::parameters& P);
|
|
|
|
int pget(lua::state& L, lua::parameters& P);
|
|
|
|
int size(lua::state& L, lua::parameters& P);
|
|
|
|
int hash(lua::state& L, lua::parameters& P);
|
|
|
|
template<bool scaled, bool porterduff> int blit(lua::state& L, lua::parameters& P);
|
|
|
|
int save_png(lua::state& L, lua::parameters& P);
|
|
|
|
int adjust_transparency(lua::state& L, lua::parameters& P);
|
|
|
|
int _save_png(lua::state& L, lua::parameters& P, bool is_method);
|
2012-03-03 11:27:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct lua_loaded_bitmap
|
|
|
|
{
|
|
|
|
size_t w;
|
|
|
|
size_t h;
|
|
|
|
bool d;
|
|
|
|
std::vector<int64_t> bitmap;
|
|
|
|
std::vector<int64_t> palette;
|
2013-07-17 22:28:00 +03:00
|
|
|
static struct lua_loaded_bitmap load(std::istream& stream);
|
2012-03-03 11:27:26 +02:00
|
|
|
static struct lua_loaded_bitmap load(const std::string& name);
|
2014-01-26 17:42:22 +02:00
|
|
|
template<bool png> static int load(lua::state& L, lua::parameters& P);
|
|
|
|
template<bool png> static int load_str(lua::state& L, lua::parameters& P);
|
2012-03-03 11:27:26 +02:00
|
|
|
};
|
|
|
|
|
2015-06-23 14:32:59 +03:00
|
|
|
template<bool T> class lua_bitmap_holder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
lua_bitmap_holder(lua_bitmap& _b, lua_palette& _p) : b(_b), p(_p) {};
|
|
|
|
size_t stride() { return b.width; }
|
|
|
|
void lock()
|
|
|
|
{
|
|
|
|
p.palette_mutex.lock();
|
|
|
|
palette = p.colors;
|
|
|
|
pallim = p.color_count;
|
|
|
|
}
|
|
|
|
void unlock()
|
|
|
|
{
|
|
|
|
p.palette_mutex.unlock();
|
|
|
|
}
|
|
|
|
void draw(size_t bmpidx, typename framebuffer::fb<T>::element_t& target)
|
|
|
|
{
|
|
|
|
uint16_t i = b.pixels[bmpidx];
|
|
|
|
if(i < pallim)
|
|
|
|
palette[i].apply(target);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
lua_bitmap& b;
|
|
|
|
lua_palette& p;
|
|
|
|
framebuffer::color* palette;
|
|
|
|
size_t pallim;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<bool T> class lua_dbitmap_holder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
lua_dbitmap_holder(lua_dbitmap& _d) : d(_d) {};
|
|
|
|
size_t stride() { return d.width; }
|
|
|
|
void lock() {}
|
|
|
|
void unlock() {}
|
|
|
|
void draw(size_t bmpidx, typename framebuffer::fb<T>::element_t& target)
|
|
|
|
{
|
|
|
|
d.pixels[bmpidx].apply(target);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
lua_dbitmap& d;
|
|
|
|
};
|
|
|
|
|
2012-03-03 11:27:26 +02:00
|
|
|
|
2015-06-23 14:46:14 +03:00
|
|
|
template<bool T, class B> void lua_bitmap_composite(struct framebuffer::fb<T>& scr, int32_t xp,
|
|
|
|
int32_t yp, const range& X, const range& Y, const range& sX, const range& sY, bool outside, B bmp) throw()
|
|
|
|
{
|
|
|
|
if(!X.size() || !Y.size()) return;
|
|
|
|
size_t stride = bmp.stride();
|
|
|
|
bmp.lock();
|
|
|
|
|
|
|
|
for(uint32_t r = Y.low(); r != Y.high(); r++) {
|
|
|
|
typename framebuffer::fb<T>::element_t* rptr = scr.rowptr(yp + r);
|
|
|
|
size_t eptr = xp + X.low();
|
|
|
|
uint32_t xmin = X.low();
|
|
|
|
bool cut = outside && sY.in(r);
|
|
|
|
if(cut && sX.in(xmin)) {
|
|
|
|
xmin = sX.high();
|
|
|
|
eptr += (sX.high() - X.low());
|
|
|
|
}
|
|
|
|
for(uint32_t c = xmin; c < X.high(); c++, eptr++) {
|
|
|
|
if(__builtin_expect(cut && c == sX.low(), 0)) {
|
|
|
|
c += sX.size();
|
|
|
|
eptr += sX.size();
|
|
|
|
}
|
|
|
|
bmp.draw(r * stride + c, rptr[eptr]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bmp.unlock();
|
|
|
|
}
|
|
|
|
|
2012-03-03 11:27:26 +02:00
|
|
|
#endif
|