cocos2d-x lua 3.10 在lua层得到像素rgba数据

C++层实现原文:
http://www.jianshu.com/p/16fa878f74c2

因为lua层 没有
Image:getData()
这个接口
所以只能自己导出
为了避免导出整个数据 可能会引起大容量内存的复制
所以我只做了一个 得到一个像素点数据的接口
接口如下:

//G:\Mycocos310Cpp\touchAlpha\cocos2d\cocos\platform\CCImage.h
Color4B Image::getOneData(int nTouch_x, int nTouch_y, Size imageSize)
{
    int picx = nTouch_x;
        //png在内存中是从左上角开始读取像素的 而cocos是左下角开始 需要转换(记得-1)
    int picy = imageSize.height - nTouch_y - 1;
    CCLOG("x = %d, y = %d", picx, picy);

    Color4B color4B = { 0, 0, 0, 0 };
    unsigned int *pixel = (unsigned int *)_data;
    pixel = pixel + (picy * (int)imageSize.width) * 1 + picx * 1;
    //R通道
    color4B.r = *pixel & 0xff;
    //G通道
    color4B.g = (*pixel >> 8) & 0xff;
    //B通过
    color4B.b = (*pixel >> 16) & 0xff;
    //Alpha通道,我们有用的就是Alpha
    color4B.a = (*pixel >> 24) & 0xff;
    return color4B;
}
//G:\Mycocos310Cpp\touchAlpha\cocos2d\cocos\platform\CCImage.cpp
/**
@by lyf 2017年11月11日(双十一)
@得到一个像素的数据
@param nTouch_x 鼠标点击到图片的坐标 X 从左下角开始计算
@param nTouch_y 鼠标点击到图片的坐标 X 从左下角开始计算
@param imageSize 图片的范围大小 getContentSize()
@return  该像素点颜色值(包括alpha通道)
*/
Color4B getOneData(int nTouch_x, int nTouch_y, Size imageSize);

导出到lua接口

//E:\Cocos\Cocos2d-x\cocos2d-x-3.10\cocos\scripting\lua-bindings\auto\lua_cocos2dx_auto.cpp
tolua_function(tolua_S, "getOneData", lua_cocos2dx_Image_getOneData);
cocos2d-x lua 3.10 在lua层得到像素rgba数据_第1张图片
image.png
//E:\Cocos\Cocos2d-x\cocos2d-x-3.10\cocos\scripting\lua-bindings\auto\lua_cocos2dx_auto.cpp
//新增lua导出获得一个像素点的具体像素值 包括通道
int lua_cocos2dx_Image_getOneData(lua_State* tolua_S)
{
    int argc = 0;
    cocos2d::Image* cobj = nullptr;
    bool ok = true;

#if COCOS2D_DEBUG >= 1
    tolua_Error tolua_err;
#endif

    cobj = (cocos2d::Image*)tolua_tousertype(tolua_S, 1, 0);

#if COCOS2D_DEBUG >= 1
    if (!cobj)
    {
        tolua_error(tolua_S, "invalid 'cobj' in function 'lua_cocos2dx_Image_getOneData'", nullptr);
        return 0;
    }
#endif

    argc = lua_gettop(tolua_S) - 1;
    if (argc == 3)
    {
        int arg0;
        int arg1;
        cocos2d::Size arg2;
        ok &= luaval_to_int32(tolua_S, 2, (int *)&arg0, "cc.Image:getOneData");
        if (!ok)
        {
            tolua_error(tolua_S, "invalid arguments in function 'lua_cocos2dx_Image_getOneData'", nullptr);
            return 0;
        }
        ok &= luaval_to_int32(tolua_S, 3, (int *)&arg1, "cc.Image:getOneData");
        if (!ok)
        {
            tolua_error(tolua_S, "invalid arguments in function 'lua_cocos2dx_Image_getOneData'", nullptr);
            return 0;
        }
        ok &= luaval_to_size(tolua_S, 4, &arg2, "cc.Image:getOneData");
        if (!ok)
        {
            tolua_error(tolua_S, "invalid arguments in function 'lua_cocos2dx_Image_getOneData'", nullptr);
            return 0;
        }
        const cocos2d::Color4B& ret = cobj->getOneData(arg0, arg1, arg2);
        color4b_to_luaval(tolua_S, ret);
        return 1;
    }
    luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "ccui.Text:getTextColor", argc, 0);
    return 0;

#if COCOS2D_DEBUG >= 1
tolua_lerror:
    tolua_error(tolua_S, "#ferror in function 'lua_cocos2dx_Image_getOneData'.", &tolua_err);
#endif

    return 0;
}

写完后编译
拷贝发生改变的exe 和 libcocos2d.dll


cocos2d-x lua 3.10 在lua层得到像素rgba数据_第2张图片
image.png

因为我是在框架项目中改变的 其余项目共享exe 和dll框架
所以框架改变后需要拷贝 如果只是一份项目 则无需此步

lua层调用代码 先来个简单的接口调用部分

--i:\Test310\simulator\win32\src\app\views\MainScene.lua
local MainScene = class("MainScene", cc.load("mvc").ViewBase)

--MainScene.RESOURCE_FILENAME = "MainScene.csb"
function MainScene:onCreate()
    printf("resource node = %s", tostring(self:getResourceNode()))
    
    self.sprite = cc.Sprite:create("red3.png")
    self.sprite:setPosition(50, 50)
    self:addChild(self.sprite, 1)
    local size = self.sprite:getContentSize() -- 返回size

    local pImage = cc.Image:new()
    pImage:initWithImageFile("red3.png")
    local size = self.sprite:getContentSize()
    for j = 0, 2 do
        for i = 0, 2 do
            print(i, j)
            local ovt = pImage:getOneData(i, j, size)
            dump(ovt, "ovt = ")--仅需这里打印像素数据测试
        end
    end
    --pImage 这里结束后就直接回收了 没有addChild
end

return MainScene

打印结果

[LUA-print] resource node = nil
libpng warning: iCCP: known incorrect sRGB profile
[LUA-print] 0   0
[LUA-print] dump from: [string ".\app/views/MainScene.lua"]:20: in function 'onC
reate'
[LUA-print] - "ovt = " = {
[LUA-print] -     "a" = 255
[LUA-print] -     "b" = 0
[LUA-print] -     "g" = 255
[LUA-print] -     "r" = 92
[LUA-print] - }
[LUA-print] 1   0
[LUA-print] dump from: [string ".\app/views/MainScene.lua"]:20: in function 'onC
reate'
[LUA-print] - "ovt = " = {
[LUA-print] -     "a" = 255
[LUA-print] -     "b" = 0
[LUA-print] -     "g" = 255
[LUA-print] -     "r" = 92
[LUA-print] - }
[LUA-print] 2   0
[LUA-print] dump from: [string ".\app/views/MainScene.lua"]:20: in function 'onC
reate'
[LUA-print] - "ovt = " = {
[LUA-print] -     "a" = 255
[LUA-print] -     "b" = 0
[LUA-print] -     "g" = 255
[LUA-print] -     "r" = 92
[LUA-print] - }
[LUA-print] 0   1
[LUA-print] dump from: [string ".\app/views/MainScene.lua"]:20: in function 'onC
reate'
[LUA-print] - "ovt = " = {
[LUA-print] -     "a" = 255
[LUA-print] -     "b" = 0
[LUA-print] -     "g" = 255
[LUA-print] -     "r" = 92
[LUA-print] - }
[LUA-print] 1   1
[LUA-print] dump from: [string ".\app/views/MainScene.lua"]:20: in function 'onC
reate'
[LUA-print] - "ovt = " = {
[LUA-print] -     "a" = 0
[LUA-print] -     "b" = 0
[LUA-print] -     "g" = 0
[LUA-print] -     "r" = 0
[LUA-print] - }
[LUA-print] 2   1
[LUA-print] dump from: [string ".\app/views/MainScene.lua"]:20: in function 'onC
reate'
[LUA-print] - "ovt = " = {
[LUA-print] -     "a" = 255
[LUA-print] -     "b" = 0
[LUA-print] -     "g" = 255
[LUA-print] -     "r" = 92
[LUA-print] - }
[LUA-print] 0   2
[LUA-print] dump from: [string ".\app/views/MainScene.lua"]:20: in function 'onC
reate'
[LUA-print] - "ovt = " = {
[LUA-print] -     "a" = 255
[LUA-print] -     "b" = 0
[LUA-print] -     "g" = 255
[LUA-print] -     "r" = 92
[LUA-print] - }
[LUA-print] 1   2
[LUA-print] dump from: [string ".\app/views/MainScene.lua"]:20: in function 'onC
reate'
[LUA-print] - "ovt = " = {
[LUA-print] -     "a" = 255
[LUA-print] -     "b" = 0
[LUA-print] -     "g" = 255
[LUA-print] -     "r" = 92
[LUA-print] - }
[LUA-print] 2   2
[LUA-print] dump from: [string ".\app/views/MainScene.lua"]:20: in function 'onC
reate'
[LUA-print] - "ovt = " = {
[LUA-print] -     "a" = 255
[LUA-print] -     "b" = 0
[LUA-print] -     "g" = 255
[LUA-print] -     "r" = 92
[LUA-print] - }

上传图片样式


cocos2d-x lua 3.10 在lua层得到像素rgba数据_第3张图片
image.png

可自行下载原图


red3.png

图片就是一张9像素 3*3的绿色图片 中间1像素镂空

你可能感兴趣的:(cocos2d-x lua 3.10 在lua层得到像素rgba数据)