add drawImage to lua
git-svn-id: http://skia.googlecode.com/svn/trunk@9500 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkData.h b/include/core/SkData.h
index ab7da47..eae84b0 100644
--- a/include/core/SkData.h
+++ b/include/core/SkData.h
@@ -89,7 +89,13 @@
* 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.
diff --git a/samplecode/SampleLua.cpp b/samplecode/SampleLua.cpp
index 104a427..00a3227 100644
--- a/samplecode/SampleLua.cpp
+++ b/samplecode/SampleLua.cpp
@@ -27,6 +27,8 @@
"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 @@
" 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 "
""
diff --git a/src/core/SkData.cpp b/src/core/SkData.cpp
index c1a2136..a1b42b0 100644
--- a/src/core/SkData.cpp
+++ b/src/core/SkData.cpp
@@ -97,6 +97,16 @@
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);
diff --git a/src/utils/SkLua.cpp b/src/utils/SkLua.cpp
index 3c8c9f5..6754746 100644
--- a/src/utils/SkLua.cpp
+++ b/src/utils/SkLua.cpp
@@ -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 @@
DEF_MTNAME(SkCanvas)
DEF_MTNAME(SkDocument)
+DEF_MTNAME(SkImage)
DEF_MTNAME(SkMatrix)
DEF_MTNAME(SkRRect)
DEF_MTNAME(SkPath)
@@ -302,6 +305,25 @@
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 @@
{ "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 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 @@
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 @@
// 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 @@
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);