1 #include "lua/internal.hpp"
2 #include "core/framebuffer.hpp"
3 #include "core/instance.hpp"
4 #include "core/messages.hpp"
5 #include "core/misc.hpp"
6 #include "library/lua-framebuffer.hpp"
7 #include "library/minmax.hpp"
8 #include "library/png.hpp"
9 #include "library/sha256.hpp"
10 #include "library/serialization.hpp"
11 #include "library/string.hpp"
12 #include "library/zip.hpp"
13 #include "lua/bitmap.hpp"
14 #include "library/threads.hpp"
18 std::vector
<char> lua_dbitmap::save_png() const
23 img
.has_palette
= false;
24 img
.has_alpha
= false;
25 img
.data
.resize(width
* height
);
26 for(size_t i
= 0; i
< width
* height
; i
++) {
27 const framebuffer::color
& c
= pixels
[i
];
30 img
.data
[i
] = c
.orig
+ ((uint32_t)(c
.origa
- (c
.origa
>> 7) + (c
.origa
>> 8)) << 24);
32 std::ostringstream tmp1
;
34 std::string tmp2
= tmp1
.str();
35 return std::vector
<char>(tmp2
.begin(), tmp2
.end());
38 std::vector
<char> lua_bitmap::save_png(const lua_palette
& pal
) const
43 img
.has_palette
= true;
44 img
.has_alpha
= false;
45 img
.data
.resize(width
* height
);
46 img
.palette
.resize(pal
.color_count
);
47 for(size_t i
= 0; i
< width
* height
; i
++) {
48 img
.data
[i
] = pixels
[i
];
50 for(size_t i
= 0; i
< pal
.color_count
; i
++) {
51 const framebuffer::color
& c
= pal
.colors
[i
];
54 img
.palette
[i
] = c
.orig
+ ((uint32_t)(c
.origa
- (c
.origa
>> 7) + (c
.origa
>> 8)) << 24);
56 std::ostringstream tmp1
;
58 std::string tmp2
= tmp1
.str();
59 return std::vector
<char>(tmp2
.begin(), tmp2
.end());
64 const char* CONST_base64chars
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
66 struct render_object_bitmap
: public framebuffer::object
68 render_object_bitmap(int32_t _x
, int32_t _y
, lua::objpin
<lua_bitmap
>& _bitmap
,
69 lua::objpin
<lua_palette
>& _palette
, int32_t _x0
, int32_t _y0
, uint32_t _dw
, uint32_t _dh
,
70 bool _outside
) throw()
83 render_object_bitmap(int32_t _x
, int32_t _y
, lua::objpin
<lua_dbitmap
>& _bitmap
, int32_t _x0
,
84 int32_t _y0
, uint32_t _dw
, uint32_t _dh
, bool _outside
) throw()
96 ~render_object_bitmap() throw()
100 bool kill_request(void* obj
) throw()
102 return kill_request_ifeq(p
.object(), obj
) ||
103 kill_request_ifeq(b
.object(), obj
) ||
104 kill_request_ifeq(b2
.object(), obj
);
107 template<bool T
> void composite_op(struct framebuffer::fb
<T
>& scr
) throw()
109 uint32_t oX
= x
+ scr
.get_origin_x() - x0
;
110 uint32_t oY
= y
+ scr
.get_origin_y() - y0
;
120 range bX
= ((range::make_w(scr
.get_width()) - oX
) & range::make_w(w
) &
121 range::make_s(x0
, dw
));
122 range bY
= ((range::make_w(scr
.get_height()) - oY
) & range::make_w(h
) &
123 range::make_s(y0
, dh
));
124 range sX
= range::make_s(-x
+ x0
, scr
.get_last_blit_width());
125 range sY
= range::make_s(-y
+ y0
, scr
.get_last_blit_height());
128 lua_bitmap_composite(scr
, oX
, oY
, bX
, bY
, sX
, sY
, outside
,
129 lua_bitmap_holder
<T
>(*b
, *p
));
131 lua_bitmap_composite(scr
, oX
, oY
, bX
, bY
, sX
, sY
, outside
,
132 lua_dbitmap_holder
<T
>(*b2
));
134 void operator()(struct framebuffer::fb
<false>& x
) throw() { composite_op(x
); }
135 void operator()(struct framebuffer::fb
<true>& x
) throw() { composite_op(x
); }
136 void clone(framebuffer::queue
& q
) const throw(std::bad_alloc
) { q
.clone_helper(this); }
140 lua::objpin
<lua_bitmap
> b
;
141 lua::objpin
<lua_dbitmap
> b2
;
142 lua::objpin
<lua_palette
> p
;
150 struct operand_dbitmap
152 typedef framebuffer::color pixel_t
;
153 typedef framebuffer::color rpixel_t
;
154 operand_dbitmap(lua_dbitmap
& _bitmap
)
155 : bitmap(_bitmap
), _transparent(-1)
157 pixels
= bitmap
.pixels
;
159 size_t get_width() { return bitmap
.width
; }
160 size_t get_height() { return bitmap
.height
; }
161 const rpixel_t
& read(size_t idx
) { return pixels
[idx
]; }
162 const pixel_t
& lookup(const rpixel_t
& p
) { return p
; }
163 void write(size_t idx
, const pixel_t
& v
) { pixels
[idx
] = v
; }
164 bool is_opaque(const rpixel_t
& p
) { return p
.origa
> 0; }
165 const pixel_t
& transparent() { return _transparent
; }
169 framebuffer::color _transparent
;
172 struct operand_bitmap
174 typedef uint16_t pixel_t
;
175 typedef uint16_t rpixel_t
;
176 operand_bitmap(lua_bitmap
& _bitmap
)
179 pixels
= bitmap
.pixels
;
181 size_t get_width() { return bitmap
.width
; }
182 size_t get_height() { return bitmap
.height
; }
183 const rpixel_t
& read(size_t idx
) { return pixels
[idx
]; }
184 const pixel_t
& lookup(const rpixel_t
& p
) { return p
; }
185 void write(size_t idx
, const pixel_t
& v
) { pixels
[idx
] = v
; }
186 bool is_opaque(const rpixel_t
& p
) { return p
> 0; }
187 pixel_t
transparent() { return 0; }
193 struct operand_bitmap_pal
195 typedef framebuffer::color pixel_t
;
196 typedef uint16_t rpixel_t
;
197 operand_bitmap_pal(lua_bitmap
& _bitmap
, lua_palette
& _palette
)
198 : bitmap(_bitmap
), palette(_palette
), _transparent(-1)
200 pixels
= bitmap
.pixels
;
201 limit
= palette
.color_count
;
202 pal
= palette
.colors
;
204 size_t get_width() { return bitmap
.width
; }
205 size_t get_height() { return bitmap
.height
; }
206 const rpixel_t
& read(size_t idx
) { return pixels
[idx
]; }
207 const pixel_t
& lookup(const rpixel_t
& p
) { return *((p
< limit
) ? pal
+ p
: &_transparent
); }
208 bool is_opaque(const rpixel_t
& p
) { return p
> 0; }
209 const pixel_t
& transparent() { return _transparent
; }
212 lua_palette
& palette
;
214 framebuffer::color
* pal
;
216 framebuffer::color _transparent
;
221 bool iskey(uint16_t& c
) const { return false; }
222 bool iskey(framebuffer::color
& c
) const { return false; }
225 struct colorkey_direct
227 colorkey_direct(uint64_t _ck
)
229 framebuffer::color
c(_ck
);
233 bool iskey(framebuffer::color
& c
) const { return (c
.orig
== ck
&& c
.origa
== cka
); }
238 struct colorkey_palette
240 colorkey_palette(uint64_t _ck
) { ck
= _ck
; }
241 bool iskey(uint16_t& c
) const { return (c
== ck
); }
245 template<class _src
, class _dest
, class colorkey
> struct srcdest
247 srcdest(_dest Xdest
, _src Xsrc
, const colorkey
& _ckey
)
248 : dest(Xdest
), src(Xsrc
), ckey(_ckey
)
250 swidth
= src
.get_width();
251 sheight
= src
.get_height();
252 dwidth
= dest
.get_width();
253 dheight
= dest
.get_height();
255 void copy(size_t didx
, size_t sidx
)
257 typename
_src::rpixel_t c
= src
.read(sidx
);
259 dest
.write(didx
, src
.lookup(c
));
261 size_t swidth
, sheight
, dwidth
, dheight
;
268 template<class _src
, class _dest
, class colorkey
> srcdest
<_src
, _dest
, colorkey
> mk_srcdest(_dest dest
,
269 _src src
, const colorkey
& ckey
)
271 return srcdest
<_src
, _dest
, colorkey
>(dest
, src
, ckey
);
274 struct srcdest_priority
276 srcdest_priority(lua_bitmap
& dest
, lua_bitmap
& src
)
278 darray
= dest
.pixels
;
281 sheight
= src
.height
;
283 dheight
= dest
.height
;
285 void copy(size_t didx
, size_t sidx
)
287 uint16_t c
= sarray
[sidx
];
291 size_t swidth
, sheight
, dwidth
, dheight
;
313 porterduff_oper
get_pd_oper(const std::string
& oper
)
315 if(oper
== "Src") return PD_SRC
;
316 if(oper
== "Atop") return PD_ATOP
;
317 if(oper
== "Over") return PD_OVER
;
318 if(oper
== "In") return PD_IN
;
319 if(oper
== "Out") return PD_OUT
;
320 if(oper
== "Dest") return PD_DEST
;
321 if(oper
== "DestAtop") return PD_DEST_ATOP
;
322 if(oper
== "DestOver") return PD_DEST_OVER
;
323 if(oper
== "DestIn") return PD_DEST_IN
;
324 if(oper
== "DestOut") return PD_DEST_OUT
;
325 if(oper
== "Clear") return PD_CLEAR
;
326 if(oper
== "Xor") return PD_XOR
;
327 (stringfmt() << "Bad Porter-Duff operator '" << oper
<< "'").throwex();
328 return PD_SRC
; //NOTREACHED
331 template<porterduff_oper oper
, class _src
, class _dest
> struct srcdest_porterduff
333 srcdest_porterduff(_dest Xdest
, _src Xsrc
)
334 : dest(Xdest
), src(Xsrc
)
336 swidth
= src
.get_width();
337 sheight
= src
.get_height();
338 dwidth
= dest
.get_width();
339 dheight
= dest
.get_height();
341 void copy(size_t didx
, size_t sidx
)
343 typename
_dest::rpixel_t vd
= dest
.read(didx
);
344 typename
_src::rpixel_t vs
= src
.read(sidx
);
345 bool od
= dest
.is_opaque(vd
);
346 bool os
= src
.is_opaque(vs
);
347 typename
_dest::pixel_t ld
= dest
.lookup(vd
);
348 typename
_src::pixel_t ls
= src
.lookup(vs
);
349 typename
_dest::pixel_t t
= dest
.transparent();
350 typename
_dest::pixel_t r
;
352 case PD_SRC
: r
= ls
; break;
353 case PD_ATOP
: r
= od
? (os
? ls
: ld
) : t
; break;
354 case PD_OVER
: r
= os
? ls
: ld
; break;
355 case PD_IN
: r
= (od
& os
) ? ls
: t
; break;
356 case PD_OUT
: r
= (!od
&& os
) ? ls
: t
; break;
357 case PD_DEST
: r
= ld
; break;
358 case PD_DEST_ATOP
: r
= os
? (od
? ld
: ls
) : t
; break;
359 case PD_DEST_OVER
: r
= od
? ld
: ls
; break;
360 case PD_DEST_IN
: r
= (od
& os
) ? ld
: t
; break;
361 case PD_DEST_OUT
: r
= (od
& !os
) ? ld
: t
; break;
362 case PD_CLEAR
: r
= t
; break;
363 case PD_XOR
: r
= od
? (os
? t
: ld
) : ls
; break;
367 size_t swidth
, sheight
, dwidth
, dheight
;
373 template<porterduff_oper oper
, class _src
, class _dest
> srcdest_porterduff
<oper
, _src
, _dest
>
374 mk_porterduff(_dest dest
, _src src
)
376 return srcdest_porterduff
<oper
, _src
, _dest
>(dest
, src
);
379 template<class srcdest
>
380 void xblit_copy(srcdest sd
, uint32_t dx
, uint32_t dy
, uint32_t sx
, uint32_t sy
, uint32_t w
, uint32_t h
)
382 while((dx
+ w
> sd
.dwidth
|| sx
+ w
> sd
.swidth
) && w
> 0)
384 while((dy
+ h
> sd
.dheight
|| sy
+ h
> sd
.sheight
) && h
> 0)
386 if(dx
+ w
< w
|| dy
+ h
< h
) return; //Don't do overflowing blits.
387 if(sx
+ w
< w
|| sy
+ h
< h
) return; //Don't do overflowing blits.
388 size_t sidx
= sy
* sd
.swidth
+ sx
;
389 size_t didx
= dy
* sd
.dwidth
+ dx
;
390 size_t srskip
= sd
.swidth
- w
;
391 size_t drskip
= sd
.dwidth
- w
;
392 for(uint32_t j
= 0; j
< h
; j
++) {
393 for(uint32_t i
= 0; i
< w
; i
++) {
403 template<class srcdest
>
404 void xblit_scaled(srcdest sd
, uint32_t dx
, uint32_t dy
, uint32_t sx
, uint32_t sy
, uint32_t w
, uint32_t h
,
405 uint32_t hscl
, uint32_t vscl
)
407 if(!hscl
|| !vscl
) return;
408 w
= max(static_cast<uint32_t>(sd
.dwidth
/ hscl
), w
);
409 h
= max(static_cast<uint32_t>(sd
.dheight
/ vscl
), h
);
410 while((dx
+ hscl
* w
> sd
.dwidth
|| sx
+ w
> sd
.swidth
) && w
> 0)
412 while((dy
+ vscl
* h
> sd
.dheight
|| sy
+ h
> sd
.sheight
) && h
> 0)
414 if(dx
+ hscl
* w
< dx
|| dy
+ vscl
* h
< dy
) return; //Don't do overflowing blits.
415 if(sx
+ w
< w
|| sy
+ h
< h
) return; //Don't do overflowing blits.
416 size_t sidx
= sy
* sd
.swidth
+ sx
;
417 size_t didx
= dy
* sd
.dwidth
+ dx
;
418 size_t drskip
= sd
.dwidth
- hscl
* w
;
419 uint32_t _w
= hscl
* w
;
420 for(uint32_t j
= 0; j
< vscl
* h
; j
++) {
421 uint32_t _sidx
= sidx
;
422 for(uint32_t i
= 0; i
< _w
; i
+= hscl
) {
423 for(uint32_t k
= 0; k
< hscl
; k
++)
424 sd
.copy(didx
+ k
, _sidx
);
428 if((j
% vscl
) == vscl
- 1)
434 template<bool scaled
, class srcdest
>
435 inline void xblit(srcdest sd
, uint32_t dx
, uint32_t dy
, uint32_t sx
, uint32_t sy
, uint32_t w
, uint32_t h
,
436 uint32_t hscl
, uint32_t vscl
)
439 xblit_scaled(sd
, dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
);
441 xblit_copy(sd
, dx
, dy
, sx
, sy
, w
, h
);
444 template<bool scaled
, class src
, class dest
>
445 inline void xblit_pal(dest _dest
, src _src
, uint64_t ck
, uint32_t dx
, uint32_t dy
, uint32_t sx
,
446 uint32_t sy
, uint32_t w
, uint32_t h
, uint32_t hscl
, uint32_t vscl
)
449 xblit
<scaled
>(mk_srcdest(_dest
, _src
, colorkey_none()), dx
, dy
, sx
, sy
, w
, h
,
452 xblit
<scaled
>(mk_srcdest(_dest
, _src
, colorkey_palette(ck
)), dx
, dy
, sx
, sy
, w
, h
,
456 template<bool scaled
, class src
, class dest
>
457 inline void xblit_dir(dest _dest
, src _src
, uint64_t ck
, uint32_t dx
, uint32_t dy
, uint32_t sx
,
458 uint32_t sy
, uint32_t w
, uint32_t h
, uint32_t hscl
, uint32_t vscl
)
460 if(ck
== 0x100000000ULL
)
461 xblit
<scaled
>(mk_srcdest(_dest
, _src
, colorkey_none()), dx
, dy
, sx
, sy
, w
, h
,
464 xblit
<scaled
>(mk_srcdest(_dest
, _src
, colorkey_direct(ck
)), dx
, dy
, sx
, sy
, w
, h
,
468 template<bool scaled
, porterduff_oper oper
, class src
, class dest
>
469 inline void xblit_pduff2(dest _dest
, src _src
, uint32_t dx
, uint32_t dy
, uint32_t sx
,
470 uint32_t sy
, uint32_t w
, uint32_t h
, uint32_t hscl
, uint32_t vscl
)
472 xblit
<scaled
>(mk_porterduff
<oper
>(_dest
, _src
), dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
);
475 template<bool scaled
, class src
, class dest
>
476 inline void xblit_pduff(dest _dest
, src _src
, uint32_t dx
, uint32_t dy
, uint32_t sx
,
477 uint32_t sy
, uint32_t w
, uint32_t h
, uint32_t hscl
, uint32_t vscl
, porterduff_oper oper
)
481 xblit_pduff2
<scaled
, PD_ATOP
>(_dest
, _src
, dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
);
484 xblit_pduff2
<scaled
, PD_CLEAR
>(_dest
, _src
, dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
);
487 xblit_pduff2
<scaled
, PD_DEST
>(_dest
, _src
, dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
);
490 xblit_pduff2
<scaled
, PD_DEST_ATOP
>(_dest
, _src
, dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
);
493 xblit_pduff2
<scaled
, PD_DEST_IN
>(_dest
, _src
, dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
);
496 xblit_pduff2
<scaled
, PD_DEST_OUT
>(_dest
, _src
, dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
);
499 xblit_pduff2
<scaled
, PD_DEST_OVER
>(_dest
, _src
, dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
);
502 xblit_pduff2
<scaled
, PD_IN
>(_dest
, _src
, dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
);
505 xblit_pduff2
<scaled
, PD_OUT
>(_dest
, _src
, dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
);
508 xblit_pduff2
<scaled
, PD_OVER
>(_dest
, _src
, dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
);
511 xblit_pduff2
<scaled
, PD_SRC
>(_dest
, _src
, dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
);
514 xblit_pduff2
<scaled
, PD_XOR
>(_dest
, _src
, dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
);
519 int bitmap_load_fn(lua::state
& L
, std::function
<lua_loaded_bitmap()> src
)
523 lua_dbitmap
* b
= lua::_class
<lua_dbitmap
>::create(L
, bitmap
.w
, bitmap
.h
);
524 for(size_t i
= 0; i
< bitmap
.w
* bitmap
.h
; i
++)
525 b
->pixels
[i
] = framebuffer::color(bitmap
.bitmap
[i
]);
528 lua_bitmap
* b
= lua::_class
<lua_bitmap
>::create(L
, bitmap
.w
, bitmap
.h
);
529 lua_palette
* p
= lua::_class
<lua_palette
>::create(L
);
530 for(size_t i
= 0; i
< bitmap
.w
* bitmap
.h
; i
++)
531 b
->pixels
[i
] = bitmap
.bitmap
[i
];
532 p
->adjust_palette_size(bitmap
.palette
.size());
533 for(size_t i
= 0; i
< bitmap
.palette
.size(); i
++)
534 p
->colors
[i
] = framebuffer::color(bitmap
.palette
[i
]);
539 template<typename pixel
>
540 inline void _sample_texture(pixel
* dest
, uint32_t dwidth
, uint32_t dheight
, pixel
* source
, uint32_t swidth
,
541 uint32_t sheight
, int32_t xx
, int32_t xy
, int32_t x0
, int32_t yx
, int32_t yy
, int32_t y0
, uint32_t s
,
542 bool wrap
, pixel transparent
)
546 for(int32_t i
= 0; i
< (int32_t)dheight
; i
++) {
547 int64_t ssx
= (int64_t)xy
* i
+ (int64_t)x0
;
548 int64_t ssy
= (int64_t)yy
* i
+ (int64_t)y0
;
549 for(int32_t j
= 0; j
< (int32_t)dwidth
; j
++) {
550 //Compute whole and fractional parts.
551 int64_t isx
= ssx
/ (int32_t)s
;
552 int32_t fsx
= ssx
% (int32_t)s
;
553 int64_t isy
= ssy
/ (int32_t)s
;
554 int32_t fsy
= ssy
% (int32_t)s
;
555 //Make the fractional parts always positive.
556 if(fsx
< 0) { fsx
+= s
; isx
--; }
557 if(fsy
< 0) { fsy
+= s
; isy
--; }
558 //Round the whole parts.
562 pixel p
= transparent
;
565 if(isx
< 0) isx
+= swidth
;
567 if(isy
< 0) isy
+= sheight
;
568 p
= source
[isy
* swidth
+ isx
];
569 } else if(isx
>= 0 && isx
< swidth
&& isy
>= 0 && isy
< sheight
) {
570 p
= source
[isy
* swidth
+ isx
];
573 //Update values for next pixel.
581 inline int64_t mangle_color(uint32_t c
)
586 return ((256 - (c
>> 24) - (c
>> 31)) << 24) | (c
& 0xFFFFFF);
589 int base64val(char ch
)
591 if(ch
>= 'A' && ch
<= 'Z')
593 if(ch
>= 'a' && ch
<= 'z')
595 if(ch
>= '0' && ch
<= '9')
601 if(ch
== ' ' || ch
== '\t' || ch
== '\r' || ch
== '\n')
608 std::string
base64_encode(const std::string
& str
)
610 std::ostringstream x
;
614 mem
= (mem
<< 8) + (unsigned char)i
;
616 uint8_t c1
= (mem
>> 18) & 0x3F;
617 uint8_t c2
= (mem
>> 12) & 0x3F;
618 uint8_t c3
= (mem
>> 6) & 0x3F;
619 uint8_t c4
= mem
& 0x3F;
620 x
<< CONST_base64chars
[c1
];
621 x
<< CONST_base64chars
[c2
];
622 x
<< CONST_base64chars
[c3
];
623 x
<< CONST_base64chars
[c4
];
629 uint8_t c1
= (mem
>> 10) & 0x3F;
630 uint8_t c2
= (mem
>> 4) & 0x3F;
631 uint8_t c3
= (mem
<< 2) & 0x3F;
632 x
<< CONST_base64chars
[c1
];
633 x
<< CONST_base64chars
[c2
];
634 x
<< CONST_base64chars
[c3
];
638 uint8_t c1
= (mem
>> 2) & 0x3F;
639 uint8_t c2
= (mem
<< 4) & 0x3F;
640 x
<< CONST_base64chars
[c1
];
641 x
<< CONST_base64chars
[c2
];
647 std::string
base64_decode(const std::string
& str
)
651 uint32_t memsize
= 1;
653 std::ostringstream x
;
655 int v
= base64val(i
);
658 posmod
= (posmod
+ 1) & 3;
659 if(v
== -2 && (posmod
== 1 || posmod
== 2))
660 throw std::runtime_error("Invalid Base64");
666 throw std::runtime_error("Invalid Base64");
667 memory
= memory
* 64 + v
;
668 memsize
= memsize
* 64;
671 x
<< static_cast<uint8_t>(memory
/ memsize
);
679 int bitmap_load_png_fn(lua::state
& L
, T
& src
)
681 png::decoder
img(src
);
682 if(img
.has_palette
) {
683 lua_bitmap
* b
= lua::_class
<lua_bitmap
>::create(L
, img
.width
, img
.height
);
684 lua_palette
* p
= lua::_class
<lua_palette
>::create(L
);
685 for(size_t i
= 0; i
< img
.width
* img
.height
; i
++)
686 b
->pixels
[i
] = img
.data
[i
];
687 p
->adjust_palette_size(img
.palette
.size());
688 for(size_t i
= 0; i
< img
.palette
.size(); i
++)
689 p
->colors
[i
] = framebuffer::color(mangle_color(img
.palette
[i
]));
692 lua_dbitmap
* b
= lua::_class
<lua_dbitmap
>::create(L
, img
.width
, img
.height
);
693 for(size_t i
= 0; i
< img
.width
* img
.height
; i
++)
694 b
->pixels
[i
] = framebuffer::color(mangle_color(img
.data
[i
]));
699 int bitmap_palette_fn(lua::state
& L
, std::istream
& s
)
701 lua_palette
* p
= lua::_class
<lua_palette
>::create(L
);
704 std::getline(s
, line
);
707 if(!regex_match("[ \t]*(#.*)?", line
)) {
709 } else if(r
= regex("[ \t]*([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]*", line
)) {
710 int64_t cr
, cg
, cb
, ca
;
711 cr
= parse_value
<uint8_t>(r
[1]);
712 cg
= parse_value
<uint8_t>(r
[2]);
713 cb
= parse_value
<uint8_t>(r
[3]);
714 ca
= 256 - parse_value
<uint16_t>(r
[4]);
716 p
->push_back(framebuffer::color(-1));
718 p
->push_back(framebuffer::color((ca
<< 24) | (cr
<< 16)
720 } else if(r
= regex("[ \t]*([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]*", line
)) {
722 cr
= parse_value
<uint8_t>(r
[1]);
723 cg
= parse_value
<uint8_t>(r
[2]);
724 cb
= parse_value
<uint8_t>(r
[3]);
725 p
->push_back(framebuffer::color((cr
<< 16) | (cg
<< 8) | cb
));
726 } else if(r
= regex("[ \t]*([^ \t]|[^ \t].*[^ \t])[ \t]*", line
)) {
727 p
->push_back(framebuffer::color(r
[1]));
729 throw std::runtime_error("Invalid line format (" + line
+ ")");
734 inline framebuffer::color
tadjust(framebuffer::color c
, uint16_t adj
)
736 uint32_t rgb
= c
.orig
;
737 uint32_t a
= c
.origa
;
742 return framebuffer::color(-1);
744 return framebuffer::color(rgb
| ((uint32_t)(256 - a
) << 24));
747 lua::_class
<lua_loaded_bitmap
> LUA_class_loaded_bitmap(lua_class_gui
, "IMAGELOADER", {
748 {"load", lua_loaded_bitmap::load
<false>},
749 {"load_str", lua_loaded_bitmap::load_str
<false>},
750 {"load_png", lua_loaded_bitmap::load
<true>},
751 {"load_png_str", lua_loaded_bitmap::load_str
<true>},
754 lua::_class
<lua_palette
> LUA_class_palette(lua_class_gui
, "PALETTE", {
755 {"new", lua_palette::create
},
756 {"load", lua_palette::load
},
757 {"load_str", lua_palette::load_str
},
759 {"set", &lua_palette::set
},
760 {"get", &lua_palette::get
},
761 {"hash", &lua_palette::hash
},
762 {"debug", &lua_palette::debug
},
763 {"adjust_transparency", &lua_palette::adjust_transparency
},
764 }, &lua_palette::print
);
766 lua::_class
<lua_bitmap
> LUA_class_bitmap(lua_class_gui
, "BITMAP", {
767 {"new", lua_bitmap::create
},
769 {"draw", &lua_bitmap::draw
<false, false>},
770 {"draw_clip", &lua_bitmap::draw
<false, true>},
771 {"draw_outside", &lua_bitmap::draw
<true, false>},
772 {"draw_clip_outside", &lua_bitmap::draw
<true, true>},
773 {"pset", &lua_bitmap::pset
},
774 {"pget", &lua_bitmap::pget
},
775 {"size", &lua_bitmap::size
},
776 {"hash", &lua_bitmap::hash
},
777 {"blit", &lua_bitmap::blit
<false, false>},
778 {"blit_priority", &lua_bitmap::blit_priority
<false>},
779 {"blit_scaled", &lua_bitmap::blit
<true, false>},
780 {"blit_scaled_priority", &lua_bitmap::blit_priority
<true>},
781 {"blit_porterduff", &lua_bitmap::blit
<false, true>},
782 {"blit_scaled_porterduff", &lua_bitmap::blit
<true, true>},
783 {"save_png", &lua_bitmap::save_png
},
784 {"sample_texture", &lua_bitmap::sample_texture
},
785 }, &lua_bitmap::print
);
787 lua::_class
<lua_dbitmap
> LUA_class_dbitmap(lua_class_gui
, "DBITMAP", {
788 {"new", lua_dbitmap::create
},
790 {"draw", &lua_dbitmap::draw
<false, false>},
791 {"draw_clip", &lua_dbitmap::draw
<false, true>},
792 {"draw_outside", &lua_dbitmap::draw
<true, false>},
793 {"draw_clip_outside", &lua_dbitmap::draw
<true, true>},
794 {"pset", &lua_dbitmap::pset
},
795 {"pget", &lua_dbitmap::pget
},
796 {"size", &lua_dbitmap::size
},
797 {"hash", &lua_dbitmap::hash
},
798 {"blit", &lua_dbitmap::blit
<false, false>},
799 {"blit_scaled", &lua_dbitmap::blit
<true, false>},
800 {"blit_porterduff", &lua_dbitmap::blit
<false, true>},
801 {"blit_scaled_porterduff", &lua_dbitmap::blit
<true, true>},
802 {"save_png", &lua_dbitmap::save_png
},
803 {"adjust_transparency", &lua_dbitmap::adjust_transparency
},
804 {"sample_texture", &lua_dbitmap::sample_texture
},
805 }, &lua_dbitmap::print
);
809 lua_palette::lua_palette(lua::state
& L
)
812 scolors
= colors
= lua::align_overcommit
<lua_palette
, framebuffer::color
>(this);
813 for(unsigned i
= 0; i
< reserved_colors
; i
++)
814 scolors
[i
] = framebuffer::color(-1);
817 lua_palette::~lua_palette()
819 CORE().fbuf
->render_kill_request(this);
822 std::string
lua_palette::print()
824 size_t s
= color_count
;
825 return (stringfmt() << s
<< " " << ((s
!= 1) ? "colors" : "color")).str();
828 int lua_palette::create(lua::state
& L
, lua::parameters
& P
)
830 lua::_class
<lua_palette
>::create(L
);
834 int lua_palette::load(lua::state
& L
, lua::parameters
& P
)
836 std::string name
, name2
;
838 P(name
, P
.optional(name2
, ""));
840 std::istream
& s
= zip::openrel(name
, name2
);
842 int r
= bitmap_palette_fn(L
, s
);
851 int lua_palette::load_str(lua::state
& L
, lua::parameters
& P
)
857 std::istringstream
s(content
);
858 return bitmap_palette_fn(L
, s
);
861 int lua_palette::set(lua::state
& L
, lua::parameters
& P
)
863 framebuffer::color nc
;
866 P(P
.skipped(), c
, nc
);
868 if(this->color_count
<= c
) {
869 this->adjust_palette_size(static_cast<uint32_t>(c
) + 1);
871 this->colors
[c
] = nc
;
875 int lua_palette::get(lua::state
& L
, lua::parameters
& P
)
877 framebuffer::color nc
;
883 if(this->color_count
<= c
) {
886 nc
= this->colors
[c
];
887 uint32_t rgb
= nc
.orig
;
888 uint32_t a
= nc
.origa
;
892 _nc
= ((256 - a
) << 24) | rgb
;
897 int lua_palette::hash(lua::state
& L
, lua::parameters
& P
)
900 const int buffersize
= 256;
902 char buf
[buffersize
];
903 unsigned realsize
= 0;
904 for(unsigned i
= 0; i
< this->color_count
; i
++)
905 if(this->colors
[i
].origa
) realsize
= i
+ 1;
906 for(unsigned i
= 0; i
< realsize
; i
++) {
907 if(bufferuse
+ 6 > buffersize
) {
908 h
.write(buf
, bufferuse
);
911 serialization::u32b(buf
+ bufferuse
+ 0, this->colors
[i
].orig
);
912 serialization::u16b(buf
+ bufferuse
+ 4, this->colors
[i
].origa
);
915 if(bufferuse
> 0) h
.write(buf
, bufferuse
);
916 L
.pushlstring(h
.read());
920 int lua_palette::debug(lua::state
& L
, lua::parameters
& P
)
922 for(size_t i
= 0; i
< color_count
; i
++)
923 messages
<< "Color #" << i
<< ": " << colors
[i
].orig
<< ":" << colors
[i
].origa
<< std::endl
;
927 int lua_palette::adjust_transparency(lua::state
& L
, lua::parameters
& P
)
931 P(P
.skipped(), tadj
);
933 for(size_t i
= 0; i
< color_count
; i
++)
934 colors
[i
] = tadjust(colors
[i
], tadj
);
938 void lua_palette::adjust_palette_size(size_t newsize
)
940 threads::alock
h(palette_mutex
);
941 if(newsize
> reserved_colors
) {
942 lcolors
.resize(newsize
);
943 if(color_count
<= reserved_colors
) {
944 for(unsigned i
= 0; i
< color_count
; i
++)
945 lcolors
[i
] = colors
[i
];
947 colors
= &lcolors
[0];
949 if(color_count
> reserved_colors
) {
950 for(unsigned i
= 0; i
< color_count
; i
++)
951 scolors
[i
] = lcolors
[i
];
955 color_count
= newsize
;
958 void lua_palette::push_back(const framebuffer::color
& cx
)
960 size_t c
= color_count
;
961 adjust_palette_size(c
+ 1);
966 lua_bitmap::lua_bitmap(lua::state
& L
, uint32_t w
, uint32_t h
)
968 if(h
> 0 && overcommit(w
, h
) / h
/ sizeof(uint16_t) < w
) throw std::bad_alloc();
971 pixels
= lua::align_overcommit
<lua_bitmap
, uint16_t>(this);
972 memset(pixels
, 0, 2 * width
* height
);
975 lua_bitmap::~lua_bitmap()
977 CORE().fbuf
->render_kill_request(this);
980 std::string
lua_bitmap::print()
982 return (stringfmt() << width
<< "*" << height
).str();
985 int lua_bitmap::create(lua::state
& L
, lua::parameters
& P
)
990 P(w
, h
, P
.optional(c
, 0));
992 lua_bitmap
* b
= lua::_class
<lua_bitmap
>::create(L
, w
, h
);
993 for(size_t i
= 0; i
< b
->width
* b
->height
; i
++)
998 template<bool outside
, bool clip
>
999 int lua_bitmap::draw(lua::state
& L
, lua::parameters
& P
)
1001 auto& core
= CORE();
1003 lua::objpin
<lua_bitmap
> b
;
1004 lua::objpin
<lua_palette
> p
;
1006 if(!core
.lua2
->render_ctx
) return 0;
1010 int32_t x0
= 0, y0
= 0, dw
= b
->width
, dh
= b
->height
;
1011 if(clip
) P(x0
, y0
, dw
, dh
);
1013 core
.lua2
->render_ctx
->queue
->create_add
<render_object_bitmap
>(x
, y
, b
, p
, x0
, y0
, dw
, dh
, outside
);
1017 int lua_bitmap::pset(lua::state
& L
, lua::parameters
& P
)
1022 P(P
.skipped(), x
, y
, c
);
1024 if(x
>= this->width
|| y
>= this->height
)
1026 this->pixels
[y
* this->width
+ x
] = c
;
1030 int lua_bitmap::pget(lua::state
& L
, lua::parameters
& P
)
1034 P(P
.skipped(), x
, y
);
1036 if(x
>= this->width
|| y
>= this->height
)
1038 L
.pushnumber(this->pixels
[y
* this->width
+ x
]);
1042 int lua_bitmap::size(lua::state
& L
, lua::parameters
& P
)
1044 L
.pushnumber(this->width
);
1045 L
.pushnumber(this->height
);
1049 int lua_bitmap::hash(lua::state
& L
, lua::parameters
& P
)
1052 const int buffersize
= 256;
1054 char buf
[buffersize
];
1055 memset(buf
, 0, buffersize
);
1056 serialization::u64b(buf
+ 0, this->width
);
1057 serialization::u64b(buf
+ 8, this->height
);
1059 for(unsigned i
= 0; i
< this->width
* this->height
; i
++) {
1060 if(bufferuse
+ 2 > buffersize
) {
1061 h
.write(buf
, bufferuse
);
1064 serialization::u16b(buf
+ bufferuse
+ 0, this->pixels
[i
]);
1067 if(bufferuse
> 0) h
.write(buf
, bufferuse
);
1068 L
.pushlstring(h
.read());
1072 template<bool scaled
, bool porterduff
> int lua_bitmap::blit(lua::state
& L
, lua::parameters
& P
)
1074 uint32_t dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
;
1077 P(P
.skipped(), dx
, dy
, src_p
, sx
, sy
, w
, h
);
1079 P(hscl
, P
.optional2(vscl
, hscl
));
1082 porterduff_oper pd_oper
= get_pd_oper(P
.arg
<std::string
>());
1083 xblit_pduff
<scaled
>(operand_bitmap(*this), operand_bitmap(*src_p
), dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
,
1086 int64_t ck
= P
.arg_opt
<uint64_t>(65536);
1087 xblit_pal
<scaled
>(operand_bitmap(*this), operand_bitmap(*src_p
), ck
, dx
, dy
, sx
, sy
, w
, h
,
1093 template<bool scaled
> int lua_bitmap::blit_priority(lua::state
& L
, lua::parameters
& P
)
1095 uint32_t dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
;
1098 P(P
.skipped(), dx
, dy
, src_p
, sx
, sy
, w
, h
);
1100 P(hscl
, P
.optional2(vscl
, hscl
));
1102 xblit
<scaled
>(srcdest_priority(*this, *src_p
), dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
);
1106 int lua_bitmap::save_png(lua::state
& L
, lua::parameters
& P
)
1108 std::string name
, name2
;
1113 if((was_filename
= P
.is_string())) P(name
);
1114 if(P
.is_string()) P(name2
);
1117 auto buf
= this->save_png(*p
);
1119 std::string filename
= zip::resolverel(name
, name2
);
1120 std::ofstream
strm(filename
, std::ios::binary
);
1122 throw std::runtime_error("Can't open output file");
1123 strm
.write(&buf
[0], buf
.size());
1125 throw std::runtime_error("Can't write output file");
1128 std::ostringstream strm
;
1129 strm
.write(&buf
[0], buf
.size());
1130 L
.pushlstring(base64_encode(strm
.str()));
1135 int lua_bitmap::sample_texture(lua::state
& L
, lua::parameters
& P
)
1138 int32_t xx
, xy
, yx
, yy
, x0
, y0
;
1143 P(P
.skipped(), src_p
, xx
, xy
, x0
, yx
, yy
, y0
, s
, wrap
);
1145 _sample_texture(pixels
, width
, height
, src_p
->pixels
, src_p
->width
, src_p
->height
, xx
, xy
, x0
, yx
, yy
, y0
,
1151 lua_dbitmap::lua_dbitmap(lua::state
& L
, uint32_t w
, uint32_t h
)
1153 if(h
> 0 && overcommit(w
, h
) / h
/ sizeof(framebuffer::color
) < w
) throw std::bad_alloc();
1156 pixels
= lua::align_overcommit
<lua_dbitmap
, framebuffer::color
>(this);
1157 //Initialize the bitmap data.
1158 framebuffer::color
transparent(-1);
1159 for(size_t i
= 0; i
< (size_t)width
* height
; i
++)
1160 new(pixels
+ i
) framebuffer::color(transparent
);
1163 lua_dbitmap::~lua_dbitmap()
1165 CORE().fbuf
->render_kill_request(this);
1168 std::string
lua_dbitmap::print()
1170 return (stringfmt() << width
<< "*" << height
).str();
1173 int lua_dbitmap::create(lua::state
& L
, lua::parameters
& P
)
1176 framebuffer::color c
;
1178 P(w
, h
, P
.optional(c
, -1));
1180 lua_dbitmap
* b
= lua::_class
<lua_dbitmap
>::create(L
, w
, h
);
1181 for(size_t i
= 0; i
< b
->width
* b
->height
; i
++)
1186 template<bool outside
, bool clip
>
1187 int lua_dbitmap::draw(lua::state
& L
, lua::parameters
& P
)
1189 auto& core
= CORE();
1191 lua::objpin
<lua_dbitmap
> b
;
1193 if(!core
.lua2
->render_ctx
) return 0;
1197 int32_t x0
= 0, y0
= 0, dw
= b
->width
, dh
= b
->height
;
1198 if(clip
) P(x0
, y0
, dw
, dh
);
1200 core
.lua2
->render_ctx
->queue
->create_add
<render_object_bitmap
>(x
, y
, b
, x0
, y0
, dw
, dh
, outside
);
1204 int lua_dbitmap::pset(lua::state
& L
, lua::parameters
& P
)
1207 framebuffer::color c
;
1209 P(P
.skipped(), x
, y
, c
);
1211 if(x
>= this->width
|| y
>= this->height
)
1213 this->pixels
[y
* this->width
+ x
] = c
;
1217 int lua_dbitmap::pget(lua::state
& L
, lua::parameters
& P
)
1221 P(P
.skipped(), x
, y
);
1223 if(x
>= this->width
|| y
>= this->height
)
1225 L
.pushnumber((this->pixels
[y
* this->width
+ x
]).asnumber());
1229 int lua_dbitmap::size(lua::state
& L
, lua::parameters
& P
)
1231 L
.pushnumber(this->width
);
1232 L
.pushnumber(this->height
);
1236 int lua_dbitmap::hash(lua::state
& L
, lua::parameters
& P
)
1239 const int buffersize
= 256;
1241 char buf
[buffersize
];
1242 memset(buf
, 0, buffersize
);
1243 serialization::u64b(buf
+ 0, this->width
);
1244 serialization::u64b(buf
+ 4, this->height
);
1246 for(unsigned i
= 0; i
< this->width
* this->height
; i
++) {
1247 if(bufferuse
+ 6 > buffersize
) {
1248 h
.write(buf
, bufferuse
);
1251 serialization::u32b(buf
+ bufferuse
+ 0, this->pixels
[i
].orig
);
1252 serialization::u16b(buf
+ bufferuse
+ 4, this->pixels
[i
].origa
);
1255 if(bufferuse
> 0) h
.write(buf
, bufferuse
);
1256 L
.pushlstring(h
.read());
1260 template<bool scaled
, bool porterduff
> int lua_dbitmap::blit(lua::state
& L
, lua::parameters
& P
)
1262 uint32_t dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
;
1264 P(P
.skipped(), dx
, dy
);
1266 //DBitmap or Bitmap+Palette.
1267 bool src_d
= P
.is
<lua_dbitmap
>();
1268 bool src_p
= P
.is
<lua_bitmap
>();
1269 int sidx
= P
.skip();
1270 if(!src_d
&& !src_p
)
1271 P
.expected("BITMAP or DBITMAP", sidx
);
1274 spal
= P
.skip(); //Reserve for palette.
1279 P(hscl
, P
.optional2(vscl
, hscl
));
1281 int64_t ckx
= 0x100000000ULL
;
1282 porterduff_oper pd_oper
;
1284 pd_oper
= get_pd_oper(P
.arg
<std::string
>());
1286 //Hack: Direct-color bitmaps should take color spec, with special NONE value.
1288 ckx
= P
.arg_opt
<int64_t>(0x10000);
1289 else if(P
.is_novalue())
1292 ckx
= P
.arg
<framebuffer::color
>().asnumber();
1295 operand_dbitmap
dest(*this);
1297 operand_dbitmap
src(*P
.arg
<lua_dbitmap
*>(sidx
));
1299 xblit_pduff
<scaled
>(dest
, src
, dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
, pd_oper
);
1301 xblit_dir
<scaled
>(dest
, src
, ckx
, dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
);
1303 operand_bitmap_pal
src(*P
.arg
<lua_bitmap
*>(sidx
), *P
.arg
<lua_palette
*>(spal
));
1305 xblit_pduff
<scaled
>(dest
, src
, dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
, pd_oper
);
1307 xblit_pal
<scaled
>(dest
, src
, ckx
, dx
, dy
, sx
, sy
, w
, h
, hscl
, vscl
);
1312 int lua_dbitmap::save_png(lua::state
& L
, lua::parameters
& P
)
1314 std::string name
, name2
;
1318 if((was_filename
= P
.is_string())) P(name
);
1319 if(P
.is_string()) P(name2
);
1321 auto buf
= this->save_png();
1323 std::string filename
= zip::resolverel(name
, name2
);
1324 std::ofstream
strm(filename
, std::ios::binary
);
1326 throw std::runtime_error("Can't open output file");
1327 strm
.write(&buf
[0], buf
.size());
1329 throw std::runtime_error("Can't write output file");
1332 std::ostringstream strm
;
1333 strm
.write(&buf
[0], buf
.size());
1334 L
.pushlstring(base64_encode(strm
.str()));
1339 int lua_dbitmap::adjust_transparency(lua::state
& L
, lua::parameters
& P
)
1343 P(P
.skipped(), tadj
);
1345 for(size_t i
= 0; i
< width
* height
; i
++)
1346 pixels
[i
] = tadjust(pixels
[i
], tadj
);
1350 int lua_dbitmap::sample_texture(lua::state
& L
, lua::parameters
& P
)
1353 int32_t xx
, xy
, yx
, yy
, x0
, y0
;
1357 P(P
.skipped(), src_p
, xx
, xy
, x0
, yx
, yy
, y0
, s
, wrap
);
1359 _sample_texture(pixels
, width
, height
, src_p
->pixels
, src_p
->width
, src_p
->height
, xx
, xy
, x0
, yx
, yy
, y0
,
1360 s
, wrap
, framebuffer::color());
1364 template<bool png
> int lua_loaded_bitmap::load(lua::state
& L
, lua::parameters
& P
)
1366 std::string name
, name2
;
1368 P(name
, P
.optional(name2
, ""));
1371 std::string filename
= zip::resolverel(name
, name2
);
1372 return bitmap_load_png_fn(L
, filename
);
1374 return bitmap_load_fn(L
, [&name
, &name2
]() -> lua_loaded_bitmap
{
1375 std::string name3
= zip::resolverel(name
, name2
);
1376 return lua_loaded_bitmap::load(name3
);
1380 template<bool png
> int lua_loaded_bitmap::load_str(lua::state
& L
, lua::parameters
& P
)
1382 std::string contents
;
1387 contents
= base64_decode(contents
);
1388 std::istringstream
strm(contents
);
1389 return bitmap_load_png_fn(L
, strm
);
1391 return bitmap_load_fn(L
, [&contents
]() -> lua_loaded_bitmap
{
1392 std::istringstream
strm(contents
);
1393 return lua_loaded_bitmap::load(strm
);