2011-11-14 17:30:16 +00:00
|
|
|
/*
|
|
|
|
KindlePDFViewer: buffer for blitting muPDF data to framebuffer (blitbuffer)
|
|
|
|
Copyright (C) 2011 Hans-Werner Hilse <hilse@web.de>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-12-02 00:31:13 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-11-14 17:30:16 +00:00
|
|
|
#include "blitbuffer.h"
|
|
|
|
|
2012-10-10 14:59:07 +00:00
|
|
|
/* debugging statements, switch as needed */
|
|
|
|
#ifdef DEBUG
|
|
|
|
#define ASSERT_BLITBUFFER_BOUNDARIES(bb,bb_ptr) \
|
|
|
|
if((bb_ptr < bb->data) || (bb_ptr >= (bb->data + bb->pitch * bb->h))) { \
|
|
|
|
fprintf(stderr, "violated blitbuffer constraints in file %s, line %d!\r\n", __FILE__, __LINE__); exit(1); \
|
|
|
|
}
|
|
|
|
#else // DEBUG
|
|
|
|
#define ASSERT_BLITBUFFER_BOUNDARIES(bb,bb_ptr) {}
|
|
|
|
#endif // DEBUG
|
2012-04-26 07:15:34 +00:00
|
|
|
|
2012-10-10 14:59:07 +00:00
|
|
|
inline int setPixel(BlitBuffer *bb, int x, int y, int c) {
|
2012-04-26 07:15:34 +00:00
|
|
|
uint8_t *dstptr = (uint8_t*)(bb->data) + (y * bb->pitch) + (x / 2);
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(bb, dstptr);
|
2012-04-26 07:15:34 +00:00
|
|
|
|
|
|
|
if(x % 2 == 0) {
|
|
|
|
*dstptr &= 0x0F;
|
|
|
|
*dstptr |= c << 4;
|
|
|
|
} else {
|
|
|
|
*dstptr &= 0xF0;
|
|
|
|
*dstptr |= c;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-10 03:34:00 +00:00
|
|
|
/*
|
|
|
|
* if ptich equals zero, we calculate it as (w + 1) / 2
|
|
|
|
*/
|
|
|
|
int newBlitBufferNative(lua_State *L, int w, int h, int pitch, BlitBuffer **newBuffer) {
|
2011-12-02 00:31:13 +00:00
|
|
|
BlitBuffer *bb = (BlitBuffer*) lua_newuserdata(L, sizeof(BlitBuffer));
|
2011-11-14 17:30:16 +00:00
|
|
|
luaL_getmetatable(L, "blitbuffer");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
|
|
|
|
bb->w = w;
|
2013-01-10 03:34:00 +00:00
|
|
|
bb->pitch = ((pitch == 0) ? ((w + 1) / 2) : pitch);
|
2011-11-14 17:30:16 +00:00
|
|
|
bb->h = h;
|
2011-12-02 00:31:13 +00:00
|
|
|
bb->data = malloc(bb->pitch * h);
|
|
|
|
if(bb->data == NULL) {
|
|
|
|
return luaL_error(L, "cannot allocate memory for blitbuffer");
|
|
|
|
}
|
|
|
|
memset(bb->data, 0, bb->pitch * h);
|
|
|
|
bb->allocated = 1;
|
2011-12-02 21:28:48 +00:00
|
|
|
if(newBuffer != NULL) {
|
|
|
|
*newBuffer = bb;
|
|
|
|
}
|
2011-11-14 17:30:16 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-12-02 21:28:48 +00:00
|
|
|
static int newBlitBuffer(lua_State *L) {
|
|
|
|
int w = luaL_checkint(L, 1);
|
|
|
|
int h = luaL_checkint(L, 2);
|
2013-01-10 03:34:00 +00:00
|
|
|
int pitch = luaL_optint(L, 3, 0);
|
|
|
|
return newBlitBufferNative(L, w, h, pitch, NULL);
|
2011-12-02 21:28:48 +00:00
|
|
|
}
|
|
|
|
|
2011-12-01 00:35:53 +00:00
|
|
|
static int getWidth(lua_State *L) {
|
|
|
|
BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 1, "blitbuffer");
|
|
|
|
lua_pushinteger(L, bb->w);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getHeight(lua_State *L) {
|
|
|
|
BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 1, "blitbuffer");
|
|
|
|
lua_pushinteger(L, bb->h);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-11-14 17:30:16 +00:00
|
|
|
static int freeBlitBuffer(lua_State *L) {
|
|
|
|
BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 1, "blitbuffer");
|
|
|
|
|
2012-04-16 20:51:13 +00:00
|
|
|
// should be save if called twice
|
2011-12-02 00:31:13 +00:00
|
|
|
if(bb->allocated && bb->data != NULL) {
|
|
|
|
free(bb->data);
|
|
|
|
bb->data = NULL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int blitFullToBuffer(lua_State *L) {
|
|
|
|
BlitBuffer *dst = (BlitBuffer*) luaL_checkudata(L, 1, "blitbuffer");
|
|
|
|
BlitBuffer *src = (BlitBuffer*) luaL_checkudata(L, 2, "blitbuffer");
|
|
|
|
|
2012-06-13 17:43:57 +00:00
|
|
|
if(src->w != dst->w || src->h != dst->h || src->pitch != dst->pitch) {
|
2013-01-10 03:34:00 +00:00
|
|
|
return luaL_error(L, "dst and src blitbuffer size not match!");
|
2011-12-02 00:31:13 +00:00
|
|
|
}
|
2012-04-15 14:05:44 +00:00
|
|
|
|
2011-12-02 00:31:13 +00:00
|
|
|
memcpy(dst->data, src->data, src->pitch * src->h);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-10 15:58:31 +00:00
|
|
|
/**
|
|
|
|
* check/adapt boundaries for blitting operations
|
|
|
|
*
|
|
|
|
* @return 0 if no blitting is needed, 1 otherwise
|
|
|
|
*/
|
|
|
|
int fitBlitBufferBoundaries(BlitBuffer* src, BlitBuffer* dst, int* xdest, int* ydest, int* xoffs, int* yoffs, int* w, int* h) {
|
2011-12-02 00:31:13 +00:00
|
|
|
// check bounds
|
2012-10-10 15:58:31 +00:00
|
|
|
if(*ydest < 0) {
|
2012-03-13 17:34:04 +00:00
|
|
|
// negative ydest, try to compensate
|
2012-10-10 15:58:31 +00:00
|
|
|
if(*ydest + *h > 0) {
|
2012-03-13 17:34:04 +00:00
|
|
|
// shrink h by negative dest offset
|
2012-10-10 15:58:31 +00:00
|
|
|
*h += *ydest;
|
2012-03-13 17:34:04 +00:00
|
|
|
// extend source offset
|
2012-10-10 15:58:31 +00:00
|
|
|
*yoffs += -(*ydest);
|
|
|
|
*ydest = 0;
|
2012-03-13 17:34:04 +00:00
|
|
|
} else {
|
|
|
|
// effectively no height
|
|
|
|
return 0;
|
|
|
|
}
|
2012-10-10 15:58:31 +00:00
|
|
|
} else if(*ydest >= dst->h) {
|
2012-03-13 17:34:04 +00:00
|
|
|
// we're told to paint to off-bound target coords
|
|
|
|
return 0;
|
|
|
|
}
|
2012-10-10 15:58:31 +00:00
|
|
|
if(*ydest + *h > dst->h) {
|
2012-03-13 17:34:04 +00:00
|
|
|
// clamp height if too large for target size
|
2012-10-10 15:58:31 +00:00
|
|
|
*h = dst->h - *ydest;
|
2012-03-13 17:34:04 +00:00
|
|
|
}
|
2012-10-10 15:58:31 +00:00
|
|
|
if(*yoffs >= src->h) {
|
2012-03-13 17:34:04 +00:00
|
|
|
// recalculated source offset is out of bounds
|
2011-12-02 00:31:13 +00:00
|
|
|
return 0;
|
2012-10-10 15:58:31 +00:00
|
|
|
} else if(*yoffs + *h > src->h) {
|
2012-03-13 17:34:04 +00:00
|
|
|
// clamp height if too large for source size
|
2012-10-10 15:58:31 +00:00
|
|
|
*h = src->h - *yoffs;
|
2011-12-02 00:31:13 +00:00
|
|
|
}
|
2012-03-13 17:34:04 +00:00
|
|
|
// same stuff for x coords:
|
2012-10-10 15:58:31 +00:00
|
|
|
if(*xdest < 0) {
|
|
|
|
if(*xdest + *w > 0) {
|
|
|
|
*w += *xdest;
|
|
|
|
*xoffs += -(*xdest);
|
|
|
|
*xdest = 0;
|
2012-03-13 17:34:04 +00:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-10-10 15:58:31 +00:00
|
|
|
} else if(*xdest >= dst->w) {
|
2011-12-02 00:31:13 +00:00
|
|
|
return 0;
|
2012-03-13 17:34:04 +00:00
|
|
|
}
|
2012-10-10 15:58:31 +00:00
|
|
|
if(*xdest + *w > dst->w) {
|
|
|
|
*w = dst->w - *xdest;
|
2011-12-02 00:31:13 +00:00
|
|
|
}
|
2012-10-10 15:58:31 +00:00
|
|
|
if(*xoffs >= src->w) {
|
2011-12-02 00:31:13 +00:00
|
|
|
return 0;
|
2012-10-10 15:58:31 +00:00
|
|
|
} else if(*xoffs + *w > src->w) {
|
|
|
|
*w = src->w - *xoffs;
|
2011-12-02 00:31:13 +00:00
|
|
|
}
|
2012-10-10 15:58:31 +00:00
|
|
|
return 1; // continue processing
|
|
|
|
}
|
|
|
|
|
|
|
|
static int blitToBuffer(lua_State *L) {
|
|
|
|
BlitBuffer *dst = (BlitBuffer*) luaL_checkudata(L, 1, "blitbuffer");
|
|
|
|
BlitBuffer *src = (BlitBuffer*) luaL_checkudata(L, 2, "blitbuffer");
|
|
|
|
int xdest = luaL_checkint(L, 3);
|
|
|
|
int ydest = luaL_checkint(L, 4);
|
|
|
|
int xoffs = luaL_checkint(L, 5);
|
|
|
|
int yoffs = luaL_checkint(L, 6);
|
|
|
|
int w = luaL_checkint(L, 7);
|
|
|
|
int h = luaL_checkint(L, 8);
|
|
|
|
int x, y;
|
2011-12-02 00:31:13 +00:00
|
|
|
|
|
|
|
uint8_t *dstptr;
|
|
|
|
uint8_t *srcptr;
|
|
|
|
|
2012-10-10 15:58:31 +00:00
|
|
|
if(!fitBlitBufferBoundaries(src, dst, &xdest, &ydest, &xoffs, &yoffs, &w, &h))
|
|
|
|
return 0;
|
|
|
|
|
2011-12-02 00:31:13 +00:00
|
|
|
if(xdest & 1) {
|
|
|
|
/* this will render the leftmost column */
|
2012-04-15 14:05:44 +00:00
|
|
|
dstptr = (uint8_t*)(dst->data +
|
|
|
|
ydest * dst->pitch +
|
2011-12-02 00:31:13 +00:00
|
|
|
xdest / 2);
|
|
|
|
srcptr = (uint8_t*)(src->data +
|
|
|
|
yoffs * src->pitch +
|
|
|
|
xoffs / 2 );
|
|
|
|
if(xoffs & 1) {
|
|
|
|
for(y = 0; y < h; y++) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, dstptr);
|
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(src, srcptr);
|
2011-12-02 00:31:13 +00:00
|
|
|
*dstptr &= 0xF0;
|
|
|
|
*dstptr |= *srcptr & 0x0F;
|
|
|
|
dstptr += dst->pitch;
|
|
|
|
srcptr += src->pitch;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(y = 0; y < h; y++) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, dstptr);
|
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(src, srcptr);
|
2011-12-02 00:31:13 +00:00
|
|
|
*dstptr &= 0xF0;
|
|
|
|
*dstptr |= *srcptr >> 4;
|
|
|
|
dstptr += dst->pitch;
|
|
|
|
srcptr += src->pitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xdest++;
|
|
|
|
xoffs++;
|
|
|
|
w--;
|
|
|
|
}
|
|
|
|
|
2012-04-15 14:05:44 +00:00
|
|
|
dstptr = (uint8_t*)(dst->data +
|
|
|
|
ydest * dst->pitch +
|
2011-12-02 00:31:13 +00:00
|
|
|
xdest / 2);
|
|
|
|
srcptr = (uint8_t*)(src->data +
|
|
|
|
yoffs * src->pitch +
|
|
|
|
xoffs / 2 );
|
|
|
|
|
|
|
|
if(xoffs & 1) {
|
|
|
|
for(y = 0; y < h; y++) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, dstptr);
|
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(src, srcptr);
|
2011-12-02 00:31:13 +00:00
|
|
|
for(x = 0; x < (w / 2); x++) {
|
|
|
|
dstptr[x] = (srcptr[x] << 4) | (srcptr[x+1] >> 4);
|
|
|
|
}
|
|
|
|
if(w & 1) {
|
|
|
|
dstptr[x] &= 0x0F;
|
|
|
|
dstptr[x] |= srcptr[x] << 4;
|
|
|
|
}
|
|
|
|
dstptr += dst->pitch;
|
|
|
|
srcptr += src->pitch;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(y = 0; y < h; y++) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, dstptr);
|
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(src, srcptr);
|
2011-12-02 00:31:13 +00:00
|
|
|
memcpy(dstptr, srcptr, w / 2);
|
|
|
|
if(w & 1) {
|
|
|
|
dstptr[w/2] &= 0x0F;
|
|
|
|
dstptr[w/2] |= (srcptr[w/2] & 0xF0);
|
|
|
|
}
|
|
|
|
dstptr += dst->pitch;
|
|
|
|
srcptr += src->pitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int addblitToBuffer(lua_State *L) {
|
|
|
|
BlitBuffer *dst = (BlitBuffer*) luaL_checkudata(L, 1, "blitbuffer");
|
|
|
|
BlitBuffer *src = (BlitBuffer*) luaL_checkudata(L, 2, "blitbuffer");
|
|
|
|
int xdest = luaL_checkint(L, 3);
|
|
|
|
int ydest = luaL_checkint(L, 4);
|
|
|
|
int xoffs = luaL_checkint(L, 5);
|
|
|
|
int yoffs = luaL_checkint(L, 6);
|
|
|
|
int w = luaL_checkint(L, 7);
|
|
|
|
int h = luaL_checkint(L, 8);
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
uint8_t *dstptr;
|
|
|
|
uint8_t *srcptr;
|
|
|
|
|
2012-10-10 15:58:31 +00:00
|
|
|
if(!fitBlitBufferBoundaries(src, dst, &xdest, &ydest, &xoffs, &yoffs, &w, &h))
|
|
|
|
return 0;
|
|
|
|
|
2011-12-02 00:31:13 +00:00
|
|
|
if(xdest & 1) {
|
|
|
|
/* this will render the leftmost column */
|
2012-04-15 14:05:44 +00:00
|
|
|
dstptr = (uint8_t*)(dst->data +
|
|
|
|
ydest * dst->pitch +
|
2011-12-02 00:31:13 +00:00
|
|
|
xdest / 2);
|
|
|
|
srcptr = (uint8_t*)(src->data +
|
|
|
|
yoffs * src->pitch +
|
|
|
|
xoffs / 2 );
|
|
|
|
if(xoffs & 1) {
|
|
|
|
for(y = 0; y < h; y++) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, dstptr);
|
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(src, srcptr);
|
2011-12-02 00:31:13 +00:00
|
|
|
uint8_t v = (*dstptr & 0x0F) + (*srcptr & 0x0F);
|
|
|
|
*dstptr = (*dstptr & 0xF0) | (v < 0x0F ? v : 0x0F);
|
|
|
|
dstptr += dst->pitch;
|
|
|
|
srcptr += src->pitch;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(y = 0; y < h; y++) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, dstptr);
|
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(src, srcptr);
|
2011-12-02 00:31:13 +00:00
|
|
|
uint8_t v = (*dstptr & 0x0F) + (*srcptr >> 4);
|
|
|
|
*dstptr = (*dstptr & 0xF0) | (v < 0x0F ? v : 0x0F);
|
|
|
|
dstptr += dst->pitch;
|
|
|
|
srcptr += src->pitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xdest++;
|
|
|
|
xoffs++;
|
|
|
|
w--;
|
|
|
|
}
|
|
|
|
|
2012-04-15 14:05:44 +00:00
|
|
|
dstptr = (uint8_t*)(dst->data +
|
|
|
|
ydest * dst->pitch +
|
2011-12-02 00:31:13 +00:00
|
|
|
xdest / 2);
|
|
|
|
srcptr = (uint8_t*)(src->data +
|
|
|
|
yoffs * src->pitch +
|
|
|
|
xoffs / 2 );
|
|
|
|
|
|
|
|
if(xoffs & 1) {
|
|
|
|
for(y = 0; y < h; y++) {
|
|
|
|
for(x = 0; x < (w / 2); x++) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, dstptr);
|
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(src, srcptr);
|
2011-12-02 00:31:13 +00:00
|
|
|
uint16_t v1 = (dstptr[x] & 0xF0) + ((srcptr[x] & 0x0F) << 4);
|
|
|
|
uint8_t v2 = (dstptr[x] & 0x0F) + (srcptr[x+1] >> 4);
|
|
|
|
dstptr[x] = (v1 < 0xF0 ? v1 : 0xF0) | (v2 < 0x0F ? v2 : 0x0F);
|
|
|
|
}
|
|
|
|
if(w & 1) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, dstptr);
|
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(src, srcptr);
|
2011-12-02 00:31:13 +00:00
|
|
|
uint16_t v1 = (dstptr[x] & 0xF0) + ((srcptr[x] & 0x0F) << 4);
|
|
|
|
dstptr[x] = (dstptr[x] & 0x0F) | (v1 < 0xF0 ? v1 : 0xF0);
|
|
|
|
}
|
|
|
|
dstptr += dst->pitch;
|
|
|
|
srcptr += src->pitch;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(y = 0; y < h; y++) {
|
|
|
|
for(x = 0; x < (w / 2); x++) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, dstptr);
|
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(src, srcptr);
|
2011-12-02 00:31:13 +00:00
|
|
|
uint16_t v1 = (dstptr[x] & 0xF0) + (srcptr[x] & 0xF0);
|
|
|
|
uint8_t v2 = (dstptr[x] & 0x0F) + (srcptr[x] & 0x0F);
|
|
|
|
dstptr[x] = (v1 < 0xF0 ? v1 : 0xF0) | (v2 < 0x0F ? v2 : 0x0F);
|
|
|
|
}
|
|
|
|
if(w & 1) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, dstptr);
|
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(src, srcptr);
|
2011-12-02 00:31:13 +00:00
|
|
|
uint16_t v1 = (dstptr[x] & 0xF0) + (srcptr[x] & 0xF0);
|
|
|
|
dstptr[x] = (dstptr[x] & 0x0F) | (v1 < 0xF0 ? v1 : 0xF0);
|
|
|
|
}
|
|
|
|
dstptr += dst->pitch;
|
|
|
|
srcptr += src->pitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int paintRect(lua_State *L) {
|
|
|
|
BlitBuffer *dst = (BlitBuffer*) luaL_checkudata(L, 1, "blitbuffer");
|
|
|
|
int x = luaL_checkint(L, 2);
|
|
|
|
int y = luaL_checkint(L, 3);
|
|
|
|
int w = luaL_checkint(L, 4);
|
|
|
|
int h = luaL_checkint(L, 5);
|
|
|
|
int c = luaL_checkint(L, 6);
|
|
|
|
uint8_t *dstptr;
|
|
|
|
|
|
|
|
int cy;
|
2012-10-10 14:59:07 +00:00
|
|
|
|
|
|
|
if(x < 0) {
|
|
|
|
if (x+w > 0) {
|
|
|
|
w += x;
|
|
|
|
x = 0;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-12-02 00:31:13 +00:00
|
|
|
}
|
2012-10-10 14:59:07 +00:00
|
|
|
|
|
|
|
if(y < 0) {
|
|
|
|
if (y+h > 0) {
|
|
|
|
h += y;
|
|
|
|
y = 0;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-02 00:31:13 +00:00
|
|
|
if(x + w > dst->w) {
|
|
|
|
w = dst->w - x;
|
|
|
|
}
|
|
|
|
if(y + h > dst->h) {
|
|
|
|
h = dst->h - y;
|
|
|
|
}
|
|
|
|
|
2012-10-10 14:59:07 +00:00
|
|
|
if(w <= 0 || h <= 0 || x >= dst->w || y >= dst->h) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-02 00:31:13 +00:00
|
|
|
if(x & 1) {
|
2012-03-06 03:10:20 +00:00
|
|
|
/* This will render the leftmost column
|
|
|
|
* in the case when x is odd. After this,
|
|
|
|
* x will become even. */
|
2012-04-15 14:05:44 +00:00
|
|
|
dstptr = (uint8_t*)(dst->data +
|
|
|
|
y * dst->pitch +
|
2011-12-02 00:31:13 +00:00
|
|
|
x / 2);
|
|
|
|
for(cy = 0; cy < h; cy++) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, dstptr);
|
2011-12-02 00:31:13 +00:00
|
|
|
*dstptr &= 0xF0;
|
|
|
|
*dstptr |= c;
|
|
|
|
dstptr += dst->pitch;
|
|
|
|
}
|
|
|
|
x++;
|
|
|
|
w--;
|
|
|
|
}
|
2012-04-15 14:05:44 +00:00
|
|
|
dstptr = (uint8_t*)(dst->data +
|
|
|
|
y * dst->pitch +
|
2011-12-02 00:31:13 +00:00
|
|
|
x / 2);
|
|
|
|
for(cy = 0; cy < h; cy++) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, dstptr);
|
2011-12-02 00:31:13 +00:00
|
|
|
memset(dstptr, c | (c << 4), w / 2);
|
2011-12-02 00:43:55 +00:00
|
|
|
dstptr += dst->pitch;
|
2011-12-02 00:31:13 +00:00
|
|
|
}
|
|
|
|
if(w & 1) {
|
2012-04-15 14:05:44 +00:00
|
|
|
/* This will render the rightmost column
|
2012-03-06 03:10:20 +00:00
|
|
|
* in the case when (w & 1) && !(x & 1) or
|
|
|
|
* !(w & 1) && (x & 1). */
|
2012-04-15 14:05:44 +00:00
|
|
|
dstptr = (uint8_t*)(dst->data +
|
|
|
|
y * dst->pitch +
|
2012-03-06 03:10:20 +00:00
|
|
|
(x + w) / 2);
|
2011-12-02 00:31:13 +00:00
|
|
|
for(cy = 0; cy < h; cy++) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, dstptr);
|
2011-12-02 00:31:13 +00:00
|
|
|
*dstptr &= 0x0F;
|
|
|
|
*dstptr |= (c << 4);
|
|
|
|
dstptr += dst->pitch;
|
|
|
|
}
|
|
|
|
}
|
2011-11-14 17:30:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-15 09:03:09 +00:00
|
|
|
static int invertRect(lua_State *L) {
|
|
|
|
BlitBuffer *dst = (BlitBuffer*) luaL_checkudata(L, 1, "blitbuffer");
|
|
|
|
int x = luaL_checkint(L, 2);
|
|
|
|
int y = luaL_checkint(L, 3);
|
|
|
|
int w = luaL_checkint(L, 4);
|
|
|
|
int h = luaL_checkint(L, 5);
|
|
|
|
uint8_t *dstptr;
|
|
|
|
|
|
|
|
int cy, cx;
|
2012-09-26 16:54:49 +00:00
|
|
|
|
|
|
|
//printf("## invertRect x=%d y=%d w=%d h=%d\n",x,y,w,h);
|
|
|
|
|
|
|
|
if (x < 0) {
|
|
|
|
if ( x + w > 0 ) {
|
|
|
|
w = w + x;
|
|
|
|
x = 0;
|
|
|
|
} else {
|
|
|
|
//printf("## invertRect x out of bound\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y < 0) {
|
|
|
|
if ( y + h > 0 ) {
|
|
|
|
h = h + y;
|
|
|
|
y = 0;
|
|
|
|
} else {
|
|
|
|
//printf("## invertRect y out of bound\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-15 09:03:09 +00:00
|
|
|
if(w <= 0 || h <= 0 || x >= dst->w || y >= dst->h) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(x + w > dst->w) {
|
|
|
|
w = dst->w - x;
|
|
|
|
}
|
|
|
|
if(y + h > dst->h) {
|
|
|
|
h = dst->h - y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(x & 1) {
|
|
|
|
/* This will invert the leftmost column
|
|
|
|
* in the case when x is odd. After this,
|
|
|
|
* x will become even. */
|
|
|
|
dstptr = (uint8_t*)(dst->data +
|
|
|
|
y * dst->pitch +
|
|
|
|
x / 2);
|
|
|
|
for(cy = 0; cy < h; cy++) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, dstptr);
|
2012-03-15 09:03:09 +00:00
|
|
|
*dstptr ^= 0x0F;
|
|
|
|
dstptr += dst->pitch;
|
|
|
|
}
|
|
|
|
x++;
|
|
|
|
w--;
|
|
|
|
}
|
|
|
|
dstptr = (uint8_t*)(dst->data +
|
|
|
|
y * dst->pitch +
|
|
|
|
x / 2);
|
|
|
|
for(cy = 0; cy < h; cy++) {
|
|
|
|
for(cx = 0; cx < w/2; cx++) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, (dstptr+cx));
|
2012-03-15 09:03:09 +00:00
|
|
|
*(dstptr+cx) ^= 0xFF;
|
|
|
|
}
|
|
|
|
dstptr += dst->pitch;
|
|
|
|
}
|
|
|
|
if(w & 1) {
|
|
|
|
/* This will invert the rightmost column
|
|
|
|
* in the case when (w & 1) && !(x & 1) or
|
|
|
|
* !(w & 1) && (x & 1). */
|
|
|
|
dstptr = (uint8_t*)(dst->data +
|
|
|
|
y * dst->pitch +
|
|
|
|
(x + w) / 2);
|
|
|
|
for(cy = 0; cy < h; cy++) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, dstptr);
|
2012-03-15 09:03:09 +00:00
|
|
|
*dstptr ^= 0xF0;
|
|
|
|
dstptr += dst->pitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-09 13:52:36 +00:00
|
|
|
static int dimRect(lua_State *L) {
|
|
|
|
BlitBuffer *dst = (BlitBuffer*) luaL_checkudata(L, 1, "blitbuffer");
|
|
|
|
int x = luaL_checkint(L, 2);
|
|
|
|
int y = luaL_checkint(L, 3);
|
|
|
|
int w = luaL_checkint(L, 4);
|
|
|
|
int h = luaL_checkint(L, 5);
|
|
|
|
uint8_t *dstptr;
|
|
|
|
|
|
|
|
int cy, cx;
|
2012-10-10 14:59:07 +00:00
|
|
|
|
|
|
|
if (x < 0) {
|
|
|
|
if ( x + w > 0 ) {
|
|
|
|
w = w + x;
|
|
|
|
x = 0;
|
|
|
|
} else {
|
|
|
|
//printf("## invertRect x out of bound\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y < 0) {
|
|
|
|
if ( y + h > 0 ) {
|
|
|
|
h = h + y;
|
|
|
|
y = 0;
|
|
|
|
} else {
|
|
|
|
//printf("## invertRect y out of bound\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-09 13:52:36 +00:00
|
|
|
if(w <= 0 || h <= 0 || x >= dst->w || y >= dst->h) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(x + w > dst->w) {
|
|
|
|
w = dst->w - x;
|
|
|
|
}
|
|
|
|
if(y + h > dst->h) {
|
|
|
|
h = dst->h - y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(x & 1) {
|
2012-04-09 18:25:40 +00:00
|
|
|
/* This will dimm the leftmost column
|
2012-04-09 13:52:36 +00:00
|
|
|
* in the case when x is odd. After this,
|
|
|
|
* x will become even. */
|
|
|
|
dstptr = (uint8_t*)(dst->data +
|
|
|
|
y * dst->pitch +
|
|
|
|
x / 2);
|
|
|
|
for(cy = 0; cy < h; cy++) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, dstptr);
|
2012-04-09 13:52:36 +00:00
|
|
|
int px = *dstptr & 0x0F;
|
2012-04-09 18:25:40 +00:00
|
|
|
*dstptr &= 0xF0 | px >> 1;
|
2012-04-09 13:52:36 +00:00
|
|
|
dstptr += dst->pitch;
|
|
|
|
}
|
|
|
|
x++;
|
|
|
|
w--;
|
|
|
|
}
|
|
|
|
dstptr = (uint8_t*)(dst->data +
|
|
|
|
y * dst->pitch +
|
|
|
|
x / 2);
|
|
|
|
for(cy = 0; cy < h; cy++) {
|
|
|
|
for(cx = 0; cx < w/2; cx++) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, (dstptr+cx));
|
2012-04-09 13:52:36 +00:00
|
|
|
*(dstptr+cx) =
|
2012-04-10 13:05:13 +00:00
|
|
|
( *(dstptr+cx) >> 1 ) & 0xF0 |
|
|
|
|
( *(dstptr+cx) & 0x0F ) >> 1;
|
2012-04-09 13:52:36 +00:00
|
|
|
}
|
|
|
|
dstptr += dst->pitch;
|
|
|
|
}
|
|
|
|
if(w & 1) {
|
2012-04-09 18:25:40 +00:00
|
|
|
/* This will dimm the rightmost column
|
2012-04-09 13:52:36 +00:00
|
|
|
* in the case when (w & 1) && !(x & 1) or
|
|
|
|
* !(w & 1) && (x & 1). */
|
|
|
|
dstptr = (uint8_t*)(dst->data +
|
|
|
|
y * dst->pitch +
|
|
|
|
(x + w) / 2);
|
|
|
|
for(cy = 0; cy < h; cy++) {
|
2012-10-10 14:59:07 +00:00
|
|
|
ASSERT_BLITBUFFER_BOUNDARIES(dst, dstptr);
|
2012-04-09 13:52:36 +00:00
|
|
|
int px = *dstptr & 0xF0;
|
|
|
|
*dstptr &= 0x0F | ( px >> 1 & 0xF0 );
|
|
|
|
dstptr += dst->pitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-26 07:15:34 +00:00
|
|
|
/*
|
|
|
|
* @r: radius
|
|
|
|
* @c: color of the line to draw
|
|
|
|
* @w: width of the line to draw
|
|
|
|
*/
|
|
|
|
static int paintCircle(lua_State *L) {
|
|
|
|
BlitBuffer *dst = (BlitBuffer*) luaL_checkudata(L, 1, "blitbuffer");
|
|
|
|
int center_x = luaL_checkint(L, 2);
|
|
|
|
int center_y = luaL_checkint(L, 3);
|
|
|
|
int r = luaL_checkint(L, 4);
|
|
|
|
int c = luaL_optint(L, 5, 15);
|
|
|
|
int w = luaL_optint(L, 6, r);
|
|
|
|
|
|
|
|
if( (center_x + r > dst->h) || (center_x - r < 0) ||
|
|
|
|
(center_y + r > dst->w) || (center_y - r < 0) ||
|
|
|
|
(r == 0)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(w > r) {
|
|
|
|
w = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int tmp_y;
|
|
|
|
/* for outer circle */
|
|
|
|
int x = 0, y = r;
|
|
|
|
float delta = 5/4 - r;
|
|
|
|
/* for inter circle */
|
|
|
|
int r2 = r - w;
|
|
|
|
int x2 = 0, y2 = r2;
|
|
|
|
float delta2 = 5/4 - r;
|
|
|
|
|
|
|
|
/* draw two axles */
|
|
|
|
for(tmp_y = r; tmp_y > r2; tmp_y--) {
|
2012-10-10 14:59:07 +00:00
|
|
|
setPixel(dst, center_x+0, center_y+tmp_y, c);
|
|
|
|
setPixel(dst, center_x-0, center_y-tmp_y, c);
|
|
|
|
setPixel(dst, center_x+tmp_y, center_y+0, c);
|
|
|
|
setPixel(dst, center_x-tmp_y, center_y-0, c);
|
2012-04-26 07:15:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while(x < y) {
|
|
|
|
/* decrease y if we are out of circle */
|
|
|
|
x++;
|
|
|
|
if (delta > 0) {
|
|
|
|
y--;
|
|
|
|
delta = delta + 2*x - 2*y + 2;
|
|
|
|
} else {
|
|
|
|
delta = delta + 2*x + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* inner circle finished drawing, increase y linearly for filling */
|
|
|
|
if(x2 > y2) {
|
|
|
|
y2++;
|
|
|
|
x2++;
|
|
|
|
} else {
|
|
|
|
x2++;
|
|
|
|
if (delta2 > 0) {
|
|
|
|
y2--;
|
|
|
|
delta2 = delta2 + 2*x2 - 2*y2 + 2;
|
|
|
|
} else {
|
|
|
|
delta2 = delta2 + 2*x2 + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(tmp_y = y; tmp_y > y2; tmp_y--) {
|
2012-10-10 14:59:07 +00:00
|
|
|
setPixel(dst, center_x+x, center_y+tmp_y, c);
|
|
|
|
setPixel(dst, center_x+tmp_y, center_y+x, c);
|
2012-04-26 07:15:34 +00:00
|
|
|
|
2012-10-10 14:59:07 +00:00
|
|
|
setPixel(dst, center_x+tmp_y, center_y-x, c);
|
|
|
|
setPixel(dst, center_x+x, center_y-tmp_y, c);
|
2012-04-26 07:15:34 +00:00
|
|
|
|
2012-10-10 14:59:07 +00:00
|
|
|
setPixel(dst, center_x-x, center_y-tmp_y, c);
|
|
|
|
setPixel(dst, center_x-tmp_y, center_y-x, c);
|
2012-04-26 07:15:34 +00:00
|
|
|
|
2012-10-10 14:59:07 +00:00
|
|
|
setPixel(dst, center_x-tmp_y, center_y+x, c);
|
|
|
|
setPixel(dst, center_x-x, center_y+tmp_y, c);
|
2012-04-26 07:15:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(r == w) {
|
2012-10-10 14:59:07 +00:00
|
|
|
setPixel(dst, center_x, center_y, c);
|
2012-04-26 07:15:34 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int paintRoundedCorner(lua_State *L) {
|
|
|
|
BlitBuffer *dst = (BlitBuffer*) luaL_checkudata(L, 1, "blitbuffer");
|
|
|
|
int off_x = luaL_checkint(L, 2);
|
|
|
|
int off_y = luaL_checkint(L, 3);
|
|
|
|
int w = luaL_checkint(L, 4);
|
|
|
|
int h = luaL_checkint(L, 5);
|
|
|
|
int bw = luaL_checkint(L, 6);
|
|
|
|
int r = luaL_checkint(L, 7);
|
|
|
|
int c = luaL_optint(L, 8, 15);
|
|
|
|
|
|
|
|
if((2*r > h) || (2*r > w) || (r == 0)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(r > h) {
|
|
|
|
r = h;
|
|
|
|
}
|
|
|
|
if(r > w) {
|
|
|
|
r = w;
|
|
|
|
}
|
|
|
|
if(bw > r) {
|
|
|
|
bw = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int tmp_y;
|
|
|
|
/* for outer circle */
|
|
|
|
int x = 0, y = r;
|
|
|
|
float delta = 5/4 - r;
|
|
|
|
/* for inter circle */
|
|
|
|
int r2 = r - bw;
|
|
|
|
int x2 = 0, y2 = r2;
|
|
|
|
float delta2 = 5/4 - r;
|
|
|
|
|
|
|
|
/* draw two axles */
|
|
|
|
/*for(tmp_y = r; tmp_y > r2; tmp_y--) {*/
|
2012-10-10 14:59:07 +00:00
|
|
|
/*setPixel(dst, (w-r)+off_x+0, (h-r)+off_y+tmp_y-1, c);*/
|
|
|
|
/*setPixel(dst, (w-r)+off_x-0, (r)+off_y-tmp_y, c);*/
|
|
|
|
/*setPixel(dst, (w-r)+off_x+tmp_y, (h-r)+off_y+0, c);*/
|
|
|
|
/*setPixel(dst, (r)+off_x-tmp_y, (h-r)+off_y-0-1, c);*/
|
2012-04-26 07:15:34 +00:00
|
|
|
/*}*/
|
|
|
|
|
|
|
|
while(x < y) {
|
|
|
|
/* decrease y if we are out of circle */
|
|
|
|
x++;
|
|
|
|
if (delta > 0) {
|
|
|
|
y--;
|
|
|
|
delta = delta + 2*x - 2*y + 2;
|
|
|
|
} else {
|
|
|
|
delta = delta + 2*x + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* inner circle finished drawing, increase y linearly for filling */
|
|
|
|
if(x2 > y2) {
|
|
|
|
y2++;
|
|
|
|
x2++;
|
|
|
|
} else {
|
|
|
|
x2++;
|
|
|
|
if (delta2 > 0) {
|
|
|
|
y2--;
|
|
|
|
delta2 = delta2 + 2*x2 - 2*y2 + 2;
|
|
|
|
} else {
|
|
|
|
delta2 = delta2 + 2*x2 + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(tmp_y = y; tmp_y > y2; tmp_y--) {
|
2012-10-10 14:59:07 +00:00
|
|
|
setPixel(dst, (w-r)+off_x+x-1, (h-r)+off_y+tmp_y-1, c);
|
|
|
|
setPixel(dst, (w-r)+off_x+tmp_y-1, (h-r)+off_y+x-1, c);
|
2012-04-26 07:15:34 +00:00
|
|
|
|
2012-10-10 14:59:07 +00:00
|
|
|
setPixel(dst, (w-r)+off_x+tmp_y-1, (r)+off_y-x, c);
|
|
|
|
setPixel(dst, (w-r)+off_x+x-1, (r)+off_y-tmp_y, c);
|
2012-04-26 07:15:34 +00:00
|
|
|
|
2012-10-10 14:59:07 +00:00
|
|
|
setPixel(dst, (r)+off_x-x, (r)+off_y-tmp_y, c);
|
|
|
|
setPixel(dst, (r)+off_x-tmp_y, (r)+off_y-x, c);
|
2012-04-26 07:15:34 +00:00
|
|
|
|
2012-10-10 14:59:07 +00:00
|
|
|
setPixel(dst, (r)+off_x-tmp_y, (h-r)+off_y+x-1, c);
|
|
|
|
setPixel(dst, (r)+off_x-x, (h-r)+off_y+tmp_y-1, c);
|
2012-04-26 07:15:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-19 18:11:32 +00:00
|
|
|
static const struct luaL_Reg blitbuffer_func[] = {
|
2011-11-14 17:30:16 +00:00
|
|
|
{"new", newBlitBuffer},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2012-03-19 18:11:32 +00:00
|
|
|
static const struct luaL_Reg blitbuffer_meth[] = {
|
2011-12-01 00:35:53 +00:00
|
|
|
{"getWidth", getWidth},
|
|
|
|
{"getHeight", getHeight},
|
2011-12-02 00:31:13 +00:00
|
|
|
{"blitFrom", blitToBuffer},
|
|
|
|
{"addblitFrom", addblitToBuffer},
|
|
|
|
{"blitFullFrom", blitFullToBuffer},
|
|
|
|
{"paintRect", paintRect},
|
2012-04-26 07:15:34 +00:00
|
|
|
{"paintCircle", paintCircle},
|
|
|
|
{"paintRoundedCorner", paintRoundedCorner},
|
2012-03-15 09:03:09 +00:00
|
|
|
{"invertRect", invertRect},
|
2012-04-09 13:52:36 +00:00
|
|
|
{"dimRect", dimRect},
|
2011-11-14 17:30:16 +00:00
|
|
|
{"free", freeBlitBuffer},
|
2011-12-02 00:31:13 +00:00
|
|
|
{"__gc", freeBlitBuffer},
|
2011-11-14 17:30:16 +00:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
int luaopen_blitbuffer(lua_State *L) {
|
|
|
|
luaL_newmetatable(L, "blitbuffer");
|
|
|
|
lua_pushstring(L, "__index");
|
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
luaL_register(L, NULL, blitbuffer_meth);
|
2011-12-07 23:45:39 +00:00
|
|
|
lua_setglobal(L, "blitbuffer");
|
|
|
|
luaL_register(L, "Blitbuffer", blitbuffer_func);
|
|
|
|
|
2011-11-14 17:30:16 +00:00
|
|
|
return 1;
|
|
|
|
}
|