From e068aab6eaa219187a9ba539e3cdb9dd87e9b3d5 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 12 Oct 2013 00:40:18 -0300 Subject: [PATCH] The function passed to ImageData:mapPixel must return at least r,g,b. Alpha component defaults to 255 (resolves issue #603) --- src/modules/image/wrap_ImageData.cpp | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/modules/image/wrap_ImageData.cpp b/src/modules/image/wrap_ImageData.cpp index d47fbbc77..80b396286 100644 --- a/src/modules/image/wrap_ImageData.cpp +++ b/src/modules/image/wrap_ImageData.cpp @@ -145,9 +145,6 @@ static int w_ImageData_mapPixelUnsafe(lua_State *L) if (!(t->inside(sx, sy) && t->inside(sx+w-1, sy+h-1))) return luaL_error(L, "Invalid rectangle dimensions."); - // Default pixel component values (r, g, b, a.) - const unsigned char pixel_defaults[4] = {0, 0, 0, 255}; - // Cache-friendlier loop. :) for (int y = sy; y < sy+h; y++) { @@ -172,22 +169,17 @@ static int w_ImageData_mapPixelUnsafe(lua_State *L) for (int i = 0; i < 4; i++) { int ttype = lua_type(L, -4 + i); - switch (ttype) - { - case LUA_TNUMBER: + + if (ttype == LUA_TNUMBER) parray[i] = (unsigned char) lua_tonumber(L, -4 + i); - break; - case LUA_TNONE: - case LUA_TNIL: - parray[i] = pixel_defaults[i]; - break; - - default: - // Level 2 because this is function will be wrapped. + else if (i == 3 && (ttype == LUA_TNONE || ttype == LUA_TNIL)) + parray[i] = 255; // Alpha component defaults to 255. + else + // Error (level 2 because this is function will be wrapped.) return luax_retnumbererror(L, 2, i + 1, ttype); - } } + // Pop return values. lua_pop(L, 4); // We're locking the entire function, instead of each setPixel call.