add drawImage to lua

git-svn-id: http://skia.googlecode.com/svn/trunk@9500 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
mike@reedtribe.org 2013-06-11 02:20:28 +00:00
parent 5f9f2b0875
commit 792bbd14c6
4 changed files with 86 additions and 2 deletions

View File

@ -89,7 +89,13 @@ public:
* takes ownership of that allocation, and will handling calling sk_free.
*/
static SkData* NewFromMalloc(const void* data, size_t length);
/**
* Create a new dataref the file with the specified path.
* If the file cannot be opened, this returns NULL.
*/
static SkData* NewFromFileName(const char path[]);
/**
* Create a new dataref from a SkFILE.
* This does not take ownership of the SkFILE, nor close it.

View File

@ -27,6 +27,8 @@ static const char gCode[] = ""
"local paint = Sk.newPaint();"
"paint:setAntiAlias(true);"
""
"local image = Sk.loadImage('/skia/trunk/sailboat.jpg');"
""
"local color = {a = 1, r = 1, g = 0, b = 0};"
""
"function rnd(range) "
@ -76,6 +78,7 @@ static const char gCode[] = ""
" canvas:translate(x, 0);"
" canvas:drawOval(r, paint) "
" x = x + 1;"
" canvas:drawImage(image, x, r.bottom + 50, 0.5);"
" if x > 100 then x = 0 end;"
"end "
""

View File

@ -97,6 +97,16 @@ SkData* SkData::NewFromFILE(SkFILE* f) {
return SkData::NewWithProc(addr, size, sk_mmap_releaseproc, NULL);
}
SkData* SkData::NewFromFileName(const char path[]) {
SkFILE* f = path ? sk_fopen(path, kRead_SkFILE_Flag) : NULL;
if (NULL == f) {
return NULL;
}
SkData* data = NewFromFILE(f);
sk_fclose(f);
return data;
}
SkData* SkData::NewFromFD(int fd) {
size_t size;
void* addr = sk_fdmmap(fd, &size);

View File

@ -7,10 +7,12 @@
#include "SkLua.h"
#include "SkCanvas.h"
#include "SkData.h"
#include "SkDocument.h"
#include "SkImage.h"
#include "SkMatrix.h"
#include "SkPaint.h"
#include "SkPath.h"
#include "SkMatrix.h"
#include "SkRRect.h"
#include "SkString.h"
#include "SkTypeface.h"
@ -30,6 +32,7 @@ template <typename T> const char* get_mtname();
DEF_MTNAME(SkCanvas)
DEF_MTNAME(SkDocument)
DEF_MTNAME(SkImage)
DEF_MTNAME(SkMatrix)
DEF_MTNAME(SkRRect)
DEF_MTNAME(SkPath)
@ -302,6 +305,25 @@ static int lcanvas_drawCircle(lua_State* L) {
return 0;
}
static int lcanvas_drawImage(lua_State* L) {
SkCanvas* canvas = get_ref<SkCanvas>(L, 1);
SkImage* image = get_ref<SkImage>(L, 2);
if (NULL == image) {
return 0;
}
SkScalar x = lua2scalar(L, 3);
SkScalar y = lua2scalar(L, 4);
SkPaint paint;
const SkPaint* paintPtr = NULL;
if (lua_isnumber(L, 5)) {
paint.setAlpha(SkScalarRoundToInt(lua2scalar(L, 5) * 255));
paintPtr = &paint;
}
image->draw(canvas, x, y, paintPtr);
return 0;
}
static int lcanvas_drawPath(lua_State* L) {
get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2),
*get_obj<SkPaint>(L, 3));
@ -358,6 +380,7 @@ static const struct luaL_Reg gSkCanvas_Methods[] = {
{ "drawRect", lcanvas_drawRect },
{ "drawOval", lcanvas_drawOval },
{ "drawCircle", lcanvas_drawCircle },
{ "drawImage", lcanvas_drawImage },
{ "drawPath", lcanvas_drawPath },
{ "drawText", lcanvas_drawText },
{ "getSaveCount", lcanvas_getSaveCount },
@ -731,6 +754,30 @@ static const struct luaL_Reg gSkRRect_Methods[] = {
///////////////////////////////////////////////////////////////////////////////
static int limage_width(lua_State* L) {
lua_pushinteger(L, get_ref<SkImage>(L, 1)->width());
return 1;
}
static int limage_height(lua_State* L) {
lua_pushinteger(L, get_ref<SkImage>(L, 1)->height());
return 1;
}
static int limage_gc(lua_State* L) {
get_ref<SkImage>(L, 1)->unref();
return 0;
}
static const struct luaL_Reg gSkImage_Methods[] = {
{ "width", limage_width },
{ "height", limage_height },
{ "__gc", limage_gc },
{ NULL, NULL }
};
///////////////////////////////////////////////////////////////////////////////
static int ltypeface_gc(lua_State* L) {
get_ref<SkTypeface>(L, 1)->unref();
return 0;
@ -827,6 +874,22 @@ static int lsk_newTypeface(lua_State* L) {
return 1;
}
static int lsk_loadImage(lua_State* L) {
if (lua_gettop(L) > 0 && lua_isstring(L, 1)) {
const char* name = lua_tolstring(L, 1, NULL);
SkAutoDataUnref data(SkData::NewFromFileName(name));
if (data.get()) {
SkImage* image = SkImage::NewEncodedData(data.get());
if (image) {
push_ref(L, image);
image->unref();
return 1;
}
}
}
return 0;
}
static void register_Sk(lua_State* L) {
lua_newtable(L);
lua_pushvalue(L, -1);
@ -834,6 +897,7 @@ static void register_Sk(lua_State* L) {
// the Sk table is still on top
setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF);
setfield_function(L, "loadImage", lsk_loadImage);
setfield_function(L, "newPaint", lsk_newPaint);
setfield_function(L, "newPath", lsk_newPath);
setfield_function(L, "newRRect", lsk_newRRect);
@ -854,6 +918,7 @@ void SkLua::Load(lua_State* L) {
register_Sk(L);
REG_CLASS(L, SkCanvas);
REG_CLASS(L, SkDocument);
REG_CLASS(L, SkImage);
REG_CLASS(L, SkPath);
REG_CLASS(L, SkPaint);
REG_CLASS(L, SkRRect);